@mastra/core 0.20.2 → 0.21.0-alpha.1

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 (314) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/README.md +30 -121
  3. package/dist/agent/agent.d.ts +5 -10
  4. package/dist/agent/agent.d.ts.map +1 -1
  5. package/dist/agent/agent.types.d.ts +12 -14
  6. package/dist/agent/agent.types.d.ts.map +1 -1
  7. package/dist/agent/index.cjs +19 -11
  8. package/dist/agent/index.d.ts +2 -1
  9. package/dist/agent/index.d.ts.map +1 -1
  10. package/dist/agent/index.js +2 -2
  11. package/dist/agent/input-processor/index.cjs +6 -6
  12. package/dist/agent/input-processor/index.js +1 -1
  13. package/dist/agent/message-list/index.d.ts +4 -4
  14. package/dist/agent/message-list/index.d.ts.map +1 -1
  15. package/dist/agent/message-list/prompt/attachments-to-parts.d.ts +1 -1
  16. package/dist/agent/message-list/prompt/attachments-to-parts.d.ts.map +1 -1
  17. package/dist/agent/message-list/prompt/convert-file.d.ts +2 -2
  18. package/dist/agent/message-list/prompt/convert-file.d.ts.map +1 -1
  19. package/dist/agent/message-list/types.d.ts +2 -2
  20. package/dist/agent/message-list/types.d.ts.map +1 -1
  21. package/dist/agent/message-list/utils/convert-messages.d.ts +2 -2
  22. package/dist/agent/types.d.ts +13 -5
  23. package/dist/agent/types.d.ts.map +1 -1
  24. package/dist/agent/utils.d.ts +110 -0
  25. package/dist/agent/utils.d.ts.map +1 -0
  26. package/dist/agent/workflows/prepare-stream/index.d.ts +2 -2
  27. package/dist/agent/workflows/prepare-stream/index.d.ts.map +1 -1
  28. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts.map +1 -1
  29. package/dist/agent/workflows/prepare-stream/prepare-memory-step.d.ts +2 -2
  30. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts +1 -2
  31. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts.map +1 -1
  32. package/dist/agent/workflows/prepare-stream/schema.d.ts +2 -2
  33. package/dist/ai-tracing/index.cjs +32 -32
  34. package/dist/ai-tracing/index.js +1 -1
  35. package/dist/ai-tracing/spans/base.d.ts +2 -0
  36. package/dist/ai-tracing/spans/base.d.ts.map +1 -1
  37. package/dist/ai-tracing/types.d.ts +24 -0
  38. package/dist/ai-tracing/types.d.ts.map +1 -1
  39. package/dist/{chunk-BWC4WQTS.js → chunk-24L3LVID.js} +21 -14
  40. package/dist/chunk-24L3LVID.js.map +1 -0
  41. package/dist/{chunk-7ZXDZXGD.cjs → chunk-2BTTHMDJ.cjs} +14 -2
  42. package/dist/chunk-2BTTHMDJ.cjs.map +1 -0
  43. package/dist/{chunk-KK5LUATU.js → chunk-2VSJYKJF.js} +4 -4
  44. package/dist/{chunk-KK5LUATU.js.map → chunk-2VSJYKJF.js.map} +1 -1
  45. package/dist/chunk-33JJWAVI.js +2077 -0
  46. package/dist/chunk-33JJWAVI.js.map +1 -0
  47. package/dist/chunk-3ZEJSGTJ.cjs +9289 -0
  48. package/dist/chunk-3ZEJSGTJ.cjs.map +1 -0
  49. package/dist/{chunk-6ZSVXXMJ.js → chunk-624FF2ZG.js} +14 -2
  50. package/dist/chunk-624FF2ZG.js.map +1 -0
  51. package/dist/{chunk-2QV245Q5.cjs → chunk-6OJROHMC.cjs} +25 -18
  52. package/dist/chunk-6OJROHMC.cjs.map +1 -0
  53. package/dist/{chunk-T5SM2HLV.cjs → chunk-74P72XBM.cjs} +4 -4
  54. package/dist/chunk-74P72XBM.cjs.map +1 -0
  55. package/dist/chunk-75CDUY5G.js +158 -0
  56. package/dist/chunk-75CDUY5G.js.map +1 -0
  57. package/dist/chunk-BLPOOPRL.js +3164 -0
  58. package/dist/chunk-BLPOOPRL.js.map +1 -0
  59. package/dist/{chunk-JZ5MQXUL.cjs → chunk-DUMAWQ7I.cjs} +4 -4
  60. package/dist/{chunk-JZ5MQXUL.cjs.map → chunk-DUMAWQ7I.cjs.map} +1 -1
  61. package/dist/chunk-EQV7XSTY.cjs +2090 -0
  62. package/dist/chunk-EQV7XSTY.cjs.map +1 -0
  63. package/dist/chunk-ILZI4MEU.js +9263 -0
  64. package/dist/chunk-ILZI4MEU.js.map +1 -0
  65. package/dist/{chunk-4PRV2Y55.cjs → chunk-LOUJSR3F.cjs} +5 -5
  66. package/dist/{chunk-4PRV2Y55.cjs.map → chunk-LOUJSR3F.cjs.map} +1 -1
  67. package/dist/{chunk-M2MHQQGJ.js → chunk-ONHTH5RH.js} +4 -4
  68. package/dist/chunk-ONHTH5RH.js.map +1 -0
  69. package/dist/{chunk-YBC4V5GE.js → chunk-PPW3SAWO.js} +5 -9
  70. package/dist/{chunk-YBC4V5GE.js.map → chunk-PPW3SAWO.js.map} +1 -1
  71. package/dist/{chunk-DBYLVMIV.js → chunk-Q4YVANA4.js} +25 -7
  72. package/dist/chunk-Q4YVANA4.js.map +1 -0
  73. package/dist/chunk-QMKBXESK.cjs +3186 -0
  74. package/dist/chunk-QMKBXESK.cjs.map +1 -0
  75. package/dist/{chunk-EB2KTBHB.js → chunk-R3W5CNKP.js} +100 -82
  76. package/dist/chunk-R3W5CNKP.js.map +1 -0
  77. package/dist/{chunk-ECMIW6W2.cjs → chunk-RXGL66T6.cjs} +134 -111
  78. package/dist/chunk-RXGL66T6.cjs.map +1 -0
  79. package/dist/{chunk-PPCSJI73.cjs → chunk-SL534RIL.cjs} +33 -14
  80. package/dist/chunk-SL534RIL.cjs.map +1 -0
  81. package/dist/{chunk-YICS4NNU.cjs → chunk-SQXKJWFX.cjs} +12 -12
  82. package/dist/{chunk-YICS4NNU.cjs.map → chunk-SQXKJWFX.cjs.map} +1 -1
  83. package/dist/{chunk-X5SB7NR3.cjs → chunk-SSDFGDFJ.cjs} +9 -10
  84. package/dist/chunk-SSDFGDFJ.cjs.map +1 -0
  85. package/dist/{chunk-OC7MSESV.cjs → chunk-TTJM3STK.cjs} +7 -11
  86. package/dist/{chunk-OC7MSESV.cjs.map → chunk-TTJM3STK.cjs.map} +1 -1
  87. package/dist/{chunk-RE7SRMBE.cjs → chunk-UPRLKS2E.cjs} +16 -16
  88. package/dist/chunk-UPRLKS2E.cjs.map +1 -0
  89. package/dist/{chunk-7H72OAZ3.js → chunk-UQCUAYWV.js} +3 -3
  90. package/dist/{chunk-7H72OAZ3.js.map → chunk-UQCUAYWV.js.map} +1 -1
  91. package/dist/{chunk-SWNIMD7I.js → chunk-XL5MZTLP.js} +3 -3
  92. package/dist/chunk-XL5MZTLP.js.map +1 -0
  93. package/dist/{chunk-HTX7EHW5.js → chunk-XQ5A3ISB.js} +3 -3
  94. package/dist/{chunk-HTX7EHW5.js.map → chunk-XQ5A3ISB.js.map} +1 -1
  95. package/dist/chunk-Y2PKC5BX.cjs +160 -0
  96. package/dist/chunk-Y2PKC5BX.cjs.map +1 -0
  97. package/dist/{chunk-VSTMNPZJ.js → chunk-YUV3LZMY.js} +7 -8
  98. package/dist/chunk-YUV3LZMY.js.map +1 -0
  99. package/dist/index.cjs +53 -49
  100. package/dist/index.js +10 -10
  101. package/dist/llm/index.cjs +9 -5
  102. package/dist/llm/index.d.ts +4 -3
  103. package/dist/llm/index.d.ts.map +1 -1
  104. package/dist/llm/index.js +1 -1
  105. package/dist/llm/model/base.types.d.ts +2 -2
  106. package/dist/llm/model/base.types.d.ts.map +1 -1
  107. package/dist/llm/model/gateways/base.d.ts +1 -1
  108. package/dist/llm/model/gateways/base.d.ts.map +1 -1
  109. package/dist/llm/model/gateways/models-dev.d.ts +1 -1
  110. package/dist/llm/model/gateways/models-dev.d.ts.map +1 -1
  111. package/dist/llm/model/gateways/netlify.d.ts +1 -1
  112. package/dist/llm/model/gateways/netlify.d.ts.map +1 -1
  113. package/dist/llm/model/index.d.ts +2 -1
  114. package/dist/llm/model/index.d.ts.map +1 -1
  115. package/dist/llm/model/model.d.ts +2 -2
  116. package/dist/llm/model/model.d.ts.map +1 -1
  117. package/dist/llm/model/model.loop.d.ts +3 -3
  118. package/dist/llm/model/model.loop.d.ts.map +1 -1
  119. package/dist/llm/model/model.loop.types.d.ts +2 -3
  120. package/dist/llm/model/model.loop.types.d.ts.map +1 -1
  121. package/dist/llm/model/provider-registry.d.ts +98 -0
  122. package/dist/llm/model/provider-registry.d.ts.map +1 -0
  123. package/dist/llm/model/provider-types.generated.d.ts +679 -0
  124. package/dist/llm/model/registry-generator.d.ts +29 -0
  125. package/dist/llm/model/registry-generator.d.ts.map +1 -0
  126. package/dist/llm/model/resolve-model.d.ts +46 -0
  127. package/dist/llm/model/resolve-model.d.ts.map +1 -0
  128. package/dist/llm/model/router.d.ts +2 -2
  129. package/dist/llm/model/router.d.ts.map +1 -1
  130. package/dist/llm/model/shared.types.d.ts +10 -4
  131. package/dist/llm/model/shared.types.d.ts.map +1 -1
  132. package/dist/loop/index.cjs +2 -2
  133. package/dist/loop/index.js +1 -1
  134. package/dist/loop/loop.d.ts +1 -1
  135. package/dist/loop/loop.d.ts.map +1 -1
  136. package/dist/loop/network/index.d.ts.map +1 -1
  137. package/dist/loop/telemetry/index.d.ts +1 -1
  138. package/dist/loop/telemetry/index.d.ts.map +1 -1
  139. package/dist/loop/test-utils/options.d.ts.map +1 -1
  140. package/dist/loop/test-utils/streamObject.d.ts +1 -1
  141. package/dist/loop/test-utils/streamObject.d.ts.map +1 -1
  142. package/dist/loop/test-utils/utils.d.ts +2 -2
  143. package/dist/loop/test-utils/utils.d.ts.map +1 -1
  144. package/dist/loop/types.d.ts +4 -3
  145. package/dist/loop/types.d.ts.map +1 -1
  146. package/dist/loop/workflows/agentic-execution/index.d.ts +73 -73
  147. package/dist/loop/workflows/agentic-execution/index.d.ts.map +1 -1
  148. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts +50 -50
  149. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts.map +1 -1
  150. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts +25 -25
  151. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts.map +1 -1
  152. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts +1 -1
  153. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts.map +1 -1
  154. package/dist/loop/workflows/agentic-loop/index.d.ts +73 -73
  155. package/dist/loop/workflows/agentic-loop/index.d.ts.map +1 -1
  156. package/dist/loop/workflows/run-state.d.ts +1 -1
  157. package/dist/loop/workflows/run-state.d.ts.map +1 -1
  158. package/dist/loop/workflows/schema.d.ts +33 -33
  159. package/dist/loop/workflows/schema.d.ts.map +1 -1
  160. package/dist/loop/workflows/stream.d.ts +1 -1
  161. package/dist/loop/workflows/stream.d.ts.map +1 -1
  162. package/dist/mastra/hooks.d.ts.map +1 -1
  163. package/dist/mastra/index.cjs +2 -2
  164. package/dist/mastra/index.js +1 -1
  165. package/dist/memory/index.cjs +4 -4
  166. package/dist/memory/index.js +1 -1
  167. package/dist/memory/memory.d.ts +3 -3
  168. package/dist/memory/memory.d.ts.map +1 -1
  169. package/dist/memory/types.d.ts +3 -3
  170. package/dist/memory/types.d.ts.map +1 -1
  171. package/dist/models-dev-A334FF64.js +3 -0
  172. package/dist/models-dev-A334FF64.js.map +1 -0
  173. package/dist/models-dev-ECOIQLLK.cjs +12 -0
  174. package/dist/models-dev-ECOIQLLK.cjs.map +1 -0
  175. package/dist/netlify-CEYZ5O54.js +3 -0
  176. package/dist/netlify-CEYZ5O54.js.map +1 -0
  177. package/dist/netlify-WFAL2AA4.cjs +12 -0
  178. package/dist/netlify-WFAL2AA4.cjs.map +1 -0
  179. package/dist/processors/index.cjs +11 -11
  180. package/dist/processors/index.js +1 -1
  181. package/dist/processors/processors/language-detector.d.ts +2 -0
  182. package/dist/processors/processors/language-detector.d.ts.map +1 -1
  183. package/dist/processors/processors/moderation.d.ts +6 -3
  184. package/dist/processors/processors/moderation.d.ts.map +1 -1
  185. package/dist/processors/processors/pii-detector.d.ts +8 -4
  186. package/dist/processors/processors/pii-detector.d.ts.map +1 -1
  187. package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -1
  188. package/dist/processors/processors/structured-output.d.ts +4 -2
  189. package/dist/processors/processors/structured-output.d.ts.map +1 -1
  190. package/dist/processors/processors/system-prompt-scrubber.d.ts +2 -1
  191. package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -1
  192. package/dist/processors/runner.d.ts +8 -2
  193. package/dist/processors/runner.d.ts.map +1 -1
  194. package/dist/provider-registry.json +1625 -0
  195. package/dist/registry-generator-7XDUEFUT.cjs +111 -0
  196. package/dist/registry-generator-7XDUEFUT.cjs.map +1 -0
  197. package/dist/registry-generator-ZKIT6I3E.js +103 -0
  198. package/dist/registry-generator-ZKIT6I3E.js.map +1 -0
  199. package/dist/relevance/index.cjs +4 -4
  200. package/dist/relevance/index.js +1 -1
  201. package/dist/relevance/mastra-agent/index.d.ts +2 -2
  202. package/dist/relevance/mastra-agent/index.d.ts.map +1 -1
  203. package/dist/scores/base.d.ts +6 -6
  204. package/dist/scores/base.d.ts.map +1 -1
  205. package/dist/scores/index.cjs +9 -9
  206. package/dist/scores/index.js +2 -2
  207. package/dist/scores/run-experiment/index.d.ts +1 -1
  208. package/dist/scores/run-experiment/index.d.ts.map +1 -1
  209. package/dist/scores/scoreTraces/index.cjs +10 -10
  210. package/dist/scores/scoreTraces/index.cjs.map +1 -1
  211. package/dist/scores/scoreTraces/index.js +5 -5
  212. package/dist/scores/scoreTraces/index.js.map +1 -1
  213. package/dist/scores/types.d.ts +1 -1
  214. package/dist/scores/types.d.ts.map +1 -1
  215. package/dist/storage/index.cjs +3 -3
  216. package/dist/storage/index.js +1 -1
  217. package/dist/stream/aisdk/v4/input.d.ts +1 -1
  218. package/dist/stream/aisdk/v4/input.d.ts.map +1 -1
  219. package/dist/stream/aisdk/v5/compat/content.d.ts +2 -2
  220. package/dist/stream/aisdk/v5/compat/content.d.ts.map +1 -1
  221. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts +2 -2
  222. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts.map +1 -1
  223. package/dist/stream/aisdk/v5/compat/ui-message.d.ts +1 -1
  224. package/dist/stream/aisdk/v5/compat/ui-message.d.ts.map +1 -1
  225. package/dist/stream/aisdk/v5/compat/validation.d.ts +1 -1
  226. package/dist/stream/aisdk/v5/compat/validation.d.ts.map +1 -1
  227. package/dist/stream/aisdk/v5/execute.d.ts +5 -4
  228. package/dist/stream/aisdk/v5/execute.d.ts.map +1 -1
  229. package/dist/stream/aisdk/v5/input.d.ts +1 -1
  230. package/dist/stream/aisdk/v5/input.d.ts.map +1 -1
  231. package/dist/stream/aisdk/v5/output-helpers.d.ts +9 -9
  232. package/dist/stream/aisdk/v5/output-helpers.d.ts.map +1 -1
  233. package/dist/stream/aisdk/v5/output.d.ts +40 -39
  234. package/dist/stream/aisdk/v5/output.d.ts.map +1 -1
  235. package/dist/stream/aisdk/v5/test-utils.d.ts +1 -1
  236. package/dist/stream/aisdk/v5/test-utils.d.ts.map +1 -1
  237. package/dist/stream/aisdk/v5/transform.d.ts +2 -2
  238. package/dist/stream/aisdk/v5/transform.d.ts.map +1 -1
  239. package/dist/stream/base/input.d.ts +1 -1
  240. package/dist/stream/base/input.d.ts.map +1 -1
  241. package/dist/stream/base/output-format-handlers.d.ts +5 -2
  242. package/dist/stream/base/output-format-handlers.d.ts.map +1 -1
  243. package/dist/stream/base/output.d.ts +31 -25
  244. package/dist/stream/base/output.d.ts.map +1 -1
  245. package/dist/stream/base/schema.d.ts +1 -1
  246. package/dist/stream/base/schema.d.ts.map +1 -1
  247. package/dist/stream/index.cjs +13 -9
  248. package/dist/stream/index.d.ts +1 -0
  249. package/dist/stream/index.d.ts.map +1 -1
  250. package/dist/stream/index.js +2 -2
  251. package/dist/stream/types.d.ts +19 -6
  252. package/dist/stream/types.d.ts.map +1 -1
  253. package/dist/test-utils/llm-mock.cjs +6 -6
  254. package/dist/test-utils/llm-mock.cjs.map +1 -1
  255. package/dist/test-utils/llm-mock.d.ts +1 -1
  256. package/dist/test-utils/llm-mock.d.ts.map +1 -1
  257. package/dist/test-utils/llm-mock.js +3 -3
  258. package/dist/test-utils/llm-mock.js.map +1 -1
  259. package/dist/tools/types.d.ts +2 -2
  260. package/dist/tools/types.d.ts.map +1 -1
  261. package/dist/utils.cjs +17 -17
  262. package/dist/utils.d.ts +3 -2
  263. package/dist/utils.d.ts.map +1 -1
  264. package/dist/utils.js +1 -1
  265. package/dist/vector/embed.d.ts +2 -2
  266. package/dist/vector/embed.d.ts.map +1 -1
  267. package/dist/vector/index.cjs +4 -4
  268. package/dist/vector/index.js +1 -1
  269. package/dist/vector/vector.d.ts +2 -2
  270. package/dist/vector/vector.d.ts.map +1 -1
  271. package/dist/workflows/default.d.ts +2 -2
  272. package/dist/workflows/default.d.ts.map +1 -1
  273. package/dist/workflows/evented/execution-engine.d.ts +1 -1
  274. package/dist/workflows/evented/execution-engine.d.ts.map +1 -1
  275. package/dist/workflows/evented/index.cjs +10 -10
  276. package/dist/workflows/evented/index.js +1 -1
  277. package/dist/workflows/execution-engine.d.ts +1 -1
  278. package/dist/workflows/execution-engine.d.ts.map +1 -1
  279. package/dist/workflows/index.cjs +12 -12
  280. package/dist/workflows/index.js +1 -1
  281. package/dist/workflows/legacy/index.cjs +22 -22
  282. package/dist/workflows/legacy/index.js +1 -1
  283. package/dist/workflows/legacy/machine.d.ts +1 -1
  284. package/dist/workflows/legacy/workflow.d.ts +1 -1
  285. package/dist/workflows/step.d.ts +1 -1
  286. package/dist/workflows/step.d.ts.map +1 -1
  287. package/dist/workflows/types.d.ts +1 -1
  288. package/dist/workflows/types.d.ts.map +1 -1
  289. package/dist/workflows/workflow.d.ts +25 -8
  290. package/dist/workflows/workflow.d.ts.map +1 -1
  291. package/package.json +24 -19
  292. package/src/llm/model/provider-types.generated.d.ts +679 -0
  293. package/dist/chunk-2QV245Q5.cjs.map +0 -1
  294. package/dist/chunk-6ZSVXXMJ.js.map +0 -1
  295. package/dist/chunk-7ZXDZXGD.cjs.map +0 -1
  296. package/dist/chunk-BWC4WQTS.js.map +0 -1
  297. package/dist/chunk-BZ6BIHLE.js +0 -1418
  298. package/dist/chunk-BZ6BIHLE.js.map +0 -1
  299. package/dist/chunk-DBYLVMIV.js.map +0 -1
  300. package/dist/chunk-EB2KTBHB.js.map +0 -1
  301. package/dist/chunk-ECMIW6W2.cjs.map +0 -1
  302. package/dist/chunk-M2MHQQGJ.js.map +0 -1
  303. package/dist/chunk-PPCSJI73.cjs.map +0 -1
  304. package/dist/chunk-RE7SRMBE.cjs.map +0 -1
  305. package/dist/chunk-REVAU76X.cjs +0 -1423
  306. package/dist/chunk-REVAU76X.cjs.map +0 -1
  307. package/dist/chunk-SWNIMD7I.js.map +0 -1
  308. package/dist/chunk-T5SM2HLV.cjs.map +0 -1
  309. package/dist/chunk-VSTMNPZJ.js.map +0 -1
  310. package/dist/chunk-X5SB7NR3.cjs.map +0 -1
  311. package/dist/llm/model/provider-registry.generated.d.ts +0 -99
  312. package/dist/llm/model/provider-registry.generated.d.ts.map +0 -1
  313. package/dist/stream/aisdk/v5/model-supports.d.ts +0 -25
  314. package/dist/stream/aisdk/v5/model-supports.d.ts.map +0 -1
