| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276 |
- #!/usr/bin/env node
- import { writeFileSync } from "fs";
- import { join, dirname } from "path";
- import { fileURLToPath } from "url";
- import {
- CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL,
- CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL,
- CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL,
- CLOUDFLARE_WORKERS_AI_BASE_URL,
- } from "../src/providers/cloudflare.ts";
- import type { AnthropicMessagesCompat, Api, KnownProvider, Model, OpenAICompletionsCompat } from "../src/types.ts";
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = dirname(__filename);
- const packageRoot = join(__dirname, "..");
- interface ModelsDevModel {
- id: string;
- name: string;
- tool_call?: boolean;
- reasoning?: boolean;
- limit?: {
- context?: number;
- output?: number;
- };
- cost?: {
- input?: number;
- output?: number;
- cache_read?: number;
- cache_write?: number;
- };
- modalities?: {
- input?: string[];
- output?: string[];
- };
- provider?: {
- npm?: string;
- };
- }
- interface NvidiaNimModelListItem {
- id: string;
- }
- interface AiGatewayModel {
- id: string;
- name?: string;
- context_window?: number;
- max_tokens?: number;
- tags?: string[];
- pricing?: {
- input?: string | number;
- output?: string | number;
- input_cache_read?: string | number;
- input_cache_write?: string | number;
- };
- }
- const COPILOT_STATIC_HEADERS = {
- "User-Agent": "GitHubCopilotChat/0.35.0",
- "Editor-Version": "vscode/1.107.0",
- "Editor-Plugin-Version": "copilot-chat/0.35.0",
- "Copilot-Integration-Id": "vscode-chat",
- } as const;
- const KIMI_STATIC_HEADERS = {
- "User-Agent": "KimiCLI/1.5",
- } as const;
- const TOGETHER_BASE_URL = "https://api.together.ai/v1";
- const TOGETHER_BASE_COMPAT: OpenAICompletionsCompat = {
- supportsStore: false,
- supportsDeveloperRole: false,
- supportsReasoningEffort: false,
- maxTokensField: "max_tokens",
- supportsStrictMode: false,
- supportsLongCacheRetention: false,
- };
- const TOGETHER_TOGGLE_REASONING_COMPAT: OpenAICompletionsCompat = {
- ...TOGETHER_BASE_COMPAT,
- thinkingFormat: "together",
- };
- const TOGETHER_REASONING_EFFORT_COMPAT: OpenAICompletionsCompat = {
- ...TOGETHER_BASE_COMPAT,
- supportsReasoningEffort: true,
- thinkingFormat: "openai",
- };
- const TOGETHER_TOGGLE_REASONING_EFFORT_COMPAT: OpenAICompletionsCompat = {
- ...TOGETHER_TOGGLE_REASONING_COMPAT,
- supportsReasoningEffort: true,
- };
- const TOGETHER_REASONING_ONLY_MODELS = new Set([
- "deepseek-ai/DeepSeek-R1",
- "MiniMaxAI/MiniMax-M2.7",
- ]);
- const TOGETHER_REASONING_EFFORT_MODELS = new Set(["openai/gpt-oss-20b", "openai/gpt-oss-120b"]);
- const TOGETHER_TOGGLE_REASONING_EFFORT_MODELS = new Set(["deepseek-ai/DeepSeek-V4-Pro"]);
- const TOGETHER_FIXED_REASONING_LEVEL_MAP = {
- off: null,
- minimal: null,
- low: null,
- medium: null,
- } as const;
- const TOGETHER_REASONING_EFFORT_LEVEL_MAP = {
- off: null,
- minimal: null,
- } as const;
- const TOGETHER_DEEPSEEK_V4_THINKING_LEVEL_MAP = {
- minimal: null,
- low: null,
- medium: null,
- high: "high",
- xhigh: null,
- } as const;
- const TOGETHER_TOGGLE_REASONING_LEVEL_MAP = {
- minimal: null,
- low: null,
- medium: null,
- } as const;
- const AI_GATEWAY_MODELS_URL = "https://ai-gateway.vercel.sh/v1";
- const AI_GATEWAY_BASE_URL = "https://ai-gateway.vercel.sh";
- const NVIDIA_BASE_URL = "https://integrate.api.nvidia.com/v1";
- const NVIDIA_HEADERS = {
- "NVCF-POLL-SECONDS": "3600",
- } as const;
- const NVIDIA_OPENAI_COMPAT: OpenAICompletionsCompat = {
- supportsStore: false,
- supportsDeveloperRole: false,
- supportsReasoningEffort: false,
- maxTokensField: "max_tokens",
- supportsStrictMode: false,
- supportsLongCacheRetention: false,
- };
- const NVIDIA_NIM_UNSUPPORTED_MODELS = new Set([
- "abacusai/dracarys-llama-3.1-70b-instruct",
- "bytedance/seed-oss-36b-instruct",
- "deepseek-ai/deepseek-v4-flash",
- "deepseek-ai/deepseek-v4-pro",
- "google/gemma-2-2b-it",
- "google/gemma-3n-e2b-it",
- "google/gemma-3n-e4b-it",
- "google/gemma-4-31b-it",
- "meta/llama-3.2-1b-instruct",
- "meta/llama-4-maverick-17b-128e-instruct",
- "microsoft/phi-4-mini-instruct",
- "minimaxai/minimax-m2.7",
- "mistralai/mistral-nemotron",
- "nvidia/nemotron-mini-4b-instruct",
- "qwen/qwen3-next-80b-a3b-instruct",
- "qwen/qwen3.5-397b-a17b",
- "sarvamai/sarvam-m",
- "upstage/solar-10.7b-instruct",
- ]);
- const ZAI_TOOL_STREAM_UNSUPPORTED_MODELS = new Set(["glm-4.5", "glm-4.5-air", "glm-4.5-flash", "glm-4.5v"]);
- const EAGER_TOOL_INPUT_STREAMING_UNSUPPORTED_ANTHROPIC_MODELS = new Set([
- "github-copilot:claude-haiku-4.5",
- "github-copilot:claude-sonnet-4",
- "github-copilot:claude-sonnet-4.5",
- ]);
- const DEEPSEEK_V4_THINKING_LEVEL_MAP = {
- minimal: null,
- low: null,
- medium: null,
- high: "high",
- xhigh: "max",
- } as const;
- const ANT_LING_RING_THINKING_LEVEL_MAP = {
- off: null,
- minimal: null,
- low: null,
- medium: null,
- high: "high",
- xhigh: "xhigh",
- } as const;
- const OPENAI_RESPONSES_NONE_REASONING_MODELS = new Set([
- "gpt-5.1",
- "gpt-5.2",
- "gpt-5.3-codex",
- "gpt-5.4",
- "gpt-5.4-mini",
- "gpt-5.4-nano",
- "gpt-5.5",
- "gpt-5.6-sol",
- "gpt-5.6-terra",
- "gpt-5.6-luna",
- ]);
- const MODELS_DEV_OPENAI_UNSUPPORTED_MODEL_IDS = new Set(["gpt-5.6"]);
- function mergeThinkingLevelMap(model: Model<any>, map: NonNullable<Model<any>["thinkingLevelMap"]>): void {
- model.thinkingLevelMap = { ...model.thinkingLevelMap, ...map };
- }
- function getTogetherCompat(modelId: string, reasoning: boolean): OpenAICompletionsCompat {
- if (!reasoning) return TOGETHER_BASE_COMPAT;
- if (TOGETHER_REASONING_EFFORT_MODELS.has(modelId)) return TOGETHER_REASONING_EFFORT_COMPAT;
- if (TOGETHER_TOGGLE_REASONING_EFFORT_MODELS.has(modelId)) return TOGETHER_TOGGLE_REASONING_EFFORT_COMPAT;
- if (TOGETHER_REASONING_ONLY_MODELS.has(modelId)) return TOGETHER_BASE_COMPAT;
- return TOGETHER_TOGGLE_REASONING_COMPAT;
- }
- function getTogetherThinkingLevelMap(
- modelId: string,
- reasoning: boolean,
- ): NonNullable<Model<any>["thinkingLevelMap"]> | undefined {
- if (!reasoning) return undefined;
- if (TOGETHER_REASONING_EFFORT_MODELS.has(modelId)) return { ...TOGETHER_REASONING_EFFORT_LEVEL_MAP };
- if (TOGETHER_TOGGLE_REASONING_EFFORT_MODELS.has(modelId)) return { ...TOGETHER_DEEPSEEK_V4_THINKING_LEVEL_MAP };
- if (TOGETHER_REASONING_ONLY_MODELS.has(modelId)) return { ...TOGETHER_FIXED_REASONING_LEVEL_MAP };
- return { ...TOGETHER_TOGGLE_REASONING_LEVEL_MAP };
- }
- function supportsOpenAiXhigh(modelId: string): boolean {
- return (
- modelId.includes("gpt-5.2") ||
- modelId.includes("gpt-5.3") ||
- modelId.includes("gpt-5.4") ||
- modelId.includes("gpt-5.5") ||
- modelId.includes("gpt-5.6")
- );
- }
- function supportsOpenAiMax(model: Model<Api>): boolean {
- return (
- model.id.includes("gpt-5.6") &&
- (model.api === "openai-responses" ||
- model.api === "azure-openai-responses" ||
- model.api === "openai-codex-responses" ||
- model.api === "openai-completions")
- );
- }
- function isGoogleThinkingApi(model: Model<any>): boolean {
- return model.api === "google-generative-ai" || model.api === "google-vertex";
- }
- function isAnthropicAdaptiveThinkingModel(modelId: string): boolean {
- return (
- modelId.includes("opus-4-6") ||
- modelId.includes("opus-4.6") ||
- modelId.includes("opus-4-7") ||
- modelId.includes("opus-4.7") ||
- modelId.includes("opus-4-8") ||
- modelId.includes("opus-4.8") ||
- modelId.includes("sonnet-4-6") ||
- modelId.includes("sonnet-4.6") ||
- modelId.includes("fable-5")
- );
- }
- function isAnthropicTemperatureUnsupportedModel(modelId: string): boolean {
- const id = modelId.toLowerCase();
- return id.includes("opus-4-7") || id.includes("opus-4.7") || id.includes("opus-4-8") || id.includes("opus-4.8");
- }
- function mergeAnthropicMessagesCompat(model: Model<Api>, compat: AnthropicMessagesCompat): void {
- model.compat = { ...(model.compat as AnthropicMessagesCompat | undefined), ...compat };
- }
- function isGemini3ProModel(modelId: string): boolean {
- return /gemini-3(?:\.\d+)?-pro/.test(modelId.toLowerCase());
- }
- function isGemini3FlashModel(modelId: string): boolean {
- return /gemini-3(?:\.\d+)?-flash/.test(modelId.toLowerCase());
- }
- function isGemma4Model(modelId: string): boolean {
- return /gemma-?4/.test(modelId.toLowerCase());
- }
- function applyThinkingLevelMetadata(model: Model<any>): void {
- if (
- (model.api === "openai-responses" || model.api === "azure-openai-responses") &&
- model.id.startsWith("gpt-5")
- ) {
- mergeThinkingLevelMap(model, { off: null });
- }
- if (model.provider === "github-copilot" && model.id.startsWith("gpt-5")) {
- mergeThinkingLevelMap(model, { minimal: "low" });
- }
- if (
- model.api === "openai-responses" &&
- model.provider === "openai" &&
- OPENAI_RESPONSES_NONE_REASONING_MODELS.has(model.id)
- ) {
- mergeThinkingLevelMap(model, { off: "none" });
- }
- if (supportsOpenAiXhigh(model.id)) {
- mergeThinkingLevelMap(model, { xhigh: "xhigh" });
- }
- if (supportsOpenAiMax(model)) {
- mergeThinkingLevelMap(model, { max: "max" });
- }
- if (model.provider === "openai" && model.id === "gpt-5.5") {
- mergeThinkingLevelMap(model, { minimal: null });
- }
- if (model.id.endsWith("gpt-5.5-pro")) {
- mergeThinkingLevelMap(model, { off: null, minimal: null, low: null });
- }
- if (model.id.includes("opus-4-6") || model.id.includes("opus-4.6")) {
- mergeThinkingLevelMap(model, { xhigh: "max" });
- }
- if (
- model.id.includes("opus-4-7") ||
- model.id.includes("opus-4.7") ||
- model.id.includes("opus-4-8") ||
- model.id.includes("opus-4.8")
- ) {
- mergeThinkingLevelMap(model, { xhigh: "xhigh" });
- }
- if (
- (model.api === "anthropic-messages" || model.api === "bedrock-converse-stream") &&
- model.id.includes("fable-5")
- ) {
- mergeThinkingLevelMap(model, { xhigh: "xhigh" });
- }
- if (model.api === "anthropic-messages" && isAnthropicAdaptiveThinkingModel(model.id)) {
- mergeAnthropicMessagesCompat(model, { forceAdaptiveThinking: true });
- }
- if (model.api === "anthropic-messages" && isAnthropicTemperatureUnsupportedModel(model.id)) {
- mergeAnthropicMessagesCompat(model, { supportsTemperature: false });
- }
- if (model.api === "openai-completions" && model.id.includes("deepseek-v4")) {
- mergeThinkingLevelMap(
- model,
- model.provider === "openrouter"
- ? { ...DEEPSEEK_V4_THINKING_LEVEL_MAP, xhigh: "xhigh" }
- : DEEPSEEK_V4_THINKING_LEVEL_MAP,
- );
- }
- if (isGoogleThinkingApi(model) && isGemini3ProModel(model.id)) {
- mergeThinkingLevelMap(model, { off: null, minimal: null, low: "LOW", medium: null, high: "HIGH" });
- }
- if (isGoogleThinkingApi(model) && isGemini3FlashModel(model.id)) {
- mergeThinkingLevelMap(model, { off: null });
- }
- if (isGoogleThinkingApi(model) && isGemma4Model(model.id)) {
- mergeThinkingLevelMap(model, { off: null, minimal: "MINIMAL", low: null, medium: null, high: "HIGH" });
- }
- if (model.provider === "groq" && model.id === "qwen/qwen3-32b") {
- mergeThinkingLevelMap(model, { minimal: null, low: null, medium: null, high: "default" });
- }
- if (model.provider === "openai-codex" && supportsOpenAiXhigh(model.id)) {
- mergeThinkingLevelMap(model, { minimal: "low" });
- }
- if (model.provider === "openrouter" && model.id.startsWith("inception/mercury-2")) {
- // Mercury 2 in instant mode (reasoning_effort: "none") disables tool calling.
- // Mark "off" unsupported so the openai-completions provider omits the reasoning param
- // instead of defaulting to {reasoning:{effort:"none"}} (see openai-completions.ts:575).
- // Pi's low/medium/high pass through verbatim; OpenRouter normalizes to Mercury's vocabulary.
- mergeThinkingLevelMap(model, { off: null });
- }
- if (model.provider === "opencode-go" && model.id === "kimi-k2.6") {
- // OpenCode Go exposes Kimi K2.6 thinking as on/off, not distinct effort tiers.
- mergeThinkingLevelMap(model, { minimal: null, low: null, medium: null });
- }
- if (model.provider === "opencode" && model.id === "grok-build-0.1") {
- // OpenCode Zen Grok Build reasons by default but rejects explicit reasoningEffort.
- mergeThinkingLevelMap(model, { off: null, minimal: null, low: null, medium: null });
- }
- if (model.provider === "ant-ling" && model.reasoning) {
- // Ring reasons by default. Only high/xhigh have documented explicit effort controls.
- mergeThinkingLevelMap(model, ANT_LING_RING_THINKING_LEVEL_MAP);
- }
- }
- function getAnthropicMessagesCompat(provider: string, modelId: string): AnthropicMessagesCompat | undefined {
- const compat: AnthropicMessagesCompat = {};
- if (EAGER_TOOL_INPUT_STREAMING_UNSUPPORTED_ANTHROPIC_MODELS.has(`${provider}:${modelId}`)) {
- compat.supportsEagerToolInputStreaming = false;
- }
- if (provider === "xiaomi" || provider.startsWith("xiaomi-token-plan-")) {
- compat.allowEmptySignature = true;
- }
- return Object.keys(compat).length > 0 ? compat : undefined;
- }
- function getBedrockBaseUrl(modelId: string): string {
- return modelId.startsWith("eu.")
- ? "https://bedrock-runtime.eu-central-1.amazonaws.com"
- : "https://bedrock-runtime.us-east-1.amazonaws.com";
- }
- function normalizeNvidiaModelId(modelId: string): string {
- return modelId.toLowerCase().replaceAll("_", ".");
- }
- async function fetchNvidiaNimModelIds(): Promise<Map<string, string>> {
- try {
- console.log("Fetching models from NVIDIA NIM API...");
- const response = await fetch(`${NVIDIA_BASE_URL}/models`);
- const data = (await response.json()) as { data?: NvidiaNimModelListItem[] };
- const modelIds = new Map<string, string>();
- for (const model of data.data ?? []) {
- modelIds.set(model.id, model.id);
- modelIds.set(normalizeNvidiaModelId(model.id), model.id);
- }
- console.log(`Fetched ${data.data?.length ?? 0} model IDs from NVIDIA NIM`);
- return modelIds;
- } catch (error) {
- console.error("Failed to fetch NVIDIA NIM models:", error);
- return new Map();
- }
- }
- async function fetchOpenRouterModels(): Promise<Model<any>[]> {
- try {
- console.log("Fetching models from OpenRouter API...");
- const response = await fetch("https://openrouter.ai/api/v1/models");
- const data = await response.json();
- const models: Model<any>[] = [];
- for (const model of data.data) {
- // Only include models that support tools
- if (!model.supported_parameters?.includes("tools")) continue;
- // Parse provider from model ID
- let provider: KnownProvider = "openrouter";
- let modelKey = model.id;
- modelKey = model.id; // Keep full ID for OpenRouter
- // Parse input modalities
- const input: ("text" | "image")[] = ["text"];
- if (model.architecture?.modality?.includes("image")) {
- input.push("image");
- }
- // Convert pricing from $/token to $/million tokens
- const inputCost = parseFloat(model.pricing?.prompt || "0") * 1_000_000;
- const outputCost = parseFloat(model.pricing?.completion || "0") * 1_000_000;
- const cacheReadCost = parseFloat(model.pricing?.input_cache_read || "0") * 1_000_000;
- const cacheWriteCost = parseFloat(model.pricing?.input_cache_write || "0") * 1_000_000;
- const normalizedModel: Model<any> = {
- id: modelKey,
- name: model.name,
- api: "openai-completions",
- baseUrl: "https://openrouter.ai/api/v1",
- provider,
- reasoning: model.supported_parameters?.includes("reasoning") || false,
- input,
- cost: {
- input: inputCost,
- output: outputCost,
- cacheRead: cacheReadCost,
- cacheWrite: cacheWriteCost,
- },
- contextWindow: model.context_length || 4096,
- maxTokens: model.top_provider?.max_completion_tokens || 4096,
- };
- models.push(normalizedModel);
- }
- console.log(`Fetched ${models.length} tool-capable models from OpenRouter`);
- return models;
- } catch (error) {
- console.error("Failed to fetch OpenRouter models:", error);
- return [];
- }
- }
- async function fetchAiGatewayModels(): Promise<Model<any>[]> {
- try {
- console.log("Fetching models from Vercel AI Gateway API...");
- const response = await fetch(`${AI_GATEWAY_MODELS_URL}/models`);
- const data = await response.json();
- const models: Model<any>[] = [];
- const toNumber = (value: string | number | undefined): number => {
- if (typeof value === "number") {
- return Number.isFinite(value) ? value : 0;
- }
- const parsed = parseFloat(value ?? "0");
- return Number.isFinite(parsed) ? parsed : 0;
- };
- const items = Array.isArray(data.data) ? (data.data as AiGatewayModel[]) : [];
- for (const model of items) {
- const tags = Array.isArray(model.tags) ? model.tags : [];
- // Only include models that support tools
- if (!tags.includes("tool-use")) continue;
- const input: ("text" | "image")[] = ["text"];
- if (tags.includes("vision")) {
- input.push("image");
- }
- const inputCost = toNumber(model.pricing?.input) * 1_000_000;
- const outputCost = toNumber(model.pricing?.output) * 1_000_000;
- const cacheReadCost = toNumber(model.pricing?.input_cache_read) * 1_000_000;
- const cacheWriteCost = toNumber(model.pricing?.input_cache_write) * 1_000_000;
- models.push({
- id: model.id,
- name: model.name || model.id,
- api: "anthropic-messages",
- baseUrl: AI_GATEWAY_BASE_URL,
- provider: "vercel-ai-gateway",
- reasoning: tags.includes("reasoning"),
- input,
- cost: {
- input: inputCost,
- output: outputCost,
- cacheRead: cacheReadCost,
- cacheWrite: cacheWriteCost,
- },
- contextWindow: model.context_window || 4096,
- maxTokens: model.max_tokens || 4096,
- });
- }
- console.log(`Fetched ${models.length} tool-capable models from Vercel AI Gateway`);
- return models;
- } catch (error) {
- console.error("Failed to fetch Vercel AI Gateway models:", error);
- return [];
- }
- }
- async function loadModelsDevData(): Promise<Model<any>[]> {
- try {
- console.log("Fetching models from models.dev API...");
- const response = await fetch("https://models.dev/api.json");
- const data = await response.json();
- const models: Model<any>[] = [];
- const nvidiaNimModelIds = data.nvidia?.models ? await fetchNvidiaNimModelIds() : new Map<string, string>();
- // Process Amazon Bedrock models
- if (data["amazon-bedrock"]?.models) {
- for (const [modelId, model] of Object.entries(data["amazon-bedrock"].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- let id = modelId;
- if (id.startsWith("ai21.jamba")) {
- // These models doesn't support tool use in streaming mode
- continue;
- }
- if (id.startsWith("mistral.mistral-7b-instruct-v0")) {
- // These models doesn't support system messages
- continue;
- }
- models.push({
- id,
- name: m.name || id,
- api: "bedrock-converse-stream" as const,
- provider: "amazon-bedrock" as const,
- baseUrl: getBedrockBaseUrl(id),
- reasoning: m.reasoning === true,
- input: (m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"]) as ("text" | "image")[],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Anthropic models
- if (data.anthropic?.models) {
- for (const [modelId, model] of Object.entries(data.anthropic.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "anthropic-messages",
- provider: "anthropic",
- baseUrl: "https://api.anthropic.com",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Google models
- if (data.google?.models) {
- for (const [modelId, model] of Object.entries(data.google.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "google-generative-ai",
- provider: "google",
- baseUrl: "https://generativelanguage.googleapis.com/v1beta",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process OpenAI models
- if (data.openai?.models) {
- for (const [modelId, model] of Object.entries(data.openai.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true || MODELS_DEV_OPENAI_UNSUPPORTED_MODEL_IDS.has(modelId)) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-responses",
- provider: "openai",
- baseUrl: "https://api.openai.com/v1",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Groq models
- if (data.groq?.models) {
- for (const [modelId, model] of Object.entries(data.groq.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "groq",
- baseUrl: "https://api.groq.com/openai/v1",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Cerebras models
- if (data.cerebras?.models) {
- for (const [modelId, model] of Object.entries(data.cerebras.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "cerebras",
- baseUrl: "https://api.cerebras.ai/v1",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Cloudflare Workers AI models
- if (data["cloudflare-workers-ai"]?.models) {
- for (const [modelId, model] of Object.entries(data["cloudflare-workers-ai"].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "cloudflare-workers-ai",
- baseUrl: CLOUDFLARE_WORKERS_AI_BASE_URL,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- compat: { sendSessionAffinityHeaders: true },
- });
- }
- }
- // Process Cloudflare AI Gateway models
- if (data["cloudflare-ai-gateway"]?.models) {
- for (const [prefixedId, model] of Object.entries(data["cloudflare-ai-gateway"].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- const slashIdx = prefixedId.indexOf("/");
- if (slashIdx === -1) continue;
- const upstream = prefixedId.slice(0, slashIdx);
- const nativeId = prefixedId.slice(slashIdx + 1);
- let api: "anthropic-messages" | "openai-completions" | "openai-responses";
- let baseUrl: string;
- let id: string;
- if (upstream === "openai") {
- api = "openai-responses";
- baseUrl = CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL;
- id = nativeId;
- } else if (upstream === "anthropic") {
- api = "anthropic-messages";
- baseUrl = CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL;
- id = nativeId;
- } else if (upstream === "workers-ai") {
- api = "openai-completions";
- baseUrl = CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL;
- id = prefixedId;
- } else {
- continue;
- }
- // workers-ai/* through the gateway forwards x-session-affinity to
- // the underlying Workers AI runtime for prefix-cache routing.
- const compat = upstream === "workers-ai" ? { sendSessionAffinityHeaders: true } : undefined;
- models.push({
- id,
- name: m.name || id,
- api,
- provider: "cloudflare-ai-gateway",
- baseUrl,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- ...(compat ? { compat } : {}),
- });
- }
- }
- // Process xAi models
- if (data.xai?.models) {
- for (const [modelId, model] of Object.entries(data.xai.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "xai",
- baseUrl: "https://api.x.ai/v1",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process zAi models
- const zaiCodingPlanVariants = [
- { provider: "zai", baseUrl: "https://api.z.ai/api/coding/paas/v4" },
- { provider: "zai-coding-cn", baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4" },
- ] as const;
- if (data["zai-coding-plan"]?.models) {
- for (const { provider, baseUrl } of zaiCodingPlanVariants) {
- for (const [modelId, model] of Object.entries(data["zai-coding-plan"].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- const supportsImage = m.modalities?.input?.includes("image");
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider,
- baseUrl,
- reasoning: m.reasoning === true,
- input: supportsImage ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- compat: {
- supportsDeveloperRole: false,
- thinkingFormat: "zai",
- ...(!ZAI_TOOL_STREAM_UNSUPPORTED_MODELS.has(modelId) ? { zaiToolStream: true } : {}),
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- }
- // Process Mistral models
- if (data.mistral?.models) {
- for (const [modelId, model] of Object.entries(data.mistral.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "mistral-conversations",
- provider: "mistral",
- baseUrl: "https://api.mistral.ai",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Hugging Face models
- if (data.huggingface?.models) {
- for (const [modelId, model] of Object.entries(data.huggingface.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "huggingface",
- baseUrl: "https://router.huggingface.co/v1",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- compat: {
- supportsDeveloperRole: false,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Fireworks models
- if (data["fireworks-ai"]?.models) {
- for (const [modelId, model] of Object.entries(data["fireworks-ai"].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "anthropic-messages",
- provider: "fireworks",
- // Fireworks Anthropic-compatible API - SDK appends /v1/messages
- baseUrl: "https://api.fireworks.ai/inference",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- // Fireworks prompt caching uses automatic prefix matching + session affinity.
- // x-session-affinity routes requests to the same replica for cache hits.
- // cache_control on tools and eager_input_streaming are not supported.
- // See: https://docs.fireworks.ai/tools-sdks/anthropic-compatibility
- compat: {
- sendSessionAffinityHeaders: true,
- supportsEagerToolInputStreaming: false,
- supportsCacheControlOnTools: false,
- supportsLongCacheRetention: false,
- },
- });
- }
- }
- // Process NVIDIA NIM models
- if (data.nvidia?.models) {
- for (const [modelId, model] of Object.entries(data.nvidia.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- if (!m.modalities?.input?.includes("text")) continue;
- if (!m.modalities?.output?.includes("text")) continue;
- const liveModelId = nvidiaNimModelIds.get(modelId) ?? nvidiaNimModelIds.get(normalizeNvidiaModelId(modelId));
- if (!liveModelId) continue;
- if (NVIDIA_NIM_UNSUPPORTED_MODELS.has(liveModelId)) continue;
- models.push({
- id: liveModelId,
- name: m.name || liveModelId,
- api: "openai-completions",
- provider: "nvidia",
- baseUrl: NVIDIA_BASE_URL,
- headers: { ...NVIDIA_HEADERS },
- reasoning: m.reasoning === true,
- input: m.modalities.input.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- compat: NVIDIA_OPENAI_COMPAT,
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Together AI models
- const togetherProvider = data.together ?? data.togetherai ?? data["together-ai"];
- if (togetherProvider?.models) {
- for (const [modelId, model] of Object.entries(togetherProvider.models)) {
- const m = model as ModelsDevModel & { status?: string };
- if (m.tool_call !== true) continue;
- if (m.status === "deprecated") continue;
- const reasoning = m.reasoning === true;
- const thinkingLevelMap = getTogetherThinkingLevelMap(modelId, reasoning);
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider: "together",
- baseUrl: TOGETHER_BASE_URL,
- reasoning,
- ...(thinkingLevelMap ? { thinkingLevelMap } : {}),
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- compat: getTogetherCompat(modelId, reasoning),
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process OpenCode models (Zen and Go)
- // API mapping based on provider.npm field:
- // - @ai-sdk/openai → openai-responses
- // - @ai-sdk/anthropic → anthropic-messages
- // - @ai-sdk/google → google-generative-ai
- // - null/undefined/@ai-sdk/openai-compatible → openai-completions
- const opencodeVariants = [
- { key: "opencode", provider: "opencode", basePath: "https://opencode.ai/zen" },
- { key: "opencode-go", provider: "opencode-go", basePath: "https://opencode.ai/zen/go" },
- ] as const;
- for (const variant of opencodeVariants) {
- if (!data[variant.key]?.models) continue;
- for (const [modelId, model] of Object.entries(data[variant.key].models)) {
- const m = model as ModelsDevModel & { status?: string };
- if (m.tool_call !== true) continue;
- if (m.status === "deprecated") continue;
- const npm = m.provider?.npm;
- let api: Api;
- let baseUrl: string;
- let compat: OpenAICompletionsCompat | undefined;
- if (npm === "@ai-sdk/openai") {
- api = "openai-responses";
- baseUrl = `${variant.basePath}/v1`;
- } else if (npm === "@ai-sdk/anthropic") {
- api = "anthropic-messages";
- // Anthropic SDK appends /v1/messages to baseURL
- baseUrl = variant.basePath;
- } else if (npm === "@ai-sdk/google") {
- api = "google-generative-ai";
- baseUrl = `${variant.basePath}/v1`;
- } else if (npm === "@ai-sdk/alibaba") {
- api = "openai-completions";
- baseUrl = `${variant.basePath}/v1`;
- compat = { cacheControlFormat: "anthropic" };
- } else {
- // null, undefined, or @ai-sdk/openai-compatible
- api = "openai-completions";
- baseUrl = `${variant.basePath}/v1`;
- }
- if (variant.provider === "opencode" && modelId === "grok-build-0.1") {
- compat = { ...(compat ?? {}), supportsReasoningEffort: false };
- }
- if ((variant.provider === "opencode" || variant.provider === "opencode-go") && modelId === "kimi-k2.6") {
- // OpenCode Kimi K2.6 accepts Anthropic-style thinking objects
- // and rejects string thinking values or combined reasoning_effort.
- compat = { ...(compat ?? {}), thinkingFormat: "deepseek", supportsReasoningEffort: false };
- }
- // Fix known mismatches between models.dev npm data and actual
- // OpenCode Go endpoint behaviour. models.dev reports these models
- // as @ai-sdk/anthropic, but the OpenCode Go endpoints either don't
- // accept Anthropic SDK auth (MiniMax M2.7) or are served through
- // the OpenAI-compatible /v1/chat/completions path (Qwen 3.5/3.6).
- // Switch them to openai-completions so requests use Bearer auth
- // and the standard /v1/chat/completions endpoint.
- if (variant.provider === "opencode-go") {
- if (modelId === "minimax-m2.7") {
- api = "openai-completions";
- baseUrl = `${variant.basePath}/v1`;
- }
- if (modelId === "qwen3.5-plus" || modelId === "qwen3.6-plus") {
- api = "openai-completions";
- baseUrl = `${variant.basePath}/v1`;
- // Qwen/DashScope uses enable_thinking at the top level.
- compat = { ...(compat ?? {}), thinkingFormat: "qwen" };
- }
- }
- if (api === "openai-completions") {
- compat = { ...(compat ?? {}), maxTokensField: "max_tokens" };
- }
- models.push({
- id: modelId,
- name: m.name || modelId,
- api,
- provider: variant.provider,
- baseUrl,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- ...(compat ? { compat } : {}),
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process GitHub Copilot models
- if (data["github-copilot"]?.models) {
- for (const [modelId, model] of Object.entries(data["github-copilot"].models)) {
- const m = model as ModelsDevModel & { status?: string };
- if (m.tool_call !== true) continue;
- if (m.status === "deprecated") continue;
- // Claude 4.x models route to Anthropic Messages API
- const isCopilotClaude4 = /^claude-(haiku|sonnet|opus)-4([.\-]|$)/.test(modelId);
- // gpt-5 models require responses API, others use completions
- const needsResponsesApi = modelId.startsWith("gpt-5") || modelId.startsWith("oswe");
- const api: Api = isCopilotClaude4
- ? "anthropic-messages"
- : needsResponsesApi
- ? "openai-responses"
- : "openai-completions";
- const anthropicCompat =
- api === "anthropic-messages" ? getAnthropicMessagesCompat("github-copilot", modelId) : undefined;
- const copilotModel: Model<any> = {
- id: modelId,
- name: m.name || modelId,
- api,
- provider: "github-copilot",
- baseUrl: "https://api.individual.githubcopilot.com",
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 128000,
- maxTokens: m.limit?.output || 8192,
- headers: { ...COPILOT_STATIC_HEADERS },
- ...(anthropicCompat ? { compat: anthropicCompat } : {}),
- // compat only applies to openai-completions
- ...(api === "openai-completions" ? {
- compat: {
- supportsStore: false,
- supportsDeveloperRole: false,
- supportsReasoningEffort: false,
- },
- } : {}),
- };
- models.push(copilotModel);
- }
- }
- // Process MiniMax models
- const minimaxVariants = [
- { key: "minimax", provider: "minimax", baseUrl: "https://api.minimax.io/anthropic" },
- { key: "minimax-cn", provider: "minimax-cn", baseUrl: "https://api.minimaxi.com/anthropic" },
- ] as const;
- for (const { key, provider, baseUrl } of minimaxVariants) {
- if (data[key]?.models) {
- for (const [modelId, model] of Object.entries(data[key].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "anthropic-messages",
- provider,
- // MiniMax's Anthropic-compatible API - SDK appends /v1/messages
- baseUrl,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- }
- // Process Kimi For Coding models
- if (data["kimi-for-coding"]?.models) {
- const kimiModels = data["kimi-for-coding"].models as Record<string, ModelsDevModel>;
- const hasCanonicalModel = Object.prototype.hasOwnProperty.call(kimiModels, "kimi-for-coding");
- const kimiAliases = new Set(["k2p5", "k2p6"]);
- for (const [modelId, model] of Object.entries(kimiModels)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- // models.dev may expose versioned aliases (e.g. k2p5/k2p6).
- // Normalize aliases to the canonical model id and drop duplicates when canonical exists.
- if (kimiAliases.has(modelId) && hasCanonicalModel) continue;
- const normalizedId = kimiAliases.has(modelId) ? "kimi-for-coding" : modelId;
- const normalizedName = kimiAliases.has(modelId) ? "Kimi For Coding" : m.name || normalizedId;
- models.push({
- id: normalizedId,
- name: normalizedName,
- api: "anthropic-messages",
- provider: "kimi-coding",
- // Kimi For Coding's Anthropic-compatible API - SDK appends /v1/messages
- baseUrl: "https://api.kimi.com/coding",
- headers: { ...KIMI_STATIC_HEADERS },
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- // Process Moonshot AI models
- const moonshotVariants = [
- { key: "moonshotai", provider: "moonshotai", baseUrl: "https://api.moonshot.ai/v1" },
- { key: "moonshotai-cn", provider: "moonshotai-cn", baseUrl: "https://api.moonshot.cn/v1" },
- ] as const;
- const moonshotCompat: OpenAICompletionsCompat = {
- supportsStore: false,
- supportsDeveloperRole: false,
- supportsReasoningEffort: false,
- maxTokensField: "max_tokens",
- supportsStrictMode: false,
- thinkingFormat: "deepseek",
- };
- for (const { key, provider, baseUrl } of moonshotVariants) {
- if (!data[key]?.models) continue;
- for (const [modelId, model] of Object.entries(data[key].models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider,
- baseUrl,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- compat: moonshotCompat,
- });
- }
- }
- // Process Xiaomi MiMo models
- // Built-in `xiaomi` targets the API billing endpoint (single stable URL,
- // keys from platform.xiaomimimo.com). The three `xiaomi-token-plan-*`
- // providers cover prepaid Token Plan endpoints in cn / ams / sgp.
- const xiaomiCompat: OpenAICompletionsCompat = {
- requiresReasoningContentOnAssistantMessages: true,
- thinkingFormat: "deepseek",
- };
- const xiaomiVariants = [
- { provider: "xiaomi", baseUrl: "https://api.xiaomimimo.com/v1" },
- { provider: "xiaomi-token-plan-cn", baseUrl: "https://token-plan-cn.xiaomimimo.com/v1" },
- { provider: "xiaomi-token-plan-ams", baseUrl: "https://token-plan-ams.xiaomimimo.com/v1" },
- { provider: "xiaomi-token-plan-sgp", baseUrl: "https://token-plan-sgp.xiaomimimo.com/v1" },
- ] as const;
- if (data.xiaomi?.models) {
- for (const { provider, baseUrl } of xiaomiVariants) {
- for (const [modelId, model] of Object.entries(data.xiaomi.models)) {
- const m = model as ModelsDevModel;
- if (m.tool_call !== true) continue;
- if (provider.startsWith("xiaomi-token-plan-") && modelId === "mimo-v2-flash") continue;
- models.push({
- id: modelId,
- name: m.name || modelId,
- api: "openai-completions",
- provider,
- baseUrl,
- compat: xiaomiCompat,
- reasoning: m.reasoning === true,
- input: m.modalities?.input?.includes("image") ? ["text", "image"] : ["text"],
- cost: {
- input: m.cost?.input || 0,
- output: m.cost?.output || 0,
- cacheRead: m.cost?.cache_read || 0,
- cacheWrite: m.cost?.cache_write || 0,
- },
- contextWindow: m.limit?.context || 4096,
- maxTokens: m.limit?.output || 4096,
- });
- }
- }
- }
- console.log(`Loaded ${models.length} tool-capable models from models.dev`);
- return models;
- } catch (error) {
- console.error("Failed to load models.dev data:", error);
- return [];
- }
- }
- async function generateModels() {
- // Fetch models from both sources
- // models.dev: Anthropic, Google, OpenAI, Groq, Cerebras
- // OpenRouter: xAI and other providers (excluding Anthropic, Google, OpenAI)
- // AI Gateway: OpenAI-compatible catalog with tool-capable models
- const modelsDevModels = await loadModelsDevData();
- const openRouterModels = await fetchOpenRouterModels();
- const aiGatewayModels = await fetchAiGatewayModels();
- // Combine models (models.dev has priority)
- const allModels = [...modelsDevModels, ...openRouterModels, ...aiGatewayModels].filter(
- (model) =>
- !((model.provider === "opencode" || model.provider === "opencode-go") && model.id === "gpt-5.3-codex-spark"),
- );
- // Fix incorrect cache pricing for Claude Opus 4.5 from models.dev
- // models.dev has 3x the correct pricing (1.5/18.75 instead of 0.5/6.25)
- const opus45 = allModels.find(m => m.provider === "anthropic" && m.id === "claude-opus-4-5");
- if (opus45) {
- opus45.cost.cacheRead = 0.5;
- opus45.cost.cacheWrite = 6.25;
- }
- // Temporary overrides until upstream model metadata is corrected.
- for (const candidate of allModels) {
- if (candidate.provider === "amazon-bedrock" && candidate.id.includes("anthropic.claude-opus-4-6-v1")) {
- candidate.cost.cacheRead = 0.5;
- candidate.cost.cacheWrite = 6.25;
- }
- if (
- (candidate.provider === "anthropic" ||
- candidate.provider === "opencode" ||
- candidate.provider === "opencode-go" ||
- candidate.provider === "github-copilot") &&
- (candidate.id === "claude-opus-4-6" ||
- candidate.id === "claude-sonnet-4-6" ||
- candidate.id === "claude-opus-4.6" ||
- candidate.id === "claude-sonnet-4.6")
- ) {
- candidate.contextWindow = 1000000;
- }
- // OpenCode variants list Claude Sonnet 4/4.5 with 1M context, actual limit is 200K
- if (
- (candidate.provider === "opencode" || candidate.provider === "opencode-go") &&
- (candidate.id === "claude-sonnet-4-5" || candidate.id === "claude-sonnet-4")
- ) {
- candidate.contextWindow = 200000;
- }
- if ((candidate.provider === "opencode" || candidate.provider === "opencode-go") && candidate.id === "gpt-5.4") {
- candidate.contextWindow = 272000;
- candidate.maxTokens = 128000;
- }
- if (candidate.provider === "openai" && (candidate.id === "gpt-5.4" || candidate.id === "gpt-5.5")) {
- candidate.contextWindow = 272000;
- candidate.maxTokens = 128000;
- }
- // models.dev reports gpt-5-pro output as 272000 (a duplicate of the input sub-limit);
- // the actual max output is 128000. Also propagates to the derived Azure clone.
- if (candidate.provider === "openai" && candidate.id === "gpt-5-pro") {
- candidate.maxTokens = 128000;
- }
- // Keep selected OpenRouter model metadata stable until upstream settles.
- if (candidate.provider === "openrouter" && candidate.id === "moonshotai/kimi-k2.5") {
- candidate.cost.input = 0.41;
- candidate.cost.output = 2.06;
- candidate.cost.cacheRead = 0.07;
- candidate.maxTokens = 4096;
- }
- if (candidate.provider === "openrouter" && candidate.id.startsWith("moonshotai/kimi-k2.6")) {
- candidate.compat = {
- ...candidate.compat,
- supportsDeveloperRole: false,
- requiresReasoningContentOnAssistantMessages: true,
- };
- }
- if (candidate.provider === "openrouter" && candidate.id === "z-ai/glm-5") {
- candidate.cost.input = 0.6;
- candidate.cost.output = 1.9;
- candidate.cost.cacheRead = 0.119;
- }
- }
- // Add missing EU Opus 4.6 profile
- if (!allModels.some((m) => m.provider === "amazon-bedrock" && m.id === "eu.anthropic.claude-opus-4-6-v1")) {
- allModels.push({
- id: "eu.anthropic.claude-opus-4-6-v1",
- name: "Claude Opus 4.6 (EU)",
- api: "bedrock-converse-stream",
- provider: "amazon-bedrock",
- baseUrl: getBedrockBaseUrl("eu.anthropic.claude-opus-4-6-v1"),
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 5,
- output: 25,
- cacheRead: 0.5,
- cacheWrite: 6.25,
- },
- contextWindow: 200000,
- maxTokens: 128000,
- });
- }
- // Add missing Claude Opus 4.6
- if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-6")) {
- allModels.push({
- id: "claude-opus-4-6",
- name: "Claude Opus 4.6",
- api: "anthropic-messages",
- baseUrl: "https://api.anthropic.com",
- provider: "anthropic",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 5,
- output: 25,
- cacheRead: 0.5,
- cacheWrite: 6.25,
- },
- contextWindow: 1000000,
- maxTokens: 128000,
- });
- }
- // Add missing Claude Opus 4.7
- if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-7")) {
- allModels.push({
- id: "claude-opus-4-7",
- name: "Claude Opus 4.7",
- api: "anthropic-messages",
- baseUrl: "https://api.anthropic.com",
- provider: "anthropic",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 5,
- output: 25,
- cacheRead: 0.5,
- cacheWrite: 6.25,
- },
- contextWindow: 1000000,
- maxTokens: 128000,
- });
- }
- // Add missing Claude Opus 4.8
- if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-opus-4-8")) {
- allModels.push({
- id: "claude-opus-4-8",
- name: "Claude Opus 4.8",
- api: "anthropic-messages",
- baseUrl: "https://api.anthropic.com",
- provider: "anthropic",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 5,
- output: 25,
- cacheRead: 0.5,
- cacheWrite: 6.25,
- },
- contextWindow: 1000000,
- maxTokens: 128000,
- });
- }
- // Add missing Claude Sonnet 4.6
- if (!allModels.some(m => m.provider === "anthropic" && m.id === "claude-sonnet-4-6")) {
- allModels.push({
- id: "claude-sonnet-4-6",
- name: "Claude Sonnet 4.6",
- api: "anthropic-messages",
- baseUrl: "https://api.anthropic.com",
- provider: "anthropic",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 3,
- output: 15,
- cacheRead: 0.3,
- cacheWrite: 3.75,
- },
- contextWindow: 1000000,
- maxTokens: 64000,
- });
- }
- // Add missing Gemini 3.1 Flash Lite Preview until models.dev includes it.
- if (!allModels.some((m) => m.provider === "google" && m.id === "gemini-3.1-flash-lite-preview")) {
- allModels.push({
- id: "gemini-3.1-flash-lite-preview",
- name: "Gemini 3.1 Flash Lite Preview",
- api: "google-generative-ai",
- baseUrl: "https://generativelanguage.googleapis.com/v1beta",
- provider: "google",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 0,
- output: 0,
- cacheRead: 0,
- cacheWrite: 0,
- },
- contextWindow: 1048576,
- maxTokens: 65536,
- });
- }
- // Add missing gpt models
- const missingOpenAiModels: Model<"openai-responses">[] = [
- {
- id: "gpt-5.6-sol",
- name: "GPT-5.6 Sol",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
- contextWindow: 272000,
- maxTokens: 128000,
- },
- {
- id: "gpt-5.6-terra",
- name: "GPT-5.6 Terra",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
- contextWindow: 272000,
- maxTokens: 128000,
- },
- {
- id: "gpt-5.6-luna",
- name: "GPT-5.6 Luna",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 1, output: 6, cacheRead: 0.1, cacheWrite: 1.25 },
- contextWindow: 272000,
- maxTokens: 128000,
- },
- {
- id: "gpt-5-chat-latest",
- name: "GPT-5 Chat Latest",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: false,
- input: ["text", "image"],
- cost: {
- input: 1.25,
- output: 10,
- cacheRead: 0.125,
- cacheWrite: 0,
- },
- contextWindow: 128000,
- maxTokens: 16384,
- },
- ];
- for (const model of missingOpenAiModels) {
- if (!allModels.some((candidate) => candidate.provider === model.provider && candidate.id === model.id)) {
- allModels.push(model);
- }
- }
- if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.1-codex")) {
- allModels.push({
- id: "gpt-5.1-codex",
- name: "GPT-5.1 Codex",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 1.25,
- output: 5,
- cacheRead: 0.125,
- cacheWrite: 1.25,
- },
- contextWindow: 400000,
- maxTokens: 128000,
- });
- }
- if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.1-codex-max")) {
- allModels.push({
- id: "gpt-5.1-codex-max",
- name: "GPT-5.1 Codex Max",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 1.25,
- output: 10,
- cacheRead: 0.125,
- cacheWrite: 0,
- },
- contextWindow: 400000,
- maxTokens: 128000,
- });
- }
- if (!allModels.some(m => m.provider === "openai" && m.id === "gpt-5.3-codex-spark")) {
- allModels.push({
- id: "gpt-5.3-codex-spark",
- name: "GPT-5.3 Codex Spark",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text"],
- cost: {
- input: 0,
- output: 0,
- cacheRead: 0,
- cacheWrite: 0,
- },
- contextWindow: 128000,
- maxTokens: 16384,
- });
- }
- // Add missing GitHub Copilot GPT-5.3 models until models.dev includes them.
- const copilotBaseModel = allModels.find(
- (m) => m.provider === "github-copilot" && m.id === "gpt-5.2-codex",
- );
- if (copilotBaseModel) {
- if (!allModels.some((m) => m.provider === "github-copilot" && m.id === "gpt-5.3-codex")) {
- allModels.push({
- ...copilotBaseModel,
- id: "gpt-5.3-codex",
- name: "GPT-5.3 Codex",
- });
- }
- }
- if (!allModels.some((m) => m.provider === "openai" && m.id === "gpt-5.4")) {
- allModels.push({
- id: "gpt-5.4",
- name: "GPT-5.4",
- api: "openai-responses",
- baseUrl: "https://api.openai.com/v1",
- provider: "openai",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 2.5,
- output: 15,
- cacheRead: 0.25,
- cacheWrite: 0,
- },
- contextWindow: 272000,
- maxTokens: 128000,
- });
- }
- const deepseekCompat: OpenAICompletionsCompat = {
- requiresReasoningContentOnAssistantMessages: true,
- thinkingFormat: "deepseek",
- };
- const deepseekV4Models: Model<"openai-completions">[] = [
- {
- id: "deepseek-v4-flash",
- name: "DeepSeek V4 Flash",
- api: "openai-completions",
- baseUrl: "https://api.deepseek.com",
- provider: "deepseek",
- reasoning: true,
- input: ["text"],
- cost: {
- input: 0.14,
- output: 0.28,
- cacheRead: 0.0028,
- cacheWrite: 0,
- },
- contextWindow: 1000000,
- maxTokens: 384000,
- compat: deepseekCompat,
- },
- {
- id: "deepseek-v4-pro",
- name: "DeepSeek V4 Pro",
- api: "openai-completions",
- baseUrl: "https://api.deepseek.com",
- provider: "deepseek",
- reasoning: true,
- input: ["text"],
- cost: {
- input: 0.435,
- output: 0.87,
- cacheRead: 0.003625,
- cacheWrite: 0,
- },
- contextWindow: 1000000,
- maxTokens: 384000,
- compat: deepseekCompat,
- },
- ];
- allModels.push(...deepseekV4Models);
- const antLingCompat: OpenAICompletionsCompat = {
- supportsStore: false,
- supportsDeveloperRole: false,
- supportsReasoningEffort: false,
- maxTokensField: "max_tokens",
- supportsLongCacheRetention: false,
- };
- const antLingModels: Model<"openai-completions">[] = [
- {
- id: "Ling-2.6-flash",
- name: "Ling 2.6 Flash",
- api: "openai-completions",
- baseUrl: "https://api.ant-ling.com/v1",
- provider: "ant-ling",
- reasoning: false,
- input: ["text"],
- cost: { input: 0.01, output: 0.02, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 262144,
- maxTokens: 65536,
- compat: antLingCompat,
- },
- {
- id: "Ling-2.6-1T",
- name: "Ling 2.6 1T",
- api: "openai-completions",
- baseUrl: "https://api.ant-ling.com/v1",
- provider: "ant-ling",
- reasoning: false,
- input: ["text"],
- cost: { input: 0.06, output: 0.25, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 262144,
- maxTokens: 65536,
- compat: antLingCompat,
- },
- {
- id: "Ring-2.6-1T",
- name: "Ring 2.6 1T",
- api: "openai-completions",
- baseUrl: "https://api.ant-ling.com/v1",
- provider: "ant-ling",
- reasoning: true,
- input: ["text"],
- cost: { input: 0.06, output: 0.25, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 262144,
- maxTokens: 65536,
- compat: { ...antLingCompat, thinkingFormat: "ant-ling" },
- },
- ];
- allModels.push(...antLingModels);
- for (const candidate of allModels) {
- if (candidate.api === "openai-completions" && candidate.id.includes("deepseek-v4")) {
- candidate.compat = {
- ...candidate.compat,
- ...(candidate.provider === "openrouter"
- ? {
- requiresReasoningContentOnAssistantMessages:
- deepseekCompat.requiresReasoningContentOnAssistantMessages,
- }
- : deepseekCompat),
- };
- }
- }
- const minimaxDirectSupportedIds = new Set(["MiniMax-M2.7", "MiniMax-M2.7-highspeed", "MiniMax-M3"]);
- for (let i = allModels.length - 1; i >= 0; i--) {
- const candidate = allModels[i];
- if (
- (candidate.provider === "minimax" || candidate.provider === "minimax-cn") &&
- !minimaxDirectSupportedIds.has(candidate.id)
- ) {
- allModels.splice(i, 1);
- }
- }
- // OpenAI Codex (ChatGPT OAuth) models
- // NOTE: These are not fetched from models.dev; we keep a small, explicit list to avoid aliases.
- // Older model limits are based on observed server behavior; GPT-5.6 follows Codex's 372k catalog limit.
- const CODEX_BASE_URL = "https://chatgpt.com/backend-api";
- const CODEX_CONTEXT = 272000;
- const CODEX_GPT_56_CONTEXT = 372000;
- const CODEX_SPARK_CONTEXT = 128000;
- const CODEX_MAX_TOKENS = 128000;
- const codexModels: Model<"openai-codex-responses">[] = [
- {
- id: "gpt-5.3-codex-spark",
- name: "GPT-5.3 Codex Spark",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text"],
- cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
- contextWindow: CODEX_SPARK_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.4",
- name: "GPT-5.4",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 0 },
- contextWindow: CODEX_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.4-mini",
- name: "GPT-5.4 mini",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
- contextWindow: CODEX_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.5",
- name: "GPT-5.5",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
- contextWindow: CODEX_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.6-luna",
- name: "GPT-5.6 Luna",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 1, output: 6, cacheRead: 0.1, cacheWrite: 1.25 },
- contextWindow: CODEX_GPT_56_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.6-sol",
- name: "GPT-5.6 Sol",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
- contextWindow: CODEX_GPT_56_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- {
- id: "gpt-5.6-terra",
- name: "GPT-5.6 Terra",
- api: "openai-codex-responses",
- provider: "openai-codex",
- baseUrl: CODEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 },
- contextWindow: CODEX_GPT_56_CONTEXT,
- maxTokens: CODEX_MAX_TOKENS,
- },
- ];
- allModels.push(...codexModels);
- // Add missing Grok models
- const missingGrokModels: Model<"openai-completions">[] = [
- {
- id: "grok-3",
- name: "Grok 3",
- api: "openai-completions",
- baseUrl: "https://api.x.ai/v1",
- provider: "xai",
- reasoning: false,
- input: ["text"],
- cost: { input: 3, output: 15, cacheRead: 0.75, cacheWrite: 0 },
- contextWindow: 131072,
- maxTokens: 8192,
- },
- {
- id: "grok-3-fast",
- name: "Grok 3 Fast",
- api: "openai-completions",
- baseUrl: "https://api.x.ai/v1",
- provider: "xai",
- reasoning: false,
- input: ["text"],
- cost: { input: 5, output: 25, cacheRead: 1.25, cacheWrite: 0 },
- contextWindow: 131072,
- maxTokens: 8192,
- },
- {
- id: "grok-code-fast-1",
- name: "Grok Code Fast 1",
- api: "openai-completions",
- baseUrl: "https://api.x.ai/v1",
- provider: "xai",
- reasoning: false,
- input: ["text"],
- cost: {
- input: 0.2,
- output: 1.5,
- cacheRead: 0.02,
- cacheWrite: 0,
- },
- contextWindow: 32768,
- maxTokens: 8192,
- },
- ];
- for (const model of missingGrokModels) {
- if (!allModels.some(m => m.provider === model.provider && m.id === model.id)) {
- allModels.push(model);
- }
- }
- // Add missing Mistral Medium 3.5 model until models.dev includes it
- if (!allModels.some(m => m.provider === "mistral" && m.id === "mistral-medium-3.5")) {
- allModels.push({
- id: "mistral-medium-3.5",
- name: "Mistral Medium 3.5",
- api: "mistral-conversations",
- provider: "mistral",
- baseUrl: "https://api.mistral.ai",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- input: 1.5,
- output: 7.5,
- cacheRead: 0,
- cacheWrite: 0,
- },
- contextWindow: 262144, // 256k tokens
- maxTokens: 262144,
- });
- }
- // Add "auto" alias for openrouter/auto
- if (!allModels.some(m => m.provider === "openrouter" && m.id === "auto")) {
- allModels.push({
- id: "auto",
- name: "Auto",
- api: "openai-completions",
- provider: "openrouter",
- baseUrl: "https://openrouter.ai/api/v1",
- reasoning: true,
- input: ["text", "image"],
- cost: {
- // we dont know about the costs because OpenRouter auto routes to different models
- // and then charges you for the underlying used model
- input:0,
- output:0,
- cacheRead:0,
- cacheWrite:0,
- },
- contextWindow: 2000000,
- maxTokens: 30000,
- });
- }
- const VERTEX_BASE_URL = "https://{location}-aiplatform.googleapis.com";
- const vertexModels: Model<"google-vertex">[] = [
- {
- id: "gemini-3-pro-preview",
- name: "Gemini 3 Pro Preview (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
- contextWindow: 1000000,
- maxTokens: 64000,
- },
- {
- id: "gemini-3.1-pro-preview",
- name: "Gemini 3.1 Pro Preview (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-3.1-pro-preview-customtools",
- name: "Gemini 3.1 Pro Preview Custom Tools (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-3-flash-preview",
- name: "Gemini 3 Flash Preview (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.5, output: 3, cacheRead: 0.05, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-2.0-flash",
- name: "Gemini 2.0 Flash (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: false,
- input: ["text", "image"],
- cost: { input: 0.15, output: 0.6, cacheRead: 0.0375, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 8192,
- },
- {
- id: "gemini-2.0-flash-lite",
- name: "Gemini 2.0 Flash Lite (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.075, output: 0.3, cacheRead: 0.01875, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-2.5-pro",
- name: "Gemini 2.5 Pro (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-2.5-flash",
- name: "Gemini 2.5 Flash (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.3, output: 2.5, cacheRead: 0.03, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-2.5-flash-lite-preview-09-2025",
- name: "Gemini 2.5 Flash Lite Preview 09-25 (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.1, output: 0.4, cacheRead: 0.01, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-2.5-flash-lite",
- name: "Gemini 2.5 Flash Lite (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: true,
- input: ["text", "image"],
- cost: { input: 0.1, output: 0.4, cacheRead: 0.01, cacheWrite: 0 },
- contextWindow: 1048576,
- maxTokens: 65536,
- },
- {
- id: "gemini-1.5-pro",
- name: "Gemini 1.5 Pro (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: false,
- input: ["text", "image"],
- cost: { input: 1.25, output: 5, cacheRead: 0.3125, cacheWrite: 0 },
- contextWindow: 1000000,
- maxTokens: 8192,
- },
- {
- id: "gemini-1.5-flash",
- name: "Gemini 1.5 Flash (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: false,
- input: ["text", "image"],
- cost: { input: 0.075, output: 0.3, cacheRead: 0.01875, cacheWrite: 0 },
- contextWindow: 1000000,
- maxTokens: 8192,
- },
- {
- id: "gemini-1.5-flash-8b",
- name: "Gemini 1.5 Flash-8B (Vertex)",
- api: "google-vertex",
- provider: "google-vertex",
- baseUrl: VERTEX_BASE_URL,
- reasoning: false,
- input: ["text", "image"],
- cost: { input: 0.0375, output: 0.15, cacheRead: 0.01, cacheWrite: 0 },
- contextWindow: 1000000,
- maxTokens: 8192,
- },
- ];
- allModels.push(...vertexModels);
- // Azure Foundry deploys these with larger context windows than OpenAI's own API,
- // which caps gpt-5.4/gpt-5.5 at 272k. See models-sold-directly-by-azure docs.
- const AZURE_CONTEXT_WINDOW_OVERRIDES: Record<string, number> = {
- "gpt-5.4": 1050000,
- "gpt-5.5": 1050000,
- };
- const azureOpenAiModels: Model<Api>[] = allModels
- .filter((model) => model.provider === "openai" && model.api === "openai-responses")
- .map((model) => ({
- ...model,
- api: "azure-openai-responses",
- provider: "azure-openai-responses",
- baseUrl: "",
- contextWindow: AZURE_CONTEXT_WINDOW_OVERRIDES[model.id] ?? model.contextWindow,
- }));
- allModels.push(...azureOpenAiModels);
- for (const model of allModels) {
- applyThinkingLevelMetadata(model);
- }
- // Group by provider and deduplicate by model ID
- const providers: Record<string, Record<string, Model<any>>> = {};
- for (const model of allModels) {
- if (!providers[model.provider]) {
- providers[model.provider] = {};
- }
- // Use model ID as key to automatically deduplicate
- // Only add if not already present (models.dev takes priority over OpenRouter)
- if (!providers[model.provider][model.id]) {
- providers[model.provider][model.id] = model;
- }
- }
- // Generate TypeScript file
- let output = `// This file is auto-generated by scripts/generate-models.ts
- // Do not edit manually - run 'npm run generate-models' to update
- import type { Model } from "./types.ts";
- export const MODELS = {
- `;
- // Generate provider sections (sorted for deterministic output)
- const sortedProviderIds = Object.keys(providers).sort();
- for (const providerId of sortedProviderIds) {
- const models = providers[providerId];
- output += `\t${JSON.stringify(providerId)}: {\n`;
- const sortedModelIds = Object.keys(models).sort();
- for (const modelId of sortedModelIds) {
- const model = models[modelId];
- output += `\t\t"${model.id}": {\n`;
- output += `\t\t\tid: "${model.id}",\n`;
- output += `\t\t\tname: "${model.name}",\n`;
- output += `\t\t\tapi: "${model.api}",\n`;
- output += `\t\t\tprovider: "${model.provider}",\n`;
- if (model.baseUrl !== undefined) {
- output += `\t\t\tbaseUrl: "${model.baseUrl}",\n`;
- }
- if (model.headers) {
- output += `\t\t\theaders: ${JSON.stringify(model.headers)},\n`;
- }
- if (model.compat) {
- output += ` compat: ${JSON.stringify(model.compat)},
- `;
- }
- output += `\t\t\treasoning: ${model.reasoning},\n`;
- if (model.thinkingLevelMap) {
- output += `\t\t\tthinkingLevelMap: ${JSON.stringify(model.thinkingLevelMap)},\n`;
- }
- output += `\t\t\tinput: [${model.input.map(i => `"${i}"`).join(", ")}],\n`;
- output += `\t\t\tcost: {\n`;
- output += `\t\t\t\tinput: ${model.cost.input},\n`;
- output += `\t\t\t\toutput: ${model.cost.output},\n`;
- output += `\t\t\t\tcacheRead: ${model.cost.cacheRead},\n`;
- output += `\t\t\t\tcacheWrite: ${model.cost.cacheWrite},\n`;
- output += `\t\t\t},\n`;
- output += `\t\t\tcontextWindow: ${model.contextWindow},\n`;
- output += `\t\t\tmaxTokens: ${model.maxTokens},\n`;
- output += `\t\t} satisfies Model<"${model.api}">,\n`;
- }
- output += `\t},\n`;
- }
- output += `} as const;
- `;
- // Write file
- writeFileSync(join(packageRoot, "src/models.generated.ts"), output);
- console.log("Generated src/models.generated.ts");
- // Print statistics
- const totalModels = allModels.length;
- const reasoningModels = allModels.filter(m => m.reasoning).length;
- console.log(`\nModel Statistics:`);
- console.log(` Total tool-capable models: ${totalModels}`);
- console.log(` Reasoning-capable models: ${reasoningModels}`);
- for (const [provider, models] of Object.entries(providers)) {
- console.log(` ${provider}: ${Object.keys(models).length} models`);
- }
- }
- // Run the generator
- generateModels().catch(console.error);
|