@@ -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