Skip to content

Commit 3aa7b89

Browse files
committed
feat(opencode): track native image job outcomes and previews
1 parent 5f2bb93 commit 3aa7b89

5 files changed

Lines changed: 531 additions & 10 deletions

File tree

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

Lines changed: 210 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ const colorForm = {
317317
const resumed = (
318318
entries: ReadonlyArray<ProviderReplayEntry>,
319319
options?: {
320-
readonly version?: "2.2.5";
320+
readonly version?: "2.2.5" | "2.2.6";
321321
readonly goal?: OrchestrationV2ProviderThread["goal"];
322322
readonly external?: boolean;
323323
readonly supervised?: boolean;
@@ -335,7 +335,7 @@ const resumed = (
335335
out("session.get", { sessionID: SESSION }),
336336
replyData("session.get", sessionInfo()),
337337
...noOpenRequests,
338-
...(options?.version === "2.2.5"
338+
...(options?.version !== undefined
339339
? [out("rpc.call", "<any>"), reply("rpc.call", { output: null })]
340340
: []),
341341
...withInstructions([
@@ -2891,6 +2891,214 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28912891
}).pipe(Effect.scoped),
28922892
);
28932893

2894+
it.effect.each(["completed", "error", "cancelled"] as const)(
2895+
"retains native image job %s after its launch returns",
2896+
(outcome) =>
2897+
Effect.gen(function* () {
2898+
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-image" };
2899+
const notice = (sessionID: string) =>
2900+
event("session.inbox.enqueued", {
2901+
sessionID,
2902+
inboxID: `msg_image_${sessionID}`,
2903+
item: {
2904+
type: "synthetic",
2905+
delivery: "steer",
2906+
payload: {
2907+
description: "Image generation",
2908+
text:
2909+
outcome === "completed"
2910+
? "Image outcome retained. Saved image: /work/opencode2/image.png (test/image)."
2911+
: "Image outcome retained",
2912+
metadata: {
2913+
source: "experimental_image_generate",
2914+
jobID: "image_test",
2915+
state: outcome,
2916+
path: "/work/opencode2/image",
2917+
},
2918+
},
2919+
},
2920+
});
2921+
const { runtime, thread } = yield* resumed(
2922+
[
2923+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2924+
promptAccepted,
2925+
event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
2926+
event("session.tool.called", { ...tool, input: { prompt: "A tree" }, executed: false }),
2927+
event(
2928+
"session.tool.success",
2929+
{
2930+
...tool,
2931+
content: [{ type: "text", text: "Job launched" }],
2932+
metadata: {
2933+
jobID: "image_test",
2934+
path: "/work/opencode2/image",
2935+
model: "test/image",
2936+
},
2937+
executed: false,
2938+
},
2939+
undefined,
2940+
2,
2941+
),
2942+
event("session.execution.succeeded", { sessionID: SESSION }),
2943+
notice("ses_other"),
2944+
notice(SESSION),
2945+
...(outcome === "completed"
2946+
? [
2947+
out("image.read", {
2948+
path: "/api/fs/read/image.png",
2949+
"location[directory]": WORK,
2950+
}),
2951+
reply("image.read", { imageBase64: "iVBORw0KGgo=" }),
2952+
]
2953+
: []),
2954+
],
2955+
{ external: true },
2956+
);
2957+
let launched = false;
2958+
const events = yield* runtime.events.pipe(
2959+
Stream.takeUntil((e) => {
2960+
if (e.type !== "provider_thread.updated") return false;
2961+
if (
2962+
e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_test")
2963+
)
2964+
launched = true;
2965+
return launched && e.pendingBackgroundWork === false;
2966+
}),
2967+
Stream.runCollect,
2968+
Effect.forkScoped,
2969+
);
2970+
yield* runtime.startTurn(turnInput(thread));
2971+
const collected = yield* Fiber.join(events);
2972+
const items = collected.flatMap((e) =>
2973+
e.type === "turn_item.updated" && e.turnItem.nativeItemRef?.nativeId === "call-image"
2974+
? [e.turnItem]
2975+
: [],
2976+
);
2977+
assert.deepEqual(
2978+
items.map((item) => item.status),
2979+
[
2980+
"running",
2981+
"running",
2982+
"running",
2983+
outcome === "error" ? "failed" : outcome === "cancelled" ? "interrupted" : "completed",
2984+
],
2985+
);
2986+
assert.include(JSON.stringify(items.at(-1)), "Image outcome retained");
2987+
if (outcome === "completed") {
2988+
const item = items.at(-1);
2989+
assert.deepEqual(item?.type === "dynamic_tool" ? toolOutputImages(item.output) : [], [
2990+
{ mimeType: "image/png", data: "iVBORw0KGgo=" },
2991+
]);
2992+
}
2993+
const rosters = collected.flatMap((e) =>
2994+
e.type === "provider_thread.updated"
2995+
? [e.providerThread.pendingBackgroundTasks ?? []]
2996+
: [],
2997+
);
2998+
assert.isTrue(rosters.some((tasks) => tasks.some((task) => task.taskId === "image_test")));
2999+
assert.deepEqual(rosters.at(-1), []);
3000+
}).pipe(Effect.scoped),
3001+
);
3002+
3003+
it.effect.each(["history", "list"] as const)(
3004+
"recovers native image job outcomes from %s after reconnect",
3005+
(source) =>
3006+
Effect.gen(function* () {
3007+
const { runtime, thread } = yield* resumed(
3008+
[
3009+
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3010+
promptAccepted,
3011+
...(() => {
3012+
const frames: Array<ProviderReplayEntry> = reconnected({}, "succeeded", {
3013+
content: [
3014+
{
3015+
type: "tool",
3016+
id: "call-image-gap",
3017+
name: "experimental_image_generate",
3018+
executed: false,
3019+
state: {
3020+
status: "completed",
3021+
input: { prompt: "A tree" },
3022+
content: [{ type: "text", text: "Job launched" }],
3023+
metadata: { jobID: "image_gap", path: "/tmp/image", model: "test/image" },
3024+
},
3025+
time: { created: 2, ran: 2, completed: 2 },
3026+
},
3027+
],
3028+
});
3029+
if (source === "list")
3030+
frames.splice(2, 0, out("rpc.call", "<any>"), reply("rpc.call", { output: null }));
3031+
return frames;
3032+
})(),
3033+
out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
3034+
reply("message.list", {
3035+
data:
3036+
source === "list"
3037+
? []
3038+
: [
3039+
{
3040+
id: "msg_image_gap",
3041+
time: { created: 3 },
3042+
type: "synthetic",
3043+
text: "Recovered image failure",
3044+
description: "Image generation",
3045+
metadata: {
3046+
source: "experimental_image_generate",
3047+
jobID: "image_gap",
3048+
state: "error",
3049+
path: "/tmp/image",
3050+
},
3051+
},
3052+
],
3053+
cursor: {},
3054+
}),
3055+
...(source === "list"
3056+
? [
3057+
out("imageJobs", { sessionID: SESSION, kind: "image" }),
3058+
reply("imageJobs", {
3059+
data: [
3060+
{
3061+
kind: "image",
3062+
id: "image_gap",
3063+
sessionID: SESSION,
3064+
status: "error",
3065+
path: "/tmp/image",
3066+
model: "test/image",
3067+
time: { started: 2, completed: 3 },
3068+
},
3069+
],
3070+
}),
3071+
]
3072+
: []),
3073+
],
3074+
source === "list" ? { version: "2.2.6" } : undefined,
3075+
);
3076+
const events = yield* runtime.events.pipe(
3077+
Stream.takeUntil(
3078+
(e) =>
3079+
e.type === "turn_item.updated" &&
3080+
e.turnItem.nativeItemRef?.nativeId === "call-image-gap" &&
3081+
e.turnItem.status === "failed",
3082+
),
3083+
Stream.runCollect,
3084+
Effect.forkScoped,
3085+
);
3086+
yield* runtime.startTurn(turnInput(thread));
3087+
const collected = yield* Fiber.join(events);
3088+
assert.include(
3089+
JSON.stringify(collected.at(-1)),
3090+
source === "list" ? "Image generation error" : "Recovered image failure",
3091+
);
3092+
assert.isTrue(
3093+
collected.some(
3094+
(e) =>
3095+
e.type === "provider_thread.updated" &&
3096+
e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_gap"),
3097+
),
3098+
);
3099+
}).pipe(Effect.scoped),
3100+
);
3101+
28943102
it.effect("projects successful native todo updates into one checklist", () =>
28953103
Effect.gen(function* () {
28963104
const updates = [

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ const operationOf = (
5454
query: Record<string, unknown>,
5555
body: unknown,
5656
) => {
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 } };
5761
const rpc = /^\/api\/rpc\/([^/]+)\/([^/]+)$/.exec(path);
5862
if (method === "POST" && rpc !== null)
5963
return {
@@ -176,6 +180,11 @@ const replayHttpClient = (
176180
}
177181
// Recorded responses are the raw HTTP bodies; `null` is an empty 204.
178182
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+
}
179188
if (body === "<hang>") return new Promise<Response>(() => {});
180189
if (typeof body === "object" && body !== null && "status" in body && "body" in body) {
181190
return new Response(encodeJson(body.body), {

‎packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Layer from "effect/Layer";
88
import * as Redacted from "effect/Redacted";
99
import * as Schema from "effect/Schema";
1010
import * as TestClock from "effect/testing/TestClock";
11+
import * as UrlParams from "effect/http/UrlParams";
1112
import * as HttpClient from "effect/http/HttpClient";
1213
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1314
import { describe } from "vite-plus/test";
@@ -167,3 +168,67 @@ describe("OpenCode2Client message history", () => {
167168
}),
168169
);
169170
});
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("reads scoped image jobs without accepting other sessions or job kinds", () =>
202+
Effect.gen(function* () {
203+
const job = {
204+
kind: "image",
205+
id: "image_test",
206+
sessionID: "ses_owner",
207+
status: "running",
208+
path: "/work/image",
209+
model: "test/image",
210+
} as const;
211+
const transport = HttpClient.make((request) =>
212+
Effect.sync(() => {
213+
assert.strictEqual(request.headers.authorization, utf8Basic("secret"));
214+
return HttpClientResponse.fromWeb(
215+
request,
216+
Response.json({
217+
data: [
218+
job,
219+
{ ...job, sessionID: "ses_other" },
220+
{ ...job, kind: "shell" },
221+
{ ...job, status: "future" },
222+
],
223+
}),
224+
);
225+
}),
226+
);
227+
const service = yield* OpenCode2Client.make.pipe(
228+
Effect.provideService(HttpClient.HttpClient, transport),
229+
);
230+
const api = yield* service.connect({ baseUrl: "http://remote.test", password: "secret" });
231+
assert.deepEqual(yield* api.imageJobs("ses_owner"), [job]);
232+
}),
233+
);
234+
});

0 commit comments

Comments
 (0)