Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Keep agent replies and hide JSON payloads

Bitti321581609791
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 2eb5464..28e5206 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -27,6 +27,7 @@ This alpha update adds SeedVR2 and Real-ESRGAN export upscaling, Long Shot and B
## Fixes and improvements
+- Agent chat appends replies and retains earlier messages, corrections and errors throughout the conversation. Raw JSON and command payloads stay hidden; readable replies and summaries are shown.
- Backdrop prompts describe a flat, constant color field in every shot, limit reference guidance to subject appearance, and use neutral studio styling when no look is specified.
- Zoom uses SlopFab's native edge blend directly, avoiding a second feather or color correction when saving. Source transparency is retained.
- Generate all includes the final scene in an even-length Long Shot sequence, continuing from the preceding scene and preserving earlier joined video and audio. Four scenes generate in 1 → 3 → 2 → 4 order; adding another scene turns the final continuation into a bridge on its next generation.
diff --git a/src/components/workspace/AgentDock.test.tsx b/src/components/workspace/AgentDock.test.tsx
index e6a6af3..3109c63 100644
--- a/src/components/workspace/AgentDock.test.tsx
+++ b/src/components/workspace/AgentDock.test.tsx
@@ -264,7 +264,7 @@ describe("Slop output panel", () => {
expect(field).toHaveValue("Leonard and Penny, in live action");
});
- it("shows one reply per turn: no repeated drafts while working, only the answer once done", async () => {
+ it("appends distinct replies and correction notes and preserves them when the turn finishes", async () => {
let finish!: (value: Awaited<ReturnType<typeof runAgentTurn>>) => void;
vi.mocked(runAgentTurn).mockImplementation(() => new Promise((resolve) => { finish = resolve; }));
render(<AgentDock context="this project" record={project()} providers={providers} onPromptStart={() => undefined} onCommands={async () => undefined} />);
@@ -277,22 +277,121 @@ describe("Slop output panel", () => {
const send = (event: AgentTurnEventPayload["event"]) => act(() => eventHandler?.({ payload: { requestId, event } }));
send({ type: "message", text: "First draft." });
+ const firstBubble = within(log).getByText("First draft.").closest(".agent-message");
send({ type: "message", text: "First draft." });
send({ type: "validation", round: 1, maxRounds: 3, text: "Too short." });
send({ type: "message", text: "Second draft." });
send({ type: "validation", round: 2, maxRounds: 3, text: "Still short." });
- expect(log.querySelectorAll(".agent-conversation__output")).toHaveLength(1);
- expect(log.querySelectorAll(".agent-conversation__validation")).toHaveLength(1);
+ expect(log.querySelectorAll(".agent-conversation__output")).toHaveLength(2);
+ expect(log.querySelectorAll(".agent-conversation__validation")).toHaveLength(2);
expect(log).toHaveTextContent("Second draft.");
- expect(log).not.toHaveTextContent("First draft.");
+ expect(within(log).getByText("First draft.").closest(".agent-message")).toBe(firstBubble);
await act(async () => finish({ result: { kind: "answer", content: "Hello **there**." }, events: [], messages: [
{ id: "u", role: "user", content: "Hi", createdAt: "" },
{ id: "a", role: "assistant", content: "Hello **there**.", createdAt: "" },
] as never }));
- expect(log.querySelectorAll(".agent-conversation__output, .agent-conversation__validation")).toHaveLength(0);
+ expect(log.querySelectorAll(".agent-conversation__output, .agent-conversation__validation")).toHaveLength(4);
expect(log.querySelectorAll(".agent-conversation__assistant")).toHaveLength(1);
expect(within(log).getByText("there").tagName).toBe("STRONG");
+ expect([...log.querySelectorAll(".agent-message__bubble")].map((bubble) => bubble.textContent)).toEqual([
+ "Hi", "First draft.", "Correction 1 of 3: Too short.", "Second draft.", "Correction 2 of 3: Still short.", "Hello there.",
+ ]);
+ });
+
+ it("keeps every completed turn visible and appends even when response history is empty", async () => {
+ const record = project();
+ render(<AgentDock context="this project" record={record} providers={providers} onPromptStart={vi.fn()} onCommands={async () => undefined} />);
+ const log = screen.getByRole("log", { name: "Slop output" });
+ for (let turn = 1; turn <= 4; turn++) {
+ vi.mocked(runAgentTurn).mockResolvedValueOnce({ result: { kind: "answer", content: `Reply ${turn}` }, events: [], messages: [] });
+ fireEvent.change(screen.getByRole("textbox", { name: "Ask Slop about this project" }), { target: { value: `Prompt ${turn}` } });
+ fireEvent.click(screen.getByRole("button", { name: "Send to Slop" }));
+ await within(log).findByText(`Reply ${turn}`);
+ }
+ expect([...log.querySelectorAll(".agent-message__bubble")].map((bubble) => bubble.textContent)).toEqual([
+ "Prompt 1", "Reply 1", "Prompt 2", "Reply 2", "Prompt 3", "Reply 3", "Prompt 4", "Reply 4",
+ ]);
+ expect(vi.mocked(runAgentTurn).mock.calls[3][4]!.map((message) => message.content)).toEqual([
+ "Prompt 1", "Reply 1", "Prompt 2", "Reply 2", "Prompt 3", "Reply 3",
+ ]);
+ });
+
+ it("never shows JSON payloads and deduplicates the final transport echo only within its turn", async () => {
+ let finish!: (value: Awaited<ReturnType<typeof runAgentTurn>>) => void;
+ vi.mocked(runAgentTurn).mockImplementation(() => new Promise((resolve) => { finish = resolve; }));
+ render(<AgentDock context="this project" record={project()} providers={providers} onPromptStart={vi.fn()} onCommands={async () => undefined} />);
+ const log = screen.getByRole("log", { name: "Slop output" });
+ for (let turn = 1; turn <= 2; turn++) {
+ fireEvent.change(screen.getByRole("textbox", { name: "Ask Slop about this project" }), { target: { value: `Request ${turn}` } });
+ fireEvent.click(screen.getByRole("button", { name: "Send to Slop" }));
+ await waitFor(() => expect(runAgentTurn).toHaveBeenCalledTimes(turn));
+ const requestId = vi.mocked(runAgentTurn).mock.calls.at(-1)![3];
+ const send = (text: string) => act(() => eventHandler?.({ payload: { requestId, event: { type: "message", text } } }));
+ for (const raw of ['{"op":"scene.set","id":"secret-payload"}', '{"kind":"answer","content":',
+ JSON.stringify({ type: "item.completed", item: { type: "agent_message", text: '```json\n{"op":"scene.set","id":"secret-payload"}\n```' } })]) {
+ send(raw);
+ expect(log).not.toHaveTextContent("secret-payload");
+ expect(log).not.toHaveTextContent('{"');
+ }
+ send(JSON.stringify({ kind: "answer", content: "Done." }));
+ await act(async () => finish({ result: { kind: "answer", content: "Done." }, events: [], messages: [] }));
+ expect(within(log).getAllByText("Done.")).toHaveLength(turn);
+ }
+ });
+
+ it("retains errors and stopped replies when another request starts", async () => {
+ let finish!: (value: Awaited<ReturnType<typeof runAgentTurn>>) => void;
+ vi.mocked(runAgentTurn).mockRejectedValueOnce(new Error("Connection failed."))
+ .mockImplementationOnce(() => new Promise((resolve) => { finish = resolve; }))
+ .mockResolvedValueOnce({ result: { kind: "answer", content: "Finished." }, events: [], messages: [] });
+ render(<AgentDock context="this project" record={project()} providers={providers} onPromptStart={vi.fn()} onCommands={async () => undefined} />);
+ const send = (text: string) => {
+ fireEvent.change(screen.getByRole("textbox", { name: "Ask Slop about this project" }), { target: { value: text } });
+ fireEvent.click(screen.getByRole("button", { name: "Send to Slop" }));
+ };
+ send("First request");
+ await screen.findByText(/Connection failed/);
+ send("Second request");
+ await waitFor(() => expect(runAgentTurn).toHaveBeenCalledTimes(2));
+ const requestId = vi.mocked(runAgentTurn).mock.calls[1][3];
+ act(() => eventHandler?.({ payload: { requestId, event: { type: "message", text: "Working on it." } } }));
+ fireEvent.click(screen.getByRole("button", { name: "Cancel agent turn" }));
+ await act(async () => finish({ result: { kind: "answer", content: "Late reply." }, events: [], messages: [] }));
+ send("Third request");
+ await screen.findByText("Finished.");
+ expect(screen.getByText(/Connection failed/)).toBeInTheDocument();
+ expect(screen.getByText("Second request")).toBeInTheDocument();
+ expect(screen.getByText("Working on it.")).toBeInTheDocument();
+ expect(screen.getByText("Request stopped.")).toBeInTheDocument();
+ expect(screen.queryByText("Late reply.")).not.toBeInTheDocument();
+ });
+
+ it("discards late replies from a previous project without clearing the new project's turn", async () => {
+ let finishOld!: (value: Awaited<ReturnType<typeof runAgentTurn>>) => void;
+ let finishNew!: (value: Awaited<ReturnType<typeof runAgentTurn>>) => void;
+ vi.mocked(runAgentTurn).mockImplementationOnce(() => new Promise((resolve) => { finishOld = resolve; }))
+ .mockImplementationOnce(() => new Promise((resolve) => { finishNew = resolve; }));
+ const onCommands = vi.fn();
+ const props = { context: "this project", providers, onPromptStart: vi.fn(), onCommands };
+ const view = render(<AgentDock {...props} record={project()} />);
+ const send = (text: string) => {
+ fireEvent.change(screen.getByRole("textbox", { name: "Ask Slop about this project" }), { target: { value: text } });
+ fireEvent.click(screen.getByRole("button", { name: "Send to Slop" }));
+ };
+ send("Old project request");
+ await waitFor(() => expect(runAgentTurn).toHaveBeenCalledOnce());
+ view.rerender(<AgentDock {...props} record={project()} />);
+ expect(screen.queryByText("Old project request")).not.toBeInTheDocument();
+ send("New project request");
+ await waitFor(() => expect(runAgentTurn).toHaveBeenCalledTimes(2));
+ await act(async () => finishOld({ result: { kind: "commands", summary: "Stale reply", commands: [] }, events: [], messages: [] }));
+ expect(screen.queryByText("Stale reply")).not.toBeInTheDocument();
+ expect(onCommands).not.toHaveBeenCalled();
+ expect(screen.getByRole("button", { name: "Cancel agent turn" })).toBeInTheDocument();
+ await act(async () => finishNew({ result: { kind: "answer", content: "New reply" }, events: [], messages: [] }));
+ expect(screen.getByText("New project request")).toBeInTheDocument();
+ expect(screen.getByText("New reply")).toBeInTheDocument();
});
it("formats Slop's replies as a message", () => {
diff --git a/src/components/workspace/AgentDock.tsx b/src/components/workspace/AgentDock.tsx
index 0ad0827..7d54602 100644
--- a/src/components/workspace/AgentDock.tsx
+++ b/src/components/workspace/AgentDock.tsx
@@ -1,6 +1,6 @@
import { listen } from "@tauri-apps/api/event";
import { Agent32, Clear16, Dismiss16, Send16, Stop14 } from "../ui/icons";
-import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";
+import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
import { describeDiagnosticError, errorContext, writeDiagnostic } from "../../lib/diagnostics";
import { isTauri } from "../../lib/persistence";
import {
@@ -19,6 +19,8 @@ import type { ProjectRecord } from "../../lib/project";
import type { AgentMessage } from "../../lib/project";
import { ComboBox, EmptyState, PaneHeader, ProgressRing, tooltipProps } from "../ui";
import { MessageText } from "./MessageText";
+import { readableProviderOutput } from "../../lib/agentOutput";
+export { readableProviderOutput } from "../../lib/agentOutput";
/** How tall the prompt box grows before it scrolls. */
export const COMPOSER_MAX_LINES = 6;
@@ -28,6 +30,12 @@ export interface AgentActivity {
text: string;
}
+interface TranscriptMessage {
+ id: string;
+ kind: AgentActivity["kind"] | "user" | "assistant" | "error";
+ text: string;
+}
+
/** The agent as a docked tool pane: a 36px pane toolbar (the provider picker
* leading, clear and close trailing — no title, the workspace's aside already
* names the pane), the conversation, and a flush text box with a send button.
@@ -61,9 +69,8 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
const [prompt, setPrompt] = useState("");
const [lastPrompt, setLastPrompt] = useState<string | null>(null);
const [requestId, setRequestId] = useState<string | null>(null);
- const [error, setError] = useState<string | null>(null);
- const [activity, setActivity] = useState<AgentActivity[]>([]);
- const [pendingPrompt, setPendingPrompt] = useState<string | null>(null);
+ const [transcript, setTranscript] = useState<TranscriptMessage[]>([]);
+ const seenActivity = useRef(new Set<string>());
const [sessionMessages, setSessionMessages] = useState<AgentMessage[]>([]);
const activeRequest = useRef<string | null>(null);
const activeController = useRef<AbortController | null>(null);
@@ -90,19 +97,31 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
}, [prompt]);
const selected = providers.find((item) => item.id === provider);
const ready = selected?.state === "ready";
- const messages = useMemo(() => sessionMessages.slice(-4), [sessionMessages]);
- const empty = !messages.length && !pendingPrompt && !activity.length && !error && !requestId;
+ const empty = !transcript.length && !requestId;
+
+ const appendActivity = (item: AgentActivity | { kind: "assistant" | "error"; text: string }) => {
+ // CLIs can emit the same reply in both a message and a final result.
+ // Deduplicate only within this turn; never replace a posted message.
+ const key = `${item.kind === "assistant" ? "output" : item.kind}\0${item.text}`;
+ if (seenActivity.current.has(key)) return;
+ seenActivity.current.add(key);
+ const message = { ...item, id: crypto.randomUUID() };
+ setTranscript((current) => [...current, message]);
+ };
useEffect(() => {
setSessionMessages([]);
- setActivity([]);
- setError(null);
- setPendingPrompt(null);
- }, [record.config.id]);
+ setTranscript([]);
+ seenActivity.current.clear();
+ setRequestId(null);
+ setWorkDetail(null);
+ }, [record.config.id, record.folderPath]);
useEffect(() => () => {
activeController.current?.abort(new Error("Agent request cancelled."));
if (activeRequest.current) void cancelAgentTurn(activeRequest.current);
+ activeRequest.current = null;
+ activeController.current = null;
}, [record.config.id, record.folderPath]);
/* One listener lives for the dock's lifetime. A request id filters out any
@@ -112,10 +131,10 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
let disposed = false;
let stop: (() => void) | undefined;
void listen<AgentTurnEventPayload>(AGENT_TURN_EVENT, ({ payload }) => {
- if (payload.requestId !== activeRequest.current) return;
+ if (payload.requestId !== activeRequest.current || activeController.current?.signal.aborted) return;
const item = activityFromEvent(payload.event);
if (!item) return;
- setActivity((current) => mergeActivity(current, item));
+ appendActivity(item);
}).then((unlisten) => {
if (disposed) unlisten();
else stop = unlisten;
@@ -129,7 +148,7 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
useEffect(() => {
const panel = conversation.current;
if (panel) panel.scrollTop = panel.scrollHeight;
- }, [activity, messages, error, pendingPrompt, requestId]);
+ }, [transcript, requestId, workDetail]);
/* The text box is disabled while a turn runs; keep the focus in the pane
(on the stop button) so Esc still reaches it. */
@@ -151,46 +170,54 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
const send = async () => {
const clean = prompt.trim();
- if (!clean || !ready || requestId) return;
+ if (!clean || !ready || activeRequest.current) return;
onPromptStart();
const id = crypto.randomUUID();
activeRequest.current = id;
setRequestId(id);
- setError(null);
- setPendingPrompt(clean);
+ const userMessage: AgentMessage = { id: crypto.randomUUID(), role: "user", content: clean, createdAt: new Date().toISOString() };
+ setTranscript((current) => [...current, { id: userMessage.id, kind: "user", text: clean }]);
setLastPrompt(clean);
setPrompt("");
- setActivity([]);
+ seenActivity.current.clear();
+ const controller = new AbortController();
+ activeController.current = controller;
try {
// The native agent uses this legacy field to select its instructions.
// Override only the request snapshot, never the saved project document.
- const controller = new AbortController();
- activeController.current = controller;
setWorkDetail(null);
const response = await runAgentWorkflow({ ...record, config: { ...record.config, generationType: mode } },
- provider, clean, id, sessionMessages, controller.signal, generation, setWorkDetail, scope);
+ provider, clean, id, sessionMessages, controller.signal, generation,
+ (detail) => { if (activeRequest.current === id) setWorkDetail(readableProviderOutput(detail)); }, scope);
controller.signal.throwIfAborted();
+ if (activeRequest.current !== id) return;
if ("commands" in response.result) await onCommands(response.result.commands);
- setSessionMessages(response.messages);
- // The finished reply is in the conversation now; the streamed drafts and
- // correction notes that led to it would only repeat it.
- setActivity([]);
- setPendingPrompt(null);
+ controller.signal.throwIfAborted();
+ if (activeRequest.current !== id) return;
+ const content = "summary" in response.result ? response.result.summary : response.result.content;
+ setSessionMessages((current) => [...current, userMessage,
+ { id: crypto.randomUUID(), role: "assistant", content, createdAt: new Date().toISOString() }]);
+ const text = readableProviderOutput(content);
+ if (text) appendActivity({ kind: "assistant", text });
} catch (reason) {
- if (activeController.current?.signal.aborted) {
- setPendingPrompt(null);
+ if (activeRequest.current !== id) return;
+ if (controller.signal.aborted) {
+ appendActivity({ kind: "assistant", text: "Request stopped." });
} else {
const detail = describeDiagnosticError(reason);
writeDiagnostic("error", "agent", "turn.failed", detail, { requestId: id, provider, projectId: record.config.id, ...errorContext(reason) });
- setError(detail);
+ const readable = readableProviderOutput(detail);
+ appendActivity({ kind: "error", text: `Couldn’t finish that request.${readable ? ` ${readable}` : ""}` });
}
} finally {
- const restoreFocus = document.activeElement === field.current || document.activeElement === cancelButton.current;
- activeRequest.current = null;
- activeController.current = null;
- setWorkDetail(null);
- setRequestId(null);
- if (restoreFocus) requestAnimationFrame(() => field.current?.focus());
+ if (activeRequest.current === id) {
+ const restoreFocus = document.activeElement === field.current || document.activeElement === cancelButton.current;
+ activeRequest.current = null;
+ activeController.current = null;
+ setWorkDetail(null);
+ setRequestId(null);
+ if (restoreFocus) requestAnimationFrame(() => field.current?.focus());
+ }
}
};
const cancel = () => {
@@ -199,9 +226,8 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
};
const clear = () => {
setSessionMessages([]);
- setActivity([]);
- setError(null);
- setPendingPrompt(null);
+ setTranscript([]);
+ seenActivity.current.clear();
};
return (
@@ -246,14 +272,10 @@ export function AgentDock({ context, record, scope = "project", mode = record.co
title="Nothing asked yet"
description={scope === "settings" ? "Ask Slop to configure generators, find weight files or update LoRAs." : `Ask Slop to write scenes from a prompt, refine shots or edit ${context}.`}
/>}
- {messages.map((message) => message.role === "user"
- ? <Bubble key={message.id} className="agent-conversation__user" speaker="You">{message.content}</Bubble>
- : <Bubble key={message.id} className="agent-conversation__assistant" speaker="Slop"><MessageText text={message.content} /></Bubble>)}
- {pendingPrompt && <Bubble className="agent-conversation__pending" speaker="You">{pendingPrompt}</Bubble>}
- {activity.map((item, index) => <Bubble key={`${item.kind}-${index}`} className={`agent-conversation__${item.kind}`} speaker={activityLabel(item.kind)}>
- {item.kind === "output" ? <MessageText text={item.text} /> : item.text}
+ {transcript.map((item) => <Bubble key={item.id} className={`agent-conversation__${item.kind}`}
+ speaker={item.kind === "user" ? "You" : item.kind === "assistant" || item.kind === "error" ? "Slop" : activityLabel(item.kind)}>
+ {item.kind === "output" || item.kind === "assistant" ? <MessageText text={item.text} /> : item.text}
</Bubble>)}
- {error && <Bubble className="agent-conversation__error" speaker="Slop">Couldn’t finish that request. {error}</Bubble>}
{requestId && <Bubble className="agent-conversation__waiting" speaker="Slop" aria-label="Waiting for the next agent response">
<span className="agent-conversation__thinking"><ProgressRing size={16} aria-label={workDetail ? "Working" : "Thinking"} /><span aria-hidden="true">{workDetail ?? "Thinking…"}</span></span>
</Bubble>}
@@ -300,16 +322,6 @@ function Bubble({ className, speaker, children, "aria-label": label }: { classNa
</div>;
}
-/** Providers repeat themselves: one reply arrives as a streamed record, again
- * in the CLI's final result record, and again on every validation round. Keep
- * one live draft and the latest correction note (each moved to the end, so the
- * log still reads in order) and each distinct diagnostic once. */
-export function mergeActivity(current: AgentActivity[], item: AgentActivity): AgentActivity[] {
- if (current.some((existing) => existing.kind === item.kind && existing.text === item.text)) return current;
- const kept = item.kind === "diagnostic" ? current : current.filter((existing) => existing.kind !== item.kind);
- return [...kept, item].slice(-100);
-}
-
const activityLabel =(kind: AgentActivity["kind"]): string => {
switch (kind) {
case "output": return "Slop";
@@ -322,8 +334,14 @@ function activityFromEvent(event: AgentTurnEvent): AgentActivity | null {
switch (event.type) {
case "started":
case "completed": return null;
- case "validation": return { kind: "validation", text: `Correction ${event.round} of ${event.maxRounds}: ${event.text}` };
- case "diagnostic": return event.text.trim() ? { kind: "diagnostic", text: event.text.trim() } : null;
+ case "validation": {
+ const text = readableProviderOutput(event.text);
+ return { kind: "validation", text: `Correction ${event.round} of ${event.maxRounds}${text ? `: ${text}` : ""}` };
+ }
+ case "diagnostic": {
+ const text = readableProviderOutput(event.text);
+ return text ? { kind: "diagnostic", text } : null;
+ }
case "message": {
const text = readableProviderOutput(event.text);
return text ? { kind: "output", text } : null;
@@ -331,42 +349,6 @@ function activityFromEvent(event: AgentTurnEvent): AgentActivity | null {
}
}
-/** Provider CLIs stream NDJSON. Pull out the model-authored text so the panel
- * shows what Slop is saying instead of transport records. */
-export function readableProviderOutput(line: string): string | null {
- const clean = line.trim();
- if (!clean) return null;
- let value: unknown;
- try { value = JSON.parse(clean); } catch { return clean; }
- if (!value || typeof value !== "object") return clean;
- const record = value as Record<string, unknown>;
- const message = record.message as Record<string, unknown> | undefined;
- const candidates: unknown[] = [];
- if (Array.isArray(message?.content)) {
- candidates.push(...message.content.map((part) => (part as Record<string, unknown>)?.text ?? (part as Record<string, unknown>)?.thinking));
- }
- candidates.push((record.item as Record<string, unknown> | undefined)?.text, record.result);
- for (const candidate of candidates) {
- if (typeof candidate !== "string" || !candidate.trim()) continue;
- const text = candidate.trim();
- try {
- const turn = JSON.parse(text) as Record<string, unknown>;
- const summary = "summary" in turn ? turn.summary : turn.content;
- if (typeof summary === "string" && summary.trim()) return summary.trim();
- } catch { /* A normal model message is already displayable text. */ }
- for (const line of text.split(/\r?\n/).reverse()) {
- try {
- const command = JSON.parse(line) as Record<string, unknown>;
- if (command.op === "commit" && typeof command.summary === "string" && command.summary.trim()) {
- return command.summary.trim();
- }
- } catch { /* Keep looking for a JSONL commit line. */ }
- }
- return text;
- }
- return null;
-}
-
const providerStateLabel = (state: ProviderStatus["state"]) => state === "checking" ? "Checking…" : state === "ready" ? "Ready" : state === "notInstalled" ? "Not installed" : state === "authRequired" ? "Sign in required" : state === "disabled" ? "Disabled" : "Unavailable";
const providerNextStep = ({ state, label }: ProviderStatus): string => {
diff --git a/src/lib/agentOutput.test.ts b/src/lib/agentOutput.test.ts
new file mode 100644
index 0000000..7eb63b5
--- /dev/null
+++ b/src/lib/agentOutput.test.ts
@@ -0,0 +1,36 @@
+import { expect, it } from "vitest";
+import { readableProviderOutput } from "./agentOutput";
+
+const command = '{"op":"scene.set","id":"scene","title":"Opening"}';
+const commit = '{"op":"commit","summary":"Updated the opening."}';
+const reply = '{"kind":"answer","content":"Ready **now**."}';
+
+it.each([command, JSON.stringify({ type: "item.completed", item: { type: "agent_message", text: command } }),
+ JSON.stringify({ type: "item.completed", item: { type: "command_execution", text: "Tool output" } }),
+ '{"kind":"inspect","requests":[{"op":"generator.list"}]}', '[{"op":"scene.set"}]',
+ '{"kind":"answer","content":', '{"op":', '"op": "scene.set",', "}", "```json\n" + command,
+ "```json\n" + command + "\n```", "```json", "```", '{"unknown":"payload"}',
+])("hides raw, partial and tool JSON: %s", (text) => {
+ expect(readableProviderOutput(text)).toBeNull();
+});
+
+it.each([reply, JSON.stringify({ type: "result", result: reply }),
+ JSON.stringify({ type: "assistant", message: { content: [{ type: "text", text: reply }] } }),
+ "```json\n" + reply + "\n```",
+])("extracts readable replies without their JSON wrapper: %s", (text) => {
+ expect(readableProviderOutput(text)).toBe("Ready **now**.");
+});
+
+it.each([`${command}\n${commit}`, `\`\`\`jsonl\n${command}\n${commit}\n\`\`\``,
+ JSON.stringify({ type: "item.completed", item: { type: "agent_message", text: `${command}\n${commit}` } }),
+])("shows only the summary of a command batch: %s", (text) => {
+ expect(readableProviderOutput(text)).toBe("Updated the opening.");
+});
+
+it("keeps prose and Markdown while removing embedded JSON blocks", () => {
+ const prose = "Plan:\n\n- **Warm** light\n- [Open the scene](scene:one)";
+ expect(readableProviderOutput(prose)).toBe(prose);
+ expect(readableProviderOutput("42")).toBe("42");
+ expect(readableProviderOutput("[Open the scene](scene:one)")).toBe("[Open the scene](scene:one)");
+ expect(readableProviderOutput(`Updated.\n\n\`\`\`json\n${command}\n\`\`\`\n\nAll set.`)).toBe("Updated.\n\n\n\nAll set.");
+});
diff --git a/src/lib/agentOutput.ts b/src/lib/agentOutput.ts
new file mode 100644
index 0000000..c663bd9
--- /dev/null
+++ b/src/lib/agentOutput.ts
@@ -0,0 +1,50 @@
+/** Only human-readable provider content belongs in chat. Unknown transport
+ * records, tool payloads and incomplete JSON are intentionally invisible. */
+export function readableProviderOutput(input: string): string | null {
+ const structured = (text: string) => /^(?:[{}\[\]]|"(?:[^"\\]|\\.)*"\s*:)/.test(text)
+ && !/^\[[^\]\n]+\]\(/.test(text); // Keep ordinary Markdown links.
+ const join = (values: (string | null)[]) => [...new Set(values.filter((value): value is string => Boolean(value)))].join("\n\n") || null;
+ const read = (input: string, depth: number): string | null => {
+ if (depth > 8) return null;
+ const text = input.trim();
+ if (!text) return null;
+ if (/^```(?:jsonl?)?$/i.test(text)) return null;
+ const fence = text.match(/^```(?:json|jsonl)?\s*\n([\s\S]*?)\n```$/i);
+ if (fence && (structured(fence[1].trim()) || /^```json/i.test(text))) return read(fence[1], depth + 1);
+ let value: unknown;
+ try { value = JSON.parse(text); } catch {
+ if (structured(text)) {
+ // JSONL may contain many commands followed by a readable commit.
+ // Never use incomplete or unrecognized JSON as a text fallback.
+ return join(text.split(/\r?\n/).flatMap((line) => {
+ try { JSON.parse(line); return [read(line, depth + 1)]; } catch { return []; }
+ }));
+ }
+ // Preserve prose around command blocks, without flashing the payload.
+ return text.replace(/```(?:json|jsonl)\b[^\n]*\n([\s\S]*?)(?:```|$)/gi,
+ (_match, payload: string) => read(payload, depth + 1) ?? "")
+ .split(/\r?\n/).map((line) => structured(line.trim()) ? read(line, depth + 1) ?? "" : line).join("\n").trim() || null;
+ }
+ if (typeof value === "string") return read(value, depth + 1);
+ if (typeof value === "number" || typeof value === "boolean") return text;
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
+ const record = value as Record<string, unknown>;
+ const readable = (candidate: unknown) => typeof candidate === "string" ? read(candidate, depth + 1) : null;
+ if (record.op === "commit") return readable(record.summary);
+ if (record.kind === "answer" || record.kind === "question") return readable(record.content);
+ if (["commands", "generatorCommands", "generation"].includes(String(record.kind))) return readable(record.summary);
+ if (record.type === "assistant") {
+ const message = record.message as { content?: unknown } | undefined;
+ if (Array.isArray(message?.content)) return join(message.content.map((part) =>
+ part?.type === "text" ? readable(part.text) : null));
+ return readable(message?.content);
+ }
+ if (record.type === "item.completed") {
+ const item = record.item as { type?: string; text?: unknown } | undefined;
+ if (item && (!item.type || item.type === "agent_message")) return readable(item.text);
+ }
+ if (record.type === "result") return readable(record.result);
+ return null;
+ };
+ return read(input, 0);
+}