@mastra/core 0.6.2 → 0.6.3

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 (115) hide show
  1. package/dist/agent/index.cjs +2 -2
  2. package/dist/agent/index.d.cts +3 -3
  3. package/dist/agent/index.d.ts +3 -3
  4. package/dist/agent/index.js +1 -1
  5. package/dist/{base-BwuLIP_x.d.ts → base-CbMRVSw8.d.ts} +142 -7
  6. package/dist/{base-BZViaFTt.d.ts → base-Cmunaaxb.d.ts} +1 -1
  7. package/dist/{base-BESiSy3v.d.cts → base-DsOQiX2q.d.cts} +142 -7
  8. package/dist/{base-DboIg_Cd.d.cts → base-ObPJ-w8K.d.cts} +1 -1
  9. package/dist/base.cjs +2 -2
  10. package/dist/base.d.cts +2 -2
  11. package/dist/base.d.ts +2 -2
  12. package/dist/base.js +1 -1
  13. package/dist/bundler/index.cjs +2 -2
  14. package/dist/bundler/index.d.cts +2 -2
  15. package/dist/bundler/index.d.ts +2 -2
  16. package/dist/bundler/index.js +1 -1
  17. package/dist/{chunk-6PNJOPFO.js → chunk-3CNO7YB5.js} +1 -1
  18. package/dist/{chunk-HJPMYDWO.cjs → chunk-4PYORXWM.cjs} +2 -2
  19. package/dist/{chunk-BPL5FORD.cjs → chunk-5ROCYEY7.cjs} +6 -6
  20. package/dist/{chunk-CMSZAZ3I.cjs → chunk-75XZXEUM.cjs} +8 -8
  21. package/dist/{chunk-GMAMAKLH.js → chunk-7VTZI3YN.js} +1 -1
  22. package/dist/{chunk-44N24GPK.js → chunk-DW4X6TXX.js} +4 -4
  23. package/dist/{chunk-DGCOY6YF.js → chunk-F6KZ3CUF.js} +1 -1
  24. package/dist/{chunk-KPKFLQFR.cjs → chunk-IQLRSKED.cjs} +2 -2
  25. package/dist/{chunk-SYQ7NK2E.cjs → chunk-JJ4YQTFT.cjs} +2 -2
  26. package/dist/{chunk-3JSTLSIP.js → chunk-L7CR75HA.js} +1 -0
  27. package/dist/{chunk-XLZRGA3F.cjs → chunk-LHUFN2BG.cjs} +29 -6
  28. package/dist/{chunk-WB2HREXE.cjs → chunk-NPOKIPWC.cjs} +1 -0
  29. package/dist/{chunk-GHONEERA.js → chunk-OC5LOLNS.js} +3 -3
  30. package/dist/{chunk-3LURIF6I.cjs → chunk-ONDCHP6G.cjs} +2 -2
  31. package/dist/{chunk-IIWRJFLQ.cjs → chunk-PL7PVTGF.cjs} +4 -4
  32. package/dist/{chunk-7TZAPEAT.js → chunk-RU7CSPAV.js} +1 -1
  33. package/dist/{chunk-ENT7U27Y.cjs → chunk-RUA6BSB6.cjs} +2 -2
  34. package/dist/{chunk-2BT7KDXB.js → chunk-S7PRIZBF.js} +1 -1
  35. package/dist/{chunk-4DD2PP2R.js → chunk-SMBKF6K5.js} +1 -1
  36. package/dist/{chunk-22HQQDJZ.js → chunk-U6V76AAB.js} +25 -2
  37. package/dist/{chunk-WOMOGDGR.cjs → chunk-U7ONOIBO.cjs} +2 -2
  38. package/dist/{chunk-4VHCCQ7P.js → chunk-VN4M67DA.js} +1 -1
  39. package/dist/{chunk-CLJYFFOS.js → chunk-WBE5RTFI.js} +1 -1
  40. package/dist/{chunk-FL3GQXQ2.cjs → chunk-WVVKLIUW.cjs} +2 -2
  41. package/dist/{chunk-ECNK7OU3.js → chunk-XLXJUYKH.js} +1 -1
  42. package/dist/{chunk-VA3TRXPD.cjs → chunk-YW6NIHPF.cjs} +2 -2
  43. package/dist/{chunk-QLYZGX5Z.js → chunk-ZABXT2MN.js} +1 -1
  44. package/dist/{chunk-2PTWBPQ6.cjs → chunk-ZG5FKEL2.cjs} +2 -2
  45. package/dist/deployer/index.cjs +2 -2
  46. package/dist/deployer/index.d.cts +2 -2
  47. package/dist/deployer/index.d.ts +2 -2
  48. package/dist/deployer/index.js +1 -1
  49. package/dist/eval/index.d.cts +3 -3
  50. package/dist/eval/index.d.ts +3 -3
  51. package/dist/{index-mKY1XrpK.d.cts → index-CquI0inB.d.cts} +1 -0
  52. package/dist/{index-mKY1XrpK.d.ts → index-CquI0inB.d.ts} +1 -0
  53. package/dist/index.cjs +46 -46
  54. package/dist/index.d.cts +6 -8
  55. package/dist/index.d.ts +6 -8
  56. package/dist/index.js +13 -13
  57. package/dist/integration/index.d.cts +3 -3
  58. package/dist/integration/index.d.ts +3 -3
  59. package/dist/llm/index.d.cts +3 -3
  60. package/dist/llm/index.d.ts +3 -3
  61. package/dist/logger/index.cjs +9 -9
  62. package/dist/logger/index.d.cts +1 -1
  63. package/dist/logger/index.d.ts +1 -1
  64. package/dist/logger/index.js +1 -1
  65. package/dist/mastra/index.cjs +2 -2
  66. package/dist/mastra/index.d.cts +3 -3
  67. package/dist/mastra/index.d.ts +3 -3
  68. package/dist/mastra/index.js +1 -1
  69. package/dist/memory/index.cjs +2 -2
  70. package/dist/memory/index.d.cts +3 -3
  71. package/dist/memory/index.d.ts +3 -3
  72. package/dist/memory/index.js +1 -1
  73. package/dist/network/index.cjs +300 -0
  74. package/dist/network/index.d.cts +19 -0
  75. package/dist/network/index.d.ts +19 -0
  76. package/dist/network/index.js +298 -0
  77. package/dist/relevance/index.cjs +4 -4
  78. package/dist/relevance/index.d.cts +3 -3
  79. package/dist/relevance/index.d.ts +3 -3
  80. package/dist/relevance/index.js +1 -1
  81. package/dist/storage/index.cjs +2 -2
  82. package/dist/storage/index.d.cts +3 -3
  83. package/dist/storage/index.d.ts +3 -3
  84. package/dist/storage/index.js +1 -1
  85. package/dist/storage/libsql/index.cjs +2 -2
  86. package/dist/storage/libsql/index.d.cts +3 -3
  87. package/dist/storage/libsql/index.d.ts +3 -3
  88. package/dist/storage/libsql/index.js +1 -1
  89. package/dist/telemetry/index.d.cts +3 -3
  90. package/dist/telemetry/index.d.ts +3 -3
  91. package/dist/tools/index.d.cts +4 -18
  92. package/dist/tools/index.d.ts +4 -18
  93. package/dist/tts/index.cjs +2 -2
  94. package/dist/tts/index.d.cts +2 -2
  95. package/dist/tts/index.d.ts +2 -2
  96. package/dist/tts/index.js +1 -1
  97. package/dist/utils.d.cts +5 -5
  98. package/dist/utils.d.ts +5 -5
  99. package/dist/vector/index.cjs +2 -2
  100. package/dist/vector/index.d.cts +2 -2
  101. package/dist/vector/index.d.ts +2 -2
  102. package/dist/vector/index.js +1 -1
  103. package/dist/vector/libsql/index.cjs +3 -3
  104. package/dist/vector/libsql/index.d.cts +2 -2
  105. package/dist/vector/libsql/index.d.ts +2 -2
  106. package/dist/vector/libsql/index.js +1 -1
  107. package/dist/voice/index.cjs +2 -2
  108. package/dist/voice/index.d.cts +3 -3
  109. package/dist/voice/index.d.ts +3 -3
  110. package/dist/voice/index.js +1 -1
  111. package/dist/workflows/index.cjs +17 -17
  112. package/dist/workflows/index.d.cts +4 -4
  113. package/dist/workflows/index.d.ts +4 -4
  114. package/dist/workflows/index.js +1 -1
  115. package/package.json +1 -1
