xonecas.io
64875
Mc

mini-coder / commit b185a90

chore: maintenance and code review

xonecas2026-09-22 b185a90561b4b8db5662283716b3ca77efa1bdfb parent c4dabd7

10 files changed

AGENTS.md+6−4
@@ -64,7 +64,8 @@ are references. Extract ideas, do not port code.
6464
6565 Global config only, at `$XDG_CONFIG_HOME/mini-coder/config.json`
6666 (default `~/.config/mini-coder/config.json`). No project-local config, no
67-config-override flags. A missing file is valid and uses defaults.
67+config-override flags. A missing file is valid and uses defaults, except for
68+`provider` and `model`, which are required.
6869
6970 Because `pi-ai` owns the provider catalog, auth env vars, base URLs, wire APIs,
7071 and compat detection, model config stays thin. A built-in provider needs only
@@ -78,9 +79,10 @@ Fields and defaults:
7879 - `discoverAgentFiles` — include discovered `AGENTS.md`/`CLAUDE.md`. Default `true`.
7980 - `skillsDirs` — directories scanned for `SKILL.md`. Default `[]` (no skills).
8081 - `tools` — enabled tool names. Default `["edit", "bash"]`.
81-- `provider` — built-in `pi-ai` provider id or a `customProviders[].id`.
82- Default `"anthropic"`.
83-- `model` — model id within that provider. Default `"claude-sonnet-4-5"`.
82+- `provider` — **required.** Built-in `pi-ai` provider id or a
83+ `customProviders[].id`. No default; a config without it is an error.
84+- `model` — **required.** Model id within that provider. No default; mini-coder
85+ never picks a model.
8486 - `thinkingEffort` — one of `minimal`, `low`, `medium`, `high`, `xhigh`, `max`.
8587 Default `"medium"`.
8688 - `customProviders` — array; default `[]`. Each entry:
src/agent.ts+1−1
@@ -12,7 +12,7 @@ import {
1212 type UserMessage,
1313 } from "@earendil-works/pi-ai";
1414 import type { Session } from "./session.ts";
15-import { executeTool, type ToolDetails, type ToolResult } from "./tools.ts";
15+import { executeTool, type ToolDetails, type ToolResult } from "./tools/index.ts";
1616 import type { ToolName } from "./config.ts";
1717
1818 export type Phase = "preparing" | "waitingModel" | "streaming" | "runningTool" | "pausing" | "idle";
src/cli.ts+1−1
@@ -11,7 +11,7 @@ import type {
1111 } from "@earendil-works/pi-ai";
1212 import { loadConfig, resolveModel, type ToolName } from "./config.ts";
1313 import { buildSystemPrompt } from "./prompt.ts";
14-import { toolSchemas } from "./tools.ts";
14+import { toolSchemas } from "./tools/index.ts";
1515 import { Session } from "./session.ts";
1616 import { NO_INTERACTION, runAgentTurn } from "./agent.ts";
1717 import { runTui } from "./tui/tui.ts";
src/config.ts+2−2
@@ -48,8 +48,8 @@ const ConfigSchema = Type.Object(
4848 discoverAgentFiles: Type.Boolean({ default: true }),
4949 skillsDirs: Type.Array(Type.String(), { default: [] }),
5050 tools: Type.Array(ToolNameSchema, { default: ["edit", "bash"] }),
51- provider: Type.String({ default: "anthropic" }),
52- model: Type.String({ default: "claude-sonnet-4-5" }),
51+ provider: Type.String(),
52+ model: Type.String(),
5353 thinkingEffort: Type.Union(
5454 [
5555 Type.Literal("minimal"),
src/tools.ts+0−183
@@ -1,183 +0,0 @@
1-import { spawn } from "node:child_process";
2-import { existsSync, readFileSync, writeFileSync } from "node:fs";
3-import { Type, type Static, type TSchema, type Tool, type ToolCall } from "@earendil-works/pi-ai";
4-import { Value } from "typebox/value";
5-import { createTwoFilesPatch } from "diff";
6-import type { ToolName } from "./config.ts";
7-
8-export interface ToolContext {
9- signal: AbortSignal;
10- onOutput?: (chunk: string) => void;
11-}
12-
13-export interface ToolDetails {
14- truncated: boolean;
15- omittedChars: number;
16- totalChars: number;
17-}
18-
19-export interface ToolResult {
20- text: string;
21- isError: boolean;
22- details?: ToolDetails;
23-}
24-
25-const EDIT_PARAMS = Type.Object(
26- {
27- path: Type.String({ description: "File path" }),
28- oldText: Type.String({ description: "Exact text to replace; empty to create a file" }),
29- newText: Type.String({ description: "Replacement text" }),
30- },
31- { additionalProperties: false },
32-);
33-type EditArgs = Static<typeof EDIT_PARAMS>;
34-
35-const BASH_PARAMS = Type.Object(
36- { command: Type.String({ description: "Command to run" }) },
37- { additionalProperties: false },
38-);
39-type BashArgs = Static<typeof BASH_PARAMS>;
40-
41-const TOOL_SCHEMAS: Record<ToolName, Tool> = {
42- edit: {
43- name: "edit",
44- description:
45- "Edit a file by exact text replacement. oldText must occur exactly once. " +
46- "With empty oldText, create a new file (fails if it exists).",
47- parameters: EDIT_PARAMS,
48- },
49- bash: {
50- name: "bash",
51- description: "Run a bash command in the current working directory.",
52- parameters: BASH_PARAMS,
53- },
54-};
55-
56-export function toolSchemas(names: ToolName[]): Tool[] {
57- return names.map((name) => TOOL_SCHEMAS[name]);
58-}
59-
60-export function executeTool(name: ToolName, call: ToolCall, ctx: ToolContext): Promise<ToolResult> {
61- try {
62- if (name === "edit") return Promise.resolve(edit(parseArgs(EDIT_PARAMS, call.arguments), ctx.signal));
63- return bash(parseArgs(BASH_PARAMS, call.arguments), ctx);
64- } catch (error) {
65- return Promise.resolve({ text: (error as Error).message, isError: true });
66- }
67-}
68-
69-function parseArgs<T extends TSchema>(schema: T, value: unknown): Static<T> {
70- try {
71- return Value.Parse(schema, value);
72- } catch {
73- const first = [...Value.Errors(schema, value)][0];
74- throw new Error(first ? `${first.instancePath || "/"} ${first.message}` : "invalid arguments");
75- }
76-}
77-
78-function edit(args: EditArgs, signal: AbortSignal): ToolResult {
79- const { path, oldText, newText } = args;
80- const exists = existsSync(path);
81-
82- if (oldText === "") {
83- if (exists) return { text: `edit failed: ${path} already exists`, isError: true };
84- if (signal.aborted) return { text: "edit cancelled", isError: true };
85- writeFileSync(path, newText);
86- return { text: `created ${path}\n${unified(path, "", newText)}`, isError: false };
87- }
88-
89- if (!exists) return { text: `edit failed: ${path} does not exist`, isError: true };
90- const before = readFileSync(path, "utf8");
91- const count = before.split(oldText).length - 1;
92- if (count === 0) return { text: `edit failed: oldText not found in ${path}`, isError: true };
93- if (count > 1) return { text: `edit failed: oldText matches ${count} times in ${path}`, isError: true };
94-
95- const after = before.replace(oldText, newText);
96- if (signal.aborted) return { text: "edit cancelled", isError: true };
97- writeFileSync(path, after);
98- return { text: `edited ${path}\n${unified(path, before, after)}`, isError: false };
99-}
100-
101-function unified(path: string, before: string, after: string): string {
102- return createTwoFilesPatch(path, path, before, after, "", "", { context: 3 });
103-}
104-
105-const MAX_HEAD = 10_000;
106-const MAX_TAIL = 6_000;
107-const TRUNCATED = "\n\n... output truncated ...\n\n";
108-
109-function bash(args: BashArgs, ctx: ToolContext): Promise<ToolResult> {
110- return new Promise((resolve) => {
111- const child = spawn("bash", ["-c", args.command], {
112- cwd: process.cwd(),
113- detached: true,
114- stdio: ["ignore", "pipe", "pipe"],
115- });
116-
117- let head = "";
118- let tail = "";
119- let omitted = 0;
120- const append = (chunk: Buffer) => {
121- let text = chunk.toString();
122- ctx.onOutput?.(text);
123- if (head.length < MAX_HEAD) {
124- const take = Math.min(MAX_HEAD - head.length, text.length);
125- head += text.slice(0, take);
126- text = text.slice(take);
127- }
128- if (text === "") return;
129- tail += text;
130- if (tail.length > MAX_TAIL) {
131- omitted += tail.length - MAX_TAIL;
132- tail = tail.slice(tail.length - MAX_TAIL);
133- }
134- };
135- child.stdout?.on("data", append);
136- child.stderr?.on("data", append);
137-
138- let settled = false;
139- const finish = (code: number | null) => {
140- if (settled) return;
141- settled = true;
142- ctx.signal.removeEventListener("abort", onAbort);
143- clearTimeout(killTimer);
144- const exit = code ?? (ctx.signal.aborted ? "aborted" : "unknown");
145- const truncated = omitted > 0;
146- const body = truncated ? head + TRUNCATED + tail : head + tail;
147- const text = body + (body.endsWith("\n") || body === "" ? "" : "\n") + `exit code: ${exit}`;
148- resolve({
149- text,
150- isError: code !== 0,
151- details: truncated
152- ? { truncated: true, omittedChars: omitted, totalChars: head.length + tail.length + omitted }
153- : undefined,
154- });
155- };
156-
157- let killTimer: NodeJS.Timeout | undefined;
158- const onAbort = () => {
159- if (!child.pid) return;
160- try {
161- process.kill(-child.pid, "SIGTERM");
162- } catch {
163- /* already gone */
164- }
165- killTimer = setTimeout(() => {
166- try {
167- process.kill(-child.pid!, "SIGKILL");
168- } catch {
169- /* already gone */
170- }
171- }, 300);
172- };
173-
174- if (ctx.signal.aborted) onAbort();
175- else ctx.signal.addEventListener("abort", onAbort, { once: true });
176-
177- child.on("error", (error) => {
178- append(Buffer.from(`${head || tail ? "\n" : ""}bash failed: ${error.message}\n`));
179- finish(null);
180- });
181- child.on("close", (code) => finish(code));
182- });
183-}
src/tools/bash.ts+89−0
@@ -0,0 +1,89 @@
1+import { spawn } from "node:child_process";
2+import { Type, type Static } from "@earendil-works/pi-ai";
3+import type { ToolContext, ToolResult } from "./common.ts";
4+
5+export const BASH_PARAMS = Type.Object(
6+ { command: Type.String({ description: "Command to run" }) },
7+ { additionalProperties: false },
8+);
9+export type BashArgs = Static<typeof BASH_PARAMS>;
10+
11+const MAX_HEAD = 10_000;
12+const MAX_TAIL = 6_000;
13+const TRUNCATED = "\n\n... output truncated ...\n\n";
14+
15+export function bash(args: BashArgs, ctx: ToolContext): Promise<ToolResult> {
16+ return new Promise((resolve) => {
17+ const child = spawn("bash", ["-c", args.command], {
18+ cwd: process.cwd(),
19+ detached: true,
20+ stdio: ["ignore", "pipe", "pipe"],
21+ });
22+
23+ let head = "";
24+ let tail = "";
25+ let omitted = 0;
26+ const append = (chunk: Buffer) => {
27+ let text = chunk.toString();
28+ ctx.onOutput?.(text);
29+ if (head.length < MAX_HEAD) {
30+ const take = Math.min(MAX_HEAD - head.length, text.length);
31+ head += text.slice(0, take);
32+ text = text.slice(take);
33+ }
34+ if (text === "") return;
35+ tail += text;
36+ if (tail.length > MAX_TAIL) {
37+ omitted += tail.length - MAX_TAIL;
38+ tail = tail.slice(tail.length - MAX_TAIL);
39+ }
40+ };
41+ child.stdout?.on("data", append);
42+ child.stderr?.on("data", append);
43+
44+ let settled = false;
45+ const finish = (code: number | null) => {
46+ if (settled) return;
47+ settled = true;
48+ ctx.signal.removeEventListener("abort", onAbort);
49+ clearTimeout(killTimer);
50+ const exit = code ?? (ctx.signal.aborted ? "aborted" : "unknown");
51+ const truncated = omitted > 0;
52+ const body = truncated ? head + TRUNCATED + tail : head + tail;
53+ const text = body + (body.endsWith("\n") || body === "" ? "" : "\n") + `exit code: ${exit}`;
54+ resolve({
55+ text,
56+ isError: code !== 0,
57+ details: truncated
58+ ? { truncated: true, omittedChars: omitted, totalChars: head.length + tail.length + omitted }
59+ : undefined,
60+ });
61+ };
62+
63+ let killTimer: NodeJS.Timeout | undefined;
64+ const onAbort = () => {
65+ if (!child.pid) return;
66+ try {
67+ process.kill(-child.pid, "SIGTERM");
68+ } catch {
69+ /* already gone */
70+ }
71+ killTimer = setTimeout(() => {
72+ try {
73+ process.kill(-child.pid!, "SIGKILL");
74+ } catch {
75+ /* already gone */
76+ }
77+ }, 300);
78+ };
79+
80+ if (ctx.signal.aborted) onAbort();
81+ else ctx.signal.addEventListener("abort", onAbort, { once: true });
82+
83+ child.on("error", (error) => {
84+ append(Buffer.from(`${head || tail ? "\n" : ""}bash failed: ${error.message}\n`));
85+ finish(null);
86+ });
87+ child.on("close", (code) => finish(code));
88+ });
89+}
src/tools/common.ts+28−0
@@ -0,0 +1,28 @@
1+import type { Static, TSchema } from "@earendil-works/pi-ai";
2+import { Value } from "typebox/value";
3+
4+export interface ToolContext {
5+ signal: AbortSignal;
6+ onOutput?: (chunk: string) => void;
7+}
8+
9+export interface ToolDetails {
10+ truncated: boolean;
11+ omittedChars: number;
12+ totalChars: number;
13+}
14+
15+export interface ToolResult {
16+ text: string;
17+ isError: boolean;
18+ details?: ToolDetails;
19+}
20+
21+export function parseArgs<T extends TSchema>(schema: T, value: unknown): Static<T> {
22+ try {
23+ return Value.Parse(schema, value);
24+ } catch {
25+ const first = [...Value.Errors(schema, value)][0];
26+ throw new Error(first ? `${first.instancePath || "/"} ${first.message}` : "invalid arguments");
27+ }
28+}
src/tools/edit.ts+41−0
@@ -0,0 +1,41 @@
1+import { existsSync, readFileSync, writeFileSync } from "node:fs";
2+import { Type, type Static } from "@earendil-works/pi-ai";
3+import { createTwoFilesPatch } from "diff";
4+import type { ToolResult } from "./common.ts";
5+
6+export const EDIT_PARAMS = Type.Object(
7+ {
8+ path: Type.String({ description: "File path" }),
9+ oldText: Type.String({ description: "Exact text to replace; empty to create a file" }),
10+ newText: Type.String({ description: "Replacement text" }),
11+ },
12+ { additionalProperties: false },
13+);
14+export type EditArgs = Static<typeof EDIT_PARAMS>;
15+
16+export function edit(args: EditArgs, signal: AbortSignal): ToolResult {
17+ const { path, oldText, newText } = args;
18+ const exists = existsSync(path);
19+
20+ if (oldText === "") {
21+ if (exists) return { text: `edit failed: ${path} already exists`, isError: true };
22+ if (signal.aborted) return { text: "edit cancelled", isError: true };
23+ writeFileSync(path, newText);
24+ return { text: `created ${path}\n${unified(path, "", newText)}`, isError: false };
25+ }
26+
27+ if (!exists) return { text: `edit failed: ${path} does not exist`, isError: true };
28+ const before = readFileSync(path, "utf8");
29+ const count = before.split(oldText).length - 1;
30+ if (count === 0) return { text: `edit failed: oldText not found in ${path}`, isError: true };
31+ if (count > 1) return { text: `edit failed: oldText matches ${count} times in ${path}`, isError: true };
32+
33+ const after = before.replace(oldText, newText);
34+ if (signal.aborted) return { text: "edit cancelled", isError: true };
35+ writeFileSync(path, after);
36+ return { text: `edited ${path}\n${unified(path, before, after)}`, isError: false };
37+}
38+
39+function unified(path: string, before: string, after: string): string {
40+ return createTwoFilesPatch(path, path, before, after, "", "", { context: 3 });
41+}
src/tools/index.ts+35−0
@@ -0,0 +1,35 @@
1+import type { Tool, ToolCall } from "@earendil-works/pi-ai";
2+import type { ToolName } from "../config.ts";
3+import { parseArgs, type ToolContext, type ToolResult } from "./common.ts";
4+import { BASH_PARAMS, bash } from "./bash.ts";
5+import { EDIT_PARAMS, edit } from "./edit.ts";
6+
7+export type { ToolContext, ToolDetails, ToolResult } from "./common.ts";
8+
9+const TOOL_SCHEMAS: Record<ToolName, Tool> = {
10+ edit: {
11+ name: "edit",
12+ description:
13+ "Edit a file by exact text replacement. oldText must occur exactly once. " +
14+ "With empty oldText, create a new file (fails if it exists).",
15+ parameters: EDIT_PARAMS,
16+ },
17+ bash: {
18+ name: "bash",
19+ description: "Run a bash command in the current working directory.",
20+ parameters: BASH_PARAMS,
21+ },
22+};
23+
24+export function toolSchemas(names: ToolName[]): Tool[] {
25+ return names.map((name) => TOOL_SCHEMAS[name]);
26+}
27+
28+export function executeTool(name: ToolName, call: ToolCall, ctx: ToolContext): Promise<ToolResult> {
29+ try {
30+ if (name === "edit") return Promise.resolve(edit(parseArgs(EDIT_PARAMS, call.arguments), ctx.signal));
31+ return bash(parseArgs(BASH_PARAMS, call.arguments), ctx);
32+ } catch (error) {
33+ return Promise.resolve({ text: (error as Error).message, isError: true });
34+ }
35+}
src/tui/tui.ts+1−1
@@ -34,7 +34,7 @@ const SPINNER_MS = 120;
3434
3535 /**
3636 * Display-only elision for tool bodies, deliberately worded differently from
37- * the model-facing `... output truncated ...` marker in `tools.ts`.
37+ * the model-facing `... output truncated ...` marker in `tools/bash.ts`.
3838 */
3939 const MAX_BODY_ROWS = 12;
4040 const ELIDED_HEAD = 4;