@trans-ai/trans-code 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,6 +53,34 @@ describe('read_file', () => {
53
53
  expect(output).toContain('short');
54
54
  expect(output.length).toBeLessThan(2400);
55
55
  });
56
+
57
+ test('returns image files as base64 multimodal content', async () => {
58
+ const dir = workspace();
59
+ const session = createToolSession(dir);
60
+ // Minimal fake PNG: magic bytes + payload.
61
+ const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3]);
62
+ writeFileSync(path.join(dir, 'logo.png'), png);
63
+
64
+ const result = await readFileTool.execute({ path: 'logo.png' }, ctx(session));
65
+ expect(typeof result).toBe('object');
66
+ const output = result as { output: string; images?: Array<{ data: string; mediaType: string }> };
67
+ expect(output.images?.length).toBe(1);
68
+ expect(output.images?.[0]?.mediaType).toBe('image/png');
69
+ expect(output.images?.[0]?.data).toBe(png.toString('base64'));
70
+ expect(output.output).toContain('image');
71
+ expect(hasFileBeenRead(session, path.join(dir, 'logo.png'))).toBe(true);
72
+ });
73
+
74
+ test('rejects oversized images instead of blowing up the context', async () => {
75
+ const dir = workspace();
76
+ const session = createToolSession(dir);
77
+ const huge = Buffer.alloc(3_500_000, 7);
78
+ writeFileSync(path.join(dir, 'big.jpg'), huge);
79
+
80
+ await expect(readFileTool.execute({ path: 'big.jpg' }, ctx(session))).rejects.toThrow(
81
+ /3 MB|3MB/,
82
+ );
83
+ });
56
84
  });
57
85
 