@@ -0,0 +1,300 @@
1
+ 'use strict';
2
+
3
+ var chunk75XZXEUM_cjs = require('../chunk-75XZXEUM.cjs');
4
+ var chunk2W2GYEYQ_cjs = require('../chunk-2W2GYEYQ.cjs');
5
+ var chunkPL7PVTGF_cjs = require('../chunk-PL7PVTGF.cjs');
6
+ var chunkNPOKIPWC_cjs = require('../chunk-NPOKIPWC.cjs');
7
+ var zod = require('zod');
8
+
9
+ var AgentNetwork = class extends chunkPL7PVTGF_cjs.MastraBase {
10
+ #instructions;
11
+ #agents;
12
+ #model;
13
+ #routingAgent;
14
+ #agentHistory = {};
15
+ constructor(config) {
16
+ super({ component: chunkNPOKIPWC_cjs.RegisteredLogger.NETWORK, name: "AgentNetwork" });
17
+ this.#instructions = config.instructions;
18
+ this.#agents = config.agents;
19
+ this.#model = config.model;
20
+ this.#routingAgent = new chunk75XZXEUM_cjs.Agent({
21
+ name: config.name,
22
+ instructions: this.getInstructions(),
23
+ model: this.#model,
24
+ tools: this.getTools()
25
+ });
26
+ }
27
+ formatAgentId(name) {
28
+ return name.replace(/[^a-zA-Z0-9_-]/g, "_");
29
+ }
30
+ getTools() {
31
+ return {
32
+ transmit: chunk2W2GYEYQ_cjs.createTool({
33
+ id: "transmit",
34
+ description: "Call one or more specialized agents to handle specific tasks",
35
+ inputSchema: zod.z.object({
36
+ actions: zod.z.array(
37
+ zod.z.object({
38
+ agent: zod.z.string().describe("The name of the agent to call"),
39
+ input: zod.z.string().describe("The input to pass to the agent"),
40
+ includeHistory: zod.z.boolean().optional().describe("Whether to include previous agent outputs in the context")
41
+ })
42
+ )
43
+ }),
44
+ execute: async ({ context }) => {
45
+ try {
46
+ const actions = context.actions;
47
+ console.log(`Executing ${actions.length} specialized agents`);
48
+ const results = await Promise.all(
49
+ actions.map(
50
+ (action) => this.executeAgent(action.agent, [{ role: "user", content: action.input }], action.includeHistory)
51
+ )
52
+ );
53
+ console.log(results);
54
+ actions.forEach((action, index) => {
55
+ this.#addToAgentHistory(action.agent, {
56
+ input: action.input,
57
+ output: results[index] || ""
58
+ // Ensure output is always a string
59
+ });
60
+ });
61
+ return actions.map((action, index) => `[${action.agent}]: ${results[index]}`).join("\n\n");
62
+ } catch (err) {
63
+ const error = err;
64
+ console.error("Error in transmit tool:", error);
65
+ return `Error executing agents: ${error.message}`;
66
+ }
67
+ }
68
+ })
69
+ };
70
+ }
71
+ #addToAgentHistory(agentId, interaction) {
72
+ if (!this.#agentHistory[agentId]) {
73
+ this.#agentHistory[agentId] = [];
74
+ }
75
+ this.#agentHistory[agentId].push({
76
+ ...interaction,
77
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
78
+ });
79
+ }
80
+ getAgentHistory(agentId) {
81
+ return this.#agentHistory[agentId] || [];
82
+ }
83
+ #clearNetworkHistoryBeforeRun() {
84
+ this.#agentHistory = {};
85
+ }
86
+ /**
87
+ * Get the history of all agent interactions that have occurred in this network
88
+ * @returns A record of agent interactions, keyed by agent ID
89
+ */
90
+ getAgentInteractionHistory() {
91
+ return { ...this.#agentHistory };
92
+ }
93
+ /**
94
+ * Get a summary of agent interactions in a more readable format, displayed chronologically
95
+ * @returns A formatted string with all agent interactions in chronological order
96
+ */
97
+ getAgentInteractionSummary() {
98
+ const history = this.#agentHistory;
99
+ const agentIds = Object.keys(history);
100
+ if (agentIds.length === 0) {
101
+ return "No agent interactions have occurred yet.";
102
+ }
103
+ const allInteractions = [];
104
+ let globalSequence = 0;
105
+ agentIds.forEach((agentId) => {
106
+ const interactions = history[agentId] || [];
107
+ interactions.forEach((interaction, index) => {
108
+ allInteractions.push({
109
+ agentId,
110
+ interaction,
111
+ index,
112
+ // Assign a sequence number based on when it was added to the history
113
+ sequence: globalSequence++
114
+ });
115
+ });
116
+ });
117
+ allInteractions.sort((a, b) => {
118
+ if (a.interaction.timestamp && b.interaction.timestamp) {
119
+ return new Date(a.interaction.timestamp).getTime() - new Date(b.interaction.timestamp).getTime();
120
+ }
121
+ return a.sequence - b.sequence;
122
+ });
123
+ if (allInteractions.length === 0) {
124
+ return "No agent interactions have occurred yet.";
125
+ }
126
+ return "# Chronological Agent Interactions\n\n" + allInteractions.map(
127
+ (item, i) => `## Step ${i + 1}: Agent ${item.agentId} at ${item.interaction.timestamp}
128
+ **Input:** ${item.interaction.input.substring(0, 100)}${item.interaction.input.length > 100 ? "..." : ""}
129
+
130
+ **Output:** ${item.interaction.output.substring(0, 100)}${item.interaction.output.length > 100 ? "..." : ""}`
131
+ ).join("\n\n");
132
+ }
133
+ async executeAgent(agentId, input, includeHistory = false) {
134
+ try {
135
+ const agent = this.#agents.find((agent2) => this.formatAgentId(agent2.name) === agentId);
136
+ if (!agent) {
137
+ throw new Error(
138
+ `Agent "${agentId}" not found. Available agents: ${this.#agents.map((a) => this.formatAgentId(a.name)).join(", ")}`
139
+ );
140
+ }
141
+ let messagesWithContext = [...input];
142
+ if (includeHistory) {
143
+ const allHistory = Object.entries(this.#agentHistory);
144
+ if (allHistory.length > 0) {
145
+ const contextMessage = {
146
+ role: "system",
147
+ content: `Previous agent interactions:
148
+
149
+ ${allHistory.map(([agentName, interactions]) => {
150
+ return `## ${agentName}
151
+ ${interactions.map(
152
+ (interaction, i) => `Interaction ${i + 1} (${interaction.timestamp || "No timestamp"}):
153
+ - Input: ${interaction.input}
154
+ - Output: ${interaction.output}`
155
+ ).join("\n\n")}`;
156
+ }).join("\n\n")}`
157
+ };
158
+ messagesWithContext = [contextMessage, ...messagesWithContext];
159
+ }
160
+ }
161
+ const result = await agent.generate(messagesWithContext);
162
+ console.log(result);
163
+ return result.text;
164
+ } catch (err) {
165
+ const error = err;
166
+ console.error(`Error executing agent "${agentId}":`, error);
167
+ return `Unable to execute agent "${agentId}": ${error.message}`;
168
+ }
169
+ }
170
+ getInstructions() {
171
+ const agentList = this.#agents.map((agent) => {
172
+ const id = this.formatAgentId(agent.name);
173
+ return ` - **${id}**: ${agent.name}`;
174
+ }).join("\n");
175
+ return `
176
+ You are a router in a network of specialized AI agents.
177
+ Your job is to decide which agent should handle each step of a task.
178
+
179
+ ## System Instructions
180
+ ${this.#instructions}
181
+
182
+ ## Available Specialized Agents
183
+ You can call these agents using the "transmit" tool:
184
+ ${agentList}
185
+
186
+ ## How to Use the "transmit" Tool
187
+
188
+ The "transmit" tool allows you to call one or more specialized agents.
189
+
190
+ ### Single Agent Call
191
+ To call a single agent, use this format:
192
+ \`\`\`json
193
+ {
194
+ "actions": [
195
+ {
196
+ "agent": "agent_name",
197
+ "input": "detailed instructions for the agent"
198
+ }
199
+ ]
200
+ }
201
+ \`\`\`
202
+
203
+ ### Multiple Parallel Agent Calls
204
+ To call multiple agents in parallel, use this format:
205
+ \`\`\`json
206
+ {
207
+ "actions": [
208
+ {
209
+ "agent": "first_agent_name",
210
+ "input": "detailed instructions for the first agent"
211
+ },
212
+ {
213
+ "agent": "second_agent_name",
214
+ "input": "detailed instructions for the second agent"
215
+ }
216
+ ]
217
+ }
218
+ \`\`\`
219
+
220
+ ## Context Sharing
221
+
222
+ When calling an agent, you can choose to include the output from previous agents in the context.
223
+ This allows the agent to take into account the results from previous steps.
224
+
225
+ To include context, add the "includeHistory" field to the action and set it to true:
226
+ \`\`\`json
227
+ {
228
+ "actions": [
229
+ {
230
+ "agent": "agent_name",
231
+ "input": "detailed instructions for the agent",
232
+ "includeHistory": true
233
+ }
234
+ ]
235
+ }
236
+ \`\`\`
237
+
238
+ ## Best Practices
239
+ 1. Break down complex tasks into smaller steps
240
+ 2. Choose the most appropriate agent for each step
241
+ 3. Provide clear, detailed instructions to each agent
242
+ 4. Synthesize the results from multiple agents when needed
243
+ 5. Provide a final summary or answer to the user
244
+
245
+ ## Workflow
246
+ 1. Analyze the user's request
247
+ 2. Identify which specialized agent(s) can help
248
+ 3. Call the appropriate agent(s) using the transmit tool
249
+ 4. Review the agent's response
250
+ 5. Either call more agents or provide a final answer
251
+ `;
252
+ }
253
+ getRoutingAgent() {
254
+ return this.#routingAgent;
255
+ }
256
+ getAgents() {
257
+ return this.#agents;
258
+ }
259
+ async generate(messages, args) {
260
+ this.#clearNetworkHistoryBeforeRun();
261
+ console.log(`AgentNetwork: Starting generation with ${this.#agents.length} available agents`);
262
+ const ops = {
263
+ maxSteps: this.#agents?.length * 10,
264
+ // Default to 10 steps per agent
265
+ ...args
266
+ };
267
+ console.log(`AgentNetwork: Routing with max steps: ${ops.maxSteps}`);
268
+ const result = await this.#routingAgent.generate(
269
+ messages,
270
+ ops
271
+ );
272
+ console.log(`AgentNetwork: Generation complete with ${result.steps?.length || 0} steps`);
273
+ return result;
274
+ }
275
+ async stream(messages, args) {
276
+ this.#clearNetworkHistoryBeforeRun();
277
+ console.log(`AgentNetwork: Starting generation with ${this.#agents.length} available agents`);
278
+ const ops = {
279
+ maxSteps: this.#agents?.length * 10,
280
+ // Default to 10 steps per agent
281
+ ...args
282
+ };
283
+ console.log(`AgentNetwork: Routing with max steps: ${ops.maxSteps}`);
284
+ const result = await this.#routingAgent.stream(
285
+ messages,
286
+ ops
287
+ );
288
+ return result;
289
+ }
290
+ __registerMastra(p) {
291
+ this.#routingAgent.__registerMastra(p);
292
+ for (const agent of this.#agents) {
293
+ if (typeof agent.__registerMastra === "function") {
294
+ agent.__registerMastra(p);
295
+ }
296
+ }
297
+ }
298
+ };
299
+
300
+ exports.AgentNetwork = AgentNetwork;
@@ -0,0 +1,19 @@
1
+ export { f as AgentNetwork, aA as AgentNetworkConfig } from '../base-DsOQiX2q.cjs';
2
+ import 'ai';
3
+ import '../base-ObPJ-w8K.cjs';
4
+ import '@opentelemetry/api';
5
+ import '../index-CquI0inB.cjs';
6
+ import 'stream';
7
+ import 'pino';
8
+ import '@opentelemetry/sdk-trace-base';
9
+ import 'sift';
10
+ import 'zod';
11
+ import 'json-schema';
12
+ import '../types-CwTG2XyQ.cjs';
13
+ import '../deployer/index.cjs';
14
+ import '../bundler/index.cjs';
15
+ import '../vector/index.cjs';
16
+ import '../vector/filter/index.cjs';
17
+ import 'xstate';
18
+ import 'node:events';
19
+ import '../tts/index.cjs';
@@ -0,0 +1,19 @@
1
+ export { f as AgentNetwork, aA as AgentNetworkConfig } from '../base-CbMRVSw8.js';
2
+ import 'ai';
3
+ import '../base-Cmunaaxb.js';
4
+ import '@opentelemetry/api';
5
+ import '../index-CquI0inB.js';
6
+ import 'stream';
7
+ import 'pino';
8
+ import '@opentelemetry/sdk-trace-base';
9
+ import 'sift';
10
+ import 'zod';
11
+ import 'json-schema';
12
+ import '../types-CwTG2XyQ.js';
13
+ import '../deployer/index.js';
14
+ import '../bundler/index.js';
15
+ import '../vector/index.js';
16
+ import '../vector/filter/index.js';
17
+ import 'xstate';
18
+ import 'node:events';
19
+ import '../tts/index.js';
@@ -0,0 +1,298 @@
1
+ import { Agent } from '../chunk-DW4X6TXX.js';
2
+ import { createTool } from '../chunk-ZINPRHAN.js';
3
+ import { MastraBase } from '../chunk-VN4M67DA.js';
4
+ import { RegisteredLogger } from '../chunk-L7CR75HA.js';
5
+ import { z } from 'zod';
6
+
7
+ var AgentNetwork = class extends MastraBase {
8
+ #instructions;
9
+ #agents;
10
+ #model;
11
+ #routingAgent;
12
+ #agentHistory = {};
13
+ constructor(config) {
14
+ super({ component: RegisteredLogger.NETWORK, name: "AgentNetwork" });
15
+ this.#instructions = config.instructions;
16
+ this.#agents = config.agents;
17
+ this.#model = config.model;
18
+ this.#routingAgent = new Agent({
19
+ name: config.name,
20
+ instructions: this.getInstructions(),
21
+ model: this.#model,
22
+ tools: this.getTools()
23
+ });
24
+ }
25
+ formatAgentId(name) {
26
+ return name.replace(/[^a-zA-Z0-9_-]/g, "_");
27
+ }
28
+ getTools() {
29
+ return {
30
+ transmit: createTool({
31
+ id: "transmit",
32
+ description: "Call one or more specialized agents to handle specific tasks",
33
+ inputSchema: z.object({
34
+ actions: z.array(
35
+ z.object({
36
+ agent: z.string().describe("The name of the agent to call"),
37
+ input: z.string().describe("The input to pass to the agent"),
38
+ includeHistory: z.boolean().optional().describe("Whether to include previous agent outputs in the context")
39
+ })
40
+ )
41
+ }),
42
+ execute: async ({ context }) => {
43
+ try {
44
+ const actions = context.actions;
45
+ console.log(`Executing ${actions.length} specialized agents`);
46
+ const results = await Promise.all(
47
+ actions.map(
48
+ (action) => this.executeAgent(action.agent, [{ role: "user", content: action.input }], action.includeHistory)
49
+ )
50
+ );
51
+ console.log(results);
52
+ actions.forEach((action, index) => {
53
+ this.#addToAgentHistory(action.agent, {
54
+ input: action.input,
55
+ output: results[index] || ""
56
+ // Ensure output is always a string
57
+ });
58
+ });
59
+ return actions.map((action, index) => `[${action.agent}]: ${results[index]}`).join("\n\n");
60
+ } catch (err) {
61
+ const error = err;
62
+ console.error("Error in transmit tool:", error);
63
+ return `Error executing agents: ${error.message}`;
64
+ }
65
+ }
66
+ })
67
+ };
68
+ }
69
+ #addToAgentHistory(agentId, interaction) {
70
+ if (!this.#agentHistory[agentId]) {
71
+ this.#agentHistory[agentId] = [];
72
+ }
73
+ this.#agentHistory[agentId].push({
74
+ ...interaction,
75
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
76
+ });
77
+ }
78
+ getAgentHistory(agentId) {
79
+ return this.#agentHistory[agentId] || [];
80
+ }
81
+ #clearNetworkHistoryBeforeRun() {
82
+ this.#agentHistory = {};
83
+ }
84
+ /**
85
+ * Get the history of all agent interactions that have occurred in this network
86
+ * @returns A record of agent interactions, keyed by agent ID
87
+ */
88
+ getAgentInteractionHistory() {
89
+ return { ...this.#agentHistory };
90
+ }
91
+ /**
92
+ * Get a summary of agent interactions in a more readable format, displayed chronologically
93
+ * @returns A formatted string with all agent interactions in chronological order
94
+ */
95
+ getAgentInteractionSummary() {
96
+ const history = this.#agentHistory;
97
+ const agentIds = Object.keys(history);
98
+ if (agentIds.length === 0) {
99
+ return "No agent interactions have occurred yet.";
100
+ }
101
+ const allInteractions = [];
102
+ let globalSequence = 0;
103
+ agentIds.forEach((agentId) => {
104
+ const interactions = history[agentId] || [];
105
+ interactions.forEach((interaction, index) => {
106
+ allInteractions.push({
107
+ agentId,
108
+ interaction,
109
+ index,
110
+ // Assign a sequence number based on when it was added to the history
111
+ sequence: globalSequence++
112
+ });
113
+ });
114
+ });
115
+ allInteractions.sort((a, b) => {
116
+ if (a.interaction.timestamp && b.interaction.timestamp) {
117
+ return new Date(a.interaction.timestamp).getTime() - new Date(b.interaction.timestamp).getTime();
118
+ }
119
+ return a.sequence - b.sequence;
120
+ });
121
+ if (allInteractions.length === 0) {
122
+ return "No agent interactions have occurred yet.";
123
+ }
124
+ return "# Chronological Agent Interactions\n\n" + allInteractions.map(
125
+ (item, i) => `## Step ${i + 1}: Agent ${item.agentId} at ${item.interaction.timestamp}
126
+ **Input:** ${item.interaction.input.substring(0, 100)}${item.interaction.input.length > 100 ? "..." : ""}
127
+
128
+ **Output:** ${item.interaction.output.substring(0, 100)}${item.interaction.output.length > 100 ? "..." : ""}`
129
+ ).join("\n\n");
130
+ }
131
+ async executeAgent(agentId, input, includeHistory = false) {
132
+ try {
133
+ const agent = this.#agents.find((agent2) => this.formatAgentId(agent2.name) === agentId);
134
+ if (!agent) {
135
+ throw new Error(
136
+ `Agent "${agentId}" not found. Available agents: ${this.#agents.map((a) => this.formatAgentId(a.name)).join(", ")}`
137
+ );
138
+ }
139
+ let messagesWithContext = [...input];
140
+ if (includeHistory) {
141
+ const allHistory = Object.entries(this.#agentHistory);
142
+ if (allHistory.length > 0) {
143
+ const contextMessage = {
144
+ role: "system",
145
+ content: `Previous agent interactions:
146
+
147
+ ${allHistory.map(([agentName, interactions]) => {
148
+ return `## ${agentName}
149
+ ${interactions.map(
150
+ (interaction, i) => `Interaction ${i + 1} (${interaction.timestamp || "No timestamp"}):
151
+ - Input: ${interaction.input}
152
+ - Output: ${interaction.output}`
153
+ ).join("\n\n")}`;
154
+ }).join("\n\n")}`
155
+ };
156
+ messagesWithContext = [contextMessage, ...messagesWithContext];
157
+ }
158
+ }
159
+ const result = await agent.generate(messagesWithContext);
160
+ console.log(result);
161
+ return result.text;
162
+ } catch (err) {
163
+ const error = err;
164
+ console.error(`Error executing agent "${agentId}":`, error);
165
+ return `Unable to execute agent "${agentId}": ${error.message}`;
166
+ }
167
+ }
168
+ getInstructions() {
169
+ const agentList = this.#agents.map((agent) => {
170
+ const id = this.formatAgentId(agent.name);
171
+ return ` - **${id}**: ${agent.name}`;
172
+ }).join("\n");
173
+ return `
174
+ You are a router in a network of specialized AI agents.
175
+ Your job is to decide which agent should handle each step of a task.
176
+
177
+ ## System Instructions
178
+ ${this.#instructions}
179
+
180
+ ## Available Specialized Agents
181
+ You can call these agents using the "transmit" tool:
182
+ ${agentList}
183
+
184
+ ## How to Use the "transmit" Tool
185
+
186
+ The "transmit" tool allows you to call one or more specialized agents.
187
+
188
+ ### Single Agent Call
189
+ To call a single agent, use this format:
190
+ \`\`\`json
191
+ {
192
+ "actions": [
193
+ {
194
+ "agent": "agent_name",
195
+ "input": "detailed instructions for the agent"
196
+ }
197
+ ]
198
+ }
199
+ \`\`\`
200
+
201
+ ### Multiple Parallel Agent Calls
202
+ To call multiple agents in parallel, use this format:
203
+ \`\`\`json
204
+ {
205
+ "actions": [
206
+ {
207
+ "agent": "first_agent_name",
208
+ "input": "detailed instructions for the first agent"
209
+ },
210
+ {
211
+ "agent": "second_agent_name",
212
+ "input": "detailed instructions for the second agent"
213
+ }
214
+ ]
215
+ }
216
+ \`\`\`
217
+
218
+ ## Context Sharing
219
+
220
+ When calling an agent, you can choose to include the output from previous agents in the context.
221
+ This allows the agent to take into account the results from previous steps.
222
+
223
+ To include context, add the "includeHistory" field to the action and set it to true:
224
+ \`\`\`json
225
+ {
226
+ "actions": [
227
+ {
228
+ "agent": "agent_name",
229
+ "input": "detailed instructions for the agent",
230
+ "includeHistory": true
231
+ }
232
+ ]
233
+ }
234
+ \`\`\`
235
+
236
+ ## Best Practices
237
+ 1. Break down complex tasks into smaller steps
238
+ 2. Choose the most appropriate agent for each step
239
+ 3. Provide clear, detailed instructions to each agent
240
+ 4. Synthesize the results from multiple agents when needed
241
+ 5. Provide a final summary or answer to the user
242
+
243
+ ## Workflow
244
+ 1. Analyze the user's request
245
+ 2. Identify which specialized agent(s) can help
246
+ 3. Call the appropriate agent(s) using the transmit tool
247
+ 4. Review the agent's response
248
+ 5. Either call more agents or provide a final answer
249
+ `;
250
+ }
251
+ getRoutingAgent() {
252
+ return this.#routingAgent;
253
+ }
254
+ getAgents() {
255
+ return this.#agents;
256
+ }
257
+ async generate(messages, args) {
258
+ this.#clearNetworkHistoryBeforeRun();
259
+ console.log(`AgentNetwork: Starting generation with ${this.#agents.length} available agents`);
260
+ const ops = {
261
+ maxSteps: this.#agents?.length * 10,
262
+ // Default to 10 steps per agent
263
+ ...args
264
+ };
265
+ console.log(`AgentNetwork: Routing with max steps: ${ops.maxSteps}`);
266
+ const result = await this.#routingAgent.generate(
267
+ messages,
268
+ ops
269
+ );
270
+ console.log(`AgentNetwork: Generation complete with ${result.steps?.length || 0} steps`);
271
+ return result;
272
+ }
273
+ async stream(messages, args) {
274
+ this.#clearNetworkHistoryBeforeRun();
275
+ console.log(`AgentNetwork: Starting generation with ${this.#agents.length} available agents`);
276
+ const ops = {
277
+ maxSteps: this.#agents?.length * 10,
278
+ // Default to 10 steps per agent
279
+ ...args
280
+ };
281
+ console.log(`AgentNetwork: Routing with max steps: ${ops.maxSteps}`);
282
+ const result = await this.#routingAgent.stream(
283
+ messages,
284
+ ops
285
+ );
286
+ return result;
287
+ }
288
+ __registerMastra(p) {
289
+ this.#routingAgent.__registerMastra(p);
290
+ for (const agent of this.#agents) {
291
+ if (typeof agent.__registerMastra === "function") {
292
+ agent.__registerMastra(p);
293
+ }
294
+ }
295
+ }
296
+ };
297
+
298
+ export { AgentNetwork };
@@ -1,18 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var chunk2PTWBPQ6_cjs = require('../chunk-2PTWBPQ6.cjs');
3
+ var chunkZG5FKEL2_cjs = require('../chunk-ZG5FKEL2.cjs');
4
4
 
