Files
opencode-manager/shared/src/opencode/modelRef.ts
T
Chris Scott 65b72e55ae 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.
2026-10-06 20:44:27 -04:00

46 lines
1.6 KiB
TypeScript

import type { ConfigEntry, ModelRef } from '@opencode/client'
type ConfigDocumentModel = Extract<ConfigEntry, { type: 'document' }>['info']['model']
export function parseOpenCodeModelRef(model: string): ModelRef | undefined {
const providerEnd = model.indexOf('/')
if (providerEnd <= 0) return undefined
const providerID = model.slice(0, providerEnd)
const variantStart = model.indexOf('#', providerEnd + 1)
const id = model.slice(providerEnd + 1, variantStart === -1 ? undefined : variantStart)
const variant = variantStart === -1 ? undefined : model.slice(variantStart + 1)
if (!id || providerID.includes('#') || (variant !== undefined && (!variant || variant.includes('#')))) {
return undefined
}
return { providerID, id, ...(variant ? { variant } : {}) }
}
export function formatOpenCodeModelRef(ref: ModelRef): string {
return ref.variant ? `${ref.providerID}/${ref.id}#${ref.variant}` : `${ref.providerID}/${ref.id}`
}
export function configModelRef(
model: string | { providerID: string; model: string; variant?: string } | undefined | null,
): ModelRef | undefined {
if (!model) return undefined
if (typeof model === 'string') return parseOpenCodeModelRef(model)
return {
providerID: model.providerID,
id: model.model,
...(model.variant ? { variant: model.variant } : {}),
}
}
export function selectConfiguredModelRef(entries: ConfigEntry[]): ModelRef | undefined {
const model = entries.reduce<ConfigDocumentModel>(
(current, entry) => (entry.type === 'document' && entry.info.model ? entry.info.model : current),
undefined,
)
return configModelRef(model)
}