@mastra/server 0.0.0-ai-v5-20250626003446 → 0.0.0-ai-v5-20250718021026

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 (69) hide show
  1. package/LICENSE.md +11 -42
  2. package/dist/_tsup-dts-rollup.d.cts +265 -22
  3. package/dist/_tsup-dts-rollup.d.ts +265 -22
  4. package/dist/{chunk-OCWPVYNI.cjs → chunk-2KZFMI6P.cjs} +1 -0
  5. package/dist/{chunk-5BKZNI2F.cjs → chunk-34GEZJPU.cjs} +29 -23
  6. package/dist/{chunk-TGJMNUYJ.js → chunk-36BLNJHS.js} +108 -70
  7. package/dist/{chunk-XUTX4CAB.cjs → chunk-3TMDONWY.cjs} +154 -75
  8. package/dist/{chunk-QLG2PFHE.js → chunk-4D66QEKC.js} +2 -2
  9. package/dist/{chunk-BNEY4P4P.cjs → chunk-7TP2LX5L.cjs} +16 -16
  10. package/dist/{chunk-P54JFWUD.js → chunk-BYCOGHDE.js} +3 -3
  11. package/dist/{chunk-57CJTIPW.cjs → chunk-CCGRCYWJ.cjs} +2 -2
  12. package/dist/{chunk-B4MQFJ7G.cjs → chunk-G7KH752Y.cjs} +5 -5
  13. package/dist/{chunk-I3FR2N5I.cjs → chunk-HAGPF7OS.cjs} +12 -12
  14. package/dist/{chunk-ASKESBJW.cjs → chunk-HISYIDYH.cjs} +107 -69
  15. package/dist/{chunk-NGURCFEJ.cjs → chunk-HRSAZUIK.cjs} +20 -20
  16. package/dist/{chunk-R5VGYBV6.js → chunk-KHZKYUNR.js} +3 -3
  17. package/dist/{chunk-NYN7KFXL.js → chunk-LCM566I4.js} +1 -0
  18. package/dist/chunk-LF7P5PLR.js +14 -0
  19. package/dist/{chunk-BSUAZLUK.js → chunk-N6KVUULV.js} +110 -25
  20. package/dist/{chunk-2XXCPCYB.js → chunk-OKL5JVWU.js} +15 -9
  21. package/dist/{chunk-3CSC35VL.js → chunk-ORHUZSM2.js} +96 -19
  22. package/dist/chunk-PZQDCRPV.cjs +16 -0
  23. package/dist/{chunk-IMBY5XUG.cjs → chunk-QGX47B5D.cjs} +7 -7
  24. package/dist/{chunk-BFOA2QQY.js → chunk-RG473F6Y.js} +2 -2
  25. package/dist/{chunk-WJY57THV.cjs → chunk-RHSWAXKB.cjs} +22 -22
  26. package/dist/{chunk-H5PTF3Y4.js → chunk-RSEO4XPX.js} +1 -1
  27. package/dist/{chunk-WE32JG64.cjs → chunk-UCV4247U.cjs} +43 -43
  28. package/dist/chunk-VDTQM2M5.cjs +321 -0
  29. package/dist/{chunk-2HXKRRNS.js → chunk-WBRFIHSF.js} +4 -4
  30. package/dist/{chunk-55DOQLP6.js → chunk-WNVFNNWN.js} +2 -2
  31. package/dist/{chunk-CLYX4KLH.js → chunk-ZYAFP2AV.js} +8 -8
  32. package/dist/server/handlers/agents.cjs +7 -7
  33. package/dist/server/handlers/agents.js +1 -1
  34. package/dist/server/handlers/error.cjs +2 -2
  35. package/dist/server/handlers/error.js +1 -1
  36. package/dist/server/handlers/legacyWorkflows.cjs +11 -11
  37. package/dist/server/handlers/legacyWorkflows.js +1 -1
  38. package/dist/server/handlers/logs.cjs +4 -4
  39. package/dist/server/handlers/logs.js +1 -1
  40. package/dist/server/handlers/memory.cjs +17 -9
  41. package/dist/server/handlers/memory.d.cts +2 -0
  42. package/dist/server/handlers/memory.d.ts +2 -0
  43. package/dist/server/handlers/memory.js +1 -1
  44. package/dist/server/handlers/network.cjs +5 -5
  45. package/dist/server/handlers/network.js +1 -1
  46. package/dist/server/handlers/telemetry.cjs +3 -3
  47. package/dist/server/handlers/telemetry.js +1 -1
  48. package/dist/server/handlers/tools.cjs +5 -5
  49. package/dist/server/handlers/tools.js +1 -1
  50. package/dist/server/handlers/utils.cjs +2 -2
  51. package/dist/server/handlers/utils.js +1 -1
  52. package/dist/server/handlers/vNextNetwork.cjs +218 -0
  53. package/dist/server/handlers/vNextNetwork.d.cts +6 -0
  54. package/dist/server/handlers/vNextNetwork.d.ts +6 -0
  55. package/dist/server/handlers/vNextNetwork.js +211 -0
  56. package/dist/server/handlers/vector.cjs +7 -7
  57. package/dist/server/handlers/vector.js +1 -1
  58. package/dist/server/handlers/voice.cjs +5 -5
  59. package/dist/server/handlers/voice.js +1 -1
  60. package/dist/server/handlers/workflows.cjs +21 -13
  61. package/dist/server/handlers/workflows.d.cts +2 -0
  62. package/dist/server/handlers/workflows.d.ts +2 -0
  63. package/dist/server/handlers/workflows.js +1 -1
  64. package/dist/server/handlers.cjs +20 -20
  65. package/dist/server/handlers.js +10 -10
  66. package/package.json +9 -9
  67. package/dist/chunk-64U3UDTH.cjs +0 -13
  68. package/dist/chunk-M5ABIP7D.js +0 -11
  69. package/dist/chunk-PADRC33G.cjs +0 -234
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
- var chunkASKESBJW_cjs = require('./chunk-ASKESBJW.cjs');
4
- var chunk57CJTIPW_cjs = require('./chunk-57CJTIPW.cjs');
5
- var chunk64U3UDTH_cjs = require('./chunk-64U3UDTH.cjs');
6
- var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
3
+ var chunkHISYIDYH_cjs = require('./chunk-HISYIDYH.cjs');
4
+ var chunkCCGRCYWJ_cjs = require('./chunk-CCGRCYWJ.cjs');
5
+ var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
6
+ var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
7
7
  var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
