@mastra/core 1.0.0-beta.3 → 1.0.0-beta.5

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 +369 -0
  2. package/dist/agent/agent.d.ts +2 -2
  3. package/dist/agent/agent.d.ts.map +1 -1
  4. package/dist/agent/agent.types.d.ts +2 -0
  5. package/dist/agent/agent.types.d.ts.map +1 -1
  6. package/dist/agent/index.cjs +9 -9
  7. package/dist/agent/index.js +2 -2
  8. package/dist/agent/message-list/index.cjs +3 -3
  9. package/dist/agent/message-list/index.d.ts +5 -0
  10. package/dist/agent/message-list/index.d.ts.map +1 -1
  11. package/dist/agent/message-list/index.js +1 -1
  12. package/dist/agent/message-list/prompt/attachments-to-parts.d.ts.map +1 -1
  13. package/dist/agent/types.d.ts +2 -3
  14. package/dist/agent/types.d.ts.map +1 -1
  15. package/dist/agent/utils.d.ts.map +1 -1
  16. package/dist/agent/workflows/prepare-stream/index.d.ts.map +1 -1
  17. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts.map +1 -1
  18. package/dist/agent/workflows/prepare-stream/stream-step.d.ts +8 -1
  19. package/dist/agent/workflows/prepare-stream/stream-step.d.ts.map +1 -1
  20. package/dist/{chunk-4DWZ4Z6H.js → chunk-3B2OPLGG.js} +13 -7
  21. package/dist/chunk-3B2OPLGG.js.map +1 -0
  22. package/dist/{chunk-GRGPQ32U.js → chunk-3ZQ7LX73.js} +13 -13
  23. package/dist/chunk-3ZQ7LX73.js.map +1 -0
  24. package/dist/{chunk-VZGBVYXA.cjs → chunk-556MJ7CL.cjs} +33 -27
  25. package/dist/chunk-556MJ7CL.cjs.map +1 -0
  26. package/dist/{chunk-HGNRQ3OG.js → chunk-5O52O25J.js} +15 -8
  27. package/dist/chunk-5O52O25J.js.map +1 -0
  28. package/dist/{chunk-VU6DVS7J.js → chunk-5SA2EZ33.js} +421 -29
  29. package/dist/chunk-5SA2EZ33.js.map +1 -0
  30. package/dist/chunk-6XCINXZ7.cjs +194 -0
  31. package/dist/chunk-6XCINXZ7.cjs.map +1 -0
  32. package/dist/{chunk-KOSW5PP5.js → chunk-7ZADRRDW.js} +466 -125
  33. package/dist/chunk-7ZADRRDW.js.map +1 -0
  34. package/dist/{chunk-JXESKY4A.js → chunk-B5J5HYDN.js} +7 -5
  35. package/dist/chunk-B5J5HYDN.js.map +1 -0
  36. package/dist/chunk-BWYU7D33.js +192 -0
  37. package/dist/chunk-BWYU7D33.js.map +1 -0
  38. package/dist/{chunk-MCUX2D5Q.js → chunk-D7CJ4HIQ.js} +263 -24
  39. package/dist/chunk-D7CJ4HIQ.js.map +1 -0
  40. package/dist/{chunk-G36A2JRR.cjs → chunk-DQIZ5FFX.cjs} +457 -326
  41. package/dist/chunk-DQIZ5FFX.cjs.map +1 -0
  42. package/dist/{chunk-ZPMFINU2.cjs → chunk-HNHZGFZY.cjs} +466 -131
  43. package/dist/chunk-HNHZGFZY.cjs.map +1 -0
  44. package/dist/{chunk-3VOUB4ZU.cjs → chunk-IITXXVYI.cjs} +17 -17
  45. package/dist/chunk-IITXXVYI.cjs.map +1 -0
  46. package/dist/{chunk-QUKUN6NR.cjs → chunk-ISMGVGUM.cjs} +105 -5
  47. package/dist/chunk-ISMGVGUM.cjs.map +1 -0
  48. package/dist/{chunk-OQF4H5Y2.js → chunk-KJIQGPQR.js} +4 -4
  49. package/dist/{chunk-OQF4H5Y2.js.map → chunk-KJIQGPQR.js.map} +1 -1
  50. package/dist/{chunk-OWX2PUFH.cjs → chunk-KP42JLXE.cjs} +506 -236
  51. package/dist/chunk-KP42JLXE.cjs.map +1 -0
  52. package/dist/{chunk-4RSHBKDJ.cjs → chunk-KWWD3U7G.cjs} +5 -5
  53. package/dist/chunk-KWWD3U7G.cjs.map +1 -0
  54. package/dist/{chunk-N4SJ4YX7.cjs → chunk-NHP6ZIDG.cjs} +271 -31
  55. package/dist/chunk-NHP6ZIDG.cjs.map +1 -0
  56. package/dist/{chunk-UIZSWUKP.js → chunk-NZAXAFI3.js} +104 -6
  57. package/dist/chunk-NZAXAFI3.js.map +1 -0
  58. package/dist/{chunk-O6NA3Z43.cjs → chunk-OUUPUAGA.cjs} +10 -8
  59. package/dist/chunk-OUUPUAGA.cjs.map +1 -0
  60. package/dist/{chunk-D6EDHNGV.js → chunk-PC6EKOWK.js} +64 -11
  61. package/dist/chunk-PC6EKOWK.js.map +1 -0
  62. package/dist/{chunk-YQ7NLZZ3.cjs → chunk-QGWNF2QJ.cjs} +74 -618
  63. package/dist/chunk-QGWNF2QJ.cjs.map +1 -0
  64. package/dist/{chunk-HBJPYQRN.cjs → chunk-RROQ46B6.cjs} +69 -16
  65. package/dist/chunk-RROQ46B6.cjs.map +1 -0
  66. package/dist/{chunk-G3OOCXAI.js → chunk-T2CJRA6E.js} +4 -4
  67. package/dist/chunk-T2CJRA6E.js.map +1 -0
  68. package/dist/{chunk-T3WZCEC4.js → chunk-T2UNO766.js} +47 -591
  69. package/dist/chunk-T2UNO766.js.map +1 -0
  70. package/dist/{chunk-EZVRSZMK.cjs → chunk-U3VE2EVB.cjs} +11 -11
  71. package/dist/{chunk-EZVRSZMK.cjs.map → chunk-U3VE2EVB.cjs.map} +1 -1
  72. package/dist/{chunk-4IKJAKCD.cjs → chunk-V537VSV4.cjs} +74 -16
  73. package/dist/chunk-V537VSV4.cjs.map +1 -0
  74. package/dist/{chunk-XRIVPHXV.cjs → chunk-VYJXTHII.cjs} +422 -30
  75. package/dist/chunk-VYJXTHII.cjs.map +1 -0
  76. package/dist/{chunk-LRSB62Z6.cjs → chunk-X7F4CSGR.cjs} +15 -8
  77. package/dist/chunk-X7F4CSGR.cjs.map +1 -0
  78. package/dist/{chunk-CKGIPST2.js → chunk-XBO6W7LZ.js} +462 -193
  79. package/dist/chunk-XBO6W7LZ.js.map +1 -0
  80. package/dist/{chunk-KEURQGCQ.js → chunk-XIDKHXNR.js} +74 -17
  81. package/dist/chunk-XIDKHXNR.js.map +1 -0
  82. package/dist/{chunk-JTXVR2RA.cjs → chunk-XJQX54QP.cjs} +5 -5
  83. package/dist/{chunk-JTXVR2RA.cjs.map → chunk-XJQX54QP.cjs.map} +1 -1
  84. package/dist/{chunk-BAMR7HKO.js → chunk-YDFX3JR2.js} +457 -326
  85. package/dist/chunk-YDFX3JR2.js.map +1 -0
  86. package/dist/{chunk-5CWWU22H.js → chunk-ZTTMSCLU.js} +3 -3
  87. package/dist/{chunk-5CWWU22H.js.map → chunk-ZTTMSCLU.js.map} +1 -1
  88. package/dist/evals/base.d.ts +1 -1
  89. package/dist/evals/base.d.ts.map +1 -1
  90. package/dist/evals/base.test-utils.d.ts +25 -25
  91. package/dist/evals/index.cjs +4 -4
  92. package/dist/evals/index.js +1 -1
  93. package/dist/evals/scoreTraces/index.cjs +3 -3
  94. package/dist/evals/scoreTraces/index.js +1 -1
  95. package/dist/index.cjs +2 -2
  96. package/dist/index.js +1 -1
  97. package/dist/integration/index.cjs +2 -2
  98. package/dist/integration/index.js +1 -1
  99. package/dist/llm/index.cjs +18 -14
  100. package/dist/llm/index.d.ts +2 -1
  101. package/dist/llm/index.d.ts.map +1 -1
  102. package/dist/llm/index.js +5 -5
  103. package/dist/llm/model/aisdk/v5/model.d.ts.map +1 -1
  104. package/dist/llm/model/gateway-resolver.d.ts.map +1 -1
  105. package/dist/llm/model/gateways/azure.d.ts +36 -0
  106. package/dist/llm/model/gateways/azure.d.ts.map +1 -0
  107. package/dist/llm/model/gateways/base.d.ts +3 -6
  108. package/dist/llm/model/gateways/base.d.ts.map +1 -1
  109. package/dist/llm/model/gateways/index.d.ts +4 -1
  110. package/dist/llm/model/gateways/index.d.ts.map +1 -1
  111. package/dist/llm/model/gateways/models-dev.d.ts +2 -2
  112. package/dist/llm/model/gateways/models-dev.d.ts.map +1 -1
  113. package/dist/llm/model/gateways/netlify.d.ts +2 -2
  114. package/dist/llm/model/gateways/netlify.d.ts.map +1 -1
  115. package/dist/llm/model/model.d.ts.map +1 -1
  116. package/dist/llm/model/model.loop.d.ts +1 -1
  117. package/dist/llm/model/model.loop.d.ts.map +1 -1
  118. package/dist/llm/model/provider-registry.d.ts.map +1 -1
  119. package/dist/llm/model/provider-types.generated.d.ts +192 -11
  120. package/dist/llm/model/registry-generator.d.ts +12 -0
  121. package/dist/llm/model/registry-generator.d.ts.map +1 -1
  122. package/dist/llm/model/router.d.ts.map +1 -1
  123. package/dist/loop/index.cjs +2 -2
  124. package/dist/loop/index.js +1 -1
  125. package/dist/loop/loop.d.ts.map +1 -1
  126. package/dist/loop/network/index.d.ts.map +1 -1
  127. package/dist/loop/test-utils/generateText.d.ts.map +1 -1
  128. package/dist/loop/test-utils/resultObject.d.ts.map +1 -1
  129. package/dist/loop/test-utils/streamObject.d.ts.map +1 -1
  130. package/dist/loop/test-utils/toUIMessageStream.d.ts.map +1 -1
  131. package/dist/loop/test-utils/utils.d.ts.map +1 -1
  132. package/dist/loop/types.d.ts +8 -0
  133. package/dist/loop/types.d.ts.map +1 -1
  134. package/dist/loop/workflows/agentic-execution/index.d.ts.map +1 -1
  135. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts.map +1 -1
  136. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts.map +1 -1
  137. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts +1 -1
  138. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts.map +1 -1
  139. package/dist/mastra/index.cjs +2 -2
  140. package/dist/mastra/index.d.ts +9 -9
  141. package/dist/mastra/index.d.ts.map +1 -1
  142. package/dist/mastra/index.js +1 -1
  143. package/dist/mcp/index.cjs.map +1 -1
  144. package/dist/mcp/index.d.ts +4 -4
  145. package/dist/mcp/index.d.ts.map +1 -1
  146. package/dist/mcp/index.js.map +1 -1
  147. package/dist/mcp/types.d.ts +2 -2
  148. package/dist/mcp/types.d.ts.map +1 -1
  149. package/dist/memory/index.cjs +89 -34
  150. package/dist/memory/index.cjs.map +1 -1
  151. package/dist/memory/index.js +85 -30
  152. package/dist/memory/index.js.map +1 -1
  153. package/dist/memory/mock.d.ts +9 -13
  154. package/dist/memory/mock.d.ts.map +1 -1
  155. package/dist/models-dev-F6MTIYTO.js +3 -0
  156. package/dist/{models-dev-GCVENVWA.js.map → models-dev-F6MTIYTO.js.map} +1 -1
  157. package/dist/models-dev-XIVR5EJV.cjs +12 -0
  158. package/dist/{models-dev-TIBJR6IG.cjs.map → models-dev-XIVR5EJV.cjs.map} +1 -1
  159. package/dist/netlify-MXBOGAJR.cjs +12 -0
  160. package/dist/{netlify-NTSNNT6F.cjs.map → netlify-MXBOGAJR.cjs.map} +1 -1
  161. package/dist/netlify-RX3JXCFQ.js +3 -0
  162. package/dist/{netlify-O5NJW7CF.js.map → netlify-RX3JXCFQ.js.map} +1 -1
  163. package/dist/processors/index.cjs +11 -11
  164. package/dist/processors/index.d.ts +2 -2
  165. package/dist/processors/index.d.ts.map +1 -1
  166. package/dist/processors/index.js +1 -1
  167. package/dist/processors/processors/batch-parts.d.ts +1 -1
  168. package/dist/processors/processors/batch-parts.d.ts.map +1 -1
  169. package/dist/processors/processors/language-detector.d.ts +1 -1
  170. package/dist/processors/processors/language-detector.d.ts.map +1 -1
  171. package/dist/processors/processors/moderation.d.ts +1 -1
  172. package/dist/processors/processors/moderation.d.ts.map +1 -1
  173. package/dist/processors/processors/pii-detector.d.ts +1 -1
  174. package/dist/processors/processors/pii-detector.d.ts.map +1 -1
  175. package/dist/processors/processors/prompt-injection-detector.d.ts +1 -1
  176. package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -1
  177. package/dist/processors/processors/structured-output.d.ts +1 -1
  178. package/dist/processors/processors/structured-output.d.ts.map +1 -1
  179. package/dist/processors/processors/system-prompt-scrubber.d.ts +1 -1
  180. package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -1
  181. package/dist/processors/processors/token-limiter.d.ts +1 -1
  182. package/dist/processors/processors/token-limiter.d.ts.map +1 -1
  183. package/dist/processors/processors/unicode-normalizer.d.ts +1 -1
  184. package/dist/processors/processors/unicode-normalizer.d.ts.map +1 -1
  185. package/dist/provider-registry-3LUCE7FT.js +3 -0
  186. package/dist/{provider-registry-74GMFZKT.js.map → provider-registry-3LUCE7FT.js.map} +1 -1
  187. package/dist/provider-registry-NBRXBOQT.cjs +40 -0
  188. package/dist/{provider-registry-BZP3DIIV.cjs.map → provider-registry-NBRXBOQT.cjs.map} +1 -1
  189. package/dist/provider-registry.json +400 -22
  190. package/dist/{registry-generator-JPCV47SC.cjs → registry-generator-DEPPRYYJ.cjs} +21 -6
  191. package/dist/registry-generator-DEPPRYYJ.cjs.map +1 -0
  192. package/dist/{registry-generator-XD4FPZTU.js → registry-generator-FLW6NV42.js} +21 -7
  193. package/dist/registry-generator-FLW6NV42.js.map +1 -0
  194. package/dist/relevance/index.cjs +2 -2
  195. package/dist/relevance/index.js +1 -1
  196. package/dist/server/auth.d.ts +11 -0
  197. package/dist/server/auth.d.ts.map +1 -1
  198. package/dist/server/composite-auth.d.ts +9 -0
  199. package/dist/server/composite-auth.d.ts.map +1 -0
  200. package/dist/server/index.cjs +41 -0
  201. package/dist/server/index.cjs.map +1 -1
  202. package/dist/server/index.d.ts +1 -0
  203. package/dist/server/index.d.ts.map +1 -1
  204. package/dist/server/index.js +41 -1
  205. package/dist/server/index.js.map +1 -1
  206. package/dist/storage/index.cjs +29 -29
  207. package/dist/storage/index.js +1 -1
  208. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts.map +1 -1
  209. package/dist/stream/aisdk/v5/output.d.ts.map +1 -1
  210. package/dist/stream/base/output.d.ts.map +1 -1
  211. package/dist/stream/index.cjs +11 -11
  212. package/dist/stream/index.js +2 -2
  213. package/dist/stream/types.d.ts +7 -2
  214. package/dist/stream/types.d.ts.map +1 -1
  215. package/dist/test-utils/llm-mock.cjs +68 -31
  216. package/dist/test-utils/llm-mock.cjs.map +1 -1
  217. package/dist/test-utils/llm-mock.d.ts +4 -2
  218. package/dist/test-utils/llm-mock.d.ts.map +1 -1
  219. package/dist/test-utils/llm-mock.js +67 -30
  220. package/dist/test-utils/llm-mock.js.map +1 -1
  221. package/dist/tools/index.cjs +6 -6
  222. package/dist/tools/index.js +2 -2
  223. package/dist/tools/is-vercel-tool.cjs +2 -2
  224. package/dist/tools/is-vercel-tool.js +1 -1
  225. package/dist/tools/stream.d.ts +1 -0
  226. package/dist/tools/stream.d.ts.map +1 -1
  227. package/dist/tools/tool-builder/builder.d.ts +2 -0
  228. package/dist/tools/tool-builder/builder.d.ts.map +1 -1
  229. package/dist/tools/tool.d.ts +6 -6
  230. package/dist/tools/tool.d.ts.map +1 -1
  231. package/dist/tools/types.d.ts +6 -2
  232. package/dist/tools/types.d.ts.map +1 -1
  233. package/dist/tools/validation.d.ts +13 -1
  234. package/dist/tools/validation.d.ts.map +1 -1
  235. package/dist/utils.cjs +22 -22
  236. package/dist/utils.d.ts +1 -1
  237. package/dist/utils.d.ts.map +1 -1
  238. package/dist/utils.js +1 -1
  239. package/dist/vector/filter/index.cjs +7 -189
  240. package/dist/vector/filter/index.cjs.map +1 -1
  241. package/dist/vector/filter/index.js +1 -190
  242. package/dist/vector/filter/index.js.map +1 -1
  243. package/dist/vector/index.cjs +5 -0
  244. package/dist/vector/index.cjs.map +1 -1
  245. package/dist/vector/index.d.ts +1 -0
  246. package/dist/vector/index.d.ts.map +1 -1
  247. package/dist/vector/index.js +1 -0
  248. package/dist/vector/index.js.map +1 -1
  249. package/dist/vector/types.d.ts +86 -3
  250. package/dist/vector/types.d.ts.map +1 -1
  251. package/dist/vector/vector.d.ts +39 -2
  252. package/dist/vector/vector.d.ts.map +1 -1
  253. package/dist/voice/aisdk/index.d.ts +3 -0
  254. package/dist/voice/aisdk/index.d.ts.map +1 -0
  255. package/dist/voice/aisdk/speech.d.ts +23 -0
  256. package/dist/voice/aisdk/speech.d.ts.map +1 -0
  257. package/dist/voice/aisdk/transcription.d.ts +22 -0
  258. package/dist/voice/aisdk/transcription.d.ts.map +1 -0
  259. package/dist/voice/composite-voice.d.ts +4 -3
  260. package/dist/voice/composite-voice.d.ts.map +1 -1
  261. package/dist/voice/index.cjs +12 -4
  262. package/dist/voice/index.d.ts +1 -0
  263. package/dist/voice/index.d.ts.map +1 -1
  264. package/dist/voice/index.js +1 -1
  265. package/dist/workflows/default.d.ts.map +1 -1
  266. package/dist/workflows/evented/index.cjs +10 -10
  267. package/dist/workflows/evented/index.js +1 -1
  268. package/dist/workflows/evented/step-executor.d.ts.map +1 -1
  269. package/dist/workflows/index.cjs +23 -19
  270. package/dist/workflows/index.js +1 -1
  271. package/dist/workflows/types.d.ts +2 -2
  272. package/dist/workflows/types.d.ts.map +1 -1
  273. package/dist/workflows/utils.d.ts +7 -0
  274. package/dist/workflows/utils.d.ts.map +1 -1
  275. package/dist/workflows/workflow.d.ts +4 -2
  276. package/dist/workflows/workflow.d.ts.map +1 -1
  277. package/package.json +17 -16
  278. package/src/llm/model/provider-types.generated.d.ts +192 -11
  279. package/dist/chunk-3VOUB4ZU.cjs.map +0 -1
  280. package/dist/chunk-4DWZ4Z6H.js.map +0 -1
  281. package/dist/chunk-4IKJAKCD.cjs.map +0 -1
  282. package/dist/chunk-4RSHBKDJ.cjs.map +0 -1
  283. package/dist/chunk-BAMR7HKO.js.map +0 -1
  284. package/dist/chunk-CKGIPST2.js.map +0 -1
  285. package/dist/chunk-D6EDHNGV.js.map +0 -1
  286. package/dist/chunk-G36A2JRR.cjs.map +0 -1
  287. package/dist/chunk-G3OOCXAI.js.map +0 -1
  288. package/dist/chunk-GRGPQ32U.js.map +0 -1
  289. package/dist/chunk-HBJPYQRN.cjs.map +0 -1
  290. package/dist/chunk-HGNRQ3OG.js.map +0 -1
  291. package/dist/chunk-JXESKY4A.js.map +0 -1
  292. package/dist/chunk-KEURQGCQ.js.map +0 -1
  293. package/dist/chunk-KOSW5PP5.js.map +0 -1
  294. package/dist/chunk-LRSB62Z6.cjs.map +0 -1
  295. package/dist/chunk-MCUX2D5Q.js.map +0 -1
  296. package/dist/chunk-N4SJ4YX7.cjs.map +0 -1
  297. package/dist/chunk-O6NA3Z43.cjs.map +0 -1
  298. package/dist/chunk-OWX2PUFH.cjs.map +0 -1
  299. package/dist/chunk-QUKUN6NR.cjs.map +0 -1
  300. package/dist/chunk-T3WZCEC4.js.map +0 -1
  301. package/dist/chunk-UIZSWUKP.js.map +0 -1
  302. package/dist/chunk-VU6DVS7J.js.map +0 -1
  303. package/dist/chunk-VZGBVYXA.cjs.map +0 -1
  304. package/dist/chunk-XRIVPHXV.cjs.map +0 -1
  305. package/dist/chunk-YQ7NLZZ3.cjs.map +0 -1
  306. package/dist/chunk-ZPMFINU2.cjs.map +0 -1
  307. package/dist/models-dev-GCVENVWA.js +0 -3
  308. package/dist/models-dev-TIBJR6IG.cjs +0 -12
  309. package/dist/netlify-NTSNNT6F.cjs +0 -12
  310. package/dist/netlify-O5NJW7CF.js +0 -3
  311. package/dist/provider-registry-74GMFZKT.js +0 -3
  312. package/dist/provider-registry-BZP3DIIV.cjs +0 -40
  313. package/dist/registry-generator-JPCV47SC.cjs.map +0 -1
  314. package/dist/registry-generator-XD4FPZTU.js.map +0 -1
