xonecas.io
64875
Mc

mini-coder / commit 4390963

feat: add skills support and general code cleanup

xonecas2026-04-24 439096369daf40fde474961fcb93a1c262fbbc6b parent 64c439a

9 files changed

AGENTS.md+5−0
@@ -5,6 +5,11 @@ Version `mini-coder@0.5.x` is currently stable and mostly complete, thats our re
55 Benchmarking from the start, keep the agent dumb, build the plugin-library-first architecture, then
66 run a an automated benchmarking loop to shape/hone the harness.
77
8+## Dog-fooding
9+
10+Be aware that your are mini-coder, running the dev version of this repository while editing it.
11+We are dog-fooding the code: changes to prompts, tools, context building, streaming, or runtime behavior may affect the agent we are actively using during the same development session.
12+
813 ## Core dependencies
914
1015 **https://github.com/badlogic/pi-mono/tree/main/packages/ai**
bun.lock+7−16
@@ -7,7 +7,8 @@
77 "dependencies": {
88 "@cel-tui/components": "^0.8.1",
99 "@cel-tui/core": "^0.8.1",
10- "@mariozechner/pi-ai": "^0.68.1",
10+ "@mariozechner/pi-ai": "^0.70.0",
11+ "yaml": "^2.8.3",
1112 },
1213 "devDependencies": {
1314 "@biomejs/biome": "^2.4.12",
@@ -122,7 +123,7 @@
122123
123124 "@google/genai": ["@google/genai@1.50.1", "", { "dependencies": { "google-auth-library": "^10.3.0", "p-retry": "^4.6.2", "protobufjs": "^7.5.4", "ws": "^8.18.0" }, "peerDependencies": { "@modelcontextprotocol/sdk": "^1.25.2" }, "optionalPeers": ["@modelcontextprotocol/sdk"] }, "sha512-YbkX7H9+1Pt8wOt7DDREy8XSoiL6fRDzZQRyaVBarFf8MR3zHGqVdvM4cLbDXqPhxqvegZShgfxb8kw9C7YhAQ=="],
124125
125- "@mariozechner/pi-ai": ["@mariozechner/pi-ai@0.68.1", "", { "dependencies": { "@anthropic-ai/sdk": "^0.90.0", "@aws-sdk/client-bedrock-runtime": "^3.1030.0", "@google/genai": "^1.40.0", "@mistralai/mistralai": "^2.2.0", "@sinclair/typebox": "^0.34.41", "ajv": "^8.17.1", "ajv-formats": "^3.0.1", "chalk": "^5.6.2", "openai": "6.26.0", "partial-json": "^0.1.7", "proxy-agent": "^6.5.0", "undici": "^7.19.1", "zod-to-json-schema": "^3.24.6" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-z52kMOZK1 …
126+ "@mariozechner/pi-ai": ["@mariozechner/pi-ai@0.70.0", "", { "dependencies": { "@anthropic-ai/sdk": "^0.90.0", "@aws-sdk/client-bedrock-runtime": "^3.1030.0", "@google/genai": "^1.40.0", "@mistralai/mistralai": "^2.2.0", "chalk": "^5.6.2", "openai": "6.26.0", "partial-json": "^0.1.7", "proxy-agent": "^6.5.0", "typebox": "^1.1.24", "undici": "^7.19.1", "zod-to-json-schema": "^3.24.6" }, "bin": { "pi-ai": "dist/cli.js" } }, "sha512-lVT9bb0eFkNr5YXvZ5r00TNA5r110fOO8uJV9VLCQ5GdtunWIjcptWitzIjjl2M …
126127
127128 "@mistralai/mistralai": ["@mistralai/mistralai@2.2.0", "", { "dependencies": { "ws": "^8.18.0", "zod": "^3.25.0 || ^4.0.0", "zod-to-json-schema": "^3.25.0" } }, "sha512-JQUGIXjFWnw/J9LpTSf/ZXwVW3Sh8FBAcfTo5QvAHqkl4CfSiIwnjRJhMoAFcP6ncCe84YPU1ncDGX+p3OXnfg=="],
128129
@@ -146,8 +147,6 @@
146147
147148 "@protobufjs/utf8": ["@protobufjs/utf8@1.1.0", "", {}, "sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw=="],
148149
149- "@sinclair/typebox": ["@sinclair/typebox@0.34.49", "", {}, "sha512-brySQQs7Jtn0joV8Xh9ZV/hZb9Ozb0pmazDIASBkYKCjXrXU3mpcFahmK/z4YDhGkQvP9mWJbVyahdtU5wQA+A=="],
150-
151150 "@smithy/config-resolver": ["@smithy/config-resolver@4.4.17", "", { "dependencies": { "@smithy/node-config-provider": "^4.3.14", "@smithy/types": "^4.14.1", "@smithy/util-config-provider": "^4.2.2", "@smithy/util-endpoints": "^3.4.2", "@smithy/util-middleware": "^4.2.14", "tslib": "^2.6.2" } }, "sha512-TzDZcAnhTyAHbXVxWZo7/tEcrIeFq20IBk8So3OLOetWpR8EwY/yEqBMBFaJMeyEiREDq4NfEl+qO3OAUD+vbQ=="],
152151
153152 "@smithy/core": ["@smithy/core@3.23.16", "", { "dependencies": { "@smithy/protocol-http": "^5.3.14", "@smithy/types": "^4.14.1", "@smithy/url-parser": "^4.2.14", "@smithy/util-base64": "^4.3.2", "@smithy/util-body-length-browser": "^4.2.2", "@smithy/util-middleware": "^4.2.14", "@smithy/util-stream": "^4.5.24", "@smithy/util-utf8": "^4.2.2", "@smithy/uuid": "^1.1.2", "tslib": "^2.6.2" } }, "sha512-JStomOrINQA1VqNEopLsgcdgwd42au7mykKqVr30XFw89wLt9sDxJDi4djVPRwQmmzyTGy/uOvTc2ultMpFi1w=="],
@@ -246,10 +245,6 @@
246245
247246 "agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],
248247
249- "ajv": ["ajv@8.18.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A=="],
250-
251- "ajv-formats": ["ajv-formats@3.0.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ=="],
252-
253248 "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="],
254249
255250 "ast-types": ["ast-types@0.13.4", "", { "dependencies": { "tslib": "^2.0.1" } }, "sha512-x1FCFnFifvYDDzTaLII71vG5uvDwgtmDTEVWAxrgeiR8VjMONcCXJx7E+USjDtHlwFmt9MysbqgF9b9Vjr6w+w=="],
@@ -286,10 +281,6 @@
286281
287282 "extend": ["extend@3.0.2", "", {}, "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="],
288283
289- "fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
290-
291- "fast-uri": ["fast-uri@3.1.0", "", {}, "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA=="],
292-
293284 "fast-xml-builder": ["fast-xml-builder@1.1.5", "", { "dependencies": { "path-expression-matcher": "^1.1.3" } }, "sha512-4TJn/8FKLeslLAH3dnohXqE3QSoxkhvaMzepOIZytwJXZO69Bfz0HBdDHzOTOon6G59Zrk6VQ2bEiv1t61rfkA=="],
294285
295286 "fast-xml-parser": ["fast-xml-parser@5.5.8", "", { "dependencies": { "fast-xml-builder": "^1.1.4", "path-expression-matcher": "^1.2.0", "strnum": "^2.2.0" }, "bin": { "fxparser": "src/cli/cli.js" } }, "sha512-Z7Fh2nVQSb2d+poDViM063ix2ZGt9jmY1nWhPfHBOK2Hgnb/OW3P4Et3P/81SEej0J7QbWtJqxO05h8QYfK7LQ=="],
@@ -320,8 +311,6 @@
320311
321312 "json-schema-to-ts": ["json-schema-to-ts@3.1.1", "", { "dependencies": { "@babel/runtime": "^7.18.3", "ts-algebra": "^2.0.0" } }, "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g=="],
322313
323- "json-schema-traverse": ["json-schema-traverse@1.0.0", "", {}, "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="],
324-
325314 "jwa": ["jwa@2.0.1", "", { "dependencies": { "buffer-equal-constant-time": "^1.0.1", "ecdsa-sig-formatter": "1.0.11", "safe-buffer": "^5.0.1" } }, "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg=="],
326315
327316 "jws": ["jws@4.0.1", "", { "dependencies": { "jwa": "^2.0.1", "safe-buffer": "^5.0.1" } }, "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA=="],
@@ -358,8 +347,6 @@
358347
359348 "proxy-from-env": ["proxy-from-env@1.1.0", "", {}, "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="],
360349
361- "require-from-string": ["require-from-string@2.0.2", "", {}, "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw=="],
362-
363350 "retry": ["retry@0.13.1", "", {}, "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg=="],
364351
365352 "safe-buffer": ["safe-buffer@5.2.1", "", {}, "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="],
@@ -382,6 +369,8 @@
382369
383370 "tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
384371
372+ "typebox": ["typebox@1.1.33", "", {}, "sha512-+/MWwlQ1q2GSVwoxi/+u5JsHkgLQKcCN2Nsjree9c+K7GJu40qbaHrFETmfV1i9Fs1TcOVfynW+jJvIWcXtvjw=="],
373+
385374 "typescript": ["typescript@6.0.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw=="],
386375
387376 "undici": ["undici@7.25.0", "", {}, "sha512-xXnp4kTyor2Zq+J1FfPI6Eq3ew5h6Vl0F/8d9XU5zZQf1tX9s2Su1/3PiMmUANFULpmksxkClamIZcaUqryHsQ=="],
@@ -392,6 +381,8 @@
392381
393382 "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=="],
394383
384+ "yaml": ["yaml@2.8.3", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-AvbaCLOO2Otw/lW5bmh9d/WEdcDFdQp2Z2ZUH3pX9U2ihyUY0nvLv7J6TrWowklRGPYbB/IuIMfYgxaCPg5Bpg=="],
385+
395386 "yoctocolors": ["yoctocolors@2.1.2", "", {}, "sha512-CzhO+pFNo8ajLM2d2IW/R93ipy99LWjtwblvC1RsoSUMZgyLbYFr221TnSNT7GjGdYui6P459mw9JH/g/zW2ug=="],
396387
397388 "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=="],
package.json+2−1
@@ -22,6 +22,7 @@
2222 "dependencies": {
2323 "@cel-tui/components": "^0.8.1",
2424 "@cel-tui/core": "^0.8.1",
25- "@mariozechner/pi-ai": "^0.68.1"
25+ "@mariozechner/pi-ai": "^0.70.0",
26+ "yaml": "^2.8.3"
2627 }
2728 }
src/agent.ts+73−5
@@ -1,3 +1,4 @@
1+import { readdir } from "node:fs/promises";
12 import { homedir } from "node:os";
23 import { join } from "node:path";
34 import {
@@ -9,6 +10,7 @@ import {
910 streamSimple,
1011 type ToolResultMessage,
1112 } from "@mariozechner/pi-ai";
13+import { parseSkillFrontmatter } from "./shared";
1214 import type { CliOptions, ToolAndRunner } from "./types";
1315
1416 export const TASK_PROMPT = `# You are "mini-coder", an elite coding agent.
@@ -40,7 +42,76 @@ export async function getAGENTSFiles() {
4042 content.push(await localFile.text());
4143 }
4244
43- return content.join("\n\n");
45+ return content.join("\n\n").trim();
46+}
47+
48+// `SKILLS.md` discovery from [~.]/agents/skills/*/SKILL.md
49+export async function getSkills(): Promise<string> {
50+ let skillsBlock: string = "";
51+ const skillRoots = [
52+ join(homedir(), ".agents", "skills"),
53+ join(process.cwd(), ".agents", "skills"),
54+ ];
55+
56+ for (const root of skillRoots) {
57+ let entries: string[];
58+
59+ try {
60+ entries = await readdir(root);
61+ } catch {
62+ continue;
63+ }
64+
65+ for (const entry of entries) {
66+ const path = join(root, entry, "SKILL.md");
67+ const file = Bun.file(path);
68+
69+ if (!(await file.exists())) {
70+ continue;
71+ }
72+
73+ const parsed = parseSkillFrontmatter(await file.text());
74+
75+ if (!parsed) {
76+ continue;
77+ }
78+
79+ skillsBlock += `## ${parsed.name}
80+
81+> File: ${path}
82+
83+${parsed.description}
84+
85+`;
86+ }
87+ }
88+
89+ if (!skillsBlock.length) return "";
90+
91+ const skills = `# Skills
92+
93+The following skills provide specialized instructions for specific tasks.
94+Use the shell tool to read a skill's file when the task matches its description.
95+
96+${skillsBlock}`;
97+
98+ return skills.trim();
99+}
100+
101+export async function buildSystemPrompt(systemPrompt: string) {
102+ const agentsContent = await getAGENTSFiles();
103+ const skillsContent = await getSkills();
104+ let complete = systemPrompt;
105+
106+ if (skillsContent) {
107+ complete += `\n${skillsContent}`;
108+ }
109+
110+ if (agentsContent) {
111+ complete += `\n${agentsContent}`;
112+ }
113+
114+ return complete;
44115 }
45116
46117 export async function streamAgent(
@@ -53,11 +124,8 @@ export async function streamAgent(
53124 toolsFn?: (tool: ToolResultMessage) => void,
54125 completeFn?: (msg: AssistantMessage, context: Context) => void,
55126 ) {
56- const agentsContent = await getAGENTSFiles();
57- const completeSystemPrompt = `${systemPrompt}\n${agentsContent}`;
58-
59127 const context: Context = {
60- systemPrompt: completeSystemPrompt,
128+ systemPrompt: await buildSystemPrompt(systemPrompt),
61129 messages,
62130 tools: tools.map((t) => t.tool),
63131 };
src/args.ts+2−2
@@ -19,13 +19,13 @@ function getModelConfig(modelName: string, provider: KnownProvider) {
1919 // TODO: For when the user set's thinking value.
2020 // function _isThinkingLevel(value: string): value is ThinkingLevel {
2121 // const THINKING_LEVELS = ["low", "medium", "minimal", "high", "xhigh"]
22-// return (THINKING_LEVELS as readonly string[]).includes(value);
22+// return (THINKING_LEVELS as readkonly string[]).includes(value);
2323 // }
2424
2525 export async function handleArgv(argv: string[]): Promise<CliOptions> {
2626 // TODO: Fetch defaults from settings file
2727 let options: CliOptions = {
28- model: getModelConfig("gpt-5.4", "openai-codex"),
28+ model: getModelConfig("gpt-5.5", "openai-codex"),
2929 effort: "xhigh" as ThinkingLevel,
3030 };
3131
src/shared.ts+29−0
@@ -1,5 +1,8 @@
11 import { homedir } from "node:os";
22 import { join } from "node:path";
3+import { parseDocument } from "yaml";
4+
5+// Mixed bag of helpers that can be shared accross the codebase
36
47 export const DATA_DIR = join(homedir(), ".config", "mini-coder");
58 export const AUTH_PATH = join(DATA_DIR, "auth.json");
@@ -77,3 +80,29 @@ export function takeTail<T>(arr: T[], x: number): T[] {
7780 export function extimateTokens(text: string): number {
7881 return Math.ceil(text.length / 4);
7982 }
83+
84+export function parseSkillFrontmatter(content: string) {
85+ const match = /^---\s*\n([\s\S]*?)\n---/.exec(content);
86+
87+ if (!match) {
88+ return undefined;
89+ }
90+
91+ const doc = parseDocument(match[1]);
92+ const data = doc.toJS() as unknown;
93+
94+ if (!data || typeof data !== "object") {
95+ return undefined;
96+ }
97+
98+ const record = data as Record<string, unknown>;
99+ const name = typeof record.name === "string" ? record.name.trim() : "";
100+ const description =
101+ typeof record.description === "string" ? record.description.trim() : "";
102+
103+ if (!name || !description) {
104+ return undefined;
105+ }
106+
107+ return { name, description };
108+}
src/tui-components.ts+47−2
@@ -1,4 +1,5 @@
11 import { type Color, HStack, Text } from "@cel-tui/core";
2+import { onceEvery } from "./shared";
23
34 export const theme = {
45 black: "color00" as Color,
@@ -26,8 +27,6 @@ export const theme = {
2627 bwhite: "color15" as Color,
2728 };
2829
29-// Examples:
30-//
3130 // TextPill("mini-coder", theme.bblack, theme.black),
3231 // TextPill("mini-coder", theme.bred, theme.red),
3332 // TextPill("mini-coder", theme.bgreen, theme.green),
@@ -43,3 +42,49 @@ export function TextPill(content: string, fgColor: Color, bgColor: Color) {
4342 ]),
4443 ]);
4544 }
45+
46+export function Spinner() {
47+ const spinnerFrames = [
48+ "⠁",
49+ "⠂",
50+ "⠄",
51+ "⡀",
52+ "⡈",
53+ "⡐",
54+ "⡠",
55+ "⣀",
56+ "⣁",
57+ "⣂",
58+ "⣄",
59+ "⣌",
60+ "⣔",
61+ "⣤",
62+ "⣥",
63+ "⣦",
64+ "⣮",
65+ "⣶",
66+ "⣷",
67+ "⣿",
68+ "⡿",
69+ "⠿",
70+ "⢟",
71+ "⠟",
72+ "⡛",
73+ "⠛",
74+ "⠫",
75+ "⢋",
76+ "⠋",
77+ "⠍",
78+ "⡉",
79+ "⠉",
80+ "⠑",
81+ "⠡",
82+ "⢁",
83+ ];
84+ let spinnerTick = 0;
85+ const spinnerEvery = onceEvery(4, () => spinnerTick++);
86+ const currentSpinner = () =>
87+ spinnerFrames[spinnerTick % spinnerFrames.length];
88+
89+ return { spinnerEvery, currentSpinner };
90+}
src/tui-state.ts+69−0
@@ -0,0 +1,69 @@
1+import type { ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
2+import { extimateTokens, secureRandomString, takeTail } from "./shared";
3+import type { TUIMessage } from "./types";
4+
5+// Create the TUIState message for context tool call and result.
6+export function createTUIToolMessage(
7+ source: ToolCall | ToolResultMessage,
8+ existing?: Partial<TUIMessage>,
9+): TUIMessage {
10+ // This truncation is TUI only. The file is also
11+ // truncated at tool level to avoid big files being
12+ // sent to the llm. We truncate that even further for the user.
13+ // We only show the tail of the file, which includes if the file
14+ // was truncated at tool level to the user.
15+
16+ // Join text if there is more than one block.
17+ const showLines = 6;
18+ const content = "content" in source ? source.content : [];
19+ const text = content.length
20+ ? content
21+ .map((c) => (c.type === "text" ? c.text : ""))
22+ .filter((c) => Boolean(c))
23+ .join("\n")
24+ : (existing?.content ?? "");
25+ // Grab the tail
26+ let tail = takeTail(text.split("\n"), showLines).join("\n");
27+ // Some commands output without newlines tons of chars.
28+ // if needed to take roughly X lines at 100 chars worth of tail.
29+ if (tail.length > showLines * 100) {
30+ tail = tail.slice(showLines * 100 * -1);
31+ }
32+
33+ // Now the similar cut is needed in arguments, but here we care about
34+ // seeing the start of the command, like `cd bla/ && cat ...`
35+ const argsMaxLength = 100 * showLines; // estimaded by 100 char line width times X lines.
36+ let args = existing?.header ?? "Writting...";
37+
38+ // Shell tool call
39+ if ("arguments" in source && "command" in source.arguments) {
40+ args =
41+ source.arguments?.command?.length > argsMaxLength
42+ ? source.arguments.command.substring(0, argsMaxLength)
43+ : source.arguments.command;
44+ }
45+
46+ // Edit tool call
47+ if ("arguments" in source && "path" in source.arguments) {
48+ const before = source.arguments.oldText;
49+ const after = source.arguments.newText;
50+ const tokensWritten = extimateTokens(
51+ [source.arguments.path, before, after].join(""),
52+ );
53+ args = `Wrote ~${tokensWritten}t`;
54+ }
55+
56+ const msg: TUIMessage = {
57+ id: "id" in source ? source.id : (existing?.id ?? secureRandomString(8)),
58+ timestamp:
59+ "timestamp" in source
60+ ? source.timestamp
61+ : (existing?.timestamp ?? Date.now()),
62+ role: "tool",
63+ label: "name" in source ? source.name : (existing?.label ?? ""),
64+ header: args,
65+ content: tail,
66+ durationMs: existing?.durationMs ? existing.durationMs : 0,
67+ };
68+ return msg;
69+}
src/tui.ts+6−101
@@ -1,65 +1,24 @@
11 import { basename } from "node:path";
22 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
3-import type { Message, ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
3+import type { Message } from "@mariozechner/pi-ai";
44 import { streamAgent, TASK_PROMPT } from "./agent";
55 import { getApiKey } from "./oauth";
6-import { onceEvery, secureRandomString, takeTail } from "./shared";
6+import { secureRandomString } from "./shared";
77 import { bash, runBashTool } from "./tool-bash";
88 import { edit, runEditTool } from "./tool-edit";
9-import { TextPill, theme } from "./tui-components";
9+import { Spinner, TextPill, theme } from "./tui-components";
1010 import { Conversation } from "./tui-conversation";
1111 import { Editor } from "./tui-editor";
12+import { createTUIToolMessage } from "./tui-state";
1213 import type { ToolAndRunner, TUIMessage, TUIState } from "./types";
1314
1415 export function initTUI(state: TUIState, leave: (s: string) => void) {
1516 const cwd = basename(process.cwd());
16-
17- // TODO: move to components, have a function return the everyFn and current spinner
18- const spinnerFrames = [
19- "⠁",
20- "⠂",
21- "⠄",
22- "⡀",
23- "⡈",
24- "⡐",
25- "⡠",
26- "⣀",
27- "⣁",
28- "⣂",
29- "⣄",
30- "⣌",
31- "⣔",
32- "⣤",
33- "⣥",
34- "⣦",
35- "⣮",
36- "⣶",
37- "⣷",
38- "⣿",
39- "⡿",
40- "⠿",
41- "⢟",
42- "⠟",
43- "⡛",
44- "⠛",
45- "⠫",
46- "⢋",
47- "⠋",
48- "⠍",
49- "⡉",
50- "⠉",
51- "⠑",
52- "⠡",
53- "⢁",
54- ];
55- let spinnerTick = 0;
56- const spinnerEvery = onceEvery(4, () => spinnerTick++);
57- const currentSpinner = () =>
58- spinnerFrames[spinnerTick % spinnerFrames.length];
17+ const { spinnerEvery, currentSpinner } = Spinner();
5918
6019 // Because we don't render often, the ui feels unresponsive at times.
6120 // This ensure Xfps, and excessive calls get coalesced in cel-tui.
62- const fps = 30;
21+ const fps = 60;
6322 const baseFramerateIntervalId = setInterval(() => {
6423 if (state.streaming) {
6524 spinnerEvery();
@@ -120,60 +79,6 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
12079 );
12180 }
12281
123-// Create the TUIState message for context tool call and result.
124-function createTUIToolMessage(
125- source: ToolCall | ToolResultMessage,
126- existing?: Partial<TUIMessage>,
127-): TUIMessage {
128- // This truncation is TUI only. The file is also
129- // truncated at tool level to avoid big files being
130- // sent to the llm. We truncate that even further for the user.
131- // We only show the tail of the file, which includes if the file
132- // was truncated at tool level to the user.
133-
134- // Join text if there is more than one block.
135- const showLines = 6;
136- const content = "content" in source ? source.content : [];
137- const text = content.length
138- ? content
139- .map((c) => (c.type === "text" ? c.text : ""))
140- .filter((c) => Boolean(c))
141- .join("\n")
142- : (existing?.content ?? "");
143- // Grab the tail
144- let tail = takeTail(text.split("\n"), showLines).join("\n");
145- // Some commands output without newlines tons of chars.
146- // if needed to take roughly 6 lines at 100 chars worth of tail.
147- if (tail.length > showLines * 100) {
148- tail = tail.slice(showLines * 100 * -1);
149- }
150-
151- // Now the similar cut is needed in arguments, but here we care about
152- // seeing the start of the command, like `cd bla/ && cat ...`
153- const argsMaxLength = 600; // estimaded by 100 char line width x 10 lines.
154- let args = existing?.header ?? "Writting...";
155- if ("arguments" in source && "command" in source.arguments) {
156- args =
157- source.arguments?.command?.length > argsMaxLength
158- ? source.arguments.command.substring(0, argsMaxLength)
159- : source.arguments.command;
160- }
161-
162- const msg: TUIMessage = {
163- id: "id" in source ? source.id : (existing?.id ?? secureRandomString(8)),
164- timestamp:
165- "timestamp" in source
166- ? source.timestamp
167- : (existing?.timestamp ?? Date.now()),
168- role: "tool",
169- label: "name" in source ? source.name : (existing?.label ?? ""),
170- header: args,
171- content: tail,
172- durationMs: existing?.durationMs ? existing.durationMs : 0,
173- };
174- return msg;
175-}
176-
17782 export async function streamTUI(state: TUIState) {
17883 if (state.streaming) return;
17984 state.streaming = true;