From 54b4698a72b73b200501ebcb5eae8e2be3800f2a Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:38:36 -0600 Subject: [PATCH] feat(core): read Console artifacts (#64555) --- .../src/plugin/provider/opencode-artifacts.ts | 35 +++++++++++-- .../provider-opencode-artifacts.test.ts | 51 +++++++++++++++++-- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/packages/core/src/plugin/provider/opencode-artifacts.ts b/packages/core/src/plugin/provider/opencode-artifacts.ts index 96f25ef9d60..8775364f562 100644 --- a/packages/core/src/plugin/provider/opencode-artifacts.ts +++ b/packages/core/src/plugin/provider/opencode-artifacts.ts @@ -15,6 +15,12 @@ type Connection = NonNullable publish(slug, input, true)), Effect.map((published) => ({ url: published.url })), ), - // Console has no API that returns an artifact's files. - read: () => Effect.fail(new Error("OpenCode Console can't return an artifact's files yet")), + // Console inlines UTF-8 text files up to a limit; the rest download as blobs of the same revision. + read: (id) => + Effect.gen(function* () { + const slug = yield* slugOf(id) + const send = yield* sender(ctx, http, connection, slug) + const listing = yield* send("GET", `/api/artifacts/${slug}/files`).pipe( + Effect.flatMap(HttpClientResponse.schemaBodyJson(Files)), + ) + const files = yield* Effect.forEach( + listing.files, + (file) => + file.text !== undefined + ? Effect.succeed([file.path, new TextEncoder().encode(file.text)] as const) + : send("GET", `/api/artifacts/${slug}/blobs/${file.sha256}?revision=${listing.revision}`).pipe( + Effect.flatMap((response) => response.arrayBuffer), + Effect.map((bytes) => [file.path, new Uint8Array(bytes)] as const), + ), + { concurrency: 4 }, + ) + return Object.fromEntries(files) + }).pipe(Effect.scoped), delete: (id) => Effect.gen(function* () { const slug = yield* slugOf(id) @@ -151,7 +176,7 @@ const sender = Effect.fnUntraced(function* ( const credential = yield* ctx.integration.connection.resolve(connection) if (!credential || credential.type === "external") return yield* Effect.fail(new Error("OpenCode Console is not connected")) - return (method: "POST" | "PUT" | "DELETE", path: string, body?: Uint8Array | object) => { + return (method: "GET" | "POST" | "PUT" | "DELETE", path: string, body?: Uint8Array | object) => { const request = HttpClientRequest.make(method)(`${ConsoleAccess.server(credential)}${path}`).pipe( HttpClientRequest.acceptJson, HttpClientRequest.setHeaders(ConsoleAccess.headers(credential)), @@ -177,7 +202,9 @@ const sender = Effect.fnUntraced(function* ( problem?.owner !== undefined ? `${problem.owner} already shared an OpenCode Console artifact named ${slug}; give this one another title` : response.status === 404 - ? "Artifacts are off for this OpenCode Console workspace" + ? method === "GET" + ? `OpenCode Console has no artifact named ${slug}` + : "Artifacts are off for this OpenCode Console workspace" : (problem?.message ?? `OpenCode Console answered ${response.status}`), ), ), diff --git a/packages/core/test/plugin/provider-opencode-artifacts.test.ts b/packages/core/test/plugin/provider-opencode-artifacts.test.ts index 5e0b594f347..63cee258eae 100644 --- a/packages/core/test/plugin/provider-opencode-artifacts.test.ts +++ b/packages/core/test/plugin/provider-opencode-artifacts.test.ts @@ -21,11 +21,13 @@ function fakeConsole() { const state = { artifacts: true, watches: true, - blobs: new Set(), + blobs: new Map(), + blobReads: new Array(), uploads: new Array(), prepared: new Array>(), published: new Array<{ slug: string; body: Record }>(), deleted: new Array(), + gone: false, } const server = Bun.serve({ port: 0, @@ -48,9 +50,26 @@ function fakeConsole() { return Response.json({ missing: body.files.map((file) => file.sha256).filter((sha) => !state.blobs.has(sha)) }) } if (url.pathname.startsWith("/api/artifacts/blobs/")) { + const bytes = new Uint8Array(await request.arrayBuffer()) state.uploads.push(slug) - state.blobs.add(slug) - return Response.json({ sha256: slug, bytes: (await request.arrayBuffer()).byteLength }) + state.blobs.set(slug, bytes) + return Response.json({ sha256: slug, bytes: bytes.byteLength }) + } + // Lists the last published revision, inlining only index.html so other files download as blobs. + if (url.pathname.endsWith("/files")) { + if (state.gone) return new Response("Not found", { status: 404 }) + const published = state.published.at(-1)?.body as { files: { path: string; sha256: string }[] } + return Response.json({ + revision: state.published.length, + files: published.files.map((file) => ({ + ...file, + ...(file.path === "index.html" ? { text: new TextDecoder().decode(state.blobs.get(file.sha256)) } : {}), + })), + }) + } + if (url.pathname.includes("/blobs/")) { + state.blobReads.push(`${slug}?${url.searchParams}`) + return new Response(state.blobs.get(slug)) } if (url.pathname.startsWith("/api/artifacts/") && request.method === "PUT") { const body = (await request.json()) as Record @@ -162,6 +181,32 @@ describe("OpencodePlugin artifacts", () => { ), ) + it.effect("reads the files back, downloading the ones Console doesn't inline from the listed revision", () => + withConsole((console) => + Effect.gen(function* () { + const artifacts = yield* Artifact.Service + yield* start(console) + const created = yield* artifacts.create({ title: "Report", files }) + const content = yield* artifacts.get(created.id) + expect(content.files).toEqual(files) + expect(console.state.blobReads).toEqual([expect.stringMatching(/^[0-9a-f]{64}\?revision=1$/)]) + }), + ), + ) + + it.effect("says Console has no such artifact when it was deleted there", () => + withConsole((console) => + Effect.gen(function* () { + const artifacts = yield* Artifact.Service + yield* start(console) + const created = yield* artifacts.create({ title: "Report", files }) + console.state.gone = true + const error = yield* Effect.flip(artifacts.get(created.id)) + expect(String(error)).toContain(`OpenCode Console has no artifact named ${console.state.published[0]?.slug}`) + }), + ), + ) + it.effect("gives two artifacts with one title their own Console artifacts", () => withConsole((console) => Effect.gen(function* () {