8
8
  var tools = require('@mastra/core/tools');
9
9
 
@@ -25,8 +25,8 @@ async function getToolsHandler({ tools }) {
25
25
  const tool = _tool;
26
26
  acc[id] = {
27
27
  ...tool,
28
- inputSchema: tool.inputSchema ? chunkASKESBJW_cjs.stringify(chunkASKESBJW_cjs.esm_default(tool.inputSchema)) : void 0,
29
- outputSchema: tool.outputSchema ? chunkASKESBJW_cjs.stringify(chunkASKESBJW_cjs.esm_default(tool.outputSchema)) : void 0
28
+ inputSchema: tool.inputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(tool.inputSchema)) : void 0,
29
+ outputSchema: tool.outputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(tool.outputSchema)) : void 0
30
30
  };
31
31
  return acc;
32
32
  },
@@ -34,23 +34,23 @@ async function getToolsHandler({ tools }) {
34
34
  );
35
35
  return serializedTools;
36
36
  } catch (error) {
37
- return chunk64U3UDTH_cjs.handleError(error, "Error getting tools");
37
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting tools");
38
38
  }
39
39
  }
40
40
  async function getToolByIdHandler({ tools, toolId }) {
41
41
  try {
42
42
  const tool = Object.values(tools || {}).find((tool2) => tool2.id === toolId);
43
43
  if (!tool) {
44
- throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Tool not found" });
44
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Tool not found" });
45
45
  }
46
46
  const serializedTool = {
47
47
  ...tool,
48
- inputSchema: tool.inputSchema ? chunkASKESBJW_cjs.stringify(chunkASKESBJW_cjs.esm_default(tool.inputSchema)) : void 0,
49
- outputSchema: tool.outputSchema ? chunkASKESBJW_cjs.stringify(chunkASKESBJW_cjs.esm_default(tool.outputSchema)) : void 0
48
+ inputSchema: tool.inputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(tool.inputSchema)) : void 0,
49
+ outputSchema: tool.outputSchema ? chunkHISYIDYH_cjs.stringify(chunkHISYIDYH_cjs.esm_default(tool.outputSchema)) : void 0
50
50
  };
51
51
  return serializedTool;
52
52
  } catch (error) {
53
- return chunk64U3UDTH_cjs.handleError(error, "Error getting tool");
53
+ return chunkPZQDCRPV_cjs.handleError(error, "Error getting tool");
54
54
  }
55
55
  }
