mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
feat(opencode): publish artifacts to OpenCode Console through a console artifact provider (#63929)
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
export * as OpencodeArtifacts from "./opencode-artifacts.js"
|
||||
|
||||
import { createHash } from "node:crypto"
|
||||
import type { Context } from "@opencode/plugin/effect/plugin"
|
||||
import { Effect, Option, Schema } from "effect"
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import { ConsoleAccess } from "../../console-access.js"
|
||||
import { IntegrationConnection } from "../../integration/connection.js"
|
||||
|
||||
/** The `GET /api/v2/config` response header Console sends while the caller may share artifacts. */
|
||||
export const header = "x-opencode-artifacts"
|
||||
const providerID = "console"
|
||||
|
||||
type Connection = NonNullable<Effect.Success<ReturnType<Context["integration"]["connection"]["active"]>>>
|
||||
|
||||
const Prepared = Schema.Struct({ missing: Schema.Array(Schema.String) })
|
||||
const Published = Schema.Struct({ url: Schema.String })
|
||||
const Problem = Schema.Struct({ message: Schema.optional(Schema.String), owner: Schema.optional(Schema.String) })
|
||||
|
||||
/**
|
||||
* The `console` artifact provider for one Location, the default while `sync` is given the Console connection that may
|
||||
* share. While event watches run (`wakes`), publishing makes the session watch the artifact's comments.
|
||||
*/
|
||||
export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.HttpClient) {
|
||||
let current: { readonly connection: Connection; readonly wakes: boolean } | undefined
|
||||
// Console names an artifact by slug; it comes from the title once, so a retitled update stays the same artifact.
|
||||
const slugKey = (id: string) => `artifact-slug:${id}`
|
||||
|
||||
yield* ctx.experimental.artifact.transform((editor) => {
|
||||
if (!current) return
|
||||
const connection = current.connection
|
||||
const wakes = current.wakes
|
||||
// Uploads only the files Console doesn't store yet, then publishes them as the artifact `slug`.
|
||||
const publish = Effect.fnUntraced(function* (
|
||||
slug: string,
|
||||
input: {
|
||||
readonly title: string
|
||||
readonly files: Readonly<Record<string, Uint8Array>>
|
||||
readonly sessionID?: string
|
||||
},
|
||||
replace: boolean,
|
||||
) {
|
||||
const send = yield* sender(ctx, http, connection, slug)
|
||||
const files = Object.entries(input.files).map(([path, bytes]) => ({
|
||||
path,
|
||||
bytes,
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
}))
|
||||
const prepared = yield* send("POST", "/api/artifacts/prepare", {
|
||||
slug,
|
||||
files: files.map((file) => ({ path: file.path, sha256: file.sha256, bytes: file.bytes.length })),
|
||||
...(replace ? { replace } : {}),
|
||||
}).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Prepared)))
|
||||
const missing = new Set(prepared.missing)
|
||||
// Files with the same bytes upload once.
|
||||
yield* Effect.forEach(
|
||||
new Map(files.filter((file) => missing.has(file.sha256)).map((file) => [file.sha256, file.bytes])),
|
||||
([sha256, bytes]) => send("PUT", `/api/artifacts/blobs/${sha256}`, bytes),
|
||||
{ concurrency: 4, discard: true },
|
||||
)
|
||||
return yield* send("PUT", `/api/artifacts/${slug}`, {
|
||||
title: input.title.slice(0, 200).trim(),
|
||||
files: files.map((file) => ({ path: file.path, sha256: file.sha256 })),
|
||||
...(replace ? { replace } : {}),
|
||||
...(wakes && input.sessionID ? { sessionID: input.sessionID } : {}),
|
||||
}).pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(Published)))
|
||||
}, Effect.scoped)
|
||||
const slugOf = (id: string) =>
|
||||
ctx.storage
|
||||
.get(slugKey(id))
|
||||
.pipe(
|
||||
Effect.flatMap((slug) =>
|
||||
typeof slug === "string"
|
||||
? Effect.succeed(slug)
|
||||
: Effect.fail(new Error(`No OpenCode Console artifact for ${id}`)),
|
||||
),
|
||||
)
|
||||
|
||||
editor.add({
|
||||
id: providerID,
|
||||
name: "OpenCode Console",
|
||||
capabilities: ["static"],
|
||||
prompt:
|
||||
"Artifacts are private pages the workspace's members open and comment on inside OpenCode Console. Each needs an index.html." +
|
||||
(wakes ? " Comments wake this session." : ""),
|
||||
create: (input) =>
|
||||
Effect.gen(function* () {
|
||||
// A suffix from the ID, so two artifacts with one title never publish over each other.
|
||||
const slug = `${slugify(input.title)?.slice(0, 47).replace(/-+$/, "") ?? "artifact"}-${input.id.slice(-6).toLowerCase()}`
|
||||
const published = yield* publish(slug, input, false)
|
||||
yield* ctx.storage.set(slugKey(input.id), slug)
|
||||
return { url: published.url }
|
||||
}),
|
||||
// It's the same artifact, so it shares over the name whoever shared it last.
|
||||
update: (id, input) =>
|
||||
slugOf(id).pipe(
|
||||
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")),
|
||||
delete: (id) =>
|
||||
Effect.gen(function* () {
|
||||
const slug = yield* slugOf(id)
|
||||
const send = yield* sender(ctx, http, connection, slug)
|
||||
yield* send("DELETE", `/api/artifacts/${slug}`)
|
||||
yield* ctx.storage.remove(slugKey(id))
|
||||
}).pipe(Effect.scoped),
|
||||
})
|
||||
editor.default.set(providerID)
|
||||
})
|
||||
|
||||
/** Offers the provider through `connection`, or removes it when it is undefined. */
|
||||
const sync = Effect.fnUntraced(function* (connection: Connection | undefined, wakes: boolean) {
|
||||
const next = connection && { connection, wakes }
|
||||
if (
|
||||
IntegrationConnection.key(next?.connection) === IntegrationConnection.key(current?.connection) &&
|
||||
next?.wakes === current?.wakes
|
||||
)
|
||||
return
|
||||
current = next
|
||||
yield* ctx.experimental.artifact.reload()
|
||||
})
|
||||
|
||||
return { sync }
|
||||
})
|
||||
|
||||
/** Console's slug rules: lowercase letters, digits and single dashes, at most 54 characters. */
|
||||
function slugify(name: string) {
|
||||
return (
|
||||
name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-]+/g, "-")
|
||||
.replace(/-+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 54)
|
||||
.replace(/-+$/, "") || undefined
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends Console requests as the connection's member; a refusal fails in plain words about the artifact `slug`. Deleting
|
||||
* one Console no longer has, such as one deleted in Console, succeeds.
|
||||
*/
|
||||
const sender = Effect.fnUntraced(function* (
|
||||
ctx: Context,
|
||||
http: HttpClient.HttpClient,
|
||||
connection: Connection,
|
||||
slug: string,
|
||||
) {
|
||||
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) => {
|
||||
const request = HttpClientRequest.make(method)(`${ConsoleAccess.server(credential)}${path}`).pipe(
|
||||
HttpClientRequest.acceptJson,
|
||||
HttpClientRequest.setHeaders(ConsoleAccess.headers(credential)),
|
||||
)
|
||||
return HttpClient.withScope(http)
|
||||
.execute(
|
||||
body instanceof Uint8Array
|
||||
? HttpClientRequest.bodyUint8Array(request, body, "application/octet-stream")
|
||||
: body === undefined
|
||||
? request
|
||||
: HttpClientRequest.bodyJsonUnsafe(request, body),
|
||||
)
|
||||
.pipe(
|
||||
Effect.flatMap((response) =>
|
||||
response.status < 300 || (method === "DELETE" && response.status === 404)
|
||||
? Effect.succeed(response)
|
||||
: HttpClientResponse.schemaBodyJson(Problem)(response).pipe(
|
||||
Effect.option,
|
||||
Effect.map(Option.getOrUndefined),
|
||||
Effect.flatMap((problem) =>
|
||||
Effect.fail(
|
||||
new Error(
|
||||
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"
|
||||
: (problem?.message ?? `OpenCode Console answered ${response.status}`),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -30,6 +30,7 @@ import { SharedCache } from "../../shared-cache.js"
|
||||
import { Skill } from "../../skill.js"
|
||||
import { SkillDiscovery } from "../../skill/discovery.js"
|
||||
import { WebSearch } from "../../websearch.js"
|
||||
import { OpencodeArtifacts } from "./opencode-artifacts.js"
|
||||
import { OpencodeWatches } from "./opencode-watches.js"
|
||||
import { ProviderRefresh } from "./refresh.js"
|
||||
|
||||
@@ -220,6 +221,7 @@ export const OpencodePlugin = define<
|
||||
let reported: IntegrationConnection.Status | undefined
|
||||
let explainers: Effect.Success<ReturnType<typeof ctx.session.hook>>[] = []
|
||||
const watches = yield* OpencodeWatches.make(ctx, http)
|
||||
const artifacts = yield* OpencodeArtifacts.make(ctx, http)
|
||||
|
||||
// Every project signed in to the same workspace shares one fetch, reused while it is younger than `maxAge`.
|
||||
const load = Effect.fn("OpencodePlugin.load")(function* (maxAge: Duration.Input = Duration.minutes(1)) {
|
||||
@@ -338,6 +340,7 @@ export const OpencodePlugin = define<
|
||||
yield* syncInstructionsAndSkills(next, allowed)
|
||||
const watching = allowed && next.remote?.watches === true && (yield* flags.enabled("console_watches"))
|
||||
yield* syncWatches(watching ? next.connection : undefined)
|
||||
yield* artifacts.sync(allowed && next.remote?.artifacts === true ? next.connection : undefined, watching)
|
||||
})
|
||||
// Watching starts when the console_watches flag is on and the Console's config says the workspace has event watches,
|
||||
// and stops when either no longer holds.
|
||||
@@ -607,8 +610,9 @@ function fetchConfig(http: HttpClient.HttpClient, value: Credential.Key | Creden
|
||||
}
|
||||
const watches = response.headers[OpencodeWatches.header] !== undefined
|
||||
const lists = response.headers[listsHeader] !== undefined
|
||||
const artifacts = response.headers[OpencodeArtifacts.header] !== undefined
|
||||
const body = yield* HttpClientResponse.filterStatusOk(response).pipe(Effect.flatMap((ok) => ok.json))
|
||||
return { ...(yield* Schema.decodeUnknownEffect(ConfigIntegration.Remote)(body)), watches, lists }
|
||||
return { ...(yield* Schema.decodeUnknownEffect(ConfigIntegration.Remote)(body)), watches, lists, artifacts }
|
||||
}),
|
||||
),
|
||||
Effect.scoped,
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { Artifact } from "@opencode/core/artifact"
|
||||
import { Credential } from "@opencode/core/credential"
|
||||
import { Flag } from "@opencode/core/flag"
|
||||
import { Integration } from "@opencode/core/integration"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
import { PluginHost } from "@opencode/core/plugin/host"
|
||||
import { OpencodePlugin } from "@opencode/core/plugin/provider/opencode"
|
||||
import { OpencodeArtifacts } from "@opencode/core/plugin/provider/opencode-artifacts"
|
||||
import { OpencodeWatches } from "@opencode/core/plugin/provider/opencode-watches"
|
||||
import { eventually } from "../lib/eventually"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { PluginTestLayer } from "./fixture"
|
||||
|
||||
const it = testEffect(PluginTestLayer.pipe(Layer.provide(Layer.succeed(Flag.Overrides, "artifacts,console_watches"))))
|
||||
|
||||
/** Console's artifact routes, in-process: it stores blobs by digest and answers `prepare` with the ones it lacks. */
|
||||
function fakeConsole() {
|
||||
const state = {
|
||||
artifacts: true,
|
||||
watches: true,
|
||||
blobs: new Set<string>(),
|
||||
uploads: new Array<string>(),
|
||||
prepared: new Array<Record<string, unknown>>(),
|
||||
published: new Array<{ slug: string; body: Record<string, unknown> }>(),
|
||||
deleted: new Array<string>(),
|
||||
}
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: async (request) => {
|
||||
const url = new URL(request.url)
|
||||
const slug = url.pathname.split("/").at(-1) ?? ""
|
||||
if (url.pathname === "/api/v2/config")
|
||||
return Response.json(
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
...(state.artifacts ? { [OpencodeArtifacts.header]: "1" } : {}),
|
||||
...(state.watches ? { [OpencodeWatches.header]: "1" } : {}),
|
||||
},
|
||||
},
|
||||
)
|
||||
if (url.pathname === "/api/artifacts/prepare") {
|
||||
const body = (await request.json()) as { files: { sha256: string }[] }
|
||||
state.prepared.push(body)
|
||||
return Response.json({ missing: body.files.map((file) => file.sha256).filter((sha) => !state.blobs.has(sha)) })
|
||||
}
|
||||
if (url.pathname.startsWith("/api/artifacts/blobs/")) {
|
||||
state.uploads.push(slug)
|
||||
state.blobs.add(slug)
|
||||
return Response.json({ sha256: slug, bytes: (await request.arrayBuffer()).byteLength })
|
||||
}
|
||||
if (url.pathname.startsWith("/api/artifacts/") && request.method === "PUT") {
|
||||
const body = (await request.json()) as Record<string, unknown>
|
||||
state.published.push({ slug, body })
|
||||
return Response.json({ url: `https://opencode.ai/console/org_1/artifacts/${slug}`, watching: false })
|
||||
}
|
||||
if (url.pathname.startsWith("/api/artifacts/") && request.method === "DELETE") {
|
||||
state.deleted.push(slug)
|
||||
return new Response(null, { status: 204 })
|
||||
}
|
||||
return new Response("Not found", { status: 404 })
|
||||
},
|
||||
})
|
||||
return { server, state }
|
||||
}
|
||||
|
||||
const withConsole = <A, E, R>(use: (console: ReturnType<typeof fakeConsole>) => Effect.Effect<A, E, R>) =>
|
||||
Effect.acquireUseRelease(Effect.sync(fakeConsole), use, (console) => Effect.promise(() => console.server.stop(true)))
|
||||
|
||||
/** Signs in to the fake Console and starts the plugin. */
|
||||
const start = Effect.fnUntraced(function* (console: ReturnType<typeof fakeConsole>) {
|
||||
const credentials = yield* Credential.Service
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({ type: "key", key: "secret", metadata: { server: console.server.url.origin } }),
|
||||
})
|
||||
const host = yield* PluginHost.make(yield* Plugin.Service, "test")
|
||||
yield* OpencodePlugin.effect(host)
|
||||
return host
|
||||
})
|
||||
|
||||
const defaultProvider = Effect.gen(function* () {
|
||||
const artifacts = yield* Artifact.Service
|
||||
return (yield* artifacts.providers()).find((provider) => provider.default)?.id
|
||||
})
|
||||
|
||||
const files = { "index.html": "<h1>Hi</h1>", "app.js": "console.log(1)" }
|
||||
|
||||
describe("OpencodePlugin artifacts", () => {
|
||||
it.effect("adds no Console provider while the Console config doesn't advertise artifacts", () =>
|
||||
withConsole((console) =>
|
||||
Effect.gen(function* () {
|
||||
console.state.artifacts = false
|
||||
yield* start(console)
|
||||
expect(yield* defaultProvider).toBe(Artifact.LocalProviderID)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("makes Console the default provider while advertised, and removes it when the header goes", () =>
|
||||
withConsole((console) =>
|
||||
Effect.gen(function* () {
|
||||
const artifacts = yield* Artifact.Service
|
||||
yield* start(console)
|
||||
expect(yield* artifacts.providers()).toContainEqual(
|
||||
Artifact.Provider.make({
|
||||
id: Artifact.ProviderID.make("console"),
|
||||
name: "OpenCode Console",
|
||||
capabilities: ["static"],
|
||||
default: true,
|
||||
}),
|
||||
)
|
||||
expect((yield* artifacts.prompts()).defaultProvider.prompt).toContain("Comments wake this session.")
|
||||
|
||||
console.state.artifacts = false
|
||||
yield* TestClock.adjust("1 minute")
|
||||
const providers = yield* eventually(artifacts.providers(), (list) => list.length === 1)
|
||||
expect(providers.map((provider) => [provider.id, provider.default])).toEqual([[Artifact.LocalProviderID, true]])
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect(
|
||||
"publishes with the session and returns Console's URL; an update keeps the slug and uploads only new files",
|
||||
() =>
|
||||
withConsole((console) =>
|
||||
Effect.gen(function* () {
|
||||
const artifacts = yield* Artifact.Service
|
||||
const session = yield* (yield* start(console)).session.create({})
|
||||
const created = yield* artifacts.create({ title: "Q3 Report!", files, sessionID: session.id })
|
||||
const slug = console.state.published[0]?.slug ?? ""
|
||||
expect(slug).toMatch(/^q3-report-[a-z0-9]{6}$/)
|
||||
expect(created).toMatchObject({
|
||||
provider: "console",
|
||||
url: `https://opencode.ai/console/org_1/artifacts/${slug}`,
|
||||
})
|
||||
expect(console.state.uploads).toHaveLength(2)
|
||||
expect(console.state.prepared[0]).not.toHaveProperty("replace")
|
||||
expect(console.state.published[0]).toMatchObject({
|
||||
slug,
|
||||
body: { title: "Q3 Report!", sessionID: session.id },
|
||||
})
|
||||
|
||||
const updated = yield* artifacts.update(created.id, {
|
||||
title: "Q4 Report",
|
||||
files: { ...files, "index.html": "<h1>Bye</h1>" },
|
||||
})
|
||||
expect(updated.url).toBe(`https://opencode.ai/console/org_1/artifacts/${slug}`)
|
||||
expect(console.state.uploads).toHaveLength(3)
|
||||
expect(console.state.prepared[1]).toMatchObject({ slug, replace: true })
|
||||
expect(console.state.published[1]).toMatchObject({
|
||||
slug,
|
||||
body: { title: "Q4 Report", replace: true },
|
||||
})
|
||||
|
||||
yield* artifacts.remove(created.id)
|
||||
expect(console.state.deleted).toEqual([slug])
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("gives two artifacts with one title their own Console artifacts", () =>
|
||||
withConsole((console) =>
|
||||
Effect.gen(function* () {
|
||||
const artifacts = yield* Artifact.Service
|
||||
yield* start(console)
|
||||
yield* artifacts.create({ title: "Report", files })
|
||||
yield* artifacts.create({ title: "Report", files })
|
||||
const slugs = console.state.published.map((item) => item.slug)
|
||||
expect(slugs[0]).toMatch(/^report-[a-z0-9]{6}$/)
|
||||
expect(new Set(slugs).size).toBe(2)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("without event watches, publishes without the session and promises no wake", () =>
|
||||
withConsole((console) =>
|
||||
Effect.gen(function* () {
|
||||
console.state.watches = false
|
||||
const artifacts = yield* Artifact.Service
|
||||
const session = yield* (yield* start(console)).session.create({})
|
||||
expect((yield* artifacts.prompts()).defaultProvider.prompt).not.toContain("wake")
|
||||
yield* artifacts.create({ title: "Demo", files, sessionID: session.id })
|
||||
expect(console.state.published[0]?.body).not.toHaveProperty("sessionID")
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user