xonecas.io
64875
Mc

mini-coder / commit f0bf109

refactor(cli): extract streamed preview rendering state

xonecas2026-03-14 f0bf1095313916c53034fd70bcd6890dda9dd207 parent 09bcb3f

2 files changed

src/cli/stream-preview.ts+83−0
@@ -0,0 +1,83 @@
1+import { visibleLength } from "./terminal-width.ts";
2+
3+interface PartialPreviewTracker {
4+ partialWritten: number;
5+ partialVisibleChars: number;
6+ streamPrefix: string;
7+ streamPrefixWritten: boolean;
8+}
9+
10+export function createPartialPreviewTracker(): PartialPreviewTracker {
11+ return {
12+ partialWritten: 0,
13+ partialVisibleChars: 0,
14+ streamPrefix: "",
15+ streamPrefixWritten: false,
16+ };
17+}
18+
19+export function resetPartialPreviewTracker(
20+ tracker: PartialPreviewTracker,
21+): void {
22+ tracker.partialWritten = 0;
23+ tracker.partialVisibleChars = 0;
24+ tracker.streamPrefix = "";
25+ tracker.streamPrefixWritten = false;
26+}
27+
28+export function setStreamPreviewPrefix(
29+ tracker: PartialPreviewTracker,
30+ prefix: string,
31+ written: boolean,
32+): void {
33+ tracker.streamPrefix = prefix;
34+ tracker.streamPrefixWritten = written;
35+ if (written && prefix) {
36+ tracker.partialVisibleChars += visibleLength(prefix);
37+ }
38+}
39+
40+function estimateWrappedRows(
41+ visibleChars: number,
42+ terminalColumns: number,
43+): number {
44+ if (visibleChars <= 0) return 1;
45+ if (terminalColumns <= 0) return 1;
46+ return Math.max(1, Math.ceil(visibleChars / terminalColumns));
47+}
48+
49+export function buildClearPartialPreview(
50+ tracker: PartialPreviewTracker,
51+ terminalColumns: number,
52+): string {
53+ if (tracker.partialWritten <= 0) return "";
54+ const rows = estimateWrappedRows(
55+ tracker.partialVisibleChars,
56+ terminalColumns,
57+ );
58+ let seq = "\r\x1b[2K";
59+ for (let i = 1; i < rows; i++) {
60+ seq += "\x1b[1A\r\x1b[2K";
61+ }
62+ return seq;
63+}
64+
65+export function streamPartialPreviewDelta(
66+ tracker: PartialPreviewTracker,
67+ rawBuffer: string,
68+): string {
69+ if (rawBuffer.length <= tracker.partialWritten) return "";
70+
71+ let out = "";
72+ if (!tracker.streamPrefixWritten && tracker.streamPrefix) {
73+ out += tracker.streamPrefix;
74+ tracker.streamPrefixWritten = true;
75+ tracker.partialVisibleChars += visibleLength(tracker.streamPrefix);
76+ }
77+
78+ const delta = rawBuffer.slice(tracker.partialWritten);
79+ out += delta;
80+ tracker.partialVisibleChars += visibleLength(delta);
81+ tracker.partialWritten = rawBuffer.length;
82+ return out;
83+}
src/cli/stream-render.ts+23−57
@@ -9,7 +9,13 @@ import {
99 normalizeReasoningText,
1010 } from "./reasoning.ts";
1111 import type { Spinner } from "./spinner.ts";
12-import { visibleLength } from "./terminal-width.ts";
12+import {
13+ buildClearPartialPreview,
14+ createPartialPreviewTracker,
15+ resetPartialPreviewTracker,
16+ setStreamPreviewPrefix,
17+ streamPartialPreviewDelta,
18+} from "./stream-preview.ts";
1319 import { renderToolCall, renderToolResult } from "./tool-render.ts";
1420
1521 export async function renderTurn(
@@ -28,25 +34,11 @@ export async function renderTurn(
2834 let inReasoning = false;
2935 // rawBuffer holds the current incomplete (no trailing \n) line being built.
3036 let rawBuffer = "";
31- // How many characters of rawBuffer have already been written to the terminal
32- // as a raw partial line (before markdown rendering). When a newline arrives
33- // the partial is overwritten with the properly styled complete line.
34- let partialWritten = 0;
35- // Approximate visible character count shown for the streamed partial preview
36- // (including any streamed prefix). Used to clear wrapped terminal rows.
37- let partialVisibleChars = 0;
38- // Prefix shown before raw partial content during streaming previews.
39- // For text replies: "◆ " (already written before streaming starts, so
40- // streamPartialLine skips it). For reasoning: "│ " (written by
41- // streamPartialLine since renderSingleLine doesn't emit a separate write).
42- let streamPrefix = "";
43- // Whether streamPrefix has already been written to the terminal.
44- // True for text replies; false for reasoning (streamPartialLine emits it).
45- let streamPrefixWritten = false;
4637 // Prefix prepended when overwriting the raw partial with the styled complete
4738 // line. For text replies: "◆ " (renderSingleLine output doesn't include it).
4839 // For reasoning: "" (renderSingleLine already includes "│ " in its output).
4940 let styledPrefix = "";
41+ const partialPreview = createPartialPreviewTracker();
5042 let accumulatedText = "";
5143 let accumulatedReasoning = "";
5244
@@ -80,31 +72,11 @@ export async function renderTurn(
8072 return rendered.output;
8173 }
8274
83- function estimateWrappedRows(visibleChars: number): number {
84- if (visibleChars <= 0) return 1;
85- const cols = process.stdout.columns ?? 0;
86- if (cols <= 0) return 1;
87- return Math.max(1, Math.ceil(visibleChars / cols));
88- }
89-
90- function clearPartialPreview(): string {
91- if (partialWritten <= 0) return "";
92- const rows = estimateWrappedRows(partialVisibleChars);
93- let seq = "\r\x1b[2K";
94- for (let i = 1; i < rows; i++) {
95- seq += "\x1b[1A\r\x1b[2K";
96- }
97- return seq;
98- }
99-
10075 // Reset per-line partial-preview tracking. styledPrefix is intentionally
10176 // excluded — it is turn-level state (◆ for text, "" for reasoning) and must
10277 // persist across lines within the same turn.
10378 function resetLineState(): void {
104- partialWritten = 0;
105- partialVisibleChars = 0;
106- streamPrefix = "";
107- streamPrefixWritten = false;
79+ resetPartialPreviewTracker(partialPreview);
10880 }
10981
11082 /**
@@ -119,10 +91,13 @@ export async function renderTurn(
11991 spinner.stop();
12092 const out = renderSingleLine(rawBuffer);
12193 rawBuffer = "";
122- if (partialWritten > 0) {
94+ if (partialPreview.partialWritten > 0) {
12395 // Raw partial content is on the terminal — clear wrapped rows and
12496 // replace it with the properly styled version.
125- const clearSeq = clearPartialPreview();
97+ const clearSeq = buildClearPartialPreview(
98+ partialPreview,
99+ process.stdout.columns ?? 0,
100+ );
126101 if (out !== null) write(`${clearSeq}${styledPrefix}${out}`);
127102 else write(clearSeq);
128103 } else {
@@ -163,8 +138,11 @@ export async function renderTurn(
163138 const out = renderSingleLine(raw);
164139 // If raw partial content was already streamed to the terminal, clear it
165140 // and replace with the properly styled complete-line render.
166- if (firstLine && partialWritten > 0) {
167- const clearSeq = clearPartialPreview();
141+ if (firstLine && partialPreview.partialWritten > 0) {
142+ const clearSeq = buildClearPartialPreview(
143+ partialPreview,
144+ process.stdout.columns ?? 0,
145+ );
168146 if (out !== null) batchOutput += `${clearSeq}${styledPrefix}${out}\n`;
169147 else batchOutput += `${clearSeq}\n`;
170148 } else if (out !== null) {
@@ -189,18 +167,9 @@ export async function renderTurn(
189167 * it hasn't been written yet (e.g. "│ " gutter for reasoning).
190168 */
191169 function streamPartialLine(): void {
192- if (rawBuffer.length <= partialWritten) return;
170+ const out = streamPartialPreviewDelta(partialPreview, rawBuffer);
171+ if (!out) return;
193172 spinner.stop();
194- let out = "";
195- if (!streamPrefixWritten && streamPrefix) {
196- out += streamPrefix;
197- streamPrefixWritten = true;
198- partialVisibleChars += visibleLength(streamPrefix);
199- }
200- const delta = rawBuffer.slice(partialWritten);
201- out += delta;
202- partialVisibleChars += visibleLength(delta);
203- partialWritten = rawBuffer.length;
204173 write(out);
205174 }
206175
@@ -215,10 +184,8 @@ export async function renderTurn(
215184 write(`${G.reply} `);
216185 // "◆ " is already on the terminal. Record it for overwrite
217186 // restoration and count it toward wrapped-row clearing.
218- streamPrefix = `${G.reply} `;
187+ setStreamPreviewPrefix(partialPreview, `${G.reply} `, true);
219188 styledPrefix = `${G.reply} `;
220- streamPrefixWritten = true;
221- partialVisibleChars += visibleLength(streamPrefix);
222189 inText = true;
223190 }
224191 rawBuffer += event.delta;
@@ -246,9 +213,8 @@ export async function renderTurn(
246213 inFence = false; // reasoning is never markdown; reset fence state at boundary
247214 // streamPrefix provides the 2-space indent for the raw partial
248215 // preview; renderSingleLine applies the same indent for completed lines.
249- streamPrefix = " ";
216+ setStreamPreviewPrefix(partialPreview, " ", false);
250217 styledPrefix = "";
251- streamPrefixWritten = false;
252218 }
253219 rawBuffer += delta;
254220 flushCompleteLines();