Skip to content

Commit 52890a7

Browse files
committed
fix(server): OpenCode 2 turns send pasted images to the model
The OpenCode 2 adapter put a pasted image in the prompt only as its saved path ("[Attached image ... is saved at: ...]"), so a model saw it only if it opened the file with a tool. The 1.x adapter also sends each image as a file part (toOpenCodeFileParts). Turns and steers now also pass each pasted image to `session.prompt` as a `files` entry with a file URL, which OpenCode 2 reads and sends to the model as an image. The path text stays in the prompt, as with 1.x. Model: Claude Opus 5.5 (1M). Harness: Claude Code in T3 Code.
1 parent 3e6b450 commit 52890a7

2 files changed

Lines changed: 123 additions & 2 deletions

File tree

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

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
import { assert, it } from "@effect/vitest";
77
import {
8+
ChatAttachmentId,
9+
ChatFileAttachment,
10+
ChatImageAttachment,
811
CheckpointId,
912
EnvironmentId,
1013
MessageId,
@@ -46,7 +49,7 @@ import * as IdAllocator from "../IdAllocator.ts";
4649
import type { ProviderContinuationRequest } from "../ProviderContinuationRequests.ts";
4750
import * as ProviderContinuationRequests from "../ProviderContinuationRequests.ts";
4851
import { OPENCODE_PROVIDER } from "./OpenCodeAdapterV2.ts";
49-
import { OPENCODE_2_STILL_STOPPING } from "./OpenCode2AdapterV2.ts";
52+
import { OPENCODE_2_STILL_STOPPING, promptImageFiles } from "./OpenCode2AdapterV2.ts";
5053
import { openCode2ReplayRuntime } from "./OpenCode2AdapterV2.testkit.ts";
5154

5255
const SESSION = "ses_f148ca2deffeJcwCnRQtb0YFNX";
@@ -2702,6 +2705,96 @@ describe("OpenCode2 adapter", () => {
27022705
}).pipe(Effect.scoped),
27032706
);
27042707

