Skip to content

Commit 6e6bafa

Browse files
committed
fix(opencode): skip tool result images whose data URI type conflicts with their mime
1 parent 90e37ff commit 6e6bafa

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
@@ -2589,6 +2589,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
25892589
}),
25902590
);
25912591

2592+
it.effect.each(toolResultSources)(
2593+
"leaves out an image whose data URI type conflicts with its mime, read from $source",
2594+
({ toolOutputs }) =>
2595+
Effect.gen(function* () {
2596+
const svg = btoa("<svg/>");
2597+
assert.deepEqual(
2598+
yield* toolOutputs([
2599+
caption,
2600+
{ type: "file", uri: `data:image/svg+xml;base64,${svg}`, mime: "image/png" },
2601+
{ type: "file", uri: `data:;base64,${PNG}`, mime: "image/png" },
2602+
]),
2603+
["Generated a cat."],
2604+
);
2605+
}),
2606+
);
2607+
25922608
it.effect("finishes a workspace command whose stream dropped before its inbox event", () =>
25932609
Effect.gen(function* () {
25942610
// session.command returns 204 without an inbox id; the stream drops before session.inbox.enqueued provides it.

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

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

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

511511
/**
512512
* An image tool such as `image_generation` answers with a file block, not text.
513513
* Only raster images inlined as base64 are kept; anything else can't be served.
514+
* The data URI's media type must match the block's mime, so a conflicting file isn't relabeled.
514515
* Blocks keep the provider's order so text and images stay interleaved.
515516
*/
516517
const toolContentOf = (
@@ -523,9 +524,11 @@ const toolContentOf = (
523524
) =>
524525
content.flatMap((part): ReadonlyArray<OpenCodeToolContentBlock> => {
525526
if (part.type === "text") return part.text ? [{ type: "text", text: part.text }] : [];
526-
const data = part.type === "file" ? BASE64_DATA_URI.exec(part.uri ?? "")?.[1] : undefined;
527+
const match = part.type === "file" ? BASE64_DATA_URI.exec(part.uri ?? "") : null;
527528
const image =
528-
data === undefined ? null : readToolOutputImage({ type: "image", mimeType: part.mime, data });
529+
match == null || match[1]!.toLowerCase() !== part.mime?.toLowerCase()
530+
? null
531+
: readToolOutputImage({ type: "image", mimeType: part.mime, data: match[2] });
529532
return image?.data === undefined
530533
? []
531534
: [{ type: "image", mimeType: image.mimeType, data: image.data }];

0 commit comments

Comments
 (0)