The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | DEFAULT_TEXT_GENERATION_MODEL, |
| 3 | DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER, |
| 4 | defaultInstanceIdForDriver, |
| 5 | type ModelSelection, |
| 6 | type ProjectId, |
| 7 | ProviderDriverKind, |
| 8 | ProviderInstanceId, |
| 9 | type ServerProvider, |
| 10 | type ServerSettings, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { |
| 13 | type CustomModelDefinition, |
| 14 | createModelSelection, |
| 15 | normalizeCustomModelSlug, |
| 16 | readCustomModelEntries, |
| 17 | resolveSelectableModel, |
| 18 | } from "@t3tools/shared/model"; |
| 19 | import { getComposerProviderState } from "./components/chat/composerProviderState"; |
| 20 | import { type ClientSettings, UnifiedSettings } from "@t3tools/contracts/settings"; |
| 21 | import { resolveModelPreferences } from "@t3tools/shared/serverSettings"; |
| 22 | import * as Arr from "effect/Array"; |
| 23 | import * as Equal from "effect/Equal"; |
| 24 | import * as Result from "effect/Result"; |
| 25 | import { |
| 26 | getDefaultServerModel, |
| 27 | getProviderModels, |
| 28 | hasRuntimeModelCatalog, |
| 29 | isRuntimeModelDefaultAlias, |
| 30 | resolveSelectableProvider, |
| 31 | } from "./providerModels"; |
| 32 | import { ModelEsque } from "./components/chat/providerIconUtils"; |
| 33 | import { |
| 34 | type ProviderInstanceEntry, |
| 35 | deriveProviderInstanceEntries, |
| 36 | NO_PROVIDER_MODEL_SELECTION, |
| 37 | resolveDefaultProviderModelSelection, |
| 38 | resolveProjectDefaultProviderModelSelection, |
| 39 | } from "./providerInstances"; |
| 40 | import { sortModelsForProviderInstance } from "./modelOrdering"; |
| 41 | import { resolveModelShortName } from "@t3tools/client-runtime/provider-clients"; |
| 42 | |
| 43 | export const MAX_CUSTOM_MODEL_LENGTH = 256; |
| 44 | const DEFAULT_TEXT_GENERATION_INSTANCE_ID = ProviderInstanceId.make("codex"); |
| 45 | |
| 46 | // Legacy per-driver buckets would leak custom-instance models into stock instances. |
| 47 | export function readInstanceCustomModels( |
| 48 | settings: UnifiedSettings, |
| 49 | instanceId: ProviderInstanceId, |
| 50 | driverKind: ProviderDriverKind, |
| 51 | ): ReadonlyArray<CustomModelDefinition> { |
| 52 | if (driverKind === "antigravity") return []; |
| 53 | const instance = settings.providerInstances?.[instanceId]; |
| 54 | const config = instance?.config; |
| 55 | if (config !== null && typeof config === "object") { |
| 56 | const value = (config as Record<string, unknown>).customModels; |
| 57 | if (Array.isArray(value)) { |
| 58 | return readCustomModelEntries(value); |
| 59 | } |
| 60 | } |
| 61 | return []; |
| 62 | } |
| 63 | |
| 64 | export interface AppModelOption { |
| 65 | slug: string; |
| 66 | name: string; |
| 67 | shortName?: string; |
| 68 | subProvider?: string; |
| 69 | aliases?: ReadonlyArray<string>; |
| 70 | badge?: "new"; |
| 71 | isCustom: boolean; |
| 72 | isDefault?: boolean; |
| 73 | isLegacy?: boolean; |
| 74 | isUnavailable?: boolean; |
| 75 | } |
| 76 | |
| 77 | function appendUnavailableDynamicModelSelection( |
| 78 | options: AppModelOption[], |
| 79 | rawModels: ReadonlyArray<ServerProvider["models"][number]>, |
| 80 | provider: ProviderDriverKind, |
| 81 | selectedModel: string | null | undefined, |
| 82 | hiddenModels: ReadonlyArray<string>, |
| 83 | ): AppModelOption[] { |
| 84 | if (!hasRuntimeModelCatalog(provider)) return options; |
| 85 | const slug = normalizeCustomModelSlug(selectedModel); |
| 86 | if (!slug) return options; |
| 87 | if (isRuntimeModelDefaultAlias(provider, slug)) return options; |
| 88 | |
| 89 | if (resolveSelectableModel(provider, slug, rawModels) !== null) return options; |
| 90 | if (hiddenModels.includes(slug)) return options; |
| 91 | if (options.some((option) => option.slug === slug)) return options; |
| 92 | |
| 93 | return [...options, { slug, name: slug, isCustom: false, isUnavailable: true }]; |
| 94 | } |
| 95 | |
| 96 | function toAppModelOption( |
| 97 | model: ServerProvider["models"][number], |
| 98 | driverKind: ProviderDriverKind, |
| 99 | ): AppModelOption { |
| 100 | const option: AppModelOption = { |
| 101 | slug: model.slug, |
| 102 | name: model.name, |
| 103 | isCustom: model.isCustom, |
| 104 | }; |
| 105 | const shortName = resolveModelShortName(model, driverKind); |
| 106 | if (shortName) option.shortName = shortName; |
| 107 | if (model.subProvider) option.subProvider = model.subProvider; |
| 108 | if (model.aliases) option.aliases = model.aliases; |
| 109 | if (model.badge) option.badge = model.badge; |
| 110 | if (model.isDefault) option.isDefault = true; |
| 111 | if (model.isLegacy) option.isLegacy = true; |
| 112 | return option; |
| 113 | } |
| 114 | |
| 115 | function readInstanceModelPreferences( |
| 116 | settings: UnifiedSettings, |
| 117 | instanceId: ProviderInstanceId, |
| 118 | ): { readonly hiddenModels: ReadonlyArray<string>; readonly modelOrder: ReadonlyArray<string> } { |
| 119 | return ( |
| 120 | settings.providerModelPreferences?.[instanceId] ?? { |
| 121 | hiddenModels: [], |
| 122 | modelOrder: [], |
| 123 | } |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | function applyInstanceModelPreferences( |
| 128 | options: ReadonlyArray<AppModelOption>, |
| 129 | preferences: { |
| 130 | readonly hiddenModels: ReadonlyArray<string>; |
| 131 | readonly modelOrder: ReadonlyArray<string>; |
| 132 | }, |
| 133 | ): AppModelOption[] { |
| 134 | const hiddenModels = new Set(preferences.hiddenModels); |
| 135 | return sortModelsForProviderInstance( |
| 136 | options.filter((option) => option.isCustom || !hiddenModels.has(option.slug)), |
| 137 | { modelOrder: preferences.modelOrder }, |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | function normalizeCustomModelEntries( |
| 142 | models: ReadonlyArray<CustomModelDefinition>, |
| 143 | builtInModelSlugs: ReadonlySet<string>, |
| 144 | ): CustomModelDefinition[] { |
| 145 | const normalizedModels: CustomModelDefinition[] = []; |
| 146 | const seen = new Set<string>(); |
| 147 | |
| 148 | for (const candidate of models) { |
| 149 | if ( |
| 150 | candidate.slug.length > MAX_CUSTOM_MODEL_LENGTH || |
| 151 | builtInModelSlugs.has(candidate.slug) || |
| 152 | seen.has(candidate.slug) |
| 153 | ) { |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | seen.add(candidate.slug); |
| 158 | normalizedModels.push(candidate); |
| 159 | } |
| 160 | |
| 161 | return normalizedModels; |
| 162 | } |
| 163 | |
| 164 | function getAppModelOptions( |
| 165 | settings: UnifiedSettings, |
| 166 | providers: ReadonlyArray<ServerProvider>, |
| 167 | provider: ProviderDriverKind, |
| 168 | selectedModel?: string | null, |
| 169 | ): AppModelOption[] { |
| 170 | const rawModels = getProviderModels(providers, provider); |
| 171 | // Server custom-model snapshots can lag removals; rebuild custom rows from settings. |
| 172 | const options: AppModelOption[] = rawModels |
| 173 | .filter((model) => !model.isCustom) |
| 174 | .map((model) => toAppModelOption(model, provider)); |
| 175 | const seen = new Set(options.map((option) => option.slug)); |
| 176 | const builtInModelSlugs = new Set( |
| 177 | Arr.filterMap(getProviderModels(providers, provider), (model) => |
| 178 | model.isCustom ? Result.failVoid : Result.succeed(model.slug), |
| 179 | ), |
| 180 | ); |
| 181 | |
| 182 | const defaultInstanceId = defaultInstanceIdForDriver(provider); |
| 183 | const customModels = readInstanceCustomModels(settings, defaultInstanceId, provider); |
| 184 | for (const entry of normalizeCustomModelEntries(customModels, builtInModelSlugs)) { |
| 185 | if (seen.has(entry.slug)) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | seen.add(entry.slug); |
| 190 | const shortName = resolveModelShortName(entry, provider); |
| 191 | options.push({ |
| 192 | slug: entry.slug, |
| 193 | name: entry.name, |
| 194 | isCustom: true, |
| 195 | ...(shortName ? { shortName } : {}), |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | const preferences = readInstanceModelPreferences(settings, defaultInstanceId); |
| 200 | return appendUnavailableDynamicModelSelection( |
| 201 | applyInstanceModelPreferences(options, preferences), |
| 202 | rawModels, |
| 203 | provider, |
| 204 | selectedModel, |
| 205 | preferences.hiddenModels, |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | export function getAppModelOptionsForInstance( |
| 210 | settings: UnifiedSettings, |
| 211 | entry: ProviderInstanceEntry, |
| 212 | selectedModel?: string | null, |
| 213 | ): AppModelOption[] { |
| 214 | const options: AppModelOption[] = entry.models |
| 215 | .filter((model) => !model.isCustom) |
| 216 | .map((model) => toAppModelOption(model, entry.driverKind)); |
| 217 | const seen = new Set(options.map((option) => option.slug)); |
| 218 | const builtInModelSlugs = new Set( |
| 219 | Arr.filterMap(entry.models, (model) => |
| 220 | model.isCustom ? Result.failVoid : Result.succeed(model.slug), |
| 221 | ), |
| 222 | ); |
| 223 | |
| 224 | const customModels = readInstanceCustomModels(settings, entry.instanceId, entry.driverKind); |
| 225 | for (const custom of normalizeCustomModelEntries(customModels, builtInModelSlugs)) { |
| 226 | if (seen.has(custom.slug)) { |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | seen.add(custom.slug); |
| 231 | const shortName = resolveModelShortName(custom, entry.driverKind); |
| 232 | options.push({ |
| 233 | slug: custom.slug, |
| 234 | name: custom.name, |
| 235 | isCustom: true, |
| 236 | ...(shortName ? { shortName } : {}), |
| 237 | }); |
| 238 | } |
| 239 | |
| 240 | const preferences = readInstanceModelPreferences(settings, entry.instanceId); |
| 241 | return appendUnavailableDynamicModelSelection( |
| 242 | applyInstanceModelPreferences(options, preferences), |
| 243 | entry.models, |
| 244 | entry.driverKind, |
| 245 | selectedModel, |
| 246 | preferences.hiddenModels, |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | export function resolveAppModelSelection( |
| 251 | provider: ProviderDriverKind, |
| 252 | settings: UnifiedSettings, |
| 253 | providers: ReadonlyArray<ServerProvider>, |
| 254 | selectedModel: string | null | undefined, |
| 255 | ): string { |
| 256 | const resolvedProvider = resolveSelectableProvider(providers, provider); |
| 257 | const options = getAppModelOptions(settings, providers, resolvedProvider, selectedModel); |
| 258 | return ( |
| 259 | resolveSelectableModel(resolvedProvider, selectedModel, options) ?? |
| 260 | getDefaultServerModel(providers, resolvedProvider) |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | export function resolveAppModelSelectionForInstance( |
| 265 | instanceId: ProviderInstanceId, |
| 266 | settings: UnifiedSettings, |
| 267 | providers: ReadonlyArray<ServerProvider>, |
| 268 | selectedModel: string | null | undefined, |
| 269 | resolutionOptions?: { readonly preserveUnavailableSelection?: boolean }, |
| 270 | ): string | null { |
| 271 | const entry = deriveProviderInstanceEntries(providers).find( |
| 272 | (candidate) => candidate.instanceId === instanceId, |
| 273 | ); |
| 274 | if (!entry) return null; |
| 275 | const options = getAppModelOptionsForInstance( |
| 276 | settings, |
| 277 | entry, |
| 278 | resolutionOptions?.preserveUnavailableSelection ? selectedModel : null, |
| 279 | ); |
| 280 | const resolvedSelection = resolveSelectableModel(entry.driverKind, selectedModel, options); |
| 281 | if (resolvedSelection) { |
| 282 | return resolvedSelection; |
| 283 | } |
| 284 | if (resolutionOptions?.preserveUnavailableSelection && hasRuntimeModelCatalog(entry.driverKind)) { |
| 285 | const unavailableSelection = normalizeCustomModelSlug(selectedModel); |
| 286 | const hiddenModels = readInstanceModelPreferences(settings, entry.instanceId).hiddenModels; |
| 287 | if ( |
| 288 | unavailableSelection && |
| 289 | !hiddenModels.includes(unavailableSelection) && |
| 290 | resolveSelectableModel(entry.driverKind, selectedModel, entry.models) === null && |
| 291 | !isRuntimeModelDefaultAlias(entry.driverKind, unavailableSelection) |
| 292 | ) { |
| 293 | return unavailableSelection; |
| 294 | } |
| 295 | } |
| 296 | return options.find((option) => option.isDefault)?.slug ?? options[0]?.slug ?? null; |
| 297 | } |
| 298 | |
| 299 | // New drafts carry the stored selection; capabilities resolve its raw options later. |
| 300 | export function resolveEffectiveDefaultModelSelection( |
| 301 | settings: UnifiedSettings, |
| 302 | providers: ReadonlyArray<ServerProvider>, |
| 303 | selection: ModelSelection, |
| 304 | ): ModelSelection { |
| 305 | const model = resolveAppModelSelectionForInstance( |
| 306 | selection.instanceId, |
| 307 | settings, |
| 308 | providers, |
| 309 | selection.model, |
| 310 | { preserveUnavailableSelection: true }, |
| 311 | ); |
| 312 | return model === null || model === selection.model ? selection : { ...selection, model }; |
| 313 | } |
| 314 | |
| 315 | export function defaultModelSelectionsAreMixed( |
| 316 | clientSettings: ClientSettings, |
| 317 | targets: ReadonlyArray<{ |
| 318 | projectId: ProjectId | null; |
| 319 | settings: ServerSettings; |
| 320 | providers: ReadonlyArray<ServerProvider>; |
| 321 | }>, |
| 322 | ): boolean { |
| 323 | const selections = targets.map(({ projectId, settings, providers }) => { |
| 324 | const stored = settings.defaultModelSelection; |
| 325 | const selection = |
| 326 | stored === null |
| 327 | ? resolveProjectDefaultProviderModelSelection(providers, settings, projectId) |
| 328 | : resolveDefaultProviderModelSelection(providers, stored); |
| 329 | return { |
| 330 | stored, |
| 331 | effective: selection |
| 332 | ? resolveEffectiveDefaultModelSelection( |
| 333 | { |
| 334 | ...clientSettings, |
| 335 | ...settings, |
| 336 | ...resolveModelPreferences(settings, clientSettings), |
| 337 | }, |
| 338 | providers, |
| 339 | selection, |
| 340 | ) |
| 341 | : null, |
| 342 | }; |
| 343 | }); |
| 344 | return selections.some((selection) => !Equal.equals(selection, selections[0])); |
| 345 | } |
| 346 | |
| 347 | export function getCustomModelOptionsByInstance( |
| 348 | settings: UnifiedSettings, |
| 349 | providers: ReadonlyArray<ServerProvider>, |
| 350 | selectedInstanceId?: ProviderInstanceId | null, |
| 351 | selectedModel?: string | null, |
| 352 | ): ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>> { |
| 353 | const out = new Map<ProviderInstanceId, ReadonlyArray<ModelEsque>>(); |
| 354 | for (const entry of deriveProviderInstanceEntries(providers)) { |
| 355 | out.set( |
| 356 | entry.instanceId, |
| 357 | getAppModelOptionsForInstance( |
| 358 | settings, |
| 359 | entry, |
| 360 | entry.instanceId === selectedInstanceId ? selectedModel : null, |
| 361 | ), |
| 362 | ); |
| 363 | } |
| 364 | return out; |
| 365 | } |
| 366 | |
| 367 | function getTextGenerationModelOptionsForInstance( |
| 368 | settings: UnifiedSettings, |
| 369 | entry: ProviderInstanceEntry, |
| 370 | selectedModel?: string | null, |
| 371 | ): AppModelOption[] { |
| 372 | const unsupported = new Set( |
| 373 | entry.models |
| 374 | .filter((model) => model.supportsTextGeneration === false) |
| 375 | .map((model) => model.slug), |
| 376 | ); |
| 377 | // Filter after custom entries are rebuilt so a display-name override cannot restore a model. |
| 378 | return getAppModelOptionsForInstance(settings, entry, selectedModel).filter( |
| 379 | (option) => !unsupported.has(option.slug), |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | export function getTextGenerationModelOptionsByInstance( |
| 384 | settings: UnifiedSettings, |
| 385 | providers: ReadonlyArray<ServerProvider>, |
| 386 | selectedInstanceId?: ProviderInstanceId | null, |
| 387 | selectedModel?: string | null, |
| 388 | ): ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>> { |
| 389 | return new Map( |
| 390 | deriveProviderInstanceEntries( |
| 391 | providers.filter((provider) => provider.supportsTextGeneration !== false), |
| 392 | ).map((entry) => [ |
| 393 | entry.instanceId, |
| 394 | getTextGenerationModelOptionsForInstance( |
| 395 | settings, |
| 396 | entry, |
| 397 | entry.instanceId === selectedInstanceId ? selectedModel : null, |
| 398 | ), |
| 399 | ]), |
| 400 | ); |
| 401 | } |
| 402 | |
| 403 | export function selectsPlanAgent(options: ModelSelection["options"]): boolean { |
| 404 | return options?.some((option) => option.id === "agent" && option.value === "plan") ?? false; |
| 405 | } |
| 406 | |
| 407 | export function resolveAppModelSelectionState( |
| 408 | settings: UnifiedSettings, |
| 409 | providers: ReadonlyArray<ServerProvider>, |
| 410 | ): ModelSelection { |
| 411 | const selection = settings.textGenerationModelSelection ?? { |
| 412 | instanceId: DEFAULT_TEXT_GENERATION_INSTANCE_ID, |
| 413 | model: DEFAULT_TEXT_GENERATION_MODEL, |
| 414 | }; |
| 415 | const supportedProviders = providers.filter( |
| 416 | (provider) => provider.supportsTextGeneration !== false, |
| 417 | ); |
| 418 | const entries = deriveProviderInstanceEntries(supportedProviders); |
| 419 | const selectedEntry = entries.find( |
| 420 | (entry) => entry.instanceId === selection.instanceId && entry.enabled && entry.isAvailable, |
| 421 | ); |
| 422 | const entry = |
| 423 | selectedEntry ?? entries.find((candidate) => candidate.enabled && candidate.isAvailable); |
| 424 | if (entry) { |
| 425 | const selectedModel = selectedEntry ? selection.model : null; |
| 426 | // Use the cheap generation default; the catalog default is the expensive chat flagship. |
| 427 | const options = getTextGenerationModelOptionsForInstance(settings, entry); |
| 428 | const model = |
| 429 | resolveSelectableModel(entry.driverKind, selectedModel, options) ?? |
| 430 | resolveSelectableModel( |
| 431 | entry.driverKind, |
| 432 | DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[entry.driverKind], |
| 433 | options, |
| 434 | ) ?? |
| 435 | options.find((option) => option.isDefault)?.slug ?? |
| 436 | options[0]?.slug ?? |
| 437 | entry.models.find((model) => model.supportsTextGeneration !== false)?.slug ?? |
| 438 | DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[entry.driverKind]; |
| 439 | if (!model) { |
| 440 | return createModelSelection(entry.instanceId, "", []); |
| 441 | } |
| 442 | const provider = entry.driverKind; |
| 443 | const { modelOptionsForDispatch } = getComposerProviderState({ |
| 444 | provider, |
| 445 | model, |
| 446 | models: entry.models, |
| 447 | modelOptions: selectedEntry ? selection.options : undefined, |
| 448 | includePlanAgent: selectsPlanAgent(selection.options), |
| 449 | }); |
| 450 | |
| 451 | return createModelSelection(entry.instanceId, model, modelOptionsForDispatch); |
| 452 | } |
| 453 | |
| 454 | return NO_PROVIDER_MODEL_SELECTION; |
| 455 | } |