xonecas.io
64875
Mc

mini-coder / commit 9caf27f

feat: render markdown in streaming assistant output

Add src/cli/markdown.ts with a lightweight block+inline renderer
(headings, bold, italic, inline code, fenced code blocks, lists,
blockquotes, horizontal rules) using yoctocolors.

Integrate into renderTurn() in output.ts via a line-buffered flush
strategy: completed lines are rendered through renderMarkdown() on each
delta; any remaining partial line is flushed at turn/tool boundaries.

xonecas2026-02-20 9caf27f10536d12d1e51c25d5368bbee898f8864 parent 879f01d

2 files changed

src/cli/markdown.ts+122−0
@@ -0,0 +1,122 @@
1+import * as c from "yoctocolors";
2+
3+// ─── Inline span renderer ─────────────────────────────────────────────────────
4+// Handles: `code`, **bold**, *italic* within a single line of text.
5+
6+function renderInline(text: string): string {
7+ let out = "";
8+ let i = 0;
9+
10+ while (i < text.length) {
11+ // Inline code: `...`
12+ if (text[i] === "`") {
13+ const end = text.indexOf("`", i + 1);
14+ if (end !== -1) {
15+ out += c.yellow(text.slice(i, end + 1));
16+ i = end + 1;
17+ continue;
18+ }
19+ }
20+
21+ // Bold: **...**
22+ if (text.slice(i, i + 2) === "**") {
23+ const end = text.indexOf("**", i + 2);
24+ if (end !== -1) {
25+ out += c.bold(text.slice(i + 2, end));
26+ i = end + 2;
27+ continue;
28+ }
29+ }
30+
31+ // Italic: *...* (not preceded by another *)
32+ if (text[i] === "*" && text[i - 1] !== "*" && text[i + 1] !== "*") {
33+ const end = text.indexOf("*", i + 1);
34+ if (end !== -1 && text[end - 1] !== "*") {
35+ out += c.dim(text.slice(i + 1, end));
36+ i = end + 1;
37+ continue;
38+ }
39+ }
40+
41+ out += text[i];
42+ i++;
43+ }
44+
45+ return out;
46+}
47+
48+// ─── Block renderer ───────────────────────────────────────────────────────────
49+
50+export function renderMarkdown(text: string): string {
51+ const lines = text.split("\n");
52+ const out: string[] = [];
53+ let inFence = false;
54+
55+ for (const raw of lines) {
56+ // Fenced code block toggle (``` or ~~~)
57+ if (/^(`{3,}|~{3,})/.test(raw)) {
58+ inFence = !inFence;
59+ // Show the fence line dimmed (language hint visible but subdued)
60+ out.push(c.dim(raw));
61+ continue;
62+ }
63+
64+ // Inside fenced block — yellow, no inline processing
65+ if (inFence) {
66+ out.push(c.yellow(raw));
67+ continue;
68+ }
69+
70+ // Horizontal rule: --- or *** or ===
71+ if (/^(-{3,}|\*{3,}|={3,})\s*$/.test(raw)) {
72+ out.push(c.dim("─".repeat(40)));
73+ continue;
74+ }
75+
76+ // Headings
77+ const h3 = raw.match(/^(#{3,})\s+(.*)/);
78+ if (h3) {
79+ out.push(c.bold(renderInline(h3[2] ?? "")));
80+ continue;
81+ }
82+ const h2 = raw.match(/^##\s+(.*)/);
83+ if (h2) {
84+ out.push(c.bold(c.cyan(renderInline(h2[1] ?? ""))));
85+ continue;
86+ }
87+ const h1 = raw.match(/^#\s+(.*)/);
88+ if (h1) {
89+ out.push(c.bold(c.cyan(renderInline(h1[1] ?? ""))));
90+ continue;
91+ }
92+
93+ // Blockquote: > ...
94+ const bq = raw.match(/^>\s?(.*)/);
95+ if (bq) {
96+ out.push(c.dim(`│ ${renderInline(bq[1] ?? "")}`));
97+ continue;
98+ }
99+
100+ // Unordered list: - or * or +
101+ const ul = raw.match(/^(\s*)[*\-+]\s+(.*)/);
102+ if (ul) {
103+ const indent = ul[1] ?? "";
104+ out.push(`${indent}${c.dim("·")} ${renderInline(ul[2] ?? "")}`);
105+ continue;
106+ }
107+
108+ // Ordered list: 1. ...
109+ const ol = raw.match(/^(\s*)(\d+)\.\s+(.*)/);
110+ if (ol) {
111+ const indent = ol[1] ?? "";
112+ const num = ol[2] ?? "";
113+ out.push(`${indent}${c.dim(`${num}.`)} ${renderInline(ol[3] ?? "")}`);
114+ continue;
115+ }
116+
117+ // Plain text (inline spans still applied)
118+ out.push(renderInline(raw));
119+ }
120+
121+ return out.join("\n");
122+}
src/cli/output.ts+34−1
@@ -1,5 +1,6 @@
11 import * as c from "yoctocolors";
22 import type { TurnEvent } from "../llm-api/types.ts";
3+import { renderMarkdown } from "./markdown.ts";
34
45 // ─── Terminal restore ─────────────────────────────────────────────────────────
56
@@ -393,11 +394,39 @@ export async function renderTurn(
393394 newMessages: import("../llm-api/turn.ts").CoreMessage[];
394395 }> {
395396 let inText = false;
397+ // Full text accumulated so far — needed for correct markdown context
398+ // (e.g. knowing whether we're inside a fenced code block).
399+ let textBuffer = "";
400+ // Tracks how many characters of textBuffer have already been written to
401+ // stdout so we only write new content on each delta.
402+ let writtenLen = 0;
396403 let inputTokens = 0;
397404 let outputTokens = 0;
398405 let contextTokens = 0;
399406 let newMessages: import("../llm-api/turn.ts").CoreMessage[] = [];
400407
408+ // Flush all completed lines (everything up to the last newline) through
409+ // renderMarkdown and write only the newly rendered portion to stdout.
410+ function flushLines(): void {
411+ const lastNl = textBuffer.lastIndexOf("\n");
412+ if (lastNl < writtenLen) return; // no new complete lines
413+ const fullRendered = renderMarkdown(textBuffer.slice(0, lastNl + 1));
414+ const alreadyRendered = renderMarkdown(textBuffer.slice(0, writtenLen));
415+ const newOutput = fullRendered.slice(alreadyRendered.length);
416+ if (newOutput) write(newOutput);
417+ writtenLen = lastNl + 1;
418+ }
419+
420+ // Flush any remaining buffered text (including incomplete last line).
421+ function flushText(): void {
422+ if (writtenLen >= textBuffer.length) return;
423+ const fullRendered = renderMarkdown(textBuffer);
424+ const alreadyRendered = renderMarkdown(textBuffer.slice(0, writtenLen));
425+ const newOutput = fullRendered.slice(alreadyRendered.length);
426+ if (newOutput) write(newOutput);
427+ writtenLen = textBuffer.length;
428+ }
429+
401430 for await (const event of events) {
402431 switch (event.type) {
403432 case "text-delta": {
@@ -406,12 +435,14 @@ export async function renderTurn(
406435 process.stdout.write(`${G.reply} `);
407436 inText = true;
408437 }
409- write(event.delta);
438+ textBuffer += event.delta;
439+ flushLines();
410440 break;
411441 }
412442
413443 case "tool-call-start": {
414444 if (inText) {
445+ flushText();
415446 writeln();
416447 inText = false;
417448 }
@@ -430,6 +461,7 @@ export async function renderTurn(
430461
431462 case "turn-complete": {
432463 if (inText) {
464+ flushText();
433465 writeln();
434466 inText = false;
435467 }
@@ -443,6 +475,7 @@ export async function renderTurn(
443475
444476 case "turn-error": {
445477 if (inText) {
478+ flushText();
446479 writeln();
447480 inText = false;
448481 }