xonecas.io
64875
Mc

mini-coder / commit 86932d9

feat: syntax highlighting for markdown agent messages

xonecas2026-04-23 86932d94c693db48b9a5ea154efe18a219dcad7d parent b789630

4 files changed

bun.lock+15−0
@@ -5,6 +5,7 @@
55 "": {
66 "name": "mini-coder-next",
77 "dependencies": {
8+ "@cel-tui/components": "^0.8.1",
89 "@cel-tui/core": "^0.8.1",
910 "@mariozechner/pi-ai": "^0.68.1",
1011 },
@@ -111,6 +112,10 @@
111112
112113 "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@2.4.12", "", { "os": "win32", "cpu": "x64" }, "sha512-yMckRzTyZ83hkk8iDFWswqSdU8tvZxspJKnYNh7JZr/zhZNOlzH13k4ecboU6MurKExCe2HUkH75pGI/O2JwGA=="],
113114
115+ "@cel-tui/clew": ["@cel-tui/clew@0.8.1", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-4X021mpOQYSPR2HzmSMoxSX3wn6zoGpCgUPePdJPTy/IQe0N0vRLooDVZr74uVaUt8UxHLYUrH4WN7DuJq/YqA=="],
116+
117+ "@cel-tui/components": ["@cel-tui/components@0.8.1", "", { "dependencies": { "@cel-tui/clew": "0.8.1", "@cel-tui/core": "0.8.1", "@cel-tui/types": "0.8.1", "yoctomarkdown": "^0.1.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-lAtTk0ECLTrcQrXEtRCrY2/o+nRNP3ygItRcBe1TSa4Je4saAW5FjBRzqbOO/lQ0zhdhaxoWugMdKMRZHcc/Ww=="],
118+
114119 "@cel-tui/core": ["@cel-tui/core@0.8.1", "", { "dependencies": { "@cel-tui/types": "0.8.1", "get-east-asian-width": "^1.5.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-ShYX/p7YaD4gCOEEZA7wKxOUQn0NblB1YGEvxqvrZi2TxgA3arUh/+o287RnPYJN5idDNHr0Obe3Aaio6YaJkQ=="],
115120
116121 "@cel-tui/types": ["@cel-tui/types@0.8.1", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-Z0ehxq2f0nnV5Ia4af7xs0WgqTp9/okfmNVusTG+G5/tfcnH7bTMKbLECLmokx/MqF+a4Gcw0wwpBz3obghFeA=="],
@@ -245,6 +250,8 @@
245250
246251 "ajv-formats": ["ajv-formats@3.0.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ=="],
247252
253+ "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="],
254+
248255 "ast-types": ["ast-types@0.13.4", "", { "dependencies": { "tslib": "^2.0.1" } }, "sha512-x1FCFnFifvYDDzTaLII71vG5uvDwgtmDTEVWAxrgeiR8VjMONcCXJx7E+USjDtHlwFmt9MysbqgF9b9Vjr6w+w=="],
249256
250257 "base64-js": ["base64-js@1.5.1", "", {}, "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="],
@@ -365,6 +372,10 @@
365372
366373 "source-map": ["source-map@0.6.1", "", {}, "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="],
367374
375+ "string-width": ["string-width@8.2.0", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-6hJPQ8N0V0P3SNmP6h2J99RLuzrWz2gvT7VnK5tKvrNqJoyS9W4/Fb8mo31UiPvy00z7DQXkP2hnKBVav76thw=="],
376+
377+ "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="],
378+
368379 "strnum": ["strnum@2.2.3", "", {}, "sha512-oKx6RUCuHfT3oyVjtnrmn19H1SiCqgJSg+54XqURKp5aCMbrXrhLjRN9TjuwMjiYstZ0MzDrHqkGZ5dFTKd+zg=="],
369380
370381 "ts-algebra": ["ts-algebra@2.0.0", "", {}, "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw=="],
@@ -381,6 +392,10 @@
381392
382393 "ws": ["ws@8.20.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-sAt8BhgNbzCtgGbt2OxmpuryO63ZoDk/sqaB/znQm94T4fCEsy/yV+7CdC1kJhOU9lboAEU7R3kquuycDoibVA=="],
383394
395+ "yoctocolors": ["yoctocolors@2.1.2", "", {}, "sha512-CzhO+pFNo8ajLM2d2IW/R93ipy99LWjtwblvC1RsoSUMZgyLbYFr221TnSNT7GjGdYui6P459mw9JH/g/zW2ug=="],
396+
397+ "yoctomarkdown": ["yoctomarkdown@0.1.0", "", { "dependencies": { "string-width": "^8.2.0", "strip-ansi": "^7.2.0", "yoctocolors": "^2.1.2" }, "peerDependencies": { "typescript": "^5.9.3" }, "bin": { "yoctomarkdown": "src/cli.ts" } }, "sha512-N5ccZ2dQA3ZJSbImSeSxqfDHC/aBcyPWc5WI57corl4gB2Vsr4W11jswPhVNNI6R49ajFDNBNLLIqJTp5KtcQA=="],
398+
384399 "zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
385400
386401 "zod-to-json-schema": ["zod-to-json-schema@3.25.2", "", { "peerDependencies": { "zod": "^3.25.28 || ^4" } }, "sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA=="],
package.json+1−0
@@ -20,6 +20,7 @@
2020 "typescript": "^6.0.3"
2121 },
2222 "dependencies": {
23+ "@cel-tui/components": "^0.8.1",
2324 "@cel-tui/core": "^0.8.1",
2425 "@mariozechner/pi-ai": "^0.68.1"
2526 }
src/tui-conversation.ts+27−13
@@ -1,29 +1,33 @@
11 import { cel, HStack, Text, VStack } from "@cel-tui/core";
22 import { TextPill, theme } from "./tui-components";
3+import { memoizedSyntaxHighlight } from "./tui-syntax-highlight";
34 import type { TUIState } from "./types";
45
5-function timeAgo(timestamp: number): string {
6- const seconds = Math.floor((Date.now() - timestamp) / 1000);
7-
8- if (seconds < 60) return `${seconds}s ago`;
6+function elapsedTime(seconds: number): string {
7+ if (seconds < 60) return `${seconds}s`;
98
109 const minutes = Math.floor(seconds / 60);
11- if (minutes < 60) return `${minutes}m ago`;
10+ if (minutes < 60) return `${minutes}m`;
1211
1312 const hours = Math.floor(minutes / 60);
14- if (hours < 24) return `${hours}h ago`;
13+ if (hours < 24) return `${hours}h`;
1514
1615 const days = Math.floor(hours / 24);
17- if (days < 7) return `${days}d ago`;
16+ if (days < 7) return `${days}d`;
1817
1918 const weeks = Math.floor(days / 7);
20- if (weeks < 4) return `${weeks}w ago`;
19+ if (weeks < 4) return `${weeks}w`;
2120
2221 const months = Math.floor(days / 30);
23- if (months < 12) return `${months}mo ago`;
22+ if (months < 12) return `${months}mo`;
2423
2524 const years = Math.floor(days / 365);
26- return `${years}y ago`;
25+ return `${years}y`;
26+}
27+
28+function relativeTime(timestamp: number): string {
29+ const seconds = Math.floor((Date.now() - timestamp) / 1000);
30+ return elapsedTime(seconds);
2731 }
2832
2933 export function Conversation(state: TUIState) {
@@ -41,11 +45,21 @@ export function Conversation(state: TUIState) {
4145 },
4246 [
4347 ...state.messages.map((msg) => {
44- const dur = msg.durationMs ? `Took ${msg.durationMs / 1000}s` : "-";
48+ const dur = msg.durationMs
49+ ? `Took ${elapsedTime(msg.durationMs / 1000)}`
50+ : "-";
4551 return VStack({ gap: 1 }, [
46- Text(msg.content, { wrap: "word" }),
52+ // TODO: `@cel-tui/components` only keeps **4 cached highlight states per language/theme** (`MAX_STATES_PER_KEY = 4` in the
53+ // installed package). After the 4th agent reply, older messages will be re-tokenized from scratch on every render.
54+ msg.role === "agent"
55+ ? memoizedSyntaxHighlight(
56+ msg.id ?? `${Date.now()}`,
57+ msg.content,
58+ "markdown",
59+ )
60+ : Text(msg.content, { wrap: "word" }),
4761 HStack({ gap: 1, justifyContent: "end" }, [
48- Text(timeAgo(msg.timestamp), {
62+ Text(`${relativeTime(msg.timestamp)} ago.`, {
4963 fgColor: theme.bblack,
5064 italic: true,
5165 }),
src/tui-syntax-highlight.ts+61−0
@@ -0,0 +1,61 @@
1+import {
2+ SyntaxHighlight,
3+ type SyntaxHighlightProps,
4+} from "@cel-tui/components";
5+import type { ContainerNode } from "@cel-tui/types";
6+
7+type CacheEntry = {
8+ content: string;
9+ language: string;
10+ themeKey: string;
11+ node: ContainerNode;
12+};
13+
14+const cache = new Map<string, CacheEntry>();
15+const MAX_ENTRIES = 200;
16+
17+function getThemeKey(props?: SyntaxHighlightProps): string {
18+ return JSON.stringify(props?.theme ?? null);
19+}
20+
21+export function memoizedSyntaxHighlight(
22+ messageKey: string,
23+ content: string,
24+ language = "markdown",
25+ props?: SyntaxHighlightProps,
26+): ContainerNode {
27+ const themeKey = getThemeKey(props);
28+ const cached = cache.get(messageKey);
29+
30+ if (
31+ cached &&
32+ cached.content === content &&
33+ cached.language === language &&
34+ cached.themeKey === themeKey
35+ ) {
36+ return cached.node;
37+ }
38+
39+ const node = SyntaxHighlight(content, language, props);
40+
41+ cache.set(messageKey, {
42+ content,
43+ language,
44+ themeKey,
45+ node,
46+ });
47+
48+ // tiny FIFO cap so cache doesn't grow forever
49+ if (cache.size > MAX_ENTRIES) {
50+ const oldestKey = cache.keys().next().value;
51+ if (oldestKey !== undefined) {
52+ cache.delete(oldestKey);
53+ }
54+ }
55+
56+ return node;
57+}
58+
59+export function clearSyntaxHighlightCache(): void {
60+ cache.clear();
61+}