Skip to content
Closed
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
9 changes: 9 additions & 0 deletions packages/opencode/src/tool/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Filesystem } from "../util/filesystem"

const DEFAULT_READ_LIMIT = 2000
const MAX_LINE_LENGTH = 2000
const MAX_OUTPUT_SIZE = 200 * 1024 // 200KB in bytes

export const ReadTool = Tool.define("read", {
description: DESCRIPTION,
Expand Down Expand Up @@ -49,6 +50,14 @@ export const ReadTool = Tool.define("read", {
throw new Error(`File not found: ${filepath}`)
}

// Check file size before reading
const stat = await file.stat()
const fileSize = stat.size
// If file is too large and no offset/limit provided
if (fileSize > MAX_OUTPUT_SIZE && !params.offset && !params.limit) {
throw new Error(`File content (${Math.round(fileSize / 1024)}KB) exceeds maximum allowed size (${Math.round(MAX_OUTPUT_SIZE / 1024)}KB). Please use offset and limit parameters to read specific portions of the file, or use the GrepTool to search for specific content.`)
}

const limit = params.limit ?? DEFAULT_READ_LIMIT
const offset = params.offset || 0
const isImage = isImageFile(filepath)
Expand Down