@@ -230,14 +230,137 @@ function convertImageFilePart(part, downloadedAssets) {
230
230
  }
231
231
  }
232
232
  }
233
+ zod.z.union([
234
+ zod.z.string(),
235
+ zod.z.instanceof(Uint8Array),
236
+ zod.z.instanceof(ArrayBuffer),
237
+ zod.z.custom(
238
+ // Buffer might not be available in some environments such as CloudFlare:
239
+ (value) => globalThis.Buffer?.isBuffer(value) ?? false,
240
+ { message: "Must be a Buffer" }
241
+ )
242
+ ]);
243
+ function convertDataContentToBase64String(content) {
244
+ if (typeof content === "string") {
245
+ return content;
246
+ }
247
+ if (content instanceof ArrayBuffer) {
248
+ return chunkSVLMF4UZ_cjs.convertUint8ArrayToBase64(new Uint8Array(content));
249
+ }
250
+ return chunkSVLMF4UZ_cjs.convertUint8ArrayToBase64(content);
251
+ }
252
+
253
+ // src/agent/message-list/prompt/image-utils.ts
254
+ function parseDataUri(dataUri) {
255
+ if (!dataUri.startsWith("data:")) {
256
+ return {
257
+ isDataUri: false,
258
+ base64Content: dataUri
259
+ };
260
+ }
261
+ const base64Index = dataUri.indexOf(",");
262
+ if (base64Index === -1) {
263
+ return {
264
+ isDataUri: true,
265
+ base64Content: dataUri
266
+ };
267
+ }
268
+ const header = dataUri.substring(5, base64Index);
269
+ const base64Content = dataUri.substring(base64Index + 1);
270
+ const semicolonIndex = header.indexOf(";");
271
+ const mimeType = semicolonIndex !== -1 ? header.substring(0, semicolonIndex) : header;
272
+ return {
273
+ isDataUri: true,
274
+ mimeType: mimeType || void 0,
275
+ base64Content
276
+ };
277
+ }
278
+ function createDataUri(base64Content, mimeType = "application/octet-stream") {
279
+ if (base64Content.startsWith("data:")) {
280
+ return base64Content;
281
+ }
282
+ return `data:${mimeType};base64,${base64Content}`;
283
+ }
284
+ function imageContentToString(image, fallbackMimeType) {
285
+ if (typeof image === "string") {
286
+ return image;
287
+ }
288
+ if (image instanceof URL) {
289
+ return image.toString();
290
+ }
291
+ if (image instanceof Uint8Array || image instanceof ArrayBuffer || globalThis.Buffer && Buffer.isBuffer(image)) {
292
+ const base64 = convertDataContentToBase64String(image);
293
+ return base64;
294
+ }
295
+ return String(image);
296
+ }
297
+ function getImageCacheKey(image) {
298
+ if (image instanceof URL) {
299
+ return image.toString();
300
+ }
301
+ if (typeof image === "string") {
302
+ return image.length;
303
+ }
304
+ if (image instanceof Uint8Array) {
305
+ return image.byteLength;
306
+ }
307
+ if (image instanceof ArrayBuffer) {
308
+ return image.byteLength;
309
+ }
310
+ return image;
311
+ }
312
+ function isValidUrl(str) {
313
+ try {
314
+ new URL(str);
315
+ return true;
316
+ } catch {
317
+ if (str.startsWith("//")) {
318
+ try {
319
+ new URL(`https:${str}`);
320
+ return true;
321
+ } catch {
322
+ return false;
323
+ }
324
+ }
325
+ return false;
326
+ }
327
+ }
328
+ function categorizeFileData(data, fallbackMimeType) {
329
+ const parsed = parseDataUri(data);
330
+ const mimeType = parsed.isDataUri && parsed.mimeType ? parsed.mimeType : fallbackMimeType;
331
+ if (parsed.isDataUri) {
332
+ return {
333
+ type: "dataUri",
334
+ mimeType,
335
+ data
336
+ };
337
+ }
338
+ if (isValidUrl(data)) {
339
+ return {
340
+ type: "url",
341
+ mimeType,
342
+ data
343
+ };
344
+ }
345
+ return {
346
+ type: "raw",
347
+ mimeType,
348
+ data
349
+ };
350
+ }
233
351
 
