Skip to content

Commit e7bde64

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 8c61e25 commit e7bde64

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
77
import { assert, it } from "@effect/vitest";
88
import {
9+
ChatAttachmentId,
10+
ChatFileAttachment,
11+
ChatImageAttachment,
912
CheckpointId,
1013
EnvironmentId,
1114
MessageId,
@@ -47,7 +50,11 @@ import * as IdAllocator from "../IdAllocator.ts";
4750
import type { ProviderContinuationRequest } from "../ProviderContinuationRequests.ts";
4851
import * as ProviderContinuationRequests from "../ProviderContinuationRequests.ts";
4952
import { OPENCODE_PROVIDER } from "./OpenCodeAdapterV2.ts";
50-
import { OPENCODE_2_STILL_STOPPING, t3McpServerName } from "./OpenCode2AdapterV2.ts";
53+
import {
54+
OPENCODE_2_STILL_STOPPING,
55+
promptImageFiles,
56+
t3McpServerName,
57+
} from "./OpenCode2AdapterV2.ts";
5158
import { openCode2ReplayRuntime } from "./OpenCode2AdapterV2.testkit.ts";
5259

5360
const SESSION = "ses_f148ca2deffeJcwCnRQtb0YFNX";
@@ -2787,6 +2794,96 @@ describe("OpenCode2 adapter", () => {
27872794
}).pipe(Effect.provide(NodeCrypto.layer)),
27882795
);
27892796

