|
| 1 | +import path from "path" |
| 2 | +import { Cline } from "../Cline" |
| 3 | +import { ClineSayTool } from "../../shared/ExtensionMessage" |
| 4 | +import { ToolUse } from "../assistant-message" |
| 5 | +import { formatResponse } from "../prompts/responses" |
| 6 | +import { AskApproval, HandleError, PushToolResult } from "./types" |
| 7 | +import { isPathOutsideWorkspace } from "../../utils/pathUtils" |
| 8 | +import { getReadablePath } from "../../utils/path" |
| 9 | +import { countFileLines } from "../../integrations/misc/line-counter" |
| 10 | +import { readLines } from "../../integrations/misc/read-lines" |
| 11 | +import { extractTextFromFile, addLineNumbers } from "../../integrations/misc/extract-text" |
| 12 | +import { parseSourceCodeDefinitionsForFile } from "../../services/tree-sitter" |
| 13 | +import { isBinaryFile } from "isbinaryfile" |
| 14 | + |
| 15 | +export async function readFileTool( |
| 16 | + cline: Cline, |
| 17 | + block: ToolUse, |
| 18 | + askApproval: AskApproval, |
| 19 | + handleError: HandleError, |
| 20 | + pushToolResult: PushToolResult, |
| 21 | +) { |
| 22 | + switch (true) { |
| 23 | + default: |
| 24 | + const relPath: string | undefined = block.params.path |
| 25 | + const startLineStr: string | undefined = block.params.start_line |
| 26 | + const endLineStr: string | undefined = block.params.end_line |
| 27 | + |
| 28 | + // Get the full path and determine if it's outside the workspace |
| 29 | + const fullPath = relPath ? path.resolve(cline.cwd, relPath) : "" |
| 30 | + const isOutsideWorkspace = isPathOutsideWorkspace(fullPath) |
| 31 | + |
| 32 | + const sharedMessageProps: ClineSayTool = { |
| 33 | + tool: "readFile", |
| 34 | + path: getReadablePath(cline.cwd, relPath), |
| 35 | + isOutsideWorkspace, |
| 36 | + } |
| 37 | + try { |
| 38 | + if (block.partial) { |
| 39 | + const partialMessage = JSON.stringify({ |
| 40 | + ...sharedMessageProps, |
| 41 | + content: undefined, |
| 42 | + } satisfies ClineSayTool) |
| 43 | + await cline.ask("tool", partialMessage, block.partial).catch(() => {}) |
| 44 | + break |
| 45 | + } else { |
| 46 | + if (!relPath) { |
| 47 | + cline.consecutiveMistakeCount++ |
| 48 | + pushToolResult(await cline.sayAndCreateMissingParamError("read_file", "path")) |
| 49 | + break |
| 50 | + } |
| 51 | + |
| 52 | + // Check if we're doing a line range read |
| 53 | + let isRangeRead = false |
| 54 | + let startLine: number | undefined = undefined |
| 55 | + let endLine: number | undefined = undefined |
| 56 | + |
| 57 | + // Check if we have either range parameter |
| 58 | + if (startLineStr || endLineStr) { |
| 59 | + isRangeRead = true |
| 60 | + } |
| 61 | + |
| 62 | + // Parse start_line if provided |
| 63 | + if (startLineStr) { |
| 64 | + startLine = parseInt(startLineStr) |
| 65 | + if (isNaN(startLine)) { |
| 66 | + // Invalid start_line |
| 67 | + cline.consecutiveMistakeCount++ |
| 68 | + await cline.say("error", `Failed to parse start_line: `) |
| 69 | + pushToolResult(formatResponse.toolError("Invalid start_line value")) |
| 70 | + break |
| 71 | + } |
| 72 | + startLine -= 1 // Convert to 0-based index |
| 73 | + } |
| 74 | + |
| 75 | + // Parse end_line if provided |
| 76 | + if (endLineStr) { |
| 77 | + endLine = parseInt(endLineStr) |
| 78 | + |
| 79 | + if (isNaN(endLine)) { |
| 80 | + // Invalid end_line |
| 81 | + cline.consecutiveMistakeCount++ |
| 82 | + await cline.say("error", `Failed to parse end_line: `) |
| 83 | + pushToolResult(formatResponse.toolError("Invalid end_line value")) |
| 84 | + break |
| 85 | + } |
| 86 | + |
| 87 | + // Convert to 0-based index |
| 88 | + endLine -= 1 |
| 89 | + } |
| 90 | + |
| 91 | + const accessAllowed = cline.rooIgnoreController?.validateAccess(relPath) |
| 92 | + if (!accessAllowed) { |
| 93 | + await cline.say("rooignore_error", relPath) |
| 94 | + pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath))) |
| 95 | + break |
| 96 | + } |
| 97 | + |
| 98 | + cline.consecutiveMistakeCount = 0 |
| 99 | + const absolutePath = path.resolve(cline.cwd, relPath) |
| 100 | + const completeMessage = JSON.stringify({ |
| 101 | + ...sharedMessageProps, |
| 102 | + content: absolutePath, |
| 103 | + } satisfies ClineSayTool) |
| 104 | + |
| 105 | + const didApprove = await askApproval("tool", completeMessage) |
| 106 | + if (!didApprove) { |
| 107 | + break |
| 108 | + } |
| 109 | + |
| 110 | + // Get the maxReadFileLine setting |
| 111 | + const { maxReadFileLine = 500 } = (await cline.providerRef.deref()?.getState()) ?? {} |
| 112 | + |
| 113 | + // Count total lines in the file |
| 114 | + let totalLines = 0 |
| 115 | + try { |
| 116 | + totalLines = await countFileLines(absolutePath) |
| 117 | + } catch (error) { |
| 118 | + console.error(`Error counting lines in file :`, error) |
| 119 | + } |
| 120 | + |
| 121 | + // now execute the tool like normal |
| 122 | + let content: string |
| 123 | + let isFileTruncated = false |
| 124 | + let sourceCodeDef = "" |
| 125 | + |
| 126 | + const isBinary = await isBinaryFile(absolutePath).catch(() => false) |
| 127 | + |
| 128 | + if (isRangeRead) { |
| 129 | + if (startLine === undefined) { |
| 130 | + content = addLineNumbers(await readLines(absolutePath, endLine, startLine)) |
| 131 | + } else { |
| 132 | + content = addLineNumbers(await readLines(absolutePath, endLine, startLine), startLine + 1) |
| 133 | + } |
| 134 | + } else if (!isBinary && maxReadFileLine >= 0 && totalLines > maxReadFileLine) { |
| 135 | + // If file is too large, only read the first maxReadFileLine lines |
| 136 | + isFileTruncated = true |
| 137 | + |
| 138 | + const res = await Promise.all([ |
| 139 | + maxReadFileLine > 0 ? readLines(absolutePath, maxReadFileLine - 1, 0) : "", |
| 140 | + parseSourceCodeDefinitionsForFile(absolutePath, cline.rooIgnoreController), |
| 141 | + ]) |
| 142 | + |
| 143 | + content = res[0].length > 0 ? addLineNumbers(res[0]) : "" |
| 144 | + const result = res[1] |
| 145 | + if (result) { |
| 146 | + sourceCodeDef = `\n\n` |
| 147 | + } |
| 148 | + } else { |
| 149 | + // Read entire file |
| 150 | + content = await extractTextFromFile(absolutePath) |
| 151 | + } |
| 152 | + |
| 153 | + // Add truncation notice if applicable |
| 154 | + if (isFileTruncated) { |
| 155 | + content += `\n\n[Showing only ${maxReadFileLine} of ${totalLines} total lines. Use start_line and end_line if you need to read more]` |
| 156 | + } |
| 157 | + |
| 158 | + pushToolResult(content) |
| 159 | + break |
| 160 | + } |
| 161 | + } catch (error) { |
| 162 | + await handleError("reading file", error) |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments