feat(core): expose workspace adaptors to plugins (#21927)

This commit is contained in:
James Long
2026-04-13 13:33:13 -04:00
committed by GitHub
parent b8801dbd22
commit bf50d1c028
17 changed files with 1745 additions and 43 deletions

View File

@@ -125,6 +125,9 @@ test("remaps fallback oauth model urls to the enterprise host", async () => {
project: {} as never,
directory: "",
worktree: "",
experimental_workspace: {
register() {},
},
serverUrl: new URL("https://example.com"),
$: {} as never,
})

View File

@@ -0,0 +1,99 @@
import { afterAll, afterEach, describe, expect, test } from "bun:test"
import path from "path"
import { pathToFileURL } from "url"
import { tmpdir } from "../fixture/fixture"
const disableDefault = process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS
process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = "1"
const { Plugin } = await import("../../src/plugin/index")
const { Workspace } = await import("../../src/control-plane/workspace")
const { Instance } = await import("../../src/project/instance")
afterEach(async () => {
await Instance.disposeAll()
})
afterAll(() => {
if (disableDefault === undefined) {
delete process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS
return
}
process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = disableDefault
})
describe("plugin.workspace", () => {
test("plugin can install a workspace adaptor", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
const type = `plug-${Math.random().toString(36).slice(2)}`
const file = path.join(dir, "plugin.ts")
const mark = path.join(dir, "created.json")
const space = path.join(dir, "space")
await Bun.write(
file,
[
"export default async ({ experimental_workspace }) => {",
` experimental_workspace.register(${JSON.stringify(type)}, {`,
' name: "plug",',
' description: "plugin workspace adaptor",',
" configure(input) {",
` return { ...input, name: \"plug\", branch: \"plug/main\", directory: ${JSON.stringify(space)} }`,
" },",
" async create(input) {",
` await Bun.write(${JSON.stringify(mark)}, JSON.stringify(input))`,
" },",
" async remove() {},",
" target(input) {",
' return { type: "local", directory: input.directory }',
" },",
" })",
" return {}",
"}",
"",
].join("\n"),
)
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify(
{
$schema: "https://opencode.ai/config.json",
plugin: [pathToFileURL(file).href],
},
null,
2,
),
)
return { mark, space, type }
},
})
const info = await Instance.provide({
directory: tmp.path,
fn: async () => {
await Plugin.init()
return Workspace.create({
type: tmp.extra.type,
branch: null,
extra: { key: "value" },
projectID: Instance.project.id,
})
},
})
expect(info.type).toBe(tmp.extra.type)
expect(info.name).toBe("plug")
expect(info.branch).toBe("plug/main")
expect(info.directory).toBe(tmp.extra.space)
expect(info.extra).toEqual({ key: "value" })
expect(JSON.parse(await Bun.file(tmp.extra.mark).text())).toMatchObject({
type: tmp.extra.type,
name: "plug",
branch: "plug/main",
directory: tmp.extra.space,
extra: { key: "value" },
})
})
})