xonecas.io
64875
Mc

mini-coder / commit 726a2be

feat: read tool

xonecas2026-04-30 726a2beb545a9f146efd57357f50475a09eba1ca parent 4b9f056

9 files changed

TODO.md+0−2
@@ -4,8 +4,6 @@
44
55 ## TODO
66
7-- [ ] `read` tool --read file with offset and limit.
8-- [ ] `readImage` tool — embed image files in prompts (big real gap). Also update `tui-conversation.ts` `messageCacheKey` and `toolMessageNode` to handle image blocks.
97 - [ ] Session fork/undo — either tree-view forking (like pi coding agent) or undo flow, or both. Complex feature, needs UI and storage design.
108 - [ ] Path autocomplete in TUI — `Tab` on file paths opens path picker.
119 - [ ] Status bar richness — detailed git counts (staged/modified/untracked/ahead/behind).
package.json+1−1
@@ -1,6 +1,6 @@
11 {
22 "name": "mini-coder",
3- "version": "0.6.0",
3+ "version": "0.6.1",
44 "type": "module",
55 "packageManager": "bun@1.3.12",
66 "bin": {
src/agent.ts+8−2
@@ -1,6 +1,7 @@
11 import {
22 type AssistantMessage,
33 type Context,
4+ type ImageContent,
45 type Message,
56 streamSimple,
67 type TextContent,
@@ -239,14 +240,19 @@ async function* toolRunner(
239240 },
240241 };
241242 } else if (e.type === "result") {
242- // handle final message
243+ // handle final message, and check for images
244+ let img: ImageContent | null = null;
245+ if (e.image) {
246+ img = { ...e.image, type: "image" };
247+ }
248+
243249 yield {
244250 type: "tool_result",
245251 message: {
246252 role: "toolResult",
247253 toolCallId: call.id,
248254 toolName: call.name,
249- content: [content],
255+ content: img ? [content, img] : [content],
250256 isError: false,
251257 timestamp,
252258 },
src/headless.ts+2−0
@@ -3,6 +3,7 @@ import { streamAgent } from "./agent";
33 import { buildSystemPrompt, injectEnvReminder, MAIN_PROMPT } from "./prompt";
44 import { bash, runBashTool } from "./tool-bash";
55 import { edit, runEditTool } from "./tool-edit";
6+import { read, runReadTool } from "./tool-read";
67 import type { AgentContex, CliOptions, ToolAndRunner } from "./types";
78
89 export async function streamHeadless(
@@ -12,6 +13,7 @@ export async function streamHeadless(
1213 const tools: ToolAndRunner[] = [
1314 { tool: bash, runner: runBashTool },
1415 { tool: edit, runner: runEditTool },
16+ { tool: read, runner: runReadTool },
1517 ];
1618 const envReminder = await injectEnvReminder();
1719 const messages: Message[] = [
src/prompt.ts+12−0
@@ -21,6 +21,18 @@ For example: "Clients are handled in the \`connectToServer\` function in src/ser
2121
2222 User messages and Tool results may include <system-reminder> tags. These contain system-generated reminders and bear no direct relation to the specific tool result in which they appear.
2323
24+## Tools
25+- You have access to bash, read and edit tools. Prefer using read and edit for file operations, use bash for finding read candidates or to run development commands.
26+
27+<example>
28+> User: please read the README.md and add rich code examples.
29+
30+- Use the bash tool to find the path for README.md, prefer "ls" or "fd/find", and "rg/grep".
31+- Then read the file with the read tool to find the replacement areas and mathcing patterns
32+- Edit the file using the edit tool. Review the output diff, use the read tool again to verify if needed.
33+- Reply to the user that the edit was done.
34+</example>
35+
2436 ## Workflow
2537 - Stay rooted on the user's request. Don't wander into tangents or explore out of curiosity.
2638 - Gather only the information needed to fulfill the request, then stop exploring and complete it.
src/tool-read.ts+100−0
@@ -0,0 +1,100 @@
1+import { Buffer } from "node:buffer";
2+import { extname, isAbsolute, join } from "node:path";
3+import { type Tool, Type } from "@mariozechner/pi-ai";
4+import type { ToolRunnerEvent } from "./types";
5+
6+const imageMimeTypes: Record<string, string> = {
7+ ".bmp": "image/bmp",
8+ ".gif": "image/gif",
9+ ".jpeg": "image/jpeg",
10+ ".jpg": "image/jpeg",
11+ ".png": "image/png",
12+ ".svg": "image/svg+xml",
13+ ".webp": "image/webp",
14+};
15+
16+const description = `## Read tool
17+
18+Read a file by path.
19+
20+For text files, returns line-numbered text. Use \`offset\` and \`limit\` to read a specific range.
21+You can also read images files.
22+`;
23+
24+export const read: Tool = {
25+ name: "read",
26+ description,
27+ parameters: Type.Object({
28+ path: Type.String({
29+ description:
30+ "File path. Absolute or relative to the current working directory.",
31+ }),
32+ offset: Type.Optional(
33+ Type.Number({
34+ description:
35+ "Optional 1-based line number to start reading from. Text files only.",
36+ }),
37+ ),
38+ limit: Type.Optional(
39+ Type.Number({
40+ description:
41+ "Optional maximum number of lines to read. Text files only.",
42+ }),
43+ ),
44+ }),
45+};
46+
47+export async function* runReadTool(
48+ args: Record<string, any>,
49+ signal?: AbortSignal,
50+): AsyncGenerator<ToolRunnerEvent> {
51+ const filePath = isAbsolute(args.path)
52+ ? args.path
53+ : join(process.cwd(), args.path);
54+ const file = Bun.file(filePath);
55+ const exists = await file.exists();
56+
57+ if (!exists) {
58+ yield { type: "result", text: `File not found: ${filePath}` };
59+ return;
60+ }
61+
62+ if (signal?.aborted) {
63+ yield { type: "result", text: "Aborted before read." };
64+ return;
65+ }
66+
67+ const mimeType = imageMimeTypes[extname(filePath).toLowerCase()] ?? file.type;
68+ if (mimeType.startsWith("image/")) {
69+ const data = Buffer.from(await file.arrayBuffer()).toString("base64");
70+ const text = `Image read: ${filePath}\nMIME type: ${mimeType}\nSize: ${file.size} bytes`;
71+ yield {
72+ type: "result",
73+ text,
74+ image: { data, mimeType },
75+ };
76+ return;
77+ }
78+
79+ const content = await file.text();
80+ const lines = content.split(/\r?\n/);
81+ if (content.endsWith("\n")) lines.pop();
82+
83+ const offset = args.offset ?? 1;
84+ const startIndex = offset - 1;
85+ const endIndex = args.limit
86+ ? Math.min(lines.length, startIndex + args.limit)
87+ : lines.length;
88+ const width = String(endIndex).length;
89+ const body = lines
90+ .slice(startIndex, endIndex)
91+ .map((line, idx) => `${String(offset + idx).padStart(width)} | ${line}`)
92+ .join("\n");
93+
94+ let text = `File: ${filePath}\nLines: ${offset}-${endIndex} of ${lines.length}\n\n${body}`;
95+ if (endIndex < lines.length) {
96+ text += `\n\nMore lines available. Use offset ${endIndex + 1} to continue.`;
97+ }
98+
99+ yield { type: "result", text };
100+}
src/tui-conversation.ts+1−0
@@ -116,6 +116,7 @@ function userMessageNode(msg: UserMessage): Node {
116116 }
117117
118118 function toolMessageNode(msg: ToolResultMessage): Node {
119+ // TODO: read tool image output
119120 // Output only shows last 10 lines of scroll.
120121 const text = msg.content
121122 .filter((c) => c.type === "text")
src/tui.ts+2−0
@@ -6,6 +6,7 @@ import { updateSession } from "./session";
66 import { estimateTokens, secureRandomString } from "./shared";
77 import { bash, runBashTool } from "./tool-bash";
88 import { edit, runEditTool } from "./tool-edit";
9+import { read, runReadTool } from "./tool-read";
910 import {
1011 ActivityPill,
1112 ContextPill,
@@ -140,6 +141,7 @@ async function streamAgentTUI(state: TUIState) {
140141 const tools: ToolAndRunner[] = [
141142 { tool: bash, runner: runBashTool },
142143 { tool: edit, runner: runEditTool },
144+ { tool: read, runner: runReadTool },
143145 ];
144146
145147 let userContent = state.prompt;
src/types.ts+5−1
@@ -129,7 +129,11 @@ export type AgentToolEvent =
129129
130130 export type ToolRunnerEvent =
131131 | { type: "output"; text: string }
132- | { type: "result"; text: string };
132+ | {
133+ type: "result";
134+ text: string;
135+ image?: { data: string; mimeType: string };
136+ };
133137
134138 export type ToolAndRunner = {
135139 tool: Tool;