// P1-8 debug page (?debug=1): project picker, conversation list, composer, raw timeline, permission buttons. // For integration only; the real UI is rewritten in P2. import { useEffect, useRef, useState } from "react"; import { useConnection } from "./stores/connection"; import { ws } from "./main"; import { SelectMenu } from "./components/SelectMenu"; type Project = { id: string; name: string; path: string }; type ThreadMeta = { id: string; projectId: string; title: string; provider: string; status: string; archived: boolean; }; type Ev = Record & { seq?: number; type?: string }; type Pending = { requestId: string; kind: string; options?: { id: string; label: string }[] }; const call = (m: string, p?: unknown) => ws.call(m, p); export function DebugApp() { const { state, version } = useConnection(); const [projects, setProjects] = useState([]); const [threads, setThreads] = useState([]); const [projectId, setProjectId] = useState(""); const [threadId, setThreadId] = useState(""); const [events, setEvents] = useState([]); const [pending, setPending] = useState([]); const [queue, setQueue] = useState([]); const [input, setInput] = useState(""); const [err, setErr] = useState(""); const subRef = useRef<(() => void) | null>(null); useEffect(() => { if (state !== "open") return; void call("projects.list").then((r) => setProjects((r as Project[]) ?? [])); void call("threads.list").then((r) => setThreads((r as ThreadMeta[]) ?? [])); }, [state]); useEffect(() => { subRef.current?.(); subRef.current = null; setEvents([]); setPending([]); setQueue([]); if (!threadId || state !== "open") return; void call("threads.get", { id: threadId }).then((r) => { const res = r as { events?: Ev[]; pending?: Pending[]; queue?: unknown[] }; setEvents(res.events ?? []); setPending(res.pending ?? []); setQueue(res.queue ?? []); }); subRef.current = ws.subscribe(`thread:${threadId}`, (_ch, data) => { const d = data as { type?: string; event?: Ev; queue?: unknown[] }; if (d.type === "event" && d.event) { setEvents((evs) => [...evs, d.event!]); if (d.event.type === "permission_resolved" || d.event.type === "question_answered") { const rid = (d.event as { requestId?: string }).requestId; setPending((ps) => ps.filter((p) => p.requestId !== rid)); } } if (d.type === "queue" && d.queue) setQueue(d.queue); }); return () => subRef.current?.(); }, [threadId, state]); // Pending changes are also filled in from timeline events (when a permission_request event arrives) useEffect(() => { const reqs = events.filter((e) => e.type === "permission_request" || e.type === "question"); const resolved = new Set( events .filter((e) => e.type === "permission_resolved" || e.type === "question_answered") .map((e) => (e as { requestId?: string }).requestId), ); const next = reqs .filter((e) => !resolved.has((e as { requestId?: string }).requestId)) .map((e) => ({ requestId: (e as { requestId?: string }).requestId ?? "", kind: e.type === "question" ? "question" : "permission", options: (e as { options?: { id: string; label: string }[] }).options, })); setPending((cur) => { const merged = new Map(cur.map((p) => [p.requestId, p])); for (const p of next) merged.set(p.requestId, p); return [...merged.values()]; }); }, [events]); const refresh = async () => { if (!threadId) return; const r = (await call("threads.get", { id: threadId }).catch(() => null)) as { events?: Ev[]; pending?: Pending[]; queue?: unknown[]; } | null; if (r) { setEvents(r.events ?? []); setPending((cur) => { const merged = new Map(cur.map((p) => [p.requestId, p])); for (const p of r.pending ?? []) merged.set(p.requestId, p); return [...merged.values()]; }); setQueue(r.queue ?? []); } }; const send = async (behavior?: "queue" | "steer") => { setErr(""); if (!threadId || !input.trim()) return; const text = input; setInput(""); try { await call("thread.send", { id: threadId, text, behavior }); } catch (e) { setErr(String((e as Error)?.message ?? e)); } // sub.add may not have completed yet → fetch the current events once to catch up await refresh(); }; const respond = async (requestId: string, optionId?: string, answers?: Record) => { await call("thread.respond", { id: threadId, requestId, optionId, answers }).catch((e) => setErr(String((e as Error)?.message ?? e))); setTimeout(() => void refresh(), 1500); // Let the approved tool_call ok land in the list }; const createThread = async () => { if (!projectId) return; const meta = (await call("threads.create", { projectId, provider: "mock" }).catch((e) => { setErr(String((e as Error)?.message ?? e)); return null; })) as ThreadMeta | null; if (meta) { setThreads((ts) => [meta, ...ts]); setThreadId(meta.id); } }; return (
{state === "open" ? `已连接 · ${version ?? ""}` : "连接中…"}
({ value: p.id, label: p.name }))]} />
{threads.filter((t) => !projectId || t.projectId === projectId).map((t) => (
setThreadId(t.id)} style={{ padding: "6px 8px", cursor: "pointer", borderRadius: 4, background: t.id === threadId ? "var(--bg-active, #333)" : "transparent", }} > {t.title} ({t.status})
))}
{events.map((e, i) => (
              {(e.seq ?? "") + " " + (e.type ?? "?") + " " + JSON.stringify(e).slice(0, 400)}
            
))} {pending.map((p) => (
授权/提问 {p.requestId}
{p.options?.map((o) => ( ))} {p.kind === "question" && ( )}
))}
{queue.length > 0 &&
排队中:{queue.length} 条
} {err &&
{err}
}
setInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") void send(); }} placeholder="/perm /plan /slow /exit …" style={{ flex: 1 }} />
); }