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

+33 -120
View File
@@ -1,168 +1,81 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { createOpenCodeClient } from '@/api/opencode'
import {
commitRevert,
sendPrompt,
stageRevert,
} from '@/api/opencode'
import { parseOpenCodeModelRef } from '@opencode-manager/shared/opencode'
import { useSyncSessionSelection } from '@/hooks/useOpenCode'
import { showToast } from '@/lib/toast'
import { messagesQueryKey } from '@/lib/queryInvalidation'
import type { Message, Part, MessageWithParts } from '@/api/types'
import { sessionTranscriptQueryKey } from '@/lib/queryInvalidation'
interface UseRemoveMessageOptions {
opcodeUrl: string | null
sessionId: string
directory?: string
}
interface RemoveMessageContext {
previousMessages?: MessageWithParts[]
}
export function useRemoveMessage({ opcodeUrl, sessionId, directory }: UseRemoveMessageOptions) {
export function useRemoveMessage({ sessionId, directory }: UseRemoveMessageOptions) {
const queryClient = useQueryClient()
return useMutation<unknown, Error, { messageID: string; partID?: string }, RemoveMessageContext>({
mutationFn: async ({ messageID, partID }: { messageID: string, partID?: string }) => {
if (!opcodeUrl) throw new Error('OpenCode URL not available')
const client = createOpenCodeClient(opcodeUrl, directory)
return client.revertMessage(sessionId, { messageID, partID })
return useMutation({
mutationFn: async ({ messageID }: { messageID: string }) => {
await stageRevert(sessionId, messageID)
await commitRevert(sessionId)
},
onMutate: async ({ messageID }) => {
const queryKey = messagesQueryKey(opcodeUrl, sessionId, directory)
await queryClient.cancelQueries({ queryKey })
const previousMessages = queryClient.getQueryData<MessageWithParts[]>(queryKey)
if (previousMessages) {
const messageIndex = previousMessages.findIndex(m => m.info.id === messageID)
if (messageIndex !== -1) {
const newMessages = previousMessages.slice(0, messageIndex)
queryClient.setQueryData(queryKey, newMessages)
}
}
return { previousMessages }
},
onError: (_error, _variables, _context: RemoveMessageContext | undefined) => {
if (_context?.previousMessages) {
queryClient.setQueryData(
messagesQueryKey(opcodeUrl, sessionId, directory),
_context.previousMessages
)
}
onError: () => {
showToast.error('Failed to remove message')
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: sessionTranscriptQueryKey(sessionId) })
queryClient.invalidateQueries({
queryKey: messagesQueryKey(opcodeUrl, sessionId, directory)
})
queryClient.invalidateQueries({
queryKey: ['opencode', 'session', opcodeUrl, sessionId, directory]
queryKey: ['opencode', 'session', sessionId, directory]
})
}
})
}
interface UseRefreshMessageOptions {
opcodeUrl: string | null
sessionId: string
directory?: string
}
export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefreshMessageOptions) {
export function useRefreshMessage({ sessionId, directory }: UseRefreshMessageOptions) {
const queryClient = useQueryClient()
const removeMessage = useRemoveMessage({ opcodeUrl, sessionId, directory })
const removeMessage = useRemoveMessage({ sessionId, directory })
const syncSelection = useSyncSessionSelection(directory)
return useMutation({
mutationFn: async ({
assistantMessageID,
mutationFn: async ({
assistantMessageID,
userMessageContent,
model,
agent
}: {
}: {
assistantMessageID: string
userMessageContent: string
model?: string
agent?: string
}) => {
if (!opcodeUrl) throw new Error('OpenCode URL not available')
await removeMessage.mutateAsync({ messageID: assistantMessageID })
const client = createOpenCodeClient(opcodeUrl, directory)
const optimisticUserID = `optimistic_user_${Date.now()}_${Math.random()}`
const userMessageInfo = {
id: optimisticUserID,
role: 'user' as const,
const modelRef = model ? parseOpenCodeModelRef(model) : undefined
await syncSelection({ sessionID: sessionId, model: modelRef, agent })
await sendPrompt({
sessionID: sessionId,
time: { created: Date.now() }
} as Message
const userMessageParts = [{
id: `${optimisticUserID}_part_0`,
type: 'text' as const,
text: userMessageContent,
messageID: optimisticUserID,
sessionID: sessionId
}] as Part[]
})
const optimisticMessageWithParts: MessageWithParts = {
info: userMessageInfo,
parts: userMessageParts,
}
queryClient.setQueryData<MessageWithParts[]>(
messagesQueryKey(opcodeUrl, sessionId, directory),
(old) => [...(old || []), optimisticMessageWithParts]
)
interface RefreshPromptRequest {
parts: Array<{ type: 'text'; text: string }>
model?: { providerID: string; modelID: string }
agent?: string
}
const requestData: RefreshPromptRequest = {
parts: [{ type: 'text', text: userMessageContent }]
}
if (model) {
const [providerID, modelID] = model.split('/')
if (providerID && modelID) {
requestData.model = { providerID, modelID }
}
}
if (agent) {
requestData.agent = agent
}
await client.sendPromptAsync(sessionId, requestData)
return { optimisticUserID, userMessageContent }
return { userMessageContent }
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: sessionTranscriptQueryKey(sessionId) })
queryClient.invalidateQueries({
queryKey: messagesQueryKey(opcodeUrl, sessionId, directory)
})
queryClient.invalidateQueries({
queryKey: ['opencode', 'session', opcodeUrl, sessionId, directory]
queryKey: ['opencode', 'session', sessionId, directory]
})
},
onError: (_, variables) => {
void variables
queryClient.setQueryData<MessageWithParts[]>(
messagesQueryKey(opcodeUrl, sessionId, directory),
(old) => {
const messages = old || []
const optimisticIndex = messages.findIndex((m) => m.info.id.startsWith('optimistic_user_'))
if (optimisticIndex !== -1) {
return messages.slice(0, optimisticIndex)
}
return messages
}
)
onError: () => {
showToast.error('Failed to refresh message')
}
})
}
}