xonecas.io
64875
Mc

mini-coder / commit dd1add2

tools: hide cwd from model schemas, keep injected via withCwdDefault

xonecas2026-02-20 dd1add2a77fc68005c9472f59493f76368e5e296 parent c5797d6

7 files changed

src/tools/edit.ts+3−7
@@ -4,7 +4,7 @@ import type { ToolDef } from "../llm-api/types.ts";
44 import { generateDiff } from "./diff.ts";
55 import { findLineByHash } from "./hashline.ts";
66
7-const EditInput = z.object({
7+const EditSchema = z.object({
88 path: z.string().describe("File path to edit (absolute or relative to cwd)"),
99 startAnchor: z
1010 .string()
@@ -21,13 +21,9 @@ const EditInput = z.object({
2121 .describe(
2222 "Replacement text. Omit or pass empty string to delete the range.",
2323 ),
24- cwd: z
25- .string()
26- .optional()
27- .describe("Working directory for resolving relative paths"),
2824 });
2925
30-type EditInput = z.infer<typeof EditInput>;
26+type EditInput = z.infer<typeof EditSchema> & { cwd?: string };
3127
3228 export interface EditOutput {
3329 path: string;
@@ -45,7 +41,7 @@ export const editTool: ToolDef<EditInput, EditOutput> = {
4541 "Provide startAnchor alone to target a single line, or add endAnchor for a range. " +
4642 "Set newContent to the replacement text, or omit it to delete the range. " +
4743 "To create or overwrite a file use `write`. To insert without replacing any lines use `insert`.",
48- schema: EditInput,
44+ schema: EditSchema,
4945 execute: async (input) => {
5046 const cwd = input.cwd ?? process.cwd();
5147 const filePath = input.path.startsWith("/")
src/tools/glob.ts+3−7
@@ -2,18 +2,14 @@ import { join, relative } from "node:path";
22 import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
44
5-const GlobInput = z.object({
5+const GlobSchema = z.object({
66 pattern: z
77 .string()
88 .describe("Glob pattern to match files against, e.g. '**/*.ts'"),
9- cwd: z
10- .string()
11- .optional()
12- .describe("Directory to search in (defaults to process.cwd())"),
139 ignore: z.array(z.string()).optional().describe("Glob patterns to exclude"),
1410 });
1511
16-type GlobInput = z.infer<typeof GlobInput>;
12+type GlobInput = z.infer<typeof GlobSchema> & { cwd?: string };
1713
1814 export interface GlobOutput {
1915 files: string[];
@@ -28,7 +24,7 @@ export const globTool: ToolDef<GlobInput, GlobOutput> = {
2824 description:
2925 "Find files matching a glob pattern. Returns relative paths sorted by modification time. " +
3026 "Use this to discover files before reading them.",
31- schema: GlobInput,
27+ schema: GlobSchema,
3228 execute: async (input) => {
3329 const cwd = input.cwd ?? process.cwd();
3430 const defaultIgnore = [
src/tools/grep.ts+3−7
@@ -3,16 +3,12 @@ import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
44 import { formatHashLine } from "./hashline.ts";
55
6-const GrepInput = z.object({
6+const GrepSchema = z.object({
77 pattern: z.string().describe("Regular expression to search for"),
88 include: z
99 .string()
1010 .optional()
1111 .describe("Glob pattern to filter files, e.g. '*.ts' or '*.{ts,tsx}'"),
12- cwd: z
13- .string()
14- .optional()
15- .describe("Directory to search in (defaults to process.cwd())"),
1612 contextLines: z
1713 .number()
1814 .int()
@@ -25,7 +21,7 @@ const GrepInput = z.object({
2521 maxResults: z.number().int().min(1).max(200).optional().default(50),
2622 });
2723
28-type GrepInput = z.infer<typeof GrepInput>;
24+type GrepInput = z.infer<typeof GrepSchema> & { cwd?: string };
2925
3026 export interface GrepMatch {
3127 file: string;
@@ -56,7 +52,7 @@ export const grepTool: ToolDef<GrepInput, GrepOutput> = {
5652 description:
5753 "Search for a regex pattern across files. Returns file paths, line numbers, and context. " +
5854 "Use this to find code patterns, function definitions, or specific text.",
59- schema: GrepInput,
55+ schema: GrepSchema,
6056 execute: async (input) => {
6157 const cwd = input.cwd ?? process.cwd();
6258 const flags = input.caseSensitive ? "" : "i";
src/tools/insert.ts+3−7
@@ -4,7 +4,7 @@ import type { ToolDef } from "../llm-api/types.ts";
44 import { generateDiff } from "./diff.ts";
55 import { findLineByHash } from "./hashline.ts";
66
7-const InsertInput = z.object({
7+const InsertSchema = z.object({
88 path: z.string().describe("File path to edit (absolute or relative to cwd)"),
99 anchor: z
1010 .string()
@@ -13,13 +13,9 @@ const InsertInput = z.object({
1313 .enum(["before", "after"])
1414 .describe('Insert the content "before" or "after" the anchor line'),
1515 content: z.string().describe("Text to insert"),
16- cwd: z
17- .string()
18- .optional()
19- .describe("Working directory for resolving relative paths"),
2016 });
2117
22-type InsertInput = z.infer<typeof InsertInput>;
18+type InsertInput = z.infer<typeof InsertSchema> & { cwd?: string };
2319
2420 export interface InsertOutput {
2521 path: string;
@@ -36,7 +32,7 @@ export const insertTool: ToolDef<InsertInput, InsertOutput> = {
3632 "The anchor line itself is not modified. " +
3733 'Anchors come from the `read` or `grep` tools (format: "line:hash", e.g. "11:a3"). ' +
3834 "To replace or delete lines use `edit`. To create or overwrite a file use `write`.",
39- schema: InsertInput,
35+ schema: InsertSchema,
4036 execute: async (input) => {
4137 const cwd = input.cwd ?? process.cwd();
4238 const filePath = input.path.startsWith("/")
src/tools/read.ts+3−7
@@ -3,7 +3,7 @@ import { z } from "zod";
33 import type { ToolDef } from "../llm-api/types.ts";
44 import { formatHashLine } from "./hashline.ts";
55
6-const ReadInput = z.object({
6+const ReadSchema = z.object({
77 path: z.string().describe("File path to read (absolute or relative to cwd)"),
88 line: z
99 .number()
@@ -18,13 +18,9 @@ const ReadInput = z.object({
1818 .max(500)
1919 .optional()
2020 .describe("Lines to read (default: 500, max: 500)"),
21- cwd: z
22- .string()
23- .optional()
24- .describe("Working directory for resolving relative paths"),
2521 });
2622
27-type ReadInput = z.infer<typeof ReadInput>;
23+type ReadInput = z.infer<typeof ReadSchema> & { cwd?: string };
2824
2925 export interface ReadOutput {
3026 path: string;
@@ -44,7 +40,7 @@ export const readTool: ToolDef<ReadInput, ReadOutput> = {
4440 "`count` sets how many lines to read (default 500, max 500). " +
4541 "Check `truncated` and `totalLines` in the result to detect when more content exists; " +
4642 "paginate by incrementing `line`.",
47- schema: ReadInput,
43+ schema: ReadSchema,
4844 execute: async (input) => {
4945 const cwd = input.cwd ?? process.cwd();
5046 const filePath = input.path.startsWith("/")
src/tools/shell.ts+3−7
@@ -2,12 +2,8 @@ import { z } from "zod";
22 import { restoreTerminal } from "../cli/output.ts";
33 import type { ToolDef } from "../llm-api/types.ts";
44
5-const ShellInput = z.object({
5+const ShellSchema = z.object({
66 command: z.string().describe("Shell command to execute"),
7- cwd: z
8- .string()
9- .optional()
10- .describe("Working directory for the command (defaults to process.cwd())"),
117 timeout: z
128 .number()
139 .int()
@@ -22,7 +18,7 @@ const ShellInput = z.object({
2218 .describe("Additional environment variables to set"),
2319 });
2420
25-type ShellInput = z.infer<typeof ShellInput>;
21+type ShellInput = z.infer<typeof ShellSchema> & { cwd?: string };
2622
2723 export interface ShellOutput {
2824 stdout: string;
@@ -40,7 +36,7 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
4036 "Execute a shell command. Returns stdout, stderr, and exit code. " +
4137 "Use this for running tests, builds, git commands, and other CLI operations. " +
4238 "Prefer non-interactive commands. Avoid commands that run indefinitely.",
43- schema: ShellInput,
39+ schema: ShellSchema,
4440 execute: async (input) => {
4541 const cwd = input.cwd ?? process.cwd();
4642 const timeout = input.timeout ?? 30_000;
src/tools/write.ts+33−37
@@ -4,48 +4,44 @@ import { z } from "zod";
44 import type { ToolDef } from "../llm-api/types.ts";
55 import { generateDiff } from "./diff.ts";
66
7-const WriteInput = z.object({
8- path: z.string().describe("File path to write (absolute or relative to cwd)"),
9- content: z.string().describe("Full content to write to the file"),
10- cwd: z
11- .string()
12- .optional()
13- .describe("Working directory for resolving relative paths"),
7+const WriteSchema = z.object({
8+ path: z.string().describe("File path to write (absolute or relative to cwd)"),
9+ content: z.string().describe("Full content to write to the file"),
1410 });
1511
16-type WriteInput = z.infer<typeof WriteInput>;
12+type WriteInput = z.infer<typeof WriteSchema> & { cwd?: string };
1713
1814 export interface WriteOutput {
19- path: string;
20- diff: string;
21- created: boolean;
15+ path: string;
16+ diff: string;
17+ created: boolean;
2218 }
2319
2420 export const writeTool: ToolDef<WriteInput, WriteOutput> = {
25- name: "write",
26- description:
27- "Create a new file or fully overwrite an existing file with the given content. " +
28- "Use this for new files or when replacing an entire file. " +
29- "For targeted line edits on existing files, use `edit` or `insert` instead.",
30- schema: WriteInput,
31- execute: async (input) => {
32- const cwd = input.cwd ?? process.cwd();
33- const filePath = input.path.startsWith("/")
34- ? input.path
35- : join(cwd, input.path);
36-
37- const relPath = relative(cwd, filePath);
38-
39- const dir = dirname(filePath);
40- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
41-
42- const file = Bun.file(filePath);
43- const created = !(await file.exists());
44- const before = created ? "" : await file.text();
45-
46- await Bun.write(filePath, input.content);
47-
48- const diff = generateDiff(relPath, before, input.content);
49- return { path: relPath, diff, created };
50- },
21+ name: "write",
22+ description:
23+ "Create a new file or fully overwrite an existing file with the given content. " +
24+ "Use this only for new files. " +
25+ "For targeted line edits on existing files, **use `edit` or `insert` instead**.",
26+ schema: WriteSchema,
27+ execute: async (input) => {
28+ const cwd = input.cwd ?? process.cwd();
29+ const filePath = input.path.startsWith("/")
30+ ? input.path
31+ : join(cwd, input.path);
32+
33+ const relPath = relative(cwd, filePath);
34+
35+ const dir = dirname(filePath);
36+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
37+
38+ const file = Bun.file(filePath);
39+ const created = !(await file.exists());
40+ const before = created ? "" : await file.text();
41+
42+ await Bun.write(filePath, input.content);
43+
44+ const diff = generateDiff(relPath, before, input.content);
45+ return { path: relPath, diff, created };
46+ },
5147 };