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
37 changes: 32 additions & 5 deletions packages/opencode/src/tool/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ export const ReadTool = Tool.define("read", {
throw new Error(`File not found: ${filepath}`)
}

const isImage = isImageFile(filepath)
const supportsImages = await (async () => {
if (!ctx.extra?.["providerID"] || !ctx.extra?.["modelID"]) return false
const model = await (async () => {
if (!ctx.extra?.["providerID"] || !ctx.extra?.["modelID"]) return null
const providerID = ctx.extra["providerID"] as string
const modelID = ctx.extra["modelID"] as string
const model = await Provider.getModel(providerID, modelID).catch(() => undefined)
if (!model) return false
return model.info.modalities?.input?.includes("image") ?? false
return model
})()

const isImage = isImageFile(filepath)
const supportsImages = model?.info.modalities?.input?.includes("image") ?? false
if (isImage) {
if (!supportsImages) {
throw new Error(`Failed to read image: ${filepath}, model may not be able to read images`)
Expand All @@ -117,6 +118,32 @@ export const ReadTool = Tool.define("read", {
}
}

const isPdf = path.extname(filepath).toLowerCase() === ".pdf"
const supportsPdfs = model?.info.modalities?.input?.includes("pdf") ?? false
if (isPdf) {
if (!supportsPdfs) {
throw new Error(`Failed to read pdf: ${filepath}, model may not be able to read pdfs`)
}
const mime = file.type
const msg = "Pdf read successfully"
return {
title,
output: msg,
metadata: {
preview: msg,
},
attachments: [
{
id: Identifier.ascending("part"),
sessionID: ctx.sessionID,
messageID: ctx.messageID,
type: "file",
mime,
url: `data:${mime};base64,${Buffer.from(await file.bytes()).toString("base64")}`,
},
],
}
}
const isBinary = await isBinaryFile(filepath, file)
if (isBinary) throw new Error(`Cannot read binary file: ${filepath}`)

Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/tool/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Usage:
- 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.
- You can read pdf files using this tool.