From a04dccbc2a0ede1eb7af0505959e38334247228b Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sun, 20 Jul 2025 10:56:38 +0200 Subject: [PATCH] feat: custom systemMessageDescription for built in tools --- core/tools/definitions/codebaseTool.ts | 6 + core/tools/definitions/createNewFile.ts | 9 ++ core/tools/definitions/createRuleBlock.ts | 58 ++++++-- core/tools/definitions/editFile.ts | 22 ++- core/tools/definitions/fetchUrlContent.ts | 6 + core/tools/definitions/globSearch.ts | 6 + core/tools/definitions/grepSearch.ts | 6 + core/tools/definitions/ls.ts | 9 ++ .../definitions/readCurrentlyOpenFile.ts | 7 + core/tools/definitions/readFile.ts | 6 + core/tools/definitions/requestRule.test.ts | 20 +-- core/tools/definitions/requestRule.ts | 42 ++++-- core/tools/definitions/runTerminalCommand.ts | 29 ++-- .../definitions/searchAndReplaceInFile.ts | 127 +++++++++++------- core/tools/definitions/searchWeb.ts | 6 + core/tools/definitions/viewDiff.ts | 6 + core/tools/definitions/viewRepoMap.ts | 6 + core/tools/definitions/viewSubdirectory.ts | 6 + 18 files changed, 282 insertions(+), 95 deletions(-) diff --git a/core/tools/definitions/codebaseTool.ts b/core/tools/definitions/codebaseTool.ts index fffe51e02..0714a230b 100644 --- a/core/tools/definitions/codebaseTool.ts +++ b/core/tools/definitions/codebaseTool.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const codebaseTool: Tool = { type: "function", @@ -26,4 +27,9 @@ export const codebaseTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.CodebaseTool, + `To search the codebase, use the ${BuiltInToolNames.CodebaseTool} tool with a natural language query. For example, to find authentication logic, you might respond with:`, + [["query", "How is user authentication handled in this codebase?"]], + ), }; diff --git a/core/tools/definitions/createNewFile.ts b/core/tools/definitions/createNewFile.ts index 41bea0b91..152af5693 100644 --- a/core/tools/definitions/createNewFile.ts +++ b/core/tools/definitions/createNewFile.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const createNewFileTool: Tool = { type: "function", @@ -30,4 +31,12 @@ export const createNewFileTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.CreateNewFile, + `To create a NEW file, use the ${BuiltInToolNames.CreateNewFile} tool with the relative filepath and new contents. For example, to create a file located at 'path/to/file.txt', you would respond with:`, + [ + ["filepath", "path/to/the_file.txt"], + ["contents", "Contents of the file"], + ], + ), }; diff --git a/core/tools/definitions/createRuleBlock.ts b/core/tools/definitions/createRuleBlock.ts index 92aecb158..c7ce54d26 100644 --- a/core/tools/definitions/createRuleBlock.ts +++ b/core/tools/definitions/createRuleBlock.ts @@ -1,5 +1,20 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; + +const NAME_ARG_DESC = + "Short, descriptive name summarizing the rule's purpose (e.g. 'React Standards', 'Type Hints')"; +const RULE_ARG_DESC = + "Clear, imperative instruction for future code generation (e.g. 'Use named exports', 'Add Python type hints'). Each rule should focus on one specific standard."; +const DESC_ARG_DESC = + "Description of when this rule should be applied. Required for Agent Requested rules (AI decides when to apply). Optional for other types."; +const GLOB_ARG_DESC = + "Optional file patterns to which this rule applies (e.g. ['**/*.{ts,tsx}'] or ['src/**/*.ts', 'tests/**/*.ts'])"; +const REGEX_ARG_DESC = + "Optional regex patterns to match against file content. Rule applies only to files whose content matches the pattern (e.g. 'useEffect' for React hooks or '\\bclass\\b' for class definitions)"; + +const ALWAYS_APPLY_DESC = + "Whether this rule should always be applied. Set to false for Agent Requested and Manual rules. Omit or set to true for Always and Auto Attached rules."; export const createRuleBlock: Tool = { type: "function", @@ -20,35 +35,54 @@ export const createRuleBlock: Tool = { properties: { name: { type: "string", - description: - "Short, descriptive name summarizing the rule's purpose (e.g. 'React Standards', 'Type Hints')", + description: NAME_ARG_DESC, }, rule: { type: "string", - description: - "Clear, imperative instruction for future code generation (e.g. 'Use named exports', 'Add Python type hints'). Each rule should focus on one specific standard.", + description: RULE_ARG_DESC, }, description: { type: "string", - description: - "Description of when this rule should be applied. Required for Agent Requested rules (AI decides when to apply). Optional for other types.", + description: DESC_ARG_DESC, }, globs: { type: "string", - description: - "Optional file patterns to which this rule applies (e.g. ['**/*.{ts,tsx}'] or ['src/**/*.ts', 'tests/**/*.ts'])", + description: GLOB_ARG_DESC, }, regex: { type: "string", - description: - "Optional regex patterns to match against file content. Rule applies only to files whose content matches the pattern (e.g. 'useEffect' for React hooks or '\\bclass\\b' for class definitions)", + description: REGEX_ARG_DESC, }, alwaysApply: { type: "boolean", - description: - "Whether this rule should always be applied. Set to false for Agent Requested and Manual rules. Omit or set to true for Always and Auto Attached rules.", + description: ALWAYS_APPLY_DESC, }, }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.CreateRuleBlock, + `Sometimes the user will provide feedback or guidance on your output. If you were not aware of these "rules", consider using the ${BuiltInToolNames.CreateRuleBlock} tool to persist the rule for future interactions. +This tool cannot be used to edit existing rules, but you can search in the ".continue/rules" folder and use the edit tool to manage rules. +To create a rule, respond with a ${BuiltInToolNames.CreateRuleBlock} tool call and the following arguments: +- name: ${NAME_ARG_DESC} +- rule: ${RULE_ARG_DESC} +- description: ${DESC_ARG_DESC} +- globs: ${GLOB_ARG_DESC} +- alwaysApply: ${ALWAYS_APPLY_DESC} +For example:`, + [ + ["name", "Use PropTypes"], + [ + "rule", + "Always use PropTypes when declaring React component properties", + ], + [ + "description", + "Ensure that all prop types are explicitly declared for better type safety and code maintainability in React components.", + ], + ["globs", "**/*.js"], + ["alwaysApply", "false"], + ], + ), }; diff --git a/core/tools/definitions/editFile.ts b/core/tools/definitions/editFile.ts index 7abd658fc..59e20c681 100644 --- a/core/tools/definitions/editFile.ts +++ b/core/tools/definitions/editFile.ts @@ -1,6 +1,7 @@ import { Tool } from "../.."; import { EDIT_CODE_INSTRUCTIONS } from "../../llm/defaultSystemMessages"; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; import { NO_PARALLEL_TOOL_CALLING_INSRUCTION } from "./searchAndReplaceInFile"; export interface EditToolArgs { @@ -8,6 +9,9 @@ export interface EditToolArgs { changes: string; } +const CHANGES_DESCRIPTION = + "Any modifications to the file, showing only needed changes. Do NOT wrap this in a codeblock or write anything besides the code changes. In larger files, use brief language-appropriate placeholders for large unmodified sections, e.g. '// ... existing code ...'"; + export const editFileTool: Tool = { type: "function", displayTitle: "Edit File", @@ -31,10 +35,24 @@ export const editFileTool: Tool = { }, changes: { type: "string", - description: - "Any modifications to the file, showing only needed changes. Do NOT wrap this in a codeblock or write anything besides the code changes. In larger files, use brief language-appropriate placeholders for large unmodified sections, e.g. '// ... existing code ...'", + description: CHANGES_DESCRIPTION, }, }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.EditExistingFile, + `To edit an EXISTING file, use the ${BuiltInToolNames.EditExistingFile} tool with +- filepath: the relative filepath to the file. +- changes: ${CHANGES_DESCRIPTION} +Only use this tool if you already know the contents of the file. Otherwise, use the ${BuiltInToolNames.ReadFile} or ${BuiltInToolNames.ReadCurrentlyOpenFile} tool to read it first. +For example:`, + [ + ["filepath", "path/to/the_file.ts"], + [ + "changes", + "// ... existing code ...\nfunction subtract(a: number, b: number): number {\n return a - b;\n}\n// ... rest of code ...", + ], + ], + ), }; diff --git a/core/tools/definitions/fetchUrlContent.ts b/core/tools/definitions/fetchUrlContent.ts index e0122f55b..ae57fc532 100644 --- a/core/tools/definitions/fetchUrlContent.ts +++ b/core/tools/definitions/fetchUrlContent.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const fetchUrlContentTool: Tool = { type: "function", @@ -25,4 +26,9 @@ export const fetchUrlContentTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.FetchUrlContent, + `To fetch the content of a URL, use the ${BuiltInToolNames.FetchUrlContent} tool. For example, to read the contents of a webpage, you might respond with:`, + [["url", "https://example.com"]], + ), }; diff --git a/core/tools/definitions/globSearch.ts b/core/tools/definitions/globSearch.ts index 42e8afd2d..e0f195a66 100644 --- a/core/tools/definitions/globSearch.ts +++ b/core/tools/definitions/globSearch.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const globSearchTool: Tool = { type: "function", @@ -25,4 +26,9 @@ export const globSearchTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.FileGlobSearch, + `To return a list of files based on a glob search pattern, use the ${BuiltInToolNames.FileGlobSearch} tool`, + [["pattern", "*.py"]], + ), }; diff --git a/core/tools/definitions/grepSearch.ts b/core/tools/definitions/grepSearch.ts index 60a4e2710..e276d5a8f 100644 --- a/core/tools/definitions/grepSearch.ts +++ b/core/tools/definitions/grepSearch.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const grepSearchTool: Tool = { type: "function", @@ -26,4 +27,9 @@ export const grepSearchTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.GrepSearch, + `To perform a grep search within the project, call the ${BuiltInToolNames.GrepSearch} tool with the query pattern to match. For example:`, + [["query", ".*main_services.*"]], + ), }; diff --git a/core/tools/definitions/ls.ts b/core/tools/definitions/ls.ts index 979f9450b..35da8dd8f 100644 --- a/core/tools/definitions/ls.ts +++ b/core/tools/definitions/ls.ts @@ -1,6 +1,7 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const lsTool: Tool = { type: "function", @@ -31,4 +32,12 @@ export const lsTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.LSTool, + `To list files and folders in a given directory, call the ${BuiltInToolNames.LSTool} tool with "dirPath" and "recursive". For example:`, + [ + ["dirPath", "path/to/dir"], + ["recursive", "false"], + ], + ), }; diff --git a/core/tools/definitions/readCurrentlyOpenFile.ts b/core/tools/definitions/readCurrentlyOpenFile.ts index 9c15105a6..3f07f816f 100644 --- a/core/tools/definitions/readCurrentlyOpenFile.ts +++ b/core/tools/definitions/readCurrentlyOpenFile.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const readCurrentlyOpenFileTool: Tool = { type: "function", @@ -19,4 +20,10 @@ export const readCurrentlyOpenFileTool: Tool = { properties: {}, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.ReadCurrentlyOpenFile, + `To view the user's currently open file, use the ${BuiltInToolNames.ReadCurrentlyOpenFile} tool. +If the user is asking about a file and you don't see any code, use this to check the current file`, + [], + ), }; diff --git a/core/tools/definitions/readFile.ts b/core/tools/definitions/readFile.ts index 42737fe63..fb13eba05 100644 --- a/core/tools/definitions/readFile.ts +++ b/core/tools/definitions/readFile.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const readFileTool: Tool = { type: "function", @@ -26,4 +27,9 @@ export const readFileTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.ReadFile, + `To read a file with a known filepath, use the ${BuiltInToolNames.ReadFile} tool. For example, to read a file located at 'path/to/file.txt', you would respond with this:`, + [["filepath", "path/to/the_file.txt"]], + ), }; diff --git a/core/tools/definitions/requestRule.test.ts b/core/tools/definitions/requestRule.test.ts index e1d0feb81..0fd7b5de8 100644 --- a/core/tools/definitions/requestRule.test.ts +++ b/core/tools/definitions/requestRule.test.ts @@ -25,7 +25,8 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); expect(result).toBe( - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\nNo rules available.", + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + + "No rules available.", ); }); @@ -69,7 +70,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Agent Rule 1: First agent-requested rule\n" + "Agent Rule 2: Second agent-requested rule"; @@ -90,7 +91,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Rule Without Description: undefined"; expect(result).toBe(expected); @@ -110,7 +111,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "undefined: Rule without name"; expect(result).toBe(expected); @@ -130,7 +131,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Rule Without File: Rule without file path"; expect(result).toBe(expected); @@ -159,7 +160,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Valid Agent Rule: This should be included"; expect(result).toBe(expected); @@ -198,7 +199,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Valid Agent Rule: This should be included"; expect(result).toBe(expected); @@ -210,7 +211,8 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); expect(result).toBe( - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\nNo rules available.", + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + + "No rules available.", ); }); @@ -229,7 +231,7 @@ describe("getRequestRuleDescription", () => { const result = getRequestRuleDescription(rules); const expected = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + "Single Rule: The only agent-requested rule"; expect(result).toBe(expected); diff --git a/core/tools/definitions/requestRule.ts b/core/tools/definitions/requestRule.ts index cb60cda73..4e2f1ef61 100644 --- a/core/tools/definitions/requestRule.ts +++ b/core/tools/definitions/requestRule.ts @@ -1,30 +1,41 @@ import { ConfigDependentToolParams, GetTool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export interface RequestRuleArgs { name: string; } -export function getRequestRuleDescription( - rules: ConfigDependentToolParams["rules"], -): string { +function getAvailableRules(rules: ConfigDependentToolParams["rules"]) { // Must be explicitly false and no globs const agentRequestedRules = rules.filter( (rule) => rule.alwaysApply === false && !rule.globs, ); - const prefix = - "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n"; - - const body = agentRequestedRules - .map((rule) => `${rule.name}: ${rule.description}`) - .join("\n"); - - if (body.length === 0) { - return prefix + "No rules available."; + if (agentRequestedRules.length === 0) { + return "No rules available."; } - return prefix + body; + return agentRequestedRules + .map((rule) => `${rule.name}: ${rule.description}`) + .join("\n"); +} + +export function getRequestRuleDescription( + rules: ConfigDependentToolParams["rules"], +): string { + const prefix = + "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n"; + return prefix + getAvailableRules(rules); +} + +function getRequestRuleSystemMessageDescription( + rules: ConfigDependentToolParams["rules"], +): string { + const prefix = `To retrieve "rules" that contain more context/instructions based on their descriptions, use the ${BuiltInToolNames.RequestRule} tool with the name of the rule. The available rules are:\n`; + const availableRules = getAvailableRules(rules); + const suffix = "\n\nFor example, you might respond with:"; + return prefix + availableRules + suffix; } export const requestRuleTool: GetTool = ({ rules }) => ({ @@ -49,4 +60,9 @@ export const requestRuleTool: GetTool = ({ rules }) => ({ }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.RequestRule, + getRequestRuleSystemMessageDescription(rules), + [["name", "rule_name"]], + ), }); diff --git a/core/tools/definitions/runTerminalCommand.ts b/core/tools/definitions/runTerminalCommand.ts index f9ddd4f9a..d0119c796 100644 --- a/core/tools/definitions/runTerminalCommand.ts +++ b/core/tools/definitions/runTerminalCommand.ts @@ -1,13 +1,13 @@ +import os from "os"; import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; - -import os from "os"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; /** * Get the preferred shell for the current platform * @returns The preferred shell command or path */ -export function getPreferredShell(): string { +function getPreferredShell(): string { const platform = os.platform(); if (platform === "win32") { @@ -20,7 +20,13 @@ export function getPreferredShell(): string { } } -export const PLATFORM_INFO = `Choose terminal commands and scripts optimized for ${os.platform} and ${os.arch} and shell ${getPreferredShell()}.`; +const PLATFORM_INFO = `Choose terminal commands and scripts optimized for ${os.platform()} and ${os.arch()} and shell ${getPreferredShell()}.`; + +const RUN_COMMAND_NOTES = `The shell is not stateful and will not remember any previous commands.\ + When a command is run in the background ALWAYS suggest using shell commands to stop it; NEVER suggest using Ctrl+C.\ + When suggesting subsequent shell commands ALWAYS format them in shell command blocks.\ + Do NOT perform actions requiring special/admin privileges.\ + ${PLATFORM_INFO}`; export const runTerminalCommandTool: Tool = { type: "function", @@ -32,12 +38,7 @@ export const runTerminalCommandTool: Tool = { group: BUILT_IN_GROUP_NAME, function: { name: BuiltInToolNames.RunTerminalCommand, - description: `Run a terminal command in the current directory.\ - The shell is not stateful and will not remember any previous commands.\ - When a command is run in the background ALWAYS suggest using shell commands to stop it; NEVER suggest using Ctrl+C.\ - When suggesting subsequent shell commands ALWAYS format them in shell command blocks.\ - Do NOT perform actions requiring special/admin privileges.\ - ${PLATFORM_INFO}`, + description: `Run a terminal command in the current directory.\n${RUN_COMMAND_NOTES}`, parameters: { type: "object", required: ["command"], @@ -55,4 +56,12 @@ export const runTerminalCommandTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.RunTerminalCommand, + `To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool +${RUN_COMMAND_NOTES} +You can also optionally include the waitForCompletion argument set to false to run the command in the background. +For example, to see the git log, you could respond with:`, + [["command", "git log"]], + ), }; diff --git a/core/tools/definitions/searchAndReplaceInFile.ts b/core/tools/definitions/searchAndReplaceInFile.ts index 97b79ca4d..aa12e7c72 100644 --- a/core/tools/definitions/searchAndReplaceInFile.ts +++ b/core/tools/definitions/searchAndReplaceInFile.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export interface SearchAndReplaceInFileArgs { filepath: string; @@ -10,46 +11,16 @@ export interface SearchAndReplaceInFileArgs { export const NO_PARALLEL_TOOL_CALLING_INSRUCTION = "Note this tool CANNOT be called in parallel."; -/** - * This tool is in an experimental state. - * 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 - */ -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: `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}`, - parameters: { - type: "object", - required: ["filepath", "diffs"], - properties: { - filepath: { - type: "string", - description: - "The path of the file to modify, relative to the root of the workspace.", - }, - diffs: { - type: "array", - items: { - type: "string", - }, - description: `An array of strings, each containing one or more SEARCH/REPLACE blocks following this exact format: -\`\`\` -------- SEARCH +const SEARCH_AND_REPLACE_TOOL_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}`; +const SEARCH_AND_REPLACE_FILEPATH_DESCRIPTION = + "The path of the file to modify, relative to the root of the workspace."; +const SEARCH_AND_REPLACE_EXAMPLE_BLOCK = `------- SEARCH [exact content to find] ======= [new content to replace with] -+++++++ REPLACE -\`\`\` ++++++++ REPLACE`; -Critical rules: +const SEARCH_AND_REPLACE_RULES = `Critical rules: 1. SEARCH content must match the associated file section to find EXACTLY: * Match character-for-character including whitespace, indentation, line endings * Include all comments, docstrings, etc. @@ -69,26 +40,84 @@ Critical rules: * 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] 6. Special operations: * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location) - * To delete code: Use empty REPLACE section + * To delete code: Use empty REPLACE section`; + +const SEARCH_AND_REPLACE_DIFFS_DESCRIPTION = `An array of strings, each containing one or more SEARCH/REPLACE blocks following this exact format: +\`\`\` +${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} +\`\`\` + +${SEARCH_AND_REPLACE_RULES} Usage: \`\`\` -------- SEARCH -[exact content to find] -======= -[new content to replace with] -+++++++ REPLACE +${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} -------- SEARCH -[exact content to find] -======= -[new content to replace with] -+++++++ REPLACE - \`\`\` +${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} +\`\`\` -Each string in the diffs array can contain multiple SEARCH/REPLACE blocks, and all will be applied sequentially in the order they appear.`, +Each string in the diffs array can contain multiple SEARCH/REPLACE blocks, and all will be applied sequentially in the order they appear.`; + +/** + * This tool is in an experimental state. + * 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 + */ +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: SEARCH_AND_REPLACE_TOOL_DESCRIPTION, + parameters: { + type: "object", + required: ["filepath", "diffs"], + properties: { + filepath: { + type: "string", + description: SEARCH_AND_REPLACE_FILEPATH_DESCRIPTION, + }, + diffs: { + type: "array", + items: { + type: "string", + }, + description: SEARCH_AND_REPLACE_DIFFS_DESCRIPTION, }, }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.SearchAndReplaceInFile, + `To make targeted edits by replacing sections of content in an existing file, use the ${BuiltInToolNames.SearchAndReplaceInFile} tool with a "diffs" argument containing an array of SEARCH/REPLACE blocks that define exact changes to specific parts of the file. +Each block should follow this format: +${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} + +${SEARCH_AND_REPLACE_RULES} + + For example, you could respond with:`, + [ + ["filepath", "path/to/file.ts"], + [ + "diff", + `[ +"------- SEARCH +[exact content to find] +======= +[new content to replace with] ++++++++ REPLACE", +"------- SEARCH +[exact content to find] +======= +[new content to replace with] ++++++++ REPLACE" +]`, + ], + ], + ), }; diff --git a/core/tools/definitions/searchWeb.ts b/core/tools/definitions/searchWeb.ts index d2ce78efc..bcaa743a3 100644 --- a/core/tools/definitions/searchWeb.ts +++ b/core/tools/definitions/searchWeb.ts @@ -1,6 +1,7 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const searchWebTool: Tool = { type: "function", @@ -25,4 +26,9 @@ export const searchWebTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.SearchWeb, + `To search the web, use the ${BuiltInToolNames.SearchWeb} tool with a natural language query. For example, to search for the current weather, you would respond with:`, + [["query", "What is the current weather in San Francisco?"]], + ), }; diff --git a/core/tools/definitions/viewDiff.ts b/core/tools/definitions/viewDiff.ts index d835986b0..bf488f171 100644 --- a/core/tools/definitions/viewDiff.ts +++ b/core/tools/definitions/viewDiff.ts @@ -1,6 +1,7 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const viewDiffTool: Tool = { type: "function", @@ -19,4 +20,9 @@ export const viewDiffTool: Tool = { properties: {}, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.ViewDiff, + `To view the current git diff, use the ${BuiltInToolNames.ViewDiff} tool. This will show you the changes made in the working directory compared to the last commit.`, + [], + ), }; diff --git a/core/tools/definitions/viewRepoMap.ts b/core/tools/definitions/viewRepoMap.ts index dc76e8309..a58584fc1 100644 --- a/core/tools/definitions/viewRepoMap.ts +++ b/core/tools/definitions/viewRepoMap.ts @@ -1,6 +1,7 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const viewRepoMapTool: Tool = { type: "function", @@ -19,4 +20,9 @@ export const viewRepoMapTool: Tool = { properties: {}, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.ViewRepoMap, + `To view the repository map, use the ${BuiltInToolNames.ViewRepoMap} tool. This will provide a visual representation of the project's structure and organization.`, + [], + ), }; diff --git a/core/tools/definitions/viewSubdirectory.ts b/core/tools/definitions/viewSubdirectory.ts index 41c1a904c..11c27a775 100644 --- a/core/tools/definitions/viewSubdirectory.ts +++ b/core/tools/definitions/viewSubdirectory.ts @@ -1,5 +1,6 @@ import { Tool } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; +import { createSystemMessageExampleCall } from "../systemMessageTools/buildToolsSystemMessage"; export const viewSubdirectoryTool: Tool = { type: "function", @@ -25,4 +26,9 @@ export const viewSubdirectoryTool: Tool = { }, }, }, + systemMessageDescription: createSystemMessageExampleCall( + BuiltInToolNames.ViewSubdirectory, + `To view a map of a specific folder within the project, you can use the ${BuiltInToolNames.ViewSubdirectory} tool. This will provide a visual representation of the folder's structure and organization.`, + [["directory_path", "path/to/subdirectory"]], + ), };