56
56
  function executeToolHandler(tools$1) {
@@ -63,16 +63,16 @@ function executeToolHandler(tools$1) {
63
63
  }) => {
64
64
  try {
65
65
  if (!toolId) {
66
- throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Tool ID is required" });
66
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Tool ID is required" });
67
67
  }
68
68
  const tool = Object.values(tools$1 || {}).find((tool2) => tool2.id === toolId);
69
69
  if (!tool) {
70
- throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Tool not found" });
70
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Tool not found" });
71
71
  }
72
72
  if (!tool?.execute) {
73
- throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Tool is not executable" });
73
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Tool is not executable" });
74
74
  }
75
- chunk57CJTIPW_cjs.validateBody({ data });
75
+ chunkCCGRCYWJ_cjs.validateBody({ data });
76
76
  if (tools.isVercelTool(tool)) {
77
77
  const result2 = await tool.execute(data);
78
78
  return result2;
@@ -85,7 +85,7 @@ function executeToolHandler(tools$1) {
85
85
  });
86
86
  return result;
87
87
  } catch (error) {
88
- return chunk64U3UDTH_cjs.handleError(error, "Error executing tool");
88
+ return chunkPZQDCRPV_cjs.handleError(error, "Error executing tool");
89
89
  }
90
90
  };
91
91
  }
