Skip to content

Commit b642cdc

Browse files
committed
fix(opencode): skip tool result images whose data URI type conflicts with their mime
1 parent f26b6e0 commit b642cdc

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
31503150
}),
31513151
);
31523152

3153+
it.effect.each(toolResultSources)(
3154+
"leaves out an image whose data URI type conflicts with its mime, read from $source",
3155+
({ toolOutputs }) =>
3156+
Effect.gen(function* () {
3157+
const svg = btoa("<svg/>");
3158+
assert.deepEqual(
3159+
yield* toolOutputs([
3160+
caption,
3161+
{ type: "file", uri: `data:image/svg+xml;base64,${svg}`, mime: "image/png" },
3162+
{ type: "file", uri: `data:;base64,${PNG}`, mime: "image/png" },
3163+
]),
3164+
["Generated a cat."],
3165+
);
3166+
}),
3167+
);
3168+
31533169
it.effect("finishes a workspace command whose stream dropped before its inbox event", () =>
31543170
Effect.gen(function* () {
31553171
// `session.command` answers 204 without the inbox item's id, and the

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,12 @@ const sessionIdOf = (providerThread: OrchestrationV2ProviderThread) => {
510510
const textOf = (content: ReadonlyArray<{ readonly type: string; readonly text?: string }>) =>
511511
content.flatMap((part) => (part.type === "text" && part.text ? [part.text] : [])).join("\n");
512512

513-
const BASE64_DATA_URI = /^data:[^,]*;base64,(.+)$/s;
513+
const BASE64_DATA_URI = /^data:([^;,]*)[^,]*;base64,(.+)$/s;
514514

515515
/**
516516
* An image tool such as `image_generation` answers with a file block, not text.
517517
* Only raster images inlined as base64 are kept; anything else can't be served.
518+
* The data URI's media type must match the block's mime, so a conflicting file isn't relabeled.
518519
* Blocks keep the provider's order so text and images stay interleaved.
519520
*/
520521
const toolContentOf = (
@@ -527,9 +528,11 @@ const toolContentOf = (
527528
) =>
528529
content.flatMap((part): ReadonlyArray<OpenCodeToolContentBlock> => {
529530
if (part.type === "text") return part.text ? [{ type: "text", text: part.text }] : [];
530-
const data = part.type === "file" ? BASE64_DATA_URI.exec(part.uri ?? "")?.[1] : undefined;
531+
const match = part.type === "file" ? BASE64_DATA_URI.exec(part.uri ?? "") : null;
531532
const image =
532-
data === undefined ? null : readToolOutputImage({ type: "image", mimeType: part.mime, data });
533+
match == null || match[1]!.toLowerCase() !== part.mime?.toLowerCase()
534+
? null
535+
: readToolOutputImage({ type: "image", mimeType: part.mime, data: match[2] });
533536
return image?.data === undefined
534537
? []
535538
: [{ type: "image", mimeType: image.mimeType, data: image.data }];

0 commit comments

Comments
 (0)