@mastra/core 1.0.0-beta.0 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (562) hide show
  1. package/CHANGELOG.md +1136 -0
  2. package/dist/agent/agent-legacy.d.ts +19 -22
  3. package/dist/agent/agent-legacy.d.ts.map +1 -1
  4. package/dist/agent/agent-types.test-d.d.ts +2 -0
  5. package/dist/agent/agent-types.test-d.d.ts.map +1 -0
  6. package/dist/agent/agent.d.ts +6 -6
  7. package/dist/agent/agent.d.ts.map +1 -1
  8. package/dist/agent/agent.types.d.ts +6 -4
  9. package/dist/agent/agent.types.d.ts.map +1 -1
  10. package/dist/agent/index.cjs +9 -9
  11. package/dist/agent/index.js +2 -2
  12. package/dist/agent/message-list/index.cjs +3 -3
  13. package/dist/agent/message-list/index.d.ts +60 -3
  14. package/dist/agent/message-list/index.d.ts.map +1 -1
  15. package/dist/agent/message-list/index.js +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 +1 -1
  18. package/dist/agent/message-list/prompt/convert-file.d.ts.map +1 -1
  19. package/dist/agent/message-list/prompt/download-assets.d.ts.map +1 -1
  20. package/dist/agent/save-queue/index.d.ts +3 -1
  21. package/dist/agent/save-queue/index.d.ts.map +1 -1
  22. package/dist/agent/test-utils.d.ts +37 -0
  23. package/dist/agent/test-utils.d.ts.map +1 -1
  24. package/dist/agent/trip-wire.d.ts.map +1 -1
  25. package/dist/agent/types.d.ts +21 -6
  26. package/dist/agent/types.d.ts.map +1 -1
  27. package/dist/agent/utils.d.ts +3 -3
  28. package/dist/agent/utils.d.ts.map +1 -1
  29. package/dist/agent/workflows/prepare-stream/index.d.ts +9 -7
  30. package/dist/agent/workflows/prepare-stream/index.d.ts.map +1 -1
  31. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts +3 -3
  32. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts.map +1 -1
  33. package/dist/agent/workflows/prepare-stream/prepare-memory-step.d.ts +6 -5
  34. package/dist/agent/workflows/prepare-stream/prepare-memory-step.d.ts.map +1 -1
  35. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts +2 -1
  36. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts.map +1 -1
  37. package/dist/agent/workflows/prepare-stream/schema.d.ts +8 -8
  38. package/dist/agent/workflows/prepare-stream/schema.d.ts.map +1 -1
  39. package/dist/agent/workflows/prepare-stream/stream-step.d.ts +11 -1
  40. package/dist/agent/workflows/prepare-stream/stream-step.d.ts.map +1 -1
  41. package/dist/base.cjs +2 -2
  42. package/dist/base.js +1 -1
  43. package/dist/bundler/index.cjs +2 -2
  44. package/dist/bundler/index.js +1 -1
  45. package/dist/cache/index.cjs +3 -3
  46. package/dist/cache/index.js +1 -1
  47. package/dist/{chunk-4CDL2QJT.js → chunk-267YOF47.js} +121 -36
  48. package/dist/chunk-267YOF47.js.map +1 -0
  49. package/dist/{chunk-OJNJA5ZI.js → chunk-3CKZSDTQ.js} +3 -3
  50. package/dist/{chunk-OJNJA5ZI.js.map → chunk-3CKZSDTQ.js.map} +1 -1
  51. package/dist/{chunk-JV2KH24V.js → chunk-3GKKHF36.js} +693 -375
  52. package/dist/chunk-3GKKHF36.js.map +1 -0
  53. package/dist/{chunk-TQTAMPSC.js → chunk-3N3QE4ZK.js} +3 -3
  54. package/dist/chunk-3N3QE4ZK.js.map +1 -0
  55. package/dist/{chunk-QUKUN6NR.cjs → chunk-4JKEUSCC.cjs} +107 -7
  56. package/dist/chunk-4JKEUSCC.cjs.map +1 -0
  57. package/dist/{chunk-XEVG546F.js → chunk-4Q3Z6QRE.js} +178 -12
  58. package/dist/chunk-4Q3Z6QRE.js.map +1 -0
  59. package/dist/{chunk-CBAB7GOD.cjs → chunk-6BJ7XHRH.cjs} +2 -2
  60. package/dist/{chunk-CBAB7GOD.cjs.map → chunk-6BJ7XHRH.cjs.map} +1 -1
  61. package/dist/{chunk-26SQQNMU.js → chunk-6CYYFT4O.js} +11389 -8193
  62. package/dist/chunk-6CYYFT4O.js.map +1 -0
  63. package/dist/{chunk-32CTMD2C.js → chunk-6ONQWD6S.js} +1221 -347
  64. package/dist/chunk-6ONQWD6S.js.map +1 -0
  65. package/dist/{chunk-J7O6WENZ.cjs → chunk-6TBWJV35.js} +5 -5
  66. package/dist/chunk-6TBWJV35.js.map +1 -0
  67. package/dist/chunk-6XCINXZ7.cjs +194 -0
  68. package/dist/chunk-6XCINXZ7.cjs.map +1 -0
  69. package/dist/{chunk-E7K4FTLN.cjs → chunk-72YCRTEX.cjs} +18 -2
  70. package/dist/chunk-72YCRTEX.cjs.map +1 -0
  71. package/dist/{chunk-BXOL277H.cjs → chunk-7THRV5KZ.cjs} +762 -27
  72. package/dist/chunk-7THRV5KZ.cjs.map +1 -0
  73. package/dist/{chunk-76K3IYWM.js → chunk-ADADLFDR.js} +3 -3
  74. package/dist/{chunk-76K3IYWM.js.map → chunk-ADADLFDR.js.map} +1 -1
  75. package/dist/{chunk-DSNPWVIG.cjs → chunk-AF74UXR5.cjs} +4 -3
  76. package/dist/chunk-AF74UXR5.cjs.map +1 -0
  77. package/dist/{chunk-QCQLOMJM.cjs → chunk-BCRRHJGV.cjs} +206 -65
  78. package/dist/chunk-BCRRHJGV.cjs.map +1 -0
  79. package/dist/chunk-BWYU7D33.js +192 -0
  80. package/dist/chunk-BWYU7D33.js.map +1 -0
  81. package/dist/{chunk-MV7KHWUT.js → chunk-BXONP7HX.js} +391 -15
  82. package/dist/chunk-BXONP7HX.js.map +1 -0
  83. package/dist/chunk-CDRYT2A5.js +513 -0
  84. package/dist/chunk-CDRYT2A5.js.map +1 -0
  85. package/dist/{chunk-S5MJLXMG.cjs → chunk-CZEJQSWB.cjs} +481 -124
  86. package/dist/chunk-CZEJQSWB.cjs.map +1 -0
  87. package/dist/chunk-DD2VNRQM.js +62 -0
  88. package/dist/chunk-DD2VNRQM.js.map +1 -0
  89. package/dist/{chunk-KEXGB7FK.cjs → chunk-DGV2FWB4.cjs} +7 -7
  90. package/dist/{chunk-KEXGB7FK.cjs.map → chunk-DGV2FWB4.cjs.map} +1 -1
  91. package/dist/{chunk-Y63IFHEZ.cjs → chunk-EOPEDKLG.cjs} +4 -4
  92. package/dist/chunk-EOPEDKLG.cjs.map +1 -0
  93. package/dist/{chunk-UIZSWUKP.js → chunk-F2GAJSBI.js} +105 -7
  94. package/dist/chunk-F2GAJSBI.js.map +1 -0
  95. package/dist/{chunk-VOY2RXOC.cjs → chunk-G4IFCTL5.cjs} +182 -15
  96. package/dist/chunk-G4IFCTL5.cjs.map +1 -0
  97. package/dist/chunk-KJBMTK5B.cjs +64 -0
  98. package/dist/chunk-KJBMTK5B.cjs.map +1 -0
  99. package/dist/{chunk-S6URFGCZ.js → chunk-LSHPJWM5.js} +3 -3
  100. package/dist/{chunk-S6URFGCZ.js.map → chunk-LSHPJWM5.js.map} +1 -1
  101. package/dist/chunk-MGCGWPQJ.cjs +275 -0
  102. package/dist/chunk-MGCGWPQJ.cjs.map +1 -0
  103. package/dist/chunk-MRFUISXC.cjs +4981 -0
  104. package/dist/chunk-MRFUISXC.cjs.map +1 -0
  105. package/dist/chunk-MSZP3FNH.cjs +518 -0
  106. package/dist/chunk-MSZP3FNH.cjs.map +1 -0
  107. package/dist/{chunk-KJ2SW6VA.js → chunk-NRUZYMHE.js} +4 -3
  108. package/dist/chunk-NRUZYMHE.js.map +1 -0
  109. package/dist/{chunk-7SKXKUYT.js → chunk-NUA6TSRD.js} +752 -17
  110. package/dist/chunk-NUA6TSRD.js.map +1 -0
  111. package/dist/chunk-OEIVMCWX.js +4959 -0
  112. package/dist/chunk-OEIVMCWX.js.map +1 -0
  113. package/dist/{chunk-VJUZZB2I.js → chunk-OIDPIW42.js} +26 -11
  114. package/dist/chunk-OIDPIW42.js.map +1 -0
  115. package/dist/{chunk-QUZGDSWE.cjs → chunk-OYIDRI3A.cjs} +32 -17
  116. package/dist/chunk-OYIDRI3A.cjs.map +1 -0
  117. package/dist/{chunk-YIK3ASEG.cjs → chunk-P2D2VO2O.cjs} +1226 -350
  118. package/dist/chunk-P2D2VO2O.cjs.map +1 -0
  119. package/dist/{chunk-L7XKOKOW.js → chunk-PK2A5WBG.js} +472 -125
  120. package/dist/chunk-PK2A5WBG.js.map +1 -0
  121. package/dist/{chunk-X7JMA3IY.js → chunk-PSCMWPLC.js} +2 -2
  122. package/dist/{chunk-X7JMA3IY.js.map → chunk-PSCMWPLC.js.map} +1 -1
  123. package/dist/{chunk-WM6CK2F3.cjs → chunk-QD3UWFAV.cjs} +130 -48
  124. package/dist/chunk-QD3UWFAV.cjs.map +1 -0
  125. package/dist/{chunk-FD734TPS.cjs → chunk-SL7Q6IZF.cjs} +11425 -8211
  126. package/dist/chunk-SL7Q6IZF.cjs.map +1 -0
  127. package/dist/{chunk-ECFXGXWO.cjs → chunk-SSPUH2N7.cjs} +693 -375
  128. package/dist/chunk-SSPUH2N7.cjs.map +1 -0
  129. package/dist/{chunk-BMAFVZ2D.cjs → chunk-T2VLUFGG.cjs} +4 -4
  130. package/dist/{chunk-BMAFVZ2D.cjs.map → chunk-T2VLUFGG.cjs.map} +1 -1
  131. package/dist/chunk-THZTRBFS.js +268 -0
  132. package/dist/chunk-THZTRBFS.js.map +1 -0
  133. package/dist/{chunk-Y6ROD72V.cjs → chunk-UIGRFDO6.cjs} +4 -4
  134. package/dist/{chunk-Y6ROD72V.cjs.map → chunk-UIGRFDO6.cjs.map} +1 -1
  135. package/dist/{chunk-GRBGQ2GE.js → chunk-UVHSM2GU.cjs} +9 -3
  136. package/dist/chunk-UVHSM2GU.cjs.map +1 -0
  137. package/dist/{chunk-GGYKYORQ.cjs → chunk-WX2DLWXF.cjs} +407 -22
  138. package/dist/chunk-WX2DLWXF.cjs.map +1 -0
  139. package/dist/{chunk-P6APHXPZ.js → chunk-XRJQ5ZA7.js} +179 -38
  140. package/dist/chunk-XRJQ5ZA7.js.map +1 -0
  141. package/dist/{chunk-KIZIOFZC.js → chunk-Y36Y5MTD.js} +17 -3
  142. package/dist/chunk-Y36Y5MTD.js.map +1 -0
  143. package/dist/{chunk-VV753WCB.cjs → chunk-YWMMBIOM.cjs} +21 -20
  144. package/dist/chunk-YWMMBIOM.cjs.map +1 -0
  145. package/dist/{chunk-BU4IAJWF.js → chunk-Z57R5WS4.js} +16 -15
  146. package/dist/chunk-Z57R5WS4.js.map +1 -0
  147. package/dist/deployer/index.cjs +2 -2
  148. package/dist/deployer/index.js +1 -1
  149. package/dist/di/index.cjs +10 -2
  150. package/dist/di/index.d.ts +1 -1
  151. package/dist/di/index.d.ts.map +1 -1
  152. package/dist/di/index.js +1 -1
  153. package/dist/evals/base.d.ts +10 -2
  154. package/dist/evals/base.d.ts.map +1 -1
  155. package/dist/evals/base.test-utils.d.ts +25 -25
  156. package/dist/evals/index.cjs +9 -9
  157. package/dist/evals/index.js +2 -2
  158. package/dist/evals/scoreTraces/index.cjs +7 -6
  159. package/dist/evals/scoreTraces/index.cjs.map +1 -1
  160. package/dist/evals/scoreTraces/index.js +4 -3
  161. package/dist/evals/scoreTraces/index.js.map +1 -1
  162. package/dist/evals/scoreTraces/scoreTracesWorkflow.d.ts.map +1 -1
  163. package/dist/evals/types.d.ts +25 -24
  164. package/dist/evals/types.d.ts.map +1 -1
  165. package/dist/index.cjs +2 -2
  166. package/dist/index.js +1 -1
  167. package/dist/integration/index.cjs +2 -2
  168. package/dist/integration/index.js +1 -1
  169. package/dist/llm/index.cjs +30 -10
  170. package/dist/llm/index.d.ts +2 -0
  171. package/dist/llm/index.d.ts.map +1 -1
  172. package/dist/llm/index.js +5 -1
  173. package/dist/llm/model/aisdk/v5/model.d.ts +47 -0
  174. package/dist/llm/model/aisdk/v5/model.d.ts.map +1 -0
  175. package/dist/llm/model/gateway-resolver.d.ts.map +1 -1
  176. package/dist/llm/model/gateways/azure.d.ts +36 -0
  177. package/dist/llm/model/gateways/azure.d.ts.map +1 -0
  178. package/dist/llm/model/gateways/base.d.ts +9 -4
  179. package/dist/llm/model/gateways/base.d.ts.map +1 -1
  180. package/dist/llm/model/gateways/constants.d.ts.map +1 -1
  181. package/dist/llm/model/gateways/index.d.ts +4 -1
  182. package/dist/llm/model/gateways/index.d.ts.map +1 -1
  183. package/dist/llm/model/gateways/models-dev.d.ts +3 -2
  184. package/dist/llm/model/gateways/models-dev.d.ts.map +1 -1
  185. package/dist/llm/model/gateways/netlify.d.ts +4 -3
  186. package/dist/llm/model/gateways/netlify.d.ts.map +1 -1
  187. package/dist/llm/model/is-v2-model.d.ts +3 -0
  188. package/dist/llm/model/is-v2-model.d.ts.map +1 -0
  189. package/dist/llm/model/model-method-from-agent.d.ts +4 -0
  190. package/dist/llm/model/model-method-from-agent.d.ts.map +1 -0
  191. package/dist/llm/model/model.d.ts.map +1 -1
  192. package/dist/llm/model/model.loop.d.ts +2 -2
  193. package/dist/llm/model/model.loop.d.ts.map +1 -1
  194. package/dist/llm/model/model.loop.types.d.ts +2 -0
  195. package/dist/llm/model/model.loop.types.d.ts.map +1 -1
  196. package/dist/llm/model/provider-options.d.ts +4 -1
  197. package/dist/llm/model/provider-options.d.ts.map +1 -1
  198. package/dist/llm/model/provider-registry.d.ts +11 -1
  199. package/dist/llm/model/provider-registry.d.ts.map +1 -1
  200. package/dist/llm/model/provider-types.generated.d.ts +520 -33
  201. package/dist/llm/model/registry-generator.d.ts +12 -0
  202. package/dist/llm/model/registry-generator.d.ts.map +1 -1
  203. package/dist/llm/model/resolve-model.d.ts +1 -1
  204. package/dist/llm/model/resolve-model.d.ts.map +1 -1
  205. package/dist/llm/model/router.d.ts +12 -5
  206. package/dist/llm/model/router.d.ts.map +1 -1
  207. package/dist/llm/model/shared.types.d.ts +10 -4
  208. package/dist/llm/model/shared.types.d.ts.map +1 -1
  209. package/dist/logger/constants.d.ts +1 -0
  210. package/dist/logger/constants.d.ts.map +1 -1
  211. package/dist/logger/index.cjs +11 -11
  212. package/dist/logger/index.js +2 -2
  213. package/dist/logger/transport.d.ts +1 -1
  214. package/dist/logger/transport.d.ts.map +1 -1
  215. package/dist/loop/index.cjs +2 -2
  216. package/dist/loop/index.js +1 -1
  217. package/dist/loop/loop.d.ts.map +1 -1
  218. package/dist/loop/network/index.d.ts.map +1 -1
  219. package/dist/loop/test-utils/MastraLanguageModelV2Mock.d.ts +37 -0
  220. package/dist/loop/test-utils/MastraLanguageModelV2Mock.d.ts.map +1 -0
  221. package/dist/loop/test-utils/fullStream.d.ts.map +1 -1
  222. package/dist/loop/test-utils/generateText.d.ts.map +1 -1
  223. package/dist/loop/test-utils/options.d.ts.map +1 -1
  224. package/dist/loop/test-utils/resultObject.d.ts.map +1 -1
  225. package/dist/loop/test-utils/streamObject.d.ts.map +1 -1
  226. package/dist/loop/test-utils/textStream.d.ts.map +1 -1
  227. package/dist/loop/test-utils/toUIMessageStream.d.ts.map +1 -1
  228. package/dist/loop/test-utils/tools.d.ts.map +1 -1
  229. package/dist/loop/test-utils/utils.d.ts +1 -1
  230. package/dist/loop/test-utils/utils.d.ts.map +1 -1
  231. package/dist/loop/types.d.ts +22 -5
  232. package/dist/loop/types.d.ts.map +1 -1
  233. package/dist/loop/workflows/agentic-execution/index.d.ts +42 -42
  234. package/dist/loop/workflows/agentic-execution/index.d.ts.map +1 -1
  235. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts +29 -29
  236. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts.map +1 -1
  237. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts +14 -14
  238. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts.map +1 -1
  239. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts +1 -1
  240. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts.map +1 -1
  241. package/dist/loop/workflows/agentic-loop/index.d.ts +44 -43
  242. package/dist/loop/workflows/agentic-loop/index.d.ts.map +1 -1
  243. package/dist/loop/workflows/run-state.d.ts +2 -2
  244. package/dist/loop/workflows/run-state.d.ts.map +1 -1
  245. package/dist/loop/workflows/schema.d.ts +14 -14
  246. package/dist/loop/workflows/stream.d.ts +1 -1
  247. package/dist/loop/workflows/stream.d.ts.map +1 -1
  248. package/dist/mastra/index.cjs +2 -2
  249. package/dist/mastra/index.d.ts +377 -12
  250. package/dist/mastra/index.d.ts.map +1 -1
  251. package/dist/mastra/index.js +1 -1
  252. package/dist/mcp/index.cjs +4 -4
  253. package/dist/mcp/index.cjs.map +1 -1
  254. package/dist/mcp/index.d.ts +4 -4
  255. package/dist/mcp/index.d.ts.map +1 -1
  256. package/dist/mcp/index.js +2 -2
  257. package/dist/mcp/index.js.map +1 -1
  258. package/dist/mcp/types.d.ts +2 -2
  259. package/dist/mcp/types.d.ts.map +1 -1
  260. package/dist/memory/index.cjs +21 -327
  261. package/dist/memory/index.cjs.map +1 -1
  262. package/dist/memory/index.js +1 -327
  263. package/dist/memory/index.js.map +1 -1
  264. package/dist/memory/memory.d.ts +28 -18
  265. package/dist/memory/memory.d.ts.map +1 -1
  266. package/dist/memory/mock.d.ts +15 -14
  267. package/dist/memory/mock.d.ts.map +1 -1
  268. package/dist/memory/types.d.ts +69 -11
  269. package/dist/memory/types.d.ts.map +1 -1
  270. package/dist/models-dev-23RN2WHG.js +3 -0
  271. package/dist/{models-dev-7U4NRMM3.js.map → models-dev-23RN2WHG.js.map} +1 -1
  272. package/dist/models-dev-EO3SUIY2.cjs +12 -0
  273. package/dist/{models-dev-VKSAQPRK.cjs.map → models-dev-EO3SUIY2.cjs.map} +1 -1
  274. package/dist/netlify-GXJ5D5DD.js +3 -0
  275. package/dist/{netlify-42ZNWIDQ.js.map → netlify-GXJ5D5DD.js.map} +1 -1
  276. package/dist/netlify-KJLY3GFS.cjs +12 -0
  277. package/dist/{netlify-2IDXTNFW.cjs.map → netlify-KJLY3GFS.cjs.map} +1 -1
  278. package/dist/observability/index.cjs +16 -8
  279. package/dist/observability/index.js +1 -1
  280. package/dist/observability/types/tracing.d.ts +236 -13
  281. package/dist/observability/types/tracing.d.ts.map +1 -1
  282. package/dist/observability/utils.d.ts +47 -1
  283. package/dist/observability/utils.d.ts.map +1 -1
  284. package/dist/processors/index.cjs +35 -11
  285. package/dist/processors/index.d.ts +101 -23
  286. package/dist/processors/index.d.ts.map +1 -1
  287. package/dist/processors/index.js +1 -1
  288. package/dist/processors/memory/index.d.ts +7 -0
  289. package/dist/processors/memory/index.d.ts.map +1 -0
  290. package/dist/processors/memory/message-history.d.ts +43 -0
  291. package/dist/processors/memory/message-history.d.ts.map +1 -0
  292. package/dist/processors/memory/semantic-recall.d.ts +152 -0
  293. package/dist/processors/memory/semantic-recall.d.ts.map +1 -0
  294. package/dist/processors/memory/working-memory.d.ts +65 -0
  295. package/dist/processors/memory/working-memory.d.ts.map +1 -0
  296. package/dist/processors/processors/batch-parts.d.ts +1 -1
  297. package/dist/processors/processors/batch-parts.d.ts.map +1 -1
  298. package/dist/processors/processors/index.d.ts +2 -1
  299. package/dist/processors/processors/index.d.ts.map +1 -1
  300. package/dist/processors/processors/language-detector.d.ts +15 -1
  301. package/dist/processors/processors/language-detector.d.ts.map +1 -1
  302. package/dist/processors/processors/moderation.d.ts +15 -1
  303. package/dist/processors/processors/moderation.d.ts.map +1 -1
  304. package/dist/processors/processors/pii-detector.d.ts +15 -1
  305. package/dist/processors/processors/pii-detector.d.ts.map +1 -1
  306. package/dist/processors/processors/prompt-injection-detector.d.ts +15 -1
  307. package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -1
  308. package/dist/processors/processors/structured-output.d.ts +3 -2
  309. package/dist/processors/processors/structured-output.d.ts.map +1 -1
  310. package/dist/processors/processors/system-prompt-scrubber.d.ts +1 -1
  311. package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -1
  312. package/dist/processors/processors/token-limiter.d.ts +22 -12
  313. package/dist/processors/processors/token-limiter.d.ts.map +1 -1
  314. package/dist/processors/processors/tool-call-filter.d.ts +28 -0
  315. package/dist/processors/processors/tool-call-filter.d.ts.map +1 -0
  316. package/dist/processors/processors/unicode-normalizer.d.ts +1 -1
  317. package/dist/processors/processors/unicode-normalizer.d.ts.map +1 -1
  318. package/dist/processors/runner.d.ts +35 -4
  319. package/dist/processors/runner.d.ts.map +1 -1
  320. package/dist/provider-registry-A7FILT6Q.cjs +40 -0
  321. package/dist/provider-registry-A7FILT6Q.cjs.map +1 -0
  322. package/dist/provider-registry-XM2YDI5X.js +3 -0
  323. package/dist/provider-registry-XM2YDI5X.js.map +1 -0
  324. package/dist/provider-registry.json +1135 -77
  325. package/dist/{registry-generator-MK63POJO.cjs → registry-generator-34SC4TAU.cjs} +23 -6
  326. package/dist/registry-generator-34SC4TAU.cjs.map +1 -0
  327. package/dist/{registry-generator-H4YNODDH.js → registry-generator-UMTNPBJX.js} +23 -7
  328. package/dist/registry-generator-UMTNPBJX.js.map +1 -0
  329. package/dist/relevance/index.cjs +2 -2
  330. package/dist/relevance/index.js +1 -1
  331. package/dist/request-context/index.cjs +10 -2
  332. package/dist/request-context/index.d.ts +26 -0
  333. package/dist/request-context/index.d.ts.map +1 -1
  334. package/dist/request-context/index.js +1 -1
  335. package/dist/server/auth.d.ts +11 -0
  336. package/dist/server/auth.d.ts.map +1 -1
  337. package/dist/server/base.d.ts +51 -0
  338. package/dist/server/base.d.ts.map +1 -0
  339. package/dist/server/composite-auth.d.ts +9 -0
  340. package/dist/server/composite-auth.d.ts.map +1 -0
  341. package/dist/server/index.cjs +124 -3
  342. package/dist/server/index.cjs.map +1 -1
  343. package/dist/server/index.d.ts +8 -0
  344. package/dist/server/index.d.ts.map +1 -1
  345. package/dist/server/index.js +121 -3
  346. package/dist/server/index.js.map +1 -1
  347. package/dist/server/simple-auth.d.ts +27 -0
  348. package/dist/server/simple-auth.d.ts.map +1 -0
  349. package/dist/storage/base.d.ts +76 -3
  350. package/dist/storage/base.d.ts.map +1 -1
  351. package/dist/storage/constants.d.ts +3 -1
  352. package/dist/storage/constants.d.ts.map +1 -1
  353. package/dist/storage/domains/agents/base.d.ts +49 -0
  354. package/dist/storage/domains/agents/base.d.ts.map +1 -0
  355. package/dist/storage/domains/agents/index.d.ts +3 -0
  356. package/dist/storage/domains/agents/index.d.ts.map +1 -0
  357. package/dist/storage/domains/agents/inmemory.d.ts +22 -0
  358. package/dist/storage/domains/agents/inmemory.d.ts.map +1 -0
  359. package/dist/storage/domains/index.d.ts +1 -0
  360. package/dist/storage/domains/index.d.ts.map +1 -1
  361. package/dist/storage/domains/memory/inmemory.d.ts.map +1 -1
  362. package/dist/storage/domains/operations/inmemory.d.ts.map +1 -1
  363. package/dist/storage/domains/scores/base.d.ts +2 -2
  364. package/dist/storage/domains/scores/base.d.ts.map +1 -1
  365. package/dist/storage/domains/scores/inmemory.d.ts +2 -2
  366. package/dist/storage/domains/scores/inmemory.d.ts.map +1 -1
  367. package/dist/storage/domains/workflows/base.d.ts +4 -0
  368. package/dist/storage/domains/workflows/base.d.ts.map +1 -1
  369. package/dist/storage/domains/workflows/inmemory.d.ts +5 -1
  370. package/dist/storage/domains/workflows/inmemory.d.ts.map +1 -1
  371. package/dist/storage/index.cjs +65 -29
  372. package/dist/storage/index.js +1 -1
  373. package/dist/storage/mock.d.ts +7 -2
  374. package/dist/storage/mock.d.ts.map +1 -1
  375. package/dist/storage/storageWithInit.d.ts.map +1 -1
  376. package/dist/storage/types.d.ts +85 -2
  377. package/dist/storage/types.d.ts.map +1 -1
  378. package/dist/storage/utils.d.ts +82 -0
  379. package/dist/storage/utils.d.ts.map +1 -1
  380. package/dist/stream/MastraAgentNetworkStream.d.ts +1 -1
  381. package/dist/stream/MastraAgentNetworkStream.d.ts.map +1 -1
  382. package/dist/stream/MastraWorkflowStream.d.ts +1 -1
  383. package/dist/stream/MastraWorkflowStream.d.ts.map +1 -1
  384. package/dist/stream/RunOutput.d.ts +2 -2
  385. package/dist/stream/RunOutput.d.ts.map +1 -1
  386. package/dist/stream/aisdk/v4/usage.d.ts +19 -0
  387. package/dist/stream/aisdk/v4/usage.d.ts.map +1 -0
  388. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts.map +1 -1
  389. package/dist/stream/aisdk/v5/execute.d.ts +8 -4
  390. package/dist/stream/aisdk/v5/execute.d.ts.map +1 -1
  391. package/dist/stream/aisdk/v5/input.d.ts +4 -1
  392. package/dist/stream/aisdk/v5/input.d.ts.map +1 -1
  393. package/dist/stream/aisdk/v5/output.d.ts +9 -9
  394. package/dist/stream/aisdk/v5/output.d.ts.map +1 -1
  395. package/dist/stream/aisdk/v5/transform.d.ts.map +1 -1
  396. package/dist/stream/base/base.d.ts +1 -1
  397. package/dist/stream/base/base.d.ts.map +1 -1
  398. package/dist/stream/base/input.d.ts +1 -1
  399. package/dist/stream/base/output-format-handlers.d.ts +14 -1
  400. package/dist/stream/base/output-format-handlers.d.ts.map +1 -1
  401. package/dist/stream/base/output.d.ts +3 -3
  402. package/dist/stream/base/output.d.ts.map +1 -1
  403. package/dist/stream/index.cjs +15 -11
  404. package/dist/stream/index.d.ts +2 -2
  405. package/dist/stream/index.d.ts.map +1 -1
  406. package/dist/stream/index.js +2 -2
  407. package/dist/stream/types.d.ts +48 -25
  408. package/dist/stream/types.d.ts.map +1 -1
  409. package/dist/test-utils/llm-mock.cjs +101 -30
  410. package/dist/test-utils/llm-mock.cjs.map +1 -1
  411. package/dist/test-utils/llm-mock.d.ts +7 -2
  412. package/dist/test-utils/llm-mock.d.ts.map +1 -1
  413. package/dist/test-utils/llm-mock.js +97 -30
  414. package/dist/test-utils/llm-mock.js.map +1 -1
  415. package/dist/tools/index.cjs +6 -6
  416. package/dist/tools/index.js +2 -2
  417. package/dist/tools/is-vercel-tool.cjs +2 -2
  418. package/dist/tools/is-vercel-tool.js +1 -1
  419. package/dist/tools/stream.d.ts +10 -4
  420. package/dist/tools/stream.d.ts.map +1 -1
  421. package/dist/tools/tool-builder/builder.d.ts +2 -0
  422. package/dist/tools/tool-builder/builder.d.ts.map +1 -1
  423. package/dist/tools/tool-stream-types.test-d.d.ts +2 -0
  424. package/dist/tools/tool-stream-types.test-d.d.ts.map +1 -0
  425. package/dist/tools/tool.d.ts +21 -8
  426. package/dist/tools/tool.d.ts.map +1 -1
  427. package/dist/tools/types.d.ts +33 -6
  428. package/dist/tools/types.d.ts.map +1 -1
  429. package/dist/tools/validation.d.ts +25 -1
  430. package/dist/tools/validation.d.ts.map +1 -1
  431. package/dist/tts/index.cjs +2 -2
  432. package/dist/tts/index.js +1 -1
  433. package/dist/types/zod-compat.d.ts +14 -1
  434. package/dist/types/zod-compat.d.ts.map +1 -1
  435. package/dist/utils.cjs +25 -21
  436. package/dist/utils.d.ts +9 -4
  437. package/dist/utils.d.ts.map +1 -1
  438. package/dist/utils.js +1 -1
  439. package/dist/vector/filter/index.cjs +7 -189
  440. package/dist/vector/filter/index.cjs.map +1 -1
  441. package/dist/vector/filter/index.js +1 -190
  442. package/dist/vector/filter/index.js.map +1 -1
  443. package/dist/vector/index.cjs +7 -2
  444. package/dist/vector/index.cjs.map +1 -1
  445. package/dist/vector/index.d.ts +1 -0
  446. package/dist/vector/index.d.ts.map +1 -1
  447. package/dist/vector/index.js +2 -1
  448. package/dist/vector/index.js.map +1 -1
  449. package/dist/vector/types.d.ts +86 -3
  450. package/dist/vector/types.d.ts.map +1 -1
  451. package/dist/vector/vector.d.ts +39 -2
  452. package/dist/vector/vector.d.ts.map +1 -1
  453. package/dist/voice/aisdk/index.d.ts +3 -0
  454. package/dist/voice/aisdk/index.d.ts.map +1 -0
  455. package/dist/voice/aisdk/speech.d.ts +23 -0
  456. package/dist/voice/aisdk/speech.d.ts.map +1 -0
  457. package/dist/voice/aisdk/transcription.d.ts +22 -0
  458. package/dist/voice/aisdk/transcription.d.ts.map +1 -0
  459. package/dist/voice/composite-voice.d.ts +4 -3
  460. package/dist/voice/composite-voice.d.ts.map +1 -1
  461. package/dist/voice/index.cjs +12 -4
  462. package/dist/voice/index.d.ts +1 -0
  463. package/dist/voice/index.d.ts.map +1 -1
  464. package/dist/voice/index.js +1 -1
  465. package/dist/workflows/default.d.ts +179 -270
  466. package/dist/workflows/default.d.ts.map +1 -1
  467. package/dist/workflows/evented/execution-engine.d.ts +3 -1
  468. package/dist/workflows/evented/execution-engine.d.ts.map +1 -1
  469. package/dist/workflows/evented/index.cjs +10 -10
  470. package/dist/workflows/evented/index.js +1 -1
  471. package/dist/workflows/evented/step-executor.d.ts +1 -1
  472. package/dist/workflows/evented/step-executor.d.ts.map +1 -1
  473. package/dist/workflows/evented/workflow-event-processor/index.d.ts +5 -4
  474. package/dist/workflows/evented/workflow-event-processor/index.d.ts.map +1 -1
  475. package/dist/workflows/evented/workflow-event-processor/loop.d.ts +1 -1
  476. package/dist/workflows/evented/workflow-event-processor/loop.d.ts.map +1 -1
  477. package/dist/workflows/evented/workflow-event-processor/parallel.d.ts +2 -2
  478. package/dist/workflows/evented/workflow-event-processor/parallel.d.ts.map +1 -1
  479. package/dist/workflows/evented/workflow-event-processor/sleep.d.ts +2 -2
  480. package/dist/workflows/evented/workflow-event-processor/sleep.d.ts.map +1 -1
  481. package/dist/workflows/evented/workflow.d.ts +2 -1
  482. package/dist/workflows/evented/workflow.d.ts.map +1 -1
  483. package/dist/workflows/execution-engine.d.ts +8 -6
  484. package/dist/workflows/execution-engine.d.ts.map +1 -1
  485. package/dist/workflows/handlers/control-flow.d.ts +133 -0
  486. package/dist/workflows/handlers/control-flow.d.ts.map +1 -0
  487. package/dist/workflows/handlers/entry.d.ts +43 -0
  488. package/dist/workflows/handlers/entry.d.ts.map +1 -0
  489. package/dist/workflows/handlers/sleep.d.ts +60 -0
  490. package/dist/workflows/handlers/sleep.d.ts.map +1 -0
  491. package/dist/workflows/handlers/step.d.ts +58 -0
  492. package/dist/workflows/handlers/step.d.ts.map +1 -0
  493. package/dist/workflows/index.cjs +36 -16
  494. package/dist/workflows/index.js +1 -1
  495. package/dist/workflows/step.d.ts +8 -6
  496. package/dist/workflows/step.d.ts.map +1 -1
  497. package/dist/workflows/types.d.ts +224 -10
  498. package/dist/workflows/types.d.ts.map +1 -1
  499. package/dist/workflows/utils.d.ts +36 -0
  500. package/dist/workflows/utils.d.ts.map +1 -1
  501. package/dist/workflows/workflow.d.ts +135 -54
  502. package/dist/workflows/workflow.d.ts.map +1 -1
  503. package/package.json +26 -29
  504. package/src/llm/model/provider-types.generated.d.ts +520 -33
  505. package/dist/chunk-26SQQNMU.js.map +0 -1
  506. package/dist/chunk-32CTMD2C.js.map +0 -1
  507. package/dist/chunk-436FFEF6.js +0 -34
  508. package/dist/chunk-436FFEF6.js.map +0 -1
  509. package/dist/chunk-4CDL2QJT.js.map +0 -1
  510. package/dist/chunk-7AHYOMHJ.js +0 -149
  511. package/dist/chunk-7AHYOMHJ.js.map +0 -1
  512. package/dist/chunk-7SKXKUYT.js.map +0 -1
  513. package/dist/chunk-BNBRQS7N.js +0 -910
  514. package/dist/chunk-BNBRQS7N.js.map +0 -1
  515. package/dist/chunk-BU4IAJWF.js.map +0 -1
  516. package/dist/chunk-BXOL277H.cjs.map +0 -1
  517. package/dist/chunk-DSNPWVIG.cjs.map +0 -1
  518. package/dist/chunk-E7K4FTLN.cjs.map +0 -1
  519. package/dist/chunk-ECFXGXWO.cjs.map +0 -1
  520. package/dist/chunk-ET6UOTTU.cjs +0 -154
  521. package/dist/chunk-ET6UOTTU.cjs.map +0 -1
  522. package/dist/chunk-FD734TPS.cjs.map +0 -1
  523. package/dist/chunk-GGYKYORQ.cjs.map +0 -1
  524. package/dist/chunk-GRBGQ2GE.js.map +0 -1
  525. package/dist/chunk-HGNRQ3OG.js +0 -58
  526. package/dist/chunk-HGNRQ3OG.js.map +0 -1
  527. package/dist/chunk-IU2SZXJQ.cjs +0 -913
  528. package/dist/chunk-IU2SZXJQ.cjs.map +0 -1
  529. package/dist/chunk-J7O6WENZ.cjs.map +0 -1
  530. package/dist/chunk-JV2KH24V.js.map +0 -1
  531. package/dist/chunk-KIZIOFZC.js.map +0 -1
  532. package/dist/chunk-KJ2SW6VA.js.map +0 -1
  533. package/dist/chunk-L7XKOKOW.js.map +0 -1
  534. package/dist/chunk-LJFJTTZQ.cjs +0 -4
  535. package/dist/chunk-LJFJTTZQ.cjs.map +0 -1
  536. package/dist/chunk-LRSB62Z6.cjs +0 -60
  537. package/dist/chunk-LRSB62Z6.cjs.map +0 -1
  538. package/dist/chunk-MV7KHWUT.js.map +0 -1
  539. package/dist/chunk-P6APHXPZ.js.map +0 -1
  540. package/dist/chunk-QCQLOMJM.cjs.map +0 -1
  541. package/dist/chunk-QUKUN6NR.cjs.map +0 -1
  542. package/dist/chunk-QUZGDSWE.cjs.map +0 -1
  543. package/dist/chunk-S5MJLXMG.cjs.map +0 -1
  544. package/dist/chunk-TQTAMPSC.js.map +0 -1
  545. package/dist/chunk-UIZSWUKP.js.map +0 -1
  546. package/dist/chunk-V3VLOOSW.cjs +0 -36
  547. package/dist/chunk-V3VLOOSW.cjs.map +0 -1
  548. package/dist/chunk-VJUZZB2I.js.map +0 -1
  549. package/dist/chunk-VOY2RXOC.cjs.map +0 -1
  550. package/dist/chunk-VV753WCB.cjs.map +0 -1
  551. package/dist/chunk-WM6CK2F3.cjs.map +0 -1
  552. package/dist/chunk-XEVG546F.js.map +0 -1
  553. package/dist/chunk-Y63IFHEZ.cjs.map +0 -1
  554. package/dist/chunk-YIK3ASEG.cjs.map +0 -1
  555. package/dist/chunk-ZV5CC35D.js +0 -3
  556. package/dist/chunk-ZV5CC35D.js.map +0 -1
  557. package/dist/models-dev-7U4NRMM3.js +0 -3
  558. package/dist/models-dev-VKSAQPRK.cjs +0 -12
  559. package/dist/netlify-2IDXTNFW.cjs +0 -12
  560. package/dist/netlify-42ZNWIDQ.js +0 -3
  561. package/dist/registry-generator-H4YNODDH.js.map +0 -1
  562. package/dist/registry-generator-MK63POJO.cjs.map +0 -1
