mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
feat(core): read Console artifacts (#64555)
This commit is contained in:
@@ -15,6 +15,12 @@ type Connection = NonNullable<Effect.Success<ReturnType<Context["integration"]["
|
||||
|
||||
const Prepared = Schema.Struct({ missing: Schema.Array(Schema.String) })
|
||||
const Published = Schema.Struct({ url: Schema.String })
|
||||
const Files = Schema.Struct({
|
||||
revision: Schema.Number,
|
||||
files: Schema.Array(
|
||||
Schema.Struct({ path: Schema.String, sha256: Schema.String, text: Schema.optional(Schema.String) }),
|
||||
),
|
||||
})
|
||||
const Problem = Schema.Struct({ message: Schema.optional(Schema.String), owner: Schema.optional(Schema.String) })
|
||||
|
||||
/**
|
||||
@@ -97,8 +103,27 @@ export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.
|
||||
Effect.flatMap((slug) => 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}`),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -21,11 +21,13 @@ function fakeConsole() {
|
||||
const state = {
|
||||
artifacts: true,
|
||||
watches: true,
|
||||
blobs: new Set<string>(),
|
||||
blobs: new Map<string, Uint8Array>(),
|
||||
blobReads: new Array<string>(),
|
||||
uploads: new Array<string>(),
|
||||
prepared: new Array<Record<string, unknown>>(),
|
||||
published: new Array<{ slug: string; body: Record<string, unknown> }>(),
|
||||
deleted: new Array<string>(),
|
||||
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<string, unknown>
|
||||
@@ -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* () {
|
||||
|
||||
Reference in New Issue
Block a user