2797+
it.effect("sends a turn's and a steer's pasted images as prompt files", () =>
2798+
Effect.gen(function* () {
2799+
const steerId = "msg_recorded_steer_image";
2800+
const { runtime, thread } = yield* resumed([
2801+
out("session.prompt", {
2802+
sessionID: SESSION,
2803+
text: "<any>",
2804+
files: [{ uri: "<any>", name: "screenshot.png" }],
2805+
}),
2806+
promptAccepted,
2807+
event("session.execution.started", { sessionID: SESSION }),
2808+
out("session.prompt", {
2809+
sessionID: SESSION,
2810+
id: steerId,
2811+
text: "<any>",
2812+
files: [{ uri: "<any>", name: "chart.png" }],
2813+
delivery: "steer",
2814+
}),
2815+
replyData("session.prompt", {
2816+
id: steerId,
2817+
sessionID: SESSION,
2818+
time: { created: 1790656601500 },
2819+
type: "user",
2820+
payload: { text: "And this one?" },
2821+
delivery: "steer",
2822+
}),
2823+
event("session.inbox.delivered", { sessionID: SESSION, inboxID: steerId }),
2824+
event("session.execution.succeeded", { sessionID: SESSION }),
2825+
]);
2826+
const image = (name: string, id: string) =>
2827+
ChatImageAttachment.make({
2828+
type: "image",
2829+
id: ChatAttachmentId.make(`thread-opencode2-adapter-${id}-1234-1234-1234-123456789abc`),
2830+
name,
2831+
mimeType: "image/png",
2832+
sizeBytes: 4,
2833+
});
2834+
const ids = yield* IdAllocator.IdAllocatorV2;
2835+
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2836+
const input = turnInput(thread);
2837+
yield* runtime.startTurn({
2838+
...input,
2839+
message: {
2840+
...input.message,
2841+
text: "What's in this image?",
2842+
attachments: [image("screenshot.png", "12345678")],
2843+
},
2844+
});
2845+
yield* runtime.steerTurn({
2846+
threadId,
2847+
runId: input.runId,
2848+
providerThread: thread,
2849+
providerTurnId: ids.derive.providerTurn({
2850+
driver: OPENCODE_PROVIDER,
2851+
nativeTurnId: `${SESSION}:attempt:attempt:opencode2-adapter`,
2852+
}),
2853+
message: {
2854+
...input.message,
2855+
messageId: MessageId.make("message:opencode2-adapter:steer"),
2856+
text: "And this one?",
2857+
attachments: [image("chart.png", "abcdefab")],
2858+
},
2859+
});
2860+
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2861+
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
2862+
);
2863+
2864+
it("gives OpenCode a file URL for each pasted image and leaves other attachments as paths", () => {
2865+
const image = ChatImageAttachment.make({
2866+
type: "image",
2867+
id: ChatAttachmentId.make("thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc"),
2868+
name: "screen shot.png",
2869+
mimeType: "image/png",
2870+
sizeBytes: 4,
2871+
});
2872+
const document = ChatFileAttachment.make({
2873+
type: "file",
2874+
id: ChatAttachmentId.make("thread-opencode2-adapter-abcdefab-1234-1234-1234-123456789abc"),
2875+
name: "requirements.pdf",
2876+
mimeType: "application/pdf",
2877+
sizeBytes: 4,
2878+
});
2879+
assert.deepEqual(promptImageFiles([image, document], "/home/a user/.t3/userdata/attachments"), [
2880+
{
2881+
uri: "file:///home/a%20user/.t3/userdata/attachments/thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc.png",
2882+
name: "screen shot.png",
2883+
},
2884+
]);
2885+
});
2886+
27902887
it.effect("reads user and assistant text from the session's message list", () =>
27912888
Effect.gen(function* () {
27922889
const runtime = yield* openCode2ReplayRuntimeWithInstructions([

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*
2222
* @module orchestration-v2/Adapters/OpenCode2AdapterV2
2323
*/
24+
import * as NodeURL from "node:url";
2425

2526
import {
2627
AbsolutePath,
@@ -38,6 +39,7 @@ import {
3839
import { Mcp } from "@opencode/schema/mcp";
3940
import {
4041
isOrchestrationV2WorkActive,
42+
type ChatAttachment,
4143
type OrchestrationV2AppThread,
4244
type OrchestrationV2ConversationMessage,
4345
type OrchestrationV2ExecutionNode,
@@ -70,6 +72,7 @@ import * as Schema from "effect/Schema";
7072
import * as Semaphore from "effect/Semaphore";
7173
import * as Stream from "effect/Stream";
7274

75+
import { resolveAttachmentPath } from "../../attachmentStore.ts";
7376
import * as ServerConfig from "../../config.ts";
7477
import { paginate, type OpenCode2StreamEvent } from "../../provider/opencode2/OpenCode2Client.ts";
7578
import * as OpenCode2Server from "../../provider/opencode2/OpenCode2Server.ts";
@@ -85,7 +88,10 @@ import * as KeyedLock from "@t3tools/shared/KeyedLock";
8588
import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model";
8689
import { causeErrorTag } from "@t3tools/shared/observability";
8790

88-
import { providerMessageTextWithAttachmentPaths } from "../AttachmentPrompt.ts";
91+
import {
92+
isProviderNativeImageAttachment,
93+
providerMessageTextWithAttachmentPaths,
94+
} from "../AttachmentPrompt.ts";
8995
import * as IdAllocator from "../IdAllocator.ts";
9096
import { backgroundWorkNotification, type BackgroundWorkReport } from "../Notification.ts";
9197
import * as ProviderContinuationRequests from "../ProviderContinuationRequests.ts";
@@ -801,6 +807,20 @@ const skillsNamed = (text: string, known: ReadonlySet<string>) => [
801807
),
802808
];
803809

810+
/**
811+
* A message's pasted images as prompt files, which OpenCode reads from T3's
812+
* attachment directory and gives the model as images. The prompt text still
813+
* names where each one is saved, as with 1.x.
814+
*/
815+
export const promptImageFiles = (
816+
attachments: ReadonlyArray<ChatAttachment>,
817+
attachmentsDir: string,
818+
) =>
819+
attachments.filter(isProviderNativeImageAttachment).flatMap((attachment) => {
820+
const path = resolveAttachmentPath({ attachmentsDir, attachment });
821+
return path === null ? [] : [{ uri: NodeURL.pathToFileURL(path).href, name: attachment.name }];
822+
});
823+
804824
/**
805825
* The turn's own tokens: steps add up, and the last step's input is the live
806826
* context size. A subagent's tokens are its own session's, never these.
@@ -3541,12 +3561,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
35413561
),
35423562
)
35433563
: [];
3564+
const files = promptImageFiles(turnInput.message.attachments, serverConfig.attachmentsDir);
35443565
if (!sending()) return;
35453566
return yield* client.session
35463567
.prompt({
35473568
sessionID,
35483569
id,
35493570
text: promptText(turnInput),
3571+
...(files.length === 0 ? {} : { files }),
35503572
...(skills.length === 0
35513573
? {}
35523574
: { skills: skills.map((id) => ({ id: Skill.ID.make(id) })) }),
@@ -3882,6 +3904,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
38823904
if (turn.settledInbox.has(inboxID)) return;
38833905
turn.steers.add(inboxID);
38843906
state.strandedSteers.delete(inboxID);
3907+
const files = promptImageFiles(
3908+
steerInput.message.attachments,
3909+
serverConfig.attachmentsDir,
3910+
);
38853911
yield* client.session
38863912
.prompt({
38873913
sessionID: Session.ID.make(sessionId),
@@ -3891,6 +3917,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
38913917
attachments: steerInput.message.attachments,
38923918
attachmentsDir: serverConfig.attachmentsDir,
38933919
}).trim(),
3920+
...(files.length === 0 ? {} : { files }),
38943921
delivery: "steer",
38953922
})
38963923
.pipe(

0 commit comments

Comments
 (0)