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
85interface ChatModelInsert {
86 constName: string
87 providerOptionsEntry: string
88 hasMaxOutputTokens: boolean
89 /** Anthropic: add to the combined tools + output_config.format set. */
90 acceptsCombinedToolsAndSchema?: boolean
91}
92
93interface ChatModelCatalogInsertConfig {
94 chatArrayName: string
95 arrayRef: ArrayRef
96 providerOptionsTypeName: string
97 inputModalitiesTypeName: string
98 toolCapabilitiesTypeName?: string
99 maxOutputTokensMapName?: string
100 combinedToolsAndSchemaSetName?: string
101 providerOptionsIsMappedType: boolean
102}
103
104/**
105 * Write a new chat model into the catalog tables the adapter types read:
106 * the exported id array, provider-options map, input-modalities map,
107 * tool-capabilities map, and (Anthropic) max-output-tokens object.
108 */
109export function applyChatModelCatalogInserts(
110 content: string,
111 config: ChatModelCatalogInsertConfig,
112 chatModels: Array<ChatModelInsert>,
113): string {
114 if (chatModels.length === 0) return content
115
116 let next = addToArray(
117 content,
118 config.chatArrayName,
119 chatModels.map(({ constName }) => constName),
120 config.arrayRef,
121 )
122
123 if (!config.providerOptionsIsMappedType) {
124 next = addToTypeMap(
125 next,
126 config.providerOptionsTypeName,
127 chatModels.map(
128 ({ constName, providerOptionsEntry }) =>
129 ` [${constName}${config.arrayRef}]: ${providerOptionsEntry}`,
130 ),
131 )
132 }
133
134 next = addToTypeMap(
135 next,
136 config.inputModalitiesTypeName,
137 chatModels.map(
138 ({ constName }) =>
139 ` [${constName}${config.arrayRef}]: typeof ${constName}.supports.input`,
140 ),
141 )
142
143 if (config.toolCapabilitiesTypeName) {
144 next = addToTypeMap(
145 next,
146 config.toolCapabilitiesTypeName,
147 chatModels.map(
148 ({ constName }) =>
149 ` [${constName}${config.arrayRef}]: typeof ${constName}.supports.tools`,
150 ),
151 )
152 }
153
154 if (config.combinedToolsAndSchemaSetName) {
155 const combined = chatModels.filter(
156 ({ acceptsCombinedToolsAndSchema }) => acceptsCombinedToolsAndSchema,
157 )
158 if (combined.length > 0) {
159 next = addToArray(
160 next,
161 config.combinedToolsAndSchemaSetName,
162 combined.map(({ constName }) => constName),
163 config.arrayRef,
164 )
165 }
166 }
167
168 if (config.maxOutputTokensMapName) {
169 const maxOutputEntries = chatModels
170 .filter(({ hasMaxOutputTokens }) => hasMaxOutputTokens)
171 .map(
172 ({ constName }) =>
173 ` [${constName}${config.arrayRef}]: ${constName}.max_output_tokens,`,
174 )
175 if (maxOutputEntries.length > 0) {
176 next = addToObjectMap(
177 next,
178 config.maxOutputTokensMapName,
179 maxOutputEntries,
180 )
181 }
182 }
183
184 return next
185}