@aryee337/aery-ai 0.1.148 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (592) hide show
  1. package/CHANGELOG.md +2914 -0
  2. package/README.md +614 -813
  3. package/dist/types/api-registry.d.ts +30 -0
  4. package/dist/types/auth-broker/client.d.ts +66 -0
  5. package/dist/types/auth-broker/index.d.ts +5 -0
  6. package/dist/types/auth-broker/refresher.d.ts +25 -0
  7. package/dist/types/auth-broker/remote-store.d.ts +96 -0
  8. package/dist/types/auth-broker/server.d.ts +32 -0
  9. package/dist/types/auth-broker/types.d.ts +105 -0
  10. package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
  11. package/dist/types/auth-gateway/http.d.ts +39 -0
  12. package/dist/types/auth-gateway/index.d.ts +3 -0
  13. package/dist/types/auth-gateway/server.d.ts +36 -0
  14. package/dist/types/auth-gateway/types.d.ts +117 -0
  15. package/dist/types/auth-storage.d.ts +739 -0
  16. package/dist/types/index.d.ts +49 -0
  17. package/dist/types/model-cache.d.ts +17 -0
  18. package/dist/types/model-manager.d.ts +64 -0
  19. package/dist/types/model-thinking.d.ts +100 -0
  20. package/dist/types/models.d.ts +12 -0
  21. package/dist/types/provider-details.d.ts +24 -0
  22. package/dist/types/provider-models/bundled-references.d.ts +4 -0
  23. package/dist/types/provider-models/descriptors.d.ts +50 -0
  24. package/dist/types/provider-models/google.d.ts +24 -0
  25. package/dist/types/provider-models/index.d.ts +5 -0
  26. package/dist/types/provider-models/ollama.d.ts +7 -0
  27. package/dist/types/provider-models/openai-compat.d.ts +296 -0
  28. package/dist/types/provider-models/special.d.ts +16 -0
  29. package/dist/types/providers/aery-native-client.d.ts +13 -0
  30. package/dist/types/providers/aery-native-server.d.ts +68 -0
  31. package/dist/types/providers/amazon-bedrock.d.ts +38 -0
  32. package/dist/types/providers/anthropic-client.d.ts +99 -0
  33. package/dist/types/providers/anthropic-messages-server-schema.d.ts +465 -0
  34. package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
  35. package/dist/types/providers/anthropic-wire.d.ts +262 -0
  36. package/dist/types/providers/anthropic.d.ts +206 -0
  37. package/dist/types/providers/aws-credentials.d.ts +43 -0
  38. package/dist/types/providers/aws-eventstream.d.ts +38 -0
  39. package/dist/types/providers/aws-sigv4.d.ts +55 -0
  40. package/dist/types/providers/azure-openai-responses.d.ts +15 -0
  41. package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
  42. package/dist/types/providers/cursor.d.ts +43 -0
  43. package/dist/types/providers/error-message.d.ts +27 -0
  44. package/dist/types/providers/github-copilot-headers.d.ts +40 -0
  45. package/dist/types/providers/gitlab-duo.d.ts +27 -0
  46. package/dist/types/providers/google-auth.d.ts +24 -0
  47. package/dist/types/providers/google-gemini-cli.d.ts +81 -0
  48. package/dist/types/providers/google-gemini-headers.d.ts +18 -0
  49. package/dist/types/providers/google-shared.d.ts +171 -0
  50. package/dist/types/providers/google-types.d.ts +138 -0
  51. package/dist/types/providers/google-vertex.d.ts +7 -0
  52. package/dist/types/providers/google.d.ts +4 -0
  53. package/dist/types/providers/grammar.d.ts +1 -0
  54. package/dist/types/providers/kimi.d.ts +27 -0
  55. package/dist/types/providers/mock.d.ts +173 -0
  56. package/dist/types/providers/ollama.d.ts +6 -0
  57. package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
  58. package/dist/types/providers/openai-chat-server-schema.d.ts +817 -0
  59. package/dist/types/providers/openai-chat-server.d.ts +16 -0
  60. package/dist/types/providers/openai-codex/constants.d.ts +26 -0
  61. package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
  62. package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
  63. package/dist/types/providers/openai-codex-responses.d.ts +67 -0
  64. package/dist/types/providers/openai-completions-compat.d.ts +25 -0
  65. package/dist/types/providers/openai-completions.d.ts +54 -0
  66. package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
  67. package/dist/types/providers/openai-responses-server.d.ts +17 -0
  68. package/dist/types/providers/openai-responses-shared.d.ts +100 -0
  69. package/dist/types/providers/openai-responses.d.ts +66 -0
  70. package/dist/types/providers/register-builtins.d.ts +31 -0
  71. package/dist/types/providers/synthetic.d.ts +26 -0
  72. package/dist/{providers → types/providers}/transform-messages.d.ts +6 -2
  73. package/dist/types/providers/vision-guard.d.ts +8 -0
  74. package/dist/types/providers/xai-responses.d.ts +23 -0
  75. package/dist/types/rate-limit-utils.d.ts +19 -0
  76. package/dist/types/stream.d.ts +28 -0
  77. package/dist/types/types.d.ts +801 -0
  78. package/dist/types/usage/claude.d.ts +4 -0
  79. package/dist/types/usage/gemini.d.ts +2 -0
  80. package/dist/types/usage/github-copilot.d.ts +7 -0
  81. package/dist/types/usage/google-antigravity.d.ts +2 -0
  82. package/dist/types/usage/kimi.d.ts +2 -0
  83. package/dist/types/usage/minimax-code.d.ts +2 -0
  84. package/dist/types/usage/openai-codex.d.ts +3 -0
  85. package/dist/types/usage/shared.d.ts +1 -0
  86. package/dist/types/usage/zai.d.ts +2 -0
  87. package/dist/types/usage.d.ts +260 -0
  88. package/dist/types/utils/abort.d.ts +19 -0
  89. package/dist/types/utils/abortable-iterator.d.ts +4 -0
  90. package/dist/types/utils/anthropic-auth.d.ts +35 -0
  91. package/dist/types/utils/discovery/antigravity.d.ts +61 -0
  92. package/dist/types/utils/discovery/codex.d.ts +38 -0
  93. package/dist/types/utils/discovery/cursor.d.ts +23 -0
  94. package/dist/types/utils/discovery/gemini.d.ts +25 -0
  95. package/dist/types/utils/discovery/index.d.ts +4 -0
  96. package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
  97. package/dist/types/utils/event-stream.d.ts +28 -0
  98. package/dist/types/utils/fireworks-model-id.d.ts +10 -0
  99. package/dist/types/utils/foundry.d.ts +1 -0
  100. package/dist/types/utils/http-inspector.d.ts +31 -0
  101. package/dist/types/utils/idle-iterator.d.ts +78 -0
  102. package/dist/types/utils/json-parse.d.ts +37 -0
  103. package/dist/types/utils/oauth/__tests__/xai-oauth.test.d.ts +1 -0
  104. package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
  105. package/dist/types/utils/oauth/anthropic.d.ts +22 -0
  106. package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
  107. package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
  108. package/dist/types/utils/oauth/callback-server.d.ts +57 -0
  109. package/dist/types/utils/oauth/cerebras.d.ts +1 -0
  110. package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
  111. package/dist/types/utils/oauth/cursor.d.ts +15 -0
  112. package/dist/types/utils/oauth/deepseek.d.ts +10 -0
  113. package/dist/types/utils/oauth/firepass.d.ts +1 -0
  114. package/dist/types/utils/oauth/fireworks.d.ts +1 -0
  115. package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
  116. package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
  117. package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
  118. package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
  119. package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
  120. package/dist/types/utils/oauth/huggingface.d.ts +19 -0
  121. package/dist/types/utils/oauth/index.d.ts +38 -0
  122. package/dist/types/utils/oauth/kagi.d.ts +17 -0
  123. package/dist/types/utils/oauth/kilo.d.ts +5 -0
  124. package/dist/types/utils/oauth/kimi.d.ts +21 -0
  125. package/dist/types/utils/oauth/litellm.d.ts +18 -0
  126. package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
  127. package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
  128. package/dist/types/utils/oauth/moonshot.d.ts +1 -0
  129. package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
  130. package/dist/types/utils/oauth/nvidia.d.ts +18 -0
  131. package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
  132. package/dist/types/utils/oauth/ollama.d.ts +18 -0
  133. package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
  134. package/dist/types/utils/oauth/opencode.d.ts +18 -0
  135. package/dist/types/utils/oauth/openrouter.d.ts +1 -0
  136. package/dist/types/utils/oauth/parallel.d.ts +17 -0
  137. package/dist/types/utils/oauth/perplexity.d.ts +9 -0
  138. package/dist/{utils → types/utils}/oauth/pkce.d.ts +0 -5
  139. package/dist/types/utils/oauth/qianfan.d.ts +17 -0
  140. package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
  141. package/dist/types/utils/oauth/synthetic.d.ts +1 -0
  142. package/dist/types/utils/oauth/tavily.d.ts +17 -0
  143. package/dist/types/utils/oauth/together.d.ts +1 -0
  144. package/dist/types/utils/oauth/types.d.ts +44 -0
  145. package/dist/types/utils/oauth/venice.d.ts +18 -0
  146. package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
  147. package/dist/types/utils/oauth/vllm.d.ts +16 -0
  148. package/dist/types/utils/oauth/wafer.d.ts +2 -0
  149. package/dist/types/utils/oauth/xai-oauth.d.ts +60 -0
  150. package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
  151. package/dist/types/utils/oauth/zai.d.ts +18 -0
  152. package/dist/types/utils/oauth/zenmux.d.ts +1 -0
  153. package/dist/types/utils/oauth/zhipu.d.ts +18 -0
  154. package/dist/{utils → types/utils}/overflow.d.ts +9 -11
  155. package/dist/types/utils/parse-bind.d.ts +23 -0
  156. package/dist/types/utils/provider-response.d.ts +3 -0
  157. package/dist/types/utils/request-debug.d.ts +29 -0
  158. package/dist/types/utils/retry-after.d.ts +3 -0
  159. package/dist/types/utils/retry.d.ts +26 -0
  160. package/dist/types/utils/schema/adapt.d.ts +24 -0
  161. package/dist/types/utils/schema/compatibility.d.ts +30 -0
  162. package/dist/types/utils/schema/dereference.d.ts +11 -0
  163. package/dist/types/utils/schema/draft.d.ts +10 -0
  164. package/dist/types/utils/schema/equality.d.ts +4 -0
  165. package/dist/types/utils/schema/fields.d.ts +49 -0
  166. package/dist/types/utils/schema/index.d.ts +13 -0
  167. package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
  168. package/dist/types/utils/schema/meta-validator.d.ts +2 -0
  169. package/dist/types/utils/schema/normalize.d.ts +93 -0
  170. package/dist/types/utils/schema/spill.d.ts +8 -0
  171. package/dist/types/utils/schema/stamps.d.ts +25 -0
  172. package/dist/types/utils/schema/types.d.ts +4 -0
  173. package/dist/types/utils/schema/wire.d.ts +53 -0
  174. package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
  175. package/dist/types/utils/sdk-stream-timeout.d.ts +33 -0
  176. package/dist/types/utils/sse-debug.d.ts +10 -0
  177. package/dist/types/utils/stream-markup-healing.d.ts +80 -0
  178. package/dist/types/utils/tool-choice.d.ts +50 -0
  179. package/dist/types/utils/validation.d.ts +17 -0
  180. package/dist/types/utils.d.ts +28 -0
  181. package/package.json +139 -105
  182. package/src/api-registry.ts +96 -0
  183. package/src/auth-broker/client.ts +358 -0
  184. package/src/auth-broker/index.ts +5 -0
  185. package/src/auth-broker/refresher.ts +117 -0
  186. package/src/auth-broker/remote-store.ts +623 -0
  187. package/src/auth-broker/server.ts +644 -0
  188. package/src/auth-broker/types.ts +127 -0
  189. package/src/auth-broker/wire-schemas.ts +200 -0
  190. package/src/auth-gateway/http.ts +194 -0
  191. package/src/auth-gateway/index.ts +3 -0
  192. package/src/auth-gateway/server.ts +818 -0
  193. package/src/auth-gateway/types.ts +143 -0
  194. package/src/auth-storage.ts +4422 -0
  195. package/src/index.ts +54 -0
  196. package/src/model-cache.ts +129 -0
  197. package/src/model-manager.ts +469 -0
  198. package/src/model-thinking.ts +782 -0
  199. package/src/models.json +83530 -0
  200. package/src/models.json.d.ts +9 -0
  201. package/src/models.ts +56 -0
  202. package/src/prompts/turn-aborted-guidance.md +4 -0
  203. package/src/provider-details.ts +90 -0
  204. package/src/provider-models/bundled-references.ts +38 -0
  205. package/src/provider-models/descriptors.ts +355 -0
  206. package/src/provider-models/google.ts +88 -0
  207. package/src/provider-models/index.ts +5 -0
  208. package/src/provider-models/ollama.ts +153 -0
  209. package/src/provider-models/openai-compat.ts +2817 -0
  210. package/src/provider-models/special.ts +67 -0
  211. package/src/providers/aery-native-client.ts +228 -0
  212. package/src/providers/aery-native-server.ts +212 -0
  213. package/src/providers/amazon-bedrock.ts +873 -0
  214. package/src/providers/anthropic-client.ts +318 -0
  215. package/src/providers/anthropic-messages-server-schema.ts +243 -0
  216. package/src/providers/anthropic-messages-server.ts +683 -0
  217. package/src/providers/anthropic-wire.ts +268 -0
  218. package/src/providers/anthropic.ts +3094 -0
  219. package/src/providers/aws-credentials.ts +501 -0
  220. package/src/providers/aws-eventstream.ts +185 -0
  221. package/src/providers/aws-sigv4.ts +218 -0
  222. package/src/providers/azure-openai-responses.ts +361 -0
  223. package/src/providers/cursor/gen/agent_pb.ts +15274 -0
  224. package/src/providers/cursor/proto/agent.proto +3526 -0
  225. package/src/providers/cursor/proto/buf.gen.yaml +6 -0
  226. package/src/providers/cursor/proto/buf.yaml +17 -0
  227. package/src/providers/cursor.ts +2621 -0
  228. package/src/providers/error-message.ts +21 -0
  229. package/src/providers/github-copilot-headers.ts +140 -0
  230. package/src/providers/gitlab-duo.ts +372 -0
  231. package/src/providers/google-auth.ts +252 -0
  232. package/src/providers/google-gemini-cli.ts +809 -0
  233. package/src/providers/google-gemini-headers.ts +41 -0
  234. package/src/providers/google-shared.ts +917 -0
  235. package/src/providers/google-types.ts +167 -0
  236. package/src/providers/google-vertex.ts +91 -0
  237. package/src/providers/google.ts +41 -0
  238. package/src/providers/grammar.ts +70 -0
  239. package/src/providers/kimi.ts +52 -0
  240. package/src/providers/mock.ts +496 -0
  241. package/src/providers/ollama.ts +644 -0
  242. package/src/providers/openai-anthropic-shim.ts +138 -0
  243. package/src/providers/openai-chat-server-schema.ts +252 -0
  244. package/src/providers/openai-chat-server.ts +647 -0
  245. package/src/providers/openai-codex/constants.ts +43 -0
  246. package/src/providers/openai-codex/request-transformer.ts +161 -0
  247. package/src/providers/openai-codex/response-handler.ts +81 -0
  248. package/src/providers/openai-codex-responses.ts +3018 -0
  249. package/src/providers/openai-completions-compat.ts +300 -0
  250. package/src/providers/openai-completions.ts +1979 -0
  251. package/src/providers/openai-responses-server-schema.ts +290 -0
  252. package/src/providers/openai-responses-server.ts +1183 -0
  253. package/src/providers/openai-responses-shared.ts +873 -0
  254. package/src/providers/openai-responses.ts +679 -0
  255. package/src/providers/register-builtins.ts +436 -0
  256. package/src/providers/synthetic.ts +50 -0
  257. package/src/providers/transform-messages.ts +382 -0
  258. package/src/providers/vision-guard.ts +31 -0
  259. package/src/providers/xai-responses.ts +82 -0
  260. package/src/rate-limit-utils.ts +84 -0
  261. package/src/stream.ts +1065 -0
  262. package/src/types.ts +944 -0
  263. package/src/usage/claude.ts +482 -0
  264. package/src/usage/gemini.ts +250 -0
  265. package/src/usage/github-copilot.ts +421 -0
  266. package/src/usage/google-antigravity.ts +201 -0
  267. package/src/usage/kimi.ts +271 -0
  268. package/src/usage/minimax-code.ts +31 -0
  269. package/src/usage/openai-codex.ts +503 -0
  270. package/src/usage/shared.ts +10 -0
  271. package/src/usage/zai.ts +247 -0
  272. package/src/usage.ts +185 -0
  273. package/src/utils/abort.ts +51 -0
  274. package/src/utils/abortable-iterator.ts +69 -0
  275. package/src/utils/anthropic-auth.ts +93 -0
  276. package/src/utils/discovery/antigravity.ts +261 -0
  277. package/src/utils/discovery/codex.ts +371 -0
  278. package/src/utils/discovery/cursor.ts +306 -0
  279. package/src/utils/discovery/gemini.ts +248 -0
  280. package/src/utils/discovery/index.ts +4 -0
  281. package/src/utils/discovery/openai-compatible.ts +224 -0
  282. package/src/utils/event-stream.ts +142 -0
  283. package/src/utils/fireworks-model-id.ts +30 -0
  284. package/src/utils/foundry.ts +8 -0
  285. package/src/utils/http-inspector.ts +176 -0
  286. package/src/utils/idle-iterator.ts +267 -0
  287. package/src/utils/json-parse.ts +182 -0
  288. package/src/utils/oauth/__tests__/xai-oauth.test.ts +107 -0
  289. package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
  290. package/src/utils/oauth/anthropic.ts +273 -0
  291. package/src/utils/oauth/api-key-login.ts +87 -0
  292. package/src/utils/oauth/api-key-validation.ts +92 -0
  293. package/src/utils/oauth/callback-server.ts +276 -0
  294. package/src/utils/oauth/cerebras.ts +16 -0
  295. package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
  296. package/src/utils/oauth/cursor.ts +157 -0
  297. package/src/utils/oauth/deepseek.ts +53 -0
  298. package/src/utils/oauth/firepass.ts +24 -0
  299. package/src/utils/oauth/fireworks.ts +15 -0
  300. package/src/utils/oauth/github-copilot.ts +362 -0
  301. package/src/utils/oauth/gitlab-duo.ts +123 -0
  302. package/src/utils/oauth/google-antigravity.ts +200 -0
  303. package/src/utils/oauth/google-gemini-cli.ts +256 -0
  304. package/src/utils/oauth/google-oauth-shared.ts +110 -0
  305. package/src/utils/oauth/huggingface.ts +62 -0
  306. package/src/utils/oauth/index.ts +484 -0
  307. package/src/utils/oauth/kagi.ts +47 -0
  308. package/src/utils/oauth/kilo.ts +87 -0
  309. package/src/utils/oauth/kimi.ts +254 -0
  310. package/src/utils/oauth/litellm.ts +47 -0
  311. package/src/utils/oauth/lm-studio.ts +38 -0
  312. package/src/utils/oauth/minimax-code.ts +78 -0
  313. package/src/utils/oauth/moonshot.ts +23 -0
  314. package/src/utils/oauth/nanogpt.ts +15 -0
  315. package/src/utils/oauth/nvidia.ts +70 -0
  316. package/src/utils/oauth/oauth.html +203 -0
  317. package/src/utils/oauth/ollama-cloud.ts +28 -0
  318. package/src/utils/oauth/ollama.ts +47 -0
  319. package/src/utils/oauth/openai-codex.ts +299 -0
  320. package/src/utils/oauth/opencode.ts +49 -0
  321. package/src/utils/oauth/openrouter.ts +20 -0
  322. package/src/utils/oauth/parallel.ts +46 -0
  323. package/src/utils/oauth/perplexity.ts +206 -0
  324. package/src/utils/oauth/pkce.ts +18 -0
  325. package/src/utils/oauth/qianfan.ts +58 -0
  326. package/src/utils/oauth/qwen-portal.ts +60 -0
  327. package/src/utils/oauth/synthetic.ts +15 -0
  328. package/src/utils/oauth/tavily.ts +46 -0
  329. package/src/utils/oauth/together.ts +16 -0
  330. package/src/utils/oauth/types.ts +99 -0
  331. package/src/utils/oauth/venice.ts +59 -0
  332. package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
  333. package/src/utils/oauth/vllm.ts +40 -0
  334. package/src/utils/oauth/wafer.ts +50 -0
  335. package/src/utils/oauth/xai-oauth.ts +342 -0
  336. package/src/utils/oauth/xiaomi.ts +139 -0
  337. package/src/utils/oauth/zai.ts +60 -0
  338. package/src/utils/oauth/zenmux.ts +15 -0
  339. package/src/utils/oauth/zhipu.ts +60 -0
  340. package/src/utils/overflow.ts +137 -0
  341. package/src/utils/parse-bind.ts +54 -0
  342. package/src/utils/provider-response.ts +30 -0
  343. package/src/utils/request-debug.ts +336 -0
  344. package/src/utils/retry-after.ts +110 -0
  345. package/src/utils/retry.ts +54 -0
  346. package/src/utils/schema/CONSTRAINTS.md +164 -0
  347. package/src/utils/schema/adapt.ts +36 -0
  348. package/src/utils/schema/compatibility.ts +435 -0
  349. package/src/utils/schema/dereference.ts +98 -0
  350. package/src/utils/schema/draft.ts +341 -0
  351. package/src/utils/schema/equality.ts +97 -0
  352. package/src/utils/schema/fields.ts +191 -0
  353. package/src/utils/schema/index.ts +13 -0
  354. package/src/utils/schema/json-schema-validator.ts +577 -0
  355. package/src/utils/schema/meta-validator.ts +167 -0
  356. package/src/utils/schema/normalize.ts +1588 -0
  357. package/src/utils/schema/spill.ts +43 -0
  358. package/src/utils/schema/stamps.ts +97 -0
  359. package/src/utils/schema/types.ts +10 -0
  360. package/src/utils/schema/wire.ts +293 -0
  361. package/src/utils/schema/zod-decontaminate.ts +331 -0
  362. package/src/utils/sdk-stream-timeout.ts +43 -0
  363. package/src/utils/sse-debug.ts +289 -0
  364. package/src/utils/stream-markup-healing.ts +612 -0
  365. package/src/utils/tool-choice.ts +99 -0
  366. package/src/utils/validation.ts +1024 -0
  367. package/src/utils.ts +166 -0
  368. package/dist/api-registry.d.ts +0 -20
  369. package/dist/api-registry.d.ts.map +0 -1
  370. package/dist/api-registry.js +0 -44
  371. package/dist/api-registry.js.map +0 -1
  372. package/dist/bedrock-provider.d.ts +0 -5
  373. package/dist/bedrock-provider.d.ts.map +0 -1
  374. package/dist/bedrock-provider.js +0 -6
  375. package/dist/bedrock-provider.js.map +0 -1
  376. package/dist/cli.d.ts +0 -3
  377. package/dist/cli.d.ts.map +0 -1
  378. package/dist/cli.js +0 -130
  379. package/dist/cli.js.map +0 -1
  380. package/dist/env-api-keys.d.ts +0 -18
  381. package/dist/env-api-keys.d.ts.map +0 -1
  382. package/dist/env-api-keys.js +0 -178
  383. package/dist/env-api-keys.js.map +0 -1
  384. package/dist/image-models.d.ts +0 -10
  385. package/dist/image-models.d.ts.map +0 -1
  386. package/dist/image-models.generated.d.ts +0 -440
  387. package/dist/image-models.generated.d.ts.map +0 -1
  388. package/dist/image-models.generated.js +0 -442
  389. package/dist/image-models.generated.js.map +0 -1
  390. package/dist/image-models.js +0 -23
  391. package/dist/image-models.js.map +0 -1
  392. package/dist/images-api-registry.d.ts +0 -14
  393. package/dist/images-api-registry.d.ts.map +0 -1
  394. package/dist/images-api-registry.js +0 -22
  395. package/dist/images-api-registry.js.map +0 -1
  396. package/dist/images.d.ts +0 -4
  397. package/dist/images.d.ts.map +0 -1
  398. package/dist/images.js +0 -14
  399. package/dist/images.js.map +0 -1
  400. package/dist/index.d.ts +0 -32
  401. package/dist/index.d.ts.map +0 -1
  402. package/dist/index.js +0 -20
  403. package/dist/index.js.map +0 -1
  404. package/dist/models.d.ts +0 -18
  405. package/dist/models.d.ts.map +0 -1
  406. package/dist/models.generated.d.ts +0 -17480
  407. package/dist/models.generated.d.ts.map +0 -1
  408. package/dist/models.generated.js +0 -16339
  409. package/dist/models.generated.js.map +0 -1
  410. package/dist/models.js +0 -71
  411. package/dist/models.js.map +0 -1
  412. package/dist/oauth.d.ts +0 -2
  413. package/dist/oauth.d.ts.map +0 -1
  414. package/dist/oauth.js +0 -2
  415. package/dist/oauth.js.map +0 -1
  416. package/dist/providers/aery-error-formatting.d.ts +0 -13
  417. package/dist/providers/aery-error-formatting.d.ts.map +0 -1
  418. package/dist/providers/aery-error-formatting.js +0 -112
  419. package/dist/providers/aery-error-formatting.js.map +0 -1
  420. package/dist/providers/amazon-bedrock.d.ts +0 -38
  421. package/dist/providers/amazon-bedrock.d.ts.map +0 -1
  422. package/dist/providers/amazon-bedrock.js +0 -763
  423. package/dist/providers/amazon-bedrock.js.map +0 -1
  424. package/dist/providers/anthropic.d.ts +0 -71
  425. package/dist/providers/anthropic.d.ts.map +0 -1
  426. package/dist/providers/anthropic.js +0 -949
  427. package/dist/providers/anthropic.js.map +0 -1
  428. package/dist/providers/azure-openai-responses.d.ts +0 -15
  429. package/dist/providers/azure-openai-responses.d.ts.map +0 -1
  430. package/dist/providers/azure-openai-responses.js +0 -225
  431. package/dist/providers/azure-openai-responses.js.map +0 -1
  432. package/dist/providers/cloudflare.d.ts +0 -13
  433. package/dist/providers/cloudflare.d.ts.map +0 -1
  434. package/dist/providers/cloudflare.js +0 -26
  435. package/dist/providers/cloudflare.js.map +0 -1
  436. package/dist/providers/faux.d.ts +0 -56
  437. package/dist/providers/faux.d.ts.map +0 -1
  438. package/dist/providers/faux.js +0 -368
  439. package/dist/providers/faux.js.map +0 -1
  440. package/dist/providers/github-copilot-headers.d.ts +0 -8
  441. package/dist/providers/github-copilot-headers.d.ts.map +0 -1
  442. package/dist/providers/github-copilot-headers.js +0 -29
  443. package/dist/providers/github-copilot-headers.js.map +0 -1
  444. package/dist/providers/google-gemini-cli.d.ts +0 -74
  445. package/dist/providers/google-gemini-cli.d.ts.map +0 -1
  446. package/dist/providers/google-gemini-cli.js +0 -779
  447. package/dist/providers/google-gemini-cli.js.map +0 -1
  448. package/dist/providers/google-shared.d.ts +0 -70
  449. package/dist/providers/google-shared.d.ts.map +0 -1
  450. package/dist/providers/google-shared.js +0 -329
  451. package/dist/providers/google-shared.js.map +0 -1
  452. package/dist/providers/google-vertex.d.ts +0 -15
  453. package/dist/providers/google-vertex.d.ts.map +0 -1
  454. package/dist/providers/google-vertex.js +0 -442
  455. package/dist/providers/google-vertex.js.map +0 -1
  456. package/dist/providers/google.d.ts +0 -13
  457. package/dist/providers/google.d.ts.map +0 -1
  458. package/dist/providers/google.js +0 -400
  459. package/dist/providers/google.js.map +0 -1
  460. package/dist/providers/images/openrouter.d.ts +0 -3
  461. package/dist/providers/images/openrouter.d.ts.map +0 -1
  462. package/dist/providers/images/openrouter.js +0 -129
  463. package/dist/providers/images/openrouter.js.map +0 -1
  464. package/dist/providers/images/register-builtins.d.ts +0 -4
  465. package/dist/providers/images/register-builtins.d.ts.map +0 -1
  466. package/dist/providers/images/register-builtins.js +0 -34
  467. package/dist/providers/images/register-builtins.js.map +0 -1
  468. package/dist/providers/mistral.d.ts +0 -25
  469. package/dist/providers/mistral.d.ts.map +0 -1
  470. package/dist/providers/mistral.js +0 -535
  471. package/dist/providers/mistral.js.map +0 -1
  472. package/dist/providers/openai-codex-responses.d.ts +0 -30
  473. package/dist/providers/openai-codex-responses.d.ts.map +0 -1
  474. package/dist/providers/openai-codex-responses.js +0 -1090
  475. package/dist/providers/openai-codex-responses.js.map +0 -1
  476. package/dist/providers/openai-completions.d.ts +0 -19
  477. package/dist/providers/openai-completions.d.ts.map +0 -1
  478. package/dist/providers/openai-completions.js +0 -950
  479. package/dist/providers/openai-completions.js.map +0 -1
  480. package/dist/providers/openai-prompt-cache.d.ts +0 -3
  481. package/dist/providers/openai-prompt-cache.d.ts.map +0 -1
  482. package/dist/providers/openai-prompt-cache.js +0 -10
  483. package/dist/providers/openai-prompt-cache.js.map +0 -1
  484. package/dist/providers/openai-responses-shared.d.ts +0 -18
  485. package/dist/providers/openai-responses-shared.d.ts.map +0 -1
  486. package/dist/providers/openai-responses-shared.js +0 -492
  487. package/dist/providers/openai-responses-shared.js.map +0 -1
  488. package/dist/providers/openai-responses.d.ts +0 -13
  489. package/dist/providers/openai-responses.d.ts.map +0 -1
  490. package/dist/providers/openai-responses.js +0 -237
  491. package/dist/providers/openai-responses.js.map +0 -1
  492. package/dist/providers/register-builtins.d.ts +0 -38
  493. package/dist/providers/register-builtins.d.ts.map +0 -1
  494. package/dist/providers/register-builtins.js +0 -278
  495. package/dist/providers/register-builtins.js.map +0 -1
  496. package/dist/providers/simple-options.d.ts +0 -8
  497. package/dist/providers/simple-options.d.ts.map +0 -1
  498. package/dist/providers/simple-options.js +0 -41
  499. package/dist/providers/simple-options.js.map +0 -1
  500. package/dist/providers/transform-messages.d.ts.map +0 -1
  501. package/dist/providers/transform-messages.js +0 -184
  502. package/dist/providers/transform-messages.js.map +0 -1
  503. package/dist/session-resources.d.ts +0 -4
  504. package/dist/session-resources.d.ts.map +0 -1
  505. package/dist/session-resources.js +0 -22
  506. package/dist/session-resources.js.map +0 -1
  507. package/dist/stream.d.ts +0 -8
  508. package/dist/stream.d.ts.map +0 -1
  509. package/dist/stream.js +0 -27
  510. package/dist/stream.js.map +0 -1
  511. package/dist/types.d.ts +0 -498
  512. package/dist/types.d.ts.map +0 -1
  513. package/dist/types.js +0 -2
  514. package/dist/types.js.map +0 -1
  515. package/dist/utils/diagnostics.d.ts +0 -19
  516. package/dist/utils/diagnostics.d.ts.map +0 -1
  517. package/dist/utils/diagnostics.js +0 -25
  518. package/dist/utils/diagnostics.js.map +0 -1
  519. package/dist/utils/event-stream.d.ts +0 -21
  520. package/dist/utils/event-stream.d.ts.map +0 -1
  521. package/dist/utils/event-stream.js +0 -81
  522. package/dist/utils/event-stream.js.map +0 -1
  523. package/dist/utils/hash.d.ts +0 -3
  524. package/dist/utils/hash.d.ts.map +0 -1
  525. package/dist/utils/hash.js +0 -14
  526. package/dist/utils/hash.js.map +0 -1
  527. package/dist/utils/headers.d.ts +0 -2
  528. package/dist/utils/headers.d.ts.map +0 -1
  529. package/dist/utils/headers.js +0 -8
  530. package/dist/utils/headers.js.map +0 -1
  531. package/dist/utils/json-parse.d.ts +0 -16
  532. package/dist/utils/json-parse.d.ts.map +0 -1
  533. package/dist/utils/json-parse.js +0 -113
  534. package/dist/utils/json-parse.js.map +0 -1
  535. package/dist/utils/node-http-proxy.d.ts +0 -10
  536. package/dist/utils/node-http-proxy.d.ts.map +0 -1
  537. package/dist/utils/node-http-proxy.js +0 -97
  538. package/dist/utils/node-http-proxy.js.map +0 -1
  539. package/dist/utils/oauth/anthropic.d.ts +0 -25
  540. package/dist/utils/oauth/anthropic.d.ts.map +0 -1
  541. package/dist/utils/oauth/anthropic.js +0 -335
  542. package/dist/utils/oauth/anthropic.js.map +0 -1
  543. package/dist/utils/oauth/device-code.d.ts +0 -19
  544. package/dist/utils/oauth/device-code.d.ts.map +0 -1
  545. package/dist/utils/oauth/device-code.js +0 -55
  546. package/dist/utils/oauth/device-code.js.map +0 -1
  547. package/dist/utils/oauth/github-copilot.d.ts +0 -30
  548. package/dist/utils/oauth/github-copilot.d.ts.map +0 -1
  549. package/dist/utils/oauth/github-copilot.js +0 -268
  550. package/dist/utils/oauth/github-copilot.js.map +0 -1
  551. package/dist/utils/oauth/google-antigravity.d.ts +0 -26
  552. package/dist/utils/oauth/google-antigravity.d.ts.map +0 -1
  553. package/dist/utils/oauth/google-antigravity.js +0 -377
  554. package/dist/utils/oauth/google-antigravity.js.map +0 -1
  555. package/dist/utils/oauth/google-gemini-cli.d.ts +0 -26
  556. package/dist/utils/oauth/google-gemini-cli.d.ts.map +0 -1
  557. package/dist/utils/oauth/google-gemini-cli.js +0 -482
  558. package/dist/utils/oauth/google-gemini-cli.js.map +0 -1
  559. package/dist/utils/oauth/index.d.ts +0 -63
  560. package/dist/utils/oauth/index.d.ts.map +0 -1
  561. package/dist/utils/oauth/index.js +0 -131
  562. package/dist/utils/oauth/index.js.map +0 -1
  563. package/dist/utils/oauth/oauth-page.d.ts +0 -3
  564. package/dist/utils/oauth/oauth-page.d.ts.map +0 -1
  565. package/dist/utils/oauth/oauth-page.js +0 -105
  566. package/dist/utils/oauth/oauth-page.js.map +0 -1
  567. package/dist/utils/oauth/openai-codex.d.ts +0 -34
  568. package/dist/utils/oauth/openai-codex.d.ts.map +0 -1
  569. package/dist/utils/oauth/openai-codex.js +0 -385
  570. package/dist/utils/oauth/openai-codex.js.map +0 -1
  571. package/dist/utils/oauth/pkce.d.ts.map +0 -1
  572. package/dist/utils/oauth/pkce.js +0 -31
  573. package/dist/utils/oauth/pkce.js.map +0 -1
  574. package/dist/utils/oauth/types.d.ts +0 -64
  575. package/dist/utils/oauth/types.d.ts.map +0 -1
  576. package/dist/utils/oauth/types.js +0 -2
  577. package/dist/utils/oauth/types.js.map +0 -1
  578. package/dist/utils/overflow.d.ts.map +0 -1
  579. package/dist/utils/overflow.js +0 -151
  580. package/dist/utils/overflow.js.map +0 -1
  581. package/dist/utils/sanitize-unicode.d.ts +0 -22
  582. package/dist/utils/sanitize-unicode.d.ts.map +0 -1
  583. package/dist/utils/sanitize-unicode.js +0 -26
  584. package/dist/utils/sanitize-unicode.js.map +0 -1
  585. package/dist/utils/typebox-helpers.d.ts +0 -17
  586. package/dist/utils/typebox-helpers.d.ts.map +0 -1
  587. package/dist/utils/typebox-helpers.js +0 -21
  588. package/dist/utils/typebox-helpers.js.map +0 -1
  589. package/dist/utils/validation.d.ts +0 -18
  590. package/dist/utils/validation.d.ts.map +0 -1
  591. package/dist/utils/validation.js +0 -281
  592. package/dist/utils/validation.js.map +0 -1
