TUI: keep streamed text when final output is empty

Closes #747
This commit is contained in:
Shadow
2026-01-12 21:29:15 -06:00
parent 68569afb4b
commit 2467a103b2
4 changed files with 45 additions and 1 deletions

View File

@@ -49,6 +49,14 @@ export class ChatLog extends Container {
this.streamingAssistant.setText(text);
}
getStreamingText(runId?: string) {
if (!this.streamingAssistant) return null;
if (runId && this.streamingRunId && runId !== this.streamingRunId) {
return null;
}
return this.streamingText;
}
finalizeAssistant(text: string, runId?: string) {
if (
this.streamingAssistant &&

20
src/tui/tui.test.ts Normal file
View File

@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { resolveFinalAssistantText } from "./tui.js";
describe("resolveFinalAssistantText", () => {
it("falls back to streamed text when final text is empty", () => {
expect(
resolveFinalAssistantText({ finalText: "", streamedText: "Hello" }),
).toBe("Hello");
});
it("prefers the final text when present", () => {
expect(
resolveFinalAssistantText({
finalText: "All done",
streamedText: "partial",
}),
).toBe("All done");
});
});

View File

@@ -39,6 +39,17 @@ export type TuiOptions = {
message?: string;
};
export function resolveFinalAssistantText(params: {
finalText?: string | null;
streamedText?: string | null;
}) {
const finalText = params.finalText ?? "";
if (finalText.trim()) return finalText;
const streamedText = params.streamedText ?? "";
if (streamedText.trim()) return streamedText;
return "(no output)";
}
type ChatEvent = {
runId: string;
sessionKey: string;
@@ -642,7 +653,11 @@ export async function runTui(opts: TuiOptions) {
const text = extractTextFromMessage(evt.message, {
includeThinking: showThinking,
});
chatLog.finalizeAssistant(text || "(no output)", evt.runId);
const finalText = resolveFinalAssistantText({
finalText: text,
streamedText: chatLog.getStreamingText(evt.runId),
});
chatLog.finalizeAssistant(finalText, evt.runId);
noteFinalizedRun(evt.runId);
activeChatRunId = null;
setActivityStatus("idle");