From bf43ec64e21fff93749f8a8d7e050600d63fcce2 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Wed, 25 Jun 2025 15:47:48 -0700 Subject: [PATCH] feat: search and replace tool def --- core/config/profile/doLoadConfig.ts | 2 + core/config/sharedConfig.ts | 7 +++ core/index.d.ts | 2 + core/llm/constructMessages.ts | 9 ++- core/tools/builtIn.ts | 6 +- core/tools/definitions/editFile.ts | 4 +- .../definitions/searchAndReplaceInFile.ts | 55 +++++++++++++++++++ core/tools/index.ts | 9 ++- extensions/vscode/package-lock.json | 2 +- gui/package-lock.json | 2 +- gui/src/pages/config/UserSettingsForm.tsx | 21 +++++-- gui/src/redux/slices/uiSlice.ts | 1 + 12 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 core/tools/definitions/searchAndReplaceInFile.ts diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index 7685546c9..f993f6212 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -229,6 +229,8 @@ export default async function doLoadConfig(options: { newConfig.tools.push( ...getConfigDependentToolDefinitions({ rules: newConfig.rules, + enableExperimentalTools: + newConfig.experimental?.enableExperimentalTools ?? false, }), ); diff --git a/core/config/sharedConfig.ts b/core/config/sharedConfig.ts index 439925188..25fe5f0e3 100644 --- a/core/config/sharedConfig.ts +++ b/core/config/sharedConfig.ts @@ -19,6 +19,7 @@ export const sharedConfigSchema = z readResponseTTS: z.boolean(), promptPath: z.string(), useCurrentFileAsContext: z.boolean(), + enableExperimentalTools: z.boolean(), // `ui` in `ContinueConfig` showSessionTabs: z.boolean(), @@ -157,6 +158,12 @@ export function modifyAnyConfigWithSharedConfig< configCopy.experimental = { ...configCopy.experimental, }; + + if (sharedConfig.enableExperimentalTools !== undefined) { + configCopy.experimental.enableExperimentalTools = + sharedConfig.enableExperimentalTools; + } + if (sharedConfig.promptPath !== undefined) { configCopy.experimental.promptPath = sharedConfig.promptPath; } diff --git a/core/index.d.ts b/core/index.d.ts index 9b54701ff..e3a9e2a6d 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1007,6 +1007,7 @@ interface ToolChoice { export interface ConfigDependentToolParams { rules: RuleWithSource[]; + enableExperimentalTools: boolean; } export type GetTool = (params: ConfigDependentToolParams) => Tool; @@ -1334,6 +1335,7 @@ export interface ExperimentalConfig { modelRoles?: ExperimentalModelRoles; defaultContext?: DefaultContextProvider[]; promptPath?: string; + enableExperimentalTools?: boolean; /** * Quick actions are a way to add custom commands to the Code Lens of diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 49115cb6f..dc9dbc8bf 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -19,10 +19,12 @@ export const DEFAULT_CHAT_SYSTEM_MESSAGE_URL = export const DEFAULT_AGENT_SYSTEM_MESSAGE_URL = "https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts"; -const EDIT_MESSAGE = `\ +export const CODEBLOCK_FORMATTING_INSTRUCTIONS = `\ Always include the language and file name in the info string when you write code blocks. If you are editing "src/main.py" for example, your code block should start with '\`\`\`python src/main.py' +`; +export const EDIT_CODE_INSTRUCTIONS = `\ When addressing code modification requests, present a concise code snippet that emphasizes only the necessary changes and uses abbreviated placeholders for unmodified sections. For example: @@ -69,14 +71,15 @@ export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\ If the user asks to make changes to files offer that they can use the Apply Button on the code block, or switch to Agent Mode to make the suggested updates automatically. If needed consisely explain to the user they can switch to agent mode using the Mode Selector dropdown and provide no other details. -${EDIT_MESSAGE} +${CODEBLOCK_FORMATTING_INSTRUCTIONS} +${EDIT_CODE_INSTRUCTIONS} `; export const DEFAULT_AGENT_SYSTEM_MESSAGE = `\ You are in agent mode. -${EDIT_MESSAGE} +${CODEBLOCK_FORMATTING_INSTRUCTIONS} `; /** diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index 5a2408a0c..00878dfeb 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -1,6 +1,7 @@ export enum BuiltInToolNames { ReadFile = "builtin_read_file", EditExistingFile = "builtin_edit_existing_file", + SearchAndReplaceInFile = "builtin_search_and_replace_in_file", ReadCurrentlyOpenFile = "builtin_read_currently_open_file", CreateNewFile = "builtin_create_new_file", RunTerminalCommand = "builtin_run_terminal_command", @@ -20,4 +21,7 @@ export enum BuiltInToolNames { export const BUILT_IN_GROUP_NAME = "Built-In"; -export const CLIENT_TOOLS_IMPLS = [BuiltInToolNames.EditExistingFile]; +export const CLIENT_TOOLS_IMPLS = [ + BuiltInToolNames.EditExistingFile, + BuiltInToolNames.SearchAndReplaceInFile, +]; diff --git a/core/tools/definitions/editFile.ts b/core/tools/definitions/editFile.ts index 6c6ad6fd9..53e18cd43 100644 --- a/core/tools/definitions/editFile.ts +++ b/core/tools/definitions/editFile.ts @@ -1,4 +1,5 @@ import { Tool } from "../.."; +import { EDIT_CODE_INSTRUCTIONS } from "../../llm/constructMessages"; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; export interface EditToolArgs { @@ -17,8 +18,7 @@ export const editFileTool: Tool = { isInstant: false, function: { name: BuiltInToolNames.EditExistingFile, - description: - "Use this tool to edit an existing file. If you don't know the contents of the file, read it first.", + description: `Use this tool to edit an existing file. If you don't know the contents of the file, read it first.\n${EDIT_CODE_INSTRUCTIONS}`, parameters: { type: "object", required: ["filepath", "changes"], diff --git a/core/tools/definitions/searchAndReplaceInFile.ts b/core/tools/definitions/searchAndReplaceInFile.ts new file mode 100644 index 000000000..6ce475e6b --- /dev/null +++ b/core/tools/definitions/searchAndReplaceInFile.ts @@ -0,0 +1,55 @@ +import { Tool } from "../.."; +import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; + +export interface SearchAndReplaceInFileArgs { + filepath: string; + diff: string; +} + +/** + * Critical requirements: +1. SEARCH content must match the file section EXACTLY (character-for-character, including whitespace and indentation) +2. Each SEARCH/REPLACE block replaces only the first matching occurrence +3. For multiple changes, use multiple SEARCH/REPLACE blocks in file order +4. Keep blocks concise - include only the lines that need to change plus minimal context for uniqueness +5. To delete code, use an empty REPLACE section +6. To move code, use two blocks (one to delete, one to insert at new location) + */ + +export const searchAndReplaceInFileTool: Tool = { + type: "function", + displayTitle: "Edit File", + wouldLikeTo: "edit {{{ filepath }}}", + isCurrently: "editing {{{ filepath }}}", + hasAlready: "edited {{{ filepath }}}", + group: BUILT_IN_GROUP_NAME, + readonly: false, + isInstant: false, + function: { + name: BuiltInToolNames.SearchAndReplaceInFile, + description: + "Use this tool to make precise modifications to existing files using SEARCH/REPLACE blocks. This tool is ideal for targeted changes to specific sections of a file.", + parameters: { + type: "object", + required: ["filepath", "diff"], + properties: { + filepath: { + type: "string", + description: + "The path of the file to modify, relative to the root of the workspace.", + }, + diff: { + type: "string", + description: `One or more SEARCH/REPLACE blocks following this exact format: +\`\`\` +------- SEARCH +[exact content to find] +======= +[new content to replace with] ++++++++ REPLACE +\`\`\``, + }, + }, + }, + }, +}; diff --git a/core/tools/index.ts b/core/tools/index.ts index c4d1c658b..b3429e53c 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -10,12 +10,12 @@ import { readCurrentlyOpenFileTool } from "./definitions/readCurrentlyOpenFile"; import { readFileTool } from "./definitions/readFile"; import { requestRuleTool } from "./definitions/requestRule"; import { runTerminalCommandTool } from "./definitions/runTerminalCommand"; +import { searchAndReplaceInFileTool } from "./definitions/searchAndReplaceInFile"; import { searchWebTool } from "./definitions/searchWeb"; import { viewDiffTool } from "./definitions/viewDiff"; export const baseToolDefinitions = [ readFileTool, - editFileTool, createNewFileTool, runTerminalCommandTool, grepSearchTool, @@ -33,4 +33,9 @@ export const baseToolDefinitions = [ export const getConfigDependentToolDefinitions = ( params: ConfigDependentToolParams, -): Tool[] => [requestRuleTool(params)]; +): Tool[] => [ + requestRuleTool(params), + ...(params.enableExperimentalTools + ? [searchAndReplaceInFileTool] + : [editFileTool]), +]; diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 9ebbad2d0..14d37a512 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -108,7 +108,7 @@ "@aws-sdk/credential-providers": "^3.778.0", "@continuedev/config-types": "^1.0.13", "@continuedev/config-yaml": "file:../packages/config-yaml", - "@continuedev/fetch": "^1.0.13", + "@continuedev/fetch": "^1.0.14", "@continuedev/llm-info": "^1.0.8", "@continuedev/openai-adapters": "file:../packages/openai-adapters", "@modelcontextprotocol/sdk": "^1.12.0", diff --git a/gui/package-lock.json b/gui/package-lock.json index 62eea45da..512c679b4 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -114,7 +114,7 @@ "@aws-sdk/credential-providers": "^3.778.0", "@continuedev/config-types": "^1.0.13", "@continuedev/config-yaml": "file:../packages/config-yaml", - "@continuedev/fetch": "^1.0.13", + "@continuedev/fetch": "^1.0.14", "@continuedev/llm-info": "^1.0.8", "@continuedev/openai-adapters": "file:../packages/openai-adapters", "@modelcontextprotocol/sdk": "^1.12.0", diff --git a/gui/src/pages/config/UserSettingsForm.tsx b/gui/src/pages/config/UserSettingsForm.tsx index 7f521b95f..39ae10596 100644 --- a/gui/src/pages/config/UserSettingsForm.tsx +++ b/gui/src/pages/config/UserSettingsForm.tsx @@ -78,7 +78,8 @@ export function UserSettingsForm() { const disableSessionTitles = config.disableSessionTitles ?? false; const useCurrentFileAsContext = config.experimental?.useCurrentFileAsContext ?? false; - + const enableExperimentalTools = + config.experimental?.enableExperimentalTools ?? false; const allowAnonymousTelemetry = config.allowAnonymousTelemetry ?? true; const disableIndexing = config.disableIndexing ?? false; @@ -106,9 +107,11 @@ export function UserSettingsForm() { const [hubEnabled, setHubEnabled] = useState(false); useEffect(() => { - ideMessenger.ide.getIdeSettings().then(({ continueTestEnvironment }) => { - setHubEnabled(continueTestEnvironment === "production"); - }); + void ideMessenger.ide + .getIdeSettings() + .then(({ continueTestEnvironment }) => { + setHubEnabled(continueTestEnvironment === "production"); + }); }, [ideMessenger]); return ( @@ -386,6 +389,16 @@ export function UserSettingsForm() { } text="Add Current File by Default" /> + + + handleUpdate({ + enableExperimentalTools: !enableExperimentalTools, + }) + } + text="Enable experimental tools" + /> diff --git a/gui/src/redux/slices/uiSlice.ts b/gui/src/redux/slices/uiSlice.ts index 15d6216a4..0efcfe2ce 100644 --- a/gui/src/redux/slices/uiSlice.ts +++ b/gui/src/redux/slices/uiSlice.ts @@ -63,6 +63,7 @@ export const uiSlice = createSlice({ [BuiltInToolNames.LSTool]: "allowedWithoutPermission", [BuiltInToolNames.CreateRuleBlock]: "allowedWithPermission", [BuiltInToolNames.RequestRule]: "disabled", + [BuiltInToolNames.SearchAndReplaceInFile]: "allowedWithPermission", }, toolGroupSettings: { [BUILT_IN_GROUP_NAME]: "include",