@mastra/server 0.0.1-alpha.5 → 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.
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 +305 -52
  4. package/dist/_tsup-dts-rollup.d.ts +305 -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-6B4OMWT6.cjs +343 -0
  10. package/dist/chunk-75ZPJI57.cjs +9 -0
  11. package/dist/{chunk-VB7KH62D.cjs → chunk-7DLZLWJV.cjs} +16 -12
  12. package/dist/{chunk-M56ECCHK.cjs → chunk-BNEY4P4P.cjs} +20 -19
  13. package/dist/{chunk-2YONKUWB.js → chunk-C7564HUT.js} +58 -33
  14. package/dist/chunk-D4IRYCUI.cjs +235 -0
  15. package/dist/{chunk-RBQASTUP.js → chunk-DJJIUEL2.js} +13 -5
  16. package/dist/{chunk-L7XE5QTW.js → chunk-H5PTF3Y4.js} +1 -1
  17. package/dist/{chunk-3RVHWGWO.js → chunk-HFWCEP5S.js} +6 -12
  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-TRDNDNGQ.js → chunk-NYN7KFXL.js} +1 -7
  23. package/dist/{chunk-FV45V6WC.cjs → chunk-OCWPVYNI.cjs} +0 -7
  24. package/dist/{chunk-DVPP5S6I.js → chunk-OR3CIE2H.js} +9 -6
  25. package/dist/chunk-P6SCPDYW.js +500 -0
  26. package/dist/chunk-QY36IPTP.js +331 -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 +13 -13
  66. package/dist/server/handlers/workflows.d.cts +2 -2
  67. package/dist/server/handlers/workflows.d.ts +2 -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
@@ -1,25 +1,26 @@
1
- import { stringify, esm_default } from './chunk-VK6FX47H.js';
2
- import { handleError } from './chunk-3AHQ5RGN.js';
3
- import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
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';
4
5
  import { ReadableStream } from 'node:stream/web';
5
6
 
