mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* feat(opencode): migrate OpenCode Manager to OpenCode 2 Port the backend lifecycle, generated plugins, event aggregation, schedules, worktrees, provider credentials, MCP, config handling, the frontend chat model, and the ocm CLI to OpenCode 2. Adopt @opencode/client 2.0.15 as the single typed V2 API contract, require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every V1-only code path. * refactor(opencode): reload config in place and drop v1 compatibility shims Route config/skill/AGENTS.md changes through the OpenCode location reload API instead of a full server restart, stop auto-resuming interrupted sessions, remove the restart coordinator and v1 MCP/version shims, and pin support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries, service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers. * refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows - extract shared semver and model-ref utilities, reuse across version checks - share the OpenCode release download script across Docker, entrypoint, dev setup - unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN - scope the repo proxy location rewrite and surface ocm proxy errors - rework frontend session projection, permission lookup, and cache invalidation * fix(schedules): reject unavailable agents before creating a session * feat(opencode): add ocm install, schedule agent fallback, and session move guards
103 lines
3.5 KiB
TypeScript
103 lines
3.5 KiB
TypeScript
import packageJson from '../package.json' with { type: 'json' }
|
|
import { delay } from './delay.js'
|
|
import { describeCause } from './token-store.js'
|
|
|
|
export const OCM_VERSION = packageJson.version
|
|
export const MIN_MANAGER_VERSION = '0.19.0'
|
|
|
|
export class ManagerTooOldError extends Error {
|
|
constructor(managerUrl: string) {
|
|
super(`OpenCode Manager at ${managerUrl} is too old for ocm ${OCM_VERSION}; upgrade the Manager to >= ${MIN_MANAGER_VERSION}`)
|
|
this.name = 'ManagerTooOldError'
|
|
}
|
|
}
|
|
|
|
export class RepoProxyNotFoundError extends Error {
|
|
constructor(managerUrl: string, repoId: number) {
|
|
super(`repo ${repoId} is not available on OpenCode Manager at ${managerUrl} (missing or not ready)`)
|
|
this.name = 'RepoProxyNotFoundError'
|
|
}
|
|
}
|
|
|
|
export class ManagerTokenInvalidError extends Error {
|
|
constructor(managerUrl: string) {
|
|
super(`OpenCode Manager rejected the token for ${managerUrl} (HTTP 401). The token may have been rotated: copy the current one from Settings -> Manager Internal Token, then run \`ocm login ${managerUrl}\` to store it again.`)
|
|
this.name = 'ManagerTokenInvalidError'
|
|
}
|
|
}
|
|
|
|
export class RepoProxyResponseError extends Error {
|
|
constructor(managerUrl: string, status: number, body: string) {
|
|
const detail = body.trim()
|
|
super(`OpenCode Manager at ${managerUrl} returned HTTP ${status} from the repo proxy${detail ? `: ${detail}` : ''}`)
|
|
this.name = 'RepoProxyResponseError'
|
|
}
|
|
}
|
|
|
|
export class RepoProxyUnreachableError extends Error {
|
|
constructor(managerUrl: string, cause: unknown) {
|
|
super(`could not reach OpenCode Manager at ${managerUrl}: ${describeCause(cause)}`, { cause })
|
|
this.name = 'RepoProxyUnreachableError'
|
|
}
|
|
}
|
|
|
|
export type WarmRepoProxyOptions = {
|
|
attempts?: number
|
|
fetch?: typeof fetch
|
|
wait?: (ms: number) => Promise<void>
|
|
}
|
|
|
|
export function repoProxyBaseUrl(managerUrl: string): string {
|
|
return `${managerUrl}/api/opencode-proxy`
|
|
}
|
|
|
|
export function repoProxyUrl(managerUrl: string, repoId: number): string {
|
|
return `${repoProxyBaseUrl(managerUrl)}/repos/${repoId}`
|
|
}
|
|
|
|
function isRepoNotFoundBody(body: string): boolean {
|
|
try {
|
|
const parsed: unknown = JSON.parse(body)
|
|
return typeof parsed === 'object' && parsed !== null && (parsed as { error?: unknown }).error === 'Repo not found'
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export async function warmRepoProxy(managerUrl: string, token: string, repoId: number, options: WarmRepoProxyOptions = {}): Promise<void> {
|
|
const fetchImpl = options.fetch ?? fetch
|
|
const attempts = Math.max(1, options.attempts ?? 3)
|
|
const wait = options.wait ?? delay
|
|
const url = `${repoProxyUrl(managerUrl, repoId)}/api/session?limit=1`
|
|
|
|
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
let res: Response | null = null
|
|
let networkError: unknown
|
|
try {
|
|
res = await fetchImpl(url, { headers: { Authorization: `Bearer ${token}` } })
|
|
} catch (err) {
|
|
networkError = err
|
|
}
|
|
|
|
const isFinalAttempt = attempt === attempts
|
|
|
|
if (res) {
|
|
const body = await res.text().catch(() => '')
|
|
if (res.ok) return
|
|
if (res.status === 404) {
|
|
throw isRepoNotFoundBody(body) ? new RepoProxyNotFoundError(managerUrl, repoId) : new ManagerTooOldError(managerUrl)
|
|
}
|
|
if (res.status === 401) {
|
|
throw new ManagerTokenInvalidError(managerUrl)
|
|
}
|
|
if (isFinalAttempt) {
|
|
throw new RepoProxyResponseError(managerUrl, res.status, body)
|
|
}
|
|
} else if (isFinalAttempt) {
|
|
throw new RepoProxyUnreachableError(managerUrl, networkError)
|
|
}
|
|
|
|
await wait(attempt * 500)
|
|
}
|
|
}
|