Elffuss BraidHub

kikocis/continue public

4940c71be2bf · 5.2 KiBRawPermalink
1import { Tool } from "../..";
2import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";
3import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage";
4
5export interface SearchAndReplaceInFileArgs {
6 filepath: string;
7 diffs: string[];
8}
9
10// Currently unsupported and filtered out in `gui/src/redux/slices/sessionSlice.ts`
11export const NO_PARALLEL_TOOL_CALLING_INSRUCTION =
12 "Note this tool CANNOT be called in parallel.";
13
14const SEARCH_AND_REPLACE_EXAMPLE_BLOCK = `------- SEARCH
15[exact content to find]
16=======
17[new content to replace with]
18+++++++ REPLACE`;
19
20const SEARCH_AND_REPLACE_RULES = `Critical rules:
211. SEARCH content must match the associated file section to find EXACTLY:
22 * Match character-for-character including whitespace, indentation, line endings
23 * Include all comments, docstrings, etc.
242. SEARCH/REPLACE blocks will ONLY replace the first match occurrence.
25 * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes.
26 * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change.
27 * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file.
283. **Order matters**: DIFFs in the array should be ordered from top to bottom of the file to ensure correct application.
294. Keep SEARCH/REPLACE blocks concise:
30 * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file.
31 * Include just the changing lines, and a few surrounding lines if needed for uniqueness.
32 * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks.
33 * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures.
345. Splitting up tool calls:
35 * When making multiple closely related edits to the same file, you should try to use multiple SEARCH/REPLACE blocks in a single tool call, rather than making multiple separate tool calls
36 * If you need to make follow up edits or group your work into logical segments, it is okay to perform additional tool calls
37 * DO NOT make back-to-back tool calls. Instead interleave with brief explanation of what each will do. For example, instead of [explanation, tool call, tool call] you should do [explanation, tool call, explanation, tool call]
386. Special operations:
39 * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location)
40 * To delete code: Use empty REPLACE section
417. You should always read the file before trying to edit it, to ensure you know the up-to-date file contents`;
42
43const SEARCH_AND_REPLACE_DIFFS_DESCRIPTION = `An array of strings, each containing one or more SEARCH/REPLACE blocks following this exact format:
44\`\`\`
45${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}
46\`\`\`
47
48${SEARCH_AND_REPLACE_RULES}
49
50Usage:
51\`\`\`
52${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}
53
54${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}
55\`\`\`
56
57Each string in the diffs array can contain multiple SEARCH/REPLACE blocks, and all will be applied sequentially in the order they appear.`;
58
59/**
60 * This tool is in an experimental state.
61 * Our starting point is heavily inspired by Cline's `replace_in_file` tool: https://github.com/cline/cline/blob/2709ccefcddc616e89a70962f017bcbbca1f17bf/src/core/prompts/system.ts#L87-L121
62 */
63export const searchAndReplaceInFileTool: Tool = {
64 type: "function",
65 displayTitle: "Edit File",
66 wouldLikeTo: "edit {{{ filepath }}}",
67 isCurrently: "editing {{{ filepath }}}",
68 hasAlready: "edited {{{ filepath }}}",
69 group: BUILT_IN_GROUP_NAME,
70 readonly: false,
71 isInstant: false,
72 function: {
73 name: BuiltInToolNames.SearchAndReplaceInFile,
74 description: `Request to replace sections of content in an existing file using multiple SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file. ${NO_PARALLEL_TOOL_CALLING_INSRUCTION}`,
75 parameters: {
76 type: "object",
77 required: ["filepath", "diffs"],
78 properties: {
79 filepath: {
80 type: "string",
81 description: `The path of the file to modify, relative to the root of the workspace.`,
82 },
83 diffs: {
84 type: "array",
85 items: {
86 type: "string",
87 },
88 description: SEARCH_AND_REPLACE_DIFFS_DESCRIPTION,
89 },
90 },
91 },
92 },
93 systemMessageDescription: createSystemMessageExampleCall(
94 BuiltInToolNames.SearchAndReplaceInFile,
95 `To make targeted edits by replacing sections of content in an existing file, use the ${BuiltInToolNames.SearchAndReplaceInFile} tool with a filepath (relative to the root of the workspace) and a "diffs" argument containing an array of SEARCH/REPLACE blocks that define exact changes to specific parts of the file.
96Each block should follow this format:
97${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}
98
99${SEARCH_AND_REPLACE_RULES}
100
101 For example, you could respond with:`,
102 [
103 ["filepath", "path/to/file.ts"],
104 [
105 "diffs",
106 `[
107"${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}",
108"${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}"
109]`,
110 ],
111 ],
112 ),
113 defaultToolPolicy: "allowedWithPermission",
114};