@mastra/server 0.0.0-a2a-20250421213654

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 (97) hide show
  1. package/LICENSE.md +46 -0
  2. package/README.md +159 -0
  3. package/dist/_tsup-dts-rollup.d.cts +897 -0
  4. package/dist/_tsup-dts-rollup.d.ts +897 -0
  5. package/dist/chunk-3KWAWPRU.cjs +127 -0
  6. package/dist/chunk-4VNS5WPM.js +37 -0
  7. package/dist/chunk-57CJTIPW.cjs +18 -0
  8. package/dist/chunk-5SWCVTNL.cjs +5579 -0
  9. package/dist/chunk-64U3UDTH.cjs +13 -0
  10. package/dist/chunk-6QEBCWQG.js +136 -0
  11. package/dist/chunk-75KYFUMD.cjs +165 -0
  12. package/dist/chunk-B2TWGIVC.cjs +101 -0
  13. package/dist/chunk-CVPOREIE.cjs +42 -0
  14. package/dist/chunk-CZOSYERC.js +312 -0
  15. package/dist/chunk-FFRES7WX.js +157 -0
  16. package/dist/chunk-H5PTF3Y4.js +16 -0
  17. package/dist/chunk-IDPBXKRV.js +121 -0
  18. package/dist/chunk-J2MCTUD4.js +121 -0
  19. package/dist/chunk-JQCFFVSK.cjs +127 -0
  20. package/dist/chunk-LGJ46RCS.js +96 -0
  21. package/dist/chunk-LTP7PHVE.cjs +321 -0
  22. package/dist/chunk-M5ABIP7D.js +11 -0
  23. package/dist/chunk-NJF275KR.js +49 -0
  24. package/dist/chunk-NYN7KFXL.js +34 -0
  25. package/dist/chunk-O7X557PN.cjs +140 -0
  26. package/dist/chunk-OCWPVYNI.cjs +36 -0
  27. package/dist/chunk-OMN3UI6X.js +5576 -0
  28. package/dist/chunk-P7UZNROR.js +306 -0
  29. package/dist/chunk-PJHJORJL.js +204 -0
  30. package/dist/chunk-RXH2S65Z.cjs +54 -0
  31. package/dist/chunk-UVVUDJBW.cjs +214 -0
  32. package/dist/chunk-Y7WN2UYV.cjs +318 -0
  33. package/dist/chunk-YB2FGKQ5.js +121 -0
  34. package/dist/chunk-ZCRMOXNW.cjs +129 -0
  35. package/dist/index.cjs +2 -0
  36. package/dist/index.d.cts +1 -0
  37. package/dist/index.d.ts +1 -0
  38. package/dist/index.js +1 -0
  39. package/dist/magic-string.es-5UDOWOAZ.js +1294 -0
  40. package/dist/magic-string.es-IL2775P6.cjs +1300 -0
  41. package/dist/server/handlers/a2a.cjs +34 -0
  42. package/dist/server/handlers/a2a.d.cts +7 -0
  43. package/dist/server/handlers/a2a.d.ts +7 -0
  44. package/dist/server/handlers/a2a.js +1 -0
  45. package/dist/server/handlers/agents.cjs +30 -0
  46. package/dist/server/handlers/agents.d.cts +6 -0
  47. package/dist/server/handlers/agents.d.ts +6 -0
  48. package/dist/server/handlers/agents.js +1 -0
  49. package/dist/server/handlers/error.cjs +10 -0
  50. package/dist/server/handlers/error.d.cts +1 -0
  51. package/dist/server/handlers/error.d.ts +1 -0
  52. package/dist/server/handlers/error.js +1 -0
  53. package/dist/server/handlers/logs.cjs +18 -0
  54. package/dist/server/handlers/logs.d.cts +3 -0
  55. package/dist/server/handlers/logs.d.ts +3 -0
  56. package/dist/server/handlers/logs.js +1 -0
  57. package/dist/server/handlers/memory.cjs +38 -0
  58. package/dist/server/handlers/memory.d.cts +8 -0
  59. package/dist/server/handlers/memory.d.ts +8 -0
  60. package/dist/server/handlers/memory.js +1 -0
  61. package/dist/server/handlers/network.cjs +22 -0
  62. package/dist/server/handlers/network.d.cts +4 -0
  63. package/dist/server/handlers/network.d.ts +4 -0
  64. package/dist/server/handlers/network.js +1 -0
  65. package/dist/server/handlers/telemetry.cjs +14 -0
  66. package/dist/server/handlers/telemetry.d.cts +2 -0
  67. package/dist/server/handlers/telemetry.d.ts +2 -0
  68. package/dist/server/handlers/telemetry.js +1 -0
  69. package/dist/server/handlers/test-utils.cjs +16209 -0
  70. package/dist/server/handlers/test-utils.d.cts +1 -0
  71. package/dist/server/handlers/test-utils.d.ts +1 -0
  72. package/dist/server/handlers/test-utils.js +16207 -0
  73. package/dist/server/handlers/tools.cjs +22 -0
  74. package/dist/server/handlers/tools.d.cts +4 -0
  75. package/dist/server/handlers/tools.d.ts +4 -0
  76. package/dist/server/handlers/tools.js +1 -0
  77. package/dist/server/handlers/utils.cjs +10 -0
  78. package/dist/server/handlers/utils.d.cts +1 -0
  79. package/dist/server/handlers/utils.d.ts +1 -0
  80. package/dist/server/handlers/utils.js +1 -0
  81. package/dist/server/handlers/vector.cjs +30 -0
  82. package/dist/server/handlers/vector.d.cts +6 -0
  83. package/dist/server/handlers/vector.d.ts +6 -0
  84. package/dist/server/handlers/vector.js +1 -0
  85. package/dist/server/handlers/voice.cjs +18 -0
  86. package/dist/server/handlers/voice.d.cts +3 -0
  87. package/dist/server/handlers/voice.d.ts +3 -0
  88. package/dist/server/handlers/voice.js +1 -0
  89. package/dist/server/handlers/workflows.cjs +46 -0
  90. package/dist/server/handlers/workflows.d.cts +10 -0
  91. package/dist/server/handlers/workflows.d.ts +10 -0
  92. package/dist/server/handlers/workflows.js +1 -0
  93. package/dist/server/handlers.cjs +55 -0
  94. package/dist/server/handlers.d.cts +10 -0
  95. package/dist/server/handlers.d.ts +10 -0
  96. package/dist/server/handlers.js +10 -0
  97. package/package.json +71 -0
