@mastra/core 0.13.2 → 0.14.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. package/README.md +5 -44
  2. package/dist/agent/agent.types.d.ts +12 -2
  3. package/dist/agent/agent.types.d.ts.map +1 -1
  4. package/dist/agent/index.cjs +14 -14
  5. package/dist/agent/index.d.ts +17 -17
  6. package/dist/agent/index.d.ts.map +1 -1
  7. package/dist/agent/index.js +2 -2
  8. package/dist/agent/input-processor/index.cjs +28 -0
  9. package/dist/agent/input-processor/index.js +3 -0
  10. package/dist/agent/input-processor/processors/index.d.ts +4 -4
  11. package/dist/agent/input-processor/processors/index.d.ts.map +1 -1
  12. package/dist/agent/input-processor/processors/language-detector.d.ts +6 -139
  13. package/dist/agent/input-processor/processors/language-detector.d.ts.map +1 -1
  14. package/dist/agent/input-processor/processors/moderation.d.ts +5 -94
  15. package/dist/agent/input-processor/processors/moderation.d.ts.map +1 -1
  16. package/dist/agent/input-processor/processors/pii-detector.d.ts +6 -164
  17. package/dist/agent/input-processor/processors/pii-detector.d.ts.map +1 -1
  18. package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts +6 -96
  19. package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts.map +1 -1
  20. package/dist/agent/input-processor/processors/unicode-normalizer.d.ts +9 -25
  21. package/dist/agent/input-processor/processors/unicode-normalizer.d.ts.map +1 -1
  22. package/dist/agent/message-list/index.d.ts +138 -39
  23. package/dist/agent/message-list/index.d.ts.map +1 -1
  24. package/dist/agent/message-list/types.d.ts +3 -0
  25. package/dist/agent/message-list/types.d.ts.map +1 -0
  26. package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts +3 -0
  27. package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts.map +1 -0
  28. package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts +3 -0
  29. package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts.map +1 -0
  30. package/dist/agent/message-list/utils/ai-v5/tool.d.ts +16 -0
  31. package/dist/agent/message-list/utils/ai-v5/tool.d.ts.map +1 -0
  32. package/dist/agent/types.d.ts +34 -4
  33. package/dist/agent/types.d.ts.map +1 -1
  34. package/dist/ai-tracing/base.d.ts +22 -7
  35. package/dist/ai-tracing/base.d.ts.map +1 -1
  36. package/dist/ai-tracing/default.d.ts +3 -3
  37. package/dist/ai-tracing/default.d.ts.map +1 -1
  38. package/dist/ai-tracing/index.cjs +253 -72
  39. package/dist/ai-tracing/index.cjs.map +1 -1
  40. package/dist/ai-tracing/index.d.ts +1 -0
  41. package/dist/ai-tracing/index.d.ts.map +1 -1
  42. package/dist/ai-tracing/index.js +243 -71
  43. package/dist/ai-tracing/index.js.map +1 -1
  44. package/dist/ai-tracing/no-op.d.ts +39 -7
  45. package/dist/ai-tracing/no-op.d.ts.map +1 -1
  46. package/dist/ai-tracing/registry.d.ts +29 -4
  47. package/dist/ai-tracing/registry.d.ts.map +1 -1
  48. package/dist/ai-tracing/types.d.ts +112 -53
  49. package/dist/ai-tracing/types.d.ts.map +1 -1
  50. package/dist/ai-tracing/utils.d.ts +24 -0
  51. package/dist/ai-tracing/utils.d.ts.map +1 -0
  52. package/dist/base.cjs +2 -2
  53. package/dist/base.js +1 -1
  54. package/dist/bundler/index.cjs +2 -2
  55. package/dist/bundler/index.js +1 -1
  56. package/dist/{chunk-N3VGOJZV.cjs → chunk-4BMFOQDC.cjs} +27 -47
  57. package/dist/chunk-4BMFOQDC.cjs.map +1 -0
  58. package/dist/{chunk-MTRRRTB4.js → chunk-4XDSHUFK.js} +7 -6
  59. package/dist/chunk-4XDSHUFK.js.map +1 -0
  60. package/dist/{chunk-SSJVFUBZ.js → chunk-67L5DRLO.js} +3 -3
  61. package/dist/{chunk-SSJVFUBZ.js.map → chunk-67L5DRLO.js.map} +1 -1
  62. package/dist/{chunk-FUERFM46.js → chunk-6GF5M4GX.js} +3 -3
  63. package/dist/{chunk-FUERFM46.js.map → chunk-6GF5M4GX.js.map} +1 -1
  64. package/dist/{chunk-7E2SNI5D.cjs → chunk-7AXX55C5.cjs} +5 -5
  65. package/dist/chunk-7AXX55C5.cjs.map +1 -0
  66. package/dist/{chunk-6W6LYR7M.js → chunk-7XGDFDZ2.js} +3 -3
  67. package/dist/{chunk-6W6LYR7M.js.map → chunk-7XGDFDZ2.js.map} +1 -1
  68. package/dist/{chunk-R4HT5XUH.cjs → chunk-ASJVWHIK.cjs} +993 -124
  69. package/dist/chunk-ASJVWHIK.cjs.map +1 -0
  70. package/dist/{chunk-ZDZ57MIU.js → chunk-AWILTHBM.js} +3 -3
  71. package/dist/{chunk-ZDZ57MIU.js.map → chunk-AWILTHBM.js.map} +1 -1
  72. package/dist/{chunk-DDYSOZ25.js → chunk-AYXRNQH3.js} +3 -3
  73. package/dist/{chunk-DDYSOZ25.js.map → chunk-AYXRNQH3.js.map} +1 -1
  74. package/dist/{chunk-U2CK6AN5.cjs → chunk-BCCEYWAV.cjs} +4 -4
  75. package/dist/{chunk-U2CK6AN5.cjs.map → chunk-BCCEYWAV.cjs.map} +1 -1
  76. package/dist/{chunk-MH64VYGF.cjs → chunk-CSTWQQ3C.cjs} +14 -13
  77. package/dist/chunk-CSTWQQ3C.cjs.map +1 -0
  78. package/dist/{chunk-FTPL7ILZ.js → chunk-CWZDKGJ6.js} +3 -3
  79. package/dist/{chunk-FTPL7ILZ.js.map → chunk-CWZDKGJ6.js.map} +1 -1
  80. package/dist/{chunk-MVSUZXER.cjs → chunk-D7P76YH6.cjs} +4 -4
  81. package/dist/{chunk-MVSUZXER.cjs.map → chunk-D7P76YH6.cjs.map} +1 -1
  82. package/dist/{chunk-IL5SHDY4.js → chunk-DAMIFZPW.js} +949 -103
  83. package/dist/chunk-DAMIFZPW.js.map +1 -0
  84. package/dist/{chunk-KLXRYVVQ.cjs → chunk-DVI6XHUC.cjs} +4 -4
  85. package/dist/{chunk-KLXRYVVQ.cjs.map → chunk-DVI6XHUC.cjs.map} +1 -1
  86. package/dist/chunk-ELKY3FEM.cjs +14 -0
  87. package/dist/{chunk-IP5NGA2S.cjs.map → chunk-ELKY3FEM.cjs.map} +1 -1
  88. package/dist/{chunk-TOODGJKM.js → chunk-GZVSHXPP.js} +3 -3
  89. package/dist/chunk-GZVSHXPP.js.map +1 -0
  90. package/dist/{chunk-WA3SWCXD.cjs → chunk-I4XP6RAN.cjs} +9 -9
  91. package/dist/{chunk-WA3SWCXD.cjs.map → chunk-I4XP6RAN.cjs.map} +1 -1
  92. package/dist/{chunk-PLVSXEQP.cjs → chunk-IAP4IWKM.cjs} +4 -4
  93. package/dist/{chunk-PLVSXEQP.cjs.map → chunk-IAP4IWKM.cjs.map} +1 -1
  94. package/dist/{chunk-YT4RGZYO.cjs → chunk-KXCUCBEI.cjs} +8 -8
  95. package/dist/{chunk-YT4RGZYO.cjs.map → chunk-KXCUCBEI.cjs.map} +1 -1
  96. package/dist/{chunk-Z74LG5VH.cjs → chunk-LFAAEOQC.cjs} +21 -4
  97. package/dist/chunk-LFAAEOQC.cjs.map +1 -0
  98. package/dist/{chunk-DK7AVDMI.cjs → chunk-LVVAUDVQ.cjs} +4 -4
  99. package/dist/{chunk-DK7AVDMI.cjs.map → chunk-LVVAUDVQ.cjs.map} +1 -1
  100. package/dist/{chunk-HBMZEZTO.js → chunk-ON4S33NP.js} +3 -3
  101. package/dist/{chunk-HBMZEZTO.js.map → chunk-ON4S33NP.js.map} +1 -1
  102. package/dist/{chunk-L6YBPFYF.js → chunk-OPT2L5AM.js} +1089 -81
  103. package/dist/chunk-OPT2L5AM.js.map +1 -0
  104. package/dist/{chunk-N2KMAW6T.js → chunk-P7WF6NQU.js} +20 -4
  105. package/dist/chunk-P7WF6NQU.js.map +1 -0
  106. package/dist/{chunk-KEMW2BER.js → chunk-PY4MHJTE.js} +3 -3
  107. package/dist/{chunk-KEMW2BER.js.map → chunk-PY4MHJTE.js.map} +1 -1
  108. package/dist/{chunk-TZVJV7EV.js → chunk-QSJZLHWA.js} +5 -5
  109. package/dist/{chunk-TZVJV7EV.js.map → chunk-QSJZLHWA.js.map} +1 -1
  110. package/dist/{chunk-DCOKWJ5G.cjs → chunk-RJCNC57P.cjs} +1110 -79
  111. package/dist/chunk-RJCNC57P.cjs.map +1 -0
  112. package/dist/{chunk-LMW44O4V.cjs → chunk-RPV7GQAX.cjs} +4 -4
  113. package/dist/{chunk-LMW44O4V.cjs.map → chunk-RPV7GQAX.cjs.map} +1 -1
  114. package/dist/{chunk-TZOR5M7H.js → chunk-RYCRCJCY.js} +25 -45
  115. package/dist/chunk-RYCRCJCY.js.map +1 -0
  116. package/dist/{chunk-IDDUQR6P.cjs → chunk-V5WKCX3G.cjs} +3 -3
  117. package/dist/chunk-V5WKCX3G.cjs.map +1 -0
  118. package/dist/{chunk-6AR2Z5ZG.js → chunk-X3GXU6TZ.js} +3 -3
  119. package/dist/chunk-X3GXU6TZ.js.map +1 -0
  120. package/dist/deployer/index.cjs +2 -2
  121. package/dist/deployer/index.js +1 -1
  122. package/dist/index.cjs +74 -70
  123. package/dist/index.js +14 -14
  124. package/dist/llm/index.d.ts +2 -1
  125. package/dist/llm/index.d.ts.map +1 -1
  126. package/dist/llm/model/base.types.d.ts +2 -6
  127. package/dist/llm/model/base.types.d.ts.map +1 -1
  128. package/dist/llm/model/index.d.ts +0 -1
  129. package/dist/llm/model/index.d.ts.map +1 -1
  130. package/dist/llm/model/model.d.ts +3 -2
  131. package/dist/llm/model/model.d.ts.map +1 -1
  132. package/dist/llm/model/model.loop.d.ts +25 -0
  133. package/dist/llm/model/model.loop.d.ts.map +1 -0
  134. package/dist/llm/model/model.loop.types.d.ts +39 -0
  135. package/dist/llm/model/model.loop.types.d.ts.map +1 -0
  136. package/dist/llm/model/shared.types.d.ts +8 -0
  137. package/dist/llm/model/shared.types.d.ts.map +1 -0
  138. package/dist/logger/constants.d.ts +1 -1
  139. package/dist/logger/index.cjs +6 -6
  140. package/dist/logger/index.js +1 -1
  141. package/dist/loop/index.cjs +2848 -0
  142. package/dist/loop/index.cjs.map +1 -0
  143. package/dist/loop/index.d.ts +2 -0
  144. package/dist/loop/index.d.ts.map +1 -0
  145. package/dist/loop/index.js +2842 -0
  146. package/dist/loop/index.js.map +1 -0
  147. package/dist/loop/loop.d.ts +5 -0
  148. package/dist/loop/loop.d.ts.map +1 -0
  149. package/dist/loop/telemetry/index.d.ts +36 -0
  150. package/dist/loop/telemetry/index.d.ts.map +1 -0
  151. package/dist/loop/telemetry/noop.d.ts +3 -0
  152. package/dist/loop/telemetry/noop.d.ts.map +1 -0
  153. package/dist/loop/test-utils/fullStream.d.ts +6 -0
  154. package/dist/loop/test-utils/fullStream.d.ts.map +1 -0
  155. package/dist/loop/test-utils/generateText.d.ts +6 -0
  156. package/dist/loop/test-utils/generateText.d.ts.map +1 -0
  157. package/dist/loop/test-utils/mockTracer.d.ts +47 -0
  158. package/dist/loop/test-utils/mockTracer.d.ts.map +1 -0
  159. package/dist/loop/test-utils/options.d.ts +6 -0
  160. package/dist/loop/test-utils/options.d.ts.map +1 -0
  161. package/dist/loop/test-utils/resultObject.d.ts +6 -0
  162. package/dist/loop/test-utils/resultObject.d.ts.map +1 -0
  163. package/dist/loop/test-utils/streamObject.d.ts +6 -0
  164. package/dist/loop/test-utils/streamObject.d.ts.map +1 -0
  165. package/dist/loop/test-utils/telemetry.d.ts +6 -0
  166. package/dist/loop/test-utils/telemetry.d.ts.map +1 -0
  167. package/dist/loop/test-utils/textStream.d.ts +6 -0
  168. package/dist/loop/test-utils/textStream.d.ts.map +1 -0
  169. package/dist/loop/test-utils/toUIMessageStream.d.ts +6 -0
  170. package/dist/loop/test-utils/toUIMessageStream.d.ts.map +1 -0
  171. package/dist/loop/test-utils/tools.d.ts +6 -0
  172. package/dist/loop/test-utils/tools.d.ts.map +1 -0
  173. package/dist/loop/test-utils/utils.d.ts +40 -0
  174. package/dist/loop/test-utils/utils.d.ts.map +1 -0
  175. package/dist/loop/types.d.ts +68 -0
  176. package/dist/loop/types.d.ts.map +1 -0
  177. package/dist/loop/workflow/llm-execution.d.ts +80 -0
  178. package/dist/loop/workflow/llm-execution.d.ts.map +1 -0
  179. package/dist/loop/workflow/outer-llm-step.d.ts +43 -0
  180. package/dist/loop/workflow/outer-llm-step.d.ts.map +1 -0
  181. package/dist/loop/workflow/run-state.d.ts +24 -0
  182. package/dist/loop/workflow/run-state.d.ts.map +1 -0
  183. package/dist/loop/workflow/schema.d.ts +80 -0
  184. package/dist/loop/workflow/schema.d.ts.map +1 -0
  185. package/dist/loop/workflow/stream.d.ts +6 -0
  186. package/dist/loop/workflow/stream.d.ts.map +1 -0
  187. package/dist/loop/workflow/tool-call-step.d.ts +41 -0
  188. package/dist/loop/workflow/tool-call-step.d.ts.map +1 -0
  189. package/dist/mastra/index.cjs +2 -2
  190. package/dist/mastra/index.d.ts +4 -5
  191. package/dist/mastra/index.d.ts.map +1 -1
  192. package/dist/mastra/index.js +1 -1
  193. package/dist/mcp/index.cjs +4 -4
  194. package/dist/mcp/index.js +2 -2
  195. package/dist/memory/index.cjs +4 -4
  196. package/dist/memory/index.js +1 -1
  197. package/dist/network/index.cjs +6 -6
  198. package/dist/network/index.cjs.map +1 -1
  199. package/dist/network/index.js +3 -3
  200. package/dist/network/index.js.map +1 -1
  201. package/dist/network/network.d.ts +8 -7
  202. package/dist/network/network.d.ts.map +1 -1
  203. package/dist/network/vNext/index.cjs +21 -21
  204. package/dist/network/vNext/index.cjs.map +1 -1
  205. package/dist/network/vNext/index.d.ts +7 -7
  206. package/dist/network/vNext/index.d.ts.map +1 -1
  207. package/dist/network/vNext/index.js +7 -7
  208. package/dist/network/vNext/index.js.map +1 -1
  209. package/dist/processors/index.cjs +512 -0
  210. package/dist/processors/index.cjs.map +1 -0
  211. package/dist/processors/index.d.ts +38 -0
  212. package/dist/processors/index.d.ts.map +1 -0
  213. package/dist/processors/index.js +481 -0
  214. package/dist/processors/index.js.map +1 -0
  215. package/dist/processors/processors/batch-parts.d.ts +42 -0
  216. package/dist/processors/processors/batch-parts.d.ts.map +1 -0
  217. package/dist/processors/processors/index.d.ts +10 -0
  218. package/dist/processors/processors/index.d.ts.map +1 -0
  219. package/dist/processors/processors/language-detector.d.ts +151 -0
  220. package/dist/processors/processors/language-detector.d.ts.map +1 -0
  221. package/dist/processors/processors/moderation.d.ts +130 -0
  222. package/dist/processors/processors/moderation.d.ts.map +1 -0
  223. package/dist/processors/processors/pii-detector.d.ts +197 -0
  224. package/dist/processors/processors/pii-detector.d.ts.map +1 -0
  225. package/dist/processors/processors/prompt-injection-detector.d.ts +108 -0
  226. package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -0
  227. package/dist/processors/processors/structured-output.d.ts +42 -0
  228. package/dist/processors/processors/structured-output.d.ts.map +1 -0
  229. package/dist/processors/processors/system-prompt-scrubber.d.ts +92 -0
  230. package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -0
  231. package/dist/processors/processors/token-limiter.d.ts +66 -0
  232. package/dist/processors/processors/token-limiter.d.ts.map +1 -0
  233. package/dist/processors/processors/unicode-normalizer.d.ts +34 -0
  234. package/dist/processors/processors/unicode-normalizer.d.ts.map +1 -0
  235. package/dist/processors/runner.d.ts +41 -0
  236. package/dist/processors/runner.d.ts.map +1 -0
  237. package/dist/relevance/index.cjs +4 -4
  238. package/dist/relevance/index.js +1 -1
  239. package/dist/scores/index.cjs +5 -5
  240. package/dist/scores/index.js +2 -2
  241. package/dist/server/index.cjs +2 -2
  242. package/dist/server/index.js +1 -1
  243. package/dist/storage/domains/operations/base.d.ts.map +1 -1
  244. package/dist/storage/index.cjs +15 -17
  245. package/dist/storage/index.cjs.map +1 -1
  246. package/dist/storage/index.js +4 -6
  247. package/dist/storage/index.js.map +1 -1
  248. package/dist/stream/aisdk/v4/transform.d.ts +26 -0
  249. package/dist/stream/aisdk/v4/transform.d.ts.map +1 -1
  250. package/dist/stream/aisdk/v5/compat.d.ts +159 -0
  251. package/dist/stream/aisdk/v5/compat.d.ts.map +1 -0
  252. package/dist/stream/aisdk/v5/execute.d.ts +29 -0
  253. package/dist/stream/aisdk/v5/execute.d.ts.map +1 -0
  254. package/dist/stream/aisdk/v5/file.d.ts +38 -0
  255. package/dist/stream/aisdk/v5/file.d.ts.map +1 -0
  256. package/dist/stream/aisdk/v5/input.d.ts +15 -0
  257. package/dist/stream/aisdk/v5/input.d.ts.map +1 -0
  258. package/dist/stream/aisdk/v5/object/schema.d.ts +4 -0
  259. package/dist/stream/aisdk/v5/object/schema.d.ts.map +1 -0
  260. package/dist/stream/aisdk/v5/object/stream-object.d.ts +34 -0
  261. package/dist/stream/aisdk/v5/object/stream-object.d.ts.map +1 -0
  262. package/dist/stream/aisdk/v5/output-helpers.d.ts +76 -0
  263. package/dist/stream/aisdk/v5/output-helpers.d.ts.map +1 -0
  264. package/dist/stream/aisdk/v5/output.d.ts +119 -0
  265. package/dist/stream/aisdk/v5/output.d.ts.map +1 -0
  266. package/dist/stream/aisdk/v5/test-utils.d.ts +19 -0
  267. package/dist/stream/aisdk/v5/test-utils.d.ts.map +1 -0
  268. package/dist/stream/aisdk/v5/transform.d.ts +31 -0
  269. package/dist/stream/aisdk/v5/transform.d.ts.map +1 -0
  270. package/dist/stream/base/input.d.ts +1 -1
  271. package/dist/stream/base/output.d.ts +89 -0
  272. package/dist/stream/base/output.d.ts.map +1 -0
  273. package/dist/stream/types.d.ts +30 -0
  274. package/dist/stream/types.d.ts.map +1 -1
  275. package/dist/test-utils/llm-mock.cjs +2 -2
  276. package/dist/test-utils/llm-mock.cjs.map +1 -1
  277. package/dist/test-utils/llm-mock.d.ts +2 -2
  278. package/dist/test-utils/llm-mock.d.ts.map +1 -1
  279. package/dist/test-utils/llm-mock.js +2 -2
  280. package/dist/test-utils/llm-mock.js.map +1 -1
  281. package/dist/tools/tool-builder/builder.d.ts +3 -2
  282. package/dist/tools/tool-builder/builder.d.ts.map +1 -1
  283. package/dist/tools/types.d.ts +9 -0
  284. package/dist/tools/types.d.ts.map +1 -1
  285. package/dist/tts/index.cjs +2 -2
  286. package/dist/tts/index.js +1 -1
  287. package/dist/types.d.ts +2 -0
  288. package/dist/types.d.ts.map +1 -1
  289. package/dist/utils.cjs +19 -15
  290. package/dist/utils.d.ts +3 -2
  291. package/dist/utils.d.ts.map +1 -1
  292. package/dist/utils.js +1 -1
  293. package/dist/vector/index.cjs +2 -2
  294. package/dist/vector/index.js +1 -1
  295. package/dist/voice/index.cjs +4 -4
  296. package/dist/voice/index.js +1 -1
  297. package/dist/workflows/default.d.ts.map +1 -1
  298. package/dist/workflows/index.cjs +10 -10
  299. package/dist/workflows/index.js +1 -1
  300. package/dist/workflows/legacy/index.cjs +22 -22
  301. package/dist/workflows/legacy/index.js +1 -1
  302. package/loop.d.ts +1 -0
  303. package/package.json +16 -1
  304. package/processors.d.ts +1 -0
  305. package/dist/agent/input-processor/processors/index.cjs +0 -28
  306. package/dist/agent/input-processor/processors/index.js +0 -3
  307. package/dist/agent/input-processor/runner.d.ts +0 -4
  308. package/dist/agent/input-processor/runner.d.ts.map +0 -1
  309. package/dist/chunk-6AR2Z5ZG.js.map +0 -1
  310. package/dist/chunk-7E2SNI5D.cjs.map +0 -1
  311. package/dist/chunk-DCOKWJ5G.cjs.map +0 -1
  312. package/dist/chunk-IDDUQR6P.cjs.map +0 -1
  313. package/dist/chunk-IL5SHDY4.js.map +0 -1
  314. package/dist/chunk-IP5NGA2S.cjs +0 -14
  315. package/dist/chunk-L6YBPFYF.js.map +0 -1
  316. package/dist/chunk-MH64VYGF.cjs.map +0 -1
  317. package/dist/chunk-MTRRRTB4.js.map +0 -1
  318. package/dist/chunk-N2KMAW6T.js.map +0 -1
  319. package/dist/chunk-N3VGOJZV.cjs.map +0 -1
  320. package/dist/chunk-R4HT5XUH.cjs.map +0 -1
  321. package/dist/chunk-TOODGJKM.js.map +0 -1
  322. package/dist/chunk-TZOR5M7H.js.map +0 -1
  323. package/dist/chunk-Z74LG5VH.cjs.map +0 -1
  324. package/dist/llm/model/base.d.ts +0 -26
  325. package/dist/llm/model/base.d.ts.map +0 -1
  326. /package/dist/agent/input-processor/{processors/index.cjs.map → index.cjs.map} +0 -0
  327. /package/dist/agent/input-processor/{processors/index.js.map → index.js.map} +0 -0
