The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ModelSelection, ProjectId, ServerProvider } from "@t3tools/contracts"; |
| 2 | import type { UnifiedSettings } from "@t3tools/contracts/settings"; |
| 3 | import { msg } from "gt-react"; |
| 4 | |
| 5 | import { resolveEffectiveDefaultModelSelection } from "../../modelSelection"; |
| 6 | import { |
| 7 | resolveDefaultProviderModelSelection, |
| 8 | resolveProjectDefaultProviderModelSelection, |
| 9 | type ProviderInstanceEntry, |
| 10 | } from "../../providerInstances"; |
| 11 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 12 | |
| 13 | export interface DefaultModelSettingsPresentation { |
| 14 | readonly selection: ModelSelection | null; |
| 15 | /** Set when the saved model's provider cannot run; effort stays hidden until it can. */ |
| 16 | readonly providerNote: string | null; |
| 17 | } |
| 18 | |
| 19 | export function resolveDefaultModelSettingsPresentation( |
| 20 | input: { |
| 21 | readonly entries: ReadonlyArray<ProviderInstanceEntry>; |
| 22 | readonly providers: ReadonlyArray<ServerProvider>; |
| 23 | readonly settings: UnifiedSettings; |
| 24 | readonly projectId: ProjectId | null; |
| 25 | }, |
| 26 | resolve: MessageResolver = m, |
| 27 | ): DefaultModelSettingsPresentation { |
| 28 | const saved = input.settings.defaultModelSelection; |
| 29 | if (saved === null) { |
| 30 | const automatic = resolveProjectDefaultProviderModelSelection( |
| 31 | input.providers, |
| 32 | input.settings, |
| 33 | input.projectId, |
| 34 | ); |
| 35 | return { |
| 36 | selection: automatic |
| 37 | ? resolveEffectiveDefaultModelSelection(input.settings, input.providers, automatic) |
| 38 | : null, |
| 39 | providerNote: null, |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | const savedEntry = input.entries.find((entry) => entry.instanceId === saved.instanceId); |
| 44 | const resolved = resolveDefaultProviderModelSelection(input.providers, saved); |
| 45 | // Settings can turn a provider off before the server snapshot catches up, so check both. |
| 46 | if ( |
| 47 | !savedEntry?.enabled || |
| 48 | !savedEntry.isAvailable || |
| 49 | resolved?.instanceId !== saved.instanceId |
| 50 | ) { |
| 51 | return { selection: saved, providerNote: providerNote(savedEntry, resolve) }; |
| 52 | } |
| 53 | return { |
| 54 | selection: resolveEffectiveDefaultModelSelection(input.settings, input.providers, resolved), |
| 55 | providerNote: null, |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | function providerNote(entry: ProviderInstanceEntry | undefined, resolve: MessageResolver): string { |
| 60 | if (!entry) return resolve(msg("This provider is not available on this environment.")); |
| 61 | if (!entry.enabled) |
| 62 | return resolve(msg("{provider} is turned off.", { provider: entry.displayName })); |
| 63 | return resolve(msg("{provider} is unavailable.", { provider: entry.displayName })); |
| 64 | } |