Skip to content

Commit 0b9f910

Browse files
committed
fix(server): send OpenCode 2 only the pasted images whose saved file still exists
OpenCode rejects the whole prompt when an attached file can't be read, so an image deleted from the attachment store would fail the turn. It now keeps only its path text, as before images were sent.
1 parent fe2e71d commit 0b9f910

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* HTTP server. Frames reuse the shapes recorded against 2.0.18.
55
*/
66
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
7+
import * as NodeServices from "@effect/platform-node/NodeServices";
78
import { assert, it } from "@effect/vitest";
89
import {
910
ChatAttachmentId,
@@ -32,6 +33,7 @@ import * as DateTime from "effect/DateTime";
3233
import * as Deferred from "effect/Deferred";
3334
import * as Effect from "effect/Effect";
3435
import * as Fiber from "effect/Fiber";
36+
import * as FileSystem from "effect/FileSystem";
3537
import * as Layer from "effect/Layer";
3638
import * as Logger from "effect/Logger";
3739
import * as Option from "effect/Option";
@@ -49,6 +51,7 @@ import * as ProviderContinuationRequests from "@t3tools/provider-core/server/Pro
4951
import {
5052
OPENCODE_2_STILL_STOPPING,
5153
OPENCODE_PROVIDER,
54+
presentPromptFiles,
5255
promptImageFiles,
5356
t3McpServerName,
5457
} from "@t3tools/provider-opencode/testing";
@@ -3538,9 +3541,15 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
35383541
it.effect("sends a turn's and a steer's pasted images as prompt files", () =>
35393542
Effect.gen(function* () {
35403543
const steerId = "msg_recorded_steer_image";
3541-
const attachmentsDir = "/t3/attachments";
3544+
const fileSystem = yield* FileSystem.FileSystem;
3545+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3546+
prefix: "t3-oc2-images-",
3547+
});
35423548
const screenshot = `${attachmentsDir}/thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc.png`;
35433549
const chart = `${attachmentsDir}/thread-opencode2-adapter-abcdefab-1234-1234-1234-123456789abc.png`;
3550+
yield* Effect.forEach([screenshot, chart], (file) =>
3551+
fileSystem.writeFileString(file, "png!"),
3552+
);
35443553
const { runtime, thread } = yield* resumed(
35453554
[
35463555
out("session.prompt", {
@@ -3605,7 +3614,20 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
36053614
},
36063615
});
36073616
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
3608-
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
3617+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(IdAllocator.layer, NodeServices.layer))),
3618+
);
3619+
3620+
it.effect("drops a pasted image whose saved file is gone", () =>
3621+
Effect.gen(function* () {
3622+
const fileSystem = yield* FileSystem.FileSystem;
3623+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3624+
prefix: "t3-oc2-images-",
3625+
});
3626+
const kept = { uri: `file://${attachmentsDir}/kept.png`, name: "kept.png" };
3627+
yield* fileSystem.writeFileString(`${attachmentsDir}/kept.png`, "png!");
3628+
const deleted = { uri: `file://${attachmentsDir}/deleted.png`, name: "deleted.png" };
3629+
assert.deepEqual(yield* presentPromptFiles(fileSystem, [kept, deleted]), [kept]);
3630+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
36093631
);
36103632

36113633
it("gives a server T3 started a file URL for each pasted image, an external one none", () => {

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import * as Crypto from "effect/Crypto";
6565
import * as Effect from "effect/Effect";
6666
import * as Hex from "effect/encoding/Hex";
6767
import * as Exit from "effect/Exit";
68+
import * as FileSystem from "effect/FileSystem";
6869
import * as Option from "effect/Option";
6970
import * as Queue from "effect/Queue";
7071
import * as Schedule from "effect/Schedule";
@@ -844,6 +845,15 @@ export const promptImageFiles = (
844845
: [{ uri: NodeURL.pathToFileURL(path).href, name: attachment.name }];
845846
});
846847

848+
/** OpenCode rejects the whole prompt for a missing file, so a deleted image keeps only its path text. */
849+
export const presentPromptFiles = (
850+
fileSystem: FileSystem.FileSystem,
851+
files: ReadonlyArray<{ readonly uri: string; readonly name: string }>,
852+
) =>
853+
Effect.filter(files, (file) =>
854+
fileSystem.exists(NodeURL.fileURLToPath(file.uri)).pipe(Effect.orElseSucceed(() => false)),
855+
);
856+
847857
/**
848858
* The turn's own tokens: steps add up, and the last step's input is the live
849859
* context size. A subagent's tokens are its own session's, never these.
@@ -875,6 +885,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
875885
const idAllocator = yield* IdAllocator.IdAllocatorV2;
876886
const host = yield* ProviderHost.ProviderHost;
877887
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
888+
const fileSystem = yield* FileSystem.FileSystem;
878889
const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
879890
const crypto = yield* Crypto.Crypto;
880891
const driver = OPENCODE_PROVIDER;
@@ -3770,10 +3781,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
37703781
),
37713782
)
37723783
: [];
3773-
const files = promptImageFiles(
3774-
turnInput.message.attachments,
3775-
host.resolveAttachmentPath,
3776-
connection.external,
3784+
const files = yield* presentPromptFiles(
3785+
fileSystem,
3786+
promptImageFiles(
3787+
turnInput.message.attachments,
3788+
host.resolveAttachmentPath,
3789+
connection.external,
3790+
),
37773791
);
37783792
if (!sending()) return;
37793793
return yield* client.session
@@ -4131,10 +4145,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
41314145
if (turn.settledInbox.has(inboxID)) return;
41324146
turn.steers.add(inboxID);
41334147
state.strandedSteers.delete(inboxID);
4134-
const files = promptImageFiles(
4135-
steerInput.message.attachments,
4136-
host.resolveAttachmentPath,
4137-
connection.external,
4148+
const files = yield* presentPromptFiles(
4149+
fileSystem,
4150+
promptImageFiles(
4151+
steerInput.message.attachments,
4152+
host.resolveAttachmentPath,
4153+
connection.external,
4154+
),
41384155
);
41394156
yield* client.session
41404157
.prompt({

‎packages/provider-opencode/src/testing.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
} from "./server/adapter.ts";
1313
export {
1414
OPENCODE_2_STILL_STOPPING,
15+
presentPromptFiles,
1516
promptImageFiles,
1617
t3McpServerName,
1718
} from "./server/v2/adapter.ts";

0 commit comments

Comments
 (0)