fix: refactor TUI stream assembly (#1202, thanks @aaronveklabs)

Co-authored-by: Aaron <aaron@vektor-labs.com>
This commit is contained in:
Peter Steinberger
2026-01-20 08:34:44 +00:00
parent 9609a3af40
commit 074db1905a
8 changed files with 271 additions and 235 deletions

View File

@@ -1,6 +1,10 @@
import { describe, expect, it } from "vitest";
import { extractTextFromMessage } from "./tui-formatters.js";
import {
extractContentFromMessage,
extractTextFromMessage,
extractThinkingFromMessage,
} from "./tui-formatters.js";
describe("extractTextFromMessage", () => {
it("renders errorMessage when assistant content is empty", () => {
@@ -27,4 +31,60 @@ describe("extractTextFromMessage", () => {
expect(text).toContain("unknown error");
});
it("joins multiple text blocks with single newlines", () => {
const text = extractTextFromMessage({
role: "assistant",
content: [
{ type: "text", text: "first" },
{ type: "text", text: "second" },
],
});
expect(text).toBe("first\nsecond");
});
it("places thinking before content when included", () => {
const text = extractTextFromMessage(
{
role: "assistant",
content: [
{ type: "text", text: "hello" },
{ type: "thinking", thinking: "ponder" },
],
},
{ includeThinking: true },
);
expect(text).toBe("[thinking]\nponder\n\nhello");
});
});
describe("extractThinkingFromMessage", () => {
it("collects only thinking blocks", () => {
const text = extractThinkingFromMessage({
role: "assistant",
content: [
{ type: "thinking", thinking: "alpha" },
{ type: "text", text: "hello" },
{ type: "thinking", thinking: "beta" },
],
});
expect(text).toBe("alpha\nbeta");
});
});
describe("extractContentFromMessage", () => {
it("collects only text blocks", () => {
const text = extractContentFromMessage({
role: "assistant",
content: [
{ type: "thinking", thinking: "alpha" },
{ type: "text", text: "hello" },
],
});
expect(text).toBe("hello");
});
});