From a9a4b475666974923638b8265f76df8e50136f45 Mon Sep 17 00:00:00 2001 From: Callum Alpass Date: Tue, 3 Nov 2026 03:08:26 +1100 Subject: [PATCH] Fit streamed markdown tables to their content, and test that replies don't jump Replies already stream through ; tables now size to their cells with the theme's border colour. Tests cover tables mid-stream and settled, the newest line staying in view, and a scrolled-up view staying still. --- docs/reference.md | 2 +- src/ui/ThreadView.tsx | 2 +- test/app/fixtures.ts | 8 ++++++ test/app/harness.tsx | 13 +++++++-- test/app/streaming.test.tsx | 57 +++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 test/app/streaming.test.tsx diff --git a/docs/reference.md b/docs/reference.md index ebb0d7b..4da3f5e 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -96,7 +96,7 @@ Typing a trigger opens a list of suggestions above the box: The layout follows T3 Code's web app, and much of it uses the web app's own code. - **Sidebar:** the same sections as the app (Pinned, Active, Working, then Snoozed and Settled folded to a count) and the same status words: `Working 2m`, `Waiting`, `Approval`, `Input`, `Request`, `Limited`, `Failed`, `Done`. Threads that need you are bold. -- **Conversation:** each finished turn's work folds into one line (`Worked for 3m 12s`), your messages sit in a raised block with a bar down the left, and a running turn shows its live step and `Working for 12s`. Threads open scrolled to the bottom. Long threads load as a window of recent turns; press `o` for older ones. +- **Conversation:** each finished turn's work folds into one line (`Worked for 3m 12s`), your messages sit in a raised block with a bar down the left, and a running turn shows its live step and `Working for 12s`. Replies render as markdown while they stream, tables included, and settle into their final form when done; at the bottom the newest line stays in view, and scrolled up the text you're reading stays put. Threads open scrolled to the bottom. Long threads load as a window of recent turns; press `o` for older ones. - **Turns:** `[`/`]` select a turn and mark its prompt. `R` or `Revert to this turn` asks `Edit from here?`, then rewinds the chat and returns the prompt to the message box, keeping file changes. `Revert files too` in the palette also restores files, including your own edits. Stop a running turn first. Rewinding messages with attachments still needs the app. - **Command output:** expand work with `e` / `Expand all work`, or click a command. Output streams while its expanded row is in view; collapsing it, scrolling away or opening another thread stops it. - **Thread details:** a column beside the conversation with the workspace, model and mode, the branch with ahead/behind and its pull request, uncommitted changes per file, lineage (forks and subagents), background work and the goal. It opens by itself on terminals 160 columns or wider; on narrow ones `d` shows it in place of the conversation. diff --git a/src/ui/ThreadView.tsx b/src/ui/ThreadView.tsx index 5eea0b9..3f86cb5 100644 --- a/src/ui/ThreadView.tsx +++ b/src/ui/ThreadView.tsx @@ -179,7 +179,7 @@ const Row = memo(function Row(props: { if (message.role === "system") return isRightClick(event) && mouse.menu(menu)}>{message.text}; return ( isRightClick(event) && mouse.menu(menu)}> - + {summary && summary.files.length > 0 ? : null} {props.footer ? {props.footer} : null} diff --git a/test/app/fixtures.ts b/test/app/fixtures.ts index a8b2d57..7b57e55 100644 --- a/test/app/fixtures.ts +++ b/test/app/fixtures.ts @@ -166,3 +166,11 @@ export function withOmittedOutput(): OrchestrationV2ThreadProjection { visibleTurnItems: [{ position: 0, visibility: "local", sourceThreadId: omittedCommand.threadId, sourceItemId: omittedCommand.id, item: omittedCommand }], } as unknown as OrchestrationV2ThreadProjection; } + +/** The finished turn, its reply replaced by `text`, still streaming in if `streaming`. */ +export function withReply(text: string, streaming: boolean): OrchestrationV2ThreadProjection { + const turn = structuredClone(withCompletedTurn()) as unknown as { turnItems: Record[]; visibleTurnItems: { item: Record }[] }; + for (const item of turn.turnItems) if (item.type === "assistant_message") Object.assign(item, { text, streaming, status: streaming ? "running" : "completed" }); + for (const visible of turn.visibleTurnItems) visible.item = turn.turnItems.find((item) => item.id === visible.item.id)!; + return turn as unknown as OrchestrationV2ThreadProjection; +} diff --git a/test/app/harness.tsx b/test/app/harness.tsx index c3a9e03..961eaa3 100644 --- a/test/app/harness.tsx +++ b/test/app/harness.tsx @@ -51,6 +51,7 @@ export function fakeSession( } = {}, ) { const calls: Call[] = []; + const threadListeners = new Set<(value: unknown) => void>(); const snapshot = state.snapshot ?? v2ShellSnapshot; const projection = state.projection ?? v2Projection; const live = (value: T) => (listener: (value: T) => void) => { @@ -60,7 +61,11 @@ export function fakeSession( const known: Record = { getShell: () => ({ status: state.status ?? { type: "live" }, snapshot, serverUrl: "ws://test" }), onShell: () => () => {}, - watchThread: (_id: ThreadId, listener: (value: unknown) => void) => live({ status: "live", projection, history: { cursor: null, hasMore: false, loading: false } })(listener), + watchThread: (_id: ThreadId, listener: (value: unknown) => void) => { + threadListeners.add(listener); + listener({ status: "live", projection, history: { cursor: null, hasMore: false, loading: false } }); + return () => threadListeners.delete(listener); + }, watchServerConfig: live(state.serverConfig ? { status: "live", config: state.serverConfig } : { status: "loading", config: null }), watchServers: live({ status: "live", items: [] }), watchAutomations: live({ status: "live", items: [] }), @@ -85,7 +90,11 @@ export function fakeSession( }; }, }) as unknown as T3Session; - return { session, calls }; + /** Sends watchers of the thread a new projection, as the server does when it changes. */ + const updateThread = (next: OrchestrationV2ThreadProjection) => { + for (const listener of threadListeners) listener({ status: "live", projection: next, history: { cursor: null, hasMore: false, loading: false } }); + }; + return { session, calls, updateThread }; } /** Renders the whole app against a fake session, with helpers to drive it by key and mouse. */ diff --git a/test/app/streaming.test.tsx b/test/app/streaming.test.tsx new file mode 100644 index 0000000..d8dbf6e --- /dev/null +++ b/test/app/streaming.test.tsx @@ -0,0 +1,57 @@ +import { afterEach, describe, expect, test } from "bun:test"; + +import { withReply } from "./fixtures.ts"; +import { fakeSession, renderApp } from "./harness.tsx"; + +let app: Awaited> | null = null; +const open = async (...args: Parameters) => (app = await renderApp(...args)); +afterEach(() => { + app?.destroy(); + app = null; +}); + +const table = "| Name | Value |\n| --- | --- |\n| alpha | 1 |\n| beta | a longer cell |\n"; +const paragraphs = (from: number, to: number) => Array.from({ length: to - from }, (_, index) => `Paragraph ${from + index} of the answer.\n\n`).join(""); +/** The frame without its scrollbar column, which moves as the content grows. */ +const content = (frame: string) => frame.split("\n").map((line) => line.slice(0, -3)).join("\n"); + +describe("streaming replies", () => { + test("tables render as tables, while streaming and once settled", async () => { + const fake = fakeSession({ projection: withReply(`Here:\n\n${table.slice(0, 40)}`, true) }); + const app = await open({ session: fake }); + expect(app.frame()).toContain("│ Name │ Value │"); + fake.updateThread(withReply(`Here:\n\n${table}\nAfter.`, false)); + await app.settle(50); + expect(app.frame()).toContain("│ beta │ a longer cell │"); + expect(app.frame()).not.toContain("| --- |"); + }); + + test("at the bottom, a long answer keeps its newest line in view", async () => { + const fake = fakeSession({ projection: withReply("", true) }); + const app = await open({ session: fake, height: 30 }); + for (let count = 10; count <= 60; count += 10) { + fake.updateThread(withReply(paragraphs(0, count), true)); + await app.settle(); + expect(app.frame()).toContain(`Paragraph ${count - 1} of the answer.`); + } + }); + + test("scrolled up, the text in view stays put as the answer grows and settles", async () => { + let text = paragraphs(0, 40); + const fake = fakeSession({ projection: withReply(text, true) }); + const app = await open({ session: fake, height: 30 }); + await app.mockMouse.scroll(60, 10, "up"); + await app.mockMouse.scroll(60, 10, "up"); + await app.settle(30); + const before = content(app.frame()); + for (let more = 40; more < 50; more++) { + text += `Paragraph ${more} of the answer.\n\n`; + fake.updateThread(withReply(text, true)); + await app.settle(); + expect(content(app.frame())).toBe(before); + } + fake.updateThread(withReply(`${text}${table}`, false)); + await app.settle(30); + expect(content(app.frame())).toBe(before); + }); +});