import type { ModelCapabilities, ModelSelection, ServerConfig as T3ServerConfig, } from "@t3tools/contracts"; import { buildProviderOptionSelectionsFromDescriptors, getProviderOptionDescriptors, } from "@t3tools/shared/model"; export type ModelOption = { readonly key: string; readonly label: string; readonly subtitle: string; readonly providerKey: string; readonly providerLabel: string; readonly providerDriver: string; readonly isDefault: boolean; readonly isLegacy: boolean; readonly capabilities: ModelCapabilities | null; readonly selection: ModelSelection; }; export type ProviderGroup = { readonly providerKey: string; readonly providerLabel: string; readonly models: ReadonlyArray; }; function providerDisplayLabel(provider: { readonly displayName?: string | undefined; readonly driver: string; readonly instanceId: string; }): string { if (provider.displayName) return provider.displayName; if (provider.driver === "codex") return "Codex"; if (provider.driver === "claudeAgent") return "Claude"; return provider.instanceId; } function normalizeSelectionOptions( selection: ModelSelection, capabilities: ModelCapabilities | null, ): ModelSelection { if (!capabilities) { return selection; } const options = buildProviderOptionSelectionsFromDescriptors( getProviderOptionDescriptors({ caps: capabilities, selections: selection.options, }), ); return options ? { ...selection, options } : { instanceId: selection.instanceId, model: selection.model, }; } /** * A stored model selection is only usable when its provider instance is * currently enabled, installed, and authenticated on the server. Returns the * selection unchanged when usable, otherwise `null` so callers fall through to * the server's default model. A missing config (environment offline) cannot be * validated, so stored selections pass through untouched. */ export function resolveSelectableModelSelection( config: T3ServerConfig | null | undefined, selection: ModelSelection | null, ): ModelSelection | null { if (!selection || !config) { return selection; } const provider = config.providers.find( (candidate) => candidate.instanceId === selection.instanceId, ); return provider && provider.enabled && provider.installed && provider.auth.status !== "unauthenticated" ? selection : null; } /** * Like resolveSelectableModelSelection, but additionally rejects legacy * models. Used for implicit defaults (stored draft, project last-used): a * new thread should never quietly start on a legacy model, so those fall * through to the provider's default instead. Explicit picks in the settings * sheet are unaffected. */ export function resolveDefaultableModelSelection( config: T3ServerConfig | null | undefined, selection: ModelSelection | null, ): ModelSelection | null { const usable = resolveSelectableModelSelection(config, selection); if (!usable || !config) { return usable; } const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId); const model = provider?.models.find((candidate) => candidate.slug === usable.model); return model?.isLegacy === true ? null : usable; } export function buildModelOptions( config: T3ServerConfig | null | undefined, fallbackModelSelection: ModelSelection | null, ): ReadonlyArray { const options = new Map(); for (const provider of config?.providers ?? []) { if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") { continue; } const providerLabel = providerDisplayLabel(provider); for (const model of provider.models) { const key = `${provider.instanceId}:${model.slug}`; options.set(key, { key, label: model.name, subtitle: providerLabel, providerKey: provider.instanceId, providerLabel, providerDriver: provider.driver, isDefault: model.isDefault === true, isLegacy: model.isLegacy === true, capabilities: model.capabilities, selection: normalizeSelectionOptions( { instanceId: provider.instanceId, model: model.slug, }, model.capabilities, ), }); } } if (fallbackModelSelection) { const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`; const existing = options.get(key); if (existing) { options.set(key, { ...existing, selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities), }); } else { const providerLabel = fallbackModelSelection.instanceId; options.set(key, { key, label: fallbackModelSelection.model, subtitle: providerLabel, providerKey: fallbackModelSelection.instanceId, providerLabel, providerDriver: fallbackModelSelection.instanceId, isDefault: false, isLegacy: false, capabilities: null, selection: fallbackModelSelection, }); } } return [...options.values()]; } export function groupByProvider(options: ReadonlyArray): ReadonlyArray { const groups = new Map(); for (const option of options) { const existing = groups.get(option.providerKey); if (existing) { existing.models.push(option); } else { groups.set(option.providerKey, { providerLabel: option.providerLabel, models: [option], }); } } return [...groups.entries()].map(([providerKey, group]) => ({ providerKey, providerLabel: group.providerLabel, models: group.models, })); }