6
- // src/server/handlers/workflows.ts
7
- var workflows_exports = {};
8
- __export(workflows_exports, {
9
- createRunHandler: () => createRunHandler,
10
- getWorkflowByIdHandler: () => getWorkflowByIdHandler,
11
- getWorkflowRunHandler: () => getWorkflowRunHandler,
12
- getWorkflowRunsHandler: () => getWorkflowRunsHandler,
13
- getWorkflowsHandler: () => getWorkflowsHandler,
14
- resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
15
- resumeWorkflowHandler: () => resumeWorkflowHandler,
16
- startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
17
- startWorkflowRunHandler: () => startWorkflowRunHandler,
18
- watchWorkflowHandler: () => watchWorkflowHandler
7
+ // src/server/handlers/legacyWorkflows.ts
8
+ var legacyWorkflows_exports = {};
9
+ __export(legacyWorkflows_exports, {
10
+ createLegacyWorkflowRunHandler: () => createLegacyWorkflowRunHandler,
11
+ getLegacyWorkflowByIdHandler: () => getLegacyWorkflowByIdHandler,
12
+ getLegacyWorkflowRunHandler: () => getLegacyWorkflowRunHandler,
13
+ getLegacyWorkflowRunsHandler: () => getLegacyWorkflowRunsHandler,
14
+ getLegacyWorkflowsHandler: () => getLegacyWorkflowsHandler,
15
+ resumeAsyncLegacyWorkflowHandler: () => resumeAsyncLegacyWorkflowHandler,
16
+ resumeLegacyWorkflowHandler: () => resumeLegacyWorkflowHandler,
17
+ startAsyncLegacyWorkflowHandler: () => startAsyncLegacyWorkflowHandler,
18
+ startLegacyWorkflowRunHandler: () => startLegacyWorkflowRunHandler,
19
+ watchLegacyWorkflowHandler: () => watchLegacyWorkflowHandler
19
20
  });
20
- async function getWorkflowsHandler({ mastra }) {
21
+ async function getLegacyWorkflowsHandler({ mastra }) {
21
22
  try {
22
- const workflows = mastra.getWorkflows({ serialized: false });
23
+ const workflows = mastra.legacy_getWorkflows({ serialized: false });
23
24
  const _workflows = Object.entries(workflows).reduce((acc, [key, workflow]) => {
24
25
  if (workflow.isNested) return acc;
25
26
  acc[key] = {
@@ -32,7 +33,9 @@ async function getWorkflowsHandler({ mastra }) {
32
33
  steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
33
34
  const _step = step;
34
35
  acc2[key2] = {
35
- ..._step,
36
+ id: _step.id,
37
+ description: _step.description,
38
+ workflowId: _step.workflowId,
36
39
  inputSchema: _step.inputSchema ? stringify(esm_default(_step.inputSchema)) : void 0,
37
40
  outputSchema: _step.outputSchema ? stringify(esm_default(_step.outputSchema)) : void 0
38
41
  };
@@ -46,12 +49,12 @@ async function getWorkflowsHandler({ mastra }) {
46
49
  throw new HTTPException(500, { message: error?.message || "Error getting workflows" });
47
50
  }
48
51
  }
49
- async function getWorkflowByIdHandler({ mastra, workflowId }) {
52
+ async function getLegacyWorkflowByIdHandler({ mastra, workflowId }) {
50
53
  try {
51
54
  if (!workflowId) {
52
55
  throw new HTTPException(400, { message: "Workflow ID is required" });
53
56
  }
54
- const workflow = mastra.getWorkflow(workflowId);
57
+ const workflow = mastra.legacy_getWorkflow(workflowId);
55
58
  if (!workflow) {
56
59
  throw new HTTPException(404, { message: "Workflow not found" });
57
60
  }
@@ -65,7 +68,9 @@ async function getWorkflowByIdHandler({ mastra, workflowId }) {
65
68
  steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
66
69
  const _step = step;
67
70
  acc[key] = {
68
- ..._step,
71
+ id: _step.id,
72
+ description: _step.description,
73
+ workflowId: _step.workflowId,
69
74
  inputSchema: _step.inputSchema ? stringify(esm_default(_step.inputSchema)) : void 0,
70
75
  outputSchema: _step.outputSchema ? stringify(esm_default(_step.outputSchema)) : void 0
71
76
  };
@@ -76,8 +81,9 @@ async function getWorkflowByIdHandler({ mastra, workflowId }) {
76
81
  throw new HTTPException(500, { message: error?.message || "Error getting workflow" });
77
82
  }
78
83
  }
79
- async function startAsyncWorkflowHandler({
84
+ async function startAsyncLegacyWorkflowHandler({
80
85
  mastra,
86
+ runtimeContext,
81
87
  workflowId,
82
88
  runId,
83
89
  triggerData
@@ -86,30 +92,32 @@ async function startAsyncWorkflowHandler({
86
92
  if (!workflowId) {
87
93
  throw new HTTPException(400, { message: "Workflow ID is required" });
88
94
  }
89
- const workflow = mastra.getWorkflow(workflowId);
95
+ const workflow = mastra.legacy_getWorkflow(workflowId);
90
96
  if (!workflow) {
91
97
  throw new HTTPException(404, { message: "Workflow not found" });
92
98
  }
93
99
  if (!runId) {
94
- const { start } = workflow.createRun();
95
- const result2 = await start({
96
- triggerData
100
+ const newRun = workflow.createRun();
101
+ const result2 = await newRun.start({
102
+ triggerData,
103
+ runtimeContext
97
104
  });
98
105
  return result2;
99
106
  }
100
- const run = workflow.getRun(runId);
107
+ const run = workflow.getMemoryRun(runId);
101
108
  if (!run) {
102
109
  throw new HTTPException(404, { message: "Workflow run not found" });
103
110
  }
104
111
  const result = await run.start({
105
- triggerData
112
+ triggerData,
113
+ runtimeContext
106
114
  });
107
115
  return result;
108
116
  } catch (error) {
109
117
  throw new HTTPException(500, { message: error?.message || "Error executing workflow" });
110
118
  }
111
119
  }
112
- async function getWorkflowRunHandler({
120
+ async function getLegacyWorkflowRunHandler({
113
121
  mastra,
114
122
  workflowId,
115
123
  runId
@@ -121,11 +129,11 @@ async function getWorkflowRunHandler({
121
129
  if (!runId) {
122
130
  throw new HTTPException(400, { message: "Run ID is required" });
123
131
  }
124
- const workflow = mastra.getWorkflow(workflowId);
132
+ const workflow = mastra.legacy_getWorkflow(workflowId);
125
133
  if (!workflow) {
126
134
  throw new HTTPException(404, { message: "Workflow not found" });
127
135
  }
128
- const run = workflow.getRun(runId);
136
+ const run = await workflow.getRun(runId);
129
137
  if (!run) {
130
138
  throw new HTTPException(404, { message: "Workflow run not found" });
131
139
  }
@@ -134,7 +142,7 @@ async function getWorkflowRunHandler({
134
142
  throw new HTTPException(500, { message: error?.message || "Error getting workflow run" });
135
143
  }
136
144
  }
137
- async function createRunHandler({
145
+ async function createLegacyWorkflowRunHandler({
138
146
  mastra,
139
147
  workflowId,
140
148
  runId: prevRunId
@@ -143,18 +151,19 @@ async function createRunHandler({
143
151
  if (!workflowId) {
144
152
  throw new HTTPException(400, { message: "Workflow ID is required" });
145
153
  }
146
- const workflow = mastra.getWorkflow(workflowId);
154
+ const workflow = mastra.legacy_getWorkflow(workflowId);
147
155
  if (!workflow) {
148
156
  throw new HTTPException(404, { message: "Workflow not found" });
149
157
  }
150
- const { runId } = workflow.createRun({ runId: prevRunId });
151
- return { runId };
158
+ const newRun = workflow.createRun({ runId: prevRunId });
159
+ return { runId: newRun.runId };
152
160
  } catch (error) {
153
161
  throw new HTTPException(500, { message: error?.message || "Error creating workflow run" });
154
162
  }
155
163
  }
156
- async function startWorkflowRunHandler({
164
+ async function startLegacyWorkflowRunHandler({
157
165
  mastra,
166
+ runtimeContext,
158
167
  workflowId,
159
168
  runId,
160
169
  triggerData
@@ -166,20 +175,21 @@ async function startWorkflowRunHandler({
166
175
  if (!runId) {
167
176
  throw new HTTPException(400, { message: "runId required to start run" });
168
177
  }
169
- const workflow = mastra.getWorkflow(workflowId);
170
- const run = workflow.getRun(runId);
178
+ const workflow = mastra.legacy_getWorkflow(workflowId);
179
+ const run = workflow.getMemoryRun(runId);
171
180
  if (!run) {
172
181
  throw new HTTPException(404, { message: "Workflow run not found" });
173
182
  }
174
- await run.start({
175
- triggerData
183
+ void run.start({
184
+ triggerData,
185
+ runtimeContext
176
186
  });
177
187
  return { message: "Workflow run started" };
178
188
  } catch (e) {
179
189
  return handleError(e, "Error starting workflow run");
180
190
  }
181
191
  }
182
- async function watchWorkflowHandler({
192
+ async function watchLegacyWorkflowHandler({
183
193
  mastra,
184
194
  workflowId,
185
195
  runId
@@ -191,8 +201,8 @@ async function watchWorkflowHandler({
191
201
  if (!runId) {
192
202
  throw new HTTPException(400, { message: "runId required to watch workflow" });
193
203
  }
194
- const workflow = mastra.getWorkflow(workflowId);
195
- const run = workflow.getRun(runId);
204
+ const workflow = mastra.legacy_getWorkflow(workflowId);
205
+ const run = workflow.getMemoryRun(runId);
196
206
  if (!run) {
197
207
  throw new HTTPException(404, { message: "Workflow run not found" });
198
208
  }
@@ -208,8 +218,10 @@ async function watchWorkflowHandler({
208
218
  asyncRef = null;
209
219
  }
210
220
  asyncRef = setImmediate(() => {
211
- if (!workflow.getRun(runId2)) {
221
+ const runDone = Object.values(activePathsObj).every((value) => value.status !== "executing");
222
+ if (runDone) {
212
223
  controller.close();
224
+ unwatch?.();
213
225
  }
214
226
  });
215
227
  });
@@ -223,11 +235,12 @@ async function watchWorkflowHandler({
223
235
  return handleError(error, "Error watching workflow");
224
236
  }
225
237
  }
226
- async function resumeAsyncWorkflowHandler({
238
+ async function resumeAsyncLegacyWorkflowHandler({
227
239
  mastra,
228
240
  workflowId,
229
241
  runId,
230
- body
242
+ body,
243
+ runtimeContext
231
244
  }) {
232
245
  try {
233
246
  if (!workflowId) {
@@ -236,25 +249,27 @@ async function resumeAsyncWorkflowHandler({
236
249
  if (!runId) {
237
250
  throw new HTTPException(400, { message: "runId required to resume workflow" });
238
251
  }
239
- const workflow = mastra.getWorkflow(workflowId);
240
- const run = workflow.getRun(runId);
252
+ const workflow = mastra.legacy_getWorkflow(workflowId);
253
+ const run = workflow.getMemoryRun(runId);
241
254
  if (!run) {
242
255
  throw new HTTPException(404, { message: "Workflow run not found" });
243
256
  }
244
257
  const result = await run.resume({
245
258
  stepId: body.stepId,
246
- context: body.context
259
+ context: body.context,
260
+ runtimeContext
247
261
  });
248
262
  return result;
249
263
  } catch (error) {
250
264
  return handleError(error, "Error resuming workflow step");
251
265
  }
252
266
  }
253
- async function resumeWorkflowHandler({
267
+ async function resumeLegacyWorkflowHandler({
254
268
  mastra,
255
269
  workflowId,
256
270
  runId,
257
- body
271
+ body,
272
+ runtimeContext
258
273
  }) {
259
274
  try {
260
275
  if (!workflowId) {
@@ -263,27 +278,36 @@ async function resumeWorkflowHandler({
263
278
  if (!runId) {
264
279
  throw new HTTPException(400, { message: "runId required to resume workflow" });
265
280
  }
266
- const workflow = mastra.getWorkflow(workflowId);
267
- const run = workflow.getRun(runId);
281
+ const workflow = mastra.legacy_getWorkflow(workflowId);
282
+ const run = workflow.getMemoryRun(runId);
268
283
  if (!run) {
269
284
  throw new HTTPException(404, { message: "Workflow run not found" });
270
285
  }
271
- await run.resume({
286
+ void run.resume({
272
287
  stepId: body.stepId,
273
- context: body.context
288
+ context: body.context,
289
+ runtimeContext
274
290
  });
275
291
  return { message: "Workflow run resumed" };
276
292
  } catch (error) {
277
293
  return handleError(error, "Error resuming workflow");
278
294
  }
279
295
  }
280
- async function getWorkflowRunsHandler({ mastra, workflowId }) {
296
+ async function getLegacyWorkflowRunsHandler({
297
+ mastra,
298
+ workflowId,
299
+ fromDate,
300
+ toDate,
301
+ limit,
302
+ offset,
303
+ resourceId
304
+ }) {
281
305
  try {
282
306
  if (!workflowId) {
283
307
  throw new HTTPException(400, { message: "Workflow ID is required" });
284
308
  }
285
- const workflow = mastra.getWorkflow(workflowId);
286
- const workflowRuns = await workflow.getWorkflowRuns() || {
309
+ const workflow = mastra.legacy_getWorkflow(workflowId);
310
+ const workflowRuns = await workflow.getWorkflowRuns({ fromDate, toDate, limit, offset, resourceId }) || {
287
311
  runs: [],
288
312
  total: 0
289
313
  };
@@ -293,4 +317,4 @@ async function getWorkflowRunsHandler({ mastra, workflowId }) {
293
317
  }
294
318
  }
295
319
 
296
- export { createRunHandler, getWorkflowByIdHandler, getWorkflowRunHandler, getWorkflowRunsHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, watchWorkflowHandler, workflows_exports };
320
+ export { createLegacyWorkflowRunHandler, getLegacyWorkflowByIdHandler, getLegacyWorkflowRunHandler, getLegacyWorkflowRunsHandler, getLegacyWorkflowsHandler, legacyWorkflows_exports, resumeAsyncLegacyWorkflowHandler, resumeLegacyWorkflowHandler, startAsyncLegacyWorkflowHandler, startLegacyWorkflowRunHandler, watchLegacyWorkflowHandler };
@@ -1,6 +1,6 @@
1
- import { validateBody } from './chunk-L7XE5QTW.js';
2
- import { handleError } from './chunk-3AHQ5RGN.js';
3
- import { __export } from './chunk-TRDNDNGQ.js';
1
+ import { validateBody } from './chunk-H5PTF3Y4.js';
2
+ import { handleError } from './chunk-M5ABIP7D.js';
3
+ import { __export } from './chunk-MLKGABMK.js';
4
4
 
5
5
  // src/server/handlers/logs.ts
6
6
  var logs_exports = {};
@@ -9,7 +9,10 @@ __export(logs_exports, {
9
9
  getLogsByRunIdHandler: () => getLogsByRunIdHandler,
10
10
  getLogsHandler: () => getLogsHandler
11
11
  });
12
- async function getLogsHandler({ mastra, transportId }) {
12
+ async function getLogsHandler({
13
+ mastra,
14
+ transportId
15
+ }) {
13
16
  try {
14
17
  validateBody({ transportId });
15
18
  const logs = await mastra.getLogs(transportId);
@@ -34,9 +37,10 @@ async function getLogsByRunIdHandler({
34
37
  async function getLogTransports({ mastra }) {
35
38
  try {
36
39
  const logger = mastra.getLogger();
37
- const transports = logger.transports;
40
+ console.log(logger);
41
+ const transports = logger.getTransports();
38
42
  return {
39
- transports: transports ? Object.keys(transports) : []
43
+ transports: transports ? [...transports.keys()] : []
40
44
  };
41
45
  } catch (error) {
42
46
  return handleError(error, "Error getting log Transports");
@@ -0,0 +1,227 @@
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/runtime-context';
7
+
8
+ // src/server/handlers/agents.ts
9
+ var agents_exports = {};
10
+ __export(agents_exports, {
11
+ generateHandler: () => generateHandler,
12
+ getAgentByIdHandler: () => getAgentByIdHandler,
13
+ getAgentsHandler: () => getAgentsHandler,
14
+ getEvalsByAgentIdHandler: () => getEvalsByAgentIdHandler,
15
+ getLiveEvalsByAgentIdHandler: () => getLiveEvalsByAgentIdHandler,
16
+ streamGenerateHandler: () => streamGenerateHandler
17
+ });
18
+ async function getAgentsHandler({ mastra, runtimeContext }) {
19
+ try {
20
+ const agents = mastra.getAgents();
21
+ const serializedAgentsMap = await Promise.all(
22
+ Object.entries(agents).map(async ([id, agent]) => {
23
+ const instructions = await agent.getInstructions({ runtimeContext });
24
+ const tools = await agent.getTools({ runtimeContext });
25
+ const llm = await agent.getLLM({ runtimeContext });
26
+ const serializedAgentTools = Object.entries(tools || {}).reduce((acc, [key, tool]) => {
27
+ const _tool = tool;
28
+ acc[key] = {
29
+ ..._tool,
30
+ inputSchema: _tool.inputSchema ? stringify(esm_default(_tool.inputSchema)) : void 0,
31
+ outputSchema: _tool.outputSchema ? stringify(esm_default(_tool.outputSchema)) : void 0
32
+ };
33
+ return acc;
34
+ }, {});
35
+ let serializedAgentWorkflows = {};
36
+ if ("getWorkflows" in agent) {
37
+ const logger = mastra.getLogger();
38
+ try {
39
+ const workflows = await agent.getWorkflows({ runtimeContext });
40
+ serializedAgentWorkflows = Object.entries(workflows || {}).reduce((acc, [key, workflow]) => {
41
+ return {
42
+ ...acc,
43
+ [key]: {
44
+ name: workflow.name
45
+ }
46
+ };
47
+ }, {});
48
+ } catch (error) {
49
+ logger.error("Error getting workflows for agent", { agentName: agent.name, error });
50
+ }
51
+ }
52
+ return {
53
+ id,
54
+ name: agent.name,
55
+ instructions,
56
+ tools: serializedAgentTools,
57
+ workflows: serializedAgentWorkflows,
58
+ provider: llm?.getProvider(),
59
+ modelId: llm?.getModelId()
60
+ };
61
+ })
62
+ );
63
+ const serializedAgents = serializedAgentsMap.reduce((acc, { id, ...rest }) => {
64
+ acc[id] = rest;
65
+ return acc;
66
+ }, {});
67
+ return serializedAgents;
68
+ } catch (error) {
69
+ return handleError(error, "Error getting agents");
70
+ }
71
+ }
72
+ async function getAgentByIdHandler({
73
+ mastra,
74
+ runtimeContext,
75
+ agentId
76
+ }) {
77
+ try {
78
+ const agent = mastra.getAgent(agentId);
79
+ if (!agent) {
80
+ throw new HTTPException(404, { message: "Agent not found" });
81
+ }
82
+ const tools = await agent.getTools({ runtimeContext });
83
+ const serializedAgentTools = Object.entries(tools || {}).reduce((acc, [key, tool]) => {
84
+ const _tool = tool;
85
+ acc[key] = {
86
+ ..._tool,
87
+ inputSchema: _tool.inputSchema ? stringify(esm_default(_tool.inputSchema)) : void 0,
88
+ outputSchema: _tool.outputSchema ? stringify(esm_default(_tool.outputSchema)) : void 0
89
+ };
90
+ return acc;
91
+ }, {});
92
+ let serializedAgentWorkflows = {};
93
+ if ("getWorkflows" in agent) {
94
+ const logger = mastra.getLogger();
95
+ try {
96
+ const workflows = await agent.getWorkflows({ runtimeContext });
97
+ serializedAgentWorkflows = Object.entries(workflows || {}).reduce((acc, [key, workflow]) => {
98
+ return {
99
+ ...acc,
100
+ [key]: {
101
+ name: workflow.name
102
+ }
103
+ };
104
+ }, {});
105
+ } catch (error) {
106
+ logger.error("Error getting workflows for agent", { agentName: agent.name, error });
107
+ }
108
+ }
109
+ const instructions = await agent.getInstructions({ runtimeContext });
110
+ const llm = await agent.getLLM({ runtimeContext });
111
+ return {
112
+ name: agent.name,
113
+ instructions,
114
+ tools: serializedAgentTools,
115
+ workflows: serializedAgentWorkflows,
116
+ provider: llm?.getProvider(),
117
+ modelId: llm?.getModelId()
118
+ };
119
+ } catch (error) {
120
+ return handleError(error, "Error getting agent");
121
+ }
122
+ }
123
+ async function getEvalsByAgentIdHandler({
124
+ mastra,
125
+ runtimeContext,
126
+ agentId
127
+ }) {
128
+ try {
129
+ const agent = mastra.getAgent(agentId);
130
+ const evals = await mastra.getStorage()?.getEvalsByAgentName?.(agent.name, "test") || [];
131
+ const instructions = await agent.getInstructions({ runtimeContext });
132
+ return {
133
+ id: agentId,
134
+ name: agent.name,
135
+ instructions,
136
+ evals
137
+ };
138
+ } catch (error) {
139
+ return handleError(error, "Error getting test evals");
140
+ }
141
+ }
142
+ async function getLiveEvalsByAgentIdHandler({
143
+ mastra,
144
+ runtimeContext,
145
+ agentId
146
+ }) {
147
+ try {
148
+ const agent = mastra.getAgent(agentId);
149
+ const evals = await mastra.getStorage()?.getEvalsByAgentName?.(agent.name, "live") || [];
150
+ const instructions = await agent.getInstructions({ runtimeContext });
151
+ return {
152
+ id: agentId,
153
+ name: agent.name,
154
+ instructions,
155
+ evals
156
+ };
157
+ } catch (error) {
158
+ return handleError(error, "Error getting live evals");
159
+ }
160
+ }
161
+ async function generateHandler({
162
+ mastra,
163
+ runtimeContext,
164
+ agentId,
165
+ body
166
+ }) {
167
+ try {
168
+ const agent = mastra.getAgent(agentId);
169
+ if (!agent) {
170
+ throw new HTTPException(404, { message: "Agent not found" });
171
+ }
172
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
173
+ const finalResourceId = resourceId ?? resourceid;
174
+ const finalRuntimeContext = new RuntimeContext([
175
+ ...Array.from(runtimeContext.entries()),
176
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
177
+ ]);
178
+ validateBody({ messages });
179
+ const result = await agent.generate(messages, {
180
+ ...rest,
181
+ // @ts-expect-error TODO fix types
182
+ resourceId: finalResourceId,
183
+ runtimeContext: finalRuntimeContext
184
+ });
185
+ return result;
186
+ } catch (error) {
187
+ return handleError(error, "Error generating from agent");
188
+ }
189
+ }
190
+ async function streamGenerateHandler({
191
+ mastra,
192
+ runtimeContext,
193
+ agentId,
194
+ body
195
+ }) {
196
+ try {
197
+ const agent = mastra.getAgent(agentId);
198
+ if (!agent) {
199
+ throw new HTTPException(404, { message: "Agent not found" });
200
+ }
201
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
202
+ const finalResourceId = resourceId ?? resourceid;
203
+ const finalRuntimeContext = new RuntimeContext([
204
+ ...Array.from(runtimeContext.entries()),
205
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
206
+ ]);
207
+ validateBody({ messages });
208
+ const streamResult = await agent.stream(messages, {
209
+ ...rest,
210
+ // @ts-expect-error TODO fix types
211
+ resourceId: finalResourceId,
212
+ runtimeContext: finalRuntimeContext
213
+ });
214
+ const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
215
+ sendUsage: true,
216
+ sendReasoning: true,
217
+ getErrorMessage: (error) => {
218
+ return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
219
+ }
220
+ });
221
+ return streamResponse;
222
+ } catch (error) {
223
+ throw new HTTPException(error?.status ?? 500, { message: error?.message ?? "Error streaming from agent" });
224
+ }
225
+ }
226
+
227
+ export { agents_exports, generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler };