@mastra/core 0.10.13 → 0.10.14
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.
- package/a2a.d.ts +1 -0
- package/agent.d.ts +1 -0
- package/base.d.ts +1 -0
- package/bundler.d.ts +1 -0
- package/deployer.d.ts +1 -0
- package/di.d.ts +1 -0
- package/dist/a2a/index.cjs +79 -0
- package/dist/a2a/index.d.cts +780 -0
- package/dist/a2a/index.d.ts +780 -0
- package/dist/a2a/index.js +68 -0
- package/dist/agent/index.cjs +15 -0
- package/dist/agent/index.d.cts +29 -0
- package/dist/agent/index.d.ts +29 -0
- package/dist/agent/index.js +2 -0
- package/dist/base-B_y9sMg0.d.cts +162 -0
- package/dist/base-CS5eSXbL.d.cts +4117 -0
- package/dist/base-ClrXcCRx.d.ts +162 -0
- package/dist/base-DBhKcre4.d.ts +4117 -0
- package/dist/base.cjs +10 -0
- package/dist/base.d.cts +6 -0
- package/dist/base.d.ts +6 -0
- package/dist/base.js +1 -0
- package/dist/bundler/index.cjs +10 -0
- package/dist/bundler/index.d.cts +30 -0
- package/dist/bundler/index.d.ts +30 -0
- package/dist/bundler/index.js +1 -0
- package/dist/chunk-2GRYVZ2O.cjs +244 -0
- package/dist/chunk-2HDFKWFU.js +2249 -0
- package/dist/chunk-32C7JDIZ.js +1 -0
- package/dist/chunk-4UBOJFSL.js +989 -0
- package/dist/chunk-4UWPFBC6.js +88 -0
- package/dist/chunk-4Z3OU5RY.cjs +31 -0
- package/dist/chunk-5HTMDAXP.js +359 -0
- package/dist/chunk-5IEKR756.js +53 -0
- package/dist/chunk-5YDTZN2X.js +114 -0
- package/dist/chunk-6UNGH46J.js +75 -0
- package/dist/chunk-6Y4UL5Z6.cjs +94 -0
- package/dist/chunk-7F6BQXE2.cjs +425 -0
- package/dist/chunk-7H2GET5Z.cjs +668 -0
- package/dist/chunk-7HZ6NIAF.cjs +2 -0
- package/dist/chunk-7MZNOW6W.cjs +2263 -0
- package/dist/chunk-7XQIPES3.js +668 -0
- package/dist/chunk-AKYTYALY.js +70 -0
- package/dist/chunk-ATXBSEFT.js +22 -0
- package/dist/chunk-B6TOBUS6.cjs +80 -0
- package/dist/chunk-B7SQOKEC.cjs +91 -0
- package/dist/chunk-BB4KXGBU.js +83 -0
- package/dist/chunk-BB6DPGIV.cjs +6 -0
- package/dist/chunk-C4LMN2IR.js +27 -0
- package/dist/chunk-DFFVEKIG.js +407 -0
- package/dist/chunk-E7AX3U6M.cjs +659 -0
- package/dist/chunk-EWCOOO3H.js +5007 -0
- package/dist/chunk-F2WMR75C.cjs +183 -0
- package/dist/chunk-FBKJ3652.cjs +5034 -0
- package/dist/chunk-FEYYOBBG.cjs +24 -0
- package/dist/chunk-FL5SZ2XU.js +181 -0
- package/dist/chunk-GH2KM66J.js +37 -0
- package/dist/chunk-GWFS5DAR.cjs +37 -0
- package/dist/chunk-HNEE7IF4.js +60 -0
- package/dist/chunk-HSVOEWAM.cjs +2 -0
- package/dist/chunk-J52TXHZV.cjs +73 -0
- package/dist/chunk-JNMQKJH4.js +10 -0
- package/dist/chunk-JQOMTERC.js +89 -0
- package/dist/chunk-LABUWBKX.cjs +71 -0
- package/dist/chunk-LXFZUKP3.cjs +34 -0
- package/dist/chunk-MP2QBLUJ.cjs +70 -0
- package/dist/chunk-MUNFCOMB.cjs +62 -0
- package/dist/chunk-NH5WJNNS.js +1 -0
- package/dist/chunk-P3Q73CAW.cjs +55 -0
- package/dist/chunk-PA2YIVIT.js +61 -0
- package/dist/chunk-QFTBW7ZZ.cjs +2 -0
- package/dist/chunk-QQ5K5TZE.cjs +619 -0
- package/dist/chunk-QUSEDVYI.cjs +991 -0
- package/dist/chunk-R4V75T7J.js +1 -0
- package/dist/chunk-SGGPJWRQ.js +69 -0
- package/dist/chunk-ST5RMVLG.cjs +87 -0
- package/dist/chunk-TC2SCOTE.js +605 -0
- package/dist/chunk-U64IJDC5.cjs +109 -0
- package/dist/chunk-UX3B6S2I.cjs +65 -0
- package/dist/chunk-V5D2LIF5.js +68 -0
- package/dist/chunk-VG4OPO2R.js +240 -0
- package/dist/chunk-WQNOATKB.js +103 -0
- package/dist/chunk-Y7D2JLKS.js +4 -0
- package/dist/chunk-YJEHXYK5.js +657 -0
- package/dist/chunk-YOQP5T77.js +32 -0
- package/dist/chunk-ZIZ3CVHN.cjs +120 -0
- package/dist/chunk-ZPOUMTTH.cjs +362 -0
- package/dist/chunk-ZZLBNB3U.cjs +12 -0
- package/dist/deployer/index.cjs +10 -0
- package/dist/deployer/index.d.cts +19 -0
- package/dist/deployer/index.d.ts +19 -0
- package/dist/deployer/index.js +1 -0
- package/dist/di/index.cjs +10 -0
- package/dist/di/index.d.cts +1 -0
- package/dist/di/index.d.ts +1 -0
- package/dist/di/index.js +1 -0
- package/dist/error/index.cjs +22 -0
- package/dist/error/index.d.cts +86 -0
- package/dist/error/index.d.ts +86 -0
- package/dist/error/index.js +1 -0
- package/dist/eval/index.cjs +14 -0
- package/dist/eval/index.d.cts +43 -0
- package/dist/eval/index.d.ts +43 -0
- package/dist/eval/index.js +1 -0
- package/dist/hooks/index.cjs +18 -0
- package/dist/hooks/index.d.cts +33 -0
- package/dist/hooks/index.d.ts +33 -0
- package/dist/hooks/index.js +1 -0
- package/dist/index.cjs +281 -0
- package/dist/index.d.cts +92 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.js +112 -0
- package/dist/integration/index.cjs +14 -0
- package/dist/integration/index.d.cts +65 -0
- package/dist/integration/index.d.ts +65 -0
- package/dist/integration/index.js +1 -0
- package/dist/llm/index.cjs +10 -0
- package/dist/llm/index.d.cts +29 -0
- package/dist/llm/index.d.ts +29 -0
- package/dist/llm/index.js +1 -0
- package/dist/logger/index.cjs +43 -0
- package/dist/logger/index.d.cts +96 -0
- package/dist/logger/index.d.ts +96 -0
- package/dist/logger/index.js +2 -0
- package/dist/logger-B8XXh6ya.d.cts +159 -0
- package/dist/logger-Bpa2oLL4.d.ts +159 -0
- package/dist/mastra/index.cjs +10 -0
- package/dist/mastra/index.d.cts +29 -0
- package/dist/mastra/index.d.ts +29 -0
- package/dist/mastra/index.js +1 -0
- package/dist/mcp/index.cjs +106 -0
- package/dist/mcp/index.d.cts +29 -0
- package/dist/mcp/index.d.ts +29 -0
- package/dist/mcp/index.js +100 -0
- package/dist/memory/index.cjs +18 -0
- package/dist/memory/index.d.cts +29 -0
- package/dist/memory/index.d.ts +29 -0
- package/dist/memory/index.js +1 -0
- package/dist/network/index.cjs +311 -0
- package/dist/network/index.d.cts +29 -0
- package/dist/network/index.d.ts +29 -0
- package/dist/network/index.js +309 -0
- package/dist/network/vNext/index.cjs +873 -0
- package/dist/network/vNext/index.d.cts +29 -0
- package/dist/network/vNext/index.d.ts +29 -0
- package/dist/network/vNext/index.js +871 -0
- package/dist/relevance/index.cjs +18 -0
- package/dist/relevance/index.d.cts +49 -0
- package/dist/relevance/index.d.ts +49 -0
- package/dist/relevance/index.js +1 -0
- package/dist/runtime-context/index.cjs +10 -0
- package/dist/runtime-context/index.d.cts +52 -0
- package/dist/runtime-context/index.d.ts +52 -0
- package/dist/runtime-context/index.js +1 -0
- package/dist/server/index.cjs +62 -0
- package/dist/server/index.d.cts +52 -0
- package/dist/server/index.d.ts +52 -0
- package/dist/server/index.js +59 -0
- package/dist/storage/index.cjs +336 -0
- package/dist/storage/index.d.cts +149 -0
- package/dist/storage/index.d.ts +149 -0
- package/dist/storage/index.js +303 -0
- package/dist/telemetry/index.cjs +30 -0
- package/dist/telemetry/index.d.cts +75 -0
- package/dist/telemetry/index.d.ts +75 -0
- package/dist/telemetry/index.js +1 -0
- package/dist/telemetry/otel-vendor.cjs +103 -0
- package/dist/telemetry/otel-vendor.d.cts +20 -0
- package/dist/telemetry/otel-vendor.d.ts +20 -0
- package/dist/telemetry/otel-vendor.js +57 -0
- package/dist/tools/index.cjs +18 -0
- package/dist/tools/index.d.cts +41 -0
- package/dist/tools/index.d.ts +41 -0
- package/dist/tools/index.js +1 -0
- package/dist/tts/index.cjs +10 -0
- package/dist/tts/index.d.cts +28 -0
- package/dist/tts/index.d.ts +28 -0
- package/dist/tts/index.js +1 -0
- package/dist/types-Bo1uigWx.d.cts +17 -0
- package/dist/types-Bo1uigWx.d.ts +17 -0
- package/dist/utils.cjs +58 -0
- package/dist/utils.d.cts +149 -0
- package/dist/utils.d.ts +149 -0
- package/dist/utils.js +1 -0
- package/dist/vector/filter/index.cjs +192 -0
- package/dist/vector/filter/index.d.cts +128 -0
- package/dist/vector/filter/index.d.ts +128 -0
- package/dist/vector/filter/index.js +190 -0
- package/dist/vector/index.cjs +10 -0
- package/dist/vector/index.d.cts +77 -0
- package/dist/vector/index.d.ts +77 -0
- package/dist/vector/index.js +1 -0
- package/dist/voice/index.cjs +18 -0
- package/dist/voice/index.d.cts +29 -0
- package/dist/voice/index.d.ts +29 -0
- package/dist/voice/index.js +1 -0
- package/dist/workflows/constants.cjs +10 -0
- package/dist/workflows/constants.d.cts +3 -0
- package/dist/workflows/constants.d.ts +3 -0
- package/dist/workflows/constants.js +1 -0
- package/dist/workflows/index.cjs +42 -0
- package/dist/workflows/index.d.cts +282 -0
- package/dist/workflows/index.d.ts +282 -0
- package/dist/workflows/index.js +1 -0
- package/dist/workflows/legacy/index.cjs +90 -0
- package/dist/workflows/legacy/index.d.cts +91 -0
- package/dist/workflows/legacy/index.d.ts +91 -0
- package/dist/workflows/legacy/index.js +1 -0
- package/error.d.ts +1 -0
- package/eval.d.ts +1 -0
- package/hooks.d.ts +1 -0
- package/integration.d.ts +1 -0
- package/llm.d.ts +1 -0
- package/logger.d.ts +1 -0
- package/mastra.d.ts +1 -0
- package/mcp.d.ts +1 -0
- package/memory.d.ts +1 -0
- package/network/vNext.d.ts +1 -0
- package/network.d.ts +1 -0
- package/package.json +1 -1
- package/relevance.d.ts +1 -0
- package/runtime-context.d.ts +1 -0
- package/server.d.ts +1 -0
- package/storage.d.ts +1 -0
- package/telemetry/otel-vendor.d.ts +1 -0
- package/telemetry.d.ts +1 -0
- package/tools.d.ts +1 -0
- package/tts.d.ts +1 -0
- package/utils.d.ts +1 -0
- package/vector/filter.d.ts +1 -0
- package/vector.d.ts +1 -0
- package/voice.d.ts +1 -0
- package/workflows/_constants.d.ts +1 -0
- package/workflows/legacy.d.ts +1 -0
- package/workflows.d.ts +1 -0
|
@@ -0,0 +1,873 @@
|
|
|
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;
|