Open History to see the last commit
1/**
2 * Map one LLM Gateway `/v1/models` row into the fields `model-meta.ts` needs.
3 * modelschemas has no `llmgateway` provider, so this catalog is the source.
4 */
5
6import type { ReasoningShape } from './reasoning-shapes.ts'
7
8export type LlmGatewayInput = 'text' | 'image' | 'audio' | 'video' | 'document'
9
10export type LlmGatewayFeature =
11 | 'streaming'
12 | 'tools'
13 | 'json_object'
14 | 'json_schema'
15 | 'reasoning'
16 | 'vision'
17
18const INPUT_ORDER: Array<LlmGatewayInput> = [
19 'text',
20 'image',
21 'document',
22 'audio',
23 'video',
24]
25
26const INPUT_ALIASES: Record<string, LlmGatewayInput> = {
27 text: 'text',
28 image: 'image',
29 audio: 'audio',
30 video: 'video',
31 pdf: 'document',
32 file: 'document',
33 document: 'document',
34}
35
36/** `none` is the gateway's off switch. The SDK level is `off`. */
37const EFFORT_LEVELS: Record<string, keyof ReasoningShape['levels']> = {
38 none: 'off',
39 minimal: 'minimal',
40 low: 'low',
41 medium: 'medium',
42 high: 'high',
43 xhigh: 'xhigh',
44 max: 'max',
45}
46
47const FEATURE_ORDER: Array<LlmGatewayFeature> = [
48 'streaming',
49 'tools',
50 'json_object',
51 'json_schema',
52 'reasoning',
53 'vision',
54]
55
56export interface LlmGatewayCatalogRow {
57 id?: unknown
58 architecture?: {
59 input_modalities?: unknown
60 output_modalities?: unknown
61 }
62 context_length?: unknown
63 max_output?: unknown
64 supported_parameters?: unknown
65 json_output?: unknown
66 structured_outputs?: unknown
67 providers?: unknown
68}
69
70export interface LlmGatewayModelMeta {
71 id: string
72 contextWindow?: number
73 maxCompletionTokens?: number
74 input: Array<LlmGatewayInput>
75 features: Array<LlmGatewayFeature>
76 reasoning?: ReasoningShape
77}
78
79interface ProviderRow {
80 tools?: unknown
81 reasoning?: unknown
82 reasoning_efforts?: unknown
83 vision?: unknown
84 streaming?: unknown
85 max_output?: unknown
86}
87
88function stringList(value: unknown): Array<string> {
89 if (!Array.isArray(value)) return []
90 return value.filter((item): item is string => typeof item === 'string')
91}
92
93function providersOf(row: LlmGatewayCatalogRow): Array<ProviderRow> {
94 if (!Array.isArray(row.providers)) return []
95 return row.providers.filter(
96 (provider): provider is ProviderRow =>
97 typeof provider === 'object' && provider !== null,
98 )
99}
100
101function positiveInt(value: unknown): number | undefined {
102 const n = typeof value === 'number' ? value : Number(value)
103 if (!Number.isInteger(n) || n <= 0) return undefined
104 return n
105}
106
107function inputOf(row: LlmGatewayCatalogRow): Array<LlmGatewayInput> {
108 const mapped = stringList(row.architecture?.input_modalities)
109 .map((modality) => INPUT_ALIASES[modality.toLowerCase()])
110 .filter((modality): modality is LlmGatewayInput => modality !== undefined)
111 if (!mapped.includes('text')) mapped.unshift('text')
112 return INPUT_ORDER.filter((modality) => mapped.includes(modality))
113}
114
115function reasoningOf(
116 providers: Array<ProviderRow>,
117): ReasoningShape | undefined {
118 const efforts = providers
119 .map((provider) => stringList(provider.reasoning_efforts))
120 .find((list) => list.length > 0)
121 if (!efforts) return undefined
122 const levels: ReasoningShape['levels'] = {}
123 for (const effort of efforts) {
124 const level = EFFORT_LEVELS[effort.toLowerCase()]
125 if (!level) continue
126 levels[level] = effort.toLowerCase() === 'none' ? 'none' : level
127 }
128 if (Object.keys(levels).length === 0) return undefined
129 return { levels, budget: false }
130}
131
132/**
133 * `null` when the row is not a chat model we emit: `custom`, a `provider/`
134 * id, or a model whose output is not text.
135 */
136export function catalogRowToMeta(
137 row: LlmGatewayCatalogRow,
138): LlmGatewayModelMeta | null {
139 if (typeof row.id !== 'string' || row.id.length === 0) return null
140 if (row.id === 'custom' || row.id.includes('/')) return null
141
142 const outputs = stringList(row.architecture?.output_modalities)
143 if (outputs.length > 0 && !outputs.includes('text')) return null
144
145 const providers = providersOf(row)
146 const params = new Set(stringList(row.supported_parameters))
147 const input = inputOf(row)
148 const reasoning = reasoningOf(providers)
149 const flags = new Set<LlmGatewayFeature>()
150 if (providers.some((provider) => provider.streaming === true)) {
151 flags.add('streaming')
152 }
153 if (
154 providers.some((provider) => provider.tools === true) ||
155 params.has('tools')
156 ) {
157 flags.add('tools')
158 }
159 if (row.json_output === true) flags.add('json_object')
160 if (row.structured_outputs === true) flags.add('json_schema')
161 if (reasoning || providers.some((provider) => provider.reasoning === true)) {
162 flags.add('reasoning')
163 }
164 if (
165 providers.some((provider) => provider.vision === true) ||
166 input.includes('image')
167 ) {
168 flags.add('vision')
169 }
170
171 const maxFromProvider = providers
172 .map((provider) => positiveInt(provider.max_output))
173 .find((value) => value !== undefined)
174
175 return {
176 id: row.id,
177 contextWindow: positiveInt(row.context_length),
178 maxCompletionTokens: positiveInt(row.max_output) ?? maxFromProvider,
179 input,
180 features: FEATURE_ORDER.filter((feature) => flags.has(feature)),
181 reasoning,
182 }
183}