@@ -1,19 +1,20 @@
1
- import { MastraLLM } from './chunk-TZOR5M7H.js';
2
- import { DefaultVoice } from './chunk-SSJVFUBZ.js';
3
- import { MessageList } from './chunk-L6YBPFYF.js';
1
+ import { DefaultVoice } from './chunk-67L5DRLO.js';
2
+ import { MastraLLMV1 } from './chunk-RYCRCJCY.js';
4
3
  import { InstrumentClass, Telemetry } from './chunk-RQBS6DFE.js';
4
+ import { MessageList } from './chunk-OPT2L5AM.js';
5
5
  import { executeHook } from './chunk-TTELJD4F.js';
6
- import { ensureToolProperties, makeCoreTool, createMastraProxy } from './chunk-N2KMAW6T.js';
6
+ import { ensureToolProperties, makeCoreTool, createMastraProxy } from './chunk-P7WF6NQU.js';
7
7
  import { RuntimeContext } from './chunk-HLRWYUFN.js';
8
8
  import { MastraError } from './chunk-4O4YNORN.js';
9
- import { MastraBase } from './chunk-FUERFM46.js';
10
- import { RegisteredLogger } from './chunk-6AR2Z5ZG.js';
9
+ import { MastraBase } from './chunk-6GF5M4GX.js';
10
+ import { RegisteredLogger } from './chunk-X3GXU6TZ.js';
11
11
  import { __commonJS, __toESM, __decoratorStart, __decorateElement, __runInitializers } from './chunk-3HXBPDKN.js';
