generate-models.ts 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. #!/usr/bin/env node
  2. import { writeFileSync } from "fs";
  3. import { join, dirname } from "path";
  4. import { fileURLToPath } from "url";
  5. import {
  6. CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL,
  7. CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL,
  8. CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL,
  9. CLOUDFLARE_WORKERS_AI_BASE_URL,
  10. } from "../src/providers/cloudflare.ts";
  11. import type { AnthropicMessagesCompat, Api, KnownProvider, Model, OpenAICompletionsCompat } from "../src/types.ts";
  12. const __filename = fileURLToPath(import.meta.url);
  13. const __dirname = dirname(__filename);
  14. const packageRoot = join(__dirname, "..");
  15. interface ModelsDevModel {
  16. id: string;
  17. name: string;
  18. tool_call?: boolean;
  19. reasoning?: boolean;
  20. limit?: {
  21. context?: number;
  22. output?: number;
  23. };
  24. cost?: {
  25. input?: number;
  26. output?: number;
  27. cache_read?: number;
  28. cache_write?: number;
  29. };
  30. modalities?: {
  31. input?: string[];
  32. output?: string[];
  33. };
  34. provider?: {
  35. npm?: string;
  36. };
  37. }
  38. interface NvidiaNimModelListItem {
  39. id: string;
  40. }
  41. interface AiGatewayModel {
  42. id: string;
  43. name?: string;
  44. context_window?: number;
  45. max_tokens?: number;
  46. tags?: string[];
  47. pricing?: {
  48. input?: string | number;
  49. output?: string | number;
  50. input_cache_read?: string | number;
  51. input_cache_write?: string | number;
  52. };
  53. }
  54. const COPILOT_STATIC_HEADERS = {
  55. "User-Agent": "GitHubCopilotChat/0.35.0",
  56. "Editor-Version": "vscode/1.107.0",
  57. "Editor-Plugin-Version": "copilot-chat/0.35.0",
  58. "Copilot-Integration-Id": "vscode-chat",
  59. } as const;
  60. const KIMI_STATIC_HEADERS = {
  61. "User-Agent": "KimiCLI/1.5",
  62. } as const;
  63. const TOGETHER_BASE_URL = "https://api.together.ai/v1";
  64. const TOGETHER_BASE_COMPAT: OpenAICompletionsCompat = {
  65. supportsStore: false,
  66. supportsDeveloperRole: false,
  67. supportsReasoningEffort: false,
  68. maxTokensField: "max_tokens",
  69. supportsStrictMode: false,
  70. supportsLongCacheRetention: false,
  71. };
  72. const TOGETHER_TOGGLE_REASONING_COMPAT: OpenAICompletionsCompat = {
  73. ...TOGETHER_BASE_COMPAT,
  74. thinkingFormat: "together",
  75. };
  76. const TOGETHER_REASONING_EFFORT_COMPAT: OpenAICompletionsCompat = {
  77. ...TOGETHER_BASE_COMPAT,
  78. supportsReasoningEffort: true,
  79. thinkingFormat: "openai",
  80. };
  81. const TOGETHER_TOGGLE_REASONING_EFFORT_COMPAT: OpenAICompletionsCompat = {
  82. ...TOGETHER_TOGGLE_REASONING_COMPAT,
  83. supportsReasoningEffort: true,
  84. };
  85. const TOGETHER_REASONING_ONLY_MODELS = new Set([
  86. "deepseek-ai/DeepSeek-R1",
  87. "MiniMaxAI/MiniMax-M2.7",
  88. ]);
  89. const TOGETHER_REASONING_EFFORT_MODELS = new Set(["openai/gpt-oss-20b", "openai/gpt-oss-120b"]);
  90. const TOGETHER_TOGGLE_REASONING_EFFORT_MODELS = new Set(["deepseek-ai/DeepSeek-V4-Pro"]);
  91. const TOGETHER_FIXED_REASONING_LEVEL_MAP = {
  92. off: null,
  93. minimal: null,
  94. low: null,
  95. medium: null,
  96. } as const;
  97. const TOGETHER_REASONING_EFFORT_LEVEL_MAP = {
  98. off: null,
  99. minimal: null,
  100. } as const;
  101. const TOGETHER_DEEPSEEK_V4_THINKING_LEVEL_MAP = {
  102. minimal: null,
  103. low: null,
  104. medium: null,
  105. high: "high",
  106. xhigh: null,
  107. } as const;
  108. const TOGETHER_TOGGLE_REASONING_LEVEL_MAP = {
  109. minimal: null,
  110. low: null,
  111. medium: null,
  112. } as const;
  113. const AI_GATEWAY_MODELS_URL = "https://ai-gateway.vercel.sh/v1";
  114. const AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh";
  115. const NVIDIA_BASE_URL = "https://integrate.api.nvidia.com/v1";
  116. const NVIDIA_HEADERS = {
  117. "NVCF-POLL-SECONDS": "3600",
  118. } as const;
  119. const NVIDIA_OPENAI_COMPAT: OpenAICompletionsCompat = {
  120. supportsStore: false,
  121. supportsDeveloperRole: false,
  122. supportsReasoningEffort: false,
  123. maxTokensField: "max_tokens",
  124. supportsStrictMode: false,
  125. supportsLongCacheRetention: false,
  126. };
  127. const NVIDIA_NIM_UNSUPPORTED_MODELS = new Set([
  128. "abacusai/dracarys-llama-3.1-70b-instruct",
  129. "bytedance/seed-oss-36b-instruct",
  130. "deepseek-ai/deepseek-v4-flash",
  131. "deepseek-ai/deepseek-v4-pro",
  132. "google/gemma-2-2b-it",
  133. "google/gemma-3n-e2b-it",
  134. "google/gemma-3n-e4b-it",
  135. "google/gemma-4-31b-it",
  136. "meta/llama-3.2-1b-instruct",
  137. "meta/llama-4-maverick-17b-128e-instruct",
  138. "microsoft/phi-4-mini-instruct",
  139. "minimaxai/minimax-m2.7",
  140. "mistralai/mistral-nemotron",
  141. "nvidia/nemotron-mini-4b-instruct",
  142. "qwen/qwen3-next-80b-a3b-instruct",
  143. "qwen/qwen3.5-397b-a17b",
  144. "sarvamai/sarvam-m",
  145. "upstage/solar-10.7b-instruct",
  146. ]);
  147. const ZAI_TOOL_STREAM_UNSUPPORTED_MODELS = new Set(["glm-4.5", "glm-4.5-air", "glm-4.5-flash", "glm-4.5v"]);
  148. const EAGER_TOOL_INPUT_STREAMING_UNSUPPORTED_ANTHROPIC_MODELS = new Set([
  149. "github-copilot:claude-haiku-4.5",
  150. "github-copilot:claude-sonnet-4",
  151. "github-copilot:claude-sonnet-4.5",
  152. ]);
  153. const DEEPSEEK_V4_THINKING_LEVEL_MAP = {
  154. minimal: null,
  155. low: null,
  156. medium: null,
  157. high: "high",
  158. xhigh: "max",
  159. } as const;
  160. const ANT_LING_RING_THINKING_LEVEL_MAP = {
  161. off: null,
  162. minimal: null,
  163. low: null,
  164. medium: null,
  165. high: "high",
  166. xhigh: "xhigh",
  167. } as const;
  168. const OPENAI_RESPONSES_NONE_REASONING_MODELS = new Set([
  169. "gpt-5.1",
  170. "gpt-5.2",
  171. "gpt-5.3-codex",
  172. "gpt-5.4",
  173. "gpt-5.4-mini",
  174. "gpt-5.4-nano",
  175. "gpt-5.5",
  176. "gpt-5.6-sol",
  177. "gpt-5.6-terra",
  178. "gpt-5.6-luna",
  179. ]);
  180. const MODELS_DEV_OPENAI_UNSUPPORTED_MODEL_IDS = new Set(["gpt-5.6"]);
  181. function mergeThinkingLevelMap(model: Model<any>, map: NonNullable<Model<any>["thinkingLevelMap"]>): void {
  182. model.thinkingLevelMap = { ...model.thinkingLevelMap, ...map };
  183. }
  184. function getTogetherCompat(modelId: string, reasoning: boolean): OpenAICompletionsCompat {
  185. if (!reasoning) return TOGETHER_BASE_COMPAT;
  186. if (TOGETHER_REASONING_EFFORT_MODELS.has(modelId)) return TOGETHER_REASONING_EFFORT_COMPAT;
  187. if (TOGETHER_TOGGLE_REASONING_EFFORT_MODELS.has(modelId)) return TOGETHER_TOGGLE_REASONING_EFFORT_COMPAT;
  188. if (TOGETHER_REASONING_ONLY_MODELS.has(modelId)) return TOGETHER_BASE_COMPAT;
  189. return TOGETHER_TOGGLE_REASONING_COMPAT;
  190. }
  191. function getTogetherThinkingLevelMap(
  192. modelId: string,
  193. reasoning: boolean,
  194. ): NonNullable<Model<any>["thinkingLevelMap"]> | undefined {
  195. if (!reasoning) return undefined;
  196. if (TOGETHER_REASONING_EFFORT_MODELS.has(modelId)) return { ...TOGETHER_REASONING_EFFORT_LEVEL_MAP };
  197. if (TOGETHER_TOGGLE_REASONING_EFFORT_MODELS.has(modelId)) return { ...TOGETHER_DEEPSEEK_V4_THINKING_LEVEL_MAP };
  198. if (TOGETHER_REASONING_ONLY_MODELS.has(modelId)) return { ...TOGETHER_FIXED_REASONING_LEVEL_MAP };
  199. return { ...TOGETHER_TOGGLE_REASONING_LEVEL_MAP };
  200. }
  201. function supportsOpenAiXhigh(modelId: string): boolean {
  202. return (
  203. modelId.includes("gpt-5.2") ||
  204. modelId.includes("gpt-5.3") ||
  205. modelId.includes("gpt-5.4") ||
  206. modelId.includes("gpt-5.5") ||
  207. modelId.includes("gpt-5.6")
  208. );
  209. }
  210. function supportsOpenAiMax(model: Model<Api>): boolean {
  211. return (
  212. model.id.includes("gpt-5.6") &&
  213. (model.api === "openai-responses" ||
  214. model.api === "azure-openai-responses" ||
  215. model.api === "openai-codex-responses" ||
  216. model.api === "openai-completions")
  217. );
  218. }
  219. function isGoogleThinkingApi(model: Model<any>): boolean {
  220. return model.api === "google-generative-ai" || model.api === "google-vertex";
  221. }
  222. function isAnthropicAdaptiveThinkingModel(modelId: string): boolean {
  223. return (
  224. modelId.includes("opus-4-6") ||
  225. modelId.includes("opus-4.6") ||
  226. modelId.includes("opus-4-7") ||
  227. modelId.includes("opus-4.7") ||
  228. modelId.includes("opus-4-8") ||
  229. modelId.includes("opus-4.8") ||
  230. modelId.includes("sonnet-4-6") ||
  231. modelId.includes("sonnet-4.6") ||
  232. modelId.includes("fable-5")
  233. );
  234. }
  235. function isAnthropicTemperatureUnsupportedModel(modelId: string): boolean {
  236. const id = modelId.toLowerCase();
  237. return id.includes("opus-4-7") || id.includes("opus-4.7") || id.includes("opus-4-8") || id.includes("opus-4.8");
  238. }
  239. function mergeAnthropicMessagesCompat(model: Model<Api>, compat: AnthropicMessagesCompat): void {
  240. model.compat = { ...(model.compat as AnthropicMessagesCompat | undefined), ...compat };
  241. }
  242. function isGemini3ProModel(modelId: string): boolean {
  243. return /gemini-3(?:\.\d+)?-pro/.test(modelId.toLowerCase());
  244. }
  245. function isGemini3FlashModel(modelId: string): boolean {
  246. return /gemini-3(?:\.\d+)?-flash/.test(modelId.toLowerCase());
  247. }
  248. function isGemma4Model(modelId: string): boolean {
  249. return /gemma-?4/.test(modelId.toLowerCase());
  250. }
  251. function applyThinkingLevelMetadata(model: Model<any>): void {
  252. if (
  253. (model.api === "openai-responses" || model.api === "azure-openai-responses") &&
  254. model.id.startsWith("gpt-5")
  255. ) {
  256. mergeThinkingLevelMap(model, { off: null });
  257. }
  258. if (model.provider === "github-copilot" && model.id.startsWith("gpt-5")) {
  259. mergeThinkingLevelMap(model, { minimal: "low" });
  260. }
  261. if (
  262. model.api === "openai-responses" &&
  263. model.provider === "openai" &&
  264. OPENAI_RESPONSES_NONE_REASONING_MODELS.has(model.id)
  265. ) {
  266. mergeThinkingLevelMap(model, { off: "none" });
  267. }
  268. if (supportsOpenAiXhigh(model.id)) {
  269. mergeThinkingLevelMap(model, { xhigh: "xhigh" });
  270. }
  271. if (supportsOpenAiMax(model)) {
  272. mergeThinkingLevelMap(model, { max: "max" });
  273. }
  274. if (model.provider === "openai" && model.id === "gpt-5.5") {
  275. mergeThinkingLevelMap(model, { minimal: null });
  276. }
  277. if (model.id.endsWith("gpt-5.5-pro")) {
  278. mergeThinkingLevelMap(model, { off: null, minimal: null, low: null });
  279. }
  280. if (model.id.includes("opus-4-6") || model.id.includes("opus-4.6")) {
  281. mergeThinkingLevelMap(model, { xhigh: "max" });
  282. }
  283. if (
  284. model.id.includes("opus-4-7") ||
  285. model.id.includes("opus-4.7") ||
  286. model.id.includes("opus-4-8") ||
  287. model.id.includes("opus-4.8")
  288. ) {
  289. mergeThinkingLevelMap(model, { xhigh: "xhigh" });
  290. }
  291. if (
  292. (model.api === "anthropic-messages" || model.api === "bedrock-converse-stream") &&
  293. model.id.includes("fable-5")
  294. ) {
  295. mergeThinkingLevelMap(model, { xhigh: "xhigh" });
  296. }
  297. if (model.api === "anthropic-messages" && isAnthropicAdaptiveThinkingModel(model.id)) {
  298. mergeAnthropicMessagesCompat(model, { forceAdaptiveThinking: true });
  299. }
  300. if (model.api === "anthropic-messages" && isAnthropicTemperatureUnsupportedModel(model.id)) {
  301. mergeAnthropicMessagesCompat(model, { supportsTemperature: false });
  302. }
  303. if (model.api === "openai-completions" && model.id.includes("deepseek-v4")) {
  304. mergeThinkingLevelMap(
  305. model,
  306. model.provider === "openrouter"
  307. ? { ...DEEPSEEK_V4_THINKING_LEVEL_MAP, xhigh: "xhigh" }
  308. : DEEPSEEK_V4_THINKING_LEVEL_MAP,
  309. );
  310. }
  311. if (isGoogleThinkingApi(model) && isGemini3ProModel(model.id)) {
  312. mergeThinkingLevelMap(model, { off: null, minimal: null, low: "LOW", medium: null, high: "HIGH" });
  313. }
  314. if (isGoogleThinkingApi(model) && isGemini3FlashModel(model.id)) {
  315. mergeThinkingLevelMap(model, { off: null });
  316. }
  317. if (isGoogleThinkingApi(model) && isGemma4Model(model.id)) {
  318. mergeThinkingLevelMap(model, { off: null, minimal: "MINIMAL", low: null, medium: null, high: "HIGH" });
  319. }
  320. if (model.provider === "groq" && model.id === "qwen/qwen3-32b") {
  321. mergeThinkingLevelMap(model, { minimal: null, low: null, medium: null, high: "default" });
  322. }
  323. if (model.provider === "openai-codex" && supportsOpenAiXhigh(model.id)) {
  324. mergeThinkingLevelMap(model, { minimal: "low" });
  325. }
  326. if (model.provider === "openrouter" && model.id.startsWith("inception/mercury-2")) {
  327. // Mercury 2 in instant mode (reasoning_effort: "none") disables tool calling.
  328. // Mark "off" unsupported so the openai-completions provider omits the reasoning param
  329. // instead of defaulting to {reasoning:{effort:"none"}} (see openai-completions.ts:575).
  330. // Pi's low/medium/high pass through verbatim; OpenRouter normalizes to Mercury's vocabulary.
  331. mergeThinkingLevelMap(model, { off: null });
  332. }
  333. if (model.provider === "opencode-go" && model.id === "kimi-k2.6") {
  334. // OpenCode Go exposes Kimi K2.6 thinking as on/off, not distinct effort tiers.
  335. mergeThinkingLevelMap(model, { minimal: null, low: null, medium: null });
  336. }
  337. if (model.provider === "opencode" && model.id === "grok-build-0.1") {
  338. // OpenCode Zen Grok Build reasons by default but rejects explicit reasoningEffort.
  339. mergeThinkingLevelMap(model, { off: null, minimal: null, low: null, medium: null });
  340. }
  341. if (model.provider === "ant-ling" && model.reasoning) {
  342. // Ring reasons by default. Only high/xhigh have documented explicit effort controls.
  343. mergeThinkingLevelMap(model, ANT_LING_RING_THINKING_LEVEL_MAP);
  344. }
  345. }
  346. function getAnthropicMessagesCompat(provider: string, modelId: string): AnthropicMessagesCompat | undefined {
  347. const compat: AnthropicMessagesCompat = {};
  348. if (EAGER_TOOL_INPUT_STREAMING_UNSUPPORTED_ANTHROPIC_MODELS.has(`${provider}:${modelId}`)) {
  349. compat.supportsEagerToolInputStreaming = false;
  350. }
  351. if (provider === "xiaomi" || provider.startsWith("xiaomi-token-plan-")) {
  352. compat.allowEmptySignature = true;
  353. }
  354. return Object.keys(compat).length > 0 ? compat : undefined;
  355. }
  356. function getBedrockBaseUrl(modelId: string): string {
  357. return modelId.startsWith("eu.")
  358. ? "https://bedrock-runtime.eu-central-1.amazonaws.com"
  359. : "https://bedrock-runtime.us-east-1.amazonaws.com";
  360. }
  361. function normalizeNvidiaModelId(modelId: string): string {
  362. return modelId.toLowerCase().replaceAll("_", ".");
  363. }
  364. async function fetchNvidiaNimModelIds(): Promise<Map<string, string>> {
  365. try {
  366. console.log("Fetching models from NVIDIA NIM API...");
  367. const response = await fetch(`${NVIDIA_BASE_URL}/models`);
  368. const data = (await response.json()) as { data?: NvidiaNimModelListItem[] };
  369. const modelIds = new Map<string, string>();
  370. for (const model of data.data ?? []) {
  371. modelIds.set(model.id, model.id);
  372. modelIds.set(normalizeNvidiaModelId(model.id), model.id);
  373. }
  374. console.log(`Fetched ${data.data?.length ?? 0} model IDs from NVIDIA NIM`);
  375. return modelIds;
  376. } catch (error) {
  377. console.error("Failed to fetch NVIDIA NIM models:", error);
  378. return new Map();
  379. }
  380. }
  381. async function fetchOpenRouterModels(): Promise<Model<any>[]> {
  382. try {
  383. console.log("Fetching models from OpenRouter API...");
  384. const response = await fetch("https://openrouter.ai/api/v1/models");
  385. const data = await response.json();
  386. const models: Model<any>[] = [];
  387. for (const model of data.data) {
  388. // Only include models that support tools
  389. if (!model.supported_parameters?.includes("tools")) continue;
  390. // Parse provider from model ID
  391. let provider: KnownProvider = "openrouter";
  392. let modelKey = model.id;
  393. modelKey = model.id; // Keep full ID for OpenRouter
  394. // Parse input modalities
  395. const input: ("text" | "image")[] = ["text"];
  396. if (model.architecture?.modality?.includes("image")) {
  397. input.push("image");
  398. }
  399. // Convert pricing from $/token to $/million tokens
  400. const inputCost = parseFloat(model.pricing?.prompt || "0") * 1_000_000;
  401. const outputCost = parseFloat(model.pricing?.completion || "0") * 1_000_000;
  402. const cacheReadCost = parseFloat(model.pricing?.input_cache_read || "0") * 1_000_000;
  403. const cacheWriteCost = parseFloat(model.pricing?.input_cache_write || "0") * 1_000_000;
  404. const normalizedModel: Model<any> = {
  405. id: modelKey,
  406. name: model.name,
  407. api: "openai-completions",
  408. baseUrl: "https://openrouter.ai/api/v1",
  409. provider,
  410. reasoning: model.supported_parameters?.includes("reasoning") || false,
  411. input,
  412. cost: {
  413. input: inputCost,
  414. output: outputCost,
  415. cacheRead: cacheReadCost,
  416. cacheWrite: cacheWriteCost,
  417. },
  418. contextWindow: model.context_length || 4096,
  419. maxTokens: model.top_provider?.max_completion_tokens || 4096,
  420. };
  421. models.push(normalizedModel);
  422. }
  423. console.log(`Fetched ${models.length} tool-capable models from OpenRouter`);
  424. return models;
  425. } catch (error) {
  426. console.error("Failed to fetch OpenRouter models:", error);
  427. return [];
  428. }
  429. }
  430. async function fetchAiGatewayModels(): Promise<Model<any>[]> {
  431. try {
  432. console.log("Fetching models from Vercel AI Gateway API...");
  433. const response = await fetch(`${AI_GATEWAY_MODELS_URL}/models`);
  434. const data = await response.json();
  435. const models: Model<any>[] = [];
  436. const toNumber = (value: string | number | undefined): number => {
  437. if (typeof value === "number") {
  438. return Number.isFinite(value) ? value : 0;
  439. }
  440. const parsed = parseFloat(value ?? "0");
  441. return Number.isFinite(parsed) ? parsed : 0;
  442. };
  443. const items = Array.isArray(data.data) ? (data.data as AiGatewayModel[]) : [];
  444. for (const model of items) {
  445. const tags = Array.isArray(model.tags) ? model.tags : [];
  446. // Only include models that support tools
  447. if (!tags.includes("tool-use")) continue;
  448. const input: ("text" | "image")[] = ["text"];
  449. if (tags.includes("vision")) {
  450. input.push("image");
  451. }
  452. const inputCost = toNumber(model.pricing?.input) * 1_000_000;
  453. const outputCost = toNumber(model.pricing?.output) * 1_000_000;
  454. const cacheReadCost = toNumber(model.pricing?.input_cache_read) * 1_000_000;
  455. const cacheWriteCost = toNumber(model.pricing?.input_cache_write) * 1_000_000;
  456. models.push({
  457. id: model.id,
  458. name: model.name || model.id,
  459. api: "anthropic-messages",
  460. baseUrl: AI_GATEWAY_BASE_URL,
  461. provider: "vercel-ai-gateway",
  462. reasoning: tags.includes("reasoning"),
  463. input,
  464. cost: {
  465. input: inputCost,
  466. output: outputCost,
  467. cacheRead: cacheReadCost,
  468. cacheWrite: cacheWriteCost,
  469. },
  470. contextWindow: model.context_window || 4096,
  471. maxTokens: model.max_tokens || 4096,
  472. });
  473. }
  474. console.log(`Fetched ${models.length} tool-capable models from Vercel AI Gateway`);
  475. return models;
  476. } catch (error) {
  477. console.error("Failed to fetch Vercel AI Gateway models:", error);
  478. return [];
  479. }
  480. }
  481. async function loadModelsDevData(): Promise<Model<any>[]> {
  482. try {
  483. console.log("Fetching models from models.dev API...");
  484. const response = await fetch("https://models.dev/api.json");
  485. const data = await response.json();
  486. const models: Model<any>[] = [];
  487. const nvidiaNimModelIds = data.nvidia?.models ? await fetchNvidiaNimModelIds() : new Map<string, string>();
  488. // Process Amazon Bedrock models
  489. if (data["amazon-bedrock"]?.models) {
  490. for (const [modelId, model] of Object.entries(data["amazon-bedrock"].models)) {
  491. const m = model as ModelsDevModel;
  492. if (m.tool_call !== true) continue;
  493. let id = modelId;
  494. if (id.startsWith("ai21.jamba")) {
  495. // These models doesn't support tool use in streaming mode
  496. continue;
  497. }
  498. if (id.startsWith("mistral.mistral-7b-instruct-v0")) {
  499. // These models doesn't support system messages
  500. continue;
  501. }
  502. models.push({
  503. id,
  504. name: m.name || id,
  505. api: "bedrock-converse-stream" as const,
  506. provider: "amazon-bedrock" as const,
  507. baseUrl: getBedrockBaseUrl(id),
  508. reasoning: m.reasoning === true,
  509. input: (m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"]) as ("text" | "image")[],
  510. cost: {
  511. input: m.cost?.input || 0,
  512. output: m.cost?.output || 0,
  513. cacheRead: m.cost?.cache_read || 0,
  514. cacheWrite: m.cost?.cache_write || 0,
  515. },
  516. contextWindow: m.limit?.context || 4096,
  517. maxTokens: m.limit?.output || 4096,
  518. });
  519. }
  520. }
  521. // Process Anthropic models
  522. if (data.anthropic?.models) {
  523. for (const [modelId, model] of Object.entries(data.anthropic.models)) {
  524. const m = model as ModelsDevModel;
  525. if (m.tool_call !== true) continue;
  526. models.push({
  527. id: modelId,
  528. name: m.name || modelId,
  529. api: "anthropic-messages",
  530. provider: "anthropic",
  531. baseUrl: "https://api.anthropic.com",
  532. reasoning: m.reasoning === true,
  533. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  534. cost: {
  535. input: m.cost?.input || 0,
  536. output: m.cost?.output || 0,
  537. cacheRead: m.cost?.cache_read || 0,
  538. cacheWrite: m.cost?.cache_write || 0,
  539. },
  540. contextWindow: m.limit?.context || 4096,
  541. maxTokens: m.limit?.output || 4096,
  542. });
  543. }
  544. }
  545. // Process Google models
  546. if (data.google?.models) {
  547. for (const [modelId, model] of Object.entries(data.google.models)) {
  548. const m = model as ModelsDevModel;
  549. if (m.tool_call !== true) continue;
  550. models.push({
  551. id: modelId,
  552. name: m.name || modelId,
  553. api: "google-generative-ai",
  554. provider: "google",
  555. baseUrl: "https://generativelanguage.googleapis.com/v1beta",
  556. reasoning: m.reasoning === true,
  557. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  558. cost: {
  559. input: m.cost?.input || 0,
  560. output: m.cost?.output || 0,
  561. cacheRead: m.cost?.cache_read || 0,
  562. cacheWrite: m.cost?.cache_write || 0,
  563. },
  564. contextWindow: m.limit?.context || 4096,
  565. maxTokens: m.limit?.output || 4096,
  566. });
  567. }
  568. }
  569. // Process OpenAI models
  570. if (data.openai?.models) {
  571. for (const [modelId, model] of Object.entries(data.openai.models)) {
  572. const m = model as ModelsDevModel;
  573. if (m.tool_call !== true || MODELS_DEV_OPENAI_UNSUPPORTED_MODEL_IDS.has(modelId)) continue;
  574. models.push({
  575. id: modelId,
  576. name: m.name || modelId,
  577. api: "openai-responses",
  578. provider: "openai",
  579. baseUrl: "https://api.openai.com/v1",
  580. reasoning: m.reasoning === true,
  581. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  582. cost: {
  583. input: m.cost?.input || 0,
  584. output: m.cost?.output || 0,
  585. cacheRead: m.cost?.cache_read || 0,
  586. cacheWrite: m.cost?.cache_write || 0,
  587. },
  588. contextWindow: m.limit?.context || 4096,
  589. maxTokens: m.limit?.output || 4096,
  590. });
  591. }
  592. }
  593. // Process Groq models
  594. if (data.groq?.models) {
  595. for (const [modelId, model] of Object.entries(data.groq.models)) {
  596. const m = model as ModelsDevModel;
  597. if (m.tool_call !== true) continue;
  598. models.push({
  599. id: modelId,
  600. name: m.name || modelId,
  601. api: "openai-completions",
  602. provider: "groq",
  603. baseUrl: "https://api.groq.com/openai/v1",
  604. reasoning: m.reasoning === true,
  605. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  606. cost: {
  607. input: m.cost?.input || 0,
  608. output: m.cost?.output || 0,
  609. cacheRead: m.cost?.cache_read || 0,
  610. cacheWrite: m.cost?.cache_write || 0,
  611. },
  612. contextWindow: m.limit?.context || 4096,
  613. maxTokens: m.limit?.output || 4096,
  614. });
  615. }
  616. }
  617. // Process Cerebras models
  618. if (data.cerebras?.models) {
  619. for (const [modelId, model] of Object.entries(data.cerebras.models)) {
  620. const m = model as ModelsDevModel;
  621. if (m.tool_call !== true) continue;
  622. models.push({
  623. id: modelId,
  624. name: m.name || modelId,
  625. api: "openai-completions",
  626. provider: "cerebras",
  627. baseUrl: "https://api.cerebras.ai/v1",
  628. reasoning: m.reasoning === true,
  629. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  630. cost: {
  631. input: m.cost?.input || 0,
  632. output: m.cost?.output || 0,
  633. cacheRead: m.cost?.cache_read || 0,
  634. cacheWrite: m.cost?.cache_write || 0,
  635. },
  636. contextWindow: m.limit?.context || 4096,
  637. maxTokens: m.limit?.output || 4096,
  638. });
  639. }
  640. }
  641. // Process Cloudflare Workers AI models
  642. if (data["cloudflare-workers-ai"]?.models) {
  643. for (const [modelId, model] of Object.entries(data["cloudflare-workers-ai"].models)) {
  644. const m = model as ModelsDevModel;
  645. if (m.tool_call !== true) continue;
  646. models.push({
  647. id: modelId,
  648. name: m.name || modelId,
  649. api: "openai-completions",
  650. provider: "cloudflare-workers-ai",
  651. baseUrl: CLOUDFLARE_WORKERS_AI_BASE_URL,
  652. reasoning: m.reasoning === true,
  653. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  654. cost: {
  655. input: m.cost?.input || 0,
  656. output: m.cost?.output || 0,
  657. cacheRead: m.cost?.cache_read || 0,
  658. cacheWrite: m.cost?.cache_write || 0,
  659. },
  660. contextWindow: m.limit?.context || 4096,
  661. maxTokens: m.limit?.output || 4096,
  662. compat: { sendSessionAffinityHeaders: true },
  663. });
  664. }
  665. }
  666. // Process Cloudflare AI Gateway models
  667. if (data["cloudflare-ai-gateway"]?.models) {
  668. for (const [prefixedId, model] of Object.entries(data["cloudflare-ai-gateway"].models)) {
  669. const m = model as ModelsDevModel;
  670. if (m.tool_call !== true) continue;
  671. const slashIdx = prefixedId.indexOf("/");
  672. if (slashIdx === -1) continue;
  673. const upstream = prefixedId.slice(0, slashIdx);
  674. const nativeId = prefixedId.slice(slashIdx + 1);
  675. let api: "anthropic-messages" | "openai-completions" | "openai-responses";
  676. let baseUrl: string;
  677. let id: string;
  678. if (upstream === "openai") {
  679. api = "openai-responses";
  680. baseUrl = CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL;
  681. id = nativeId;
  682. } else if (upstream === "anthropic") {
  683. api = "anthropic-messages";
  684. baseUrl = CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL;
  685. id = nativeId;
  686. } else if (upstream === "workers-ai") {
  687. api = "openai-completions";
  688. baseUrl = CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL;
  689. id = prefixedId;
  690. } else {
  691. continue;
  692. }
  693. // workers-ai/* through the gateway forwards x-session-affinity to
  694. // the underlying Workers AI runtime for prefix-cache routing.
  695. const compat = upstream === "workers-ai" ? { sendSessionAffinityHeaders: true } : undefined;
  696. models.push({
  697. id,
  698. name: m.name || id,
  699. api,
  700. provider: "cloudflare-ai-gateway",
  701. baseUrl,
  702. reasoning: m.reasoning === true,
  703. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  704. cost: {
  705. input: m.cost?.input || 0,
  706. output: m.cost?.output || 0,
  707. cacheRead: m.cost?.cache_read || 0,
  708. cacheWrite: m.cost?.cache_write || 0,
  709. },
  710. contextWindow: m.limit?.context || 4096,
  711. maxTokens: m.limit?.output || 4096,
  712. ...(compat ? { compat } : {}),
  713. });
  714. }
  715. }
  716. // Process xAi models
  717. if (data.xai?.models) {
  718. for (const [modelId, model] of Object.entries(data.xai.models)) {
  719. const m = model as ModelsDevModel;
  720. if (m.tool_call !== true) continue;
  721. models.push({
  722. id: modelId,
  723. name: m.name || modelId,
  724. api: "openai-completions",
  725. provider: "xai",
  726. baseUrl: "https://api.x.ai/v1",
  727. reasoning: m.reasoning === true,
  728. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  729. cost: {
  730. input: m.cost?.input || 0,
  731. output: m.cost?.output || 0,
  732. cacheRead: m.cost?.cache_read || 0,
  733. cacheWrite: m.cost?.cache_write || 0,
  734. },
  735. contextWindow: m.limit?.context || 4096,
  736. maxTokens: m.limit?.output || 4096,
  737. });
  738. }
  739. }
  740. // Process zAi models
  741. const zaiCodingPlanVariants = [
  742. { provider: "zai", baseUrl: "https://api.z.ai/api/coding/paas/v4" },
  743. { provider: "zai-coding-cn", baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4" },
  744. ] as const;
  745. if (data["zai-coding-plan"]?.models) {
  746. for (const { provider, baseUrl } of zaiCodingPlanVariants) {
  747. for (const [modelId, model] of Object.entries(data["zai-coding-plan"].models)) {
  748. const m = model as ModelsDevModel;
  749. if (m.tool_call !== true) continue;
  750. const supportsImage = m.modalities?.input?.includes("image");
  751. models.push({
  752. id: modelId,
  753. name: m.name || modelId,
  754. api: "openai-completions",
  755. provider,
  756. baseUrl,
  757. reasoning: m.reasoning === true,
  758. input: supportsImage ? ["text", "image"] : ["text"],
  759. cost: {
  760. input: m.cost?.input || 0,
  761. output: m.cost?.output || 0,
  762. cacheRead: m.cost?.cache_read || 0,
  763. cacheWrite: m.cost?.cache_write || 0,
  764. },
  765. compat: {
  766. supportsDeveloperRole: false,
  767. thinkingFormat: "zai",
  768. ...(!ZAI_TOOL_STREAM_UNSUPPORTED_MODELS.has(modelId) ? { zaiToolStream: true } : {}),
  769. },
  770. contextWindow: m.limit?.context || 4096,
  771. maxTokens: m.limit?.output || 4096,
  772. });
  773. }
  774. }
  775. }
  776. // Process Mistral models
  777. if (data.mistral?.models) {
  778. for (const [modelId, model] of Object.entries(data.mistral.models)) {
  779. const m = model as ModelsDevModel;
  780. if (m.tool_call !== true) continue;
  781. models.push({
  782. id: modelId,
  783. name: m.name || modelId,
  784. api: "mistral-conversations",
  785. provider: "mistral",
  786. baseUrl: "https://api.mistral.ai",
  787. reasoning: m.reasoning === true,
  788. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  789. cost: {
  790. input: m.cost?.input || 0,
  791. output: m.cost?.output || 0,
  792. cacheRead: m.cost?.cache_read || 0,
  793. cacheWrite: m.cost?.cache_write || 0,
  794. },
  795. contextWindow: m.limit?.context || 4096,
  796. maxTokens: m.limit?.output || 4096,
  797. });
  798. }
  799. }
  800. // Process Hugging Face models
  801. if (data.huggingface?.models) {
  802. for (const [modelId, model] of Object.entries(data.huggingface.models)) {
  803. const m = model as ModelsDevModel;
  804. if (m.tool_call !== true) continue;
  805. models.push({
  806. id: modelId,
  807. name: m.name || modelId,
  808. api: "openai-completions",
  809. provider: "huggingface",
  810. baseUrl: "https://router.huggingface.co/v1",
  811. reasoning: m.reasoning === true,
  812. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  813. cost: {
  814. input: m.cost?.input || 0,
  815. output: m.cost?.output || 0,
  816. cacheRead: m.cost?.cache_read || 0,
  817. cacheWrite: m.cost?.cache_write || 0,
  818. },
  819. compat: {
  820. supportsDeveloperRole: false,
  821. },
  822. contextWindow: m.limit?.context || 4096,
  823. maxTokens: m.limit?.output || 4096,
  824. });
  825. }
  826. }
  827. // Process Fireworks models
  828. if (data["fireworks-ai"]?.models) {
  829. for (const [modelId, model] of Object.entries(data["fireworks-ai"].models)) {
  830. const m = model as ModelsDevModel;
  831. if (m.tool_call !== true) continue;
  832. models.push({
  833. id: modelId,
  834. name: m.name || modelId,
  835. api: "anthropic-messages",
  836. provider: "fireworks",
  837. // Fireworks Anthropic-compatible API - SDK appends /v1/messages
  838. baseUrl: "https://api.fireworks.ai/inference",
  839. reasoning: m.reasoning === true,
  840. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  841. cost: {
  842. input: m.cost?.input || 0,
  843. output: m.cost?.output || 0,
  844. cacheRead: m.cost?.cache_read || 0,
  845. cacheWrite: m.cost?.cache_write || 0,
  846. },
  847. contextWindow: m.limit?.context || 4096,
  848. maxTokens: m.limit?.output || 4096,
  849. // Fireworks prompt caching uses automatic prefix matching + session affinity.
  850. // x-session-affinity routes requests to the same replica for cache hits.
  851. // cache_control on tools and eager_input_streaming are not supported.
  852. // See: https://docs.fireworks.ai/tools-sdks/anthropic-compatibility
  853. compat: {
  854. sendSessionAffinityHeaders: true,
  855. supportsEagerToolInputStreaming: false,
  856. supportsCacheControlOnTools: false,
  857. supportsLongCacheRetention: false,
  858. },
  859. });
  860. }
  861. }
  862. // Process NVIDIA NIM models
  863. if (data.nvidia?.models) {
  864. for (const [modelId, model] of Object.entries(data.nvidia.models)) {
  865. const m = model as ModelsDevModel;
  866. if (m.tool_call !== true) continue;
  867. if (!m.modalities?.input?.includes("text")) continue;
  868. if (!m.modalities?.output?.includes("text")) continue;
  869. const liveModelId = nvidiaNimModelIds.get(modelId) ?? nvidiaNimModelIds.get(normalizeNvidiaModelId(modelId));
  870. if (!liveModelId) continue;
  871. if (NVIDIA_NIM_UNSUPPORTED_MODELS.has(liveModelId)) continue;
  872. models.push({
  873. id: liveModelId,
  874. name: m.name || liveModelId,
  875. api: "openai-completions",
  876. provider: "nvidia",
  877. baseUrl: NVIDIA_BASE_URL,
  878. headers: { ...NVIDIA_HEADERS },
  879. reasoning: m.reasoning === true,
  880. input: m.modalities.input.includes("image") ? ["text", "image"] : ["text"],
  881. cost: {
  882. input: m.cost?.input || 0,
  883. output: m.cost?.output || 0,
  884. cacheRead: m.cost?.cache_read || 0,
  885. cacheWrite: m.cost?.cache_write || 0,
  886. },
  887. compat: NVIDIA_OPENAI_COMPAT,
  888. contextWindow: m.limit?.context || 4096,
  889. maxTokens: m.limit?.output || 4096,
  890. });
  891. }
  892. }
  893. // Process Together AI models
  894. const togetherProvider = data.together ?? data.togetherai ?? data["together-ai"];
  895. if (togetherProvider?.models) {
  896. for (const [modelId, model] of Object.entries(togetherProvider.models)) {
  897. const m = model as ModelsDevModel & { status?: string };
  898. if (m.tool_call !== true) continue;
  899. if (m.status === "deprecated") continue;
  900. const reasoning = m.reasoning === true;
  901. const thinkingLevelMap = getTogetherThinkingLevelMap(modelId, reasoning);
  902. models.push({
  903. id: modelId,
  904. name: m.name || modelId,
  905. api: "openai-completions",
  906. provider: "together",
  907. baseUrl: TOGETHER_BASE_URL,
  908. reasoning,
  909. ...(thinkingLevelMap ? { thinkingLevelMap } : {}),
  910. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  911. cost: {
  912. input: m.cost?.input || 0,
  913. output: m.cost?.output || 0,
  914. cacheRead: m.cost?.cache_read || 0,
  915. cacheWrite: m.cost?.cache_write || 0,
  916. },
  917. compat: getTogetherCompat(modelId, reasoning),
  918. contextWindow: m.limit?.context || 4096,
  919. maxTokens: m.limit?.output || 4096,
  920. });
  921. }
  922. }
  923. // Process OpenCode models (Zen and Go)
  924. // API mapping based on provider.npm field:
  925. // - @ai-sdk/openai → openai-responses
  926. // - @ai-sdk/anthropic → anthropic-messages
  927. // - @ai-sdk/google → google-generative-ai
  928. // - null/undefined/@ai-sdk/openai-compatible → openai-completions
  929. const opencodeVariants = [
  930. { key: "opencode", provider: "opencode", basePath: "https://opencode.ai/zen" },
  931. { key: "opencode-go", provider: "opencode-go", basePath: "https://opencode.ai/zen/go" },
  932. ] as const;
  933. for (const variant of opencodeVariants) {
  934. if (!data[variant.key]?.models) continue;
  935. for (const [modelId, model] of Object.entries(data[variant.key].models)) {
  936. const m = model as ModelsDevModel & { status?: string };
  937. if (m.tool_call !== true) continue;
  938. if (m.status === "deprecated") continue;
  939. const npm = m.provider?.npm;
  940. let api: Api;
  941. let baseUrl: string;
  942. let compat: OpenAICompletionsCompat | undefined;
  943. if (npm === "@ai-sdk/openai") {
  944. api = "openai-responses";
  945. baseUrl = `${variant.basePath}/v1`;
  946. } else if (npm === "@ai-sdk/anthropic") {
  947. api = "anthropic-messages";
  948. // Anthropic SDK appends /v1/messages to baseURL
  949. baseUrl = variant.basePath;
  950. } else if (npm === "@ai-sdk/google") {
  951. api = "google-generative-ai";
  952. baseUrl = `${variant.basePath}/v1`;
  953. } else if (npm === "@ai-sdk/alibaba") {
  954. api = "openai-completions";
  955. baseUrl = `${variant.basePath}/v1`;
  956. compat = { cacheControlFormat: "anthropic" };
  957. } else {
  958. // null, undefined, or @ai-sdk/openai-compatible
  959. api = "openai-completions";
  960. baseUrl = `${variant.basePath}/v1`;
  961. }
  962. if (variant.provider === "opencode" && modelId === "grok-build-0.1") {
  963. compat = { ...(compat ?? {}), supportsReasoningEffort: false };
  964. }
  965. if ((variant.provider === "opencode" || variant.provider === "opencode-go") && modelId === "kimi-k2.6") {
  966. // OpenCode Kimi K2.6 accepts Anthropic-style thinking objects
  967. // and rejects string thinking values or combined reasoning_effort.
  968. compat = { ...(compat ?? {}), thinkingFormat: "deepseek", supportsReasoningEffort: false };
  969. }
  970. // Fix known mismatches between models.dev npm data and actual
  971. // OpenCode Go endpoint behaviour. models.dev reports these models
  972. // as @ai-sdk/anthropic, but the OpenCode Go endpoints either don't
  973. // accept Anthropic SDK auth (MiniMax M2.7) or are served through
  974. // the OpenAI-compatible /v1/chat/completions path (Qwen 3.5/3.6).
  975. // Switch them to openai-completions so requests use Bearer auth
  976. // and the standard /v1/chat/completions endpoint.
  977. if (variant.provider === "opencode-go") {
  978. if (modelId === "minimax-m2.7") {
  979. api = "openai-completions";
  980. baseUrl = `${variant.basePath}/v1`;
  981. }
  982. if (modelId === "qwen3.5-plus" || modelId === "qwen3.6-plus") {
  983. api = "openai-completions";
  984. baseUrl = `${variant.basePath}/v1`;
  985. // Qwen/DashScope uses enable_thinking at the top level.
  986. compat = { ...(compat ?? {}), thinkingFormat: "qwen" };
  987. }
  988. }
  989. if (api === "openai-completions") {
  990. compat = { ...(compat ?? {}), maxTokensField: "max_tokens" };
  991. }
  992. models.push({
  993. id: modelId,
  994. name: m.name || modelId,
  995. api,
  996. provider: variant.provider,
  997. baseUrl,
  998. reasoning: m.reasoning === true,
  999. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1000. cost: {
  1001. input: m.cost?.input || 0,
  1002. output: m.cost?.output || 0,
  1003. cacheRead: m.cost?.cache_read || 0,
  1004. cacheWrite: m.cost?.cache_write || 0,
  1005. },
  1006. ...(compat ? { compat } : {}),
  1007. contextWindow: m.limit?.context || 4096,
  1008. maxTokens: m.limit?.output || 4096,
  1009. });
  1010. }
  1011. }
  1012. // Process GitHub Copilot models
  1013. if (data["github-copilot"]?.models) {
  1014. for (const [modelId, model] of Object.entries(data["github-copilot"].models)) {
  1015. const m = model as ModelsDevModel & { status?: string };
  1016. if (m.tool_call !== true) continue;
  1017. if (m.status === "deprecated") continue;
  1018. // Claude 4.x models route to Anthropic Messages API
  1019. const isCopilotClaude4 = /^claude-(haiku|sonnet|opus)-4([.\-]|$)/.test(modelId);
  1020. // gpt-5 models require responses API, others use completions
  1021. const needsResponsesApi = modelId.startsWith("gpt-5") || modelId.startsWith("oswe");
  1022. const api: Api = isCopilotClaude4
  1023. ? "anthropic-messages"
  1024. : needsResponsesApi
  1025. ? "openai-responses"
  1026. : "openai-completions";
  1027. const anthropicCompat =
  1028. api === "anthropic-messages" ? getAnthropicMessagesCompat("github-copilot", modelId) : undefined;
  1029. const copilotModel: Model<any> = {
  1030. id: modelId,
  1031. name: m.name || modelId,
  1032. api,
  1033. provider: "github-copilot",
  1034. baseUrl: "https://api.individual.githubcopilot.com",
  1035. reasoning: m.reasoning === true,
  1036. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1037. cost: {
  1038. input: m.cost?.input || 0,
  1039. output: m.cost?.output || 0,
  1040. cacheRead: m.cost?.cache_read || 0,
  1041. cacheWrite: m.cost?.cache_write || 0,
  1042. },
  1043. contextWindow: m.limit?.context || 128000,
  1044. maxTokens: m.limit?.output || 8192,
  1045. headers: { ...COPILOT_STATIC_HEADERS },
  1046. ...(anthropicCompat ? { compat: anthropicCompat } : {}),
  1047. // compat only applies to openai-completions
  1048. ...(api === "openai-completions" ? {
  1049. compat: {
  1050. supportsStore: false,
  1051. supportsDeveloperRole: false,
  1052. supportsReasoningEffort: false,
  1053. },
  1054. } : {}),
  1055. };
  1056. models.push(copilotModel);
  1057. }
  1058. }
  1059. // Process MiniMax models
  1060. const minimaxVariants = [
  1061. { key: "minimax", provider: "minimax", baseUrl: "https://api.minimax.io/anthropic" },
  1062. { key: "minimax-cn", provider: "minimax-cn", baseUrl: "https://api.minimaxi.com/anthropic" },
  1063. ] as const;
  1064. for (const { key, provider, baseUrl } of minimaxVariants) {
  1065. if (data[key]?.models) {
  1066. for (const [modelId, model] of Object.entries(data[key].models)) {
  1067. const m = model as ModelsDevModel;
  1068. if (m.tool_call !== true) continue;
  1069. models.push({
  1070. id: modelId,
  1071. name: m.name || modelId,
  1072. api: "anthropic-messages",
  1073. provider,
  1074. // MiniMax's Anthropic-compatible API - SDK appends /v1/messages
  1075. baseUrl,
  1076. reasoning: m.reasoning === true,
  1077. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1078. cost: {
  1079. input: m.cost?.input || 0,
  1080. output: m.cost?.output || 0,
  1081. cacheRead: m.cost?.cache_read || 0,
  1082. cacheWrite: m.cost?.cache_write || 0,
  1083. },
  1084. contextWindow: m.limit?.context || 4096,
  1085. maxTokens: m.limit?.output || 4096,
  1086. });
  1087. }
  1088. }
  1089. }
  1090. // Process Kimi For Coding models
  1091. if (data["kimi-for-coding"]?.models) {
  1092. const kimiModels = data["kimi-for-coding"].models as Record<string, ModelsDevModel>;
  1093. const hasCanonicalModel = Object.prototype.hasOwnProperty.call(kimiModels, "kimi-for-coding");
  1094. const kimiAliases = new Set(["k2p5", "k2p6"]);
  1095. for (const [modelId, model] of Object.entries(kimiModels)) {
  1096. const m = model as ModelsDevModel;
  1097. if (m.tool_call !== true) continue;
  1098. // models.dev may expose versioned aliases (e.g. k2p5/k2p6).
  1099. // Normalize aliases to the canonical model id and drop duplicates when canonical exists.
  1100. if (kimiAliases.has(modelId) && hasCanonicalModel) continue;
  1101. const normalizedId = kimiAliases.has(modelId) ? "kimi-for-coding" : modelId;
  1102. const normalizedName = kimiAliases.has(modelId) ? "Kimi For Coding" : m.name || normalizedId;
  1103. models.push({
  1104. id: normalizedId,
  1105. name: normalizedName,
  1106. api: "anthropic-messages",
  1107. provider: "kimi-coding",
  1108. // Kimi For Coding's Anthropic-compatible API - SDK appends /v1/messages
  1109. baseUrl: "https://api.kimi.com/coding",
  1110. headers: { ...KIMI_STATIC_HEADERS },
  1111. reasoning: m.reasoning === true,
  1112. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1113. cost: {
  1114. input: m.cost?.input || 0,
  1115. output: m.cost?.output || 0,
  1116. cacheRead: m.cost?.cache_read || 0,
  1117. cacheWrite: m.cost?.cache_write || 0,
  1118. },
  1119. contextWindow: m.limit?.context || 4096,
  1120. maxTokens: m.limit?.output || 4096,
  1121. });
  1122. }
  1123. }
  1124. // Process Moonshot AI models
  1125. const moonshotVariants = [
  1126. { key: "moonshotai", provider: "moonshotai", baseUrl: "https://api.moonshot.ai/v1" },
  1127. { key: "moonshotai-cn", provider: "moonshotai-cn", baseUrl: "https://api.moonshot.cn/v1" },
  1128. ] as const;
  1129. const moonshotCompat: OpenAICompletionsCompat = {
  1130. supportsStore: false,
  1131. supportsDeveloperRole: false,
  1132. supportsReasoningEffort: false,
  1133. maxTokensField: "max_tokens",
  1134. supportsStrictMode: false,
  1135. thinkingFormat: "deepseek",
  1136. };
  1137. for (const { key, provider, baseUrl } of moonshotVariants) {
  1138. if (!data[key]?.models) continue;
  1139. for (const [modelId, model] of Object.entries(data[key].models)) {
  1140. const m = model as ModelsDevModel;
  1141. if (m.tool_call !== true) continue;
  1142. models.push({
  1143. id: modelId,
  1144. name: m.name || modelId,
  1145. api: "openai-completions",
  1146. provider,
  1147. baseUrl,
  1148. reasoning: m.reasoning === true,
  1149. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1150. cost: {
  1151. input: m.cost?.input || 0,
  1152. output: m.cost?.output || 0,
  1153. cacheRead: m.cost?.cache_read || 0,
  1154. cacheWrite: m.cost?.cache_write || 0,
  1155. },
  1156. contextWindow: m.limit?.context || 4096,
  1157. maxTokens: m.limit?.output || 4096,
  1158. compat: moonshotCompat,
  1159. });
  1160. }
  1161. }
  1162. // Process Xiaomi MiMo models
  1163. // Built-in `xiaomi` targets the API billing endpoint (single stable URL,
  1164. // keys from platform.xiaomimimo.com). The three `xiaomi-token-plan-*`
  1165. // providers cover prepaid Token Plan endpoints in cn / ams / sgp.
  1166. const xiaomiCompat: OpenAICompletionsCompat = {
  1167. requiresReasoningContentOnAssistantMessages: true,
  1168. thinkingFormat: "deepseek",
  1169. };
  1170. const xiaomiVariants = [
  1171. { provider: "xiaomi", baseUrl: "https://api.xiaomimimo.com/v1" },
  1172. { provider: "xiaomi-token-plan-cn", baseUrl: "https://token-plan-cn.xiaomimimo.com/v1" },
  1173. { provider: "xiaomi-token-plan-ams", baseUrl: "https://token-plan-ams.xiaomimimo.com/v1" },
  1174. { provider: "xiaomi-token-plan-sgp", baseUrl: "https://token-plan-sgp.xiaomimimo.com/v1" },
  1175. ] as const;
  1176. if (data.xiaomi?.models) {
  1177. for (const { provider, baseUrl } of xiaomiVariants) {
  1178. for (const [modelId, model] of Object.entries(data.xiaomi.models)) {
  1179. const m = model as ModelsDevModel;
  1180. if (m.tool_call !== true) continue;
  1181. if (provider.startsWith("xiaomi-token-plan-") && modelId === "mimo-v2-flash") continue;
  1182. models.push({
  1183. id: modelId,
  1184. name: m.name || modelId,
  1185. api: "openai-completions",
  1186. provider,
  1187. baseUrl,
  1188. compat: xiaomiCompat,
  1189. reasoning: m.reasoning === true,
  1190. input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
  1191. cost: {
  1192. input: m.cost?.input || 0,
  1193. output: m.cost?.output || 0,
  1194. cacheRead: m.cost?.cache_read || 0,
  1195. cacheWrite: m.cost?.cache_write || 0,
  1196. },
  1197. contextWindow: m.limit?.context || 4096,
  1198. maxTokens: m.limit?.output || 4096,
  1199. });
  1200. }
  1201. }
  1202. }
  1203. console.log(`Loaded ${models.length} tool-capable models from models.dev`);
  1204. return models;
  1205. } catch (error) {
  1206. console.error("Failed to load models.dev data:", error);
  1207. return [];
  1208. }
  1209. }
  1210. async function generateModels() {
  1211. // Fetch models from both sources
  1212. // models.dev: Anthropic, Google, OpenAI, Groq, Cerebras
  1213. // OpenRouter: xAI and other providers (excluding Anthropic, Google, OpenAI)
  1214. // AI Gateway: OpenAI-compatible catalog with tool-capable models
  1215. const modelsDevModels = await loadModelsDevData();
  1216. const openRouterModels = await fetchOpenRouterModels();
  1217. const aiGatewayModels = await fetchAiGatewayModels();
  1218. // Combine models (models.dev has priority)
  1219. const allModels = [...modelsDevModels, ...openRouterModels, ...aiGatewayModels].filter(
  1220. (model) =>
  1221. !((model.provider === "opencode" || model.provider === "opencode-go") && model.id === "gpt-5.3-codex-spark"),
  1222. );
  1223. // Fix incorrect cache pricing for Claude Opus 4.5 from models.dev
  1224. // models.dev has 3x the correct pricing (1.5/18.75 instead of 0.5/6.25)
  1225. const opus45 = allModels.find(m => m.provider === "anthropic" && m.id === "claude-opus-4-5");
  1226. if (opus45) {
  1227. opus45.cost.cacheRead = 0.5;
  1228. opus45.cost.cacheWrite = 6.25;
  1229. }
  1230. // Temporary overrides until upstream model metadata is corrected.
  1231. for (const candidate of allModels) {
  1232. if (candidate.provider === "amazon-bedrock" && candidate.id.includes("anthropic.claude-opus-4-6-v1")) {
  1233. candidate.cost.cacheRead = 0.5;
  1234. candidate.cost.cacheWrite = 6.25;
  1235. }
  1236. if (
  1237. (candidate.provider === "anthropic" ||
  1238. candidate.provider === "opencode" ||
  1239. candidate.provider === "opencode-go" ||
  1240. candidate.provider === "github-copilot") &&
  1241. (candidate.id === "claude-opus-4-6" ||
  1242. candidate.id === "claude-sonnet-4-6" ||
  1243. candidate.id === "claude-opus-4.6" ||
  1244. candidate.id === "claude-sonnet-4.6")
  1245. ) {
  1246. candidate.contextWindow = 1000000;
  1247. }
  1248. // OpenCode variants list Claude Sonnet 4/4.5 with 1M context, actual limit is 200K
  1249. if (
  1250. (candidate.provider === "opencode" || candidate.provider === "opencode-go") &&
  1251. (candidate.id === "claude-sonnet-4-5" || candidate.id === "claude-sonnet-4")
  1252. ) {
  1253. candidate.contextWindow = 200000;
  1254. }
  1255. if ((candidate.provider === "opencode" || candidate.provider === "opencode-go") && candidate.id === "gpt-5.4") {
  1256. candidate.contextWindow = 272000;
  1257. candidate.maxTokens = 128000;
  1258. }
  1259. if (candidate.provider === "openai" && (candidate.id === "gpt-5.4" || candidate.id === "gpt-5.5")) {
  1260. candidate.contextWindow = 272000;
  1261. candidate.maxTokens = 128000;
  1262. }
  1263. // models.dev reports gpt-5-pro output as 272000 (a duplicate of the input sub-limit);
  1264. // the actual max output is 128000. Also propagates to the derived Azure clone.
  1265. if (candidate.provider === "openai" && candidate.id === "gpt-5-pro") {
  1266. candidate.maxTokens = 128000;
  1267. }
  1268. // Keep selected OpenRouter model metadata stable until upstream settles.
  1269. if (candidate.provider === "openrouter" && candidate.id === "moonshotai/kimi-k2.5") {
  1270. candidate.cost.input = 0.41;
  1271. candidate.cost.output = 2.06;
  1272. candidate.cost.cacheRead = 0.07;
  1273. candidate.maxTokens = 4096;
  1274. }
  1275. if (candidate.provider === "openrouter" && candidate.id.startsWith("moonshotai/kimi-k2.6")) {
  1276. candidate.compat = {
  1277. ...candidate.compat,
  1278. supportsDeveloperRole: false,
  1279. requiresReasoningContentOnAssistantMessages: true,
  1280. };
  1281. }
  1282. if (candidate.provider === "openrouter" && candidate.id === "z-ai/glm-5") {
  1283. candidate.cost.input = 0.6;
  1284. candidate.cost.output = 1.9;
  1285. candidate.cost.cacheRead = 0.119;
  1286. }
  1287. }
  1288. // Add missing EU Opus 4.6 profile
  1289. if (!allModels.some((m) => m.provider === "amazon-bedrock" && m.id === "eu.anthropic.claude-opus-4-6-v1")) {
  1290. allModels.push({
  1291. id: "eu.anthropic.claude-opus-4-6-v1",
  1292. name: "Claude Opus 4.6 (EU)",
  1293. api: "bedrock-converse-stream",
  1294. provider: "amazon-bedrock",
  1295. baseUrl: getBedrockBaseUrl("eu.anthropic.claude-opus-4-6-v1"),
  1296. reasoning: true,
  1297. input: ["text", "image"],
  1298. cost: {
  1299. input: 5,
  1300. output: 25,
  1301. cacheRead: 0.5,
  1302. cacheWrite: 6.25,
  1303. },
  1304. contextWindow: 200000,
  1305. maxTokens: 128000,
  1306. });
  1307. }
  1308. // Add missing Claude Opus 4.6
  1309. if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-6")) {
  1310. allModels.push({
  1311. id: "claude-opus-4-6",
  1312. name: "Claude Opus 4.6",
  1313. api: "anthropic-messages",
  1314. baseUrl: "https://api.anthropic.com",
  1315. provider: "anthropic",
  1316. reasoning: true,
  1317. input: ["text", "image"],
  1318. cost: {
  1319. input: 5,
  1320. output: 25,
  1321. cacheRead: 0.5,
  1322. cacheWrite: 6.25,
  1323. },
  1324. contextWindow: 1000000,
  1325. maxTokens: 128000,
  1326. });
  1327. }
  1328. // Add missing Claude Opus 4.7
  1329. if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-7")) {
  1330. allModels.push({
  1331. id: "claude-opus-4-7",
  1332. name: "Claude Opus 4.7",
  1333. api: "anthropic-messages",
  1334. baseUrl: "https://api.anthropic.com",
  1335. provider: "anthropic",
  1336. reasoning: true,
  1337. input: ["text", "image"],
  1338. cost: {
  1339. input: 5,
  1340. output: 25,
  1341. cacheRead: 0.5,
  1342. cacheWrite: 6.25,
  1343. },
  1344. contextWindow: 1000000,
  1345. maxTokens: 128000,
  1346. });
  1347. }
  1348. // Add missing Claude Opus 4.8
  1349. if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-8")) {
  1350. allModels.push({
  1351. id: "claude-opus-4-8",
  1352. name: "Claude Opus 4.8",
  1353. api: "anthropic-messages",
  1354. baseUrl: "https://api.anthropic.com",
  1355. provider: "anthropic",
  1356. reasoning: true,
  1357. input: ["text", "image"],
  1358. cost: {
  1359. input: 5,
  1360. output: 25,
  1361. cacheRead: 0.5,
  1362. cacheWrite: 6.25,
  1363. },
  1364. contextWindow: 1000000,
  1365. maxTokens: 128000,
  1366. });
  1367. }
  1368. // Add missing Claude Sonnet 4.6
  1369. if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-sonnet-4-6")) {
  1370. allModels.push({
  1371. id: "claude-sonnet-4-6",
  1372. name: "Claude Sonnet 4.6",
  1373. api: "anthropic-messages",
  1374. baseUrl: "https://api.anthropic.com",
  1375. provider: "anthropic",
  1376. reasoning: true,
  1377. input: ["text", "image"],
  1378. cost: {
  1379. input: 3,
  1380. output: 15,
  1381. cacheRead: 0.3,
  1382. cacheWrite: 3.75,
  1383. },
  1384. contextWindow: 1000000,
  1385. maxTokens: 64000,
  1386. });
  1387. }
  1388. // Add missing Gemini 3.1 Flash Lite Preview until models.dev includes it.
  1389. if (!allModels.some((m) => m.provider === "google" && m.id === "gemini-3.1-flash-lite-preview")) {
  1390. allModels.push({
  1391. id: "gemini-3.1-flash-lite-preview",
  1392. name: "Gemini 3.1 Flash Lite Preview",
  1393. api: "google-generative-ai",
  1394. baseUrl: "https://generativelanguage.googleapis.com/v1beta",
  1395. provider: "google",
  1396. reasoning: true,
  1397. input: ["text", "image"],
  1398. cost: {
  1399. input: 0,
  1400. output: 0,
  1401. cacheRead: 0,
  1402. cacheWrite: 0,
  1403. },
  1404. contextWindow: 1048576,
  1405. maxTokens: 65536,
  1406. });
  1407. }
  1408. // Add missing gpt models
  1409. const missingOpenAiModels: Model<"openai-responses">[] = [
  1410. {
  1411. id: "gpt-5.6-sol",
  1412. name: "GPT-5.6 Sol",
  1413. api: "openai-responses",
  1414. baseUrl: "https://api.openai.com/v1",
  1415. provider: "openai",
  1416. reasoning: true,
  1417. input: ["text", "image"],
  1418. cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
  1419. contextWindow: 272000,
  1420. maxTokens: 128000,
  1421. },
  1422. {
  1423. id: "gpt-5.6-terra",
  1424. name: "GPT-5.6 Terra",
  1425. api: "openai-responses",
  1426. baseUrl: "https://api.openai.com/v1",
  1427. provider: "openai",
  1428. reasoning: true,
  1429. input: ["text", "image"],
  1430. cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
  1431. contextWindow: 272000,
  1432. maxTokens: 128000,
  1433. },
  1434. {
  1435. id: "gpt-5.6-luna",
  1436. name: "GPT-5.6 Luna",
  1437. api: "openai-responses",
  1438. baseUrl: "https://api.openai.com/v1",
  1439. provider: "openai",
  1440. reasoning: true,
  1441. input: ["text", "image"],
  1442. cost: { input: 1, output: 6, cacheRead: 0.1, cacheWrite: 1.25 },
  1443. contextWindow: 272000,
  1444. maxTokens: 128000,
  1445. },
  1446. {
  1447. id: "gpt-5-chat-latest",
  1448. name: "GPT-5 Chat Latest",
  1449. api: "openai-responses",
  1450. baseUrl: "https://api.openai.com/v1",
  1451. provider: "openai",
  1452. reasoning: false,
  1453. input: ["text", "image"],
  1454. cost: {
  1455. input: 1.25,
  1456. output: 10,
  1457. cacheRead: 0.125,
  1458. cacheWrite: 0,
  1459. },
  1460. contextWindow: 128000,
  1461. maxTokens: 16384,
  1462. },
  1463. ];
  1464. for (const model of missingOpenAiModels) {
  1465. if (!allModels.some((candidate) => candidate.provider === model.provider && candidate.id === model.id)) {
  1466. allModels.push(model);
  1467. }
  1468. }
  1469. if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.1-codex")) {
  1470. allModels.push({
  1471. id: "gpt-5.1-codex",
  1472. name: "GPT-5.1 Codex",
  1473. api: "openai-responses",
  1474. baseUrl: "https://api.openai.com/v1",
  1475. provider: "openai",
  1476. reasoning: true,
  1477. input: ["text", "image"],
  1478. cost: {
  1479. input: 1.25,
  1480. output: 5,
  1481. cacheRead: 0.125,
  1482. cacheWrite: 1.25,
  1483. },
  1484. contextWindow: 400000,
  1485. maxTokens: 128000,
  1486. });
  1487. }
  1488. if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.1-codex-max")) {
  1489. allModels.push({
  1490. id: "gpt-5.1-codex-max",
  1491. name: "GPT-5.1 Codex Max",
  1492. api: "openai-responses",
  1493. baseUrl: "https://api.openai.com/v1",
  1494. provider: "openai",
  1495. reasoning: true,
  1496. input: ["text", "image"],
  1497. cost: {
  1498. input: 1.25,
  1499. output: 10,
  1500. cacheRead: 0.125,
  1501. cacheWrite: 0,
  1502. },
  1503. contextWindow: 400000,
  1504. maxTokens: 128000,
  1505. });
  1506. }
  1507. if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.3-codex-spark")) {
  1508. allModels.push({
  1509. id: "gpt-5.3-codex-spark",
  1510. name: "GPT-5.3 Codex Spark",
  1511. api: "openai-responses",
  1512. baseUrl: "https://api.openai.com/v1",
  1513. provider: "openai",
  1514. reasoning: true,
  1515. input: ["text"],
  1516. cost: {
  1517. input: 0,
  1518. output: 0,
  1519. cacheRead: 0,
  1520. cacheWrite: 0,
  1521. },
  1522. contextWindow: 128000,
  1523. maxTokens: 16384,
  1524. });
  1525. }
  1526. // Add missing GitHub Copilot GPT-5.3 models until models.dev includes them.
  1527. const copilotBaseModel = allModels.find(
  1528. (m) => m.provider === "github-copilot" && m.id === "gpt-5.2-codex",
  1529. );
  1530. if (copilotBaseModel) {
  1531. if (!allModels.some((m) => m.provider === "github-copilot" && m.id === "gpt-5.3-codex")) {
  1532. allModels.push({
  1533. ...copilotBaseModel,
  1534. id: "gpt-5.3-codex",
  1535. name: "GPT-5.3 Codex",
  1536. });
  1537. }
  1538. }
  1539. if (!allModels.some((m) => m.provider === "openai" && m.id === "gpt-5.4")) {
  1540. allModels.push({
  1541. id: "gpt-5.4",
  1542. name: "GPT-5.4",
  1543. api: "openai-responses",
  1544. baseUrl: "https://api.openai.com/v1",
  1545. provider: "openai",
  1546. reasoning: true,
  1547. input: ["text", "image"],
  1548. cost: {
  1549. input: 2.5,
  1550. output: 15,
  1551. cacheRead: 0.25,
  1552. cacheWrite: 0,
  1553. },
  1554. contextWindow: 272000,
  1555. maxTokens: 128000,
  1556. });
  1557. }
  1558. const deepseekCompat: OpenAICompletionsCompat = {
  1559. requiresReasoningContentOnAssistantMessages: true,
  1560. thinkingFormat: "deepseek",
  1561. };
  1562. const deepseekV4Models: Model<"openai-completions">[] = [
  1563. {
  1564. id: "deepseek-v4-flash",
  1565. name: "DeepSeek V4 Flash",
  1566. api: "openai-completions",
  1567. baseUrl: "https://api.deepseek.com",
  1568. provider: "deepseek",
  1569. reasoning: true,
  1570. input: ["text"],
  1571. cost: {
  1572. input: 0.14,
  1573. output: 0.28,
  1574. cacheRead: 0.0028,
  1575. cacheWrite: 0,
  1576. },
  1577. contextWindow: 1000000,
  1578. maxTokens: 384000,
  1579. compat: deepseekCompat,
  1580. },
  1581. {
  1582. id: "deepseek-v4-pro",
  1583. name: "DeepSeek V4 Pro",
  1584. api: "openai-completions",
  1585. baseUrl: "https://api.deepseek.com",
  1586. provider: "deepseek",
  1587. reasoning: true,
  1588. input: ["text"],
  1589. cost: {
  1590. input: 0.435,
  1591. output: 0.87,
  1592. cacheRead: 0.003625,
  1593. cacheWrite: 0,
  1594. },
  1595. contextWindow: 1000000,
  1596. maxTokens: 384000,
  1597. compat: deepseekCompat,
  1598. },
  1599. ];
  1600. allModels.push(...deepseekV4Models);
  1601. const antLingCompat: OpenAICompletionsCompat = {
  1602. supportsStore: false,
  1603. supportsDeveloperRole: false,
  1604. supportsReasoningEffort: false,
  1605. maxTokensField: "max_tokens",
  1606. supportsLongCacheRetention: false,
  1607. };
  1608. const antLingModels: Model<"openai-completions">[] = [
  1609. {
  1610. id: "Ling-2.6-flash",
  1611. name: "Ling 2.6 Flash",
  1612. api: "openai-completions",
  1613. baseUrl: "https://api.ant-ling.com/v1",
  1614. provider: "ant-ling",
  1615. reasoning: false,
  1616. input: ["text"],
  1617. cost: { input: 0.01, output: 0.02, cacheRead: 0, cacheWrite: 0 },
  1618. contextWindow: 262144,
  1619. maxTokens: 65536,
  1620. compat: antLingCompat,
  1621. },
  1622. {
  1623. id: "Ling-2.6-1T",
  1624. name: "Ling 2.6 1T",
  1625. api: "openai-completions",
  1626. baseUrl: "https://api.ant-ling.com/v1",
  1627. provider: "ant-ling",
  1628. reasoning: false,
  1629. input: ["text"],
  1630. cost: { input: 0.06, output: 0.25, cacheRead: 0, cacheWrite: 0 },
  1631. contextWindow: 262144,
  1632. maxTokens: 65536,
  1633. compat: antLingCompat,
  1634. },
  1635. {
  1636. id: "Ring-2.6-1T",
  1637. name: "Ring 2.6 1T",
  1638. api: "openai-completions",
  1639. baseUrl: "https://api.ant-ling.com/v1",
  1640. provider: "ant-ling",
  1641. reasoning: true,
  1642. input: ["text"],
  1643. cost: { input: 0.06, output: 0.25, cacheRead: 0, cacheWrite: 0 },
  1644. contextWindow: 262144,
  1645. maxTokens: 65536,
  1646. compat: { ...antLingCompat, thinkingFormat: "ant-ling" },
  1647. },
  1648. ];
  1649. allModels.push(...antLingModels);
  1650. for (const candidate of allModels) {
  1651. if (candidate.api === "openai-completions" && candidate.id.includes("deepseek-v4")) {
  1652. candidate.compat = {
  1653. ...candidate.compat,
  1654. ...(candidate.provider === "openrouter"
  1655. ? {
  1656. requiresReasoningContentOnAssistantMessages:
  1657. deepseekCompat.requiresReasoningContentOnAssistantMessages,
  1658. }
  1659. : deepseekCompat),
  1660. };
  1661. }
  1662. }
  1663. const minimaxDirectSupportedIds = new Set(["MiniMax-M2.7", "MiniMax-M2.7-highspeed", "MiniMax-M3"]);
  1664. for (let i = allModels.length - 1; i >= 0; i--) {
  1665. const candidate = allModels[i];
  1666. if (
  1667. (candidate.provider === "minimax" || candidate.provider === "minimax-cn") &&
  1668. !minimaxDirectSupportedIds.has(candidate.id)
  1669. ) {
  1670. allModels.splice(i, 1);
  1671. }
  1672. }
  1673. // OpenAI Codex (ChatGPT OAuth) models
  1674. // NOTE: These are not fetched from models.dev; we keep a small, explicit list to avoid aliases.
  1675. // Older model limits are based on observed server behavior; GPT-5.6 follows Codex's 372k catalog limit.
  1676. const CODEX_BASE_URL = "https://chatgpt.com/backend-api";
  1677. const CODEX_CONTEXT = 272000;
  1678. const CODEX_GPT_56_CONTEXT = 372000;
  1679. const CODEX_SPARK_CONTEXT = 128000;
  1680. const CODEX_MAX_TOKENS = 128000;
  1681. const codexModels: Model<"openai-codex-responses">[] = [
  1682. {
  1683. id: "gpt-5.3-codex-spark",
  1684. name: "GPT-5.3 Codex Spark",
  1685. api: "openai-codex-responses",
  1686. provider: "openai-codex",
  1687. baseUrl: CODEX_BASE_URL,
  1688. reasoning: true,
  1689. input: ["text"],
  1690. cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
  1691. contextWindow: CODEX_SPARK_CONTEXT,
  1692. maxTokens: CODEX_MAX_TOKENS,
  1693. },
  1694. {
  1695. id: "gpt-5.4",
  1696. name: "GPT-5.4",
  1697. api: "openai-codex-responses",
  1698. provider: "openai-codex",
  1699. baseUrl: CODEX_BASE_URL,
  1700. reasoning: true,
  1701. input: ["text", "image"],
  1702. cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 0 },
  1703. contextWindow: CODEX_CONTEXT,
  1704. maxTokens: CODEX_MAX_TOKENS,
  1705. },
  1706. {
  1707. id: "gpt-5.4-mini",
  1708. name: "GPT-5.4 mini",
  1709. api: "openai-codex-responses",
  1710. provider: "openai-codex",
  1711. baseUrl: CODEX_BASE_URL,
  1712. reasoning: true,
  1713. input: ["text", "image"],
  1714. cost: { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
  1715. contextWindow: CODEX_CONTEXT,
  1716. maxTokens: CODEX_MAX_TOKENS,
  1717. },
  1718. {
  1719. id: "gpt-5.5",
  1720. name: "GPT-5.5",
  1721. api: "openai-codex-responses",
  1722. provider: "openai-codex",
  1723. baseUrl: CODEX_BASE_URL,
  1724. reasoning: true,
  1725. input: ["text", "image"],
  1726. cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
  1727. contextWindow: CODEX_CONTEXT,
  1728. maxTokens: CODEX_MAX_TOKENS,
  1729. },
  1730. {
  1731. id: "gpt-5.6-luna",
  1732. name: "GPT-5.6 Luna",
  1733. api: "openai-codex-responses",
  1734. provider: "openai-codex",
  1735. baseUrl: CODEX_BASE_URL,
  1736. reasoning: true,
  1737. input: ["text", "image"],
  1738. cost: { input: 1, output: 6, cacheRead: 0.1, cacheWrite: 1.25 },
  1739. contextWindow: CODEX_GPT_56_CONTEXT,
  1740. maxTokens: CODEX_MAX_TOKENS,
  1741. },
  1742. {
  1743. id: "gpt-5.6-sol",
  1744. name: "GPT-5.6 Sol",
  1745. api: "openai-codex-responses",
  1746. provider: "openai-codex",
  1747. baseUrl: CODEX_BASE_URL,
  1748. reasoning: true,
  1749. input: ["text", "image"],
  1750. cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
  1751. contextWindow: CODEX_GPT_56_CONTEXT,
  1752. maxTokens: CODEX_MAX_TOKENS,
  1753. },
  1754. {
  1755. id: "gpt-5.6-terra",
  1756. name: "GPT-5.6 Terra",
  1757. api: "openai-codex-responses",
  1758. provider: "openai-codex",
  1759. baseUrl: CODEX_BASE_URL,
  1760. reasoning: true,
  1761. input: ["text", "image"],
  1762. cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
  1763. contextWindow: CODEX_GPT_56_CONTEXT,
  1764. maxTokens: CODEX_MAX_TOKENS,
  1765. },
  1766. ];
  1767. allModels.push(...codexModels);
  1768. // Add missing Grok models
  1769. const missingGrokModels: Model<"openai-completions">[] = [
  1770. {
  1771. id: "grok-3",
  1772. name: "Grok 3",
  1773. api: "openai-completions",
  1774. baseUrl: "https://api.x.ai/v1",
  1775. provider: "xai",
  1776. reasoning: false,
  1777. input: ["text"],
  1778. cost: { input: 3, output: 15, cacheRead: 0.75, cacheWrite: 0 },
  1779. contextWindow: 131072,
  1780. maxTokens: 8192,
  1781. },
  1782. {
  1783. id: "grok-3-fast",
  1784. name: "Grok 3 Fast",
  1785. api: "openai-completions",
  1786. baseUrl: "https://api.x.ai/v1",
  1787. provider: "xai",
  1788. reasoning: false,
  1789. input: ["text"],
  1790. cost: { input: 5, output: 25, cacheRead: 1.25, cacheWrite: 0 },
  1791. contextWindow: 131072,
  1792. maxTokens: 8192,
  1793. },
  1794. {
  1795. id: "grok-code-fast-1",
  1796. name: "Grok Code Fast 1",
  1797. api: "openai-completions",
  1798. baseUrl: "https://api.x.ai/v1",
  1799. provider: "xai",
  1800. reasoning: false,
  1801. input: ["text"],
  1802. cost: {
  1803. input: 0.2,
  1804. output: 1.5,
  1805. cacheRead: 0.02,
  1806. cacheWrite: 0,
  1807. },
  1808. contextWindow: 32768,
  1809. maxTokens: 8192,
  1810. },
  1811. ];
  1812. for (const model of missingGrokModels) {
  1813. if (!allModels.some(m => m.provider === model.provider && m.id === model.id)) {
  1814. allModels.push(model);
  1815. }
  1816. }
  1817. // Add missing Mistral Medium 3.5 model until models.dev includes it
  1818. if (!allModels.some(m => m.provider === "mistral" && m.id === "mistral-medium-3.5")) {
  1819. allModels.push({
  1820. id: "mistral-medium-3.5",
  1821. name: "Mistral Medium 3.5",
  1822. api: "mistral-conversations",
  1823. provider: "mistral",
  1824. baseUrl: "https://api.mistral.ai",
  1825. reasoning: true,
  1826. input: ["text", "image"],
  1827. cost: {
  1828. input: 1.5,
  1829. output: 7.5,
  1830. cacheRead: 0,
  1831. cacheWrite: 0,
  1832. },
  1833. contextWindow: 262144, // 256k tokens
  1834. maxTokens: 262144,
  1835. });
  1836. }
  1837. // Add "auto" alias for openrouter/auto
  1838. if (!allModels.some(m => m.provider === "openrouter" && m.id === "auto")) {
  1839. allModels.push({
  1840. id: "auto",
  1841. name: "Auto",
  1842. api: "openai-completions",
  1843. provider: "openrouter",
  1844. baseUrl: "https://openrouter.ai/api/v1",
  1845. reasoning: true,
  1846. input: ["text", "image"],
  1847. cost: {
  1848. // we dont know about the costs because OpenRouter auto routes to different models
  1849. // and then charges you for the underlying used model
  1850. input:0,
  1851. output:0,
  1852. cacheRead:0,
  1853. cacheWrite:0,
  1854. },
  1855. contextWindow: 2000000,
  1856. maxTokens: 30000,
  1857. });
  1858. }
  1859. const VERTEX_BASE_URL = "https://{location}-aiplatform.googleapis.com";
  1860. const vertexModels: Model<"google-vertex">[] = [
  1861. {
  1862. id: "gemini-3-pro-preview",
  1863. name: "Gemini 3 Pro Preview (Vertex)",
  1864. api: "google-vertex",
  1865. provider: "google-vertex",
  1866. baseUrl: VERTEX_BASE_URL,
  1867. reasoning: true,
  1868. input: ["text", "image"],
  1869. cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
  1870. contextWindow: 1000000,
  1871. maxTokens: 64000,
  1872. },
  1873. {
  1874. id: "gemini-3.1-pro-preview",
  1875. name: "Gemini 3.1 Pro Preview (Vertex)",
  1876. api: "google-vertex",
  1877. provider: "google-vertex",
  1878. baseUrl: VERTEX_BASE_URL,
  1879. reasoning: true,
  1880. input: ["text", "image"],
  1881. cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
  1882. contextWindow: 1048576,
  1883. maxTokens: 65536,
  1884. },
  1885. {
  1886. id: "gemini-3.1-pro-preview-customtools",
  1887. name: "Gemini 3.1 Pro Preview Custom Tools (Vertex)",
  1888. api: "google-vertex",
  1889. provider: "google-vertex",
  1890. baseUrl: VERTEX_BASE_URL,
  1891. reasoning: true,
  1892. input: ["text", "image"],
  1893. cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
  1894. contextWindow: 1048576,
  1895. maxTokens: 65536,
  1896. },
  1897. {
  1898. id: "gemini-3-flash-preview",
  1899. name: "Gemini 3 Flash Preview (Vertex)",
  1900. api: "google-vertex",
  1901. provider: "google-vertex",
  1902. baseUrl: VERTEX_BASE_URL,
  1903. reasoning: true,
  1904. input: ["text", "image"],
  1905. cost: { input: 0.5, output: 3, cacheRead: 0.05, cacheWrite: 0 },
  1906. contextWindow: 1048576,
  1907. maxTokens: 65536,
  1908. },
  1909. {
  1910. id: "gemini-2.0-flash",
  1911. name: "Gemini 2.0 Flash (Vertex)",
  1912. api: "google-vertex",
  1913. provider: "google-vertex",
  1914. baseUrl: VERTEX_BASE_URL,
  1915. reasoning: false,
  1916. input: ["text", "image"],
  1917. cost: { input: 0.15, output: 0.6, cacheRead: 0.0375, cacheWrite: 0 },
  1918. contextWindow: 1048576,
  1919. maxTokens: 8192,
  1920. },
  1921. {
  1922. id: "gemini-2.0-flash-lite",
  1923. name: "Gemini 2.0 Flash Lite (Vertex)",
  1924. api: "google-vertex",
  1925. provider: "google-vertex",
  1926. baseUrl: VERTEX_BASE_URL,
  1927. reasoning: true,
  1928. input: ["text", "image"],
  1929. cost: { input: 0.075, output: 0.3, cacheRead: 0.01875, cacheWrite: 0 },
  1930. contextWindow: 1048576,
  1931. maxTokens: 65536,
  1932. },
  1933. {
  1934. id: "gemini-2.5-pro",
  1935. name: "Gemini 2.5 Pro (Vertex)",
  1936. api: "google-vertex",
  1937. provider: "google-vertex",
  1938. baseUrl: VERTEX_BASE_URL,
  1939. reasoning: true,
  1940. input: ["text", "image"],
  1941. cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
  1942. contextWindow: 1048576,
  1943. maxTokens: 65536,
  1944. },
  1945. {
  1946. id: "gemini-2.5-flash",
  1947. name: "Gemini 2.5 Flash (Vertex)",
  1948. api: "google-vertex",
  1949. provider: "google-vertex",
  1950. baseUrl: VERTEX_BASE_URL,
  1951. reasoning: true,
  1952. input: ["text", "image"],
  1953. cost: { input: 0.3, output: 2.5, cacheRead: 0.03, cacheWrite: 0 },
  1954. contextWindow: 1048576,
  1955. maxTokens: 65536,
  1956. },
  1957. {
  1958. id: "gemini-2.5-flash-lite-preview-09-2025",
  1959. name: "Gemini 2.5 Flash Lite Preview 09-25 (Vertex)",
  1960. api: "google-vertex",
  1961. provider: "google-vertex",
  1962. baseUrl: VERTEX_BASE_URL,
  1963. reasoning: true,
  1964. input: ["text", "image"],
  1965. cost: { input: 0.1, output: 0.4, cacheRead: 0.01, cacheWrite: 0 },
  1966. contextWindow: 1048576,
  1967. maxTokens: 65536,
  1968. },
  1969. {
  1970. id: "gemini-2.5-flash-lite",
  1971. name: "Gemini 2.5 Flash Lite (Vertex)",
  1972. api: "google-vertex",
  1973. provider: "google-vertex",
  1974. baseUrl: VERTEX_BASE_URL,
  1975. reasoning: true,
  1976. input: ["text", "image"],
  1977. cost: { input: 0.1, output: 0.4, cacheRead: 0.01, cacheWrite: 0 },
  1978. contextWindow: 1048576,
  1979. maxTokens: 65536,
  1980. },
  1981. {
  1982. id: "gemini-1.5-pro",
  1983. name: "Gemini 1.5 Pro (Vertex)",
  1984. api: "google-vertex",
  1985. provider: "google-vertex",
  1986. baseUrl: VERTEX_BASE_URL,
  1987. reasoning: false,
  1988. input: ["text", "image"],
  1989. cost: { input: 1.25, output: 5, cacheRead: 0.3125, cacheWrite: 0 },
  1990. contextWindow: 1000000,
  1991. maxTokens: 8192,
  1992. },
  1993. {
  1994. id: "gemini-1.5-flash",
  1995. name: "Gemini 1.5 Flash (Vertex)",
  1996. api: "google-vertex",
  1997. provider: "google-vertex",
  1998. baseUrl: VERTEX_BASE_URL,
  1999. reasoning: false,
  2000. input: ["text", "image"],
  2001. cost: { input: 0.075, output: 0.3, cacheRead: 0.01875, cacheWrite: 0 },
  2002. contextWindow: 1000000,
  2003. maxTokens: 8192,
  2004. },
  2005. {
  2006. id: "gemini-1.5-flash-8b",
  2007. name: "Gemini 1.5 Flash-8B (Vertex)",
  2008. api: "google-vertex",
  2009. provider: "google-vertex",
  2010. baseUrl: VERTEX_BASE_URL,
  2011. reasoning: false,
  2012. input: ["text", "image"],
  2013. cost: { input: 0.0375, output: 0.15, cacheRead: 0.01, cacheWrite: 0 },
  2014. contextWindow: 1000000,
  2015. maxTokens: 8192,
  2016. },
  2017. ];
  2018. allModels.push(...vertexModels);
  2019. // Azure Foundry deploys these with larger context windows than OpenAI's own API,
  2020. // which caps gpt-5.4/gpt-5.5 at 272k. See models-sold-directly-by-azure docs.
  2021. const AZURE_CONTEXT_WINDOW_OVERRIDES: Record<string, number> = {
  2022. "gpt-5.4": 1050000,
  2023. "gpt-5.5": 1050000,
  2024. };
  2025. const azureOpenAiModels: Model<Api>[] = allModels
  2026. .filter((model) => model.provider === "openai" && model.api === "openai-responses")
  2027. .map((model) => ({
  2028. ...model,
  2029. api: "azure-openai-responses",
  2030. provider: "azure-openai-responses",
  2031. baseUrl: "",
  2032. contextWindow: AZURE_CONTEXT_WINDOW_OVERRIDES[model.id] ?? model.contextWindow,
  2033. }));
  2034. allModels.push(...azureOpenAiModels);
  2035. for (const model of allModels) {
  2036. applyThinkingLevelMetadata(model);
  2037. }
  2038. // Group by provider and deduplicate by model ID
  2039. const providers: Record<string, Record<string, Model<any>>> = {};
  2040. for (const model of allModels) {
  2041. if (!providers[model.provider]) {
  2042. providers[model.provider] = {};
  2043. }
  2044. // Use model ID as key to automatically deduplicate
  2045. // Only add if not already present (models.dev takes priority over OpenRouter)
  2046. if (!providers[model.provider][model.id]) {
  2047. providers[model.provider][model.id] = model;
  2048. }
  2049. }
  2050. // Generate TypeScript file
  2051. let output = `// This file is auto-generated by scripts/generate-models.ts
  2052. // Do not edit manually - run 'npm run generate-models' to update
  2053. import type { Model } from "./types.ts";
  2054. export const MODELS = {
  2055. `;
  2056. // Generate provider sections (sorted for deterministic output)
  2057. const sortedProviderIds = Object.keys(providers).sort();
  2058. for (const providerId of sortedProviderIds) {
  2059. const models = providers[providerId];
  2060. output += `\t${JSON.stringify(providerId)}: {\n`;
  2061. const sortedModelIds = Object.keys(models).sort();
  2062. for (const modelId of sortedModelIds) {
  2063. const model = models[modelId];
  2064. output += `\t\t"${model.id}": {\n`;
  2065. output += `\t\t\tid: "${model.id}",\n`;
  2066. output += `\t\t\tname: "${model.name}",\n`;
  2067. output += `\t\t\tapi: "${model.api}",\n`;
  2068. output += `\t\t\tprovider: "${model.provider}",\n`;
  2069. if (model.baseUrl !== undefined) {
  2070. output += `\t\t\tbaseUrl: "${model.baseUrl}",\n`;
  2071. }
  2072. if (model.headers) {
  2073. output += `\t\t\theaders: ${JSON.stringify(model.headers)},\n`;
  2074. }
  2075. if (model.compat) {
  2076. output += ` compat: ${JSON.stringify(model.compat)},
  2077. `;
  2078. }
  2079. output += `\t\t\treasoning: ${model.reasoning},\n`;
  2080. if (model.thinkingLevelMap) {
  2081. output += `\t\t\tthinkingLevelMap: ${JSON.stringify(model.thinkingLevelMap)},\n`;
  2082. }
  2083. output += `\t\t\tinput: [${model.input.map(i => `"${i}"`).join(", ")}],\n`;
  2084. output += `\t\t\tcost: {\n`;
  2085. output += `\t\t\t\tinput: ${model.cost.input},\n`;
  2086. output += `\t\t\t\toutput: ${model.cost.output},\n`;
  2087. output += `\t\t\t\tcacheRead: ${model.cost.cacheRead},\n`;
  2088. output += `\t\t\t\tcacheWrite: ${model.cost.cacheWrite},\n`;
  2089. output += `\t\t\t},\n`;
  2090. output += `\t\t\tcontextWindow: ${model.contextWindow},\n`;
  2091. output += `\t\t\tmaxTokens: ${model.maxTokens},\n`;
  2092. output += `\t\t} satisfies Model<"${model.api}">,\n`;
  2093. }
  2094. output += `\t},\n`;
  2095. }
  2096. output += `} as const;
  2097. `;
  2098. // Write file
  2099. writeFileSync(join(packageRoot, "src/models.generated.ts"), output);
  2100. console.log("Generated src/models.generated.ts");
  2101. // Print statistics
  2102. const totalModels = allModels.length;
  2103. const reasoningModels = allModels.filter(m => m.reasoning).length;
  2104. console.log(`\nModel Statistics:`);
  2105. console.log(` Total tool-capable models: ${totalModels}`);
  2106. console.log(` Reasoning-capable models: ${reasoningModels}`);
  2107. for (const [provider, models] of Object.entries(providers)) {
  2108. console.log(` ${provider}: ${Object.keys(models).length} models`);
  2109. }
  2110. }
  2111. // Run the generator
  2112. generateModels().catch(console.error);