mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(workspace): route workspace plugin through manager proxy with slug-based project resolution
This commit is contained in:
1 parent
5edeff326c
commit
681bf8cda2
6 files changed
+127
-37
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { Plugin, PluginInput, WorkspaceAdaptor, WorkspaceInfo } from "@opencode-ai/plugin"
|
||||
import type { Plugin, WorkspaceAdaptor, WorkspaceInfo } from "@opencode-ai/plugin"
|
||||
import { ManagerClient } from "./client.js"
|
||||
import { pluginLogger } from "./logger.js"
|
||||
import { PluginOptionsSchema, type ManagerProject } from "./types.js"
|
||||
@@ -37,33 +37,19 @@ export function makeAdaptor(cfg: ResolvedConfig, project: ManagerProject): Works
|
||||
pluginLogger.info(`adaptor.remove slug=${project.slug} workspaceID=${info.id}`)
|
||||
},
|
||||
target(info) {
|
||||
pluginLogger.info(`adaptor.target slug=${project.slug} workspaceID=${info.id} dir=${project.directory}`)
|
||||
return { type: "local" as const, directory: project.directory }
|
||||
const base = cfg.url.replace(/\/$/, "")
|
||||
const url = `${base}/api/workspace-plugin/opencode/${encodeURIComponent(project.slug)}`
|
||||
pluginLogger.info(`adaptor.target slug=${project.slug} workspaceID=${info.id} url=${url}`)
|
||||
return {
|
||||
type: "remote" as const,
|
||||
url,
|
||||
headers: { Authorization: `Bearer ${cfg.token}` },
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const prewarmed = new Set<string>()
|
||||
|
||||
async function prewarmProject(input: PluginInput, project: ManagerProject): Promise<void> {
|
||||
if (prewarmed.has(project.directory)) return
|
||||
prewarmed.add(project.directory)
|
||||
const start = Date.now()
|
||||
try {
|
||||
await input.client.project.current({ query: { directory: project.directory } })
|
||||
pluginLogger.info(
|
||||
`prewarm.ok slug=${project.slug} dir=${project.directory} elapsed_ms=${Date.now() - start}`,
|
||||
)
|
||||
} catch (error) {
|
||||
prewarmed.delete(project.directory)
|
||||
pluginLogger.warn(
|
||||
`prewarm.failed slug=${project.slug} dir=${project.directory} elapsed_ms=${Date.now() - start}`,
|
||||
error,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const OpencodeManagerWorkspacePlugin: Plugin = async (input, options) => {
|
||||
export const OpencodeManagerWorkspacePlugin: Plugin = async ({ experimental_workspace }, options) => {
|
||||
try {
|
||||
const cfg = resolveConfig(options)
|
||||
pluginLogger.info(`plugin.init url=${cfg.url}`)
|
||||
@@ -71,10 +57,9 @@ export const OpencodeManagerWorkspacePlugin: Plugin = async (input, options) =>
|
||||
const projects = await client.listProjects()
|
||||
pluginLogger.info(`plugin.init loaded ${projects.length} project(s)`)
|
||||
for (const project of projects) {
|
||||
input.experimental_workspace.register(`manager:${project.slug}`, makeAdaptor(cfg, project))
|
||||
experimental_workspace.register(`manager:${project.slug}`, makeAdaptor(cfg, project))
|
||||
}
|
||||
pluginLogger.info(`plugin.ready`)
|
||||
void Promise.all(projects.map((project) => prewarmProject(input, project)))
|
||||
return {}
|
||||
} catch (error) {
|
||||
pluginLogger.error(`plugin.init failed`, error)
|
||||
|
||||
@@ -104,21 +104,35 @@ describe('adaptor.target', () => {
|
||||
token: 'tok-abc',
|
||||
}
|
||||
|
||||
it('returns { type: "local", directory: project.directory }', async () => {
|
||||
it('returns remote target pointing at manager proxy for the project slug', async () => {
|
||||
const { makeAdaptor } = await import('../src/index.js')
|
||||
const project = { slug: '42', name: 'Test Project', directory: '/workspace/repos/test' }
|
||||
const adaptor = makeAdaptor(baseCfg, project)
|
||||
const result = adaptor.target(sampleInfo)
|
||||
expect(result.type).toBe('local')
|
||||
expect(result.directory).toBe('/workspace/repos/test')
|
||||
expect(result.type).toBe('remote')
|
||||
if (result.type !== 'remote') throw new Error('expected remote target')
|
||||
expect(result.url).toBe('http://manager.test:5003/api/workspace-plugin/opencode/42')
|
||||
expect(result.headers).toEqual({ Authorization: 'Bearer tok-abc' })
|
||||
})
|
||||
|
||||
it('returns directory from project, not from info', async () => {
|
||||
it('url-encodes slugs with special characters', async () => {
|
||||
const { makeAdaptor } = await import('../src/index.js')
|
||||
const project = { slug: '42', name: 'Test Project', directory: '/workspace/repos/test' }
|
||||
const project = { slug: 'my repo/branch', name: 'Test Project', directory: '/workspace/repos/test' }
|
||||
const adaptor = makeAdaptor(baseCfg, project)
|
||||
const result = adaptor.target(sampleInfo)
|
||||
expect(result.directory).toBe('/workspace/repos/test')
|
||||
if (result.type !== 'remote') throw new Error('expected remote target')
|
||||
expect(result.url).toBe('http://manager.test:5003/api/workspace-plugin/opencode/my%20repo%2Fbranch')
|
||||
})
|
||||
|
||||
it('strips trailing slash from the manager base url', async () => {
|
||||
const { makeAdaptor } = await import('../src/index.js')
|
||||
const adaptor = makeAdaptor(
|
||||
{ url: 'http://manager.test:5003/', token: 'tok-abc' },
|
||||
{ slug: '42', name: 'Test Project', directory: '/workspace/repos/test' },
|
||||
)
|
||||
const result = adaptor.target(sampleInfo)
|
||||
if (result.type !== 'remote') throw new Error('expected remote target')
|
||||
expect(result.url).toBe('http://manager.test:5003/api/workspace-plugin/opencode/42')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user