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:
@@ -11,6 +11,7 @@ export interface ChatSocket {
|
|||||||
const connectChat = (): ChatSocket => new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/api/chat`);
|
const connectChat = (): ChatSocket => new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/api/chat`);
|
||||||
type Message = { role: "user" | "assistant" | "tool"; text: string; path?: string };
|
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 }: {
|
export function ChatPanel({ notebookPath, openNotebook, openTerminal, connect = connectChat }: {
|
||||||
notebookPath?: string; openNotebook(path: string): void; openTerminal(): void; connect?: () => ChatSocket;
|
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">
|
<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.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}`}>
|
{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>)}<div ref={end} />
|
||||||
</div>
|
</div>
|
||||||
{error && <p role="alert">{error}</p>}
|
{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>}
|
{!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;
|
<form onSubmit={e => { e.preventDefault(); if (!ready || busy || !text.trim()) return;
|
||||||
setMessages(previous => [...previous, { role: "user", text }]); setBusy(true); setError(""); setLogin(false); setSummary("");
|
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>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>
|
<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>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type { ChatEvent } from "@shellbook/core";
|
|||||||
function setup() {
|
function setup() {
|
||||||
const socket: ChatSocket = { onopen: null, onclose: null, onerror: null, onmessage: null, send: vi.fn(), close: vi.fn() };
|
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 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")));
|
act(() => socket.onopen?.(new Event("open")));
|
||||||
const emit = (event: ChatEvent) => act(() => socket.onmessage?.({ data: JSON.stringify(event) } as MessageEvent));
|
const emit = (event: ChatEvent) => act(() => socket.onmessage?.({ data: JSON.stringify(event) } as MessageEvent));
|
||||||
return { socket, emit, openNotebook, openTerminal, ...view };
|
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();
|
const { socket, emit, openNotebook } = setup();
|
||||||
fireEvent.change(screen.getByLabelText("Message"), { target: { value: "Create nginx" } });
|
fireEvent.change(screen.getByLabelText("Message"), { target: { value: "Create nginx" } });
|
||||||
fireEvent.click(screen.getByRole("button", { name: "Send" }));
|
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**" });
|
emit({ type: "delta", text: "Hello **" }); emit({ type: "delta", text: "world**" });
|
||||||
expect(screen.getByText("world").tagName).toBe("STRONG");
|
expect(screen.getByText("world").tagName).toBe("STRONG");
|
||||||
emit({ type: "tool", name: "Edit", path: "notebooks/nginx.md" });
|
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"}');
|
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");
|
emit({ type: "finished", durationMs: 1000, costUsd: .01 }); expect(screen.getByRole("status")).toHaveTextContent("Finished in 1.0s");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user