mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* 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
158 lines
4.8 KiB
TypeScript
158 lines
4.8 KiB
TypeScript
import { create } from 'zustand'
|
|
import { parseOpenCodeModelRef } from '@opencode-manager/shared/opencode'
|
|
import type { Provider } from '@/api/providers'
|
|
|
|
export interface ModelSelection {
|
|
providerID: string
|
|
modelID: string
|
|
}
|
|
|
|
interface ModelStore {
|
|
model: ModelSelection | null
|
|
variants: Record<string, string | undefined>
|
|
lastConfigModel: string | undefined
|
|
|
|
setModel: (model: ModelSelection) => void
|
|
setActiveModel: (model: ModelSelection) => void
|
|
syncModelState: (state: { recent: ModelSelection[], favorite: ModelSelection[], variant: Record<string, string | undefined> }) => void
|
|
syncFromConfig: (configModel: string | undefined, force?: boolean) => void
|
|
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[], recent?: ModelSelection[], fallbackModel?: string) => void
|
|
getModelString: () => string | null
|
|
setVariant: (model: ModelSelection, variant: string | undefined) => void
|
|
getVariant: (model: ModelSelection) => string | undefined
|
|
clearVariant: (model: ModelSelection) => void
|
|
}
|
|
|
|
export function modelExists(model: ModelSelection | null, providers: Provider[]): boolean {
|
|
if (!model) return false
|
|
return providers.some(
|
|
(p) => p.id === model.providerID && p.models && model.modelID in p.models
|
|
)
|
|
}
|
|
|
|
function modelSelectionFromString(model: string): ModelSelection | null {
|
|
const ref = parseOpenCodeModelRef(model)
|
|
return ref ? { providerID: ref.providerID, modelID: ref.id } : null
|
|
}
|
|
|
|
function mergeVariants(
|
|
current: Record<string, string | undefined>,
|
|
incoming: Record<string, string | undefined>
|
|
): Record<string, string | undefined> | undefined {
|
|
const next = { ...incoming, ...current }
|
|
const currentKeys = Object.keys(current)
|
|
const nextKeys = Object.keys(next)
|
|
|
|
if (currentKeys.length !== nextKeys.length) return next
|
|
return nextKeys.some((key) => current[key] !== next[key]) ? next : undefined
|
|
}
|
|
|
|
if (typeof localStorage !== 'undefined') localStorage.removeItem('opencode-model-selection')
|
|
|
|
export const useModelStore = create<ModelStore>()((set, get) => ({
|
|
model: null,
|
|
variants: {},
|
|
lastConfigModel: undefined,
|
|
|
|
setModel: (model: ModelSelection) => {
|
|
set({ model })
|
|
},
|
|
|
|
setActiveModel: (model: ModelSelection) => {
|
|
set({ model })
|
|
},
|
|
|
|
syncModelState: (modelState) => {
|
|
const variants = mergeVariants(get().variants, modelState.variant)
|
|
if (variants) {
|
|
set({ variants })
|
|
}
|
|
},
|
|
|
|
syncFromConfig: (configModel: string | undefined, force = false) => {
|
|
const state = get()
|
|
if (!force && state.lastConfigModel === configModel) return
|
|
|
|
if (configModel) {
|
|
const parsed = modelSelectionFromString(configModel)
|
|
if (parsed) {
|
|
set({ model: parsed, lastConfigModel: configModel })
|
|
return
|
|
}
|
|
}
|
|
set({ lastConfigModel: configModel })
|
|
},
|
|
|
|
validateAndSyncModel: (configModel: string | undefined, providers?: Provider[], recent: ModelSelection[] = [], fallbackModel?: string) => {
|
|
if (!providers) {
|
|
if (configModel) {
|
|
get().syncFromConfig(configModel)
|
|
}
|
|
return
|
|
}
|
|
|
|
const state = get()
|
|
if (state.model && modelExists(state.model, providers)) return
|
|
|
|
const parsedConfig = configModel ? modelSelectionFromString(configModel) : null
|
|
if (parsedConfig && modelExists(parsedConfig, providers)) {
|
|
get().syncFromConfig(configModel, true)
|
|
return
|
|
}
|
|
|
|
const recentModel = recent.find((model) => modelExists(model, providers))
|
|
if (recentModel) {
|
|
set({ model: recentModel, lastConfigModel: configModel })
|
|
return
|
|
}
|
|
|
|
const parsedFallback = fallbackModel ? modelSelectionFromString(fallbackModel) : null
|
|
if (parsedFallback && modelExists(parsedFallback, providers)) {
|
|
set({ model: parsedFallback, lastConfigModel: configModel })
|
|
return
|
|
}
|
|
|
|
if (state.model === null && state.lastConfigModel === configModel) return
|
|
set({ model: null, lastConfigModel: configModel })
|
|
},
|
|
|
|
getModelString: () => {
|
|
const { model } = get()
|
|
if (!model) return null
|
|
return `${model.providerID}/${model.modelID}`
|
|
},
|
|
|
|
setVariant: (model: ModelSelection, variant: string | undefined) => {
|
|
const key = `${model.providerID}/${model.modelID}`
|
|
if (get().variants[key] === variant) return
|
|
|
|
set((state) => {
|
|
return {
|
|
variants: {
|
|
...state.variants,
|
|
[key]: variant,
|
|
},
|
|
}
|
|
})
|
|
},
|
|
|
|
getVariant: (model: ModelSelection) => {
|
|
const state = get()
|
|
const key = `${model.providerID}/${model.modelID}`
|
|
return state.variants[key]
|
|
},
|
|
|
|
clearVariant: (model: ModelSelection) => {
|
|
const key = `${model.providerID}/${model.modelID}`
|
|
if (!(key in get().variants)) return
|
|
|
|
set((state) => {
|
|
const newVariants = { ...state.variants }
|
|
delete newVariants[key]
|
|
return {
|
|
variants: newVariants,
|
|
}
|
|
})
|
|
},
|
|
}))
|