fix(web): send workspace-relative notebook paths from the chat panel (#8)

The app passes notebook paths relative to notebooks/, but the chat
protocol uses workspace paths (notebooks/...). Every message sent while a
notebook was open was rejected as invalid, and "Open notebook" links on
Claude's edits loaded a non-existent path. ChatPanel now converts at its
boundary in both directions.
This commit is contained in:
Florian Schmidt
2026-11-16 10:23:05 +01:00
parent 63458dd3e4
commit 488f992bd3
2 changed files with 6 additions and 5 deletions
+3 -2
View File
@@ -11,6 +11,7 @@ export interface ChatSocket {
const connectChat = (): ChatSocket => new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/api/chat`);
type Message = { role: "user" | "assistant" | "tool"; text: string; path?: string };
/** notebookPath and openNotebook use app paths (relative to notebooks/); the chat wire uses workspace paths. */
export function ChatPanel({ notebookPath, openNotebook, openTerminal, connect = connectChat }: {
notebookPath?: string; openNotebook(path: string): void; openTerminal(): void; connect?: () => ChatSocket;
}) {
@@ -77,7 +78,7 @@ export function ChatPanel({ notebookPath, openNotebook, openTerminal, connect =
<div className="chat-messages" role="log" aria-label="Chat messages" aria-live="polite">
{!messages.length && <p>{selected ? "Continue this conversation. Earlier messages are retained by the Claude CLI; this view shows messages from this browser session." : "Ask Claude to create or edit a notebook. Do not send secrets."}</p>}
{messages.map((m, i) => <div key={i} className={`chat-message ${m.role}`}>
{m.role === "tool" ? <small>{m.text} {m.path && <button onClick={() => openNotebook(m.path!)}>Open notebook</button>}</small> : <><strong>{m.role === "user" ? "You" : "Claude"}</strong><ReactMarkdown>{m.text}</ReactMarkdown></>}
{m.role === "tool" ? <small>{m.text} {m.path && <button onClick={() => openNotebook(m.path!.replace(/^notebooks\//, ""))}>Open notebook</button>}</small> : <><strong>{m.role === "user" ? "You" : "Claude"}</strong><ReactMarkdown>{m.text}</ReactMarkdown></>}
</div>)}<div ref={end} />
</div>
{error && <p role="alert">{error}</p>}
@@ -86,7 +87,7 @@ export function ChatPanel({ notebookPath, openNotebook, openTerminal, connect =
{!messages.length && <div className="actions">{["Create a notebook that…", "Add a check that…"].map(prompt => <button key={prompt} onClick={() => setText(prompt)}>{prompt}</button>)}</div>}
<form onSubmit={e => { e.preventDefault(); if (!ready || busy || !text.trim()) return;
setMessages(previous => [...previous, { role: "user", text }]); setBusy(true); setError(""); setLogin(false); setSummary("");
send({ type: "send", conversationId: selectedRef.current || undefined, message: text, notebookPath, allowShell }); setText("");
send({ type: "send", conversationId: selectedRef.current || undefined, message: text, notebookPath: notebookPath && `notebooks/${notebookPath}`, allowShell }); setText("");
}}>
<label>Message<textarea value={text} onChange={e => setText(e.target.value)} maxLength={32000} required rows={3} /></label>
<label className="chat-shell"><input type="checkbox" checked={allowShell} disabled={busy} onChange={e => setAllowShell(e.target.checked)} />Allow Claude to run shell commands on the ShellBook server</label>
+3 -3
View File
@@ -6,7 +6,7 @@ import type { ChatEvent } from "@shellbook/core";
function setup() {
const socket: ChatSocket = { onopen: null, onclose: null, onerror: null, onmessage: null, send: vi.fn(), close: vi.fn() };
const openNotebook = vi.fn(), openTerminal = vi.fn();
const view = render(<ChatPanel notebookPath="notebooks/current.md" connect={() => socket} openNotebook={openNotebook} openTerminal={openTerminal} />);
const view = render(<ChatPanel notebookPath="setup/current.md" connect={() => socket} openNotebook={openNotebook} openTerminal={openTerminal} />);
act(() => socket.onopen?.(new Event("open")));
const emit = (event: ChatEvent) => act(() => socket.onmessage?.({ data: JSON.stringify(event) } as MessageEvent));
return { socket, emit, openNotebook, openTerminal, ...view };
@@ -15,11 +15,11 @@ it("sends notebook context, defaults shell off, streams Markdown, opens edited n
const { socket, emit, openNotebook } = setup();
fireEvent.change(screen.getByLabelText("Message"), { target: { value: "Create nginx" } });
fireEvent.click(screen.getByRole("button", { name: "Send" }));
expect(socket.send).toHaveBeenCalledWith(JSON.stringify({ type: "send", message: "Create nginx", notebookPath: "notebooks/current.md", allowShell: false }));
expect(socket.send).toHaveBeenCalledWith(JSON.stringify({ type: "send", message: "Create nginx", notebookPath: "notebooks/setup/current.md", allowShell: false }));
emit({ type: "delta", text: "Hello **" }); emit({ type: "delta", text: "world**" });
expect(screen.getByText("world").tagName).toBe("STRONG");
emit({ type: "tool", name: "Edit", path: "notebooks/nginx.md" });
fireEvent.click(screen.getByText("Open notebook")); expect(openNotebook).toHaveBeenCalledWith("notebooks/nginx.md");
fireEvent.click(screen.getByText("Open notebook")); expect(openNotebook).toHaveBeenCalledWith("nginx.md");
fireEvent.click(screen.getByText("Stop")); expect(socket.send).toHaveBeenLastCalledWith('{"type":"cancel"}');
emit({ type: "finished", durationMs: 1000, costUsd: .01 }); expect(screen.getByRole("status")).toHaveTextContent("Finished in 1.0s");
});