@mastra/server 0.0.1-alpha.6 → 0.10.0
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/{LICENSE → LICENSE.md} +3 -1
- package/README.md +48 -135
- package/dist/_tsup-dts-rollup.d.cts +305 -52
- package/dist/_tsup-dts-rollup.d.ts +305 -52
- package/dist/{chunk-4JINXASC.js → chunk-55DOQLP6.js} +5 -4
- package/dist/{chunk-QN4KF3BH.cjs → chunk-57CJTIPW.cjs} +2 -2
- package/dist/chunk-5SN4U5AC.cjs +508 -0
- package/dist/{chunk-ZLBRQFDD.cjs → chunk-64U3UDTH.cjs} +2 -2
- package/dist/chunk-6B4OMWT6.cjs +343 -0
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/{chunk-VB7KH62D.cjs → chunk-7DLZLWJV.cjs} +16 -12
- package/dist/{chunk-M56ECCHK.cjs → chunk-BNEY4P4P.cjs} +20 -19
- package/dist/{chunk-2YONKUWB.js → chunk-C7564HUT.js} +58 -33
- package/dist/chunk-D4IRYCUI.cjs +235 -0
- package/dist/{chunk-RBQASTUP.js → chunk-DJJIUEL2.js} +13 -5
- package/dist/{chunk-L7XE5QTW.js → chunk-H5PTF3Y4.js} +1 -1
- package/dist/{chunk-3RVHWGWO.js → chunk-HFWCEP5S.js} +6 -12
- package/dist/chunk-LIVAK2DM.js +2001 -0
- package/dist/{chunk-3AHQ5RGN.js → chunk-M5ABIP7D.js} +1 -1
- package/dist/{chunk-CWSDZEZG.cjs → chunk-MHKNLNAN.cjs} +15 -12
- package/dist/chunk-MLKGABMK.js +7 -0
- package/dist/{chunk-TRDNDNGQ.js → chunk-NYN7KFXL.js} +1 -7
- package/dist/{chunk-FV45V6WC.cjs → chunk-OCWPVYNI.cjs} +0 -7
- package/dist/{chunk-DVPP5S6I.js → chunk-OR3CIE2H.js} +9 -6
- package/dist/chunk-P6SCPDYW.js +500 -0
- package/dist/chunk-QY36IPTP.js +331 -0
- package/dist/{chunk-YNSGUC2O.js → chunk-TJKLBTFB.js} +23 -7
- package/dist/chunk-VPNDC2DI.cjs +148 -0
- package/dist/{chunk-ILW7XYNJ.js → chunk-W7VCKPAD.js} +84 -60
- package/dist/{chunk-2JQC6JWP.js → chunk-WFBJJNCK.js} +10 -6
- package/dist/chunk-WUC6LSTW.js +227 -0
- package/dist/{chunk-2FJURXCL.cjs → chunk-Y7UWRW5X.cjs} +43 -35
- package/dist/{chunk-A7DF4ETD.cjs → chunk-YBVOQN4M.cjs} +22 -28
- package/dist/chunk-YIOVBYZH.cjs +332 -0
- package/dist/chunk-YWLUOY3D.cjs +2004 -0
- package/dist/{chunk-4C3EPMMF.cjs → chunk-ZE5AAC4I.cjs} +40 -24
- package/dist/server/handlers/a2a.cjs +30 -0
- package/dist/server/handlers/a2a.d.cts +6 -0
- package/dist/server/handlers/a2a.d.ts +6 -0
- package/dist/server/handlers/a2a.js +1 -0
- package/dist/server/handlers/agents.cjs +7 -7
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/error.cjs +2 -2
- package/dist/server/handlers/error.js +1 -1
- package/dist/server/handlers/legacyWorkflows.cjs +46 -0
- package/dist/server/handlers/legacyWorkflows.d.cts +10 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +10 -0
- package/dist/server/handlers/legacyWorkflows.js +1 -0
- package/dist/server/handlers/logs.cjs +4 -4
- package/dist/server/handlers/logs.js +1 -1
- package/dist/server/handlers/memory.cjs +9 -9
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/network.cjs +5 -5
- package/dist/server/handlers/network.js +1 -1
- package/dist/server/handlers/telemetry.cjs +3 -3
- package/dist/server/handlers/telemetry.js +1 -1
- package/dist/server/handlers/tools.cjs +5 -5
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/utils.cjs +2 -2
- package/dist/server/handlers/utils.js +1 -1
- package/dist/server/handlers/vector.cjs +7 -7
- package/dist/server/handlers/vector.js +1 -1
- package/dist/server/handlers/voice.cjs +4 -4
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers/workflows.cjs +13 -13
- package/dist/server/handlers/workflows.d.cts +2 -2
- package/dist/server/handlers/workflows.d.ts +2 -2
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +33 -23
- package/dist/server/handlers.d.cts +2 -0
- package/dist/server/handlers.d.ts +2 -0
- package/dist/server/handlers.js +11 -9
- package/package.json +9 -7
- package/dist/chunk-5JOF627H.cjs +0 -123
- package/dist/chunk-BMA2ORRT.js +0 -152
- package/dist/chunk-GV52BII2.cjs +0 -160
- package/dist/chunk-QH6XWSXP.cjs +0 -5579
- package/dist/chunk-VK6FX47H.js +0 -5576
- package/dist/chunk-X37I6GZT.cjs +0 -308
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { stringify, esm_default } from './chunk-LIVAK2DM.js';
|
|
2
|
+
import { handleError } from './chunk-M5ABIP7D.js';
|
|
3
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
4
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
5
|
+
import { ReadableStream } from 'node:stream/web';
|
|
6
|
+
import { RuntimeContext } from '@mastra/core/di';
|
|
7
|
+
|
|
8
|
+
// src/server/handlers/workflows.ts
|
|
9
|
+
var workflows_exports = {};
|
|
10
|
+
__export(workflows_exports, {
|
|
11
|
+
createWorkflowRunHandler: () => createWorkflowRunHandler,
|
|
12
|
+
getWorkflowByIdHandler: () => getWorkflowByIdHandler,
|
|
13
|
+
getWorkflowRunByIdHandler: () => getWorkflowRunByIdHandler,
|
|
14
|
+
getWorkflowRunsHandler: () => getWorkflowRunsHandler,
|
|
15
|
+
getWorkflowsHandler: () => getWorkflowsHandler,
|
|
16
|
+
resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
|
|
17
|
+
resumeWorkflowHandler: () => resumeWorkflowHandler,
|
|
18
|
+
startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
|
|
19
|
+
startWorkflowRunHandler: () => startWorkflowRunHandler,
|
|
20
|
+
watchWorkflowHandler: () => watchWorkflowHandler
|
|
21
|
+
});
|
|
22
|
+
async function getWorkflowsHandler({ mastra }) {
|
|
23
|
+
try {
|
|
24
|
+
const workflows = mastra.getWorkflows({ serialized: false });
|
|
25
|
+
const _workflows = Object.entries(workflows).reduce((acc, [key, workflow]) => {
|
|
26
|
+
acc[key] = {
|
|
27
|
+
name: workflow.name,
|
|
28
|
+
description: workflow.description,
|
|
29
|
+
steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
|
|
30
|
+
acc2[key2] = {
|
|
31
|
+
id: step.id,
|
|
32
|
+
description: step.description,
|
|
33
|
+
inputSchema: step.inputSchema ? stringify(esm_default(step.inputSchema)) : void 0,
|
|
34
|
+
outputSchema: step.outputSchema ? stringify(esm_default(step.outputSchema)) : void 0,
|
|
35
|
+
resumeSchema: step.resumeSchema ? stringify(esm_default(step.resumeSchema)) : void 0,
|
|
36
|
+
suspendSchema: step.suspendSchema ? stringify(esm_default(step.suspendSchema)) : void 0
|
|
37
|
+
};
|
|
38
|
+
return acc2;
|
|
39
|
+
}, {}),
|
|
40
|
+
stepGraph: workflow.serializedStepGraph,
|
|
41
|
+
inputSchema: workflow.inputSchema ? stringify(esm_default(workflow.inputSchema)) : void 0,
|
|
42
|
+
outputSchema: workflow.outputSchema ? stringify(esm_default(workflow.outputSchema)) : void 0
|
|
43
|
+
};
|
|
44
|
+
return acc;
|
|
45
|
+
}, {});
|
|
46
|
+
return _workflows;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new HTTPException(500, { message: error?.message || "Error getting workflows" });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function getWorkflowByIdHandler({ mastra, workflowId }) {
|
|
52
|
+
try {
|
|
53
|
+
if (!workflowId) {
|
|
54
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
55
|
+
}
|
|
56
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
57
|
+
if (!workflow) {
|
|
58
|
+
throw new HTTPException(404, { message: "Workflow not found" });
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
|
|
62
|
+
acc[key] = {
|
|
63
|
+
id: step.id,
|
|
64
|
+
description: step.description,
|
|
65
|
+
inputSchema: step.inputSchema ? stringify(esm_default(step.inputSchema)) : void 0,
|
|
66
|
+
outputSchema: step.outputSchema ? stringify(esm_default(step.outputSchema)) : void 0,
|
|
67
|
+
resumeSchema: step.resumeSchema ? stringify(esm_default(step.resumeSchema)) : void 0,
|
|
68
|
+
suspendSchema: step.suspendSchema ? stringify(esm_default(step.suspendSchema)) : void 0
|
|
69
|
+
};
|
|
70
|
+
return acc;
|
|
71
|
+
}, {}),
|
|
72
|
+
name: workflow.name,
|
|
73
|
+
description: workflow.description,
|
|
74
|
+
stepGraph: workflow.serializedStepGraph,
|
|
75
|
+
inputSchema: workflow.inputSchema ? stringify(esm_default(workflow.inputSchema)) : void 0,
|
|
76
|
+
outputSchema: workflow.outputSchema ? stringify(esm_default(workflow.outputSchema)) : void 0
|
|
77
|
+
};
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw new HTTPException(500, { message: error?.message || "Error getting workflow" });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function getWorkflowRunByIdHandler({
|
|
83
|
+
mastra,
|
|
84
|
+
workflowId,
|
|
85
|
+
runId
|
|
86
|
+
}) {
|
|
87
|
+
try {
|
|
88
|
+
if (!workflowId) {
|
|
89
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
90
|
+
}
|
|
91
|
+
if (!runId) {
|
|
92
|
+
throw new HTTPException(400, { message: "Run ID is required" });
|
|
93
|
+
}
|
|
94
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
95
|
+
if (!workflow) {
|
|
96
|
+
throw new HTTPException(404, { message: "Workflow not found" });
|
|
97
|
+
}
|
|
98
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
99
|
+
if (!run) {
|
|
100
|
+
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
101
|
+
}
|
|
102
|
+
return run;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw new HTTPException(500, { message: error?.message || "Error getting workflow run" });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function createWorkflowRunHandler({
|
|
108
|
+
mastra,
|
|
109
|
+
workflowId,
|
|
110
|
+
runId: prevRunId
|
|
111
|
+
}) {
|
|
112
|
+
try {
|
|
113
|
+
if (!workflowId) {
|
|
114
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
115
|
+
}
|
|
116
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
117
|
+
if (!workflow) {
|
|
118
|
+
throw new HTTPException(404, { message: "Workflow not found" });
|
|
119
|
+
}
|
|
120
|
+
const run = workflow.createRun({ runId: prevRunId });
|
|
121
|
+
return { runId: run.runId };
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw new HTTPException(500, { message: error?.message || "Error creating workflow run" });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async function startAsyncWorkflowHandler({
|
|
127
|
+
mastra,
|
|
128
|
+
runtimeContext,
|
|
129
|
+
workflowId,
|
|
130
|
+
runId,
|
|
131
|
+
inputData,
|
|
132
|
+
runtimeContextFromRequest
|
|
133
|
+
}) {
|
|
134
|
+
try {
|
|
135
|
+
if (!workflowId) {
|
|
136
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
137
|
+
}
|
|
138
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
139
|
+
if (!workflow) {
|
|
140
|
+
throw new HTTPException(404, { message: "Workflow not found" });
|
|
141
|
+
}
|
|
142
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
143
|
+
...Array.from(runtimeContext?.entries() ?? []),
|
|
144
|
+
...Array.from(Object.entries(runtimeContextFromRequest ?? {}))
|
|
145
|
+
]);
|
|
146
|
+
const _run = workflow.createRun({ runId });
|
|
147
|
+
const result = await _run.start({
|
|
148
|
+
inputData,
|
|
149
|
+
runtimeContext: finalRuntimeContext
|
|
150
|
+
});
|
|
151
|
+
return result;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
throw new HTTPException(500, { message: error?.message || "Error executing workflow" });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function startWorkflowRunHandler({
|
|
157
|
+
mastra,
|
|
158
|
+
runtimeContext,
|
|
159
|
+
workflowId,
|
|
160
|
+
runId,
|
|
161
|
+
inputData,
|
|
162
|
+
runtimeContextFromRequest
|
|
163
|
+
}) {
|
|
164
|
+
try {
|
|
165
|
+
if (!workflowId) {
|
|
166
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
167
|
+
}
|
|
168
|
+
if (!runId) {
|
|
169
|
+
throw new HTTPException(400, { message: "runId required to start run" });
|
|
170
|
+
}
|
|
171
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
172
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
173
|
+
if (!run) {
|
|
174
|
+
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
175
|
+
}
|
|
176
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
177
|
+
...Array.from(runtimeContext?.entries() ?? []),
|
|
178
|
+
...Array.from(Object.entries(runtimeContextFromRequest ?? {}))
|
|
179
|
+
]);
|
|
180
|
+
const _run = workflow.createRun({ runId });
|
|
181
|
+
void _run.start({
|
|
182
|
+
inputData,
|
|
183
|
+
runtimeContext: finalRuntimeContext
|
|
184
|
+
});
|
|
185
|
+
return { message: "Workflow run started" };
|
|
186
|
+
} catch (e) {
|
|
187
|
+
return handleError(e, "Error starting workflow run");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async function watchWorkflowHandler({
|
|
191
|
+
mastra,
|
|
192
|
+
workflowId,
|
|
193
|
+
runId
|
|
194
|
+
}) {
|
|
195
|
+
try {
|
|
196
|
+
if (!workflowId) {
|
|
197
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
198
|
+
}
|
|
199
|
+
if (!runId) {
|
|
200
|
+
throw new HTTPException(400, { message: "runId required to watch workflow" });
|
|
201
|
+
}
|
|
202
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
203
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
204
|
+
if (!run) {
|
|
205
|
+
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
206
|
+
}
|
|
207
|
+
const _run = workflow.createRun({ runId });
|
|
208
|
+
let unwatch;
|
|
209
|
+
let asyncRef = null;
|
|
210
|
+
const stream = new ReadableStream({
|
|
211
|
+
start(controller) {
|
|
212
|
+
unwatch = _run.watch(({ type, payload, eventTimestamp }) => {
|
|
213
|
+
controller.enqueue(JSON.stringify({ type, payload, eventTimestamp, runId }));
|
|
214
|
+
if (asyncRef) {
|
|
215
|
+
clearImmediate(asyncRef);
|
|
216
|
+
asyncRef = null;
|
|
217
|
+
}
|
|
218
|
+
asyncRef = setImmediate(async () => {
|
|
219
|
+
const runDone = payload.workflowState.status !== "running";
|
|
220
|
+
if (runDone) {
|
|
221
|
+
controller.close();
|
|
222
|
+
unwatch?.();
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
},
|
|
227
|
+
cancel() {
|
|
228
|
+
unwatch?.();
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
return stream;
|
|
232
|
+
} catch (error) {
|
|
233
|
+
return handleError(error, "Error watching workflow");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
async function resumeAsyncWorkflowHandler({
|
|
237
|
+
mastra,
|
|
238
|
+
workflowId,
|
|
239
|
+
runId,
|
|
240
|
+
body,
|
|
241
|
+
runtimeContext,
|
|
242
|
+
runtimeContextFromRequest
|
|
243
|
+
}) {
|
|
244
|
+
try {
|
|
245
|
+
if (!workflowId) {
|
|
246
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
247
|
+
}
|
|
248
|
+
if (!runId) {
|
|
249
|
+
throw new HTTPException(400, { message: "runId required to resume workflow" });
|
|
250
|
+
}
|
|
251
|
+
if (!body.step) {
|
|
252
|
+
throw new HTTPException(400, { message: "step required to resume workflow" });
|
|
253
|
+
}
|
|
254
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
255
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
256
|
+
if (!run) {
|
|
257
|
+
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
258
|
+
}
|
|
259
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
260
|
+
...Array.from(runtimeContext?.entries() ?? []),
|
|
261
|
+
...Array.from(Object.entries(runtimeContextFromRequest ?? {}))
|
|
262
|
+
]);
|
|
263
|
+
const _run = workflow.createRun({ runId });
|
|
264
|
+
const result = await _run.resume({
|
|
265
|
+
step: body.step,
|
|
266
|
+
resumeData: body.resumeData,
|
|
267
|
+
runtimeContext: finalRuntimeContext
|
|
268
|
+
});
|
|
269
|
+
return result;
|
|
270
|
+
} catch (error) {
|
|
271
|
+
return handleError(error, "Error resuming workflow step");
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async function resumeWorkflowHandler({
|
|
275
|
+
mastra,
|
|
276
|
+
workflowId,
|
|
277
|
+
runId,
|
|
278
|
+
body,
|
|
279
|
+
runtimeContext
|
|
280
|
+
}) {
|
|
281
|
+
try {
|
|
282
|
+
if (!workflowId) {
|
|
283
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
284
|
+
}
|
|
285
|
+
if (!runId) {
|
|
286
|
+
throw new HTTPException(400, { message: "runId required to resume workflow" });
|
|
287
|
+
}
|
|
288
|
+
if (!body.step) {
|
|
289
|
+
throw new HTTPException(400, { message: "step required to resume workflow" });
|
|
290
|
+
}
|
|
291
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
292
|
+
const run = await workflow.getWorkflowRunById(runId);
|
|
293
|
+
if (!run) {
|
|
294
|
+
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
295
|
+
}
|
|
296
|
+
const _run = workflow.createRun({ runId });
|
|
297
|
+
void _run.resume({
|
|
298
|
+
step: body.step,
|
|
299
|
+
resumeData: body.resumeData,
|
|
300
|
+
runtimeContext
|
|
301
|
+
});
|
|
302
|
+
return { message: "Workflow run resumed" };
|
|
303
|
+
} catch (error) {
|
|
304
|
+
return handleError(error, "Error resuming workflow");
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async function getWorkflowRunsHandler({
|
|
308
|
+
mastra,
|
|
309
|
+
workflowId,
|
|
310
|
+
fromDate,
|
|
311
|
+
toDate,
|
|
312
|
+
limit,
|
|
313
|
+
offset,
|
|
314
|
+
resourceId
|
|
315
|
+
}) {
|
|
316
|
+
try {
|
|
317
|
+
if (!workflowId) {
|
|
318
|
+
throw new HTTPException(400, { message: "Workflow ID is required" });
|
|
319
|
+
}
|
|
320
|
+
const workflow = mastra.getWorkflow(workflowId);
|
|
321
|
+
const workflowRuns = await workflow.getWorkflowRuns({ fromDate, toDate, limit, offset, resourceId }) || {
|
|
322
|
+
runs: [],
|
|
323
|
+
total: 0
|
|
324
|
+
};
|
|
325
|
+
return workflowRuns;
|
|
326
|
+
} catch (error) {
|
|
327
|
+
return handleError(error, "Error getting workflow runs");
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export { createWorkflowRunHandler, getWorkflowByIdHandler, getWorkflowRunByIdHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, watchWorkflowHandler, workflows_exports };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { stringify, esm_default } from './chunk-
|
|
2
|
-
import { validateBody } from './chunk-
|
|
3
|
-
import { handleError } from './chunk-
|
|
4
|
-
import {
|
|
1
|
+
import { stringify, esm_default } from './chunk-LIVAK2DM.js';
|
|
2
|
+
import { validateBody } from './chunk-H5PTF3Y4.js';
|
|
3
|
+
import { handleError } from './chunk-M5ABIP7D.js';
|
|
4
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
5
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
6
|
+
import { RuntimeContext } from '@mastra/core/di';
|
|
5
7
|
import { isVercelTool } from '@mastra/core/tools';
|
|
6
8
|
|
|
7
9
|
// src/server/handlers/tools.ts
|
|
@@ -55,7 +57,9 @@ function executeToolHandler(tools) {
|
|
|
55
57
|
mastra,
|
|
56
58
|
runId,
|
|
57
59
|
toolId,
|
|
58
|
-
data
|
|
60
|
+
data,
|
|
61
|
+
runtimeContext,
|
|
62
|
+
runtimeContextFromRequest
|
|
59
63
|
}) => {
|
|
60
64
|
try {
|
|
61
65
|
if (!toolId) {
|
|
@@ -73,10 +77,15 @@ function executeToolHandler(tools) {
|
|
|
73
77
|
const result2 = await tool.execute(data);
|
|
74
78
|
return result2;
|
|
75
79
|
}
|
|
80
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
81
|
+
...Array.from(runtimeContext.entries()),
|
|
82
|
+
...Array.from(Object.entries(runtimeContextFromRequest ?? {}))
|
|
83
|
+
]);
|
|
76
84
|
const result = await tool.execute({
|
|
77
85
|
context: data,
|
|
78
86
|
mastra,
|
|
79
|
-
runId
|
|
87
|
+
runId,
|
|
88
|
+
runtimeContext: finalRuntimeContext
|
|
80
89
|
});
|
|
81
90
|
return result;
|
|
82
91
|
} catch (error) {
|
|
@@ -88,7 +97,9 @@ async function executeAgentToolHandler({
|
|
|
88
97
|
mastra,
|
|
89
98
|
agentId,
|
|
90
99
|
toolId,
|
|
91
|
-
data
|
|
100
|
+
data,
|
|
101
|
+
runtimeContext,
|
|
102
|
+
runtimeContextFromRequest
|
|
92
103
|
}) {
|
|
93
104
|
try {
|
|
94
105
|
const agent = agentId ? mastra.getAgent(agentId) : null;
|
|
@@ -102,8 +113,13 @@ async function executeAgentToolHandler({
|
|
|
102
113
|
if (!tool?.execute) {
|
|
103
114
|
throw new HTTPException(400, { message: "Tool is not executable" });
|
|
104
115
|
}
|
|
116
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
117
|
+
...Array.from(runtimeContext.entries()),
|
|
118
|
+
...Array.from(Object.entries(runtimeContextFromRequest ?? {}))
|
|
119
|
+
]);
|
|
105
120
|
const result = await tool.execute({
|
|
106
121
|
context: data,
|
|
122
|
+
runtimeContext: finalRuntimeContext,
|
|
107
123
|
mastra,
|
|
108
124
|
runId: agentId
|
|
109
125
|
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk57CJTIPW_cjs = require('./chunk-57CJTIPW.cjs');
|
|
4
|
+
var chunk64U3UDTH_cjs = require('./chunk-64U3UDTH.cjs');
|
|
5
|
+
var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
|
|
6
|
+
var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
|
|
7
|
+
|
|
8
|
+
// src/server/handlers/network.ts
|
|
9
|
+
var network_exports = {};
|
|
10
|
+
chunk75ZPJI57_cjs.__export(network_exports, {
|
|
11
|
+
generateHandler: () => generateHandler,
|
|
12
|
+
getNetworkByIdHandler: () => getNetworkByIdHandler,
|
|
13
|
+
getNetworksHandler: () => getNetworksHandler,
|
|
14
|
+
streamGenerateHandler: () => streamGenerateHandler
|
|
15
|
+
});
|
|
16
|
+
async function getNetworksHandler({
|
|
17
|
+
mastra,
|
|
18
|
+
runtimeContext
|
|
19
|
+
}) {
|
|
20
|
+
try {
|
|
21
|
+
const networks = mastra.getNetworks();
|
|
22
|
+
const serializedNetworks = await Promise.all(
|
|
23
|
+
networks.map(async (network) => {
|
|
24
|
+
const routingAgent = network.getRoutingAgent();
|
|
25
|
+
const routingLLM = await routingAgent.getLLM({ runtimeContext });
|
|
26
|
+
const agents = network.getAgents();
|
|
27
|
+
return {
|
|
28
|
+
id: network.formatAgentId(routingAgent.name),
|
|
29
|
+
name: routingAgent.name,
|
|
30
|
+
instructions: routingAgent.instructions,
|
|
31
|
+
agents: await Promise.all(
|
|
32
|
+
agents.map(async (agent) => {
|
|
33
|
+
const llm = await agent.getLLM({ runtimeContext });
|
|
34
|
+
return {
|
|
35
|
+
name: agent.name,
|
|
36
|
+
provider: llm?.getProvider(),
|
|
37
|
+
modelId: llm?.getModelId()
|
|
38
|
+
};
|
|
39
|
+
})
|
|
40
|
+
),
|
|
41
|
+
routingModel: {
|
|
42
|
+
provider: routingLLM?.getProvider(),
|
|
43
|
+
modelId: routingLLM?.getModelId()
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
return serializedNetworks;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error getting networks");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function getNetworkByIdHandler({
|
|
54
|
+
mastra,
|
|
55
|
+
networkId,
|
|
56
|
+
runtimeContext
|
|
57
|
+
}) {
|
|
58
|
+
try {
|
|
59
|
+
const networks = mastra.getNetworks();
|
|
60
|
+
const network = networks.find((network2) => {
|
|
61
|
+
const routingAgent2 = network2.getRoutingAgent();
|
|
62
|
+
return network2.formatAgentId(routingAgent2.name) === networkId;
|
|
63
|
+
});
|
|
64
|
+
if (!network) {
|
|
65
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
66
|
+
}
|
|
67
|
+
const routingAgent = network.getRoutingAgent();
|
|
68
|
+
const routingLLM = await routingAgent.getLLM({ runtimeContext });
|
|
69
|
+
const agents = network.getAgents();
|
|
70
|
+
const serializedNetwork = {
|
|
71
|
+
id: network.formatAgentId(routingAgent.name),
|
|
72
|
+
name: routingAgent.name,
|
|
73
|
+
instructions: routingAgent.instructions,
|
|
74
|
+
agents: await Promise.all(
|
|
75
|
+
agents.map(async (agent) => {
|
|
76
|
+
const llm = await agent.getLLM({ runtimeContext });
|
|
77
|
+
return {
|
|
78
|
+
name: agent.name,
|
|
79
|
+
provider: llm?.getProvider(),
|
|
80
|
+
modelId: llm?.getModelId()
|
|
81
|
+
};
|
|
82
|
+
})
|
|
83
|
+
),
|
|
84
|
+
routingModel: {
|
|
85
|
+
provider: routingLLM?.getProvider(),
|
|
86
|
+
modelId: routingLLM?.getModelId()
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return serializedNetwork;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error getting network by ID");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function generateHandler({
|
|
95
|
+
mastra,
|
|
96
|
+
runtimeContext,
|
|
97
|
+
networkId,
|
|
98
|
+
body
|
|
99
|
+
}) {
|
|
100
|
+
try {
|
|
101
|
+
const network = mastra.getNetwork(networkId);
|
|
102
|
+
if (!network) {
|
|
103
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
104
|
+
}
|
|
105
|
+
chunk57CJTIPW_cjs.validateBody({ messages: body.messages });
|
|
106
|
+
const { messages, ...rest } = body;
|
|
107
|
+
const result = await network.generate(messages, { ...rest, runtimeContext });
|
|
108
|
+
return result;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error generating from network");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function streamGenerateHandler({
|
|
114
|
+
mastra,
|
|
115
|
+
networkId,
|
|
116
|
+
body,
|
|
117
|
+
runtimeContext
|
|
118
|
+
}) {
|
|
119
|
+
try {
|
|
120
|
+
const network = mastra.getNetwork(networkId);
|
|
121
|
+
if (!network) {
|
|
122
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
123
|
+
}
|
|
124
|
+
chunk57CJTIPW_cjs.validateBody({ messages: body.messages });
|
|
125
|
+
const { messages, output, ...rest } = body;
|
|
126
|
+
const streamResult = await network.stream(messages, {
|
|
127
|
+
output,
|
|
128
|
+
...rest,
|
|
129
|
+
runtimeContext
|
|
130
|
+
});
|
|
131
|
+
const streamResponse = output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
|
|
132
|
+
sendUsage: true,
|
|
133
|
+
sendReasoning: true,
|
|
134
|
+
getErrorMessage: (error) => {
|
|
135
|
+
return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return streamResponse;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error streaming from network");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
exports.generateHandler = generateHandler;
|
|
145
|
+
exports.getNetworkByIdHandler = getNetworkByIdHandler;
|
|
146
|
+
exports.getNetworksHandler = getNetworksHandler;
|
|
147
|
+
exports.network_exports = network_exports;
|
|
148
|
+
exports.streamGenerateHandler = streamGenerateHandler;
|