xonecas.io
64875
Mc

mini-coder / commit 8a8ff87

fix: minor language changes

xonecas2026-02-20 8a8ff879a8a1286fed04ab1197a322a24a43e1f6 parent dd1add2

2 files changed

src/tools/edit.ts+108−108
@@ -5,128 +5,128 @@ import { generateDiff } from "./diff.ts";
55 import { findLineByHash } from "./hashline.ts";
66
77 const EditSchema = z.object({
8- path: z.string().describe("File path to edit (absolute or relative to cwd)"),
9- startAnchor: z
10- .string()
11- .describe('Start anchor from a prior read/grep, e.g. "11:a3"'),
12- endAnchor: z
13- .string()
14- .optional()
15- .describe(
16- 'End anchor (inclusive), e.g. "33:0e". Omit to target only the startAnchor line.',
17- ),
18- newContent: z
19- .string()
20- .optional()
21- .describe(
22- "Replacement text. Omit or pass empty string to delete the range.",
23- ),
8+ path: z.string().describe("File path to edit (absolute or relative to cwd)"),
9+ startAnchor: z
10+ .string()
11+ .describe('Start anchor from a prior read/grep, e.g. "11:a3"'),
12+ endAnchor: z
13+ .string()
14+ .optional()
15+ .describe(
16+ 'End anchor (inclusive), e.g. "33:0e". Omit to target only the startAnchor line.',
17+ ),
18+ newContent: z
19+ .string()
20+ .optional()
21+ .describe(
22+ "Replacement text. Omit or pass empty string to delete the range.",
23+ ),
2424 });
2525
2626 type EditInput = z.infer<typeof EditSchema> & { cwd?: string };
2727
2828 export interface EditOutput {
29- path: string;
30- diff: string;
29+ path: string;
30+ diff: string;
3131 }
3232
3333 const HASH_NOT_FOUND_ERROR =
34- "Hash not found. Re-read the file to get current anchors.";
34+ "Hash not found. Re-read the file to get current anchors.";
3535
3636 export const editTool: ToolDef<EditInput, EditOutput> = {
37- name: "edit",
38- description:
39- "Replace or delete a range of lines in an existing file using hashline anchors. " +
40- 'Anchors come from the `read` or `grep` tools (format: "line:hash", e.g. "11:a3"). ' +
41- "Provide startAnchor alone to target a single line, or add endAnchor for a range. " +
42- "Set newContent to the replacement text, or omit it to delete the range. " +
43- "To create or overwrite a file use `write`. To insert without replacing any lines use `insert`.",
44- schema: EditSchema,
45- execute: async (input) => {
46- const cwd = input.cwd ?? process.cwd();
47- const filePath = input.path.startsWith("/")
48- ? input.path
49- : join(cwd, input.path);
50-
51- const relPath = relative(cwd, filePath);
52-
53- const file = Bun.file(filePath);
54- if (!(await file.exists())) {
55- throw new Error(
56- `File not found: "${relPath}". To create a new file use the \`write\` tool.`,
57- );
58- }
59-
60- const start = parseAnchor(input.startAnchor, "startAnchor");
61- const end = input.endAnchor
62- ? parseAnchor(input.endAnchor, "endAnchor")
63- : null;
64-
65- if (end && end.line < start.line) {
66- throw new Error(
67- "endAnchor line number must be >= startAnchor line number.",
68- );
69- }
70-
71- const original = await file.text();
72- const lines = original.split("\n");
73-
74- const startLine = findLineByHash(lines, start.hash, start.line);
75- if (!startLine) throw new Error(HASH_NOT_FOUND_ERROR);
76-
77- let endLine = startLine;
78- if (end) {
79- const resolvedEnd = findLineByHash(lines, end.hash, end.line);
80- if (!resolvedEnd) throw new Error(HASH_NOT_FOUND_ERROR);
81- endLine = resolvedEnd;
82- }
83- if (endLine < startLine) {
84- throw new Error("endAnchor resolves before startAnchor.");
85- }
86-
87- const replacement =
88- input.newContent === undefined || input.newContent === ""
89- ? []
90- : input.newContent.split("\n");
91-
92- const updatedLines = [
93- ...lines.slice(0, startLine - 1),
94- ...replacement,
95- ...lines.slice(endLine),
96- ];
97-
98- const updated = updatedLines.join("\n");
99- await Bun.write(filePath, updated);
100-
101- const diff = generateDiff(relPath, original, updated);
102- return { path: relPath, diff };
103- },
37+ name: "edit",
38+ description:
39+ "Replace or delete a range of lines in an existing file using hashline anchors. " +
40+ 'Anchors come from the `read` or `grep` tools (format: "line:hash", e.g. "11:a3"). ' +
41+ "Provide startAnchor alone to target a single line, or add endAnchor for a range. " +
42+ "Set newContent to the replacement text, or omit it to delete the range. " +
43+ "To create a file use `write`. To insert without replacing any lines use `insert`.",
44+ schema: EditSchema,
45+ execute: async (input) => {
46+ const cwd = input.cwd ?? process.cwd();
47+ const filePath = input.path.startsWith("/")
48+ ? input.path
49+ : join(cwd, input.path);
50+
51+ const relPath = relative(cwd, filePath);
52+
53+ const file = Bun.file(filePath);
54+ if (!(await file.exists())) {
55+ throw new Error(
56+ `File not found: "${relPath}". To create a new file use the \`write\` tool.`,
57+ );
58+ }
59+
60+ const start = parseAnchor(input.startAnchor, "startAnchor");
61+ const end = input.endAnchor
62+ ? parseAnchor(input.endAnchor, "endAnchor")
63+ : null;
64+
65+ if (end && end.line < start.line) {
66+ throw new Error(
67+ "endAnchor line number must be >= startAnchor line number.",
68+ );
69+ }
70+
71+ const original = await file.text();
72+ const lines = original.split("\n");
73+
74+ const startLine = findLineByHash(lines, start.hash, start.line);
75+ if (!startLine) throw new Error(HASH_NOT_FOUND_ERROR);
76+
77+ let endLine = startLine;
78+ if (end) {
79+ const resolvedEnd = findLineByHash(lines, end.hash, end.line);
80+ if (!resolvedEnd) throw new Error(HASH_NOT_FOUND_ERROR);
81+ endLine = resolvedEnd;
82+ }
83+ if (endLine < startLine) {
84+ throw new Error("endAnchor resolves before startAnchor.");
85+ }
86+
87+ const replacement =
88+ input.newContent === undefined || input.newContent === ""
89+ ? []
90+ : input.newContent.split("\n");
91+
92+ const updatedLines = [
93+ ...lines.slice(0, startLine - 1),
94+ ...replacement,
95+ ...lines.slice(endLine),
96+ ];
97+
98+ const updated = updatedLines.join("\n");
99+ await Bun.write(filePath, updated);
100+
101+ const diff = generateDiff(relPath, original, updated);
102+ return { path: relPath, diff };
103+ },
104104 };
105105
106106 interface ParsedAnchor {
107- line: number;
108- hash: string;
107+ line: number;
108+ hash: string;
109109 }
110110
111111 function parseAnchor(value: string, name: string): ParsedAnchor {
112- const match = /^\s*(\d+):([0-9a-fA-F]{2})\s*$/.exec(value);
113- if (!match) {
114- throw new Error(
115- `Invalid ${name}. Expected format: "line:hh" (e.g. "11:a3").`,
116- );
117- }
118-
119- const line = Number(match[1]);
120- if (!Number.isInteger(line) || line < 1) {
121- throw new Error(`Invalid ${name} line number.`);
122- }
123-
124- const hash = match[2];
125- if (!hash) {
126- throw new Error(
127- `Invalid ${name}. Expected format: "line:hh" (e.g. "11:a3").`,
128- );
129- }
130-
131- return { line, hash: hash.toLowerCase() };
112+ const match = /^\s*(\d+):([0-9a-fA-F]{2})\s*$/.exec(value);
113+ if (!match) {
114+ throw new Error(
115+ `Invalid ${name}. Expected format: "line:hh" (e.g. "11:a3").`,
116+ );
117+ }
118+
119+ const line = Number(match[1]);
120+ if (!Number.isInteger(line) || line < 1) {
121+ throw new Error(`Invalid ${name} line number.`);
122+ }
123+
124+ const hash = match[2];
125+ if (!hash) {
126+ throw new Error(
127+ `Invalid ${name}. Expected format: "line:hh" (e.g. "11:a3").`,
128+ );
129+ }
130+
131+ return { line, hash: hash.toLowerCase() };
132132 }
src/tools/insert.ts+78−78
@@ -5,98 +5,98 @@ import { generateDiff } from "./diff.ts";
55 import { findLineByHash } from "./hashline.ts";
66
77 const InsertSchema = z.object({
8- path: z.string().describe("File path to edit (absolute or relative to cwd)"),
9- anchor: z
10- .string()
11- .describe('Anchor line from a prior read/grep, e.g. "11:a3"'),
12- position: z
13- .enum(["before", "after"])
14- .describe('Insert the content "before" or "after" the anchor line'),
15- content: z.string().describe("Text to insert"),
8+ path: z.string().describe("File path to edit (absolute or relative to cwd)"),
9+ anchor: z
10+ .string()
11+ .describe('Anchor line from a prior read/grep, e.g. "11:a3"'),
12+ position: z
13+ .enum(["before", "after"])
14+ .describe('Insert the content "before" or "after" the anchor line'),
15+ content: z.string().describe("Text to insert"),
1616 });
1717
1818 type InsertInput = z.infer<typeof InsertSchema> & { cwd?: string };
1919
2020 export interface InsertOutput {
21- path: string;
22- diff: string;
21+ path: string;
22+ diff: string;
2323 }
2424
2525 const HASH_NOT_FOUND_ERROR =
26- "Hash not found. Re-read the file to get current anchors.";
26+ "Hash not found. Re-read the file to get current anchors.";
2727
2828 export const insertTool: ToolDef<InsertInput, InsertOutput> = {
29- name: "insert",
30- description:
31- "Insert new lines before or after an anchor line in an existing file. " +
32- "The anchor line itself is not modified. " +
33- 'Anchors come from the `read` or `grep` tools (format: "line:hash", e.g. "11:a3"). ' +
34- "To replace or delete lines use `edit`. To create or overwrite a file use `write`.",
35- schema: InsertSchema,
36- execute: async (input) => {
37- const cwd = input.cwd ?? process.cwd();
38- const filePath = input.path.startsWith("/")
39- ? input.path
40- : join(cwd, input.path);
41-
42- const relPath = relative(cwd, filePath);
43-
44- const file = Bun.file(filePath);
45- if (!(await file.exists())) {
46- throw new Error(
47- `File not found: "${relPath}". To create a new file use the \`write\` tool.`,
48- );
49- }
50-
51- const parsed = parseAnchor(input.anchor);
52-
53- const original = await file.text();
54- const lines = original.split("\n");
55-
56- const anchorLine = findLineByHash(lines, parsed.hash, parsed.line);
57- if (!anchorLine) throw new Error(HASH_NOT_FOUND_ERROR);
58-
59- const insertAt = input.position === "before" ? anchorLine - 1 : anchorLine;
60- const insertLines = input.content.split("\n");
61-
62- const updatedLines = [
63- ...lines.slice(0, insertAt),
64- ...insertLines,
65- ...lines.slice(insertAt),
66- ];
67-
68- const updated = updatedLines.join("\n");
69- await Bun.write(filePath, updated);
70-
71- const diff = generateDiff(relPath, original, updated);
72- return { path: relPath, diff };
73- },
29+ name: "insert",
30+ description:
31+ "Insert new lines before or after an anchor line in an existing file. " +
32+ "The anchor line itself is not modified. " +
33+ 'Anchors come from the `read` or `grep` tools (format: "line:hash", e.g. "11:a3"). ' +
34+ "To replace or delete lines use `edit`. To create a file use `write`.",
35+ schema: InsertSchema,
36+ execute: async (input) => {
37+ const cwd = input.cwd ?? process.cwd();
38+ const filePath = input.path.startsWith("/")
39+ ? input.path
40+ : join(cwd, input.path);
41+
42+ const relPath = relative(cwd, filePath);
43+
44+ const file = Bun.file(filePath);
45+ if (!(await file.exists())) {
46+ throw new Error(
47+ `File not found: "${relPath}". To create a new file use the \`write\` tool.`,
48+ );
49+ }
50+
51+ const parsed = parseAnchor(input.anchor);
52+
53+ const original = await file.text();
54+ const lines = original.split("\n");
55+
56+ const anchorLine = findLineByHash(lines, parsed.hash, parsed.line);
57+ if (!anchorLine) throw new Error(HASH_NOT_FOUND_ERROR);
58+
59+ const insertAt = input.position === "before" ? anchorLine - 1 : anchorLine;
60+ const insertLines = input.content.split("\n");
61+
62+ const updatedLines = [
63+ ...lines.slice(0, insertAt),
64+ ...insertLines,
65+ ...lines.slice(insertAt),
66+ ];
67+
68+ const updated = updatedLines.join("\n");
69+ await Bun.write(filePath, updated);
70+
71+ const diff = generateDiff(relPath, original, updated);
72+ return { path: relPath, diff };
73+ },
7474 };
7575
7676 interface ParsedAnchor {
77- line: number;
78- hash: string;
77+ line: number;
78+ hash: string;
7979 }
8080
8181 function parseAnchor(value: string): ParsedAnchor {
82- const match = /^\s*(\d+):([0-9a-fA-F]{2})\s*$/.exec(value);
83- if (!match) {
84- throw new Error(
85- `Invalid anchor. Expected format: "line:hh" (e.g. "11:a3").`,
86- );
87- }
88-
89- const line = Number(match[1]);
90- if (!Number.isInteger(line) || line < 1) {
91- throw new Error("Invalid anchor line number.");
92- }
93-
94- const hash = match[2];
95- if (!hash) {
96- throw new Error(
97- `Invalid anchor. Expected format: "line:hh" (e.g. "11:a3").`,
98- );
99- }
100-
101- return { line, hash: hash.toLowerCase() };
82+ const match = /^\s*(\d+):([0-9a-fA-F]{2})\s*$/.exec(value);
83+ if (!match) {
84+ throw new Error(
85+ `Invalid anchor. Expected format: "line:hh" (e.g. "11:a3").`,
86+ );
87+ }
88+
89+ const line = Number(match[1]);
90+ if (!Number.isInteger(line) || line < 1) {
91+ throw new Error("Invalid anchor line number.");
92+ }
93+
94+ const hash = match[2];
95+ if (!hash) {
96+ throw new Error(
97+ `Invalid anchor. Expected format: "line:hh" (e.g. "11:a3").`,
98+ );
99+ }
100+
101+ return { line, hash: hash.toLowerCase() };
102102 }