@@ -173,7 +173,7 @@ function convertImageFilePart(part, downloadedAssets) {
173
173
  const { data: convertedData, mediaType: convertedMediaType } = convertToDataContent(originalData);
174
174
  let mediaType = convertedMediaType ?? part.mediaType;
175
175
  let data = convertedData;
176
- if (data instanceof URL) {
176
+ if (data instanceof URL && downloadedAssets) {
177
177
  const downloadedFile = downloadedAssets[data.toString()];
178
178
  if (downloadedFile) {
179
179
  data = downloadedFile.data;
@@ -208,14 +208,137 @@ function convertImageFilePart(part, downloadedAssets) {
208
208
  }
209
209
  }
210
210
  }
211
+ z.union([
212
+ z.string(),
213
+ z.instanceof(Uint8Array),
214
+ z.instanceof(ArrayBuffer),
215
+ z.custom(
216
+ // Buffer might not be available in some environments such as CloudFlare:
217
+ (value) => globalThis.Buffer?.isBuffer(value) ?? false,
218
+ { message: "Must be a Buffer" }
219
+ )
220
+ ]);
221
+ function convertDataContentToBase64String(content) {
222
+ if (typeof content === "string") {
223
+ return content;
224
+ }
225
+ if (content instanceof ArrayBuffer) {
226
+ return convertUint8ArrayToBase64$1(new Uint8Array(content));
227
+ }
228
+ return convertUint8ArrayToBase64$1(content);
229
+ }
230
+
231
+ // src/agent/message-list/prompt/image-utils.ts
232
+ function parseDataUri(dataUri) {
233
+ if (!dataUri.startsWith("data:")) {
234
+ return {
235
+ isDataUri: false,
236
+ base64Content: dataUri
237
+ };
238
+ }
239
+ const base64Index = dataUri.indexOf(",");
240
+ if (base64Index === -1) {
241
+ return {
242
+ isDataUri: true,
243
+ base64Content: dataUri
244
+ };
245
+ }
246
+ const header = dataUri.substring(5, base64Index);
247
+ const base64Content = dataUri.substring(base64Index + 1);
248
+ const semicolonIndex = header.indexOf(";");
249
+ const mimeType = semicolonIndex !== -1 ? header.substring(0, semicolonIndex) : header;
250
+ return {
251
+ isDataUri: true,
252
+ mimeType: mimeType || void 0,
253
+ base64Content
254
+ };
255
+ }
256
+ function createDataUri(base64Content, mimeType = "application/octet-stream") {
257
+ if (base64Content.startsWith("data:")) {
258
+ return base64Content;
259
+ }
260
+ return `data:${mimeType};base64,${base64Content}`;
261
+ }
262
+ function imageContentToString(image, fallbackMimeType) {
263
+ if (typeof image === "string") {
264
+ return image;
265
+ }
266
+ if (image instanceof URL) {
267
+ return image.toString();
268
+ }
269
+ if (image instanceof Uint8Array || image instanceof ArrayBuffer || globalThis.Buffer && Buffer.isBuffer(image)) {
270
+ const base64 = convertDataContentToBase64String(image);
271
+ return base64;
272
+ }
273
+ return String(image);
274
+ }
275
+ function getImageCacheKey(image) {
276
+ if (image instanceof URL) {
277
+ return image.toString();
278
+ }
279
+ if (typeof image === "string") {
280
+ return image.length;
281
+ }
282
+ if (image instanceof Uint8Array) {
283
+ return image.byteLength;
284
+ }
285
+ if (image instanceof ArrayBuffer) {
286
+ return image.byteLength;
287
+ }
288
+ return image;
289
+ }
290
+ function isValidUrl(str) {
291
+ try {
292
+ new URL(str);
293
+ return true;
294
+ } catch {
295
+ if (str.startsWith("//")) {
296
+ try {
297
+ new URL(`https:${str}`);
298
+ return true;
299
+ } catch {
300
+ return false;
301
+ }
302
+ }
303
+ return false;
304
+ }
305
+ }
306
+ function categorizeFileData(data, fallbackMimeType) {
307
+ const parsed = parseDataUri(data);
308
+ const mimeType = parsed.isDataUri && parsed.mimeType ? parsed.mimeType : fallbackMimeType;
309
+ if (parsed.isDataUri) {
310
+ return {
311
+ type: "dataUri",
312
+ mimeType,
313
+ data
314
+ };
315
+ }
316
+ if (isValidUrl(data)) {
317
+ return {
318
+ type: "url",
319
+ mimeType,
320
+ data
321
+ };
322
+ }
323
+ return {
324
+ type: "raw",
325
+ mimeType,
326
+ data
327
+ };
328
+ }
211
329
 
212
330
  // src/agent/message-list/prompt/attachments-to-parts.ts
213
331
  function attachmentsToParts(attachments) {
214
332
  const parts = [];
215
333
  for (const attachment of attachments) {
334
+ const categorized = categorizeFileData(attachment.url, attachment.contentType);
335
+ let urlString = attachment.url;
336
+ if (categorized.type === "raw") {
337
+ urlString = createDataUri(attachment.url, attachment.contentType || "application/octet-stream");
338
+ }
216
339
  let url;
217
340
  try {
218
- url = new URL(attachment.url);
341
+ url = new URL(urlString);
219
342
  } catch {
220
343
  throw new Error(`Invalid URL: ${attachment.url}`);
221
344
  }
@@ -240,13 +363,13 @@ function attachmentsToParts(attachments) {
240
363
  if (attachment.contentType?.startsWith("image/")) {
241
364
  parts.push({
242
365
  type: "image",
243
- image: attachment.url,
366
+ image: urlString,
244
367
  mimeType: attachment.contentType
245
368
  });
246
369
  } else if (attachment.contentType?.startsWith("text/")) {
247
370
  parts.push({
248
371
  type: "file",
249
- data: attachment.url,
372
+ data: urlString,
250
373
  mimeType: attachment.contentType
251
374
  });
252
375
  } else {
@@ -255,7 +378,7 @@ function attachmentsToParts(attachments) {
255
378
  }
256
379
  parts.push({
257
380
  type: "file",
258
- data: attachment.url,
381
+ data: urlString,
259
382
  mimeType: attachment.contentType
260
383
  });
261
384
  }
@@ -566,25 +689,6 @@ function convertToV1Messages(messages) {
566
689
  }
567
690
  return v1Messages;
568
691
  }
569
- z.union([
570
- z.string(),
571
- z.instanceof(Uint8Array),
572
- z.instanceof(ArrayBuffer),
573
- z.custom(
574
- // Buffer might not be available in some environments such as CloudFlare:
575
- (value) => globalThis.Buffer?.isBuffer(value) ?? false,
576
- { message: "Must be a Buffer" }
577
- )
578
- ]);
579
- function convertDataContentToBase64String(content) {
580
- if (typeof content === "string") {
581
- return content;
582
- }
583
- if (content instanceof ArrayBuffer) {
584
- return convertUint8ArrayToBase64$1(new Uint8Array(content));
585
- }
586
- return convertUint8ArrayToBase64$1(content);
587
- }
588
692
 
589
693
  // src/utils/fetchWithRetry.ts
590
694
  async function fetchWithRetry(url, options = {}, maxRetries = 3) {
@@ -675,18 +779,20 @@ async function downloadAssetsFromMessages({
675
779
  }
676
780
  }
677
781
  return { mediaType, data };
678
- }).filter((part) => part.data instanceof URL).map((part) => ({
679
- url: part.data,
680
- isUrlSupportedByModel: part.mediaType != null && isUrlSupported({
681
- url: part.data.toString(),
682
- mediaType: part.mediaType,
683
- supportedUrls: supportedUrls ?? {}
684
- })
685
- }));
782
+ }).filter((part) => part.data instanceof URL).map((part) => {
783
+ return {
784
+ url: part.data,
785
+ isUrlSupportedByModel: part.mediaType != null && isUrlSupported({
786
+ url: part.data.toString(),
787
+ mediaType: part.mediaType,
788
+ supportedUrls: supportedUrls ?? {}
789
+ })
790
+ };
791
+ });
686
792
  const downloadedFiles = await pMap(
687
793
  filesToDownload,
688
794
  async (fileItem) => {
689
- if (!fileItem.isUrlSupportedByModel) {
795
+ if (fileItem.isUrlSupportedByModel) {
690
796
  return null;
691
797
  }
692
798
  return {
@@ -704,105 +810,6 @@ async function downloadAssetsFromMessages({
704
810
  return Object.fromEntries(downloadFileList);
705
811
  }
706
812
 
707
- // src/agent/message-list/prompt/image-utils.ts
708
- function parseDataUri(dataUri) {
709
- if (!dataUri.startsWith("data:")) {
710
- return {
711
- isDataUri: false,
712
- base64Content: dataUri
713
- };
714
- }
715
- const base64Index = dataUri.indexOf(",");
716
- if (base64Index === -1) {
717
- return {
718
- isDataUri: true,
719
- base64Content: dataUri
720
- };
721
- }
722
- const header = dataUri.substring(5, base64Index);
723
- const base64Content = dataUri.substring(base64Index + 1);
724
- const semicolonIndex = header.indexOf(";");
725
- const mimeType = semicolonIndex !== -1 ? header.substring(0, semicolonIndex) : header;
726
- return {
727
- isDataUri: true,
728
- mimeType: mimeType || void 0,
729
- base64Content
730
- };
731
- }
732
- function createDataUri(base64Content, mimeType = "application/octet-stream") {
733
- if (base64Content.startsWith("data:")) {
734
- return base64Content;
735
- }
736
- return `data:${mimeType};base64,${base64Content}`;
737
- }
738
- function imageContentToString(image, fallbackMimeType) {
739
- if (typeof image === "string") {
740
- return image;
741
- }
742
- if (image instanceof URL) {
743
- return image.toString();
744
- }
745
- if (image instanceof Uint8Array || image instanceof ArrayBuffer || globalThis.Buffer && Buffer.isBuffer(image)) {
746
- const base64 = convertDataContentToBase64String(image);
747
- return base64;
748
- }
749
- return String(image);
750
- }
751
- function getImageCacheKey(image) {
752
- if (image instanceof URL) {
753
- return image.toString();
754
- }
755
- if (typeof image === "string") {
756
- return image.length;
757
- }
758
- if (image instanceof Uint8Array) {
759
- return image.byteLength;
760
- }
761
- if (image instanceof ArrayBuffer) {
762
- return image.byteLength;
763
- }
764
- return image;
765
- }
766
- function isValidUrl(str) {
767
- try {
768
- new URL(str);
769
- return true;
770
- } catch {
771
- if (str.startsWith("//")) {
772
- try {
773
- new URL(`https:${str}`);
774
- return true;
775
- } catch {
776
- return false;
777
- }
778
- }
779
- return false;
780
- }
781
- }
782
- function categorizeFileData(data, fallbackMimeType) {
783
- const parsed = parseDataUri(data);
784
- const mimeType = parsed.isDataUri && parsed.mimeType ? parsed.mimeType : fallbackMimeType;
785
- if (parsed.isDataUri) {
786
- return {
787
- type: "dataUri",
788
- mimeType,
789
- data
790
- };
791
- }
792
- if (isValidUrl(data)) {
793
- return {
794
- type: "url",
795
- mimeType,
796
- data
797
- };
798
- }
799
- return {
800
- type: "raw",
801
- mimeType,
802
- data
803
- };
804
- }
805
-
806
813
  // src/agent/message-list/utils/ai-v5/gemini-compatibility.ts
807
814
  function ensureGeminiCompatibleMessages(messages) {
808
815
  const result = [...messages];
@@ -870,6 +877,9 @@ function convertMessages(messages) {
870
877
  }
871
878
 
872
879
  // src/agent/message-list/index.ts
880
+ function filterDataParts(parts) {
881
+ return parts.filter((part) => !part.type.startsWith("data-"));
882
+ }
873
883
  var MessageList = class _MessageList {
874
884
  messages = [];
875
885
  // passed in by dev in input or context
@@ -888,6 +898,9 @@ var MessageList = class _MessageList {
888
898
  userContextMessagesPersisted = /* @__PURE__ */ new Set();
889
899
  generateMessageId;
890
900
  _agentNetworkAppend = false;
901
+ // Event recording for observability
902
+ isRecording = false;
903
+ recordedEvents = [];
891
904
  constructor({
892
905
  threadId,
893
906
  resourceId,
@@ -901,10 +914,34 @@ var MessageList = class _MessageList {
901
914
  this.generateMessageId = generateMessageId;
902
915
  this._agentNetworkAppend = _agentNetworkAppend || false;
903
916
  }
917
+ /**
918
+ * Start recording mutations to the MessageList for observability/tracing
919
+ */
920
+ startRecording() {
921
+ this.isRecording = true;
922
+ this.recordedEvents = [];
923
+ }
924
+ /**
925
+ * Stop recording and return the list of recorded events
926
+ */
927
+ stopRecording() {
928
+ this.isRecording = false;
929
+ const events = [...this.recordedEvents];
930
+ this.recordedEvents = [];
931
+ return events;
932
+ }
904
933
  add(messages, messageSource) {
905
934
  if (messageSource === `user`) messageSource = `input`;
906
935
  if (!messages) return this;
907
- for (const message of Array.isArray(messages) ? messages : [messages]) {
936
+ const messageArray = Array.isArray(messages) ? messages : [messages];
937
+ if (this.isRecording) {
938
+ this.recordedEvents.push({
939
+ type: "add",
940
+ source: messageSource,
941
+ count: messageArray.length
942
+ });
943
+ }
944
+ for (const message of messageArray) {
908
945
  this.addOne(
909
946
  typeof message === `string` ? {
910
947
  role: "user",
@@ -966,6 +1003,22 @@ var MessageList = class _MessageList {
966
1003
  this.userContextMessagesPersisted = this.deserializeSet(state.userContextMessagesPersisted);
967
1004
  return this;
968
1005
  }
1006
+ makeMessageSourceChecker() {
1007
+ const sources = {
1008
+ memory: new Set(Array.from(this.memoryMessages.values()).map((m) => m.id)),
1009
+ output: new Set(Array.from(this.newResponseMessages.values()).map((m) => m.id)),
1010
+ input: new Set(Array.from(this.newUserMessages.values()).map((m) => m.id))
1011
+ };
1012
+ return {
1013
+ ...sources,
1014
+ getSource: (msg) => {
1015
+ if (sources.memory.has(msg.id)) return "memory";
1016
+ if (sources.input.has(msg.id)) return "input";
1017
+ if (sources.output.has(msg.id)) return "response";
1018
+ return null;
1019
+ }
1020
+ };
1021
+ }
969
1022
  getLatestUserContent() {
970
1023
  const currentUserMessages = this.all.core().filter((m) => m.role === "user");
971
1024
  const content = currentUserMessages.at(-1)?.content;
@@ -990,11 +1043,34 @@ var MessageList = class _MessageList {
990
1043
  }
991
1044
  get clear() {
992
1045
  return {
1046
+ all: {
1047
+ db: () => {
1048
+ const allMessages = [...this.messages];
1049
+ this.messages = [];
1050
+ this.newUserMessages.clear();
1051
+ this.newResponseMessages.clear();
1052
+ this.userContextMessages.clear();
1053
+ if (this.isRecording && allMessages.length > 0) {
1054
+ this.recordedEvents.push({
1055
+ type: "clear",
1056
+ count: allMessages.length
1057
+ });
1058
+ }
1059
+ return allMessages;
1060
+ }
1061
+ },
993
1062
  input: {
994
1063
  db: () => {
995
1064
  const userMessages = Array.from(this.newUserMessages);
996
1065
  this.messages = this.messages.filter((m) => !this.newUserMessages.has(m));
997
1066
  this.newUserMessages.clear();
1067
+ if (this.isRecording && userMessages.length > 0) {
1068
+ this.recordedEvents.push({
1069
+ type: "clear",
1070
+ source: "input",
1071
+ count: userMessages.length
1072
+ });
1073
+ }
998
1074
  return userMessages;
999
1075
  }
1000
1076
  },
@@ -1003,11 +1079,46 @@ var MessageList = class _MessageList {
1003
1079
  const responseMessages = Array.from(this.newResponseMessages);
1004
1080
  this.messages = this.messages.filter((m) => !this.newResponseMessages.has(m));
1005
1081
  this.newResponseMessages.clear();
1082
+ if (this.isRecording && responseMessages.length > 0) {
1083
+ this.recordedEvents.push({
1084
+ type: "clear",
1085
+ source: "response",
1086
+ count: responseMessages.length
1087
+ });
1088
+ }
1006
1089
  return responseMessages;
1007
1090
  }
1008
1091
  }
1009
1092
  };
1010
1093
  }
1094
+ /**
1095
+ * Remove messages by ID
1096
+ * @param ids - Array of message IDs to remove
1097
+ * @returns Array of removed messages
1098
+ */
1099
+ removeByIds(ids) {
1100
+ const idsSet = new Set(ids);
1101
+ const removed = [];
1102
+ this.messages = this.messages.filter((m) => {
1103
+ if (idsSet.has(m.id)) {
1104
+ removed.push(m);
1105
+ this.memoryMessages.delete(m);
1106
+ this.newUserMessages.delete(m);
1107
+ this.newResponseMessages.delete(m);
1108
+ this.userContextMessages.delete(m);
1109
+ return false;
1110
+ }
1111
+ return true;
1112
+ });
1113
+ if (this.isRecording && removed.length > 0) {
1114
+ this.recordedEvents.push({
1115
+ type: "removeByIds",
1116
+ ids,
1117
+ count: removed.length
1118
+ });
1119
+ }
1120
+ return removed;
1121
+ }
1011
1122
  all = {
1012
1123
  db: () => this.messages,
1013
1124
  v1: () => convertToV1Messages(this.all.db()),
@@ -1041,7 +1152,10 @@ var MessageList = class _MessageList {
1041
1152
  supportedUrls: options?.supportedUrls
1042
1153
  });
1043
1154
  let messages = [...systemMessages, ...modelMessages];
1044
- if (Object.keys(downloadedAssets || {}).length > 0) {
1155
+ const hasImageOrFileContent = modelMessages.some(
1156
+ (message) => message.role === "user" && typeof message.content !== "string" && message.content.some((part) => part.type === "image" || part.type === "file")
1157
+ );
1158
+ if (hasImageOrFileContent) {
1045
1159
  messages = messages.map((message) => {
1046
1160
  if (message.role === "user") {
1047
1161
  if (typeof message.content === "string") {
@@ -1308,12 +1422,47 @@ var MessageList = class _MessageList {
1308
1422
  if (unsavedMessages.length === 0) return void 0;
1309
1423
  return Math.min(...unsavedMessages.map((m) => new Date(m.createdAt).getTime()));
1310
1424
  }
1425
+ /**
1426
+ * Check if a message is a new user or response message that should be saved.
1427
+ * Checks by message ID to handle cases where the message object may be a copy.
1428
+ */
1429
+ isNewMessage(messageOrId) {
1430
+ const id = typeof messageOrId === "string" ? messageOrId : messageOrId.id;
1431
+ if (typeof messageOrId !== "string") {
1432
+ if (this.newUserMessages.has(messageOrId) || this.newResponseMessages.has(messageOrId)) {
1433
+ return true;
1434
+ }
1435
+ }
1436
+ return Array.from(this.newUserMessages).some((m) => m.id === id) || Array.from(this.newResponseMessages).some((m) => m.id === id);
1437
+ }
1311
1438
  getSystemMessages(tag) {
1312
1439
  if (tag) {
1313
1440
  return this.taggedSystemMessages[tag] || [];
1314
1441
  }
1315
1442
  return this.systemMessages;
1316
1443
  }
1444
+ /**
1445
+ * Get all system messages (both tagged and untagged)
1446
+ * @returns Array of all system messages
1447
+ */
1448
+ getAllSystemMessages() {
1449
+ return [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()];
1450
+ }
1451
+ /**
1452
+ * Replace all system messages with new ones
1453
+ * This clears both tagged and untagged system messages and replaces them with the provided array
1454
+ * @param messages - Array of system messages to set
1455
+ */
1456
+ replaceAllSystemMessages(messages) {
1457
+ this.systemMessages = [];
1458
+ this.taggedSystemMessages = {};
1459
+ for (const message of messages) {
1460
+ if (message.role === "system") {
1461
+ this.systemMessages.push(message);
1462
+ }
1463
+ }
1464
+ return this;
1465
+ }
1317
1466
  addSystem(messages, tag) {
1318
1467
  if (!messages) return this;
1319
1468
  for (const message of Array.isArray(messages) ? messages : [messages]) {
@@ -1372,8 +1521,21 @@ var MessageList = class _MessageList {
1372
1521
  if (tag && !this.isDuplicateSystem(coreMessage, tag)) {
1373
1522
  this.taggedSystemMessages[tag] ||= [];
1374
1523
  this.taggedSystemMessages[tag].push(coreMessage);
1524
+ if (this.isRecording) {
1525
+ this.recordedEvents.push({
1526
+ type: "addSystem",
1527
+ tag,
1528
+ message: coreMessage
1529
+ });
1530
+ }
1375
1531
  } else if (!tag && !this.isDuplicateSystem(coreMessage)) {
1376
1532
  this.systemMessages.push(coreMessage);
1533
+ if (this.isRecording) {
1534
+ this.recordedEvents.push({
1535
+ type: "addSystem",
1536
+ message: coreMessage
1537
+ });
1538
+ }
1377
1539
  }
1378
1540
  }
1379
1541
  isDuplicateSystem(message, tag) {
@@ -1399,9 +1561,20 @@ var MessageList = class _MessageList {
1399
1561
  if (m.content.parts.length) {
1400
1562
  for (const part of m.content.parts) {
1401
1563
  if (part.type === `file`) {
1564
+ let normalizedUrl;
1565
+ if (typeof part.data === "string") {
1566
+ const categorized = categorizeFileData(part.data, part.mimeType);
1567
+ if (categorized.type === "raw") {
1568
+ normalizedUrl = createDataUri(part.data, part.mimeType || "application/octet-stream");
1569
+ } else {
1570
+ normalizedUrl = part.data;
1571
+ }
1572
+ } else {
1573
+ normalizedUrl = part.data;
1574
+ }
1402
1575
  experimentalAttachments.push({
1403
1576
  contentType: part.mimeType,
1404
- url: part.data
1577
+ url: normalizedUrl
1405
1578
  });
1406
1579
  } else if (part.type === "tool-invocation" && (part.toolInvocation.state === "call" || part.toolInvocation.state === "partial-call")) {
1407
1580
  continue;
@@ -1439,13 +1612,14 @@ var MessageList = class _MessageList {
1439
1612
  if (parts.length === 0 && experimentalAttachments.length > 0) {
1440
1613
  parts.push({ type: "text", text: "" });
1441
1614
  }
1615
+ const v4Parts = filterDataParts(parts);
1442
1616
  if (m.role === `user`) {
1443
1617
  const uiMessage2 = {
1444
1618
  id: m.id,
1445
1619
  role: m.role,
1446
1620
  content: m.content.content || contentString,
1447
1621
  createdAt: m.createdAt,
1448
- parts,
1622
+ parts: v4Parts,
1449
1623
  experimental_attachments: experimentalAttachments
1450
1624
  };
1451
1625
  if (m.content.metadata) {
@@ -1459,7 +1633,7 @@ var MessageList = class _MessageList {
1459
1633
  role: m.role,
1460
1634
  content: isSingleTextContentArray ? contentString : m.content.content || contentString,
1461
1635
  createdAt: m.createdAt,
1462
- parts,
1636
+ parts: v4Parts,
1463
1637
  reasoning: void 0,
1464
1638
  toolInvocations: `toolInvocations` in m.content ? m.content.toolInvocations?.filter((t) => t.state === "result") : void 0
1465
1639
  };
@@ -1473,7 +1647,7 @@ var MessageList = class _MessageList {
1473
1647
  role: m.role,
1474
1648
  content: m.content.content || contentString,
1475
1649
  createdAt: m.createdAt,
1476
- parts,
1650
+ parts: v4Parts,
1477
1651
  experimental_attachments: experimentalAttachments
1478
1652
  };
1479
1653
  if (m.content.metadata) {
@@ -1642,6 +1816,9 @@ var MessageList = class _MessageList {
1642
1816
  } else if (messageSource === `response`) {
1643
1817
  this.newResponseMessages.add(messageV2);
1644
1818
  this.newResponseMessagesPersisted.add(messageV2);
1819
+ if (this.newUserMessages.has(messageV2)) {
1820
+ this.newUserMessages.delete(messageV2);
1821
+ }
1645
1822
  } else if (messageSource === `input`) {
1646
1823
  this.newUserMessages.add(messageV2);
1647
1824
  this.newUserMessagesPersisted.add(messageV2);
@@ -1666,13 +1843,11 @@ var MessageList = class _MessageList {
1666
1843
  insertAt
1667
1844
  // optional
1668
1845
  }) {
1669
- const partKey = _MessageList.cacheKeyFromAIV4Parts([part]);
1846
+ const partKey = _MessageList.cacheKeyFromDBParts([part]);
1670
1847
  const latestPartCount = latestMessage.content.parts.filter(
1671
- (p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
1672
- ).length;
1673
- const newPartCount = newMessage.content.parts.filter(
1674
- (p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
1848
+ (p) => _MessageList.cacheKeyFromDBParts([p]) === partKey
1675
1849
  ).length;
1850
+ const newPartCount = newMessage.content.parts.filter((p) => _MessageList.cacheKeyFromDBParts([p]) === partKey).length;
1676
1851
  if (latestPartCount < newPartCount) {
1677
1852
  const partIndex = newMessage.content.parts.indexOf(part);
1678
1853
  const hasStepStartBefore = partIndex > 0 && newMessage.content.parts[partIndex - 1]?.type === "step-start";
@@ -1708,7 +1883,7 @@ var MessageList = class _MessageList {
1708
1883
  for (let i = 0; i < messageV2.content.parts.length; ++i) {
1709
1884
  const part = messageV2.content.parts[i];
1710
1885
  if (!part) continue;
1711
- const key = _MessageList.cacheKeyFromAIV4Parts([part]);
1886
+ const key = _MessageList.cacheKeyFromDBParts([part]);
1712
1887
  const partToAdd = partsToAdd.get(i);
1713
1888
  if (!key || !partToAdd) continue;
1714
1889
  if (anchorMap.size > 0) {
@@ -1719,7 +1894,7 @@ var MessageList = class _MessageList {
1719
1894
  const offset = leftAnchorV2 === -1 ? i : i - leftAnchorV2;
1720
1895
  const insertAt = leftAnchorLatest + offset;
1721
1896
  const rightAnchorLatest = rightAnchorV2 !== -1 ? anchorMap.get(rightAnchorV2) : latestMessage.content.parts.length;
1722
- if (insertAt >= 0 && insertAt <= rightAnchorLatest && !latestMessage.content.parts.slice(insertAt, rightAnchorLatest).some((p) => _MessageList.cacheKeyFromAIV4Parts([p]) === _MessageList.cacheKeyFromAIV4Parts([part]))) {
1897
+ if (insertAt >= 0 && insertAt <= rightAnchorLatest && !latestMessage.content.parts.slice(insertAt, rightAnchorLatest).some((p) => _MessageList.cacheKeyFromDBParts([p]) === _MessageList.cacheKeyFromDBParts([part]))) {
1723
1898
  this.pushNewMessagePart({
1724
1899
  latestMessage,
1725
1900
  newMessage: messageV2,
@@ -1770,8 +1945,10 @@ var MessageList = class _MessageList {
1770
1945
  }
1771
1946
  if (_MessageList.isAIV5CoreMessage(message)) {
1772
1947
  const dbMsg = _MessageList.aiV5ModelMessageToMastraDBMessage(message, messageSource);
1948
+ const rawCreatedAt = "metadata" in message && message.metadata && typeof message.metadata === "object" && "createdAt" in message.metadata ? message.metadata.createdAt : void 0;
1773
1949
  const result = {
1774
1950
  ...dbMsg,
1951
+ createdAt: this.generateCreatedAt(messageSource, rawCreatedAt),
1775
1952
  threadId: this.memoryInfo?.threadId,
1776
1953
  resourceId: this.memoryInfo?.resourceId
1777
1954
  };
@@ -1779,8 +1956,10 @@ var MessageList = class _MessageList {
1779
1956
  }
1780
1957
  if (_MessageList.isAIV5UIMessage(message)) {
1781
1958
  const dbMsg = _MessageList.aiV5UIMessageToMastraDBMessage(message);
1959
+ const rawCreatedAt = "createdAt" in message ? message.createdAt : void 0;
1782
1960
  return {
1783
1961
  ...dbMsg,
1962
+ createdAt: this.generateCreatedAt(messageSource, rawCreatedAt),
1784
1963
  threadId: this.memoryInfo?.threadId,
1785
1964
  resourceId: this.memoryInfo?.resourceId
1786
1965
  };
@@ -1790,16 +1969,16 @@ var MessageList = class _MessageList {
1790
1969
  lastCreatedAt;
1791
1970
  // this makes sure messages added in order will always have a date atleast 1ms apart.
1792
1971
  generateCreatedAt(messageSource, start) {
1793
- start = start instanceof Date ? start : start ? new Date(start) : void 0;
1794
- if (start && !this.lastCreatedAt) {
1795
- this.lastCreatedAt = start.getTime();
1796
- return start;
1972
+ const startDate = start instanceof Date ? start : typeof start === "string" || typeof start === "number" ? new Date(start) : void 0;
1973
+ if (startDate && !this.lastCreatedAt) {
1974
+ this.lastCreatedAt = startDate.getTime();
1975
+ return startDate;
1797
1976
  }
1798
- if (start && messageSource === `memory`) {
1799
- return start;
1977
+ if (startDate && messageSource === `memory`) {
1978
+ return startDate;
1800
1979
  }
1801
1980
  const now = /* @__PURE__ */ new Date();
1802
- const nowTime = start?.getTime() || now.getTime();
1981
+ const nowTime = startDate?.getTime() || now.getTime();
1803
1982
  const lastTime = this.messages.reduce((p, m) => {
1804
1983
  if (m.createdAt.getTime() > p) return m.createdAt.getTime();
1805
1984
  return p;
@@ -1853,6 +2032,12 @@ var MessageList = class _MessageList {
1853
2032
  return ti;
1854
2033
  });
1855
2034
  }
2035
+ if (!message.threadId && this.memoryInfo?.threadId) {
2036
+ message.threadId = this.memoryInfo.threadId;
2037
+ if (!message.resourceId && this.memoryInfo?.resourceId) {
2038
+ message.resourceId = this.memoryInfo.resourceId;
2039
+ }
2040
+ }
1856
2041
  return message;
1857
2042
  }
1858
2043
  aiV4UIMessageToMastraDBMessage(message, messageSource) {
@@ -1890,137 +2075,180 @@ var MessageList = class _MessageList {
1890
2075
  if (typeof coreMessage.content === "string") {
1891
2076
  parts.push({
1892
2077
  type: "text",
1893
- text: coreMessage.content,
1894
- // Preserve providerOptions from CoreMessage (e.g., for system messages with cacheControl)
1895
- ..."providerOptions" in coreMessage && coreMessage.providerOptions ? { providerMetadata: coreMessage.providerOptions } : {}
2078
+ text: coreMessage.content
1896
2079
  });
1897
2080
  } else if (Array.isArray(coreMessage.content)) {
1898
- for (const part of coreMessage.content) {
1899
- switch (part.type) {
1900
- case "text":
2081
+ for (const aiV4Part of coreMessage.content) {
2082
+ switch (aiV4Part.type) {
2083
+ case "text": {
1901
2084
  const prevPart = parts.at(-1);
1902
2085
  if (coreMessage.role === "assistant" && prevPart && prevPart.type === "tool-invocation") {
1903
2086
  parts.push({ type: "step-start" });
1904
2087
  }
1905
- const mergedProviderMetadata = {
1906
- ..."providerOptions" in coreMessage && coreMessage.providerOptions ? coreMessage.providerOptions : {},
1907
- ..."providerOptions" in part && part.providerOptions ? part.providerOptions : {}
1908
- };
1909
- parts.push({
2088
+ const part = {
1910
2089
  type: "text",
1911
- text: part.text,
1912
- ...Object.keys(mergedProviderMetadata).length > 0 ? { providerMetadata: mergedProviderMetadata } : {}
1913
- });
2090
+ text: aiV4Part.text
2091
+ };
2092
+ if (aiV4Part.providerOptions) {
2093
+ part.providerMetadata = aiV4Part.providerOptions;
2094
+ }
2095
+ parts.push(part);
1914
2096
  break;
1915
- case "tool-call":
1916
- parts.push({
2097
+ }
2098
+ case "tool-call": {
2099
+ const part = {
1917
2100
  type: "tool-invocation",
1918
2101
  toolInvocation: {
1919
2102
  state: "call",
1920
- toolCallId: part.toolCallId,
1921
- toolName: part.toolName,
1922
- args: part.args
2103
+ toolCallId: aiV4Part.toolCallId,
2104
+ toolName: aiV4Part.toolName,
2105
+ args: aiV4Part.args
1923
2106
  }
1924
- });
2107
+ };
2108
+ if (aiV4Part.providerOptions) {
2109
+ part.providerMetadata = aiV4Part.providerOptions;
2110
+ }
2111
+ parts.push(part);
1925
2112
  break;
2113
+ }
1926
2114
  case "tool-result":
1927
- let toolArgs = {};
1928
- const toolCallInSameMsg = coreMessage.content.find(
1929
- (p) => p.type === "tool-call" && p.toolCallId === part.toolCallId
1930
- );
1931
- if (toolCallInSameMsg && toolCallInSameMsg.type === "tool-call") {
1932
- toolArgs = toolCallInSameMsg.args;
1933
- }
1934
- if (Object.keys(toolArgs).length === 0) {
1935
- for (let i = this.messages.length - 1; i >= 0; i--) {
1936
- const msg = this.messages[i];
1937
- if (msg && msg.role === "assistant" && msg.content.parts) {
1938
- const toolCallPart = msg.content.parts.find(
1939
- (p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === part.toolCallId && p.toolInvocation.state === "call"
1940
- );
1941
- if (toolCallPart && toolCallPart.type === "tool-invocation" && toolCallPart.toolInvocation.args) {
1942
- toolArgs = toolCallPart.toolInvocation.args;
1943
- break;
2115
+ {
2116
+ let toolArgs = {};
2117
+ const toolCallInSameMsg = coreMessage.content.find(
2118
+ (p) => p.type === "tool-call" && p.toolCallId === aiV4Part.toolCallId
2119
+ );
2120
+ if (toolCallInSameMsg && toolCallInSameMsg.type === "tool-call") {
2121
+ toolArgs = toolCallInSameMsg.args;
2122
+ }
2123
+ if (Object.keys(toolArgs).length === 0) {
2124
+ for (let i = this.messages.length - 1; i >= 0; i--) {
2125
+ const msg = this.messages[i];
2126
+ if (msg && msg.role === "assistant" && msg.content.parts) {
2127
+ const toolCallPart = msg.content.parts.find(
2128
+ (p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === aiV4Part.toolCallId && p.toolInvocation.state === "call"
2129
+ );
2130
+ if (toolCallPart && toolCallPart.type === "tool-invocation" && toolCallPart.toolInvocation.args) {
2131
+ toolArgs = toolCallPart.toolInvocation.args;
2132
+ break;
2133
+ }
1944
2134
  }
1945
2135
  }
1946
2136
  }
2137
+ const invocation = {
2138
+ state: "result",
2139
+ toolCallId: aiV4Part.toolCallId,
2140
+ toolName: aiV4Part.toolName,
2141
+ result: aiV4Part.result ?? "",
2142
+ // undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
2143
+ args: toolArgs
2144
+ // Use the args from the corresponding tool-call
2145
+ };
2146
+ const part = {
2147
+ type: "tool-invocation",
2148
+ toolInvocation: invocation
2149
+ };
2150
+ if (aiV4Part.providerOptions) {
2151
+ part.providerMetadata = aiV4Part.providerOptions;
2152
+ }
2153
+ parts.push(part);
2154
+ toolInvocations.push(invocation);
1947
2155
  }
1948
- const invocation = {
1949
- state: "result",
1950
- toolCallId: part.toolCallId,
1951
- toolName: part.toolName,
1952
- result: part.result ?? "",
1953
- // undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
1954
- args: toolArgs
1955
- // Use the args from the corresponding tool-call
1956
- };
1957
- parts.push({
1958
- type: "tool-invocation",
1959
- toolInvocation: invocation
1960
- });
1961
- toolInvocations.push(invocation);
1962
2156
  break;
1963
2157
  case "reasoning":
1964
- parts.push({
1965
- type: "reasoning",
1966
- reasoning: "",
1967
- // leave this blank so we aren't double storing it in the db along with details
1968
- details: [{ type: "text", text: part.text, signature: part.signature }]
1969
- });
2158
+ {
2159
+ const part = {
2160
+ type: "reasoning",
2161
+ reasoning: "",
2162
+ // leave this blank so we aren't double storing it in the db along with details
2163
+ details: [{ type: "text", text: aiV4Part.text, signature: aiV4Part.signature }]
2164
+ };
2165
+ if (aiV4Part.providerOptions) {
2166
+ part.providerMetadata = aiV4Part.providerOptions;
2167
+ }
2168
+ parts.push(part);
2169
+ }
1970
2170
  break;
1971
2171
  case "redacted-reasoning":
1972
- parts.push({
1973
- type: "reasoning",
1974
- reasoning: "",
1975
- // No text reasoning for redacted parts
1976
- details: [{ type: "redacted", data: part.data }]
1977
- });
2172
+ {
2173
+ const part = {
2174
+ type: "reasoning",
2175
+ reasoning: "",
2176
+ // No text reasoning for redacted parts
2177
+ details: [{ type: "redacted", data: aiV4Part.data }]
2178
+ };
2179
+ if (aiV4Part.providerOptions) {
2180
+ part.providerMetadata = aiV4Part.providerOptions;
2181
+ }
2182
+ parts.push(part);
2183
+ }
1978
2184
  break;
1979
- case "image":
1980
- parts.push({
2185
+ case "image": {
2186
+ const part = {
1981
2187
  type: "file",
1982
- data: imageContentToString(part.image),
1983
- mimeType: part.mimeType
1984
- });
2188
+ data: imageContentToString(aiV4Part.image),
2189
+ mimeType: aiV4Part.mimeType
2190
+ };
2191
+ if (aiV4Part.providerOptions) {
2192
+ part.providerMetadata = aiV4Part.providerOptions;
2193
+ }
2194
+ parts.push(part);
1985
2195
  break;
1986
- case "file":
1987
- if (part.data instanceof URL) {
1988
- parts.push({
2196
+ }
2197
+ case "file": {
2198
+ if (aiV4Part.data instanceof URL) {
2199
+ const part = {
1989
2200
  type: "file",
1990
- data: part.data.toString(),
1991
- mimeType: part.mimeType
1992
- });
1993
- } else if (typeof part.data === "string") {
1994
- const categorized = categorizeFileData(part.data, part.mimeType);
2201
+ data: aiV4Part.data.toString(),
2202
+ mimeType: aiV4Part.mimeType
2203
+ };
2204
+ if (aiV4Part.providerOptions) {
2205
+ part.providerMetadata = aiV4Part.providerOptions;
2206
+ }
2207
+ parts.push(part);
2208
+ } else if (typeof aiV4Part.data === "string") {
2209
+ const categorized = categorizeFileData(aiV4Part.data, aiV4Part.mimeType);
1995
2210
  if (categorized.type === "url" || categorized.type === "dataUri") {
1996
- parts.push({
2211
+ const part = {
1997
2212
  type: "file",
1998
- data: part.data,
2213
+ data: aiV4Part.data,
1999
2214
  mimeType: categorized.mimeType || "image/png"
2000
- });
2215
+ };
2216
+ if (aiV4Part.providerOptions) {
2217
+ part.providerMetadata = aiV4Part.providerOptions;
2218
+ }
2219
+ parts.push(part);
2001
2220
  } else {
2002
2221
  try {
2003
- parts.push({
2222
+ const part = {
2004
2223
  type: "file",
2005
2224
  mimeType: categorized.mimeType || "image/png",
2006
- data: convertDataContentToBase64String(part.data)
2007
- });
2225
+ data: convertDataContentToBase64String(aiV4Part.data)
2226
+ };
2227
+ if (aiV4Part.providerOptions) {
2228
+ part.providerMetadata = aiV4Part.providerOptions;
2229
+ }
2230
+ parts.push(part);
2008
2231
  } catch (error) {
2009
2232
  console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2010
2233
  }
2011
2234
  }
2012
2235
  } else {
2013
2236
  try {
2014
- parts.push({
2237
+ const part = {
2015
2238
  type: "file",
2016
- mimeType: part.mimeType,
2017
- data: convertDataContentToBase64String(part.data)
2018
- });
2239
+ mimeType: aiV4Part.mimeType,
2240
+ data: convertDataContentToBase64String(aiV4Part.data)
2241
+ };
2242
+ if (aiV4Part.providerOptions) {
2243
+ part.providerMetadata = aiV4Part.providerOptions;
2244
+ }
2245
+ parts.push(part);
2019
2246
  } catch (error) {
2020
2247
  console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2021
2248
  }
2022
2249
  }
2023
2250
  break;
2251
+ }
2024
2252
  }
2025
2253
  }
2026
2254
  }
@@ -2031,10 +2259,17 @@ var MessageList = class _MessageList {
2031
2259
  if (toolInvocations.length) content.toolInvocations = toolInvocations;
2032
2260
  if (typeof coreMessage.content === `string`) content.content = coreMessage.content;
2033
2261
  if (experimentalAttachments.length) content.experimental_attachments = experimentalAttachments;
2262
+ if (coreMessage.providerOptions) {
2263
+ content.providerMetadata = coreMessage.providerOptions;
2264
+ }
2265
+ if ("metadata" in coreMessage && coreMessage.metadata !== null && coreMessage.metadata !== void 0) {
2266
+ content.metadata = coreMessage.metadata;
2267
+ }
2268
+ const rawCreatedAt = "metadata" in coreMessage && coreMessage.metadata && typeof coreMessage.metadata === "object" && "createdAt" in coreMessage.metadata ? coreMessage.metadata.createdAt : void 0;
2034
2269
  return {
2035
2270
  id,
2036
2271
  role: _MessageList.getRole(coreMessage),
2037
- createdAt: this.generateCreatedAt(messageSource),
2272
+ createdAt: this.generateCreatedAt(messageSource, rawCreatedAt),
2038
2273
  threadId: this.memoryInfo?.threadId,
2039
2274
  resourceId: this.memoryInfo?.resourceId,
2040
2275
  content
@@ -2072,25 +2307,48 @@ var MessageList = class _MessageList {
2072
2307
  let key = ``;
2073
2308
  for (const part of parts) {
2074
2309
  key += part.type;
2075
- if (part.type === `text`) {
2076
- key += part.text;
2077
- }
2078
- if (part.type === `tool-invocation`) {
2079
- key += part.toolInvocation.toolCallId;
2080
- key += part.toolInvocation.state;
2081
- }
2082
- if (part.type === `reasoning`) {
2083
- key += part.reasoning;
2084
- key += part.details.reduce((prev, current) => {
2085
- if (current.type === `text`) {
2086
- return prev + current.text.length + (current.signature?.length || 0);
2087
- }
2088
- return prev;
2089
- }, 0);
2310
+ key += _MessageList.cacheKeyFromAIV4Part(part);
2311
+ }
2312
+ return key;
2313
+ }
2314
+ static cacheKeyFromAIV4Part(part) {
2315
+ let cacheKey = "";
2316
+ if (part.type === `text`) {
2317
+ cacheKey += part.text;
2318
+ }
2319
+ if (part.type === `tool-invocation`) {
2320
+ cacheKey += part.toolInvocation.toolCallId;
2321
+ cacheKey += part.toolInvocation.state;
2322
+ }
2323
+ if (part.type === `reasoning`) {
2324
+ cacheKey += part.reasoning;
2325
+ cacheKey += part.details.reduce((prev, current) => {
2326
+ if (current.type === `text`) {
2327
+ return prev + current.text.length + (current.signature?.length || 0);
2328
+ }
2329
+ return prev;
2330
+ }, 0);
2331
+ const partAny = part;
2332
+ if (partAny && Object.hasOwn(partAny, "providerMetadata") && partAny.providerMetadata && Object.hasOwn(partAny.providerMetadata, "openai") && partAny.providerMetadata.openai && Object.hasOwn(partAny.providerMetadata.openai, "itemId")) {
2333
+ const itemId = partAny.providerMetadata.openai.itemId;
2334
+ cacheKey += `|${itemId}`;
2090
2335
  }
2091
- if (part.type === `file`) {
2092
- key += part.data;
2093
- key += part.mimeType;
2336
+ }
2337
+ if (part.type === `file`) {
2338
+ cacheKey += part.data;
2339
+ cacheKey += part.mimeType;
2340
+ }
2341
+ return cacheKey;
2342
+ }
2343
+ static cacheKeyFromDBParts(parts) {
2344
+ let key = ``;
2345
+ for (const part of parts) {
2346
+ key += part.type;
2347
+ if (part.type.startsWith("data-")) {
2348
+ const data = part.data;
2349
+ key += JSON.stringify(data);
2350
+ } else {
2351
+ key += _MessageList.cacheKeyFromAIV4Part(part);
2094
2352
  }
2095
2353
  }
2096
2354
  return key;
@@ -2160,7 +2418,7 @@ var MessageList = class _MessageList {
2160
2418
  const twoMM2 = _MessageList.isMastraDBMessage(two) && two;
2161
2419
  if (oneMM2 && !twoMM2) return false;
2162
2420
  if (oneMM2 && twoMM2) {
2163
- return oneMM2.id === twoMM2.id && _MessageList.cacheKeyFromAIV4Parts(oneMM2.content.parts) === _MessageList.cacheKeyFromAIV4Parts(twoMM2.content.parts);
2421
+ return oneMM2.id === twoMM2.id && _MessageList.cacheKeyFromDBParts(oneMM2.content.parts) === _MessageList.cacheKeyFromDBParts(twoMM2.content.parts);
2164
2422
  }
2165
2423
  const oneUIV5 = _MessageList.isAIV5UIMessage(one) && one;
2166
2424
  const twoUIV5 = _MessageList.isAIV5UIMessage(two) && two;
@@ -2233,9 +2491,23 @@ var MessageList = class _MessageList {
2233
2491
  if (role === `tool` || role === `assistant`) {
2234
2492
  throw new Error(incompatibleMessage);
2235
2493
  }
2494
+ let processedImage;
2495
+ if (part.image instanceof URL || part.image instanceof Uint8Array) {
2496
+ processedImage = part.image;
2497
+ } else if (Buffer.isBuffer(part.image) || part.image instanceof ArrayBuffer) {
2498
+ processedImage = new Uint8Array(part.image);
2499
+ } else {
2500
+ const categorized = categorizeFileData(part.image, part.mimeType);
2501
+ if (categorized.type === "raw") {
2502
+ const dataUri = createDataUri(part.image, part.mimeType || "image/png");
2503
+ processedImage = new URL(dataUri);
2504
+ } else {
2505
+ processedImage = new URL(part.image);
2506
+ }
2507
+ }
2236
2508
  roleContent[role].push({
2237
2509
  ...part,
2238
- 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)
2510
+ image: processedImage
2239
2511
  });
2240
2512
  break;
2241
2513
  }
@@ -2381,6 +2653,9 @@ var MessageList = class _MessageList {
2381
2653
  if (dbMsg.createdAt) metadata.createdAt = dbMsg.createdAt;
2382
2654
  if (dbMsg.threadId) metadata.threadId = dbMsg.threadId;
2383
2655
  if (dbMsg.resourceId) metadata.resourceId = dbMsg.resourceId;
2656
+ if (dbMsg.content.providerMetadata) {
2657
+ metadata.providerMetadata = dbMsg.content.providerMetadata;
2658
+ }
2384
2659
  const hasToolInvocationParts = dbMsg.content.parts?.some((p) => p.type === "tool-invocation");
2385
2660
  if (dbMsg.content.toolInvocations && !hasToolInvocationParts) {
2386
2661
  for (const invocation of dbMsg.content.toolInvocations) {
@@ -2447,18 +2722,20 @@ var MessageList = class _MessageList {
2447
2722
  continue;
2448
2723
  }
2449
2724
  if (part.type === "reasoning") {
2450
- const reasoningPart = part;
2451
- const text = reasoningPart.text || reasoningPart.reasoning || (reasoningPart.details?.reduce((p, c) => {
2725
+ const text = part.reasoning || (part.details?.reduce((p, c) => {
2452
2726
  if (c.type === `text` && c.text) return p + c.text;
2453
2727
  return p;
2454
2728
  }, "") ?? "");
2455
- if (text || reasoningPart.details?.length) {
2456
- parts.push({
2729
+ if (text || part.details?.length) {
2730
+ const v5UIPart = {
2457
2731
  type: "reasoning",
2458
2732
  text: text || "",
2459
- state: "done",
2460
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2461
- });
2733
+ state: "done"
2734
+ };
2735
+ if (part.providerMetadata) {
2736
+ v5UIPart.providerMetadata = part.providerMetadata;
2737
+ }
2738
+ parts.push(v5UIPart);
2462
2739
  }
2463
2740
  continue;
2464
2741
  }
@@ -2471,12 +2748,15 @@ var MessageList = class _MessageList {
2471
2748
  }
2472
2749
  const categorized = typeof part.data === "string" ? categorizeFileData(part.data, part.mimeType) : { type: "raw", mimeType: part.mimeType};
2473
2750
  if (categorized.type === "url" && typeof part.data === "string") {
2474
- parts.push({
2751
+ const v5UIPart = {
2475
2752
  type: "file",
2476
2753
  url: part.data,
2477
- mediaType: categorized.mimeType || "image/png",
2478
- providerMetadata: part.providerMetadata
2479
- });
2754
+ mediaType: categorized.mimeType || "image/png"
2755
+ };
2756
+ if (part.providerMetadata) {
2757
+ v5UIPart.providerMetadata = part.providerMetadata;
2758
+ }
2759
+ parts.push(v5UIPart);
2480
2760
  } else {
2481
2761
  let filePartData;
2482
2762
  let extractedMimeType = part.mimeType;
@@ -2500,27 +2780,37 @@ var MessageList = class _MessageList {
2500
2780
  } else {
2501
2781
  dataUri = createDataUri(filePartData, finalMimeType);
2502
2782
  }
2503
- parts.push({
2783
+ const v5UIPart = {
2504
2784
  type: "file",
2505
2785
  url: dataUri,
2506
2786
  // Use url field with data URI
2507
- mediaType: finalMimeType,
2508
- providerMetadata: part.providerMetadata
2509
- });
2787
+ mediaType: finalMimeType
2788
+ };
2789
+ if (part.providerMetadata) {
2790
+ v5UIPart.providerMetadata = part.providerMetadata;
2791
+ }
2792
+ parts.push(v5UIPart);
2510
2793
  }
2511
2794
  } else if (part.type === "source") {
2512
- const sourcePart = part;
2513
- parts.push({
2795
+ const v5UIPart = {
2514
2796
  type: "source-url",
2515
- url: sourcePart.source.url,
2516
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2517
- });
2797
+ url: part.source.url,
2798
+ sourceId: part.source.id,
2799
+ title: part.source.title
2800
+ };
2801
+ if (part.providerMetadata) {
2802
+ v5UIPart.providerMetadata = part.providerMetadata;
2803
+ }
2804
+ parts.push(v5UIPart);
2518
2805
  } else if (part.type === "text") {
2519
- parts.push({
2806
+ const v5UIPart = {
2520
2807
  type: "text",
2521
- text: part.text,
2522
- ...part.providerMetadata && { providerMetadata: part.providerMetadata }
2523
- });
2808
+ text: part.text
2809
+ };
2810
+ if (part.providerMetadata) {
2811
+ v5UIPart.providerMetadata = part.providerMetadata;
2812
+ }
2813
+ parts.push(v5UIPart);
2524
2814
  hasNonToolReasoningParts = true;
2525
2815
  } else {
2526
2816
  parts.push(part);
@@ -2684,32 +2974,69 @@ var MessageList = class _MessageList {
2684
2974
  }
2685
2975
  };
2686
2976
  }
2977
+ /**
2978
+ * Convert image or file to data URI or URL for V2 file part
2979
+ */
2980
+ static getDataStringFromAIV5DataPart = (part) => {
2981
+ let mimeType;
2982
+ let data;
2983
+ if ("data" in part) {
2984
+ mimeType = part.mediaType || "application/octet-stream";
2985
+ data = part.data;
2986
+ } else if ("image" in part) {
2987
+ mimeType = part.mediaType || "image/jpeg";
2988
+ data = part.image;
2989
+ } else {
2990
+ throw new MastraError({
2991
+ id: "MASTRA_AIV5_DATA_PART_INVALID",
2992
+ domain: "AGENT" /* AGENT */,
2993
+ category: "USER" /* USER */,
2994
+ text: "Invalid AIV5 data part in getDataStringFromAIV5DataPart",
2995
+ details: {
2996
+ part
2997
+ }
2998
+ });
2999
+ }
3000
+ if (data instanceof URL) {
3001
+ return data.toString();
3002
+ } else {
3003
+ if (data instanceof Buffer) {
3004
+ const base64 = data.toString("base64");
3005
+ return `data:${mimeType};base64,${base64}`;
3006
+ } else if (typeof data === "string") {
3007
+ return data.startsWith("data:") || data.startsWith("http") ? data : `data:${mimeType};base64,${data}`;
3008
+ } else if (data instanceof Uint8Array) {
3009
+ const base64 = Buffer.from(data).toString("base64");
3010
+ return `data:${mimeType};base64,${base64}`;
3011
+ } else if (data instanceof ArrayBuffer) {
3012
+ const base64 = Buffer.from(data).toString("base64");
3013
+ return `data:${mimeType};base64,${base64}`;
3014
+ } else {
3015
+ return "";
3016
+ }
3017
+ }
3018
+ };
2687
3019
  /**
2688
3020
  * Direct conversion from AIV5 ModelMessage to MastraDBMessage
2689
3021
  * Combines logic from aiV5ModelMessageToMastraMessageV3 + mastraMessageV3ToV2
2690
3022
  */
2691
3023
  static aiV5ModelMessageToMastraDBMessage(modelMsg, _messageSource) {
2692
3024
  const content = Array.isArray(modelMsg.content) ? modelMsg.content : [{ type: "text", text: modelMsg.content }];
2693
- const v2Parts = [];
3025
+ const mastraDBParts = [];
2694
3026
  const toolInvocations = [];
2695
3027
  const reasoningParts = [];
2696
3028
  const experimental_attachments = [];
2697
- const textParts = [];
2698
3029
  let lastPartWasToolResult = false;
2699
3030
  for (const part of content) {
2700
- const providerMetadata = {
2701
- ...modelMsg.providerMetadata || {},
2702
- ...part.providerMetadata || {}
2703
- };
2704
- const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
2705
3031
  if (part.type === "text") {
2706
3032
  const textPart = {
2707
3033
  type: "text",
2708
- text: part.text,
2709
- ...hasProviderMetadata && { experimental_providerMetadata: providerMetadata }
3034
+ text: part.text
2710
3035
  };
2711
- v2Parts.push(textPart);
2712
- textParts.push({ text: part.text, providerMetadata: hasProviderMetadata ? providerMetadata : void 0 });
3036
+ if (part.providerOptions) {
3037
+ textPart.providerMetadata = part.providerOptions;
3038
+ }
3039
+ mastraDBParts.push(textPart);
2713
3040
  lastPartWasToolResult = false;
2714
3041
  } else if (part.type === "tool-call") {
2715
3042
  const toolCallPart = part;
@@ -2720,10 +3047,12 @@ var MessageList = class _MessageList {
2720
3047
  toolName: toolCallPart.toolName,
2721
3048
  args: toolCallPart.input,
2722
3049
  state: "call"
2723
- },
2724
- ...hasProviderMetadata && { providerMetadata }
3050
+ }
2725
3051
  };
2726
- v2Parts.push(toolInvocationPart);
3052
+ if (part.providerOptions) {
3053
+ toolInvocationPart.providerMetadata = part.providerOptions;
3054
+ }
3055
+ mastraDBParts.push(toolInvocationPart);
2727
3056
  toolInvocations.push({
2728
3057
  toolCallId: toolCallPart.toolCallId,
2729
3058
  toolName: toolCallPart.toolName,
@@ -2734,78 +3063,67 @@ var MessageList = class _MessageList {
2734
3063
  } else if (part.type === "tool-result") {
2735
3064
  const toolResultPart = part;
2736
3065
  const matchingCall = toolInvocations.find((inv) => inv.toolCallId === toolResultPart.toolCallId);
2737
- const matchingV2Part = v2Parts.find(
3066
+ const matchingV2Part = mastraDBParts.find(
2738
3067
  (p) => p.type === "tool-invocation" && "toolInvocation" in p && p.toolInvocation.toolCallId === toolResultPart.toolCallId
2739
3068
  );
3069
+ const updateMatchingCallInvocationResult = (toolResultPart2, matchingCall2) => {
3070
+ matchingCall2.state = "result";
3071
+ matchingCall2.result = typeof toolResultPart2.output === "object" && toolResultPart2.output && "value" in toolResultPart2.output ? toolResultPart2.output.value : toolResultPart2.output;
3072
+ };
2740
3073
  if (matchingCall) {
2741
- matchingCall.state = "result";
2742
- matchingCall.result = typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output;
3074
+ updateMatchingCallInvocationResult(toolResultPart, matchingCall);
2743
3075
  } else {
2744
- const resultPartWithName = toolResultPart;
2745
- toolInvocations.push({
3076
+ const call = {
3077
+ state: "call",
2746
3078
  toolCallId: toolResultPart.toolCallId,
2747
- toolName: resultPartWithName.toolName || "unknown",
2748
- args: {},
2749
- result: typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output,
2750
- state: "result"
2751
- });
3079
+ toolName: toolResultPart.toolName || "unknown",
3080
+ args: {}
3081
+ };
3082
+ updateMatchingCallInvocationResult(toolResultPart, call);
3083
+ toolInvocations.push(call);
2752
3084
  }
2753
3085
  if (matchingV2Part && matchingV2Part.type === "tool-invocation") {
2754
- matchingV2Part.toolInvocation.state = "result";
2755
- matchingV2Part.toolInvocation.result = typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output;
2756
- if (hasProviderMetadata) {
2757
- matchingV2Part.providerMetadata = providerMetadata;
2758
- }
3086
+ updateMatchingCallInvocationResult(toolResultPart, matchingV2Part.toolInvocation);
2759
3087
  } else {
2760
- const resultPartWithName = toolResultPart;
2761
3088
  const toolInvocationPart = {
2762
3089
  type: "tool-invocation",
2763
3090
  toolInvocation: {
2764
3091
  toolCallId: toolResultPart.toolCallId,
2765
- toolName: resultPartWithName.toolName || "unknown",
3092
+ toolName: toolResultPart.toolName || "unknown",
2766
3093
  args: {},
2767
- result: typeof toolResultPart.output === "object" && toolResultPart.output && "value" in toolResultPart.output ? toolResultPart.output.value : toolResultPart.output,
2768
- state: "result"
2769
- },
2770
- ...hasProviderMetadata && { providerMetadata }
3094
+ state: "call"
3095
+ }
2771
3096
  };
2772
- v2Parts.push(toolInvocationPart);
3097
+ updateMatchingCallInvocationResult(toolResultPart, toolInvocationPart.toolInvocation);
3098
+ mastraDBParts.push(toolInvocationPart);
2773
3099
  }
2774
3100
  lastPartWasToolResult = true;
2775
3101
  } else if (part.type === "reasoning") {
2776
- const reasoningPart = part;
2777
3102
  const v2ReasoningPart = {
2778
3103
  type: "reasoning",
2779
- reasoning: reasoningPart.text,
2780
- details: [{ type: "text", text: reasoningPart.text }],
2781
- ...hasProviderMetadata && { providerMetadata }
3104
+ reasoning: "",
3105
+ // leave this blank so we aren't double storing it in the db along with details
3106
+ details: [{ type: "text", text: part.text }]
2782
3107
  };
2783
- v2Parts.push(v2ReasoningPart);
2784
- reasoningParts.push(reasoningPart.text);
3108
+ if (part.providerOptions) {
3109
+ v2ReasoningPart.providerMetadata = part.providerOptions;
3110
+ }
3111
+ mastraDBParts.push(v2ReasoningPart);
3112
+ reasoningParts.push(part.text);
2785
3113
  lastPartWasToolResult = false;
2786
3114
  } else if (part.type === "image") {
2787
3115
  const imagePart = part;
2788
- let imageData;
2789
- const mimeType = imagePart.mimeType || "image/jpeg";
2790
- if ("url" in imagePart && typeof imagePart.url === "string") {
2791
- imageData = imagePart.url;
2792
- } else if ("data" in imagePart) {
2793
- if (typeof imagePart.data === "string") {
2794
- imageData = imagePart.data.startsWith("data:") || imagePart.data.startsWith("http") ? imagePart.data : `data:${mimeType};base64,${imagePart.data}`;
2795
- } else {
2796
- const base64 = Buffer.from(imagePart.data).toString("base64");
2797
- imageData = `data:${mimeType};base64,${base64}`;
2798
- }
2799
- } else {
2800
- imageData = "";
2801
- }
3116
+ const mimeType = imagePart.mediaType || "image/jpeg";
3117
+ const imageData = this.getDataStringFromAIV5DataPart(imagePart);
2802
3118
  const imageFilePart = {
2803
3119
  type: "file",
2804
3120
  data: imageData,
2805
- mimeType,
2806
- ...hasProviderMetadata && { providerMetadata }
3121
+ mimeType
2807
3122
  };
2808
- v2Parts.push(imageFilePart);
3123
+ if (part.providerOptions) {
3124
+ imageFilePart.providerMetadata = part.providerOptions;
3125
+ }
3126
+ mastraDBParts.push(imageFilePart);
2809
3127
  experimental_attachments.push({
2810
3128
  url: imageData,
2811
3129
  contentType: mimeType
@@ -2814,26 +3132,16 @@ var MessageList = class _MessageList {
2814
3132
  } else if (part.type === "file") {
2815
3133
  const filePart = part;
2816
3134
  const mimeType = filePart.mediaType || "application/octet-stream";
2817
- let fileData;
2818
- if ("url" in filePart && typeof filePart.url === "string") {
2819
- fileData = filePart.url;
2820
- } else if ("data" in filePart) {
2821
- if (typeof filePart.data === "string") {
2822
- fileData = filePart.data.startsWith("data:") || filePart.data.startsWith("http") ? filePart.data : `data:${mimeType};base64,${filePart.data}`;
2823
- } else {
2824
- const base64 = Buffer.from(filePart.data).toString("base64");
2825
- fileData = `data:${mimeType};base64,${base64}`;
2826
- }
2827
- } else {
2828
- fileData = "";
2829
- }
3135
+ const fileData = this.getDataStringFromAIV5DataPart(filePart);
2830
3136
  const v2FilePart = {
2831
3137
  type: "file",
2832
3138
  data: fileData,
2833
- mimeType,
2834
- ...hasProviderMetadata && { providerMetadata }
3139
+ mimeType
2835
3140
  };
2836
- v2Parts.push(v2FilePart);
3141
+ if (part.providerOptions) {
3142
+ v2FilePart.providerMetadata = part.providerOptions;
3143
+ }
3144
+ mastraDBParts.push(v2FilePart);
2837
3145
  experimental_attachments.push({
2838
3146
  url: fileData,
2839
3147
  contentType: mimeType
@@ -2841,34 +3149,34 @@ var MessageList = class _MessageList {
2841
3149
  lastPartWasToolResult = false;
2842
3150
  }
2843
3151
  }
2844
- if (modelMsg.role === "assistant" && lastPartWasToolResult && v2Parts.length > 0) {
2845
- const lastPart = v2Parts[v2Parts.length - 1];
3152
+ if (modelMsg.role === "assistant" && lastPartWasToolResult && mastraDBParts.length > 0) {
3153
+ const lastPart = mastraDBParts[mastraDBParts.length - 1];
2846
3154
  if (lastPart && lastPart.type !== "text") {
2847
3155
  const emptyTextPart = { type: "text", text: "" };
2848
- v2Parts.push(emptyTextPart);
2849
- textParts.push({ text: "" });
3156
+ mastraDBParts.push(emptyTextPart);
2850
3157
  }
2851
3158
  }
2852
- let contentString = void 0;
2853
- if (textParts.length > 0) {
2854
- contentString = textParts.map((p) => p.text).join("\n");
2855
- }
2856
- const metadata = {};
3159
+ const contentString = mastraDBParts.filter((p) => p.type === "text").map((p) => p.text).join("\n");
3160
+ const metadata = "metadata" in modelMsg && modelMsg.metadata !== null && modelMsg.metadata !== void 0 ? modelMsg.metadata : {};
2857
3161
  const id = `id` in modelMsg && typeof modelMsg.id === `string` ? modelMsg.id : `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2858
- return {
3162
+ const message = {
2859
3163
  id,
2860
3164
  role: _MessageList.getRole(modelMsg),
2861
3165
  createdAt: /* @__PURE__ */ new Date(),
2862
3166
  content: {
2863
3167
  format: 2,
2864
- parts: v2Parts,
3168
+ parts: mastraDBParts,
2865
3169
  toolInvocations: toolInvocations.length > 0 ? toolInvocations : void 0,
2866
3170
  reasoning: reasoningParts.length > 0 ? reasoningParts.join("\n") : void 0,
2867
3171
  experimental_attachments: experimental_attachments.length > 0 ? experimental_attachments : void 0,
2868
- content: contentString,
2869
- metadata
3172
+ content: contentString || void 0,
3173
+ metadata: Object.keys(metadata).length > 0 ? metadata : void 0
2870
3174
  }
2871
3175
  };
3176
+ if (modelMsg.providerOptions) {
3177
+ message.content.providerMetadata = modelMsg.providerOptions;
3178
+ }
3179
+ return message;
2872
3180
  }
2873
3181
  aiV4CoreMessagesToAIV5ModelMessages(messages, source) {
2874
3182
  return this.aiV5UIMessagesToAIV5ModelMessages(
@@ -2879,14 +3187,24 @@ var MessageList = class _MessageList {
2879
3187
  const sanitized = this.sanitizeV5UIMessages(messages, filterIncompleteToolCalls);
2880
3188
  const preprocessed = this.addStartStepPartsForAIV5(sanitized);
2881
3189
  const result = AIV5.convertToModelMessages(preprocessed);
2882
- return result;
3190
+ return result.map((modelMsg, index) => {
3191
+ const uiMsg = preprocessed[index];
3192
+ if (uiMsg?.metadata && typeof uiMsg.metadata === "object" && "providerMetadata" in uiMsg.metadata && uiMsg.metadata.providerMetadata) {
3193
+ return {
3194
+ ...modelMsg,
3195
+ providerOptions: uiMsg.metadata.providerMetadata
3196
+ };
3197
+ }
3198
+ return modelMsg;
3199
+ });
2883
3200
  }
2884
3201
  addStartStepPartsForAIV5(messages) {
2885
3202
  for (const message of messages) {
2886
3203
  if (message.role !== `assistant`) continue;
2887
3204
  for (const [index, part] of message.parts.entries()) {
2888
3205
  if (!AIV5.isToolUIPart(part)) continue;
2889
- if (message.parts.at(index + 1)?.type !== `step-start`) {
3206
+ const nextPart = message.parts.at(index + 1);
3207
+ if (nextPart && nextPart.type !== `step-start` && !AIV5.isToolUIPart(nextPart)) {
2890
3208
  message.parts.splice(index + 1, 0, { type: "step-start" });
2891
3209
  }
2892
3210
  }
@@ -3012,5 +3330,5 @@ var MessageList = class _MessageList {
3012
3330
  };
3013
3331
 
3014
3332
  export { DefaultGeneratedFile, DefaultGeneratedFileWithType, MessageList, convertMessages };
3015
- //# sourceMappingURL=chunk-JV2KH24V.js.map
3016
- //# sourceMappingURL=chunk-JV2KH24V.js.map
3333
+ //# sourceMappingURL=chunk-3GKKHF36.js.map
3334
+ //# sourceMappingURL=chunk-3GKKHF36.js.map