Skip to content

Commit 316f407

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-26119
2 parents 87ae413 + b883156 commit 316f407

8 files changed

Lines changed: 856 additions & 23 deletions

File tree

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

Lines changed: 420 additions & 2 deletions
Large diffs are not rendered by default.

‎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), {

‎docs/operations/scenarios.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,19 @@ cd scripts && pnpm exec e2e run scenarios/replay-turn-streams.e2e.ts --headed
7070

7171
e2e starts the dev stack through [`start-app.ts`](../../scripts/scenarios/start-app.ts) on a free
7272
port, runs the tests, and stops it. Results land in `scripts/.e2e`: `report.json`, `summary.md`,
73-
and per test a `trace.md`, its screenshots, and a video. The first run downloads e2e's browser.
73+
and per test a `trace.md`, its screenshots, and a video. `vp run scenarios --target web` runs one
74+
target; flags go straight after the script name, without `--`.
75+
76+
The first run downloads e2e's browser (about 120 MB). To fetch it ahead of time, such as in a
77+
machine image, run this from the repository root after `vp i`:
78+
79+
```sh
80+
node scripts/node_modules/playwright-core/cli.js install --only-shell chromium ffmpeg
81+
```
82+
83+
e2e opens the web app at `127.0.0.1`, and the start script tells the web dev server to bind that
84+
address. It does not use `localhost`, which some machines resolve to `::1` first.
85+
7486
`e2e run --help` lists the rest: `--grep`, `--watch`, `--last-failed`, `--changed-since`.
7587

7688
Run one at a time per checkout: a run recreates the checkout's scenario T3 home and its `.e2e`

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

Lines changed: 81 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,83 @@ 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("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‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as Path from "effect/Path";
2+
import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
13
import { OpenCode, type OpenCodeClient } from "@opencode/client/effect";
24
import { OpenCodeEvent } from "@opencode/protocol/groups/event";
35
import * as Context from "effect/Context";
@@ -50,9 +52,43 @@ export type OpenCode2StreamEvent =
5052
| OpenCode2UnreadableTerminal
5153
| OpenCode2UnreadableStart;
5254

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+
5382
/** events resolves on subscription acceptance; subscribe before work because the stream is volatile. */
5483
export interface OpenCode2Api {
5584
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>;
5692
readonly events: Effect.Effect<
5793
Stream.Stream<
5894
OpenCode2StreamEvent,
@@ -202,6 +238,7 @@ const fillCompactionRecents = HttpClient.transformResponse(
202238
/** @public */
203239
export const make = Effect.gen(function* () {
204240
const httpClient = yield* HttpClient.HttpClient;
241+
const paths = yield* Path.Path.pipe(Effect.provide(Path.layer));
205242
return OpenCode2Client.of({
206243
connect: Effect.fn("OpenCode2Client.connect")(function ({ baseUrl, password }) {
207244
const authenticated = HttpClient.mapRequest(
@@ -212,6 +249,51 @@ export const make = Effect.gen(function* () {
212249
Effect.provideService(HttpClient.HttpClient, fillCompactionRecents(authenticated)),
213250
Effect.map((client) => ({
214251
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+
),
215297
events: readEvents.pipe(
216298
Effect.provideService(
217299
HttpClient.HttpClient,

0 commit comments

Comments
 (0)