The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ProviderInstanceId } from "@t3tools/contracts"; |
| 2 | |
| 3 | const MODEL_KEY_PREFIX = "model:"; |
| 4 | const LEGACY_SECTION_KEY_PREFIX = "legacy-models:"; |
| 5 | const SUB_PROVIDER_SECTION_KEY_PREFIX = "sub-provider:"; |
| 6 | |
| 7 | export function modelPickerModelKey(instanceId: ProviderInstanceId, slug: string): string { |
| 8 | return `${MODEL_KEY_PREFIX}${instanceId.length}:${instanceId}${slug}`; |
| 9 | } |
| 10 | |
| 11 | export function parseModelPickerModelKey( |
| 12 | key: string, |
| 13 | ): { instanceId: ProviderInstanceId; slug: string } | null { |
| 14 | if (!key.startsWith(MODEL_KEY_PREFIX)) { |
| 15 | return null; |
| 16 | } |
| 17 | const encoded = key.slice(MODEL_KEY_PREFIX.length); |
| 18 | const separatorIndex = encoded.indexOf(":"); |
| 19 | if (separatorIndex === -1) { |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | const instanceIdLengthText = encoded.slice(0, separatorIndex); |
| 24 | if (!/^\d+$/.test(instanceIdLengthText)) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | const instanceIdLength = Number(instanceIdLengthText); |
| 29 | const value = encoded.slice(separatorIndex + 1); |
| 30 | if (!Number.isSafeInteger(instanceIdLength) || instanceIdLength > value.length) { |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | return { |
| 35 | instanceId: value.slice(0, instanceIdLength) as ProviderInstanceId, |
| 36 | slug: value.slice(instanceIdLength), |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | export function modelPickerLegacySectionKey(instanceId: ProviderInstanceId): string { |
| 41 | return `${LEGACY_SECTION_KEY_PREFIX}${instanceId}`; |
| 42 | } |
| 43 | |
| 44 | export function parseModelPickerLegacySectionKey(key: string): ProviderInstanceId | null { |
| 45 | return key.startsWith(LEGACY_SECTION_KEY_PREFIX) |
| 46 | ? (key.slice(LEGACY_SECTION_KEY_PREFIX.length) as ProviderInstanceId) |
| 47 | : null; |
| 48 | } |
| 49 | |
| 50 | export function modelPickerSubProviderSectionKey( |
| 51 | instanceId: ProviderInstanceId, |
| 52 | subProvider: string, |
| 53 | ): string { |
| 54 | return `${SUB_PROVIDER_SECTION_KEY_PREFIX}${instanceId.length}:${instanceId}${subProvider}`; |
| 55 | } |