|
| 1 | +import z from 'zod/v4' |
| 2 | + |
| 3 | +import { $getNativeToolCallExampleString, jsonToolResultSchema } from '../utils' |
| 4 | + |
| 5 | +import type { $ToolParams } from '../../constants' |
| 6 | + |
| 7 | +export const proposeUpdateFileResultSchema = z.union([ |
| 8 | + z.object({ |
| 9 | + file: z.string(), |
| 10 | + message: z.string(), |
| 11 | + unifiedDiff: z.string(), |
| 12 | + }), |
| 13 | + z.object({ |
| 14 | + file: z.string(), |
| 15 | + errorMessage: z.string(), |
| 16 | + }), |
| 17 | +]) |
| 18 | + |
| 19 | +const toolName = 'propose_str_replace' |
| 20 | +const endsAgentStep = false |
| 21 | +const inputSchema = z |
| 22 | + .object({ |
| 23 | + path: z |
| 24 | + .string() |
| 25 | + .min(1, 'Path cannot be empty') |
| 26 | + .describe(`The path to the file to edit.`), |
| 27 | + replacements: z |
| 28 | + .array( |
| 29 | + z |
| 30 | + .object({ |
| 31 | + old: z |
| 32 | + .string() |
| 33 | + .min(1, 'Old cannot be empty') |
| 34 | + .describe( |
| 35 | + `The string to replace. This must be an *exact match* of the string you want to replace, including whitespace and punctuation.`, |
| 36 | + ), |
| 37 | + new: z |
| 38 | + .string() |
| 39 | + .describe( |
| 40 | + `The string to replace the corresponding old string with. Can be empty to delete.`, |
| 41 | + ), |
| 42 | + allowMultiple: z |
| 43 | + .boolean() |
| 44 | + .optional() |
| 45 | + .default(false) |
| 46 | + .describe( |
| 47 | + 'Whether to allow multiple replacements of old string.', |
| 48 | + ), |
| 49 | + }) |
| 50 | + .describe('Pair of old and new strings.'), |
| 51 | + ) |
| 52 | + .min(1, 'Replacements cannot be empty') |
| 53 | + .describe('Array of replacements to make.'), |
| 54 | + }) |
| 55 | + .describe(`Propose string replacements in a file without actually applying them.`) |
| 56 | +const description = ` |
| 57 | +Propose edits to a file without actually applying them. Use this tool when you want to draft changes that will be reviewed before being applied. |
| 58 | +
|
| 59 | +This tool works identically to str_replace but the changes are not written to disk. Instead, it returns the unified diff of what would change. Multiple propose calls on the same file will stack correctly. |
| 60 | +
|
| 61 | +Example: |
| 62 | +${$getNativeToolCallExampleString({ |
| 63 | + toolName, |
| 64 | + inputSchema, |
| 65 | + input: { |
| 66 | + path: 'path/to/file', |
| 67 | + replacements: [ |
| 68 | + { old: 'This is the old string', new: 'This is the new string' }, |
| 69 | + { |
| 70 | + old: '\nfoo:', |
| 71 | + new: '\nbar:', |
| 72 | + allowMultiple: true, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + endsAgentStep, |
| 77 | +})} |
| 78 | + `.trim() |
| 79 | + |
| 80 | +export const proposeStrReplaceParams = { |
| 81 | + toolName, |
| 82 | + endsAgentStep, |
| 83 | + description, |
| 84 | + inputSchema, |
| 85 | + outputSchema: jsonToolResultSchema(proposeUpdateFileResultSchema), |
| 86 | +} satisfies $ToolParams |
0 commit comments