@mastra/core 0.10.12 → 0.10.13

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 (235) hide show
  1. package/package.json +1 -1
  2. package/a2a.d.ts +0 -1
  3. package/agent.d.ts +0 -1
  4. package/base.d.ts +0 -1
  5. package/bundler.d.ts +0 -1
  6. package/deployer.d.ts +0 -1
  7. package/di.d.ts +0 -1
  8. package/dist/a2a/index.cjs +0 -79
  9. package/dist/a2a/index.d.cts +0 -780
  10. package/dist/a2a/index.d.ts +0 -780
  11. package/dist/a2a/index.js +0 -68
  12. package/dist/agent/index.cjs +0 -15
  13. package/dist/agent/index.d.cts +0 -29
  14. package/dist/agent/index.d.ts +0 -29
  15. package/dist/agent/index.js +0 -2
  16. package/dist/base-B_y9sMg0.d.cts +0 -162
  17. package/dist/base-CS5eSXbL.d.cts +0 -4117
  18. package/dist/base-ClrXcCRx.d.ts +0 -162
  19. package/dist/base-DBhKcre4.d.ts +0 -4117
  20. package/dist/base.cjs +0 -10
  21. package/dist/base.d.cts +0 -6
  22. package/dist/base.d.ts +0 -6
  23. package/dist/base.js +0 -1
  24. package/dist/bundler/index.cjs +0 -10
  25. package/dist/bundler/index.d.cts +0 -30
  26. package/dist/bundler/index.d.ts +0 -30
  27. package/dist/bundler/index.js +0 -1
  28. package/dist/chunk-2GRYVZ2O.cjs +0 -244
  29. package/dist/chunk-2HDFKWFU.js +0 -2249
  30. package/dist/chunk-32C7JDIZ.js +0 -1
  31. package/dist/chunk-4UBOJFSL.js +0 -989
  32. package/dist/chunk-4UWPFBC6.js +0 -88
  33. package/dist/chunk-4Z3OU5RY.cjs +0 -31
  34. package/dist/chunk-5HTMDAXP.js +0 -359
  35. package/dist/chunk-5IEKR756.js +0 -53
  36. package/dist/chunk-5YDTZN2X.js +0 -114
  37. package/dist/chunk-6UNGH46J.js +0 -75
  38. package/dist/chunk-6Y4UL5Z6.cjs +0 -94
  39. package/dist/chunk-7F6BQXE2.cjs +0 -425
  40. package/dist/chunk-7H2GET5Z.cjs +0 -668
  41. package/dist/chunk-7HZ6NIAF.cjs +0 -2
  42. package/dist/chunk-7MZNOW6W.cjs +0 -2263
  43. package/dist/chunk-7XQIPES3.js +0 -668
  44. package/dist/chunk-AKYTYALY.js +0 -70
  45. package/dist/chunk-ATXBSEFT.js +0 -22
  46. package/dist/chunk-B6TOBUS6.cjs +0 -80
  47. package/dist/chunk-B7SQOKEC.cjs +0 -91
  48. package/dist/chunk-BB4KXGBU.js +0 -83
  49. package/dist/chunk-BB6DPGIV.cjs +0 -6
  50. package/dist/chunk-C4LMN2IR.js +0 -27
  51. package/dist/chunk-DFFVEKIG.js +0 -407
  52. package/dist/chunk-E7AX3U6M.cjs +0 -659
  53. package/dist/chunk-EWCOOO3H.js +0 -5007
  54. package/dist/chunk-F2WMR75C.cjs +0 -183
  55. package/dist/chunk-FBKJ3652.cjs +0 -5034
  56. package/dist/chunk-FEYYOBBG.cjs +0 -24
  57. package/dist/chunk-FL5SZ2XU.js +0 -181
  58. package/dist/chunk-GH2KM66J.js +0 -37
  59. package/dist/chunk-GWFS5DAR.cjs +0 -37
  60. package/dist/chunk-HNEE7IF4.js +0 -60
  61. package/dist/chunk-HSVOEWAM.cjs +0 -2
  62. package/dist/chunk-J52TXHZV.cjs +0 -73
  63. package/dist/chunk-JNMQKJH4.js +0 -10
  64. package/dist/chunk-JQOMTERC.js +0 -89
  65. package/dist/chunk-LABUWBKX.cjs +0 -71
  66. package/dist/chunk-LXFZUKP3.cjs +0 -34
  67. package/dist/chunk-MP2QBLUJ.cjs +0 -70
  68. package/dist/chunk-MUNFCOMB.cjs +0 -62
  69. package/dist/chunk-NH5WJNNS.js +0 -1
  70. package/dist/chunk-P3Q73CAW.cjs +0 -55
  71. package/dist/chunk-PA2YIVIT.js +0 -61
  72. package/dist/chunk-QFTBW7ZZ.cjs +0 -2
  73. package/dist/chunk-QQ5K5TZE.cjs +0 -619
  74. package/dist/chunk-QUSEDVYI.cjs +0 -991
  75. package/dist/chunk-R4V75T7J.js +0 -1
  76. package/dist/chunk-SGGPJWRQ.js +0 -69
  77. package/dist/chunk-ST5RMVLG.cjs +0 -87
  78. package/dist/chunk-TC2SCOTE.js +0 -605
  79. package/dist/chunk-U64IJDC5.cjs +0 -109
  80. package/dist/chunk-UX3B6S2I.cjs +0 -65
  81. package/dist/chunk-V5D2LIF5.js +0 -68
  82. package/dist/chunk-VG4OPO2R.js +0 -240
  83. package/dist/chunk-WQNOATKB.js +0 -103
  84. package/dist/chunk-Y7D2JLKS.js +0 -4
  85. package/dist/chunk-YJEHXYK5.js +0 -657
  86. package/dist/chunk-YOQP5T77.js +0 -32
  87. package/dist/chunk-ZIZ3CVHN.cjs +0 -120
  88. package/dist/chunk-ZPOUMTTH.cjs +0 -362
  89. package/dist/chunk-ZZLBNB3U.cjs +0 -12
  90. package/dist/deployer/index.cjs +0 -10
  91. package/dist/deployer/index.d.cts +0 -19
  92. package/dist/deployer/index.d.ts +0 -19
  93. package/dist/deployer/index.js +0 -1
  94. package/dist/di/index.cjs +0 -10
  95. package/dist/di/index.d.cts +0 -1
  96. package/dist/di/index.d.ts +0 -1
  97. package/dist/di/index.js +0 -1
  98. package/dist/error/index.cjs +0 -22
  99. package/dist/error/index.d.cts +0 -86
  100. package/dist/error/index.d.ts +0 -86
  101. package/dist/error/index.js +0 -1
  102. package/dist/eval/index.cjs +0 -14
  103. package/dist/eval/index.d.cts +0 -43
  104. package/dist/eval/index.d.ts +0 -43
  105. package/dist/eval/index.js +0 -1
  106. package/dist/hooks/index.cjs +0 -18
  107. package/dist/hooks/index.d.cts +0 -33
  108. package/dist/hooks/index.d.ts +0 -33
  109. package/dist/hooks/index.js +0 -1
  110. package/dist/index.cjs +0 -281
  111. package/dist/index.d.cts +0 -92
  112. package/dist/index.d.ts +0 -92
  113. package/dist/index.js +0 -112
  114. package/dist/integration/index.cjs +0 -14
  115. package/dist/integration/index.d.cts +0 -65
  116. package/dist/integration/index.d.ts +0 -65
  117. package/dist/integration/index.js +0 -1
  118. package/dist/llm/index.cjs +0 -10
  119. package/dist/llm/index.d.cts +0 -29
  120. package/dist/llm/index.d.ts +0 -29
  121. package/dist/llm/index.js +0 -1
  122. package/dist/logger/index.cjs +0 -43
  123. package/dist/logger/index.d.cts +0 -96
  124. package/dist/logger/index.d.ts +0 -96
  125. package/dist/logger/index.js +0 -2
  126. package/dist/logger-B8XXh6ya.d.cts +0 -159
  127. package/dist/logger-Bpa2oLL4.d.ts +0 -159
  128. package/dist/mastra/index.cjs +0 -10
  129. package/dist/mastra/index.d.cts +0 -29
  130. package/dist/mastra/index.d.ts +0 -29
  131. package/dist/mastra/index.js +0 -1
  132. package/dist/mcp/index.cjs +0 -106
  133. package/dist/mcp/index.d.cts +0 -29
  134. package/dist/mcp/index.d.ts +0 -29
  135. package/dist/mcp/index.js +0 -100
  136. package/dist/memory/index.cjs +0 -18
  137. package/dist/memory/index.d.cts +0 -29
  138. package/dist/memory/index.d.ts +0 -29
  139. package/dist/memory/index.js +0 -1
  140. package/dist/network/index.cjs +0 -311
  141. package/dist/network/index.d.cts +0 -29
  142. package/dist/network/index.d.ts +0 -29
  143. package/dist/network/index.js +0 -309
  144. package/dist/network/vNext/index.cjs +0 -873
  145. package/dist/network/vNext/index.d.cts +0 -29
  146. package/dist/network/vNext/index.d.ts +0 -29
  147. package/dist/network/vNext/index.js +0 -871
  148. package/dist/relevance/index.cjs +0 -18
  149. package/dist/relevance/index.d.cts +0 -49
  150. package/dist/relevance/index.d.ts +0 -49
  151. package/dist/relevance/index.js +0 -1
  152. package/dist/runtime-context/index.cjs +0 -10
  153. package/dist/runtime-context/index.d.cts +0 -52
  154. package/dist/runtime-context/index.d.ts +0 -52
  155. package/dist/runtime-context/index.js +0 -1
  156. package/dist/server/index.cjs +0 -62
  157. package/dist/server/index.d.cts +0 -52
  158. package/dist/server/index.d.ts +0 -52
  159. package/dist/server/index.js +0 -59
  160. package/dist/storage/index.cjs +0 -336
  161. package/dist/storage/index.d.cts +0 -149
  162. package/dist/storage/index.d.ts +0 -149
  163. package/dist/storage/index.js +0 -303
  164. package/dist/telemetry/index.cjs +0 -30
  165. package/dist/telemetry/index.d.cts +0 -75
  166. package/dist/telemetry/index.d.ts +0 -75
  167. package/dist/telemetry/index.js +0 -1
  168. package/dist/telemetry/otel-vendor.cjs +0 -103
  169. package/dist/telemetry/otel-vendor.d.cts +0 -20
  170. package/dist/telemetry/otel-vendor.d.ts +0 -20
  171. package/dist/telemetry/otel-vendor.js +0 -57
  172. package/dist/tools/index.cjs +0 -18
  173. package/dist/tools/index.d.cts +0 -41
  174. package/dist/tools/index.d.ts +0 -41
  175. package/dist/tools/index.js +0 -1
  176. package/dist/tts/index.cjs +0 -10
  177. package/dist/tts/index.d.cts +0 -28
  178. package/dist/tts/index.d.ts +0 -28
  179. package/dist/tts/index.js +0 -1
  180. package/dist/types-Bo1uigWx.d.cts +0 -17
  181. package/dist/types-Bo1uigWx.d.ts +0 -17
  182. package/dist/utils.cjs +0 -58
  183. package/dist/utils.d.cts +0 -149
  184. package/dist/utils.d.ts +0 -149
  185. package/dist/utils.js +0 -1
  186. package/dist/vector/filter/index.cjs +0 -192
  187. package/dist/vector/filter/index.d.cts +0 -128
  188. package/dist/vector/filter/index.d.ts +0 -128
  189. package/dist/vector/filter/index.js +0 -190
  190. package/dist/vector/index.cjs +0 -10
  191. package/dist/vector/index.d.cts +0 -77
  192. package/dist/vector/index.d.ts +0 -77
  193. package/dist/vector/index.js +0 -1
  194. package/dist/voice/index.cjs +0 -18
  195. package/dist/voice/index.d.cts +0 -29
  196. package/dist/voice/index.d.ts +0 -29
  197. package/dist/voice/index.js +0 -1
  198. package/dist/workflows/constants.cjs +0 -10
  199. package/dist/workflows/constants.d.cts +0 -3
  200. package/dist/workflows/constants.d.ts +0 -3
  201. package/dist/workflows/constants.js +0 -1
  202. package/dist/workflows/index.cjs +0 -42
  203. package/dist/workflows/index.d.cts +0 -282
  204. package/dist/workflows/index.d.ts +0 -282
  205. package/dist/workflows/index.js +0 -1
  206. package/dist/workflows/legacy/index.cjs +0 -90
  207. package/dist/workflows/legacy/index.d.cts +0 -91
  208. package/dist/workflows/legacy/index.d.ts +0 -91
  209. package/dist/workflows/legacy/index.js +0 -1
  210. package/error.d.ts +0 -1
  211. package/eval.d.ts +0 -1
  212. package/hooks.d.ts +0 -1
  213. package/integration.d.ts +0 -1
  214. package/llm.d.ts +0 -1
  215. package/logger.d.ts +0 -1
  216. package/mastra.d.ts +0 -1
  217. package/mcp.d.ts +0 -1
  218. package/memory.d.ts +0 -1
  219. package/network/vNext.d.ts +0 -1
  220. package/network.d.ts +0 -1
  221. package/relevance.d.ts +0 -1
  222. package/runtime-context.d.ts +0 -1
  223. package/server.d.ts +0 -1
  224. package/storage.d.ts +0 -1
  225. package/telemetry/otel-vendor.d.ts +0 -1
  226. package/telemetry.d.ts +0 -1
  227. package/tools.d.ts +0 -1
  228. package/tts.d.ts +0 -1
  229. package/utils.d.ts +0 -1
  230. package/vector/filter.d.ts +0 -1
  231. package/vector.d.ts +0 -1
  232. package/voice.d.ts +0 -1
  233. package/workflows/_constants.d.ts +0 -1
  234. package/workflows/legacy.d.ts +0 -1
  235. package/workflows.d.ts +0 -1