234
352
  // src/agent/message-list/prompt/attachments-to-parts.ts
235
353
  function attachmentsToParts(attachments) {
236
354
  const parts = [];
237
355
  for (const attachment of attachments) {
356
+ const categorized = categorizeFileData(attachment.url, attachment.contentType);
357
+ let urlString = attachment.url;
358
+ if (categorized.type === "raw") {
359
+ urlString = createDataUri(attachment.url, attachment.contentType || "application/octet-stream");
360
+ }
238
361
  let url;
239
362
  try {
240
- url = new URL(attachment.url);
363
+ url = new URL(urlString);
241
364
  } catch {
242
365
  throw new Error(`Invalid URL: ${attachment.url}`);
243
366
  }
@@ -262,13 +385,13 @@ function attachmentsToParts(attachments) {
262
385
  if (attachment.contentType?.startsWith("image/")) {
263
386
  parts.push({
264
387
  type: "image",
265
- image: attachment.url,
388
+ image: urlString,
266
389
  mimeType: attachment.contentType
267
390
  });
268
391
  } else if (attachment.contentType?.startsWith("text/")) {
269
392
  parts.push({
270
393
  type: "file",
271
- data: attachment.url,
394
+ data: urlString,
272
395
  mimeType: attachment.contentType
273
396
  });
274
397
  } else {
@@ -277,7 +400,7 @@ function attachmentsToParts(attachments) {
277
400
  }
278
401
  parts.push({
279
402
  type: "file",
280
- data: attachment.url,
403
+ data: urlString,
281
404
  mimeType: attachment.contentType
282
405
  });
283
406
  }
@@ -588,25 +711,6 @@ function convertToV1Messages(messages) {
588
711
  }
589
712
  return v1Messages;
590
713
  }
591
- zod.z.union([
592
- zod.z.string(),
593
- zod.z.instanceof(Uint8Array),
594
- zod.z.instanceof(ArrayBuffer),
595
- zod.z.custom(
596
- // Buffer might not be available in some environments such as CloudFlare:
597
- (value) => globalThis.Buffer?.isBuffer(value) ?? false,
598
- { message: "Must be a Buffer" }
599
- )
600
- ]);
601
- function convertDataContentToBase64String(content) {
602
- if (typeof content === "string") {
603
- return content;
604
- }
605
- if (content instanceof ArrayBuffer) {
606
- return chunkSVLMF4UZ_cjs.convertUint8ArrayToBase64(new Uint8Array(content));
607
- }
608
- return chunkSVLMF4UZ_cjs.convertUint8ArrayToBase64(content);
609
- }
610
714
 
611
715
  // src/utils/fetchWithRetry.ts
612
716
  async function fetchWithRetry(url, options = {}, maxRetries = 3) {
@@ -728,105 +832,6 @@ async function downloadAssetsFromMessages({
728
832
  return Object.fromEntries(downloadFileList);
729
833
  }
730
834
 
731
- // src/agent/message-list/prompt/image-utils.ts
732
- function parseDataUri(dataUri) {
733
- if (!dataUri.startsWith("data:")) {
734
- return {
735
- isDataUri: false,
736
- base64Content: dataUri
737
- };
738
- }
739
- const base64Index = dataUri.indexOf(",");
740
- if (base64Index === -1) {
741
- return {
742
- isDataUri: true,
743
- base64Content: dataUri
744
- };
745
- }
746
- const header = dataUri.substring(5, base64Index);
747
- const base64Content = dataUri.substring(base64Index + 1);
748
- const semicolonIndex = header.indexOf(";");
749
- const mimeType = semicolonIndex !== -1 ? header.substring(0, semicolonIndex) : header;
750
- return {
751
- isDataUri: true,
752
- mimeType: mimeType || void 0,
753
- base64Content
754
- };
755
- }
756
- function createDataUri(base64Content, mimeType = "application/octet-stream") {
757
- if (base64Content.startsWith("data:")) {
758
- return base64Content;
759
- }
760
- return `data:${mimeType};base64,${base64Content}`;
761
- }
762
- function imageContentToString(image, fallbackMimeType) {
763
- if (typeof image === "string") {
764
- return image;
765
- }
766
- if (image instanceof URL) {
767
- return image.toString();
768
- }
769
- if (image instanceof Uint8Array || image instanceof ArrayBuffer || globalThis.Buffer && Buffer.isBuffer(image)) {
770
- const base64 = convertDataContentToBase64String(image);
771
- return base64;
772
- }
773
- return String(image);
774
- }
775
- function getImageCacheKey(image) {
776
- if (image instanceof URL) {
777
- return image.toString();
778
- }
779
- if (typeof image === "string") {
780
- return image.length;
781
- }
782
- if (image instanceof Uint8Array) {
783
- return image.byteLength;
784
- }
785
- if (image instanceof ArrayBuffer) {
786
- return image.byteLength;
787
- }
788
- return image;
789
- }
790
- function isValidUrl(str) {
791
- try {
792
- new URL(str);
793
- return true;
794
- } catch {
795
- if (str.startsWith("//")) {
796
- try {
797
- new URL(`https:${str}`);
798
- return true;
799
- } catch {
800
- return false;
801
- }
802
- }
803
- return false;
804
- }
805
- }
806
- function categorizeFileData(data, fallbackMimeType) {
807
- const parsed = parseDataUri(data);
808
- const mimeType = parsed.isDataUri && parsed.mimeType ? parsed.mimeType : fallbackMimeType;
809
- if (parsed.isDataUri) {
810
- return {
811
- type: "dataUri",
812
- mimeType,
813
- data
814
- };
815
- }
816
- if (isValidUrl(data)) {
817
- return {
818
- type: "url",
819
- mimeType,
820
- data
821
- };
822
- }
823
- return {
824
- type: "raw",
825
- mimeType,
826
- data
827
- };
828
- }
829
-
830
835
  // src/agent/message-list/utils/ai-v5/gemini-compatibility.ts
831
836
  function ensureGeminiCompatibleMessages(messages) {
832
837
  const result = [...messages];
@@ -1426,9 +1431,20 @@ var MessageList = class _MessageList {
1426
1431
  if (m.content.parts.length) {
1427
1432
  for (const part of m.content.parts) {
1428
1433
  if (part.type === `file`) {
1434
+ let normalizedUrl;
1435
+ if (typeof part.data === "string") {
1436
+ const categorized = categorizeFileData(part.data, part.mimeType);
1437
+ if (categorized.type === "raw") {
1438
+ normalizedUrl = createDataUri(part.data, part.mimeType || "application/octet-stream");
1439
+ } else {
1440
+ normalizedUrl = part.data;
1441
+ }
1442
+ } else {
1443
+ normalizedUrl = part.data;
1444
+ }
1429
1445
  experimentalAttachments.push({
1430
1446
  contentType: part.mimeType,
1431
- url: part.data
1447
+ url: normalizedUrl
1432
1448
  });
1433
1449
  } else if (part.type === "tool-invocation" && (part.toolInvocation.state === "call" || part.toolInvocation.state === "partial-call")) {
1434
1450
  continue;
@@ -1880,6 +1896,12 @@ var MessageList = class _MessageList {
1880
1896
  return ti;
1881
1897
  });
1882
1898
  }
1899
+ if (!message.threadId && this.memoryInfo?.threadId) {
1900
+ message.threadId = this.memoryInfo.threadId;
1901
+ if (!message.resourceId && this.memoryInfo?.resourceId) {
1902
+ message.resourceId = this.memoryInfo.resourceId;
1903
+ }
1904
+ }
1883
1905
  return message;
1884
1906
  }
1885
1907
  aiV4UIMessageToMastraDBMessage(message, messageSource) {
@@ -1917,137 +1939,180 @@ var MessageList = class _MessageList {
1917
1939
  if (typeof coreMessage.content === "string") {
1918
1940
  parts.push({
1919
1941
  type: "text",
1920
- text: coreMessage.content,
1921
- // Preserve providerOptions from CoreMessage (e.g., for system messages with cacheControl)
1922
- ..."providerOptions" in coreMessage && coreMessage.providerOptions ? { providerMetadata: coreMessage.providerOptions } : {}
1942
+ text: coreMessage.content
1923
1943
  });
1924
1944
  } else if (Array.isArray(coreMessage.content)) {
1925
- for (const part of coreMessage.content) {
1926
- switch (part.type) {
1927
- case "text":
1945
+ for (const aiV4Part of coreMessage.content) {
1946
+ switch (aiV4Part.type) {
1947
+ case "text": {
1928
1948
  const prevPart = parts.at(-1);
1929
1949
  if (coreMessage.role === "assistant" && prevPart && prevPart.type === "tool-invocation") {
1930
1950
  parts.push({ type: "step-start" });
1931
1951
  }
1932
- const mergedProviderMetadata = {
1933
- ..."providerOptions" in coreMessage && coreMessage.providerOptions ? coreMessage.providerOptions : {},
1934
- ..."providerOptions" in part && part.providerOptions ? part.providerOptions : {}
1935
- };
1936
- parts.push({
1952
+ const part = {
1937
1953
  type: "text",
1938
- text: part.text,
1939
- ...Object.keys(mergedProviderMetadata).length > 0 ? { providerMetadata: mergedProviderMetadata } : {}
1940
- });
1954
+ text: aiV4Part.text
1955
+ };
1956
+ if (aiV4Part.providerOptions) {
1957
+ part.providerMetadata = aiV4Part.providerOptions;
1958
+ }
1959
+ parts.push(part);
1941
1960
  break;
1942
- case "tool-call":
1943
- parts.push({
1961
+ }
1962
+ case "tool-call": {
1963
+ const part = {
1944
1964
  type: "tool-invocation",
1945
1965
  toolInvocation: {
1946
1966
  state: "call",
1947
- toolCallId: part.toolCallId,
1948
- toolName: part.toolName,
1949
- args: part.args
1967
+ toolCallId: aiV4Part.toolCallId,
1968
+ toolName: aiV4Part.toolName,
1969
+ args: aiV4Part.args
1950
1970
  }
1951
- });
1971
+ };
1972
+ if (aiV4Part.providerOptions) {
1973
+ part.providerMetadata = aiV4Part.providerOptions;
1974
+ }
1975
+ parts.push(part);
1952
1976
  break;
1977
+ }
1953
1978
  case "tool-result":
1954
- let toolArgs = {};
1955
- const toolCallInSameMsg = coreMessage.content.find(
1956
- (p) => p.type === "tool-call" && p.toolCallId === part.toolCallId
1957
- );
1958
- if (toolCallInSameMsg && toolCallInSameMsg.type === "tool-call") {
1959
- toolArgs = toolCallInSameMsg.args;
1960
- }
1961
- if (Object.keys(toolArgs).length === 0) {
1962
- for (let i = this.messages.length - 1; i >= 0; i--) {
1963
- const msg = this.messages[i];
1964
- if (msg && msg.role === "assistant" && msg.content.parts) {
1965
- const toolCallPart = msg.content.parts.find(
1966
- (p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === part.toolCallId && p.toolInvocation.state === "call"
1967
- );
1968
- if (toolCallPart && toolCallPart.type === "tool-invocation" && toolCallPart.toolInvocation.args) {
1969
- toolArgs = toolCallPart.toolInvocation.args;
1970
- break;
1979
+ {
1980
+ let toolArgs = {};
1981
+ const toolCallInSameMsg = coreMessage.content.find(
1982
+ (p) => p.type === "tool-call" && p.toolCallId === aiV4Part.toolCallId
1983
+ );
1984
+ if (toolCallInSameMsg && toolCallInSameMsg.type === "tool-call") {
1985
+ toolArgs = toolCallInSameMsg.args;
1986
+ }
1987
+ if (Object.keys(toolArgs).length === 0) {
1988
+ for (let i = this.messages.length - 1; i >= 0; i--) {
1989
+ const msg = this.messages[i];
1990
+ if (msg && msg.role === "assistant" && msg.content.parts) {
1991
+ const toolCallPart = msg.content.parts.find(
1992
+ (p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === aiV4Part.toolCallId && p.toolInvocation.state === "call"
1993
+ );
1994
+ if (toolCallPart && toolCallPart.type === "tool-invocation" && toolCallPart.toolInvocation.args) {
1995
+ toolArgs = toolCallPart.toolInvocation.args;
1996
+ break;
1997
+ }
1971
1998
  }
1972
1999
  }
1973
2000
  }
2001
+ const invocation = {
2002
+ state: "result",
2003
+ toolCallId: aiV4Part.toolCallId,
2004
+ toolName: aiV4Part.toolName,
2005
+ result: aiV4Part.result ?? "",
2006
+ // undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
2007
+ args: toolArgs
2008
+ // Use the args from the corresponding tool-call
2009
+ };
2010
+ const part = {
2011
+ type: "tool-invocation",
2012
+ toolInvocation: invocation
2013
+ };
2014
+ if (aiV4Part.providerOptions) {
2015
+ part.providerMetadata = aiV4Part.providerOptions;
2016
+ }
2017
+ parts.push(part);
2018
+ toolInvocations.push(invocation);
1974
2019
  }
1975
- const invocation = {
1976
- state: "result",
1977
- toolCallId: part.toolCallId,
1978
- toolName: part.toolName,
1979
- result: part.result ?? "",
1980
- // undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
1981
- args: toolArgs
1982
- // Use the args from the corresponding tool-call
1983
- };
1984
- parts.push({
1985
- type: "tool-invocation",
1986
- toolInvocation: invocation
1987
- });
1988
- toolInvocations.push(invocation);
1989
2020
  break;
1990
2021
  case "reasoning":
1991
- parts.push({
1992
- type: "reasoning",
1993
- reasoning: "",
1994
- // leave this blank so we aren't double storing it in the db along with details
1995
- details: [{ type: "text", text: part.text, signature: part.signature }]
1996
- });
2022
+ {
2023
+ const part = {
2024
+ type: "reasoning",
2025
+ reasoning: "",
2026
+ // leave this blank so we aren't double storing it in the db along with details
2027
+ details: [{ type: "text", text: aiV4Part.text, signature: aiV4Part.signature }]
2028
+ };
2029
+ if (aiV4Part.providerOptions) {
2030
+ part.providerMetadata = aiV4Part.providerOptions;
2031
+ }
2032
+ parts.push(part);
2033
+ }
1997
2034
  break;
1998
2035
  case "redacted-reasoning":
1999
- parts.push({
2000
- type: "reasoning",
2001
- reasoning: "",
2002
- // No text reasoning for redacted parts
2003
- details: [{ type: "redacted", data: part.data }]
2004
- });
2036
+ {
2037
+ const part = {
2038
+ type: "reasoning",
2039
+ reasoning: "",
2040
+ // No text reasoning for redacted parts
2041
+ details: [{ type: "redacted", data: aiV4Part.data }]
2042
+ };
2043
+ if (aiV4Part.providerOptions) {
2044
+ part.providerMetadata = aiV4Part.providerOptions;
2045
+ }
2046
+ parts.push(part);
2047
+ }
2005
2048
  break;
2006
- case "image":
2007
- parts.push({
2049
+ case "image": {
2050
+ const part = {
2008
2051
  type: "file",
2009
- data: imageContentToString(part.image),
2010
- mimeType: part.mimeType
2011
- });
2052
+ data: imageContentToString(aiV4Part.image),
2053
+ mimeType: aiV4Part.mimeType
2054
+ };
2055
+ if (aiV4Part.providerOptions) {
2056
+ part.providerMetadata = aiV4Part.providerOptions;
2057
+ }
2058
+ parts.push(part);
2012
2059
  break;
2013
- case "file":
2014
- if (part.data instanceof URL) {
2015
- parts.push({
2060
+ }
2061
+ case "file": {
2062
+ if (aiV4Part.data instanceof URL) {
2063
+ const part = {
2016
2064
  type: "file",
2017
- data: part.data.toString(),
2018
- mimeType: part.mimeType
2019
- });
2020
- } else if (typeof part.data === "string") {
2021
- const categorized = categorizeFileData(part.data, part.mimeType);
2065
+ data: aiV4Part.data.toString(),
2066
+ mimeType: aiV4Part.mimeType
2067
+ };
2068
+ if (aiV4Part.providerOptions) {
2069
+ part.providerMetadata = aiV4Part.providerOptions;
2070
+ }
2071
+ parts.push(part);
2072
+ } else if (typeof aiV4Part.data === "string") {
2073
+ const categorized = categorizeFileData(aiV4Part.data, aiV4Part.mimeType);
2022
2074
  if (categorized.type === "url" || categorized.type === "dataUri") {
2023
- parts.push({
2075
+ const part = {
2024
2076
  type: "file",
2025
- data: part.data,
2077
+ data: aiV4Part.data,
2026
2078
  mimeType: categorized.mimeType || "image/png"
2027
- });
2079
+ };
2080
+ if (aiV4Part.providerOptions) {
2081
+ part.providerMetadata = aiV4Part.providerOptions;
2082
+ }
2083
+ parts.push(part);
2028
2084
  } else {
2029
2085
  try {
2030
- parts.push({
2086
+ const part = {
2031
2087
  type: "file",
2032
2088
  mimeType: categorized.mimeType || "image/png",
2033
- data: convertDataContentToBase64String(part.data)
2034
- });
2089
+ data: convertDataContentToBase64String(aiV4Part.data)
2090
+ };
2091
+ if (aiV4Part.providerOptions) {
2092
+ part.providerMetadata = aiV4Part.providerOptions;
2093
+ }
2094
+ parts.push(part);
2035
2095
  } catch (error) {
2036
2096
  console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2037
2097
  }
2038
2098
  }
2039
2099
  } else {
2040
2100
  try {
2041
- parts.push({
2101
+ const part = {
2042
2102
  type: "file",
2043
- mimeType: part.mimeType,
2044
- data: convertDataContentToBase64String(part.data)
2045
- });
2103
+ mimeType: aiV4Part.mimeType,
2104
+ data: convertDataContentToBase64String(aiV4Part.data)
2105
+ };
2106
+ if (aiV4Part.providerOptions) {
2107
+ part.providerMetadata = aiV4Part.providerOptions;
2108
+ }
2109
+ parts.push(part);
2046
2110
  } catch (error) {
2047
2111
  console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2048
2112
  }
2049
2113
  }
2050
2114
  break;
2115
+ }
2051
2116
  }
2052
2117
  }
2053
2118
  }
@@ -2058,6 +2123,12 @@ var MessageList = class _MessageList {
2058
2123
  if (toolInvocations.length) content.toolInvocations = toolInvocations;
2059
2124
  if (typeof coreMessage.content === `string`) content.content = coreMessage.content;
2060
2125
  if (experimentalAttachments.length) content.experimental_attachments = experimentalAttachments;
2126
+ if (coreMessage.providerOptions) {
2127
+ content.providerMetadata = coreMessage.providerOptions;
2128
+ }
2129
+ if ("metadata" in coreMessage && coreMessage.metadata !== null && coreMessage.metadata !== void 0) {
2130
+ content.metadata = coreMessage.metadata;
2131
+ }
2061
2132
  return {
2062
2133
  id,
2063
2134
  role: _MessageList.getRole(coreMessage),
@@ -2260,9 +2331,23 @@ var MessageList = class _MessageList {
2260
2331
  if (role === `tool` || role === `assistant`) {
2261
2332
  throw new Error(incompatibleMessage);
2262
2333
  }
2334
+ let processedImage;
2335
+ if (part.image instanceof URL || part.image instanceof Uint8Array) {
2336
+ processedImage = part.image;
2337
+ } else if (Buffer.isBuffer(part.image) || part.image instanceof ArrayBuffer) {
2338
+ processedImage = new Uint8Array(part.image);
2339
+ } else {
2340
+ const categorized = categorizeFileData(part.image, part.mimeType);
2341
+ if (categorized.type === "raw") {
2342
+ const dataUri = createDataUri(part.image, part.mimeType || "image/png");
2343
+ processedImage = new URL(dataUri);
2344
+ } else {
2345
+ processedImage = new URL(part.image);
2346
+ }
2347
+ }
2263
2348
  roleContent[role].push({
2264
2349
  ...part,
2265
- image: part.image instanceof URL || part.image instanceof Uint8Array ? part.image : Buffer.isBuffer(part.image) || part.image instanceof ArrayBuffer ? new Uint8Array(part.image) : new URL(part.image)
2350
+ image: processedImage
2266
2351
  });
2267
2352
  break;
2268
2353
  }
@@ -2408,6 +2493,9 @@ var MessageList = class _MessageList {
2408
2493
  if (dbMsg.createdAt) metadata.createdAt = dbMsg.createdAt;
2409
2494
  if (dbMsg.threadId) metadata.threadId = dbMsg.threadId;
2410
2495
  if (dbMsg.resourceId) metadata.resourceId = dbMsg.resourceId;
2496
+ if (dbMsg.content.providerMetadata) {
2497
+ metadata.providerMetadata = dbMsg.content.providerMetadata;
2498
+ }
2411
2499
  const hasToolInvocationParts = dbMsg.content.parts?.some((p) => p.type === "tool-invocation");
2412
2500
  if (dbMsg.content.toolInvocations && !hasToolInvocationParts) {
2413
2501
  for (const invocation of dbMsg.content.toolInvocations) {
@@ -2474,18 +2562,20 @@ var MessageList = class _MessageList {
2474
2562
  continue;
2475
2563
  }
2476
2564
  if (part.type === "reasoning") {
2477
- const reasoningPart = part;
2478
- const text = reasoningPart.text || reasoningPart.reasoning || (reasoningPart.details?.reduce((p, c) => {
2565
+ const text = part.reasoning || (part.details?.reduce((p, c) => {
2479
2566
  if (c.type === `text` && c.text) return p + c.text;
2480
2567
  return p;
2481
2568
  }, "") ?? "");
2482
- if (text || reasoningPart.details?.length) {
2483
- parts.push({
2569
+ if (text || part.details?.length) {
2570
+ const v5UIPart = {
2484
2571
  type: "reasoning",
2485
2572
  text: text || "",
2486
- state: "done",
2487
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2488
- });
2573
+ state: "done"
2574
+ };
2575
+ if (part.providerMetadata) {
2576
+ v5UIPart.providerMetadata = part.providerMetadata;
2577
+ }
2578
+ parts.push(v5UIPart);
2489
2579
  }
2490
2580
  continue;
2491
2581
  }
@@ -2498,12 +2588,15 @@ var MessageList = class _MessageList {
2498
2588
  }
2499
2589
  const categorized = typeof part.data === "string" ? categorizeFileData(part.data, part.mimeType) : { type: "raw", mimeType: part.mimeType};
2500
2590
  if (categorized.type === "url" && typeof part.data === "string") {
2501
- parts.push({
2591
+ const v5UIPart = {
2502
2592
  type: "file",
2503
2593
  url: part.data,
2504
- mediaType: categorized.mimeType || "image/png",
2505
- providerMetadata: part.providerMetadata
2506
- });
2594
+ mediaType: categorized.mimeType || "image/png"
2595
+ };
2596
+ if (part.providerMetadata) {
2597
+ v5UIPart.providerMetadata = part.providerMetadata;
2598
+ }
2599
+ parts.push(v5UIPart);
2507
2600
  } else {
2508
2601
  let filePartData;
2509
2602
  let extractedMimeType = part.mimeType;
@@ -2527,27 +2620,37 @@ var MessageList = class _MessageList {
2527
2620
  } else {
2528
2621
  dataUri = createDataUri(filePartData, finalMimeType);
2529
2622
  }
2530
- parts.push({
2623
+ const v5UIPart = {
2531
2624
  type: "file",
2532
2625
  url: dataUri,
2533
2626
  // Use url field with data URI
2534
- mediaType: finalMimeType,
2535
- providerMetadata: part.providerMetadata
2536
- });
2627
+ mediaType: finalMimeType
2628
+ };
2629
+ if (part.providerMetadata) {
2630
+ v5UIPart.providerMetadata = part.providerMetadata;
2631
+ }
2632
+ parts.push(v5UIPart);
2537
2633
  }
2538
2634
  } else if (part.type === "source") {
2539
- const sourcePart = part;
2540
- parts.push({
2635
+ const v5UIPart = {
2541
2636
  type: "source-url",
2542
- url: sourcePart.source.url,
2543
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2544
- });
2637
+ url: part.source.url,
2638
+ sourceId: part.source.id,
2639
+ title: part.source.title
2640
+ };
2641
+ if (part.providerMetadata) {
2642
+ v5UIPart.providerMetadata = part.providerMetadata;
2643
+ }
2644
+ parts.push(v5UIPart);
2545
2645
  } else if (part.type === "text") {
2546
- parts.push({
2646
+ const v5UIPart = {
2547
2647
  type: "text",
2548
- text: part.text,
2549
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2550
- });
2648
+ text: part.text
2649
+ };
2650
+ if (part.providerMetadata) {
2651
+ v5UIPart.providerMetadata = part.providerMetadata;
2652
+ }
2653
+ parts.push(v5UIPart);
2551
2654
  hasNonToolReasoningParts = true;
2552
2655
  } else {
2553
2656
  parts.push(part);
@@ -2711,32 +2814,69 @@ var MessageList = class _MessageList {
2711
2814
  }
2712
2815
  };
2713
2816
  }
2817
+ /**
2818
+ * Convert image or file to data URI or URL for V2 file part
2819
+ */
2820
+ static getDataStringFromAIV5DataPart = (part) => {
2821
+ let mimeType;
2822
+ let data;
2823
+ if ("data" in part) {
2824
+ mimeType = part.mediaType || "application/octet-stream";
2825
+ data = part.data;
2826
+ } else if ("image" in part) {
2827
+ mimeType = part.mediaType || "image/jpeg";
2828
+ data = part.image;
2829
+ } else {
2830
+ throw new chunkTWH4PTDG_cjs.MastraError({
2831
+ id: "MASTRA_AIV5_DATA_PART_INVALID",
2832
+ domain: "AGENT" /* AGENT */,
2833
+ category: "USER" /* USER */,
2834
+ text: "Invalid AIV5 data part in getDataStringFromAIV5DataPart",
2835
+ details: {
2836
+ part
2837
+ }
2838
+ });
2839
+ }
2840
+ if (data instanceof URL) {
2841
+ return data.toString();
2842
+ } else {
2843
+ if (data instanceof Buffer) {
2844
+ const base64 = data.toString("base64");
2845
+ return `data:${mimeType};base64,${base64}`;
2846
+ } else if (typeof data === "string") {
2847
+ return data.startsWith("data:") || data.startsWith("http") ? data : `data:${mimeType};base64,${data}`;
2848
+ } else if (data instanceof Uint8Array) {
2849
+ const base64 = Buffer.from(data).toString("base64");
2850
+ return `data:${mimeType};base64,${base64}`;
2851
+ } else if (data instanceof ArrayBuffer) {
2852
+ const base64 = Buffer.from(data).toString("base64");
2853
+ return `data:${mimeType};base64,${base64}`;
2854
+ } else {
2855
+ return "";
2856
+ }
2857
+ }
2858
+ };
2714
2859
  /**
2715
2860
  * Direct conversion from AIV5 ModelMessage to MastraDBMessage
2716
2861
  * Combines logic from aiV5ModelMessageToMastraMessageV3 + mastraMessageV3ToV2
2717
2862
  */
2718
2863
  static aiV5ModelMessageToMastraDBMessage(modelMsg, _messageSource) {
2719
2864
  const content = Array.isArray(modelMsg.content) ? modelMsg.content : [{ type: "text", text: modelMsg.content }];
2720
- const v2Parts = [];
2865
+ const mastraDBParts = [];
2721
2866
  const toolInvocations = [];
2722
2867
  const reasoningParts = [];
2723
2868
  const experimental_attachments = [];
2724
- const textParts = [];
2725
2869
  let lastPartWasToolResult = false;
2726
2870
  for (const part of content) {
2727
- const providerMetadata = {
2728
- ...modelMsg.providerMetadata || {},
2729
- ...part.providerMetadata || {}
2730
- };
2731
- const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
2732
2871
  if (part.type === "text") {
2733
2872
  const textPart = {
2734
2873
  type: "text",
2735
- text: part.text,
2736
- ...hasProviderMetadata && { experimental_providerMetadata: providerMetadata }
2874
+ text: part.text
2737
2875
  };
2738
- v2Parts.push(textPart);
2739
- textParts.push({ text: part.text, providerMetadata: hasProviderMetadata ? providerMetadata : void 0 });
2876
+ if (part.providerOptions) {
2877
+ textPart.providerMetadata = part.providerOptions;
2878
+ }
2879
+ mastraDBParts.push(textPart);
2740
2880
  lastPartWasToolResult = false;
2741
2881
  } else if (part.type === "tool-call") {
2742
2882
  const toolCallPart = part;
@@ -2747,10 +2887,12 @@ var MessageList = class _MessageList {
2747
2887
  toolName: toolCallPart.toolName,
2748
2888
  args: toolCallPart.input,
2749
2889
  state: "call"
2750
- },
2751
- ...hasProviderMetadata && { providerMetadata }
2890
+ }
2752
2891
  };
2753
- v2Parts.push(toolInvocationPart);
2892
+ if (part.providerOptions) {
2893
+ toolInvocationPart.providerMetadata = part.providerOptions;
2894
+ }
2895
+ mastraDBParts.push(toolInvocationPart);
2754
2896
  toolInvocations.push({
2755
2897
  toolCallId: toolCallPart.toolCallId,
2756
2898
  toolName: toolCallPart.toolName,
@@ -2761,78 +2903,67 @@ var MessageList = class _MessageList {
2761
2903
  } else if (part.type === "tool-result") {
2762
2904
  const toolResultPart = part;
2763
2905
  const matchingCall = toolInvocations.find((inv) => inv.toolCallId === toolResultPart.toolCallId);
2764
- const matchingV2Part = v2Parts.find(
2906
+ const matchingV2Part = mastraDBParts.find(
2765
2907
  (p) => p.type === "tool-invocation" && "toolInvocation" in p && p.toolInvocation.toolCallId === toolResultPart.toolCallId
2766
2908
  );
2909
+ const updateMatchingCallInvocationResult = (toolResultPart2, matchingCall2) => {
2910
+ matchingCall2.state = "result";
2911
+ matchingCall2.result = typeof toolResultPart2.output === "object" && toolResultPart2.output && "value" in toolResultPart2.output ? toolResultPart2.output.value : toolResultPart2.output;
2912
+ };
2767
2913
  if (matchingCall) {
2768
- matchingCall.state = "result";
2769
- matchingCall.result = typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output;
2914
+ updateMatchingCallInvocationResult(toolResultPart, matchingCall);
2770
2915
  } else {
2771
- const resultPartWithName = toolResultPart;
2772
- toolInvocations.push({
2916
+ const call = {
2917
+ state: "call",
2773
2918
  toolCallId: toolResultPart.toolCallId,
2774
- toolName: resultPartWithName.toolName || "unknown",
2775
- args: {},
2776
- result: typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output,
2777
- state: "result"
2778
- });
2919
+ toolName: toolResultPart.toolName || "unknown",
2920
+ args: {}
2921
+ };
2922
+ updateMatchingCallInvocationResult(toolResultPart, call);
2923
+ toolInvocations.push(call);
2779
2924
  }
2780
2925
  if (matchingV2Part && matchingV2Part.type === "tool-invocation") {
2781
- matchingV2Part.toolInvocation.state = "result";
2782
- matchingV2Part.toolInvocation.result = typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output;
2783
- if (hasProviderMetadata) {
2784
- matchingV2Part.providerMetadata = providerMetadata;
2785
- }
2926
+ updateMatchingCallInvocationResult(toolResultPart, matchingV2Part.toolInvocation);
2786
2927
  } else {
2787
- const resultPartWithName = toolResultPart;
2788
2928
  const toolInvocationPart = {
2789
2929
  type: "tool-invocation",
2790
2930
  toolInvocation: {
2791
2931
  toolCallId: toolResultPart.toolCallId,
2792
- toolName: resultPartWithName.toolName || "unknown",
2932
+ toolName: toolResultPart.toolName || "unknown",
2793
2933
  args: {},
2794
- result: typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output,
2795
- state: "result"
2796
- },
2797
- ...hasProviderMetadata && { providerMetadata }
2934
+ state: "call"
2935
+ }
2798
2936
  };
2799
- v2Parts.push(toolInvocationPart);
2937
+ updateMatchingCallInvocationResult(toolResultPart, toolInvocationPart.toolInvocation);
2938
+ mastraDBParts.push(toolInvocationPart);
2800
2939
  }
2801
2940
  lastPartWasToolResult = true;
2802
2941
  } else if (part.type === "reasoning") {
2803
- const reasoningPart = part;
2804
2942
  const v2ReasoningPart = {
2805
2943
  type: "reasoning",
2806
- reasoning: reasoningPart.text,
2807
- details: [{ type: "text", text: reasoningPart.text }],
2808
- ...hasProviderMetadata && { providerMetadata }
2944
+ reasoning: "",
2945
+ // leave this blank so we aren't double storing it in the db along with details
2946
+ details: [{ type: "text", text: part.text }]
2809
2947
  };
2810
- v2Parts.push(v2ReasoningPart);
2811
- reasoningParts.push(reasoningPart.text);
2948
+ if (part.providerOptions) {
2949
+ v2ReasoningPart.providerMetadata = part.providerOptions;
2950
+ }
2951
+ mastraDBParts.push(v2ReasoningPart);
2952
+ reasoningParts.push(part.text);
2812
2953
  lastPartWasToolResult = false;
2813
2954
  } else if (part.type === "image") {
2814
2955
  const imagePart = part;
2815
- let imageData;
2816
- const mimeType = imagePart.mimeType || "image/jpeg";
2817
- if ("url" in imagePart && typeof imagePart.url === "string") {
2818
- imageData = imagePart.url;
2819
- } else if ("data" in imagePart) {
2820
- if (typeof imagePart.data === "string") {
2821
- imageData = imagePart.data.startsWith("data:") || imagePart.data.startsWith("http") ? imagePart.data : `data:${mimeType};base64,${imagePart.data}`;
2822
- } else {
2823
- const base64 = Buffer.from(imagePart.data).toString("base64");
2824
- imageData = `data:${mimeType};base64,${base64}`;
2825
- }
2826
- } else {
2827
- imageData = "";
2828
- }
2956
+ const mimeType = imagePart.mediaType || "image/jpeg";
2957
+ const imageData = this.getDataStringFromAIV5DataPart(imagePart);
2829
2958
  const imageFilePart = {
2830
2959
  type: "file",
2831
2960
  data: imageData,
2832
- mimeType,
2833
- ...hasProviderMetadata && { providerMetadata }
2961
+ mimeType
2834
2962
  };
2835
- v2Parts.push(imageFilePart);
2963
+ if (part.providerOptions) {
2964
+ imageFilePart.providerMetadata = part.providerOptions;
2965
+ }
2966
+ mastraDBParts.push(imageFilePart);
2836
2967
  experimental_attachments.push({
2837
2968
  url: imageData,
2838
2969
  contentType: mimeType
@@ -2841,26 +2972,16 @@ var MessageList = class _MessageList {
2841
2972
  } else if (part.type === "file") {
2842
2973
  const filePart = part;
2843
2974
  const mimeType = filePart.mediaType || "application/octet-stream";
2844
- let fileData;
2845
- if ("url" in filePart && typeof filePart.url === "string") {
2846
- fileData = filePart.url;
2847
- } else if ("data" in filePart) {
2848
- if (typeof filePart.data === "string") {
2849
- fileData = filePart.data.startsWith("data:") || filePart.data.startsWith("http") ? filePart.data : `data:${mimeType};base64,${filePart.data}`;
2850
- } else {
2851
- const base64 = Buffer.from(filePart.data).toString("base64");
2852
- fileData = `data:${mimeType};base64,${base64}`;
2853
- }
2854
- } else {
2855
- fileData = "";
2856
- }
2975
+ const fileData = this.getDataStringFromAIV5DataPart(filePart);
2857
2976
  const v2FilePart = {
2858
2977
  type: "file",
2859
2978
  data: fileData,
2860
- mimeType,
2861
- ...hasProviderMetadata && { providerMetadata }
2979
+ mimeType
2862
2980
  };
2863
- v2Parts.push(v2FilePart);
2981
+ if (part.providerOptions) {
2982
+ v2FilePart.providerMetadata = part.providerOptions;
2983
+ }
2984
+ mastraDBParts.push(v2FilePart);
2864
2985
  experimental_attachments.push({
2865
2986
  url: fileData,
2866
2987
  contentType: mimeType
@@ -2868,34 +2989,34 @@ var MessageList = class _MessageList {
2868
2989
  lastPartWasToolResult = false;
2869
2990
  }
2870
2991
  }
2871
- if (modelMsg.role === "assistant" && lastPartWasToolResult && v2Parts.length > 0) {
2872
- const lastPart = v2Parts[v2Parts.length - 1];
2992
+ if (modelMsg.role === "assistant" && lastPartWasToolResult && mastraDBParts.length > 0) {
2993
+ const lastPart = mastraDBParts[mastraDBParts.length - 1];
2873
2994
  if (lastPart && lastPart.type !== "text") {
2874
2995
  const emptyTextPart = { type: "text", text: "" };
2875
- v2Parts.push(emptyTextPart);
2876
- textParts.push({ text: "" });
2996
+ mastraDBParts.push(emptyTextPart);
2877
2997
  }
2878
2998
  }
2879
- let contentString = void 0;
2880
- if (textParts.length > 0) {
2881
- contentString = textParts.map((p) => p.text).join("\n");
2882
- }
2883
- const metadata = {};
2999
+ const contentString = mastraDBParts.filter((p) => p.type === "text").map((p) => p.text).join("\n");
3000
+ const metadata = "metadata" in modelMsg && modelMsg.metadata !== null && modelMsg.metadata !== void 0 ? modelMsg.metadata : {};
2884
3001
  const id = `id` in modelMsg && typeof modelMsg.id === `string` ? modelMsg.id : `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2885
- return {
3002
+ const message = {
2886
3003
  id,
2887
3004
  role: _MessageList.getRole(modelMsg),
2888
3005
  createdAt: /* @__PURE__ */ new Date(),
2889
3006
  content: {
2890
3007
  format: 2,
2891
- parts: v2Parts,
3008
+ parts: mastraDBParts,
2892
3009
  toolInvocations: toolInvocations.length > 0 ? toolInvocations : void 0,
2893
3010
  reasoning: reasoningParts.length > 0 ? reasoningParts.join("\n") : void 0,
2894
3011
  experimental_attachments: experimental_attachments.length > 0 ? experimental_attachments : void 0,
2895
- content: contentString,
2896
- metadata
3012
+ content: contentString || void 0,
3013
+ metadata: Object.keys(metadata).length > 0 ? metadata : void 0
2897
3014
  }
2898
3015
  };
3016
+ if (modelMsg.providerOptions) {
3017
+ message.content.providerMetadata = modelMsg.providerOptions;
3018
+ }
3019
+ return message;
2899
3020
  }
2900
3021
  aiV4CoreMessagesToAIV5ModelMessages(messages, source) {
2901
3022
  return this.aiV5UIMessagesToAIV5ModelMessages(
@@ -2906,14 +3027,24 @@ var MessageList = class _MessageList {
2906
3027
  const sanitized = this.sanitizeV5UIMessages(messages, filterIncompleteToolCalls);
2907
3028
  const preprocessed = this.addStartStepPartsForAIV5(sanitized);
2908
3029
  const result = AIV5__namespace.convertToModelMessages(preprocessed);
2909
- return result;
3030
+ return result.map((modelMsg, index) => {
3031
+ const uiMsg = preprocessed[index];
3032
+ if (uiMsg?.metadata && typeof uiMsg.metadata === "object" && "providerMetadata" in uiMsg.metadata && uiMsg.metadata.providerMetadata) {
3033
+ return {
3034
+ ...modelMsg,
3035
+ providerOptions: uiMsg.metadata.providerMetadata
3036
+ };
3037
+ }
3038
+ return modelMsg;
3039
+ });
2910
3040
  }
2911
3041
  addStartStepPartsForAIV5(messages) {
2912
3042
  for (const message of messages) {
2913
3043
  if (message.role !== `assistant`) continue;
2914
3044
  for (const [index, part] of message.parts.entries()) {
2915
3045
  if (!AIV5__namespace.isToolUIPart(part)) continue;
2916
- if (message.parts.at(index + 1)?.type !== `step-start`) {
3046
+ const nextPart = message.parts.at(index + 1);
3047
+ if (nextPart && nextPart.type !== `step-start` && !AIV5__namespace.isToolUIPart(nextPart)) {
2917
3048
  message.parts.splice(index + 1, 0, { type: "step-start" });
2918
3049
  }
2919
3050
  }
@@ -3042,5 +3173,5 @@ exports.DefaultGeneratedFile = DefaultGeneratedFile;
3042
3173
  exports.DefaultGeneratedFileWithType = DefaultGeneratedFileWithType;
3043
3174
  exports.MessageList = MessageList;
3044
3175
  exports.convertMessages = convertMessages;
3045
- //# sourceMappingURL=chunk-G36A2JRR.cjs.map
3046
- //# sourceMappingURL=chunk-G36A2JRR.cjs.map
3176
+ //# sourceMappingURL=chunk-DQIZ5FFX.cjs.map
3177
+ //# sourceMappingURL=chunk-DQIZ5FFX.cjs.map