import { create } from "zustand"; import { prefs } from "../lib/prefs"; import { SYSTEM_PROMPT, TOOL_SPECS, TOOLS, type ToolKind } from "../lib/copilotTools"; import { chatStream, DEFAULT_MODEL, LLMError, MODEL_PRESETS, validateModel, type ChatMessage, type ModelCheck, type ToolCall } from "../lib/llm"; import { useAuthStore } from "./useAuthStore"; import { useCanvasStore } from "./useCanvasStore"; import type { CanvasEdge, CanvasNode } from "../lib/types"; const KEY = "gr-canvas-copilot"; const THREADS_KEY = "gr-canvas-copilot-threads"; const MAX_THREADS = 500; export type Approval = "ask" | "auto" | "yolo"; export const APPROVAL_LABEL: Record = { ask: { label: "Ask first", hint: "You approve every change and every run" }, auto: { label: "Edit freely", hint: "Edits apply right away; asks before spending quota on a run" }, yolo: { label: "Never ask", hint: "Edits and runs happen without asking" }, }; export interface Proposal { callId: string; name: string; kind: ToolKind; args: Record; summary: string; decision?: "approved" | "rejected"; status?: "running" | "done" | "error"; detail?: string; /** Compact result for the expandable view. */ result?: string; } /** Graph state captured before a turn, so the turn's edits can be reverted. Outputs are dropped to keep storage small. */ export interface Checkpoint { nodes: CanvasNode[]; edges: CanvasEdge[]; } export type ChatItem = | { id: string; kind: "user"; text: string; cp?: Checkpoint; changed?: boolean } | { id: string; kind: "assistant"; text: string; marks?: number[]; streaming?: boolean } | { id: string; kind: "tools"; items: Proposal[]; awaiting: boolean } | { id: string; kind: "error"; text: string }; /** One timeline. Editing or retrying a message forks a new branch that copies the prefix, so ids stay comparable. */ export interface Branch { id: string; createdAt: number; items: ChatItem[]; transcript: ChatMessage[]; } export interface Thread { id: string; title: string; createdAt: number; updatedAt: number; branches: Branch[]; active: string; /** Workflow open when the chat started. */ workflowId?: string; /** Listed from the bucket index, not downloaded yet (branches is empty). */ stub?: boolean; /** Counts for stubs, from the index. */ msgCount?: number; branchCount?: number; } /** Where threads are stored (the user's bucket); registered by the sync store. */ export interface ChatBackend { load: (id: string) => Promise; remove: (id: string) => void; } let backend: ChatBackend | null = null; export const setChatBackend = (b: ChatBackend | null) => { backend = b; }; export const threadMessages = (t: Thread) => (t.stub ? (t.msgCount ?? 0) : activeBranch(t).items.filter((i) => i.kind === "user").length); export const threadBranches = (t: Thread) => (t.stub ? (t.branchCount ?? 1) : t.branches.length); export const activeBranch = (t: Thread): Branch => t.branches.find((b) => b.id === t.active) ?? t.branches[0]; /** Alternative versions of the user message at `index` on the active branch: one representative branch per distinct message. */ export function versionsAt(t: Thread, index: number): Branch[] { const cur = activeBranch(t); const prefix = cur.items.slice(0, index).map((i) => i.id); const seen = new Map(); for (const b of t.branches) { const it = b.items[index]; if (!it || it.kind !== "user") continue; if (prefix.some((id, k) => b.items[k]?.id !== id)) continue; const prev = seen.get(it.id); // Prefer the branch the user is on, else the newest fork of this version. if (!prev || b.id === cur.id || (prev.id !== cur.id && b.createdAt > prev.createdAt)) seen.set(it.id, b); } return [...seen.values()].sort((a, b) => a.createdAt - b.createdAt); } export interface SavedModel { id: string; label: string; custom?: boolean; } export type Layout = "dock" | "sidebar"; interface CopilotState { open: boolean; layout: Layout; view: "chat" | "history"; model: string; models: SavedModel[]; checks: Record; approval: Approval; threads: Thread[]; activeId: string | null; busy: boolean; draft: string; setOpen: (v: boolean) => void; toggle: () => void; setLayout: (l: Layout) => void; setView: (v: "chat" | "history") => void; setDraft: (v: string) => void; setModel: (id: string) => void; addModel: (id: string) => Promise; removeModel: (id: string) => void; checkModel: (id: string) => Promise; setApproval: (a: Approval) => void; newThread: () => void; openThread: (id: string) => Promise; /** Id of a chat being downloaded from the bucket. */ loadingThread: string | null; deleteThread: (id: string) => void; send: (text: string) => Promise; /** Fork at a user message, restore its checkpoint, and resend with new text (retry when text is unchanged). */ editMessage: (itemId: string, text: string) => Promise; retry: (itemId: string) => Promise; restore: (itemId: string) => void; switchBranch: (branchId: string) => void; decide: (callId: string, decision: "approved" | "rejected") => void; decideAll: (decision: "approved" | "rejected") => void; stop: () => void; } function loadPrefs() { try { const raw = prefs.get(KEY); if (raw) { const p = JSON.parse(raw); const models: SavedModel[] = Array.isArray(p.models) && p.models.length ? p.models : MODEL_PRESETS.map((m) => ({ id: m.id, label: m.label })); return { model: typeof p.model === "string" ? p.model : DEFAULT_MODEL, models, approval: (["ask", "auto", "yolo"].includes(p.approval) ? p.approval : "ask") as Approval, layout: (p.layout === "sidebar" ? "sidebar" : "dock") as Layout, }; } } catch { /* ignore */ } return { model: DEFAULT_MODEL, models: MODEL_PRESETS.map((m) => ({ id: m.id, label: m.label })), approval: "ask" as Approval, layout: "dock" as Layout }; } /** Cleans a stored thread: older single-timeline shape, no streaming / awaiting leftovers. */ export function normaliseThread(x: Thread & { items?: ChatItem[]; transcript?: ChatMessage[] }): Thread { const branches: Branch[] = x.branches?.length ? x.branches : [{ id: "b0", createdAt: x.createdAt, items: x.items ?? [], transcript: x.transcript ?? [] }]; return { id: x.id, title: x.title, createdAt: x.createdAt, updatedAt: x.updatedAt, workflowId: x.workflowId, active: x.active && branches.some((b) => b.id === x.active) ? x.active : branches[0].id, branches: branches.map((b) => ({ ...b, items: b.items.map((i) => (i.kind === "assistant" ? { ...i, marks: undefined, streaming: false } : i.kind === "tools" ? { ...i, awaiting: false } : i)) })), }; } /** Threads that older versions kept inside the preferences blob (migrated to per-chat files). */ export function legacyThreads(): Thread[] { try { const t = JSON.parse(prefs.get(THREADS_KEY) ?? "null"); if (Array.isArray(t)) return t.map(normaliseThread); } catch { /* ignore */ } return []; } export const LEGACY_THREADS_KEY = THREADS_KEY; let uid = Date.now() % 100000; const nid = () => `c${(++uid).toString(36)}`; let abort: AbortController | null = null; let waiter: (() => void) | null = null; const parseArgs = (s: string): Record => { try { const v = JSON.parse(s || "{}"); return v && typeof v === "object" ? v : {}; } catch { return {}; } }; function needsApproval(kind: ToolKind, approval: Approval) { if (kind === "read") return false; if (approval === "yolo") return false; if (approval === "auto") return kind === "run"; return true; } export const useCopilotStore = create((set, get) => { const patchThreadRaw = (id: string, fn: (t: Thread) => Partial) => set((s) => ({ threads: s.threads.map((t) => (t.id === id ? { ...t, ...fn(t), updatedAt: Date.now() } : t)) })); /** Patch one branch of a thread. */ const patchBranch = (tid: string, bid: string, fn: (b: Branch) => Partial) => patchThreadRaw(tid, (t) => ({ branches: t.branches.map((b) => (b.id === bid ? { ...b, ...fn(b) } : b)) })); const patchActive = (tid: string, fn: (b: Branch) => Partial) => { const t = get().threads.find((x) => x.id === tid); if (t) patchBranch(tid, activeBranch(t).id, fn); }; const branch = (tid: string, bid: string) => get().threads.find((t) => t.id === tid)?.branches.find((b) => b.id === bid); return { open: false, view: "chat", ...loadPrefs(), checks: {}, threads: [], loadingThread: null, activeId: null, busy: false, draft: "", setOpen: (v) => set({ open: v }), toggle: () => set((s) => ({ open: !s.open })), setLayout: (layout) => set({ layout }), setView: (view) => set({ view }), setDraft: (v) => set({ draft: v }), setModel: (id) => { set({ model: id }); void get().checkModel(id); }, checkModel: async (id) => { const c = get().checks[id]; if (c && !("pending" in c)) return; set((s) => ({ checks: { ...s.checks, [id]: { pending: true } } })); const r = await validateModel(id, useAuthStore.getState().token()).catch((e) => ({ ok: false, providers: [], error: e instanceof Error ? e.message : String(e) })); set((s) => ({ checks: { ...s.checks, [id]: r } })); }, addModel: async (raw) => { const id = raw.trim().replace(/^https?:\/\/huggingface\.co\//, ""); const r = await validateModel(id, useAuthStore.getState().token()); set((s) => ({ checks: { ...s.checks, [id]: r } })); if (r.ok) set((s) => ({ models: s.models.some((m) => m.id === id) ? s.models : [...s.models, { id, label: id.split("/")[1], custom: true }], model: id })); return r; }, removeModel: (id) => set((s) => { const models = s.models.filter((m) => m.id !== id); return { models, model: s.model === id ? (models[0]?.id ?? DEFAULT_MODEL) : s.model }; }), setApproval: (a) => set({ approval: a }), newThread: () => set({ activeId: null, view: "chat", draft: "" }), openThread: async (id) => { const t = get().threads.find((x) => x.id === id); if (!t) return; if (t.stub) { if (!backend || get().loadingThread) return; set({ loadingThread: id }); try { const full = await backend.load(id); if (!full) throw new Error("it is missing from your storage"); set((s) => ({ threads: s.threads.map((x) => (x.id === id ? normaliseThread(full) : x)) })); } catch (e) { useCanvasStore.getState().setNotice(`Couldn't open that chat: ${e instanceof Error ? e.message : String(e)}`); return; } finally { set({ loadingThread: null }); } } set({ activeId: id, view: "chat" }); }, deleteThread: (id) => { if (get().activeId === id) get().stop(); backend?.remove(id); set((s) => ({ threads: s.threads.filter((t) => t.id !== id), activeId: s.activeId === id ? null : s.activeId })); }, restore: (itemId) => { const t = get().threads.find((x) => x.id === get().activeId); const it = t && activeBranch(t).items.find((i) => i.id === itemId); if (!it || it.kind !== "user" || !it.cp) return; applyCheckpoint(it.cp); useCanvasStore.getState().setNotice("Canvas restored to checkpoint"); }, switchBranch: (branchId) => { const tid = get().activeId; if (!tid || get().busy) return; patchThreadRaw(tid, () => ({ active: branchId })); }, editMessage: async (itemId, text) => { const tid = get().activeId; const t = get().threads.find((x) => x.id === tid); if (!tid || !t || get().busy) return; const cur = activeBranch(t); const index = cur.items.findIndex((i) => i.id === itemId); const it = cur.items[index]; if (!it || it.kind !== "user") return; const userNo = cur.items.slice(0, index).filter((i) => i.kind === "user").length; let seen = 0; let cut = cur.transcript.length; for (let k = 0; k < cur.transcript.length; k++) { if (cur.transcript[k].role === "user" && seen++ === userNo) { cut = k; break; } } const fork: Branch = { id: nid(), createdAt: Date.now(), items: cur.items.slice(0, index), transcript: cur.transcript.slice(0, cut) }; patchThreadRaw(tid, (x) => ({ branches: [...x.branches, fork], active: fork.id })); if (it.cp) applyCheckpoint(it.cp); await get().send(text.trim() || it.text); }, retry: async (itemId) => { const t = get().threads.find((x) => x.id === get().activeId); if (!t) return; const items = activeBranch(t).items; let index = items.findIndex((i) => i.id === itemId); // Retrying a reply means retrying the user message that produced it. while (index >= 0 && items[index].kind !== "user") index--; if (index < 0) return; await get().editMessage(items[index].id, (items[index] as { text: string }).text); }, send: async (text) => { const token = useAuthStore.getState().token(); if (get().busy) return; let tid = get().activeId; if (!tid || !get().threads.some((t) => t.id === tid)) { tid = nid(); const b0: Branch = { id: nid(), createdAt: Date.now(), items: [], transcript: [] }; const thread: Thread = { id: tid, title: text.trim().slice(0, 80), createdAt: Date.now(), updatedAt: Date.now(), branches: [b0], active: b0.id, workflowId: useCanvasStore.getState().docId }; set((s) => ({ threads: [thread, ...s.threads].slice(0, MAX_THREADS), activeId: tid, view: "chat" })); } const bid = activeBranch(get().threads.find((t) => t.id === tid)!).id; const pushItem = (item: ChatItem) => patchBranch(tid!, bid, (b) => ({ items: [...b.items, item] })); const patchItem = (iid: string, fn: (i: ChatItem) => ChatItem) => patchBranch(tid!, bid, (b) => ({ items: b.items.map((i) => (i.id === iid ? fn(i) : i)) })); const pushMsg = (m: ChatMessage) => patchBranch(tid!, bid, (b) => ({ transcript: [...b.transcript, m] })); const userId = nid(); const cp = takeCheckpoint(); pushItem({ id: userId, kind: "user", text, cp }); if (!token) { pushItem({ id: nid(), kind: "error", text: "Sign in with Hugging Face to use the copilot." }); return; } abort = new AbortController(); const signal = abort.signal; set({ busy: true, draft: "" }); pushMsg({ role: "user", content: text }); const transcript = () => branch(tid!, bid)?.transcript ?? []; try { for (let round = 0; round < 16; round++) { if (signal.aborted) break; let msgId: string | null = null; let len = 0; const reply = await chatStream({ model: get().model, messages: [{ role: "system", content: SYSTEM_PROMPT }, ...transcript()], tools: TOOL_SPECS, token, signal }, (delta) => { if (!msgId) { msgId = nid(); pushItem({ id: msgId, kind: "assistant", text: "", marks: [], streaming: true }); } const at = len; len += delta.length; patchItem(msgId, (i) => (i.kind === "assistant" ? { ...i, text: i.text + delta, marks: [...(i.marks ?? []), at].slice(-64) } : i)); }); if (msgId) { const finalText = (reply.content ?? "").trim(); patchItem(msgId, (i) => (i.kind === "assistant" ? { ...i, text: finalText, streaming: false, marks: undefined } : i)); if (!finalText) patchBranch(tid, bid, (b) => ({ items: b.items.filter((i) => i.id !== msgId) })); } pushMsg(reply); const calls = reply.tool_calls ?? []; if (!calls.length) break; const approval = get().approval; const proposals: Proposal[] = calls.map((c: ToolCall) => { const tool = TOOLS[c.function.name]; const args = parseArgs(c.function.arguments); return { callId: c.id, name: c.function.name, kind: tool?.kind ?? "read", args, summary: tool ? tool.summarise(args) : `Unknown tool ${c.function.name}`, decision: !tool || !needsApproval(tool.kind, approval) ? "approved" : undefined, }; }); const groupId = nid(); const awaiting = proposals.some((p) => !p.decision); pushItem({ id: groupId, kind: "tools", items: proposals, awaiting }); if (awaiting) { await new Promise((resolve) => { waiter = resolve; signal.addEventListener("abort", () => resolve(), { once: true }); }); waiter = null; if (signal.aborted) break; } const current = branch(tid, bid)?.items.find((i) => i.id === groupId); const decided = current?.kind === "tools" ? current.items : proposals; const patch = (callId: string, p: Partial) => patchItem(groupId, (i) => (i.kind === "tools" ? { ...i, awaiting: false, items: i.items.map((x) => (x.callId === callId ? { ...x, ...p } : x)) } : i)); for (const p of decided) { if (signal.aborted) break; const tool = TOOLS[p.name]; let result: unknown; if (p.decision !== "approved") { result = { rejected: true, note: "The user declined this action. Do not retry it; ask what they want instead if needed." }; patch(p.callId, { status: "done", decision: "rejected" }); } else if (!tool) { result = { error: `Unknown tool ${p.name}` }; patch(p.callId, { status: "error", detail: "Unknown tool" }); } else { patch(p.callId, { status: "running" }); try { result = await tool.run(p.args, { token, signal }); // Only a real error value counts: get_node returns `error: undefined` for a healthy node. const rawErr = result && typeof result === "object" && !Array.isArray(result) ? (result as { error?: unknown }).error : undefined; const err = rawErr == null || rawErr === "" ? undefined : typeof rawErr === "string" ? rawErr : JSON.stringify(rawErr); patch(p.callId, { ...(err ? { status: "error", detail: err } : { status: "done", detail: briefResult(p.name, result) }), result: compact(result) }); } catch (e) { const msg = (e instanceof Error ? e.message : typeof e === "string" ? e : "") || `${p.name} failed without an error message`; result = { error: msg }; patch(p.callId, { status: "error", detail: msg, result: msg }); } } pushMsg({ role: "tool", tool_call_id: p.callId, name: p.name, content: (JSON.stringify(result ?? null) ?? "null").slice(0, 12000) }); } } } catch (e) { if (!signal.aborted) { const msg = e instanceof LLMError && e.status === 401 ? "Your Hugging Face session was rejected. Sign in again." : e instanceof Error ? e.message : String(e); pushItem({ id: nid(), kind: "error", text: msg }); } } finally { // Leave the transcript well-formed: no assistant tool_calls without their tool results. const changed = !sameGraph(cp, takeCheckpoint()); patchBranch(tid, bid, (b) => { const tr = [...b.transcript]; const last = tr[tr.length - 1]; if (last?.role === "assistant" && last.tool_calls?.length) { const answered = new Set(tr.filter((m) => m.role === "tool").map((m) => m.tool_call_id)); if (!last.tool_calls.every((c) => answered.has(c.id))) tr.pop(); } return { transcript: tr, items: b.items.map((i) => i.id === userId && i.kind === "user" ? { ...i, changed } : i.kind === "assistant" && i.streaming ? { ...i, streaming: false, marks: undefined } : i.kind === "tools" && i.awaiting ? { ...i, awaiting: false } : i, ), }; }); abort = null; set({ busy: false }); } }, decide: (callId, decision) => { const tid = get().activeId; if (!tid) return; patchActive(tid, (b) => ({ items: b.items.map((i) => (i.kind === "tools" && i.awaiting ? { ...i, items: i.items.map((p) => (p.callId === callId ? { ...p, decision } : p)) } : i)) })); maybeRelease(get); }, decideAll: (decision) => { const tid = get().activeId; if (!tid) return; patchActive(tid, (b) => ({ items: b.items.map((i) => (i.kind === "tools" && i.awaiting ? { ...i, items: i.items.map((p) => (p.decision ? p : { ...p, decision })) } : i)) })); maybeRelease(get); }, stop: () => { abort?.abort(); const tid = get().activeId; if (tid) patchActive(tid, (b) => ({ items: b.items.map((i) => (i.kind === "tools" && i.awaiting ? { ...i, awaiting: false, items: i.items.map((p) => (p.decision ? p : { ...p, decision: "rejected" as const, status: "done" as const })) } : i)) })); }, }; }); function maybeRelease(get: () => CopilotState) { const t = get().threads.find((x) => x.id === get().activeId); const open = t && activeBranch(t).items.find((i) => i.kind === "tools" && i.awaiting); if (open && open.kind === "tools" && open.items.every((p) => p.decision)) waiter?.(); } function takeCheckpoint(): Checkpoint { const { nodes, edges } = useCanvasStore.getState(); return { nodes: nodes.map((n) => { const { outputs: _o, statusText: _s, error: _e, ...data } = n.data; return { ...n, selected: false, dragging: false, measured: undefined, data: { ...data, status: "idle", progress: 0 } } as CanvasNode; }), edges: edges.map((e) => ({ ...e, selected: false, data: e.data ? { ...e.data, flowing: false } : e.data })), }; } function sameGraph(a: Checkpoint, b: Checkpoint) { return JSON.stringify(a) === JSON.stringify(b); } function applyCheckpoint(cp: Checkpoint) { const s = useCanvasStore.getState(); if (s.run.running) s.cancelRun(); useCanvasStore.setState({ nodes: cp.nodes.map((n) => ({ ...n, data: { ...n.data } })), edges: cp.edges.map((e) => ({ ...e })), dirty: true }); } function compact(r: unknown): string { try { const s = JSON.stringify(r, null, 1); return s.length > 1200 ? `${s.slice(0, 1200)}…` : s; } catch { return String(r); } } function briefResult(name: string, r: unknown): string | undefined { if (!r || typeof r !== "object") return undefined; if (Array.isArray(r)) return `${r.length} result${r.length === 1 ? "" : "s"}`; const o = r as Record; if (name === "add_node" && typeof o.id === "string") return `→ ${o.id}`; if (name === "add_space" && Array.isArray(o.endpoints)) return `${o.endpoints.length} endpoint${o.endpoints.length === 1 ? "" : "s"}`; if (name === "get_workflow" && Array.isArray(o.nodes)) return `${o.nodes.length} nodes`; if ((name === "move_nodes" || name === "auto_layout") && typeof o.moved === "number") return `${o.moved} moved`; return undefined; } /** Re-reads settings (after state.json was loaded). Chats are loaded separately from the bucket. */ export function rehydrateCopilot() { useCopilotStore.setState(loadPrefs()); } /** Replaces the chat list (after sign-in / sign-out). */ export function setThreads(threads: Thread[]) { abort?.abort(); useCopilotStore.setState({ threads, activeId: null, busy: false, loadingThread: null, view: "chat" }); } useCopilotStore.subscribe((s, p) => { if (s.model === p.model && s.models === p.models && s.approval === p.approval && s.layout === p.layout) return; prefs.set(KEY, JSON.stringify({ model: s.model, models: s.models, approval: s.approval, layout: s.layout })); }); export const useActiveThread = () => useCopilotStore((s) => s.threads.find((t) => t.id === s.activeId) ?? null); export const useActiveItems = () => useCopilotStore((s) => { const t = s.threads.find((x) => x.id === s.activeId); return t ? activeBranch(t).items : EMPTY; }); const EMPTY: ChatItem[] = []; /** Opens the copilot and asks it to explain why a node failed. */ export const askCopilot = (title: string) => { const s = useCopilotStore.getState(); s.setOpen(true); void s.send(`Why did "${title}" fail? Read its logs and run status, explain the cause in plain words, and fix it if you can.`); };