5
5
 
6
6
 
7
7
  Object.defineProperty(exports, "CohereRelevanceScorer", {
8
8
  enumerable: true,
9
- get: function () { return chunk2PTWBPQ6_cjs.CohereRelevanceScorer; }
9
+ get: function () { return chunkZG5FKEL2_cjs.CohereRelevanceScorer; }
10
10
  });
11
11
  Object.defineProperty(exports, "MastraAgentRelevanceScorer", {
12
12
  enumerable: true,
13
- get: function () { return chunk2PTWBPQ6_cjs.MastraAgentRelevanceScorer; }
13
+ get: function () { return chunkZG5FKEL2_cjs.MastraAgentRelevanceScorer; }
14
14
  });
15
15
  Object.defineProperty(exports, "createSimilarityPrompt", {
16
16
  enumerable: true,
17
- get: function () { return chunk2PTWBPQ6_cjs.createSimilarityPrompt; }
17
+ get: function () { return chunkZG5FKEL2_cjs.createSimilarityPrompt; }
18
18
  });
@@ -1,7 +1,7 @@
1
- import '../base-DboIg_Cd.cjs';
2
- import { aw as MastraLanguageModel } from '../base-BESiSy3v.cjs';
1
+ import '../base-ObPJ-w8K.cjs';
2
+ import { az as MastraLanguageModel } from '../base-DsOQiX2q.cjs';
3
3
  import '@opentelemetry/api';
4
- import '../index-mKY1XrpK.cjs';
4
+ import '../index-CquI0inB.cjs';
5
5
  import 'stream';
6
6
  import 'pino';
7
7
  import '@opentelemetry/sdk-trace-base';
@@ -1,7 +1,7 @@
1
- import '../base-BZViaFTt.js';
2
- import { aw as MastraLanguageModel } from '../base-BwuLIP_x.js';
1
+ import '../base-Cmunaaxb.js';
2
+ import { az as MastraLanguageModel } from '../base-CbMRVSw8.js';
3
3
  import '@opentelemetry/api';
4
- import '../index-mKY1XrpK.js';
4
+ import '../index-CquI0inB.js';
5
5
  import 'stream';
6
6
  import 'pino';
7
7
  import '@opentelemetry/sdk-trace-base';
@@ -1 +1 @@
1
- export { CohereRelevanceScorer, MastraAgentRelevanceScorer, createSimilarityPrompt } from '../chunk-DGCOY6YF.js';
1
+ export { CohereRelevanceScorer, MastraAgentRelevanceScorer, createSimilarityPrompt } from '../chunk-F6KZ3CUF.js';