xonecas.io
64875
Mc

mini-coder / commit b714ade

chore: agent gardening, is that what I'm calling this?

Remove agent-isms, duplication, over engineering, etc...

xonecas2026-09-26 b714ade65a203472a1c02221eb578ef29e914413 parent e716b2d

7 files changed

src/agent.ts+17−6
@@ -58,9 +58,20 @@ interface AgentRun extends AgentOptions {
5858 onEvent: (event: AgentEvent) => void;
5959 }
6060
61+/** The concatenated text blocks of an assistant message, ignoring the rest. */
62+export function assistantText(message: AssistantMessage): string {
63+ return message.content
64+ .filter((block) => block.type === "text")
65+ .map((block) => block.text)
66+ .join("");
67+}
68+
6169 export async function runAgentTurn(run: AgentRun): Promise<void> {
6270 const { messages, session, signal, interaction, onEvent } = run;
6371
72+ const cancelled = (): void => onEvent({ type: "cancelled" });
73+ const failed = (message: string): void => onEvent({ type: "error", message });
74+
6475 /** Appends a user message the model reads at the next step boundary. */
6576 const steer = (content: string): void => {
6677 if (content === "") return;
@@ -91,7 +102,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
91102 for (;;) {
92103 const steering = await pauseStep();
93104 if (steering === null) {
94- onEvent({ type: "cancelled" });
105+ cancelled();
95106 return;
96107 }
97108 steer(steering);
@@ -116,7 +127,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
116127 sessionId: session.id ?? undefined,
117128 });
118129 } catch (error) {
119- onEvent({ type: "error", message: (error as Error).message });
130+ failed((error as Error).message);
120131 return;
121132 }
122133
@@ -138,7 +149,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
138149 }
139150 }
140151 } catch (error) {
141- onEvent({ type: "error", message: (error as Error).message });
152+ failed((error as Error).message);
142153 return;
143154 }
144155
@@ -148,11 +159,11 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
148159 onEvent({ type: "message", message: assistant });
149160
150161 if (assistant.stopReason === "aborted") {
151- onEvent({ type: "cancelled" });
162+ cancelled();
152163 return;
153164 }
154165 if (assistant.stopReason === "error") {
155- onEvent({ type: "error", message: assistant.errorMessage ?? "provider error" });
166+ failed(assistant.errorMessage ?? "provider error");
156167 return;
157168 }
158169
@@ -169,7 +180,7 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
169180 for (const call of toolCalls) {
170181 const steering = await pauseStep();
171182 if (steering === null) {
172- onEvent({ type: "cancelled" });
183+ cancelled();
173184 return;
174185 }
175186 if (steering !== "") held.push(steering);
src/cli.ts+2−5
@@ -4,7 +4,7 @@ import { loadConfig, resolveModel } from "./config.ts";
44 import { buildSystemPrompt } from "./prompt.ts";
55 import { acceptsImages, toolSchemas } from "./tools/index.ts";
66 import { Session } from "./session.ts";
7-import { NO_INTERACTION, runAgentTurn, type AgentOptions } from "./agent.ts";
7+import { NO_INTERACTION, assistantText, runAgentTurn, type AgentOptions } from "./agent.ts";
88 import { runTui } from "./tui/tui.ts";
99
1010 function parseArgs(argv: string[]): { print: string | null } {
@@ -61,10 +61,7 @@ async function runPrint(prompt: string, ctx: AgentOptions): Promise<number> {
6161
6262 const last = messages.filter((message): message is AssistantMessage => message.role === "assistant").at(-1);
6363 if (!failed && !cancelled && last !== undefined) {
64- const text = last.content
65- .filter((block) => block.type === "text")
66- .map((block) => block.text)
67- .join("");
64+ const text = assistantText(last);
6865 if (text !== "") process.stdout.write(text.endsWith("\n") ? text : `${text}\n`);
6966 }
7067 return failed || cancelled ? 1 : 0;
src/tools/bash.ts+7−10
@@ -61,20 +61,17 @@ export function bash(args: BashArgs, ctx: ToolContext): Promise<ToolResult> {
6161 };
6262
6363 let killTimer: NodeJS.Timeout | undefined;
64- const onAbort = () => {
65- if (!child.pid) return;
64+ const kill = (signal: NodeJS.Signals): void => {
6665 try {
67- process.kill(-child.pid, "SIGTERM");
66+ process.kill(-child.pid!, signal);
6867 } catch {
6968 /* already gone */
7069 }
71- killTimer = setTimeout(() => {
72- try {
73- process.kill(-child.pid!, "SIGKILL");
74- } catch {
75- /* already gone */
76- }
77- }, 300);
70+ };
71+ const onAbort = () => {
72+ if (!child.pid) return;
73+ kill("SIGTERM");
74+ killTimer = setTimeout(() => kill("SIGKILL"), 300);
7875 };
7976
8077 if (ctx.signal.aborted) onAbort();
src/tui/commands.ts+5−7
@@ -9,7 +9,7 @@ export interface CommandContext {
99 interface Command {
1010 name: string; // no leading slash, lowercase
1111 description: string; // one line, shown by /help
12- run(ctx: CommandContext, args: string): void;
12+ run(ctx: CommandContext): void;
1313 }
1414
1515 /** The keybindings the TUI accepts, in the order `/help` prints them. */
@@ -39,12 +39,10 @@ const help: Command = {
3939
4040 const COMMANDS: Command[] = [help];
4141
42-/** `/name args` for a known `name`, else null; unknown slash text stays a message. */
43-export function findCommand(text: string): { command: Command; args: string } | null {
44- const match = /^\/(\S+)(?:\s+([\s\S]*))?$/.exec(text);
45- if (match === null) return null;
46- const command = COMMANDS.find((candidate) => candidate.name === match[1]);
47- return command === undefined ? null : { command, args: match[2] ?? "" };
42+/** `/name` for a known `name`, else null; unknown slash text stays a message. */
43+export function findCommand(text: string): Command | null {
44+ const match = /^\/(\S+)/.exec(text);
45+ return match === null ? null : (COMMANDS.find((candidate) => candidate.name === match[1]) ?? null);
4846 }
4947
5048 /** Tab completion for a half-typed command name; null leaves the draft alone. */
src/tui/styles.ts+8−30
@@ -5,38 +5,16 @@ import { NORMAL_FG, PALETTE, sgrFg } from "./theme.ts";
55 * re-asserting `Normal`'s, never by resetting: the row's background, set by the
66 * renderer, must survive, and no cell may fall back to the terminal's colours.
77 */
8-function foreground(text: string, hex: string): string {
9- return `${sgrFg(hex)}${text}${sgrFg(NORMAL_FG)}`;
10-}
8+const fg = (hex: string) => (text: string): string => `${sgrFg(hex)}${text}${sgrFg(NORMAL_FG)}`;
119
1210 /** `Comment`, the colour diff context lines share. */
13-export function dim(text: string): string {
14- return foreground(text, PALETTE.comment);
15-}
16-
17-export function red(text: string): string {
18- return foreground(text, PALETTE.red);
19-}
20-
21-export function green(text: string): string {
22- return foreground(text, PALETTE.green);
23-}
24-
25-export function yellow(text: string): string {
26- return foreground(text, PALETTE.yellow);
27-}
28-
11+export const dim = fg(PALETTE.comment);
12+export const red = fg(PALETTE.red);
13+export const green = fg(PALETTE.green);
14+export const yellow = fg(PALETTE.yellow);
2915 /** Diff hunk headers; the palette's `blue` sits closest to the old ANSI cyan. */
30-export function cyan(text: string): string {
31- return foreground(text, PALETTE.blue);
32-}
33-
16+export const cyan = fg(PALETTE.blue);
3417 /** The user's own words; also `Function`, which user rows never collide with. */
35-export function blue(text: string): string {
36- return foreground(text, PALETTE.blue);
37-}
38-
18+export const blue = fg(PALETTE.blue);
3919 /** The tool accent: call-line heads. */
40-export function teal(text: string): string {
41- return foreground(text, PALETTE.teal);
42-}
20+export const teal = fg(PALETTE.teal);
src/tui/theme.ts+0−1
@@ -78,7 +78,6 @@ export const STYLES: Record<string, Style | undefined> = {
7878 escape: { fg: PALETTE.magenta },
7979 function: { fg: PALETTE.blue },
8080 "function.builtin": { fg: PALETTE.blue1 },
81- "function.method": { fg: PALETTE.blue },
8281 keyword: { fg: PALETTE.purple },
8382 number: { fg: PALETTE.orange },
8483 operator: { fg: PALETTE.blue5 },
src/tui/tui.ts+6−10
@@ -1,6 +1,6 @@
11 import process from "node:process";
22 import type { AssistantMessage, JsonObject, Message, UserMessage } from "@earendil-works/pi-ai";
3-import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
3+import { assistantText, runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
44 import { Terminal, expandTabs, sanitize, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
66 import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
@@ -285,10 +285,10 @@ class Tui {
285285 return;
286286 }
287287 if (text.trim() === "") return;
288- const invocation = findCommand(text);
289- if (invocation !== null) {
288+ const command = findCommand(text);
289+ if (command !== null) {
290290 this.editor.clear();
291- invocation.command.run(this.commandContext, invocation.args);
291+ command.run(this.commandContext);
292292 this.render();
293293 return;
294294 }
@@ -443,13 +443,9 @@ class Tui {
443443 private commitMessage(message: AssistantMessage): void {
444444 this.activity.reset();
445445 this.commitLines(this.reply.flush());
446- const text = message.content
447- .filter((block) => block.type === "text")
448- .map((block) => block.text)
449- .join("");
446+ const text = assistantText(message);
450447 if (text.trim() !== "" && !this.streamed.includes(text)) {
451- const stream = new MarkdownStream();
452- this.commitLines([...stream.feed(text.trimEnd()), ...stream.flush()]);
448+ this.commitLines([...this.reply.feed(text.trimEnd()), ...this.reply.flush()]);
453449 }
454450 this.streamed = "";
455451 }