1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'fs';
import path from 'path';
import * as Diff from 'diff';
import {
BaseTool,
ToolCallConfirmationDetails,
ToolConfirmationOutcome,
ToolEditConfirmationDetails,
ToolResult,
ToolResultDisplay,
} from './tools.js';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { makeRelative, shortenPath } from '../utils/paths.js';
import { isNodeError } from '../utils/errors.js';
import { GeminiClient } from '../core/client.js';
import { Config, ApprovalMode } from '../config/config.js';
import { ensureCorrectEdit } from '../utils/editCorrector.js';
import { DEFAULT_DIFF_OPTIONS } from './diffOptions.js';
import { ReadFileTool } from './read-file.js';
/**
* Parameters for the Edit tool
*/
export interface EditToolParams {
/**
* The absolute path to the file to modify
*/
file_path: string;
/**
* Array of edits to apply
*/
edits: Array<{
old_string: string;
new_string: string;
}>;
/**
* Number of replacements expected. Defaults to 1 if not specified.
* Use when you want to replace multiple occurrences.
*/
expected_replacements?: number;
}
interface EditResult extends ToolResult {
editsApplied: number;
editsAttempted: number;
editsFailed: number;
failedEdits?: Array<{
index: number;
oldString: string;
newString: string;
error: string;
}>;
}
interface FailedEdit {
index: number;
oldString: string;
newString: string;
error: string;
}
/**
* Implementation of the Edit tool logic
*/
export class EditTool extends BaseTool<EditToolParams, EditResult> {
static readonly Name = 'replace';
private readonly config: Config;
private readonly rootDirectory: string;
private readonly client: GeminiClient;
/**
* Creates a new instance of the EditLogic
* @param rootDirectory Root directory to ground this tool in.
*/
constructor(config: Config) {
super(
EditTool.Name,
'EditFile',
`Replaces text within a file. By default, replaces a single occurrence, but can replace multiple occurrences when \`expected_replacements\` is specified. This tool also supports batch editing with multiple edits in a single operation. Requires providing significant context around the change to ensure precise targeting. Always use the ${ReadFileTool.Name} tool to examine the file's current content before attempting a text replacement.
Expectation for required parameters:
1. \`file_path\` MUST be an absolute path; otherwise an error will be thrown.
2. \`old_string\` MUST be the exact literal text to replace (including all whitespace, indentation, newlines, and surrounding code etc.).
3. \`new_string\` MUST be the exact literal text to replace \`old_string\` with (also including all whitespace, indentation, newlines, and surrounding code etc.). Ensure the resulting code is correct and idiomatic.
4. NEVER escape \`old_string\` or \`new_string\`, that would break the exact literal text requirement.
**Important:** If ANY of the above are not satisfied, the tool will fail. CRITICAL for \`old_string\`: Must uniquely identify the single instance to change. Include at least 3 lines of context BEFORE and AFTER the target text, matching whitespace and indentation precisely. If this string matches multiple locations, or does not match exactly, the tool will fail.
**Multiple replacements:** Set \`expected_replacements\` to the number of occurrences you want to replace. The tool will replace ALL occurrences that match \`old_string\` exactly. Ensure the number of replacements matches your expectation.`,
{
properties: {
file_path: {
description:
"The absolute path to the file to modify. Must start with '/'.",
type: 'string',
},
edits: {
description:
'Array of edit operations to apply. Each edit should have old_string and new_string properties.',
type: 'array',
items: {
type: 'object',
properties: {
old_string: {
description:
'The exact literal text to replace, preferably unescaped. CRITICAL: Must uniquely identify the single instance to change. Include at least 3 lines of context BEFORE and AFTER the target text, matching whitespace and indentation precisely.',
type: 'string',
},
new_string: {
description:
'The exact literal text to replace old_string with, preferably unescaped. Provide the EXACT text. Ensure the resulting code is correct and idiomatic.',
type: 'string',
},
},
required: ['old_string', 'new_string'],
},
},
expected_replacements: {
type: 'number',
description:
'Number of replacements expected. Defaults to 1 if not specified. Use when you want to replace multiple occurrences.',
minimum: 1,
},
},
required: ['file_path', 'edits'],
type: 'object',
},
);
this.config = config;
this.rootDirectory = path.resolve(this.config.getTargetDir());
this.client = config.getGeminiClient();
}
/**
* Checks if a path is within the root directory.
* @param pathToCheck The absolute path to check.
* @returns True if the path is within the root directory, false otherwise.
*/
private isWithinRoot(pathToCheck: string): boolean {
const normalizedPath = path.normalize(pathToCheck);
const normalizedRoot = this.rootDirectory;
const rootWithSep = normalizedRoot.endsWith(path.sep)
? normalizedRoot
: normalizedRoot + path.sep;
return (
normalizedPath === normalizedRoot ||
normalizedPath.startsWith(rootWithSep)
);
}
/**
* Validates the parameters for the Edit tool
* @param params Parameters to validate
* @returns Error message string or null if valid
*/
validateToolParams(params: EditToolParams): string | null {
if (
this.schema.parameters &&
!SchemaValidator.validate(
this.schema.parameters as Record<string, unknown>,
params,
)
) {
return 'Parameters failed schema validation.';
}
if (!path.isAbsolute(params.file_path)) {
return `File path must be absolute: ${params.file_path}`;
}
if (!this.isWithinRoot(params.file_path)) {
return `File path must be within the root directory (${this.rootDirectory}): ${params.file_path}`;
}
// Validate that edits array is provided and not empty
if (!params.edits || params.edits.length === 0) {
return 'Must provide "edits" array with at least one edit.';
}
return null;
}
private _applyReplacement(
currentContent: string | null,
oldString: string,
newString: string,
isNewFile: boolean,
): string {
if (isNewFile) {
return newString;
}
if (currentContent === null) {
// Should not happen if not a new file, but defensively return empty or newString if oldString is also empty
return oldString === '' ? newString : '';
}
// If oldString is empty and it's not a new file, do not modify the content.
if (oldString === '' && !isNewFile) {
return currentContent;
}
return currentContent.replaceAll(oldString, newString);
}
/**
* Applies multiple edits to file content in sequence
* @param params Edit parameters
* @param abortSignal Abort signal for cancellation
* @returns Result with detailed edit metrics
*/
private async applyMultipleEdits(
params: EditToolParams,
abortSignal: AbortSignal,
): Promise<{
newContent: string;
editsApplied: number;
editsAttempted: number;
editsFailed: number;
failedEdits: FailedEdit[];
isNewFile: boolean;
originalContent: string | null;
}> {
// Read current file content or determine if this is a new file
let currentContent: string | null = null;
let fileExists = false;
let isNewFile = false;
try {
currentContent = fs.readFileSync(params.file_path, 'utf8');
fileExists = true;
} catch (err: unknown) {
if (!isNodeError(err) || err.code !== 'ENOENT') {
throw err;
}
}
// If file doesn't exist and first edit has empty old_string, it's file creation
if (!fileExists && params.edits[0].old_string === '') {
isNewFile = true;
currentContent = '';
} else if (!fileExists) {
throw new Error(`File does not exist: ${params.file_path}`);
} else if (fileExists && params.edits[0].old_string === '') {
// Protect against accidentally creating a file that already exists
throw new Error(`File already exists: ${params.file_path}`);
}
const expectedReplacements = params.expected_replacements ?? 1;
const result = {
newContent: currentContent || '',
editsApplied: 0,
editsAttempted: params.edits.length,
editsFailed: 0,
failedEdits: [] as FailedEdit[],
isNewFile,
originalContent: currentContent,
};
// Apply each edit
for (let i = 0; i < params.edits.length; i++) {
const edit = params.edits[i];
// Handle new file creation with empty old_string
if (isNewFile && edit.old_string === '') {
result.newContent = edit.new_string;
result.editsApplied++;
continue;
}
// Use edit corrector for better matching
try {
const correctedEdit = await ensureCorrectEdit(
result.newContent,
{
...params,
old_string: edit.old_string,
new_string: edit.new_string,
},
this.client,
abortSignal,
);
// Handle both single and multiple replacements based on expected_replacements
if (expectedReplacements === 1 && correctedEdit.occurrences === 1) {
result.newContent = result.newContent.replace(
correctedEdit.params.old_string,
correctedEdit.params.new_string,
);
result.editsApplied++;
} else if (
expectedReplacements > 1 &&
correctedEdit.occurrences === expectedReplacements
) {
result.newContent = result.newContent.replaceAll(
correctedEdit.params.old_string,
correctedEdit.params.new_string,
);
result.editsApplied++;
} else {
result.editsFailed++;
result.failedEdits.push({
index: i,
oldString: edit.old_string,
newString: edit.new_string,
error:
correctedEdit.occurrences === 0
? 'String not found'
: `Expected ${expectedReplacements} occurrences but found ${correctedEdit.occurrences}`,
});
}
} catch (error) {
result.editsFailed++;
result.failedEdits.push({
index: i,
oldString: edit.old_string,
newString: edit.new_string,
error: error instanceof Error ? error.message : String(error),
});
}
}
return result;
}
/**
* Handles the confirmation prompt for the Edit tool in the CLI.
* It needs to calculate the diff to show the user.
*/
async shouldConfirmExecute(
params: EditToolParams,
abortSignal: AbortSignal,
): Promise<ToolCallConfirmationDetails | false> {
if (this.config.getApprovalMode() === ApprovalMode.AUTO_EDIT) {
return false;
}
const validationError = this.validateToolParams(params);
if (validationError) {
console.error(
`[EditTool Wrapper] Attempted confirmation with invalid parameters: ${validationError}`,
);
return false;
}
try {
// Calculate what the edits would produce
const editResult = await this.applyMultipleEdits(params, abortSignal);
// Don't show confirmation if no edits would be applied
if (editResult.editsApplied === 0 && !editResult.isNewFile) {
return false;
}
// Read current content for diff comparison
let currentContent: string | null = null;
try {
currentContent = fs.readFileSync(params.file_path, 'utf8');
} catch (err: unknown) {
if (isNodeError(err) && err.code === 'ENOENT') {
currentContent = '';
} else {
console.error(`Error reading file for confirmation diff: ${err}`);
return false;
}
}
// Generate diff for confirmation
const fileName = path.basename(params.file_path);
const fileDiff = Diff.createPatch(
fileName,
currentContent || '',
editResult.newContent,
'Current',
'Proposed',
DEFAULT_DIFF_OPTIONS,
);
const editsCount = params.edits.length;
const title =
editsCount > 1
? `Confirm ${editsCount} Edits: ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`
: `Confirm Edit: ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`;
const confirmationDetails: ToolEditConfirmationDetails = {
type: 'edit',
title,
fileName,
fileDiff,
onConfirm: async (outcome: ToolConfirmationOutcome) => {
if (outcome === ToolConfirmationOutcome.ProceedAlways) {
this.config.setApprovalMode(ApprovalMode.AUTO_EDIT);
}
},
};
return confirmationDetails;
} catch (error) {
console.error(`Error generating confirmation diff: ${error}`);
return false;
}
}
getDescription(params: EditToolParams): string {
if (!params.file_path) {
return `Model did not provide valid parameters for edit tool`;
}
const relativePath = makeRelative(params.file_path, this.rootDirectory);
if (!params.edits || params.edits.length === 0) {
return `Edit ${shortenPath(relativePath)}`;
}
if (params.edits.length === 1) {
const edit = params.edits[0];
if (edit.old_string === '') {
return `Create ${shortenPath(relativePath)}`;
}
const oldSnippet =
edit.old_string.split('\n')[0].substring(0, 30) +
(edit.old_string.length > 30 ? '...' : '');
const newSnippet =
edit.new_string.split('\n')[0].substring(0, 30) +
(edit.new_string.length > 30 ? '...' : '');
return `${shortenPath(relativePath)}: ${oldSnippet} => ${newSnippet}`;
} else {
return `Edit ${shortenPath(relativePath)} (${params.edits.length} edits)`;
}
}
/**
* Executes the edit operation with the given parameters.
* @param params Parameters for the edit operation
* @returns Result of the edit operation
*/
async execute(
params: EditToolParams,
abortSignal: AbortSignal,
): Promise<EditResult> {
const validationError = this.validateToolParams(params);
if (validationError) {
return {
llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`,
returnDisplay: `Error: ${validationError}`,
editsApplied: 0,
editsAttempted: 0,
editsFailed: 1,
};
}
try {
const editResult = await this.applyMultipleEdits(params, abortSignal);
// Apply the changes to the file
this.ensureParentDirectoriesExist(params.file_path);
fs.writeFileSync(params.file_path, editResult.newContent, 'utf8');
// Generate appropriate response messages
let displayResult: ToolResultDisplay;
let llmContent: string;
if (editResult.isNewFile) {
displayResult = `Created ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`;
llmContent = `Created new file: ${params.file_path}`;
} else if (editResult.editsApplied > 0) {
// Generate diff for display using original content before writing
const fileName = path.basename(params.file_path);
// Use the original content from before the edit was applied
const originalContent = editResult.originalContent || '';
const fileDiff = Diff.createPatch(
fileName,
originalContent,
editResult.newContent,
'Current',
'Proposed',
DEFAULT_DIFF_OPTIONS,
);
displayResult = { fileDiff, fileName };
llmContent = `Successfully applied ${editResult.editsApplied}/${editResult.editsAttempted} edits to ${params.file_path}`;
} else {
displayResult = `No edits applied to ${shortenPath(makeRelative(params.file_path, this.rootDirectory))}`;
llmContent = `Failed to apply any edits to ${params.file_path}`;
}
// Add details about failed edits
if (editResult.editsFailed > 0) {
const failureDetails = editResult.failedEdits
.map((f) => `Edit ${f.index + 1}: ${f.error}`)
.join('; ');
llmContent += `. Failed edits: ${failureDetails}`;
}
return {
llmContent,
returnDisplay: displayResult,
editsApplied: editResult.editsApplied,
editsAttempted: editResult.editsAttempted,
editsFailed: editResult.editsFailed,
failedEdits: editResult.failedEdits,
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
const editsAttempted = params.edits.length;
return {
llmContent: `Error executing edits: ${errorMsg}`,
returnDisplay: `Error: ${errorMsg}`,
editsApplied: 0,
editsAttempted,
editsFailed: editsAttempted,
};
}
}
/**
* Creates parent directories if they don't exist
*/
private ensureParentDirectoriesExist(filePath: string): void {
const dirName = path.dirname(filePath);
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, { recursive: true });
}
}
}
|