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

+29 -87
View File
@@ -1,12 +1,10 @@
import { useMemo, useRef, useEffect } from 'react'
import { useMessages, useConfig, useAgents } from './useOpenCode'
import { useMemo, useEffect } from 'react'
import { useSession, useAgents } from './useOpenCode'
import { useSessionAgentStore } from '@/stores/sessionAgentStore'
import type { components } from '@/api/opencode-types'
type UserMessage = components['schemas']['UserMessage']
interface AgentInfo {
name: string
id: string
name?: string
mode?: string
hidden?: boolean
}
@@ -17,34 +15,28 @@ const getPrimaryAgents = (agents: AgentInfo[] | undefined): AgentInfo[] => {
) ?? []
}
const resolveAvailableAgentName = (
agentName: string | undefined,
const resolveAvailableAgentId = (
agentId: string | undefined,
agents: AgentInfo[] | undefined,
agentsLoaded: boolean
): string | undefined => {
if (!agentName) return undefined
if (!agentsLoaded) return agentName
if (!agentId) return undefined
if (!agentsLoaded) return agentId
const normalizedAgentName = agentName.toLowerCase()
const normalizedAgentId = agentId.toLowerCase()
return getPrimaryAgents(agents).find(
(agent) => agent.name.toLowerCase() === normalizedAgentName
)?.name
(agent) => agent.id.toLowerCase() === normalizedAgentId
)?.id
}
export function resolveDefaultSessionAgent(
configDefaultAgent: string | undefined,
agents: AgentInfo[] | undefined,
agentsLoaded: boolean
): string {
const primaryAgents = getPrimaryAgents(agents)
const resolvedConfigAgent = resolveAvailableAgentName(configDefaultAgent, agents, agentsLoaded)
if (resolvedConfigAgent) {
return resolvedConfigAgent
}
if (agentsLoaded && primaryAgents.length > 0) {
return primaryAgents[0].name
return primaryAgents[0].id
}
return 'build'
@@ -54,92 +46,42 @@ interface SessionAgentResult {
agent: string
model: { providerID: string; modelID: string } | undefined
variant: string | undefined
fromMessage: boolean
}
export function useSessionAgent(
opcodeUrl: string | null | undefined,
sessionID: string | undefined,
directory?: string
) {
const { data: messages } = useMessages(opcodeUrl, sessionID, directory)
const { data: config } = useConfig(opcodeUrl, directory)
const { data: agents, isSuccess: agentsLoaded } = useAgents(opcodeUrl, directory)
const { data: session } = useSession(sessionID, directory)
const { data: agents, isSuccess: agentsLoaded } = useAgents(directory)
const storedAgent = useSessionAgentStore((s) => s.agents[sessionID ?? ''] ?? null)
const setAgent = useSessionAgentStore((s) => s.setAgent)
const prevRef = useRef<SessionAgentResult>({ agent: 'build', model: undefined, variant: undefined, fromMessage: false })
const defaultAgent = useMemo(
() => resolveDefaultSessionAgent(config?.default_agent, agents, agentsLoaded),
[config?.default_agent, agents, agentsLoaded]
() => resolveDefaultSessionAgent(agents, agentsLoaded),
[agents, agentsLoaded]
)
const result = useMemo(() => {
const stabilize = (next: SessionAgentResult): SessionAgentResult => {
const prev = prevRef.current
if (
prev.agent === next.agent &&
prev.variant === next.variant &&
prev.fromMessage === next.fromMessage &&
prev.model?.providerID === next.model?.providerID &&
prev.model?.modelID === next.model?.modelID
) {
return prev
}
const sessionAgent = resolveAvailableAgentId(session?.agent, agents, agentsLoaded)
const fallbackAgent = resolveAvailableAgentId(storedAgent ?? undefined, agents, agentsLoaded)
prevRef.current = next
return next
}
const result = useMemo<SessionAgentResult>(() => {
const model = session?.model
? { providerID: session.model.providerID, modelID: session.model.id }
: undefined
const withoutMessageAgent = (
model: SessionAgentResult['model'],
variant: string | undefined
): SessionAgentResult => stabilize({
agent: resolveAvailableAgentName(storedAgent, agents, agentsLoaded) ?? defaultAgent,
return {
agent: sessionAgent ?? fallbackAgent ?? defaultAgent,
model,
variant,
fromMessage: false,
})
if (!messages || messages.length === 0) {
return withoutMessageAgent(undefined, undefined)
variant: session?.model?.variant,
}
let latestAgent: string | undefined
let latestModel: { providerID: string; modelID: string } | undefined
let latestVariant: string | undefined
for (let i = messages.length - 1; i >= 0; i--) {
const msgWithParts = messages[i]
if (msgWithParts.info.role === 'user') {
const userInfo = msgWithParts.info as UserMessage
if (userInfo.agent) {
latestAgent = userInfo.agent
latestModel = userInfo.model
latestVariant = userInfo.variant
break
}
}
}
const resolvedLatestAgent = resolveAvailableAgentName(latestAgent, agents, agentsLoaded)
if (!resolvedLatestAgent) {
return withoutMessageAgent(latestModel, latestVariant)
}
return stabilize({
agent: resolvedLatestAgent,
model: latestModel,
variant: latestVariant,
fromMessage: true,
})
}, [messages, storedAgent, defaultAgent, agents, agentsLoaded])
}, [sessionAgent, fallbackAgent, defaultAgent, session?.model])
useEffect(() => {
if (result.agent && sessionID && result.fromMessage) {
setAgent(sessionID, result.agent)
if (session?.agent && sessionID) {
setAgent(sessionID, session.agent)
}
}, [result.agent, result.fromMessage, sessionID, setAgent])
}, [session?.agent, sessionID, setAgent])
return { agent: result.agent, model: result.model, variant: result.variant }
}