@@ -0,0 +1,2817 @@
1
+ import type { ModelManagerOptions } from "../model-manager";
2
+ import { Effort } from "../model-thinking";
3
+ import { getBundledModels } from "../models";
4
+ import type { Api, Model, ThinkingConfig } from "../types";
5
+ import { isAnthropicOAuthToken, isRecord, toBoolean, toNumber, toPositiveNumber } from "../utils";
6
+ import {
7
+ fetchOpenAICompatibleModels,
8
+ type OpenAICompatibleModelMapperContext,
9
+ type OpenAICompatibleModelRecord,
10
+ } from "../utils/discovery/openai-compatible";
11
+ import { toFireworksPublicModelId } from "../utils/fireworks-model-id";
12
+ import { getGitHubCopilotBaseUrl, OPENCODE_HEADERS, parseGitHubCopilotApiKey } from "../utils/oauth/github-copilot";
13
+ import { createBundledReferenceMap, createReferenceResolver } from "./bundled-references";
14
+
15
+ const MODELS_DEV_URL = "https://models.dev/api.json";
16
+ const ANTHROPIC_BASE_URL = "https://api.anthropic.com/v1";
17
+ const ANTHROPIC_OAUTH_BETA =
18
+ "claude-code-20250219,oauth-2025-04-20,context-1m-2025-08-07,interleaved-thinking-2025-05-14,redact-thinking-2026-02-12,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,advanced-tool-use-2025-11-20,effort-2025-11-24,extended-cache-ttl-2025-04-11";
19
+
20
+ export interface ModelsDevModel {
21
+ id?: string;
22
+ name?: string;
23
+ tool_call?: boolean;
24
+ reasoning?: boolean;
25
+ limit?: {
26
+ context?: number;
27
+ output?: number;
28
+ };
29
+ cost?: {
30
+ input?: number;
31
+ output?: number;
32
+ cache_read?: number;
33
+ cache_write?: number;
34
+ };
35
+ modalities?: {
36
+ input?: string[];
37
+ };
38
+ status?: string;
39
+ provider?: { npm?: string };
40
+ }
41
+
42
+ function toModelName(value: unknown, fallback: string): string {
43
+ if (typeof value !== "string") {
44
+ return fallback;
45
+ }
46
+ const trimmed = value.trim();
47
+ return trimmed.length > 0 ? trimmed : fallback;
48
+ }
49
+
50
+ function toInputCapabilities(value: unknown): ("text" | "image")[] {
51
+ if (!Array.isArray(value)) {
52
+ return ["text"];
53
+ }
54
+ const supportsImage = value.some(item => item === "image");
55
+ return supportsImage ? ["text", "image"] : ["text"];
56
+ }
57
+
58
+ async function fetchModelsDevPayload(fetchImpl: typeof fetch = fetch): Promise<unknown> {
59
+ const response = await fetchImpl(MODELS_DEV_URL, {
60
+ method: "GET",
61
+ headers: { Accept: "application/json" },
62
+ });
63
+ if (!response.ok) {
64
+ throw new Error(`models.dev fetch failed: ${response.status}`);
65
+ }
66
+ return response.json();
67
+ }
68
+
69
+ function mapAnthropicModelsDev(payload: unknown, baseUrl: string): Model<"anthropic-messages">[] {
70
+ if (!isRecord(payload)) {
71
+ return [];
72
+ }
73
+ const anthropicPayload = payload.anthropic;
74
+ if (!isRecord(anthropicPayload)) {
75
+ return [];
76
+ }
77
+ const modelsValue = anthropicPayload.models;
78
+ if (!isRecord(modelsValue)) {
79
+ return [];
80
+ }
81
+
82
+ const models: Model<"anthropic-messages">[] = [];
83
+ for (const [modelId, rawModel] of Object.entries(modelsValue)) {
84
+ if (!isRecord(rawModel)) {
85
+ continue;
86
+ }
87
+ const model = rawModel as ModelsDevModel;
88
+ if (model.tool_call !== true) {
89
+ continue;
90
+ }
91
+ models.push({
92
+ id: modelId,
93
+ name: toModelName(model.name, modelId),
94
+ api: "anthropic-messages",
95
+ provider: "anthropic",
96
+ baseUrl,
97
+ reasoning: model.reasoning === true,
98
+ input: toInputCapabilities(model.modalities?.input),
99
+ cost: {
100
+ input: toNumber(model.cost?.input) ?? 0,
101
+ output: toNumber(model.cost?.output) ?? 0,
102
+ cacheRead: toNumber(model.cost?.cache_read) ?? 0,
103
+ cacheWrite: toNumber(model.cost?.cache_write) ?? 0,
104
+ },
105
+ contextWindow: toPositiveNumber(model.limit?.context, UNK_CONTEXT_WINDOW),
106
+ maxTokens: toPositiveNumber(model.limit?.output, UNK_MAX_TOKENS),
107
+ });
108
+ }
109
+
110
+ models.sort((left, right) => left.id.localeCompare(right.id));
111
+ return models;
112
+ }
113
+
114
+ function buildAnthropicDiscoveryHeaders(apiKey: string): Record<string, string> {
115
+ const oauthToken = isAnthropicOAuthToken(apiKey);
116
+ const headers: Record<string, string> = {
117
+ "anthropic-version": "2023-06-01",
118
+ "anthropic-dangerous-direct-browser-access": "true",
119
+ "anthropic-beta": ANTHROPIC_OAUTH_BETA,
120
+ };
121
+ if (oauthToken) {
122
+ headers.Authorization = `Bearer ${apiKey}`;
123
+ } else {
124
+ headers["x-api-key"] = apiKey;
125
+ }
126
+ return headers;
127
+ }
128
+
129
+ function buildAnthropicReferenceMap(
130
+ modelsDevModels: readonly Model<"anthropic-messages">[],
131
+ ): Map<string, Model<"anthropic-messages">> {
132
+ const merged = new Map<string, Model<"anthropic-messages">>();
133
+ for (const model of modelsDevModels) {
134
+ merged.set(model.id, model);
135
+ }
136
+ // Anthropic /v1/models does not carry token limits, so bundled metadata stays canonical
137
+ // for known models while models.dev only fills gaps for newly discovered ids.
138
+ const bundledModels = getBundledModels("anthropic").filter(
139
+ (model): model is Model<"anthropic-messages"> => model.api === "anthropic-messages",
140
+ );
141
+ for (const model of bundledModels) {
142
+ merged.set(model.id, model);
143
+ }
144
+ return merged;
145
+ }
146
+
147
+ function mapWithBundledReference<TApi extends Api>(
148
+ entry: OpenAICompatibleModelRecord,
149
+ defaults: Model<TApi>,
150
+ reference: Model<TApi> | undefined,
151
+ ): Model<TApi> {
152
+ const name = toModelName(entry.name, reference?.name ?? defaults.name);
153
+ if (!reference) {
154
+ return {
155
+ ...defaults,
156
+ name,
157
+ };
158
+ }
159
+ return {
160
+ ...reference,
161
+ id: defaults.id,
162
+ name,
163
+ baseUrl: defaults.baseUrl,
164
+ contextWindow: toPositiveNumber(entry.context_length, reference.contextWindow),
165
+ maxTokens: toPositiveNumber(entry.max_completion_tokens, reference.maxTokens),
166
+ };
167
+ }
168
+
169
+ function normalizeAnthropicBaseUrl(baseUrl: string | undefined, fallback: string): string {
170
+ const value = baseUrl?.trim();
171
+ if (!value) {
172
+ return fallback;
173
+ }
174
+ return value.endsWith("/") ? value.slice(0, -1) : value;
175
+ }
176
+
177
+ function toAnthropicDiscoveryBaseUrl(baseUrl: string): string {
178
+ return baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
179
+ }
180
+
181
+ function normalizeOllamaBaseUrl(baseUrl?: string): string {
182
+ const value = baseUrl?.trim();
183
+ if (!value) {
184
+ return "http://127.0.0.1:11434/v1";
185
+ }
186
+ const trimmed = value.endsWith("/") ? value.slice(0, -1) : value;
187
+ return trimmed.endsWith("/v1") ? trimmed : `${trimmed}/v1`;
188
+ }
189
+
190
+ function toOllamaNativeBaseUrl(baseUrl: string): string {
191
+ return baseUrl.endsWith("/v1") ? baseUrl.slice(0, -3) : baseUrl;
192
+ }
193
+
194
+ async function fetchOllamaNativeModels(
195
+ baseUrl: string,
196
+ resolveMetadata: (modelId: string) => Promise<OllamaResolvedMetadata>,
197
+ ): Promise<Model<"openai-responses">[] | null> {
198
+ const nativeBaseUrl = toOllamaNativeBaseUrl(baseUrl);
199
+ let response: Response;
200
+ try {
201
+ response = await fetch(`${nativeBaseUrl}/api/tags`, {
202
+ method: "GET",
203
+ headers: { Accept: "application/json" },
204
+ });
205
+ } catch {
206
+ return null;
207
+ }
208
+ if (!response.ok) {
209
+ return null;
210
+ }
211
+ const payload = (await response.json()) as { models?: Array<{ name?: string; model?: string }> };
212
+ const entries = payload.models ?? [];
213
+ const resolved = await Promise.all(
214
+ entries.map(async (entry): Promise<Model<"openai-responses"> | null> => {
215
+ const id = entry.model ?? entry.name;
216
+ if (!id) return null;
217
+ const metadata = await resolveMetadata(id);
218
+ return {
219
+ id,
220
+ name: entry.name ?? id,
221
+ api: "openai-responses",
222
+ provider: "ollama",
223
+ baseUrl,
224
+ reasoning: metadata.reasoning ?? false,
225
+ thinking: metadata.thinking,
226
+ input: metadata.input ?? ["text"],
227
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
228
+ contextWindow: metadata.contextWindow,
229
+ maxTokens: metadata.maxTokens,
230
+ };
231
+ }),
232
+ );
233
+ const models: Model<"openai-responses">[] = resolved.filter((m): m is Model<"openai-responses"> => m !== null);
234
+ return models.sort((left, right) => left.id.localeCompare(right.id));
235
+ }
236
+
237
+ /**
238
+ * Fallback context window for Ollama models when `/api/show` is unavailable
239
+ * or omits a `model_info.<arch>.context_length` field. Matches the size
240
+ * Ollama's cloud catalog reports for stock models.
241
+ */
242
+ const OLLAMA_FALLBACK_CONTEXT_WINDOW = 128_000;
243
+ /** Cap max output tokens at a value that matches AERY's other openai-responses defaults. */
244
+ const OLLAMA_DEFAULT_MAX_TOKENS = 8192;
245
+
246
+ interface OllamaResolvedMetadata {
247
+ contextWindow: number;
248
+ maxTokens: number;
249
+ capabilities?: string[];
250
+ reasoning?: boolean;
251
+ thinking?: ThinkingConfig;
252
+ input?: ("text" | "image")[];
253
+ }
254
+
255
+ interface OllamaShowMetadata {
256
+ contextWindow?: number;
257
+ maxTokens?: number;
258
+ capabilities?: string[];
259
+ reasoning?: boolean;
260
+ thinking?: ThinkingConfig;
261
+ input?: ("text" | "image")[];
262
+ }
263
+
264
+ function getOllamaContextWindow(modelInfo: Record<string, unknown> | undefined): number | undefined {
265
+ if (!modelInfo) {
266
+ return undefined;
267
+ }
268
+ for (const [key, value] of Object.entries(modelInfo)) {
269
+ if (typeof value !== "number" || value <= 0) {
270
+ continue;
271
+ }
272
+ if (key.endsWith(".context_length") || key.endsWith(".num_ctx") || key.endsWith(".context_window")) {
273
+ return value;
274
+ }
275
+ }
276
+ }
277
+
278
+ function getOllamaCapabilities(value: unknown): string[] | undefined {
279
+ if (!Array.isArray(value)) {
280
+ return undefined;
281
+ }
282
+ return value.filter((item): item is string => typeof item === "string");
283
+ }
284
+
285
+ function getOllamaThinkingConfig(capabilities: string[] | undefined): ThinkingConfig | undefined {
286
+ if (!capabilities?.includes("thinking")) {
287
+ return undefined;
288
+ }
289
+ return {
290
+ mode: "effort",
291
+ minLevel: Effort.Minimal,
292
+ maxLevel: Effort.High,
293
+ };
294
+ }
295
+
296
+ /**
297
+ * Query Ollama's `/api/show` endpoint for a single model and pull native
298
+ * context and capability metadata from the response. Returns `undefined` when
299
+ * the endpoint is unavailable so callers can layer their own fallback.
300
+ */
301
+ async function fetchOllamaShowMetadata(
302
+ nativeBaseUrl: string,
303
+ modelId: string,
304
+ ): Promise<OllamaShowMetadata | undefined> {
305
+ try {
306
+ const response = await fetch(`${nativeBaseUrl}/api/show`, {
307
+ method: "POST",
308
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
309
+ body: JSON.stringify({ model: modelId }),
310
+ });
311
+ if (!response.ok) {
312
+ return undefined;
313
+ }
314
+ const payload = (await response.json()) as { capabilities?: unknown; model_info?: Record<string, unknown> };
315
+ const capabilities = getOllamaCapabilities(payload.capabilities);
316
+ const contextWindow = getOllamaContextWindow(payload.model_info);
317
+ return {
318
+ contextWindow,
319
+ maxTokens: contextWindow ? OLLAMA_DEFAULT_MAX_TOKENS : undefined,
320
+ capabilities,
321
+ reasoning: capabilities ? capabilities.includes("thinking") : undefined,
322
+ thinking: getOllamaThinkingConfig(capabilities),
323
+ input: capabilities
324
+ ? capabilities.includes("vision")
325
+ ? (["text", "image"] as Array<"text" | "image">)
326
+ : (["text"] as Array<"text">)
327
+ : undefined,
328
+ };
329
+ } catch {
330
+ // fall through; caller decides on the fallback
331
+ }
332
+ return undefined;
333
+ }
334
+
335
+ /**
336
+ * Build a resolver that fetches `/api/show` metadata per model id and caches
337
+ * the result in-memory for the lifetime of the manager. Successful lookups are
338
+ * cached so repeated `fetchDynamicModels` calls do not refetch; failed
339
+ * lookups stay uncached so a later refresh can recover.
340
+ */
341
+ function createOllamaMetadataResolver(nativeBaseUrl: string): (modelId: string) => Promise<OllamaResolvedMetadata> {
342
+ const cache = new Map<string, Promise<OllamaResolvedMetadata>>();
343
+ return modelId => {
344
+ const cached = cache.get(modelId);
345
+ if (cached) return cached;
346
+ const pending = (async () => {
347
+ const metadata = await fetchOllamaShowMetadata(nativeBaseUrl, modelId);
348
+ if (!metadata) {
349
+ cache.delete(modelId);
350
+ return { contextWindow: OLLAMA_FALLBACK_CONTEXT_WINDOW, maxTokens: OLLAMA_DEFAULT_MAX_TOKENS };
351
+ }
352
+ return {
353
+ ...metadata,
354
+ contextWindow: metadata.contextWindow ?? OLLAMA_FALLBACK_CONTEXT_WINDOW,
355
+ maxTokens: metadata.maxTokens ?? OLLAMA_DEFAULT_MAX_TOKENS,
356
+ };
357
+ })();
358
+ cache.set(modelId, pending);
359
+ void pending.catch(() => cache.delete(modelId));
360
+ return pending;
361
+ };
362
+ }
363
+
364
+ const OPENAI_NON_RESPONSES_PREFIXES = [
365
+ "text-embedding",
366
+ "whisper-",
367
+ "tts-",
368
+ "omni-moderation",
369
+ "omni-transcribe",
370
+ "omni-speech",
371
+ "gpt-image-",
372
+ "gpt-realtime",
373
+ ] as const;
374
+
375
+ function isLikelyOpenAIResponsesModelId(id: string, references: Map<string, Model<"openai-responses">>): boolean {
376
+ const trimmed = id.trim();
377
+ if (!trimmed) {
378
+ return false;
379
+ }
380
+ if (references.has(trimmed)) {
381
+ return true;
382
+ }
383
+ const normalized = trimmed.toLowerCase();
384
+ if (OPENAI_NON_RESPONSES_PREFIXES.some(prefix => normalized.startsWith(prefix))) {
385
+ return false;
386
+ }
387
+ if (normalized.includes("embedding")) {
388
+ return false;
389
+ }
390
+ return (
391
+ normalized.startsWith("gpt-") ||
392
+ normalized.startsWith("o1") ||
393
+ normalized.startsWith("o3") ||
394
+ normalized.startsWith("o4") ||
395
+ normalized.startsWith("chatgpt")
396
+ );
397
+ }
398
+
399
+ const NANO_GPT_NON_TEXT_MODEL_TOKENS = [
400
+ "embedding",
401
+ "image",
402
+ "vision",
403
+ "audio",
404
+ "speech",
405
+ "transcribe",
406
+ "moderation",
407
+ "realtime",
408
+ "whisper",
409
+ "tts",
410
+ ] as const;
411
+
412
+ /** Regex matching NanoGPT `:thinking` suffixed model IDs (with or without a level). */
413
+ const NANO_GPT_THINKING_SUFFIX_RE = /:thinking(:[^:]+)?$/;
414
+
415
+ function isLikelyNanoGptTextModelId(id: string): boolean {
416
+ const normalized = id.trim().toLowerCase();
417
+ if (!normalized) {
418
+ return false;
419
+ }
420
+ if (NANO_GPT_THINKING_SUFFIX_RE.test(normalized)) {
421
+ return false;
422
+ }
423
+ return !NANO_GPT_NON_TEXT_MODEL_TOKENS.some(token => normalized.includes(token));
424
+ }
425
+
426
+ type SimpleProviderConfig = { apiKey?: string; baseUrl?: string };
427
+
428
+ function createSimpleOpenAICompletionsOptions(
429
+ providerId: Parameters<typeof getBundledModels>[0],
430
+ defaultBaseUrl: string,
431
+ config?: SimpleProviderConfig,
432
+ ): ModelManagerOptions<"openai-completions"> {
433
+ const apiKey = config?.apiKey;
434
+ const baseUrl = config?.baseUrl ?? defaultBaseUrl;
435
+ const references = createBundledReferenceMap<"openai-completions">(providerId);
436
+ return {
437
+ providerId,
438
+ ...(apiKey && {
439
+ fetchDynamicModels: () =>
440
+ fetchOpenAICompatibleModels({
441
+ api: "openai-completions",
442
+ provider: providerId,
443
+ baseUrl,
444
+ apiKey,
445
+ mapModel: (entry, defaults) => {
446
+ const reference = references.get(defaults.id);
447
+ return mapWithBundledReference(entry, defaults, reference);
448
+ },
449
+ }),
450
+ }),
451
+ };
452
+ }
453
+
454
+ function createSimpleOpenAIResponsesOptions(
455
+ providerId: Parameters<typeof getBundledModels>[0],
456
+ defaultBaseUrl: string,
457
+ config?: SimpleProviderConfig,
458
+ ): ModelManagerOptions<"openai-responses"> {
459
+ const apiKey = config?.apiKey;
460
+ const baseUrl = config?.baseUrl ?? defaultBaseUrl;
461
+ const references = createBundledReferenceMap<"openai-responses">(providerId);
462
+ return {
463
+ providerId,
464
+ ...(apiKey && {
465
+ fetchDynamicModels: () =>
466
+ fetchOpenAICompatibleModels({
467
+ api: "openai-responses",
468
+ provider: providerId,
469
+ baseUrl,
470
+ apiKey,
471
+ mapModel: (entry, defaults) => {
472
+ const reference = references.get(defaults.id);
473
+ return mapWithBundledReference(entry, defaults, reference);
474
+ },
475
+ }),
476
+ }),
477
+ };
478
+ }
479
+
480
+ function createSimpleAnthropicProviderOptions(
481
+ providerId: Parameters<typeof getBundledModels>[0],
482
+ defaultBaseUrlFallback: string,
483
+ config?: SimpleProviderConfig,
484
+ ): ModelManagerOptions<"anthropic-messages"> {
485
+ const apiKey = config?.apiKey;
486
+ const baseUrl = normalizeAnthropicBaseUrl(config?.baseUrl, defaultBaseUrlFallback);
487
+ const discoveryBaseUrl = toAnthropicDiscoveryBaseUrl(baseUrl);
488
+ const references = createBundledReferenceMap<"anthropic-messages">(providerId);
489
+ return {
490
+ providerId,
491
+ ...(apiKey && {
492
+ fetchDynamicModels: () =>
493
+ fetchOpenAICompatibleModels({
494
+ api: "anthropic-messages",
495
+ provider: providerId,
496
+ baseUrl: discoveryBaseUrl,
497
+ headers: buildAnthropicDiscoveryHeaders(apiKey),
498
+ mapModel: (entry, defaults) => {
499
+ const reference = references.get(defaults.id);
500
+ const model = mapWithBundledReference(entry, defaults, reference);
501
+ return {
502
+ ...model,
503
+ name: toModelName(entry.display_name, model.name),
504
+ baseUrl,
505
+ };
506
+ },
507
+ }),
508
+ }),
509
+ };
510
+ }
511
+
512
+ // ---------------------------------------------------------------------------
513
+ // 1. OpenAI
514
+ // ---------------------------------------------------------------------------
515
+
516
+ export interface OpenAIModelManagerConfig {
517
+ apiKey?: string;
518
+ baseUrl?: string;
519
+ }
520
+
521
+ export function openaiModelManagerOptions(config?: OpenAIModelManagerConfig): ModelManagerOptions<"openai-responses"> {
522
+ const apiKey = config?.apiKey;
523
+ const baseUrl = config?.baseUrl ?? "https://api.openai.com/v1";
524
+ const references = createBundledReferenceMap<"openai-responses">("openai");
525
+ return {
526
+ providerId: "openai",
527
+ ...(apiKey && {
528
+ fetchDynamicModels: () =>
529
+ fetchOpenAICompatibleModels({
530
+ api: "openai-responses",
531
+ provider: "openai",
532
+ baseUrl,
533
+ apiKey,
534
+ filterModel: (_entry, model) => isLikelyOpenAIResponsesModelId(model.id, references),
535
+ mapModel: (entry, defaults) => {
536
+ const reference = references.get(defaults.id);
537
+ return mapWithBundledReference(entry, defaults, reference);
538
+ },
539
+ }),
540
+ }),
541
+ };
542
+ }
543
+
544
+ // ---------------------------------------------------------------------------
545
+ // 2. Groq
546
+ // ---------------------------------------------------------------------------
547
+
548
+ export interface GroqModelManagerConfig {
549
+ apiKey?: string;
550
+ baseUrl?: string;
551
+ }
552
+
553
+ export function groqModelManagerOptions(config?: GroqModelManagerConfig): ModelManagerOptions<"openai-completions"> {
554
+ return createSimpleOpenAICompletionsOptions("groq", "https://api.groq.com/openai/v1", config);
555
+ }
556
+
557
+ // ---------------------------------------------------------------------------
558
+ // 3. Cerebras
559
+ // ---------------------------------------------------------------------------
560
+
561
+ export interface CerebrasModelManagerConfig {
562
+ apiKey?: string;
563
+ baseUrl?: string;
564
+ }
565
+
566
+ export function cerebrasModelManagerOptions(
567
+ config?: CerebrasModelManagerConfig,
568
+ ): ModelManagerOptions<"openai-completions"> {
569
+ return createSimpleOpenAICompletionsOptions("cerebras", "https://api.cerebras.ai/v1", config);
570
+ }
571
+
572
+ // ---------------------------------------------------------------------------
573
+ // 4. Hugging Face
574
+ // ---------------------------------------------------------------------------
575
+
576
+ export interface HuggingfaceModelManagerConfig {
577
+ apiKey?: string;
578
+ baseUrl?: string;
579
+ }
580
+
581
+ export function huggingfaceModelManagerOptions(
582
+ config?: HuggingfaceModelManagerConfig,
583
+ ): ModelManagerOptions<"openai-completions"> {
584
+ return createSimpleOpenAICompletionsOptions("huggingface", "https://router.huggingface.co/v1", config);
585
+ }
586
+
587
+ // ---------------------------------------------------------------------------
588
+ // 5. NVIDIA
589
+ // ---------------------------------------------------------------------------
590
+
591
+ export interface NvidiaModelManagerConfig {
592
+ apiKey?: string;
593
+ baseUrl?: string;
594
+ }
595
+
596
+ export function nvidiaModelManagerOptions(
597
+ config?: NvidiaModelManagerConfig,
598
+ ): ModelManagerOptions<"openai-completions"> {
599
+ return createSimpleOpenAICompletionsOptions("nvidia", "https://integrate.api.nvidia.com/v1", config);
600
+ }
601
+
602
+ // ---------------------------------------------------------------------------
603
+ // 6. xAI
604
+ // ---------------------------------------------------------------------------
605
+
606
+ export interface XaiModelManagerConfig {
607
+ apiKey?: string;
608
+ baseUrl?: string;
609
+ }
610
+
611
+ export function xaiModelManagerOptions(config?: XaiModelManagerConfig): ModelManagerOptions<"openai-completions"> {
612
+ return createSimpleOpenAICompletionsOptions("xai", "https://api.x.ai/v1", config);
613
+ }
614
+
615
+ export interface XaiOAuthModelManagerConfig {
616
+ apiKey?: string;
617
+ baseUrl?: string;
618
+ }
619
+
620
+ interface XAICuratedModel {
621
+ id: string;
622
+ contextWindow: number;
623
+ name?: string;
624
+ /** Whether the model reasons natively. Defaults to true for Grok-4.x family. */
625
+ reasoning?: boolean;
626
+ /**
627
+ * Whether xAI accepts the `reasoning.effort` wire param for this model.
628
+ * Default true. When false: picker hides the effort dial (via
629
+ * getSupportedEfforts in model-thinking.ts) AND wire-side already omits
630
+ * the param via GROK_EFFORT_CAPABLE_PREFIXES in providers/xai-responses.ts.
631
+ * Must agree with that allowlist; two truths kept in sync by curated-catalog
632
+ * author convention until a follow-up Op: compress unifies them.
633
+ */
634
+ supportsReasoningEffort?: boolean;
635
+ /**
636
+ * Input modalities this model accepts. Defaults to `["text"]` when absent.
637
+ * Vision-capable Grok models MUST list `"image"` here so the curated layer
638
+ * overrides `fetchOpenAICompatibleModels`' default of `["text"]` (which
639
+ * otherwise strips image capability on every online refresh).
640
+ */
641
+ input?: ("text" | "image")[];
642
+ }
643
+
644
+ // Source of truth for the xai-oauth chat picker. Top of list = headline.
645
+ // Context windows from hermes-agent/agent/model_metadata.py:205-220
646
+ // ("Values sourced from models.dev (2026-04)"). grok-build is xAI's
647
+ // coding-fine-tuned chat model; 512K context per user spec (2026-05-17).
648
+ //
649
+ // supportsReasoningEffort=false entries reason natively but reject the wire
650
+ // `reasoning.effort` param (api.x.ai returns HTTP 400). Mirrors the HTTP-side
651
+ // GROK_EFFORT_CAPABLE_PREFIXES allowlist in providers/xai-responses.ts. The
652
+ // curated flag is the picker-visible truth; the HTTP allowlist is the wire
653
+ // truth. omitReasoningEffort in xai-responses.ts already prevents 400s; this
654
+ // fixes the picker UX wart of advertising an inert dial.
655
+ export const XAI_OAUTH_CURATED_MODELS: readonly XAICuratedModel[] = [
656
+ // grok-build is text-only per the bundled catalog; omit `input` for the default.
657
+ { id: "grok-build", contextWindow: 512_000, name: "Grok Build", supportsReasoningEffort: false },
658
+ { id: "grok-4.3", contextWindow: 1_000_000, name: "Grok 4.3", input: ["text", "image"] },
659
+ // grok-4.20-multi-agent-0309 is text-only per the bundled catalog; omit `input` for the default.
660
+ { id: "grok-4.20-multi-agent-0309", contextWindow: 2_000_000, name: "Grok 4.20 (Multi-Agent)" },
661
+ {
662
+ id: "grok-4.20-0309-reasoning",
663
+ contextWindow: 2_000_000,
664
+ name: "Grok 4.20 (Reasoning)",
665
+ supportsReasoningEffort: false,
666
+ input: ["text", "image"],
667
+ },
668
+ {
669
+ id: "grok-4.20-0309-non-reasoning",
670
+ contextWindow: 2_000_000,
671
+ name: "Grok 4.20 (Non-Reasoning)",
672
+ reasoning: false,
673
+ input: ["text", "image"],
674
+ },
675
+ ] as const;
676
+
677
+ // xAI /v1/models returns chat, image, voice, and STT entries. Tool surfaces
678
+ // route through dedicated tools (generate_image, tts) with their own model
679
+ // strings; the chat picker MUST exclude these prefixes or selecting them 400s.
680
+ const XAI_NON_CHAT_PREFIXES = ["grok-imagine-", "grok-stt-", "grok-voice-"] as const;
681
+
682
+ // Hermes-agent parity: only the `minimal -> low` clamp is applied (see
683
+ // hermes-agent/agent/transports/codex.py:92 `_effort_clamp = {"minimal":
684
+ // "low"}`). Hermes sends `xhigh` to xAI verbatim and we match that contract
685
+ // — let xAI decide if the level is valid for the specific Grok model.
686
+ // applyResponsesReasoningParams runs this through `model.compat.reasoningEffortMap`
687
+ // at request time, downstream of the omitReasoningEffort gate in xai-responses.ts.
688
+ const XAI_REASONING_EFFORT_MAP = { minimal: "low" } as const;
689
+
690
+ // Single source of truth for curated → Model fan-in. Used by the static-seed
691
+ // and the dynamic overlay/inject paths (applyXAIOAuthCuration) so curated
692
+ // reasoning/effort flags survive an online refresh (xAI's /v1/models lacks
693
+ // reasoning metadata and fetchOpenAICompatibleModels defaults reasoning to
694
+ // false). Caller supplies a `base` Model (either a freshly synthesised seed
695
+ // or a dynamic-fetched entry); the helper layers curated fields on top.
696
+ // The `minimal -> low` effort clamp (XAI_REASONING_EFFORT_MAP) is always
697
+ // merged in so dynamic-fetched models — which arrive without curated
698
+ // compat keys — still get the clamp applyResponsesReasoningParams expects.
699
+ function mergeCuratedIntoModel(base: Model<"openai-responses">, curated: XAICuratedModel): Model<"openai-responses"> {
700
+ const effort = curated.supportsReasoningEffort;
701
+ const compat = {
702
+ ...(base.compat ?? {}),
703
+ reasoningEffortMap: { ...XAI_REASONING_EFFORT_MAP, ...(base.compat?.reasoningEffortMap ?? {}) },
704
+ ...(effort === undefined ? {} : { supportsReasoningEffort: effort }),
705
+ };
706
+ return {
707
+ ...base,
708
+ contextWindow: curated.contextWindow,
709
+ name: curated.name ?? base.name,
710
+ reasoning: curated.reasoning ?? true,
711
+ input: curated.input ?? base.input,
712
+ compat,
713
+ };
714
+ }
715
+
716
+ /**
717
+ * Overlay/inject curated xai-oauth metadata onto dynamic-fetch results so
718
+ * a successful `online refresh` doesn't regress vision capability, context
719
+ * window, reasoning flags, or the effort-dial allowlist.
720
+ *
721
+ * Three passes:
722
+ * 1. Filter `XAI_NON_CHAT_PREFIXES` (picker pollution defense for tool
723
+ * surfaces routed through dedicated tools — generate_image, tts).
724
+ * 2. Overlay curated metadata onto dynamic-fetch matches. xAI's /v1/models
725
+ * does not return context_window or reasoning metadata, so without
726
+ * this overlay the runtime falls back to the bundled-reference default
727
+ * (effectively 128k context) and `reasoning: false` (suppressing the
728
+ * effort dial and stripping thinking metadata downstream).
729
+ * 3. Inject curated entries missing from the dynamic fetch. Clones the
730
+ * first surviving entry as a template so required Model fields (api,
731
+ * provider, baseUrl, cost, etc.) inherit sane defaults. If `filtered`
732
+ * is empty (offline / no auth) injection is skipped — the descriptor's
733
+ * defaultModel covers the fallback.
734
+ *
735
+ * Order: curated models first in declaration order; then dynamic remainder
736
+ * in original order.
737
+ */
738
+ function applyXAIOAuthCuration(dynamic: readonly Model<"openai-responses">[]): Model<"openai-responses">[] {
739
+ const filtered = dynamic.filter(e => !XAI_NON_CHAT_PREFIXES.some(p => e.id.startsWith(p)));
740
+
741
+ const byId = new Map<string, Model<"openai-responses">>(filtered.map(e => [e.id, e]));
742
+ for (const curated of XAI_OAUTH_CURATED_MODELS) {
743
+ const existing = byId.get(curated.id);
744
+ if (existing) {
745
+ byId.set(curated.id, mergeCuratedIntoModel(existing, curated));
746
+ }
747
+ }
748
+
749
+ const template = filtered[0];
750
+ if (template) {
751
+ for (const curated of XAI_OAUTH_CURATED_MODELS) {
752
+ if (!byId.has(curated.id)) {
753
+ // Reset id/name on the template before merging so the helper's
754
+ // `curated.name ?? base.name` clause falls back to curated.id
755
+ // (the inject contract), not to the unrelated template's label.
756
+ const base: Model<"openai-responses"> = { ...template, id: curated.id, name: curated.id };
757
+ byId.set(curated.id, mergeCuratedIntoModel(base, curated));
758
+ }
759
+ }
760
+ }
761
+
762
+ const curatedIds = new Set(XAI_OAUTH_CURATED_MODELS.map(c => c.id));
763
+ const curatedFirst = XAI_OAUTH_CURATED_MODELS.map(c => byId.get(c.id)).filter(
764
+ (e): e is Model<"openai-responses"> => e !== undefined,
765
+ );
766
+ const rest = filtered.filter(e => !curatedIds.has(e.id));
767
+ return [...curatedFirst, ...rest];
768
+ }
769
+
770
+ /**
771
+ * Render `XAI_OAUTH_CURATED_MODELS` as full `Model<"openai-responses">` entries.
772
+ *
773
+ * Single source of truth for the curated to Model fan-in, consumed by both
774
+ * - {@link xaiOAuthModelManagerOptions} (runtime static seed handed to the model
775
+ * manager so the picker is populated on a fresh login), and
776
+ * - `packages/ai/scripts/generate-models.ts` (bundles the same entries into
777
+ * `models.json`, so the synchronous `ModelRegistry.#loadModels()` boot path
778
+ * sees `xai-oauth` without waiting for a refresh — fixes the boot-time
779
+ * default-model reset when `modelRoles.default = "xai-oauth/<id>"`).
780
+ *
781
+ * `reasoning` defaults to `true` for the Grok-4.x family; the explicit
782
+ * `grok-4.20-0309-non-reasoning` entry opts out via `XAICuratedModel.reasoning`.
783
+ * `maxTokens` uses `UNK_MAX_TOKENS` so id-keyed overlays from a successful
784
+ * dynamic fetch merge cleanly. Mirrors
785
+ * `hermes-agent/hermes_cli/models.py:_XAI_STATIC_FALLBACK`.
786
+ */
787
+ export function buildXaiOAuthStaticSeed(baseUrl?: string): Model<"openai-responses">[] {
788
+ const resolvedBaseUrl = baseUrl ?? "https://api.x.ai/v1";
789
+ return XAI_OAUTH_CURATED_MODELS.map(curated => {
790
+ // Synthesise a bare base then layer curated metadata via the same helper
791
+ // the dynamic overlay/inject paths use. `name: curated.id` is a sentinel
792
+ // the helper rewrites to `curated.name ?? base.name`, so curated.name
793
+ // wins when set.
794
+ const base: Model<"openai-responses"> = {
795
+ id: curated.id,
796
+ name: curated.id,
797
+ api: "openai-responses",
798
+ provider: "xai-oauth",
799
+ baseUrl: resolvedBaseUrl,
800
+ reasoning: true,
801
+ input: ["text"],
802
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
803
+ contextWindow: curated.contextWindow,
804
+ maxTokens: UNK_MAX_TOKENS,
805
+ compat: { reasoningEffortMap: XAI_REASONING_EFFORT_MAP },
806
+ };
807
+ return mergeCuratedIntoModel(base, curated);
808
+ });
809
+ }
810
+
811
+ export function xaiOAuthModelManagerOptions(
812
+ config?: XaiOAuthModelManagerConfig,
813
+ ): ModelManagerOptions<"openai-responses"> {
814
+ const defaultBaseUrl = "https://api.x.ai/v1";
815
+ const resolvedBaseUrl = config?.baseUrl ?? defaultBaseUrl;
816
+ const base = createSimpleOpenAIResponsesOptions(
817
+ "xai-oauth" as Parameters<typeof getBundledModels>[0],
818
+ defaultBaseUrl,
819
+ config,
820
+ );
821
+ // Static seed handed to the runtime model manager so the picker populates on
822
+ // a fresh login even before `fetchDynamicModels` fires (it is gated on
823
+ // `config.apiKey` at construction time, and OAuth tokens resolve later via
824
+ // AuthStorage). `generate-models.ts` calls the same builder so `models.json`
825
+ // carries these entries too — making the synchronous `#loadModels()` boot
826
+ // path honor `modelRoles.default = "xai-oauth/<id>"` without `await refresh()`.
827
+ const staticModels = buildXaiOAuthStaticSeed(resolvedBaseUrl);
828
+ if (!base.fetchDynamicModels) {
829
+ return { ...base, staticModels };
830
+ }
831
+ // Wrap fetchDynamicModels so an `online refresh` against xAI's /v1/models
832
+ // runs through applyXAIOAuthCuration — preserves curated context windows,
833
+ // vision modality, reasoning flags, and filters tool-only model ids
834
+ // (grok-imagine-*, grok-stt-*, grok-voice-*) from the chat picker.
835
+ const inner = base.fetchDynamicModels;
836
+ return {
837
+ ...base,
838
+ staticModels,
839
+ fetchDynamicModels: async () => {
840
+ const dynamic = await inner();
841
+ return dynamic == null ? dynamic : applyXAIOAuthCuration(dynamic);
842
+ },
843
+ };
844
+ }
845
+
846
+ // ---------------------------------------------------------------------------
847
+ // 6.5 DeepSeek
848
+ // ---------------------------------------------------------------------------
849
+
850
+ export interface DeepSeekModelManagerConfig {
851
+ apiKey?: string;
852
+ baseUrl?: string;
853
+ }
854
+
855
+ export function deepseekModelManagerOptions(
856
+ config?: DeepSeekModelManagerConfig,
857
+ ): ModelManagerOptions<"openai-completions"> {
858
+ return createSimpleOpenAICompletionsOptions("deepseek", "https://api.deepseek.com", config);
859
+ }
860
+ // ---------------------------------------------------------------------------
861
+ // 6.7 Zhipu Coding Plan
862
+ // ---------------------------------------------------------------------------
863
+
864
+ export interface ZhipuCodingPlanModelManagerConfig {
865
+ apiKey?: string;
866
+ baseUrl?: string;
867
+ }
868
+
869
+ export function zhipuCodingPlanModelManagerOptions(
870
+ config?: ZhipuCodingPlanModelManagerConfig,
871
+ ): ModelManagerOptions<"openai-completions"> {
872
+ const apiKey = config?.apiKey;
873
+ const baseUrl = config?.baseUrl ?? "https://open.bigmodel.cn/api/coding/paas/v4";
874
+ return {
875
+ providerId: "zhipu-coding-plan",
876
+ ...(apiKey && {
877
+ fetchDynamicModels: () =>
878
+ fetchOpenAICompatibleModels({
879
+ api: "openai-completions",
880
+ provider: "zhipu-coding-plan",
881
+ baseUrl,
882
+ apiKey,
883
+ mapModel: (
884
+ _entry: OpenAICompatibleModelRecord,
885
+ defaults: Model<"openai-completions">,
886
+ _context: OpenAICompatibleModelMapperContext<"openai-completions">,
887
+ ): Model<"openai-completions"> => {
888
+ const id = defaults.id;
889
+ return {
890
+ ...defaults,
891
+ reasoning: ZHIPU_REASONING_MODELS[id] === true || id.includes("thinking"),
892
+ input: ZHIPU_VISION_PATTERN.test(id) ? (["text", "image"] as const) : ["text"],
893
+ compat: {
894
+ thinkingFormat: "zai",
895
+ reasoningContentField: "reasoning_content",
896
+ supportsDeveloperRole: false,
897
+ },
898
+ };
899
+ },
900
+ }),
901
+ }),
902
+ };
903
+ }
904
+
905
+ // Reasoning-capable GLM models on the BigModel coding-plan SKU. Keep this
906
+ // explicit rather than regex-matching `glm-[45]\.\d` so newly-added integers
907
+ // like `glm-5` / `glm-5-turbo` are covered and unrelated future SKUs (e.g.
908
+ // `glm-5-preview`) do not silently flip into thinking mode.
909
+ const ZHIPU_REASONING_MODELS: Readonly<Record<string, true>> = {
910
+ "glm-4.5": true,
911
+ "glm-4.5-air": true,
912
+ "glm-4.6": true,
913
+ "glm-4.7": true,
914
+ "glm-5": true,
915
+ "glm-5-turbo": true,
916
+ "glm-5.1": true,
917
+ };
918
+
919
+ // Vision-capable GLM models follow the `glm-<N>[.<N>]v[-<variant>]` shape
920
+ // (e.g. `glm-4v`, `glm-4.5v`, `glm-4v-plus`). The previous `id.includes("v")`
921
+ // check matched anything with a `v` — including the non-vision `glm-5-preview`.
922
+ const ZHIPU_VISION_PATTERN = /^glm-[45](?:\.\d+)?v(?:-|$)/;
923
+
924
+ // ---------------------------------------------------------------------------
925
+ // 7.5 Fireworks
926
+ // ---------------------------------------------------------------------------
927
+
928
+ export interface FireworksModelManagerConfig {
929
+ apiKey?: string;
930
+ baseUrl?: string;
931
+ }
932
+
933
+ function toFireworksModelName(entry: OpenAICompatibleModelRecord, fallback: string): string {
934
+ const name = toModelName(entry.name, "");
935
+ if (name) return name;
936
+ const id = typeof entry.id === "string" ? entry.id : fallback;
937
+ const shortName = id.split("/").at(-1) ?? fallback;
938
+ if (fallback !== id && fallback !== shortName) return fallback;
939
+ return shortName
940
+ .split("-")
941
+ .filter(Boolean)
942
+ .map(part => part.charAt(0).toUpperCase() + part.slice(1))
943
+ .join(" ");
944
+ }
945
+
946
+ function createModelsDevReferenceMap<TApi extends Api>(models: readonly Model<Api>[]): Map<string, Model<TApi>> {
947
+ const references = new Map<string, Model<TApi>>();
948
+ for (const model of models) {
949
+ const candidate = model as Model<TApi>;
950
+ const existing = references.get(candidate.id);
951
+ if (!existing) {
952
+ references.set(candidate.id, candidate);
953
+ continue;
954
+ }
955
+ if (candidate.contextWindow > existing.contextWindow) {
956
+ references.set(candidate.id, candidate);
957
+ continue;
958
+ }
959
+ if (candidate.contextWindow === existing.contextWindow && candidate.maxTokens > existing.maxTokens) {
960
+ references.set(candidate.id, candidate);
961
+ }
962
+ }
963
+ return references;
964
+ }
965
+
966
+ async function loadModelsDevReferences<TApi extends Api>(): Promise<Map<string, Model<TApi>>> {
967
+ try {
968
+ const payload = await fetchModelsDevPayload();
969
+ return createModelsDevReferenceMap<TApi>(
970
+ mapModelsDevToModels(payload as Record<string, unknown>, MODELS_DEV_PROVIDER_DESCRIPTORS),
971
+ );
972
+ } catch {
973
+ return new Map<string, Model<TApi>>();
974
+ }
975
+ }
976
+ export function fireworksModelManagerOptions(
977
+ config?: FireworksModelManagerConfig,
978
+ ): ModelManagerOptions<"openai-completions"> {
979
+ const apiKey = config?.apiKey;
980
+ const baseUrl = config?.baseUrl ?? "https://api.fireworks.ai/inference/v1";
981
+ const bundledReferences = createReferenceResolver(createBundledReferenceMap<"openai-completions">("fireworks"));
982
+ return {
983
+ providerId: "fireworks",
984
+ ...(apiKey && {
985
+ fetchDynamicModels: async () => {
986
+ const modelsDevReferences = await loadModelsDevReferences<"openai-completions">();
987
+ return fetchOpenAICompatibleModels({
988
+ api: "openai-completions",
989
+ provider: "fireworks",
990
+ baseUrl,
991
+ apiKey,
992
+ filterModel: entry =>
993
+ toBoolean(entry.supports_chat) === true && toBoolean(entry.supports_tools) === true,
994
+ mapModel: (entry, defaults) => {
995
+ const publicModelId = toFireworksPublicModelId(defaults.id);
996
+ const reference = modelsDevReferences.get(publicModelId) ?? bundledReferences(publicModelId);
997
+ const model = mapWithBundledReference(entry, defaults, reference);
998
+ return {
999
+ ...model,
1000
+ id: publicModelId,
1001
+ api: "openai-completions",
1002
+ provider: "fireworks",
1003
+ baseUrl,
1004
+ name: toFireworksModelName(entry, model.name),
1005
+ input: toBoolean(entry.supports_image_input) === true ? ["text", "image"] : ["text"],
1006
+ contextWindow: toPositiveNumber(entry.context_length, model.contextWindow),
1007
+ maxTokens: toPositiveNumber(entry.max_completion_tokens, model.maxTokens),
1008
+ };
1009
+ },
1010
+ });
1011
+ },
1012
+ }),
1013
+ };
1014
+ }
1015
+
1016
+ // ---------------------------------------------------------------------------
1017
+ // 7.6 Fire Pass (Fireworks Kimi K2.6 Turbo subscription)
1018
+ // ---------------------------------------------------------------------------
1019
+
1020
+ export interface FirepassModelManagerConfig {
1021
+ apiKey?: string;
1022
+ baseUrl?: string;
1023
+ }
1024
+
1025
+ /**
1026
+ * Fire Pass is a Fireworks subscription product that exposes a single router
1027
+ * model (Kimi K2.6 Turbo) under `accounts/fireworks/routers/kimi-k2p6-turbo`.
1028
+ * The dedicated `fpk_…` keys do not authorize `/v1/models`, so this manager
1029
+ * never performs dynamic discovery — the bundled catalog entry is canonical.
1030
+ * See https://docs.fireworks.ai/firepass.
1031
+ */
1032
+ export function firepassModelManagerOptions(
1033
+ _config?: FirepassModelManagerConfig,
1034
+ ): ModelManagerOptions<"openai-completions"> {
1035
+ return {
1036
+ providerId: "firepass",
1037
+ };
1038
+ }
1039
+
1040
+ // ---------------------------------------------------------------------------
1041
+ // 7.7 Wafer (Pass + Serverless)
1042
+ // ---------------------------------------------------------------------------
1043
+
1044
+ export interface WaferModelManagerConfig {
1045
+ apiKey?: string;
1046
+ baseUrl?: string;
1047
+ }
1048
+
1049
+ const WAFER_DEFAULT_BASE_URL = "https://pass.wafer.ai/v1";
1050
+ const WAFER_MAX_TOKENS_CAP = 65536;
1051
+
1052
+ /**
1053
+ * Shared mapper for Wafer's `/v1/models` records.
1054
+ *
1055
+ * Wafer wraps each entry with a `wafer` envelope describing tier, capabilities,
1056
+ * and cents-per-million pricing. The mapper folds that metadata into the
1057
+ * canonical `Model<"openai-completions">` shape and applies zai-family thinking
1058
+ * compat when the entry advertises reasoning support (GLM-family on the Pass
1059
+ * SKU). Cents-per-million → dollars-per-million via /100.
1060
+ */
1061
+ interface WaferRecord {
1062
+ context_length?: unknown;
1063
+ tier?: unknown;
1064
+ provider?: unknown;
1065
+ capabilities?: { vision?: unknown; reasoning?: unknown; tools?: unknown };
1066
+ pricing?: {
1067
+ input_cents_per_million?: unknown;
1068
+ output_cents_per_million?: unknown;
1069
+ cache_read_cents_per_million?: unknown;
1070
+ };
1071
+ display_name?: unknown;
1072
+ }
1073
+
1074
+ function readWaferRecord(entry: OpenAICompatibleModelRecord): WaferRecord | undefined {
1075
+ const raw = (entry as { wafer?: unknown }).wafer;
1076
+ return raw && typeof raw === "object" ? (raw as WaferRecord) : undefined;
1077
+ }
1078
+
1079
+ function mapWaferModel(
1080
+ providerId: "wafer-pass" | "wafer-serverless",
1081
+ baseUrl: string,
1082
+ entry: OpenAICompatibleModelRecord,
1083
+ defaults: Model<"openai-completions">,
1084
+ ): Model<"openai-completions"> {
1085
+ const wafer = readWaferRecord(entry);
1086
+ const capabilities = wafer?.capabilities ?? {};
1087
+ const reasoning = capabilities.reasoning === true;
1088
+ const vision = capabilities.vision === true;
1089
+ const contextWindow = toPositiveNumber(
1090
+ wafer?.context_length,
1091
+ toPositiveNumber((entry as { max_model_len?: unknown }).max_model_len, defaults.contextWindow),
1092
+ );
1093
+ const maxTokens = Math.min(contextWindow, WAFER_MAX_TOKENS_CAP);
1094
+ const pricing = wafer?.pricing ?? {};
1095
+ // Wafer's `/v1/models` exposes pricing through `*_cents_per_million` fields,
1096
+ // but the values are an internal wholesale unit, not literal cents — across
1097
+ // every published Serverless model on wafer.ai the user-facing rate equals
1098
+ // `cents × 125 / 10000` (i.e. wholesale × 1.25 / 100; GLM-5.1's `120` →
1099
+ // $1.50/M, Kimi-K2.6's `88` → $1.10/M, etc.). The multiply-first form keeps
1100
+ // the result a finite dyadic for every observed value.
1101
+ // For the Pass SKU the per-token rate is bundled in the flat-rate
1102
+ // subscription, so we follow the convention shared with
1103
+ // `kimi-code`/`firepass`/`alibaba-coding-plan` and seed every Pass model with
1104
+ // `cost: 0` regardless of what the upstream envelope says.
1105
+ const isPassSku = providerId === "wafer-pass";
1106
+ const cost = isPassSku
1107
+ ? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
1108
+ : {
1109
+ input: (toPositiveNumber(pricing.input_cents_per_million, 0) * 125) / 10000,
1110
+ output: (toPositiveNumber(pricing.output_cents_per_million, 0) * 125) / 10000,
1111
+ cacheRead: (toPositiveNumber(pricing.cache_read_cents_per_million, 0) * 125) / 10000,
1112
+ cacheWrite: 0,
1113
+ };
1114
+ const name = toModelName(wafer?.display_name, defaults.name);
1115
+ const base: Model<"openai-completions"> = {
1116
+ ...defaults,
1117
+ id: defaults.id,
1118
+ name,
1119
+ api: "openai-completions",
1120
+ provider: providerId,
1121
+ baseUrl,
1122
+ reasoning,
1123
+ input: vision ? (["text", "image"] as const) : ["text"],
1124
+ cost,
1125
+ contextWindow,
1126
+ maxTokens,
1127
+ };
1128
+ if (reasoning) {
1129
+ // Wafer's `wafer.provider` envelope tells us which upstream backend serves
1130
+ // the model. Each upstream accepts a different thinking-control parameter
1131
+ // on the wire — Wafer passes the body through, so we must mirror the
1132
+ // upstream's native shape:
1133
+ // - zai (GLM) and moonshotai (Kimi) → `thinking: { type: "enabled" | "disabled" }`
1134
+ // - qwen (Alibaba) → top-level `enable_thinking: boolean`
1135
+ // - deepseek → `reasoning_effort` (DeepSeek effort map; the model always
1136
+ // reasons when invoked, replay of `reasoning_content` is required on
1137
+ // tool-call turns — both handled by `detectOpenAICompat` from the id).
1138
+ // For unknown upstreams we omit `thinkingFormat` and let the per-id
1139
+ // detection in `detectOpenAICompat` pick a safe default.
1140
+ const upstream = typeof wafer?.provider === "string" ? wafer.provider : undefined;
1141
+ const thinkingFormat: "zai" | "qwen" | undefined =
1142
+ upstream === "zai" || upstream === "moonshotai" ? "zai" : upstream === "qwen" ? "qwen" : undefined;
1143
+ return {
1144
+ ...base,
1145
+ compat: {
1146
+ ...(thinkingFormat ? { thinkingFormat } : {}),
1147
+ reasoningContentField: "reasoning_content",
1148
+ supportsDeveloperRole: false,
1149
+ },
1150
+ };
1151
+ }
1152
+ return {
1153
+ ...base,
1154
+ compat: { supportsDeveloperRole: false },
1155
+ };
1156
+ }
1157
+
1158
+ function createWaferOptions(
1159
+ providerId: "wafer-pass" | "wafer-serverless",
1160
+ config: WaferModelManagerConfig | undefined,
1161
+ ): ModelManagerOptions<"openai-completions"> {
1162
+ const apiKey = config?.apiKey;
1163
+ const baseUrl = config?.baseUrl ?? WAFER_DEFAULT_BASE_URL;
1164
+ const passOnly = providerId === "wafer-pass";
1165
+ return {
1166
+ providerId,
1167
+ ...(apiKey && {
1168
+ fetchDynamicModels: () =>
1169
+ fetchOpenAICompatibleModels({
1170
+ api: "openai-completions",
1171
+ provider: providerId,
1172
+ baseUrl,
1173
+ apiKey,
1174
+ filterModel: entry => {
1175
+ if (!passOnly) return true;
1176
+ const wafer = readWaferRecord(entry);
1177
+ return wafer?.tier === "pass_included";
1178
+ },
1179
+ mapModel: (entry, defaults) => mapWaferModel(providerId, baseUrl, entry, defaults),
1180
+ }),
1181
+ }),
1182
+ };
1183
+ }
1184
+
1185
+ export function waferPassModelManagerOptions(
1186
+ config?: WaferModelManagerConfig,
1187
+ ): ModelManagerOptions<"openai-completions"> {
1188
+ return createWaferOptions("wafer-pass", config);
1189
+ }
1190
+
1191
+ export function waferServerlessModelManagerOptions(
1192
+ config?: WaferModelManagerConfig,
1193
+ ): ModelManagerOptions<"openai-completions"> {
1194
+ return createWaferOptions("wafer-serverless", config);
1195
+ }
1196
+
1197
+ // ---------------------------------------------------------------------------
1198
+ // 7. Mistral
1199
+ // ---------------------------------------------------------------------------
1200
+
1201
+ export interface MistralModelManagerConfig {
1202
+ apiKey?: string;
1203
+ baseUrl?: string;
1204
+ }
1205
+
1206
+ export function mistralModelManagerOptions(
1207
+ config?: MistralModelManagerConfig,
1208
+ ): ModelManagerOptions<"openai-completions"> {
1209
+ return createSimpleOpenAICompletionsOptions("mistral", "https://api.mistral.ai/v1", config);
1210
+ }
1211
+
1212
+ // ---------------------------------------------------------------------------
1213
+ // 8. OpenCode
1214
+ // ---------------------------------------------------------------------------
1215
+
1216
+ export interface OpenCodeModelManagerConfig {
1217
+ apiKey?: string;
1218
+ baseUrl?: string;
1219
+ }
1220
+
1221
+ function normalizeOpenCodeBasePath(baseUrl: string | undefined, fallbackBasePath: string): string {
1222
+ const value = normalizeAnthropicBaseUrl(baseUrl, fallbackBasePath);
1223
+ return value.endsWith("/v1") ? value.slice(0, -3) : value;
1224
+ }
1225
+
1226
+ function openCodeBaseUrlForApi(api: Api, basePath: string): string {
1227
+ return api === "anthropic-messages" ? basePath : `${basePath}/v1`;
1228
+ }
1229
+
1230
+ function openCodeModelManagerOptions(
1231
+ providerId: "opencode-go" | "opencode-zen",
1232
+ defaultBasePath: string,
1233
+ config?: OpenCodeModelManagerConfig,
1234
+ ): ModelManagerOptions<Api> {
1235
+ const apiKey = config?.apiKey;
1236
+ const basePath = normalizeOpenCodeBasePath(config?.baseUrl, defaultBasePath);
1237
+ const discoveryBaseUrl = openCodeBaseUrlForApi("openai-completions", basePath);
1238
+ const references = createBundledReferenceMap<Api>(providerId);
1239
+ return {
1240
+ providerId,
1241
+ ...(apiKey && {
1242
+ fetchDynamicModels: () =>
1243
+ fetchOpenAICompatibleModels<Api>({
1244
+ api: "openai-completions",
1245
+ provider: providerId,
1246
+ baseUrl: discoveryBaseUrl,
1247
+ apiKey,
1248
+ mapModel: (entry, defaults) => {
1249
+ const reference = references.get(defaults.id);
1250
+ const name = toModelName(entry.name, reference?.name ?? defaults.name);
1251
+ if (!reference) {
1252
+ return {
1253
+ ...defaults,
1254
+ name,
1255
+ };
1256
+ }
1257
+ return {
1258
+ ...reference,
1259
+ id: defaults.id,
1260
+ name,
1261
+ baseUrl: openCodeBaseUrlForApi(reference.api, basePath),
1262
+ contextWindow: toPositiveNumber(entry.context_length, reference.contextWindow),
1263
+ maxTokens: toPositiveNumber(entry.max_completion_tokens, reference.maxTokens),
1264
+ };
1265
+ },
1266
+ }),
1267
+ }),
1268
+ };
1269
+ }
1270
+
1271
+ export function opencodeZenModelManagerOptions(config?: OpenCodeModelManagerConfig): ModelManagerOptions<Api> {
1272
+ return openCodeModelManagerOptions("opencode-zen", "https://opencode.ai/zen", config);
1273
+ }
1274
+
1275
+ export function opencodeGoModelManagerOptions(config?: OpenCodeModelManagerConfig): ModelManagerOptions<Api> {
1276
+ return openCodeModelManagerOptions("opencode-go", "https://opencode.ai/zen/go", config);
1277
+ }
1278
+
1279
+ // ---------------------------------------------------------------------------
1280
+ // 9. Ollama
1281
+ // ---------------------------------------------------------------------------
1282
+
1283
+ export interface OllamaModelManagerConfig {
1284
+ apiKey?: string;
1285
+ baseUrl?: string;
1286
+ }
1287
+
1288
+ export function ollamaModelManagerOptions(config?: OllamaModelManagerConfig): ModelManagerOptions<"openai-responses"> {
1289
+ const apiKey = config?.apiKey;
1290
+ const baseUrl = normalizeOllamaBaseUrl(config?.baseUrl);
1291
+ const nativeBaseUrl = toOllamaNativeBaseUrl(baseUrl);
1292
+ const references = createBundledReferenceMap<"openai-responses">("ollama" as Parameters<typeof getBundledModels>[0]);
1293
+ const resolveMetadata = createOllamaMetadataResolver(nativeBaseUrl);
1294
+ return {
1295
+ providerId: "ollama",
1296
+ fetchDynamicModels: async () => {
1297
+ const openAiCompatible = await fetchOpenAICompatibleModels({
1298
+ api: "openai-responses",
1299
+ provider: "ollama",
1300
+ baseUrl,
1301
+ apiKey,
1302
+ mapModel: (entry, defaults) => {
1303
+ const reference = references.get(defaults.id);
1304
+ if (!reference) {
1305
+ return {
1306
+ ...defaults,
1307
+ name: toModelName(entry.name, defaults.name),
1308
+ contextWindow: OLLAMA_FALLBACK_CONTEXT_WINDOW,
1309
+ maxTokens: OLLAMA_DEFAULT_MAX_TOKENS,
1310
+ };
1311
+ }
1312
+ return mapWithBundledReference(entry, defaults, reference);
1313
+ },
1314
+ });
1315
+ if (openAiCompatible && openAiCompatible.length > 0) {
1316
+ await Promise.all(
1317
+ openAiCompatible.map(async model => {
1318
+ const metadata = await resolveMetadata(model.id);
1319
+ model.contextWindow = metadata.contextWindow;
1320
+ if (metadata.reasoning !== undefined) {
1321
+ model.reasoning = metadata.reasoning;
1322
+ model.thinking = metadata.thinking;
1323
+ }
1324
+ if (metadata.input) {
1325
+ model.input = metadata.input;
1326
+ }
1327
+ }),
1328
+ );
1329
+ return openAiCompatible;
1330
+ }
1331
+ const nativeFallback = await fetchOllamaNativeModels(baseUrl, resolveMetadata);
1332
+ if (nativeFallback && nativeFallback.length > 0) {
1333
+ return nativeFallback;
1334
+ }
1335
+ return openAiCompatible;
1336
+ },
1337
+ };
1338
+ }
1339
+
1340
+ // ---------------------------------------------------------------------------
1341
+ // 10. OpenRouter
1342
+ // ---------------------------------------------------------------------------
1343
+
1344
+ export interface OpenRouterModelManagerConfig {
1345
+ apiKey?: string;
1346
+ baseUrl?: string;
1347
+ }
1348
+
1349
+ export function openrouterModelManagerOptions(
1350
+ config?: OpenRouterModelManagerConfig,
1351
+ ): ModelManagerOptions<"openai-completions"> {
1352
+ const apiKey = config?.apiKey;
1353
+ const baseUrl = config?.baseUrl ?? "https://openrouter.ai/api/v1";
1354
+ return {
1355
+ providerId: "openrouter",
1356
+ fetchDynamicModels: () =>
1357
+ fetchOpenAICompatibleModels({
1358
+ api: "openai-completions",
1359
+ provider: "openrouter",
1360
+ baseUrl,
1361
+ apiKey,
1362
+ filterModel: (entry: OpenAICompatibleModelRecord) => {
1363
+ const params = entry.supported_parameters;
1364
+ return Array.isArray(params) && params.includes("tools");
1365
+ },
1366
+ mapModel: (
1367
+ entry: OpenAICompatibleModelRecord,
1368
+ defaults: Model<"openai-completions">,
1369
+ _context: OpenAICompatibleModelMapperContext<"openai-completions">,
1370
+ ): Model<"openai-completions"> => {
1371
+ const pricing = entry.pricing as Record<string, unknown> | undefined;
1372
+ const params = Array.isArray(entry.supported_parameters) ? (entry.supported_parameters as string[]) : [];
1373
+ const modality = String((entry.architecture as Record<string, unknown> | undefined)?.modality ?? "");
1374
+ const topProvider = entry.top_provider as Record<string, unknown> | undefined;
1375
+
1376
+ const supportsToolChoice = params.includes("tool_choice");
1377
+
1378
+ return {
1379
+ ...defaults,
1380
+ reasoning: params.includes("reasoning"),
1381
+ input: modality.includes("image") ? ["text", "image"] : ["text"],
1382
+ cost: {
1383
+ input: parseFloat(String(pricing?.prompt ?? "0")) * 1_000_000,
1384
+ output: parseFloat(String(pricing?.completion ?? "0")) * 1_000_000,
1385
+ cacheRead: parseFloat(String(pricing?.input_cache_read ?? "0")) * 1_000_000,
1386
+ cacheWrite: parseFloat(String(pricing?.input_cache_write ?? "0")) * 1_000_000,
1387
+ },
1388
+ contextWindow:
1389
+ typeof entry.context_length === "number" ? entry.context_length : defaults.contextWindow,
1390
+ maxTokens:
1391
+ typeof topProvider?.max_completion_tokens === "number"
1392
+ ? topProvider.max_completion_tokens
1393
+ : defaults.maxTokens,
1394
+ ...(!supportsToolChoice && {
1395
+ compat: { supportsToolChoice: false },
1396
+ }),
1397
+ };
1398
+ },
1399
+ }),
1400
+ };
1401
+ }
1402
+
1403
+ const ZENMUX_OPENAI_BASE_URL = "https://zenmux.ai/api/v1";
1404
+ const ZENMUX_ANTHROPIC_BASE_URL = "https://zenmux.ai/api/anthropic";
1405
+
1406
+ function normalizeZenMuxOpenAiBaseUrl(baseUrl?: string): string {
1407
+ const value = baseUrl?.trim();
1408
+ if (!value) {
1409
+ return ZENMUX_OPENAI_BASE_URL;
1410
+ }
1411
+ return value.endsWith("/") ? value.slice(0, -1) : value;
1412
+ }
1413
+
1414
+ function toZenMuxAnthropicBaseUrl(openAiBaseUrl: string): string {
1415
+ try {
1416
+ const parsed = new URL(openAiBaseUrl);
1417
+ const trimmedPath = parsed.pathname.replace(/\/+$/g, "");
1418
+ parsed.pathname = trimmedPath.endsWith("/api/v1")
1419
+ ? `${trimmedPath.slice(0, -"/api/v1".length)}/api/anthropic`
1420
+ : "/api/anthropic";
1421
+ return `${parsed.protocol}//${parsed.host}${parsed.pathname}`;
1422
+ } catch {
1423
+ return ZENMUX_ANTHROPIC_BASE_URL;
1424
+ }
1425
+ }
1426
+
1427
+ function isZenMuxAnthropicModel(entry: OpenAICompatibleModelRecord, modelId: string): boolean {
1428
+ if (typeof entry.owned_by === "string" && entry.owned_by.toLowerCase() === "anthropic") {
1429
+ return true;
1430
+ }
1431
+ return modelId.toLowerCase().startsWith("anthropic/");
1432
+ }
1433
+
1434
+ function getZenMuxPricingValue(pricings: Record<string, unknown> | undefined, key: string): number {
1435
+ const bucket = pricings?.[key];
1436
+ if (!Array.isArray(bucket)) {
1437
+ return 0;
1438
+ }
1439
+ for (const item of bucket) {
1440
+ if (!isRecord(item)) {
1441
+ continue;
1442
+ }
1443
+ const value = toNumber(item.value);
1444
+ if (value !== undefined) {
1445
+ return value;
1446
+ }
1447
+ }
1448
+ return 0;
1449
+ }
1450
+
1451
+ function getZenMuxCacheWritePrice(pricings: Record<string, unknown> | undefined): number {
1452
+ const oneHour = getZenMuxPricingValue(pricings, "input_cache_write_1_h");
1453
+ if (oneHour > 0) {
1454
+ return oneHour;
1455
+ }
1456
+ const fiveMinute = getZenMuxPricingValue(pricings, "input_cache_write_5_min");
1457
+ if (fiveMinute > 0) {
1458
+ return fiveMinute;
1459
+ }
1460
+ return getZenMuxPricingValue(pricings, "input_cache_write");
1461
+ }
1462
+
1463
+ // ---------------------------------------------------------------------------
1464
+ // 10.5 ZenMux
1465
+ // ---------------------------------------------------------------------------
1466
+
1467
+ export interface ZenMuxModelManagerConfig {
1468
+ apiKey?: string;
1469
+ baseUrl?: string;
1470
+ }
1471
+
1472
+ export function zenmuxModelManagerOptions(config?: ZenMuxModelManagerConfig): ModelManagerOptions<Api> {
1473
+ const apiKey = config?.apiKey;
1474
+ const openAiBaseUrl = normalizeZenMuxOpenAiBaseUrl(config?.baseUrl);
1475
+ const anthropicBaseUrl = toZenMuxAnthropicBaseUrl(openAiBaseUrl);
1476
+ return {
1477
+ providerId: "zenmux",
1478
+ ...(apiKey && {
1479
+ fetchDynamicModels: () =>
1480
+ fetchOpenAICompatibleModels<Api>({
1481
+ api: "openai-completions",
1482
+ provider: "zenmux",
1483
+ baseUrl: openAiBaseUrl,
1484
+ apiKey,
1485
+ mapModel: (entry, defaults) => {
1486
+ const pricings = isRecord(entry.pricings) ? entry.pricings : undefined;
1487
+ const capabilities = isRecord(entry.capabilities) ? entry.capabilities : undefined;
1488
+ const isAnthropicModel = isZenMuxAnthropicModel(entry, defaults.id);
1489
+ return {
1490
+ ...defaults,
1491
+ name: toModelName(entry.display_name, defaults.name),
1492
+ api: isAnthropicModel ? "anthropic-messages" : "openai-completions",
1493
+ baseUrl: isAnthropicModel ? anthropicBaseUrl : openAiBaseUrl,
1494
+ reasoning: capabilities?.reasoning === true || defaults.reasoning,
1495
+ input: toInputCapabilities(entry.input_modalities),
1496
+ cost: {
1497
+ input: getZenMuxPricingValue(pricings, "prompt"),
1498
+ output: getZenMuxPricingValue(pricings, "completion"),
1499
+ cacheRead: getZenMuxPricingValue(pricings, "input_cache_read"),
1500
+ cacheWrite: getZenMuxCacheWritePrice(pricings),
1501
+ },
1502
+ contextWindow: toPositiveNumber(entry.context_length, defaults.contextWindow),
1503
+ maxTokens: toPositiveNumber(entry.max_completion_tokens, defaults.maxTokens),
1504
+ };
1505
+ },
1506
+ }),
1507
+ }),
1508
+ };
1509
+ }
1510
+
1511
+ // ---------------------------------------------------------------------------
1512
+ // 10.6 Kilo Gateway
1513
+ // ---------------------------------------------------------------------------
1514
+
1515
+ export interface KiloModelManagerConfig {
1516
+ apiKey?: string;
1517
+ baseUrl?: string;
1518
+ }
1519
+
1520
+ export function kiloModelManagerOptions(config?: KiloModelManagerConfig): ModelManagerOptions<"openai-completions"> {
1521
+ const apiKey = config?.apiKey;
1522
+ const baseUrl = config?.baseUrl ?? "https://api.kilo.ai/api/gateway";
1523
+ return {
1524
+ providerId: "kilo",
1525
+ fetchDynamicModels: () =>
1526
+ fetchOpenAICompatibleModels({
1527
+ api: "openai-completions",
1528
+ provider: "kilo",
1529
+ baseUrl,
1530
+ apiKey,
1531
+ }),
1532
+ };
1533
+ }
1534
+
1535
+ // ---------------------------------------------------------------------------
1536
+ // Alibaba Coding Plan
1537
+ // ---------------------------------------------------------------------------
1538
+
1539
+ export interface AlibabaCodingPlanModelManagerConfig {
1540
+ apiKey?: string;
1541
+ baseUrl?: string;
1542
+ }
1543
+
1544
+ export function alibabaCodingPlanModelManagerOptions(
1545
+ config?: AlibabaCodingPlanModelManagerConfig,
1546
+ ): ModelManagerOptions<"openai-completions"> {
1547
+ const apiKey = config?.apiKey;
1548
+ const baseUrl = config?.baseUrl ?? "https://coding-intl.dashscope.aliyuncs.com/v1";
1549
+ const references = createBundledReferenceMap<"openai-completions">("alibaba-coding-plan");
1550
+ return {
1551
+ providerId: "alibaba-coding-plan",
1552
+ fetchDynamicModels: () =>
1553
+ fetchOpenAICompatibleModels({
1554
+ api: "openai-completions",
1555
+ provider: "alibaba-coding-plan",
1556
+ baseUrl,
1557
+ apiKey,
1558
+ mapModel: (entry, defaults) => {
1559
+ const reference = references.get(defaults.id);
1560
+ return mapWithBundledReference(entry, defaults, reference);
1561
+ },
1562
+ }),
1563
+ };
1564
+ }
1565
+
1566
+ // ---------------------------------------------------------------------------
1567
+ // 11. Vercel AI Gateway
1568
+ // ---------------------------------------------------------------------------
1569
+
1570
+ export interface VercelAiGatewayModelManagerConfig {
1571
+ apiKey?: string;
1572
+ baseUrl?: string;
1573
+ }
1574
+
1575
+ function normalizeVercelAiGatewayBaseUrls(rawBaseUrl: string | undefined): { baseUrl: string; catalogBaseUrl: string } {
1576
+ const baseUrl = (rawBaseUrl === undefined ? "https://ai-gateway.vercel.sh" : rawBaseUrl.trim()).replace(/\/+$/, "");
1577
+ const catalogBaseUrl = baseUrl === "" || baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
1578
+
1579
+ return {
1580
+ baseUrl: baseUrl.endsWith("/v1") ? baseUrl.slice(0, -3) : baseUrl,
1581
+ catalogBaseUrl,
1582
+ };
1583
+ }
1584
+
1585
+ export function vercelAiGatewayModelManagerOptions(
1586
+ config?: VercelAiGatewayModelManagerConfig,
1587
+ ): ModelManagerOptions<"anthropic-messages"> {
1588
+ const apiKey = config?.apiKey;
1589
+ const { baseUrl, catalogBaseUrl } = normalizeVercelAiGatewayBaseUrls(config?.baseUrl);
1590
+ return {
1591
+ providerId: "vercel-ai-gateway",
1592
+ fetchDynamicModels: () =>
1593
+ fetchOpenAICompatibleModels({
1594
+ api: "anthropic-messages",
1595
+ provider: "vercel-ai-gateway",
1596
+ baseUrl: catalogBaseUrl,
1597
+ apiKey,
1598
+ filterModel: (entry: OpenAICompatibleModelRecord) => {
1599
+ const tags = entry.tags;
1600
+ return Array.isArray(tags) && tags.includes("tool-use");
1601
+ },
1602
+ mapModel: (
1603
+ entry: OpenAICompatibleModelRecord,
1604
+ defaults: Model<"anthropic-messages">,
1605
+ _context: OpenAICompatibleModelMapperContext<"anthropic-messages">,
1606
+ ): Model<"anthropic-messages"> => {
1607
+ const pricing = entry.pricing as Record<string, unknown> | undefined;
1608
+ const tags = Array.isArray(entry.tags) ? (entry.tags as string[]) : [];
1609
+
1610
+ return {
1611
+ ...defaults,
1612
+ baseUrl,
1613
+ reasoning: tags.includes("reasoning"),
1614
+ input: tags.includes("vision") ? ["text", "image"] : ["text"],
1615
+ cost: {
1616
+ input: (toNumber(pricing?.input) ?? 0) * 1_000_000,
1617
+ output: (toNumber(pricing?.output) ?? 0) * 1_000_000,
1618
+ cacheRead: (toNumber(pricing?.input_cache_read) ?? 0) * 1_000_000,
1619
+ cacheWrite: (toNumber(pricing?.input_cache_write) ?? 0) * 1_000_000,
1620
+ },
1621
+ contextWindow:
1622
+ typeof entry.context_window === "number" ? entry.context_window : defaults.contextWindow,
1623
+ maxTokens: typeof entry.max_tokens === "number" ? entry.max_tokens : defaults.maxTokens,
1624
+ };
1625
+ },
1626
+ }),
1627
+ };
1628
+ }
1629
+
1630
+ // ---------------------------------------------------------------------------
1631
+ // 12. Kimi Code
1632
+ // ---------------------------------------------------------------------------
1633
+
1634
+ export interface KimiCodeModelManagerConfig {
1635
+ apiKey?: string;
1636
+ baseUrl?: string;
1637
+ }
1638
+
1639
+ export function kimiCodeModelManagerOptions(
1640
+ config?: KimiCodeModelManagerConfig,
1641
+ ): ModelManagerOptions<"openai-completions"> {
1642
+ const apiKey = config?.apiKey;
1643
+ const baseUrl = config?.baseUrl ?? "https://api.kimi.com/coding/v1";
1644
+ return {
1645
+ providerId: "kimi-code",
1646
+ ...(apiKey && {
1647
+ fetchDynamicModels: () =>
1648
+ fetchOpenAICompatibleModels({
1649
+ api: "openai-completions",
1650
+ provider: "kimi-code",
1651
+ baseUrl,
1652
+ apiKey,
1653
+ headers: {
1654
+ "User-Agent": "KimiCLI/1.0",
1655
+ "X-Msh-Platform": "kimi_cli",
1656
+ },
1657
+ mapModel: (
1658
+ entry: OpenAICompatibleModelRecord,
1659
+ defaults: Model<"openai-completions">,
1660
+ _context: OpenAICompatibleModelMapperContext<"openai-completions">,
1661
+ ): Model<"openai-completions"> => {
1662
+ const id = defaults.id;
1663
+ return {
1664
+ ...defaults,
1665
+ name: typeof entry.display_name === "string" ? entry.display_name : defaults.name,
1666
+ reasoning: entry.supports_reasoning === true || id.includes("thinking"),
1667
+ input: entry.supports_image_in === true || id.includes("k2.5") ? ["text", "image"] : ["text"],
1668
+ contextWindow: typeof entry.context_length === "number" ? entry.context_length : 262144,
1669
+ maxTokens: 32000,
1670
+ compat: {
1671
+ thinkingFormat: "zai",
1672
+ reasoningContentField: "reasoning_content",
1673
+ supportsDeveloperRole: false,
1674
+ },
1675
+ };
1676
+ },
1677
+ }),
1678
+ }),
1679
+ };
1680
+ }
1681
+
1682
+ // ---------------------------------------------------------------------------
1683
+ // 12.5. LM Studio
1684
+ // ---------------------------------------------------------------------------
1685
+
1686
+ export interface LmStudioModelManagerConfig {
1687
+ apiKey?: string;
1688
+ baseUrl?: string;
1689
+ }
1690
+
1691
+ export function lmStudioModelManagerOptions(
1692
+ config?: LmStudioModelManagerConfig,
1693
+ ): ModelManagerOptions<"openai-completions"> {
1694
+ const apiKey = config?.apiKey;
1695
+ const baseUrl = config?.baseUrl ?? Bun.env.LM_STUDIO_BASE_URL ?? "http://127.0.0.1:1234/v1";
1696
+ const references = createBundledReferenceMap<"openai-completions">("lm-studio" as any);
1697
+ return {
1698
+ providerId: "lm-studio",
1699
+ fetchDynamicModels: () =>
1700
+ fetchOpenAICompatibleModels({
1701
+ api: "openai-completions",
1702
+ provider: "lm-studio",
1703
+ baseUrl,
1704
+ apiKey,
1705
+ mapModel: (entry, defaults) => {
1706
+ const reference = references.get(defaults.id);
1707
+ return mapWithBundledReference(entry, defaults, reference);
1708
+ },
1709
+ }),
1710
+ };
1711
+ }
1712
+
1713
+ // ---------------------------------------------------------------------------
1714
+ // 13. Synthetic
1715
+ // ---------------------------------------------------------------------------
1716
+
1717
+ export interface SyntheticModelManagerConfig {
1718
+ apiKey?: string;
1719
+ baseUrl?: string;
1720
+ }
1721
+
1722
+ export function syntheticModelManagerOptions(
1723
+ config?: SyntheticModelManagerConfig,
1724
+ ): ModelManagerOptions<"openai-completions"> {
1725
+ const apiKey = config?.apiKey;
1726
+ const baseUrl = config?.baseUrl ?? "https://api.synthetic.new/openai/v1";
1727
+ const references = new Map(
1728
+ (getBundledModels("synthetic") as Model<"openai-completions">[]).map(model => [model.id, model]),
1729
+ );
1730
+ return {
1731
+ providerId: "synthetic",
1732
+ dynamicModelsAuthoritative: true,
1733
+ ...(apiKey && {
1734
+ fetchDynamicModels: () =>
1735
+ fetchOpenAICompatibleModels({
1736
+ api: "openai-completions",
1737
+ provider: "synthetic",
1738
+ baseUrl,
1739
+ apiKey,
1740
+ mapModel: (
1741
+ entry: OpenAICompatibleModelRecord,
1742
+ defaults: Model<"openai-completions">,
1743
+ _context: OpenAICompatibleModelMapperContext<"openai-completions">,
1744
+ ): Model<"openai-completions"> => {
1745
+ const reference = references.get(defaults.id);
1746
+ const referenceSupportsImage = reference?.input.includes("image") ?? false;
1747
+ return {
1748
+ ...(reference ? { ...reference, id: defaults.id, baseUrl } : defaults),
1749
+ name: toModelName(entry.name, reference?.name ?? defaults.name),
1750
+ reasoning: entry.supports_reasoning === true || (reference?.reasoning ?? false),
1751
+ input: entry.supports_vision === true || referenceSupportsImage ? ["text", "image"] : ["text"],
1752
+ contextWindow: toPositiveNumber(
1753
+ entry.context_length,
1754
+ reference?.contextWindow ?? defaults.contextWindow,
1755
+ ),
1756
+ maxTokens: toPositiveNumber(entry.max_tokens, reference?.maxTokens ?? 8192),
1757
+ };
1758
+ },
1759
+ }),
1760
+ }),
1761
+ };
1762
+ }
1763
+
1764
+ // ---------------------------------------------------------------------------
1765
+ // 14. Venice
1766
+ // ---------------------------------------------------------------------------
1767
+
1768
+ export interface VeniceModelManagerConfig {
1769
+ apiKey?: string;
1770
+ baseUrl?: string;
1771
+ }
1772
+
1773
+ export function veniceModelManagerOptions(
1774
+ config?: VeniceModelManagerConfig,
1775
+ ): ModelManagerOptions<"openai-completions"> {
1776
+ const apiKey = config?.apiKey;
1777
+ const baseUrl = config?.baseUrl ?? "https://api.venice.ai/api/v1";
1778
+ const references = createBundledReferenceMap<"openai-completions">("venice");
1779
+ return {
1780
+ providerId: "venice",
1781
+ fetchDynamicModels: () =>
1782
+ fetchOpenAICompatibleModels({
1783
+ api: "openai-completions",
1784
+ provider: "venice",
1785
+ baseUrl,
1786
+ apiKey,
1787
+ mapModel: (entry, defaults) => {
1788
+ const reference = references.get(defaults.id);
1789
+ const model = mapWithBundledReference(entry, defaults, reference);
1790
+ return {
1791
+ ...model,
1792
+ compat: { ...model.compat, supportsUsageInStreaming: false },
1793
+ };
1794
+ },
1795
+ }),
1796
+ };
1797
+ }
1798
+
1799
+ // ---------------------------------------------------------------------------
1800
+ // 15. Together
1801
+ // ---------------------------------------------------------------------------
1802
+
1803
+ export interface TogetherModelManagerConfig {
1804
+ apiKey?: string;
1805
+ baseUrl?: string;
1806
+ }
1807
+
1808
+ export function togetherModelManagerOptions(
1809
+ config?: TogetherModelManagerConfig,
1810
+ ): ModelManagerOptions<"openai-completions"> {
1811
+ return createSimpleOpenAICompletionsOptions("together", "https://api.together.xyz/v1", config);
1812
+ }
1813
+
1814
+ // ---------------------------------------------------------------------------
1815
+ // 16. Moonshot
1816
+ // ---------------------------------------------------------------------------
1817
+
1818
+ export interface MoonshotModelManagerConfig {
1819
+ apiKey?: string;
1820
+ baseUrl?: string;
1821
+ }
1822
+
1823
+ export function moonshotModelManagerOptions(
1824
+ config?: MoonshotModelManagerConfig,
1825
+ ): ModelManagerOptions<"openai-completions"> {
1826
+ const apiKey = config?.apiKey;
1827
+ const baseUrl = config?.baseUrl ?? "https://api.moonshot.ai/v1";
1828
+ const references = createBundledReferenceMap<"openai-completions">("moonshot");
1829
+ return {
1830
+ providerId: "moonshot",
1831
+ ...(apiKey && {
1832
+ fetchDynamicModels: () =>
1833
+ fetchOpenAICompatibleModels({
1834
+ api: "openai-completions",
1835
+ provider: "moonshot",
1836
+ baseUrl,
1837
+ apiKey,
1838
+ mapModel: (entry, defaults) => {
1839
+ const reference = references.get(defaults.id);
1840
+ const model = mapWithBundledReference(entry, defaults, reference);
1841
+ const id = model.id.toLowerCase();
1842
+ const isThinking = id.includes("thinking");
1843
+ const isVision = id.includes("vision") || id.includes("vl") || id.includes("k2.5");
1844
+ return {
1845
+ ...model,
1846
+ reasoning: isThinking || model.reasoning,
1847
+ input: isVision ? ["text", "image"] : model.input,
1848
+ };
1849
+ },
1850
+ }),
1851
+ }),
1852
+ };
1853
+ }
1854
+
1855
+ // ---------------------------------------------------------------------------
1856
+ // 17. Qwen Portal
1857
+ // ---------------------------------------------------------------------------
1858
+
1859
+ export interface QwenPortalModelManagerConfig {
1860
+ apiKey?: string;
1861
+ baseUrl?: string;
1862
+ }
1863
+
1864
+ export function qwenPortalModelManagerOptions(
1865
+ config?: QwenPortalModelManagerConfig,
1866
+ ): ModelManagerOptions<"openai-completions"> {
1867
+ return createSimpleOpenAICompletionsOptions("qwen-portal", "https://portal.qwen.ai/v1", config);
1868
+ }
1869
+
1870
+ // ---------------------------------------------------------------------------
1871
+ // 18. Qianfan
1872
+ // ---------------------------------------------------------------------------
1873
+
1874
+ export interface QianfanModelManagerConfig {
1875
+ apiKey?: string;
1876
+ baseUrl?: string;
1877
+ }
1878
+
1879
+ export function qianfanModelManagerOptions(
1880
+ config?: QianfanModelManagerConfig,
1881
+ ): ModelManagerOptions<"openai-completions"> {
1882
+ return createSimpleOpenAICompletionsOptions("qianfan", "https://qianfan.baidubce.com/v2", config);
1883
+ }
1884
+
1885
+ // ---------------------------------------------------------------------------
1886
+ // 19. Cloudflare AI Gateway
1887
+ // ---------------------------------------------------------------------------
1888
+
1889
+ export interface CloudflareAiGatewayModelManagerConfig {
1890
+ apiKey?: string;
1891
+ baseUrl?: string;
1892
+ }
1893
+
1894
+ export function cloudflareAiGatewayModelManagerOptions(
1895
+ config?: CloudflareAiGatewayModelManagerConfig,
1896
+ ): ModelManagerOptions<"anthropic-messages"> {
1897
+ return createSimpleAnthropicProviderOptions(
1898
+ "cloudflare-ai-gateway",
1899
+ "https://gateway.ai.cloudflare.com/v1/<account>/<gateway>/anthropic",
1900
+ config,
1901
+ );
1902
+ }
1903
+
1904
+ // ---------------------------------------------------------------------------
1905
+ // 20. Xiaomi
1906
+ // ---------------------------------------------------------------------------
1907
+
1908
+ export interface XiaomiModelManagerConfig {
1909
+ apiKey?: string;
1910
+ baseUrl?: string;
1911
+ }
1912
+
1913
+ export function xiaomiModelManagerOptions(
1914
+ config?: XiaomiModelManagerConfig,
1915
+ ): ModelManagerOptions<"openai-completions"> {
1916
+ const apiKey = config?.apiKey;
1917
+ // Xiaomi splits API keys across two backends: standard `sk-` keys hit
1918
+ // api.xiaomimimo.com; "token plan" `tp-` keys are scoped to a regional
1919
+ // cluster and are tried in order until discovery succeeds.
1920
+ const TOKEN_PLAN_BASE_URLS = [
1921
+ "https://token-plan-sgp.xiaomimimo.com/v1",
1922
+ "https://token-plan-ams.xiaomimimo.com/v1",
1923
+ "https://token-plan-cn.xiaomimimo.com/v1",
1924
+ ] as const;
1925
+ const STANDARD_BASE_URL = "https://api.xiaomimimo.com/v1";
1926
+ const isTokenPlanKey = apiKey?.startsWith("tp-");
1927
+ // Token-plan keys always use a TP cluster; config?.baseUrl (from catalog)
1928
+ // would incorrectly pin to the standard endpoint (api.xiaomimimo.com).
1929
+ const baseUrl = isTokenPlanKey ? TOKEN_PLAN_BASE_URLS[0] : (config?.baseUrl ?? STANDARD_BASE_URL);
1930
+ const references = createBundledReferenceMap<"openai-completions">("xiaomi");
1931
+ const fetchModels = (url: string) =>
1932
+ fetchOpenAICompatibleModels({
1933
+ api: "openai-completions",
1934
+ provider: "xiaomi",
1935
+ baseUrl: url,
1936
+ apiKey,
1937
+ filterModel: (_entry, model) => !model.id.includes("-tts"),
1938
+ mapModel: (entry, defaults) => {
1939
+ const reference = references.get(defaults.id);
1940
+ const model = mapWithBundledReference(entry, defaults, reference);
1941
+ return {
1942
+ ...model,
1943
+ name: toModelName(entry.display_name, model.name),
1944
+ };
1945
+ },
1946
+ });
1947
+ return {
1948
+ providerId: "xiaomi",
1949
+ ...(apiKey && {
1950
+ fetchDynamicModels: async () => {
1951
+ if (!isTokenPlanKey) {
1952
+ return fetchModels(baseUrl);
1953
+ }
1954
+ for (const url of TOKEN_PLAN_BASE_URLS) {
1955
+ const result = await fetchModels(url);
1956
+ if (result) return result;
1957
+ }
1958
+ return null;
1959
+ },
1960
+ }),
1961
+ };
1962
+ }
1963
+ // ---------------------------------------------------------------------------
1964
+ // 21. LiteLLM
1965
+ // ---------------------------------------------------------------------------
1966
+
1967
+ export interface LiteLLMModelManagerConfig {
1968
+ apiKey?: string;
1969
+ baseUrl?: string;
1970
+ }
1971
+
1972
+ export function litellmModelManagerOptions(
1973
+ config?: LiteLLMModelManagerConfig,
1974
+ ): ModelManagerOptions<"openai-completions"> {
1975
+ const apiKey = config?.apiKey;
1976
+ const baseUrl = config?.baseUrl ?? "http://localhost:4000/v1";
1977
+ const references = createBundledReferenceMap<"openai-completions">("litellm");
1978
+ return {
1979
+ providerId: "litellm",
1980
+ fetchDynamicModels: () =>
1981
+ fetchOpenAICompatibleModels({
1982
+ api: "openai-completions",
1983
+ provider: "litellm",
1984
+ baseUrl,
1985
+ apiKey,
1986
+ mapModel: (entry, defaults) => {
1987
+ const reference = references.get(defaults.id);
1988
+ return mapWithBundledReference(entry, defaults, reference);
1989
+ },
1990
+ }),
1991
+ };
1992
+ }
1993
+
1994
+ // ---------------------------------------------------------------------------
1995
+ // 22. vLLM
1996
+ // ---------------------------------------------------------------------------
1997
+
1998
+ export interface VllmModelManagerConfig {
1999
+ apiKey?: string;
2000
+ baseUrl?: string;
2001
+ }
2002
+
2003
+ export function vllmModelManagerOptions(config?: VllmModelManagerConfig): ModelManagerOptions<"openai-completions"> {
2004
+ const apiKey = config?.apiKey;
2005
+ const baseUrl = config?.baseUrl ?? "http://127.0.0.1:8000/v1";
2006
+ const references = createBundledReferenceMap<"openai-completions">("vllm" as Parameters<typeof getBundledModels>[0]);
2007
+ return {
2008
+ providerId: "vllm",
2009
+ fetchDynamicModels: () =>
2010
+ fetchOpenAICompatibleModels({
2011
+ api: "openai-completions",
2012
+ provider: "vllm",
2013
+ baseUrl,
2014
+ apiKey,
2015
+ mapModel: (entry, defaults) => {
2016
+ const model = mapWithBundledReference(entry, defaults, references.get(defaults.id));
2017
+ return {
2018
+ ...model,
2019
+ contextWindow: toPositiveNumber(entry.max_model_len, model.contextWindow),
2020
+ };
2021
+ },
2022
+ }),
2023
+ };
2024
+ }
2025
+
2026
+ // ---------------------------------------------------------------------------
2027
+ // 23. NanoGPT
2028
+ // ---------------------------------------------------------------------------
2029
+
2030
+ export interface NanoGptModelManagerConfig {
2031
+ apiKey?: string;
2032
+ baseUrl?: string;
2033
+ }
2034
+
2035
+ export function nanoGptModelManagerOptions(
2036
+ config?: NanoGptModelManagerConfig,
2037
+ ): ModelManagerOptions<"openai-completions"> {
2038
+ const apiKey = config?.apiKey;
2039
+ const baseUrl = config?.baseUrl ?? "https://nano-gpt.com/api/v1";
2040
+ const resolveReference = createReferenceResolver(
2041
+ createBundledReferenceMap<"openai-completions">("nanogpt" as Parameters<typeof getBundledModels>[0]),
2042
+ );
2043
+ return {
2044
+ providerId: "nanogpt",
2045
+ ...(apiKey && {
2046
+ fetchDynamicModels: async () => {
2047
+ // Track base IDs that have :thinking variants so we can mark them reasoning-capable.
2048
+ const thinkingBaseIds = new Set<string>();
2049
+ const models = await fetchOpenAICompatibleModels({
2050
+ api: "openai-completions",
2051
+ provider: "nanogpt",
2052
+ baseUrl,
2053
+ apiKey,
2054
+ mapModel: (entry, defaults) => {
2055
+ const reference = resolveReference(defaults.id);
2056
+ const mapped = mapWithBundledReference(entry, defaults, reference);
2057
+ return { ...mapped, api: "openai-completions", provider: "nanogpt" };
2058
+ },
2059
+ filterModel: (_entry, model) => {
2060
+ const match = NANO_GPT_THINKING_SUFFIX_RE.exec(model.id);
2061
+ if (match) {
2062
+ thinkingBaseIds.add(model.id.slice(0, match.index));
2063
+ return false;
2064
+ }
2065
+ return isLikelyNanoGptTextModelId(model.id);
2066
+ },
2067
+ });
2068
+ if (!models) return null;
2069
+ // Mark base models as reasoning-capable when a :thinking variant existed.
2070
+ for (const model of models) {
2071
+ if (!model.reasoning && thinkingBaseIds.has(model.id)) {
2072
+ (model as { reasoning: boolean }).reasoning = true;
2073
+ }
2074
+ }
2075
+ return models;
2076
+ },
2077
+ }),
2078
+ };
2079
+ }
2080
+
2081
+ // ---------------------------------------------------------------------------
2082
+ // 24. GitHub Copilot
2083
+ // ---------------------------------------------------------------------------
2084
+
2085
+ export interface GithubCopilotModelManagerConfig {
2086
+ apiKey?: string;
2087
+ baseUrl?: string;
2088
+ }
2089
+
2090
+ function inferCopilotApi(modelId: string): Api {
2091
+ if (/^claude-(haiku|sonnet|opus)-4([.-]|$)/.test(modelId)) {
2092
+ return "anthropic-messages";
2093
+ }
2094
+ if (modelId.startsWith("gpt-5") || modelId.startsWith("oswe")) {
2095
+ return "openai-responses";
2096
+ }
2097
+ return "openai-completions";
2098
+ }
2099
+
2100
+ function extractCopilotLimits(entry: OpenAICompatibleModelRecord): {
2101
+ maxPromptTokens?: number;
2102
+ maxContextWindowTokens?: number;
2103
+ maxOutputTokens?: number;
2104
+ maxNonStreamingOutputTokens?: number;
2105
+ } {
2106
+ if (!isRecord(entry.capabilities)) {
2107
+ return {};
2108
+ }
2109
+ const limitsValue = entry.capabilities.limits;
2110
+ if (!isRecord(limitsValue)) {
2111
+ return {};
2112
+ }
2113
+ return {
2114
+ maxPromptTokens: toNumber(limitsValue.max_prompt_tokens),
2115
+ maxContextWindowTokens: toNumber(limitsValue.max_context_window_tokens),
2116
+ maxOutputTokens: toNumber(limitsValue.max_output_tokens),
2117
+ maxNonStreamingOutputTokens: toNumber(limitsValue.max_non_streaming_output_tokens),
2118
+ };
2119
+ }
2120
+
2121
+ export function githubCopilotModelManagerOptions(config?: GithubCopilotModelManagerConfig): ModelManagerOptions<Api> {
2122
+ const rawApiKey = config?.apiKey;
2123
+ const configuredBaseUrl = config?.baseUrl ?? "https://api.githubcopilot.com";
2124
+ const parsedApiKey = rawApiKey ? parseGitHubCopilotApiKey(rawApiKey) : undefined;
2125
+ const apiKey = parsedApiKey?.accessToken;
2126
+ const baseUrl =
2127
+ parsedApiKey?.enterpriseUrl && configuredBaseUrl.includes("githubcopilot.com")
2128
+ ? getGitHubCopilotBaseUrl(parsedApiKey.enterpriseUrl)
2129
+ : configuredBaseUrl;
2130
+ const providerRefs = createBundledReferenceMap<Api>("github-copilot");
2131
+ const resolveReference = createReferenceResolver(providerRefs);
2132
+ return {
2133
+ providerId: "github-copilot",
2134
+ ...(apiKey && {
2135
+ fetchDynamicModels: () =>
2136
+ fetchOpenAICompatibleModels<Api>({
2137
+ api: "openai-completions",
2138
+ provider: "github-copilot",
2139
+ baseUrl,
2140
+ apiKey,
2141
+ headers: OPENCODE_HEADERS,
2142
+ mapModel: (
2143
+ entry: OpenAICompatibleModelRecord,
2144
+ defaults: Model<Api>,
2145
+ _context: OpenAICompatibleModelMapperContext<Api>,
2146
+ ): Model<Api> => {
2147
+ const reference = resolveReference(defaults.id);
2148
+ const copilotLimits = extractCopilotLimits(entry);
2149
+ // Copilot exposes token limits under capabilities.limits.*.
2150
+ // max_context_window_tokens is the model's total usable window;
2151
+ // max_prompt_tokens is Copilot's prompt/summarization budget and
2152
+ // must only be a fallback when total-window fields are absent.
2153
+ const contextWindow = toPositiveNumber(
2154
+ copilotLimits.maxContextWindowTokens,
2155
+ toPositiveNumber(
2156
+ entry.context_length,
2157
+ toPositiveNumber(
2158
+ copilotLimits.maxPromptTokens,
2159
+ reference?.contextWindow ?? defaults.contextWindow,
2160
+ ),
2161
+ ),
2162
+ );
2163
+ const maxTokens = toPositiveNumber(
2164
+ copilotLimits.maxOutputTokens,
2165
+ toPositiveNumber(
2166
+ entry.max_completion_tokens,
2167
+ toPositiveNumber(
2168
+ copilotLimits.maxNonStreamingOutputTokens,
2169
+ reference?.maxTokens ?? defaults.maxTokens,
2170
+ ),
2171
+ ),
2172
+ );
2173
+ const name =
2174
+ typeof entry.name === "string" && entry.name.trim().length > 0
2175
+ ? entry.name
2176
+ : (reference?.name ?? defaults.name);
2177
+ const api = inferCopilotApi(defaults.id);
2178
+ if (reference) {
2179
+ return {
2180
+ ...reference,
2181
+ api,
2182
+ provider: "github-copilot",
2183
+ baseUrl,
2184
+ name,
2185
+ contextWindow,
2186
+ maxTokens,
2187
+ headers: { ...OPENCODE_HEADERS, ...(providerRefs.get(defaults.id)?.headers ?? {}) },
2188
+ ...(api === "openai-completions"
2189
+ ? {
2190
+ compat: {
2191
+ supportsStore: false,
2192
+ supportsDeveloperRole: false,
2193
+ supportsReasoningEffort: false,
2194
+ },
2195
+ }
2196
+ : {}),
2197
+ };
2198
+ }
2199
+ return {
2200
+ ...defaults,
2201
+ api,
2202
+ baseUrl,
2203
+ name,
2204
+ contextWindow,
2205
+ maxTokens,
2206
+ headers: { ...OPENCODE_HEADERS },
2207
+ ...(api === "openai-completions"
2208
+ ? {
2209
+ compat: {
2210
+ supportsStore: false,
2211
+ supportsDeveloperRole: false,
2212
+ supportsReasoningEffort: false,
2213
+ },
2214
+ }
2215
+ : {}),
2216
+ };
2217
+ },
2218
+ }),
2219
+ }),
2220
+ };
2221
+ }
2222
+
2223
+ // ---------------------------------------------------------------------------
2224
+ // 24. Anthropic
2225
+ // ---------------------------------------------------------------------------
2226
+
2227
+ export interface AnthropicModelManagerConfig {
2228
+ apiKey?: string;
2229
+ baseUrl?: string;
2230
+ }
2231
+
2232
+ export function anthropicModelManagerOptions(
2233
+ config?: AnthropicModelManagerConfig,
2234
+ ): ModelManagerOptions<"anthropic-messages"> {
2235
+ const apiKey = config?.apiKey;
2236
+ const baseUrl = config?.baseUrl ?? ANTHROPIC_BASE_URL;
2237
+ return {
2238
+ providerId: "anthropic",
2239
+ modelsDev: {
2240
+ fetch: fetchModelsDevPayload,
2241
+ map: payload => mapAnthropicModelsDev(payload, baseUrl),
2242
+ },
2243
+ ...(apiKey && {
2244
+ fetchDynamicModels: async () => {
2245
+ const modelsDevModels = await fetchModelsDevPayload()
2246
+ .then(payload => mapAnthropicModelsDev(payload, baseUrl))
2247
+ .catch(() => []);
2248
+ const references = buildAnthropicReferenceMap(modelsDevModels);
2249
+ return (
2250
+ fetchOpenAICompatibleModels({
2251
+ api: "anthropic-messages",
2252
+ provider: "anthropic",
2253
+ baseUrl,
2254
+ headers: buildAnthropicDiscoveryHeaders(apiKey),
2255
+ mapModel: (
2256
+ entry: OpenAICompatibleModelRecord,
2257
+ defaults: Model<"anthropic-messages">,
2258
+ _context: OpenAICompatibleModelMapperContext<"anthropic-messages">,
2259
+ ): Model<"anthropic-messages"> => {
2260
+ const discoveredName = typeof entry.display_name === "string" ? entry.display_name : defaults.name;
2261
+ const reference = references.get(defaults.id);
2262
+ if (!reference) {
2263
+ return {
2264
+ ...defaults,
2265
+ name: discoveredName,
2266
+ };
2267
+ }
2268
+ return {
2269
+ ...reference,
2270
+ id: defaults.id,
2271
+ name: discoveredName,
2272
+ api: "anthropic-messages",
2273
+ provider: "anthropic",
2274
+ baseUrl,
2275
+ };
2276
+ },
2277
+ }) ?? null
2278
+ );
2279
+ },
2280
+ }),
2281
+ };
2282
+ }
2283
+
2284
+ // ---------------------------------------------------------------------------
2285
+ // Models.dev provider descriptors for generate-models.ts
2286
+ // ---------------------------------------------------------------------------
2287
+
2288
+ export const UNK_CONTEXT_WINDOW = 222_222;
2289
+ export const UNK_MAX_TOKENS = 8_888;
2290
+
2291
+ /** Describes how to map models.dev API data for a single provider. */
2292
+ export interface ModelsDevProviderDescriptor {
2293
+ /** Key in the models.dev API response JSON (e.g., "anthropic", "amazon-bedrock") */
2294
+ modelsDevKey: string;
2295
+ /** Provider ID in our system */
2296
+ providerId: string;
2297
+ /** Default API type for this provider's models */
2298
+ api: Api;
2299
+ /** Default base URL */
2300
+ baseUrl: string;
2301
+ /** Default context window fallback (default: UNKNNOWN_CONTEXT_WINDOW) */
2302
+ defaultContextWindow?: number;
2303
+ /** Default max tokens fallback (default: UNKNNOWN_MAX_TOKENS) */
2304
+ defaultMaxTokens?: number;
2305
+ /** Optional compat overrides applied to every model from this provider */
2306
+ compat?: Model<Api>["compat"];
2307
+ /** Optional static headers applied to every model */
2308
+ headers?: Record<string, string>;
2309
+ /**
2310
+ * Optional filter: return false to skip a model.
2311
+ * Called with (modelId, rawModel). Default: skip if tool_call !== true.
2312
+ */
2313
+ filterModel?: (modelId: string, model: ModelsDevModel) => boolean;
2314
+ /**
2315
+ * Optional transform: modify the mapped model before it's added.
2316
+ * Can return null to skip the model, or an array to emit multiple models.
2317
+ */
2318
+ transformModel?: (model: Model<Api>, modelId: string, raw: ModelsDevModel) => Model<Api> | Model<Api>[] | null;
2319
+ /**
2320
+ * Optional: override the API type per-model.
2321
+ * Called with (modelId, raw). Return the API type to use.
2322
+ * If not provided, uses the `api` field.
2323
+ */
2324
+ resolveApi?: (modelId: string, raw: ModelsDevModel) => { api: Api; baseUrl: string } | null;
2325
+ }
2326
+
2327
+ /** Generic mapper that converts models.dev data using provider descriptors. */
2328
+ export function mapModelsDevToModels(
2329
+ data: Record<string, unknown>,
2330
+ descriptors: readonly ModelsDevProviderDescriptor[],
2331
+ ): Model<Api>[] {
2332
+ const models: Model<Api>[] = [];
2333
+ for (const desc of descriptors) {
2334
+ const providerData = (data as Record<string, Record<string, unknown>>)[desc.modelsDevKey];
2335
+ if (!isRecord(providerData) || !isRecord(providerData.models)) continue;
2336
+
2337
+ for (const [modelId, rawModel] of Object.entries(providerData.models)) {
2338
+ if (!isRecord(rawModel)) continue;
2339
+ const m = rawModel as ModelsDevModel;
2340
+
2341
+ // Default filter: tool_call must be true
2342
+ if (desc.filterModel) {
2343
+ if (!desc.filterModel(modelId, m)) continue;
2344
+ } else {
2345
+ if (m.tool_call !== true) continue;
2346
+ }
2347
+
2348
+ // Resolve API and baseUrl (may be per-model for providers like OpenCode)
2349
+ const resolved = desc.resolveApi?.(modelId, m) ?? { api: desc.api, baseUrl: desc.baseUrl };
2350
+ if (!resolved) continue;
2351
+
2352
+ const mapped: Model<Api> = {
2353
+ id: modelId,
2354
+ name: toModelName(m.name, modelId),
2355
+ api: resolved.api,
2356
+ provider: desc.providerId as Model<Api>["provider"],
2357
+ baseUrl: resolved.baseUrl,
2358
+ reasoning: m.reasoning === true,
2359
+ input: toInputCapabilities(m.modalities?.input),
2360
+ cost: {
2361
+ input: toNumber(m.cost?.input) ?? 0,
2362
+ output: toNumber(m.cost?.output) ?? 0,
2363
+ cacheRead: toNumber(m.cost?.cache_read) ?? 0,
2364
+ cacheWrite: toNumber(m.cost?.cache_write) ?? 0,
2365
+ },
2366
+ contextWindow: toPositiveNumber(m.limit?.context, desc.defaultContextWindow ?? UNK_CONTEXT_WINDOW),
2367
+ maxTokens: toPositiveNumber(m.limit?.output, desc.defaultMaxTokens ?? UNK_MAX_TOKENS),
2368
+ ...(desc.compat && { compat: desc.compat }),
2369
+ ...(desc.headers && { headers: { ...desc.headers } }),
2370
+ };
2371
+
2372
+ // Apply per-model transform
2373
+ if (desc.transformModel) {
2374
+ const result = desc.transformModel(mapped, modelId, m);
2375
+ if (result === null) continue;
2376
+ if (Array.isArray(result)) {
2377
+ models.push(...result);
2378
+ } else {
2379
+ models.push(result);
2380
+ }
2381
+ } else {
2382
+ models.push(mapped);
2383
+ }
2384
+ }
2385
+ }
2386
+ return models;
2387
+ }
2388
+
2389
+ // Bedrock cross-region prefix helpers
2390
+ const BEDROCK_GLOBAL_PREFIXES = [
2391
+ "anthropic.claude-haiku-4-5",
2392
+ "anthropic.claude-sonnet-4",
2393
+ "anthropic.claude-opus-4-5",
2394
+ "amazon.nova-2-lite",
2395
+ "cohere.embed-v4",
2396
+ "twelvelabs.pegasus-1-2",
2397
+ ];
2398
+
2399
+ const BEDROCK_US_PREFIXES = [
2400
+ "amazon.nova-lite",
2401
+ "amazon.nova-micro",
2402
+ "amazon.nova-premier",
2403
+ "amazon.nova-pro",
2404
+ "anthropic.claude-3-7-sonnet",
2405
+ "anthropic.claude-opus-4-1",
2406
+ "anthropic.claude-opus-4-20250514",
2407
+ "deepseek.r1",
2408
+ "meta.llama3-2",
2409
+ "meta.llama3-3",
2410
+ "meta.llama4",
2411
+ ];
2412
+
2413
+ function bedrockCrossRegionId(id: string): string {
2414
+ if (BEDROCK_GLOBAL_PREFIXES.some(p => id.startsWith(p))) return `global.${id}`;
2415
+ if (BEDROCK_US_PREFIXES.some(p => id.startsWith(p))) return `us.${id}`;
2416
+ return id;
2417
+ }
2418
+
2419
+ interface ApiResolutionRule {
2420
+ matches: (modelId: string, raw: ModelsDevModel) => boolean;
2421
+ resolved: { api: Api; baseUrl: string };
2422
+ }
2423
+
2424
+ function resolveApiByRules(
2425
+ modelId: string,
2426
+ raw: ModelsDevModel,
2427
+ rules: readonly ApiResolutionRule[],
2428
+ fallback: { api: Api; baseUrl: string },
2429
+ ): { api: Api; baseUrl: string } {
2430
+ for (const rule of rules) {
2431
+ if (rule.matches(modelId, raw)) return rule.resolved;
2432
+ }
2433
+ return fallback;
2434
+ }
2435
+
2436
+ function createOpenCodeApiResolution(
2437
+ basePath: string,
2438
+ idOverrides: Readonly<Record<string, Api>> = {},
2439
+ ): {
2440
+ defaultResolution: { api: Api; baseUrl: string };
2441
+ rules: ApiResolutionRule[];
2442
+ } {
2443
+ const completionsBaseUrl = `${basePath}/v1`;
2444
+ // Per-API base URLs on the OpenCode-style endpoint:
2445
+ // - openai-completions / openai-responses / google-generative-ai → /v1
2446
+ // - anthropic-messages → bare basePath (the Anthropic client appends /v1/messages)
2447
+ const baseUrlForApi = (api: Api): string => (api === "anthropic-messages" ? basePath : completionsBaseUrl);
2448
+ const overrideRules: ApiResolutionRule[] = Object.entries(idOverrides).map(([id, api]) => ({
2449
+ matches: modelId => modelId === id,
2450
+ resolved: { api, baseUrl: baseUrlForApi(api) },
2451
+ }));
2452
+ return {
2453
+ defaultResolution: { api: "openai-completions", baseUrl: completionsBaseUrl },
2454
+ rules: [
2455
+ // Per-id overrides take precedence over npm-based heuristics so we can
2456
+ // correct upstream metadata mismatches (see OPENCODE_GO_API_RESOLUTION).
2457
+ ...overrideRules,
2458
+ {
2459
+ matches: (_modelId, raw) => raw.provider?.npm === "@ai-sdk/openai",
2460
+ resolved: { api: "openai-responses", baseUrl: completionsBaseUrl },
2461
+ },
2462
+ {
2463
+ matches: (_modelId, raw) => raw.provider?.npm === "@ai-sdk/anthropic",
2464
+ resolved: { api: "anthropic-messages", baseUrl: basePath },
2465
+ },
2466
+ {
2467
+ matches: (_modelId, raw) => raw.provider?.npm === "@ai-sdk/google",
2468
+ resolved: { api: "google-generative-ai", baseUrl: completionsBaseUrl },
2469
+ },
2470
+ ],
2471
+ };
2472
+ }
2473
+
2474
+ const OPENCODE_ZEN_API_RESOLUTION = createOpenCodeApiResolution("https://opencode.ai/zen");
2475
+ // OpenCode Go: models.dev declares minimax-m2.7 / qwen3.5-plus / qwen3.6-plus
2476
+ // with `provider.npm = "@ai-sdk/anthropic"`, but the OpenCode Go gateway only
2477
+ // serves them at `https://opencode.ai/zen/go/v1/chat/completions` (verified
2478
+ // against https://opencode.ai/zen/go/v1/models and the upstream endpoint
2479
+ // table at https://opencode.ai/docs/go/#endpoints — minimax-m2.5 works the
2480
+ // same way and lacks an `npm` field on models.dev so it already falls through
2481
+ // to the openai-completions default). Without this override the resolver
2482
+ // would POST anthropic-style requests to /v1/messages and the gateway would
2483
+ // return its `Page Not Found` HTML (issue #887). Override the resolver so
2484
+ // regenerating models.json keeps the correct routing.
2485
+ const OPENCODE_GO_API_RESOLUTION = createOpenCodeApiResolution("https://opencode.ai/zen/go", {
2486
+ "minimax-m2.7": "openai-completions",
2487
+ "qwen3.5-plus": "openai-completions",
2488
+ "qwen3.6-plus": "openai-completions",
2489
+ });
2490
+
2491
+ const COPILOT_BASE_URL = "https://api.githubcopilot.com";
2492
+
2493
+ const COPILOT_DEFAULT_RESOLUTION = {
2494
+ api: "openai-completions",
2495
+ baseUrl: COPILOT_BASE_URL,
2496
+ } as const satisfies { api: Api; baseUrl: string };
2497
+
2498
+ const COPILOT_API_RESOLUTION_RULES: readonly ApiResolutionRule[] = [
2499
+ {
2500
+ matches: modelId => /^claude-(haiku|sonnet|opus)-4([.-]|$)/.test(modelId),
2501
+ resolved: { api: "anthropic-messages", baseUrl: COPILOT_BASE_URL },
2502
+ },
2503
+ {
2504
+ matches: modelId => modelId.startsWith("gpt-5") || modelId.startsWith("oswe"),
2505
+ resolved: { api: "openai-responses", baseUrl: COPILOT_BASE_URL },
2506
+ },
2507
+ ];
2508
+
2509
+ function simpleModelsDevDescriptor(
2510
+ modelsDevKey: string,
2511
+ providerId: string,
2512
+ api: Api,
2513
+ baseUrl: string,
2514
+ options: Omit<ModelsDevProviderDescriptor, "modelsDevKey" | "providerId" | "api" | "baseUrl"> = {},
2515
+ ): ModelsDevProviderDescriptor {
2516
+ return {
2517
+ modelsDevKey,
2518
+ providerId,
2519
+ api,
2520
+ baseUrl,
2521
+ ...options,
2522
+ };
2523
+ }
2524
+
2525
+ function openAiCompletionsDescriptor(
2526
+ modelsDevKey: string,
2527
+ providerId: string,
2528
+ baseUrl: string,
2529
+ options: Omit<ModelsDevProviderDescriptor, "modelsDevKey" | "providerId" | "api" | "baseUrl"> = {},
2530
+ ): ModelsDevProviderDescriptor {
2531
+ return simpleModelsDevDescriptor(modelsDevKey, providerId, "openai-completions", baseUrl, options);
2532
+ }
2533
+
2534
+ function anthropicMessagesDescriptor(
2535
+ modelsDevKey: string,
2536
+ providerId: string,
2537
+ baseUrl: string,
2538
+ options: Omit<ModelsDevProviderDescriptor, "modelsDevKey" | "providerId" | "api" | "baseUrl"> = {},
2539
+ ): ModelsDevProviderDescriptor {
2540
+ return simpleModelsDevDescriptor(modelsDevKey, providerId, "anthropic-messages", baseUrl, options);
2541
+ }
2542
+
2543
+ const GOOGLE_VERTEX_BASE_URL = "https://{location}-aiplatform.googleapis.com";
2544
+ const GOOGLE_VERTEX_OPENAI_BASE_URL =
2545
+ "https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/endpoints/openapi";
2546
+ const GOOGLE_VERTEX_ANTHROPIC_BASE_URL =
2547
+ "https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:streamRawPredict";
2548
+
2549
+ function resolveGoogleVertexApi(modelId: string, raw: ModelsDevModel): { api: Api; baseUrl: string } {
2550
+ if (raw.provider?.npm === "@ai-sdk/google-vertex/anthropic") {
2551
+ return {
2552
+ api: "anthropic-messages",
2553
+ baseUrl: GOOGLE_VERTEX_ANTHROPIC_BASE_URL.replace("{model}", modelId),
2554
+ };
2555
+ }
2556
+ if (modelId.includes("/") || raw.provider?.npm === "@ai-sdk/openai-compatible") {
2557
+ return { api: "openai-completions", baseUrl: GOOGLE_VERTEX_OPENAI_BASE_URL };
2558
+ }
2559
+ return { api: "google-vertex", baseUrl: GOOGLE_VERTEX_BASE_URL };
2560
+ }
2561
+
2562
+ const MODELS_DEV_PROVIDER_DESCRIPTORS_BEDROCK: readonly ModelsDevProviderDescriptor[] = [
2563
+ // --- Amazon Bedrock ---
2564
+ {
2565
+ modelsDevKey: "amazon-bedrock",
2566
+ providerId: "amazon-bedrock",
2567
+ api: "bedrock-converse-stream",
2568
+ baseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com",
2569
+ filterModel: (id, m) => {
2570
+ if (m.tool_call !== true) return false;
2571
+ if (id.startsWith("ai21.jamba")) return false;
2572
+ if (id.startsWith("amazon.titan-text-express") || id.startsWith("mistral.mistral-7b-instruct-v0"))
2573
+ return false;
2574
+ return true;
2575
+ },
2576
+ transformModel: (model, modelId, m) => {
2577
+ const crossRegionId = bedrockCrossRegionId(modelId);
2578
+ const bedrockModel: Model<Api> = {
2579
+ ...model,
2580
+ id: crossRegionId,
2581
+ name: toModelName(m.name, crossRegionId),
2582
+ };
2583
+ // Also emit EU variants for Claude models
2584
+ if (modelId.startsWith("anthropic.claude-")) {
2585
+ return [
2586
+ bedrockModel,
2587
+ {
2588
+ ...bedrockModel,
2589
+ id: `eu.${modelId}`,
2590
+ name: `${toModelName(m.name, modelId)} (EU)`,
2591
+ },
2592
+ ];
2593
+ }
2594
+ return bedrockModel;
2595
+ },
2596
+ },
2597
+ ];
2598
+
2599
+ const MODELS_DEV_PROVIDER_DESCRIPTORS_CORE: readonly ModelsDevProviderDescriptor[] = [
2600
+ // --- Anthropic ---
2601
+ anthropicMessagesDescriptor("anthropic", "anthropic", "https://api.anthropic.com", {
2602
+ filterModel: (id, m) => {
2603
+ if (m.tool_call !== true) return false;
2604
+ if (
2605
+ id.startsWith("claude-3-5-haiku") ||
2606
+ id.startsWith("claude-3-7-sonnet") ||
2607
+ id === "claude-3-opus-20240229" ||
2608
+ id === "claude-3-sonnet-20240229"
2609
+ )
2610
+ return false;
2611
+ return true;
2612
+ },
2613
+ }),
2614
+ // --- Google ---
2615
+ simpleModelsDevDescriptor(
2616
+ "google",
2617
+ "google",
2618
+ "google-generative-ai",
2619
+ "https://generativelanguage.googleapis.com/v1beta",
2620
+ ),
2621
+ // --- OpenAI ---
2622
+ simpleModelsDevDescriptor("openai", "openai", "openai-responses", "https://api.openai.com/v1"),
2623
+ // --- Groq ---
2624
+ openAiCompletionsDescriptor("groq", "groq", "https://api.groq.com/openai/v1"),
2625
+ // --- Cerebras ---
2626
+ openAiCompletionsDescriptor("cerebras", "cerebras", "https://api.cerebras.ai/v1"),
2627
+ // --- Together ---
2628
+ openAiCompletionsDescriptor("together", "together", "https://api.together.xyz/v1"),
2629
+ // --- NVIDIA ---
2630
+ openAiCompletionsDescriptor("nvidia", "nvidia", "https://integrate.api.nvidia.com/v1", {
2631
+ defaultContextWindow: 131072,
2632
+ }),
2633
+ // --- xAI ---
2634
+ openAiCompletionsDescriptor("xai", "xai", "https://api.x.ai/v1"),
2635
+ // --- DeepSeek ---
2636
+ openAiCompletionsDescriptor("deepseek", "deepseek", "https://api.deepseek.com", {
2637
+ // Only ship the v4 family as built-ins; older deepseek-chat / deepseek-reasoner
2638
+ // ids are kept off the catalog until the issue thread asks for them.
2639
+ filterModel: (id, m) => m.tool_call === true && id.startsWith("deepseek-v4"),
2640
+ compat: {
2641
+ // DeepSeek V4 only accepts `high`/`max`; map lower AERY levels upward so
2642
+ // subagent "minimal" turns stay in documented thinking mode instead of
2643
+ // sending unsupported effort strings.
2644
+ supportsDeveloperRole: false,
2645
+ supportsReasoningEffort: true,
2646
+ reasoningEffortMap: { minimal: "high", low: "high", medium: "high", high: "high", xhigh: "max" },
2647
+ maxTokensField: "max_tokens",
2648
+ // DeepSeek V4 thinking mode rejects the `tool_choice` control parameter.
2649
+ // Tool calls still work without it; the API defaults to auto when tools exist.
2650
+ supportsToolChoice: false,
2651
+ // DeepSeek V4's OpenAI format docs enable thinking with both the toggle and
2652
+ // reasoning_effort. Keep the toggle explicit for built-in models.
2653
+ extraBody: { thinking: { type: "enabled" } },
2654
+ // DeepSeek emits chain-of-thought via `reasoning_content` and requires it
2655
+ // to round-trip on assistant tool-call messages so the model can resume
2656
+ // from prior thinking (interleaved.field=reasoning_content on models.dev,
2657
+ // matches the kimi/openrouter handling already in detectCompat).
2658
+ reasoningContentField: "reasoning_content",
2659
+ requiresReasoningContentForToolCalls: true,
2660
+ requiresAssistantContentForToolCalls: true,
2661
+ },
2662
+ }),
2663
+ ];
2664
+
2665
+ const MODELS_DEV_PROVIDER_DESCRIPTORS_CODING_PLANS: readonly ModelsDevProviderDescriptor[] = [
2666
+ // --- zAI ---
2667
+ anthropicMessagesDescriptor("zai-coding-plan", "zai", "https://api.z.ai/api/anthropic"),
2668
+ // --- Xiaomi ---
2669
+ openAiCompletionsDescriptor("xiaomi", "xiaomi", "https://api.xiaomimimo.com/v1", {
2670
+ defaultContextWindow: 262144,
2671
+ defaultMaxTokens: 8192,
2672
+ compat: {
2673
+ supportsStore: false,
2674
+ thinkingFormat: "zai",
2675
+ reasoningContentField: "reasoning_content",
2676
+ requiresReasoningContentForToolCalls: true,
2677
+ allowsSyntheticReasoningContentForToolCalls: false,
2678
+ },
2679
+ }),
2680
+ // --- MiniMax Coding Plan ---
2681
+ openAiCompletionsDescriptor("minimax-coding-plan", "minimax-code", "https://api.minimax.io/v1", {
2682
+ compat: {
2683
+ supportsStore: false,
2684
+ supportsDeveloperRole: false,
2685
+ supportsReasoningEffort: false,
2686
+ reasoningContentField: "reasoning_content",
2687
+ },
2688
+ }),
2689
+ openAiCompletionsDescriptor("minimax-cn-coding-plan", "minimax-code-cn", "https://api.minimaxi.com/v1", {
2690
+ compat: {
2691
+ supportsStore: false,
2692
+ supportsDeveloperRole: false,
2693
+ supportsReasoningEffort: false,
2694
+ reasoningContentField: "reasoning_content",
2695
+ },
2696
+ }),
2697
+ // --- Alibaba Coding Plan ---
2698
+ openAiCompletionsDescriptor(
2699
+ "alibaba-coding-plan",
2700
+ "alibaba-coding-plan",
2701
+ "https://coding-intl.dashscope.aliyuncs.com/v1",
2702
+ {
2703
+ compat: {
2704
+ supportsDeveloperRole: false,
2705
+ },
2706
+ },
2707
+ ),
2708
+ // --- Zhipu Coding Plan ---
2709
+ openAiCompletionsDescriptor(
2710
+ "zhipu-coding-plan",
2711
+ "zhipu-coding-plan",
2712
+ "https://open.bigmodel.cn/api/coding/paas/v4",
2713
+ {
2714
+ compat: {
2715
+ thinkingFormat: "zai",
2716
+ reasoningContentField: "reasoning_content",
2717
+ supportsDeveloperRole: false,
2718
+ },
2719
+ },
2720
+ ),
2721
+ ];
2722
+
2723
+ const filterActiveToolCallModels = (_id: string, m: ModelsDevModel): boolean => {
2724
+ if (m.tool_call !== true) return false;
2725
+ if (m.status === "deprecated") return false;
2726
+ return true;
2727
+ };
2728
+
2729
+ const MODELS_DEV_PROVIDER_DESCRIPTORS_GOOGLE_VERTEX: readonly ModelsDevProviderDescriptor[] = [
2730
+ simpleModelsDevDescriptor("google-vertex", "google-vertex", "google-vertex", GOOGLE_VERTEX_BASE_URL, {
2731
+ filterModel: filterActiveToolCallModels,
2732
+ resolveApi: resolveGoogleVertexApi,
2733
+ }),
2734
+ ];
2735
+
2736
+ const MODELS_DEV_PROVIDER_DESCRIPTORS_SPECIALIZED: readonly ModelsDevProviderDescriptor[] = [
2737
+ // --- Cloudflare AI Gateway ---
2738
+ anthropicMessagesDescriptor(
2739
+ "cloudflare-ai-gateway",
2740
+ "cloudflare-ai-gateway",
2741
+ "https://gateway.ai.cloudflare.com/v1/<account>/<gateway>/anthropic",
2742
+ ),
2743
+ // --- Mistral ---
2744
+ openAiCompletionsDescriptor("mistral", "mistral", "https://api.mistral.ai/v1"),
2745
+ // --- OpenCode Zen ---
2746
+ openAiCompletionsDescriptor("opencode", "opencode-zen", "https://opencode.ai/zen/v1", {
2747
+ filterModel: filterActiveToolCallModels,
2748
+ resolveApi: (modelId, raw) =>
2749
+ resolveApiByRules(
2750
+ modelId,
2751
+ raw,
2752
+ OPENCODE_ZEN_API_RESOLUTION.rules,
2753
+ OPENCODE_ZEN_API_RESOLUTION.defaultResolution,
2754
+ ),
2755
+ }),
2756
+ // --- OpenCode Go ---
2757
+ openAiCompletionsDescriptor("opencode-go", "opencode-go", "https://opencode.ai/zen/go/v1", {
2758
+ filterModel: filterActiveToolCallModels,
2759
+ resolveApi: (modelId, raw) =>
2760
+ resolveApiByRules(
2761
+ modelId,
2762
+ raw,
2763
+ OPENCODE_GO_API_RESOLUTION.rules,
2764
+ OPENCODE_GO_API_RESOLUTION.defaultResolution,
2765
+ ),
2766
+ }),
2767
+ // --- GitHub Copilot ---
2768
+ openAiCompletionsDescriptor("github-copilot", "github-copilot", COPILOT_BASE_URL, {
2769
+ defaultContextWindow: 128000,
2770
+ defaultMaxTokens: 8192,
2771
+ headers: { ...OPENCODE_HEADERS },
2772
+ filterModel: filterActiveToolCallModels,
2773
+ resolveApi: (modelId, raw) =>
2774
+ resolveApiByRules(modelId, raw, COPILOT_API_RESOLUTION_RULES, COPILOT_DEFAULT_RESOLUTION),
2775
+ transformModel: model => {
2776
+ // compat only applies to openai-completions models
2777
+ if (model.api === "openai-completions") {
2778
+ return {
2779
+ ...model,
2780
+ compat: {
2781
+ supportsStore: false,
2782
+ supportsDeveloperRole: false,
2783
+ supportsReasoningEffort: false,
2784
+ },
2785
+ };
2786
+ }
2787
+ return model;
2788
+ },
2789
+ }),
2790
+ // --- MiniMax (Anthropic) ---
2791
+ anthropicMessagesDescriptor("minimax", "minimax", "https://api.minimax.io/anthropic"),
2792
+ anthropicMessagesDescriptor("minimax-cn", "minimax-cn", "https://api.minimaxi.com/anthropic"),
2793
+ // --- Qwen Portal ---
2794
+ openAiCompletionsDescriptor("qwen-portal", "qwen-portal", "https://portal.qwen.ai/v1", {
2795
+ defaultContextWindow: 128000,
2796
+ defaultMaxTokens: 8192,
2797
+ }),
2798
+
2799
+ // --- ZenMux ---
2800
+ openAiCompletionsDescriptor("zenmux", "zenmux", ZENMUX_OPENAI_BASE_URL, {
2801
+ filterModel: filterActiveToolCallModels,
2802
+ resolveApi: modelId => {
2803
+ if (modelId.startsWith("anthropic/")) {
2804
+ return { api: "anthropic-messages" as const, baseUrl: ZENMUX_ANTHROPIC_BASE_URL };
2805
+ }
2806
+ return { api: "openai-completions" as const, baseUrl: ZENMUX_OPENAI_BASE_URL };
2807
+ },
2808
+ }),
2809
+ ];
2810
+ /** All provider descriptors for models.dev data mapping in generate-models.ts. */
2811
+ export const MODELS_DEV_PROVIDER_DESCRIPTORS: readonly ModelsDevProviderDescriptor[] = [
2812
+ ...MODELS_DEV_PROVIDER_DESCRIPTORS_BEDROCK,
2813
+ ...MODELS_DEV_PROVIDER_DESCRIPTORS_GOOGLE_VERTEX,
2814
+ ...MODELS_DEV_PROVIDER_DESCRIPTORS_CORE,
2815
+ ...MODELS_DEV_PROVIDER_DESCRIPTORS_CODING_PLANS,
2816
+ ...MODELS_DEV_PROVIDER_DESCRIPTORS_SPECIALIZED,
2817
+ ];