mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-05 08:10:14 +00:00
164 lines
4.9 KiB
TypeScript
164 lines
4.9 KiB
TypeScript
import delay from "delay"
|
|
import fs from "fs/promises"
|
|
import path from "path"
|
|
|
|
import { getReadablePath } from "../../utils/path"
|
|
import { Task } from "../task/Task"
|
|
import { AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "../../shared/tools"
|
|
import { formatResponse } from "../prompts/responses"
|
|
import { ClineSayTool } from "../../shared/ExtensionMessage"
|
|
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
|
|
import { fileExistsAtPath } from "../../utils/fs"
|
|
import { insertGroups } from "../diff/insert-groups"
|
|
import { ToolDirective } from "../message-parsing/directives"
|
|
|
|
export async function insertContentTool(
|
|
cline: Task,
|
|
block: ToolDirective,
|
|
askApproval: AskApproval,
|
|
handleError: HandleError,
|
|
pushToolResult: PushToolResult,
|
|
removeClosingTag: RemoveClosingTag,
|
|
) {
|
|
const relPath: string | undefined = block.params.path
|
|
const line: string | undefined = block.params.line
|
|
const content: string | undefined = block.params.content
|
|
|
|
const sharedMessageProps: ClineSayTool = {
|
|
tool: "insertContent",
|
|
path: getReadablePath(cline.cwd, removeClosingTag("path", relPath)),
|
|
diff: content,
|
|
lineNumber: line ? parseInt(line, 10) : undefined,
|
|
}
|
|
|
|
try {
|
|
if (block.partial) {
|
|
await cline.ask("tool", JSON.stringify(sharedMessageProps), block.partial).catch(() => {})
|
|
return
|
|
}
|
|
|
|
// Validate required parameters
|
|
if (!relPath) {
|
|
cline.consecutiveMistakeCount++
|
|
cline.recordToolError("insert_content")
|
|
pushToolResult(await cline.sayAndCreateMissingParamError("insert_content", "path"))
|
|
return
|
|
}
|
|
|
|
if (!line) {
|
|
cline.consecutiveMistakeCount++
|
|
cline.recordToolError("insert_content")
|
|
pushToolResult(await cline.sayAndCreateMissingParamError("insert_content", "line"))
|
|
return
|
|
}
|
|
|
|
if (!content) {
|
|
cline.consecutiveMistakeCount++
|
|
cline.recordToolError("insert_content")
|
|
pushToolResult(await cline.sayAndCreateMissingParamError("insert_content", "content"))
|
|
return
|
|
}
|
|
|
|
const accessAllowed = cline.rooIgnoreController?.validateAccess(relPath)
|
|
|
|
if (!accessAllowed) {
|
|
await cline.say("rooignore_error", relPath)
|
|
pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath)))
|
|
return
|
|
}
|
|
|
|
const absolutePath = path.resolve(cline.cwd, relPath)
|
|
const fileExists = await fileExistsAtPath(absolutePath)
|
|
|
|
if (!fileExists) {
|
|
cline.consecutiveMistakeCount++
|
|
cline.recordToolError("insert_content")
|
|
const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>`
|
|
await cline.say("error", formattedError)
|
|
pushToolResult(formattedError)
|
|
return
|
|
}
|
|
|
|
const lineNumber = parseInt(line, 10)
|
|
if (isNaN(lineNumber) || lineNumber < 0) {
|
|
cline.consecutiveMistakeCount++
|
|
cline.recordToolError("insert_content")
|
|
pushToolResult(formatResponse.toolError("Invalid line number. Must be a non-negative integer."))
|
|
return
|
|
}
|
|
|
|
cline.consecutiveMistakeCount = 0
|
|
|
|
// Read the file
|
|
const fileContent = await fs.readFile(absolutePath, "utf8")
|
|
cline.diffViewProvider.editType = "modify"
|
|
cline.diffViewProvider.originalContent = fileContent
|
|
const lines = fileContent.split("\n")
|
|
|
|
const updatedContent = insertGroups(lines, [
|
|
{
|
|
index: lineNumber - 1,
|
|
elements: content.split("\n"),
|
|
},
|
|
]).join("\n")
|
|
|
|
// Show changes in diff view
|
|
if (!cline.diffViewProvider.isEditing) {
|
|
await cline.ask("tool", JSON.stringify(sharedMessageProps), true).catch(() => {})
|
|
// First open with original content
|
|
await cline.diffViewProvider.open(relPath)
|
|
await cline.diffViewProvider.update(fileContent, false)
|
|
cline.diffViewProvider.scrollToFirstDiff()
|
|
await delay(200)
|
|
}
|
|
|
|
const diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
|
|
|
|
if (!diff) {
|
|
pushToolResult(`No changes needed for '${relPath}'`)
|
|
return
|
|
}
|
|
|
|
await cline.diffViewProvider.update(updatedContent, true)
|
|
|
|
const completeMessage = JSON.stringify({
|
|
...sharedMessageProps,
|
|
diff,
|
|
lineNumber: lineNumber,
|
|
} satisfies ClineSayTool)
|
|
|
|
const didApprove = await cline
|
|
.ask("tool", completeMessage, false)
|
|
.then((response) => response.response === "yesButtonClicked")
|
|
|
|
if (!didApprove) {
|
|
await cline.diffViewProvider.revertChanges()
|
|
pushToolResult("Changes were rejected by the user.")
|
|
return
|
|
}
|
|
|
|
// Call saveChanges to update the DiffViewProvider properties
|
|
await cline.diffViewProvider.saveChanges()
|
|
|
|
// Track file edit operation
|
|
if (relPath) {
|
|
await cline.fileContextTracker.trackFileContext(relPath, "roo_edited" as RecordSource)
|
|
}
|
|
|
|
cline.didEditFile = true
|
|
|
|
// Get the formatted response message
|
|
const message = await cline.diffViewProvider.pushToolWriteResult(
|
|
cline,
|
|
cline.cwd,
|
|
false, // Always false for insert_content
|
|
)
|
|
|
|
pushToolResult(message)
|
|
|
|
await cline.diffViewProvider.reset()
|
|
} catch (error) {
|
|
handleError("insert content", error)
|
|
await cline.diffViewProvider.reset()
|
|
}
|
|
}
|