@@ -0,0 +1,214 @@
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 chunkCVPOREIE_cjs = require('./chunk-CVPOREIE.cjs');
7
+
8
+ // src/server/handlers/memory.ts
9
+ var memory_exports = {};
10
+ chunkCVPOREIE_cjs.__export(memory_exports, {
11
+ createThreadHandler: () => createThreadHandler,
12
+ deleteThreadHandler: () => deleteThreadHandler,
13
+ getMemoryStatusHandler: () => getMemoryStatusHandler,
14
+ getMessagesHandler: () => getMessagesHandler,
15
+ getThreadByIdHandler: () => getThreadByIdHandler,
16
+ getThreadsHandler: () => getThreadsHandler,
17
+ saveMessagesHandler: () => saveMessagesHandler,
18
+ updateThreadHandler: () => updateThreadHandler
19
+ });
20
+ function getMemoryFromContext({
21
+ mastra,
22
+ agentId
23
+ }) {
24
+ const agent = agentId ? mastra.getAgent(agentId) : null;
25
+ if (agentId && !agent) {
26
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Agent not found" });
27
+ }
28
+ const memory = agent?.getMemory?.() || mastra.getMemory();
29
+ return memory;
30
+ }
31
+ async function getMemoryStatusHandler({ mastra, agentId }) {
32
+ try {
33
+ const memory = getMemoryFromContext({ mastra, agentId });
34
+ if (!memory) {
35
+ return { result: false };
36
+ }
37
+ return { result: true };
38
+ } catch (error) {
39
+ return chunk64U3UDTH_cjs.handleError(error, "Error getting memory status");
40
+ }
41
+ }
42
+ async function getThreadsHandler({
43
+ mastra,
44
+ agentId,
45
+ resourceId
46
+ }) {
47
+ try {
48
+ const memory = getMemoryFromContext({ mastra, agentId });
49
+ if (!memory) {
50
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
51
+ }
52
+ chunk57CJTIPW_cjs.validateBody({ resourceId });
53
+ const threads = await memory.getThreadsByResourceId({ resourceId });
54
+ return threads;
55
+ } catch (error) {
56
+ return chunk64U3UDTH_cjs.handleError(error, "Error getting threads");
57
+ }
58
+ }
59
+ async function getThreadByIdHandler({
60
+ mastra,
61
+ agentId,
62
+ threadId
63
+ }) {
64
+ try {
65
+ chunk57CJTIPW_cjs.validateBody({ threadId });
66
+ const memory = getMemoryFromContext({ mastra, agentId });
67
+ if (!memory) {
68
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
69
+ }
70
+ const thread = await memory.getThreadById({ threadId });
71
+ if (!thread) {
72
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
73
+ }
74
+ return thread;
75
+ } catch (error) {
76
+ return chunk64U3UDTH_cjs.handleError(error, "Error getting thread");
77
+ }
78
+ }
79
+ async function saveMessagesHandler({
80
+ mastra,
81
+ agentId,
82
+ body
83
+ }) {
84
+ try {
85
+ const memory = getMemoryFromContext({ mastra, agentId });
86
+ if (!memory) {
87
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
88
+ }
89
+ if (!body?.messages) {
90
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Messages are required" });
91
+ }
92
+ if (!Array.isArray(body.messages)) {
93
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Messages should be an array" });
94
+ }
95
+ const processedMessages = body.messages.map((message) => ({
96
+ ...message,
97
+ id: memory.generateId(),
98
+ createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
99
+ }));
100
+ const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
101
+ return result;
102
+ } catch (error) {
103
+ return chunk64U3UDTH_cjs.handleError(error, "Error saving messages");
104
+ }
105
+ }
106
+ async function createThreadHandler({
107
+ mastra,
108
+ agentId,
109
+ body
110
+ }) {
111
+ try {
112
+ const memory = getMemoryFromContext({ mastra, agentId });
113
+ if (!memory) {
114
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
115
+ }
116
+ chunk57CJTIPW_cjs.validateBody({ resourceId: body?.resourceId });
117
+ const result = await memory.createThread({
118
+ resourceId: body?.resourceId,
119
+ title: body?.title,
120
+ metadata: body?.metadata,
121
+ threadId: body?.threadId
122
+ });
123
+ return result;
124
+ } catch (error) {
125
+ return chunk64U3UDTH_cjs.handleError(error, "Error saving thread to memory");
126
+ }
127
+ }
128
+ async function updateThreadHandler({
129
+ mastra,
130
+ agentId,
131
+ threadId,
132
+ body
133
+ }) {
134
+ try {
135
+ const memory = getMemoryFromContext({ mastra, agentId });
136
+ if (!body) {
137
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Body is required" });
138
+ }
139
+ const { title, metadata, resourceId } = body;
140
+ const updatedAt = /* @__PURE__ */ new Date();
141
+ chunk57CJTIPW_cjs.validateBody({ threadId });
142
+ if (!memory) {
143
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
144
+ }
145
+ const thread = await memory.getThreadById({ threadId });
146
+ if (!thread) {
147
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
148
+ }
149
+ const updatedThread = {
150
+ ...thread,
151
+ title: title || thread.title,
152
+ metadata: metadata || thread.metadata,
153
+ resourceId: resourceId || thread.resourceId,
154
+ createdAt: thread.createdAt,
155
+ updatedAt
156
+ };
157
+ const result = await memory.saveThread({ thread: updatedThread });
158
+ return result;
159
+ } catch (error) {
160
+ return chunk64U3UDTH_cjs.handleError(error, "Error updating thread");
161
+ }
162
+ }
163
+ async function deleteThreadHandler({
164
+ mastra,
165
+ agentId,
166
+ threadId
167
+ }) {
168
+ try {
169
+ chunk57CJTIPW_cjs.validateBody({ threadId });
170
+ const memory = getMemoryFromContext({ mastra, agentId });
171
+ if (!memory) {
172
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
173
+ }
174
+ const thread = await memory.getThreadById({ threadId });
175
+ if (!thread) {
176
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
177
+ }
178
+ await memory.deleteThread(threadId);
179
+ return { result: "Thread deleted" };
180
+ } catch (error) {
181
+ return chunk64U3UDTH_cjs.handleError(error, "Error deleting thread");
182
+ }
183
+ }
184
+ async function getMessagesHandler({
185
+ mastra,
186
+ agentId,
187
+ threadId
188
+ }) {
189
+ try {
190
+ chunk57CJTIPW_cjs.validateBody({ threadId });
191
+ const memory = getMemoryFromContext({ mastra, agentId });
192
+ if (!memory) {
193
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
194
+ }
195
+ const thread = await memory.getThreadById({ threadId });
196
+ if (!thread) {
197
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
198
+ }
199
+ const result = await memory.query({ threadId });
200
+ return result;
201
+ } catch (error) {
202
+ return chunk64U3UDTH_cjs.handleError(error, "Error getting messages");
203
+ }
204
+ }
205
+
206
+ exports.createThreadHandler = createThreadHandler;
207
+ exports.deleteThreadHandler = deleteThreadHandler;
208
+ exports.getMemoryStatusHandler = getMemoryStatusHandler;
209
+ exports.getMessagesHandler = getMessagesHandler;
210
+ exports.getThreadByIdHandler = getThreadByIdHandler;
211
+ exports.getThreadsHandler = getThreadsHandler;
212
+ exports.memory_exports = memory_exports;
213
+ exports.saveMessagesHandler = saveMessagesHandler;
214
+ exports.updateThreadHandler = updateThreadHandler;
@@ -0,0 +1,318 @@
1
+ 'use strict';
2
+
3
+ var chunk5SWCVTNL_cjs = require('./chunk-5SWCVTNL.cjs');
4
+ var chunk64U3UDTH_cjs = require('./chunk-64U3UDTH.cjs');
5
+ var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
6
+ var chunkCVPOREIE_cjs = require('./chunk-CVPOREIE.cjs');
7
+ var web = require('stream/web');
8
+
9
+ // src/server/handlers/workflows.ts
10
+ var workflows_exports = {};
11
+ chunkCVPOREIE_cjs.__export(workflows_exports, {
12
+ createRunHandler: () => createRunHandler,
13
+ getWorkflowByIdHandler: () => getWorkflowByIdHandler,
14
+ getWorkflowRunHandler: () => getWorkflowRunHandler,
15
+ getWorkflowRunsHandler: () => getWorkflowRunsHandler,
16
+ getWorkflowsHandler: () => getWorkflowsHandler,
17
+ resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
18
+ resumeWorkflowHandler: () => resumeWorkflowHandler,
19
+ startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
20
+ startWorkflowRunHandler: () => startWorkflowRunHandler,
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
+ if (workflow.isNested) return acc;
28
+ acc[key] = {
29
+ stepGraph: workflow.stepGraph,
30
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
31
+ serializedStepGraph: workflow.serializedStepGraph,
32
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
33
+ name: workflow.name,
34
+ triggerSchema: workflow.triggerSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(workflow.triggerSchema)) : void 0,
35
+ steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
36
+ const _step = step;
37
+ acc2[key2] = {
38
+ ..._step,
39
+ inputSchema: _step.inputSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(_step.inputSchema)) : void 0,
40
+ outputSchema: _step.outputSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(_step.outputSchema)) : void 0
41
+ };
42
+ return acc2;
43
+ }, {})
44
+ };
45
+ return acc;
46
+ }, {});
47
+ return _workflows;
48
+ } catch (error) {
49
+ throw new chunkOCWPVYNI_cjs.HTTPException(500, { message: error?.message || "Error getting workflows" });
50
+ }
51
+ }
52
+ async function getWorkflowByIdHandler({ mastra, workflowId }) {
53
+ try {
54
+ if (!workflowId) {
55
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
56
+ }
57
+ const workflow = mastra.getWorkflow(workflowId);
58
+ if (!workflow) {
59
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow not found" });
60
+ }
61
+ return {
62
+ stepGraph: workflow.stepGraph,
63
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
64
+ serializedStepGraph: workflow.serializedStepGraph,
65
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
66
+ name: workflow.name,
67
+ triggerSchema: workflow.triggerSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(workflow.triggerSchema)) : void 0,
68
+ steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
69
+ const _step = step;
70
+ acc[key] = {
71
+ ..._step,
72
+ inputSchema: _step.inputSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(_step.inputSchema)) : void 0,
73
+ outputSchema: _step.outputSchema ? chunk5SWCVTNL_cjs.stringify(chunk5SWCVTNL_cjs.esm_default(_step.outputSchema)) : void 0
74
+ };
75
+ return acc;
76
+ }, {})
77
+ };
78
+ } catch (error) {
79
+ throw new chunkOCWPVYNI_cjs.HTTPException(500, { message: error?.message || "Error getting workflow" });
80
+ }
81
+ }
82
+ async function startAsyncWorkflowHandler({
83
+ mastra,
84
+ container,
85
+ workflowId,
86
+ runId,
87
+ triggerData
88
+ }) {
89
+ try {
90
+ if (!workflowId) {
91
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
92
+ }
93
+ const workflow = mastra.getWorkflow(workflowId);
94
+ if (!workflow) {
95
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow not found" });
96
+ }
97
+ if (!runId) {
98
+ const { start } = workflow.createRun();
99
+ const result2 = await start({
100
+ triggerData,
101
+ container
102
+ });
103
+ return result2;
104
+ }
105
+ const run = workflow.getRun(runId);
106
+ if (!run) {
107
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
108
+ }
109
+ const result = await run.start({
110
+ triggerData,
111
+ container
112
+ });
113
+ return result;
114
+ } catch (error) {
115
+ throw new chunkOCWPVYNI_cjs.HTTPException(500, { message: error?.message || "Error executing workflow" });
116
+ }
117
+ }
118
+ async function getWorkflowRunHandler({
119
+ mastra,
120
+ workflowId,
121
+ runId
122
+ }) {
123
+ try {
124
+ if (!workflowId) {
125
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
126
+ }
127
+ if (!runId) {
128
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Run ID is required" });
129
+ }
130
+ const workflow = mastra.getWorkflow(workflowId);
131
+ if (!workflow) {
132
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow not found" });
133
+ }
134
+ const run = workflow.getRun(runId);
135
+ if (!run) {
136
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
137
+ }
138
+ return run;
139
+ } catch (error) {
140
+ throw new chunkOCWPVYNI_cjs.HTTPException(500, { message: error?.message || "Error getting workflow run" });
141
+ }
142
+ }
143
+ async function createRunHandler({
144
+ mastra,
145
+ workflowId,
146
+ runId: prevRunId
147
+ }) {
148
+ try {
149
+ if (!workflowId) {
150
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
151
+ }
152
+ const workflow = mastra.getWorkflow(workflowId);
153
+ if (!workflow) {
154
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow not found" });
155
+ }
156
+ const { runId } = workflow.createRun({ runId: prevRunId });
157
+ return { runId };
158
+ } catch (error) {
159
+ throw new chunkOCWPVYNI_cjs.HTTPException(500, { message: error?.message || "Error creating workflow run" });
160
+ }
161
+ }
162
+ async function startWorkflowRunHandler({
163
+ mastra,
164
+ container,
165
+ workflowId,
166
+ runId,
167
+ triggerData
168
+ }) {
169
+ try {
170
+ if (!workflowId) {
171
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
172
+ }
173
+ if (!runId) {
174
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "runId required to start run" });
175
+ }
176
+ const workflow = mastra.getWorkflow(workflowId);
177
+ const run = workflow.getRun(runId);
178
+ if (!run) {
179
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
180
+ }
181
+ await run.start({
182
+ triggerData,
183
+ container
184
+ });
185
+ return { message: "Workflow run started" };
186
+ } catch (e) {
187
+ return chunk64U3UDTH_cjs.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 chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
198
+ }
199
+ if (!runId) {
200
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "runId required to watch workflow" });
201
+ }
202
+ const workflow = mastra.getWorkflow(workflowId);
203
+ const run = workflow.getRun(runId);
204
+ if (!run) {
205
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
206
+ }
207
+ let unwatch;
208
+ let asyncRef = null;
209
+ const stream = new web.ReadableStream({
210
+ start(controller) {
211
+ unwatch = run.watch(({ activePaths, runId: runId2, timestamp, results }) => {
212
+ const activePathsObj = Object.fromEntries(activePaths);
213
+ controller.enqueue(JSON.stringify({ activePaths: activePathsObj, runId: runId2, timestamp, results }));
214
+ if (asyncRef) {
215
+ clearImmediate(asyncRef);
216
+ asyncRef = null;
217
+ }
218
+ asyncRef = setImmediate(() => {
219
+ if (!workflow.getRun(runId2)) {
220
+ controller.close();
221
+ }
222
+ });
223
+ });
224
+ },
225
+ cancel() {
226
+ unwatch?.();
227
+ }
228
+ });
229
+ return stream;
230
+ } catch (error) {
231
+ return chunk64U3UDTH_cjs.handleError(error, "Error watching workflow");
232
+ }
233
+ }
234
+ async function resumeAsyncWorkflowHandler({
235
+ mastra,
236
+ workflowId,
237
+ runId,
238
+ body,
239
+ container
240
+ }) {
241
+ try {
242
+ if (!workflowId) {
243
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
244
+ }
245
+ if (!runId) {
246
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "runId required to resume workflow" });
247
+ }
248
+ const workflow = mastra.getWorkflow(workflowId);
249
+ const run = workflow.getRun(runId);
250
+ if (!run) {
251
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
252
+ }
253
+ const result = await run.resume({
254
+ stepId: body.stepId,
255
+ context: body.context,
256
+ container
257
+ });
258
+ return result;
259
+ } catch (error) {
260
+ return chunk64U3UDTH_cjs.handleError(error, "Error resuming workflow step");
261
+ }
262
+ }
263
+ async function resumeWorkflowHandler({
264
+ mastra,
265
+ workflowId,
266
+ runId,
267
+ body,
268
+ container
269
+ }) {
270
+ try {
271
+ if (!workflowId) {
272
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
273
+ }
274
+ if (!runId) {
275
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "runId required to resume workflow" });
276
+ }
277
+ const workflow = mastra.getWorkflow(workflowId);
278
+ const run = workflow.getRun(runId);
279
+ if (!run) {
280
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Workflow run not found" });
281
+ }
282
+ await run.resume({
283
+ stepId: body.stepId,
284
+ context: body.context,
285
+ container
286
+ });
287
+ return { message: "Workflow run resumed" };
288
+ } catch (error) {
289
+ return chunk64U3UDTH_cjs.handleError(error, "Error resuming workflow");
290
+ }
291
+ }
292
+ async function getWorkflowRunsHandler({ mastra, workflowId }) {
293
+ try {
294
+ if (!workflowId) {
295
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Workflow ID is required" });
296
+ }
297
+ const workflow = mastra.getWorkflow(workflowId);
298
+ const workflowRuns = await workflow.getWorkflowRuns() || {
299
+ runs: [],
300
+ total: 0
301
+ };
302
+ return workflowRuns;
303
+ } catch (error) {
304
+ return chunk64U3UDTH_cjs.handleError(error, "Error getting workflow runs");
305
+ }
306
+ }
307
+
308
+ exports.createRunHandler = createRunHandler;
309
+ exports.getWorkflowByIdHandler = getWorkflowByIdHandler;
310
+ exports.getWorkflowRunHandler = getWorkflowRunHandler;
311
+ exports.getWorkflowRunsHandler = getWorkflowRunsHandler;
312
+ exports.getWorkflowsHandler = getWorkflowsHandler;
313
+ exports.resumeAsyncWorkflowHandler = resumeAsyncWorkflowHandler;
314
+ exports.resumeWorkflowHandler = resumeWorkflowHandler;
315
+ exports.startAsyncWorkflowHandler = startAsyncWorkflowHandler;
316
+ exports.startWorkflowRunHandler = startWorkflowRunHandler;
317
+ exports.watchWorkflowHandler = watchWorkflowHandler;
318
+ exports.workflows_exports = workflows_exports;
@@ -0,0 +1,121 @@
1
+ import { stringify, esm_default } from './chunk-OMN3UI6X.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-4VNS5WPM.js';
6
+ import { isVercelTool } from '@mastra/core/tools';
7
+
8
+ // src/server/handlers/tools.ts
9
+ var tools_exports = {};
10
+ __export(tools_exports, {
11
+ executeAgentToolHandler: () => executeAgentToolHandler,
12
+ executeToolHandler: () => executeToolHandler,
13
+ getToolByIdHandler: () => getToolByIdHandler,
14
+ getToolsHandler: () => getToolsHandler
15
+ });
16
+ async function getToolsHandler({ tools }) {
17
+ try {
18
+ if (!tools) {
19
+ return {};
20
+ }
21
+ const serializedTools = Object.entries(tools).reduce(
22
+ (acc, [id, _tool]) => {
23
+ const tool = _tool;
24
+ acc[id] = {
25
+ ...tool,
26
+ inputSchema: tool.inputSchema ? stringify(esm_default(tool.inputSchema)) : void 0,
27
+ outputSchema: tool.outputSchema ? stringify(esm_default(tool.outputSchema)) : void 0
28
+ };
29
+ return acc;
30
+ },
31
+ {}
32
+ );
33
+ return serializedTools;
34
+ } catch (error) {
35
+ return handleError(error, "Error getting tools");
36
+ }
37
+ }
38
+ async function getToolByIdHandler({ tools, toolId }) {
39
+ try {
40
+ const tool = Object.values(tools || {}).find((tool2) => tool2.id === toolId);
41
+ if (!tool) {
42
+ throw new HTTPException(404, { message: "Tool not found" });
43
+ }
44
+ const serializedTool = {
45
+ ...tool,
46
+ inputSchema: tool.inputSchema ? stringify(esm_default(tool.inputSchema)) : void 0,
47
+ outputSchema: tool.outputSchema ? stringify(esm_default(tool.outputSchema)) : void 0
48
+ };
49
+ return serializedTool;
50
+ } catch (error) {
51
+ return handleError(error, "Error getting tool");
52
+ }
53
+ }
54
+ function executeToolHandler(tools) {
55
+ return async ({
56
+ mastra,
57
+ runId,
58
+ toolId,
59
+ data,
60
+ container
61
+ }) => {
62
+ try {
63
+ if (!toolId) {
64
+ throw new HTTPException(400, { message: "Tool ID is required" });
65
+ }
66
+ const tool = Object.values(tools || {}).find((tool2) => tool2.id === toolId);
67
+ if (!tool) {
68
+ throw new HTTPException(404, { message: "Tool not found" });
69
+ }
70
+ if (!tool?.execute) {
71
+ throw new HTTPException(400, { message: "Tool is not executable" });
72
+ }
73
+ validateBody({ data });
74
+ if (isVercelTool(tool)) {
75
+ const result2 = await tool.execute(data);
76
+ return result2;
77
+ }
78
+ const result = await tool.execute({
79
+ context: data,
80
+ mastra,
81
+ runId,
82
+ container
83
+ });
84
+ return result;
85
+ } catch (error) {
86
+ return handleError(error, "Error executing tool");
87
+ }
88
+ };
89
+ }
90
+ async function executeAgentToolHandler({
91
+ mastra,
92
+ agentId,
93
+ toolId,
94
+ data,
95
+ container
96
+ }) {
97
+ try {
98
+ const agent = agentId ? mastra.getAgent(agentId) : null;
99
+ if (!agent) {
100
+ throw new HTTPException(404, { message: "Tool not found" });
101
+ }
102
+ const tool = Object.values(agent?.tools || {}).find((tool2) => tool2.id === toolId);
103
+ if (!tool) {
104
+ throw new HTTPException(404, { message: "Tool not found" });
105
+ }
106
+ if (!tool?.execute) {
107
+ throw new HTTPException(400, { message: "Tool is not executable" });
108
+ }
109
+ const result = await tool.execute({
110
+ context: data,
111
+ container,
112
+ mastra,
113
+ runId: agentId
114
+ });
115
+ return result;
116
+ } catch (error) {
117
+ return handleError(error, "Error executing tool");
118
+ }
119
+ }
120
+
121
+ export { executeAgentToolHandler, executeToolHandler, getToolByIdHandler, getToolsHandler, tools_exports };