Skip to content

Commit 3f440fd

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 8cefb72 commit 3f440fd

2 files changed

Lines changed: 49 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,
@@ -31,6 +32,7 @@ import * as DateTime from "effect/DateTime";
3132
import * as Deferred from "effect/Deferred";
3233
import * as Effect from "effect/Effect";
3334
import * as Fiber from "effect/Fiber";
35+
import * as FileSystem from "effect/FileSystem";
3436
import * as Layer from "effect/Layer";
3537
import * as Logger from "effect/Logger";
3638
import * as Option from "effect/Option";
@@ -52,6 +54,7 @@ import * as ProviderContinuationRequests from "@t3tools/provider-core/server/con
5254
import { OPENCODE_PROVIDER } from "./OpenCodeAdapterV2.ts";
5355
import {
5456
OPENCODE_2_STILL_STOPPING,
57+
presentPromptFiles,
5558
promptImageFiles,
5659
t3McpServerName,
5760
} from "./OpenCode2AdapterV2.ts";
@@ -2825,9 +2828,15 @@ describe("OpenCode2 adapter", () => {
28252828
it.effect("sends a turn's and a steer's pasted images as prompt files", () =>
28262829
Effect.gen(function* () {
28272830
const steerId = "msg_recorded_steer_image";
2828-
const attachmentsDir = "/t3/attachments";
2831+
const fileSystem = yield* FileSystem.FileSystem;
2832+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
2833+
prefix: "t3-oc2-images-",
2834+
});
28292835
const screenshot = `${attachmentsDir}/thread-opencode2-adapter-12345678-1234-1234-1234-123456789abc.png`;
28302836
const chart = `${attachmentsDir}/thread-opencode2-adapter-abcdefab-1234-1234-1234-123456789abc.png`;
2837+
yield* Effect.forEach([screenshot, chart], (file) =>
2838+
fileSystem.writeFileString(file, "png!"),
2839+
);
28312840
const { runtime, thread } = yield* resumed(
28322841
[
28332842
out("session.prompt", {
@@ -2892,7 +2901,20 @@ describe("OpenCode2 adapter", () => {
28922901
},
28932902
});
28942903
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2895-
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
2904+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(IdAllocator.layer, NodeServices.layer))),
2905+
);
2906+
2907+
it.effect("drops a pasted image whose saved file is gone", () =>
2908+
Effect.gen(function* () {
2909+
const fileSystem = yield* FileSystem.FileSystem;
2910+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
2911+
prefix: "t3-oc2-images-",
2912+
});
2913+
const kept = { uri: `file://${attachmentsDir}/kept.png`, name: "kept.png" };
2914+
yield* fileSystem.writeFileString(`${attachmentsDir}/kept.png`, "png!");
2915+
const deleted = { uri: `file://${attachmentsDir}/deleted.png`, name: "deleted.png" };
2916+
assert.deepEqual(yield* presentPromptFiles(fileSystem, [kept, deleted]), [kept]);
2917+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
28962918
);
28972919

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

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import * as Crypto from "effect/Crypto";
6464
import * as Effect from "effect/Effect";
6565
import * as Hex from "effect/encoding/Hex";
6666
import * as Exit from "effect/Exit";
67+
import * as FileSystem from "effect/FileSystem";
6768
import * as Option from "effect/Option";
6869
import * as Queue from "effect/Queue";
6970
import * as Schedule from "effect/Schedule";
@@ -827,6 +828,15 @@ export const promptImageFiles = (
827828
: [{ uri: NodeURL.pathToFileURL(path).href, name: attachment.name }];
828829
});
829830

831+
/** OpenCode rejects the whole prompt for a missing file, so a deleted image keeps only its path text. */
832+
export const presentPromptFiles = (
833+
fileSystem: FileSystem.FileSystem,
834+
files: ReadonlyArray<{ readonly uri: string; readonly name: string }>,
835+
) =>
836+
Effect.filter(files, (file) =>
837+
fileSystem.exists(NodeURL.fileURLToPath(file.uri)).pipe(Effect.orElseSucceed(() => false)),
838+
);
839+
830840
/**
831841
* The turn's own tokens: steps add up, and the last step's input is the live
832842
* context size. A subagent's tokens are its own session's, never these.
@@ -857,6 +867,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
857867
const server = yield* OpenCode2Server.OpenCode2Server;
858868
const idAllocator = yield* IdAllocator.IdAllocatorV2;
859869
const serverConfig = yield* ServerConfig.ServerConfig;
870+
const fileSystem = yield* FileSystem.FileSystem;
860871
const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
861872
const crypto = yield* Crypto.Crypto;
862873
const driver = OPENCODE_PROVIDER;
@@ -3571,10 +3582,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
35713582
),
35723583
)
35733584
: [];
3574-
const files = promptImageFiles(
3575-
turnInput.message.attachments,
3576-
serverConfig.attachmentsDir,
3577-
connection.external,
3585+
const files = yield* presentPromptFiles(
3586+
fileSystem,
3587+
promptImageFiles(
3588+
turnInput.message.attachments,
3589+
serverConfig.attachmentsDir,
3590+
connection.external,
3591+
),
35783592
);
35793593
if (!sending()) return;
35803594
return yield* client.session
@@ -3918,10 +3932,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
39183932
if (turn.settledInbox.has(inboxID)) return;
39193933
turn.steers.add(inboxID);
39203934
state.strandedSteers.delete(inboxID);
3921-
const files = promptImageFiles(
3922-
steerInput.message.attachments,
3923-
serverConfig.attachmentsDir,
3924-
connection.external,
3935+
const files = yield* presentPromptFiles(
3936+
fileSystem,
3937+
promptImageFiles(
3938+
steerInput.message.attachments,
3939+
serverConfig.attachmentsDir,
3940+
connection.external,
3941+
),
39253942
);
39263943
yield* client.session
39273944
.prompt({

0 commit comments

Comments
 (0)