@scoutqa/mastra-core 0.24.6-fork.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1064) hide show
  1. package/CHANGELOG.md +5844 -0
  2. package/LICENSE.md +15 -0
  3. package/README.md +59 -0
  4. package/a2a.d.ts +1 -0
  5. package/action.d.ts +1 -0
  6. package/agent/input-processor/processors.d.ts +1 -0
  7. package/agent.d.ts +1 -0
  8. package/ai-tracing.d.ts +1 -0
  9. package/base.d.ts +1 -0
  10. package/bundler.d.ts +1 -0
  11. package/cache.d.ts +1 -0
  12. package/deployer.d.ts +1 -0
  13. package/di.d.ts +1 -0
  14. package/dist/a2a/error.d.ts +24 -0
  15. package/dist/a2a/error.d.ts.map +1 -0
  16. package/dist/a2a/index.cjs +80 -0
  17. package/dist/a2a/index.cjs.map +1 -0
  18. package/dist/a2a/index.d.ts +4 -0
  19. package/dist/a2a/index.d.ts.map +1 -0
  20. package/dist/a2a/index.js +72 -0
  21. package/dist/a2a/index.js.map +1 -0
  22. package/dist/a2a/types.d.ts +93 -0
  23. package/dist/a2a/types.d.ts.map +1 -0
  24. package/dist/action/index.d.ts +36 -0
  25. package/dist/action/index.d.ts.map +1 -0
  26. package/dist/agent/__tests__/mock-model.d.ts +6 -0
  27. package/dist/agent/__tests__/mock-model.d.ts.map +1 -0
  28. package/dist/agent/agent.d.ts +728 -0
  29. package/dist/agent/agent.d.ts.map +1 -0
  30. package/dist/agent/agent.types.d.ts +130 -0
  31. package/dist/agent/agent.types.d.ts.map +1 -0
  32. package/dist/agent/index.cjs +53 -0
  33. package/dist/agent/index.cjs.map +1 -0
  34. package/dist/agent/index.d.ts +10 -0
  35. package/dist/agent/index.d.ts.map +1 -0
  36. package/dist/agent/index.js +4 -0
  37. package/dist/agent/index.js.map +1 -0
  38. package/dist/agent/index.warning.d.ts +13 -0
  39. package/dist/agent/index.warning.d.ts.map +1 -0
  40. package/dist/agent/input-processor/index.cjs +28 -0
  41. package/dist/agent/input-processor/index.cjs.map +1 -0
  42. package/dist/agent/input-processor/index.d.ts +10 -0
  43. package/dist/agent/input-processor/index.d.ts.map +1 -0
  44. package/dist/agent/input-processor/index.js +3 -0
  45. package/dist/agent/input-processor/index.js.map +1 -0
  46. package/dist/agent/input-processor/processors/index.d.ts +6 -0
  47. package/dist/agent/input-processor/processors/index.d.ts.map +1 -0
  48. package/dist/agent/input-processor/processors/language-detector.d.ts +18 -0
  49. package/dist/agent/input-processor/processors/language-detector.d.ts.map +1 -0
  50. package/dist/agent/input-processor/processors/moderation.d.ts +18 -0
  51. package/dist/agent/input-processor/processors/moderation.d.ts.map +1 -0
  52. package/dist/agent/input-processor/processors/pii-detector.d.ts +18 -0
  53. package/dist/agent/input-processor/processors/pii-detector.d.ts.map +1 -0
  54. package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts +18 -0
  55. package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts.map +1 -0
  56. package/dist/agent/input-processor/processors/unicode-normalizer.d.ts +18 -0
  57. package/dist/agent/input-processor/processors/unicode-normalizer.d.ts.map +1 -0
  58. package/dist/agent/message-list/index.d.ts +305 -0
  59. package/dist/agent/message-list/index.d.ts.map +1 -0
  60. package/dist/agent/message-list/prompt/attachments-to-parts.d.ts +11 -0
  61. package/dist/agent/message-list/prompt/attachments-to-parts.d.ts.map +1 -0
  62. package/dist/agent/message-list/prompt/convert-file.d.ts +7 -0
  63. package/dist/agent/message-list/prompt/convert-file.d.ts.map +1 -0
  64. package/dist/agent/message-list/prompt/convert-to-mastra-v1.d.ts +4 -0
  65. package/dist/agent/message-list/prompt/convert-to-mastra-v1.d.ts.map +1 -0
  66. package/dist/agent/message-list/prompt/data-content.d.ts +17 -0
  67. package/dist/agent/message-list/prompt/data-content.d.ts.map +1 -0
  68. package/dist/agent/message-list/prompt/download-assets.d.ts +15 -0
  69. package/dist/agent/message-list/prompt/download-assets.d.ts.map +1 -0
  70. package/dist/agent/message-list/prompt/image-utils.d.ts +86 -0
  71. package/dist/agent/message-list/prompt/image-utils.d.ts.map +1 -0
  72. package/dist/agent/message-list/prompt/invalid-content-error.d.ts +14 -0
  73. package/dist/agent/message-list/prompt/invalid-content-error.d.ts.map +1 -0
  74. package/dist/agent/message-list/types.d.ts +3 -0
  75. package/dist/agent/message-list/types.d.ts.map +1 -0
  76. package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts +3 -0
  77. package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts.map +1 -0
  78. package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts +3 -0
  79. package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts.map +1 -0
  80. package/dist/agent/message-list/utils/ai-v5/gemini-compatibility.d.ts +22 -0
  81. package/dist/agent/message-list/utils/ai-v5/gemini-compatibility.d.ts.map +1 -0
  82. package/dist/agent/message-list/utils/ai-v5/tool.d.ts +16 -0
  83. package/dist/agent/message-list/utils/ai-v5/tool.d.ts.map +1 -0
  84. package/dist/agent/message-list/utils/convert-messages.d.ts +91 -0
  85. package/dist/agent/message-list/utils/convert-messages.d.ts.map +1 -0
  86. package/dist/agent/save-queue/index.d.ts +68 -0
  87. package/dist/agent/save-queue/index.d.ts.map +1 -0
  88. package/dist/agent/test-utils.d.ts +82 -0
  89. package/dist/agent/test-utils.d.ts.map +1 -0
  90. package/dist/agent/trip-wire.d.ts +18 -0
  91. package/dist/agent/trip-wire.d.ts.map +1 -0
  92. package/dist/agent/types.d.ts +358 -0
  93. package/dist/agent/types.d.ts.map +1 -0
  94. package/dist/agent/utils.d.ts +104 -0
  95. package/dist/agent/utils.d.ts.map +1 -0
  96. package/dist/agent/workflows/prepare-stream/index.d.ts +158 -0
  97. package/dist/agent/workflows/prepare-stream/index.d.ts.map +1 -0
  98. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts +35 -0
  99. package/dist/agent/workflows/prepare-stream/map-results-step.d.ts.map +1 -0
  100. package/dist/agent/workflows/prepare-stream/prepare-memory-step.d.ts +90 -0
  101. package/dist/agent/workflows/prepare-stream/prepare-memory-step.d.ts.map +1 -0
  102. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts +75 -0
  103. package/dist/agent/workflows/prepare-stream/prepare-tools-step.d.ts.map +1 -0
  104. package/dist/agent/workflows/prepare-stream/schema.d.ts +179 -0
  105. package/dist/agent/workflows/prepare-stream/schema.d.ts.map +1 -0
  106. package/dist/agent/workflows/prepare-stream/stream-step.d.ts +36 -0
  107. package/dist/agent/workflows/prepare-stream/stream-step.d.ts.map +1 -0
  108. package/dist/ai-tracing/context.d.ts +21 -0
  109. package/dist/ai-tracing/context.d.ts.map +1 -0
  110. package/dist/ai-tracing/exporters/base.d.ts +111 -0
  111. package/dist/ai-tracing/exporters/base.d.ts.map +1 -0
  112. package/dist/ai-tracing/exporters/cloud.d.ts +30 -0
  113. package/dist/ai-tracing/exporters/cloud.d.ts.map +1 -0
  114. package/dist/ai-tracing/exporters/console.d.ts +10 -0
  115. package/dist/ai-tracing/exporters/console.d.ts.map +1 -0
  116. package/dist/ai-tracing/exporters/default.d.ts +98 -0
  117. package/dist/ai-tracing/exporters/default.d.ts.map +1 -0
  118. package/dist/ai-tracing/exporters/index.d.ts +9 -0
  119. package/dist/ai-tracing/exporters/index.d.ts.map +1 -0
  120. package/dist/ai-tracing/index.cjs +148 -0
  121. package/dist/ai-tracing/index.cjs.map +1 -0
  122. package/dist/ai-tracing/index.d.ts +13 -0
  123. package/dist/ai-tracing/index.d.ts.map +1 -0
  124. package/dist/ai-tracing/index.js +3 -0
  125. package/dist/ai-tracing/index.js.map +1 -0
  126. package/dist/ai-tracing/model-tracing.d.ts +63 -0
  127. package/dist/ai-tracing/model-tracing.d.ts.map +1 -0
  128. package/dist/ai-tracing/registry.d.ts +51 -0
  129. package/dist/ai-tracing/registry.d.ts.map +1 -0
  130. package/dist/ai-tracing/span_processors/index.d.ts +5 -0
  131. package/dist/ai-tracing/span_processors/index.d.ts.map +1 -0
  132. package/dist/ai-tracing/span_processors/sensitive-data-filter.d.ts +85 -0
  133. package/dist/ai-tracing/span_processors/sensitive-data-filter.d.ts.map +1 -0
  134. package/dist/ai-tracing/spans/base.d.ts +65 -0
  135. package/dist/ai-tracing/spans/base.d.ts.map +1 -0
  136. package/dist/ai-tracing/spans/default.d.ts +13 -0
  137. package/dist/ai-tracing/spans/default.d.ts.map +1 -0
  138. package/dist/ai-tracing/spans/index.d.ts +7 -0
  139. package/dist/ai-tracing/spans/index.d.ts.map +1 -0
  140. package/dist/ai-tracing/spans/no-op.d.ts +15 -0
  141. package/dist/ai-tracing/spans/no-op.d.ts.map +1 -0
  142. package/dist/ai-tracing/tracers/base.d.ts +105 -0
  143. package/dist/ai-tracing/tracers/base.d.ts.map +1 -0
  144. package/dist/ai-tracing/tracers/default.d.ts +7 -0
  145. package/dist/ai-tracing/tracers/default.d.ts.map +1 -0
  146. package/dist/ai-tracing/tracers/index.d.ts +6 -0
  147. package/dist/ai-tracing/tracers/index.d.ts.map +1 -0
  148. package/dist/ai-tracing/types.d.ts +696 -0
  149. package/dist/ai-tracing/types.d.ts.map +1 -0
  150. package/dist/ai-tracing/utils.d.ts +68 -0
  151. package/dist/ai-tracing/utils.d.ts.map +1 -0
  152. package/dist/base.cjs +12 -0
  153. package/dist/base.cjs.map +1 -0
  154. package/dist/base.d.ts +34 -0
  155. package/dist/base.d.ts.map +1 -0
  156. package/dist/base.js +3 -0
  157. package/dist/base.js.map +1 -0
  158. package/dist/base.warning.d.ts +9 -0
  159. package/dist/base.warning.d.ts.map +1 -0
  160. package/dist/bundler/index.cjs +12 -0
  161. package/dist/bundler/index.cjs.map +1 -0
  162. package/dist/bundler/index.d.ts +31 -0
  163. package/dist/bundler/index.d.ts.map +1 -0
  164. package/dist/bundler/index.js +3 -0
  165. package/dist/bundler/index.js.map +1 -0
  166. package/dist/bundler/index.warning.d.ts +9 -0
  167. package/dist/bundler/index.warning.d.ts.map +1 -0
  168. package/dist/bundler/types.d.ts +6 -0
  169. package/dist/bundler/types.d.ts.map +1 -0
  170. package/dist/cache/base.d.ts +14 -0
  171. package/dist/cache/base.d.ts.map +1 -0
  172. package/dist/cache/index.cjs +16 -0
  173. package/dist/cache/index.cjs.map +1 -0
  174. package/dist/cache/index.d.ts +3 -0
  175. package/dist/cache/index.d.ts.map +1 -0
  176. package/dist/cache/index.js +3 -0
  177. package/dist/cache/index.js.map +1 -0
  178. package/dist/cache/inmemory.d.ts +13 -0
  179. package/dist/cache/inmemory.d.ts.map +1 -0
  180. package/dist/chunk-3HXBPDKN.js +105 -0
  181. package/dist/chunk-3HXBPDKN.js.map +1 -0
  182. package/dist/chunk-3NTOFNIU.js +60 -0
  183. package/dist/chunk-3NTOFNIU.js.map +1 -0
  184. package/dist/chunk-3PSWNGBF.js +3 -0
  185. package/dist/chunk-3PSWNGBF.js.map +1 -0
  186. package/dist/chunk-42RUESSD.cjs +3203 -0
  187. package/dist/chunk-42RUESSD.cjs.map +1 -0
  188. package/dist/chunk-436FFEF6.js +34 -0
  189. package/dist/chunk-436FFEF6.js.map +1 -0
  190. package/dist/chunk-46XGIEXM.cjs +47 -0
  191. package/dist/chunk-46XGIEXM.cjs.map +1 -0
  192. package/dist/chunk-4D4KB75Y.cjs +9 -0
  193. package/dist/chunk-4D4KB75Y.cjs.map +1 -0
  194. package/dist/chunk-4L63C6E5.cjs +2554 -0
  195. package/dist/chunk-4L63C6E5.cjs.map +1 -0
  196. package/dist/chunk-4LSFAAZW.js +3 -0
  197. package/dist/chunk-4LSFAAZW.js.map +1 -0
  198. package/dist/chunk-4R2TBRS7.cjs +191 -0
  199. package/dist/chunk-4R2TBRS7.cjs.map +1 -0
  200. package/dist/chunk-4WQYXT2I.cjs +12 -0
  201. package/dist/chunk-4WQYXT2I.cjs.map +1 -0
  202. package/dist/chunk-5NTO7S5I.cjs +173 -0
  203. package/dist/chunk-5NTO7S5I.cjs.map +1 -0
  204. package/dist/chunk-5O52O25J.js +65 -0
  205. package/dist/chunk-5O52O25J.js.map +1 -0
  206. package/dist/chunk-5X4TQR6K.js +12 -0
  207. package/dist/chunk-5X4TQR6K.js.map +1 -0
  208. package/dist/chunk-6D2K2CAA.js +776 -0
  209. package/dist/chunk-6D2K2CAA.js.map +1 -0
  210. package/dist/chunk-6KOL2B3A.cjs +75 -0
  211. package/dist/chunk-6KOL2B3A.cjs.map +1 -0
  212. package/dist/chunk-6TEQIYXV.cjs +96 -0
  213. package/dist/chunk-6TEQIYXV.cjs.map +1 -0
  214. package/dist/chunk-6VOPKVYH.cjs +57 -0
  215. package/dist/chunk-6VOPKVYH.cjs.map +1 -0
  216. package/dist/chunk-6XCINXZ7.cjs +194 -0
  217. package/dist/chunk-6XCINXZ7.cjs.map +1 -0
  218. package/dist/chunk-7J5DS33F.js +3 -0
  219. package/dist/chunk-7J5DS33F.js.map +1 -0
  220. package/dist/chunk-A5KDVZDL.cjs +124 -0
  221. package/dist/chunk-A5KDVZDL.cjs.map +1 -0
  222. package/dist/chunk-AAYCOJUL.cjs +14 -0
  223. package/dist/chunk-AAYCOJUL.cjs.map +1 -0
  224. package/dist/chunk-ABJOUEVA.cjs +10 -0
  225. package/dist/chunk-ABJOUEVA.cjs.map +1 -0
  226. package/dist/chunk-AQGLVU53.cjs +603 -0
  227. package/dist/chunk-AQGLVU53.cjs.map +1 -0
  228. package/dist/chunk-B7V6NYWH.cjs +67 -0
  229. package/dist/chunk-B7V6NYWH.cjs.map +1 -0
  230. package/dist/chunk-BJ6XIEC6.js +13 -0
  231. package/dist/chunk-BJ6XIEC6.js.map +1 -0
  232. package/dist/chunk-BLUDYAPI.js +596 -0
  233. package/dist/chunk-BLUDYAPI.js.map +1 -0
  234. package/dist/chunk-BVUMKER5.js +7 -0
  235. package/dist/chunk-BVUMKER5.js.map +1 -0
  236. package/dist/chunk-BWYU7D33.js +192 -0
  237. package/dist/chunk-BWYU7D33.js.map +1 -0
  238. package/dist/chunk-CNMPROPI.js +24 -0
  239. package/dist/chunk-CNMPROPI.js.map +1 -0
  240. package/dist/chunk-CT2DMHGC.cjs +271 -0
  241. package/dist/chunk-CT2DMHGC.cjs.map +1 -0
  242. package/dist/chunk-CYEQK4PM.cjs +778 -0
  243. package/dist/chunk-CYEQK4PM.cjs.map +1 -0
  244. package/dist/chunk-DMBN72QF.cjs +39 -0
  245. package/dist/chunk-DMBN72QF.cjs.map +1 -0
  246. package/dist/chunk-EBVYYC2Q.cjs +111 -0
  247. package/dist/chunk-EBVYYC2Q.cjs.map +1 -0
  248. package/dist/chunk-F7GI2OYP.js +3 -0
  249. package/dist/chunk-F7GI2OYP.js.map +1 -0
  250. package/dist/chunk-FUVNHPNL.cjs +4 -0
  251. package/dist/chunk-FUVNHPNL.cjs.map +1 -0
  252. package/dist/chunk-FVIT6JZ5.cjs +4 -0
  253. package/dist/chunk-FVIT6JZ5.cjs.map +1 -0
  254. package/dist/chunk-G3KSVDML.cjs +1310 -0
  255. package/dist/chunk-G3KSVDML.cjs.map +1 -0
  256. package/dist/chunk-GPWMM745.cjs +80 -0
  257. package/dist/chunk-GPWMM745.cjs.map +1 -0
  258. package/dist/chunk-GXSERFAG.js +3 -0
  259. package/dist/chunk-GXSERFAG.js.map +1 -0
  260. package/dist/chunk-HCCXJ5YJ.js +466 -0
  261. package/dist/chunk-HCCXJ5YJ.js.map +1 -0
  262. package/dist/chunk-HLRWYUFN.js +78 -0
  263. package/dist/chunk-HLRWYUFN.js.map +1 -0
  264. package/dist/chunk-HQDX7PWJ.cjs +26 -0
  265. package/dist/chunk-HQDX7PWJ.cjs.map +1 -0
  266. package/dist/chunk-HSX2K7HB.js +164 -0
  267. package/dist/chunk-HSX2K7HB.js.map +1 -0
  268. package/dist/chunk-IAJHRFO4.cjs +2936 -0
  269. package/dist/chunk-IAJHRFO4.cjs.map +1 -0
  270. package/dist/chunk-INOOZ2A2.js +3144 -0
  271. package/dist/chunk-INOOZ2A2.js.map +1 -0
  272. package/dist/chunk-IWU4YSYT.cjs +166 -0
  273. package/dist/chunk-IWU4YSYT.cjs.map +1 -0
  274. package/dist/chunk-JQE3BLYA.js +3 -0
  275. package/dist/chunk-JQE3BLYA.js.map +1 -0
  276. package/dist/chunk-JVV5LREI.js +431 -0
  277. package/dist/chunk-JVV5LREI.js.map +1 -0
  278. package/dist/chunk-LZPOIBOR.js +87 -0
  279. package/dist/chunk-LZPOIBOR.js.map +1 -0
  280. package/dist/chunk-MKCC5K77.js +90 -0
  281. package/dist/chunk-MKCC5K77.js.map +1 -0
  282. package/dist/chunk-N2ILP2YD.js +2539 -0
  283. package/dist/chunk-N2ILP2YD.js.map +1 -0
  284. package/dist/chunk-NLNKQD2T.js +7 -0
  285. package/dist/chunk-NLNKQD2T.js.map +1 -0
  286. package/dist/chunk-NPNGPMT2.js +39 -0
  287. package/dist/chunk-NPNGPMT2.js.map +1 -0
  288. package/dist/chunk-NSIYMRY7.cjs +91 -0
  289. package/dist/chunk-NSIYMRY7.cjs.map +1 -0
  290. package/dist/chunk-OIT3PCWA.js +72 -0
  291. package/dist/chunk-OIT3PCWA.js.map +1 -0
  292. package/dist/chunk-PAOBGBU7.cjs +471 -0
  293. package/dist/chunk-PAOBGBU7.cjs.map +1 -0
  294. package/dist/chunk-PE3V7GUL.cjs +4 -0
  295. package/dist/chunk-PE3V7GUL.cjs.map +1 -0
  296. package/dist/chunk-PFXXH2RP.js +91 -0
  297. package/dist/chunk-PFXXH2RP.js.map +1 -0
  298. package/dist/chunk-PJKCPRYF.js +3 -0
  299. package/dist/chunk-PJKCPRYF.js.map +1 -0
  300. package/dist/chunk-PZUZNPFM.js +166 -0
  301. package/dist/chunk-PZUZNPFM.js.map +1 -0
  302. package/dist/chunk-Q6LWNLAJ.js +3177 -0
  303. package/dist/chunk-Q6LWNLAJ.js.map +1 -0
  304. package/dist/chunk-QLMWFSDC.js +653 -0
  305. package/dist/chunk-QLMWFSDC.js.map +1 -0
  306. package/dist/chunk-R6XC4DV5.js +170 -0
  307. package/dist/chunk-R6XC4DV5.js.map +1 -0
  308. package/dist/chunk-RJOVFHWN.cjs +3160 -0
  309. package/dist/chunk-RJOVFHWN.cjs.map +1 -0
  310. package/dist/chunk-SJMKDSRF.js +179 -0
  311. package/dist/chunk-SJMKDSRF.js.map +1 -0
  312. package/dist/chunk-TCHOIK53.cjs +4 -0
  313. package/dist/chunk-TCHOIK53.cjs.map +1 -0
  314. package/dist/chunk-TEORHM5O.js +1222 -0
  315. package/dist/chunk-TEORHM5O.js.map +1 -0
  316. package/dist/chunk-TSNDVBUU.cjs +90 -0
  317. package/dist/chunk-TSNDVBUU.cjs.map +1 -0
  318. package/dist/chunk-TTELJD4F.js +86 -0
  319. package/dist/chunk-TTELJD4F.js.map +1 -0
  320. package/dist/chunk-TX4TTPYJ.cjs +436 -0
  321. package/dist/chunk-TX4TTPYJ.cjs.map +1 -0
  322. package/dist/chunk-UCPGYU55.cjs +15 -0
  323. package/dist/chunk-UCPGYU55.cjs.map +1 -0
  324. package/dist/chunk-UWTYVVVZ.js +42 -0
  325. package/dist/chunk-UWTYVVVZ.js.map +1 -0
  326. package/dist/chunk-UXG7PYML.js +118 -0
  327. package/dist/chunk-UXG7PYML.js.map +1 -0
  328. package/dist/chunk-V3VLOOSW.cjs +36 -0
  329. package/dist/chunk-V3VLOOSW.cjs.map +1 -0
  330. package/dist/chunk-VED2OIYL.cjs +4 -0
  331. package/dist/chunk-VED2OIYL.cjs.map +1 -0
  332. package/dist/chunk-VF676YCO.cjs +150 -0
  333. package/dist/chunk-VF676YCO.cjs.map +1 -0
  334. package/dist/chunk-VHEA3YXS.js +72 -0
  335. package/dist/chunk-VHEA3YXS.js.map +1 -0
  336. package/dist/chunk-VQASQG5D.js +55 -0
  337. package/dist/chunk-VQASQG5D.js.map +1 -0
  338. package/dist/chunk-VXHOOZSK.js +446 -0
  339. package/dist/chunk-VXHOOZSK.js.map +1 -0
  340. package/dist/chunk-W4UVO3HL.cjs +82 -0
  341. package/dist/chunk-W4UVO3HL.cjs.map +1 -0
  342. package/dist/chunk-WIMFJ2BA.js +2880 -0
  343. package/dist/chunk-WIMFJ2BA.js.map +1 -0
  344. package/dist/chunk-WM4RO23J.js +145 -0
  345. package/dist/chunk-WM4RO23J.js.map +1 -0
  346. package/dist/chunk-WMTXUQC4.cjs +657 -0
  347. package/dist/chunk-WMTXUQC4.cjs.map +1 -0
  348. package/dist/chunk-WNV57BEW.cjs +1705 -0
  349. package/dist/chunk-WNV57BEW.cjs.map +1 -0
  350. package/dist/chunk-X4RMXTXF.cjs +4 -0
  351. package/dist/chunk-X4RMXTXF.cjs.map +1 -0
  352. package/dist/chunk-X7F4CSGR.cjs +67 -0
  353. package/dist/chunk-X7F4CSGR.cjs.map +1 -0
  354. package/dist/chunk-XWGHD4C7.cjs +450 -0
  355. package/dist/chunk-XWGHD4C7.cjs.map +1 -0
  356. package/dist/chunk-YFKTLS32.js +1700 -0
  357. package/dist/chunk-YFKTLS32.js.map +1 -0
  358. package/dist/chunk-ZABG3SZ2.cjs +93 -0
  359. package/dist/chunk-ZABG3SZ2.cjs.map +1 -0
  360. package/dist/chunk-ZIWN73GQ.js +267 -0
  361. package/dist/chunk-ZIWN73GQ.js.map +1 -0
  362. package/dist/chunk-ZOYE65RA.cjs +173 -0
  363. package/dist/chunk-ZOYE65RA.cjs.map +1 -0
  364. package/dist/deployer/index.cjs +12 -0
  365. package/dist/deployer/index.cjs.map +1 -0
  366. package/dist/deployer/index.d.ts +12 -0
  367. package/dist/deployer/index.d.ts.map +1 -0
  368. package/dist/deployer/index.js +3 -0
  369. package/dist/deployer/index.js.map +1 -0
  370. package/dist/deployer/index.warning.d.ts +9 -0
  371. package/dist/deployer/index.warning.d.ts.map +1 -0
  372. package/dist/di/index.cjs +12 -0
  373. package/dist/di/index.cjs.map +1 -0
  374. package/dist/di/index.d.ts +2 -0
  375. package/dist/di/index.d.ts.map +1 -0
  376. package/dist/di/index.js +3 -0
  377. package/dist/di/index.js.map +1 -0
  378. package/dist/error/index.cjs +28 -0
  379. package/dist/error/index.cjs.map +1 -0
  380. package/dist/error/index.d.ts +89 -0
  381. package/dist/error/index.d.ts.map +1 -0
  382. package/dist/error/index.js +3 -0
  383. package/dist/error/index.js.map +1 -0
  384. package/dist/error/utils.d.ts +36 -0
  385. package/dist/error/utils.d.ts.map +1 -0
  386. package/dist/eval/evaluation.d.ts +14 -0
  387. package/dist/eval/evaluation.d.ts.map +1 -0
  388. package/dist/eval/index.cjs +16 -0
  389. package/dist/eval/index.cjs.map +1 -0
  390. package/dist/eval/index.d.ts +4 -0
  391. package/dist/eval/index.d.ts.map +1 -0
  392. package/dist/eval/index.js +3 -0
  393. package/dist/eval/index.js.map +1 -0
  394. package/dist/eval/metric.d.ts +8 -0
  395. package/dist/eval/metric.d.ts.map +1 -0
  396. package/dist/eval/types.d.ts +9 -0
  397. package/dist/eval/types.d.ts.map +1 -0
  398. package/dist/events/event-emitter.d.ts +11 -0
  399. package/dist/events/event-emitter.d.ts.map +1 -0
  400. package/dist/events/index.cjs +12 -0
  401. package/dist/events/index.cjs.map +1 -0
  402. package/dist/events/index.d.ts +3 -0
  403. package/dist/events/index.d.ts.map +1 -0
  404. package/dist/events/index.js +3 -0
  405. package/dist/events/index.js.map +1 -0
  406. package/dist/events/processor.d.ts +11 -0
  407. package/dist/events/processor.d.ts.map +1 -0
  408. package/dist/events/pubsub.d.ts +8 -0
  409. package/dist/events/pubsub.d.ts.map +1 -0
  410. package/dist/events/types.d.ts +8 -0
  411. package/dist/events/types.d.ts.map +1 -0
  412. package/dist/hooks/index.cjs +20 -0
  413. package/dist/hooks/index.cjs.map +1 -0
  414. package/dist/hooks/index.d.ts +36 -0
  415. package/dist/hooks/index.d.ts.map +1 -0
  416. package/dist/hooks/index.js +3 -0
  417. package/dist/hooks/index.js.map +1 -0
  418. package/dist/hooks/mitt.d.ts +22 -0
  419. package/dist/hooks/mitt.d.ts.map +1 -0
  420. package/dist/index.cjs +371 -0
  421. package/dist/index.cjs.map +1 -0
  422. package/dist/index.d.ts +19 -0
  423. package/dist/index.d.ts.map +1 -0
  424. package/dist/index.js +120 -0
  425. package/dist/index.js.map +1 -0
  426. package/dist/integration/index.cjs +16 -0
  427. package/dist/integration/index.cjs.map +1 -0
  428. package/dist/integration/index.d.ts +3 -0
  429. package/dist/integration/index.d.ts.map +1 -0
  430. package/dist/integration/index.js +3 -0
  431. package/dist/integration/index.js.map +1 -0
  432. package/dist/integration/index.warning.d.ts +3 -0
  433. package/dist/integration/index.warning.d.ts.map +1 -0
  434. package/dist/integration/integration.d.ts +21 -0
  435. package/dist/integration/integration.d.ts.map +1 -0
  436. package/dist/integration/integration.warning.d.ts +5 -0
  437. package/dist/integration/integration.warning.d.ts.map +1 -0
  438. package/dist/integration/openapi-toolset.d.ts +16 -0
  439. package/dist/integration/openapi-toolset.d.ts.map +1 -0
  440. package/dist/integration/openapi-toolset.warning.d.ts +5 -0
  441. package/dist/integration/openapi-toolset.warning.d.ts.map +1 -0
  442. package/dist/llm/index.cjs +48 -0
  443. package/dist/llm/index.cjs.map +1 -0
  444. package/dist/llm/index.d.ts +83 -0
  445. package/dist/llm/index.d.ts.map +1 -0
  446. package/dist/llm/index.js +7 -0
  447. package/dist/llm/index.js.map +1 -0
  448. package/dist/llm/model/aisdk/v5/model.d.ts +47 -0
  449. package/dist/llm/model/aisdk/v5/model.d.ts.map +1 -0
  450. package/dist/llm/model/base.types.d.ts +100 -0
  451. package/dist/llm/model/base.types.d.ts.map +1 -0
  452. package/dist/llm/model/embedding-router.d.ts +50 -0
  453. package/dist/llm/model/embedding-router.d.ts.map +1 -0
  454. package/dist/llm/model/gateway-resolver.d.ts +11 -0
  455. package/dist/llm/model/gateway-resolver.d.ts.map +1 -0
  456. package/dist/llm/model/gateways/base.d.ts +50 -0
  457. package/dist/llm/model/gateways/base.d.ts.map +1 -0
  458. package/dist/llm/model/gateways/constants.d.ts +3 -0
  459. package/dist/llm/model/gateways/constants.d.ts.map +1 -0
  460. package/dist/llm/model/gateways/index.d.ts +11 -0
  461. package/dist/llm/model/gateways/index.d.ts.map +1 -0
  462. package/dist/llm/model/gateways/models-dev.d.ts +19 -0
  463. package/dist/llm/model/gateways/models-dev.d.ts.map +1 -0
  464. package/dist/llm/model/gateways/netlify.d.ts +25 -0
  465. package/dist/llm/model/gateways/netlify.d.ts.map +1 -0
  466. package/dist/llm/model/index.d.ts +6 -0
  467. package/dist/llm/model/index.d.ts.map +1 -0
  468. package/dist/llm/model/is-v2-model.d.ts +3 -0
  469. package/dist/llm/model/is-v2-model.d.ts.map +1 -0
  470. package/dist/llm/model/model-method-from-agent.d.ts +4 -0
  471. package/dist/llm/model/model-method-from-agent.d.ts.map +1 -0
  472. package/dist/llm/model/model.d.ts +36 -0
  473. package/dist/llm/model/model.d.ts.map +1 -0
  474. package/dist/llm/model/model.loop.d.ts +26 -0
  475. package/dist/llm/model/model.loop.d.ts.map +1 -0
  476. package/dist/llm/model/model.loop.types.d.ts +31 -0
  477. package/dist/llm/model/model.loop.types.d.ts.map +1 -0
  478. package/dist/llm/model/provider-options.d.ts +42 -0
  479. package/dist/llm/model/provider-options.d.ts.map +1 -0
  480. package/dist/llm/model/provider-registry.d.ts +108 -0
  481. package/dist/llm/model/provider-registry.d.ts.map +1 -0
  482. package/dist/llm/model/provider-types.generated.d.ts +1205 -0
  483. package/dist/llm/model/registry-generator.d.ts +41 -0
  484. package/dist/llm/model/registry-generator.d.ts.map +1 -0
  485. package/dist/llm/model/resolve-model.d.ts +46 -0
  486. package/dist/llm/model/resolve-model.d.ts.map +1 -0
  487. package/dist/llm/model/router.d.ts +30 -0
  488. package/dist/llm/model/router.d.ts.map +1 -0
  489. package/dist/llm/model/shared.types.d.ts +40 -0
  490. package/dist/llm/model/shared.types.d.ts.map +1 -0
  491. package/dist/logger/constants.d.ts +28 -0
  492. package/dist/logger/constants.d.ts.map +1 -0
  493. package/dist/logger/default-logger.d.ts +49 -0
  494. package/dist/logger/default-logger.d.ts.map +1 -0
  495. package/dist/logger/index.cjs +45 -0
  496. package/dist/logger/index.cjs.map +1 -0
  497. package/dist/logger/index.d.ts +7 -0
  498. package/dist/logger/index.d.ts.map +1 -0
  499. package/dist/logger/index.js +4 -0
  500. package/dist/logger/index.js.map +1 -0
  501. package/dist/logger/logger.d.ts +88 -0
  502. package/dist/logger/logger.d.ts.map +1 -0
  503. package/dist/logger/multi-logger.d.ts +46 -0
  504. package/dist/logger/multi-logger.d.ts.map +1 -0
  505. package/dist/logger/noop-logger.d.ts +3 -0
  506. package/dist/logger/noop-logger.d.ts.map +1 -0
  507. package/dist/logger/transport.d.ts +46 -0
  508. package/dist/logger/transport.d.ts.map +1 -0
  509. package/dist/loop/index.cjs +12 -0
  510. package/dist/loop/index.cjs.map +1 -0
  511. package/dist/loop/index.d.ts +2 -0
  512. package/dist/loop/index.d.ts.map +1 -0
  513. package/dist/loop/index.js +3 -0
  514. package/dist/loop/index.js.map +1 -0
  515. package/dist/loop/loop.d.ts +6 -0
  516. package/dist/loop/loop.d.ts.map +1 -0
  517. package/dist/loop/network/index.d.ts +145 -0
  518. package/dist/loop/network/index.d.ts.map +1 -0
  519. package/dist/loop/telemetry/index.d.ts +36 -0
  520. package/dist/loop/telemetry/index.d.ts.map +1 -0
  521. package/dist/loop/telemetry/noop.d.ts +3 -0
  522. package/dist/loop/telemetry/noop.d.ts.map +1 -0
  523. package/dist/loop/test-utils/MastraLanguageModelV2Mock.d.ts +37 -0
  524. package/dist/loop/test-utils/MastraLanguageModelV2Mock.d.ts.map +1 -0
  525. package/dist/loop/test-utils/fullStream.d.ts +6 -0
  526. package/dist/loop/test-utils/fullStream.d.ts.map +1 -0
  527. package/dist/loop/test-utils/generateText.d.ts +6 -0
  528. package/dist/loop/test-utils/generateText.d.ts.map +1 -0
  529. package/dist/loop/test-utils/mock-server-response.d.ts +28 -0
  530. package/dist/loop/test-utils/mock-server-response.d.ts.map +1 -0
  531. package/dist/loop/test-utils/mockTracer.d.ts +47 -0
  532. package/dist/loop/test-utils/mockTracer.d.ts.map +1 -0
  533. package/dist/loop/test-utils/options.d.ts +6 -0
  534. package/dist/loop/test-utils/options.d.ts.map +1 -0
  535. package/dist/loop/test-utils/resultObject.d.ts +6 -0
  536. package/dist/loop/test-utils/resultObject.d.ts.map +1 -0
  537. package/dist/loop/test-utils/streamObject.d.ts +15 -0
  538. package/dist/loop/test-utils/streamObject.d.ts.map +1 -0
  539. package/dist/loop/test-utils/telemetry.d.ts +6 -0
  540. package/dist/loop/test-utils/telemetry.d.ts.map +1 -0
  541. package/dist/loop/test-utils/textStream.d.ts +6 -0
  542. package/dist/loop/test-utils/textStream.d.ts.map +1 -0
  543. package/dist/loop/test-utils/toUIMessageStream.d.ts +6 -0
  544. package/dist/loop/test-utils/toUIMessageStream.d.ts.map +1 -0
  545. package/dist/loop/test-utils/tools.d.ts +6 -0
  546. package/dist/loop/test-utils/tools.d.ts.map +1 -0
  547. package/dist/loop/test-utils/utils.d.ts +45 -0
  548. package/dist/loop/test-utils/utils.d.ts.map +1 -0
  549. package/dist/loop/types.d.ts +111 -0
  550. package/dist/loop/types.d.ts.map +1 -0
  551. package/dist/loop/workflows/agentic-execution/index.d.ts +939 -0
  552. package/dist/loop/workflows/agentic-execution/index.d.ts.map +1 -0
  553. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts +629 -0
  554. package/dist/loop/workflows/agentic-execution/llm-execution-step.d.ts.map +1 -0
  555. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts +348 -0
  556. package/dist/loop/workflows/agentic-execution/llm-mapping-step.d.ts.map +1 -0
  557. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts +58 -0
  558. package/dist/loop/workflows/agentic-execution/tool-call-step.d.ts.map +1 -0
  559. package/dist/loop/workflows/agentic-loop/index.d.ts +945 -0
  560. package/dist/loop/workflows/agentic-loop/index.d.ts.map +1 -0
  561. package/dist/loop/workflows/run-state.d.ts +29 -0
  562. package/dist/loop/workflows/run-state.d.ts.map +1 -0
  563. package/dist/loop/workflows/schema.d.ts +479 -0
  564. package/dist/loop/workflows/schema.d.ts.map +1 -0
  565. package/dist/loop/workflows/stream.d.ts +20 -0
  566. package/dist/loop/workflows/stream.d.ts.map +1 -0
  567. package/dist/mastra/hooks.d.ts +6 -0
  568. package/dist/mastra/hooks.d.ts.map +1 -0
  569. package/dist/mastra/index.cjs +12 -0
  570. package/dist/mastra/index.cjs.map +1 -0
  571. package/dist/mastra/index.d.ts +892 -0
  572. package/dist/mastra/index.d.ts.map +1 -0
  573. package/dist/mastra/index.js +3 -0
  574. package/dist/mastra/index.js.map +1 -0
  575. package/dist/mcp/index.cjs +112 -0
  576. package/dist/mcp/index.cjs.map +1 -0
  577. package/dist/mcp/index.d.ts +158 -0
  578. package/dist/mcp/index.d.ts.map +1 -0
  579. package/dist/mcp/index.js +106 -0
  580. package/dist/mcp/index.js.map +1 -0
  581. package/dist/mcp/types.d.ts +255 -0
  582. package/dist/mcp/types.d.ts.map +1 -0
  583. package/dist/memory/index.cjs +20 -0
  584. package/dist/memory/index.cjs.map +1 -0
  585. package/dist/memory/index.d.ts +3 -0
  586. package/dist/memory/index.d.ts.map +1 -0
  587. package/dist/memory/index.js +3 -0
  588. package/dist/memory/index.js.map +1 -0
  589. package/dist/memory/index.warning.d.ts +6 -0
  590. package/dist/memory/index.warning.d.ts.map +1 -0
  591. package/dist/memory/memory.d.ts +272 -0
  592. package/dist/memory/memory.d.ts.map +1 -0
  593. package/dist/memory/types.d.ts +393 -0
  594. package/dist/memory/types.d.ts.map +1 -0
  595. package/dist/models-dev-LJ4O6FNF.js +3 -0
  596. package/dist/models-dev-LJ4O6FNF.js.map +1 -0
  597. package/dist/models-dev-T2CXOEDH.cjs +12 -0
  598. package/dist/models-dev-T2CXOEDH.cjs.map +1 -0
  599. package/dist/netlify-BAEMIQNH.cjs +12 -0
  600. package/dist/netlify-BAEMIQNH.cjs.map +1 -0
  601. package/dist/netlify-L7AZ74PV.js +3 -0
  602. package/dist/netlify-L7AZ74PV.js.map +1 -0
  603. package/dist/processors/index.cjs +48 -0
  604. package/dist/processors/index.cjs.map +1 -0
  605. package/dist/processors/index.d.ts +43 -0
  606. package/dist/processors/index.d.ts.map +1 -0
  607. package/dist/processors/index.js +3 -0
  608. package/dist/processors/index.js.map +1 -0
  609. package/dist/processors/processors/batch-parts.d.ts +47 -0
  610. package/dist/processors/processors/batch-parts.d.ts.map +1 -0
  611. package/dist/processors/processors/index.d.ts +10 -0
  612. package/dist/processors/processors/index.d.ts.map +1 -0
  613. package/dist/processors/processors/language-detector.d.ts +153 -0
  614. package/dist/processors/processors/language-detector.d.ts.map +1 -0
  615. package/dist/processors/processors/moderation.d.ts +138 -0
  616. package/dist/processors/processors/moderation.d.ts.map +1 -0
  617. package/dist/processors/processors/pii-detector.d.ts +203 -0
  618. package/dist/processors/processors/pii-detector.d.ts.map +1 -0
  619. package/dist/processors/processors/prompt-injection-detector.d.ts +116 -0
  620. package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -0
  621. package/dist/processors/processors/structured-output.d.ts +53 -0
  622. package/dist/processors/processors/structured-output.d.ts.map +1 -0
  623. package/dist/processors/processors/system-prompt-scrubber.d.ts +105 -0
  624. package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -0
  625. package/dist/processors/processors/token-limiter.d.ts +66 -0
  626. package/dist/processors/processors/token-limiter.d.ts.map +1 -0
  627. package/dist/processors/processors/unicode-normalizer.d.ts +34 -0
  628. package/dist/processors/processors/unicode-normalizer.d.ts.map +1 -0
  629. package/dist/processors/runner.d.ts +46 -0
  630. package/dist/processors/runner.d.ts.map +1 -0
  631. package/dist/provider-registry-RUDYHG7J.cjs +40 -0
  632. package/dist/provider-registry-RUDYHG7J.cjs.map +1 -0
  633. package/dist/provider-registry-THITZUJ7.js +3 -0
  634. package/dist/provider-registry-THITZUJ7.js.map +1 -0
  635. package/dist/provider-registry.json +2748 -0
  636. package/dist/registry-generator-DL42NMBM.cjs +134 -0
  637. package/dist/registry-generator-DL42NMBM.cjs.map +1 -0
  638. package/dist/registry-generator-I6S4ARS6.js +124 -0
  639. package/dist/registry-generator-I6S4ARS6.js.map +1 -0
  640. package/dist/relevance/cohere/index.d.ts +9 -0
  641. package/dist/relevance/cohere/index.d.ts.map +1 -0
  642. package/dist/relevance/index.cjs +20 -0
  643. package/dist/relevance/index.cjs.map +1 -0
  644. package/dist/relevance/index.d.ts +4 -0
  645. package/dist/relevance/index.d.ts.map +1 -0
  646. package/dist/relevance/index.js +3 -0
  647. package/dist/relevance/index.js.map +1 -0
  648. package/dist/relevance/mastra-agent/index.d.ts +8 -0
  649. package/dist/relevance/mastra-agent/index.d.ts.map +1 -0
  650. package/dist/relevance/relevance-score-provider.d.ts +5 -0
  651. package/dist/relevance/relevance-score-provider.d.ts.map +1 -0
  652. package/dist/run/types.d.ts +6 -0
  653. package/dist/run/types.d.ts.map +1 -0
  654. package/dist/runtime-context/index.cjs +12 -0
  655. package/dist/runtime-context/index.cjs.map +1 -0
  656. package/dist/runtime-context/index.d.ts +57 -0
  657. package/dist/runtime-context/index.d.ts.map +1 -0
  658. package/dist/runtime-context/index.js +3 -0
  659. package/dist/runtime-context/index.js.map +1 -0
  660. package/dist/scores/base.d.ts +141 -0
  661. package/dist/scores/base.d.ts.map +1 -0
  662. package/dist/scores/base.test-utils.d.ts +124 -0
  663. package/dist/scores/base.test-utils.d.ts.map +1 -0
  664. package/dist/scores/hooks.d.ts +19 -0
  665. package/dist/scores/hooks.d.ts.map +1 -0
  666. package/dist/scores/index.cjs +37 -0
  667. package/dist/scores/index.cjs.map +1 -0
  668. package/dist/scores/index.d.ts +4 -0
  669. package/dist/scores/index.d.ts.map +1 -0
  670. package/dist/scores/index.js +4 -0
  671. package/dist/scores/index.js.map +1 -0
  672. package/dist/scores/run-experiment/index.d.ts +61 -0
  673. package/dist/scores/run-experiment/index.d.ts.map +1 -0
  674. package/dist/scores/run-experiment/scorerAccumulator.d.ts +12 -0
  675. package/dist/scores/run-experiment/scorerAccumulator.d.ts.map +1 -0
  676. package/dist/scores/scoreTraces/index.cjs +431 -0
  677. package/dist/scores/scoreTraces/index.cjs.map +1 -0
  678. package/dist/scores/scoreTraces/index.d.ts +3 -0
  679. package/dist/scores/scoreTraces/index.d.ts.map +1 -0
  680. package/dist/scores/scoreTraces/index.js +423 -0
  681. package/dist/scores/scoreTraces/index.js.map +1 -0
  682. package/dist/scores/scoreTraces/scoreTraces.d.ts +10 -0
  683. package/dist/scores/scoreTraces/scoreTraces.d.ts.map +1 -0
  684. package/dist/scores/scoreTraces/scoreTracesWorkflow.d.ts +95 -0
  685. package/dist/scores/scoreTraces/scoreTracesWorkflow.d.ts.map +1 -0
  686. package/dist/scores/scoreTraces/utils.d.ts +21 -0
  687. package/dist/scores/scoreTraces/utils.d.ts.map +1 -0
  688. package/dist/scores/types.d.ts +191 -0
  689. package/dist/scores/types.d.ts.map +1 -0
  690. package/dist/server/auth.d.ts +36 -0
  691. package/dist/server/auth.d.ts.map +1 -0
  692. package/dist/server/composite-auth.d.ts +9 -0
  693. package/dist/server/composite-auth.d.ts.map +1 -0
  694. package/dist/server/index.cjs +185 -0
  695. package/dist/server/index.cjs.map +1 -0
  696. package/dist/server/index.d.ts +37 -0
  697. package/dist/server/index.d.ts.map +1 -0
  698. package/dist/server/index.js +180 -0
  699. package/dist/server/index.js.map +1 -0
  700. package/dist/server/simple-auth.d.ts +32 -0
  701. package/dist/server/simple-auth.d.ts.map +1 -0
  702. package/dist/server/types.d.ts +140 -0
  703. package/dist/server/types.d.ts.map +1 -0
  704. package/dist/storage/base.d.ts +360 -0
  705. package/dist/storage/base.d.ts.map +1 -0
  706. package/dist/storage/base.warning.d.ts +8 -0
  707. package/dist/storage/base.warning.d.ts.map +1 -0
  708. package/dist/storage/constants.d.ts +14 -0
  709. package/dist/storage/constants.d.ts.map +1 -0
  710. package/dist/storage/domains/index.d.ts +8 -0
  711. package/dist/storage/domains/index.d.ts.map +1 -0
  712. package/dist/storage/domains/legacy-evals/base.d.ts +13 -0
  713. package/dist/storage/domains/legacy-evals/base.d.ts.map +1 -0
  714. package/dist/storage/domains/legacy-evals/index.d.ts +2 -0
  715. package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
  716. package/dist/storage/domains/legacy-evals/inmemory.d.ts +17 -0
  717. package/dist/storage/domains/legacy-evals/inmemory.d.ts.map +1 -0
  718. package/dist/storage/domains/memory/base.d.ts +96 -0
  719. package/dist/storage/domains/memory/base.d.ts.map +1 -0
  720. package/dist/storage/domains/memory/index.d.ts +3 -0
  721. package/dist/storage/domains/memory/index.d.ts.map +1 -0
  722. package/dist/storage/domains/memory/inmemory.d.ts +85 -0
  723. package/dist/storage/domains/memory/inmemory.d.ts.map +1 -0
  724. package/dist/storage/domains/observability/base.d.ts +60 -0
  725. package/dist/storage/domains/observability/base.d.ts.map +1 -0
  726. package/dist/storage/domains/observability/index.d.ts +3 -0
  727. package/dist/storage/domains/observability/index.d.ts.map +1 -0
  728. package/dist/storage/domains/observability/inmemory.d.ts +48 -0
  729. package/dist/storage/domains/observability/inmemory.d.ts.map +1 -0
  730. package/dist/storage/domains/operations/base.d.ts +68 -0
  731. package/dist/storage/domains/operations/base.d.ts.map +1 -0
  732. package/dist/storage/domains/operations/index.d.ts +3 -0
  733. package/dist/storage/domains/operations/index.d.ts.map +1 -0
  734. package/dist/storage/domains/operations/inmemory.d.ts +37 -0
  735. package/dist/storage/domains/operations/inmemory.d.ts.map +1 -0
  736. package/dist/storage/domains/scores/base.d.ts +46 -0
  737. package/dist/storage/domains/scores/base.d.ts.map +1 -0
  738. package/dist/storage/domains/scores/index.d.ts +3 -0
  739. package/dist/storage/domains/scores/index.d.ts.map +1 -0
  740. package/dist/storage/domains/scores/inmemory.d.ts +50 -0
  741. package/dist/storage/domains/scores/inmemory.d.ts.map +1 -0
  742. package/dist/storage/domains/traces/base.d.ts +14 -0
  743. package/dist/storage/domains/traces/base.d.ts.map +1 -0
  744. package/dist/storage/domains/traces/index.d.ts +3 -0
  745. package/dist/storage/domains/traces/index.d.ts.map +1 -0
  746. package/dist/storage/domains/traces/inmemory.d.ts +22 -0
  747. package/dist/storage/domains/traces/inmemory.d.ts.map +1 -0
  748. package/dist/storage/domains/workflows/base.d.ts +47 -0
  749. package/dist/storage/domains/workflows/base.d.ts.map +1 -0
  750. package/dist/storage/domains/workflows/index.d.ts +3 -0
  751. package/dist/storage/domains/workflows/index.d.ts.map +1 -0
  752. package/dist/storage/domains/workflows/inmemory.d.ts +54 -0
  753. package/dist/storage/domains/workflows/inmemory.d.ts.map +1 -0
  754. package/dist/storage/index.cjs +1821 -0
  755. package/dist/storage/index.cjs.map +1 -0
  756. package/dist/storage/index.d.ts +7 -0
  757. package/dist/storage/index.d.ts.map +1 -0
  758. package/dist/storage/index.js +1746 -0
  759. package/dist/storage/index.js.map +1 -0
  760. package/dist/storage/index.warning.d.ts +3 -0
  761. package/dist/storage/index.warning.d.ts.map +1 -0
  762. package/dist/storage/mock.d.ts +242 -0
  763. package/dist/storage/mock.d.ts.map +1 -0
  764. package/dist/storage/storageWithInit.d.ts +3 -0
  765. package/dist/storage/storageWithInit.d.ts.map +1 -0
  766. package/dist/storage/types.d.ts +206 -0
  767. package/dist/storage/types.d.ts.map +1 -0
  768. package/dist/storage/utils.d.ts +2 -0
  769. package/dist/storage/utils.d.ts.map +1 -0
  770. package/dist/stream/MastraAgentNetworkStream.d.ts +22 -0
  771. package/dist/stream/MastraAgentNetworkStream.d.ts.map +1 -0
  772. package/dist/stream/MastraWorkflowStream.d.ts +19 -0
  773. package/dist/stream/MastraWorkflowStream.d.ts.map +1 -0
  774. package/dist/stream/RunOutput.d.ts +72 -0
  775. package/dist/stream/RunOutput.d.ts.map +1 -0
  776. package/dist/stream/aisdk/v4/input.d.ts +16 -0
  777. package/dist/stream/aisdk/v4/input.d.ts.map +1 -0
  778. package/dist/stream/aisdk/v4/transform.d.ts +5 -0
  779. package/dist/stream/aisdk/v4/transform.d.ts.map +1 -0
  780. package/dist/stream/aisdk/v5/compat/consume-stream.d.ts +8 -0
  781. package/dist/stream/aisdk/v5/compat/consume-stream.d.ts.map +1 -0
  782. package/dist/stream/aisdk/v5/compat/content.d.ts +7 -0
  783. package/dist/stream/aisdk/v5/compat/content.d.ts.map +1 -0
  784. package/dist/stream/aisdk/v5/compat/delayed-promise.d.ts +23 -0
  785. package/dist/stream/aisdk/v5/compat/delayed-promise.d.ts.map +1 -0
  786. package/dist/stream/aisdk/v5/compat/index.d.ts +8 -0
  787. package/dist/stream/aisdk/v5/compat/index.d.ts.map +1 -0
  788. package/dist/stream/aisdk/v5/compat/media.d.ts +91 -0
  789. package/dist/stream/aisdk/v5/compat/media.d.ts.map +1 -0
  790. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts +11 -0
  791. package/dist/stream/aisdk/v5/compat/prepare-tools.d.ts.map +1 -0
  792. package/dist/stream/aisdk/v5/compat/ui-message.d.ts +20 -0
  793. package/dist/stream/aisdk/v5/compat/ui-message.d.ts.map +1 -0
  794. package/dist/stream/aisdk/v5/compat/validation.d.ts +17 -0
  795. package/dist/stream/aisdk/v5/compat/validation.d.ts.map +1 -0
  796. package/dist/stream/aisdk/v5/execute.d.ts +41 -0
  797. package/dist/stream/aisdk/v5/execute.d.ts.map +1 -0
  798. package/dist/stream/aisdk/v5/file.d.ts +38 -0
  799. package/dist/stream/aisdk/v5/file.d.ts.map +1 -0
  800. package/dist/stream/aisdk/v5/input.d.ts +16 -0
  801. package/dist/stream/aisdk/v5/input.d.ts.map +1 -0
  802. package/dist/stream/aisdk/v5/output-helpers.d.ts +66 -0
  803. package/dist/stream/aisdk/v5/output-helpers.d.ts.map +1 -0
  804. package/dist/stream/aisdk/v5/output.d.ts +173 -0
  805. package/dist/stream/aisdk/v5/output.d.ts.map +1 -0
  806. package/dist/stream/aisdk/v5/test-utils.d.ts +19 -0
  807. package/dist/stream/aisdk/v5/test-utils.d.ts.map +1 -0
  808. package/dist/stream/aisdk/v5/transform.d.ts +27 -0
  809. package/dist/stream/aisdk/v5/transform.d.ts.map +1 -0
  810. package/dist/stream/base/base.d.ts +7 -0
  811. package/dist/stream/base/base.d.ts.map +1 -0
  812. package/dist/stream/base/consume-stream.d.ts +5 -0
  813. package/dist/stream/base/consume-stream.d.ts.map +1 -0
  814. package/dist/stream/base/index.d.ts +2 -0
  815. package/dist/stream/base/index.d.ts.map +1 -0
  816. package/dist/stream/base/input.d.ts +16 -0
  817. package/dist/stream/base/input.d.ts.map +1 -0
  818. package/dist/stream/base/output-format-handlers.d.ts +29 -0
  819. package/dist/stream/base/output-format-handlers.d.ts.map +1 -0
  820. package/dist/stream/base/output.d.ts +313 -0
  821. package/dist/stream/base/output.d.ts.map +1 -0
  822. package/dist/stream/base/schema.d.ts +31 -0
  823. package/dist/stream/base/schema.d.ts.map +1 -0
  824. package/dist/stream/index.cjs +45 -0
  825. package/dist/stream/index.cjs.map +1 -0
  826. package/dist/stream/index.d.ts +11 -0
  827. package/dist/stream/index.d.ts.map +1 -0
  828. package/dist/stream/index.js +4 -0
  829. package/dist/stream/index.js.map +1 -0
  830. package/dist/stream/types.d.ts +734 -0
  831. package/dist/stream/types.d.ts.map +1 -0
  832. package/dist/telemetry/composite-exporter.d.ts +10 -0
  833. package/dist/telemetry/composite-exporter.d.ts.map +1 -0
  834. package/dist/telemetry/index.cjs +32 -0
  835. package/dist/telemetry/index.cjs.map +1 -0
  836. package/dist/telemetry/index.d.ts +6 -0
  837. package/dist/telemetry/index.d.ts.map +1 -0
  838. package/dist/telemetry/index.js +3 -0
  839. package/dist/telemetry/index.js.map +1 -0
  840. package/dist/telemetry/otel-vendor.cjs +105 -0
  841. package/dist/telemetry/otel-vendor.cjs.map +1 -0
  842. package/dist/telemetry/otel-vendor.d.ts +11 -0
  843. package/dist/telemetry/otel-vendor.d.ts.map +1 -0
  844. package/dist/telemetry/otel-vendor.js +59 -0
  845. package/dist/telemetry/otel-vendor.js.map +1 -0
  846. package/dist/telemetry/storage-exporter.d.ts +21 -0
  847. package/dist/telemetry/storage-exporter.d.ts.map +1 -0
  848. package/dist/telemetry/telemetry.d.ts +59 -0
  849. package/dist/telemetry/telemetry.d.ts.map +1 -0
  850. package/dist/telemetry/telemetry.decorators.d.ts +15 -0
  851. package/dist/telemetry/telemetry.decorators.d.ts.map +1 -0
  852. package/dist/telemetry/types.d.ts +74 -0
  853. package/dist/telemetry/types.d.ts.map +1 -0
  854. package/dist/telemetry/utility.d.ts +15 -0
  855. package/dist/telemetry/utility.d.ts.map +1 -0
  856. package/dist/test-utils/llm-mock.cjs +181 -0
  857. package/dist/test-utils/llm-mock.cjs.map +1 -0
  858. package/dist/test-utils/llm-mock.d.ts +21 -0
  859. package/dist/test-utils/llm-mock.d.ts.map +1 -0
  860. package/dist/test-utils/llm-mock.js +178 -0
  861. package/dist/test-utils/llm-mock.js.map +1 -0
  862. package/dist/tools/index.cjs +25 -0
  863. package/dist/tools/index.cjs.map +1 -0
  864. package/dist/tools/index.d.ts +6 -0
  865. package/dist/tools/index.d.ts.map +1 -0
  866. package/dist/tools/index.js +4 -0
  867. package/dist/tools/index.js.map +1 -0
  868. package/dist/tools/index.warning.d.ts +8 -0
  869. package/dist/tools/index.warning.d.ts.map +1 -0
  870. package/dist/tools/is-vercel-tool.cjs +12 -0
  871. package/dist/tools/is-vercel-tool.cjs.map +1 -0
  872. package/dist/tools/is-vercel-tool.d.ts +2 -0
  873. package/dist/tools/is-vercel-tool.d.ts.map +1 -0
  874. package/dist/tools/is-vercel-tool.js +3 -0
  875. package/dist/tools/is-vercel-tool.js.map +1 -0
  876. package/dist/tools/stream.d.ts +19 -0
  877. package/dist/tools/stream.d.ts.map +1 -0
  878. package/dist/tools/tool-builder/builder.d.ts +28 -0
  879. package/dist/tools/tool-builder/builder.d.ts.map +1 -0
  880. package/dist/tools/tool.d.ts +186 -0
  881. package/dist/tools/tool.d.ts.map +1 -0
  882. package/dist/tools/toolchecks.d.ts +9 -0
  883. package/dist/tools/toolchecks.d.ts.map +1 -0
  884. package/dist/tools/types.d.ts +70 -0
  885. package/dist/tools/types.d.ts.map +1 -0
  886. package/dist/tools/ui-types.d.ts +39 -0
  887. package/dist/tools/ui-types.d.ts.map +1 -0
  888. package/dist/tools/validation.d.ts +19 -0
  889. package/dist/tools/validation.d.ts.map +1 -0
  890. package/dist/tts/index.cjs +12 -0
  891. package/dist/tts/index.cjs.map +1 -0
  892. package/dist/tts/index.d.ts +22 -0
  893. package/dist/tts/index.d.ts.map +1 -0
  894. package/dist/tts/index.js +3 -0
  895. package/dist/tts/index.js.map +1 -0
  896. package/dist/tts/index.warning.d.ts +7 -0
  897. package/dist/tts/index.warning.d.ts.map +1 -0
  898. package/dist/types/dynamic-argument.d.ts +9 -0
  899. package/dist/types/dynamic-argument.d.ts.map +1 -0
  900. package/dist/types/index.cjs +4 -0
  901. package/dist/types/index.cjs.map +1 -0
  902. package/dist/types/index.d.ts +2 -0
  903. package/dist/types/index.d.ts.map +1 -0
  904. package/dist/types/index.js +3 -0
  905. package/dist/types/index.js.map +1 -0
  906. package/dist/types/zod-compat.d.ts +23 -0
  907. package/dist/types/zod-compat.d.ts.map +1 -0
  908. package/dist/utils.cjs +72 -0
  909. package/dist/utils.cjs.map +1 -0
  910. package/dist/utils.d.ts +148 -0
  911. package/dist/utils.d.ts.map +1 -0
  912. package/dist/utils.js +3 -0
  913. package/dist/utils.js.map +1 -0
  914. package/dist/vector/embed.d.ts +3 -0
  915. package/dist/vector/embed.d.ts.map +1 -0
  916. package/dist/vector/filter/base.d.ts +128 -0
  917. package/dist/vector/filter/base.d.ts.map +1 -0
  918. package/dist/vector/filter/index.cjs +12 -0
  919. package/dist/vector/filter/index.cjs.map +1 -0
  920. package/dist/vector/filter/index.d.ts +2 -0
  921. package/dist/vector/filter/index.d.ts.map +1 -0
  922. package/dist/vector/filter/index.js +3 -0
  923. package/dist/vector/filter/index.js.map +1 -0
  924. package/dist/vector/index.cjs +25 -0
  925. package/dist/vector/index.cjs.map +1 -0
  926. package/dist/vector/index.d.ts +5 -0
  927. package/dist/vector/index.d.ts.map +1 -0
  928. package/dist/vector/index.js +4 -0
  929. package/dist/vector/index.js.map +1 -0
  930. package/dist/vector/index.warning.d.ts +6 -0
  931. package/dist/vector/index.warning.d.ts.map +1 -0
  932. package/dist/vector/types.d.ts +152 -0
  933. package/dist/vector/types.d.ts.map +1 -0
  934. package/dist/vector/vector.d.ts +57 -0
  935. package/dist/vector/vector.d.ts.map +1 -0
  936. package/dist/voice/aisdk/index.d.ts +3 -0
  937. package/dist/voice/aisdk/index.d.ts.map +1 -0
  938. package/dist/voice/aisdk/speech.d.ts +23 -0
  939. package/dist/voice/aisdk/speech.d.ts.map +1 -0
  940. package/dist/voice/aisdk/transcription.d.ts +22 -0
  941. package/dist/voice/aisdk/transcription.d.ts.map +1 -0
  942. package/dist/voice/composite-voice.d.ts +78 -0
  943. package/dist/voice/composite-voice.d.ts.map +1 -0
  944. package/dist/voice/default-voice.d.ts +13 -0
  945. package/dist/voice/default-voice.d.ts.map +1 -0
  946. package/dist/voice/index.cjs +28 -0
  947. package/dist/voice/index.cjs.map +1 -0
  948. package/dist/voice/index.d.ts +5 -0
  949. package/dist/voice/index.d.ts.map +1 -0
  950. package/dist/voice/index.js +3 -0
  951. package/dist/voice/index.js.map +1 -0
  952. package/dist/voice/voice.d.ts +132 -0
  953. package/dist/voice/voice.d.ts.map +1 -0
  954. package/dist/workflows/constants.cjs +16 -0
  955. package/dist/workflows/constants.cjs.map +1 -0
  956. package/dist/workflows/constants.d.ts +3 -0
  957. package/dist/workflows/constants.d.ts.map +1 -0
  958. package/dist/workflows/constants.js +3 -0
  959. package/dist/workflows/constants.js.map +1 -0
  960. package/dist/workflows/default.d.ts +345 -0
  961. package/dist/workflows/default.d.ts.map +1 -0
  962. package/dist/workflows/evented/execution-engine.d.ts +41 -0
  963. package/dist/workflows/evented/execution-engine.d.ts.map +1 -0
  964. package/dist/workflows/evented/index.cjs +44 -0
  965. package/dist/workflows/evented/index.cjs.map +1 -0
  966. package/dist/workflows/evented/index.d.ts +5 -0
  967. package/dist/workflows/evented/index.d.ts.map +1 -0
  968. package/dist/workflows/evented/index.js +3 -0
  969. package/dist/workflows/evented/index.js.map +1 -0
  970. package/dist/workflows/evented/step-executor.d.ts +90 -0
  971. package/dist/workflows/evented/step-executor.d.ts.map +1 -0
  972. package/dist/workflows/evented/workflow-event-processor/index.d.ts +54 -0
  973. package/dist/workflows/evented/workflow-event-processor/index.d.ts.map +1 -0
  974. package/dist/workflows/evented/workflow-event-processor/loop.d.ts +21 -0
  975. package/dist/workflows/evented/workflow-event-processor/loop.d.ts.map +1 -0
  976. package/dist/workflows/evented/workflow-event-processor/parallel.d.ts +18 -0
  977. package/dist/workflows/evented/workflow-event-processor/parallel.d.ts.map +1 -0
  978. package/dist/workflows/evented/workflow-event-processor/sleep.d.ts +24 -0
  979. package/dist/workflows/evented/workflow-event-processor/sleep.d.ts.map +1 -0
  980. package/dist/workflows/evented/workflow-event-processor/utils.d.ts +30 -0
  981. package/dist/workflows/evented/workflow-event-processor/utils.d.ts.map +1 -0
  982. package/dist/workflows/evented/workflow.d.ts +81 -0
  983. package/dist/workflows/evented/workflow.d.ts.map +1 -0
  984. package/dist/workflows/execution-engine.d.ts +75 -0
  985. package/dist/workflows/execution-engine.d.ts.map +1 -0
  986. package/dist/workflows/index.cjs +60 -0
  987. package/dist/workflows/index.cjs.map +1 -0
  988. package/dist/workflows/index.d.ts +7 -0
  989. package/dist/workflows/index.d.ts.map +1 -0
  990. package/dist/workflows/index.js +3 -0
  991. package/dist/workflows/index.js.map +1 -0
  992. package/dist/workflows/index.warning.d.ts +6 -0
  993. package/dist/workflows/index.warning.d.ts.map +1 -0
  994. package/dist/workflows/legacy/index.cjs +92 -0
  995. package/dist/workflows/legacy/index.cjs.map +1 -0
  996. package/dist/workflows/legacy/index.d.ts +5 -0
  997. package/dist/workflows/legacy/index.d.ts.map +1 -0
  998. package/dist/workflows/legacy/index.js +3 -0
  999. package/dist/workflows/legacy/index.js.map +1 -0
  1000. package/dist/workflows/legacy/machine.d.ts +386 -0
  1001. package/dist/workflows/legacy/machine.d.ts.map +1 -0
  1002. package/dist/workflows/legacy/step.d.ts +15 -0
  1003. package/dist/workflows/legacy/step.d.ts.map +1 -0
  1004. package/dist/workflows/legacy/types.d.ts +397 -0
  1005. package/dist/workflows/legacy/types.d.ts.map +1 -0
  1006. package/dist/workflows/legacy/utils.d.ts +68 -0
  1007. package/dist/workflows/legacy/utils.d.ts.map +1 -0
  1008. package/dist/workflows/legacy/workflow-instance.d.ts +97 -0
  1009. package/dist/workflows/legacy/workflow-instance.d.ts.map +1 -0
  1010. package/dist/workflows/legacy/workflow.d.ts +109 -0
  1011. package/dist/workflows/legacy/workflow.d.ts.map +1 -0
  1012. package/dist/workflows/step.d.ts +64 -0
  1013. package/dist/workflows/step.d.ts.map +1 -0
  1014. package/dist/workflows/types.d.ts +351 -0
  1015. package/dist/workflows/types.d.ts.map +1 -0
  1016. package/dist/workflows/utils.d.ts +19 -0
  1017. package/dist/workflows/utils.d.ts.map +1 -0
  1018. package/dist/workflows/workflow.d.ts +493 -0
  1019. package/dist/workflows/workflow.d.ts.map +1 -0
  1020. package/dist/workflows/workflow.warning.d.ts +9 -0
  1021. package/dist/workflows/workflow.warning.d.ts.map +1 -0
  1022. package/dist/zod-to-json.cjs +12 -0
  1023. package/dist/zod-to-json.cjs.map +1 -0
  1024. package/dist/zod-to-json.d.ts +2 -0
  1025. package/dist/zod-to-json.d.ts.map +1 -0
  1026. package/dist/zod-to-json.js +3 -0
  1027. package/dist/zod-to-json.js.map +1 -0
  1028. package/error.d.ts +1 -0
  1029. package/eval.d.ts +1 -0
  1030. package/events.d.ts +1 -0
  1031. package/hooks.d.ts +1 -0
  1032. package/integration.d.ts +1 -0
  1033. package/llm.d.ts +1 -0
  1034. package/logger.d.ts +1 -0
  1035. package/loop.d.ts +1 -0
  1036. package/mastra.d.ts +1 -0
  1037. package/mcp.d.ts +1 -0
  1038. package/memory.d.ts +1 -0
  1039. package/package.json +305 -0
  1040. package/processors.d.ts +1 -0
  1041. package/relevance.d.ts +1 -0
  1042. package/runtime-context.d.ts +1 -0
  1043. package/scores/scoreTraces.d.ts +1 -0
  1044. package/scores.d.ts +1 -0
  1045. package/server.d.ts +1 -0
  1046. package/src/llm/model/provider-types.generated.d.ts +1205 -0
  1047. package/storage.d.ts +1 -0
  1048. package/stream.d.ts +1 -0
  1049. package/telemetry/otel-vendor.d.ts +1 -0
  1050. package/telemetry.d.ts +1 -0
  1051. package/test-utils/llm-mock.d.ts +1 -0
  1052. package/tools/is-vercel-tool.d.ts +1 -0
  1053. package/tools.d.ts +1 -0
  1054. package/tts.d.ts +1 -0
  1055. package/types.d.ts +1 -0
  1056. package/utils/zod-to-json.d.ts +1 -0
  1057. package/utils.d.ts +1 -0
  1058. package/vector/filter.d.ts +1 -0
  1059. package/vector.d.ts +1 -0
  1060. package/voice.d.ts +1 -0
  1061. package/workflows/_constants.d.ts +1 -0
  1062. package/workflows/evented.d.ts +1 -0
  1063. package/workflows/legacy.d.ts +1 -0
  1064. package/workflows.d.ts +1 -0