2708+
it.effect("sends a turn's and a steer's pasted images as prompt files", () =>
2709+
Effect.gen(function* () {
2710+
const steerId = "msg_recorded_steer_image";
2711+
const { runtime, thread } = yield* resumed([
2712+
out("session.prompt", {
2713+
sessionID: SESSION,
2714+
text: "<any>",
2715+
files: [{ uri: "<any>", name: "screenshot.png" }],
2716+
}),
2717+
promptAccepted,
2718+
event("session.execution.started", { sessionID: SESSION }),
2719+
out("session.prompt", {
2720+
sessionID: SESSION,
2721+
id: steerId,
2722+
text: "<any>",
2723+
files: [{ uri: "<any>", name: "chart.png" }],
2724+
delivery: "steer",
2725+
}),
2726+
replyData("session.prompt", {
2727+
id: steerId,
2728+
sessionID: SESSION,
2729+
time: { created: 1790656601500 },
2730+
type: "user",
2731+
payload: { text: "And this one?" },
2732+
delivery: "steer",
2733+
}),
2734+
event("session.inbox.delivered", { sessionID: SESSION, inboxID: steerId }),
2735+
event("session.execution.succeeded", { sessionID: SESSION }),
2736+
]);
2737+
const image = (name: string, id: string) =>
2738+
ChatImageAttachment.make({
2739+
type: "image",
2740+
id: ChatAttachmentId.make(`thread-opencode2-adapter-${id}-1234-1234-1234-123456789abc`),
2741+
name,
2742+
mimeType: "image/png",
2743+
sizeBytes: 4,
2744+
});
2745+
const ids = yield* IdAllocator.IdAllocatorV2;
2746+
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2747+
const input = turnInput(thread);
2748+
yield* runtime.startTurn({
2749+
...input,
2750+
message: {
2751+
...input.message,
2752+
text: "What's in this image?",
2753+
attachments: [image("screenshot.png", "12345678")],
2754+
},
2755+
});
2756+
yield* runtime.steerTurn({
2757+
threadId,
2758+
runId: input.runId,
2759+
providerThread: thread,
2760+
providerTurnId: ids.derive.providerTurn({
2761+
driver: OPENCODE_PROVIDER,
2762+
nativeTurnId: `${SESSION}:attempt:attempt:opencode2-adapter`,
2763+
}),
2764+
message: {
2765+
...input.message,
2766+
messageId: MessageId.make("message:opencode2-adapter:steer"),
2767+
text: "And this one?",
2768+
attachments: [image("chart.png", "abcdefab")],
2769+
},
2770+
});
2771+
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2772+
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
2773+
);
2774+
2775+
it("gives OpenCode a file URL for each pasted image and leaves other attachments as paths", () => {
2776+
const image = ChatImageAttachment.make({
2777+
type: "image",
2778+
id: ChatAttachmentId.make("thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc"),
2779+
name: "screen shot.png",
2780+
mimeType: "image/png",
2781+
sizeBytes: 4,
2782+
});
2783+
const document = ChatFileAttachment.make({
2784+
type: "file",
2785+
id: ChatAttachmentId.make("thread-opencode2-adapter-abcdefab-1234-1234-1234-123456789abc"),
2786+
name: "requirements.pdf",
2787+
mimeType: "application/pdf",
2788+
sizeBytes: 4,
2789+
});
2790+
assert.deepEqual(promptImageFiles([image, document], "/home/a user/.t3/userdata/attachments"), [
2791+
{
2792+
uri: "file:///home/a%20user/.t3/userdata/attachments/thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc.png",
2793+
name: "screen shot.png",
2794+
},
2795+
]);
2796+
});
2797+
27052798
it.effect("reads user and assistant text from the session's message list", () =>
27062799
Effect.gen(function* () {
27072800
const runtime = yield* openCode2ReplayRuntimeWithInstructions([

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*
2222
* @module orchestration-v2/Adapters/OpenCode2AdapterV2
2323
*/
24+
import * as NodeURL from "node:url";
25+
2426
import {
2527
AbsolutePath,
2628
Agent,
@@ -37,6 +39,7 @@ import {
3739
import { Mcp } from "@opencode/schema/mcp";
3840
import {
3941
isOrchestrationV2WorkActive,
42+
type ChatAttachment,
4043
type OrchestrationV2AppThread,
4144
type OrchestrationV2ConversationMessage,
4245
type OrchestrationV2ExecutionNode,
@@ -67,6 +70,7 @@ import * as Schema from "effect/Schema";
6770
import * as Semaphore from "effect/Semaphore";
6871
import * as Stream from "effect/Stream";
6972

73+
import { resolveAttachmentPath } from "../../attachmentStore.ts";
7074
import * as ServerConfig from "../../config.ts";
7175
import { paginate, type OpenCode2StreamEvent } from "../../provider/opencode2/OpenCode2Client.ts";
7276
import * as OpenCode2Server from "../../provider/opencode2/OpenCode2Server.ts";
@@ -82,7 +86,10 @@ import * as KeyedLock from "@t3tools/shared/KeyedLock";
8286
import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model";
8387
import { causeErrorTag } from "@t3tools/shared/observability";
8488

85-
import { providerMessageTextWithAttachmentPaths } from "../AttachmentPrompt.ts";
89+
import {
90+
isProviderNativeImageAttachment,
91+
providerMessageTextWithAttachmentPaths,
92+
} from "../AttachmentPrompt.ts";
8693
import * as IdAllocator from "../IdAllocator.ts";
8794
import { backgroundWorkNotification, type BackgroundWorkReport } from "../Notification.ts";
8895
import * as ProviderContinuationRequests from "../ProviderContinuationRequests.ts";
@@ -788,6 +795,20 @@ const skillsNamed = (text: string, known: ReadonlySet<string>) => [
788795
),
789796
];
790797

798+
/**
799+
* A message's pasted images as prompt files, which OpenCode reads from T3's
800+
* attachment directory and gives the model as images. The prompt text still
801+
* names where each one is saved, as with 1.x.
802+
*/
803+
export const promptImageFiles = (
804+
attachments: ReadonlyArray<ChatAttachment>,
805+
attachmentsDir: string,
806+
) =>
807+
attachments.filter(isProviderNativeImageAttachment).flatMap((attachment) => {
808+
const path = resolveAttachmentPath({ attachmentsDir, attachment });
809+
return path === null ? [] : [{ uri: NodeURL.pathToFileURL(path).href, name: attachment.name }];
810+
});
811+
791812
/**
792813
* The turn's own tokens: steps add up, and the last step's input is the live
793814
* context size. A subagent's tokens are its own session's, never these.
@@ -3525,12 +3546,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
35253546
),
35263547
)
35273548
: [];
3549+
const files = promptImageFiles(turnInput.message.attachments, serverConfig.attachmentsDir);
35283550
if (!sending()) return;
35293551
return yield* client.session
35303552
.prompt({
35313553
sessionID,
35323554
id,
35333555
text: promptText(turnInput),
3556+
...(files.length === 0 ? {} : { files }),
35343557
...(skills.length === 0
35353558
? {}
35363559
: { skills: skills.map((id) => ({ id: Skill.ID.make(id) })) }),
@@ -3866,6 +3889,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
38663889
if (turn.settledInbox.has(inboxID)) return;
38673890
turn.steers.add(inboxID);
38683891
state.strandedSteers.delete(inboxID);
3892+
const files = promptImageFiles(
3893+
steerInput.message.attachments,
3894+
serverConfig.attachmentsDir,
3895+
);
38693896
yield* client.session
38703897
.prompt({
38713898
sessionID: Session.ID.make(sessionId),
@@ -3875,6 +3902,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
38753902
attachments: steerInput.message.attachments,
38763903
attachmentsDir: serverConfig.attachmentsDir,
38773904
}).trim(),
3905+
...(files.length === 0 ? {} : { files }),
38783906
delivery: "steer",
38793907
})
38803908
.pipe(

0 commit comments

Comments
 (0)