Open History to see the last commit
1/**
2 * Shared OpenRouter id helpers for `convert-openrouter-models.ts` and
3 * `sync-provider-models.ts`.
4 *
5 * OpenRouter marks routing aliases with a leading `~`
6 * (`~anthropic/claude-haiku-latest`). The OpenRouter catalog keeps those
7 * ids so `chat({ model: '~anthropic/claude-haiku-latest' })` type-checks.
8 * Native provider sync skips them. The `~` is mapped to `_` only in the
9 * generated constant name so the file is valid JS.
10 */
11
12const CONST_NAME_RE = /^[A-Z_][A-Z0-9_]*$/
13
14export function isRoutingAlias(modelId: string): boolean {
15 return modelId.startsWith('~')
16}
17
18export function rejectRoutingAliases<T extends { id: string }>(
19 models: Array<T>,
20): Array<T> {
21 return models.filter((model) => !isRoutingAlias(model.id))
22}
23
24/**
25 * Native adapters only accept the provider's own ids. Anthropic's Messages
26 * API uses dashes in Claude version suffixes (`claude-haiku-4-5`), and
27 * OpenRouter uses dots (`claude-haiku-4.5`). The native modelschemas id is
28 * already dashed; this guards against a dotted one. Other providers keep
29 * the raw id.
30 */
31export function toNativeProviderId(rawId: string, provider: string): string {
32 if (provider === 'anthropic') {
33 return rawId.replaceAll('.', '-')
34 }
35 return rawId
36}
37
38export function toModelConstName(modelId: string): string {
39 const constName = modelId
40 .replaceAll('~', '_')
41 .replaceAll('/', '_')
42 .replaceAll('-', '_')
43 .replaceAll('.', '_')
44 .replaceAll(':', '_')
45 .toUpperCase()
46
47 if (!CONST_NAME_RE.test(constName)) {
48 throw new Error(
49 `Generated constant name is not a valid JS identifier: ${JSON.stringify(
50 constName,
51 )} (from model id ${JSON.stringify(modelId)}).`,
52 )
53 }
54
55 return constName
56}