diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index 27426ad2412..2b28bc37265 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -123,21 +123,24 @@ export const ReadTool = Tool.define("read", { const limit = params.limit ?? DEFAULT_READ_LIMIT const offset = params.offset || 0 const lines = await file.text().then((text) => text.split("\n")) + const totalLines = lines.length + const lastReadLine = Math.min(offset + limit, totalLines) + const hasMoreLines = totalLines > lastReadLine + + // Add line numbers only when paginating or when file is truncated + const shouldAddLineNumbers = params.offset !== undefined || params.limit !== undefined || hasMoreLines + const raw = lines.slice(offset, offset + limit).map((line) => { return line.length > MAX_LINE_LENGTH ? line.substring(0, MAX_LINE_LENGTH) + "..." : line }) - const content = raw.map((line, index) => { - return `${(index + offset + 1).toString().padStart(5, "0")}| ${line}` - }) + const content = shouldAddLineNumbers + ? raw.map((line, index) => `${(index + offset + 1).toString().padStart(5, "0")}| ${line}`) + : raw const preview = raw.slice(0, 20).join("\n") let output = "\n" output += content.join("\n") - const totalLines = lines.length - const lastReadLine = offset + content.length - const hasMoreLines = totalLines > lastReadLine - if (hasMoreLines) { output += `\n\n(File has more lines. Use 'offset' parameter to read beyond line ${lastReadLine})` } else { diff --git a/packages/opencode/src/tool/read.txt b/packages/opencode/src/tool/read.txt index b5bffee263e..6b5ca1d1d2e 100644 --- a/packages/opencode/src/tool/read.txt +++ b/packages/opencode/src/tool/read.txt @@ -6,7 +6,7 @@ Usage: - By default, it reads up to 2000 lines starting from the beginning of the file - You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters - Any lines longer than 2000 characters will be truncated -- Results are returned using cat -n format, with line numbers starting at 1 +- If passing offset or limit results are returned using cat -n format, with line numbers starting at 1 - You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful. - If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents. - You can read image files using this tool.