@@ -0,0 +1,3164 @@
1
+ import { MastraModelGateway, createOpenAICompatible, createAnthropic, createGoogleGenerativeAI, createOpenAI, OpenAICompatibleImageModel } from './chunk-ILZI4MEU.js';
2
+ import { UnsupportedFunctionalityError, NoSuchModelError } from '@ai-sdk/provider';
3
+ import { createJsonErrorResponseHandler, withoutTrailingSlash, generateId, withUserAgentSuffix, parseProviderOptions, postJsonToApi, createJsonResponseHandler, combineHeaders, createEventSourceResponseHandler, loadApiKey, convertToBase64 } from '@ai-sdk/provider-utils';
4
+ import * as z3 from 'zod/v4';
5
+ import { z } from 'zod/v4';
6
+
7
+ function convertToXaiChatMessages(prompt) {
8
+ const messages = [];
9
+ const warnings = [];
10
+ for (const { role, content } of prompt) {
11
+ switch (role) {
12
+ case "system": {
13
+ messages.push({ role: "system", content });
14
+ break;
15
+ }
16
+ case "user": {
17
+ if (content.length === 1 && content[0].type === "text") {
18
+ messages.push({ role: "user", content: content[0].text });
19
+ break;
20
+ }
21
+ messages.push({
22
+ role: "user",
23
+ content: content.map((part) => {
24
+ switch (part.type) {
25
+ case "text": {
26
+ return { type: "text", text: part.text };
27
+ }
28
+ case "file": {
29
+ if (part.mediaType.startsWith("image/")) {
30
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
31
+ return {
32
+ type: "image_url",
33
+ image_url: {
34
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
35
+ }
36
+ };
37
+ } else {
38
+ throw new UnsupportedFunctionalityError({
39
+ functionality: `file part media type ${part.mediaType}`
40
+ });
41
+ }
42
+ }
43
+ }
44
+ })
45
+ });
46
+ break;
47
+ }
48
+ case "assistant": {
49
+ let text = "";
50
+ const toolCalls = [];
51
+ for (const part of content) {
52
+ switch (part.type) {
53
+ case "text": {
54
+ text += part.text;
55
+ break;
56
+ }
57
+ case "tool-call": {
58
+ toolCalls.push({
59
+ id: part.toolCallId,
60
+ type: "function",
61
+ function: {
62
+ name: part.toolName,
63
+ arguments: JSON.stringify(part.input)
64
+ }
65
+ });
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ messages.push({
71
+ role: "assistant",
72
+ content: text,
73
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
74
+ });
75
+ break;
76
+ }
77
+ case "tool": {
78
+ for (const toolResponse of content) {
79
+ const output = toolResponse.output;
80
+ let contentValue;
81
+ switch (output.type) {
82
+ case "text":
83
+ case "error-text":
84
+ contentValue = output.value;
85
+ break;
86
+ case "content":
87
+ case "json":
88
+ case "error-json":
89
+ contentValue = JSON.stringify(output.value);
90
+ break;
91
+ }
92
+ messages.push({
93
+ role: "tool",
94
+ tool_call_id: toolResponse.toolCallId,
95
+ content: contentValue
96
+ });
97
+ }
98
+ break;
99
+ }
100
+ default: {
101
+ const _exhaustiveCheck = role;
102
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
103
+ }
104
+ }
105
+ }
106
+ return { messages, warnings };
107
+ }
108
+ function getResponseMetadata({
109
+ id,
110
+ model,
111
+ created
112
+ }) {
113
+ return {
114
+ id: id != null ? id : void 0,
115
+ modelId: model != null ? model : void 0,
116
+ timestamp: created != null ? new Date(created * 1e3) : void 0
117
+ };
118
+ }
119
+ function mapXaiFinishReason(finishReason) {
120
+ switch (finishReason) {
121
+ case "stop":
122
+ return "stop";
123
+ case "length":
124
+ return "length";
125
+ case "tool_calls":
126
+ case "function_call":
127
+ return "tool-calls";
128
+ case "content_filter":
129
+ return "content-filter";
130
+ default:
131
+ return "unknown";
132
+ }
133
+ }
134
+ var webSourceSchema = z3.object({
135
+ type: z3.literal("web"),
136
+ country: z3.string().length(2).optional(),
137
+ excludedWebsites: z3.array(z3.string()).max(5).optional(),
138
+ allowedWebsites: z3.array(z3.string()).max(5).optional(),
139
+ safeSearch: z3.boolean().optional()
140
+ });
141
+ var xSourceSchema = z3.object({
142
+ type: z3.literal("x"),
143
+ excludedXHandles: z3.array(z3.string()).optional(),
144
+ includedXHandles: z3.array(z3.string()).optional(),
145
+ postFavoriteCount: z3.number().int().optional(),
146
+ postViewCount: z3.number().int().optional(),
147
+ /**
148
+ * @deprecated use `includedXHandles` instead
149
+ */
150
+ xHandles: z3.array(z3.string()).optional()
151
+ });
152
+ var newsSourceSchema = z3.object({
153
+ type: z3.literal("news"),
154
+ country: z3.string().length(2).optional(),
155
+ excludedWebsites: z3.array(z3.string()).max(5).optional(),
156
+ safeSearch: z3.boolean().optional()
157
+ });
158
+ var rssSourceSchema = z3.object({
159
+ type: z3.literal("rss"),
160
+ links: z3.array(z3.string().url()).max(1)
161
+ // currently only supports one RSS link
162
+ });
163
+ var searchSourceSchema = z3.discriminatedUnion("type", [
164
+ webSourceSchema,
165
+ xSourceSchema,
166
+ newsSourceSchema,
167
+ rssSourceSchema
168
+ ]);
169
+ var xaiProviderOptions = z3.object({
170
+ reasoningEffort: z3.enum(["low", "high"]).optional(),
171
+ searchParameters: z3.object({
172
+ /**
173
+ * search mode preference
174
+ * - "off": disables search completely
175
+ * - "auto": model decides whether to search (default)
176
+ * - "on": always enables search
177
+ */
178
+ mode: z3.enum(["off", "auto", "on"]),
179
+ /**
180
+ * whether to return citations in the response
181
+ * defaults to true
182
+ */
183
+ returnCitations: z3.boolean().optional(),
184
+ /**
185
+ * start date for search data (ISO8601 format: YYYY-MM-DD)
186
+ */
187
+ fromDate: z3.string().optional(),
188
+ /**
189
+ * end date for search data (ISO8601 format: YYYY-MM-DD)
190
+ */
191
+ toDate: z3.string().optional(),
192
+ /**
193
+ * maximum number of search results to consider
194
+ * defaults to 20
195
+ */
196
+ maxSearchResults: z3.number().min(1).max(50).optional(),
197
+ /**
198
+ * data sources to search from
199
+ * defaults to ["web", "x"] if not specified
200
+ */
201
+ sources: z3.array(searchSourceSchema).optional()
202
+ }).optional()
203
+ });
204
+ var xaiErrorDataSchema = z3.object({
205
+ error: z3.object({
206
+ message: z3.string(),
207
+ type: z3.string().nullish(),
208
+ param: z3.any().nullish(),
209
+ code: z3.union([z3.string(), z3.number()]).nullish()
210
+ })
211
+ });
212
+ var xaiFailedResponseHandler = createJsonErrorResponseHandler({
213
+ errorSchema: xaiErrorDataSchema,
214
+ errorToMessage: (data) => data.error.message
215
+ });
216
+ function prepareTools({
217
+ tools,
218
+ toolChoice
219
+ }) {
220
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
221
+ const toolWarnings = [];
222
+ if (tools == null) {
223
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
224
+ }
225
+ const xaiTools = [];
226
+ for (const tool of tools) {
227
+ if (tool.type === "provider-defined") {
228
+ toolWarnings.push({ type: "unsupported-tool", tool });
229
+ } else {
230
+ xaiTools.push({
231
+ type: "function",
232
+ function: {
233
+ name: tool.name,
234
+ description: tool.description,
235
+ parameters: tool.inputSchema
236
+ }
237
+ });
238
+ }
239
+ }
240
+ if (toolChoice == null) {
241
+ return { tools: xaiTools, toolChoice: void 0, toolWarnings };
242
+ }
243
+ const type = toolChoice.type;
244
+ switch (type) {
245
+ case "auto":
246
+ case "none":
247
+ return { tools: xaiTools, toolChoice: type, toolWarnings };
248
+ case "required":
249
+ return { tools: xaiTools, toolChoice: "required", toolWarnings };
250
+ case "tool":
251
+ return {
252
+ tools: xaiTools,
253
+ toolChoice: {
254
+ type: "function",
255
+ function: { name: toolChoice.toolName }
256
+ },
257
+ toolWarnings
258
+ };
259
+ default: {
260
+ const _exhaustiveCheck = type;
261
+ throw new UnsupportedFunctionalityError({
262
+ functionality: `tool choice type: ${_exhaustiveCheck}`
263
+ });
264
+ }
265
+ }
266
+ }
267
+ var XaiChatLanguageModel = class {
268
+ constructor(modelId, config) {
269
+ this.specificationVersion = "v2";
270
+ this.supportedUrls = {
271
+ "image/*": [/^https?:\/\/.*$/]
272
+ };
273
+ this.modelId = modelId;
274
+ this.config = config;
275
+ }
276
+ get provider() {
277
+ return this.config.provider;
278
+ }
279
+ async getArgs({
280
+ prompt,
281
+ maxOutputTokens,
282
+ temperature,
283
+ topP,
284
+ topK,
285
+ frequencyPenalty,
286
+ presencePenalty,
287
+ stopSequences,
288
+ seed,
289
+ responseFormat,
290
+ providerOptions,
291
+ tools,
292
+ toolChoice
293
+ }) {
294
+ var _a15, _b, _c;
295
+ const warnings = [];
296
+ const options = (_a15 = await parseProviderOptions({
297
+ provider: "xai",
298
+ providerOptions,
299
+ schema: xaiProviderOptions
300
+ })) != null ? _a15 : {};
301
+ if (topK != null) {
302
+ warnings.push({
303
+ type: "unsupported-setting",
304
+ setting: "topK"
305
+ });
306
+ }
307
+ if (frequencyPenalty != null) {
308
+ warnings.push({
309
+ type: "unsupported-setting",
310
+ setting: "frequencyPenalty"
311
+ });
312
+ }
313
+ if (presencePenalty != null) {
314
+ warnings.push({
315
+ type: "unsupported-setting",
316
+ setting: "presencePenalty"
317
+ });
318
+ }
319
+ if (stopSequences != null) {
320
+ warnings.push({
321
+ type: "unsupported-setting",
322
+ setting: "stopSequences"
323
+ });
324
+ }
325
+ if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) {
326
+ warnings.push({
327
+ type: "unsupported-setting",
328
+ setting: "responseFormat",
329
+ details: "JSON response format schema is not supported"
330
+ });
331
+ }
332
+ const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
333
+ warnings.push(...messageWarnings);
334
+ const {
335
+ tools: xaiTools,
336
+ toolChoice: xaiToolChoice,
337
+ toolWarnings
338
+ } = prepareTools({
339
+ tools,
340
+ toolChoice
341
+ });
342
+ warnings.push(...toolWarnings);
343
+ const baseArgs = {
344
+ // model id
345
+ model: this.modelId,
346
+ // standard generation settings
347
+ max_tokens: maxOutputTokens,
348
+ temperature,
349
+ top_p: topP,
350
+ seed,
351
+ reasoning_effort: options.reasoningEffort,
352
+ // response format
353
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
354
+ type: "json_schema",
355
+ json_schema: {
356
+ name: (_b = responseFormat.name) != null ? _b : "response",
357
+ schema: responseFormat.schema,
358
+ strict: true
359
+ }
360
+ } : { type: "json_object" } : void 0,
361
+ // search parameters
362
+ search_parameters: options.searchParameters ? {
363
+ mode: options.searchParameters.mode,
364
+ return_citations: options.searchParameters.returnCitations,
365
+ from_date: options.searchParameters.fromDate,
366
+ to_date: options.searchParameters.toDate,
367
+ max_search_results: options.searchParameters.maxSearchResults,
368
+ sources: (_c = options.searchParameters.sources) == null ? void 0 : _c.map((source) => {
369
+ var _a22;
370
+ return {
371
+ type: source.type,
372
+ ...source.type === "web" && {
373
+ country: source.country,
374
+ excluded_websites: source.excludedWebsites,
375
+ allowed_websites: source.allowedWebsites,
376
+ safe_search: source.safeSearch
377
+ },
378
+ ...source.type === "x" && {
379
+ excluded_x_handles: source.excludedXHandles,
380
+ included_x_handles: (_a22 = source.includedXHandles) != null ? _a22 : source.xHandles,
381
+ post_favorite_count: source.postFavoriteCount,
382
+ post_view_count: source.postViewCount
383
+ },
384
+ ...source.type === "news" && {
385
+ country: source.country,
386
+ excluded_websites: source.excludedWebsites,
387
+ safe_search: source.safeSearch
388
+ },
389
+ ...source.type === "rss" && {
390
+ links: source.links
391
+ }
392
+ };
393
+ })
394
+ } : void 0,
395
+ // messages in xai format
396
+ messages,
397
+ // tools in xai format
398
+ tools: xaiTools,
399
+ tool_choice: xaiToolChoice
400
+ };
401
+ return {
402
+ args: baseArgs,
403
+ warnings
404
+ };
405
+ }
406
+ async doGenerate(options) {
407
+ var _a15, _b, _c;
408
+ const { args: body, warnings } = await this.getArgs(options);
409
+ const {
410
+ responseHeaders,
411
+ value: response,
412
+ rawValue: rawResponse
413
+ } = await postJsonToApi({
414
+ url: `${(_a15 = this.config.baseURL) != null ? _a15 : "https://api.x.ai/v1"}/chat/completions`,
415
+ headers: combineHeaders(this.config.headers(), options.headers),
416
+ body,
417
+ failedResponseHandler: xaiFailedResponseHandler,
418
+ successfulResponseHandler: createJsonResponseHandler(
419
+ xaiChatResponseSchema
420
+ ),
421
+ abortSignal: options.abortSignal,
422
+ fetch: this.config.fetch
423
+ });
424
+ const choice = response.choices[0];
425
+ const content = [];
426
+ if (choice.message.content != null && choice.message.content.length > 0) {
427
+ let text = choice.message.content;
428
+ const lastMessage = body.messages[body.messages.length - 1];
429
+ if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && text === lastMessage.content) {
430
+ text = "";
431
+ }
432
+ if (text.length > 0) {
433
+ content.push({ type: "text", text });
434
+ }
435
+ }
436
+ if (choice.message.reasoning_content != null && choice.message.reasoning_content.length > 0) {
437
+ content.push({
438
+ type: "reasoning",
439
+ text: choice.message.reasoning_content
440
+ });
441
+ }
442
+ if (choice.message.tool_calls != null) {
443
+ for (const toolCall of choice.message.tool_calls) {
444
+ content.push({
445
+ type: "tool-call",
446
+ toolCallId: toolCall.id,
447
+ toolName: toolCall.function.name,
448
+ input: toolCall.function.arguments
449
+ });
450
+ }
451
+ }
452
+ if (response.citations != null) {
453
+ for (const url of response.citations) {
454
+ content.push({
455
+ type: "source",
456
+ sourceType: "url",
457
+ id: this.config.generateId(),
458
+ url
459
+ });
460
+ }
461
+ }
462
+ return {
463
+ content,
464
+ finishReason: mapXaiFinishReason(choice.finish_reason),
465
+ usage: {
466
+ inputTokens: response.usage.prompt_tokens,
467
+ outputTokens: response.usage.completion_tokens,
468
+ totalTokens: response.usage.total_tokens,
469
+ reasoningTokens: (_c = (_b = response.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0
470
+ },
471
+ request: { body },
472
+ response: {
473
+ ...getResponseMetadata(response),
474
+ headers: responseHeaders,
475
+ body: rawResponse
476
+ },
477
+ warnings
478
+ };
479
+ }
480
+ async doStream(options) {
481
+ var _a15;
482
+ const { args, warnings } = await this.getArgs(options);
483
+ const body = {
484
+ ...args,
485
+ stream: true,
486
+ stream_options: {
487
+ include_usage: true
488
+ }
489
+ };
490
+ const { responseHeaders, value: response } = await postJsonToApi({
491
+ url: `${(_a15 = this.config.baseURL) != null ? _a15 : "https://api.x.ai/v1"}/chat/completions`,
492
+ headers: combineHeaders(this.config.headers(), options.headers),
493
+ body,
494
+ failedResponseHandler: xaiFailedResponseHandler,
495
+ successfulResponseHandler: createEventSourceResponseHandler(xaiChatChunkSchema),
496
+ abortSignal: options.abortSignal,
497
+ fetch: this.config.fetch
498
+ });
499
+ let finishReason = "unknown";
500
+ const usage = {
501
+ inputTokens: void 0,
502
+ outputTokens: void 0,
503
+ totalTokens: void 0
504
+ };
505
+ let isFirstChunk = true;
506
+ const contentBlocks = {};
507
+ const lastReasoningDeltas = {};
508
+ const self = this;
509
+ return {
510
+ stream: response.pipeThrough(
511
+ new TransformStream({
512
+ start(controller) {
513
+ controller.enqueue({ type: "stream-start", warnings });
514
+ },
515
+ transform(chunk, controller) {
516
+ var _a22, _b;
517
+ if (options.includeRawChunks) {
518
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
519
+ }
520
+ if (!chunk.success) {
521
+ controller.enqueue({ type: "error", error: chunk.error });
522
+ return;
523
+ }
524
+ const value = chunk.value;
525
+ if (isFirstChunk) {
526
+ controller.enqueue({
527
+ type: "response-metadata",
528
+ ...getResponseMetadata(value)
529
+ });
530
+ isFirstChunk = false;
531
+ }
532
+ if (value.citations != null) {
533
+ for (const url of value.citations) {
534
+ controller.enqueue({
535
+ type: "source",
536
+ sourceType: "url",
537
+ id: self.config.generateId(),
538
+ url
539
+ });
540
+ }
541
+ }
542
+ if (value.usage != null) {
543
+ usage.inputTokens = value.usage.prompt_tokens;
544
+ usage.outputTokens = value.usage.completion_tokens;
545
+ usage.totalTokens = value.usage.total_tokens;
546
+ usage.reasoningTokens = (_b = (_a22 = value.usage.completion_tokens_details) == null ? void 0 : _a22.reasoning_tokens) != null ? _b : void 0;
547
+ }
548
+ const choice = value.choices[0];
549
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
550
+ finishReason = mapXaiFinishReason(choice.finish_reason);
551
+ }
552
+ if ((choice == null ? void 0 : choice.delta) == null) {
553
+ return;
554
+ }
555
+ const delta = choice.delta;
556
+ const choiceIndex = choice.index;
557
+ if (delta.content != null && delta.content.length > 0) {
558
+ const textContent = delta.content;
559
+ const lastMessage = body.messages[body.messages.length - 1];
560
+ if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
561
+ return;
562
+ }
563
+ const blockId = `text-${value.id || choiceIndex}`;
564
+ if (contentBlocks[blockId] == null) {
565
+ contentBlocks[blockId] = { type: "text" };
566
+ controller.enqueue({
567
+ type: "text-start",
568
+ id: blockId
569
+ });
570
+ }
571
+ controller.enqueue({
572
+ type: "text-delta",
573
+ id: blockId,
574
+ delta: textContent
575
+ });
576
+ }
577
+ if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
578
+ const blockId = `reasoning-${value.id || choiceIndex}`;
579
+ if (lastReasoningDeltas[blockId] === delta.reasoning_content) {
580
+ return;
581
+ }
582
+ lastReasoningDeltas[blockId] = delta.reasoning_content;
583
+ if (contentBlocks[blockId] == null) {
584
+ contentBlocks[blockId] = { type: "reasoning" };
585
+ controller.enqueue({
586
+ type: "reasoning-start",
587
+ id: blockId
588
+ });
589
+ }
590
+ controller.enqueue({
591
+ type: "reasoning-delta",
592
+ id: blockId,
593
+ delta: delta.reasoning_content
594
+ });
595
+ }
596
+ if (delta.tool_calls != null) {
597
+ for (const toolCall of delta.tool_calls) {
598
+ const toolCallId = toolCall.id;
599
+ controller.enqueue({
600
+ type: "tool-input-start",
601
+ id: toolCallId,
602
+ toolName: toolCall.function.name
603
+ });
604
+ controller.enqueue({
605
+ type: "tool-input-delta",
606
+ id: toolCallId,
607
+ delta: toolCall.function.arguments
608
+ });
609
+ controller.enqueue({
610
+ type: "tool-input-end",
611
+ id: toolCallId
612
+ });
613
+ controller.enqueue({
614
+ type: "tool-call",
615
+ toolCallId,
616
+ toolName: toolCall.function.name,
617
+ input: toolCall.function.arguments
618
+ });
619
+ }
620
+ }
621
+ },
622
+ flush(controller) {
623
+ for (const [blockId, block] of Object.entries(contentBlocks)) {
624
+ controller.enqueue({
625
+ type: block.type === "text" ? "text-end" : "reasoning-end",
626
+ id: blockId
627
+ });
628
+ }
629
+ controller.enqueue({ type: "finish", finishReason, usage });
630
+ }
631
+ })
632
+ ),
633
+ request: { body },
634
+ response: { headers: responseHeaders }
635
+ };
636
+ }
637
+ };
638
+ var xaiUsageSchema = z3.object({
639
+ prompt_tokens: z3.number(),
640
+ completion_tokens: z3.number(),
641
+ total_tokens: z3.number(),
642
+ completion_tokens_details: z3.object({
643
+ reasoning_tokens: z3.number().nullish()
644
+ }).nullish()
645
+ });
646
+ var xaiChatResponseSchema = z3.object({
647
+ id: z3.string().nullish(),
648
+ created: z3.number().nullish(),
649
+ model: z3.string().nullish(),
650
+ choices: z3.array(
651
+ z3.object({
652
+ message: z3.object({
653
+ role: z3.literal("assistant"),
654
+ content: z3.string().nullish(),
655
+ reasoning_content: z3.string().nullish(),
656
+ tool_calls: z3.array(
657
+ z3.object({
658
+ id: z3.string(),
659
+ type: z3.literal("function"),
660
+ function: z3.object({
661
+ name: z3.string(),
662
+ arguments: z3.string()
663
+ })
664
+ })
665
+ ).nullish()
666
+ }),
667
+ index: z3.number(),
668
+ finish_reason: z3.string().nullish()
669
+ })
670
+ ),
671
+ object: z3.literal("chat.completion"),
672
+ usage: xaiUsageSchema,
673
+ citations: z3.array(z3.string().url()).nullish()
674
+ });
675
+ var xaiChatChunkSchema = z3.object({
676
+ id: z3.string().nullish(),
677
+ created: z3.number().nullish(),
678
+ model: z3.string().nullish(),
679
+ choices: z3.array(
680
+ z3.object({
681
+ delta: z3.object({
682
+ role: z3.enum(["assistant"]).optional(),
683
+ content: z3.string().nullish(),
684
+ reasoning_content: z3.string().nullish(),
685
+ tool_calls: z3.array(
686
+ z3.object({
687
+ id: z3.string(),
688
+ type: z3.literal("function"),
689
+ function: z3.object({
690
+ name: z3.string(),
691
+ arguments: z3.string()
692
+ })
693
+ })
694
+ ).nullish()
695
+ }),
696
+ finish_reason: z3.string().nullish(),
697
+ index: z3.number()
698
+ })
699
+ ),
700
+ usage: xaiUsageSchema.nullish(),
701
+ citations: z3.array(z3.string().url()).nullish()
702
+ });
703
+ var VERSION = "2.0.24" ;
704
+ var xaiErrorStructure = {
705
+ errorSchema: xaiErrorDataSchema,
706
+ errorToMessage: (data) => data.error.message
707
+ };
708
+ function createXai(options = {}) {
709
+ var _a15;
710
+ const baseURL = withoutTrailingSlash(
711
+ (_a15 = options.baseURL) != null ? _a15 : "https://api.x.ai/v1"
712
+ );
713
+ const getHeaders = () => withUserAgentSuffix(
714
+ {
715
+ Authorization: `Bearer ${loadApiKey({
716
+ apiKey: options.apiKey,
717
+ environmentVariableName: "XAI_API_KEY",
718
+ description: "xAI API key"
719
+ })}`,
720
+ ...options.headers
721
+ },
722
+ `ai-sdk/xai/${VERSION}`
723
+ );
724
+ const createLanguageModel = (modelId) => {
725
+ return new XaiChatLanguageModel(modelId, {
726
+ provider: "xai.chat",
727
+ baseURL,
728
+ headers: getHeaders,
729
+ generateId,
730
+ fetch: options.fetch
731
+ });
732
+ };
733
+ const createImageModel = (modelId) => {
734
+ return new OpenAICompatibleImageModel(modelId, {
735
+ provider: "xai.image",
736
+ url: ({ path }) => `${baseURL}${path}`,
737
+ headers: getHeaders,
738
+ fetch: options.fetch,
739
+ errorStructure: xaiErrorStructure
740
+ });
741
+ };
742
+ const provider = (modelId) => createLanguageModel(modelId);
743
+ provider.languageModel = createLanguageModel;
744
+ provider.chat = createLanguageModel;
745
+ provider.textEmbeddingModel = (modelId) => {
746
+ throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
747
+ };
748
+ provider.imageModel = createImageModel;
749
+ provider.image = createImageModel;
750
+ return provider;
751
+ }
752
+ createXai();
753
+ var __defProp = Object.defineProperty;
754
+ var __defProps = Object.defineProperties;
755
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
756
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
757
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
758
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
759
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
760
+ var __spreadValues = (a, b) => {
761
+ for (var prop in b || (b = {}))
762
+ if (__hasOwnProp.call(b, prop))
763
+ __defNormalProp(a, prop, b[prop]);
764
+ if (__getOwnPropSymbols)
765
+ for (var prop of __getOwnPropSymbols(b)) {
766
+ if (__propIsEnum.call(b, prop))
767
+ __defNormalProp(a, prop, b[prop]);
768
+ }
769
+ return a;
770
+ };
771
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
772
+ var marker = "vercel.ai.error";
773
+ var symbol = Symbol.for(marker);
774
+ var _a;
775
+ var _AISDKError = class _AISDKError2 extends Error {
776
+ /**
777
+ * Creates an AI SDK Error.
778
+ *
779
+ * @param {Object} params - The parameters for creating the error.
780
+ * @param {string} params.name - The name of the error.
781
+ * @param {string} params.message - The error message.
782
+ * @param {unknown} [params.cause] - The underlying cause of the error.
783
+ */
784
+ constructor({
785
+ name: name14,
786
+ message,
787
+ cause
788
+ }) {
789
+ super(message);
790
+ this[_a] = true;
791
+ this.name = name14;
792
+ this.cause = cause;
793
+ }
794
+ /**
795
+ * Checks if the given error is an AI SDK Error.
796
+ * @param {unknown} error - The error to check.
797
+ * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
798
+ */
799
+ static isInstance(error) {
800
+ return _AISDKError2.hasMarker(error, marker);
801
+ }
802
+ static hasMarker(error, marker15) {
803
+ const markerSymbol = Symbol.for(marker15);
804
+ return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
805
+ }
806
+ };
807
+ _a = symbol;
808
+ var AISDKError = _AISDKError;
809
+ var name = "AI_APICallError";
810
+ var marker2 = `vercel.ai.error.${name}`;
811
+ var symbol2 = Symbol.for(marker2);
812
+ var _a2;
813
+ var APICallError = class extends AISDKError {
814
+ constructor({
815
+ message,
816
+ url,
817
+ requestBodyValues,
818
+ statusCode,
819
+ responseHeaders,
820
+ responseBody,
821
+ cause,
822
+ isRetryable = statusCode != null && (statusCode === 408 || // request timeout
823
+ statusCode === 409 || // conflict
824
+ statusCode === 429 || // too many requests
825
+ statusCode >= 500),
826
+ // server error
827
+ data
828
+ }) {
829
+ super({ name, message, cause });
830
+ this[_a2] = true;
831
+ this.url = url;
832
+ this.requestBodyValues = requestBodyValues;
833
+ this.statusCode = statusCode;
834
+ this.responseHeaders = responseHeaders;
835
+ this.responseBody = responseBody;
836
+ this.isRetryable = isRetryable;
837
+ this.data = data;
838
+ }
839
+ static isInstance(error) {
840
+ return AISDKError.hasMarker(error, marker2);
841
+ }
842
+ };
843
+ _a2 = symbol2;
844
+ var name2 = "AI_EmptyResponseBodyError";
845
+ var marker3 = `vercel.ai.error.${name2}`;
846
+ var symbol3 = Symbol.for(marker3);
847
+ var _a3;
848
+ var EmptyResponseBodyError = class extends AISDKError {
849
+ // used in isInstance
850
+ constructor({ message = "Empty response body" } = {}) {
851
+ super({ name: name2, message });
852
+ this[_a3] = true;
853
+ }
854
+ static isInstance(error) {
855
+ return AISDKError.hasMarker(error, marker3);
856
+ }
857
+ };
858
+ _a3 = symbol3;
859
+ function getErrorMessage(error) {
860
+ if (error == null) {
861
+ return "unknown error";
862
+ }
863
+ if (typeof error === "string") {
864
+ return error;
865
+ }
866
+ if (error instanceof Error) {
867
+ return error.message;
868
+ }
869
+ return JSON.stringify(error);
870
+ }
871
+ var name3 = "AI_InvalidArgumentError";
872
+ var marker4 = `vercel.ai.error.${name3}`;
873
+ var symbol4 = Symbol.for(marker4);
874
+ var _a4;
875
+ var InvalidArgumentError = class extends AISDKError {
876
+ constructor({
877
+ message,
878
+ cause,
879
+ argument
880
+ }) {
881
+ super({ name: name3, message, cause });
882
+ this[_a4] = true;
883
+ this.argument = argument;
884
+ }
885
+ static isInstance(error) {
886
+ return AISDKError.hasMarker(error, marker4);
887
+ }
888
+ };
889
+ _a4 = symbol4;
890
+ var name4 = "AI_InvalidPromptError";
891
+ var marker5 = `vercel.ai.error.${name4}`;
892
+ var symbol5 = Symbol.for(marker5);
893
+ var _a5;
894
+ var InvalidPromptError = class extends AISDKError {
895
+ constructor({
896
+ prompt,
897
+ message,
898
+ cause
899
+ }) {
900
+ super({ name: name4, message: `Invalid prompt: ${message}`, cause });
901
+ this[_a5] = true;
902
+ this.prompt = prompt;
903
+ }
904
+ static isInstance(error) {
905
+ return AISDKError.hasMarker(error, marker5);
906
+ }
907
+ };
908
+ _a5 = symbol5;
909
+ var name5 = "AI_InvalidResponseDataError";
910
+ var marker6 = `vercel.ai.error.${name5}`;
911
+ var symbol6 = Symbol.for(marker6);
912
+ var _a6;
913
+ var InvalidResponseDataError = class extends AISDKError {
914
+ constructor({
915
+ data,
916
+ message = `Invalid response data: ${JSON.stringify(data)}.`
917
+ }) {
918
+ super({ name: name5, message });
919
+ this[_a6] = true;
920
+ this.data = data;
921
+ }
922
+ static isInstance(error) {
923
+ return AISDKError.hasMarker(error, marker6);
924
+ }
925
+ };
926
+ _a6 = symbol6;
927
+ var name6 = "AI_JSONParseError";
928
+ var marker7 = `vercel.ai.error.${name6}`;
929
+ var symbol7 = Symbol.for(marker7);
930
+ var _a7;
931
+ var JSONParseError = class extends AISDKError {
932
+ constructor({ text, cause }) {
933
+ super({
934
+ name: name6,
935
+ message: `JSON parsing failed: Text: ${text}.
936
+ Error message: ${getErrorMessage(cause)}`,
937
+ cause
938
+ });
939
+ this[_a7] = true;
940
+ this.text = text;
941
+ }
942
+ static isInstance(error) {
943
+ return AISDKError.hasMarker(error, marker7);
944
+ }
945
+ };
946
+ _a7 = symbol7;
947
+ var name7 = "AI_LoadAPIKeyError";
948
+ var marker8 = `vercel.ai.error.${name7}`;
949
+ var symbol8 = Symbol.for(marker8);
950
+ var _a8;
951
+ var LoadAPIKeyError = class extends AISDKError {
952
+ // used in isInstance
953
+ constructor({ message }) {
954
+ super({ name: name7, message });
955
+ this[_a8] = true;
956
+ }
957
+ static isInstance(error) {
958
+ return AISDKError.hasMarker(error, marker8);
959
+ }
960
+ };
961
+ _a8 = symbol8;
962
+ var name12 = "AI_TypeValidationError";
963
+ var marker13 = `vercel.ai.error.${name12}`;
964
+ var symbol13 = Symbol.for(marker13);
965
+ var _a13;
966
+ var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
967
+ constructor({ value, cause }) {
968
+ super({
969
+ name: name12,
970
+ message: `Type validation failed: Value: ${JSON.stringify(value)}.
971
+ Error message: ${getErrorMessage(cause)}`,
972
+ cause
973
+ });
974
+ this[_a13] = true;
975
+ this.value = value;
976
+ }
977
+ static isInstance(error) {
978
+ return AISDKError.hasMarker(error, marker13);
979
+ }
980
+ /**
981
+ * Wraps an error into a TypeValidationError.
982
+ * If the cause is already a TypeValidationError with the same value, it returns the cause.
983
+ * Otherwise, it creates a new TypeValidationError.
984
+ *
985
+ * @param {Object} params - The parameters for wrapping the error.
986
+ * @param {unknown} params.value - The value that failed validation.
987
+ * @param {unknown} params.cause - The original error or cause of the validation failure.
988
+ * @returns {TypeValidationError} A TypeValidationError instance.
989
+ */
990
+ static wrap({
991
+ value,
992
+ cause
993
+ }) {
994
+ return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
995
+ }
996
+ };
997
+ _a13 = symbol13;
998
+ var TypeValidationError = _TypeValidationError;
999
+ var name13 = "AI_UnsupportedFunctionalityError";
1000
+ var marker14 = `vercel.ai.error.${name13}`;
1001
+ var symbol14 = Symbol.for(marker14);
1002
+ var _a14;
1003
+ var UnsupportedFunctionalityError3 = class extends AISDKError {
1004
+ constructor({
1005
+ functionality,
1006
+ message = `'${functionality}' functionality not supported.`
1007
+ }) {
1008
+ super({ name: name13, message });
1009
+ this[_a14] = true;
1010
+ this.functionality = functionality;
1011
+ }
1012
+ static isInstance(error) {
1013
+ return AISDKError.hasMarker(error, marker14);
1014
+ }
1015
+ };
1016
+ _a14 = symbol14;
1017
+ var ParseError = class extends Error {
1018
+ constructor(message, options) {
1019
+ super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
1020
+ }
1021
+ };
1022
+ function noop(_arg) {
1023
+ }
1024
+ function createParser(callbacks) {
1025
+ if (typeof callbacks == "function")
1026
+ throw new TypeError(
1027
+ "`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
1028
+ );
1029
+ const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
1030
+ let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
1031
+ function feed(newChunk) {
1032
+ const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
1033
+ for (const line of complete)
1034
+ parseLine(line);
1035
+ incompleteLine = incomplete, isFirstChunk = false;
1036
+ }
1037
+ function parseLine(line) {
1038
+ if (line === "") {
1039
+ dispatchEvent();
1040
+ return;
1041
+ }
1042
+ if (line.startsWith(":")) {
1043
+ onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
1044
+ return;
1045
+ }
1046
+ const fieldSeparatorIndex = line.indexOf(":");
1047
+ if (fieldSeparatorIndex !== -1) {
1048
+ const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
1049
+ processField(field, value, line);
1050
+ return;
1051
+ }
1052
+ processField(line, "", line);
1053
+ }
1054
+ function processField(field, value, line) {
1055
+ switch (field) {
1056
+ case "event":
1057
+ eventType = value;
1058
+ break;
1059
+ case "data":
1060
+ data = `${data}${value}
1061
+ `;
1062
+ break;
1063
+ case "id":
1064
+ id = value.includes("\0") ? void 0 : value;
1065
+ break;
1066
+ case "retry":
1067
+ /^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
1068
+ new ParseError(`Invalid \`retry\` value: "${value}"`, {
1069
+ type: "invalid-retry",
1070
+ value,
1071
+ line
1072
+ })
1073
+ );
1074
+ break;
1075
+ default:
1076
+ onError(
1077
+ new ParseError(
1078
+ `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
1079
+ { type: "unknown-field", field, value, line }
1080
+ )
1081
+ );
1082
+ break;
1083
+ }
1084
+ }
1085
+ function dispatchEvent() {
1086
+ data.length > 0 && onEvent({
1087
+ id,
1088
+ event: eventType || void 0,
1089
+ // If the data buffer's last character is a U+000A LINE FEED (LF) character,
1090
+ // then remove the last character from the data buffer.
1091
+ data: data.endsWith(`
1092
+ `) ? data.slice(0, -1) : data
1093
+ }), id = void 0, data = "", eventType = "";
1094
+ }
1095
+ function reset(options = {}) {
1096
+ incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
1097
+ }
1098
+ return { feed, reset };
1099
+ }
1100
+ function splitLines(chunk) {
1101
+ const lines = [];
1102
+ let incompleteLine = "", searchIndex = 0;
1103
+ for (; searchIndex < chunk.length; ) {
1104
+ const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
1105
+ `, searchIndex);
1106
+ let lineEnd = -1;
1107
+ if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
1108
+ incompleteLine = chunk.slice(searchIndex);
1109
+ break;
1110
+ } else {
1111
+ const line = chunk.slice(searchIndex, lineEnd);
1112
+ lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
1113
+ ` && searchIndex++;
1114
+ }
1115
+ }
1116
+ return [lines, incompleteLine];
1117
+ }
1118
+ var EventSourceParserStream = class extends TransformStream {
1119
+ constructor({ onError, onRetry, onComment } = {}) {
1120
+ let parser;
1121
+ super({
1122
+ start(controller) {
1123
+ parser = createParser({
1124
+ onEvent: (event) => {
1125
+ controller.enqueue(event);
1126
+ },
1127
+ onError(error) {
1128
+ onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
1129
+ },
1130
+ onRetry,
1131
+ onComment
1132
+ });
1133
+ },
1134
+ transform(chunk) {
1135
+ parser.feed(chunk);
1136
+ }
1137
+ });
1138
+ }
1139
+ };
1140
+ new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
1141
+ function combineHeaders2(...headers) {
1142
+ return headers.reduce(
1143
+ (combinedHeaders, currentHeaders) => __spreadValues(__spreadValues({}, combinedHeaders), currentHeaders != null ? currentHeaders : {}),
1144
+ {}
1145
+ );
1146
+ }
1147
+ function extractResponseHeaders(response) {
1148
+ return Object.fromEntries([...response.headers]);
1149
+ }
1150
+ var createIdGenerator = ({
1151
+ prefix,
1152
+ size = 16,
1153
+ alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1154
+ separator = "-"
1155
+ } = {}) => {
1156
+ const generator = () => {
1157
+ const alphabetLength = alphabet.length;
1158
+ const chars = new Array(size);
1159
+ for (let i = 0; i < size; i++) {
1160
+ chars[i] = alphabet[Math.random() * alphabetLength | 0];
1161
+ }
1162
+ return chars.join("");
1163
+ };
1164
+ if (prefix == null) {
1165
+ return generator;
1166
+ }
1167
+ if (alphabet.includes(separator)) {
1168
+ throw new InvalidArgumentError({
1169
+ argument: "separator",
1170
+ message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
1171
+ });
1172
+ }
1173
+ return () => `${prefix}${separator}${generator()}`;
1174
+ };
1175
+ var generateId2 = createIdGenerator();
1176
+ function isAbortError(error) {
1177
+ return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
1178
+ error.name === "TimeoutError");
1179
+ }
1180
+ var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
1181
+ function handleFetchError({
1182
+ error,
1183
+ url,
1184
+ requestBodyValues
1185
+ }) {
1186
+ if (isAbortError(error)) {
1187
+ return error;
1188
+ }
1189
+ if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
1190
+ const cause = error.cause;
1191
+ if (cause != null) {
1192
+ return new APICallError({
1193
+ message: `Cannot connect to API: ${cause.message}`,
1194
+ cause,
1195
+ url,
1196
+ requestBodyValues,
1197
+ isRetryable: true
1198
+ // retry when network error
1199
+ });
1200
+ }
1201
+ }
1202
+ return error;
1203
+ }
1204
+ function removeUndefinedEntries(record) {
1205
+ return Object.fromEntries(
1206
+ Object.entries(record).filter(([_key, value]) => value != null)
1207
+ );
1208
+ }
1209
+ function loadApiKey2({
1210
+ apiKey,
1211
+ environmentVariableName,
1212
+ apiKeyParameterName = "apiKey",
1213
+ description
1214
+ }) {
1215
+ if (typeof apiKey === "string") {
1216
+ return apiKey;
1217
+ }
1218
+ if (apiKey != null) {
1219
+ throw new LoadAPIKeyError({
1220
+ message: `${description} API key must be a string.`
1221
+ });
1222
+ }
1223
+ if (typeof process === "undefined") {
1224
+ throw new LoadAPIKeyError({
1225
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
1226
+ });
1227
+ }
1228
+ apiKey = process.env[environmentVariableName];
1229
+ if (apiKey == null) {
1230
+ throw new LoadAPIKeyError({
1231
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
1232
+ });
1233
+ }
1234
+ if (typeof apiKey !== "string") {
1235
+ throw new LoadAPIKeyError({
1236
+ message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
1237
+ });
1238
+ }
1239
+ return apiKey;
1240
+ }
1241
+ var suspectProtoRx = /"__proto__"\s*:/;
1242
+ var suspectConstructorRx = /"constructor"\s*:/;
1243
+ function _parse(text) {
1244
+ const obj = JSON.parse(text);
1245
+ if (obj === null || typeof obj !== "object") {
1246
+ return obj;
1247
+ }
1248
+ if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
1249
+ return obj;
1250
+ }
1251
+ return filter(obj);
1252
+ }
1253
+ function filter(obj) {
1254
+ let next = [obj];
1255
+ while (next.length) {
1256
+ const nodes = next;
1257
+ next = [];
1258
+ for (const node of nodes) {
1259
+ if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
1260
+ throw new SyntaxError("Object contains forbidden prototype property");
1261
+ }
1262
+ if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
1263
+ throw new SyntaxError("Object contains forbidden prototype property");
1264
+ }
1265
+ for (const key in node) {
1266
+ const value = node[key];
1267
+ if (value && typeof value === "object") {
1268
+ next.push(value);
1269
+ }
1270
+ }
1271
+ }
1272
+ }
1273
+ return obj;
1274
+ }
1275
+ function secureJsonParse(text) {
1276
+ const { stackTraceLimit } = Error;
1277
+ Error.stackTraceLimit = 0;
1278
+ try {
1279
+ return _parse(text);
1280
+ } finally {
1281
+ Error.stackTraceLimit = stackTraceLimit;
1282
+ }
1283
+ }
1284
+ var validatorSymbol = Symbol.for("vercel.ai.validator");
1285
+ function validator(validate) {
1286
+ return { [validatorSymbol]: true, validate };
1287
+ }
1288
+ function isValidator(value) {
1289
+ return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
1290
+ }
1291
+ function asValidator(value) {
1292
+ return isValidator(value) ? value : standardSchemaValidator(value);
1293
+ }
1294
+ function standardSchemaValidator(standardSchema) {
1295
+ return validator(async (value) => {
1296
+ const result = await standardSchema["~standard"].validate(value);
1297
+ return result.issues == null ? { success: true, value: result.value } : {
1298
+ success: false,
1299
+ error: new TypeValidationError({
1300
+ value,
1301
+ cause: result.issues
1302
+ })
1303
+ };
1304
+ });
1305
+ }
1306
+ async function validateTypes({
1307
+ value,
1308
+ schema
1309
+ }) {
1310
+ const result = await safeValidateTypes({ value, schema });
1311
+ if (!result.success) {
1312
+ throw TypeValidationError.wrap({ value, cause: result.error });
1313
+ }
1314
+ return result.value;
1315
+ }
1316
+ async function safeValidateTypes({
1317
+ value,
1318
+ schema
1319
+ }) {
1320
+ const validator2 = asValidator(schema);
1321
+ try {
1322
+ if (validator2.validate == null) {
1323
+ return { success: true, value, rawValue: value };
1324
+ }
1325
+ const result = await validator2.validate(value);
1326
+ if (result.success) {
1327
+ return { success: true, value: result.value, rawValue: value };
1328
+ }
1329
+ return {
1330
+ success: false,
1331
+ error: TypeValidationError.wrap({ value, cause: result.error }),
1332
+ rawValue: value
1333
+ };
1334
+ } catch (error) {
1335
+ return {
1336
+ success: false,
1337
+ error: TypeValidationError.wrap({ value, cause: error }),
1338
+ rawValue: value
1339
+ };
1340
+ }
1341
+ }
1342
+ async function parseJSON({
1343
+ text,
1344
+ schema
1345
+ }) {
1346
+ try {
1347
+ const value = secureJsonParse(text);
1348
+ if (schema == null) {
1349
+ return value;
1350
+ }
1351
+ return validateTypes({ value, schema });
1352
+ } catch (error) {
1353
+ if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
1354
+ throw error;
1355
+ }
1356
+ throw new JSONParseError({ text, cause: error });
1357
+ }
1358
+ }
1359
+ async function safeParseJSON({
1360
+ text,
1361
+ schema
1362
+ }) {
1363
+ try {
1364
+ const value = secureJsonParse(text);
1365
+ if (schema == null) {
1366
+ return { success: true, value, rawValue: value };
1367
+ }
1368
+ return await safeValidateTypes({ value, schema });
1369
+ } catch (error) {
1370
+ return {
1371
+ success: false,
1372
+ error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error }),
1373
+ rawValue: void 0
1374
+ };
1375
+ }
1376
+ }
1377
+ function isParsableJson(input) {
1378
+ try {
1379
+ secureJsonParse(input);
1380
+ return true;
1381
+ } catch (e) {
1382
+ return false;
1383
+ }
1384
+ }
1385
+ function parseJsonEventStream({
1386
+ stream,
1387
+ schema
1388
+ }) {
1389
+ return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
1390
+ new TransformStream({
1391
+ async transform({ data }, controller) {
1392
+ if (data === "[DONE]") {
1393
+ return;
1394
+ }
1395
+ controller.enqueue(await safeParseJSON({ text: data, schema }));
1396
+ }
1397
+ })
1398
+ );
1399
+ }
1400
+ var getOriginalFetch2 = () => globalThis.fetch;
1401
+ var postJsonToApi2 = async ({
1402
+ url,
1403
+ headers,
1404
+ body,
1405
+ failedResponseHandler,
1406
+ successfulResponseHandler,
1407
+ abortSignal,
1408
+ fetch: fetch2
1409
+ }) => postToApi({
1410
+ url,
1411
+ headers: __spreadValues({
1412
+ "Content-Type": "application/json"
1413
+ }, headers),
1414
+ body: {
1415
+ content: JSON.stringify(body),
1416
+ values: body
1417
+ },
1418
+ failedResponseHandler,
1419
+ successfulResponseHandler,
1420
+ abortSignal,
1421
+ fetch: fetch2
1422
+ });
1423
+ var postToApi = async ({
1424
+ url,
1425
+ headers = {},
1426
+ body,
1427
+ successfulResponseHandler,
1428
+ failedResponseHandler,
1429
+ abortSignal,
1430
+ fetch: fetch2 = getOriginalFetch2()
1431
+ }) => {
1432
+ try {
1433
+ const response = await fetch2(url, {
1434
+ method: "POST",
1435
+ headers: removeUndefinedEntries(headers),
1436
+ body: body.content,
1437
+ signal: abortSignal
1438
+ });
1439
+ const responseHeaders = extractResponseHeaders(response);
1440
+ if (!response.ok) {
1441
+ let errorInformation;
1442
+ try {
1443
+ errorInformation = await failedResponseHandler({
1444
+ response,
1445
+ url,
1446
+ requestBodyValues: body.values
1447
+ });
1448
+ } catch (error) {
1449
+ if (isAbortError(error) || APICallError.isInstance(error)) {
1450
+ throw error;
1451
+ }
1452
+ throw new APICallError({
1453
+ message: "Failed to process error response",
1454
+ cause: error,
1455
+ statusCode: response.status,
1456
+ url,
1457
+ responseHeaders,
1458
+ requestBodyValues: body.values
1459
+ });
1460
+ }
1461
+ throw errorInformation.value;
1462
+ }
1463
+ try {
1464
+ return await successfulResponseHandler({
1465
+ response,
1466
+ url,
1467
+ requestBodyValues: body.values
1468
+ });
1469
+ } catch (error) {
1470
+ if (error instanceof Error) {
1471
+ if (isAbortError(error) || APICallError.isInstance(error)) {
1472
+ throw error;
1473
+ }
1474
+ }
1475
+ throw new APICallError({
1476
+ message: "Failed to process successful response",
1477
+ cause: error,
1478
+ statusCode: response.status,
1479
+ url,
1480
+ responseHeaders,
1481
+ requestBodyValues: body.values
1482
+ });
1483
+ }
1484
+ } catch (error) {
1485
+ throw handleFetchError({ error, url, requestBodyValues: body.values });
1486
+ }
1487
+ };
1488
+ var createJsonErrorResponseHandler2 = ({
1489
+ errorSchema,
1490
+ errorToMessage,
1491
+ isRetryable
1492
+ }) => async ({ response, url, requestBodyValues }) => {
1493
+ const responseBody = await response.text();
1494
+ const responseHeaders = extractResponseHeaders(response);
1495
+ if (responseBody.trim() === "") {
1496
+ return {
1497
+ responseHeaders,
1498
+ value: new APICallError({
1499
+ message: response.statusText,
1500
+ url,
1501
+ requestBodyValues,
1502
+ statusCode: response.status,
1503
+ responseHeaders,
1504
+ responseBody,
1505
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response)
1506
+ })
1507
+ };
1508
+ }
1509
+ try {
1510
+ const parsedError = await parseJSON({
1511
+ text: responseBody,
1512
+ schema: errorSchema
1513
+ });
1514
+ return {
1515
+ responseHeaders,
1516
+ value: new APICallError({
1517
+ message: errorToMessage(parsedError),
1518
+ url,
1519
+ requestBodyValues,
1520
+ statusCode: response.status,
1521
+ responseHeaders,
1522
+ responseBody,
1523
+ data: parsedError,
1524
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
1525
+ })
1526
+ };
1527
+ } catch (parseError) {
1528
+ return {
1529
+ responseHeaders,
1530
+ value: new APICallError({
1531
+ message: response.statusText,
1532
+ url,
1533
+ requestBodyValues,
1534
+ statusCode: response.status,
1535
+ responseHeaders,
1536
+ responseBody,
1537
+ isRetryable: isRetryable == null ? void 0 : isRetryable(response)
1538
+ })
1539
+ };
1540
+ }
1541
+ };
1542
+ var createEventSourceResponseHandler2 = (chunkSchema) => async ({ response }) => {
1543
+ const responseHeaders = extractResponseHeaders(response);
1544
+ if (response.body == null) {
1545
+ throw new EmptyResponseBodyError({});
1546
+ }
1547
+ return {
1548
+ responseHeaders,
1549
+ value: parseJsonEventStream({
1550
+ stream: response.body,
1551
+ schema: chunkSchema
1552
+ })
1553
+ };
1554
+ };
1555
+ var createJsonResponseHandler2 = (responseSchema) => async ({ response, url, requestBodyValues }) => {
1556
+ const responseBody = await response.text();
1557
+ const parsedResult = await safeParseJSON({
1558
+ text: responseBody,
1559
+ schema: responseSchema
1560
+ });
1561
+ const responseHeaders = extractResponseHeaders(response);
1562
+ if (!parsedResult.success) {
1563
+ throw new APICallError({
1564
+ message: "Invalid JSON response",
1565
+ cause: parsedResult.error,
1566
+ statusCode: response.status,
1567
+ responseHeaders,
1568
+ responseBody,
1569
+ url,
1570
+ requestBodyValues
1571
+ });
1572
+ }
1573
+ return {
1574
+ responseHeaders,
1575
+ value: parsedResult.value,
1576
+ rawValue: parsedResult.rawValue
1577
+ };
1578
+ };
1579
+ var { btoa} = globalThis;
1580
+ function convertUint8ArrayToBase64(array3) {
1581
+ let latin1string = "";
1582
+ for (let i = 0; i < array3.length; i++) {
1583
+ latin1string += String.fromCodePoint(array3[i]);
1584
+ }
1585
+ return btoa(latin1string);
1586
+ }
1587
+ function withoutTrailingSlash2(url) {
1588
+ return url == null ? void 0 : url.replace(/\/$/, "");
1589
+ }
1590
+ var ReasoningDetailSummarySchema = z.object({
1591
+ type: z.literal(
1592
+ "reasoning.summary"
1593
+ /* Summary */
1594
+ ),
1595
+ summary: z.string()
1596
+ });
1597
+ var ReasoningDetailEncryptedSchema = z.object({
1598
+ type: z.literal(
1599
+ "reasoning.encrypted"
1600
+ /* Encrypted */
1601
+ ),
1602
+ data: z.string()
1603
+ });
1604
+ var ReasoningDetailTextSchema = z.object({
1605
+ type: z.literal(
1606
+ "reasoning.text"
1607
+ /* Text */
1608
+ ),
1609
+ text: z.string().nullish(),
1610
+ signature: z.string().nullish()
1611
+ });
1612
+ var ReasoningDetailUnionSchema = z.union([
1613
+ ReasoningDetailSummarySchema,
1614
+ ReasoningDetailEncryptedSchema,
1615
+ ReasoningDetailTextSchema
1616
+ ]);
1617
+ var ReasoningDetailsWithUnknownSchema = z.union([
1618
+ ReasoningDetailUnionSchema,
1619
+ z.unknown().transform(() => null)
1620
+ ]);
1621
+ var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1622
+ var OpenRouterErrorResponseSchema = z.object({
1623
+ error: z.object({
1624
+ code: z.union([z.string(), z.number()]).nullable().optional().default(null),
1625
+ message: z.string(),
1626
+ type: z.string().nullable().optional().default(null),
1627
+ param: z.any().nullable().optional().default(null)
1628
+ })
1629
+ });
1630
+ var openrouterFailedResponseHandler = createJsonErrorResponseHandler2({
1631
+ errorSchema: OpenRouterErrorResponseSchema,
1632
+ errorToMessage: (data) => data.error.message
1633
+ });
1634
+ function mapOpenRouterFinishReason(finishReason) {
1635
+ switch (finishReason) {
1636
+ case "stop":
1637
+ return "stop";
1638
+ case "length":
1639
+ return "length";
1640
+ case "content_filter":
1641
+ return "content-filter";
1642
+ case "function_call":
1643
+ case "tool_calls":
1644
+ return "tool-calls";
1645
+ default:
1646
+ return "unknown";
1647
+ }
1648
+ }
1649
+ function isUrl({
1650
+ url,
1651
+ protocols
1652
+ }) {
1653
+ try {
1654
+ const urlObj = new URL(url);
1655
+ return protocols.has(urlObj.protocol);
1656
+ } catch (_) {
1657
+ return false;
1658
+ }
1659
+ }
1660
+ function getFileUrl({
1661
+ part,
1662
+ defaultMediaType
1663
+ }) {
1664
+ var _a15, _b;
1665
+ if (part.data instanceof Uint8Array) {
1666
+ const base64 = convertUint8ArrayToBase64(part.data);
1667
+ return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
1668
+ }
1669
+ const stringUrl = part.data.toString();
1670
+ if (isUrl({
1671
+ url: stringUrl,
1672
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1673
+ })) {
1674
+ return stringUrl;
1675
+ }
1676
+ return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
1677
+ }
1678
+ function getMediaType(dataUrl, defaultMediaType) {
1679
+ var _a15;
1680
+ const match = dataUrl.match(/^data:([^;]+)/);
1681
+ return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
1682
+ }
1683
+ function getBase64FromDataUrl(dataUrl) {
1684
+ const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
1685
+ return match ? match[1] : dataUrl;
1686
+ }
1687
+ function getCacheControl(providerMetadata) {
1688
+ var _a15, _b, _c;
1689
+ const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
1690
+ const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
1691
+ return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
1692
+ }
1693
+ function convertToOpenRouterChatMessages(prompt) {
1694
+ var _a15, _b, _c;
1695
+ const messages = [];
1696
+ for (const { role, content, providerOptions } of prompt) {
1697
+ switch (role) {
1698
+ case "system": {
1699
+ messages.push({
1700
+ role: "system",
1701
+ content,
1702
+ cache_control: getCacheControl(providerOptions)
1703
+ });
1704
+ break;
1705
+ }
1706
+ case "user": {
1707
+ if (content.length === 1 && ((_a15 = content[0]) == null ? void 0 : _a15.type) === "text") {
1708
+ const cacheControl = (_b = getCacheControl(providerOptions)) != null ? _b : getCacheControl(content[0].providerOptions);
1709
+ const contentWithCacheControl = cacheControl ? [
1710
+ {
1711
+ type: "text",
1712
+ text: content[0].text,
1713
+ cache_control: cacheControl
1714
+ }
1715
+ ] : content[0].text;
1716
+ messages.push({
1717
+ role: "user",
1718
+ content: contentWithCacheControl
1719
+ });
1720
+ break;
1721
+ }
1722
+ const messageCacheControl = getCacheControl(providerOptions);
1723
+ const contentParts = content.map(
1724
+ (part) => {
1725
+ var _a16, _b2, _c2, _d, _e, _f;
1726
+ const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1727
+ switch (part.type) {
1728
+ case "text":
1729
+ return {
1730
+ type: "text",
1731
+ text: part.text,
1732
+ // For text parts, only use part-specific cache control
1733
+ cache_control: cacheControl
1734
+ };
1735
+ case "file": {
1736
+ if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
1737
+ const url = getFileUrl({
1738
+ part,
1739
+ defaultMediaType: "image/jpeg"
1740
+ });
1741
+ return {
1742
+ type: "image_url",
1743
+ image_url: {
1744
+ url
1745
+ },
1746
+ // For image parts, use part-specific or message-level cache control
1747
+ cache_control: cacheControl
1748
+ };
1749
+ }
1750
+ const fileName = String(
1751
+ (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1752
+ );
1753
+ const fileData = getFileUrl({
1754
+ part,
1755
+ defaultMediaType: "application/pdf"
1756
+ });
1757
+ if (isUrl({
1758
+ url: fileData,
1759
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1760
+ })) {
1761
+ return {
1762
+ type: "file",
1763
+ file: {
1764
+ filename: fileName,
1765
+ file_data: fileData
1766
+ }
1767
+ };
1768
+ }
1769
+ return {
1770
+ type: "file",
1771
+ file: {
1772
+ filename: fileName,
1773
+ file_data: fileData
1774
+ },
1775
+ cache_control: cacheControl
1776
+ };
1777
+ }
1778
+ default: {
1779
+ return {
1780
+ type: "text",
1781
+ text: "",
1782
+ cache_control: cacheControl
1783
+ };
1784
+ }
1785
+ }
1786
+ }
1787
+ );
1788
+ messages.push({
1789
+ role: "user",
1790
+ content: contentParts
1791
+ });
1792
+ break;
1793
+ }
1794
+ case "assistant": {
1795
+ let text = "";
1796
+ let reasoning = "";
1797
+ const reasoningDetails = [];
1798
+ const toolCalls = [];
1799
+ for (const part of content) {
1800
+ switch (part.type) {
1801
+ case "text": {
1802
+ text += part.text;
1803
+ break;
1804
+ }
1805
+ case "tool-call": {
1806
+ toolCalls.push({
1807
+ id: part.toolCallId,
1808
+ type: "function",
1809
+ function: {
1810
+ name: part.toolName,
1811
+ arguments: JSON.stringify(part.input)
1812
+ }
1813
+ });
1814
+ break;
1815
+ }
1816
+ case "reasoning": {
1817
+ reasoning += part.text;
1818
+ reasoningDetails.push({
1819
+ type: "reasoning.text",
1820
+ text: part.text
1821
+ });
1822
+ break;
1823
+ }
1824
+ }
1825
+ }
1826
+ messages.push({
1827
+ role: "assistant",
1828
+ content: text,
1829
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
1830
+ reasoning: reasoning || void 0,
1831
+ reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
1832
+ cache_control: getCacheControl(providerOptions)
1833
+ });
1834
+ break;
1835
+ }
1836
+ case "tool": {
1837
+ for (const toolResponse of content) {
1838
+ const content2 = getToolResultContent(toolResponse);
1839
+ messages.push({
1840
+ role: "tool",
1841
+ tool_call_id: toolResponse.toolCallId,
1842
+ content: content2,
1843
+ cache_control: (_c = getCacheControl(providerOptions)) != null ? _c : getCacheControl(toolResponse.providerOptions)
1844
+ });
1845
+ }
1846
+ break;
1847
+ }
1848
+ }
1849
+ }
1850
+ return messages;
1851
+ }
1852
+ function getToolResultContent(input) {
1853
+ return input.output.type === "text" ? input.output.value : JSON.stringify(input.output.value);
1854
+ }
1855
+ z.union([
1856
+ z.literal("auto"),
1857
+ z.literal("none"),
1858
+ z.literal("required"),
1859
+ z.object({
1860
+ type: z.literal("function"),
1861
+ function: z.object({
1862
+ name: z.string()
1863
+ })
1864
+ })
1865
+ ]);
1866
+ function getChatCompletionToolChoice(toolChoice) {
1867
+ switch (toolChoice.type) {
1868
+ case "auto":
1869
+ case "none":
1870
+ case "required":
1871
+ return toolChoice.type;
1872
+ case "tool": {
1873
+ return {
1874
+ type: "function",
1875
+ function: { name: toolChoice.toolName }
1876
+ };
1877
+ }
1878
+ default: {
1879
+ throw new Error(`Invalid tool choice type: ${toolChoice}`);
1880
+ }
1881
+ }
1882
+ }
1883
+ var ImageResponseSchema = z.object({
1884
+ type: z.literal("image_url"),
1885
+ image_url: z.object({
1886
+ url: z.string()
1887
+ })
1888
+ });
1889
+ var ImageResponseWithUnknownSchema = z.union([
1890
+ ImageResponseSchema,
1891
+ z.unknown().transform(() => null)
1892
+ ]);
1893
+ var ImageResponseArraySchema = z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1894
+ var OpenRouterChatCompletionBaseResponseSchema = z.object({
1895
+ id: z.string().optional(),
1896
+ model: z.string().optional(),
1897
+ provider: z.string().optional(),
1898
+ usage: z.object({
1899
+ prompt_tokens: z.number(),
1900
+ prompt_tokens_details: z.object({
1901
+ cached_tokens: z.number()
1902
+ }).nullish(),
1903
+ completion_tokens: z.number(),
1904
+ completion_tokens_details: z.object({
1905
+ reasoning_tokens: z.number()
1906
+ }).nullish(),
1907
+ total_tokens: z.number(),
1908
+ cost: z.number().optional(),
1909
+ cost_details: z.object({
1910
+ upstream_inference_cost: z.number().nullish()
1911
+ }).nullish()
1912
+ }).nullish()
1913
+ });
1914
+ var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1915
+ choices: z.array(
1916
+ z.object({
1917
+ message: z.object({
1918
+ role: z.literal("assistant"),
1919
+ content: z.string().nullable().optional(),
1920
+ reasoning: z.string().nullable().optional(),
1921
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1922
+ images: ImageResponseArraySchema.nullish(),
1923
+ tool_calls: z.array(
1924
+ z.object({
1925
+ id: z.string().optional().nullable(),
1926
+ type: z.literal("function"),
1927
+ function: z.object({
1928
+ name: z.string(),
1929
+ arguments: z.string()
1930
+ })
1931
+ })
1932
+ ).optional(),
1933
+ annotations: z.array(
1934
+ z.object({
1935
+ type: z.enum(["url_citation"]),
1936
+ url_citation: z.object({
1937
+ end_index: z.number(),
1938
+ start_index: z.number(),
1939
+ title: z.string(),
1940
+ url: z.string(),
1941
+ content: z.string().optional()
1942
+ })
1943
+ })
1944
+ ).nullish()
1945
+ }),
1946
+ index: z.number().nullish(),
1947
+ logprobs: z.object({
1948
+ content: z.array(
1949
+ z.object({
1950
+ token: z.string(),
1951
+ logprob: z.number(),
1952
+ top_logprobs: z.array(
1953
+ z.object({
1954
+ token: z.string(),
1955
+ logprob: z.number()
1956
+ })
1957
+ )
1958
+ })
1959
+ ).nullable()
1960
+ }).nullable().optional(),
1961
+ finish_reason: z.string().optional().nullable()
1962
+ })
1963
+ )
1964
+ });
1965
+ var OpenRouterStreamChatCompletionChunkSchema = z.union([
1966
+ OpenRouterChatCompletionBaseResponseSchema.extend({
1967
+ choices: z.array(
1968
+ z.object({
1969
+ delta: z.object({
1970
+ role: z.enum(["assistant"]).optional(),
1971
+ content: z.string().nullish(),
1972
+ reasoning: z.string().nullish().optional(),
1973
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1974
+ images: ImageResponseArraySchema.nullish(),
1975
+ tool_calls: z.array(
1976
+ z.object({
1977
+ index: z.number().nullish(),
1978
+ id: z.string().nullish(),
1979
+ type: z.literal("function").optional(),
1980
+ function: z.object({
1981
+ name: z.string().nullish(),
1982
+ arguments: z.string().nullish()
1983
+ })
1984
+ })
1985
+ ).nullish(),
1986
+ annotations: z.array(
1987
+ z.object({
1988
+ type: z.enum(["url_citation"]),
1989
+ url_citation: z.object({
1990
+ end_index: z.number(),
1991
+ start_index: z.number(),
1992
+ title: z.string(),
1993
+ url: z.string(),
1994
+ content: z.string().optional()
1995
+ })
1996
+ })
1997
+ ).nullish()
1998
+ }).nullish(),
1999
+ logprobs: z.object({
2000
+ content: z.array(
2001
+ z.object({
2002
+ token: z.string(),
2003
+ logprob: z.number(),
2004
+ top_logprobs: z.array(
2005
+ z.object({
2006
+ token: z.string(),
2007
+ logprob: z.number()
2008
+ })
2009
+ )
2010
+ })
2011
+ ).nullable()
2012
+ }).nullish(),
2013
+ finish_reason: z.string().nullable().optional(),
2014
+ index: z.number().nullish()
2015
+ })
2016
+ )
2017
+ }),
2018
+ OpenRouterErrorResponseSchema
2019
+ ]);
2020
+ var OpenRouterChatLanguageModel = class {
2021
+ constructor(modelId, settings, config) {
2022
+ this.specificationVersion = "v2";
2023
+ this.provider = "openrouter";
2024
+ this.defaultObjectGenerationMode = "tool";
2025
+ this.supportedUrls = {
2026
+ "image/*": [
2027
+ /^data:image\/[a-zA-Z]+;base64,/,
2028
+ /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
2029
+ ],
2030
+ // 'text/*': [/^data:text\//, /^https?:\/\/.+$/],
2031
+ "application/*": [/^data:application\//, /^https?:\/\/.+$/]
2032
+ };
2033
+ this.modelId = modelId;
2034
+ this.settings = settings;
2035
+ this.config = config;
2036
+ }
2037
+ getArgs({
2038
+ prompt,
2039
+ maxOutputTokens,
2040
+ temperature,
2041
+ topP,
2042
+ frequencyPenalty,
2043
+ presencePenalty,
2044
+ seed,
2045
+ stopSequences,
2046
+ responseFormat,
2047
+ topK,
2048
+ tools,
2049
+ toolChoice
2050
+ }) {
2051
+ var _a15;
2052
+ const baseArgs = __spreadValues(__spreadValues({
2053
+ // model id:
2054
+ model: this.modelId,
2055
+ models: this.settings.models,
2056
+ // model specific settings:
2057
+ logit_bias: this.settings.logitBias,
2058
+ logprobs: this.settings.logprobs === true || typeof this.settings.logprobs === "number" ? true : void 0,
2059
+ top_logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
2060
+ user: this.settings.user,
2061
+ parallel_tool_calls: this.settings.parallelToolCalls,
2062
+ // standardized settings:
2063
+ max_tokens: maxOutputTokens,
2064
+ temperature,
2065
+ top_p: topP,
2066
+ frequency_penalty: frequencyPenalty,
2067
+ presence_penalty: presencePenalty,
2068
+ seed,
2069
+ stop: stopSequences,
2070
+ response_format: responseFormat,
2071
+ top_k: topK,
2072
+ // messages:
2073
+ messages: convertToOpenRouterChatMessages(prompt),
2074
+ // OpenRouter specific settings:
2075
+ include_reasoning: this.settings.includeReasoning,
2076
+ reasoning: this.settings.reasoning,
2077
+ usage: this.settings.usage,
2078
+ // Web search settings:
2079
+ plugins: this.settings.plugins,
2080
+ web_search_options: this.settings.web_search_options,
2081
+ // Provider routing settings:
2082
+ provider: this.settings.provider
2083
+ }, this.config.extraBody), this.settings.extraBody);
2084
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
2085
+ return __spreadProps(__spreadValues({}, baseArgs), {
2086
+ response_format: {
2087
+ type: "json_schema",
2088
+ json_schema: __spreadValues({
2089
+ schema: responseFormat.schema,
2090
+ strict: true,
2091
+ name: (_a15 = responseFormat.name) != null ? _a15 : "response"
2092
+ }, responseFormat.description && {
2093
+ description: responseFormat.description
2094
+ })
2095
+ }
2096
+ });
2097
+ }
2098
+ if (tools && tools.length > 0) {
2099
+ const mappedTools = tools.filter((tool) => tool.type === "function").map((tool) => ({
2100
+ type: "function",
2101
+ function: {
2102
+ name: tool.name,
2103
+ description: tool.description,
2104
+ parameters: tool.inputSchema
2105
+ }
2106
+ }));
2107
+ return __spreadProps(__spreadValues({}, baseArgs), {
2108
+ tools: mappedTools,
2109
+ tool_choice: toolChoice ? getChatCompletionToolChoice(toolChoice) : void 0
2110
+ });
2111
+ }
2112
+ return baseArgs;
2113
+ }
2114
+ async doGenerate(options) {
2115
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
2116
+ const providerOptions = options.providerOptions || {};
2117
+ const openrouterOptions = providerOptions.openrouter || {};
2118
+ const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2119
+ const { value: response, responseHeaders } = await postJsonToApi2({
2120
+ url: this.config.url({
2121
+ path: "/chat/completions",
2122
+ modelId: this.modelId
2123
+ }),
2124
+ headers: combineHeaders2(this.config.headers(), options.headers),
2125
+ body: args,
2126
+ failedResponseHandler: openrouterFailedResponseHandler,
2127
+ successfulResponseHandler: createJsonResponseHandler2(
2128
+ OpenRouterNonStreamChatCompletionResponseSchema
2129
+ ),
2130
+ abortSignal: options.abortSignal,
2131
+ fetch: this.config.fetch
2132
+ });
2133
+ const choice = response.choices[0];
2134
+ if (!choice) {
2135
+ throw new Error("No choice in response");
2136
+ }
2137
+ const usageInfo = response.usage ? {
2138
+ inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
2139
+ outputTokens: (_b = response.usage.completion_tokens) != null ? _b : 0,
2140
+ totalTokens: ((_c = response.usage.prompt_tokens) != null ? _c : 0) + ((_d = response.usage.completion_tokens) != null ? _d : 0),
2141
+ reasoningTokens: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0,
2142
+ cachedInputTokens: (_h = (_g = response.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
2143
+ } : {
2144
+ inputTokens: 0,
2145
+ outputTokens: 0,
2146
+ totalTokens: 0,
2147
+ reasoningTokens: 0,
2148
+ cachedInputTokens: 0
2149
+ };
2150
+ const reasoningDetails = (_i = choice.message.reasoning_details) != null ? _i : [];
2151
+ const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
2152
+ switch (detail.type) {
2153
+ case "reasoning.text": {
2154
+ if (detail.text) {
2155
+ return {
2156
+ type: "reasoning",
2157
+ text: detail.text
2158
+ };
2159
+ }
2160
+ break;
2161
+ }
2162
+ case "reasoning.summary": {
2163
+ if (detail.summary) {
2164
+ return {
2165
+ type: "reasoning",
2166
+ text: detail.summary
2167
+ };
2168
+ }
2169
+ break;
2170
+ }
2171
+ case "reasoning.encrypted": {
2172
+ if (detail.data) {
2173
+ return {
2174
+ type: "reasoning",
2175
+ text: "[REDACTED]"
2176
+ };
2177
+ }
2178
+ break;
2179
+ }
2180
+ }
2181
+ return null;
2182
+ }).filter((p) => p !== null) : choice.message.reasoning ? [
2183
+ {
2184
+ type: "reasoning",
2185
+ text: choice.message.reasoning
2186
+ }
2187
+ ] : [];
2188
+ const content = [];
2189
+ content.push(...reasoning);
2190
+ if (choice.message.content) {
2191
+ content.push({
2192
+ type: "text",
2193
+ text: choice.message.content
2194
+ });
2195
+ }
2196
+ if (choice.message.tool_calls) {
2197
+ for (const toolCall of choice.message.tool_calls) {
2198
+ content.push({
2199
+ type: "tool-call",
2200
+ toolCallId: (_j = toolCall.id) != null ? _j : generateId2(),
2201
+ toolName: toolCall.function.name,
2202
+ input: toolCall.function.arguments
2203
+ });
2204
+ }
2205
+ }
2206
+ if (choice.message.images) {
2207
+ for (const image of choice.message.images) {
2208
+ content.push({
2209
+ type: "file",
2210
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
2211
+ data: getBase64FromDataUrl(image.image_url.url)
2212
+ });
2213
+ }
2214
+ }
2215
+ if (choice.message.annotations) {
2216
+ for (const annotation of choice.message.annotations) {
2217
+ if (annotation.type === "url_citation") {
2218
+ content.push({
2219
+ type: "source",
2220
+ sourceType: "url",
2221
+ id: annotation.url_citation.url,
2222
+ url: annotation.url_citation.url,
2223
+ title: annotation.url_citation.title,
2224
+ providerMetadata: {
2225
+ openrouter: {
2226
+ content: annotation.url_citation.content || ""
2227
+ }
2228
+ }
2229
+ });
2230
+ }
2231
+ }
2232
+ }
2233
+ return {
2234
+ content,
2235
+ finishReason: mapOpenRouterFinishReason(choice.finish_reason),
2236
+ usage: usageInfo,
2237
+ warnings: [],
2238
+ providerMetadata: {
2239
+ openrouter: {
2240
+ provider: (_k = response.provider) != null ? _k : "",
2241
+ usage: {
2242
+ promptTokens: (_l = usageInfo.inputTokens) != null ? _l : 0,
2243
+ completionTokens: (_m = usageInfo.outputTokens) != null ? _m : 0,
2244
+ totalTokens: (_n = usageInfo.totalTokens) != null ? _n : 0,
2245
+ cost: (_o = response.usage) == null ? void 0 : _o.cost,
2246
+ promptTokensDetails: {
2247
+ cachedTokens: (_r = (_q = (_p = response.usage) == null ? void 0 : _p.prompt_tokens_details) == null ? void 0 : _q.cached_tokens) != null ? _r : 0
2248
+ },
2249
+ completionTokensDetails: {
2250
+ reasoningTokens: (_u = (_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? _u : 0
2251
+ },
2252
+ costDetails: {
2253
+ upstreamInferenceCost: (_x = (_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? _x : 0
2254
+ }
2255
+ }
2256
+ }
2257
+ },
2258
+ request: { body: args },
2259
+ response: {
2260
+ id: response.id,
2261
+ modelId: response.model,
2262
+ headers: responseHeaders
2263
+ }
2264
+ };
2265
+ }
2266
+ async doStream(options) {
2267
+ var _a15;
2268
+ const providerOptions = options.providerOptions || {};
2269
+ const openrouterOptions = providerOptions.openrouter || {};
2270
+ const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2271
+ const { value: response, responseHeaders } = await postJsonToApi2({
2272
+ url: this.config.url({
2273
+ path: "/chat/completions",
2274
+ modelId: this.modelId
2275
+ }),
2276
+ headers: combineHeaders2(this.config.headers(), options.headers),
2277
+ body: __spreadProps(__spreadValues({}, args), {
2278
+ stream: true,
2279
+ // only include stream_options when in strict compatibility mode:
2280
+ stream_options: this.config.compatibility === "strict" ? __spreadValues({
2281
+ include_usage: true
2282
+ }, ((_a15 = this.settings.usage) == null ? void 0 : _a15.include) ? { include_usage: true } : {}) : void 0
2283
+ }),
2284
+ failedResponseHandler: openrouterFailedResponseHandler,
2285
+ successfulResponseHandler: createEventSourceResponseHandler2(
2286
+ OpenRouterStreamChatCompletionChunkSchema
2287
+ ),
2288
+ abortSignal: options.abortSignal,
2289
+ fetch: this.config.fetch
2290
+ });
2291
+ const toolCalls = [];
2292
+ let finishReason = "other";
2293
+ const usage = {
2294
+ inputTokens: Number.NaN,
2295
+ outputTokens: Number.NaN,
2296
+ totalTokens: Number.NaN,
2297
+ reasoningTokens: Number.NaN,
2298
+ cachedInputTokens: Number.NaN
2299
+ };
2300
+ const openrouterUsage = {};
2301
+ let textStarted = false;
2302
+ let reasoningStarted = false;
2303
+ let textId;
2304
+ let reasoningId;
2305
+ let openrouterResponseId;
2306
+ let provider;
2307
+ return {
2308
+ stream: response.pipeThrough(
2309
+ new TransformStream({
2310
+ transform(chunk, controller) {
2311
+ var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2312
+ if (!chunk.success) {
2313
+ finishReason = "error";
2314
+ controller.enqueue({ type: "error", error: chunk.error });
2315
+ return;
2316
+ }
2317
+ const value = chunk.value;
2318
+ if ("error" in value) {
2319
+ finishReason = "error";
2320
+ controller.enqueue({ type: "error", error: value.error });
2321
+ return;
2322
+ }
2323
+ if (value.provider) {
2324
+ provider = value.provider;
2325
+ }
2326
+ if (value.id) {
2327
+ openrouterResponseId = value.id;
2328
+ controller.enqueue({
2329
+ type: "response-metadata",
2330
+ id: value.id
2331
+ });
2332
+ }
2333
+ if (value.model) {
2334
+ controller.enqueue({
2335
+ type: "response-metadata",
2336
+ modelId: value.model
2337
+ });
2338
+ }
2339
+ if (value.usage != null) {
2340
+ usage.inputTokens = value.usage.prompt_tokens;
2341
+ usage.outputTokens = value.usage.completion_tokens;
2342
+ usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
2343
+ openrouterUsage.promptTokens = value.usage.prompt_tokens;
2344
+ if (value.usage.prompt_tokens_details) {
2345
+ const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
2346
+ usage.cachedInputTokens = cachedInputTokens;
2347
+ openrouterUsage.promptTokensDetails = {
2348
+ cachedTokens: cachedInputTokens
2349
+ };
2350
+ }
2351
+ openrouterUsage.completionTokens = value.usage.completion_tokens;
2352
+ if (value.usage.completion_tokens_details) {
2353
+ const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
2354
+ usage.reasoningTokens = reasoningTokens;
2355
+ openrouterUsage.completionTokensDetails = {
2356
+ reasoningTokens
2357
+ };
2358
+ }
2359
+ openrouterUsage.cost = value.usage.cost;
2360
+ openrouterUsage.totalTokens = value.usage.total_tokens;
2361
+ }
2362
+ const choice = value.choices[0];
2363
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
2364
+ finishReason = mapOpenRouterFinishReason(choice.finish_reason);
2365
+ }
2366
+ if ((choice == null ? void 0 : choice.delta) == null) {
2367
+ return;
2368
+ }
2369
+ const delta = choice.delta;
2370
+ const emitReasoningChunk = (chunkText) => {
2371
+ if (!reasoningStarted) {
2372
+ reasoningId = openrouterResponseId || generateId2();
2373
+ controller.enqueue({
2374
+ type: "reasoning-start",
2375
+ id: reasoningId
2376
+ });
2377
+ reasoningStarted = true;
2378
+ }
2379
+ controller.enqueue({
2380
+ type: "reasoning-delta",
2381
+ delta: chunkText,
2382
+ id: reasoningId || generateId2()
2383
+ });
2384
+ };
2385
+ if (delta.reasoning_details && delta.reasoning_details.length > 0) {
2386
+ for (const detail of delta.reasoning_details) {
2387
+ switch (detail.type) {
2388
+ case "reasoning.text": {
2389
+ if (detail.text) {
2390
+ emitReasoningChunk(detail.text);
2391
+ }
2392
+ break;
2393
+ }
2394
+ case "reasoning.encrypted": {
2395
+ if (detail.data) {
2396
+ emitReasoningChunk("[REDACTED]");
2397
+ }
2398
+ break;
2399
+ }
2400
+ case "reasoning.summary": {
2401
+ if (detail.summary) {
2402
+ emitReasoningChunk(detail.summary);
2403
+ }
2404
+ break;
2405
+ }
2406
+ }
2407
+ }
2408
+ } else if (delta.reasoning) {
2409
+ emitReasoningChunk(delta.reasoning);
2410
+ }
2411
+ if (delta.content) {
2412
+ if (reasoningStarted && !textStarted) {
2413
+ controller.enqueue({
2414
+ type: "reasoning-end",
2415
+ id: reasoningId || generateId2()
2416
+ });
2417
+ reasoningStarted = false;
2418
+ }
2419
+ if (!textStarted) {
2420
+ textId = openrouterResponseId || generateId2();
2421
+ controller.enqueue({
2422
+ type: "text-start",
2423
+ id: textId
2424
+ });
2425
+ textStarted = true;
2426
+ }
2427
+ controller.enqueue({
2428
+ type: "text-delta",
2429
+ delta: delta.content,
2430
+ id: textId || generateId2()
2431
+ });
2432
+ }
2433
+ if (delta.annotations) {
2434
+ for (const annotation of delta.annotations) {
2435
+ if (annotation.type === "url_citation") {
2436
+ controller.enqueue({
2437
+ type: "source",
2438
+ sourceType: "url",
2439
+ id: annotation.url_citation.url,
2440
+ url: annotation.url_citation.url,
2441
+ title: annotation.url_citation.title,
2442
+ providerMetadata: {
2443
+ openrouter: {
2444
+ content: annotation.url_citation.content || ""
2445
+ }
2446
+ }
2447
+ });
2448
+ }
2449
+ }
2450
+ }
2451
+ if (delta.tool_calls != null) {
2452
+ for (const toolCallDelta of delta.tool_calls) {
2453
+ const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length - 1;
2454
+ if (toolCalls[index] == null) {
2455
+ if (toolCallDelta.type !== "function") {
2456
+ throw new InvalidResponseDataError({
2457
+ data: toolCallDelta,
2458
+ message: `Expected 'function' type.`
2459
+ });
2460
+ }
2461
+ if (toolCallDelta.id == null) {
2462
+ throw new InvalidResponseDataError({
2463
+ data: toolCallDelta,
2464
+ message: `Expected 'id' to be a string.`
2465
+ });
2466
+ }
2467
+ if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
2468
+ throw new InvalidResponseDataError({
2469
+ data: toolCallDelta,
2470
+ message: `Expected 'function.name' to be a string.`
2471
+ });
2472
+ }
2473
+ toolCalls[index] = {
2474
+ id: toolCallDelta.id,
2475
+ type: "function",
2476
+ function: {
2477
+ name: toolCallDelta.function.name,
2478
+ arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
2479
+ },
2480
+ inputStarted: false,
2481
+ sent: false
2482
+ };
2483
+ const toolCall2 = toolCalls[index];
2484
+ if (toolCall2 == null) {
2485
+ throw new Error("Tool call is missing");
2486
+ }
2487
+ if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2488
+ toolCall2.inputStarted = true;
2489
+ controller.enqueue({
2490
+ type: "tool-input-start",
2491
+ id: toolCall2.id,
2492
+ toolName: toolCall2.function.name
2493
+ });
2494
+ controller.enqueue({
2495
+ type: "tool-input-delta",
2496
+ id: toolCall2.id,
2497
+ delta: toolCall2.function.arguments
2498
+ });
2499
+ controller.enqueue({
2500
+ type: "tool-input-end",
2501
+ id: toolCall2.id
2502
+ });
2503
+ controller.enqueue({
2504
+ type: "tool-call",
2505
+ toolCallId: toolCall2.id,
2506
+ toolName: toolCall2.function.name,
2507
+ input: toolCall2.function.arguments
2508
+ });
2509
+ toolCall2.sent = true;
2510
+ }
2511
+ continue;
2512
+ }
2513
+ const toolCall = toolCalls[index];
2514
+ if (toolCall == null) {
2515
+ throw new Error("Tool call is missing");
2516
+ }
2517
+ if (!toolCall.inputStarted) {
2518
+ toolCall.inputStarted = true;
2519
+ controller.enqueue({
2520
+ type: "tool-input-start",
2521
+ id: toolCall.id,
2522
+ toolName: toolCall.function.name
2523
+ });
2524
+ }
2525
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
2526
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
2527
+ }
2528
+ controller.enqueue({
2529
+ type: "tool-input-delta",
2530
+ id: toolCall.id,
2531
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
2532
+ });
2533
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2534
+ controller.enqueue({
2535
+ type: "tool-call",
2536
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId2(),
2537
+ toolName: toolCall.function.name,
2538
+ input: toolCall.function.arguments
2539
+ });
2540
+ toolCall.sent = true;
2541
+ }
2542
+ }
2543
+ }
2544
+ if (delta.images != null) {
2545
+ for (const image of delta.images) {
2546
+ controller.enqueue({
2547
+ type: "file",
2548
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
2549
+ data: getBase64FromDataUrl(image.image_url.url)
2550
+ });
2551
+ }
2552
+ }
2553
+ },
2554
+ flush(controller) {
2555
+ var _a16;
2556
+ if (finishReason === "tool-calls") {
2557
+ for (const toolCall of toolCalls) {
2558
+ if (toolCall && !toolCall.sent) {
2559
+ controller.enqueue({
2560
+ type: "tool-call",
2561
+ toolCallId: (_a16 = toolCall.id) != null ? _a16 : generateId2(),
2562
+ toolName: toolCall.function.name,
2563
+ // Coerce invalid arguments to an empty JSON object
2564
+ input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}"
2565
+ });
2566
+ toolCall.sent = true;
2567
+ }
2568
+ }
2569
+ }
2570
+ if (reasoningStarted) {
2571
+ controller.enqueue({
2572
+ type: "reasoning-end",
2573
+ id: reasoningId || generateId2()
2574
+ });
2575
+ }
2576
+ if (textStarted) {
2577
+ controller.enqueue({
2578
+ type: "text-end",
2579
+ id: textId || generateId2()
2580
+ });
2581
+ }
2582
+ const openrouterMetadata = {
2583
+ usage: openrouterUsage
2584
+ };
2585
+ if (provider !== void 0) {
2586
+ openrouterMetadata.provider = provider;
2587
+ }
2588
+ controller.enqueue({
2589
+ type: "finish",
2590
+ finishReason,
2591
+ usage,
2592
+ providerMetadata: {
2593
+ openrouter: openrouterMetadata
2594
+ }
2595
+ });
2596
+ }
2597
+ })
2598
+ ),
2599
+ warnings: [],
2600
+ request: { body: args },
2601
+ response: { headers: responseHeaders }
2602
+ };
2603
+ }
2604
+ };
2605
+ function convertToOpenRouterCompletionPrompt({
2606
+ prompt,
2607
+ inputFormat,
2608
+ user = "user",
2609
+ assistant = "assistant"
2610
+ }) {
2611
+ if (prompt.length === 1 && prompt[0] && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0] && prompt[0].content[0].type === "text") {
2612
+ return { prompt: prompt[0].content[0].text };
2613
+ }
2614
+ let text = "";
2615
+ if (prompt[0] && prompt[0].role === "system") {
2616
+ text += `${prompt[0].content}
2617
+
2618
+ `;
2619
+ prompt = prompt.slice(1);
2620
+ }
2621
+ for (const { role, content } of prompt) {
2622
+ switch (role) {
2623
+ case "system": {
2624
+ throw new InvalidPromptError({
2625
+ message: `Unexpected system message in prompt: ${content}`,
2626
+ prompt
2627
+ });
2628
+ }
2629
+ case "user": {
2630
+ const userMessage = content.map((part) => {
2631
+ switch (part.type) {
2632
+ case "text": {
2633
+ return part.text;
2634
+ }
2635
+ case "file": {
2636
+ throw new UnsupportedFunctionalityError3({
2637
+ functionality: "file attachments"
2638
+ });
2639
+ }
2640
+ default: {
2641
+ return "";
2642
+ }
2643
+ }
2644
+ }).join("");
2645
+ text += `${user}:
2646
+ ${userMessage}
2647
+
2648
+ `;
2649
+ break;
2650
+ }
2651
+ case "assistant": {
2652
+ const assistantMessage = content.map(
2653
+ (part) => {
2654
+ switch (part.type) {
2655
+ case "text": {
2656
+ return part.text;
2657
+ }
2658
+ case "tool-call": {
2659
+ throw new UnsupportedFunctionalityError3({
2660
+ functionality: "tool-call messages"
2661
+ });
2662
+ }
2663
+ case "tool-result": {
2664
+ throw new UnsupportedFunctionalityError3({
2665
+ functionality: "tool-result messages"
2666
+ });
2667
+ }
2668
+ case "reasoning": {
2669
+ throw new UnsupportedFunctionalityError3({
2670
+ functionality: "reasoning messages"
2671
+ });
2672
+ }
2673
+ case "file": {
2674
+ throw new UnsupportedFunctionalityError3({
2675
+ functionality: "file attachments"
2676
+ });
2677
+ }
2678
+ default: {
2679
+ return "";
2680
+ }
2681
+ }
2682
+ }
2683
+ ).join("");
2684
+ text += `${assistant}:
2685
+ ${assistantMessage}
2686
+
2687
+ `;
2688
+ break;
2689
+ }
2690
+ case "tool": {
2691
+ throw new UnsupportedFunctionalityError3({
2692
+ functionality: "tool messages"
2693
+ });
2694
+ }
2695
+ }
2696
+ }
2697
+ text += `${assistant}:
2698
+ `;
2699
+ return {
2700
+ prompt: text
2701
+ };
2702
+ }
2703
+ var OpenRouterCompletionChunkSchema = z.union([
2704
+ z.object({
2705
+ id: z.string().optional(),
2706
+ model: z.string().optional(),
2707
+ choices: z.array(
2708
+ z.object({
2709
+ text: z.string(),
2710
+ reasoning: z.string().nullish().optional(),
2711
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
2712
+ finish_reason: z.string().nullish(),
2713
+ index: z.number().nullish(),
2714
+ logprobs: z.object({
2715
+ tokens: z.array(z.string()),
2716
+ token_logprobs: z.array(z.number()),
2717
+ top_logprobs: z.array(z.record(z.string(), z.number())).nullable()
2718
+ }).nullable().optional()
2719
+ })
2720
+ ),
2721
+ usage: z.object({
2722
+ prompt_tokens: z.number(),
2723
+ prompt_tokens_details: z.object({
2724
+ cached_tokens: z.number()
2725
+ }).nullish(),
2726
+ completion_tokens: z.number(),
2727
+ completion_tokens_details: z.object({
2728
+ reasoning_tokens: z.number()
2729
+ }).nullish(),
2730
+ total_tokens: z.number(),
2731
+ cost: z.number().optional()
2732
+ }).nullish()
2733
+ }),
2734
+ OpenRouterErrorResponseSchema
2735
+ ]);
2736
+ var OpenRouterCompletionLanguageModel = class {
2737
+ constructor(modelId, settings, config) {
2738
+ this.specificationVersion = "v2";
2739
+ this.provider = "openrouter";
2740
+ this.supportedUrls = {
2741
+ "image/*": [
2742
+ /^data:image\/[a-zA-Z]+;base64,/,
2743
+ /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
2744
+ ],
2745
+ "text/*": [/^data:text\//, /^https?:\/\/.+$/],
2746
+ "application/*": [/^data:application\//, /^https?:\/\/.+$/]
2747
+ };
2748
+ this.defaultObjectGenerationMode = void 0;
2749
+ this.modelId = modelId;
2750
+ this.settings = settings;
2751
+ this.config = config;
2752
+ }
2753
+ getArgs({
2754
+ prompt,
2755
+ maxOutputTokens,
2756
+ temperature,
2757
+ topP,
2758
+ frequencyPenalty,
2759
+ presencePenalty,
2760
+ seed,
2761
+ responseFormat,
2762
+ topK,
2763
+ stopSequences,
2764
+ tools,
2765
+ toolChoice
2766
+ }) {
2767
+ const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
2768
+ prompt,
2769
+ inputFormat: "prompt"
2770
+ });
2771
+ if (tools == null ? void 0 : tools.length) {
2772
+ throw new UnsupportedFunctionalityError3({
2773
+ functionality: "tools"
2774
+ });
2775
+ }
2776
+ if (toolChoice) {
2777
+ throw new UnsupportedFunctionalityError3({
2778
+ functionality: "toolChoice"
2779
+ });
2780
+ }
2781
+ return __spreadValues(__spreadValues({
2782
+ // model id:
2783
+ model: this.modelId,
2784
+ models: this.settings.models,
2785
+ // model specific settings:
2786
+ logit_bias: this.settings.logitBias,
2787
+ logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
2788
+ suffix: this.settings.suffix,
2789
+ user: this.settings.user,
2790
+ // standardized settings:
2791
+ max_tokens: maxOutputTokens,
2792
+ temperature,
2793
+ top_p: topP,
2794
+ frequency_penalty: frequencyPenalty,
2795
+ presence_penalty: presencePenalty,
2796
+ seed,
2797
+ stop: stopSequences,
2798
+ response_format: responseFormat,
2799
+ top_k: topK,
2800
+ // prompt:
2801
+ prompt: completionPrompt,
2802
+ // OpenRouter specific settings:
2803
+ include_reasoning: this.settings.includeReasoning,
2804
+ reasoning: this.settings.reasoning
2805
+ }, this.config.extraBody), this.settings.extraBody);
2806
+ }
2807
+ async doGenerate(options) {
2808
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
2809
+ const providerOptions = options.providerOptions || {};
2810
+ const openrouterOptions = providerOptions.openrouter || {};
2811
+ const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2812
+ const { value: response, responseHeaders } = await postJsonToApi2({
2813
+ url: this.config.url({
2814
+ path: "/completions",
2815
+ modelId: this.modelId
2816
+ }),
2817
+ headers: combineHeaders2(this.config.headers(), options.headers),
2818
+ body: args,
2819
+ failedResponseHandler: openrouterFailedResponseHandler,
2820
+ successfulResponseHandler: createJsonResponseHandler2(
2821
+ OpenRouterCompletionChunkSchema
2822
+ ),
2823
+ abortSignal: options.abortSignal,
2824
+ fetch: this.config.fetch
2825
+ });
2826
+ if ("error" in response) {
2827
+ throw new Error(`${response.error.message}`);
2828
+ }
2829
+ const choice = response.choices[0];
2830
+ if (!choice) {
2831
+ throw new Error("No choice in OpenRouter completion response");
2832
+ }
2833
+ return {
2834
+ content: [
2835
+ {
2836
+ type: "text",
2837
+ text: (_a15 = choice.text) != null ? _a15 : ""
2838
+ }
2839
+ ],
2840
+ finishReason: mapOpenRouterFinishReason(choice.finish_reason),
2841
+ usage: {
2842
+ inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : 0,
2843
+ outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : 0,
2844
+ totalTokens: ((_g = (_f = response.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : 0) + ((_i = (_h = response.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : 0),
2845
+ reasoningTokens: (_l = (_k = (_j = response.usage) == null ? void 0 : _j.completion_tokens_details) == null ? void 0 : _k.reasoning_tokens) != null ? _l : 0,
2846
+ cachedInputTokens: (_o = (_n = (_m = response.usage) == null ? void 0 : _m.prompt_tokens_details) == null ? void 0 : _n.cached_tokens) != null ? _o : 0
2847
+ },
2848
+ warnings: [],
2849
+ response: {
2850
+ headers: responseHeaders
2851
+ }
2852
+ };
2853
+ }
2854
+ async doStream(options) {
2855
+ const providerOptions = options.providerOptions || {};
2856
+ const openrouterOptions = providerOptions.openrouter || {};
2857
+ const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2858
+ const { value: response, responseHeaders } = await postJsonToApi2({
2859
+ url: this.config.url({
2860
+ path: "/completions",
2861
+ modelId: this.modelId
2862
+ }),
2863
+ headers: combineHeaders2(this.config.headers(), options.headers),
2864
+ body: __spreadProps(__spreadValues({}, args), {
2865
+ stream: true,
2866
+ // only include stream_options when in strict compatibility mode:
2867
+ stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
2868
+ }),
2869
+ failedResponseHandler: openrouterFailedResponseHandler,
2870
+ successfulResponseHandler: createEventSourceResponseHandler2(
2871
+ OpenRouterCompletionChunkSchema
2872
+ ),
2873
+ abortSignal: options.abortSignal,
2874
+ fetch: this.config.fetch
2875
+ });
2876
+ let finishReason = "other";
2877
+ const usage = {
2878
+ inputTokens: Number.NaN,
2879
+ outputTokens: Number.NaN,
2880
+ totalTokens: Number.NaN,
2881
+ reasoningTokens: Number.NaN,
2882
+ cachedInputTokens: Number.NaN
2883
+ };
2884
+ const openrouterUsage = {};
2885
+ return {
2886
+ stream: response.pipeThrough(
2887
+ new TransformStream({
2888
+ transform(chunk, controller) {
2889
+ var _a15, _b;
2890
+ if (!chunk.success) {
2891
+ finishReason = "error";
2892
+ controller.enqueue({ type: "error", error: chunk.error });
2893
+ return;
2894
+ }
2895
+ const value = chunk.value;
2896
+ if ("error" in value) {
2897
+ finishReason = "error";
2898
+ controller.enqueue({ type: "error", error: value.error });
2899
+ return;
2900
+ }
2901
+ if (value.usage != null) {
2902
+ usage.inputTokens = value.usage.prompt_tokens;
2903
+ usage.outputTokens = value.usage.completion_tokens;
2904
+ usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
2905
+ openrouterUsage.promptTokens = value.usage.prompt_tokens;
2906
+ if (value.usage.prompt_tokens_details) {
2907
+ const cachedInputTokens = (_a15 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a15 : 0;
2908
+ usage.cachedInputTokens = cachedInputTokens;
2909
+ openrouterUsage.promptTokensDetails = {
2910
+ cachedTokens: cachedInputTokens
2911
+ };
2912
+ }
2913
+ openrouterUsage.completionTokens = value.usage.completion_tokens;
2914
+ if (value.usage.completion_tokens_details) {
2915
+ const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
2916
+ usage.reasoningTokens = reasoningTokens;
2917
+ openrouterUsage.completionTokensDetails = {
2918
+ reasoningTokens
2919
+ };
2920
+ }
2921
+ openrouterUsage.cost = value.usage.cost;
2922
+ openrouterUsage.totalTokens = value.usage.total_tokens;
2923
+ }
2924
+ const choice = value.choices[0];
2925
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
2926
+ finishReason = mapOpenRouterFinishReason(choice.finish_reason);
2927
+ }
2928
+ if ((choice == null ? void 0 : choice.text) != null) {
2929
+ controller.enqueue({
2930
+ type: "text-delta",
2931
+ delta: choice.text,
2932
+ id: generateId2()
2933
+ });
2934
+ }
2935
+ },
2936
+ flush(controller) {
2937
+ controller.enqueue({
2938
+ type: "finish",
2939
+ finishReason,
2940
+ usage,
2941
+ providerMetadata: {
2942
+ openrouter: {
2943
+ usage: openrouterUsage
2944
+ }
2945
+ }
2946
+ });
2947
+ }
2948
+ })
2949
+ ),
2950
+ response: {
2951
+ headers: responseHeaders
2952
+ }
2953
+ };
2954
+ }
2955
+ };
2956
+ function createOpenRouter(options = {}) {
2957
+ var _a15, _b, _c;
2958
+ const baseURL = (_b = withoutTrailingSlash2((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2959
+ const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
2960
+ const getHeaders = () => __spreadValues({
2961
+ Authorization: `Bearer ${loadApiKey2({
2962
+ apiKey: options.apiKey,
2963
+ environmentVariableName: "OPENROUTER_API_KEY",
2964
+ description: "OpenRouter"
2965
+ })}`
2966
+ }, options.headers);
2967
+ const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
2968
+ provider: "openrouter.chat",
2969
+ url: ({ path }) => `${baseURL}${path}`,
2970
+ headers: getHeaders,
2971
+ compatibility,
2972
+ fetch: options.fetch,
2973
+ extraBody: options.extraBody
2974
+ });
2975
+ const createCompletionModel = (modelId, settings = {}) => new OpenRouterCompletionLanguageModel(modelId, settings, {
2976
+ provider: "openrouter.completion",
2977
+ url: ({ path }) => `${baseURL}${path}`,
2978
+ headers: getHeaders,
2979
+ compatibility,
2980
+ fetch: options.fetch,
2981
+ extraBody: options.extraBody
2982
+ });
2983
+ const createLanguageModel = (modelId, settings) => {
2984
+ if (new.target) {
2985
+ throw new Error(
2986
+ "The OpenRouter model function cannot be called with the new keyword."
2987
+ );
2988
+ }
2989
+ if (modelId === "openai/gpt-3.5-turbo-instruct") {
2990
+ return createCompletionModel(
2991
+ modelId,
2992
+ settings
2993
+ );
2994
+ }
2995
+ return createChatModel(modelId, settings);
2996
+ };
2997
+ const provider = (modelId, settings) => createLanguageModel(modelId, settings);
2998
+ provider.languageModel = createLanguageModel;
2999
+ provider.chat = createChatModel;
3000
+ provider.completion = createCompletionModel;
3001
+ return provider;
3002
+ }
3003
+ createOpenRouter({
3004
+ compatibility: "strict"
3005
+ // strict for OpenRouter API
3006
+ });
3007
+
3008
+ // src/llm/model/gateway-resolver.ts
3009
+ function parseModelRouterId(routerId, gatewayPrefix) {
3010
+ if (gatewayPrefix && !routerId.startsWith(`${gatewayPrefix}/`)) {
3011
+ throw new Error(`Expected ${gatewayPrefix}/ in model router ID ${routerId}`);
3012
+ }
3013
+ const idParts = routerId.split("/");
3014
+ if (gatewayPrefix && idParts.length < 3) {
3015
+ throw new Error(
3016
+ `Expected atleast 3 id parts ${gatewayPrefix}/provider/model, but only saw ${idParts.length} in ${routerId}`
3017
+ );
3018
+ }
3019
+ const providerId = idParts.at(gatewayPrefix ? 1 : 0);
3020
+ const modelId = idParts.slice(gatewayPrefix ? 2 : 1).join(`/`);
3021
+ if (!routerId.includes(`/`) || !providerId || !modelId) {
3022
+ throw new Error(
3023
+ `Attempted to parse provider/model from ${routerId} but this ID doesn't appear to contain a provider`
3024
+ );
3025
+ }
3026
+ return {
3027
+ providerId,
3028
+ modelId
3029
+ };
3030
+ }
3031
+
3032
+ // src/llm/model/gateways/constants.ts
3033
+ var PROVIDERS_WITH_INSTALLED_PACKAGES = ["anthropic", "google", "openai", "openrouter", "xai"];
3034
+ var EXCLUDED_PROVIDERS = ["github-copilot"];
3035
+
3036
+ // src/llm/model/gateways/models-dev.ts
3037
+ var OPENAI_COMPATIBLE_OVERRIDES = {
3038
+ cerebras: {
3039
+ url: "https://api.cerebras.ai/v1"
3040
+ },
3041
+ mistral: {
3042
+ url: "https://api.mistral.ai/v1"
3043
+ },
3044
+ groq: {
3045
+ url: "https://api.groq.com/openai/v1"
3046
+ },
3047
+ togetherai: {
3048
+ url: "https://api.together.xyz/v1"
3049
+ },
3050
+ deepinfra: {
3051
+ url: "https://api.deepinfra.com/v1/openai"
3052
+ },
3053
+ perplexity: {
3054
+ url: "https://api.perplexity.ai"
3055
+ },
3056
+ vercel: {
3057
+ url: "https://ai-gateway.vercel.sh/v1",
3058
+ apiKeyEnvVar: "AI_GATEWAY_API_KEY"
3059
+ }
3060
+ };
3061
+ var ModelsDevGateway = class extends MastraModelGateway {
3062
+ name = "models.dev";
3063
+ prefix = void 0;
3064
+ // No prefix for registry gateway
3065
+ providerConfigs = {};
3066
+ constructor(providerConfigs) {
3067
+ super();
3068
+ if (providerConfigs) this.providerConfigs = providerConfigs;
3069
+ }
3070
+ async fetchProviders() {
3071
+ const response = await fetch("https://models.dev/api.json");
3072
+ if (!response.ok) {
3073
+ throw new Error(`Failed to fetch from models.dev: ${response.statusText}`);
3074
+ }
3075
+ const data = await response.json();
3076
+ const providerConfigs = {};
3077
+ for (const [providerId, providerInfo] of Object.entries(data)) {
3078
+ if (EXCLUDED_PROVIDERS.includes(providerId)) continue;
3079
+ if (!providerInfo || typeof providerInfo !== "object" || !providerInfo.models) continue;
3080
+ const normalizedId = providerId;
3081
+ const isOpenAICompatible = providerInfo.npm === "@ai-sdk/openai-compatible" || providerInfo.npm === "@ai-sdk/gateway" || // Vercel AI Gateway is OpenAI-compatible
3082
+ normalizedId in OPENAI_COMPATIBLE_OVERRIDES;
3083
+ const hasInstalledPackage = PROVIDERS_WITH_INSTALLED_PACKAGES.includes(providerId);
3084
+ const hasApiAndEnv = providerInfo.api && providerInfo.env && providerInfo.env.length > 0;
3085
+ if (isOpenAICompatible || hasInstalledPackage || hasApiAndEnv) {
3086
+ const modelIds = Object.keys(providerInfo.models).sort();
3087
+ const url = providerInfo.api || OPENAI_COMPATIBLE_OVERRIDES[normalizedId]?.url;
3088
+ if (!hasInstalledPackage && !url) {
3089
+ continue;
3090
+ }
3091
+ const apiKeyEnvVar = providerInfo.env?.[0] || `${normalizedId.toUpperCase().replace(/-/g, "_")}_API_KEY`;
3092
+ const apiKeyHeader = !hasInstalledPackage ? OPENAI_COMPATIBLE_OVERRIDES[normalizedId]?.apiKeyHeader || "Authorization" : void 0;
3093
+ providerConfigs[normalizedId] = {
3094
+ url,
3095
+ apiKeyEnvVar,
3096
+ apiKeyHeader,
3097
+ name: providerInfo.name || providerId.charAt(0).toUpperCase() + providerId.slice(1),
3098
+ models: modelIds,
3099
+ docUrl: providerInfo.doc,
3100
+ // Include documentation URL if available
3101
+ gateway: `models.dev`
3102
+ };
3103
+ }
3104
+ }
3105
+ this.providerConfigs = providerConfigs;
3106
+ return providerConfigs;
3107
+ }
3108
+ buildUrl(routerId, envVars) {
3109
+ const { providerId } = parseModelRouterId(routerId);
3110
+ const config = this.providerConfigs[providerId];
3111
+ if (!config?.url) {
3112
+ return;
3113
+ }
3114
+ const baseUrlEnvVar = `${providerId.toUpperCase().replace(/-/g, "_")}_BASE_URL`;
3115
+ const customBaseUrl = envVars?.[baseUrlEnvVar] || process.env[baseUrlEnvVar];
3116
+ return customBaseUrl || config.url;
3117
+ }
3118
+ getApiKey(modelId) {
3119
+ const [provider, model] = modelId.split("/");
3120
+ if (!provider || !model) {
3121
+ throw new Error(`Could not identify provider from model id ${modelId}`);
3122
+ }
3123
+ const config = this.providerConfigs[provider];
3124
+ if (!config) {
3125
+ throw new Error(`Could not find config for provider ${provider} with model id ${modelId}`);
3126
+ }
3127
+ const apiKey = typeof config.apiKeyEnvVar === `string` ? process.env[config.apiKeyEnvVar] : void 0;
3128
+ if (!apiKey) {
3129
+ throw new Error(`Could not find API key process.env.${config.apiKeyEnvVar} for model id ${modelId}`);
3130
+ }
3131
+ return Promise.resolve(apiKey);
3132
+ }
3133
+ async resolveLanguageModel({
3134
+ modelId,
3135
+ providerId,
3136
+ apiKey
3137
+ }) {
3138
+ const baseURL = this.buildUrl(`${providerId}/${modelId}`);
3139
+ switch (providerId) {
3140
+ case "openai":
3141
+ return createOpenAI({ apiKey }).responses(modelId);
3142
+ case "gemini":
3143
+ case "google":
3144
+ return createGoogleGenerativeAI({
3145
+ apiKey
3146
+ }).chat(modelId);
3147
+ case "anthropic":
3148
+ return createAnthropic({ apiKey })(modelId);
3149
+ case "openrouter":
3150
+ return createOpenRouter({ apiKey })(modelId);
3151
+ case "xai":
3152
+ return createXai({
3153
+ apiKey
3154
+ })(modelId);
3155
+ default:
3156
+ if (!baseURL) throw new Error(`No API URL found for ${providerId}/${modelId}`);
3157
+ return createOpenAICompatible({ name: providerId, apiKey, baseURL }).chatModel(modelId);
3158
+ }
3159
+ }
3160
+ };
3161
+
3162
+ export { ModelsDevGateway, parseModelRouterId };
3163
+ //# sourceMappingURL=chunk-BLPOOPRL.js.map
3164
+ //# sourceMappingURL=chunk-BLPOOPRL.js.map