The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | ModelCapabilities, |
| 3 | ModelSelection, |
| 4 | ProviderInstanceId, |
| 5 | ProviderOptionDescriptor, |
| 6 | ProviderOptionSelection, |
| 7 | ServerProviderModel, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { |
| 10 | buildProviderOptionSelectionsFromDescriptors, |
| 11 | getProviderOptionDescriptors, |
| 12 | withImplicitFastModeDefault, |
| 13 | } from "@t3tools/shared/model"; |
| 14 | |
| 15 | export function resolveProviderOptionDescriptors(input: { |
| 16 | readonly capabilities: ModelCapabilities | null | undefined; |
| 17 | readonly selections: ReadonlyArray<ProviderOptionSelection> | null | undefined; |
| 18 | }): ReadonlyArray<ProviderOptionDescriptor> { |
| 19 | if (!input.capabilities) { |
| 20 | return []; |
| 21 | } |
| 22 | return getProviderOptionDescriptors({ |
| 23 | caps: input.capabilities, |
| 24 | selections: withImplicitFastModeDefault(input.capabilities, input.selections), |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | /** Spells out the Fast off the picker shows, or a provider such as Cursor applies its own default. */ |
| 29 | export function withImplicitFastModeSelection( |
| 30 | selection: ModelSelection, |
| 31 | providers: ReadonlyArray<{ |
| 32 | readonly instanceId: ProviderInstanceId; |
| 33 | readonly models: ReadonlyArray<Pick<ServerProviderModel, "slug" | "capabilities">>; |
| 34 | }>, |
| 35 | ): ModelSelection { |
| 36 | const capabilities = providers |
| 37 | .find((provider) => provider.instanceId === selection.instanceId) |
| 38 | ?.models.find((model) => model.slug === selection.model)?.capabilities; |
| 39 | const options = capabilities |
| 40 | ? withImplicitFastModeDefault(capabilities, selection.options) |
| 41 | : selection.options; |
| 42 | return options === selection.options ? selection : { ...selection, options }; |
| 43 | } |
| 44 | |
| 45 | export function applyProviderOptionSelection( |
| 46 | descriptors: ReadonlyArray<ProviderOptionDescriptor>, |
| 47 | change: ProviderOptionSelection, |
| 48 | ): ReadonlyArray<ProviderOptionSelection> | null { |
| 49 | const descriptor = descriptors.find((candidate) => candidate.id === change.id); |
| 50 | if (!descriptor) { |
| 51 | return null; |
| 52 | } |
| 53 | if ( |
| 54 | (descriptor.type === "boolean" && typeof change.value !== "boolean") || |
| 55 | (descriptor.type === "select" && |
| 56 | (typeof change.value !== "string" || |
| 57 | !descriptor.options.some((option) => option.id === change.value))) |
| 58 | ) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | const nextDescriptors = descriptors.map((candidate) => |
| 63 | candidate.id === descriptor.id |
| 64 | ? { |
| 65 | ...candidate, |
| 66 | currentValue: change.value, |
| 67 | } |
| 68 | : candidate, |
| 69 | ) as ReadonlyArray<ProviderOptionDescriptor>; |
| 70 | |
| 71 | return buildProviderOptionSelectionsFromDescriptors(nextDescriptors) ?? []; |
| 72 | } |