Open History to see the last commit
1/**
2 * File-mutation helpers for `sync-provider-models.ts`.
3 *
4 * Kept here so the insert path (chat array + type maps) can be unit-tested
5 * without running the full OpenRouter fetch.
6 */
7
8type ArrayRef = '.name' | '.id'
9
10export function insertConstants(
11 content: string,
12 constants: Array<string>,
13): string {
14 const block = '\n' + constants.join('\n\n') + '\n'
15 const exportIndex = content.indexOf('\nexport ')
16 if (exportIndex === -1) {
17 return content + block
18 }
19 return content.slice(0, exportIndex) + block + content.slice(exportIndex)
20}
21
22/**
23 * Insert entries immediately AFTER the opening bracket. Each inserted line
24 * carries its own trailing comma so the existing body does not need a
25 * comma-guess. See the grok-4.5 single-line array breakage.
26 */
27function addToArray(
28 content: string,
29 arrayName: string,
30 entries: Array<string>,
31 arrayRef: string,
32): string {
33 const decl = `export const ${arrayName} = `
34 const declIndex = content.indexOf(decl)
35 const afterDecl = declIndex === -1 ? -1 : declIndex + decl.length
36 const bracket = afterDecl === -1 ? -1 : content.indexOf('[', afterDecl)
37 if (declIndex === -1 || bracket === -1 || bracket - afterDecl > 40) {
38 console.warn(` Warning: Could not find array '${arrayName}' in file`)
39 return content
40 }
41
42 const newEntries = entries
43 .map((constName) => ` ${constName}${arrayRef},`)
44 .join('\n')
45 const insertAt = bracket + 1
46 return `${content.slice(0, insertAt)}\n${newEntries}${content.slice(insertAt)}`
47}
48
49function addToTypeMap(
50 content: string,
51 typeName: string,
52 entries: Array<string>,
53): string {
54 const pattern = new RegExp(
55 `(export type ${typeName} = \\{[\\s\\S]*?)(\\n\\})`,
56 )
57 const match = pattern.exec(content)
58 if (!match) {
59 console.warn(` Warning: Could not find type map '${typeName}' in file`)
60 return content
61 }
62
63 const newEntries = entries.join('\n')
64 return content.replace(pattern, () => `${match[1]}\n${newEntries}${match[2]}`)
65}
66
67function addToObjectMap(
68 content: string,
69 mapName: string,
70 entries: Array<string>,
71): string {
72 const pattern = new RegExp(
73 `(const ${mapName}: Record<string, number> = \\{[\\s\\S]*?)(\\n\\})`,
74 )
75 const match = pattern.exec(content)
76 if (!match) {
77 console.warn(` Warning: Could not find object map '${mapName}' in file`)
78 return content
79 }
80
81 const newEntries = entries.join('\n')
82 return content.replace(pattern, () => `${match[1]}\n${newEntries}${match[2]}`)
83}
84
85/**
86 * Append entries to the runtime map that ends with `} satisfies <typeName>`.
87 * A file without that map is left as it is.
88 */
89function addToSatisfyingMap(
90 content: string,
91 typeName: string,
92 entries: Array<string>,
93): string {
94 // ponytail: found by its `satisfies` tail, so the sync config needs no map name.
95 const pattern = new RegExp(`\\n\\} satisfies ${typeName}\\b`)
96 return content.replace(pattern, (close) => `\n${entries.join('\n')}${close}`)
97}
98
99interface ChatModelInsert {
100 constName: string
101 providerOptionsEntry: string
102 hasMaxOutputTokens: boolean
103 /** Anthropic: add to the combined tools + output_config.format set. */
104 acceptsCombinedToolsAndSchema?: boolean
105}
106
107interface ChatModelCatalogInsertConfig {
108 chatArrayName: string
109 arrayRef: ArrayRef
110 providerOptionsTypeName: string
111 inputModalitiesTypeName: string
112 toolCapabilitiesTypeName?: string
113 maxOutputTokensMapName?: string
114 combinedToolsAndSchemaSetName?: string
115 providerOptionsIsMappedType: boolean
116}
117
118/**
119 * Write a new chat model into the catalog tables the adapter types read:
120 * the exported id array, provider-options map, input-modalities type map and
121 * its runtime map (when the file has one), tool-capabilities map, and
122 * (Anthropic) max-output-tokens object.
123 */
124export function applyChatModelCatalogInserts(
125 content: string,
126 config: ChatModelCatalogInsertConfig,
127 chatModels: Array<ChatModelInsert>,
128): string {
129 if (chatModels.length === 0) return content
130
131 let next = addToArray(
132 content,
133 config.chatArrayName,
134 chatModels.map(({ constName }) => constName),
135 config.arrayRef,
136 )
137
138 if (!config.providerOptionsIsMappedType) {
139 next = addToTypeMap(
140 next,
141 config.providerOptionsTypeName,
142 chatModels.map(
143 ({ constName, providerOptionsEntry }) =>
144 ` [${constName}${config.arrayRef}]: ${providerOptionsEntry}`,
145 ),
146 )
147 }
148
149 next = addToTypeMap(
150 next,
151 config.inputModalitiesTypeName,
152 chatModels.map(
153 ({ constName }) =>
154 ` [${constName}${config.arrayRef}]: typeof ${constName}.supports.input`,
155 ),
156 )
157 next = addToSatisfyingMap(
158 next,
159 config.inputModalitiesTypeName,
160 chatModels.map(
161 ({ constName }) =>
162 ` [${constName}${config.arrayRef}]: ${constName}.supports.input,`,
163 ),
164 )
165
166 if (config.toolCapabilitiesTypeName) {
167 next = addToTypeMap(
168 next,
169 config.toolCapabilitiesTypeName,
170 chatModels.map(
171 ({ constName }) =>
172 ` [${constName}${config.arrayRef}]: typeof ${constName}.supports.tools`,
173 ),
174 )
175 }
176
177 if (config.combinedToolsAndSchemaSetName) {
178 const combined = chatModels.filter(
179 ({ acceptsCombinedToolsAndSchema }) => acceptsCombinedToolsAndSchema,
180 )
181 if (combined.length > 0) {
182 next = addToArray(
183 next,
184 config.combinedToolsAndSchemaSetName,
185 combined.map(({ constName }) => constName),
186 config.arrayRef,
187 )
188 }
189 }
190
191 if (config.maxOutputTokensMapName) {
192 const maxOutputEntries = chatModels
193 .filter(({ hasMaxOutputTokens }) => hasMaxOutputTokens)
194 .map(
195 ({ constName }) =>
196 ` [${constName}${config.arrayRef}]: ${constName}.max_output_tokens,`,
197 )
198 if (maxOutputEntries.length > 0) {
199 next = addToObjectMap(
200 next,
201 config.maxOutputTokensMapName,
202 maxOutputEntries,
203 )
204 }
205 }
206
207 return next
208}