mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor: unify model selection with the TUI
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.
This commit is contained in:
1 parent
3d143568c1
commit
65b72e55ae
82 files changed
+4418
-2624
No files matched your search
@@ -1,101 +1,40 @@
|
||||
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'
|
||||
import { logger } from '../utils/logger'
|
||||
|
||||
export const ModelSelectionSchema = z.object({
|
||||
providerID: z.string().min(1),
|
||||
modelID: z.string().min(1),
|
||||
})
|
||||
|
||||
export const ModelStateSchema = z.object({
|
||||
recent: z.array(ModelSelectionSchema).default([]),
|
||||
favorite: z.array(ModelSelectionSchema).default([]),
|
||||
variant: z.record(z.string(), z.string().optional()).default({}),
|
||||
}).passthrough()
|
||||
|
||||
export interface ModelSelectionRecord {
|
||||
providerID: string
|
||||
modelID: string
|
||||
}
|
||||
|
||||
export interface OpenCodeModelStateRecord {
|
||||
recent: ModelSelectionRecord[]
|
||||
favorite: ModelSelectionRecord[]
|
||||
variant: Record<string, string | undefined>
|
||||
}
|
||||
|
||||
export const MAX_RECENT_MODELS = 10
|
||||
|
||||
function createEmptyState(): OpenCodeModelStateRecord {
|
||||
return { recent: [], favorite: [], variant: {} }
|
||||
}
|
||||
|
||||
function toRecord(data: z.infer<typeof ModelStateSchema>): OpenCodeModelStateRecord {
|
||||
return { recent: data.recent, favorite: data.favorite, variant: data.variant }
|
||||
}
|
||||
|
||||
export async function readOpenCodeModelState(): Promise<OpenCodeModelStateRecord> {
|
||||
export async function readOpenCodeModelState(): Promise<ModelPreference> {
|
||||
const raw = await readJsonSafe<unknown>(getOpenCodeModelStatePath(), null)
|
||||
const parsed = ModelStateSchema.safeParse(raw)
|
||||
|
||||
if (!parsed.success) {
|
||||
if (raw !== null) {
|
||||
logger.warn('OpenCode model state file has invalid structure', parsed.error)
|
||||
}
|
||||
return createEmptyState()
|
||||
}
|
||||
|
||||
return toRecord(parsed.data)
|
||||
}
|
||||
|
||||
export function addRecentModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
const deduped = [
|
||||
model,
|
||||
...state.recent.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID),
|
||||
]
|
||||
return { ...state, recent: deduped.slice(0, MAX_RECENT_MODELS) }
|
||||
}
|
||||
|
||||
export function removeRecentModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
return {
|
||||
...state,
|
||||
recent: state.recent.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID),
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleFavoriteModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
const exists = state.favorite.some(
|
||||
m => m.providerID === model.providerID && m.modelID === model.modelID,
|
||||
)
|
||||
const favorite = exists
|
||||
? state.favorite.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID)
|
||||
: [...state.favorite, model]
|
||||
|
||||
return { ...state, favorite }
|
||||
return toModelPreference(decodeModelPreference(raw))
|
||||
}
|
||||
|
||||
export async function updateOpenCodeModelState(
|
||||
mutate: (state: OpenCodeModelStateRecord) => OpenCodeModelStateRecord,
|
||||
): Promise<OpenCodeModelStateRecord> {
|
||||
mutate: (state: ModelPreferenceDocument) => ModelPreferenceDocument,
|
||||
): Promise<ModelPreference> {
|
||||
const modelStatePath = getOpenCodeModelStatePath()
|
||||
|
||||
return withFileLock(modelStatePath, async () => {
|
||||
const raw = await readJsonSafe<unknown>(modelStatePath, {})
|
||||
const parsed = ModelStateSchema.safeParse(raw)
|
||||
const current = parsed.success ? toRecord(parsed.data) : createEmptyState()
|
||||
const next = mutate(current)
|
||||
const rawObject = raw && typeof raw === 'object' && !Array.isArray(raw)
|
||||
? raw as Record<string, unknown>
|
||||
: {}
|
||||
const decoded = decodeModelPreference(raw)
|
||||
const next = mutate(decoded)
|
||||
|
||||
await writeJsonAtomic(modelStatePath, {
|
||||
...rawObject,
|
||||
...decoded,
|
||||
recent: next.recent,
|
||||
favorite: next.favorite,
|
||||
variant: next.variant,
|
||||
})
|
||||
|
||||
return next
|
||||
return toModelPreference(next)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user