12
12
  import { context, trace } from '@opentelemetry/api';
13
13
  import z3, { z } from 'zod';
14
14
  import { get } from 'radash';
15
- import crypto2, { randomUUID } from 'crypto';
16
- import { ReadableStream, TransformStream } from 'stream/web';
15
+ import * as crypto2 from 'crypto';
16
+ import { randomUUID } from 'crypto';
17
+ import { ReadableStream as ReadableStream$1, TransformStream } from 'stream/web';
17
18
  import EventEmitter from 'events';
18
19
  import sift from 'sift';
19
20
  import { createActor, assign, fromPromise, setup } from 'xstate';
@@ -92,6 +93,371 @@ var WhenConditionReturnValue = /* @__PURE__ */(WhenConditionReturnValue2 => {
92
93
  // src/agent/index.ts
93
94
  var import_fast_deep_equal = __toESM(require_fast_deep_equal(), 1);
94
95
 
96
+ // src/processors/processors/structured-output.ts
97
+ var StructuredOutputProcessor = class {
98
+ name = "structured-output";
99
+ schema;
100
+ structuringAgent;
101
+ errorStrategy;
102
+ fallbackValue;
103
+ constructor(options) {
104
+ this.schema = options.schema;
105
+ this.errorStrategy = options.errorStrategy ?? "strict";
106
+ this.fallbackValue = options.fallbackValue;
107
+ this.structuringAgent = new Agent({
108
+ name: "structured-output-structurer",
109
+ instructions: options.instructions || this.generateInstructions(),
110
+ model: options.model
111
+ });
112
+ }
113
+ async processOutputResult(args) {
114
+ const {
115
+ messages,
116
+ abort
117
+ } = args;
118
+ const processedMessages = await Promise.all(messages.map(async message => {
119
+ if (message.role !== "assistant") {
120
+ return message;
121
+ }
122
+ const textContent = this.extractTextContent(message);
123
+ if (!textContent.trim()) {
124
+ return message;
125
+ }
126
+ try {
127
+ const structuredResult = await this.structuringAgent.generate(`Extract and structure the key information from the following text according to the specified schema. Keep the original meaning and details:
128
+
129
+ ${textContent}`, {
130
+ output: this.schema
131
+ });
132
+ if (!structuredResult.object) {
133
+ this.handleError("Structuring failed", "Internal agent did not generate structured output", abort);
134
+ if (this.errorStrategy === "fallback" && this.fallbackValue !== void 0) {
135
+ return {
136
+ ...message,
137
+ content: {
138
+ ...message.content,
139
+ metadata: {
140
+ ...(message.content.metadata || {}),
141
+ structuredOutput: this.fallbackValue
142
+ }
143
+ }
144
+ };
145
+ }
146
+ return message;
147
+ }
148
+ return {
149
+ ...message,
150
+ content: {
151
+ ...message.content,
152
+ parts: [{
153
+ type: "text",
154
+ text: textContent
155
+ // Keep original text unchanged
156
+ }],
157
+ metadata: {
158
+ ...(message.content.metadata || {}),
159
+ structuredOutput: structuredResult.object
160
+ }
161
+ }
162
+ };
163
+ } catch (error) {
164
+ this.handleError("Processing failed", error instanceof Error ? error.message : "Unknown error", abort);
165
+ if (this.errorStrategy === "fallback" && this.fallbackValue !== void 0) {
166
+ return {
167
+ ...message,
168
+ content: {
169
+ ...message.content,
170
+ metadata: {
171
+ ...(message.content.metadata || {}),
172
+ structuredOutput: this.fallbackValue
173
+ }
174
+ }
175
+ };
176
+ }
177
+ return message;
178
+ }
179
+ }));
180
+ return processedMessages;
181
+ }
182
+ /**
183
+ * Extract text content from a message
184
+ */
185
+ extractTextContent(message) {
186
+ let text = "";
187
+ if (message.content.parts) {
188
+ for (const part of message.content.parts) {
189
+ if (part.type === "text" && "text" in part && typeof part.text === "string") {
190
+ text += part.text + " ";
191
+ }
192
+ }
193
+ }
194
+ if (!text.trim() && typeof message.content.content === "string") {
195
+ text = message.content.content;
196
+ }
197
+ return text.trim();
198
+ }
199
+ /**
200
+ * Generate instructions for the structuring agent based on the schema
201
+ */
202
+ generateInstructions() {
203
+ return `You are a data structuring specialist. Your job is to convert unstructured text into a specific JSON format.
204
+
205
+ TASK: Convert the provided unstructured text into valid JSON that matches the following schema:
206
+
207
+ REQUIREMENTS:
208
+ - Return ONLY valid JSON, no additional text or explanation
209
+ - Extract relevant information from the input text
210
+ - If information is missing, use reasonable defaults or null values
211
+ - Maintain data types as specified in the schema
212
+ - Be consistent and accurate in your conversions
213
+
214
+ The input text may be in any format (sentences, bullet points, paragraphs, etc.). Extract the relevant data and structure it according to the schema.`;
215
+ }
216
+ /**
217
+ * Handle errors based on the configured strategy
218
+ */
219
+ handleError(context, error, abort) {
220
+ const message = `[StructuredOutputProcessor] ${context}: ${error}`;
221
+ console.error(`ERROR from StructuredOutputProcessor: ${message}`);
222
+ switch (this.errorStrategy) {
223
+ case "strict":
224
+ abort(message);
225
+ break;
226
+ case "warn":
227
+ console.warn(message);
228
+ break;
229
+ case "fallback":
230
+ console.info(`${message} (using fallback)`);
231
+ break;
232
+ }
233
+ }
234
+ };
235
+
236
+ // src/agent/trip-wire.ts
237
+ var TripWire = class extends Error {
238
+ constructor(reason) {
239
+ super(reason);
240
+ Object.setPrototypeOf(this, new.target.prototype);
241
+ }
242
+ };
243
+
244
+ // src/processors/runner.ts
245
+ var ProcessorState = class {
246
+ constructor(processorName) {
247
+ this.processorName = processorName;
248
+ }
249
+ accumulatedText = "";
250
+ customState = {};
251
+ streamParts = [];
252
+ // Internal methods for the runner
253
+ addPart(part) {
254
+ if (part.type === "text-delta") {
255
+ this.accumulatedText += part.textDelta;
256
+ }
257
+ this.streamParts.push(part);
258
+ }
259
+ };
260
+ var ProcessorRunner = class {
261
+ inputProcessors;
262
+ outputProcessors;
263
+ logger;
264
+ agentName;
265
+ constructor({
266
+ inputProcessors,
267
+ outputProcessors,
268
+ logger,
269
+ agentName
270
+ }) {
271
+ this.inputProcessors = inputProcessors ?? [];
272
+ this.outputProcessors = outputProcessors ?? [];
273
+ this.logger = logger;
274
+ this.agentName = agentName;
275
+ }
276
+ async runOutputProcessors(messageList, telemetry) {
277
+ const responseMessages = messageList.clear.response.v2();
278
+ let processableMessages = [...responseMessages];
279
+ const ctx = {
280
+ abort: () => {
281
+ throw new TripWire("Tripwire triggered");
282
+ }
283
+ };
284
+ for (const [index, processor] of this.outputProcessors.entries()) {
285
+ const abort = reason => {
286
+ throw new TripWire(reason || `Tripwire triggered by ${processor.name}`);
287
+ };
288
+ ctx.abort = abort;
289
+ const processMethod = processor.processOutputResult?.bind(processor);
290
+ if (!processMethod) {
291
+ continue;
292
+ }
293
+ if (!telemetry) {
294
+ processableMessages = await processMethod({
295
+ messages: processableMessages,
296
+ abort: ctx.abort
297
+ });
298
+ } else {
299
+ await telemetry.traceMethod(async () => {
300
+ processableMessages = await processMethod({
301
+ messages: processableMessages,
302
+ abort: ctx.abort
303
+ });
304
+ return processableMessages;
305
+ }, {
306
+ spanName: `agent.outputProcessor.${processor.name}`,
307
+ attributes: {
308
+ "processor.name": processor.name,
309
+ "processor.index": index.toString(),
310
+ "processor.total": this.outputProcessors.length.toString()
311
+ }
312
+ })();
313
+ }
314
+ }
315
+ if (processableMessages.length > 0) {
316
+ messageList.add(processableMessages, "response");
317
+ }
318
+ return messageList;
319
+ }
320
+ /**
321
+ * Process a stream part through all output processors with state management
322
+ */
323
+ async processPart(part, processorStates) {
324
+ if (!this.outputProcessors.length) {
325
+ return {
326
+ part,
327
+ blocked: false
328
+ };
329
+ }
330
+ try {
331
+ let processedPart = part;
332
+ for (const processor of this.outputProcessors) {
333
+ try {
334
+ if (processor.processOutputStream && processedPart) {
335
+ let state = processorStates.get(processor.name);
336
+ if (!state) {
337
+ state = new ProcessorState(processor.name);
338
+ processorStates.set(processor.name, state);
339
+ }
340
+ state.addPart(processedPart);
341
+ const result = await processor.processOutputStream({
342
+ part: processedPart,
343
+ streamParts: state.streamParts,
344
+ state: state.customState,
345
+ abort: reason => {
346
+ throw new TripWire(reason || `Stream part blocked by ${processor.name}`);
347
+ }
348
+ });
349
+ processedPart = result;
350
+ }
351
+ } catch (error) {
352
+ if (error instanceof TripWire) {
353
+ return {
354
+ part: null,
355
+ blocked: true,
356
+ reason: error.message
357
+ };
358
+ }
359
+ this.logger.error(`[Agent:${this.agentName}] - Output processor ${processor.name} failed:`, error);
360
+ }
361
+ }
362
+ return {
363
+ part: processedPart,
364
+ blocked: false
365
+ };
366
+ } catch (error) {
367
+ this.logger.error(`[Agent:${this.agentName}] - Stream part processing failed:`, error);
368
+ return {
369
+ part,
370
+ blocked: false
371
+ };
372
+ }
373
+ }
374
+ async runOutputProcessorsForStream(streamResult) {
375
+ return new ReadableStream({
376
+ start: async controller => {
377
+ const reader = streamResult.fullStream.getReader();
378
+ const processorStates = /* @__PURE__ */new Map();
379
+ try {
380
+ while (true) {
381
+ const {
382
+ done,
383
+ value
384
+ } = await reader.read();
385
+ if (done) {
386
+ controller.close();
387
+ break;
388
+ }
389
+ const {
390
+ part: processedPart,
391
+ blocked,
392
+ reason
393
+ } = await this.processPart(value, processorStates);
394
+ if (blocked) {
395
+ void this.logger.debug(`[Agent:${this.agentName}] - Stream part blocked by output processor`, {
396
+ reason,
397
+ originalPart: value
398
+ });
399
+ controller.enqueue({
400
+ type: "tripwire",
401
+ tripwireReason: reason || "Output processor blocked content"
402
+ });
403
+ controller.close();
404
+ break;
405
+ } else if (processedPart !== null) {
406
+ controller.enqueue(processedPart);
407
+ }
408
+ }
409
+ } catch (error) {
410
+ controller.error(error);
411
+ }
412
+ }
413
+ });
414
+ }
415
+ async runInputProcessors(messageList, telemetry) {
416
+ const userMessages = messageList.clear.input.v2();
417
+ let processableMessages = [...userMessages];
418
+ const ctx = {
419
+ abort: () => {
420
+ throw new TripWire("Tripwire triggered");
421
+ }
422
+ };
423
+ for (const [index, processor] of this.inputProcessors.entries()) {
424
+ const abort = reason => {
425
+ throw new TripWire(reason || `Tripwire triggered by ${processor.name}`);
426
+ };
427
+ ctx.abort = abort;
428
+ const processMethod = processor.processInput?.bind(processor);
429
+ if (!processMethod) {
430
+ continue;
431
+ }
432
+ if (!telemetry) {
433
+ processableMessages = await processMethod({
434
+ messages: processableMessages,
435
+ abort: ctx.abort
436
+ });
437
+ } else {
438
+ await telemetry.traceMethod(async () => {
439
+ processableMessages = await processMethod({
440
+ messages: processableMessages,
441
+ abort: ctx.abort
442
+ });
443
+ return processableMessages;
444
+ }, {
445
+ spanName: `agent.inputProcessor.${processor.name}`,
446
+ attributes: {
447
+ "processor.name": processor.name,
448
+ "processor.index": index.toString(),
449
+ "processor.total": this.inputProcessors.length.toString()
450
+ }
451
+ })();
452
+ }
453
+ }
454
+ if (processableMessages.length > 0) {
455
+ messageList.add(processableMessages, "user");
456
+ }
457
+ return messageList;
458
+ }
459
+ };
460
+
95
461
  // src/scores/hooks.ts
96
462
  function runScorer({
97
463
  runId,
@@ -209,11 +575,20 @@ function convertFullStreamChunkToMastra(value, ctx) {
209
575
  providerMetadata: value.providerMetadata
210
576
  }
211
577
  };
578
+ } else if (value.type === "tripwire") {
579
+ return {
580
+ type: "tripwire",
581
+ runId: ctx.runId,
582
+ from: "AGENT",
583
+ payload: {
584
+ tripwireReason: value.tripwireReason
585
+ }
586
+ };
212
587
  }
213
588
  }
214
589
 
215
590
  // src/stream/MastraAgentStream.ts
216
- var MastraAgentStream = class extends ReadableStream {
591
+ var MastraAgentStream = class extends ReadableStream$1 {
217
592
  #usageCount = {
218
593
  promptTokens: 0,
219
594
  completionTokens: 0,
@@ -334,56 +709,6 @@ var MastraAgentStream = class extends ReadableStream {
334
709
  }
335
710
  };
336
711
 
337
- // src/agent/trip-wire.ts
338
- var TripWire = class extends Error {
339
- constructor(reason) {
340
- super(reason);
341
- Object.setPrototypeOf(this, new.target.prototype);
342
- }
343
- };
344
-
345
- // src/agent/input-processor/runner.ts
346
- async function runInputProcessors(processors, messageList, telemetry) {
347
- const userMessages = messageList.clear.input.v2();
348
- let processableMessages = [...userMessages];
349
- const ctx = {
350
- abort: () => {
351
- throw new TripWire("Tripwire triggered");
352
- }
353
- };
354
- for (const [index, processor] of processors.entries()) {
355
- const abort = reason => {
356
- throw new TripWire(reason || `Tripwire triggered by ${processor.name}`);
357
- };
358
- ctx.abort = abort;
359
- if (!telemetry) {
360
- processableMessages = await processor.process({
361
- messages: processableMessages,
362
- abort: ctx.abort
363
- });
364
- } else {
365
- await telemetry.traceMethod(async () => {
366
- processableMessages = await processor.process({
367
- messages: processableMessages,
368
- abort: ctx.abort
369
- });
370
- return processableMessages;
371
- }, {
372
- spanName: `agent.inputProcessor.${processor.name}`,
373
- attributes: {
374
- "processor.name": processor.name,
375
- "processor.index": index.toString(),
376
- "processor.total": processors.length.toString()
377
- }
378
- })();
379
- }
380
- }
381
- if (processableMessages.length > 0) {
382
- messageList.add(processableMessages, "user");
383
- }
384
- return messageList;
385
- }
386
-
387
712
  // src/agent/save-queue/index.ts
388
713
  var SaveQueueManager = class _SaveQueueManager {
389
714
  logger;
@@ -506,7 +831,7 @@ var SaveQueueManager = class _SaveQueueManager {
506
831
  }
507
832
  };
508
833
 
509
- // src/agent/input-processor/processors/unicode-normalizer.ts
834
+ // src/processors/processors/unicode-normalizer.ts
510
835
  var UnicodeNormalizer = class {
511
836
  name = "unicode-normalizer";
512
837
  options;
@@ -518,7 +843,7 @@ var UnicodeNormalizer = class {
518
843
  trim: options.trim ?? true
519
844
  };
520
845
  }
521
- process(args) {
846
+ processInput(args) {
522
847
  try {
523
848
  return args.messages.map(message => ({
524
849
  ...message,
@@ -562,27 +887,41 @@ var UnicodeNormalizer = class {
562
887
  return normalized;
563
888
  }
564
889
  };
565
- var ModerationInputProcessor = class _ModerationInputProcessor {
890
+
891
+ // src/agent/input-processor/processors/unicode-normalizer.ts
892
+ var UnicodeNormalizerInputProcessor = class {
893
+ name = "unicode-normalizer";
894
+ processor;
895
+ constructor(options = {}) {
896
+ this.processor = new UnicodeNormalizer(options);
897
+ }
898
+ process(args) {
899
+ return this.processor.processInput(args);
900
+ }
901
+ };
902
+ var ModerationProcessor = class _ModerationProcessor {
566
903
  name = "moderation";
567
904
  moderationAgent;
568
905
  categories;
569
906
  threshold;
570
907
  strategy;
571
908
  includeScores;
909
+ chunkWindow;
572
910
  // Default OpenAI moderation categories
573
911
  static DEFAULT_CATEGORIES = ["hate", "hate/threatening", "harassment", "harassment/threatening", "self-harm", "self-harm/intent", "self-harm/instructions", "sexual", "sexual/minors", "violence", "violence/graphic"];
574
912
  constructor(options) {
575
- this.categories = options.categories || _ModerationInputProcessor.DEFAULT_CATEGORIES;
913
+ this.categories = options.categories || _ModerationProcessor.DEFAULT_CATEGORIES;
576
914
  this.threshold = options.threshold ?? 0.5;
577
915
  this.strategy = options.strategy || "block";
578
916
  this.includeScores = options.includeScores ?? false;
917
+ this.chunkWindow = options.chunkWindow ?? 0;
579
918
  this.moderationAgent = new Agent({
580
919
  name: "content-moderator",
581
920
  instructions: options.instructions || this.createDefaultInstructions(),
582
921
  model: options.model
583
922
  });
584
923
  }
585
- async process(args) {
924
+ async processInput(args) {
586
925
  try {
587
926
  const {
588
927
  messages,
@@ -615,11 +954,41 @@ var ModerationInputProcessor = class _ModerationInputProcessor {
615
954
  args.abort(`Moderation failed: ${error instanceof Error ? error.message : "Unknown error"}`);
616
955
  }
617
956
  }
957
+ async processOutputResult(args) {
958
+ return this.processInput(args);
959
+ }
960
+ async processOutputStream(args) {
961
+ try {
962
+ const {
963
+ part,
964
+ streamParts,
965
+ abort
966
+ } = args;
967
+ if (part.type !== "text-delta") {
968
+ return part;
969
+ }
970
+ const contentToModerate = this.buildContextFromChunks(streamParts);
971
+ const moderationResult = await this.moderateContent(contentToModerate, true);
972
+ if (this.isModerationFlagged(moderationResult)) {
973
+ this.handleFlaggedContent(moderationResult, this.strategy, abort);
974
+ if (this.strategy === "filter") {
975
+ return null;
976
+ }
977
+ }
978
+ return part;
979
+ } catch (error) {
980
+ if (error instanceof TripWire) {
981
+ throw error;
982
+ }
983
+ console.warn("[ModerationProcessor] Stream moderation failed:", error);
984
+ return args.part;
985
+ }
986
+ }
618
987
  /**
619
988
  * Moderate content using the internal agent
620
989
  */
621
- async moderateContent(content) {
622
- const prompt = this.createModerationPrompt(content);
990
+ async moderateContent(content, isStream = false) {
991
+ const prompt = this.createModerationPrompt(content, isStream);
623
992
  try {
624
993
  const response = await this.moderationAgent.generate(prompt, {
625
994
  output: z3.object({
@@ -634,7 +1003,7 @@ var ModerationInputProcessor = class _ModerationInputProcessor {
634
1003
  const result = response.object;
635
1004
  return result;
636
1005
  } catch (error) {
637
- console.warn("[ModerationInputProcessor] Agent moderation failed, allowing content:", error);
1006
+ console.warn("[ModerationProcessor] Agent moderation failed, allowing content:", error);
638
1007
  return {};
639
1008
  }
640
1009
  }
@@ -661,10 +1030,10 @@ var ModerationInputProcessor = class _ModerationInputProcessor {
661
1030
  abort(message);
662
1031
  break;
663
1032
  case "warn":
664
- console.warn(`[ModerationInputProcessor] ${message}`);
1033
+ console.warn(`[ModerationProcessor] ${message}`);
665
1034
  break;
666
1035
  case "filter":
667
- console.info(`[ModerationInputProcessor] Filtered message: ${message}`);
1036
+ console.info(`[ModerationProcessor] Filtered message: ${message}`);
668
1037
  break;
669
1038
  }
670
1039
  }
@@ -706,11 +1075,44 @@ Guidelines:
706
1075
  /**
707
1076
  * Create moderation prompt for the agent
708
1077
  */
709
- createModerationPrompt(content) {
710
- return `Please evaluate the following content for moderation:
1078
+ createModerationPrompt(content, isStream) {
1079
+ return `Please evaluate the following content for moderation, ${isStream ? `this is a stream, so keep in mind you only have the context of the last ${this.chunkWindow} chunks. Make your best judgement on the content.` : ""}:
711
1080
 
712
1081
  Content: "${content}"`;
713
1082
  }
1083
+ /**
1084
+ * Build context string from chunks based on chunkWindow
1085
+ * streamParts includes the current part
1086
+ */
1087
+ buildContextFromChunks(streamParts) {
1088
+ if (this.chunkWindow === 0) {
1089
+ const currentChunk = streamParts[streamParts.length - 1];
1090
+ if (currentChunk && currentChunk.type === "text-delta") {
1091
+ return currentChunk.textDelta;
1092
+ }
1093
+ return "";
1094
+ }
1095
+ const contextChunks = streamParts.slice(-this.chunkWindow);
1096
+ const textContent = contextChunks.filter(part => part.type === "text-delta").map(part => {
1097
+ if (part.type === "text-delta") {
1098
+ return part.textDelta;
1099
+ }
1100
+ return "";
1101
+ }).join("");
1102
+ return textContent;
1103
+ }
1104
+ };
1105
+
1106
+ // src/agent/input-processor/processors/moderation.ts
1107
+ var ModerationInputProcessor = class {
1108
+ name = "moderation";
1109
+ processor;
1110
+ constructor(options) {
1111
+ this.processor = new ModerationProcessor(options);
1112
+ }
1113
+ async process(args) {
1114
+ return this.processor.processInput(args);
1115
+ }
714
1116
  };
715
1117
  var PromptInjectionDetector = class _PromptInjectionDetector {
716
1118
  name = "prompt-injection-detector";
@@ -744,7 +1146,7 @@ var PromptInjectionDetector = class _PromptInjectionDetector {
744
1146
  model: options.model
745
1147
  });
746
1148
  }
747
- async process(args) {
1149
+ async processInput(args) {
748
1150
  try {
749
1151
  const {
750
1152
  messages,
@@ -901,6 +1303,18 @@ Content: "${content}"
901
1303
  ${includeRewrite}`;
902
1304
  }
903
1305
  };
1306
+
1307
+ // src/agent/input-processor/processors/prompt-injection-detector.ts
1308
+ var PromptInjectionDetectorInputProcessor = class {
1309
+ name = "prompt-injection-detector";
1310
+ processor;
1311
+ constructor(options) {
1312
+ this.processor = new PromptInjectionDetector(options);
1313
+ }
1314
+ async process(args) {
1315
+ return this.processor.processInput(args);
1316
+ }
1317
+ };
904
1318
  var PIIDetector = class _PIIDetector {
905
1319
  name = "pii-detector";
906
1320
  detectionAgent;
@@ -951,7 +1365,7 @@ var PIIDetector = class _PIIDetector {
951
1365
  model: options.model
952
1366
  });
953
1367
  }
954
- async process(args) {
1368
+ async processInput(args) {
955
1369
  try {
956
1370
  const {
957
1371
  messages,
@@ -1200,6 +1614,113 @@ ${this.detectionTypes.map(type => `- ${type}`).join("\n")}
1200
1614
 
1201
1615
  IMPORTANT: IF NO PII IS DETECTED, RETURN AN EMPTY OBJECT, DO NOT INCLUDE ANYTHING ELSE. Do not include any zeros in your response, if the response should be 0, omit it, they will be counted as false.`;
1202
1616
  }
1617
+ /**
1618
+ * Process streaming output chunks for PII detection and redaction
1619
+ */
1620
+ async processOutputStream(args) {
1621
+ const {
1622
+ part,
1623
+ abort
1624
+ } = args;
1625
+ try {
1626
+ if (part.type !== "text-delta") {
1627
+ return part;
1628
+ }
1629
+ const textContent = part.textDelta;
1630
+ if (!textContent.trim()) {
1631
+ return part;
1632
+ }
1633
+ const detectionResult = await this.detectPII(textContent);
1634
+ if (this.isPIIFlagged(detectionResult)) {
1635
+ switch (this.strategy) {
1636
+ case "block":
1637
+ abort(`PII detected in streaming content. Types: ${this.getDetectedTypes(detectionResult).join(", ")}`);
1638
+ case "warn":
1639
+ console.warn(`[PIIDetector] PII detected in streaming content: ${this.getDetectedTypes(detectionResult).join(", ")}`);
1640
+ return part;
1641
+ // Allow content through with warning
1642
+ case "filter":
1643
+ console.info(`[PIIDetector] Filtered streaming part with PII: ${this.getDetectedTypes(detectionResult).join(", ")}`);
1644
+ return null;
1645
+ // Don't emit this part
1646
+ case "redact":
1647
+ if (detectionResult.redacted_content) {
1648
+ console.info(`[PIIDetector] Redacted PII in streaming content: ${this.getDetectedTypes(detectionResult).join(", ")}`);
1649
+ return {
1650
+ ...part,
1651
+ textDelta: detectionResult.redacted_content
1652
+ };
1653
+ } else {
1654
+ console.warn(`[PIIDetector] No redaction available for streaming part, filtering`);
1655
+ return null;
1656
+ }
1657
+ default:
1658
+ return part;
1659
+ }
1660
+ }
1661
+ return part;
1662
+ } catch (error) {
1663
+ if (error instanceof TripWire) {
1664
+ throw error;
1665
+ }
1666
+ console.warn("[PIIDetector] Streaming detection failed, allowing content:", error);
1667
+ return part;
1668
+ }
1669
+ }
1670
+ /**
1671
+ * Process final output result for PII detection and redaction
1672
+ */
1673
+ async processOutputResult({
1674
+ messages,
1675
+ abort
1676
+ }) {
1677
+ try {
1678
+ if (messages.length === 0) {
1679
+ return messages;
1680
+ }
1681
+ const processedMessages = [];
1682
+ for (const message of messages) {
1683
+ const textContent = this.extractTextContent(message);
1684
+ if (!textContent.trim()) {
1685
+ processedMessages.push(message);
1686
+ continue;
1687
+ }
1688
+ const detectionResult = await this.detectPII(textContent);
1689
+ if (this.isPIIFlagged(detectionResult)) {
1690
+ const processedMessage = this.handleDetectedPII(message, detectionResult, this.strategy, abort);
1691
+ if (this.strategy === "filter") {
1692
+ continue;
1693
+ } else if (this.strategy === "redact") {
1694
+ if (processedMessage) {
1695
+ processedMessages.push(processedMessage);
1696
+ } else {
1697
+ processedMessages.push(message);
1698
+ }
1699
+ continue;
1700
+ }
1701
+ }
1702
+ processedMessages.push(message);
1703
+ }
1704
+ return processedMessages;
1705
+ } catch (error) {
1706
+ if (error instanceof TripWire) {
1707
+ throw error;
1708
+ }
1709
+ throw new Error(`PII detection failed: ${error instanceof Error ? error.stack : "Unknown error"}`);
1710
+ }
1711
+ }
1712
+ /**
1713
+ * Get detected PII types from detection result
1714
+ */
1715
+ getDetectedTypes(result) {
1716
+ if (result.detections && result.detections.length > 0) {
1717
+ return [...new Set(result.detections.map(d => d.type))];
1718
+ }
1719
+ if (result.categories) {
1720
+ return Object.entries(result.categories).filter(([_, score]) => typeof score === "number" && score >= this.threshold).map(([type]) => type);
1721
+ }
1722
+ return [];
1723
+ }
1203
1724
  /**
1204
1725
  * Create detection prompt for the agent
1205
1726
  */
@@ -1208,6 +1729,18 @@ IMPORTANT: IF NO PII IS DETECTED, RETURN AN EMPTY OBJECT, DO NOT INCLUDE ANYTHIN
1208
1729
  Content: "${content}"`;
1209
1730
  }
1210
1731
  };
1732
+
1733
+ // src/agent/input-processor/processors/pii-detector.ts
1734
+ var PIIDetectorInputProcessor = class {
1735
+ name = "pii-detector";
1736
+ processor;
1737
+ constructor(options) {
1738
+ this.processor = new PIIDetector(options);
1739
+ }
1740
+ async process(args) {
1741
+ return this.processor.processInput(args);
1742
+ }
1743
+ };
1211
1744
  var LanguageDetector = class _LanguageDetector {
1212
1745
  name = "language-detector";
1213
1746
  detectionAgent;
@@ -1274,7 +1807,7 @@ var LanguageDetector = class _LanguageDetector {
1274
1807
  model: options.model
1275
1808
  });
1276
1809
  }
1277
- async process(args) {
1810
+ async processInput(args) {
1278
1811
  try {
1279
1812
  const {
1280
1813
  messages,
@@ -1509,6 +2042,18 @@ Target: ${this.targetLanguages.join("/")}${translate}`;
1509
2042
  }
1510
2043
  };
1511
2044
 
2045
+ // src/agent/input-processor/processors/language-detector.ts
2046
+ var LanguageDetectorInputProcessor = class {
2047
+ name = "language-detector";
2048
+ processor;
2049
+ constructor(options) {
2050
+ this.processor = new LanguageDetector(options);
2051
+ }
2052
+ async process(args) {
2053
+ return this.processor.processInput(args);
2054
+ }
2055
+ };
2056
+
1512
2057
  // src/agent/index.ts
1513
2058
  function resolveMaybePromise(value, cb) {
1514
2059
  if (value instanceof Promise) {
@@ -1531,7 +2076,7 @@ function resolveThreadIdFromArgs(args) {
1531
2076
  var _Agent_decorators, _init, _a;
1532
2077
  _Agent_decorators = [InstrumentClass({
1533
2078
  prefix: "agent",
1534
- excludeMethods: ["hasOwnMemory", "getMemory", "__primitive", "__registerMastra", "__registerPrimitives", "__runInputProcessors", "__setTools", "__setLogger", "__setTelemetry", "log", "getModel", "getInstructions", "getTools", "getLLM", "getWorkflows", "getDefaultGenerateOptions", "getDefaultStreamOptions", "getDescription", "getScorers", "getVoice"]
2079
+ excludeMethods: ["hasOwnMemory", "getMemory", "__primitive", "__registerMastra", "__registerPrimitives", "__runInputProcessors", "__runOutputProcessors", "getProcessorRunner", "__setTools", "__setLogger", "__setTelemetry", "log", "getModel", "getInstructions", "getTools", "getLLM", "getWorkflows", "getDefaultGenerateOptions", "getDefaultStreamOptions", "getDescription", "getScorers", "getVoice"]
1535
2080
  })];
1536
2081
  var Agent = class extends (_a = MastraBase) {
1537
2082
  id;
@@ -1550,6 +2095,7 @@ var Agent = class extends (_a = MastraBase) {
1550
2095
  #scorers;
1551
2096
  #voice;
1552
2097
  #inputProcessors;
2098
+ #outputProcessors;
1553
2099
  // This flag is for agent network messages. We should change the agent network formatting and remove this flag after.
1554
2100
  _agentNetworkAppend = false;
1555
2101
  constructor(config) {
@@ -1611,8 +2157,30 @@ var Agent = class extends (_a = MastraBase) {
1611
2157
  if (config.inputProcessors) {
1612
2158
  this.#inputProcessors = config.inputProcessors;
1613
2159
  }
2160
+ if (config.outputProcessors) {
2161
+ this.#outputProcessors = config.outputProcessors;
2162
+ }
1614
2163
  this._agentNetworkAppend = config._agentNetworkAppend || false;
1615
2164
  }
2165
+ async getProcessorRunner({
2166
+ runtimeContext,
2167
+ inputProcessorOverrides,
2168
+ outputProcessorOverrides
2169
+ }) {
2170
+ const inputProcessors = inputProcessorOverrides ?? (this.#inputProcessors ? typeof this.#inputProcessors === "function" ? await this.#inputProcessors({
2171
+ runtimeContext
2172
+ }) : this.#inputProcessors : []);
2173
+ const outputProcessors = outputProcessorOverrides ?? (this.#outputProcessors ? typeof this.#outputProcessors === "function" ? await this.#outputProcessors({
2174
+ runtimeContext
2175
+ }) : this.#outputProcessors : []);
2176
+ this.logger.debug("outputProcessors", outputProcessors);
2177
+ return new ProcessorRunner({
2178
+ inputProcessors,
2179
+ outputProcessors,
2180
+ logger: this.logger,
2181
+ agentName: this.name
2182
+ });
2183
+ }
1616
2184
  hasOwnMemory() {
1617
2185
  return Boolean(this.#memory);
1618
2186
  }
@@ -1950,7 +2518,7 @@ var Agent = class extends (_a = MastraBase) {
1950
2518
  runtimeContext
1951
2519
  });
1952
2520
  return resolveMaybePromise(modelToUse, resolvedModel => {
1953
- const llm = new MastraLLM({
2521
+ const llm = new MastraLLMV1({
1954
2522
  model: resolvedModel,
1955
2523
  mastra: this.#mastra
1956
2524
  });
@@ -2238,35 +2806,36 @@ var Agent = class extends (_a = MastraBase) {
2238
2806
  }
2239
2807
  async __runInputProcessors({
2240
2808
  runtimeContext,
2241
- messageList
2809
+ messageList,
2810
+ inputProcessorOverrides
2242
2811
  }) {
2243
2812
  let tripwireTriggered = false;
2244
2813
  let tripwireReason = "";
2245
- if (this.#inputProcessors) {
2246
- const processors = typeof this.#inputProcessors === "function" ? await this.#inputProcessors({
2247
- runtimeContext
2248
- }) : this.#inputProcessors;
2249
- const tracedRunInputProcessors = (processors2, messageList2) => {
2814
+ if (inputProcessorOverrides?.length || this.#inputProcessors) {
2815
+ const runner = await this.getProcessorRunner({
2816
+ runtimeContext,
2817
+ inputProcessorOverrides
2818
+ });
2819
+ const tracedRunInputProcessors = messageList2 => {
2250
2820
  const telemetry = this.#mastra?.getTelemetry();
2251
2821
  if (!telemetry) {
2252
- return runInputProcessors(processors2, messageList2, void 0);
2822
+ return runner.runInputProcessors(messageList2, void 0);
2253
2823
  }
2254
2824
  return telemetry.traceMethod(async data => {
2255
- return runInputProcessors(data.processors, data.messageList, telemetry);
2825
+ return runner.runInputProcessors(data.messageList, telemetry);
2256
2826
  }, {
2257
2827
  spanName: `agent.${this.name}.inputProcessors`,
2258
2828
  attributes: {
2259
2829
  "agent.name": this.name,
2260
- "inputProcessors.count": processors2.length.toString(),
2261
- "inputProcessors.names": processors2.map(p => p.name).join(",")
2830
+ "inputProcessors.count": runner.inputProcessors.length.toString(),
2831
+ "inputProcessors.names": runner.inputProcessors.map(p => p.name).join(",")
2262
2832
  }
2263
2833
  })({
2264
- processors: processors2,
2265
2834
  messageList: messageList2
2266
2835
  });
2267
2836
  };
2268
2837
  try {
2269
- messageList = await tracedRunInputProcessors(processors, messageList);
2838
+ messageList = await tracedRunInputProcessors(messageList);
2270
2839
  } catch (error) {
2271
2840
  if (error instanceof TripWire) {
2272
2841
  tripwireTriggered = true;
@@ -2287,6 +2856,54 @@ var Agent = class extends (_a = MastraBase) {
2287
2856
  tripwireReason
2288
2857
  };
2289
2858
  }
2859
+ async __runOutputProcessors({
2860
+ runtimeContext,
2861
+ messageList,
2862
+ outputProcessorOverrides
2863
+ }) {
2864
+ let tripwireTriggered = false;
2865
+ let tripwireReason = "";
2866
+ if (outputProcessorOverrides?.length || this.#outputProcessors) {
2867
+ const runner = await this.getProcessorRunner({
2868
+ runtimeContext,
2869
+ outputProcessorOverrides
2870
+ });
2871
+ const tracedRunOutputProcessors = messageList2 => {
2872
+ const telemetry = this.#mastra?.getTelemetry();
2873
+ if (!telemetry) {
2874
+ return runner.runOutputProcessors(messageList2, void 0);
2875
+ }
2876
+ return telemetry.traceMethod(async data => {
2877
+ return runner.runOutputProcessors(data.messageList, telemetry);
2878
+ }, {
2879
+ spanName: `agent.${this.name}.outputProcessors`,
2880
+ attributes: {
2881
+ "agent.name": this.name,
2882
+ "outputProcessors.count": runner.outputProcessors.length.toString(),
2883
+ "outputProcessors.names": runner.outputProcessors.map(p => p.name).join(",")
2884
+ }
2885
+ })({
2886
+ messageList: messageList2
2887
+ });
2888
+ };
2889
+ try {
2890
+ messageList = await tracedRunOutputProcessors(messageList);
2891
+ } catch (e) {
2892
+ if (e instanceof TripWire) {
2893
+ tripwireTriggered = true;
2894
+ tripwireReason = e.message;
2895
+ this.logger.debug(`[Agent:${this.name}] - Output processor tripwire triggered: ${e.message}`);
2896
+ } else {
2897
+ throw e;
2898
+ }
2899
+ }
2900
+ }
2901
+ return {
2902
+ messageList,
2903
+ tripwireTriggered,
2904
+ tripwireReason
2905
+ };
2906
+ }
2290
2907
  async getMemoryMessages({
2291
2908
  resourceId,
2292
2909
  threadId,
@@ -3256,14 +3873,92 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3256
3873
  output,
3257
3874
  ...llmOptions
3258
3875
  } = beforeResult;
3876
+ let finalOutputProcessors = mergedGenerateOptions.outputProcessors;
3877
+ if (mergedGenerateOptions.structuredOutput) {
3878
+ const structuredProcessor = new StructuredOutputProcessor(mergedGenerateOptions.structuredOutput);
3879
+ finalOutputProcessors = finalOutputProcessors ? [...finalOutputProcessors, structuredProcessor] : [structuredProcessor];
3880
+ }
3259
3881
  if (!output || experimental_output) {
3260
3882
  const result2 = await llm.__text({
3261
3883
  ...llmOptions,
3262
3884
  experimental_output
3263
3885
  });
3886
+ const outputProcessorResult2 = await this.__runOutputProcessors({
3887
+ runtimeContext: mergedGenerateOptions.runtimeContext || new RuntimeContext(),
3888
+ outputProcessorOverrides: finalOutputProcessors,
3889
+ messageList: new MessageList({
3890
+ threadId: llmOptions.threadId || "",
3891
+ resourceId: llmOptions.resourceId || ""
3892
+ }).add({
3893
+ role: "assistant",
3894
+ content: [{
3895
+ type: "text",
3896
+ text: result2.text
3897
+ }]
3898
+ }, "response")
3899
+ });
3900
+ if (outputProcessorResult2.tripwireTriggered) {
3901
+ const tripwireResult = {
3902
+ text: "",
3903
+ object: void 0,
3904
+ usage: {
3905
+ totalTokens: 0,
3906
+ promptTokens: 0,
3907
+ completionTokens: 0
3908
+ },
3909
+ finishReason: "other",
3910
+ response: {
3911
+ id: randomUUID(),
3912
+ timestamp: /* @__PURE__ */new Date(),
3913
+ modelId: "tripwire",
3914
+ messages: []
3915
+ },
3916
+ responseMessages: [],
3917
+ toolCalls: [],
3918
+ toolResults: [],
3919
+ warnings: void 0,
3920
+ request: {
3921
+ body: JSON.stringify({
3922
+ messages: []
3923
+ })
3924
+ },
3925
+ experimental_output: void 0,
3926
+ steps: void 0,
3927
+ experimental_providerMetadata: void 0,
3928
+ tripwire: true,
3929
+ tripwireReason: outputProcessorResult2.tripwireReason
3930
+ };
3931
+ return tripwireResult;
3932
+ }
3933
+ const newText2 = outputProcessorResult2.messageList.get.response.v2().map(msg => msg.content.parts.map(part => part.type === "text" ? part.text : "").join("")).join("");
3934
+ result2.text = newText2;
3935
+ if (finalOutputProcessors && finalOutputProcessors.length > 0) {
3936
+ const messages2 = outputProcessorResult2.messageList.get.response.v2();
3937
+ this.logger.debug("Checking messages for experimentalOutput metadata:", messages2.map(m => ({
3938
+ role: m.role,
3939
+ hasContentMetadata: !!m.content.metadata,
3940
+ contentMetadata: m.content.metadata
3941
+ })));
3942
+ const messagesWithStructuredData = messages2.filter(msg => msg.content.metadata && msg.content.metadata.structuredOutput);
3943
+ this.logger.debug("Messages with structured data:", messagesWithStructuredData.length);
3944
+ if (messagesWithStructuredData[0] && messagesWithStructuredData[0].content.metadata?.structuredOutput) {
3945
+ result2.object = messagesWithStructuredData[0].content.metadata.structuredOutput;
3946
+ this.logger.debug("Using structured data from processor metadata for result.object");
3947
+ } else {
3948
+ try {
3949
+ const processedOutput = JSON.parse(newText2);
3950
+ result2.object = processedOutput;
3951
+ this.logger.debug("Using fallback JSON parsing for result.object");
3952
+ } catch (error) {
3953
+ this.logger.warn("Failed to parse processed output as JSON, updating text only", {
3954
+ error
3955
+ });
3956
+ }
3957
+ }
3958
+ }
3264
3959
  await after({
3265
3960
  result: result2,
3266
- outputText: result2.text
3961
+ outputText: newText2
3267
3962
  });
3268
3963
  return result2;
3269
3964
  }
@@ -3272,9 +3967,64 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3272
3967
  structuredOutput: output
3273
3968
  });
3274
3969
  const outputText = JSON.stringify(result.object);
3970
+ const outputProcessorResult = await this.__runOutputProcessors({
3971
+ runtimeContext: mergedGenerateOptions.runtimeContext || new RuntimeContext(),
3972
+ messageList: new MessageList({
3973
+ threadId: llmOptions.threadId || "",
3974
+ resourceId: llmOptions.resourceId || ""
3975
+ }).add({
3976
+ role: "assistant",
3977
+ content: [{
3978
+ type: "text",
3979
+ text: outputText
3980
+ }]
3981
+ }, "response")
3982
+ });
3983
+ if (outputProcessorResult.tripwireTriggered) {
3984
+ const tripwireResult = {
3985
+ text: "",
3986
+ object: void 0,
3987
+ usage: {
3988
+ totalTokens: 0,
3989
+ promptTokens: 0,
3990
+ completionTokens: 0
3991
+ },
3992
+ finishReason: "other",
3993
+ response: {
3994
+ id: randomUUID(),
3995
+ timestamp: /* @__PURE__ */new Date(),
3996
+ modelId: "tripwire",
3997
+ messages: []
3998
+ },
3999
+ responseMessages: [],
4000
+ toolCalls: [],
4001
+ toolResults: [],
4002
+ warnings: void 0,
4003
+ request: {
4004
+ body: JSON.stringify({
4005
+ messages: []
4006
+ })
4007
+ },
4008
+ experimental_output: void 0,
4009
+ steps: void 0,
4010
+ experimental_providerMetadata: void 0,
4011
+ tripwire: true,
4012
+ tripwireReason: outputProcessorResult.tripwireReason
4013
+ };
4014
+ return tripwireResult;
4015
+ }
4016
+ const newText = outputProcessorResult.messageList.get.response.v2().map(msg => msg.content.parts.map(part => part.type === "text" ? part.text : "").join("")).join("");
4017
+ try {
4018
+ const processedObject = JSON.parse(newText);
4019
+ result.object = processedObject;
4020
+ } catch (error) {
4021
+ this.logger.warn("Failed to parse processed output as JSON, keeping original result", {
4022
+ error
4023
+ });
4024
+ }
3275
4025
  await after({
3276
4026
  result,
3277
- outputText,
4027
+ outputText: newText,
3278
4028
  structuredOutput: true
3279
4029
  });
3280
4030
  return result;
@@ -3371,6 +4121,7 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3371
4121
  });
3372
4122
  const streamResult = llm.__stream({
3373
4123
  ...llmOptions,
4124
+ experimental_output,
3374
4125
  onFinish: async result => {
3375
4126
  try {
3376
4127
  const outputText = result.text;
@@ -3389,8 +4140,7 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3389
4140
  runId
3390
4141
  });
3391
4142
  },
3392
- runId,
3393
- experimental_output
4143
+ runId
3394
4144
  });
3395
4145
  return streamResult;
3396
4146
  }
@@ -3452,16 +4202,47 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3452
4202
  experimental_output,
3453
4203
  ...llmOptions
3454
4204
  } = await before();
4205
+ let effectiveOutputProcessors = mergedStreamOptions.outputProcessors || (this.#outputProcessors ? typeof this.#outputProcessors === "function" ? await this.#outputProcessors({
4206
+ runtimeContext: mergedStreamOptions.runtimeContext || new RuntimeContext()
4207
+ }) : this.#outputProcessors : []);
4208
+ if (mergedStreamOptions.structuredOutput) {
4209
+ const structuredProcessor = new StructuredOutputProcessor(mergedStreamOptions.structuredOutput);
4210
+ effectiveOutputProcessors = effectiveOutputProcessors ? [...effectiveOutputProcessors, structuredProcessor] : [structuredProcessor];
4211
+ }
3455
4212
  if (output) {
3456
4213
  const streamResult = llm.__streamObject({
3457
4214
  ...llmOptions,
3458
4215
  onFinish: async result => {
3459
- onResult(result.object);
3460
4216
  try {
3461
4217
  const outputText = JSON.stringify(result.object);
4218
+ const processedResult = await this.__runOutputProcessors({
4219
+ runtimeContext: mergedStreamOptions.runtimeContext || new RuntimeContext(),
4220
+ outputProcessorOverrides: effectiveOutputProcessors,
4221
+ messageList: new MessageList({
4222
+ threadId: llmOptions.threadId || "",
4223
+ resourceId: llmOptions.resourceId || ""
4224
+ }).add({
4225
+ role: "assistant",
4226
+ content: [{
4227
+ type: "text",
4228
+ text: outputText
4229
+ }]
4230
+ }, "response")
4231
+ });
4232
+ const newText = processedResult.messageList.get.response.v2().map(msg => msg.content.parts.map(part => part.type === "text" ? part.text : "").join("")).join("");
4233
+ try {
4234
+ const processedObject = JSON.parse(newText);
4235
+ result.object = processedObject;
4236
+ onResult(processedObject);
4237
+ } catch (error) {
4238
+ this.logger.warn("Failed to parse processed output as JSON, keeping original result", {
4239
+ error
4240
+ });
4241
+ onResult(result.object);
4242
+ }
3462
4243
  await after({
3463
4244
  result,
3464
- outputText,
4245
+ outputText: newText,
3465
4246
  structuredOutput: true
3466
4247
  });
3467
4248
  } catch (e) {
@@ -3469,6 +4250,7 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3469
4250
  error: e,
3470
4251
  runId
3471
4252
  });
4253
+ onResult(result.object);
3472
4254
  }
3473
4255
  await onFinish?.({
3474
4256
  ...result,
@@ -3478,33 +4260,97 @@ Message ${msg.threadId && msg.threadId !== threadObject.id ? "from previous conv
3478
4260
  runId,
3479
4261
  structuredOutput: output
3480
4262
  });
3481
- return streamResult.fullStream;
4263
+ if (effectiveOutputProcessors?.length) {
4264
+ const runner = await this.getProcessorRunner({
4265
+ runtimeContext: mergedStreamOptions.runtimeContext || new RuntimeContext(),
4266
+ outputProcessorOverrides: effectiveOutputProcessors
4267
+ });
4268
+ return runner.runOutputProcessorsForStream(streamResult);
4269
+ }
4270
+ return Promise.resolve(streamResult.fullStream);
3482
4271
  } else {
3483
4272
  const streamResult = llm.__stream({
3484
4273
  ...llmOptions,
4274
+ experimental_output,
3485
4275
  onFinish: async result => {
3486
- onResult(result.text);
3487
4276
  try {
3488
4277
  const outputText = result.text;
4278
+ const processedResult = await this.__runOutputProcessors({
4279
+ runtimeContext: mergedStreamOptions.runtimeContext || new RuntimeContext(),
4280
+ outputProcessorOverrides: effectiveOutputProcessors,
4281
+ messageList: new MessageList({
4282
+ threadId: llmOptions.threadId || "",
4283
+ resourceId: llmOptions.resourceId || ""
4284
+ }).add({
4285
+ role: "assistant",
4286
+ content: [{
4287
+ type: "text",
4288
+ text: outputText
4289
+ }]
4290
+ }, "response")
4291
+ });
4292
+ const newText = processedResult.messageList.get.response.v2().map(msg => msg.content.parts.map(part => part.type === "text" ? part.text : "").join("")).join("");
4293
+ result.text = newText;
4294
+ let finalObject;
4295
+ if (effectiveOutputProcessors && effectiveOutputProcessors.length > 0) {
4296
+ const messages2 = processedResult.messageList.get.response.v2();
4297
+ const messagesWithStructuredData = messages2.filter(msg => msg.content.metadata && msg.content.metadata.structuredOutput);
4298
+ if (messagesWithStructuredData[0] && messagesWithStructuredData[0].content.metadata?.structuredOutput) {
4299
+ finalObject = messagesWithStructuredData[0].content.metadata.structuredOutput;
4300
+ } else if (experimental_output) {
4301
+ try {
4302
+ finalObject = JSON.parse(newText);
4303
+ } catch (error) {
4304
+ this.logger.warn("Failed to parse processed experimental_output as JSON, using null", {
4305
+ error
4306
+ });
4307
+ finalObject = null;
4308
+ }
4309
+ } else {
4310
+ finalObject = newText;
4311
+ }
4312
+ } else if (experimental_output) {
4313
+ try {
4314
+ finalObject = JSON.parse(newText);
4315
+ } catch (error) {
4316
+ this.logger.warn("Failed to parse processed experimental_output as JSON, using null", {
4317
+ error
4318
+ });
4319
+ finalObject = null;
4320
+ }
4321
+ } else {
4322
+ finalObject = newText;
4323
+ }
4324
+ result.object = finalObject;
4325
+ onResult(finalObject);
3489
4326
  await after({
3490
4327
  result,
3491
- outputText
4328
+ outputText: newText
3492
4329
  });
3493
4330
  } catch (e) {
3494
4331
  this.logger.error("Error saving memory on finish", {
3495
4332
  error: e,
3496
4333
  runId
3497
4334
  });
4335
+ onResult(result.text);
3498
4336
  }
3499
4337
  await onFinish?.({
3500
4338
  ...result,
3501
4339
  runId
3502
4340
  });
3503
4341
  },
3504
- runId,
3505
- experimental_output
4342
+ runId
3506
4343
  });
3507
- return streamResult.fullStream;
4344
+ if (effectiveOutputProcessors?.length) {
4345
+ this.logger.debug("running output processors for stream");
4346
+ const runner = await this.getProcessorRunner({
4347
+ runtimeContext: mergedStreamOptions.runtimeContext || new RuntimeContext(),
4348
+ outputProcessorOverrides: effectiveOutputProcessors
4349
+ });
4350
+ return runner.runOutputProcessorsForStream(streamResult);
4351
+ }
4352
+ this.logger.debug("no output processors for stream");
4353
+ return Promise.resolve(streamResult.fullStream);
3508
4354
  }
3509
4355
  }
3510
4356
  });
@@ -6653,6 +7499,6 @@ var LegacyWorkflow = class extends MastraBase {
6653
7499
  }
6654
7500
  };
6655
7501
 
6656
- export { Agent, LanguageDetector, LegacyStep, LegacyWorkflow, ModerationInputProcessor, PIIDetector, PromptInjectionDetector, TripWire, UnicodeNormalizer, WhenConditionReturnValue, agentToStep, getActivePathsAndStatus, getResultActivePaths, getStepResult, getSuspendedPaths, isAgent, isConditionalKey, isErrorEvent, isFinalState, isLimboState, isTransitionEvent, isVariableReference, isWorkflow, mergeChildValue, recursivelyCheckForFinalState, resolveVariables, runScorer, updateStepInHierarchy, workflowToStep };
6657
- //# sourceMappingURL=chunk-IL5SHDY4.js.map
6658
- //# sourceMappingURL=chunk-IL5SHDY4.js.map
7502
+ export { Agent, LanguageDetector, LanguageDetectorInputProcessor, LegacyStep, LegacyWorkflow, ModerationInputProcessor, ModerationProcessor, PIIDetector, PIIDetectorInputProcessor, PromptInjectionDetector, PromptInjectionDetectorInputProcessor, StructuredOutputProcessor, TripWire, UnicodeNormalizer, UnicodeNormalizerInputProcessor, WhenConditionReturnValue, agentToStep, getActivePathsAndStatus, getResultActivePaths, getStepResult, getSuspendedPaths, isAgent, isConditionalKey, isErrorEvent, isFinalState, isLimboState, isTransitionEvent, isVariableReference, isWorkflow, mergeChildValue, recursivelyCheckForFinalState, resolveVariables, runScorer, updateStepInHierarchy, workflowToStep };
7503
+ //# sourceMappingURL=chunk-DAMIFZPW.js.map
7504
+ //# sourceMappingURL=chunk-DAMIFZPW.js.map