feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)

* 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
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-26 13:16:45 -04:00
1 parent 6c2ebd764b
commit dacde879d6
383 files changed
+29905 -46566

No files matched your search

+30 -81
View File
@@ -1,20 +1,11 @@
import type { ModelInfo, ModelRef } from '@opencode-manager/shared/opencode'
import { formatOpenCodeModelRef, openCodeLocation, parseOpenCodeModelRef } from '@opencode-manager/shared/opencode'
import type { OpenCodeClient } from './opencode/client'
interface OpenCodeConfigResponse {
model?: string
}
interface OpenCodeProviderResponse {
providers?: Array<{
id: string
models?: Record<string, unknown>
}>
default?: Record<string, string>
}
export interface ResolvedOpenCodeModel {
providerID: string
modelID: string
id: string
variant?: string
model: string
}
@@ -27,94 +18,52 @@ function normalizeModelCandidate(model: string | null | undefined): string | nul
return normalized ? normalized : null
}
function parseModel(model: string): ResolvedOpenCodeModel | null {
const [providerID, ...modelParts] = model.split('/')
const modelID = modelParts.join('/')
if (!providerID || !modelID) {
return null
}
function toResolvedModel(ref: ModelRef): ResolvedOpenCodeModel {
return {
providerID,
modelID,
model: `${providerID}/${modelID}`,
providerID: ref.providerID,
id: ref.id,
...(ref.variant ? { variant: ref.variant } : {}),
model: formatOpenCodeModelRef(ref),
}
}
function buildAvailableModels(response: OpenCodeProviderResponse): Set<string> {
const availableModels = new Set<string>()
for (const provider of response.providers ?? []) {
for (const modelID of Object.keys(provider.models ?? {})) {
availableModels.add(`${provider.id}/${modelID}`)
}
}
return availableModels
}
function uniqueCandidates(candidates: Array<string | null | undefined>): string[] {
const normalizedCandidates = candidates
.map(normalizeModelCandidate)
.filter((candidate): candidate is string => candidate !== null)
return [...new Set(normalizedCandidates)]
}
async function fetchOpenCodeConfig(client: OpenCodeClient, directory?: string): Promise<OpenCodeConfigResponse> {
return client.getJson<OpenCodeConfigResponse>('/config', { directory })
}
async function fetchOpenCodeProviders(client: OpenCodeClient, directory?: string): Promise<OpenCodeProviderResponse> {
return client.getJson<OpenCodeProviderResponse>('/config/providers', { directory })
function findAvailable(models: ModelInfo[], ref: ModelRef): ModelInfo | undefined {
return models.find((model) => model.providerID === ref.providerID && model.id === ref.id)
}
export async function resolveOpenCodeModel(
client: OpenCodeClient,
directory: string | undefined,
directory: string,
options?: {
preferredModel?: string | null
},
): Promise<ResolvedOpenCodeModel> {
const [config, providersResponse] = await Promise.all([
fetchOpenCodeConfig(client, directory),
fetchOpenCodeProviders(client, directory),
const location = openCodeLocation(directory)
const [modelsResponse, defaultResponse] = await Promise.all([
client.api.model.list(location),
client.api.model.default(location),
])
const models = modelsResponse.data
const availableModels = buildAvailableModels(providersResponse)
const defaultModels = providersResponse.default ?? {}
const candidates = uniqueCandidates([options?.preferredModel, config.model])
for (const candidate of candidates) {
if (availableModels.has(candidate)) {
const parsedCandidate = parseModel(candidate)
if (parsedCandidate) {
return parsedCandidate
}
const preferred = normalizeModelCandidate(options?.preferredModel)
if (preferred) {
const parsedPreferred = parseOpenCodeModelRef(preferred)
if (parsedPreferred && findAvailable(models, parsedPreferred)) {
return toResolvedModel(parsedPreferred)
}
}
for (const [providerID, modelID] of Object.entries(defaultModels)) {
const model = `${providerID}/${modelID}`
if (availableModels.has(model)) {
return {
providerID,
modelID,
model,
}
const defaultModel = defaultResponse.data
if (defaultModel) {
const defaultRef: ModelRef = { providerID: defaultModel.providerID, id: defaultModel.id }
if (findAvailable(models, defaultRef)) {
return toResolvedModel(defaultRef)
}
}
for (const provider of providersResponse.providers ?? []) {
const firstModelID = Object.keys(provider.models ?? {})[0]
if (firstModelID) {
return {
providerID: provider.id,
modelID: firstModelID,
model: `${provider.id}/${firstModelID}`,
}
}
const fallback = models.find((model) => model.enabled)
if (fallback) {
return toResolvedModel({ providerID: fallback.providerID, id: fallback.id })
}
throw new Error('No configured OpenCode models are available')