@devries-cameron20 / t3code Mirror · read-only / commits / 3a8c250cb0
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 00:45 UTC

feat(opencode): track native image job outcomes and previews (#34089)

OpenCode image-generation tool success acknowledges a launched background job. Keep that job pending until its native outcome arrives, retain the outcome on the original tool item, and serve size-limited image previews through existing rendering and remote file paths. Recover outcomes after stream loss, allow later exact-path notices to add previews, and clear descendant image tasks from parent rosters on completion or transport failure. Keep older-server fallback and the pinned SDK. Validation: check:changed passed with 147 tests; required CI passed and review findings were addressed on 62addc83319ad73fd25f52fbeb3cbcf06504a093. Client screenshots use controlled fixtures; no paid image generation was used. Model: GPT 6.1 Sol. Harness: Codex in T3 Code.

Yash Singh committed Nov 10, 2026 at 18:30 UTC 3a8c250cb0c8056f2f563091fd412fe5db809fa1
5 files changed +795 -10
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+420 -2
@@ -317,7 +317,7 @@ const colorForm = {
317 const resumed = (
318 entries: ReadonlyArray<ProviderReplayEntry>,
319 options?: {
320 - readonly version?: "2.2.5";
320 + readonly version?: "2.2.5" | "2.2.6";
321 readonly goal?: OrchestrationV2ProviderThread["goal"];
322 readonly external?: boolean;
323 readonly supervised?: boolean;
@@ -326,6 +326,7 @@ const resumed = (
326 readonly resolveExtractedTextPath?: (path: string) => Effect.Effect<string | undefined>;
327 readonly fileSystem?: FileSystem.FileSystem;
328 readonly onPrompt?: (input: unknown) => void;
329 + readonly borrowers?: { current: number };
330 },
331 ) =>
332 Effect.gen(function* () {
@@ -335,7 +336,7 @@ const resumed = (
336 out("session.get", { sessionID: SESSION }),
337 replyData("session.get", sessionInfo()),
338 ...noOpenRequests,
338 - ...(options?.version === "2.2.5"
339 + ...(options?.version !== undefined
340 ? [out("rpc.call", "<any>"), reply("rpc.call", { output: null })]
341 : []),
342 ...withInstructions([
@@ -352,6 +353,7 @@ const resumed = (
353 ],
354 {
355 ...(options?.version === undefined ? {} : { version: options.version }),
356 + ...(options?.borrowers === undefined ? {} : { borrowers: options.borrowers }),
357 ...(options?.fileSystem === undefined ? {} : { fileSystem: options.fileSystem }),
358 ...(options?.onPrompt === undefined ? {} : { onPrompt: options.onPrompt }),
359 ...(options?.external === undefined ? {} : { external: options.external }),
@@ -2891,6 +2893,422 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
2893 }).pipe(Effect.scoped),
2894 );
2895
2896 + it.effect.each(["completed", "error", "cancelled"] as const)(
2897 + "retains native image job %s after its launch returns",
2898 + (outcome) =>
2899 + Effect.gen(function* () {
2900 + const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-image" };
2901 + const notice = (sessionID: string) =>
2902 + event("session.inbox.enqueued", {
2903 + sessionID,
2904 + inboxID: `msg_image_${sessionID}`,
2905 + item: {
2906 + type: "synthetic",
2907 + delivery: "steer",
2908 + payload: {
2909 + description: "Image generation",
2910 + text:
2911 + outcome === "completed"
2912 + ? "Image outcome retained. Saved image: /work/opencode2/image.png (test/image)."
2913 + : "Image outcome retained",
2914 + metadata: {
2915 + source: "experimental_image_generate",
2916 + jobID: "image_test",
2917 + state: outcome,
2918 + path: "/work/opencode2/image",
2919 + },
2920 + },
2921 + },
2922 + });
2923 + const { runtime, thread } = yield* resumed(
2924 + [
2925 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2926 + promptAccepted,
2927 + event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
2928 + event("session.tool.called", { ...tool, input: { prompt: "A tree" }, executed: false }),
2929 + event(
2930 + "session.tool.success",
2931 + {
2932 + ...tool,
2933 + content: [{ type: "text", text: "Job launched" }],
2934 + metadata: {
2935 + jobID: "image_test",
2936 + path: "/work/opencode2/image",
2937 + model: "test/image",
2938 + },
2939 + executed: false,
2940 + },
2941 + undefined,
2942 + 2,
2943 + ),
2944 + event("session.execution.succeeded", { sessionID: SESSION }),
2945 + notice("ses_other"),
2946 + notice(SESSION),
2947 + ...(outcome === "completed"
2948 + ? [
2949 + out("image.read", {
2950 + path: "/api/fs/read/image.png",
2951 + "location[directory]": WORK,
2952 + }),
2953 + reply("image.read", { imageBase64: "iVBORw0KGgo=" }),
2954 + ]
2955 + : []),
2956 + ],
2957 + { external: true },
2958 + );
2959 + let launched = false;
2960 + const events = yield* runtime.events.pipe(
2961 + Stream.takeUntil((e) => {
2962 + if (e.type !== "provider_thread.updated") return false;
2963 + if (
2964 + e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_test")
2965 + )
2966 + launched = true;
2967 + return launched && e.pendingBackgroundWork === false;
2968 + }),
2969 + Stream.runCollect,
2970 + Effect.forkScoped,
2971 + );
2972 + yield* runtime.startTurn(turnInput(thread));
2973 + const collected = yield* Fiber.join(events);
2974 + const items = collected.flatMap((e) =>
2975 + e.type === "turn_item.updated" && e.turnItem.nativeItemRef?.nativeId === "call-image"
2976 + ? [e.turnItem]
2977 + : [],
2978 + );
2979 + assert.deepEqual(
2980 + items.map((item) => item.status),
2981 + [
2982 + "running",
2983 + "running",
2984 + "running",
2985 + outcome === "error" ? "failed" : outcome === "cancelled" ? "interrupted" : "completed",
2986 + ],
2987 + );
2988 + assert.include(JSON.stringify(items.at(-1)), "Image outcome retained");
2989 + if (outcome === "completed") {
2990 + const item = items.at(-1);
2991 + assert.deepEqual(item?.type === "dynamic_tool" ? toolOutputImages(item.output) : [], [
2992 + { mimeType: "image/png", data: "iVBORw0KGgo=" },
2993 + ]);
2994 + }
2995 + const rosters = collected.flatMap((e) =>
2996 + e.type === "provider_thread.updated"
2997 + ? [e.providerThread.pendingBackgroundTasks ?? []]
2998 + : [],
2999 + );
3000 + assert.isTrue(rosters.some((tasks) => tasks.some((task) => task.taskId === "image_test")));
3001 + assert.deepEqual(rosters.at(-1), []);
3002 + }).pipe(Effect.scoped),
3003 + );
3004 +
3005 + it.effect.each(["history", "list", "late-preview"] as const)(
3006 + "recovers native image job outcomes from %s after reconnect",
3007 + (source) =>
3008 + Effect.gen(function* () {
3009 + const { runtime, thread } = yield* resumed(
3010 + [
3011 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3012 + promptAccepted,
3013 + ...(() => {
3014 + const frames: Array<ProviderReplayEntry> = reconnected({}, "succeeded", {
3015 + content: [
3016 + {
3017 + type: "tool",
3018 + id: "call-image-gap",
3019 + name: "experimental_image_generate",
3020 + executed: false,
3021 + state: {
3022 + status: "completed",
3023 + input: { prompt: "A tree" },
3024 + content: [{ type: "text", text: "Job launched" }],
3025 + metadata: { jobID: "image_gap", path: "/tmp/image", model: "test/image" },
3026 + },
3027 + time: { created: 2, ran: 2, completed: 2 },
3028 + },
3029 + ],
3030 + });
3031 + if (source !== "history")
3032 + frames.splice(2, 0, out("rpc.call", "<any>"), reply("rpc.call", { output: null }));
3033 + return frames;
3034 + })(),
3035 + out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
3036 + reply("message.list", {
3037 + data:
3038 + source !== "history"
3039 + ? []
3040 + : [
3041 + {
3042 + id: "msg_image_gap",
3043 + time: { created: 3 },
3044 + type: "synthetic",
3045 + text: "Recovered image failure",
3046 + description: "Image generation",
3047 + metadata: {
3048 + source: "experimental_image_generate",
3049 + jobID: "image_gap",
3050 + state: "error",
3051 + path: "/tmp/image",
3052 + },
3053 + },
3054 + ],
3055 + cursor: {},
3056 + }),
3057 + ...(source !== "history"
3058 + ? [
3059 + out("imageJobs", { sessionID: SESSION, kind: "image" }),
3060 + reply("imageJobs", {
3061 + data: [
3062 + {
3063 + kind: "image",
3064 + id: "image_gap",
3065 + sessionID: SESSION,
3066 + status: source === "late-preview" ? "completed" : "error",
3067 + path: "/tmp/image",
3068 + model: "test/image",
3069 + time: { started: 2, completed: 3 },
3070 + },
3071 + ],
3072 + }),
3073 + ]
3074 + : []),
3075 + ...(source === "late-preview"
3076 + ? [
3077 + event("session.inbox.enqueued", {
3078 + sessionID: SESSION,
3079 + inboxID: "msg_image_late",
3080 + item: {
3081 + type: "synthetic",
3082 + delivery: "steer",
3083 + payload: {
3084 + description: "Image generation",
3085 + text: "Image request finished. Saved image: /tmp/image.png (test/image).",
3086 + metadata: {
3087 + source: "experimental_image_generate",
3088 + jobID: "image_gap",
3089 + state: "completed",
3090 + path: "/tmp/image",
3091 + },
3092 + },
3093 + },
3094 + }),
3095 + out("image.read", {
3096 + path: "/api/fs/read/image.png",
3097 + "location[directory]": "/tmp",
3098 + }),
3099 + reply("image.read", { imageBase64: "iVBORw0KGgo=" }),
3100 + ]
3101 + : []),
3102 + ],
3103 + { ...(source !== "history" ? { version: "2.2.6" as const } : {}), external: true },
3104 + );
3105 + const events = yield* runtime.events.pipe(
3106 + Stream.takeUntil(
3107 + (e) =>
3108 + e.type === "turn_item.updated" &&
3109 + e.turnItem.nativeItemRef?.nativeId === "call-image-gap" &&
3110 + (source === "late-preview"
3111 + ? e.turnItem.type === "dynamic_tool" &&
3112 + toolOutputImages(e.turnItem.output).length > 0
3113 + : e.turnItem.status === "failed"),
3114 + ),
3115 + Stream.runCollect,
3116 + Effect.forkScoped,
3117 + );
3118 + yield* runtime.startTurn(turnInput(thread));
3119 + const collected = yield* Fiber.join(events);
3120 + assert.include(
3121 + JSON.stringify(collected.at(-1)),
3122 + source === "late-preview"
3123 + ? "Saved image: /tmp/image.png"
3124 + : source !== "history"
3125 + ? "Image generation error"
3126 + : "Recovered image failure",
3127 + );
3128 + assert.isTrue(
3129 + collected.some(
3130 + (e) =>
3131 + e.type === "provider_thread.updated" &&
3132 + e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_gap"),
3133 + ),
3134 + );
3135 + }).pipe(Effect.scoped),
3136 + );
3137 +
3138 + it.effect("settles a recovered image outcome when preview I/O is interrupted", () =>
3139 + Effect.gen(function* () {
3140 + const reading = yield* Deferred.make<void>();
3141 + const interrupted = yield* Deferred.make<void>();
3142 + const fs = yield* FileSystem.FileSystem;
3143 + const { runtime, thread } = yield* resumed(
3144 + [
3145 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3146 + promptAccepted,
3147 + ...reconnected({}, "succeeded", {
3148 + content: [
3149 + {
3150 + type: "tool",
3151 + id: "call-image-slow",
3152 + name: "experimental_image_generate",
3153 + executed: false,
3154 + state: {
3155 + status: "completed",
3156 + input: { prompt: "A tree" },
3157 + content: [{ type: "text", text: "Launched" }],
3158 + metadata: { jobID: "image_slow", path: "/tmp/image", model: "test/image" },
3159 + },
3160 + time: { created: 2, ran: 2, completed: 2 },
3161 + },
3162 + ],
3163 + }),
3164 + out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
3165 + reply("message.list", {
3166 + data: [
3167 + {
3168 + id: "msg_image_slow",
3169 + type: "synthetic",
3170 + time: { created: 3 },
3171 + text: "Image request finished. Saved image: /tmp/image.png (test/image).",
3172 + description: "Image generation",
3173 + metadata: {
3174 + source: "experimental_image_generate",
3175 + jobID: "image_slow",
3176 + state: "completed",
3177 + path: "/tmp/image",
3178 + },
3179 + },
3180 + ],
3181 + cursor: {},
3182 + }),
3183 + event("session.model.selected", {
3184 + sessionID: SESSION,
3185 + model: { providerID: "opencode", id: "big-pickle", variant: "recovery-observed" },
3186 + }),
3187 + ],
3188 + {
3189 + fileSystem: {
3190 + ...fs,
3191 + stat: (path) =>
3192 + path === "/tmp/image.png"
3193 + ? Deferred.succeed(reading, undefined).pipe(
3194 + Effect.andThen(Effect.never),
3195 + Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)),
3196 + )
3197 + : fs.stat(path),
3198 + },
3199 + },
3200 + );
3201 + const events = yield* runtime.events.pipe(
3202 + Stream.takeUntil(
3203 + (e) =>
3204 + e.type === "provider_thread.updated" &&
3205 + e.providerThread.nativeMetadata?.modelSelection?.options?.some(
3206 + (option) => option.value === "recovery-observed",
3207 + ) === true,
3208 + ),
3209 + Stream.runCollect,
3210 + Effect.forkScoped,
3211 + );
3212 + yield* runtime.startTurn(turnInput(thread));
3213 + yield* Deferred.await(reading);
3214 + yield* TestClock.adjust("5 seconds");
3215 + yield* Deferred.await(interrupted);
3216 + const collected = yield* Fiber.join(events);
3217 + assert.isTrue(
3218 + collected.some(
3219 + (e) =>
3220 + e.type === "turn_item.updated" &&
3221 + e.turnItem.nativeItemRef?.nativeId === "call-image-slow" &&
3222 + e.turnItem.status === "completed",
3223 + ),
3224 + );
3225 + assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3226 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3227 + );
3228 +
3229 + it.effect.each(["root", "child"] as const)(
3230 + "settles %s native image tasks when reconnect attempts fail",
3231 + (owner) =>
3232 + Effect.gen(function* () {
3233 + const attempts = yield* Effect.forEach([0, 1, 2, 3, 4], () => Deferred.make<void>());
3234 + let count = 0;
3235 + let failures = 0;
3236 + const borrowers = {
3237 + get current() {
3238 + return count;
3239 + },
3240 + set current(next: number) {
3241 + if (next < count && count === 2) {
3242 + const attempt = attempts[failures++];
3243 + if (attempt !== undefined) Deferred.doneUnsafe(attempt, Effect.void);
3244 + }
3245 + count = next;
3246 + },
3247 + };
3248 + const tool = {
3249 + sessionID: owner === "root" ? SESSION : CHILD,
3250 + assistantMessageID: "msg_assistant",
3251 + id: "call-image-lost",
3252 + };
3253 + const { runtime, thread } = yield* resumed(
3254 + [
3255 + ...(owner === "root"
3256 + ? [out("session.prompt", { sessionID: SESSION, text: "<any>" }), promptAccepted]
3257 + : [
3258 + ...backgroundLaunch(CHILD),
3259 + event("session.execution.started", { sessionID: CHILD }),
3260 + ]),
3261 + event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
3262 + event(
3263 + "session.tool.success",
3264 + {
3265 + ...tool,
3266 + content: [{ type: "text", text: "Launched" }],
3267 + metadata: { jobID: "image_lost", path: "/tmp/image", model: "test/image" },
3268 + executed: false,
3269 + },
3270 + undefined,
3271 + 2,
3272 + ),
3273 + ...(owner === "child"
3274 + ? [event("session.execution.succeeded", { sessionID: CHILD })]
3275 + : []),
3276 + event("session.execution.succeeded", { sessionID: SESSION }),
3277 + { type: "runtime_exit", status: "success" },
3278 + ],
3279 + { borrowers },
3280 + );
3281 + const events = yield* runtime.events.pipe(Stream.runCollect, Effect.forkScoped);
3282 + yield* runtime.startTurn(withLineage(thread));
3283 + yield* Deferred.await(attempts[0]!);
3284 + for (const attempt of attempts.slice(1)) {
3285 + yield* TestClock.adjust("2 seconds");
3286 + yield* Deferred.await(attempt);
3287 + }
3288 + const collected = yield* Fiber.join(events);
3289 + assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3290 + assert.isTrue(
3291 + collected.some(
3292 + (e) =>
3293 + e.type === "turn_item.updated" &&
3294 + e.turnItem.nativeItemRef?.nativeId ===
3295 + (owner === "root" ? tool.id : `${CHILD}:${tool.id}`) &&
3296 + e.turnItem.status === "failed",
3297 + ),
3298 + );
3299 + const rootSnapshots = collected.flatMap((e) =>
3300 + e.type === "provider_thread.updated" && e.providerThread.id === thread.id ? [e] : [],
3301 + );
3302 + assert.isFalse(rootSnapshots.at(-1)?.pendingBackgroundWork);
3303 + assert.deepEqual(rootSnapshots.at(-1)?.providerThread.pendingBackgroundTasks, []);
3304 + assert.isTrue(
3305 + rootSnapshots.some((e) =>
3306 + e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_lost"),
3307 + ),
3308 + );
3309 + }).pipe(Effect.scoped),
3310 + );
3311 +
3312 it.effect("projects successful native todo updates into one checklist", () =>
3313 Effect.gen(function* () {
3314 const updates = [
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts
+9
@@ -54,6 +54,10 @@ const operationOf = (
54 query: Record<string, unknown>,
55 body: unknown,
56 ) => {
57 + if (method === "GET" && path === "/api/experimental/job")
58 + return { type: "imageJobs", input: query };
59 + if (method === "GET" && path.startsWith("/api/fs/read/"))
60 + return { type: "image.read", input: { path, ...query } };
61 const rpc = /^\/api\/rpc\/([^/]+)\/([^/]+)$/.exec(path);
62 if (method === "POST" && rpc !== null)
63 return {
@@ -176,6 +180,11 @@ const replayHttpClient = (
180 }
181 // Recorded responses are the raw HTTP bodies; `null` is an empty 204.
182 const body = await controller.response(operation.type);
183 + if (typeof body === "object" && body !== null && "imageBase64" in body) {
184 + return new Response(Buffer.from(String(body.imageBase64), "base64"), {
185 + headers: { "content-type": "image/png" },
186 + });
187 + }
188 if (body === "<hang>") return new Promise<Response>(() => {});
189 if (typeof body === "object" && body !== null && "status" in body && "body" in body) {
190 return new Response(encodeJson(body.body), {
packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts
+81
@@ -8,6 +8,7 @@ import * as Layer from "effect/Layer";
8 import * as Redacted from "effect/Redacted";
9 import * as Schema from "effect/Schema";
10 import * as TestClock from "effect/testing/TestClock";
11 +import * as UrlParams from "effect/http/UrlParams";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as HttpClientResponse from "effect/http/HttpClientResponse";
14 import { describe } from "vite-plus/test";
@@ -167,3 +168,83 @@ describe("OpenCode2Client message history", () => {
168 }),
169 );
170 });
171 +
172 +describe("OpenCode2Client native image jobs", () => {
173 + it.effect.each([
174 + { path: "/work/image.png", location: "/work" },
175 + { path: "/tmp/images/session/image.png", location: "/tmp/images/session" },
176 + ])("reads remote image $path through its native filesystem location", ({ path, location }) =>
177 + Effect.gen(function* () {
178 + const transport = HttpClient.make((request) =>
179 + Effect.sync(() => {
180 + assert.strictEqual(request.url, "http://remote.test/api/fs/read/image.png");
181 + assert.deepEqual(UrlParams.toRecord(request.urlParams), {
182 + "location[directory]": location,
183 + });
184 + return HttpClientResponse.fromWeb(
185 + request,
186 + new Response(Buffer.from("iVBORw0KGgo=", "base64")),
187 + );
188 + }),
189 + );
190 + const service = yield* OpenCode2Client.make.pipe(
191 + Effect.provideService(HttpClient.HttpClient, transport),
192 + );
193 + const api = yield* service.connect({ baseUrl: "http://remote.test", password: "secret" });
194 + assert.deepEqual(yield* api.readImage(path, "/work"), {
195 + uri: "data:image/png;base64,iVBORw0KGgo=",
196 + mime: "image/png",
197 + });
198 + }),
199 + );
200 +
201 + it.effect("rejects remote previews above the shared image limit", () =>
202 + Effect.gen(function* () {
203 + const transport = HttpClient.make((request) =>
204 + Effect.succeed(
205 + HttpClientResponse.fromWeb(request, new Response(new Uint8Array(11 * 1024 * 1024))),
206 + ),
207 + );
208 + const service = yield* OpenCode2Client.make.pipe(
209 + Effect.provideService(HttpClient.HttpClient, transport),
210 + );
211 + const api = yield* service.connect({ baseUrl: "http://remote.test", password: "secret" });
212 + const result = yield* api.readImage("/work/image.png", "/work").pipe(Effect.result);
213 + assert.equal(result._tag, "Failure");
214 + }),
215 + );
216 +
217 + it.effect("reads scoped image jobs without accepting other sessions or job kinds", () =>
218 + Effect.gen(function* () {
219 + const job = {
220 + kind: "image",
221 + id: "image_test",
222 + sessionID: "ses_owner",
223 + status: "running",
224 + path: "/work/image",
225 + model: "test/image",
226 + } as const;
227 + const transport = HttpClient.make((request) =>
228 + Effect.sync(() => {
229 + assert.strictEqual(request.headers.authorization, utf8Basic("secret"));
230 + return HttpClientResponse.fromWeb(
231 + request,
232 + Response.json({
233 + data: [
234 + job,
235 + { ...job, sessionID: "ses_other" },
236 + { ...job, kind: "shell" },
237 + { ...job, status: "future" },
238 + ],
239 + }),
240 + );
241 + }),
242 + );
243 + const service = yield* OpenCode2Client.make.pipe(
244 + Effect.provideService(HttpClient.HttpClient, transport),
245 + );
246 + const api = yield* service.connect({ baseUrl: "http://remote.test", password: "secret" });
247 + assert.deepEqual(yield* api.imageJobs("ses_owner"), [job]);
248 + }),
249 + );
250 +});
packages/provider-opencode/src/server/v2/OpenCode2Client.ts
+82
@@ -1,3 +1,5 @@
1 +import * as Path from "effect/Path";
2 +import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
3 import { OpenCode, type OpenCodeClient } from "@opencode/client/effect";
4 import { OpenCodeEvent } from "@opencode/protocol/groups/event";
5 import * as Context from "effect/Context";
@@ -50,9 +52,43 @@ export type OpenCode2StreamEvent =
52 | OpenCode2UnreadableTerminal
53 | OpenCode2UnreadableStart;
54
55 +export class OpenCode2ImageError extends Schema.TaggedError<OpenCode2ImageError>()(
56 + "OpenCode2ImageError",
57 + { cause: Schema.Defect() },
58 +) {}
59 +
60 +const IMAGE_MIME: Readonly<Record<string, string>> = {
61 + ".png": "image/png",
62 + ".jpg": "image/jpeg",
63 + ".jpeg": "image/jpeg",
64 + ".webp": "image/webp",
65 + ".gif": "image/gif",
66 +};
67 +export const imagePreviewMime = (extension: string) => IMAGE_MIME[extension.toLowerCase()];
68 +
69 +const NativeImageJob = Schema.Struct({
70 + kind: Schema.Literal("image"),
71 + id: Schema.String,
72 + sessionID: Schema.String,
73 + status: Schema.Literals(["running", "completed", "error", "cancelled"]),
74 + path: Schema.String,
75 + model: Schema.String,
76 +});
77 +const decodeJobs = Schema.decodeUnknownEffect(
78 + Schema.Struct({ data: Schema.Array(Schema.Unknown) }),
79 +);
80 +const decodeImageJob = Schema.decodeUnknownOption(NativeImageJob);
81 +
82 /** events resolves on subscription acceptance; subscribe before work because the stream is volatile. */
83 export interface OpenCode2Api {
84 readonly client: OpenCodeClient;
85 + readonly imageJobs: (
86 + sessionID: string,
87 + ) => Effect.Effect<ReadonlyArray<typeof NativeImageJob.Type>, OpenCode2ImageError>;
88 + readonly readImage: (
89 + path: string,
90 + directory: string,
91 + ) => Effect.Effect<{ uri: string; mime: string }, OpenCode2ImageError>;
92 readonly events: Effect.Effect<
93 Stream.Stream<
94 OpenCode2StreamEvent,
@@ -202,6 +238,7 @@ const fillCompactionRecents = HttpClient.transformResponse(
238 /** @public */
239 export const make = Effect.gen(function* () {
240 const httpClient = yield* HttpClient.HttpClient;
241 + const paths = yield* Path.Path.pipe(Effect.provide(Path.layer));
242 return OpenCode2Client.of({
243 connect: Effect.fn("OpenCode2Client.connect")(function ({ baseUrl, password }) {
244 const authenticated = HttpClient.mapRequest(
@@ -212,6 +249,51 @@ export const make = Effect.gen(function* () {
249 Effect.provideService(HttpClient.HttpClient, fillCompactionRecents(authenticated)),
250 Effect.map((client) => ({
251 client,
252 + imageJobs: (sessionID: string) =>
253 + authenticated
254 + .get(`${baseUrl}/api/experimental/job`, {
255 + urlParams: { sessionID, kind: "image" },
256 + })
257 + .pipe(
258 + Effect.flatMap(HttpClientResponse.filterStatusOk),
259 + Effect.flatMap((response) => response.json),
260 + Effect.flatMap(decodeJobs),
261 + Effect.map((page) =>
262 + page.data.flatMap((item) => {
263 + const job = decodeImageJob(item);
264 + return Option.isSome(job) && job.value.sessionID === sessionID
265 + ? [job.value]
266 + : [];
267 + }),
268 + ),
269 + Effect.mapError((cause) => new OpenCode2ImageError({ cause })),
270 + ),
271 + readImage: Effect.fn("OpenCode2Client.readImage")(
272 + function* (path: string, directory: string) {
273 + const mime = imagePreviewMime(paths.extname(path));
274 + if (mime === undefined)
275 + return yield* new OpenCode2ImageError({ cause: "Unsupported image format" });
276 + const relative = paths.relative(directory, path);
277 + // OpenCode confines reads to the requested location, including its own temp images.
278 + const outside =
279 + relative === ".." ||
280 + relative.startsWith(`..${paths.sep}`) ||
281 + paths.isAbsolute(relative);
282 + const location = outside ? paths.dirname(path) : directory;
283 + const file = outside ? paths.basename(path) : relative;
284 + const response = yield* authenticated
285 + .get(`${baseUrl}/api/fs/read/${encodeURIComponent(file)}`, {
286 + urlParams: { "location[directory]": location },
287 + })
288 + .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
289 + const bytes = yield* response.arrayBuffer;
290 + if (bytes.byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)
291 + return yield* new OpenCode2ImageError({ cause: "Image exceeds preview limit" });
292 + return { uri: `data:${mime};base64,${Buffer.from(bytes).toString("base64")}`, mime };
293 + },
294 + Effect.timeout("10 seconds"),
295 + Effect.mapError((cause) => new OpenCode2ImageError({ cause })),
296 + ),
297 events: readEvents.pipe(
298 Effect.provideService(
299 HttpClient.HttpClient,
packages/provider-opencode/src/server/v2/adapter.ts
+203 -8
@@ -1,3 +1,5 @@
1 +import * as Path from "effect/Path";
2 +import * as Option from "effect/Option";
3 import * as NodeURL from "node:url";
4
5 import {
@@ -15,6 +17,7 @@ import {
17 } from "@opencode/client/effect";
18 import { Mcp } from "@opencode/schema/mcp";
19 import {
20 + PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
21 isOrchestrationV2WorkActive,
22 parseGoalCommand,
23 type ChatAttachment,
@@ -304,6 +307,18 @@ interface ThreadState {
307 readonly sessionId: string;
308 providerThread: OrchestrationV2ProviderThread;
309 publishedBackgroundWork?: boolean;
310 + readonly imageJobs: Map<
311 + string,
312 + {
313 + readonly turn: ActiveTurn;
314 + readonly toolId: string;
315 + readonly tool: { readonly name: string; input: Record<string, unknown> };
316 + previewed: boolean;
317 + readonly path: string;
318 + readonly model: string;
319 + status: "running" | "completed" | "error" | "cancelled";
320 + }
321 + >;
322 goalRevision: number;
323 goalFresh: boolean;
324 goalRefreshing: boolean;
@@ -529,8 +544,11 @@ const sessionIdOf = (providerThread: OrchestrationV2ProviderThread) => {
544 const textOf = (content: ReadonlyArray<{ readonly type: string; readonly text?: string }>) =>
545 content.flatMap((part) => (part.type === "text" && part.text ? [part.text] : [])).join("\n");
546
532 -const stringField = (record: Readonly<Record<string, unknown>> | undefined, key: string) => {
533 - const value = record?.[key];
547 +const stringField = (record: unknown, key: string) => {
548 + const value =
549 + typeof record === "object" && record !== null && key in record
550 + ? Reflect.get(record, key)
551 + : undefined;
552 return typeof value === "string" && value.trim().length > 0 ? value : undefined;
553 };
554
@@ -698,6 +716,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
716 const host = yield* ProviderHost.ProviderHost;
717 const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
718 const fileSystem = yield* FileSystem.FileSystem;
719 + const paths = yield* Path.Path;
720 const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
721 const crypto = yield* Digest.Digest;
722 const driver = OPENCODE_PROVIDER;
@@ -794,6 +813,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
813 ): ThreadState => ({
814 sessionId,
815 providerThread,
816 + imageJobs: new Map(),
817 goalRevision: 0,
818 goalFresh: false,
819 goalRefreshing: false,
@@ -870,6 +890,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
890 };
891
892 const owesWork = (state: ThreadState) =>
893 + [...state.imageJobs.values()].some((job) => job.status === "running") ||
894 state.wakes.length > 0 ||
895 state.reports.size > 0 ||
896 (state.subagent !== undefined && busy.has(state.sessionId)) ||
@@ -1161,6 +1182,31 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1182 if (projected) return;
1183 }
1184 }
1185 + if (tool.name === "experimental_image_generate" && status === "completed") {
1186 + const jobID = stringField(result?.metadata, "jobID");
1187 + const path = stringField(result?.metadata, "path");
1188 + const model = stringField(result?.metadata, "model");
1189 + if (jobID !== undefined && path !== undefined && model !== undefined) {
1190 + // Tool success acknowledges launch, not the background image's outcome.
1191 + if (
1192 + state.imageJobs.get(jobID)?.status !== undefined &&
1193 + state.imageJobs.get(jobID)?.status !== "running"
1194 + )
1195 + return;
1196 + if (!state.imageJobs.has(jobID))
1197 + state.imageJobs.set(jobID, {
1198 + turn,
1199 + toolId: id,
1200 + tool,
1201 + previewed: false,
1202 + path,
1203 + model,
1204 + status: "running",
1205 + });
1206 + status = "running";
1207 + yield* syncRosterAncestors(state);
1208 + }
1209 + }
1210 const updatedAt = yield* DateTime.now;
1211 const startedAt = turn.startedAt.get(id) ?? updatedAt;
1212 const completedAt = status === "running" ? null : updatedAt;
@@ -1425,14 +1471,28 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1471 ? {}
1472 : { childThreadId: call.child.subagent.appThread.id }),
1473 }));
1474 + const imageTasks = sessionsOf(state).flatMap((session) =>
1475 + [...session.imageJobs.entries()].flatMap(([taskId, job]) =>
1476 + job.status === "running"
1477 + ? [
1478 + {
1479 + taskId,
1480 + kind: "background_task" as const,
1481 + description: `Image generation (${job.model})`,
1482 + },
1483 + ]
1484 + : [],
1485 + ),
1486 + );
1487 + const allTasks = [...roster, ...imageTasks];
1488 const current = state.providerThread.pendingBackgroundTasks ?? [];
1489 if (
1490 state.publishedBackgroundWork === hasBackground(state) &&
1431 - current.length === roster.length &&
1491 + current.length === allTasks.length &&
1492 current.every(
1493 (task, index) =>
1434 - task.taskId === roster[index]?.taskId &&
1435 - task.description === roster[index]?.description &&
1494 + task.taskId === allTasks[index]?.taskId &&
1495 + task.description === allTasks[index]?.description &&
1496 (task.kind === "subagent" ? task.childThreadId : undefined) ===
1497 roster[index]?.childThreadId,
1498 )
@@ -1441,7 +1501,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1501 }
1502 state.providerThread = {
1503 ...state.providerThread,
1444 - pendingBackgroundTasks: roster,
1504 + pendingBackgroundTasks: allTasks,
1505 updatedAt: yield* DateTime.now,
1506 };
1507 yield* emitProviderThread(state);
@@ -1726,6 +1786,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1786 yield* emitText(state, turn, open.block, open.kind, (text) => text, true);
1787 }
1788 for (const id of turn.tools.keys()) {
1789 + if ([...state.imageJobs.values()].some((job) => job.turn === turn && job.toolId === id))
1790 + continue;
1791 yield* emitTool(
1792 state,
1793 turn,
@@ -2266,7 +2328,12 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2328 files: event.data.content.flatMap((part) => (part.type === "file" ? [part] : [])),
2329 metadata: event.data.metadata,
2330 });
2269 - turn.tools.delete(event.data.id);
2331 + if (
2332 + ![...state.imageJobs.values()].some(
2333 + (job) => job.turn === turn && job.toolId === event.data.id,
2334 + )
2335 + )
2336 + turn.tools.delete(event.data.id);
2337 return;
2338 }
2339 case "session.tool.failed": {
@@ -2389,6 +2456,85 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2456 yield* offerWake(state, wake);
2457 });
2458
2459 + const imageReport = Effect.fn("OpenCode2Adapter.imageReport")(function* (
2460 + state: ThreadState,
2461 + payload: {
2462 + readonly text: string;
2463 + readonly metadata?: Readonly<Record<string, unknown>> | undefined;
2464 + },
2465 + ) {
2466 + const jobID = stringField(payload.metadata, "jobID");
2467 + const job = jobID === undefined ? undefined : state.imageJobs.get(jobID);
2468 + const outcome = stringField(payload.metadata, "state");
2469 + if (
2470 + job === undefined ||
2471 + (outcome !== "completed" && outcome !== "error" && outcome !== "cancelled")
2472 + )
2473 + return;
2474 + // Native job inventory exposes the stem, while a completion notice names the saved file.
2475 + const saved = /Saved image: (.+) \([^\n]+\)\./.exec(payload.text)?.[1];
2476 + const previewPath =
2477 + saved !== undefined && saved.startsWith(job.path + ".") ? saved : undefined;
2478 + if (
2479 + job.status !== "running" &&
2480 + !(
2481 + job.status === "completed" &&
2482 + outcome === "completed" &&
2483 + !job.previewed &&
2484 + previewPath !== undefined
2485 + )
2486 + )
2487 + return;
2488 + const files: Array<{ uri: string; mime: string }> = [];
2489 + yield* Effect.gen(function* () {
2490 + if (outcome !== "completed" || previewPath === undefined) return;
2491 + const image = yield* (
2492 + connection.external
2493 + ? connection.readImage(previewPath, state.directory)
2494 + : Effect.gen(function* () {
2495 + const mime = OpenCode2Client.imagePreviewMime(paths.extname(previewPath));
2496 + const info = yield* fileSystem.stat(previewPath);
2497 + if (mime === undefined || info.size > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)
2498 + return yield* new OpenCode2Client.OpenCode2ImageError({
2499 + cause: "Unsupported image preview",
2500 + });
2501 + const bytes = yield* fileSystem.readFile(previewPath);
2502 + if (bytes.byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)
2503 + return yield* new OpenCode2Client.OpenCode2ImageError({
2504 + cause: "Image exceeds preview limit",
2505 + });
2506 + return {
2507 + uri: `data:${mime};base64,${Buffer.from(bytes).toString("base64")}`,
2508 + mime,
2509 + };
2510 + })
2511 + ).pipe(Effect.timeout(INVENTORY_TIMEOUT), Effect.option);
2512 + if (Option.isSome(image)) files.push(image.value);
2513 + }).pipe(
2514 + Effect.ensuring(
2515 + Effect.gen(function* () {
2516 + // A known outcome must still settle its tool if preview I/O is interrupted.
2517 + job.status = outcome;
2518 + job.previewed ||= files.length > 0;
2519 + job.turn.tools.set(job.toolId, job.tool);
2520 + yield* emitTool(
2521 + state,
2522 + job.turn,
2523 + job.toolId,
2524 + outcome === "completed"
2525 + ? "completed"
2526 + : outcome === "error"
2527 + ? "failed"
2528 + : "interrupted",
2529 + { output: payload.text, metadata: undefined, files },
2530 + );
2531 + job.turn.tools.delete(job.toolId);
2532 + yield* syncRosterAncestors(state);
2533 + }),
2534 + ),
2535 + );
2536 + });
2537 +
2538 const onReport = Effect.fnUntraced(function* (
2539 state: ThreadState,
2540 inboxId: string,
@@ -2397,6 +2543,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2543 readonly metadata?: Readonly<Record<string, unknown>> | undefined;
2544 },
2545 ) {
2546 + if (stringField(payload.metadata, "source") === "experimental_image_generate") {
2547 + return yield* imageReport(state, payload);
2548 + }
2549 const childId = stringField(payload.metadata, "childID");
2550 if (stringField(payload.metadata, "source") !== "subagent" || childId === undefined) return;
2551 strays.delete(childId);
@@ -2683,8 +2832,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2832 }
2833 for (const state of threads.values()) {
2834 for (const call of runningCalls(state)) yield* settleCall(call, "failed");
2835 + for (const [jobID, job] of state.imageJobs) {
2836 + if (job.status !== "running") continue;
2837 + yield* imageReport(state, { text: message, metadata: { jobID, state: "error" } });
2838 + }
2839 for (const wake of state.wakes.splice(0)) wake.dropped = true;
2840 state.reports.clear();
2841 + yield* syncRosterAncestors(state);
2842 }
2843 yield* setSessionStatus("error", message);
2844 yield* Queue.end(events);
@@ -2768,7 +2922,12 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2922 ),
2923 metadata: part.state.metadata,
2924 });
2771 - turn.tools.delete(part.id);
2925 + if (
2926 + ![...state.imageJobs.values()].some(
2927 + (job) => job.turn === turn && job.toolId === part.id,
2928 + )
2929 + )
2930 + turn.tools.delete(part.id);
2931 } else if (part.state.status === "error") {
2932 yield* emitTool(state, turn, part.id, "failed", {
2933 output: part.state.error.message,
@@ -2784,6 +2943,36 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2943 });
2944
2945 // Restarts retain the previous session.outcome; without an idle item, the lost execution was interrupted.
2946 + const recoverImages = Effect.fn("OpenCode2Adapter.recoverImages")(function* (
2947 + state: ThreadState,
2948 + ) {
2949 + if (![...state.imageJobs.values()].some((job) => job.status === "running")) return;
2950 + yield* OpenCode2Client.paginate(
2951 + { sessionID: Session.ID.make(state.sessionId), type: "synthetic" as const, limit: 100 },
2952 + client.message.list,
2953 + ).pipe(
2954 + Stream.runForEach((message) =>
2955 + message.type === "synthetic" &&
2956 + stringField(message.metadata, "source") === "experimental_image_generate"
2957 + ? imageReport(state, message)
2958 + : Effect.void,
2959 + ),
2960 + );
2961 + if (
2962 + ![...state.imageJobs.values()].some((job) => job.status === "running") ||
2963 + compareSemverVersions(connection.version, "2.2.6") < 0
2964 + )
2965 + return;
2966 + const jobs = yield* connection.imageJobs(state.sessionId);
2967 + for (const job of jobs) {
2968 + if (job.status === "running") continue;
2969 + yield* imageReport(state, {
2970 + text: `Image generation ${job.status}. Output path: ${job.path} (${job.model}).`,
2971 + metadata: { source: "experimental_image_generate", jobID: job.id, state: job.status },
2972 + });
2973 + }
2974 + });
2975 +
2976 const reconcile = Effect.fn("OpenCode2Adapter.reconcile")(function* () {
2977 // Read deepest sessions first; subagent turns end before their waiting parents.
2978 const running = [...threads]
@@ -2833,6 +3022,12 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3022 : { status: "interrupted" };
3023 yield* endExecution(state, turn, terminal);
3024 }
3025 + for (const state of threads.values()) {
3026 + yield* recoverImages(state).pipe(
3027 + Effect.timeout(INVENTORY_TIMEOUT),
3028 + Effect.ignore({ log: true }),
3029 + );
3030 + }
3031 for (const state of background) {
3032 for (const call of runningCalls(state)) {
3033 const child = call.child?.sessionId;