xonecas.io
64875
Mc

mini-coder / commit 26c61a3

perf: incremental markdown streaming — O(N) rendering, live partial lines

- Add renderLine(raw, inFence) to markdown.ts: renders a single source
  line given fence state, returns rendered output + updated fence state
- Refactor renderMarkdown to delegate to renderLine per-line, eliminating
  ~65 lines of duplicated rendering logic
- Rewrite renderTurn streaming state in output.ts:
  - Replace textBuffer/writtenLen (O(N²) double re-renders) with
    lines[]/flushedLines/inFence — each line rendered exactly once
  - Partial last line written via \r (overwrite) on every delta so
    content is visible immediately, not held until next newline
  - Partial line rendered through renderLine (not raw) so inline spans
    are styled live during streaming
  - flushText clears partialWritten before the early-return guard,
    fixing a latent bug where a stream ending on \n could leave
    orphaned text on the terminal line

xonecas2026-02-20 26c61a38bd08782bdaf52fec771f3a14bbaf26de parent e232079

2 files changed

src/cli/markdown.ts+73−70
@@ -63,76 +63,79 @@ function renderInline(text: string): string {
6363
6464 // ─── Block renderer ───────────────────────────────────────────────────────────
6565
66-export function renderMarkdown(text: string): string {
67- const lines = text.split("\n");
68- const out: string[] = [];
69- let inFence = false;
70-
71- for (const raw of lines) {
72- // Fenced code block toggle (``` or ~~~)
73- if (/^(`{3,}|~{3,})/.test(raw)) {
74- inFence = !inFence;
75- // Show the fence line dimmed (language hint visible but subdued)
76- out.push(c.dim(raw));
77- continue;
78- }
79-
80- // Inside fenced block — yellow, no inline processing
81- if (inFence) {
82- out.push(c.yellow(raw));
83- continue;
84- }
85-
86- // Horizontal rule: --- or *** or ===
87- if (/^(-{3,}|\*{3,}|={3,})\s*$/.test(raw)) {
88- out.push(c.dim("─".repeat(40)));
89- continue;
90- }
91-
92- // Headings
93- const h3 = raw.match(/^(#{3,})\s+(.*)/);
94- if (h3) {
95- out.push(c.bold(renderInline(h3[2] ?? "")));
96- continue;
97- }
98- const h2 = raw.match(/^##\s+(.*)/);
99- if (h2) {
100- out.push(c.bold(c.cyan(renderInline(h2[1] ?? ""))));
101- continue;
102- }
103- const h1 = raw.match(/^#\s+(.*)/);
104- if (h1) {
105- out.push(c.bold(c.cyan(renderInline(h1[1] ?? ""))));
106- continue;
107- }
108-
109- // Blockquote: > ...
110- const bq = raw.match(/^>\s?(.*)/);
111- if (bq) {
112- out.push(c.dim(`│ ${renderInline(bq[1] ?? "")}`));
113- continue;
114- }
115-
116- // Unordered list: - or * or +
117- const ul = raw.match(/^(\s*)[*\-+]\s+(.*)/);
118- if (ul) {
119- const indent = ul[1] ?? "";
120- out.push(`${indent}${c.dim("·")} ${renderInline(ul[2] ?? "")}`);
121- continue;
122- }
123-
124- // Ordered list: 1. ...
125- const ol = raw.match(/^(\s*)(\d+)\.\s+(.*)/);
126- if (ol) {
127- const indent = ol[1] ?? "";
128- const num = ol[2] ?? "";
129- out.push(`${indent}${c.dim(`${num}.`)} ${renderInline(ol[3] ?? "")}`);
130- continue;
131- }
132-
133- // Plain text (inline spans still applied)
134- out.push(renderInline(raw));
66+/**
67+ * Render a single source line given the current fence state.
68+ * Returns the rendered string and the updated fence state.
69+ * Use this for incremental streaming — call once per complete line.
70+ */
71+export function renderLine(
72+ raw: string,
73+ inFence: boolean,
74+): { output: string; inFence: boolean } {
75+ // Fenced code block toggle (``` or ~~~)
76+ if (/^(`{3,}|~{3,})/.test(raw)) {
77+ return { output: c.dim(raw), inFence: !inFence };
78+ }
79+ if (inFence) {
80+ return { output: c.yellow(raw), inFence: true };
13581 }
82+ // Horizontal rule: --- or *** or ===
83+ if (/^(-{3,}|\*{3,}|={3,})\s*$/.test(raw)) {
84+ return { output: c.dim("─".repeat(40)), inFence: false };
85+ }
86+ // Headings
87+ const h3 = raw.match(/^(#{3,})\s+(.*)/);
88+ if (h3) return { output: c.bold(renderInline(h3[2] ?? "")), inFence: false };
89+ const h2 = raw.match(/^##\s+(.*)/);
90+ if (h2)
91+ return {
92+ output: c.bold(c.cyan(renderInline(h2[1] ?? ""))),
93+ inFence: false,
94+ };
95+ const h1 = raw.match(/^#\s+(.*)/);
96+ if (h1)
97+ return {
98+ output: c.bold(c.cyan(renderInline(h1[1] ?? ""))),
99+ inFence: false,
100+ };
101+ // Blockquote: > ...
102+ const bq = raw.match(/^>\s?(.*)/);
103+ if (bq)
104+ return {
105+ output: c.dim(`│ ${renderInline(bq[1] ?? "")}`),
106+ inFence: false,
107+ };
108+ // Unordered list: - or * or +
109+ const ul = raw.match(/^(\s*)[*\-+]\s+(.*)/);
110+ if (ul) {
111+ const indent = ul[1] ?? "";
112+ return {
113+ output: `${indent}${c.dim("·")} ${renderInline(ul[2] ?? "")}`,
114+ inFence: false,
115+ };
116+ }
117+ // Ordered list: 1. ...
118+ const ol = raw.match(/^(\s*)(\d+)\.\s+(.*)/);
119+ if (ol) {
120+ const indent = ol[1] ?? "";
121+ const num = ol[2] ?? "";
122+ return {
123+ output: `${indent}${c.dim(`${num}.`)} ${renderInline(ol[3] ?? "")}`,
124+ inFence: false,
125+ };
126+ }
127+ // Plain text (inline spans still applied)
128+ return { output: renderInline(raw), inFence: false };
129+}
136130
137- return out.join("\n");
131+export function renderMarkdown(text: string): string {
132+ let inFence = false;
133+ return text
134+ .split("\n")
135+ .map((raw) => {
136+ const r = renderLine(raw, inFence);
137+ inFence = r.inFence;
138+ return r.output;
139+ })
140+ .join("\n");
138141 }
src/cli/output.ts+63−24
@@ -1,7 +1,7 @@
11 import { homedir } from "node:os";
22 import * as c from "yoctocolors";
33 import type { TurnEvent } from "../llm-api/types.ts";
4-import { renderMarkdown } from "./markdown.ts";
4+import { renderLine } from "./markdown.ts";
55
66 const HOME = homedir();
77
@@ -436,37 +436,67 @@ export async function renderTurn(
436436 newMessages: import("../llm-api/turn.ts").CoreMessage[];
437437 }> {
438438 let inText = false;
439- // Full text accumulated so far — needed for correct markdown context
440- // (e.g. knowing whether we're inside a fenced code block).
441- let textBuffer = "";
442- // Tracks how many characters of textBuffer have already been written to
443- // stdout so we only write new content on each delta.
444- let writtenLen = 0;
439+ // All source lines received so far (split on \n).
440+ // The last element is the in-progress partial line (may be "").
441+ const lines: string[] = [""];
442+ // How many complete lines (index 0..flushedLines-1) have already been
443+ // printed to stdout with a trailing \n.
444+ let flushedLines = 0;
445+ // Fence state at the start of lines[flushedLines] — updated as we flush.
446+ let inFence = false;
447+ // Whether the current partial line (lines[lines.length-1]) has been
448+ // written to stdout (without a trailing \n, so \r can overwrite it).
449+ let partialWritten = false;
445450 let inputTokens = 0;
446451 let outputTokens = 0;
447452 let contextTokens = 0;
448453 let newMessages: import("../llm-api/turn.ts").CoreMessage[] = [];
449454
450- // Flush all completed lines (everything up to the last newline) through
451- // renderMarkdown and write only the newly rendered portion to stdout.
455+ // Flush all newly complete lines (everything up to the last \n).
456+ // Each complete line is rendered exactly once via renderLine, keeping
457+ // inFence state consistent across calls — O(N) total work.
458+ // After flushing complete lines, write/overwrite the partial last line
459+ // as raw text so the user sees content immediately.
452460 function flushLines(): void {
453- const lastNl = textBuffer.lastIndexOf("\n");
454- if (lastNl < writtenLen) return; // no new complete lines
455- const fullRendered = renderMarkdown(textBuffer.slice(0, lastNl + 1));
456- const alreadyRendered = renderMarkdown(textBuffer.slice(0, writtenLen));
457- const newOutput = fullRendered.slice(alreadyRendered.length);
458- if (newOutput) write(newOutput);
459- writtenLen = lastNl + 1;
461+ const complete = lines.length - 1; // indices 0..complete-1 are complete
462+ // Render and print any newly complete lines.
463+ while (flushedLines < complete) {
464+ const raw = lines[flushedLines] ?? "";
465+ const result = renderLine(raw, inFence);
466+ // If a partial line was already written on this terminal line, clear
467+ // it first with \r before printing the final rendered version.
468+ if (partialWritten) {
469+ process.stdout.write("\r\x1B[2K");
470+ partialWritten = false;
471+ }
472+ write(`${result.output}\n`);
473+
474+ inFence = result.inFence;
475+ flushedLines++;
476+ }
477+ // Write/overwrite the partial last line (no \n), rendered so inline
478+ // spans (bold, code, italic) are visible as they stream in.
479+ const partial = lines[lines.length - 1] ?? "";
480+ if (partial) {
481+ const rendered = renderLine(partial, inFence).output;
482+ process.stdout.write(`\r${rendered}`);
483+ partialWritten = true;
484+ }
460485 }
461486
462- // Flush any remaining buffered text (including incomplete last line).
487+ // Finalize: overwrite the partial line with its fully-rendered version.
488+ // Callers are responsible for writing \n after this.
463489 function flushText(): void {
464- if (writtenLen >= textBuffer.length) return;
465- const fullRendered = renderMarkdown(textBuffer);
466- const alreadyRendered = renderMarkdown(textBuffer.slice(0, writtenLen));
467- const newOutput = fullRendered.slice(alreadyRendered.length);
468- if (newOutput) write(newOutput);
469- writtenLen = textBuffer.length;
490+ const partial = lines[lines.length - 1] ?? "";
491+ // Clear whatever is on-screen first — even if partial is now "" (e.g.
492+ // stream ended with \n), partialWritten may be true and must be cleared.
493+ if (partialWritten) {
494+ process.stdout.write("\r\x1B[2K");
495+ partialWritten = false;
496+ }
497+ if (!partial) return;
498+ const result = renderLine(partial, inFence);
499+ if (result.output) write(result.output);
470500 }
471501
472502 for await (const event of events) {
@@ -477,7 +507,16 @@ export async function renderTurn(
477507 process.stdout.write(`${G.reply} `);
478508 inText = true;
479509 }
480- textBuffer += event.delta;
510+ // Split the delta on newlines and append to our lines array.
511+ // Each \n in the delta completes the current partial line and
512+ // starts a new one.
513+ const parts = event.delta.split("\n");
514+ lines[lines.length - 1] =
515+ (lines[lines.length - 1] ?? "") + (parts[0] ?? "");
516+ for (let i = 1; i < parts.length; i++) {
517+ lines.push(parts[i] ?? "");
518+ }
519+
481520 flushLines();
482521 break;
483522 }