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

+162
View File
@@ -0,0 +1,162 @@
import { OpenCode } from '@opencode/client'
export type {
AgentInfo,
CommandInfo,
ConfigEntry,
FileDiffInfo,
FormAnswer,
FormExternalField,
FormField,
FormInfo,
FormValue,
FormWhen,
IntegrationInfo,
IntegrationKeyMethod,
IntegrationMethod,
IntegrationOAuthMethod,
McpServer,
ModelInfo,
ModelRef,
PermissionRequest,
PromptAgentAttachment,
PromptFileAttachment,
PromptMention,
PromptSkillAttachment,
SessionInfo,
SessionInboxCompaction,
SessionInboxInfo,
SessionInboxUser,
SessionMessageAssistant,
SessionMessageAssistantReasoning,
SessionMessageAssistantRetry,
SessionMessageAssistantText,
SessionMessageAssistantTool,
SessionMessageCompaction,
SessionMessageInfo,
SessionMessageShell,
SessionMessageUser,
SessionRevert,
SessionStatus,
SessionStructuredError,
SessionTransferData,
SkillInfo,
V2Event,
} from '@opencode/client'
export { ClientError, isIntegrationNotFoundError, isMcpServerNotFoundError, isSessionNotFoundError, isWorktreeError } from '@opencode/client'
export { assistantText, sessionIDFromEvent, toolContentText } from './content'
export { formatOpenCodeModelRef, parseOpenCodeModelRef } from './modelRef'
export {
MCP_OAUTH_CALLBACK_PATH,
mcpOAuthRedirectUri,
mcpServersFromConfig,
mcpStatusByName,
} from './mcp'
export type {
McpServerConfig,
McpStatus,
McpStatusMap,
McpTimeoutConfig,
} from './mcp'
export {
OPENCODE_PINNED_VERSION,
OPENCODE_SUPPORTED_VERSION_RANGE,
buildOpenCodeReleaseAsset,
compareOpenCodeVersions,
describeUnsupportedOpenCodeVersion,
isStableOpenCodeVersion,
isSupportedOpenCodeVersion,
normalizeOpenCodeVersion,
parseOpenCodeVersion,
parseOpenCodeVersionOutput,
} from './release'
export const OPENCODE_SERVER_USERNAME = 'opencode'
export type OpenCodeApi = ReturnType<typeof OpenCode.make>
export interface OpenCodeApiOptions {
baseUrl: string
password?: string
fetch?: typeof fetch
headers?: Record<string, string>
}
export function buildOpenCodeBasicAuth(password: string): string {
const bytes = new TextEncoder().encode(`${OPENCODE_SERVER_USERNAME}:${password}`)
let binary = ''
for (const byte of bytes) binary += String.fromCharCode(byte)
return `Basic ${btoa(binary)}`
}
export function createOpenCodeApi(options: OpenCodeApiOptions): OpenCodeApi {
return OpenCode.make({
baseUrl: options.baseUrl,
fetch: options.fetch,
headers: {
...options.headers,
...(options.password ? { Authorization: buildOpenCodeBasicAuth(options.password) } : {}),
},
})
}
export interface OpenCodeLocation {
location: { directory: string }
}
export function openCodeLocation(directory: string | undefined): OpenCodeLocation | undefined {
return directory ? { location: { directory } } : undefined
}
const OPENCODE_ERROR_STATUS_BY_TAG: Record<string, number> = {
InvalidRequestError: 400,
RpcError: 400,
InvalidCursorError: 400,
FormInvalidAnswerError: 400,
UnauthorizedError: 401,
ForbiddenError: 403,
ProviderNotFoundError: 404,
IntegrationNotFoundError: 404,
IntegrationAttemptNotFoundError: 404,
IntegrationMethodNotFoundError: 404,
ProjectNotFoundError: 404,
FileNotFoundError: 404,
AgentNotFoundError: 404,
SessionNotFoundError: 404,
MessageNotFoundError: 404,
SkillNotFoundError: 404,
McpServerNotFoundError: 404,
CommandNotFoundError: 404,
PermissionNotFoundError: 404,
FormNotFoundError: 404,
PtyNotFoundError: 404,
ShellNotFoundError: 404,
ConflictError: 409,
SessionBusyError: 409,
FormAlreadySettledError: 409,
InstructionEntryValueTooLargeError: 413,
CommandExecutionError: 500,
RpcInternalError: 500,
UnknownError: 500,
ServiceUnavailableError: 503,
}
export function openCodeErrorStatus(error: unknown): number | undefined {
if (error === null || typeof error !== 'object') return undefined
const tag = (error as { _tag?: unknown })._tag
if (typeof tag === 'string' && OPENCODE_ERROR_STATUS_BY_TAG[tag] !== undefined) {
return OPENCODE_ERROR_STATUS_BY_TAG[tag]
}
const name = (error as { name?: unknown }).name
if (typeof name === 'string') return OPENCODE_ERROR_STATUS_BY_TAG[name]
return undefined
}