58
86
  describe('write_file', () => {
@@ -118,6 +146,74 @@ describe('edit_file', () => {
118
146
  expect(readFileSync(path.join(dir, 'prose.md'), 'utf8')).toBe('She said "go" now\n');
119
147
  });
120
148
 
149
+ test('applies multiple edits in one call', async () => {
150
+ const dir = workspace();
151
+ writeFileSync(
152
+ path.join(dir, 'multi.txt'),
153
+ 'alpha\nbeta\ngamma\nbeta\n',
154
+ );
155
+ const session = createToolSession(dir);
156
+
157
+ await read(session, 'multi.txt');
158
+ const result = await editFileTool.execute(
159
+ {
160
+ path: 'multi.txt',
161
+ edits: [
162
+ { old_string: 'alpha', new_string: 'ALPHA' },
163
+ { old_string: 'gamma', new_string: 'GAMMA' },
164
+ { old_string: 'beta\n', new_string: 'BETA\n', replace_all: true },
165
+ ],
166
+ },
167
+ ctx(session),
168
+ );
169
+
170
+ expect(readFileSync(path.join(dir, 'multi.txt'), 'utf8')).toBe('ALPHA\nBETA\nGAMMA\nBETA\n');
171
+ expect(result.output).toContain('Replaced 4 occurrence(s)');
172
+ expect(result.diff).toContain('-alpha');
173
+ expect(result.diff).toContain('+GAMMA');
174
+ });
175
+
176
+ test('multi-edit is atomic: a missing edit leaves the file untouched', async () => {
177
+ const dir = workspace();
178
+ writeFileSync(path.join(dir, 'a.txt'), 'one\ntwo\n');
179
+ const session = createToolSession(dir);
180
+
181
+ await read(session, 'a.txt');
182
+ await expect(
183
+ editFileTool.execute(
184
+ {
185
+ path: 'a.txt',
186
+ edits: [
187
+ { old_string: 'one', new_string: 'ONE' },
188
+ { old_string: 'nope', new_string: 'X' },
189
+ ],
190
+ },
191
+ ctx(session),
192
+ ),
193
+ ).rejects.toThrow(/not found/);
194
+ expect(readFileSync(path.join(dir, 'a.txt'), 'utf8')).toBe('one\ntwo\n');
195
+ });
196
+
197
+ test('multi-edit rejects an old_string contained in an earlier new_string', async () => {
198
+ const dir = workspace();
199
+ writeFileSync(path.join(dir, 'a.txt'), 'one\ntwo\n');
200
+ const session = createToolSession(dir);
201
+
202
+ await read(session, 'a.txt');
203
+ await expect(
204
+ editFileTool.execute(
205
+ {
206
+ path: 'a.txt',
207
+ edits: [
208
+ { old_string: 'one', new_string: 'one plus two' },
209
+ { old_string: 'two', new_string: 'TWO' },
210
+ ],
211
+ },
212
+ ctx(session),
213
+ ),
214
+ ).rejects.toThrow(/substring/);
215
+ });
216
+
121
217
  test('returns a line-numbered snippet around the edit', async () => {
122
218
  const dir = workspace();
123
219
  const lines = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'];
@@ -8,6 +8,33 @@ import { lineDiff } from './diff.js';
8
8
 
9
9
  const SNIPPET_CONTEXT_LINES = 4;
10
10
  const NEW_FILE_DIFF_LINES = 20;
11
+ const MAX_IMAGE_BYTES = 3_000_000;
12
+
13
+ const IMAGE_MEDIA: Record<string, string> = {
14
+ png: 'image/png',
15
+ jpg: 'image/jpeg',
16
+ jpeg: 'image/jpeg',
17
+ gif: 'image/gif',
18
+ webp: 'image/webp',
19
+ };
20
+
21
+ /** Reads workspace image paths into base64 blocks (shared with ask_model). */
22
+ export async function readImageBlocks(
23
+ paths: string[],
24
+ ctx: { cwd: string },
25
+ ): Promise<Array<{ data: string; mediaType: string }>> {
26
+ const blocks: Array<{ data: string; mediaType: string }> = [];
27
+ for (const inputPath of paths) {
28
+ const absolute = resolveInsideWorkspace(ctx.cwd, String(inputPath));
29
+ const mediaType = IMAGE_MEDIA[path.extname(absolute).slice(1).toLowerCase()];
30
+ if (!mediaType) throw new Error(`Not an image file: ${inputPath}`);
31
+ const stat = await fs.stat(absolute);
32
+ if (stat.size > MAX_IMAGE_BYTES) throw new Error(`Image too large (>3MB): ${inputPath}`);
33
+ const buffer = await fs.readFile(absolute);
34
+ blocks.push({ data: buffer.toString('base64'), mediaType });
35
+ }
36
+ return blocks;
37
+ }
11
38
 
12
39
  // Claude-style quote normalization: models emit straight quotes, files often
13
40
  // contain curly ones. Both sides are normalized before matching.
@@ -91,6 +118,24 @@ export const readFileTool: AgentTool = {
91
118
  'Use grep or bash (head/tail) to inspect it.',
92
119
  );
93
120
  }
121
+
122
+ // Images are returned as base64 multimodal blocks for vision models.
123
+ const mediaType = IMAGE_MEDIA[path.extname(absolute).slice(1).toLowerCase()];
124
+ if (mediaType) {
125
+ if (stat.size > MAX_IMAGE_BYTES) {
126
+ throw new Error(
127
+ `Image is ${(stat.size / 1_000_000).toFixed(1)} MB, above the 3 MB limit. ` +
128
+ 'Downscale it before reading.',
129
+ );
130
+ }
131
+ const buffer = await fs.readFile(absolute);
132
+ markFileRead(ctx.session, absolute);
133
+ return {
134
+ output: `[image ${displayPath(ctx.cwd, absolute)}, ${stat.size} bytes, shown visually]`,
135
+ images: [{ data: buffer.toString('base64'), mediaType }],
136
+ };
137
+ }
138
+
94
139
  if (stat.size > MAX_READ_BYTES && !input.offset && !input.limit) {
95
140
  throw new Error(
96
141
  `File is ${stat.size} bytes. Pass offset/limit to read it in chunks.`,
@@ -189,51 +234,88 @@ export const editFileTool: AgentTool = {
189
234
  isMutating: true,
190
235
  schema: z.object({
191
236
  path: z.string().describe('File path, relative to the workspace root'),
192
- old_string: z.string().describe('Exact text to replace'),
193
- new_string: z.string().describe('Replacement text'),
237
+ old_string: z.string().nullable().optional().describe('Exact text to replace (single-edit mode)'),
238
+ new_string: z.string().nullable().optional().describe('Replacement text (single-edit mode)'),
194
239
  replace_all: z.boolean().nullable().optional().describe('Replace every occurrence'),
240
+ edits: z
241
+ .array(
242
+ z.object({
243
+ old_string: z.string().describe('Exact text to replace'),
244
+ new_string: z.string().describe('Replacement text'),
245
+ replace_all: z.boolean().nullable().optional().describe('Replace every occurrence'),
246
+ }),
247
+ )
248
+ .nullable()
249
+ .optional()
250
+ .describe('Multiple edits applied in order (multi-edit mode; preferred for 2+ changes)'),
195
251
  }),
196
252
  title: (input) => `patch: ${String(input.path ?? '')}`,
197
253
  async execute(input, ctx) {
198
254
  const absolute = resolveInsideWorkspace(ctx.cwd, String(input.path));
199
255
  requireRead(ctx.session, absolute, displayPath(ctx.cwd, absolute));
200
256
 
201
- const oldString = String(input.old_string);
202
- const newString = String(input.new_string);
203
- if (oldString === newString) throw new Error('old_string and new_string are identical');
204
-
205
257
  const original = await fs.readFile(absolute, 'utf8');
258
+ const editList = Array.isArray(input.edits)
259
+ ? (input.edits as Array<{ old_string: string; new_string: string; replace_all?: boolean }>)
260
+ : [{ old_string: String(input.old_string), new_string: String(input.new_string), replace_all: input.replace_all === true }];
261
+ if (editList.length === 0) throw new Error('No edits given.');
206
262
 
207
- const actualOld = findActualString(original, oldString);
208
- if (actualOld === null) {
209
- throw new Error('old_string was not found in the file. Read it again and use exact text.');
210
- }
211
- const occurrences = actualOld === '' ? 0 : original.split(actualOld).length - 1;
212
- if (occurrences === 0) {
213
- throw new Error('old_string was not found in the file. Read it again and use exact text.');
214
- }
215
- if (occurrences > 1 && input.replace_all !== true) {
216
- throw new Error(
217
- `old_string appears ${occurrences} times. Pass replace_all=true or include more context.`,
218
- );
263
+ // Validate and apply every edit in memory first: a failure anywhere
264
+ // must leave the file untouched (atomic multi-edit).
265
+ const appliedNewStrings: string[] = [];
266
+ let updated = original;
267
+ let replacedCount = 0;
268
+ for (const edit of editList) {
269
+ const oldString = String(edit.old_string);
270
+ const newString = String(edit.new_string);
271
+ if (oldString === newString) throw new Error('old_string and new_string are identical');
272
+
273
+ const checkOld = oldString.replace(/\n+$/, '');
274
+ for (const previous of appliedNewStrings) {
275
+ if (checkOld !== '' && previous.includes(checkOld)) {
276
+ throw new Error(
277
+ 'Cannot edit file: old_string is a substring of a new_string from a previous edit. ' +
278
+ 'Merge those edits or reorder them.',
279
+ );
280
+ }
281
+ }
282
+
283
+ const actualOld = findActualString(updated, oldString);
284
+ if (actualOld === null) {
285
+ throw new Error('old_string was not found in the file. Read it again and use exact text.');
286
+ }
287
+ const occurrences = actualOld === '' ? 0 : updated.split(actualOld).length - 1;
288
+ if (occurrences === 0) {
289
+ throw new Error('old_string was not found in the file. Read it again and use exact text.');
290
+ }
291
+ if (occurrences > 1 && edit.replace_all !== true) {
292
+ throw new Error(
293
+ `old_string appears ${occurrences} times. Pass replace_all=true or include more context.`,
294
+ );
295
+ }
296
+
297
+ updated =
298
+ edit.replace_all === true
299
+ ? updated.split(actualOld).join(newString)
300
+ : updated.replace(actualOld, () => newString);
301
+ replacedCount += edit.replace_all === true ? occurrences : 1;
302
+ appliedNewStrings.push(newString);
219
303
  }
220
304
 
221
- const updated =
222
- input.replace_all === true
223
- ? original.split(actualOld).join(newString)
224
- : original.replace(actualOld, () => newString);
225
305
  await fs.writeFile(absolute, updated, 'utf8');
226
306
 
227
- const replacedCount = input.replace_all === true ? occurrences : 1;
228
- const before = original.split(actualOld)[0] ?? '';
229
- const replacementLine = before.split(/\r?\n/).length - 1;
230
- const snippetStart = Math.max(0, replacementLine - SNIPPET_CONTEXT_LINES);
231
- const snippet = snippetAfterEdit(original, actualOld, newString, snippetStart);
307
+ let snippet = '';
308
+ if (editList.length === 1) {
309
+ const actualOld = findActualString(original, String(editList[0]!.old_string)) ?? '';
310
+ const before = original.split(actualOld)[0] ?? '';
311
+ const replacementLine = before.split(/\r?\n/).length - 1;
312
+ const snippetStart = Math.max(0, replacementLine - SNIPPET_CONTEXT_LINES);
313
+ snippet = `\n\n${snippetAfterEdit(original, actualOld, String(editList[0]!.new_string), snippetStart)}`;
314
+ }
232
315
 
233
316
  return {
234
317
  output:
235
- `Replaced ${replacedCount} occurrence(s) in ${displayPath(ctx.cwd, absolute)}\n\n` +
236
- `${snippet}`,
318
+ `Replaced ${replacedCount} occurrence(s) in ${displayPath(ctx.cwd, absolute)}${snippet}`,
237
319
  diff: lineDiff(original, updated) || undefined,
238
320
  };
239
321
  },
@@ -1,3 +1,4 @@
1
+ import { execFile } from 'node:child_process';
1
2
  import { promises as fs } from 'node:fs';
2
3
  import path from 'node:path';
3
4
 
@@ -78,6 +79,45 @@ function escapeRegex(value: string): string {
78
79
  return value.replace(/[.+^${}()|[\]\\]/g, '\\$&');
79
80
  }
80
81
 
82
+ function runGit(args: string[], cwd: string): Promise<string> {
83
+ return new Promise((resolve, reject) => {
84
+ execFile('git', args, { cwd, timeout: 5000 }, (error, stdout) => {
85
+ if (error) reject(error);
86
+ else resolve(String(stdout));
87
+ });
88
+ });
89
+ }
90
+
91
+ /**
92
+ * Files under root, honoring .gitignore when inside a git repository:
93
+ * `git ls-files --cached --others --exclude-standard` gives exact git
94
+ * semantics for free. Falls back to the directory walk outside repos.
95
+ */
96
+ export async function workspaceFiles(root: string, signal?: AbortSignal): Promise<string[]> {
97
+ try {
98
+ const topLevel = (await runGit(['rev-parse', '--show-toplevel'], root)).trim();
99
+ const listing = await runGit(
100
+ ['ls-files', '--cached', '--others', '--exclude-standard', '--full-name'],
101
+ root,
102
+ );
103
+ if (!topLevel || signal?.aborted) throw new Error('fallback');
104
+ // git reports physical paths (/private/var/... on macOS) while callers
105
+ // may hold symlinked ones (/var/...); normalize before filtering.
106
+ const physicalRoot = await fs.realpath(root);
107
+ const prefix = topLevel.endsWith(path.sep) ? topLevel : `${topLevel}${path.sep}`;
108
+ const files = listing
109
+ .split('\n')
110
+ .map((line) => line.trim())
111
+ .filter(Boolean)
112
+ .map((line) => `${prefix}${line}`)
113
+ .filter((file) => file.startsWith(`${physicalRoot}${path.sep}`));
114
+ if (files.length === 0) throw new Error('fallback');
115
+ return files;
116
+ } catch {
117
+ return walkFiles(root, signal);
118
+ }
119
+ }
120
+
81
121
  export function globToRegExp(pattern: string): RegExp {
82
122
  let p = pattern.replace(/\\/g, '/').replace(/^\.\//, '');
83
123
  p = escapeRegex(p);
@@ -4,15 +4,35 @@ import type { AgentTool, InvokeResult, ToolContext } from './types.js';
4
4
  import { bashTool } from './bash.js';
5
5
  import { editFileTool, readFileTool, writeFileTool } from './files.js';
6
6
  import { globTool, grepTool } from './search.js';
7
+ import { todoWriteTool } from './todo.js';
8
+ import { createAskModelTool } from './ask-model.js';
9
+ import { createDispatchAgentTool } from './dispatch.js';
10
+ import { readImageBlocks } from './files.js';
11
+ import { askRole as providerAskRole } from '../../provider/provider.js';
7
12
 
8
- export const AGENT_TOOLS: AgentTool[] = [
13
+ const askModelTool = createAskModelTool(async (role, prompt, images, ctx) => {
14
+ const blocks = images?.length && ctx ? await readImageBlocks(images, ctx) : undefined;
15
+ return providerAskRole(role, prompt, blocks);
16
+ });
17
+
18
+ // The dispatch pool deliberately excludes dispatch_agent itself (no recursion).
19
+ const dispatchPool = [
9
20
  readFileTool,
10
21
  writeFileTool,
11
22
  editFileTool,
12
23
  globTool,
13
24
  grepTool,
14
25
  bashTool,
26
+ todoWriteTool,
27
+ askModelTool,
15
28
  ];
29
+ const dispatchAgentTool = createDispatchAgentTool({
30
+ runLoop: (options) =>
31
+ import('../loop.js').then(({ runAgentLoop }) => runAgentLoop(options)),
32
+ tools: dispatchPool,
33
+ });
34
+
35
+ export const AGENT_TOOLS: AgentTool[] = [...dispatchPool, dispatchAgentTool];
16
36
 
17
37
  export function findAgentTool(name: string): AgentTool | undefined {
18
38
  return AGENT_TOOLS.find((item) => item.name === name);
@@ -49,7 +69,12 @@ export async function invokeTool(
49
69
  if (typeof result === 'string') {
50
70
  return { ok: true, output: result || '(no output)' };
51
71
  }
52
- return { ok: true, output: result.output || '(no output)', diff: result.diff };
72
+ return {
73
+ ok: true,
74
+ output: result.output || '(no output)',
75
+ diff: result.diff,
76
+ images: result.images,
77
+ };
53
78
  } catch (error) {
54
79
  return { ok: false, output: `Error: ${(error as Error).message}` };
55
80
  }
@@ -1,5 +1,6 @@
1
1
  import { afterEach, describe, expect, test } from 'bun:test';
2
- import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { execFileSync } from 'node:child_process';
3
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3
4
  import { tmpdir } from 'node:os';
4
5
  import path from 'node:path';
5
6
  import { globTool, grepTool } from './search.js';
@@ -52,4 +53,57 @@ describe('grep', () => {
52
53
  const output = await grepTool.execute({ pattern: 'nope-xyz' }, ctx(session));
53
54
  expect(output).toContain('No matches');
54
55
  });
56
+
57
+ test('context lines appear with - markers and -- group separators', async () => {
58
+ const dir = workspace();
59
+ const lines = Array.from({ length: 12 }, (_, i) => `line ${i + 1}`);
60
+ writeFileSync(path.join(dir, 'ctx.txt'), `${lines.join('\n')}\n`);
61
+
62
+ const session = createToolSession(dir);
63
+ const output = await grepTool.execute(
64
+ { pattern: 'line 2$|line 10', context: 1 },
65
+ ctx(session),
66
+ );
67
+
68
+ expect(output).toContain('ctx.txt:2: line 2');
69
+ expect(output).toContain('ctx.txt-3- line 3');
70
+ expect(output).toContain('ctx.txt:10: line 10');
71
+ expect(output).toContain('--');
72
+ });
73
+
74
+ test('multiline patterns match across lines only with multiline: true', async () => {
75
+ const dir = workspace();
76
+ writeFileSync(path.join(dir, 'fn.ts'), 'function foo(\n x: number\n) {\n return x;\n}\n');
77
+
78
+ const session = createToolSession(dir);
79
+ const single = await grepTool.execute({ pattern: 'foo\\([\\s\\S]*?return' }, ctx(session));
80
+ expect(single).toContain('No matches');
81
+
82
+ const multi = await grepTool.execute(
83
+ { pattern: 'foo\\([\\s\\S]*?return', multiline: true },
84
+ ctx(session),
85
+ );
86
+ expect(multi).toContain('fn.ts:1-4:');
87
+ });
88
+
89
+ test('respects .gitignore inside a git repository', async () => {
90
+ const dir = workspace();
91
+ execFileSync('git', ['init', '-q'], { cwd: dir });
92
+ writeFileSync(path.join(dir, '.gitignore'), 'ignored-dir/\n*.tmp\n');
93
+ writeFileSync(path.join(dir, 'src.txt'), 'target found here\n');
94
+ mkdirSync(path.join(dir, 'ignored-dir'));
95
+ writeFileSync(path.join(dir, 'ignored-dir', 'hidden.txt'), 'target also here\n');
96
+ writeFileSync(path.join(dir, 'noise.tmp'), 'target noise\n');
97
+
98
+ const session = createToolSession(dir);
99
+ const output = await grepTool.execute({ pattern: 'target' }, ctx(session));
100
+ expect(output).toContain('src.txt');
101
+ expect(output).not.toContain('hidden.txt');
102
+ expect(output).not.toContain('noise.tmp');
103
+
104
+ const files = await globTool.execute({ pattern: '**/*' }, ctx(session));
105
+ expect(files).toContain('src.txt');
106
+ expect(files).not.toContain('hidden.txt');
107
+ expect(files).not.toContain('noise.tmp');
108
+ });
55
109
  });
@@ -9,7 +9,7 @@ import {
9
9
  matchesGlob,
10
10
  readTextIfTextFile,
11
11
  resolveInsideWorkspace,
12
- walkFiles,
12
+ workspaceFiles,
13
13
  } from './fs-utils.js';
14
14
 
15
15
  export const globTool: AgentTool = {
@@ -27,7 +27,7 @@ export const globTool: AgentTool = {
27
27
  async execute(input, ctx) {
28
28
  const root = resolveInsideWorkspace(ctx.cwd, String(input.path ?? '.'));
29
29
  const pattern = String(input.pattern);
30
- const files = await walkFiles(root, ctx.signal);
30
+ const files = await workspaceFiles(root, ctx.signal);
31
31
  const matches: string[] = [];
32
32
 
33
33
  for (const file of files) {
@@ -59,21 +59,27 @@ export const grepTool: AgentTool = {
59
59
  path: z.string().nullable().optional().describe('Directory or file to search (default: workspace root)'),
60
60
  include: z.string().nullable().optional().describe('Glob that file paths must match'),
61
61
  ignore_case: z.boolean().nullable().optional().describe('Case-insensitive search'),
62
+ multiline: z.boolean().nullable().optional().describe('Let the pattern match across line breaks'),
63
+ context: z.number().int().min(0).max(10).nullable().optional().describe('Show N context lines around each match'),
62
64
  }),
63
65
  title: (input) => `search: ${String(input.pattern ?? '')}`,
64
66
  async execute(input, ctx) {
65
67
  let regex: RegExp;
66
68
  try {
67
- regex = new RegExp(String(input.pattern), input.ignore_case ? 'i' : '');
69
+ regex = new RegExp(
70
+ String(input.pattern),
71
+ `${input.ignore_case ? 'i' : ''}m${input.multiline ? 's' : ''}`,
72
+ );
68
73
  } catch (error) {
69
74
  throw new Error(`Invalid regular expression: ${(error as Error).message}`);
70
75
  }
71
76
 
72
77
  const target = resolveInsideWorkspace(ctx.cwd, String(input.path ?? '.'));
73
78
  const stat = await fs.stat(target);
74
- const files = stat.isFile() ? [target] : await walkFiles(target, ctx.signal);
79
+ const files = stat.isFile() ? [target] : await workspaceFiles(target, ctx.signal);
75
80
  const include = input.include ? String(input.include) : undefined;
76
81
  const results: string[] = [];
82
+ const context = Number(input.context ?? 0);
77
83
 
78
84
  for (const file of files) {
79
85
  if (ctx.signal?.aborted) throw new Error('Aborted');
@@ -89,12 +95,54 @@ export const grepTool: AgentTool = {
89
95
  if (content === null) continue;
90
96
 
91
97
  const lines = content.split('\n');
92
- for (let index = 0; index < lines.length; index++) {
98
+
99
+ if (input.multiline === true) {
93
100
  regex.lastIndex = 0;
94
- if (!regex.test(lines[index])) continue;
95
- const text = lines[index].length > 500 ? `${lines[index].slice(0, 500)}...` : lines[index];
96
- results.push(`${relative}:${index + 1}: ${text}`);
101
+ let match: RegExpExecArray | null;
102
+ while ((match = regex.exec(content)) !== null) {
103
+ if (match[0] === '') break; // zero-length match guard
104
+ const startLine = content.slice(0, match.index).split('\n').length;
105
+ const endLine = startLine - 1 + match[0].split('\n').length;
106
+ const text = match[0].length > 400 ? `${match[0].slice(0, 400)}...` : match[0];
107
+ results.push(`${relative}:${startLine}-${endLine}: ${text.replace(/\n/g, '\\n')}`);
108
+ if (results.length >= MAX_GREP_MATCHES) break;
109
+ }
97
110
  if (results.length >= MAX_GREP_MATCHES) break;
111
+ continue;
112
+ }
113
+
114
+ const hits: number[] = [];
115
+ for (let index = 0; index < lines.length; index++) {
116
+ regex.lastIndex = 0;
117
+ if (regex.test(lines[index])) hits.push(index);
118
+ }
119
+
120
+ if (context > 0) {
121
+ // rg-style: hits as rel:N:, context as rel-N-, groups split by --.
122
+ const isHit = new Set(hits);
123
+ const shown = new Set<number>();
124
+ for (const hit of hits) {
125
+ for (let n = hit - context; n <= hit + context; n++) {
126
+ if (n >= 0 && n < lines.length) shown.add(n);
127
+ }
128
+ }
129
+ let previous = -2;
130
+ for (const index of [...shown].sort((a, b) => a - b)) {
131
+ if (previous >= 0 && index - previous > 1) results.push('--');
132
+ previous = index;
133
+ const raw = lines[index];
134
+ const text = raw.length > 500 ? `${raw.slice(0, 500)}...` : raw;
135
+ results.push(
136
+ isHit.has(index) ? `${relative}:${index + 1}: ${text}` : `${relative}-${index + 1}- ${text}`,
137
+ );
138
+ if (results.length >= MAX_GREP_MATCHES) break;
139
+ }
140
+ } else {
141
+ for (const hit of hits) {
142
+ const raw = lines[hit];
143
+ const text = raw.length > 500 ? `${raw.slice(0, 500)}...` : raw;
144
+ results.push(`${relative}:${hit + 1}: ${text}`);
145
+ }
98
146
  }
99
147
  if (results.length >= MAX_GREP_MATCHES) break;
100
148
  }
@@ -1,5 +1,11 @@
1
1
  import path from 'node:path';
2
2
 
3
+ /** One step of the model-maintained task list (todo_write tool). */
4
+ export interface TodoItem {
5
+ content: string;
6
+ status: 'pending' | 'in_progress' | 'completed';
7
+ }
8
+
3
9
  /**
4
10
  * State that persists across tool calls within one conversation/session:
5
11
  * the shell's current working directory (so `cd` survives between bash
@@ -15,6 +21,8 @@ export interface ToolSession {
15
21
  shellSnapshot?: Promise<string | undefined>;
16
22
  /** Absolute paths of files the model has read (or written) this session. */
17
23
  readFileState: Set<string>;
24
+ /** Current task list, owned by the todo_write tool. */
25
+ todos: TodoItem[];
18
26
  }
19
27
 
20
28
  export function createToolSession(rootCwd: string): ToolSession {
@@ -23,6 +31,7 @@ export function createToolSession(rootCwd: string): ToolSession {
23
31
  cwd: absolute,
24
32
  rootCwd: absolute,
25
33
  readFileState: new Set<string>(),
34
+ todos: [],
26
35
  };
27
36
  }
28
37
 
@@ -0,0 +1,54 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { z } from 'zod';
3
+ import { findAgentTool } from './index.js';
4
+ import { createToolSession } from './session.js';
5
+ import { todoWriteTool, type TodoItem } from './todo.js';
6
+
7
+ describe('todo_write', () => {
8
+ test('is registered in the default tool set', () => {
9
+ expect(findAgentTool('todo_write')).toBeDefined();
10
+ expect(todoWriteTool.isMutating).toBe(false);
11
+ });
12
+
13
+ test('replaces the whole list and returns a status checklist', async () => {
14
+ const session = createToolSession('/tmp');
15
+ const todos: TodoItem[] = [
16
+ { content: '调研仓库结构', status: 'completed' },
17
+ { content: '起草 README 方案', status: 'in_progress' },
18
+ { content: '写入 README', status: 'pending' },
19
+ ];
20
+
21
+ const output = await todoWriteTool.execute(
22
+ { todos },
23
+ { cwd: '/tmp', session },
24
+ );
25
+
26
+ expect(session.todos).toEqual(todos);
27
+ expect(output).toContain('[x]');
28
+ expect(output).toContain('[~]');
29
+ expect(output).toContain('[ ]');
30
+ expect(output).toContain('调研仓库结构');
31
+ });
32
+
33
+ test('reports completion when everything is done', async () => {
34
+ const session = createToolSession('/tmp');
35
+ const output = await todoWriteTool.execute(
36
+ { todos: [{ content: 'a', status: 'completed' }, { content: 'b', status: 'completed' }] },
37
+ { cwd: '/tmp', session },
38
+ );
39
+ expect(output).toMatch(/all complete/i);
40
+ });
41
+
42
+ test('rejects unknown status values via the schema', () => {
43
+ const parsed = todoWriteTool.schema.safeParse({
44
+ todos: [{ content: 'a', status: 'bogus' }],
45
+ });
46
+ expect(parsed.success).toBe(false);
47
+ });
48
+
49
+ test('schema matches the OpenAI nullable-optional convention', () => {
50
+ const shape = todoWriteTool.schema.shape;
51
+ void z;
52
+ expect(Object.keys(shape)).toEqual(['todos']);
53
+ });
54
+ });