Add the assistant: a chat that drives the MCP tools in-process
A provider set under Settings (OpenAI-compatible; key encrypted in flow-data/ai.json) turns on an Assistant panel in the sidebar. Each turn runs the FastMCP tools in-process as the person chatting, streams the rounds over SSE and keeps one transcript per user. The panel floats on any screen and shares the editor's lane: a settings panel keeps the top half, the chat the bottom. The MCP client is now built once, for the endpoint and the chat.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { useSyncExternalStore } from "react"
|
||||
|
||||
/**
|
||||
* Whether the assistant is open, and whether another panel is open beside it.
|
||||
*
|
||||
* Module state rather than context because the chat is mounted once in the
|
||||
* sidebar and toggled from there, while the editor's own panels report in
|
||||
* from a different tree: the two only have to agree on who takes which half
|
||||
* of the lane.
|
||||
*/
|
||||
type State = { open: boolean; panels: number }
|
||||
|
||||
let state: State = { open: false, panels: 0 }
|
||||
const listeners = new Set<() => void>()
|
||||
|
||||
function set(next: Partial<State>) {
|
||||
state = { ...state, ...next }
|
||||
for (const listener of listeners) listener()
|
||||
}
|
||||
|
||||
export const chatStore = {
|
||||
open: () => set({ open: true }),
|
||||
close: () => set({ open: false }),
|
||||
toggle: () => set({ open: !state.open }),
|
||||
/** A floating panel is open; returns how to say it closed again. */
|
||||
registerPanel: () => {
|
||||
set({ panels: state.panels + 1 })
|
||||
return () => set({ panels: state.panels - 1 })
|
||||
},
|
||||
}
|
||||
|
||||
export function useChat(): State {
|
||||
return useSyncExternalStore(
|
||||
(listener) => {
|
||||
listeners.add(listener)
|
||||
return () => listeners.delete(listener)
|
||||
},
|
||||
() => state,
|
||||
() => state,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user