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
15 changes: 15 additions & 0 deletions packages/opencode/src/provider/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,21 @@ function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMes
}
}

// Check for empty base64 PDF data
if (part.type === "file" && part.mediaType === "application/pdf") {
const url = "url" in part ? String(part.url) : ""
if (url.startsWith("data:")) {
const match = url.match(/^data:([^;]+);base64,(.*)$/)
if (match && (!match[2] || match[2].length === 0)) {
const name = part.filename ? `"${part.filename}"` : "PDF"
return {
type: "text" as const,
text: `ERROR: ${name} file is empty or corrupted. Please provide a valid PDF file.`,
}
}
}
}

const mime = part.type === "image" ? String(part.image).split(";")[0].replace("data:", "") : part.mediaType
const filename = part.type === "file" ? part.filename : undefined
const modality = mimeToModality(mime)
Expand Down
23 changes: 20 additions & 3 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,25 @@ export const layer = Layer.effect(
]
}

const readExit = yield* fsys.readFile(filepath).pipe(Effect.exit)
if (Exit.isFailure(readExit)) {
const error = Cause.squash(readExit.cause)
log.error("failed to read file attachment", { error, filepath })
const message = error instanceof Error ? error.message : String(error)
yield* events.publish(Session.Event.Error, {
sessionID: input.sessionID,
error: new NamedError.Unknown({ message }).toObject(),
})
return [
{
messageID: info.id,
sessionID: input.sessionID,
type: "text",
synthetic: true,
text: `Failed to read file ${filepath}: ${message}`,
},
]
}
return [
{
messageID: info.id,
Expand All @@ -986,9 +1005,7 @@ export const layer = Layer.effect(
messageID: info.id,
sessionID: input.sessionID,
type: "file",
url:
`data:${mime};base64,` +
Buffer.from(yield* fsys.readFile(filepath).pipe(Effect.catch(Effect.die))).toString("base64"),
url: `data:${mime};base64,${Buffer.from(readExit.value).toString("base64")}`,
mime,
filename: part.filename!,
source: part.source,
Expand Down
13 changes: 11 additions & 2 deletions packages/opencode/src/tool/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,17 @@ export const ReadTool = Tool.define<
const isImage = SUPPORTED_IMAGE_MIMES.has(mime)

if (isImage || isPdfAttachment(mime)) {
const bytes = yield* fs.readFile(filepath)
const msg = isPdfAttachment(mime) ? "PDF read successfully" : "Image read successfully"
const kind = isPdfAttachment(mime) ? "PDF" : "image"
const bytes = yield* fs.readFile(filepath).pipe(
Effect.catch((error) =>
Effect.fail(
new Error(
`Cannot read ${kind} file: ${filepath} (${error instanceof Error ? error.message : String(error)})`,
),
),
),
)
const msg = `${kind === "PDF" ? "PDF" : "Image"} read successfully`
return {
title,
output: msg,
Expand Down
Loading