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