Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/opencode/src/tool/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<file>\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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/tool/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.