Skip to content

Commit eaaff72

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 5534558 commit eaaff72

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
@@ -1,4 +1,5 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import { assert, it } from "@effect/vitest";
34
import {
45
ChatAttachmentId,
@@ -27,6 +28,7 @@ import * as DateTime from "effect/DateTime";
2728
import * as Deferred from "effect/Deferred";
2829
import * as Effect from "effect/Effect";
2930
import * as Fiber from "effect/Fiber";
31+
import * as FileSystem from "effect/FileSystem";
3032
import * as Layer from "effect/Layer";
3133
import * as Logger from "effect/Logger";
3234
import * as Option from "effect/Option";
@@ -44,6 +46,7 @@ import * as ProviderContinuationRequests from "@t3tools/provider-core/server/Pro
4446
import {
4547
OPENCODE_2_STILL_STOPPING,
4648
OPENCODE_PROVIDER,
49+
presentPromptFiles,
4750
promptImageFiles,
4851
t3McpServerName,
4952
} from "@t3tools/provider-opencode/testing";
@@ -3335,9 +3338,15 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
33353338
it.effect("sends a turn's and a steer's pasted images as prompt files", () =>
33363339
Effect.gen(function* () {
33373340
const steerId = "msg_recorded_steer_image";
3338-
const attachmentsDir = "/t3/attachments";
3341+
const fileSystem = yield* FileSystem.FileSystem;
3342+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3343+
prefix: "t3-oc2-images-",
3344+
});
33393345
const screenshot = `${attachmentsDir}/thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc.png`;
33403346
const chart = `${attachmentsDir}/thread-opencode2-adapter-abcdefab-1234-1234-1234-123456789abc.png`;
3347+
yield* Effect.forEach([screenshot, chart], (file) =>
3348+
fileSystem.writeFileString(file, "png!"),
3349+
);
33413350
const { runtime, thread } = yield* resumed(
33423351
[
33433352
out("session.prompt", {
@@ -3402,7 +3411,20 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
34023411
},
34033412
});
34043413
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
3405-
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
3414+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(IdAllocator.layer, NodeServices.layer))),
3415+
);
3416+
3417+
it.effect("drops a pasted image whose saved file is gone", () =>
3418+
Effect.gen(function* () {
3419+
const fileSystem = yield* FileSystem.FileSystem;
3420+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
3421+
prefix: "t3-oc2-images-",
3422+
});
3423+
const kept = { uri: `file://${attachmentsDir}/kept.png`, name: "kept.png" };
3424+
yield* fileSystem.writeFileString(`${attachmentsDir}/kept.png`, "png!");
3425+
const deleted = { uri: `file://${attachmentsDir}/deleted.png`, name: "deleted.png" };
3426+
assert.deepEqual(yield* presentPromptFiles(fileSystem, [kept, deleted]), [kept]);
3427+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
34063428
);
34073429

34083430
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
@@ -42,6 +42,7 @@ import * as Crypto from "effect/Crypto";
4242
import * as Effect from "effect/Effect";
4343
import * as Hex from "effect/encoding/Hex";
4444
import * as Exit from "effect/Exit";
45+
import * as FileSystem from "effect/FileSystem";
4546
import * as Option from "effect/Option";
4647
import * as Queue from "effect/Queue";
4748
import * as Schedule from "effect/Schedule";
@@ -663,6 +664,15 @@ export const promptImageFiles = (
663664
: [{ uri: NodeURL.pathToFileURL(path).href, name: attachment.name }];
664665
});
665666

667+
/** OpenCode rejects the whole prompt for a missing file, so a deleted image keeps only its path text. */
668+
export const presentPromptFiles = (
669+
fileSystem: FileSystem.FileSystem,
670+
files: ReadonlyArray<{ readonly uri: string; readonly name: string }>,
671+
) =>
672+
Effect.filter(files, (file) =>
673+
fileSystem.exists(NodeURL.fileURLToPath(file.uri)).pipe(Effect.orElseSucceed(() => false)),
674+
);
675+
666676
const turnTokenUsage = (turn: ActiveTurn, status: OrchestrationV2ProviderTurn["status"]) =>
667677
turn.steps === 0
668678
? {
@@ -686,6 +696,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
686696
const idAllocator = yield* IdAllocator.IdAllocatorV2;
687697
const host = yield* ProviderHost.ProviderHost;
688698
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
699+
const fileSystem = yield* FileSystem.FileSystem;
689700
const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
690701
const crypto = yield* Crypto.Crypto;
691702
const driver = OPENCODE_PROVIDER;
@@ -3275,10 +3286,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
32753286
),
32763287
)
32773288
: [];
3278-
const files = promptImageFiles(
3279-
turnInput.message.attachments,
3280-
host.resolveAttachmentPath,
3281-
connection.external,
3289+
const files = yield* presentPromptFiles(
3290+
fileSystem,
3291+
promptImageFiles(
3292+
turnInput.message.attachments,
3293+
host.resolveAttachmentPath,
3294+
connection.external,
3295+
),
32823296
);
32833297
if (!sending()) return;
32843298
return yield* client.session
@@ -3607,10 +3621,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
36073621
if (turn.settledInbox.has(inboxID)) return;
36083622
turn.steers.add(inboxID);
36093623
state.strandedSteers.delete(inboxID);
3610-
const files = promptImageFiles(
3611-
steerInput.message.attachments,
3612-
host.resolveAttachmentPath,
3613-
connection.external,
3624+
const files = yield* presentPromptFiles(
3625+
fileSystem,
3626+
promptImageFiles(
3627+
steerInput.message.attachments,
3628+
host.resolveAttachmentPath,
3629+
connection.external,
3630+
),
36143631
);
36153632
yield* client.session
36163633
.prompt({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
} from "./server/adapter.ts";
66
export {
77
OPENCODE_2_STILL_STOPPING,
8+
presentPromptFiles,
89
promptImageFiles,
910
t3McpServerName,
1011
} from "./server/v2/adapter.ts";

0 commit comments

Comments
 (0)