Files
opencode-manager/shared/src/opencode/release.ts
T
Chris Scott dacde879d6 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
2026-09-26 13:16:45 -04:00

86 lines
3.0 KiB
TypeScript

import { compareParsedSemver, compareSemver, normalizeSemver, parseSemver, type Semver } from '../utils/semver'
export const OPENCODE_PINNED_VERSION = '2.0.15'
const OPENCODE_RELEASE_BASE_URL = 'https://opencode.ai/files/bin'
const OPENCODE_ARCH_ALIASES: Record<string, string> = {
x64: 'x64',
amd64: 'x64',
x86_64: 'x64',
arm64: 'arm64',
aarch64: 'arm64',
}
const OPENCODE_PLATFORM_ARCHIVES: Partial<Record<NodeJS.Platform, 'tar.gz' | 'zip'>> = {
linux: 'tar.gz',
darwin: 'zip',
}
export type OpenCodeVersion = Semver
const OPENCODE_STABLE_VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
const OPENCODE_VERSION_OUTPUT_PATTERN = /(?<![\d.])v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)/g
export function normalizeOpenCodeVersion(version: string): string {
return normalizeSemver(version)
}
export function parseOpenCodeVersion(version: string): OpenCodeVersion | null {
return parseSemver(version)
}
export function isStableOpenCodeVersion(version: string): boolean {
return OPENCODE_STABLE_VERSION_PATTERN.test(normalizeOpenCodeVersion(version))
}
export function parseOpenCodeVersionOutput(output: string): string | null {
for (const match of output.matchAll(OPENCODE_VERSION_OUTPUT_PATTERN)) {
const candidate = (match[1] ?? '').replace(/[.+-]+$/, '')
if (parseOpenCodeVersion(candidate)) return candidate
}
return null
}
export function compareOpenCodeVersions(left: string, right: string): number {
return compareSemver(left, right)
}
function parsePinnedOpenCodeVersion(): OpenCodeVersion {
const pinned = parseOpenCodeVersion(OPENCODE_PINNED_VERSION)
if (!pinned) throw new Error(`OPENCODE_PINNED_VERSION is not a valid version: ${OPENCODE_PINNED_VERSION}`)
return pinned
}
const OPENCODE_PINNED = parsePinnedOpenCodeVersion()
export const OPENCODE_SUPPORTED_VERSION_RANGE = `>=${OPENCODE_PINNED_VERSION} <${OPENCODE_PINNED.major + 1}.0.0`
export function isSupportedOpenCodeVersion(version: string): boolean {
const parsed = parseOpenCodeVersion(version)
if (!parsed) return false
return parsed.major === OPENCODE_PINNED.major && compareParsedSemver(parsed, OPENCODE_PINNED) >= 0
}
export function describeUnsupportedOpenCodeVersion(version: string): string {
return `OpenCode ${version} is not supported; OpenCode Manager requires OpenCode ${OPENCODE_SUPPORTED_VERSION_RANGE}`
}
export function buildOpenCodeReleaseAsset(
version: string,
target: { platform: NodeJS.Platform; arch: string; musl: boolean },
): { url: string; archive: 'tar.gz' | 'zip' } {
const archive = OPENCODE_PLATFORM_ARCHIVES[target.platform]
if (!archive) throw new Error(`Unsupported OpenCode platform: ${target.platform}`)
const arch = OPENCODE_ARCH_ALIASES[target.arch]
if (!arch) throw new Error(`Unsupported OpenCode architecture: ${target.arch}`)
const muslSuffix = target.musl ? '-musl' : ''
return {
url: `${OPENCODE_RELEASE_BASE_URL}/${version}/opencode-${target.platform}-${arch}${muslSuffix}.${archive}`,
archive,
}
}