diff --git a/src/core/prompts/tools/insert-content.ts b/src/core/prompts/tools/insert-content.ts index c586c8ba90a..77e1d598092 100644 --- a/src/core/prompts/tools/insert-content.ts +++ b/src/core/prompts/tools/insert-content.ts @@ -5,31 +5,35 @@ export function getInsertContentDescription(args: ToolArgs): string { Description: Inserts content at specific line positions in a file. This is the primary tool for adding new content and code (functions/methods/classes, imports, attributes etc.) as it allows for precise insertions without overwriting existing content. The tool uses an efficient line-based insertion system that maintains file integrity and proper ordering of multiple insertions. Beware to use the proper indentation. This tool is the preferred way to add new content and code to files. Parameters: - path: (required) The path of the file to insert content into (relative to the current workspace directory ${args.cwd.toPosix()}) -- operations: (required) A JSON array of insertion operations. Each operation is an object with: - * start_line: (required) The line number where the content should be inserted. The content currently at that line will end up below the inserted content. - * content: (required) The content to insert at the specified position. IMPORTANT NOTE: If the content is a single line, it can be a string. If it's a multi-line content, it should be a string with newline characters (\n) for line breaks. Make sure to include the correct indentation for the content. +- operations: (required) An XML list of insertion operations. Each operation is defined with: + * : Container for each insertion operation + * : (required) The line number where the content should be inserted. The content currently at that line will end up below the inserted content. + * : (required) The content to insert at the specified position. Can be single line or multi-line content. Make sure to include the correct indentation for the content. Usage: File path here -[ - { - "start_line": 10, - "content": "Your content here" - } -] + + + 10 + Your content here + + Example: Insert a new function and its import statement File path here -[ - { - "start_line": 1, - "content": "import { sum } from './utils';" - }, - { - "start_line": 10, - "content": "function calculateTotal(items: number[]): number {\n return items.reduce((sum, item) => sum + item, 0);\n}" - } -] + + + 1 + import { sum } from './utils'; + + + 10 + function calculateTotal(items: number[]): number { + return items.reduce((sum, item) => sum + item, 0); +} + + + ` } diff --git a/src/core/prompts/tools/search-and-replace.ts b/src/core/prompts/tools/search-and-replace.ts index 60741726032..4217117ab2c 100644 --- a/src/core/prompts/tools/search-and-replace.ts +++ b/src/core/prompts/tools/search-and-replace.ts @@ -5,9 +5,9 @@ export function getSearchAndReplaceDescription(args: ToolArgs): string { Description: Request to perform search and replace operations on a file. Each operation can specify a search pattern (string or regex) and replacement text, with optional line range restrictions and regex flags. Shows a diff preview before applying changes. Parameters: - path: (required) The path of the file to modify (relative to the current workspace directory ${args.cwd.toPosix()}) -- operations: (required) A JSON array of search/replace operations. Each operation is an object with: +- operations: (required) A list of search/replace operations as XML elements. Each operation can have these elements: * search: (required) The text or pattern to search for - * replace: (required) The text to replace matches with. If multiple lines need to be replaced, use "\n" for newlines + * replace: (required) The text to replace matches with. * start_line: (optional) Starting line number for restricted replacement * end_line: (optional) Ending line number for restricted replacement * use_regex: (optional) Whether to treat search as a regex pattern @@ -16,37 +16,61 @@ Parameters: Usage: File path here -[ - { - "search": "text to find", - "replace": "replacement text", - "start_line": 1, - "end_line": 10 - } -] + + + text to find + replacement text + 1 + 10 + + Example: Replace "foo" with "bar" in lines 1-10 of example.ts example.ts -[ - { - "search": "foo", - "replace": "bar", - "start_line": 1, - "end_line": 10 - } -] + + + foo + bar + 1 + 10 + + Example: Replace all occurrences of "old" with "new" using regex example.ts -[ - { - "search": "old\\w+", - "replace": "new$&", - "use_regex": true, - "ignore_case": true - } -] + + + old\\w+ + new$& + true + true + + + + +Example: Multiple replacements in a JavaScript file + +app.js + + + const + let + 1 + 100 + + + function\\s+(\\w+) + const $1 = function + true + + + console\\.log + logger.info + true + true + + ` } diff --git a/src/core/tools/insertContentTool.ts b/src/core/tools/insertContentTool.ts index 24cf6c57b64..6fb157c7e8e 100644 --- a/src/core/tools/insertContentTool.ts +++ b/src/core/tools/insertContentTool.ts @@ -2,6 +2,7 @@ import { getReadablePath } from "../../utils/path" import { Cline } from "../Cline" import { ToolUse } from "../assistant-message" import { AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "./types" +import { parseXml } from "../../utils/xml" import { formatResponse } from "../prompts/responses" import { ClineSayTool } from "../../shared/ExtensionMessage" import path from "path" @@ -58,23 +59,29 @@ export async function insertContentTool( return } - let parsedOperations: Array<{ + type Operation = { start_line: number content: string - }> + } + let parsedOperations: { + operation: Operation[] | Operation + } try { - parsedOperations = JSON.parse(operations) - if (!Array.isArray(parsedOperations)) { - throw new Error("Operations must be an array") + parsedOperations = parseXml(operations) as { + operation: Operation[] | Operation } } catch (error) { cline.consecutiveMistakeCount++ - await cline.say("error", `Failed to parse operations JSON: ${error.message}`) - pushToolResult(formatResponse.toolError("Invalid operations JSON format")) + await cline.say("error", `Failed to parse operations: ${error.message}`) + pushToolResult(formatResponse.toolError("Invalid operations XML format")) return } + const normalizedOperations = Array.isArray(parsedOperations?.operation) + ? parsedOperations.operation + : [parsedOperations?.operation].filter((op): op is Operation => op !== undefined) + cline.consecutiveMistakeCount = 0 // Read the file @@ -85,7 +92,7 @@ export async function insertContentTool( const updatedContent = insertGroups( lines, - parsedOperations.map((elem) => { + normalizedOperations.map((elem) => { return { index: elem.start_line - 1, elements: elem.content.split("\n"), diff --git a/src/core/tools/searchAndReplaceTool.ts b/src/core/tools/searchAndReplaceTool.ts index 6996c9361e8..df783f556a6 100644 --- a/src/core/tools/searchAndReplaceTool.ts +++ b/src/core/tools/searchAndReplaceTool.ts @@ -2,6 +2,7 @@ import { Cline } from "../Cline" import { ToolUse } from "../assistant-message" import { AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "./types" import { formatResponse } from "../prompts/responses" +import { parseXml } from "../../utils/xml" import { ClineSayTool } from "../../shared/ExtensionMessage" import { getReadablePath } from "../../utils/path" import path from "path" @@ -57,7 +58,7 @@ export async function searchAndReplaceTool( return } - let parsedOperations: Array<{ + type Operation = { search: string replace: string start_line?: number @@ -65,27 +66,37 @@ export async function searchAndReplaceTool( use_regex?: boolean ignore_case?: boolean regex_flags?: string - }> + } + let parsedOperations: { + operation: Operation[] | Operation + } try { - parsedOperations = JSON.parse(operations) - if (!Array.isArray(parsedOperations)) { - throw new Error("Operations must be an array") + parsedOperations = parseXml(operations, [ + "operation.search", + "operation.replace", + "operation.regex_flags", + ]) as { + operation: Operation[] | Operation } } catch (error) { cline.consecutiveMistakeCount++ - await cline.say("error", `Failed to parse operations JSON: ${error.message}`) - pushToolResult(formatResponse.toolError("Invalid operations JSON format")) + await cline.say("error", `Failed to parse operations: ${error.message}`) + pushToolResult(formatResponse.toolError("Invalid operations XML format")) return } + const normalizedOperations = Array.isArray(parsedOperations?.operation) + ? parsedOperations.operation + : [parsedOperations?.operation].filter((op): op is Operation => op !== undefined) + // Read the original file content const fileContent = await fs.readFile(absolutePath, "utf-8") cline.diffViewProvider.editType = "modify" cline.diffViewProvider.originalContent = fileContent let lines = fileContent.split("\n") - for (const op of parsedOperations) { + for (const op of normalizedOperations) { const flags = op.regex_flags ?? (op.ignore_case ? "gi" : "g") const multilineFlags = flags.includes("m") ? flags : flags + "m" diff --git a/src/utils/xml.ts b/src/utils/xml.ts index 0fd6ef574c2..8ccd6e77aee 100644 --- a/src/utils/xml.ts +++ b/src/utils/xml.ts @@ -10,10 +10,13 @@ export function parseXml(xmlString: string, stopNodes?: string[]): unknown { const _stopNodes = stopNodes ?? [] try { const parser = new XMLParser({ + // Preserve attribute types (don't convert numbers/booleans) ignoreAttributes: false, attributeNamePrefix: "@_", - parseAttributeValue: false, - parseTagValue: false, + // Parse numbers and booleans in text nodes + parseAttributeValue: true, + parseTagValue: true, + // Trim whitespace from text nodes trimValues: true, stopNodes: _stopNodes, })