xonecas.io
64875
Mc

mini-coder / commit 218a23b

fix: stream committed assistant and tool messages in UI

xonecas2026-04-06 218a23b1092c331cb2f8a2b264d70c5a336aa417 parent ab382fc

5 files changed

TODO.md+0−2
@@ -9,8 +9,6 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
99
1010 ### Core behavior
1111
12-- [ ] **End-to-end streaming in the UI** (`ui.ts`) — completed assistant/tool messages should remain visible in the conversation log as soon as they happen, without waiting for the full loop to finish and reload from the DB. The current state/event flow drops already-produced assistant text during tool-use turns.
13-
1412 - [ ] **Implement `/skill:name` submission flow** (`ui.ts`) — strip the prefix from input, prepend the selected skill body to the user message, and send the rest of the user text as the instruction.
1513
1614 - [ ] **Implement image submission flow** (`ui.ts`) — when `parseInput()` returns an image, send it as `ImageContent` in the user message when the active model supports images.
src/agent.test.ts+73−0
@@ -535,6 +535,79 @@ describe("agent loop", () => {
535535 expect(toolEnds).toHaveLength(1);
536536 });
537537
538+ test("emits committed assistant and tool-result events in tool-use order", async () => {
539+ faux.setResponses([
540+ fauxAssistantMessage(
541+ [
542+ fauxText("I'll inspect the output first."),
543+ fauxToolCall("shell", { command: "echo test" }),
544+ ],
545+ { stopReason: "toolUse" },
546+ ),
547+ fauxAssistantMessage("Done."),
548+ ]);
549+
550+ const events: AgentEvent[] = [];
551+
552+ const session = createSession(db, { cwd: tmp });
553+ const userMsg = makeUser("go");
554+ const turn = appendMessage(db, session.id, userMsg);
555+
556+ await runAgentLoop({
557+ db,
558+ sessionId: session.id,
559+ turn,
560+ model: faux.getModel(),
561+ systemPrompt: "Test",
562+ tools: builtinToolDefs(),
563+ toolHandlers: builtinToolHandlers(),
564+ messages: [userMsg],
565+ cwd: tmp,
566+ onEvent: (e) => events.push(e),
567+ });
568+
569+ const assistantMessages = events.filter(
570+ (event) => event.type === "assistant_message",
571+ );
572+ const toolResults = events.filter((event) => event.type === "tool_result");
573+
574+ expect(assistantMessages).toHaveLength(2);
575+ expect(toolResults).toHaveLength(1);
576+
577+ const firstAssistant = assistantMessages[0];
578+ const toolStart = events.find((event) => event.type === "tool_start");
579+ const toolEnd = events.find((event) => event.type === "tool_end");
580+ const toolResult = toolResults[0];
581+
582+ if (
583+ !firstAssistant ||
584+ firstAssistant.type !== "assistant_message" ||
585+ !toolStart ||
586+ toolStart.type !== "tool_start" ||
587+ !toolEnd ||
588+ toolEnd.type !== "tool_end" ||
589+ !toolResult ||
590+ toolResult.type !== "tool_result"
591+ ) {
592+ throw new Error(
593+ "Expected assistant, tool start/end, and tool result events",
594+ );
595+ }
596+
597+ expect(firstAssistant.message.stopReason).toBe("toolUse");
598+ expect(firstAssistant.message.content).toContainEqual(
599+ fauxText("I'll inspect the output first."),
600+ );
601+
602+ const firstAssistantIndex = events.indexOf(firstAssistant);
603+ const toolStartIndex = events.indexOf(toolStart);
604+ const toolEndIndex = events.indexOf(toolEnd);
605+ const toolResultIndex = events.indexOf(toolResult);
606+
607+ expect(firstAssistantIndex).toBeLessThan(toolStartIndex);
608+ expect(toolEndIndex).toBeLessThan(toolResultIndex);
609+ });
610+
538611 test("emits tool delta events during tool execution", async () => {
539612 faux.setResponses([
540613 fauxAssistantMessage([fauxToolCall("shell", { command: "echo test" })], {
src/agent.ts+4−0
@@ -48,6 +48,7 @@ export type { ToolExecResult };
4848 export type AgentEvent =
4949 | { type: "text_delta"; delta: string }
5050 | { type: "thinking_delta"; delta: string }
51+ | { type: "assistant_message"; message: AssistantMessage }
5152 | {
5253 type: "tool_start";
5354 toolCallId: string;
@@ -66,6 +67,7 @@ export type AgentEvent =
6667 name: string;
6768 result: ToolExecResult;
6869 }
70+ | { type: "tool_result"; message: ToolResultMessage }
6971 | { type: "done"; message: AssistantMessage }
7072 | { type: "error"; message: AssistantMessage }
7173 | { type: "aborted"; message: AssistantMessage };
@@ -268,6 +270,7 @@ export async function runAgentLoop(
268270 // Append assistant message to history and DB
269271 messages.push(assistantMessage);
270272 appendMessage(db, sessionId, assistantMessage, turn);
273+ onEvent?.({ type: "assistant_message", message: assistantMessage });
271274
272275 // Handle stop reasons
273276 if (
@@ -339,6 +342,7 @@ export async function runAgentLoop(
339342
340343 messages.push(toolResultMessage);
341344 appendMessage(db, sessionId, toolResultMessage, turn);
345+ onEvent?.({ type: "tool_result", message: toolResultMessage });
342346 }
343347
344348 // Loop back to stream with updated context
src/ui.test.ts+203−0
@@ -3,11 +3,16 @@ import { mkdtempSync, rmSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
55 import type { Node } from "@cel-tui/types";
6+import type { AssistantMessage, Model } from "@mariozechner/pi-ai";
67 import {
8+ createAssistantMessageEventStream,
79 fauxAssistantMessage,
810 fauxText,
911 fauxThinking,
12+ fauxToolCall,
13+ registerApiProvider,
1014 registerFauxProvider,
15+ unregisterApiProviders,
1116 } from "@mariozechner/pi-ai";
1217 import {
1318 type AppState,
@@ -486,6 +491,204 @@ describe("ui rendering", () => {
486491 }
487492 });
488493
494+ test("completed assistant text stays visible while a tool is running", async () => {
495+ const faux = registerFauxProvider();
496+ const state = createTestState();
497+ const originalPath = process.env.PATH;
498+ process.env.PATH = `${process.env.PATH ?? ""}:/usr/bin:/bin`;
499+ state.cwd = process.cwd();
500+ state.canonicalCwd = process.cwd();
501+ state.model = faux.getModel();
502+ faux.setResponses([
503+ fauxAssistantMessage(
504+ [
505+ fauxText("I'll inspect the command output first."),
506+ fauxToolCall("shell", { command: "sleep 0.2; echo tool-output" }),
507+ ],
508+ { stopReason: "toolUse" },
509+ ),
510+ fauxAssistantMessage("Done."),
511+ ]);
512+
513+ try {
514+ handleInput("hello", state);
515+
516+ await waitFor(() => {
517+ const logText = collectText({
518+ type: "vstack",
519+ props: {},
520+ children: buildConversationLog(state),
521+ });
522+ return logText.includes("Running...");
523+ });
524+
525+ const logText = collectText({
526+ type: "vstack",
527+ props: {},
528+ children: buildConversationLog(state),
529+ });
530+
531+ expect(logText).toContain("I'll inspect the command output first.");
532+ expect(logText).toContain("$ sleep 0.2; echo tool-output");
533+ expect(logText).toContain("Running...");
534+ } finally {
535+ process.env.PATH = originalPath;
536+ faux.unregister();
537+ state.db.close();
538+ }
539+ });
540+
541+ test("completed tool results stay visible before the full loop finishes", async () => {
542+ const api = "ui-streaming-tool-result-test";
543+ const sourceId = "ui-streaming-tool-result-test-source";
544+ const model: Model<string> = {
545+ id: "ui-streaming-tool-result-model",
546+ name: "UI Streaming Tool Result Model",
547+ api,
548+ provider: "test",
549+ baseUrl: "http://localhost:0",
550+ reasoning: true,
551+ input: ["text"],
552+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
553+ contextWindow: 128000,
554+ maxTokens: 4096,
555+ };
556+
557+ let callCount = 0;
558+ const buildProviderStream = () => {
559+ callCount += 1;
560+ const stream = createAssistantMessageEventStream();
561+
562+ if (callCount === 1) {
563+ const message: AssistantMessage = {
564+ role: "assistant",
565+ content: [fauxToolCall("shell", { command: "echo tool-output" })],
566+ api,
567+ provider: model.provider,
568+ model: model.id,
569+ usage: {
570+ input: 0,
571+ output: 0,
572+ cacheRead: 0,
573+ cacheWrite: 0,
574+ totalTokens: 0,
575+ cost: {
576+ input: 0,
577+ output: 0,
578+ cacheRead: 0,
579+ cacheWrite: 0,
580+ total: 0,
581+ },
582+ },
583+ stopReason: "toolUse",
584+ timestamp: Date.now(),
585+ };
586+
587+ queueMicrotask(() => {
588+ stream.push({
589+ type: "done",
590+ reason: "toolUse",
591+ message,
592+ });
593+ stream.end(message);
594+ });
595+
596+ return stream;
597+ }
598+
599+ const partial: AssistantMessage = {
600+ role: "assistant",
601+ content: [fauxText("Done streaming")],
602+ api,
603+ provider: model.provider,
604+ model: model.id,
605+ usage: {
606+ input: 0,
607+ output: 0,
608+ cacheRead: 0,
609+ cacheWrite: 0,
610+ totalTokens: 0,
611+ cost: {
612+ input: 0,
613+ output: 0,
614+ cacheRead: 0,
615+ cacheWrite: 0,
616+ total: 0,
617+ },
618+ },
619+ stopReason: "stop",
620+ timestamp: Date.now(),
621+ };
622+ const finalMessage: AssistantMessage = { ...partial };
623+
624+ queueMicrotask(() => {
625+ stream.push({
626+ type: "text_delta",
627+ contentIndex: 0,
628+ delta: "Done streaming",
629+ partial,
630+ });
631+ setTimeout(() => {
632+ stream.push({
633+ type: "done",
634+ reason: "stop",
635+ message: finalMessage,
636+ });
637+ stream.end(finalMessage);
638+ }, 200);
639+ });
640+
641+ return stream;
642+ };
643+
644+ registerApiProvider(
645+ {
646+ api,
647+ stream: (_model, _context, _options) => buildProviderStream(),
648+ streamSimple: (_model, _context, _options) => buildProviderStream(),
649+ },
650+ sourceId,
651+ );
652+
653+ const state = createTestState();
654+ const originalPath = process.env.PATH;
655+ process.env.PATH = `${process.env.PATH ?? ""}:/usr/bin:/bin`;
656+ state.cwd = process.cwd();
657+ state.canonicalCwd = process.cwd();
658+ state.model = model;
659+
660+ try {
661+ handleInput("hello", state);
662+
663+ await waitFor(() => {
664+ const logText = collectText({
665+ type: "vstack",
666+ props: {},
667+ children: buildConversationLog(state),
668+ });
669+ return (
670+ state.running &&
671+ logText.includes("tool-output") &&
672+ logText.includes("Done streaming")
673+ );
674+ });
675+
676+ const logText = collectText({
677+ type: "vstack",
678+ props: {},
679+ children: buildConversationLog(state),
680+ });
681+
682+ expect(logText).toContain("$ echo tool-output");
683+ expect(logText).toContain("tool-output");
684+ expect(logText).toContain("Done streaming");
685+ } finally {
686+ process.env.PATH = originalPath;
687+ unregisterApiProviders(sourceId);
688+ state.db.close();
689+ }
690+ });
691+
489692 test("renderInputArea reuses stable TextInput handlers from the input controller", () => {
490693 const state = createTestState();
491694
src/ui.ts+17−4
@@ -1838,11 +1838,16 @@ function handleAgentEvent(event: AgentEvent, state: AppState): void {
18381838 }
18391839 break;
18401840
1841- case "tool_start":
1842- // The assistant message is appended to history before tool_start,
1843- // so clear the streaming buffers — that text is now in history.
1841+ case "assistant_message":
1842+ state.messages.push(event.message);
1843+ state.stats = computeStats(state.messages);
18441844 streamingText = "";
18451845 streamingThinking = "";
1846+ stickToBottom = true;
1847+ cel.render();
1848+ break;
1849+
1850+ case "tool_start":
18461851 pendingToolCalls.push({
18471852 toolCallId: event.toolCallId,
18481853 name: event.name,
@@ -1882,10 +1887,18 @@ function handleAgentEvent(event: AgentEvent, state: AppState): void {
18821887 break;
18831888 }
18841889
1890+ case "tool_result":
1891+ state.messages.push(event.message);
1892+ pendingToolCalls = pendingToolCalls.filter(
1893+ (toolCall) => toolCall.toolCallId !== event.message.toolCallId,
1894+ );
1895+ stickToBottom = true;
1896+ cel.render();
1897+ break;
1898+
18851899 case "done":
18861900 case "error":
18871901 case "aborted":
1888- // Completed messages are already in state.messages.
18891902 streamingText = "";
18901903 streamingThinking = "";
18911904 pendingToolCalls = [];