mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Align the manager's model selection with the OpenCode TUI: one model.json-backed preference store, one provider catalog, one ordering and default-ownership rule, and one set of availability, ref-formatting and variant helpers shared across chat, schedules, agents and the goal auditor. - Add a shared TUI-compatible model-preference module and make the backend model.json store match its format. - Consolidate the frontend provider catalog, availability check, model ref formatter and variant persistence into single owners. - Drive chat, schedule, agent and goal-auditor model lists from a shared useModelSections hook and buildModelSections, following TUI ordering. - Extract a shared ModelCombobox with a taller list, and list models the TUI way in ModelQuickSelect. - Add a shared isRecord util and model-selection comparison helpers.
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { z } from 'zod'
|
|
import { getOpenCodeModelStatePath } from '@opencode-manager/shared/config/env'
|
|
import {
|
|
decodeModelPreference,
|
|
toModelPreference,
|
|
type ModelPreference,
|
|
type ModelPreferenceDocument,
|
|
} from '@opencode-manager/shared/opencode'
|
|
import { readJsonSafe, withFileLock, writeJsonAtomic } from '../utils/atomic-json'
|
|
|
|
export const ModelSelectionSchema = z.object({
|
|
providerID: z.string().min(1),
|
|
modelID: z.string().min(1),
|
|
})
|
|
|
|
export async function readOpenCodeModelState(): Promise<ModelPreference> {
|
|
const raw = await readJsonSafe<unknown>(getOpenCodeModelStatePath(), null)
|
|
return toModelPreference(decodeModelPreference(raw))
|
|
}
|
|
|
|
export async function updateOpenCodeModelState(
|
|
mutate: (state: ModelPreferenceDocument) => ModelPreferenceDocument,
|
|
): Promise<ModelPreference> {
|
|
const modelStatePath = getOpenCodeModelStatePath()
|
|
|
|
return withFileLock(modelStatePath, async () => {
|
|
const raw = await readJsonSafe<unknown>(modelStatePath, {})
|
|
const decoded = decodeModelPreference(raw)
|
|
const next = mutate(decoded)
|
|
|
|
await writeJsonAtomic(modelStatePath, {
|
|
...decoded,
|
|
recent: next.recent,
|
|
favorite: next.favorite,
|
|
variant: next.variant,
|
|
})
|
|
|
|
return toModelPreference(next)
|
|
})
|
|
}
|