@@ -1,873 +0,0 @@
1
- 'use strict';
2
-
3
- var chunk7MZNOW6W_cjs = require('../../chunk-7MZNOW6W.cjs');
4
- var chunkBB6DPGIV_cjs = require('../../chunk-BB6DPGIV.cjs');
5
- var chunkFBKJ3652_cjs = require('../../chunk-FBKJ3652.cjs');
6
- var chunkP3Q73CAW_cjs = require('../../chunk-P3Q73CAW.cjs');
7
- var chunkZIZ3CVHN_cjs = require('../../chunk-ZIZ3CVHN.cjs');
8
- var chunkLABUWBKX_cjs = require('../../chunk-LABUWBKX.cjs');
9
- var crypto = require('crypto');
10
- var zod = require('zod');
11
- var zodToJsonSchema = require('zod-to-json-schema');
12
-
13
- var RESOURCE_TYPES = zod.z.enum(["agent", "workflow", "none", "tool", "none"]);
14
- var NewAgentNetwork = class extends chunkP3Q73CAW_cjs.MastraBase {
15
- id;
16
- name;
17
- #instructions;
18
- #model;
19
- #agents;
20
- #defaultAgent;
21
- #workflows;
22
- #tools;
23
- #memory;
24
- #mastra;
25
- constructor({
26
- id,
27
- name,
28
- instructions,
29
- model,
30
- agents,
31
- workflows,
32
- memory,
33
- tools,
34
- defaultAgent
35
- }) {
36
- super({
37
- component: chunkZIZ3CVHN_cjs.RegisteredLogger.NETWORK,
38
- name: name || "NewAgentNetwork"
39
- });
40
- this.id = id;
41
- this.name = name;
42
- this.#instructions = instructions;
43
- this.#model = model;
44
- this.#agents = agents;
45
- this.#workflows = workflows;
46
- this.#memory = memory;
47
- this.#tools = tools;
48
- this.#defaultAgent = defaultAgent;
49
- }
50
- __registerMastra(mastra) {
51
- this.#mastra = mastra;
52
- }
53
- async getAgents({ runtimeContext }) {
54
- let agentsToUse;
55
- if (typeof this.#agents === "function") {
56
- agentsToUse = await this.#agents({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
57
- } else {
58
- agentsToUse = this.#agents;
59
- }
60
- return agentsToUse;
61
- }
62
- async getWorkflows({ runtimeContext }) {
63
- let workflowsToUse;
64
- if (typeof this.#workflows === "function") {
65
- workflowsToUse = await this.#workflows({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
66
- } else {
67
- workflowsToUse = this.#workflows || {};
68
- }
69
- return workflowsToUse;
70
- }
71
- async getTools({ runtimeContext }) {
72
- let toolsToUse;
73
- if (typeof this.#tools === "function") {
74
- toolsToUse = await this.#tools({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
75
- } else {
76
- toolsToUse = this.#tools || {};
77
- }
78
- return toolsToUse;
79
- }
80
- async getMemory({ runtimeContext }) {
81
- let memoryToUse;
82
- if (!this.#memory) {
83
- return;
84
- }
85
- if (typeof this.#memory === "function") {
86
- memoryToUse = await this.#memory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
87
- } else {
88
- memoryToUse = this.#memory;
89
- }
90
- return memoryToUse;
91
- }
92
- async getInstructions({ runtimeContext }) {
93
- let instructionsToUse = this.#instructions;
94
- if (typeof instructionsToUse === "function") {
95
- instructionsToUse = await instructionsToUse({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
96
- }
97
- return instructionsToUse;
98
- }
99
- async getRoutingAgent({ runtimeContext }) {
100
- const instructionsToUse = await this.getInstructions({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
101
- const memoryToUse = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
102
- const agentsToUse = await this.getAgents({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
103
- const workflowsToUse = await this.getWorkflows({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
104
- const toolsToUse = await this.getTools({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
105
- const agentList = Object.entries(agentsToUse).map(([name, agent]) => {
106
- return ` - **${name}**: ${agent.getDescription()}`;
107
- }).join("\n");
108
- const workflowList = Object.entries(workflowsToUse).map(([name, workflow]) => {
109
- return ` - **${name}**: ${workflow.description}, input schema: ${JSON.stringify(
110
- zodToJsonSchema.zodToJsonSchema(workflow.inputSchema)
111
- )}`;
112
- }).join("\n");
113
- const toolList = Object.entries(toolsToUse).map(([name, tool]) => {
114
- return ` - **${name}**: ${tool.description}, input schema: ${JSON.stringify(
115
- zodToJsonSchema.zodToJsonSchema(tool.inputSchema || zod.z.object({}))
116
- )}`;
117
- }).join("\n");
118
- const instructions = `
119
- You are a router in a network of specialized AI agents.
120
- Your job is to decide which agent should handle each step of a task.
121
-
122
- If asking for completion of a task, make sure to follow system instructions closely.
123
-
124
- ## System Instructions
125
- ${instructionsToUse}
126
-
127
- You can only pick agents and workflows that are available in the lists below. Never call any agents or workflows that are not available in the lists below.
128
-
129
- ## Available Agents in Network
130
- ${agentList}
131
-
132
- ## Available Workflows in Network (make sure to use inputs corresponding to the input schema when calling a workflow)
133
- ${workflowList}
134
-
135
- ## Available Tools in Network (make sure to use inputs corresponding to the input schema when calling a tool)
136
- ${toolList}
137
-
138
- ${this.#defaultAgent ? `If none of the agents or workflows are appropriate, call the default agent: ${this.#defaultAgent.name}.This should not be done lightly. You should only do this if you have exhausted all other options.` : ""}
139
-
140
- If you have multiple entries that need to be called with a workflow or agent, call them separately with each input.
141
- When calling a workflow, the prompt should be a JSON value that corresponds to the input schema of the workflow. The JSON value is stringified.
142
- When calling a tool, the prompt should be a JSON value that corresponds to the input schema of the tool. The JSON value is stringified.
143
- When calling an agent, the prompt should be a text value, like you would call an LLM in a chat interface.
144
-
145
- Keep in mind that the user only sees the final result of the task. When reviewing completion, you should know that the user will not see the intermediate results.
146
- `;
147
- return new chunkFBKJ3652_cjs.Agent({
148
- name: "routing-agent",
149
- instructions,
150
- model: this.#model,
151
- memory: memoryToUse
152
- });
153
- }
154
- async loop(message, {
155
- runtimeContext,
156
- maxIterations
157
- }) {
158
- const networkWorkflow = this.createWorkflow({ runtimeContext });
159
- const finalStep = chunk7MZNOW6W_cjs.createStep({
160
- id: "final-step",
161
- inputSchema: networkWorkflow.outputSchema,
162
- outputSchema: networkWorkflow.outputSchema,
163
- execute: async ({ inputData }) => {
164
- if (maxIterations && inputData.iteration >= maxIterations) {
165
- return {
166
- ...inputData,
167
- completionReason: `Max iterations reached: ${maxIterations}`
168
- };
169
- }
170
- return inputData;
171
- }
172
- });
173
- const mainWorkflow = chunk7MZNOW6W_cjs.createWorkflow({
174
- id: "Agent-Network-Main-Workflow",
175
- inputSchema: zod.z.object({
176
- iteration: zod.z.number(),
177
- task: zod.z.string(),
178
- resourceType: RESOURCE_TYPES
179
- }),
180
- outputSchema: zod.z.object({
181
- text: zod.z.string(),
182
- iteration: zod.z.number()
183
- })
184
- }).dountil(networkWorkflow, async ({ inputData }) => {
185
- return inputData.isComplete || maxIterations && inputData.iteration >= maxIterations;
186
- }).then(finalStep).commit();
187
- const run = mainWorkflow.createRun();
188
- const result = await run.start({
189
- inputData: {
190
- task: message,
191
- resourceType: "none",
192
- iteration: 0
193
- }
194
- });
195
- if (result.status === "failed") {
196
- throw result.error;
197
- }
198
- if (result.status === "suspended") {
199
- throw new Error("Workflow suspended");
200
- }
201
- return result;
202
- }
203
- async loopStream(message, {
204
- runtimeContext,
205
- maxIterations,
206
- threadId,
207
- resourceId
208
- }) {
209
- const networkWorkflow = this.createWorkflow({ runtimeContext });
210
- const finalStep = chunk7MZNOW6W_cjs.createStep({
211
- id: "final-step",
212
- inputSchema: networkWorkflow.outputSchema,
213
- outputSchema: networkWorkflow.outputSchema,
214
- execute: async ({ inputData }) => {
215
- if (maxIterations && inputData.iteration >= maxIterations) {
216
- return {
217
- ...inputData,
218
- completionReason: `Max iterations reached: ${maxIterations}`
219
- };
220
- }
221
- return inputData;
222
- }
223
- });
224
- const mainWorkflow = chunk7MZNOW6W_cjs.createWorkflow({
225
- id: "Agent-Network-Main-Workflow",
226
- inputSchema: zod.z.object({
227
- iteration: zod.z.number(),
228
- task: zod.z.string(),
229
- resourceId: zod.z.string(),
230
- resourceType: RESOURCE_TYPES,
231
- result: zod.z.string().optional(),
232
- threadId: zod.z.string().optional(),
233
- threadResourceId: zod.z.string().optional(),
234
- isOneOff: zod.z.boolean(),
235
- verboseIntrospection: zod.z.boolean()
236
- }),
237
- outputSchema: zod.z.object({
238
- task: zod.z.string(),
239
- resourceId: zod.z.string(),
240
- resourceType: RESOURCE_TYPES,
241
- prompt: zod.z.string(),
242
- result: zod.z.string(),
243
- isComplete: zod.z.boolean().optional(),
244
- completionReason: zod.z.string().optional(),
245
- iteration: zod.z.number()
246
- })
247
- }).dountil(networkWorkflow, async ({ inputData }) => {
248
- return inputData.isComplete || maxIterations && inputData.iteration >= maxIterations;
249
- }).then(finalStep).commit();
250
- const run = mainWorkflow.createRun();
251
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
252
- await memory?.saveMessages({
253
- messages: [
254
- {
255
- id: crypto.randomUUID(),
256
- type: "text",
257
- role: "user",
258
- content: { parts: [{ type: "text", text: message }], format: 2 },
259
- createdAt: /* @__PURE__ */ new Date(),
260
- threadId: threadId || run.runId,
261
- resourceId: resourceId || this.name
262
- }
263
- ],
264
- format: "v2"
265
- });
266
- return run.stream({
267
- inputData: {
268
- task: message,
269
- resourceId: "",
270
- resourceType: "none",
271
- iteration: 0,
272
- threadResourceId: resourceId,
273
- threadId,
274
- isOneOff: false,
275
- verboseIntrospection: true
276
- }
277
- });
278
- }
279
- createWorkflow({ runtimeContext }) {
280
- const runId = crypto.randomUUID();
281
- const runtimeContextToUse = runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext();
282
- const routingStep = chunk7MZNOW6W_cjs.createStep({
283
- id: "routing-step",
284
- inputSchema: zod.z.object({
285
- task: zod.z.string(),
286
- resourceId: zod.z.string(),
287
- resourceType: RESOURCE_TYPES,
288
- result: zod.z.string().optional(),
289
- iteration: zod.z.number(),
290
- threadId: zod.z.string().optional(),
291
- threadResourceId: zod.z.string().optional(),
292
- isOneOff: zod.z.boolean(),
293
- verboseIntrospection: zod.z.boolean()
294
- }),
295
- outputSchema: zod.z.object({
296
- task: zod.z.string(),
297
- resourceId: zod.z.string(),
298
- resourceType: RESOURCE_TYPES,
299
- prompt: zod.z.string(),
300
- result: zod.z.string(),
301
- isComplete: zod.z.boolean().optional(),
302
- selectionReason: zod.z.string(),
303
- iteration: zod.z.number()
304
- }),
305
- execute: async ({ inputData, getInitData }) => {
306
- const initData = await getInitData();
307
- const routingAgent = await this.getRoutingAgent({ runtimeContext: runtimeContextToUse });
308
- let completionResult;
309
- if (inputData.resourceType !== "none" && inputData?.result) {
310
- const completionPrompt = `
311
- The ${inputData.resourceType} ${inputData.resourceId} has contributed to the task.
312
- This is the result from the agent: ${inputData.result}
313
-
314
- You need to evaluate that our task is complete. Pay very close attention to the SYSTEM INSTRUCTIONS for when the task is considered complete. Only return true if the task is complete according to the system instructions. Pay close attention to the finalResult and completionReason.
315
- Original task: ${inputData.task}
316
-
317
- {
318
- "isComplete": boolean,
319
- "completionReason": string,
320
- "finalResult": string
321
- }
322
- `;
323
- completionResult = await routingAgent.generate([{ role: "assistant", content: completionPrompt }], {
324
- output: zod.z.object({
325
- isComplete: zod.z.boolean(),
326
- finalResult: zod.z.string(),
327
- completionReason: zod.z.string()
328
- }),
329
- threadId: initData?.threadId ?? runId,
330
- resourceId: initData?.threadResourceId ?? this.name
331
- });
332
- if (completionResult.object.isComplete) {
333
- return {
334
- task: inputData.task,
335
- resourceId: "",
336
- resourceType: "none",
337
- prompt: "",
338
- result: completionResult.object.finalResult,
339
- isComplete: true,
340
- selectionReason: completionResult.object.completionReason,
341
- iteration: inputData.iteration + 1
342
- };
343
- }
344
- }
345
- const result = await routingAgent.generate(
346
- [
347
- {
348
- role: "assistant",
349
- content: `
350
- ${inputData.isOneOff ? "You are executing just one primitive based on the user task. Make sure to pick the primitive that is the best suited to accomplish the whole task. Primitives that execute only part of the task should be avoided." : "You will be calling just *one* primitive at a time to accomplish the user task, every call to you is one decision in the process of accomplishing the user task. Make sure to pick primitives that are the best suited to accomplish the whole task. Completeness is the highest priority."}
351
-
352
- The user has given you the following task:
353
- ${inputData.task}
354
- ${completionResult ? `
355
-
356
- ${completionResult.object.finalResult}` : ""}
357
-
358
- Please select the most appropriate primitive to handle this task and the prompt to be sent to the primitive.
359
- If you are calling the same agent again, make sure to adjust the prompt to be more specific.
360
-
361
- {
362
- "resourceId": string,
363
- "resourceType": "agent" | "workflow" | "tool",
364
- "prompt": string,
365
- "selectionReason": string
366
- }
367
-
368
- The 'selectionReason' property should explain why you picked the primitive${inputData.verboseIntrospection ? ", as well as why the other primitives were not picked." : "."}
369
- `
370
- }
371
- ],
372
- {
373
- output: zod.z.object({
374
- resourceId: zod.z.string(),
375
- resourceType: RESOURCE_TYPES,
376
- prompt: zod.z.string(),
377
- selectionReason: zod.z.string()
378
- }),
379
- threadId: initData?.threadId ?? runId,
380
- resourceId: initData?.threadResourceId ?? this.name
381
- }
382
- );
383
- return {
384
- task: inputData.task,
385
- result: "",
386
- resourceId: result.object.resourceId,
387
- resourceType: result.object.resourceType,
388
- prompt: result.object.prompt,
389
- isComplete: result.object.resourceId === "none" && result.object.resourceType === "none" ? true : false,
390
- selectionReason: result.object.selectionReason,
391
- iteration: inputData.iteration + 1
392
- };
393
- }
394
- });
395
- const agentStep = chunk7MZNOW6W_cjs.createStep({
396
- id: "agent-step",
397
- inputSchema: zod.z.object({
398
- task: zod.z.string(),
399
- resourceId: zod.z.string(),
400
- resourceType: RESOURCE_TYPES,
401
- prompt: zod.z.string(),
402
- result: zod.z.string(),
403
- isComplete: zod.z.boolean().optional(),
404
- selectionReason: zod.z.string(),
405
- iteration: zod.z.number()
406
- }),
407
- outputSchema: zod.z.object({
408
- task: zod.z.string(),
409
- resourceId: zod.z.string(),
410
- resourceType: RESOURCE_TYPES,
411
- result: zod.z.string(),
412
- isComplete: zod.z.boolean().optional(),
413
- iteration: zod.z.number()
414
- }),
415
- execute: async ({ inputData, [chunkBB6DPGIV_cjs.EMITTER_SYMBOL]: emitter, getInitData }) => {
416
- const agentsMap = await this.getAgents({ runtimeContext: runtimeContextToUse });
417
- const agentId = inputData.resourceId;
418
- const agent = agentsMap[inputData.resourceId];
419
- if (!agent) {
420
- throw new Error(`Agent ${agentId} not found`);
421
- }
422
- let streamPromise = {};
423
- streamPromise.promise = new Promise((resolve, reject) => {
424
- streamPromise.resolve = resolve;
425
- streamPromise.reject = reject;
426
- });
427
- const toolData = {
428
- name: agent.name,
429
- args: inputData
430
- };
431
- await emitter.emit("watch-v2", {
432
- type: "tool-call-streaming-start",
433
- ...toolData
434
- });
435
- const { fullStream } = await agent.stream(inputData.prompt, {
436
- // resourceId: inputData.resourceId,
437
- // threadId: inputData.threadId,
438
- onFinish: (result) => {
439
- streamPromise.resolve(result.text);
440
- }
441
- });
442
- for await (const chunk of fullStream) {
443
- switch (chunk.type) {
444
- case "text-delta":
445
- await emitter.emit("watch-v2", {
446
- type: "tool-call-delta",
447
- ...toolData,
448
- argsTextDelta: chunk.textDelta
449
- });
450
- break;
451
- case "step-start":
452
- case "step-finish":
453
- case "finish":
454
- case "tool-call":
455
- case "tool-result":
456
- case "tool-call-streaming-start":
457
- case "tool-call-delta":
458
- break;
459
- case "source":
460
- case "file":
461
- default:
462
- await emitter.emit("watch-v2", chunk);
463
- break;
464
- }
465
- }
466
- const finalResult = await streamPromise.promise;
467
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
468
- const initData = await getInitData();
469
- await memory?.saveMessages({
470
- messages: [
471
- {
472
- id: crypto.randomUUID(),
473
- type: "text",
474
- role: "assistant",
475
- content: { parts: [{ type: "text", text: finalResult }], format: 2 },
476
- createdAt: /* @__PURE__ */ new Date(),
477
- threadId: initData.threadId || runId,
478
- resourceId: initData.threadResourceId || this.name
479
- }
480
- ],
481
- format: "v2"
482
- });
483
- return {
484
- task: inputData.task,
485
- resourceId: inputData.resourceId,
486
- resourceType: inputData.resourceType,
487
- result: finalResult,
488
- isComplete: false,
489
- iteration: inputData.iteration
490
- };
491
- }
492
- });
493
- const workflowStep = chunk7MZNOW6W_cjs.createStep({
494
- id: "workflow-step",
495
- inputSchema: zod.z.object({
496
- task: zod.z.string(),
497
- resourceId: zod.z.string(),
498
- resourceType: RESOURCE_TYPES,
499
- prompt: zod.z.string(),
500
- result: zod.z.string(),
501
- isComplete: zod.z.boolean().optional(),
502
- selectionReason: zod.z.string(),
503
- iteration: zod.z.number()
504
- }),
505
- outputSchema: zod.z.object({
506
- task: zod.z.string(),
507
- resourceId: zod.z.string(),
508
- resourceType: RESOURCE_TYPES,
509
- result: zod.z.string(),
510
- isComplete: zod.z.boolean().optional(),
511
- iteration: zod.z.number()
512
- }),
513
- execute: async ({ inputData, [chunkBB6DPGIV_cjs.EMITTER_SYMBOL]: emitter, getInitData }) => {
514
- const workflowsMap = await this.getWorkflows({ runtimeContext: runtimeContextToUse });
515
- const wf = workflowsMap[inputData.resourceId];
516
- if (!wf) {
517
- throw new Error(`Workflow ${inputData.resourceId} not found`);
518
- }
519
- let input;
520
- try {
521
- input = JSON.parse(inputData.prompt);
522
- } catch (e) {
523
- console.error(e);
524
- throw new Error(`Invalid task input: ${inputData.task}`);
525
- }
526
- let streamPromise = {};
527
- streamPromise.promise = new Promise((resolve, reject) => {
528
- streamPromise.resolve = resolve;
529
- streamPromise.reject = reject;
530
- });
531
- const toolData = {
532
- name: wf.name,
533
- args: inputData
534
- };
535
- await emitter.emit("watch-v2", {
536
- type: "tool-call-streaming-start",
537
- ...toolData
538
- });
539
- const run = wf.createRun();
540
- const { stream, getWorkflowState } = run.stream({
541
- inputData: input,
542
- runtimeContext: runtimeContextToUse
543
- });
544
- let result;
545
- for await (const chunk of stream) {
546
- const c = chunk;
547
- switch (c.type) {
548
- case "text-delta":
549
- await emitter.emit("watch-v2", {
550
- type: "tool-call-delta",
551
- ...toolData,
552
- argsTextDelta: c.textDelta
553
- });
554
- break;
555
- case "step-result":
556
- if (c?.payload?.output) {
557
- result = c?.payload?.output;
558
- }
559
- await emitter.emit("watch-v2", c);
560
- break;
561
- case "finish":
562
- streamPromise.resolve(result);
563
- break;
564
- case "start":
565
- case "finish":
566
- case "step-start":
567
- case "step-finish":
568
- case "tool-call":
569
- case "tool-result":
570
- case "tool-call-streaming-start":
571
- case "tool-call-delta":
572
- case "source":
573
- case "file":
574
- default:
575
- await emitter.emit("watch-v2", c);
576
- break;
577
- }
578
- }
579
- let runSuccess = true;
580
- const runResult = await streamPromise.promise;
581
- const workflowState = await getWorkflowState();
582
- if (workflowState.status === "failed") {
583
- runSuccess = false;
584
- }
585
- const finalResult = JSON.stringify({
586
- runId: run.runId,
587
- runResult,
588
- runSuccess
589
- });
590
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
591
- const initData = await getInitData();
592
- await memory?.saveMessages({
593
- messages: [
594
- {
595
- id: crypto.randomUUID(),
596
- type: "text",
597
- role: "assistant",
598
- content: { parts: [{ type: "text", text: finalResult }], format: 2 },
599
- createdAt: /* @__PURE__ */ new Date(),
600
- threadId: initData.threadId || runId,
601
- resourceId: initData.threadResourceId || this.name
602
- }
603
- ],
604
- format: "v2"
605
- });
606
- return {
607
- result: finalResult || "",
608
- task: inputData.task,
609
- resourceId: inputData.resourceId,
610
- resourceType: inputData.resourceType,
611
- isComplete: false,
612
- iteration: inputData.iteration
613
- };
614
- }
615
- });
616
- const toolStep = chunk7MZNOW6W_cjs.createStep({
617
- id: "toolStep",
618
- inputSchema: zod.z.object({
619
- task: zod.z.string(),
620
- resourceId: zod.z.string(),
621
- resourceType: RESOURCE_TYPES,
622
- prompt: zod.z.string(),
623
- result: zod.z.string(),
624
- isComplete: zod.z.boolean().optional(),
625
- selectionReason: zod.z.string(),
626
- iteration: zod.z.number()
627
- }),
628
- outputSchema: zod.z.object({
629
- task: zod.z.string(),
630
- resourceId: zod.z.string(),
631
- resourceType: RESOURCE_TYPES,
632
- result: zod.z.string(),
633
- isComplete: zod.z.boolean().optional(),
634
- iteration: zod.z.number()
635
- }),
636
- execute: async ({ inputData, getInitData }) => {
637
- const toolsMap = await this.getTools({ runtimeContext: runtimeContextToUse });
638
- const tool = toolsMap[inputData.resourceId];
639
- if (!tool) {
640
- throw new Error(`Tool ${inputData.resourceId} not found`);
641
- }
642
- if (!tool.execute) {
643
- throw new Error(`Tool ${inputData.resourceId} does not have an execute function`);
644
- }
645
- let inputDataToUse;
646
- try {
647
- inputDataToUse = JSON.parse(inputData.prompt);
648
- } catch (e) {
649
- console.error(e);
650
- throw new Error(`Invalid task input: ${inputData.task}`);
651
- }
652
- const finalResult = await tool.execute({
653
- runtimeContext: runtimeContextToUse,
654
- mastra: this.#mastra,
655
- resourceId: inputData.resourceId,
656
- threadId: runId,
657
- runId,
658
- context: inputDataToUse
659
- });
660
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
661
- const initData = await getInitData();
662
- await memory?.saveMessages({
663
- messages: [
664
- {
665
- id: crypto.randomUUID(),
666
- type: "text",
667
- role: "assistant",
668
- content: { parts: [{ type: "text", text: JSON.stringify(finalResult) }], format: 2 },
669
- createdAt: /* @__PURE__ */ new Date(),
670
- threadId: initData.threadId || runId,
671
- resourceId: initData.threadResourceId || this.name
672
- }
673
- ],
674
- format: "v2"
675
- });
676
- return {
677
- task: inputData.task,
678
- resourceId: inputData.resourceId,
679
- resourceType: inputData.resourceType,
680
- result: finalResult,
681
- isComplete: false,
682
- iteration: inputData.iteration
683
- };
684
- }
685
- });
686
- const finishStep = chunk7MZNOW6W_cjs.createStep({
687
- id: "finish-step",
688
- inputSchema: zod.z.object({
689
- task: zod.z.string(),
690
- resourceId: zod.z.string(),
691
- resourceType: RESOURCE_TYPES,
692
- prompt: zod.z.string(),
693
- result: zod.z.string(),
694
- isComplete: zod.z.boolean().optional(),
695
- selectionReason: zod.z.string(),
696
- iteration: zod.z.number()
697
- }),
698
- outputSchema: zod.z.object({
699
- task: zod.z.string(),
700
- result: zod.z.string(),
701
- isComplete: zod.z.boolean(),
702
- iteration: zod.z.number()
703
- }),
704
- execute: async ({ inputData }) => {
705
- return {
706
- task: inputData.task,
707
- result: inputData.result,
708
- isComplete: !!inputData.isComplete,
709
- iteration: inputData.iteration
710
- };
711
- }
712
- });
713
- const networkWorkflow = chunk7MZNOW6W_cjs.createWorkflow({
714
- id: "Agent-Network-Outer-Workflow",
715
- inputSchema: zod.z.object({
716
- task: zod.z.string(),
717
- resourceId: zod.z.string(),
718
- resourceType: RESOURCE_TYPES,
719
- result: zod.z.string().optional(),
720
- iteration: zod.z.number(),
721
- threadId: zod.z.string().optional(),
722
- threadResourceId: zod.z.string().optional(),
723
- isOneOff: zod.z.boolean(),
724
- verboseIntrospection: zod.z.boolean()
725
- }),
726
- outputSchema: zod.z.object({
727
- task: zod.z.string(),
728
- resourceId: zod.z.string(),
729
- resourceType: RESOURCE_TYPES,
730
- prompt: zod.z.string(),
731
- result: zod.z.string(),
732
- isComplete: zod.z.boolean().optional(),
733
- completionReason: zod.z.string().optional(),
734
- iteration: zod.z.number(),
735
- threadId: zod.z.string().optional(),
736
- threadResourceId: zod.z.string().optional(),
737
- isOneOff: zod.z.boolean()
738
- })
739
- });
740
- networkWorkflow.then(routingStep).branch([
741
- [async ({ inputData }) => !inputData.isComplete && inputData.resourceType === "agent", agentStep],
742
- [async ({ inputData }) => !inputData.isComplete && inputData.resourceType === "workflow", workflowStep],
743
- [async ({ inputData }) => !inputData.isComplete && inputData.resourceType === "tool", toolStep],
744
- [async ({ inputData }) => inputData.isComplete, finishStep]
745
- ]).map({
746
- task: {
747
- step: [routingStep, agentStep, workflowStep, toolStep],
748
- path: "task"
749
- },
750
- isComplete: {
751
- step: [agentStep, workflowStep, toolStep, finishStep],
752
- path: "isComplete"
753
- },
754
- completionReason: {
755
- step: [routingStep, agentStep, workflowStep, toolStep, finishStep],
756
- path: "completionReason"
757
- },
758
- result: {
759
- step: [agentStep, workflowStep, toolStep, finishStep],
760
- path: "result"
761
- },
762
- resourceId: {
763
- step: [routingStep, agentStep, workflowStep, toolStep],
764
- path: "resourceId"
765
- },
766
- resourceType: {
767
- step: [routingStep, agentStep, workflowStep, toolStep],
768
- path: "resourceType"
769
- },
770
- iteration: {
771
- step: [routingStep, agentStep, workflowStep, toolStep],
772
- path: "iteration"
773
- },
774
- isOneOff: {
775
- initData: networkWorkflow,
776
- path: "isOneOff"
777
- },
778
- threadId: {
779
- initData: networkWorkflow,
780
- path: "threadId"
781
- },
782
- threadResourceId: {
783
- initData: networkWorkflow,
784
- path: "threadResourceId"
785
- }
786
- }).commit();
787
- return networkWorkflow;
788
- }
789
- async generate(message, {
790
- runtimeContext,
791
- threadId,
792
- resourceId
793
- }) {
794
- const networkWorkflow = this.createWorkflow({ runtimeContext });
795
- const run = networkWorkflow.createRun();
796
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
797
- await memory?.saveMessages({
798
- messages: [
799
- {
800
- id: crypto.randomUUID(),
801
- type: "text",
802
- role: "user",
803
- content: { parts: [{ type: "text", text: message }], format: 2 },
804
- createdAt: /* @__PURE__ */ new Date(),
805
- threadId: threadId || run.runId,
806
- resourceId: resourceId || this.name
807
- }
808
- ],
809
- format: "v2"
810
- });
811
- const result = await run.start({
812
- inputData: {
813
- task: message,
814
- resourceId: "",
815
- resourceType: "none",
816
- iteration: 0,
817
- threadId,
818
- threadResourceId: resourceId,
819
- isOneOff: true,
820
- verboseIntrospection: true
821
- }
822
- });
823
- if (result.status === "failed") {
824
- throw result.error;
825
- }
826
- if (result.status === "suspended") {
827
- throw new Error("Workflow suspended");
828
- }
829
- return {
830
- task: result.result.task,
831
- result: result.result.result,
832
- resourceId: result.result.resourceId,
833
- resourceType: result.result.resourceType
834
- };
835
- }
836
- async stream(message, {
837
- runtimeContext,
838
- threadId,
839
- resourceId
840
- }) {
841
- const networkWorkflow = this.createWorkflow({ runtimeContext });
842
- const run = networkWorkflow.createRun();
843
- const memory = await this.getMemory({ runtimeContext: runtimeContext || new chunkLABUWBKX_cjs.RuntimeContext() });
844
- await memory?.saveMessages({
845
- messages: [
846
- {
847
- id: crypto.randomUUID(),
848
- type: "text",
849
- role: "user",
850
- content: { parts: [{ type: "text", text: message }], format: 2 },
851
- createdAt: /* @__PURE__ */ new Date(),
852
- threadId: threadId || run.runId,
853
- resourceId: resourceId || this.name
854
- }
855
- ],
856
- format: "v2"
857
- });
858
- return run.stream({
859
- inputData: {
860
- task: `You are executing just one primitive based on the following: ${message}`,
861
- resourceId: "",
862
- resourceType: "none",
863
- iteration: 0,
864
- threadResourceId: resourceId,
865
- threadId,
866
- isOneOff: true,
867
- verboseIntrospection: true
868
- }
869
- });
870
- }
871
- };
872
-
873
- exports.NewAgentNetwork = NewAgentNetwork;