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. OpenRouter uses dots
26 * in Claude version suffixes (`claude-haiku-4.5`, `claude-fable-5.1`);
27 * Anthropic's Messages API uses dashes (`claude-haiku-4-5`,
28 * `claude-fable-5-1`). Other providers keep the OpenRouter stripped id.
29 */
30export function toNativeProviderId(
31 strippedId: string,
32 provider: 'openai' | 'anthropic' | 'gemini' | 'grok',
33): string {
34 if (provider === 'anthropic') {
35 return strippedId.replaceAll('.', '-')
36 }
37 return strippedId
38}
39
40export function toModelConstName(modelId: string): string {
41 const constName = modelId
42 .replaceAll('~', '_')
43 .replaceAll('/', '_')
44 .replaceAll('-', '_')
45 .replaceAll('.', '_')
46 .replaceAll(':', '_')
47 .toUpperCase()
48
49 if (!CONST_NAME_RE.test(constName)) {
50 throw new Error(
51 `Generated constant name is not a valid JS identifier: ${JSON.stringify(
52 constName,
53 )} (from model id ${JSON.stringify(modelId)}).`,
54 )
55 }
56
57 return constName
58}