@@ -99,15 +99,15 @@ async function executeAgentToolHandler({
99
99
  try {
100
100
  const agent = agentId ? mastra.getAgent(agentId) : null;
101
101
  if (!agent) {
102
- throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Tool not found" });
102
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Tool not found" });
103
103
  }
104
104
  const agentTools = await agent.getTools({ runtimeContext });
105
105
  const tool = Object.values(agentTools || {}).find((tool2) => tool2.id === toolId);
106
106
  if (!tool) {
107
- throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Tool not found" });
107
+ throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Tool not found" });
108
108
  }
109
109
  if (!tool?.execute) {
110
- throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Tool is not executable" });
110
+ throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Tool is not executable" });
111
111
  }
112
112
  const result = await tool.execute({
113
113
  context: data,
@@ -117,7 +117,7 @@ async function executeAgentToolHandler({
117
117
  });
118
118
  return result;
119
119
  } catch (error) {
120
- return chunk64U3UDTH_cjs.handleError(error, "Error executing tool");
120
+ return chunkPZQDCRPV_cjs.handleError(error, "Error executing tool");
121
121
  }
122
122
  }
123
123
 
@@ -1,6 +1,6 @@
1
- import { validateBody } from './chunk-H5PTF3Y4.js';
2
- import { handleError } from './chunk-M5ABIP7D.js';
3
- import { HTTPException } from './chunk-NYN7KFXL.js';
1
+ import { validateBody } from './chunk-RSEO4XPX.js';
2
+ import { handleError } from './chunk-LF7P5PLR.js';
3
+ import { HTTPException } from './chunk-LCM566I4.js';
4
4
  import { __export } from './chunk-MLKGABMK.js';
5
5
  import { Readable } from 'stream';
6
6
 
@@ -11,6 +11,7 @@ var HTTPException = class extends Error {
11
11
  super(options?.message, { cause: options?.cause });
12
12
  this.res = options?.res;
13
13
  this.status = status;
14
+ this.stack = options?.stack || this.stack;
14
15
  }
15
16
  /**
16
17
  * Returns the response object associated with the exception.
@@ -0,0 +1,14 @@
1
+ import { HTTPException } from './chunk-LCM566I4.js';
2
+
3
+ // src/server/handlers/error.ts
4
+ function handleError(error, defaultMessage) {
5
+ const apiError = error;
6
+ const apiErrorStatus = apiError.status || apiError.details?.status || 500;
7
+ throw new HTTPException(apiErrorStatus, {
8
+ message: apiError.message || defaultMessage,
9
+ stack: apiError.stack,
10
+ cause: apiError.cause
11
+ });
12
+ }
13
+
14
+ export { handleError };
@@ -1,7 +1,8 @@
1
- import { validateBody } from './chunk-H5PTF3Y4.js';
2
- import { handleError } from './chunk-M5ABIP7D.js';
3
- import { HTTPException } from './chunk-NYN7KFXL.js';
1
+ import { validateBody } from './chunk-RSEO4XPX.js';
2
+ import { handleError } from './chunk-LF7P5PLR.js';
3
+ import { HTTPException } from './chunk-LCM566I4.js';
4
4
  import { __export } from './chunk-MLKGABMK.js';
5
+ import { generateEmptyFromSchema } from '@mastra/core';
5
6
 
6
7
  // src/server/handlers/memory.ts
7
8
  var memory_exports = {};
@@ -12,23 +13,41 @@ __export(memory_exports, {
12
13
  getMessagesHandler: () => getMessagesHandler,
13
14
  getThreadByIdHandler: () => getThreadByIdHandler,
14
15
  getThreadsHandler: () => getThreadsHandler,
16
+ getWorkingMemoryHandler: () => getWorkingMemoryHandler,
15
17
  saveMessagesHandler: () => saveMessagesHandler,
16
- updateThreadHandler: () => updateThreadHandler
18
+ updateThreadHandler: () => updateThreadHandler,
19
+ updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
17
20
  });
18
- function getMemoryFromContext({
21
+ async function getMemoryFromContext({
19
22
  mastra,
20
- agentId
23
+ agentId,
24
+ networkId,
25
+ runtimeContext
21
26
  }) {
22
27
  const agent = agentId ? mastra.getAgent(agentId) : null;
23
28
  if (agentId && !agent) {
24
29
  throw new HTTPException(404, { message: "Agent not found" });
25
30
  }
26
- const memory = agent?.getMemory?.() || mastra.getMemory();
27
- return memory;
31
+ const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
32
+ if (networkId && !network) {
33
+ throw new HTTPException(404, { message: "Network not found" });
34
+ }
35
+ if (agent) {
36
+ return agent?.getMemory() || mastra.getMemory();
37
+ }
38
+ if (network) {
39
+ return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
40
+ }
41
+ return mastra.getMemory();
28
42
  }
29
- async function getMemoryStatusHandler({ mastra, agentId }) {
43
+ async function getMemoryStatusHandler({
44
+ mastra,
45
+ agentId,
46
+ networkId,
47
+ runtimeContext
48
+ }) {
30
49
  try {
31
- const memory = getMemoryFromContext({ mastra, agentId });
50
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
32
51
  if (!memory) {
33
52
  return { result: false };
34
53
  }
@@ -40,10 +59,12 @@ async function getMemoryStatusHandler({ mastra, agentId }) {
40
59
  async function getThreadsHandler({
41
60
  mastra,
42
61
  agentId,
43
- resourceId
62
+ resourceId,
63
+ networkId,
64
+ runtimeContext
44
65
  }) {
45
66
  try {
46
- const memory = getMemoryFromContext({ mastra, agentId });
67
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
47
68
  if (!memory) {
48
69
  throw new HTTPException(400, { message: "Memory is not initialized" });
49
70
  }
@@ -57,11 +78,13 @@ async function getThreadsHandler({
57
78
  async function getThreadByIdHandler({
58
79
  mastra,
59
80
  agentId,
60
- threadId
81
+ threadId,
82
+ networkId,
83
+ runtimeContext
61
84
  }) {
62
85
  try {
63
86
  validateBody({ threadId });
64
- const memory = getMemoryFromContext({ mastra, agentId });
87
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
65
88
  if (!memory) {
66
89
  throw new HTTPException(400, { message: "Memory is not initialized" });
67
90
  }
@@ -77,10 +100,12 @@ async function getThreadByIdHandler({
77
100
  async function saveMessagesHandler({
78
101
  mastra,
79
102
  agentId,
80
- body
103
+ body,
104
+ networkId,
105
+ runtimeContext
81
106
  }) {
82
107
  try {
83
- const memory = getMemoryFromContext({ mastra, agentId });
108
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
84
109
  if (!memory) {
85
110
  throw new HTTPException(400, { message: "Memory is not initialized" });
86
111
  }
@@ -104,10 +129,12 @@ async function saveMessagesHandler({
104
129
  async function createThreadHandler({
105
130
  mastra,
106
131
  agentId,
107
- body
132
+ body,
133
+ networkId,
134
+ runtimeContext
108
135
  }) {
109
136
  try {
110
- const memory = getMemoryFromContext({ mastra, agentId });
137
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
111
138
  if (!memory) {
112
139
  throw new HTTPException(400, { message: "Memory is not initialized" });
113
140
  }
@@ -127,10 +154,12 @@ async function updateThreadHandler({
127
154
  mastra,
128
155
  agentId,
129
156
  threadId,
130
- body
157
+ body,
158
+ networkId,
159
+ runtimeContext
131
160
  }) {
132
161
  try {
133
- const memory = getMemoryFromContext({ mastra, agentId });
162
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
134
163
  if (!body) {
135
164
  throw new HTTPException(400, { message: "Body is required" });
136
165
  }
@@ -161,11 +190,13 @@ async function updateThreadHandler({
161
190
  async function deleteThreadHandler({
162
191
  mastra,
163
192
  agentId,
164
- threadId
193
+ threadId,
194
+ networkId,
195
+ runtimeContext
165
196
  }) {
166
197
  try {
167
198
  validateBody({ threadId });
168
- const memory = getMemoryFromContext({ mastra, agentId });
199
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
169
200
  if (!memory) {
170
201
  throw new HTTPException(400, { message: "Memory is not initialized" });
171
202
  }
@@ -185,14 +216,16 @@ async function getMessagesHandler({
185
216
  threadId,
186
217
  limit,
187
218
  format,
188
- clientSdkCompat
219
+ clientSdkCompat,
220
+ networkId,
221
+ runtimeContext
189
222
  }) {
190
223
  if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
191
224
  throw new HTTPException(400, { message: "Invalid limit: must be a positive integer" });
192
225
  }
193
226
  try {
194
227
  validateBody({ threadId });
195
- const memory = getMemoryFromContext({ mastra, agentId });
228
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
196
229
  if (!memory) {
197
230
  throw new HTTPException(400, { message: "Memory is not initialized" });
198
231
  }
@@ -220,5 +253,57 @@ async function getMessagesHandler({
220
253
  return handleError(error, "Error getting messages");
221
254
  }
222
255
  }
256
+ async function getWorkingMemoryHandler({
257
+ mastra,
258
+ agentId,
259
+ threadId,
260
+ resourceId,
261
+ networkId,
262
+ runtimeContext,
263
+ memoryConfig
264
+ }) {
265
+ try {
266
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
267
+ validateBody({ threadId });
268
+ if (!memory) {
269
+ throw new HTTPException(400, { message: "Memory is not initialized" });
270
+ }
271
+ const thread = await memory.getThreadById({ threadId });
272
+ const threadExists = !!thread;
273
+ const template = await memory.getWorkingMemoryTemplate({ memoryConfig });
274
+ const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(generateEmptyFromSchema(template.content)) } : template;
275
+ const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
276
+ const config = memory.getMergedThreadConfig(memoryConfig || {});
277
+ const source = config.workingMemory?.scope === "resource" && resourceId ? "resource" : "thread";
278
+ return { workingMemory, source, workingMemoryTemplate, threadExists };
279
+ } catch (error) {
280
+ return handleError(error, "Error getting working memory");
281
+ }
282
+ }
283
+ async function updateWorkingMemoryHandler({
284
+ mastra,
285
+ agentId,
286
+ threadId,
287
+ body,
288
+ networkId,
289
+ runtimeContext
290
+ }) {
291
+ try {
292
+ validateBody({ threadId });
293
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
294
+ const { resourceId, memoryConfig, workingMemory } = body;
295
+ if (!memory) {
296
+ throw new HTTPException(400, { message: "Memory is not initialized" });
297
+ }
298
+ const thread = await memory.getThreadById({ threadId });
299
+ if (!thread) {
300
+ throw new HTTPException(404, { message: "Thread not found" });
301
+ }
302
+ await memory.updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig });
303
+ return { success: true };
304
+ } catch (error) {
305
+ return handleError(error, "Error updating working memory");
306
+ }
307
+ }
223
308
 
224
- export { createThreadHandler, deleteThreadHandler, getMemoryStatusHandler, getMessagesHandler, getThreadByIdHandler, getThreadsHandler, memory_exports, saveMessagesHandler, updateThreadHandler };
309
+ export { createThreadHandler, deleteThreadHandler, getMemoryStatusHandler, getMessagesHandler, getThreadByIdHandler, getThreadsHandler, getWorkingMemoryHandler, memory_exports, saveMessagesHandler, updateThreadHandler, updateWorkingMemoryHandler };
@@ -1,7 +1,7 @@
1
- import { stringify, esm_default } from './chunk-TGJMNUYJ.js';
2
- import { validateBody } from './chunk-H5PTF3Y4.js';
3
- import { handleError } from './chunk-M5ABIP7D.js';
4
- import { HTTPException } from './chunk-NYN7KFXL.js';
1
+ import { stringify, esm_default } from './chunk-36BLNJHS.js';
2
+ import { validateBody } from './chunk-RSEO4XPX.js';
3
+ import { handleError } from './chunk-LF7P5PLR.js';
4
+ import { HTTPException } from './chunk-LCM566I4.js';
5
5
  import { __export } from './chunk-MLKGABMK.js';
6
6
  import { createV4CompatibleResponse } from '@mastra/core/agent';
7
7
  import { RuntimeContext } from '@mastra/core/runtime-context';
@@ -195,10 +195,12 @@ async function generateHandler({
195
195
  mastra,
196
196
  runtimeContext,
197
197
  agentId,
198
- body
198
+ body,
199
+ abortSignal
199
200
  }) {
200
201
  try {
201
202
  const agent = mastra.getAgent(agentId);
203
+ console.log("agent", agent);
202
204
  if (!agent) {
203
205
  throw new HTTPException(404, { message: "Agent not found" });
204
206
  }
@@ -213,10 +215,12 @@ async function generateHandler({
213
215
  ...rest,
214
216
  // @ts-expect-error TODO fix types
215
217
  resourceId: finalResourceId,
216
- runtimeContext: finalRuntimeContext
218
+ runtimeContext: finalRuntimeContext,
219
+ signal: abortSignal
217
220
  });
218
221
  return result;
219
222
  } catch (error) {
223
+ console.log("error", error);
220
224
  return handleError(error, "Error generating from agent");
221
225
  }
222
226
  }
@@ -225,7 +229,8 @@ async function streamGenerateHandler({
225
229
  runtimeContext,
226
230
  agentId,
227
231
  body,
228
- clientSdkCompat
232
+ clientSdkCompat,
233
+ abortSignal
229
234
  }) {
230
235
  try {
231
236
  const agent = mastra.getAgent(agentId);
@@ -242,7 +247,8 @@ async function streamGenerateHandler({
242
247
  ...rest,
243
248
  // @ts-expect-error TODO fix types
244
249
  resourceId,
245
- runtimeContext: finalRuntimeContext
250
+ runtimeContext: finalRuntimeContext,
251
+ signal: abortSignal
246
252
  });
247
253
  const headers = {
248
254
  "Transfer-Encoding": "chunked"
@@ -262,7 +268,7 @@ async function streamGenerateHandler({
262
268
  const streamResponse = useV4Compat ? createV4CompatibleResponse(uiMessageStream.body) : uiMessageStream;
263
269
  return streamResponse;
264
270
  } catch (error) {
265
- throw new HTTPException(error?.status ?? 500, { message: error?.message ?? "Error streaming from agent" });
271
+ return handleError(error, "error streaming agent response");
266
272
  }
267
273
  }
268
274
 
@@ -1,12 +1,13 @@
1
- import { stringify, esm_default } from './chunk-TGJMNUYJ.js';
2
- import { handleError } from './chunk-M5ABIP7D.js';
3
- import { HTTPException } from './chunk-NYN7KFXL.js';
1
+ import { stringify, esm_default } from './chunk-36BLNJHS.js';
2
+ import { handleError } from './chunk-LF7P5PLR.js';
3
+ import { HTTPException } from './chunk-LCM566I4.js';
4
4
  import { __export } from './chunk-MLKGABMK.js';
5
5
  import { ReadableStream } from 'stream/web';
6
6
 
7
7
  // src/server/handlers/workflows.ts
8
8
  var workflows_exports = {};
9
9
  __export(workflows_exports, {
10
+ cancelWorkflowRunHandler: () => cancelWorkflowRunHandler,
10
11
  createWorkflowRunHandler: () => createWorkflowRunHandler,
11
12
  getWorkflowByIdHandler: () => getWorkflowByIdHandler,
12
13
  getWorkflowRunByIdHandler: () => getWorkflowRunByIdHandler,
@@ -15,11 +16,31 @@ __export(workflows_exports, {
15
16
  getWorkflowsHandler: () => getWorkflowsHandler,
16
17
  resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
17
18
  resumeWorkflowHandler: () => resumeWorkflowHandler,
19
+ sendWorkflowRunEventHandler: () => sendWorkflowRunEventHandler,
18
20
  startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
19
21
  startWorkflowRunHandler: () => startWorkflowRunHandler,
20
22
  streamWorkflowHandler: () => streamWorkflowHandler,
21
23
  watchWorkflowHandler: () => watchWorkflowHandler
22
24
  });
25
+ function getSteps(steps, path) {
26
+ return Object.entries(steps).reduce((acc, [key, step]) => {
27
+ const fullKey = path ? `${path}.${key}` : key;
28
+ acc[fullKey] = {
29
+ id: step.id,
30
+ description: step.description,
31
+ inputSchema: step.inputSchema ? stringify(esm_default(step.inputSchema)) : void 0,
32
+ outputSchema: step.outputSchema ? stringify(esm_default(step.outputSchema)) : void 0,
33
+ resumeSchema: step.resumeSchema ? stringify(esm_default(step.resumeSchema)) : void 0,
34
+ suspendSchema: step.suspendSchema ? stringify(esm_default(step.suspendSchema)) : void 0,
35
+ isWorkflow: step.component === "WORKFLOW"
36
+ };
37
+ if (step.component === "WORKFLOW" && step.steps) {
38
+ const nestedSteps = getSteps(step.steps, fullKey) || {};
39
+ acc = { ...acc, ...nestedSteps };
40
+ }
41
+ return acc;
42
+ }, {});
43
+ }
23
44
  async function getWorkflowsHandler({ mastra }) {
24
45
  try {
25
46
  const workflows = mastra.getWorkflows({ serialized: false });
@@ -38,6 +59,7 @@ async function getWorkflowsHandler({ mastra }) {
38
59
  };
39
60
  return acc2;
40
61
  }, {}),
62
+ allSteps: getSteps(workflow.steps) || {},
41
63
  stepGraph: workflow.serializedStepGraph,
42
64
  inputSchema: workflow.inputSchema ? stringify(esm_default(workflow.inputSchema)) : void 0,
43
65
  outputSchema: workflow.outputSchema ? stringify(esm_default(workflow.outputSchema)) : void 0
@@ -46,7 +68,7 @@ async function getWorkflowsHandler({ mastra }) {
46
68
  }, {});
47
69
  return _workflows;
48
70
  } catch (error) {
49
- throw new HTTPException(500, { message: error?.message || "Error getting workflows" });
71
+ return handleError(error, "Error getting workflows");
50
72
  }
51
73
  }
52
74
  async function getWorkflowsFromSystem({ mastra, workflowId }) {
@@ -104,6 +126,7 @@ async function getWorkflowByIdHandler({ mastra, workflowId }) {
104
126
  };
105
127
  return acc;
106
128
  }, {}),
129
+ allSteps: getSteps(workflow.steps) || {},
107
130
  name: workflow.name,
108
131
  description: workflow.description,
109
132
  stepGraph: workflow.serializedStepGraph,
@@ -111,7 +134,7 @@ async function getWorkflowByIdHandler({ mastra, workflowId }) {
111
134
  outputSchema: workflow.outputSchema ? stringify(esm_default(workflow.outputSchema)) : void 0
112
135
  };
113
136
  } catch (error) {
114
- throw new HTTPException(500, { message: error?.message || "Error getting workflow" });
137
+ return handleError(error, "Error getting workflow");
115
138
  }
116
139
  }
117
140
  async function getWorkflowRunByIdHandler({
@@ -136,7 +159,7 @@ async function getWorkflowRunByIdHandler({
136
159
  }
137
160
  return run;
138
161
  } catch (error) {
139
- throw new HTTPException(500, { message: error?.message || "Error getting workflow run" });
162
+ return handleError(error, "Error getting workflow run");
140
163
  }
141
164
  }
142
165
  async function getWorkflowRunExecutionResultHandler({
@@ -161,9 +184,7 @@ async function getWorkflowRunExecutionResultHandler({
161
184
  }
162
185
  return executionResult;
163
186
  } catch (error) {
164
- throw new HTTPException(500, {
165
- message: error?.message || "Error getting workflow run execution result"
166
- });
187
+ return handleError(error, "Error getting workflow run execution result");
167
188
  }
168
189
  }
169
190
  async function createWorkflowRunHandler({
@@ -179,10 +200,10 @@ async function createWorkflowRunHandler({
179
200
  if (!workflow) {
180
201
  throw new HTTPException(404, { message: "Workflow not found" });
181
202
  }
182
- const run = workflow.createRun({ runId: prevRunId });
203
+ const run = await workflow.createRunAsync({ runId: prevRunId });
183
204
  return { runId: run.runId };
184
205
  } catch (error) {
185
- throw new HTTPException(500, { message: error?.message || "Error creating workflow run" });
206
+ return handleError(error, "Error creating workflow run");
186
207
  }
187
208
  }
188
209
  async function startAsyncWorkflowHandler({
@@ -200,14 +221,14 @@ async function startAsyncWorkflowHandler({
200
221
  if (!workflow) {
201
222
  throw new HTTPException(404, { message: "Workflow not found" });
202
223
  }
203
- const _run = workflow.createRun({ runId });
224
+ const _run = await workflow.createRunAsync({ runId });
204
225
  const result = await _run.start({
205
226
  inputData,
206
227
  runtimeContext
207
228
  });
208
229
  return result;
209
230
  } catch (error) {
210
- throw new HTTPException(500, { message: error?.message || "Error executing workflow" });
231
+ return handleError(error, "Error starting async workflow");
211
232
  }
212
233
  }
213
234
  async function startWorkflowRunHandler({
@@ -232,7 +253,7 @@ async function startWorkflowRunHandler({
232
253
  if (!run) {
233
254
  throw new HTTPException(404, { message: "Workflow run not found" });
234
255
  }
235
- const _run = workflow.createRun({ runId });
256
+ const _run = await workflow.createRunAsync({ runId });
236
257
  void _run.start({
237
258
  inputData,
238
259
  runtimeContext
@@ -262,7 +283,7 @@ async function watchWorkflowHandler({
262
283
  if (!run) {
263
284
  throw new HTTPException(404, { message: "Workflow run not found" });
264
285
  }
265
- const _run = workflow.createRun({ runId });
286
+ const _run = await workflow.createRunAsync({ runId });
266
287
  let unwatch;
267
288
  let asyncRef = null;
268
289
  const stream = new ReadableStream({
@@ -309,7 +330,7 @@ async function streamWorkflowHandler({
309
330
  if (!workflow) {
310
331
  throw new HTTPException(404, { message: "Workflow not found" });
311
332
  }
312
- const run = workflow.createRun({ runId });
333
+ const run = await workflow.createRunAsync({ runId });
313
334
  const result = run.stream({
314
335
  inputData,
315
336
  runtimeContext
@@ -344,7 +365,7 @@ async function resumeAsyncWorkflowHandler({
344
365
  if (!run) {
345
366
  throw new HTTPException(404, { message: "Workflow run not found" });
346
367
  }
347
- const _run = workflow.createRun({ runId });
368
+ const _run = await workflow.createRunAsync({ runId });
348
369
  const result = await _run.resume({
349
370
  step: body.step,
350
371
  resumeData: body.resumeData,
@@ -380,7 +401,7 @@ async function resumeWorkflowHandler({
380
401
  if (!run) {
381
402
  throw new HTTPException(404, { message: "Workflow run not found" });
382
403
  }
383
- const _run = workflow.createRun({ runId });
404
+ const _run = await workflow.createRunAsync({ runId });
384
405
  void _run.resume({
385
406
  step: body.step,
386
407
  resumeData: body.resumeData,
@@ -417,5 +438,61 @@ async function getWorkflowRunsHandler({
417
438
  return handleError(error, "Error getting workflow runs");
418
439
  }
419
440
  }
441
+ async function cancelWorkflowRunHandler({
442
+ mastra,
443
+ workflowId,
444
+ runId
445
+ }) {
446
+ try {
447
+ if (!workflowId) {
448
+ throw new HTTPException(400, { message: "Workflow ID is required" });
449
+ }
450
+ if (!runId) {
451
+ throw new HTTPException(400, { message: "runId required to cancel workflow run" });
452
+ }
453
+ const { workflow } = await getWorkflowsFromSystem({ mastra, workflowId });
454
+ if (!workflow) {
455
+ throw new HTTPException(404, { message: "Workflow not found" });
456
+ }
457
+ const run = await workflow.getWorkflowRunById(runId);
458
+ if (!run) {
459
+ throw new HTTPException(404, { message: "Workflow run not found" });
460
+ }
461
+ const _run = await workflow.createRunAsync({ runId });
462
+ await _run.cancel();
463
+ return { message: "Workflow run cancelled" };
464
+ } catch (error) {
465
+ return handleError(error, "Error canceling workflow run");
466
+ }
467
+ }
468
+ async function sendWorkflowRunEventHandler({
469
+ mastra,
470
+ workflowId,
471
+ runId,
472
+ event,
473
+ data
474
+ }) {
475
+ try {
476
+ if (!workflowId) {
477
+ throw new HTTPException(400, { message: "Workflow ID is required" });
478
+ }
479
+ if (!runId) {
480
+ throw new HTTPException(400, { message: "runId required to send workflow run event" });
481
+ }
482
+ const { workflow } = await getWorkflowsFromSystem({ mastra, workflowId });
483
+ if (!workflow) {
484
+ throw new HTTPException(404, { message: "Workflow not found" });
485
+ }
486
+ const run = await workflow.getWorkflowRunById(runId);
487
+ if (!run) {
488
+ throw new HTTPException(404, { message: "Workflow run not found" });
489
+ }
490
+ const _run = await workflow.createRunAsync({ runId });
491
+ await _run.sendEvent(event, data);
492
+ return { message: "Workflow run event sent" };
493
+ } catch (error) {
494
+ return handleError(error, "Error sending workflow run event");
495
+ }
496
+ }
420
497
 
421
- export { createWorkflowRunHandler, getWorkflowByIdHandler, getWorkflowRunByIdHandler, getWorkflowRunExecutionResultHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, streamWorkflowHandler, watchWorkflowHandler, workflows_exports };
498
+ export { cancelWorkflowRunHandler, createWorkflowRunHandler, getWorkflowByIdHandler, getWorkflowRunByIdHandler, getWorkflowRunExecutionResultHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, sendWorkflowRunEventHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, streamWorkflowHandler, watchWorkflowHandler, workflows_exports };