@@ -0,0 +1,3177 @@
1
+ import { fetchWithRetry } from './chunk-WIMFJ2BA.js';
2
+ import { MastraError } from './chunk-PZUZNPFM.js';
3
+ import { convertUint8ArrayToBase64, convertBase64ToUint8Array, isUrlSupported } from '@ai-sdk/provider-utils-v5';
4
+ import { randomUUID } from 'crypto';
5
+ import * as AIV4 from 'ai';
6
+ import * as AIV5 from 'ai-v5';
7
+ import { convertUint8ArrayToBase64 as convertUint8ArrayToBase64$1 } from '@ai-sdk/provider-utils';
8
+ import { z } from 'zod';
9
+
10
+ var DefaultGeneratedFile = class {
11
+ base64Data;
12
+ uint8ArrayData;
13
+ mediaType;
14
+ constructor({ data, mediaType }) {
15
+ const isUint8Array = data instanceof Uint8Array;
16
+ this.base64Data = isUint8Array ? void 0 : data;
17
+ this.uint8ArrayData = isUint8Array ? data : void 0;
18
+ this.mediaType = mediaType;
19
+ }
20
+ // lazy conversion with caching to avoid unnecessary conversion overhead:
21
+ get base64() {
22
+ if (this.base64Data == null) {
23
+ this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
24
+ }
25
+ return this.base64Data;
26
+ }
27
+ // lazy conversion with caching to avoid unnecessary conversion overhead:
28
+ get uint8Array() {
29
+ if (this.uint8ArrayData == null) {
30
+ this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
31
+ }
32
+ return this.uint8ArrayData;
33
+ }
34
+ };
35
+ var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
36
+ type = "file";
37
+ constructor(options) {
38
+ super(options);
39
+ }
40
+ };
41
+
42
+ // src/stream/aisdk/v5/compat/content.ts
43
+ function splitDataUrl(dataUrl) {
44
+ try {
45
+ const [header, base64Content] = dataUrl.split(",");
46
+ return {
47
+ mediaType: header?.split(";")[0]?.split(":")[1],
48
+ base64Content
49
+ };
50
+ } catch {
51
+ return {
52
+ mediaType: void 0,
53
+ base64Content: void 0
54
+ };
55
+ }
56
+ }
57
+ function convertToDataContent(content) {
58
+ if (content instanceof Uint8Array) {
59
+ return { data: content, mediaType: void 0 };
60
+ }
61
+ if (content instanceof ArrayBuffer) {
62
+ return { data: new Uint8Array(content), mediaType: void 0 };
63
+ }
64
+ if (typeof content === "string") {
65
+ try {
66
+ content = new URL(content);
67
+ } catch {
68
+ }
69
+ }
70
+ if (content instanceof URL && content.protocol === "data:") {
71
+ const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(content.toString());
72
+ if (dataUrlMediaType == null || base64Content == null) {
73
+ throw new MastraError({
74
+ id: "INVALID_DATA_URL_FORMAT",
75
+ text: `Invalid data URL format in content ${content.toString()}`,
76
+ domain: "LLM" /* LLM */,
77
+ category: "USER" /* USER */
78
+ });
79
+ }
80
+ return { data: base64Content, mediaType: dataUrlMediaType };
81
+ }
82
+ return { data: content, mediaType: void 0 };
83
+ }
84
+ var imageMediaTypeSignatures = [
85
+ {
86
+ mediaType: "image/gif",
87
+ bytesPrefix: [71, 73, 70],
88
+ base64Prefix: "R0lG"
89
+ },
90
+ {
91
+ mediaType: "image/png",
92
+ bytesPrefix: [137, 80, 78, 71],
93
+ base64Prefix: "iVBORw"
94
+ },
95
+ {
96
+ mediaType: "image/jpeg",
97
+ bytesPrefix: [255, 216],
98
+ base64Prefix: "/9j/"
99
+ },
100
+ {
101
+ mediaType: "image/webp",
102
+ bytesPrefix: [82, 73, 70, 70],
103
+ base64Prefix: "UklGRg"
104
+ },
105
+ {
106
+ mediaType: "image/bmp",
107
+ bytesPrefix: [66, 77],
108
+ base64Prefix: "Qk"
109
+ },
110
+ {
111
+ mediaType: "image/tiff",
112
+ bytesPrefix: [73, 73, 42, 0],
113
+ base64Prefix: "SUkqAA"
114
+ },
115
+ {
116
+ mediaType: "image/tiff",
117
+ bytesPrefix: [77, 77, 0, 42],
118
+ base64Prefix: "TU0AKg"
119
+ },
120
+ {
121
+ mediaType: "image/avif",
122
+ bytesPrefix: [0, 0, 0, 32, 102, 116, 121, 112, 97, 118, 105, 102],
123
+ base64Prefix: "AAAAIGZ0eXBhdmlm"
124
+ },
125
+ {
126
+ mediaType: "image/heic",
127
+ bytesPrefix: [0, 0, 0, 32, 102, 116, 121, 112, 104, 101, 105, 99],
128
+ base64Prefix: "AAAAIGZ0eXBoZWlj"
129
+ }
130
+ ];
131
+ var stripID3 = (data) => {
132
+ const bytes = typeof data === "string" ? convertBase64ToUint8Array(data) : data;
133
+ const id3Size = (
134
+ // @ts-ignore
135
+ (bytes[6] & 127) << 21 | // @ts-ignore
136
+ (bytes[7] & 127) << 14 | // @ts-ignore
137
+ (bytes[8] & 127) << 7 | // @ts-ignore
138
+ bytes[9] & 127
139
+ );
140
+ return bytes.slice(id3Size + 10);
141
+ };
142
+ function stripID3TagsIfPresent(data) {
143
+ const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
144
+ data[1] === 68 && // 'D'
145
+ data[2] === 51;
146
+ return hasId3 ? stripID3(data) : data;
147
+ }
148
+ function detectMediaType({
149
+ data,
150
+ signatures
151
+ }) {
152
+ const processedData = stripID3TagsIfPresent(data);
153
+ for (const signature of signatures) {
154
+ if (typeof processedData === "string" ? processedData.startsWith(signature.base64Prefix) : processedData.length >= signature.bytesPrefix.length && signature.bytesPrefix.every((byte, index) => processedData[index] === byte)) {
155
+ return signature.mediaType;
156
+ }
157
+ }
158
+ return void 0;
159
+ }
160
+
161
+ // src/agent/message-list/prompt/convert-file.ts
162
+ function convertImageFilePart(part, downloadedAssets) {
163
+ let originalData;
164
+ const type = part.type;
165
+ switch (type) {
166
+ case "image":
167
+ originalData = part.image;
168
+ break;
169
+ case "file":
170
+ originalData = part.data;
171
+ break;
172
+ default:
173
+ throw new Error(`Unsupported part type: ${type}`);
174
+ }
175
+ const { data: convertedData, mediaType: convertedMediaType } = convertToDataContent(originalData);
176
+ let mediaType = convertedMediaType ?? part.mediaType;
177
+ let data = convertedData;
178
+ if (data instanceof URL && downloadedAssets) {
179
+ const downloadedFile = downloadedAssets[data.toString()];
180
+ if (downloadedFile) {
181
+ data = downloadedFile.data;
182
+ mediaType ??= downloadedFile.mediaType;
183
+ }
184
+ }
185
+ switch (type) {
186
+ case "image": {
187
+ if (data instanceof Uint8Array || typeof data === "string") {
188
+ mediaType = detectMediaType({ data, signatures: imageMediaTypeSignatures }) ?? mediaType;
189
+ }
190
+ return {
191
+ type: "file",
192
+ mediaType: mediaType ?? "image/*",
193
+ // any image
194
+ filename: void 0,
195
+ data,
196
+ providerOptions: part.providerOptions
197
+ };
198
+ }
199
+ case "file": {
200
+ if (mediaType == null) {
201
+ throw new Error(`Media type is missing for file part`);
202
+ }
203
+ return {
204
+ type: "file",
205
+ mediaType,
206
+ filename: part.filename,
207
+ data,
208
+ providerOptions: part.providerOptions
209
+ };
210
+ }
211
+ }
212
+ }
213
+ z.union([
214
+ z.string(),
215
+ z.instanceof(Uint8Array),
216
+ z.instanceof(ArrayBuffer),
217
+ z.custom(
218
+ // Buffer might not be available in some environments such as CloudFlare:
219
+ (value) => globalThis.Buffer?.isBuffer(value) ?? false,
220
+ { message: "Must be a Buffer" }
221
+ )
222
+ ]);
223
+ function convertDataContentToBase64String(content) {
224
+ if (typeof content === "string") {
225
+ return content;
226
+ }
227
+ if (content instanceof ArrayBuffer) {
228
+ return convertUint8ArrayToBase64$1(new Uint8Array(content));
229
+ }
230
+ return convertUint8ArrayToBase64$1(content);
231
+ }
232
+
233
+ // src/agent/message-list/prompt/image-utils.ts
234
+ function parseDataUri(dataUri) {
235
+ if (!dataUri.startsWith("data:")) {
236
+ return {
237
+ isDataUri: false,
238
+ base64Content: dataUri
239
+ };
240
+ }
241
+ const base64Index = dataUri.indexOf(",");
242
+ if (base64Index === -1) {
243
+ return {
244
+ isDataUri: true,
245
+ base64Content: dataUri
246
+ };
247
+ }
248
+ const header = dataUri.substring(5, base64Index);
249
+ const base64Content = dataUri.substring(base64Index + 1);
250
+ const semicolonIndex = header.indexOf(";");
251
+ const mimeType = semicolonIndex !== -1 ? header.substring(0, semicolonIndex) : header;
252
+ return {
253
+ isDataUri: true,
254
+ mimeType: mimeType || void 0,
255
+ base64Content
256
+ };
257
+ }
258
+ function createDataUri(base64Content, mimeType = "application/octet-stream") {
259
+ if (base64Content.startsWith("data:")) {
260
+ return base64Content;
261
+ }
262
+ return `data:${mimeType};base64,${base64Content}`;
263
+ }
264
+ function imageContentToString(image, fallbackMimeType) {
265
+ if (typeof image === "string") {
266
+ return image;
267
+ }
268
+ if (image instanceof URL) {
269
+ return image.toString();
270
+ }
271
+ if (image instanceof Uint8Array || image instanceof ArrayBuffer || globalThis.Buffer && Buffer.isBuffer(image)) {
272
+ const base64 = convertDataContentToBase64String(image);
273
+ if (fallbackMimeType && !base64.startsWith("data:")) {
274
+ return `data:${fallbackMimeType};base64,${base64}`;
275
+ }
276
+ return base64;
277
+ }
278
+ return String(image);
279
+ }
280
+ function imageContentToDataUri(image, mimeType = "image/png") {
281
+ const imageStr = imageContentToString(image, mimeType);
282
+ if (imageStr.startsWith("data:")) {
283
+ return imageStr;
284
+ }
285
+ if (imageStr.startsWith("http://") || imageStr.startsWith("https://")) {
286
+ return imageStr;
287
+ }
288
+ return `data:${mimeType};base64,${imageStr}`;
289
+ }
290
+ function getImageCacheKey(image) {
291
+ if (image instanceof URL) {
292
+ return image.toString();
293
+ }
294
+ if (typeof image === "string") {
295
+ return image.length;
296
+ }
297
+ if (image instanceof Uint8Array) {
298
+ return image.byteLength;
299
+ }
300
+ if (image instanceof ArrayBuffer) {
301
+ return image.byteLength;
302
+ }
303
+ return image;
304
+ }
305
+ function isValidUrl(str) {
306
+ try {
307
+ new URL(str);
308
+ return true;
309
+ } catch {
310
+ if (str.startsWith("//")) {
311
+ try {
312
+ new URL(`https:${str}`);
313
+ return true;
314
+ } catch {
315
+ return false;
316
+ }
317
+ }
318
+ return false;
319
+ }
320
+ }
321
+ function categorizeFileData(data, fallbackMimeType) {
322
+ const parsed = parseDataUri(data);
323
+ const mimeType = parsed.isDataUri && parsed.mimeType ? parsed.mimeType : fallbackMimeType;
324
+ if (parsed.isDataUri) {
325
+ return {
326
+ type: "dataUri",
327
+ mimeType,
328
+ data
329
+ };
330
+ }
331
+ if (isValidUrl(data)) {
332
+ return {
333
+ type: "url",
334
+ mimeType,
335
+ data
336
+ };
337
+ }
338
+ return {
339
+ type: "raw",
340
+ mimeType,
341
+ data
342
+ };
343
+ }
344
+
345
+ // src/agent/message-list/prompt/attachments-to-parts.ts
346
+ function attachmentsToParts(attachments) {
347
+ const parts = [];
348
+ for (const attachment of attachments) {
349
+ const categorized = categorizeFileData(attachment.url, attachment.contentType);
350
+ let urlString = attachment.url;
351
+ if (categorized.type === "raw") {
352
+ urlString = createDataUri(attachment.url, attachment.contentType || "application/octet-stream");
353
+ }
354
+ let url;
355
+ try {
356
+ url = new URL(urlString);
357
+ } catch {
358
+ throw new Error(`Invalid URL: ${attachment.url}`);
359
+ }
360
+ switch (url.protocol) {
361
+ case "http:":
362
+ case "https:": {
363
+ if (attachment.contentType?.startsWith("image/")) {
364
+ parts.push({ type: "image", image: url.toString(), mimeType: attachment.contentType });
365
+ } else {
366
+ if (!attachment.contentType) {
367
+ throw new Error("If the attachment is not an image, it must specify a content type");
368
+ }
369
+ parts.push({
370
+ type: "file",
371
+ data: url.toString(),
372
+ mimeType: attachment.contentType
373
+ });
374
+ }
375
+ break;
376
+ }
377
+ case "data:": {
378
+ if (attachment.contentType?.startsWith("image/")) {
379
+ parts.push({
380
+ type: "image",
381
+ image: urlString,
382
+ mimeType: attachment.contentType
383
+ });
384
+ } else if (attachment.contentType?.startsWith("text/")) {
385
+ parts.push({
386
+ type: "file",
387
+ data: urlString,
388
+ mimeType: attachment.contentType
389
+ });
390
+ } else {
391
+ if (!attachment.contentType) {
392
+ throw new Error("If the attachment is not an image or text, it must specify a content type");
393
+ }
394
+ parts.push({
395
+ type: "file",
396
+ data: urlString,
397
+ mimeType: attachment.contentType
398
+ });
399
+ }
400
+ break;
401
+ }
402
+ default: {
403
+ throw new Error(`Unsupported URL protocol: ${url.protocol}`);
404
+ }
405
+ }
406
+ }
407
+ return parts;
408
+ }
409
+
410
+ // src/agent/message-list/prompt/convert-to-mastra-v1.ts
411
+ var makePushOrCombine = (v1Messages) => {
412
+ const idUsageCount = /* @__PURE__ */ new Map();
413
+ const SPLIT_SUFFIX_PATTERN = /__split-\d+$/;
414
+ return (msg) => {
415
+ const previousMessage = v1Messages.at(-1);
416
+ if (msg.role === previousMessage?.role && Array.isArray(previousMessage.content) && Array.isArray(msg.content) && // we were creating new messages for tool calls before and not appending to the assistant message
417
+ // so don't append here so everything works as before
418
+ (msg.role !== `assistant` || msg.role === `assistant` && msg.content.at(-1)?.type !== `tool-call`)) {
419
+ for (const part of msg.content) {
420
+ previousMessage.content.push(part);
421
+ }
422
+ } else {
423
+ let baseId = msg.id;
424
+ const hasSplitSuffix = SPLIT_SUFFIX_PATTERN.test(baseId);
425
+ if (hasSplitSuffix) {
426
+ v1Messages.push(msg);
427
+ return;
428
+ }
429
+ const currentCount = idUsageCount.get(baseId) || 0;
430
+ if (currentCount > 0) {
431
+ msg.id = `${baseId}__split-${currentCount}`;
432
+ }
433
+ idUsageCount.set(baseId, currentCount + 1);
434
+ v1Messages.push(msg);
435
+ }
436
+ };
437
+ };
438
+ function convertToV1Messages(messages) {
439
+ const v1Messages = [];
440
+ const pushOrCombine = makePushOrCombine(v1Messages);
441
+ for (let i = 0; i < messages.length; i++) {
442
+ const message = messages[i];
443
+ const isLastMessage = i === messages.length - 1;
444
+ if (!message?.content) continue;
445
+ const { content, experimental_attachments: inputAttachments = [], parts: inputParts } = message.content;
446
+ const { role } = message;
447
+ const fields = {
448
+ id: message.id,
449
+ createdAt: message.createdAt,
450
+ resourceId: message.resourceId,
451
+ threadId: message.threadId
452
+ };
453
+ const experimental_attachments = [...inputAttachments];
454
+ const parts = [];
455
+ for (const part of inputParts) {
456
+ if (part.type === "file") {
457
+ experimental_attachments.push({
458
+ url: part.data,
459
+ contentType: part.mimeType
460
+ });
461
+ } else {
462
+ parts.push(part);
463
+ }
464
+ }
465
+ switch (role) {
466
+ case "user": {
467
+ if (parts == null) {
468
+ const userContent = experimental_attachments ? [{ type: "text", text: content || "" }, ...attachmentsToParts(experimental_attachments)] : { type: "text", text: content || "" };
469
+ pushOrCombine({
470
+ role: "user",
471
+ ...fields,
472
+ type: "text",
473
+ // @ts-ignore
474
+ content: userContent
475
+ });
476
+ } else {
477
+ const textParts = message.content.parts.filter((part) => part.type === "text").map((part) => ({
478
+ type: "text",
479
+ text: part.text
480
+ }));
481
+ const userContent = experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts;
482
+ pushOrCombine({
483
+ role: "user",
484
+ ...fields,
485
+ type: "text",
486
+ content: Array.isArray(userContent) && userContent.length === 1 && userContent[0]?.type === `text` && typeof content !== `undefined` ? content : userContent
487
+ });
488
+ }
489
+ break;
490
+ }
491
+ case "assistant": {
492
+ if (message.content.parts != null) {
493
+ let processBlock2 = function() {
494
+ const content2 = [];
495
+ for (const part of block) {
496
+ switch (part.type) {
497
+ case "file":
498
+ case "text": {
499
+ content2.push(part);
500
+ break;
501
+ }
502
+ case "reasoning": {
503
+ for (const detail of part.details) {
504
+ switch (detail.type) {
505
+ case "text":
506
+ content2.push({
507
+ type: "reasoning",
508
+ text: detail.text,
509
+ signature: detail.signature
510
+ });
511
+ break;
512
+ case "redacted":
513
+ content2.push({
514
+ type: "redacted-reasoning",
515
+ data: detail.data
516
+ });
517
+ break;
518
+ }
519
+ }
520
+ break;
521
+ }
522
+ case "tool-invocation":
523
+ if (part.toolInvocation.toolName !== "updateWorkingMemory") {
524
+ content2.push({
525
+ type: "tool-call",
526
+ toolCallId: part.toolInvocation.toolCallId,
527
+ toolName: part.toolInvocation.toolName,
528
+ args: part.toolInvocation.args
529
+ });
530
+ }
531
+ break;
532
+ }
533
+ }
534
+ pushOrCombine({
535
+ role: "assistant",
536
+ ...fields,
537
+ type: content2.some((c) => c.type === `tool-call`) ? "tool-call" : "text",
538
+ content: typeof content2 !== `string` && Array.isArray(content2) && content2.length === 1 && content2[0]?.type === `text` ? content2[0].text : content2
539
+ });
540
+ const stepInvocations = block.filter((part) => `type` in part && part.type === "tool-invocation").map((part) => part.toolInvocation).filter((ti) => ti.toolName !== "updateWorkingMemory");
541
+ const invocationsWithResults = stepInvocations.filter((ti) => ti.state === "result" && "result" in ti);
542
+ if (invocationsWithResults.length > 0) {
543
+ pushOrCombine({
544
+ role: "tool",
545
+ ...fields,
546
+ type: "tool-result",
547
+ content: invocationsWithResults.map((toolInvocation) => {
548
+ const { toolCallId, toolName, result } = toolInvocation;
549
+ return {
550
+ type: "tool-result",
551
+ toolCallId,
552
+ toolName,
553
+ result
554
+ };
555
+ })
556
+ });
557
+ }
558
+ block = [];
559
+ blockHasToolInvocations = false;
560
+ currentStep++;
561
+ };
562
+ let currentStep = 0;
563
+ let blockHasToolInvocations = false;
564
+ let block = [];
565
+ for (const part of message.content.parts) {
566
+ switch (part.type) {
567
+ case "text": {
568
+ if (blockHasToolInvocations) {
569
+ processBlock2();
570
+ }
571
+ block.push(part);
572
+ break;
573
+ }
574
+ case "file":
575
+ case "reasoning": {
576
+ block.push(part);
577
+ break;
578
+ }
579
+ case "tool-invocation": {
580
+ const hasNonToolContent = block.some(
581
+ (p) => p.type === "text" || p.type === "file" || p.type === "reasoning"
582
+ );
583
+ if (hasNonToolContent || (part.toolInvocation.step ?? 0) !== currentStep) {
584
+ processBlock2();
585
+ }
586
+ block.push(part);
587
+ blockHasToolInvocations = true;
588
+ break;
589
+ }
590
+ }
591
+ }
592
+ processBlock2();
593
+ const toolInvocations2 = message.content.toolInvocations;
594
+ if (toolInvocations2 && toolInvocations2.length > 0) {
595
+ const processedToolCallIds = /* @__PURE__ */ new Set();
596
+ for (const part of message.content.parts) {
597
+ if (part.type === "tool-invocation" && part.toolInvocation.toolCallId) {
598
+ processedToolCallIds.add(part.toolInvocation.toolCallId);
599
+ }
600
+ }
601
+ const unprocessedToolInvocations = toolInvocations2.filter(
602
+ (ti) => !processedToolCallIds.has(ti.toolCallId) && ti.toolName !== "updateWorkingMemory"
603
+ );
604
+ if (unprocessedToolInvocations.length > 0) {
605
+ const invocationsByStep = /* @__PURE__ */ new Map();
606
+ for (const inv of unprocessedToolInvocations) {
607
+ const step = inv.step ?? 0;
608
+ if (!invocationsByStep.has(step)) {
609
+ invocationsByStep.set(step, []);
610
+ }
611
+ invocationsByStep.get(step).push(inv);
612
+ }
613
+ const sortedSteps = Array.from(invocationsByStep.keys()).sort((a, b) => a - b);
614
+ for (const step of sortedSteps) {
615
+ const stepInvocations = invocationsByStep.get(step);
616
+ pushOrCombine({
617
+ role: "assistant",
618
+ ...fields,
619
+ type: "tool-call",
620
+ content: [
621
+ ...stepInvocations.map(({ toolCallId, toolName, args }) => ({
622
+ type: "tool-call",
623
+ toolCallId,
624
+ toolName,
625
+ args
626
+ }))
627
+ ]
628
+ });
629
+ const invocationsWithResults = stepInvocations.filter((ti) => ti.state === "result" && "result" in ti);
630
+ if (invocationsWithResults.length > 0) {
631
+ pushOrCombine({
632
+ role: "tool",
633
+ ...fields,
634
+ type: "tool-result",
635
+ content: invocationsWithResults.map((toolInvocation) => {
636
+ const { toolCallId, toolName, result } = toolInvocation;
637
+ return {
638
+ type: "tool-result",
639
+ toolCallId,
640
+ toolName,
641
+ result
642
+ };
643
+ })
644
+ });
645
+ }
646
+ }
647
+ }
648
+ }
649
+ break;
650
+ }
651
+ const toolInvocations = message.content.toolInvocations;
652
+ if (toolInvocations == null || toolInvocations.length === 0) {
653
+ pushOrCombine({ role: "assistant", ...fields, content: content || "", type: "text" });
654
+ break;
655
+ }
656
+ const maxStep = toolInvocations.reduce((max, toolInvocation) => {
657
+ return Math.max(max, toolInvocation.step ?? 0);
658
+ }, 0);
659
+ for (let i2 = 0; i2 <= maxStep; i2++) {
660
+ const stepInvocations = toolInvocations.filter(
661
+ (toolInvocation) => (toolInvocation.step ?? 0) === i2 && toolInvocation.toolName !== "updateWorkingMemory"
662
+ );
663
+ if (stepInvocations.length === 0) {
664
+ continue;
665
+ }
666
+ pushOrCombine({
667
+ role: "assistant",
668
+ ...fields,
669
+ type: "tool-call",
670
+ content: [
671
+ ...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
672
+ ...stepInvocations.map(({ toolCallId, toolName, args }) => ({
673
+ type: "tool-call",
674
+ toolCallId,
675
+ toolName,
676
+ args
677
+ }))
678
+ ]
679
+ });
680
+ const invocationsWithResults = stepInvocations.filter((ti) => ti.state === "result" && "result" in ti);
681
+ if (invocationsWithResults.length > 0) {
682
+ pushOrCombine({
683
+ role: "tool",
684
+ ...fields,
685
+ type: "tool-result",
686
+ content: invocationsWithResults.map((toolInvocation) => {
687
+ const { toolCallId, toolName, result } = toolInvocation;
688
+ return {
689
+ type: "tool-result",
690
+ toolCallId,
691
+ toolName,
692
+ result
693
+ };
694
+ })
695
+ });
696
+ }
697
+ }
698
+ if (content && !isLastMessage) {
699
+ pushOrCombine({ role: "assistant", ...fields, type: "text", content: content || "" });
700
+ }
701
+ break;
702
+ }
703
+ }
704
+ }
705
+ return v1Messages;
706
+ }
707
+ var downloadFromUrl = async ({ url, downloadRetries }) => {
708
+ const urlText = url.toString();
709
+ try {
710
+ const response = await fetchWithRetry(
711
+ urlText,
712
+ {
713
+ method: "GET"
714
+ },
715
+ downloadRetries
716
+ );
717
+ if (!response.ok) {
718
+ throw new MastraError({
719
+ id: "DOWNLOAD_ASSETS_FAILED",
720
+ text: "Failed to download asset",
721
+ domain: "LLM" /* LLM */,
722
+ category: "USER" /* USER */
723
+ });
724
+ }
725
+ return {
726
+ data: new Uint8Array(await response.arrayBuffer()),
727
+ mediaType: response.headers.get("content-type") ?? void 0
728
+ };
729
+ } catch (error) {
730
+ throw new MastraError(
731
+ {
732
+ id: "DOWNLOAD_ASSETS_FAILED",
733
+ text: "Failed to download asset",
734
+ domain: "LLM" /* LLM */,
735
+ category: "USER" /* USER */
736
+ },
737
+ error
738
+ );
739
+ }
740
+ };
741
+ async function downloadAssetsFromMessages({
742
+ messages,
743
+ downloadConcurrency = 10,
744
+ downloadRetries = 3,
745
+ supportedUrls
746
+ }) {
747
+ const pMap = (await import('p-map')).default;
748
+ const filesToDownload = messages.filter((message) => message.role === "user").map((message) => message.content).filter((content) => Array.isArray(content)).flat().filter((part) => part.type === "image" || part.type === "file").map((part) => {
749
+ const mediaType = part.mediaType ?? (part.type === "image" ? "image/*" : void 0);
750
+ let data = part.type === "image" ? part.image : part.data;
751
+ if (typeof data === "string") {
752
+ try {
753
+ data = new URL(data);
754
+ } catch {
755
+ }
756
+ }
757
+ return { mediaType, data };
758
+ }).filter((part) => part.data instanceof URL).map((part) => {
759
+ return {
760
+ url: part.data,
761
+ isUrlSupportedByModel: part.mediaType != null && isUrlSupported({
762
+ url: part.data.toString(),
763
+ mediaType: part.mediaType,
764
+ supportedUrls: supportedUrls ?? {}
765
+ })
766
+ };
767
+ });
768
+ const downloadedFiles = await pMap(
769
+ filesToDownload,
770
+ async (fileItem) => {
771
+ if (fileItem.isUrlSupportedByModel) {
772
+ return null;
773
+ }
774
+ return {
775
+ url: fileItem.url.toString(),
776
+ ...await downloadFromUrl({ url: fileItem.url, downloadRetries })
777
+ };
778
+ },
779
+ {
780
+ concurrency: downloadConcurrency
781
+ }
782
+ );
783
+ const downloadFileList = downloadedFiles.filter(
784
+ (downloadedFile) => downloadedFile?.data != null
785
+ ).map(({ url, data, mediaType }) => [url, { data, mediaType }]);
786
+ return Object.fromEntries(downloadFileList);
787
+ }
788
+
789
+ // src/agent/message-list/utils/ai-v5/gemini-compatibility.ts
790
+ function ensureGeminiCompatibleMessages(messages) {
791
+ const result = [...messages];
792
+ const firstNonSystemIndex = result.findIndex((m) => m.role !== "system");
793
+ if (firstNonSystemIndex === -1) {
794
+ throw new MastraError({
795
+ id: "NO_USER_OR_ASSISTANT_MESSAGES",
796
+ domain: "AGENT" /* AGENT */,
797
+ category: "USER" /* USER */,
798
+ text: "This request does not contain any user or assistant messages. At least one user or assistant message is required to generate a response."
799
+ });
800
+ } else if (result[firstNonSystemIndex]?.role === "assistant") {
801
+ result.splice(firstNonSystemIndex, 0, {
802
+ role: "user",
803
+ content: "."
804
+ });
805
+ }
806
+ return result;
807
+ }
808
+
809
+ // src/agent/message-list/utils/ai-v5/tool.ts
810
+ function getToolName(type) {
811
+ if (typeof type === "object" && type && "type" in type) {
812
+ type = type.type;
813
+ }
814
+ if (typeof type !== "string") {
815
+ return "unknown";
816
+ }
817
+ if (type === "dynamic-tool") {
818
+ return "dynamic-tool";
819
+ }
820
+ if (type.startsWith("tool-")) {
821
+ return type.slice("tool-".length);
822
+ }
823
+ return type;
824
+ }
825
+
826
+ // src/agent/message-list/index.ts
827
+ var MessageList = class _MessageList {
828
+ messages = [];
829
+ // passed in by dev in input or context
830
+ systemMessages = [];
831
+ // passed in by us for a specific purpose, eg memory system message
832
+ taggedSystemMessages = {};
833
+ memoryInfo = null;
834
+ // used to filter this.messages by how it was added: input/response/memory
835
+ memoryMessages = /* @__PURE__ */ new Set();
836
+ newUserMessages = /* @__PURE__ */ new Set();
837
+ newResponseMessages = /* @__PURE__ */ new Set();
838
+ userContextMessages = /* @__PURE__ */ new Set();
839
+ memoryMessagesPersisted = /* @__PURE__ */ new Set();
840
+ newUserMessagesPersisted = /* @__PURE__ */ new Set();
841
+ newResponseMessagesPersisted = /* @__PURE__ */ new Set();
842
+ userContextMessagesPersisted = /* @__PURE__ */ new Set();
843
+ generateMessageId;
844
+ _agentNetworkAppend = false;
845
+ constructor({
846
+ threadId,
847
+ resourceId,
848
+ generateMessageId,
849
+ // @ts-ignore Flag for agent network messages
850
+ _agentNetworkAppend
851
+ } = {}) {
852
+ if (threadId) {
853
+ this.memoryInfo = { threadId, resourceId };
854
+ }
855
+ this.generateMessageId = generateMessageId;
856
+ this._agentNetworkAppend = _agentNetworkAppend || false;
857
+ }
858
+ add(messages, messageSource) {
859
+ if (messageSource === `user`) messageSource = `input`;
860
+ if (!messages) return this;
861
+ for (const message of Array.isArray(messages) ? messages : [messages]) {
862
+ this.addOne(
863
+ typeof message === `string` ? {
864
+ role: "user",
865
+ content: message
866
+ } : message,
867
+ messageSource
868
+ );
869
+ }
870
+ return this;
871
+ }
872
+ serializeSet(set) {
873
+ return Array.from(set).map((value) => value.id);
874
+ }
875
+ deserializeSet(ids) {
876
+ return new Set(ids.map((id) => this.messages.find((m) => m.id === id)).filter(Boolean));
877
+ }
878
+ serializeMessage(message) {
879
+ return {
880
+ ...message,
881
+ createdAt: message.createdAt.toUTCString()
882
+ };
883
+ }
884
+ deserializeMessage(state) {
885
+ return {
886
+ ...state,
887
+ createdAt: new Date(state.createdAt)
888
+ };
889
+ }
890
+ serialize() {
891
+ return {
892
+ messages: this.messages.map(this.serializeMessage),
893
+ systemMessages: this.systemMessages,
894
+ taggedSystemMessages: this.taggedSystemMessages,
895
+ memoryInfo: this.memoryInfo,
896
+ _agentNetworkAppend: this._agentNetworkAppend,
897
+ memoryMessages: this.serializeSet(this.memoryMessages),
898
+ newUserMessages: this.serializeSet(this.newUserMessages),
899
+ newResponseMessages: this.serializeSet(this.newResponseMessages),
900
+ userContextMessages: this.serializeSet(this.userContextMessages),
901
+ memoryMessagesPersisted: this.serializeSet(this.memoryMessagesPersisted),
902
+ newUserMessagesPersisted: this.serializeSet(this.newUserMessagesPersisted),
903
+ newResponseMessagesPersisted: this.serializeSet(this.newResponseMessagesPersisted),
904
+ userContextMessagesPersisted: this.serializeSet(this.userContextMessagesPersisted)
905
+ };
906
+ }
907
+ deserialize(state) {
908
+ this.messages = state.messages.map(this.deserializeMessage);
909
+ this.systemMessages = state.systemMessages;
910
+ this.taggedSystemMessages = state.taggedSystemMessages;
911
+ this.memoryInfo = state.memoryInfo;
912
+ this._agentNetworkAppend = state._agentNetworkAppend;
913
+ this.memoryMessages = this.deserializeSet(state.memoryMessages);
914
+ this.newUserMessages = this.deserializeSet(state.newUserMessages);
915
+ this.newResponseMessages = this.deserializeSet(state.newResponseMessages);
916
+ this.userContextMessages = this.deserializeSet(state.userContextMessages);
917
+ this.memoryMessagesPersisted = this.deserializeSet(state.memoryMessagesPersisted);
918
+ this.newUserMessagesPersisted = this.deserializeSet(state.newUserMessagesPersisted);
919
+ this.newResponseMessagesPersisted = this.deserializeSet(state.newResponseMessagesPersisted);
920
+ this.userContextMessagesPersisted = this.deserializeSet(state.userContextMessagesPersisted);
921
+ return this;
922
+ }
923
+ getLatestUserContent() {
924
+ const currentUserMessages = this.all.core().filter((m) => m.role === "user");
925
+ const content = currentUserMessages.at(-1)?.content;
926
+ if (!content) return null;
927
+ return _MessageList.coreContentToString(content);
928
+ }
929
+ get get() {
930
+ return {
931
+ all: this.all,
932
+ remembered: this.remembered,
933
+ input: this.input,
934
+ response: this.response
935
+ };
936
+ }
937
+ get getPersisted() {
938
+ return {
939
+ remembered: this.rememberedPersisted,
940
+ input: this.inputPersisted,
941
+ taggedSystemMessages: this.taggedSystemMessages,
942
+ response: this.responsePersisted
943
+ };
944
+ }
945
+ get clear() {
946
+ return {
947
+ input: {
948
+ v2: () => {
949
+ const userMessages = Array.from(this.newUserMessages);
950
+ this.messages = this.messages.filter((m) => !this.newUserMessages.has(m));
951
+ this.newUserMessages.clear();
952
+ return userMessages;
953
+ }
954
+ },
955
+ response: {
956
+ v2: () => {
957
+ const responseMessages = Array.from(this.newResponseMessages);
958
+ this.messages = this.messages.filter((m) => !this.newResponseMessages.has(m));
959
+ this.newResponseMessages.clear();
960
+ return responseMessages;
961
+ }
962
+ }
963
+ };
964
+ }
965
+ all = {
966
+ v3: () => this.cleanV3Metadata(this.messages.map(this.mastraMessageV2ToMastraMessageV3)),
967
+ v2: () => this.messages,
968
+ v1: () => convertToV1Messages(this.all.v2()),
969
+ aiV5: {
970
+ model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.all.aiV5.ui()),
971
+ ui: () => this.all.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage),
972
+ // Used when calling AI SDK streamText/generateText
973
+ prompt: () => {
974
+ const systemMessages = this.aiV4CoreMessagesToAIV5ModelMessages(
975
+ [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()],
976
+ `system`
977
+ );
978
+ const modelMessages = this.all.aiV5.model();
979
+ const messages = [...systemMessages, ...modelMessages];
980
+ return ensureGeminiCompatibleMessages(messages);
981
+ },
982
+ // Used for creating LLM prompt messages without AI SDK streamText/generateText
983
+ llmPrompt: async (options = {
984
+ downloadConcurrency: 10,
985
+ downloadRetries: 3
986
+ }) => {
987
+ const modelMessages = this.all.aiV5.model();
988
+ const systemMessages = this.aiV4CoreMessagesToAIV5ModelMessages(
989
+ [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()],
990
+ `system`
991
+ );
992
+ const downloadedAssets = await downloadAssetsFromMessages({
993
+ messages: modelMessages,
994
+ downloadConcurrency: options?.downloadConcurrency,
995
+ downloadRetries: options?.downloadRetries,
996
+ supportedUrls: options?.supportedUrls
997
+ });
998
+ let messages = [...systemMessages, ...modelMessages];
999
+ const hasImageOrFileContent = modelMessages.some(
1000
+ (message) => message.role === "user" && typeof message.content !== "string" && message.content.some((part) => part.type === "image" || part.type === "file")
1001
+ );
1002
+ if (hasImageOrFileContent) {
1003
+ messages = messages.map((message) => {
1004
+ if (message.role === "user") {
1005
+ if (typeof message.content === "string") {
1006
+ return {
1007
+ role: "user",
1008
+ content: [{ type: "text", text: message.content }],
1009
+ providerOptions: message.providerOptions
1010
+ };
1011
+ }
1012
+ const convertedContent = message.content.map((part) => {
1013
+ if (part.type === "image" || part.type === "file") {
1014
+ return convertImageFilePart(part, downloadedAssets);
1015
+ }
1016
+ return part;
1017
+ }).filter((part) => part.type !== "text" || part.text !== "");
1018
+ return {
1019
+ role: "user",
1020
+ content: convertedContent,
1021
+ providerOptions: message.providerOptions
1022
+ };
1023
+ }
1024
+ return message;
1025
+ });
1026
+ }
1027
+ messages = ensureGeminiCompatibleMessages(messages);
1028
+ return messages.map(_MessageList.aiV5ModelMessageToV2PromptMessage);
1029
+ }
1030
+ },
1031
+ /* @deprecated use list.get.all.aiV4.prompt() instead */
1032
+ prompt: () => this.all.aiV4.prompt(),
1033
+ /* @deprecated use list.get.all.aiV4.ui() */
1034
+ ui: () => this.all.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1035
+ /* @deprecated use list.get.all.aiV4.core() */
1036
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
1037
+ aiV4: {
1038
+ ui: () => this.all.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1039
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
1040
+ // Used when calling AI SDK streamText/generateText
1041
+ prompt: () => {
1042
+ const coreMessages = this.all.aiV4.core();
1043
+ const messages = [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat(), ...coreMessages];
1044
+ return ensureGeminiCompatibleMessages(messages);
1045
+ },
1046
+ // Used for creating LLM prompt messages without AI SDK streamText/generateText
1047
+ llmPrompt: () => {
1048
+ const coreMessages = this.all.aiV4.core();
1049
+ const systemMessages = [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()];
1050
+ let messages = [...systemMessages, ...coreMessages];
1051
+ messages = ensureGeminiCompatibleMessages(messages);
1052
+ return messages.map(_MessageList.aiV4CoreMessageToV1PromptMessage);
1053
+ }
1054
+ }
1055
+ };
1056
+ remembered = {
1057
+ v3: () => this.remembered.v2().map(this.mastraMessageV2ToMastraMessageV3),
1058
+ v2: () => this.messages.filter((m) => this.memoryMessages.has(m)),
1059
+ v1: () => convertToV1Messages(this.remembered.v2()),
1060
+ aiV5: {
1061
+ model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.remembered.aiV5.ui()),
1062
+ ui: () => this.remembered.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage)
1063
+ },
1064
+ /* @deprecated use list.get.remembered.aiV4.ui() */
1065
+ ui: () => this.remembered.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1066
+ /* @deprecated use list.get.remembered.aiV4.core() */
1067
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
1068
+ aiV4: {
1069
+ ui: () => this.remembered.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1070
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui())
1071
+ }
1072
+ };
1073
+ // TODO: need to update this for new .aiV4/5.x() pattern
1074
+ rememberedPersisted = {
1075
+ v2: () => this.all.v2().filter((m) => this.memoryMessagesPersisted.has(m)),
1076
+ v1: () => convertToV1Messages(this.rememberedPersisted.v2()),
1077
+ ui: () => this.rememberedPersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1078
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.rememberedPersisted.ui())
1079
+ };
1080
+ input = {
1081
+ v3: () => this.cleanV3Metadata(
1082
+ this.messages.filter((m) => this.newUserMessages.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
1083
+ ),
1084
+ v2: () => this.messages.filter((m) => this.newUserMessages.has(m)),
1085
+ v1: () => convertToV1Messages(this.input.v2()),
1086
+ aiV5: {
1087
+ model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.input.aiV5.ui()),
1088
+ ui: () => this.input.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage)
1089
+ },
1090
+ /* @deprecated use list.get.input.aiV4.ui() instead */
1091
+ ui: () => this.input.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1092
+ /* @deprecated use list.get.core.aiV4.ui() instead */
1093
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.input.ui()),
1094
+ aiV4: {
1095
+ ui: () => this.input.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1096
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.input.aiV4.ui())
1097
+ }
1098
+ };
1099
+ // TODO: need to update this for new .aiV4/5.x() pattern
1100
+ inputPersisted = {
1101
+ v3: () => this.cleanV3Metadata(
1102
+ this.messages.filter((m) => this.newUserMessagesPersisted.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
1103
+ ),
1104
+ v2: () => this.messages.filter((m) => this.newUserMessagesPersisted.has(m)),
1105
+ v1: () => convertToV1Messages(this.inputPersisted.v2()),
1106
+ ui: () => this.inputPersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1107
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.inputPersisted.ui())
1108
+ };
1109
+ response = {
1110
+ v3: () => this.response.v2().map(this.mastraMessageV2ToMastraMessageV3),
1111
+ v2: () => this.messages.filter((m) => this.newResponseMessages.has(m)),
1112
+ v1: () => convertToV1Messages(this.response.v3().map(_MessageList.mastraMessageV3ToV2)),
1113
+ aiV5: {
1114
+ ui: () => this.response.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage),
1115
+ model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.response.aiV5.ui()).filter(
1116
+ (m) => m.role === `tool` || m.role === `assistant`
1117
+ ),
1118
+ modelContent: (stepNumber) => {
1119
+ if (typeof stepNumber === "number") {
1120
+ const uiMessages = this.response.aiV5.ui();
1121
+ const uiMessagesParts = uiMessages.flatMap((item) => item.parts);
1122
+ const stepBoundaries = [];
1123
+ uiMessagesParts.forEach((part, index) => {
1124
+ if (part.type === "step-start") {
1125
+ stepBoundaries.push(index);
1126
+ }
1127
+ });
1128
+ if (stepNumber === -1) {
1129
+ const toolParts = uiMessagesParts.filter((p) => p.type?.startsWith("tool-"));
1130
+ const hasStepStart = stepBoundaries.length > 0;
1131
+ if (!hasStepStart && toolParts.length > 0) {
1132
+ const lastToolPart = toolParts[toolParts.length - 1];
1133
+ if (!lastToolPart) {
1134
+ return [];
1135
+ }
1136
+ const lastToolIndex = uiMessagesParts.indexOf(lastToolPart);
1137
+ const previousToolPart = toolParts[toolParts.length - 2];
1138
+ const previousToolIndex = previousToolPart ? uiMessagesParts.indexOf(previousToolPart) : -1;
1139
+ const startIndex2 = previousToolIndex + 1;
1140
+ const stepParts3 = uiMessagesParts.slice(startIndex2, lastToolIndex + 1);
1141
+ const stepUiMessages3 = [
1142
+ {
1143
+ id: "last-step",
1144
+ role: "assistant",
1145
+ parts: stepParts3
1146
+ }
1147
+ ];
1148
+ const modelMessages3 = AIV5.convertToModelMessages(this.sanitizeV5UIMessages(stepUiMessages3));
1149
+ return modelMessages3.flatMap(this.response.aiV5.stepContent);
1150
+ }
1151
+ const totalSteps = stepBoundaries.length + 1;
1152
+ if (totalSteps === 1 && !hasStepStart) {
1153
+ const stepUiMessages3 = [
1154
+ {
1155
+ id: "last-step",
1156
+ role: "assistant",
1157
+ parts: uiMessagesParts
1158
+ }
1159
+ ];
1160
+ const modelMessages3 = AIV5.convertToModelMessages(this.sanitizeV5UIMessages(stepUiMessages3));
1161
+ return modelMessages3.flatMap(this.response.aiV5.stepContent);
1162
+ }
1163
+ const lastStepStart = stepBoundaries[stepBoundaries.length - 1];
1164
+ if (lastStepStart === void 0) {
1165
+ return [];
1166
+ }
1167
+ const stepParts2 = uiMessagesParts.slice(lastStepStart + 1);
1168
+ if (stepParts2.length === 0) {
1169
+ return [];
1170
+ }
1171
+ const stepUiMessages2 = [
1172
+ {
1173
+ id: "last-step",
1174
+ role: "assistant",
1175
+ parts: stepParts2
1176
+ }
1177
+ ];
1178
+ const modelMessages2 = AIV5.convertToModelMessages(this.sanitizeV5UIMessages(stepUiMessages2));
1179
+ return modelMessages2.flatMap(this.response.aiV5.stepContent);
1180
+ }
1181
+ if (stepNumber === 1) {
1182
+ const firstStepStart = stepBoundaries[0] ?? uiMessagesParts.length;
1183
+ if (firstStepStart === 0) {
1184
+ return [];
1185
+ }
1186
+ const stepParts2 = uiMessagesParts.slice(0, firstStepStart);
1187
+ const stepUiMessages2 = [
1188
+ {
1189
+ id: "step-1",
1190
+ role: "assistant",
1191
+ parts: stepParts2
1192
+ }
1193
+ ];
1194
+ const modelMessages2 = AIV5.convertToModelMessages(this.sanitizeV5UIMessages(stepUiMessages2));
1195
+ return modelMessages2.flatMap(this.response.aiV5.stepContent);
1196
+ }
1197
+ const stepIndex = stepNumber - 2;
1198
+ if (stepIndex < 0 || stepIndex >= stepBoundaries.length) {
1199
+ return [];
1200
+ }
1201
+ const startIndex = (stepBoundaries[stepIndex] ?? 0) + 1;
1202
+ const endIndex = stepBoundaries[stepIndex + 1] ?? uiMessagesParts.length;
1203
+ if (startIndex >= endIndex) {
1204
+ return [];
1205
+ }
1206
+ const stepParts = uiMessagesParts.slice(startIndex, endIndex);
1207
+ const stepUiMessages = [
1208
+ {
1209
+ id: `step-${stepNumber}`,
1210
+ role: "assistant",
1211
+ parts: stepParts
1212
+ }
1213
+ ];
1214
+ const modelMessages = AIV5.convertToModelMessages(this.sanitizeV5UIMessages(stepUiMessages));
1215
+ return modelMessages.flatMap(this.response.aiV5.stepContent);
1216
+ }
1217
+ return this.response.aiV5.model().map(this.response.aiV5.stepContent).flat();
1218
+ },
1219
+ stepContent: (message) => {
1220
+ const latest = message ? message : this.response.aiV5.model().at(-1);
1221
+ if (!latest) return [];
1222
+ if (typeof latest.content === `string`) {
1223
+ return [{ type: "text", text: latest.content }];
1224
+ }
1225
+ return latest.content.map((c) => {
1226
+ if (c.type === `tool-result`)
1227
+ return {
1228
+ type: "tool-result",
1229
+ input: {},
1230
+ // TODO: we need to find the tool call here and add the input from it
1231
+ output: c.output,
1232
+ toolCallId: c.toolCallId,
1233
+ toolName: c.toolName
1234
+ };
1235
+ if (c.type === `file`)
1236
+ return {
1237
+ type: "file",
1238
+ file: new DefaultGeneratedFileWithType({
1239
+ data: typeof c.data === `string` ? parseDataUri(c.data).base64Content : c.data instanceof URL ? c.data.toString() : convertDataContentToBase64String(c.data),
1240
+ mediaType: c.mediaType
1241
+ })
1242
+ };
1243
+ if (c.type === `image`) {
1244
+ return {
1245
+ type: "file",
1246
+ file: new DefaultGeneratedFileWithType({
1247
+ data: typeof c.image === `string` ? parseDataUri(c.image).base64Content : c.image instanceof URL ? c.image.toString() : convertDataContentToBase64String(c.image),
1248
+ mediaType: c.mediaType || "unknown"
1249
+ })
1250
+ };
1251
+ }
1252
+ return { ...c };
1253
+ });
1254
+ }
1255
+ },
1256
+ aiV4: {
1257
+ ui: () => this.response.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
1258
+ core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.response.aiV4.ui())
1259
+ }
1260
+ };
1261
+ // TODO: need to update this for new .aiV4/5.x() pattern
1262
+ responsePersisted = {
1263
+ v3: () => this.cleanV3Metadata(
1264
+ this.messages.filter((m) => this.newResponseMessagesPersisted.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
1265
+ ),
1266
+ v2: () => this.messages.filter((m) => this.newResponseMessagesPersisted.has(m)),
1267
+ ui: () => this.responsePersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage)
1268
+ };
1269
+ drainUnsavedMessages() {
1270
+ const messages = this.messages.filter((m) => this.newUserMessages.has(m) || this.newResponseMessages.has(m));
1271
+ this.newUserMessages.clear();
1272
+ this.newResponseMessages.clear();
1273
+ return messages;
1274
+ }
1275
+ getEarliestUnsavedMessageTimestamp() {
1276
+ const unsavedMessages = this.messages.filter((m) => this.newUserMessages.has(m) || this.newResponseMessages.has(m));
1277
+ if (unsavedMessages.length === 0) return void 0;
1278
+ return Math.min(...unsavedMessages.map((m) => new Date(m.createdAt).getTime()));
1279
+ }
1280
+ getSystemMessages(tag) {
1281
+ if (tag) {
1282
+ return this.taggedSystemMessages[tag] || [];
1283
+ }
1284
+ return this.systemMessages;
1285
+ }
1286
+ addSystem(messages, tag) {
1287
+ if (!messages) return this;
1288
+ for (const message of Array.isArray(messages) ? messages : [messages]) {
1289
+ this.addOneSystem(message, tag);
1290
+ }
1291
+ return this;
1292
+ }
1293
+ aiV4UIMessagesToAIV4CoreMessages(messages) {
1294
+ return AIV4.convertToCoreMessages(this.sanitizeAIV4UIMessages(messages));
1295
+ }
1296
+ sanitizeAIV4UIMessages(messages) {
1297
+ const msgs = messages.map((m) => {
1298
+ if (m.parts.length === 0) return false;
1299
+ const safeParts = m.parts.filter(
1300
+ (p) => p.type !== `tool-invocation` || // calls and partial-calls should be updated to be results at this point
1301
+ // if they haven't we can't send them back to the llm and need to remove them.
1302
+ p.toolInvocation.state !== `call` && p.toolInvocation.state !== `partial-call`
1303
+ );
1304
+ if (!safeParts.length) return false;
1305
+ const sanitized = {
1306
+ ...m,
1307
+ parts: safeParts
1308
+ };
1309
+ if (`toolInvocations` in m && m.toolInvocations) {
1310
+ sanitized.toolInvocations = m.toolInvocations.filter((t) => t.state === `result`);
1311
+ }
1312
+ return sanitized;
1313
+ }).filter((m) => Boolean(m));
1314
+ return msgs;
1315
+ }
1316
+ /**
1317
+ * Converts various message formats to AIV4 CoreMessage format for system messages
1318
+ * @param message - The message to convert (can be string, MastraMessageV2, or AI SDK message types)
1319
+ * @returns AIV4 CoreMessage in the proper format
1320
+ */
1321
+ systemMessageToAICore(message) {
1322
+ if (typeof message === `string`) {
1323
+ return { role: "system", content: message };
1324
+ }
1325
+ if (_MessageList.isAIV5CoreMessage(message)) {
1326
+ return this.aiV5ModelMessagesToAIV4CoreMessages([message], `system`)[0];
1327
+ }
1328
+ if (_MessageList.isMastraMessageV2(message)) {
1329
+ return _MessageList.mastraMessageV2SystemToV4Core(message);
1330
+ }
1331
+ return message;
1332
+ }
1333
+ addOneSystem(message, tag) {
1334
+ const coreMessage = this.systemMessageToAICore(message);
1335
+ if (coreMessage.role !== `system`) {
1336
+ throw new Error(
1337
+ `Expected role "system" but saw ${coreMessage.role} for message ${JSON.stringify(coreMessage, null, 2)}`
1338
+ );
1339
+ }
1340
+ if (tag && !this.isDuplicateSystem(coreMessage, tag)) {
1341
+ this.taggedSystemMessages[tag] ||= [];
1342
+ this.taggedSystemMessages[tag].push(coreMessage);
1343
+ } else if (!tag && !this.isDuplicateSystem(coreMessage)) {
1344
+ this.systemMessages.push(coreMessage);
1345
+ }
1346
+ }
1347
+ isDuplicateSystem(message, tag) {
1348
+ if (tag) {
1349
+ if (!this.taggedSystemMessages[tag]) return false;
1350
+ return this.taggedSystemMessages[tag].some(
1351
+ (m) => _MessageList.cacheKeyFromAIV4CoreMessageContent(m.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(message.content)
1352
+ );
1353
+ }
1354
+ return this.systemMessages.some(
1355
+ (m) => _MessageList.cacheKeyFromAIV4CoreMessageContent(m.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(message.content)
1356
+ );
1357
+ }
1358
+ static mastraMessageV2ToAIV4UIMessage(m) {
1359
+ const experimentalAttachments = m.content.experimental_attachments ? [...m.content.experimental_attachments] : [];
1360
+ const contentString = typeof m.content.content === `string` && m.content.content !== "" ? m.content.content : m.content.parts.reduce((prev, part) => {
1361
+ if (part.type === `text`) {
1362
+ return part.text;
1363
+ }
1364
+ return prev;
1365
+ }, "");
1366
+ const parts = [];
1367
+ if (m.content.parts.length) {
1368
+ for (const part of m.content.parts) {
1369
+ if (part.type === `file`) {
1370
+ let normalizedUrl;
1371
+ if (typeof part.data === "string") {
1372
+ const categorized = categorizeFileData(part.data, part.mimeType);
1373
+ if (categorized.type === "raw") {
1374
+ normalizedUrl = createDataUri(part.data, part.mimeType || "application/octet-stream");
1375
+ } else {
1376
+ normalizedUrl = part.data;
1377
+ }
1378
+ } else {
1379
+ normalizedUrl = part.data;
1380
+ }
1381
+ experimentalAttachments.push({
1382
+ contentType: part.mimeType,
1383
+ url: normalizedUrl
1384
+ });
1385
+ } else if (part.type === "tool-invocation" && (part.toolInvocation.state === "call" || part.toolInvocation.state === "partial-call")) {
1386
+ continue;
1387
+ } else if (part.type === "tool-invocation") {
1388
+ const toolInvocation = { ...part.toolInvocation };
1389
+ let currentStep = -1;
1390
+ let toolStep = -1;
1391
+ for (const innerPart of m.content.parts) {
1392
+ if (innerPart.type === `step-start`) currentStep++;
1393
+ if (innerPart.type === `tool-invocation` && innerPart.toolInvocation.toolCallId === part.toolInvocation.toolCallId) {
1394
+ toolStep = currentStep;
1395
+ break;
1396
+ }
1397
+ }
1398
+ if (toolStep >= 0) {
1399
+ const preparedInvocation = {
1400
+ step: toolStep,
1401
+ ...toolInvocation
1402
+ };
1403
+ parts.push({
1404
+ type: "tool-invocation",
1405
+ toolInvocation: preparedInvocation
1406
+ });
1407
+ } else {
1408
+ parts.push({
1409
+ type: "tool-invocation",
1410
+ toolInvocation
1411
+ });
1412
+ }
1413
+ } else {
1414
+ parts.push(part);
1415
+ }
1416
+ }
1417
+ }
1418
+ if (parts.length === 0 && experimentalAttachments.length > 0) {
1419
+ parts.push({ type: "text", text: "" });
1420
+ }
1421
+ if (m.role === `user`) {
1422
+ const uiMessage2 = {
1423
+ id: m.id,
1424
+ role: m.role,
1425
+ content: m.content.content || contentString,
1426
+ createdAt: m.createdAt,
1427
+ parts,
1428
+ experimental_attachments: experimentalAttachments
1429
+ };
1430
+ if (m.content.metadata) {
1431
+ uiMessage2.metadata = m.content.metadata;
1432
+ }
1433
+ return uiMessage2;
1434
+ } else if (m.role === `assistant`) {
1435
+ const isSingleTextContentArray = Array.isArray(m.content.content) && m.content.content.length === 1 && m.content.content[0].type === `text`;
1436
+ const uiMessage2 = {
1437
+ id: m.id,
1438
+ role: m.role,
1439
+ content: isSingleTextContentArray ? contentString : m.content.content || contentString,
1440
+ createdAt: m.createdAt,
1441
+ parts,
1442
+ reasoning: void 0,
1443
+ toolInvocations: `toolInvocations` in m.content ? m.content.toolInvocations?.filter((t) => t.state === "result") : void 0
1444
+ };
1445
+ if (m.content.metadata) {
1446
+ uiMessage2.metadata = m.content.metadata;
1447
+ }
1448
+ return uiMessage2;
1449
+ }
1450
+ const uiMessage = {
1451
+ id: m.id,
1452
+ role: m.role,
1453
+ content: m.content.content || contentString,
1454
+ createdAt: m.createdAt,
1455
+ parts,
1456
+ experimental_attachments: experimentalAttachments
1457
+ };
1458
+ if (m.content.metadata) {
1459
+ uiMessage.metadata = m.content.metadata;
1460
+ }
1461
+ return uiMessage;
1462
+ }
1463
+ /**
1464
+ * Converts a MastraMessageV2 system message directly to AIV4 CoreMessage format
1465
+ * This is more efficient than converting to UI message first and then to core
1466
+ * @param message - The MastraMessageV2 message to convert
1467
+ * @returns AIV4 CoreMessage with system role
1468
+ */
1469
+ static mastraMessageV2SystemToV4Core(message) {
1470
+ if (message.role !== `system` || !message.content.content)
1471
+ throw new MastraError({
1472
+ id: "INVALID_SYSTEM_MESSAGE_FORMAT",
1473
+ domain: "AGENT" /* AGENT */,
1474
+ category: "USER" /* USER */,
1475
+ text: `Invalid system message format. System messages must include 'role' and 'content' properties. The content should be a string.`,
1476
+ details: {
1477
+ receivedMessage: JSON.stringify(message, null, 2)
1478
+ }
1479
+ });
1480
+ return { role: "system", content: message.content.content };
1481
+ }
1482
+ getMessageById(id) {
1483
+ return this.messages.find((m) => m.id === id);
1484
+ }
1485
+ shouldReplaceMessage(message) {
1486
+ if (!this.messages.length) return { exists: false };
1487
+ if (!(`id` in message) || !message?.id) {
1488
+ return { exists: false };
1489
+ }
1490
+ const existingMessage = this.getMessageById(message.id);
1491
+ if (!existingMessage) return { exists: false };
1492
+ return {
1493
+ exists: true,
1494
+ shouldReplace: !_MessageList.messagesAreEqual(existingMessage, message),
1495
+ id: existingMessage.id
1496
+ };
1497
+ }
1498
+ addOne(message, messageSource) {
1499
+ if ((!(`content` in message) || !message.content && // allow empty strings
1500
+ typeof message.content !== "string") && (!(`parts` in message) || !message.parts)) {
1501
+ throw new MastraError({
1502
+ id: "INVALID_MESSAGE_CONTENT",
1503
+ domain: "AGENT" /* AGENT */,
1504
+ category: "USER" /* USER */,
1505
+ text: `Message with role "${message.role}" must have either a 'content' property (string or array) or a 'parts' property (array) that is not empty, null, or undefined. Received message: ${JSON.stringify(message, null, 2)}`,
1506
+ details: {
1507
+ role: message.role,
1508
+ messageSource,
1509
+ hasContent: "content" in message,
1510
+ hasParts: "parts" in message
1511
+ }
1512
+ });
1513
+ }
1514
+ if (message.role === `system`) {
1515
+ if (messageSource === `memory`) return null;
1516
+ const isSupportedSystemFormat = _MessageList.isAIV4CoreMessage(message) || _MessageList.isAIV5CoreMessage(message) || _MessageList.isMastraMessageV2(message);
1517
+ if (isSupportedSystemFormat) {
1518
+ return this.addSystem(message);
1519
+ }
1520
+ throw new MastraError({
1521
+ id: "INVALID_SYSTEM_MESSAGE_FORMAT",
1522
+ domain: "AGENT" /* AGENT */,
1523
+ category: "USER" /* USER */,
1524
+ text: `Invalid system message format. System messages must be CoreMessage format with 'role' and 'content' properties. The content should be a string or valid content array.`,
1525
+ details: {
1526
+ messageSource,
1527
+ receivedMessage: JSON.stringify(message, null, 2)
1528
+ }
1529
+ });
1530
+ }
1531
+ const messageV2 = this.inputToMastraMessageV2(message, messageSource);
1532
+ const { exists, shouldReplace, id } = this.shouldReplaceMessage(messageV2);
1533
+ const latestMessage = this.messages.at(-1);
1534
+ if (messageSource === `memory`) {
1535
+ for (const existingMessage of this.messages) {
1536
+ if (_MessageList.messagesAreEqual(existingMessage, messageV2)) {
1537
+ return;
1538
+ }
1539
+ }
1540
+ }
1541
+ const shouldAppendToLastAssistantMessage = latestMessage?.role === "assistant" && messageV2.role === "assistant" && latestMessage.threadId === messageV2.threadId && // If the message is from memory, don't append to the last assistant message
1542
+ messageSource !== "memory";
1543
+ const appendNetworkMessage = this._agentNetworkAppend && latestMessage && !this.memoryMessages.has(latestMessage) || !this._agentNetworkAppend;
1544
+ if (shouldAppendToLastAssistantMessage && appendNetworkMessage) {
1545
+ latestMessage.createdAt = messageV2.createdAt || latestMessage.createdAt;
1546
+ const toolResultAnchorMap = /* @__PURE__ */ new Map();
1547
+ const partsToAdd = /* @__PURE__ */ new Map();
1548
+ for (const [index, part] of messageV2.content.parts.entries()) {
1549
+ if (part.type === "tool-invocation") {
1550
+ const existingCallPart = [...latestMessage.content.parts].reverse().find((p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === part.toolInvocation.toolCallId);
1551
+ const existingCallToolInvocation = !!existingCallPart && existingCallPart.type === "tool-invocation";
1552
+ if (existingCallToolInvocation) {
1553
+ if (part.toolInvocation.state === "result") {
1554
+ existingCallPart.toolInvocation = {
1555
+ ...existingCallPart.toolInvocation,
1556
+ step: part.toolInvocation.step,
1557
+ state: "result",
1558
+ result: part.toolInvocation.result,
1559
+ args: {
1560
+ ...existingCallPart.toolInvocation.args,
1561
+ ...part.toolInvocation.args
1562
+ }
1563
+ };
1564
+ if (!latestMessage.content.toolInvocations) {
1565
+ latestMessage.content.toolInvocations = [];
1566
+ }
1567
+ const toolInvocationIndex = latestMessage.content.toolInvocations.findIndex(
1568
+ (t) => t.toolCallId === existingCallPart.toolInvocation.toolCallId
1569
+ );
1570
+ if (toolInvocationIndex === -1) {
1571
+ latestMessage.content.toolInvocations.push(existingCallPart.toolInvocation);
1572
+ } else {
1573
+ latestMessage.content.toolInvocations[toolInvocationIndex] = existingCallPart.toolInvocation;
1574
+ }
1575
+ }
1576
+ const existingIndex = latestMessage.content.parts.findIndex((p) => p === existingCallPart);
1577
+ toolResultAnchorMap.set(index, existingIndex);
1578
+ } else {
1579
+ partsToAdd.set(index, part);
1580
+ }
1581
+ } else {
1582
+ partsToAdd.set(index, part);
1583
+ }
1584
+ }
1585
+ this.addPartsToLatestMessage({
1586
+ latestMessage,
1587
+ messageV2,
1588
+ anchorMap: toolResultAnchorMap,
1589
+ partsToAdd
1590
+ });
1591
+ if (latestMessage.createdAt.getTime() < messageV2.createdAt.getTime()) {
1592
+ latestMessage.createdAt = messageV2.createdAt;
1593
+ }
1594
+ if (!latestMessage.content.content && messageV2.content.content) {
1595
+ latestMessage.content.content = messageV2.content.content;
1596
+ }
1597
+ if (latestMessage.content.content && messageV2.content.content && latestMessage.content.content !== messageV2.content.content) {
1598
+ latestMessage.content.content = messageV2.content.content;
1599
+ }
1600
+ this.pushMessageToSource(latestMessage, messageSource);
1601
+ } else {
1602
+ let existingIndex = -1;
1603
+ if (shouldReplace) {
1604
+ existingIndex = this.messages.findIndex((m) => m.id === id);
1605
+ }
1606
+ const existingMessage = existingIndex !== -1 && this.messages[existingIndex];
1607
+ if (shouldReplace && existingMessage) {
1608
+ this.messages[existingIndex] = messageV2;
1609
+ } else if (!exists) {
1610
+ this.messages.push(messageV2);
1611
+ }
1612
+ this.pushMessageToSource(messageV2, messageSource);
1613
+ }
1614
+ this.messages.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
1615
+ return this;
1616
+ }
1617
+ pushMessageToSource(messageV2, messageSource) {
1618
+ if (messageSource === `memory`) {
1619
+ this.memoryMessages.add(messageV2);
1620
+ this.memoryMessagesPersisted.add(messageV2);
1621
+ } else if (messageSource === `response`) {
1622
+ this.newResponseMessages.add(messageV2);
1623
+ this.newResponseMessagesPersisted.add(messageV2);
1624
+ } else if (messageSource === `input`) {
1625
+ this.newUserMessages.add(messageV2);
1626
+ this.newUserMessagesPersisted.add(messageV2);
1627
+ } else if (messageSource === `context`) {
1628
+ this.userContextMessages.add(messageV2);
1629
+ this.userContextMessagesPersisted.add(messageV2);
1630
+ } else {
1631
+ throw new Error(`Missing message source for message ${messageV2}`);
1632
+ }
1633
+ }
1634
+ /**
1635
+ * Pushes a new message part to the latest message.
1636
+ * @param latestMessage - The latest message to push the part to.
1637
+ * @param newMessage - The new message to push the part from.
1638
+ * @param part - The part to push.
1639
+ * @param insertAt - The index at which to insert the part. Optional.
1640
+ */
1641
+ pushNewMessagePart({
1642
+ latestMessage,
1643
+ newMessage,
1644
+ part,
1645
+ insertAt
1646
+ // optional
1647
+ }) {
1648
+ const partKey = _MessageList.cacheKeyFromAIV4Parts([part]);
1649
+ const latestPartCount = latestMessage.content.parts.filter(
1650
+ (p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
1651
+ ).length;
1652
+ const newPartCount = newMessage.content.parts.filter(
1653
+ (p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
1654
+ ).length;
1655
+ if (latestPartCount < newPartCount) {
1656
+ const partIndex = newMessage.content.parts.indexOf(part);
1657
+ const hasStepStartBefore = partIndex > 0 && newMessage.content.parts[partIndex - 1]?.type === "step-start";
1658
+ const needsStepStart = latestMessage.role === "assistant" && part.type === "text" && !hasStepStartBefore && latestMessage.content.parts.length > 0 && latestMessage.content.parts.at(-1)?.type === "tool-invocation";
1659
+ if (typeof insertAt === "number") {
1660
+ if (needsStepStart) {
1661
+ latestMessage.content.parts.splice(insertAt, 0, { type: "step-start" });
1662
+ latestMessage.content.parts.splice(insertAt + 1, 0, part);
1663
+ } else {
1664
+ latestMessage.content.parts.splice(insertAt, 0, part);
1665
+ }
1666
+ } else {
1667
+ if (needsStepStart) {
1668
+ latestMessage.content.parts.push({ type: "step-start" });
1669
+ }
1670
+ latestMessage.content.parts.push(part);
1671
+ }
1672
+ }
1673
+ }
1674
+ /**
1675
+ * Upserts parts of messageV2 into latestMessage based on the anchorMap.
1676
+ * This is used when appending a message to the last assistant message to ensure that parts are inserted in the correct order.
1677
+ * @param latestMessage - The latest message to upsert parts into.
1678
+ * @param messageV2 - The message to upsert parts from.
1679
+ * @param anchorMap - The anchor map to use for upserting parts.
1680
+ */
1681
+ addPartsToLatestMessage({
1682
+ latestMessage,
1683
+ messageV2,
1684
+ anchorMap,
1685
+ partsToAdd
1686
+ }) {
1687
+ for (let i = 0; i < messageV2.content.parts.length; ++i) {
1688
+ const part = messageV2.content.parts[i];
1689
+ if (!part) continue;
1690
+ const key = _MessageList.cacheKeyFromAIV4Parts([part]);
1691
+ const partToAdd = partsToAdd.get(i);
1692
+ if (!key || !partToAdd) continue;
1693
+ if (anchorMap.size > 0) {
1694
+ if (anchorMap.has(i)) continue;
1695
+ const leftAnchorV2 = [...anchorMap.keys()].filter((idx) => idx < i).pop() ?? -1;
1696
+ const rightAnchorV2 = [...anchorMap.keys()].find((idx) => idx > i) ?? -1;
1697
+ const leftAnchorLatest = leftAnchorV2 !== -1 ? anchorMap.get(leftAnchorV2) : 0;
1698
+ const offset = leftAnchorV2 === -1 ? i : i - leftAnchorV2;
1699
+ const insertAt = leftAnchorLatest + offset;
1700
+ const rightAnchorLatest = rightAnchorV2 !== -1 ? anchorMap.get(rightAnchorV2) : latestMessage.content.parts.length;
1701
+ if (insertAt >= 0 && insertAt <= rightAnchorLatest && !latestMessage.content.parts.slice(insertAt, rightAnchorLatest).some((p) => _MessageList.cacheKeyFromAIV4Parts([p]) === _MessageList.cacheKeyFromAIV4Parts([part]))) {
1702
+ this.pushNewMessagePart({
1703
+ latestMessage,
1704
+ newMessage: messageV2,
1705
+ part,
1706
+ insertAt
1707
+ });
1708
+ for (const [v2Idx, latestIdx] of anchorMap.entries()) {
1709
+ if (latestIdx >= insertAt) {
1710
+ anchorMap.set(v2Idx, latestIdx + 1);
1711
+ }
1712
+ }
1713
+ }
1714
+ } else {
1715
+ this.pushNewMessagePart({
1716
+ latestMessage,
1717
+ newMessage: messageV2,
1718
+ part
1719
+ });
1720
+ }
1721
+ }
1722
+ }
1723
+ inputToMastraMessageV2(message, messageSource) {
1724
+ if (
1725
+ // we can't throw if the threadId doesn't match and this message came from memory
1726
+ // this is because per-user semantic recall can retrieve messages from other threads
1727
+ messageSource !== `memory` && `threadId` in message && message.threadId && this.memoryInfo && message.threadId !== this.memoryInfo.threadId
1728
+ ) {
1729
+ throw new Error(
1730
+ `Received input message with wrong threadId. Input ${message.threadId}, expected ${this.memoryInfo.threadId}`
1731
+ );
1732
+ }
1733
+ if (`resourceId` in message && message.resourceId && this.memoryInfo?.resourceId && message.resourceId !== this.memoryInfo.resourceId) {
1734
+ throw new Error(
1735
+ `Received input message with wrong resourceId. Input ${message.resourceId}, expected ${this.memoryInfo.resourceId}`
1736
+ );
1737
+ }
1738
+ if (_MessageList.isMastraMessageV1(message)) {
1739
+ return this.mastraMessageV1ToMastraMessageV2(message, messageSource);
1740
+ }
1741
+ if (_MessageList.isMastraMessageV2(message)) {
1742
+ return this.hydrateMastraMessageV2Fields(message);
1743
+ }
1744
+ if (_MessageList.isAIV4CoreMessage(message)) {
1745
+ return this.aiV4CoreMessageToMastraMessageV2(message, messageSource);
1746
+ }
1747
+ if (_MessageList.isAIV4UIMessage(message)) {
1748
+ return this.aiV4UIMessageToMastraMessageV2(message, messageSource);
1749
+ }
1750
+ if (_MessageList.isAIV5CoreMessage(message)) {
1751
+ return _MessageList.mastraMessageV3ToV2(this.aiV5ModelMessageToMastraMessageV3(message, messageSource));
1752
+ }
1753
+ if (_MessageList.isAIV5UIMessage(message)) {
1754
+ return _MessageList.mastraMessageV3ToV2(this.aiV5UIMessageToMastraMessageV3(message, messageSource));
1755
+ }
1756
+ if (_MessageList.isMastraMessageV3(message)) {
1757
+ return _MessageList.mastraMessageV3ToV2(this.hydrateMastraMessageV3Fields(message));
1758
+ }
1759
+ throw new Error(`Found unhandled message ${JSON.stringify(message)}`);
1760
+ }
1761
+ lastCreatedAt;
1762
+ // this makes sure messages added in order will always have a date atleast 1ms apart.
1763
+ generateCreatedAt(messageSource, start) {
1764
+ start = start instanceof Date ? start : start ? new Date(start) : void 0;
1765
+ if (start && !this.lastCreatedAt) {
1766
+ this.lastCreatedAt = start.getTime();
1767
+ return start;
1768
+ }
1769
+ if (start && messageSource === `memory`) {
1770
+ return start;
1771
+ }
1772
+ const now = /* @__PURE__ */ new Date();
1773
+ const nowTime = start?.getTime() || now.getTime();
1774
+ const lastTime = this.messages.reduce((p, m) => {
1775
+ if (m.createdAt.getTime() > p) return m.createdAt.getTime();
1776
+ return p;
1777
+ }, this.lastCreatedAt || 0);
1778
+ if (nowTime <= lastTime) {
1779
+ const newDate = new Date(lastTime + 1);
1780
+ this.lastCreatedAt = newDate.getTime();
1781
+ return newDate;
1782
+ }
1783
+ this.lastCreatedAt = nowTime;
1784
+ return now;
1785
+ }
1786
+ newMessageId() {
1787
+ if (this.generateMessageId) {
1788
+ return this.generateMessageId();
1789
+ }
1790
+ return randomUUID();
1791
+ }
1792
+ mastraMessageV1ToMastraMessageV2(message, messageSource) {
1793
+ const coreV2 = this.aiV4CoreMessageToMastraMessageV2(
1794
+ {
1795
+ content: message.content,
1796
+ role: message.role
1797
+ },
1798
+ messageSource
1799
+ );
1800
+ return {
1801
+ id: message.id,
1802
+ role: coreV2.role,
1803
+ createdAt: this.generateCreatedAt(messageSource, message.createdAt),
1804
+ threadId: message.threadId,
1805
+ resourceId: message.resourceId,
1806
+ content: coreV2.content
1807
+ };
1808
+ }
1809
+ hydrateMastraMessageV3Fields(message) {
1810
+ if (!(message.createdAt instanceof Date)) message.createdAt = new Date(message.createdAt);
1811
+ return message;
1812
+ }
1813
+ hydrateMastraMessageV2Fields(message) {
1814
+ if (!(message.createdAt instanceof Date)) message.createdAt = new Date(message.createdAt);
1815
+ if (!message.threadId && this.memoryInfo?.threadId) {
1816
+ message.threadId = this.memoryInfo.threadId;
1817
+ }
1818
+ if (!message.resourceId && this.memoryInfo?.resourceId) {
1819
+ message.resourceId = this.memoryInfo.resourceId;
1820
+ }
1821
+ if (message.content.toolInvocations && message.content.parts) {
1822
+ message.content.toolInvocations = message.content.toolInvocations.map((ti) => {
1823
+ if (!ti.args || Object.keys(ti.args).length === 0) {
1824
+ const partWithArgs = message.content.parts.find(
1825
+ (part) => part.type === "tool-invocation" && part.toolInvocation && part.toolInvocation.toolCallId === ti.toolCallId && part.toolInvocation.args && Object.keys(part.toolInvocation.args).length > 0
1826
+ );
1827
+ if (partWithArgs && partWithArgs.type === "tool-invocation") {
1828
+ return { ...ti, args: partWithArgs.toolInvocation.args };
1829
+ }
1830
+ }
1831
+ return ti;
1832
+ });
1833
+ }
1834
+ return message;
1835
+ }
1836
+ aiV4UIMessageToMastraMessageV2(message, messageSource) {
1837
+ const content = {
1838
+ format: 2,
1839
+ parts: message.parts
1840
+ };
1841
+ if (message.toolInvocations) content.toolInvocations = message.toolInvocations;
1842
+ if (message.reasoning) content.reasoning = message.reasoning;
1843
+ if (message.annotations) content.annotations = message.annotations;
1844
+ if (message.experimental_attachments) {
1845
+ content.experimental_attachments = message.experimental_attachments;
1846
+ }
1847
+ if ("metadata" in message && message.metadata !== null && message.metadata !== void 0) {
1848
+ content.metadata = message.metadata;
1849
+ }
1850
+ if ("providerMetadata" in message && message.providerMetadata !== null && message.providerMetadata !== void 0) {
1851
+ content.providerMetadata = message.providerMetadata;
1852
+ }
1853
+ return {
1854
+ id: message.id || this.newMessageId(),
1855
+ role: _MessageList.getRole(message),
1856
+ createdAt: this.generateCreatedAt(messageSource, message.createdAt),
1857
+ threadId: this.memoryInfo?.threadId,
1858
+ resourceId: this.memoryInfo?.resourceId,
1859
+ content
1860
+ };
1861
+ }
1862
+ aiV4CoreMessageToMastraMessageV2(coreMessage, messageSource) {
1863
+ const id = `id` in coreMessage ? coreMessage.id : this.newMessageId();
1864
+ const parts = [];
1865
+ const experimentalAttachments = [];
1866
+ const toolInvocations = [];
1867
+ const isSingleTextContent = messageSource === `response` && Array.isArray(coreMessage.content) && coreMessage.content.length === 1 && coreMessage.content[0] && coreMessage.content[0].type === `text` && `text` in coreMessage.content[0] && coreMessage.content[0].text;
1868
+ if (isSingleTextContent && messageSource === `response`) {
1869
+ coreMessage.content = isSingleTextContent;
1870
+ }
1871
+ if (typeof coreMessage.content === "string") {
1872
+ parts.push({
1873
+ type: "text",
1874
+ text: coreMessage.content,
1875
+ // Preserve providerOptions from CoreMessage (e.g., for system messages with cacheControl)
1876
+ ..."providerOptions" in coreMessage && coreMessage.providerOptions ? { providerMetadata: coreMessage.providerOptions } : {}
1877
+ });
1878
+ } else if (Array.isArray(coreMessage.content)) {
1879
+ for (const part of coreMessage.content) {
1880
+ switch (part.type) {
1881
+ case "text":
1882
+ const prevPart = parts.at(-1);
1883
+ if (coreMessage.role === "assistant" && prevPart && prevPart.type === "tool-invocation") {
1884
+ parts.push({ type: "step-start" });
1885
+ }
1886
+ const mergedProviderMetadata = {
1887
+ ..."providerOptions" in coreMessage && coreMessage.providerOptions ? coreMessage.providerOptions : {},
1888
+ ..."providerOptions" in part && part.providerOptions ? part.providerOptions : {}
1889
+ };
1890
+ parts.push({
1891
+ type: "text",
1892
+ text: part.text,
1893
+ ...Object.keys(mergedProviderMetadata).length > 0 ? { providerMetadata: mergedProviderMetadata } : {}
1894
+ });
1895
+ break;
1896
+ case "tool-call": {
1897
+ const toolCallPart = {
1898
+ type: "tool-invocation",
1899
+ toolInvocation: {
1900
+ state: "call",
1901
+ toolCallId: part.toolCallId,
1902
+ toolName: part.toolName,
1903
+ args: part.args
1904
+ }
1905
+ };
1906
+ if ("providerOptions" in part && part.providerOptions) {
1907
+ toolCallPart.providerMetadata = part.providerOptions;
1908
+ }
1909
+ parts.push(toolCallPart);
1910
+ break;
1911
+ }
1912
+ case "tool-result":
1913
+ let toolArgs = {};
1914
+ const toolCallInSameMsg = coreMessage.content.find(
1915
+ (p) => p.type === "tool-call" && p.toolCallId === part.toolCallId
1916
+ );
1917
+ if (toolCallInSameMsg && toolCallInSameMsg.type === "tool-call") {
1918
+ toolArgs = toolCallInSameMsg.args;
1919
+ }
1920
+ if (Object.keys(toolArgs).length === 0) {
1921
+ for (let i = this.messages.length - 1; i >= 0; i--) {
1922
+ const msg = this.messages[i];
1923
+ if (msg && msg.role === "assistant" && msg.content.parts) {
1924
+ const toolCallPart = msg.content.parts.find(
1925
+ (p) => p.type === "tool-invocation" && p.toolInvocation.toolCallId === part.toolCallId && p.toolInvocation.state === "call"
1926
+ );
1927
+ if (toolCallPart && toolCallPart.type === "tool-invocation" && toolCallPart.toolInvocation.args) {
1928
+ toolArgs = toolCallPart.toolInvocation.args;
1929
+ break;
1930
+ }
1931
+ }
1932
+ }
1933
+ }
1934
+ const invocation = {
1935
+ state: "result",
1936
+ toolCallId: part.toolCallId,
1937
+ toolName: part.toolName,
1938
+ result: part.result ?? "",
1939
+ // undefined will cause AI SDK to throw an error, but for client side tool calls this really could be undefined
1940
+ args: toolArgs
1941
+ // Use the args from the corresponding tool-call
1942
+ };
1943
+ const toolResultPart = {
1944
+ type: "tool-invocation",
1945
+ toolInvocation: invocation
1946
+ };
1947
+ if ("providerOptions" in part && part.providerOptions) {
1948
+ toolResultPart.providerMetadata = part.providerOptions;
1949
+ }
1950
+ parts.push(toolResultPart);
1951
+ toolInvocations.push(invocation);
1952
+ break;
1953
+ case "reasoning": {
1954
+ const reasoningPart = {
1955
+ type: "reasoning",
1956
+ reasoning: "",
1957
+ // leave this blank so we aren't double storing it in the db along with details
1958
+ details: [{ type: "text", text: part.text, signature: part.signature }]
1959
+ };
1960
+ if ("providerOptions" in part && part.providerOptions) {
1961
+ reasoningPart.providerMetadata = part.providerOptions;
1962
+ }
1963
+ parts.push(reasoningPart);
1964
+ break;
1965
+ }
1966
+ case "redacted-reasoning":
1967
+ parts.push({
1968
+ type: "reasoning",
1969
+ reasoning: "",
1970
+ // No text reasoning for redacted parts
1971
+ details: [{ type: "redacted", data: part.data }]
1972
+ });
1973
+ break;
1974
+ case "image":
1975
+ parts.push({
1976
+ type: "file",
1977
+ data: imageContentToString(part.image),
1978
+ mimeType: part.mimeType
1979
+ });
1980
+ break;
1981
+ case "file":
1982
+ if (part.data instanceof URL) {
1983
+ parts.push({
1984
+ type: "file",
1985
+ data: part.data.toString(),
1986
+ mimeType: part.mimeType
1987
+ });
1988
+ } else if (typeof part.data === "string") {
1989
+ const categorized = categorizeFileData(part.data, part.mimeType);
1990
+ if (categorized.type === "url" || categorized.type === "dataUri") {
1991
+ parts.push({
1992
+ type: "file",
1993
+ data: part.data,
1994
+ mimeType: categorized.mimeType || "image/png"
1995
+ });
1996
+ } else {
1997
+ try {
1998
+ parts.push({
1999
+ type: "file",
2000
+ mimeType: categorized.mimeType || "image/png",
2001
+ data: convertDataContentToBase64String(part.data)
2002
+ });
2003
+ } catch (error) {
2004
+ console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2005
+ }
2006
+ }
2007
+ } else {
2008
+ try {
2009
+ parts.push({
2010
+ type: "file",
2011
+ mimeType: part.mimeType,
2012
+ data: convertDataContentToBase64String(part.data)
2013
+ });
2014
+ } catch (error) {
2015
+ console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
2016
+ }
2017
+ }
2018
+ break;
2019
+ }
2020
+ }
2021
+ }
2022
+ const content = {
2023
+ format: 2,
2024
+ parts
2025
+ };
2026
+ if (toolInvocations.length) content.toolInvocations = toolInvocations;
2027
+ if (typeof coreMessage.content === `string`) content.content = coreMessage.content;
2028
+ if (experimentalAttachments.length) content.experimental_attachments = experimentalAttachments;
2029
+ if (coreMessage.providerOptions) {
2030
+ content.providerMetadata = coreMessage.providerOptions;
2031
+ }
2032
+ if ("metadata" in coreMessage && coreMessage.metadata !== null && coreMessage.metadata !== void 0) {
2033
+ content.metadata = coreMessage.metadata;
2034
+ }
2035
+ return {
2036
+ id,
2037
+ role: _MessageList.getRole(coreMessage),
2038
+ createdAt: this.generateCreatedAt(messageSource),
2039
+ threadId: this.memoryInfo?.threadId,
2040
+ resourceId: this.memoryInfo?.resourceId,
2041
+ content
2042
+ };
2043
+ }
2044
+ static isAIV4UIMessage(msg) {
2045
+ return !_MessageList.isMastraMessage(msg) && !_MessageList.isAIV4CoreMessage(msg) && `parts` in msg && !_MessageList.hasAIV5UIMessageCharacteristics(msg);
2046
+ }
2047
+ static isAIV5CoreMessage(msg) {
2048
+ return !_MessageList.isMastraMessage(msg) && !(`parts` in msg) && `content` in msg && _MessageList.hasAIV5CoreMessageCharacteristics(msg);
2049
+ }
2050
+ static isAIV4CoreMessage(msg) {
2051
+ return !_MessageList.isMastraMessage(msg) && !(`parts` in msg) && `content` in msg && !_MessageList.hasAIV5CoreMessageCharacteristics(msg);
2052
+ }
2053
+ static isMastraMessage(msg) {
2054
+ return _MessageList.isMastraMessageV3(msg) || _MessageList.isMastraMessageV2(msg) || _MessageList.isMastraMessageV1(msg);
2055
+ }
2056
+ static isMastraMessageV1(msg) {
2057
+ return !_MessageList.isMastraMessageV2(msg) && !_MessageList.isMastraMessageV3(msg) && (`threadId` in msg || `resourceId` in msg);
2058
+ }
2059
+ static isMastraMessageV2(msg) {
2060
+ return Boolean(
2061
+ `content` in msg && msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` && `format` in msg.content && msg.content.format === 2
2062
+ );
2063
+ }
2064
+ static isMastraMessageV3(msg) {
2065
+ return Boolean(
2066
+ `content` in msg && msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` && `format` in msg.content && msg.content.format === 3
2067
+ );
2068
+ }
2069
+ static getRole(message) {
2070
+ if (message.role === `assistant` || message.role === `tool`) return `assistant`;
2071
+ if (message.role === `user`) return `user`;
2072
+ if (message.role === `system`) return `system`;
2073
+ throw new Error(
2074
+ `BUG: add handling for message role ${message.role} in message ${JSON.stringify(message, null, 2)}`
2075
+ );
2076
+ }
2077
+ static cacheKeyFromAIV4Parts(parts) {
2078
+ let key = ``;
2079
+ for (const part of parts) {
2080
+ key += part.type;
2081
+ if (part.type === `text`) {
2082
+ key += part.text;
2083
+ }
2084
+ if (part.type === `tool-invocation`) {
2085
+ key += part.toolInvocation.toolCallId;
2086
+ key += part.toolInvocation.state;
2087
+ }
2088
+ if (part.type === `reasoning`) {
2089
+ key += part.reasoning;
2090
+ key += part.details.reduce((prev, current) => {
2091
+ if (current.type === `text`) {
2092
+ return prev + current.text.length + (current.signature?.length || 0);
2093
+ }
2094
+ return prev;
2095
+ }, 0);
2096
+ }
2097
+ if (part.type === `file`) {
2098
+ key += part.data;
2099
+ key += part.mimeType;
2100
+ }
2101
+ }
2102
+ return key;
2103
+ }
2104
+ static coreContentToString(content) {
2105
+ if (typeof content === `string`) return content;
2106
+ return content.reduce((p, c) => {
2107
+ if (c.type === `text`) {
2108
+ p += c.text;
2109
+ }
2110
+ return p;
2111
+ }, "");
2112
+ }
2113
+ static cacheKeyFromAIV4CoreMessageContent(content) {
2114
+ if (typeof content === `string`) return content;
2115
+ let key = ``;
2116
+ for (const part of content) {
2117
+ key += part.type;
2118
+ if (part.type === `text`) {
2119
+ key += part.text.length;
2120
+ }
2121
+ if (part.type === `reasoning`) {
2122
+ key += part.text.length;
2123
+ }
2124
+ if (part.type === `tool-call`) {
2125
+ key += part.toolCallId;
2126
+ key += part.toolName;
2127
+ }
2128
+ if (part.type === `tool-result`) {
2129
+ key += part.toolCallId;
2130
+ key += part.toolName;
2131
+ }
2132
+ if (part.type === `file`) {
2133
+ key += part.filename;
2134
+ key += part.mimeType;
2135
+ }
2136
+ if (part.type === `image`) {
2137
+ key += getImageCacheKey(part.image);
2138
+ key += part.mimeType;
2139
+ }
2140
+ if (part.type === `redacted-reasoning`) {
2141
+ key += part.data.length;
2142
+ }
2143
+ }
2144
+ return key;
2145
+ }
2146
+ static messagesAreEqual(one, two) {
2147
+ const oneUIV4 = _MessageList.isAIV4UIMessage(one) && one;
2148
+ const twoUIV4 = _MessageList.isAIV4UIMessage(two) && two;
2149
+ if (oneUIV4 && !twoUIV4) return false;
2150
+ if (oneUIV4 && twoUIV4) {
2151
+ return _MessageList.cacheKeyFromAIV4Parts(one.parts) === _MessageList.cacheKeyFromAIV4Parts(two.parts);
2152
+ }
2153
+ const oneCMV4 = _MessageList.isAIV4CoreMessage(one) && one;
2154
+ const twoCMV4 = _MessageList.isAIV4CoreMessage(two) && two;
2155
+ if (oneCMV4 && !twoCMV4) return false;
2156
+ if (oneCMV4 && twoCMV4) {
2157
+ return _MessageList.cacheKeyFromAIV4CoreMessageContent(oneCMV4.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(twoCMV4.content);
2158
+ }
2159
+ const oneMM1 = _MessageList.isMastraMessageV1(one) && one;
2160
+ const twoMM1 = _MessageList.isMastraMessageV1(two) && two;
2161
+ if (oneMM1 && !twoMM1) return false;
2162
+ if (oneMM1 && twoMM1) {
2163
+ return oneMM1.id === twoMM1.id && _MessageList.cacheKeyFromAIV4CoreMessageContent(oneMM1.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(twoMM1.content);
2164
+ }
2165
+ const oneMM2 = _MessageList.isMastraMessageV2(one) && one;
2166
+ const twoMM2 = _MessageList.isMastraMessageV2(two) && two;
2167
+ if (oneMM2 && !twoMM2) return false;
2168
+ if (oneMM2 && twoMM2) {
2169
+ return oneMM2.id === twoMM2.id && _MessageList.cacheKeyFromAIV4Parts(oneMM2.content.parts) === _MessageList.cacheKeyFromAIV4Parts(twoMM2.content.parts);
2170
+ }
2171
+ const oneMM3 = _MessageList.isMastraMessageV3(one) && one;
2172
+ const twoMM3 = _MessageList.isMastraMessageV3(two) && two;
2173
+ if (oneMM3 && !twoMM3) return false;
2174
+ if (oneMM3 && twoMM3) {
2175
+ return oneMM3.id === twoMM3.id && _MessageList.cacheKeyFromAIV5Parts(oneMM3.content.parts) === _MessageList.cacheKeyFromAIV5Parts(twoMM3.content.parts);
2176
+ }
2177
+ const oneUIV5 = _MessageList.isAIV5UIMessage(one) && one;
2178
+ const twoUIV5 = _MessageList.isAIV5UIMessage(two) && two;
2179
+ if (oneUIV5 && !twoUIV5) return false;
2180
+ if (oneUIV5 && twoUIV5) {
2181
+ return _MessageList.cacheKeyFromAIV5Parts(one.parts) === _MessageList.cacheKeyFromAIV5Parts(two.parts);
2182
+ }
2183
+ const oneCMV5 = _MessageList.isAIV5CoreMessage(one) && one;
2184
+ const twoCMV5 = _MessageList.isAIV5CoreMessage(two) && two;
2185
+ if (oneCMV5 && !twoCMV5) return false;
2186
+ if (oneCMV5 && twoCMV5) {
2187
+ return _MessageList.cacheKeyFromAIV5ModelMessageContent(oneCMV5.content) === _MessageList.cacheKeyFromAIV5ModelMessageContent(twoCMV5.content);
2188
+ }
2189
+ return true;
2190
+ }
2191
+ cleanV3Metadata(messages) {
2192
+ return messages.map((msg) => {
2193
+ if (!msg.content.metadata || typeof msg.content.metadata !== "object") {
2194
+ return msg;
2195
+ }
2196
+ const metadata = { ...msg.content.metadata };
2197
+ const hasOriginalContent = "__originalContent" in metadata;
2198
+ const hasOriginalAttachments = "__originalExperimentalAttachments" in metadata;
2199
+ if (!hasOriginalContent && !hasOriginalAttachments) {
2200
+ return msg;
2201
+ }
2202
+ const { __originalContent, __originalExperimentalAttachments, ...cleanMetadata } = metadata;
2203
+ if (Object.keys(cleanMetadata).length === 0) {
2204
+ const { metadata: metadata2, ...contentWithoutMetadata } = msg.content;
2205
+ return { ...msg, content: contentWithoutMetadata };
2206
+ }
2207
+ return { ...msg, content: { ...msg.content, metadata: cleanMetadata } };
2208
+ });
2209
+ }
2210
+ static aiV4CoreMessageToV1PromptMessage(coreMessage) {
2211
+ if (coreMessage.role === `system`) {
2212
+ return coreMessage;
2213
+ }
2214
+ if (typeof coreMessage.content === `string` && (coreMessage.role === `assistant` || coreMessage.role === `user`)) {
2215
+ return {
2216
+ ...coreMessage,
2217
+ content: [{ type: "text", text: coreMessage.content }]
2218
+ };
2219
+ }
2220
+ if (typeof coreMessage.content === `string`) {
2221
+ throw new Error(
2222
+ `Saw text content for input CoreMessage, but the role is ${coreMessage.role}. This is only allowed for "system", "assistant", and "user" roles.`
2223
+ );
2224
+ }
2225
+ const roleContent = {
2226
+ user: [],
2227
+ assistant: [],
2228
+ tool: []
2229
+ };
2230
+ const role = coreMessage.role;
2231
+ for (const part of coreMessage.content) {
2232
+ const incompatibleMessage = `Saw incompatible message content part type ${part.type} for message role ${role}`;
2233
+ switch (part.type) {
2234
+ case "text": {
2235
+ if (role === `tool`) {
2236
+ throw new Error(incompatibleMessage);
2237
+ }
2238
+ roleContent[role].push(part);
2239
+ break;
2240
+ }
2241
+ case "redacted-reasoning":
2242
+ case "reasoning": {
2243
+ if (role !== `assistant`) {
2244
+ throw new Error(incompatibleMessage);
2245
+ }
2246
+ roleContent[role].push(part);
2247
+ break;
2248
+ }
2249
+ case "tool-call": {
2250
+ if (role === `tool` || role === `user`) {
2251
+ throw new Error(incompatibleMessage);
2252
+ }
2253
+ roleContent[role].push(part);
2254
+ break;
2255
+ }
2256
+ case "tool-result": {
2257
+ if (role === `assistant` || role === `user`) {
2258
+ throw new Error(incompatibleMessage);
2259
+ }
2260
+ roleContent[role].push(part);
2261
+ break;
2262
+ }
2263
+ case "image": {
2264
+ if (role === `tool` || role === `assistant`) {
2265
+ throw new Error(incompatibleMessage);
2266
+ }
2267
+ let processedImage;
2268
+ if (part.image instanceof URL || part.image instanceof Uint8Array) {
2269
+ processedImage = part.image;
2270
+ } else if (Buffer.isBuffer(part.image) || part.image instanceof ArrayBuffer) {
2271
+ processedImage = new Uint8Array(part.image);
2272
+ } else {
2273
+ const categorized = categorizeFileData(part.image, part.mimeType);
2274
+ if (categorized.type === "raw") {
2275
+ const dataUri = createDataUri(part.image, part.mimeType || "image/png");
2276
+ processedImage = new URL(dataUri);
2277
+ } else {
2278
+ processedImage = new URL(part.image);
2279
+ }
2280
+ }
2281
+ roleContent[role].push({
2282
+ ...part,
2283
+ image: processedImage
2284
+ });
2285
+ break;
2286
+ }
2287
+ case "file": {
2288
+ if (role === `tool`) {
2289
+ throw new Error(incompatibleMessage);
2290
+ }
2291
+ roleContent[role].push({
2292
+ ...part,
2293
+ data: part.data instanceof URL ? part.data : typeof part.data === "string" ? part.data : convertDataContentToBase64String(part.data)
2294
+ });
2295
+ break;
2296
+ }
2297
+ }
2298
+ }
2299
+ if (role === `tool`) {
2300
+ return {
2301
+ ...coreMessage,
2302
+ content: roleContent[role]
2303
+ };
2304
+ }
2305
+ if (role === `user`) {
2306
+ return {
2307
+ ...coreMessage,
2308
+ content: roleContent[role]
2309
+ };
2310
+ }
2311
+ if (role === `assistant`) {
2312
+ return {
2313
+ ...coreMessage,
2314
+ content: roleContent[role]
2315
+ };
2316
+ }
2317
+ throw new Error(
2318
+ `Encountered unknown role ${role} when converting V4 CoreMessage -> V4 LanguageModelV1Prompt, input message: ${JSON.stringify(coreMessage, null, 2)}`
2319
+ );
2320
+ }
2321
+ static aiV5ModelMessageToV2PromptMessage(modelMessage) {
2322
+ if (modelMessage.role === `system`) {
2323
+ return modelMessage;
2324
+ }
2325
+ if (typeof modelMessage.content === `string` && (modelMessage.role === `assistant` || modelMessage.role === `user`)) {
2326
+ return {
2327
+ role: modelMessage.role,
2328
+ content: [{ type: "text", text: modelMessage.content }],
2329
+ providerOptions: modelMessage.providerOptions
2330
+ };
2331
+ }
2332
+ if (typeof modelMessage.content === `string`) {
2333
+ throw new Error(
2334
+ `Saw text content for input ModelMessage, but the role is ${modelMessage.role}. This is only allowed for "system", "assistant", and "user" roles.`
2335
+ );
2336
+ }
2337
+ const roleContent = {
2338
+ user: [],
2339
+ assistant: [],
2340
+ tool: []
2341
+ };
2342
+ const role = modelMessage.role;
2343
+ for (const part of modelMessage.content) {
2344
+ const incompatibleMessage = `Saw incompatible message content part type ${part.type} for message role ${role}`;
2345
+ switch (part.type) {
2346
+ case "text": {
2347
+ if (role === `tool`) {
2348
+ throw new Error(incompatibleMessage);
2349
+ }
2350
+ roleContent[role].push(part);
2351
+ break;
2352
+ }
2353
+ case "reasoning": {
2354
+ if (role === `tool` || role === `user`) {
2355
+ throw new Error(incompatibleMessage);
2356
+ }
2357
+ roleContent[role].push(part);
2358
+ break;
2359
+ }
2360
+ case "tool-call": {
2361
+ if (role !== `assistant`) {
2362
+ throw new Error(incompatibleMessage);
2363
+ }
2364
+ roleContent[role].push(part);
2365
+ break;
2366
+ }
2367
+ case "tool-result": {
2368
+ if (role === `assistant` || role === `user`) {
2369
+ throw new Error(incompatibleMessage);
2370
+ }
2371
+ roleContent[role].push(part);
2372
+ break;
2373
+ }
2374
+ case "file": {
2375
+ if (role === `tool`) {
2376
+ throw new Error(incompatibleMessage);
2377
+ }
2378
+ roleContent[role].push({
2379
+ ...part,
2380
+ data: part.data instanceof ArrayBuffer ? new Uint8Array(part.data) : part.data
2381
+ });
2382
+ break;
2383
+ }
2384
+ case "image": {
2385
+ if (role === `tool`) {
2386
+ throw new Error(incompatibleMessage);
2387
+ }
2388
+ roleContent[role].push({
2389
+ ...part,
2390
+ mediaType: part.mediaType || "image/unknown",
2391
+ type: "file",
2392
+ data: part.image instanceof ArrayBuffer ? new Uint8Array(part.image) : part.image
2393
+ });
2394
+ break;
2395
+ }
2396
+ }
2397
+ }
2398
+ if (role === `tool`) {
2399
+ return {
2400
+ ...modelMessage,
2401
+ content: roleContent[role]
2402
+ };
2403
+ }
2404
+ if (role === `user`) {
2405
+ return {
2406
+ ...modelMessage,
2407
+ content: roleContent[role]
2408
+ };
2409
+ }
2410
+ if (role === `assistant`) {
2411
+ return {
2412
+ ...modelMessage,
2413
+ content: roleContent[role]
2414
+ };
2415
+ }
2416
+ throw new Error(
2417
+ `Encountered unknown role ${role} when converting V5 ModelMessage -> V5 LanguageModelV2Message, input message: ${JSON.stringify(modelMessage, null, 2)}`
2418
+ );
2419
+ }
2420
+ static mastraMessageV3ToV2(v3Msg) {
2421
+ const toolInvocationParts = v3Msg.content.parts.filter((p) => AIV5.isToolUIPart(p));
2422
+ const hadToolInvocations = v3Msg.content.metadata?.__hadToolInvocations === true;
2423
+ let toolInvocations = void 0;
2424
+ if (toolInvocationParts.length > 0) {
2425
+ const invocations = toolInvocationParts.map((p) => {
2426
+ const toolName = getToolName(p);
2427
+ if (p.state === `output-available`) {
2428
+ return {
2429
+ args: p.input,
2430
+ result: typeof p.output === "object" && p.output && "value" in p.output ? p.output.value : p.output,
2431
+ toolCallId: p.toolCallId,
2432
+ toolName,
2433
+ state: "result"
2434
+ };
2435
+ }
2436
+ return {
2437
+ args: p.input,
2438
+ state: "call",
2439
+ toolName,
2440
+ toolCallId: p.toolCallId
2441
+ };
2442
+ });
2443
+ toolInvocations = invocations;
2444
+ } else if (hadToolInvocations && v3Msg.role === "assistant") {
2445
+ toolInvocations = [];
2446
+ }
2447
+ const attachmentUrls = new Set(v3Msg.content.metadata?.__attachmentUrls || []);
2448
+ const v2Msg = {
2449
+ id: v3Msg.id,
2450
+ resourceId: v3Msg.resourceId,
2451
+ threadId: v3Msg.threadId,
2452
+ createdAt: v3Msg.createdAt,
2453
+ role: v3Msg.role,
2454
+ content: {
2455
+ format: 2,
2456
+ parts: v3Msg.content.parts.map((p) => {
2457
+ if (AIV5.isToolUIPart(p) || p.type === "dynamic-tool") {
2458
+ const toolName = getToolName(p);
2459
+ const shared = {
2460
+ state: p.state,
2461
+ args: p.input,
2462
+ toolCallId: p.toolCallId,
2463
+ toolName
2464
+ };
2465
+ if (p.state === `output-available`) {
2466
+ return {
2467
+ type: "tool-invocation",
2468
+ toolInvocation: {
2469
+ ...shared,
2470
+ state: "result",
2471
+ result: typeof p.output === "object" && p.output && "value" in p.output ? p.output.value : p.output
2472
+ },
2473
+ providerMetadata: p.callProviderMetadata
2474
+ };
2475
+ }
2476
+ return {
2477
+ type: "tool-invocation",
2478
+ toolInvocation: {
2479
+ ...shared,
2480
+ state: p.state === `input-available` ? `call` : `partial-call`
2481
+ }
2482
+ };
2483
+ }
2484
+ switch (p.type) {
2485
+ case "text":
2486
+ return p;
2487
+ case "file": {
2488
+ const fileDataSource = "url" in p && typeof p.url === "string" ? p.url : "data" in p && typeof p.data === "string" ? p.data : void 0;
2489
+ if (!fileDataSource) {
2490
+ return null;
2491
+ }
2492
+ if (attachmentUrls.has(fileDataSource)) {
2493
+ return null;
2494
+ }
2495
+ return {
2496
+ type: "file",
2497
+ mimeType: p.mediaType,
2498
+ data: fileDataSource,
2499
+ ...p.providerMetadata ? { providerMetadata: p.providerMetadata } : {}
2500
+ };
2501
+ }
2502
+ case "reasoning":
2503
+ if (p.text === "" && !p.providerMetadata) return null;
2504
+ return {
2505
+ type: "reasoning",
2506
+ // Keep reasoning field empty to match the format used in llm-execution-step
2507
+ // This ensures cache keys match for deduplication when merging assistant messages
2508
+ reasoning: "",
2509
+ details: p.text ? [{ type: "text", text: p.text }] : [],
2510
+ providerMetadata: p.providerMetadata
2511
+ };
2512
+ case "source-url":
2513
+ return {
2514
+ type: "source",
2515
+ source: {
2516
+ url: p.url,
2517
+ id: p.sourceId,
2518
+ sourceType: "url"
2519
+ },
2520
+ providerMetadata: p.providerMetadata
2521
+ };
2522
+ case "step-start":
2523
+ return p;
2524
+ }
2525
+ return null;
2526
+ }).filter((p) => Boolean(p))
2527
+ }
2528
+ };
2529
+ if (toolInvocations !== void 0) {
2530
+ v2Msg.content.toolInvocations = toolInvocations;
2531
+ }
2532
+ if (v3Msg.content.metadata) {
2533
+ const { __originalContent, __originalExperimentalAttachments, __attachmentUrls, ...userMetadata } = v3Msg.content.metadata;
2534
+ v2Msg.content.metadata = userMetadata;
2535
+ }
2536
+ const originalContent = v3Msg.content.metadata?.__originalContent;
2537
+ if (originalContent !== void 0) {
2538
+ const hasOnlyTextOrStepStart = v2Msg.content.parts.every((p) => p.type === `step-start` || p.type === `text`);
2539
+ if (typeof originalContent === `string` || hasOnlyTextOrStepStart) {
2540
+ v2Msg.content.content = originalContent;
2541
+ }
2542
+ }
2543
+ const originalAttachments = v3Msg.content.metadata?.__originalExperimentalAttachments;
2544
+ if (originalAttachments && Array.isArray(originalAttachments)) {
2545
+ v2Msg.content.experimental_attachments = originalAttachments || [];
2546
+ }
2547
+ const originalContentIsV5 = Array.isArray(v3Msg.content.metadata?.__originalContent) && v3Msg.content.metadata?.__originalContent.some((part) => part.type === "file");
2548
+ const urlFileParts = originalContentIsV5 ? [] : v2Msg.content.parts.filter(
2549
+ (p) => p.type === "file" && typeof p.data === "string" && (p.data.startsWith("http://") || p.data.startsWith("https://")) && !p.providerMetadata
2550
+ // Don't move if it has providerMetadata (needed for roundtrip)
2551
+ );
2552
+ if (urlFileParts.length > 0) {
2553
+ if (!v2Msg.content.experimental_attachments) {
2554
+ v2Msg.content.experimental_attachments = [];
2555
+ }
2556
+ for (const urlPart of urlFileParts) {
2557
+ if (urlPart.type === "file") {
2558
+ v2Msg.content.experimental_attachments.push({
2559
+ url: urlPart.data,
2560
+ contentType: urlPart.mimeType
2561
+ });
2562
+ }
2563
+ }
2564
+ v2Msg.content.parts = v2Msg.content.parts.filter(
2565
+ (p) => !(p.type === "file" && typeof p.data === "string" && (p.data.startsWith("http://") || p.data.startsWith("https://")) && !p.providerMetadata)
2566
+ );
2567
+ }
2568
+ if (toolInvocations && toolInvocations.length > 0) {
2569
+ const resultToolInvocations = toolInvocations.filter((t) => t.state === "result");
2570
+ if (resultToolInvocations.length > 0) {
2571
+ v2Msg.content.toolInvocations = resultToolInvocations;
2572
+ }
2573
+ }
2574
+ if (v3Msg.type) v2Msg.type = v3Msg.type;
2575
+ return v2Msg;
2576
+ }
2577
+ mastraMessageV2ToMastraMessageV3(v2Msg) {
2578
+ const parts = [];
2579
+ const v3Msg = {
2580
+ id: v2Msg.id,
2581
+ content: {
2582
+ format: 3,
2583
+ parts
2584
+ },
2585
+ role: v2Msg.role,
2586
+ createdAt: v2Msg.createdAt instanceof Date ? v2Msg.createdAt : new Date(v2Msg.createdAt),
2587
+ resourceId: v2Msg.resourceId,
2588
+ threadId: v2Msg.threadId,
2589
+ type: v2Msg.type
2590
+ };
2591
+ if (v2Msg.content.metadata) {
2592
+ v3Msg.content.metadata = { ...v2Msg.content.metadata };
2593
+ }
2594
+ if (v2Msg.content.providerMetadata) {
2595
+ v3Msg.content.providerMetadata = v2Msg.content.providerMetadata;
2596
+ }
2597
+ if (v2Msg.content.content !== void 0) {
2598
+ v3Msg.content.metadata = {
2599
+ ...v3Msg.content.metadata || {},
2600
+ __originalContent: v2Msg.content.content
2601
+ };
2602
+ }
2603
+ if (v2Msg.content.experimental_attachments !== void 0) {
2604
+ v3Msg.content.metadata = {
2605
+ ...v3Msg.content.metadata || {},
2606
+ __originalExperimentalAttachments: v2Msg.content.experimental_attachments
2607
+ };
2608
+ }
2609
+ const fileUrls = /* @__PURE__ */ new Set();
2610
+ for (const part of v2Msg.content.parts) {
2611
+ switch (part.type) {
2612
+ case "step-start":
2613
+ case "text":
2614
+ parts.push(part);
2615
+ break;
2616
+ case "tool-invocation":
2617
+ if (part.toolInvocation.state === `result`) {
2618
+ parts.push({
2619
+ type: `tool-${part.toolInvocation.toolName}`,
2620
+ toolCallId: part.toolInvocation.toolCallId,
2621
+ state: "output-available",
2622
+ input: part.toolInvocation.args,
2623
+ output: part.toolInvocation.result,
2624
+ callProviderMetadata: part.providerMetadata
2625
+ });
2626
+ } else {
2627
+ const toolPart = {
2628
+ type: `tool-${part.toolInvocation.toolName}`,
2629
+ toolCallId: part.toolInvocation.toolCallId,
2630
+ state: part.toolInvocation.state === `call` ? `input-available` : `input-streaming`,
2631
+ input: part.toolInvocation.args
2632
+ };
2633
+ if (part.providerMetadata) {
2634
+ toolPart.callProviderMetadata = part.providerMetadata;
2635
+ }
2636
+ parts.push(toolPart);
2637
+ }
2638
+ break;
2639
+ case "source":
2640
+ parts.push({
2641
+ type: "source-url",
2642
+ sourceId: part.source.id,
2643
+ url: part.source.url,
2644
+ title: part.source.title,
2645
+ providerMetadata: part.source.providerMetadata || part.providerMetadata
2646
+ });
2647
+ break;
2648
+ case "reasoning":
2649
+ const text = part.reasoning || (part.details?.reduce((p, c) => {
2650
+ if (c.type === `text`) return p + c.text;
2651
+ return p;
2652
+ }, "") ?? "");
2653
+ if (text || part.details?.length || part.providerMetadata) {
2654
+ parts.push({
2655
+ type: "reasoning",
2656
+ text: text || "",
2657
+ state: "done",
2658
+ providerMetadata: part.providerMetadata
2659
+ });
2660
+ }
2661
+ break;
2662
+ case "file": {
2663
+ const categorized = typeof part.data === "string" ? categorizeFileData(part.data, part.mimeType) : { type: "raw", mimeType: part.mimeType};
2664
+ if (categorized.type === "url" && typeof part.data === "string") {
2665
+ parts.push({
2666
+ type: "file",
2667
+ url: part.data,
2668
+ mediaType: categorized.mimeType || "image/png",
2669
+ ...part.providerMetadata ? { providerMetadata: part.providerMetadata } : {}
2670
+ });
2671
+ fileUrls.add(part.data);
2672
+ } else {
2673
+ let filePartData;
2674
+ let extractedMimeType = part.mimeType;
2675
+ if (typeof part.data === "string") {
2676
+ const parsed = parseDataUri(part.data);
2677
+ if (parsed.isDataUri) {
2678
+ filePartData = parsed.base64Content;
2679
+ if (parsed.mimeType) {
2680
+ extractedMimeType = extractedMimeType || parsed.mimeType;
2681
+ }
2682
+ } else {
2683
+ filePartData = part.data;
2684
+ }
2685
+ } else {
2686
+ filePartData = part.data;
2687
+ }
2688
+ const finalMimeType = extractedMimeType || "image/png";
2689
+ let dataUri;
2690
+ if (typeof filePartData === "string" && filePartData.startsWith("data:")) {
2691
+ dataUri = filePartData;
2692
+ } else {
2693
+ dataUri = createDataUri(filePartData, finalMimeType);
2694
+ }
2695
+ parts.push({
2696
+ type: "file",
2697
+ url: dataUri,
2698
+ // Use url field with data URI
2699
+ mediaType: finalMimeType,
2700
+ ...part.providerMetadata ? { providerMetadata: part.providerMetadata } : {}
2701
+ });
2702
+ }
2703
+ fileUrls.add(part.data);
2704
+ break;
2705
+ }
2706
+ }
2707
+ }
2708
+ if (v2Msg.content.content && !v3Msg.content.parts?.some((p) => p.type === `text`)) {
2709
+ v3Msg.content.parts.push({ type: "text", text: v2Msg.content.content });
2710
+ }
2711
+ const attachmentUrls = [];
2712
+ if (v2Msg.content.experimental_attachments?.length) {
2713
+ for (const attachment of v2Msg.content.experimental_attachments) {
2714
+ if (fileUrls.has(attachment.url)) continue;
2715
+ attachmentUrls.push(attachment.url);
2716
+ parts.push({
2717
+ url: attachment.url,
2718
+ mediaType: attachment.contentType || "unknown",
2719
+ type: "file"
2720
+ });
2721
+ }
2722
+ }
2723
+ if (attachmentUrls.length > 0) {
2724
+ v3Msg.content.metadata = {
2725
+ ...v3Msg.content.metadata || {},
2726
+ __attachmentUrls: attachmentUrls
2727
+ };
2728
+ }
2729
+ return v3Msg;
2730
+ }
2731
+ aiV5UIMessagesToAIV5ModelMessages(messages) {
2732
+ const sanitized = this.sanitizeV5UIMessages(messages);
2733
+ const preprocessed = this.addStartStepPartsForAIV5(sanitized);
2734
+ const result = AIV5.convertToModelMessages(preprocessed);
2735
+ const finalResult = result.map((modelMsg, index) => {
2736
+ const uiMsg = preprocessed[index];
2737
+ let updatedMsg = modelMsg;
2738
+ if (uiMsg?.metadata && typeof uiMsg.metadata === "object" && "providerMetadata" in uiMsg.metadata && uiMsg.metadata.providerMetadata) {
2739
+ updatedMsg = {
2740
+ ...updatedMsg,
2741
+ providerOptions: uiMsg.metadata.providerMetadata
2742
+ };
2743
+ }
2744
+ if (updatedMsg.role === "assistant" && Array.isArray(updatedMsg.content)) {
2745
+ const updatedContent = updatedMsg.content.map((part, partIndex) => {
2746
+ if (part.type === "tool-call") {
2747
+ const uiPart = uiMsg?.parts[partIndex];
2748
+ if (uiPart && "callProviderMetadata" in uiPart && uiPart.callProviderMetadata) {
2749
+ return {
2750
+ ...part,
2751
+ providerOptions: uiPart.callProviderMetadata
2752
+ };
2753
+ }
2754
+ }
2755
+ if (part.type === "reasoning") {
2756
+ const uiPart = uiMsg?.parts[partIndex];
2757
+ if (uiPart && "providerMetadata" in uiPart && uiPart.providerMetadata) {
2758
+ return {
2759
+ ...part,
2760
+ providerOptions: uiPart.providerMetadata
2761
+ };
2762
+ }
2763
+ }
2764
+ return part;
2765
+ });
2766
+ return {
2767
+ ...updatedMsg,
2768
+ content: updatedContent
2769
+ };
2770
+ }
2771
+ return updatedMsg;
2772
+ });
2773
+ return finalResult;
2774
+ }
2775
+ addStartStepPartsForAIV5(messages) {
2776
+ for (const message of messages) {
2777
+ if (message.role !== `assistant`) continue;
2778
+ for (const [index, part] of message.parts.entries()) {
2779
+ if (!AIV5.isToolUIPart(part)) continue;
2780
+ const nextPart = message.parts.at(index + 1);
2781
+ if (nextPart && nextPart.type !== `step-start` && !AIV5.isToolUIPart(nextPart)) {
2782
+ message.parts.splice(index + 1, 0, { type: "step-start" });
2783
+ }
2784
+ }
2785
+ }
2786
+ return messages;
2787
+ }
2788
+ sanitizeV5UIMessages(messages) {
2789
+ const msgs = messages.map((m) => {
2790
+ if (m.parts.length === 0) return false;
2791
+ const safeParts = m.parts.filter((p) => {
2792
+ if (!AIV5.isToolUIPart(p)) return true;
2793
+ return p.state === "output-available" || p.state === "output-error";
2794
+ });
2795
+ if (!safeParts.length) return false;
2796
+ const sanitized = {
2797
+ ...m,
2798
+ parts: safeParts.map((part) => {
2799
+ if (AIV5.isToolUIPart(part) && part.state === "output-available") {
2800
+ return {
2801
+ ...part,
2802
+ output: typeof part.output === "object" && part.output && "value" in part.output ? part.output.value : part.output
2803
+ };
2804
+ }
2805
+ return part;
2806
+ })
2807
+ };
2808
+ return sanitized;
2809
+ }).filter((m) => Boolean(m));
2810
+ return msgs;
2811
+ }
2812
+ static mastraMessageV3ToAIV5UIMessage(m) {
2813
+ const { __originalContent, ...cleanMetadata } = m.content.metadata || {};
2814
+ const metadata = {
2815
+ ...cleanMetadata
2816
+ };
2817
+ if (m.createdAt) metadata.createdAt = m.createdAt;
2818
+ if (m.threadId) metadata.threadId = m.threadId;
2819
+ if (m.resourceId) metadata.resourceId = m.resourceId;
2820
+ if (m.content.providerMetadata) metadata.providerMetadata = m.content.providerMetadata;
2821
+ const filteredParts = m.content.parts;
2822
+ return {
2823
+ id: m.id,
2824
+ role: m.role,
2825
+ metadata,
2826
+ parts: filteredParts
2827
+ };
2828
+ }
2829
+ aiV5ModelMessagesToAIV4CoreMessages(messages, messageSource) {
2830
+ const v3 = messages.map((msg) => this.aiV5ModelMessageToMastraMessageV3(msg, messageSource));
2831
+ const v2 = v3.map(_MessageList.mastraMessageV3ToV2);
2832
+ const ui = v2.map(_MessageList.mastraMessageV2ToAIV4UIMessage);
2833
+ const core = this.aiV4UIMessagesToAIV4CoreMessages(ui);
2834
+ return core;
2835
+ }
2836
+ aiV4CoreMessagesToAIV5ModelMessages(messages, source) {
2837
+ return this.aiV5UIMessagesToAIV5ModelMessages(
2838
+ messages.map((m) => this.aiV4CoreMessageToMastraMessageV2(m, source)).map((m) => this.mastraMessageV2ToMastraMessageV3(m)).map((m) => _MessageList.mastraMessageV3ToAIV5UIMessage(m))
2839
+ );
2840
+ }
2841
+ aiV5UIMessageToMastraMessageV3(message, messageSource) {
2842
+ const content = {
2843
+ format: 3,
2844
+ parts: message.parts,
2845
+ metadata: message.metadata
2846
+ };
2847
+ const metadata = message.metadata;
2848
+ const createdAt = (() => {
2849
+ if ("createdAt" in message && message.createdAt instanceof Date) {
2850
+ return message.createdAt;
2851
+ }
2852
+ if (metadata && "createdAt" in metadata && metadata.createdAt instanceof Date) {
2853
+ return metadata.createdAt;
2854
+ }
2855
+ return void 0;
2856
+ })();
2857
+ if ("metadata" in message && message.metadata) {
2858
+ content.metadata = { ...message.metadata };
2859
+ }
2860
+ return {
2861
+ id: message.id || this.newMessageId(),
2862
+ role: _MessageList.getRole(message),
2863
+ createdAt: this.generateCreatedAt(messageSource, createdAt),
2864
+ threadId: this.memoryInfo?.threadId,
2865
+ resourceId: this.memoryInfo?.resourceId,
2866
+ content
2867
+ };
2868
+ }
2869
+ aiV5ModelMessageToMastraMessageV3(coreMessage, messageSource) {
2870
+ const id = `id` in coreMessage && typeof coreMessage.id === `string` ? coreMessage.id : this.newMessageId();
2871
+ const parts = [];
2872
+ if (typeof coreMessage.content === "string") {
2873
+ parts.push({
2874
+ type: "text",
2875
+ text: coreMessage.content,
2876
+ // Preserve providerOptions from ModelMessage level (e.g., system messages with cacheControl)
2877
+ ..."providerOptions" in coreMessage && coreMessage.providerOptions ? { providerMetadata: coreMessage.providerOptions } : {}
2878
+ });
2879
+ } else if (Array.isArray(coreMessage.content)) {
2880
+ for (const part of coreMessage.content) {
2881
+ switch (part.type) {
2882
+ case "text":
2883
+ const prevPart = parts.at(-1);
2884
+ if (coreMessage.role === "assistant" && prevPart && AIV5.isToolUIPart(prevPart) && prevPart.state === "output-available") {
2885
+ parts.push({
2886
+ type: "step-start"
2887
+ });
2888
+ }
2889
+ const mergedProviderMetadataV3 = {
2890
+ ..."providerOptions" in coreMessage && coreMessage.providerOptions ? coreMessage.providerOptions : {},
2891
+ ...part.providerOptions || {}
2892
+ };
2893
+ parts.push({
2894
+ type: "text",
2895
+ text: part.text,
2896
+ ...Object.keys(mergedProviderMetadataV3).length > 0 ? { providerMetadata: mergedProviderMetadataV3 } : {}
2897
+ });
2898
+ break;
2899
+ case "tool-call":
2900
+ parts.push({
2901
+ type: `tool-${part.toolName}`,
2902
+ state: "input-available",
2903
+ toolCallId: part.toolCallId,
2904
+ input: part.input
2905
+ });
2906
+ break;
2907
+ case "tool-result":
2908
+ parts.push({
2909
+ type: `tool-${part.toolName}`,
2910
+ state: "output-available",
2911
+ toolCallId: part.toolCallId,
2912
+ output: typeof part.output === "string" ? { type: "text", value: part.output } : part.output ?? { type: "text", value: "" },
2913
+ input: {},
2914
+ callProviderMetadata: part.providerOptions
2915
+ });
2916
+ break;
2917
+ case "reasoning":
2918
+ parts.push({
2919
+ type: "reasoning",
2920
+ text: part.text,
2921
+ providerMetadata: part.providerOptions
2922
+ });
2923
+ break;
2924
+ case "image": {
2925
+ let imageData;
2926
+ let extractedMimeType = part.mediaType;
2927
+ const imageStr = imageContentToDataUri(part.image, extractedMimeType || "image/png");
2928
+ const parsed = parseDataUri(imageStr);
2929
+ if (parsed.isDataUri) {
2930
+ imageData = parsed.base64Content;
2931
+ if (!extractedMimeType && parsed.mimeType) {
2932
+ extractedMimeType = parsed.mimeType;
2933
+ }
2934
+ } else if (imageStr.startsWith("http://") || imageStr.startsWith("https://")) {
2935
+ parts.push({
2936
+ type: "file",
2937
+ url: imageStr,
2938
+ mediaType: part.mediaType || "image/jpeg",
2939
+ // Default to image/jpeg for URLs
2940
+ providerMetadata: part.providerOptions
2941
+ });
2942
+ break;
2943
+ } else {
2944
+ imageData = imageStr;
2945
+ }
2946
+ const finalMimeType = extractedMimeType || "image/jpeg";
2947
+ const dataUri = imageData.startsWith("data:") ? imageData : createDataUri(imageData, finalMimeType);
2948
+ parts.push({
2949
+ type: "file",
2950
+ url: dataUri,
2951
+ mediaType: finalMimeType,
2952
+ providerMetadata: part.providerOptions
2953
+ });
2954
+ break;
2955
+ }
2956
+ case "file": {
2957
+ if (part.data instanceof URL) {
2958
+ const urlStr = part.data.toString();
2959
+ let extractedMimeType = part.mediaType;
2960
+ const parsed = parseDataUri(urlStr);
2961
+ if (parsed.isDataUri) {
2962
+ if (!extractedMimeType && parsed.mimeType) {
2963
+ extractedMimeType = parsed.mimeType;
2964
+ }
2965
+ if (parsed.base64Content !== urlStr) {
2966
+ const dataUri = createDataUri(parsed.base64Content, extractedMimeType || "image/png");
2967
+ parts.push({
2968
+ type: "file",
2969
+ url: dataUri,
2970
+ mediaType: extractedMimeType || "image/png",
2971
+ providerMetadata: part.providerOptions
2972
+ });
2973
+ } else {
2974
+ parts.push({
2975
+ type: "file",
2976
+ url: urlStr,
2977
+ mediaType: part.mediaType || "image/png",
2978
+ providerMetadata: part.providerOptions
2979
+ });
2980
+ }
2981
+ } else {
2982
+ parts.push({
2983
+ type: "file",
2984
+ url: urlStr,
2985
+ mediaType: part.mediaType || "application/octet-stream",
2986
+ providerMetadata: part.providerOptions
2987
+ });
2988
+ }
2989
+ } else if (typeof part.data === "string") {
2990
+ const categorized = categorizeFileData(part.data, part.mediaType);
2991
+ if (categorized.type === "url" || categorized.type === "dataUri") {
2992
+ parts.push({
2993
+ type: "file",
2994
+ url: part.data,
2995
+ mediaType: categorized.mimeType || "application/octet-stream",
2996
+ providerMetadata: part.providerOptions
2997
+ });
2998
+ } else {
2999
+ try {
3000
+ const base64Data = convertDataContentToBase64String(part.data);
3001
+ const dataUri = createDataUri(base64Data, categorized.mimeType || "image/png");
3002
+ parts.push({
3003
+ type: "file",
3004
+ url: dataUri,
3005
+ mediaType: categorized.mimeType || "image/png",
3006
+ providerMetadata: part.providerOptions
3007
+ });
3008
+ } catch (error) {
3009
+ console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
3010
+ }
3011
+ }
3012
+ } else {
3013
+ try {
3014
+ const base64Data = convertDataContentToBase64String(part.data);
3015
+ const dataUri = createDataUri(base64Data, part.mediaType || "image/png");
3016
+ parts.push({
3017
+ type: "file",
3018
+ url: dataUri,
3019
+ mediaType: part.mediaType || "image/png",
3020
+ providerMetadata: part.providerOptions
3021
+ });
3022
+ } catch (error) {
3023
+ console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
3024
+ }
3025
+ }
3026
+ break;
3027
+ }
3028
+ }
3029
+ }
3030
+ }
3031
+ const content = {
3032
+ format: 3,
3033
+ parts
3034
+ };
3035
+ if ("metadata" in coreMessage && coreMessage.metadata !== null && coreMessage.metadata !== void 0) {
3036
+ content.metadata = {
3037
+ ...content.metadata || {},
3038
+ ...coreMessage.metadata
3039
+ };
3040
+ }
3041
+ if (coreMessage.content) {
3042
+ content.metadata = {
3043
+ ...content.metadata || {},
3044
+ __originalContent: coreMessage.content
3045
+ };
3046
+ }
3047
+ return {
3048
+ id,
3049
+ role: _MessageList.getRole(coreMessage),
3050
+ createdAt: this.generateCreatedAt(messageSource),
3051
+ threadId: this.memoryInfo?.threadId,
3052
+ resourceId: this.memoryInfo?.resourceId,
3053
+ content
3054
+ };
3055
+ }
3056
+ static hasAIV5UIMessageCharacteristics(msg) {
3057
+ if (`toolInvocations` in msg || `reasoning` in msg || `experimental_attachments` in msg || `data` in msg || `annotations` in msg)
3058
+ return false;
3059
+ if (!msg.parts) return false;
3060
+ for (const part of msg.parts) {
3061
+ if (`metadata` in part) return true;
3062
+ if (`toolInvocation` in part) return false;
3063
+ if (`toolCallId` in part) return true;
3064
+ if (part.type === `source`) return false;
3065
+ if (part.type === `source-url`) return true;
3066
+ if (part.type === `reasoning`) {
3067
+ if (`state` in part || `text` in part) return true;
3068
+ if (`reasoning` in part || `details` in part) return false;
3069
+ }
3070
+ if (part.type === `file` && `mediaType` in part) return true;
3071
+ }
3072
+ return false;
3073
+ }
3074
+ static isAIV5UIMessage(msg) {
3075
+ return !_MessageList.isMastraMessage(msg) && !_MessageList.isAIV5CoreMessage(msg) && `parts` in msg && _MessageList.hasAIV5UIMessageCharacteristics(msg);
3076
+ }
3077
+ static hasAIV5CoreMessageCharacteristics(msg) {
3078
+ if (`experimental_providerMetadata` in msg) return false;
3079
+ if (typeof msg.content === `string`) return false;
3080
+ for (const part of msg.content) {
3081
+ if (part.type === `tool-result` && `output` in part) return true;
3082
+ if (part.type === `tool-call` && `input` in part) return true;
3083
+ if (part.type === `tool-result` && `result` in part) return false;
3084
+ if (part.type === `tool-call` && `args` in part) return false;
3085
+ if (`mediaType` in part) return true;
3086
+ if (`mimeType` in part) return false;
3087
+ if (`experimental_providerMetadata` in part) return false;
3088
+ if (part.type === `reasoning` && `signature` in part) return false;
3089
+ if (part.type === `redacted-reasoning`) return false;
3090
+ }
3091
+ return false;
3092
+ }
3093
+ static cacheKeyFromAIV5Parts(parts) {
3094
+ let key = ``;
3095
+ for (const part of parts) {
3096
+ key += part.type;
3097
+ if (part.type === `text`) {
3098
+ key += part.text;
3099
+ }
3100
+ if (AIV5.isToolUIPart(part) || part.type === "dynamic-tool") {
3101
+ key += part.toolCallId;
3102
+ key += part.state;
3103
+ }
3104
+ if (part.type === `reasoning`) {
3105
+ key += part.text;
3106
+ }
3107
+ if (part.type === `file`) {
3108
+ key += part.url.length;
3109
+ key += part.mediaType;
3110
+ key += part.filename || "";
3111
+ }
3112
+ }
3113
+ return key;
3114
+ }
3115
+ static cacheKeyFromAIV5ModelMessageContent(content) {
3116
+ if (typeof content === `string`) return content;
3117
+ let key = ``;
3118
+ for (const part of content) {
3119
+ key += part.type;
3120
+ if (part.type === `text`) {
3121
+ key += part.text.length;
3122
+ }
3123
+ if (part.type === `reasoning`) {
3124
+ key += part.text.length;
3125
+ }
3126
+ if (part.type === `tool-call`) {
3127
+ key += part.toolCallId;
3128
+ key += part.toolName;
3129
+ }
3130
+ if (part.type === `tool-result`) {
3131
+ key += part.toolCallId;
3132
+ key += part.toolName;
3133
+ }
3134
+ if (part.type === `file`) {
3135
+ key += part.filename;
3136
+ key += part.mediaType;
3137
+ }
3138
+ if (part.type === `image`) {
3139
+ key += getImageCacheKey(part.image);
3140
+ key += part.mediaType;
3141
+ }
3142
+ }
3143
+ return key;
3144
+ }
3145
+ };
3146
+
3147
+ // src/agent/message-list/utils/convert-messages.ts
3148
+ var MessageConverter = class {
3149
+ messageList;
3150
+ constructor(messages) {
3151
+ this.messageList = new MessageList();
3152
+ this.messageList.add(messages, "memory");
3153
+ }
3154
+ to(format) {
3155
+ switch (format) {
3156
+ case "Mastra.V2":
3157
+ return this.messageList.get.all.v2();
3158
+ case "AIV4.UI":
3159
+ return this.messageList.get.all.aiV4.ui();
3160
+ case "AIV4.Core":
3161
+ return this.messageList.get.all.aiV4.core();
3162
+ case "AIV5.UI":
3163
+ return this.messageList.get.all.aiV5.ui();
3164
+ case "AIV5.Model":
3165
+ return this.messageList.get.all.aiV5.model();
3166
+ default:
3167
+ throw new Error(`Unsupported output format: ${format}`);
3168
+ }
3169
+ }
3170
+ };
3171
+ function convertMessages(messages) {
3172
+ return new MessageConverter(messages);
3173
+ }
3174
+
3175
+ export { DefaultGeneratedFile, DefaultGeneratedFileWithType, MessageList, convertMessages };
3176
+ //# sourceMappingURL=chunk-Q6LWNLAJ.js.map
3177
+ //# sourceMappingURL=chunk-Q6LWNLAJ.js.map