@mastra/server 0.0.0-separate-trace-data-from-component-20250501141108 → 0.0.0-support-d1-client-20250701191943

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 (91) hide show
  1. package/README.md +48 -135
  2. package/dist/_tsup-dts-rollup.d.cts +517 -103
  3. package/dist/_tsup-dts-rollup.d.ts +517 -103
  4. package/dist/{chunk-2FJURXCL.cjs → chunk-3DKDT264.cjs} +93 -55
  5. package/dist/{chunk-Y3SV5XK4.js → chunk-4QBIUKVY.js} +105 -16
  6. package/dist/{chunk-QN4KF3BH.cjs → chunk-57CJTIPW.cjs} +2 -2
  7. package/dist/chunk-7524QH7G.cjs +483 -0
  8. package/dist/chunk-75ZPJI57.cjs +9 -0
  9. package/dist/{chunk-M3YJLWTU.js → chunk-7GQJAMTA.js} +68 -57
  10. package/dist/chunk-BOXE6WSV.js +81 -0
  11. package/dist/{chunk-WTHDCRMY.js → chunk-BP24Z4WG.js} +12 -6
  12. package/dist/chunk-BWHGBV3K.cjs +278 -0
  13. package/dist/chunk-CNVDCLSY.cjs +86 -0
  14. package/dist/{chunk-5JNVY6DU.js → chunk-DFJUGWNU.js} +7 -5
  15. package/dist/{chunk-5YGDYMRB.cjs → chunk-DPRB3R7Y.cjs} +14 -13
  16. package/dist/{chunk-D3G23FP3.cjs → chunk-FGGMQCPP.cjs} +24 -22
  17. package/dist/{chunk-4JINXASC.js → chunk-FVMGZKFD.js} +5 -4
  18. package/dist/{chunk-Q6SHQECN.js → chunk-G37NVEDX.js} +34 -10
  19. package/dist/chunk-GHC4YV6R.js +504 -0
  20. package/dist/{chunk-L7XE5QTW.js → chunk-H5PTF3Y4.js} +1 -1
  21. package/dist/chunk-L3N7ST2Z.cjs +118 -0
  22. package/dist/chunk-LI436ITD.cjs +2042 -0
  23. package/dist/{chunk-RBQASTUP.js → chunk-LTJNMM2Y.js} +63 -25
  24. package/dist/chunk-MEGCYGBU.js +2039 -0
  25. package/dist/chunk-MLKGABMK.js +7 -0
  26. package/dist/{chunk-ZLBRQFDD.cjs → chunk-MTR2B27E.cjs} +3 -2
  27. package/dist/{chunk-TRDNDNGQ.js → chunk-NYN7KFXL.js} +1 -7
  28. package/dist/{chunk-FV45V6WC.cjs → chunk-OCWPVYNI.cjs} +0 -7
  29. package/dist/{chunk-3AHQ5RGN.js → chunk-QDOJJCS4.js} +3 -2
  30. package/dist/chunk-SDPGVWQJ.cjs +512 -0
  31. package/dist/chunk-TGZIR6AK.cjs +332 -0
  32. package/dist/{chunk-M56ECCHK.cjs → chunk-TVBPFPTN.cjs} +20 -19
  33. package/dist/{chunk-7IWQE76Z.cjs → chunk-ULFULEW4.cjs} +16 -10
  34. package/dist/{chunk-QJ3AHN64.js → chunk-VMXLEF54.js} +4 -3
  35. package/dist/chunk-Z5TH5F35.js +468 -0
  36. package/dist/server/handlers/a2a.cjs +30 -0
  37. package/dist/server/handlers/a2a.d.cts +6 -0
  38. package/dist/server/handlers/a2a.d.ts +6 -0
  39. package/dist/server/handlers/a2a.js +1 -0
  40. package/dist/server/handlers/agents.cjs +7 -7
  41. package/dist/server/handlers/agents.js +1 -1
  42. package/dist/server/handlers/error.cjs +2 -2
  43. package/dist/server/handlers/error.js +1 -1
  44. package/dist/server/handlers/legacyWorkflows.cjs +46 -0
  45. package/dist/server/handlers/legacyWorkflows.d.cts +10 -0
  46. package/dist/server/handlers/legacyWorkflows.d.ts +10 -0
  47. package/dist/server/handlers/legacyWorkflows.js +1 -0
  48. package/dist/server/handlers/logs.cjs +4 -4
  49. package/dist/server/handlers/logs.js +1 -1
  50. package/dist/server/handlers/memory.cjs +9 -9
  51. package/dist/server/handlers/memory.js +1 -1
  52. package/dist/server/handlers/network.cjs +5 -5
  53. package/dist/server/handlers/network.js +1 -1
  54. package/dist/server/handlers/telemetry.cjs +3 -3
  55. package/dist/server/handlers/telemetry.js +1 -1
  56. package/dist/server/handlers/tools.cjs +5 -5
  57. package/dist/server/handlers/tools.js +1 -1
  58. package/dist/server/handlers/utils.cjs +2 -2
  59. package/dist/server/handlers/utils.js +1 -1
  60. package/dist/server/handlers/vNextNetwork.cjs +218 -0
  61. package/dist/server/handlers/vNextNetwork.d.cts +6 -0
  62. package/dist/server/handlers/vNextNetwork.d.ts +6 -0
  63. package/dist/server/handlers/vNextNetwork.js +211 -0
  64. package/dist/server/handlers/vector.cjs +7 -7
  65. package/dist/server/handlers/vector.js +1 -1
  66. package/dist/server/handlers/voice.cjs +8 -4
  67. package/dist/server/handlers/voice.d.cts +1 -0
  68. package/dist/server/handlers/voice.d.ts +1 -0
  69. package/dist/server/handlers/voice.js +1 -1
  70. package/dist/server/handlers/workflows.cjs +25 -13
  71. package/dist/server/handlers/workflows.d.cts +5 -2
  72. package/dist/server/handlers/workflows.d.ts +5 -2
  73. package/dist/server/handlers/workflows.js +1 -1
  74. package/dist/server/handlers.cjs +32 -27
  75. package/dist/server/handlers.d.cts +2 -1
  76. package/dist/server/handlers.d.ts +2 -1
  77. package/dist/server/handlers.js +11 -10
  78. package/package.json +14 -13
  79. package/dist/chunk-3EJZQ6TQ.js +0 -49
  80. package/dist/chunk-4BIX6GMY.cjs +0 -189
  81. package/dist/chunk-55HTWX4C.cjs +0 -93
  82. package/dist/chunk-5SWCVTNL.cjs +0 -5579
  83. package/dist/chunk-AELYAUEE.cjs +0 -316
  84. package/dist/chunk-EVCC233P.cjs +0 -321
  85. package/dist/chunk-JPB6RPGB.js +0 -304
  86. package/dist/chunk-OMN3UI6X.js +0 -5576
  87. package/dist/chunk-SKBVVI24.cjs +0 -54
  88. package/dist/server/handlers/vNextWorkflows.cjs +0 -46
  89. package/dist/server/handlers/vNextWorkflows.d.cts +0 -10
  90. package/dist/server/handlers/vNextWorkflows.d.ts +0 -10
  91. package/dist/server/handlers/vNextWorkflows.js +0 -1
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var chunkQN4KF3BH_cjs = require('./chunk-QN4KF3BH.cjs');
4
- var chunkZLBRQFDD_cjs = require('./chunk-ZLBRQFDD.cjs');
5
- var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
3
+ var chunk57CJTIPW_cjs = require('./chunk-57CJTIPW.cjs');
4
+ var chunkMTR2B27E_cjs = require('./chunk-MTR2B27E.cjs');
5
+ var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
6
+ var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
6
7
 
7
8
  // src/server/handlers/memory.ts
8
9
  var memory_exports = {};
9
- chunkFV45V6WC_cjs.__export(memory_exports, {
10
+ chunk75ZPJI57_cjs.__export(memory_exports, {
10
11
  createThreadHandler: () => createThreadHandler,
11
12
  deleteThreadHandler: () => deleteThreadHandler,
12
13
  getMemoryStatusHandler: () => getMemoryStatusHandler,
@@ -16,80 +17,102 @@ chunkFV45V6WC_cjs.__export(memory_exports, {
16
17
  saveMessagesHandler: () => saveMessagesHandler,
17
18
  updateThreadHandler: () => updateThreadHandler
18
19
  });
19
- function getMemoryFromContext({
20
+ async function getMemoryFromContext({
20
21
  mastra,
21
- agentId
22
+ agentId,
23
+ networkId,
24
+ runtimeContext
22
25
  }) {
23
26
  const agent = agentId ? mastra.getAgent(agentId) : null;
24
27
  if (agentId && !agent) {
25
- throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
28
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Agent not found" });
29
+ }
30
+ const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
31
+ if (networkId && !network) {
32
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
33
+ }
34
+ if (agent) {
35
+ return agent?.getMemory() || mastra.getMemory();
36
+ }
37
+ if (network) {
38
+ return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
26
39
  }
27
- const memory = agent?.getMemory?.() || mastra.getMemory();
28
- return memory;
40
+ return mastra.getMemory();
29
41
  }
30
- async function getMemoryStatusHandler({ mastra, agentId }) {
42
+ async function getMemoryStatusHandler({
43
+ mastra,
44
+ agentId,
45
+ networkId,
46
+ runtimeContext
47
+ }) {
31
48
  try {
32
- const memory = getMemoryFromContext({ mastra, agentId });
49
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
33
50
  if (!memory) {
34
51
  return { result: false };
35
52
  }
36
53
  return { result: true };
37
54
  } catch (error) {
38
- return chunkZLBRQFDD_cjs.handleError(error, "Error getting memory status");
55
+ return chunkMTR2B27E_cjs.handleError(error, "Error getting memory status");
39
56
  }
40
57
  }
41
58
  async function getThreadsHandler({
42
59
  mastra,
43
60
  agentId,
44
- resourceId
61
+ resourceId,
62
+ networkId,
63
+ runtimeContext
45
64
  }) {
46
65
  try {
47
- const memory = getMemoryFromContext({ mastra, agentId });
66
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
48
67
  if (!memory) {
49
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
68
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
50
69
  }
51
- chunkQN4KF3BH_cjs.validateBody({ resourceId });
70
+ chunk57CJTIPW_cjs.validateBody({ resourceId });
52
71
  const threads = await memory.getThreadsByResourceId({ resourceId });
53
72
  return threads;
54
73
  } catch (error) {
55
- return chunkZLBRQFDD_cjs.handleError(error, "Error getting threads");
74
+ return chunkMTR2B27E_cjs.handleError(error, "Error getting threads");
56
75
  }
57
76
  }
58
77
  async function getThreadByIdHandler({
59
78
  mastra,
60
79
  agentId,
61
- threadId
80
+ threadId,
81
+ networkId,
82
+ runtimeContext
62
83
  }) {
63
84
  try {
64
- chunkQN4KF3BH_cjs.validateBody({ threadId });
65
- const memory = getMemoryFromContext({ mastra, agentId });
85
+ chunk57CJTIPW_cjs.validateBody({ threadId });
86
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
66
87
  if (!memory) {
67
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
88
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
68
89
  }
69
90
  const thread = await memory.getThreadById({ threadId });
70
91
  if (!thread) {
71
- throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Thread not found" });
92
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
72
93
  }
73
94
  return thread;
74
95
  } catch (error) {
75
- return chunkZLBRQFDD_cjs.handleError(error, "Error getting thread");
96
+ return chunkMTR2B27E_cjs.handleError(error, "Error getting thread");
76
97
  }
77
98
  }
78
99
  async function saveMessagesHandler({
79
100
  mastra,
80
101
  agentId,
81
- body
102
+ body,
103
+ networkId,
104
+ runtimeContext
82
105
  }) {
83
106
  try {
84
- const memory = getMemoryFromContext({ mastra, agentId });
107
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
85
108
  if (!memory) {
86
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
109
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
87
110
  }
88
111
  if (!body?.messages) {
89
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Messages are required" });
112
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Messages are required" });
90
113
  }
91
114
  if (!Array.isArray(body.messages)) {
92
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Messages should be an array" });
115
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Messages should be an array" });
93
116
  }
94
117
  const processedMessages = body.messages.map((message) => ({
95
118
  ...message,
@@ -99,20 +122,22 @@ async function saveMessagesHandler({
99
122
  const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
100
123
  return result;
101
124
  } catch (error) {
102
- return chunkZLBRQFDD_cjs.handleError(error, "Error saving messages");
125
+ return chunkMTR2B27E_cjs.handleError(error, "Error saving messages");
103
126
  }
104
127
  }
105
128
  async function createThreadHandler({
106
129
  mastra,
107
130
  agentId,
108
- body
131
+ body,
132
+ networkId,
133
+ runtimeContext
109
134
  }) {
110
135
  try {
111
- const memory = getMemoryFromContext({ mastra, agentId });
136
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
112
137
  if (!memory) {
113
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
138
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
114
139
  }
115
- chunkQN4KF3BH_cjs.validateBody({ resourceId: body?.resourceId });
140
+ chunk57CJTIPW_cjs.validateBody({ resourceId: body?.resourceId });
116
141
  const result = await memory.createThread({
117
142
  resourceId: body?.resourceId,
118
143
  title: body?.title,
@@ -121,29 +146,31 @@ async function createThreadHandler({
121
146
  });
122
147
  return result;
123
148
  } catch (error) {
124
- return chunkZLBRQFDD_cjs.handleError(error, "Error saving thread to memory");
149
+ return chunkMTR2B27E_cjs.handleError(error, "Error saving thread to memory");
125
150
  }
126
151
  }
127
152
  async function updateThreadHandler({
128
153
  mastra,
129
154
  agentId,
130
155
  threadId,
131
- body
156
+ body,
157
+ networkId,
158
+ runtimeContext
132
159
  }) {
133
160
  try {
134
- const memory = getMemoryFromContext({ mastra, agentId });
161
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
135
162
  if (!body) {
136
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Body is required" });
163
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Body is required" });
137
164
  }
138
165
  const { title, metadata, resourceId } = body;
139
166
  const updatedAt = /* @__PURE__ */ new Date();
140
- chunkQN4KF3BH_cjs.validateBody({ threadId });
167
+ chunk57CJTIPW_cjs.validateBody({ threadId });
141
168
  if (!memory) {
142
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
169
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
143
170
  }
144
171
  const thread = await memory.getThreadById({ threadId });
145
172
  if (!thread) {
146
- throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Thread not found" });
173
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
147
174
  }
148
175
  const updatedThread = {
149
176
  ...thread,
@@ -156,49 +183,60 @@ async function updateThreadHandler({
156
183
  const result = await memory.saveThread({ thread: updatedThread });
157
184
  return result;
158
185
  } catch (error) {
159
- return chunkZLBRQFDD_cjs.handleError(error, "Error updating thread");
186
+ return chunkMTR2B27E_cjs.handleError(error, "Error updating thread");
160
187
  }
161
188
  }
162
189
  async function deleteThreadHandler({
163
190
  mastra,
164
191
  agentId,
165
- threadId
192
+ threadId,
193
+ networkId,
194
+ runtimeContext
166
195
  }) {
167
196
  try {
168
- chunkQN4KF3BH_cjs.validateBody({ threadId });
169
- const memory = getMemoryFromContext({ mastra, agentId });
197
+ chunk57CJTIPW_cjs.validateBody({ threadId });
198
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
170
199
  if (!memory) {
171
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
200
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
172
201
  }
173
202
  const thread = await memory.getThreadById({ threadId });
174
203
  if (!thread) {
175
- throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Thread not found" });
204
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
176
205
  }
177
206
  await memory.deleteThread(threadId);
178
207
  return { result: "Thread deleted" };
179
208
  } catch (error) {
180
- return chunkZLBRQFDD_cjs.handleError(error, "Error deleting thread");
209
+ return chunkMTR2B27E_cjs.handleError(error, "Error deleting thread");
181
210
  }
182
211
  }
183
212
  async function getMessagesHandler({
184
213
  mastra,
185
214
  agentId,
186
- threadId
215
+ threadId,
216
+ limit,
217
+ networkId,
218
+ runtimeContext
187
219
  }) {
220
+ if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
221
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Invalid limit: must be a positive integer" });
222
+ }
188
223
  try {
189
- chunkQN4KF3BH_cjs.validateBody({ threadId });
190
- const memory = getMemoryFromContext({ mastra, agentId });
224
+ chunk57CJTIPW_cjs.validateBody({ threadId });
225
+ const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
191
226
  if (!memory) {
192
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Memory is not initialized" });
227
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: "Memory is not initialized" });
193
228
  }
194
229
  const thread = await memory.getThreadById({ threadId });
195
230
  if (!thread) {
196
- throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Thread not found" });
231
+ throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Thread not found" });
197
232
  }
198
- const result = await memory.query({ threadId });
199
- return result;
233
+ const result = await memory.query({
234
+ threadId,
235
+ ...limit && { selectBy: { last: limit } }
236
+ });
237
+ return { messages: result.messages, uiMessages: result.uiMessages };
200
238
  } catch (error) {
201
- return chunkZLBRQFDD_cjs.handleError(error, "Error getting messages");
239
+ return chunkMTR2B27E_cjs.handleError(error, "Error getting messages");
202
240
  }
203
241
  }
204
242
 
@@ -1,7 +1,9 @@
1
- import { stringify, esm_default } from './chunk-OMN3UI6X.js';
2
- import { validateBody } from './chunk-L7XE5QTW.js';
3
- import { handleError } from './chunk-3AHQ5RGN.js';
4
- import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
1
+ import { stringify, esm_default } from './chunk-MEGCYGBU.js';
2
+ import { validateBody } from './chunk-H5PTF3Y4.js';
3
+ import { handleError } from './chunk-QDOJJCS4.js';
4
+ import { HTTPException } from './chunk-NYN7KFXL.js';
5
+ import { __export } from './chunk-MLKGABMK.js';
6
+ import { RuntimeContext } from '@mastra/core/runtime-context';
5
7
 
6
8
  // src/server/handlers/agents.ts
7
9
  var agents_exports = {};
@@ -21,6 +23,8 @@ async function getAgentsHandler({ mastra, runtimeContext }) {
21
23
  const instructions = await agent.getInstructions({ runtimeContext });
22
24
  const tools = await agent.getTools({ runtimeContext });
23
25
  const llm = await agent.getLLM({ runtimeContext });
26
+ const defaultGenerateOptions = await agent.getDefaultGenerateOptions({ runtimeContext });
27
+ const defaultStreamOptions = await agent.getDefaultStreamOptions({ runtimeContext });
24
28
  const serializedAgentTools = Object.entries(tools || {}).reduce((acc, [key, tool]) => {
25
29
  const _tool = tool;
26
30
  acc[key] = {
@@ -30,13 +34,33 @@ async function getAgentsHandler({ mastra, runtimeContext }) {
30
34
  };
31
35
  return acc;
32
36
  }, {});
37
+ let serializedAgentWorkflows = {};
38
+ if ("getWorkflows" in agent) {
39
+ const logger = mastra.getLogger();
40
+ try {
41
+ const workflows = await agent.getWorkflows({ runtimeContext });
42
+ serializedAgentWorkflows = Object.entries(workflows || {}).reduce((acc, [key, workflow]) => {
43
+ return {
44
+ ...acc,
45
+ [key]: {
46
+ name: workflow.name
47
+ }
48
+ };
49
+ }, {});
50
+ } catch (error) {
51
+ logger.error("Error getting workflows for agent", { agentName: agent.name, error });
52
+ }
53
+ }
33
54
  return {
34
55
  id,
35
56
  name: agent.name,
36
57
  instructions,
37
58
  tools: serializedAgentTools,
59
+ workflows: serializedAgentWorkflows,
38
60
  provider: llm?.getProvider(),
39
- modelId: llm?.getModelId()
61
+ modelId: llm?.getModelId(),
62
+ defaultGenerateOptions,
63
+ defaultStreamOptions
40
64
  };
41
65
  })
42
66
  );
@@ -52,7 +76,8 @@ async function getAgentsHandler({ mastra, runtimeContext }) {
52
76
  async function getAgentByIdHandler({
53
77
  mastra,
54
78
  runtimeContext,
55
- agentId
79
+ agentId,
80
+ isPlayground = false
56
81
  }) {
57
82
  try {
58
83
  const agent = mastra.getAgent(agentId);
@@ -69,14 +94,59 @@ async function getAgentByIdHandler({
69
94
  };
70
95
  return acc;
71
96
  }, {});
72
- const instructions = await agent.getInstructions({ runtimeContext });
97
+ let serializedAgentWorkflows = {};
98
+ if ("getWorkflows" in agent) {
99
+ const logger = mastra.getLogger();
100
+ try {
101
+ const workflows = await agent.getWorkflows({ runtimeContext });
102
+ serializedAgentWorkflows = Object.entries(workflows || {}).reduce((acc, [key, workflow]) => {
103
+ return {
104
+ ...acc,
105
+ [key]: {
106
+ name: workflow.name,
107
+ steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
108
+ return {
109
+ ...acc2,
110
+ [key2]: {
111
+ id: step.id,
112
+ description: step.description
113
+ }
114
+ };
115
+ }, {})
116
+ }
117
+ };
118
+ }, {});
119
+ } catch (error) {
120
+ logger.error("Error getting workflows for agent", { agentName: agent.name, error });
121
+ }
122
+ }
123
+ let proxyRuntimeContext = runtimeContext;
124
+ if (isPlayground) {
125
+ proxyRuntimeContext = new Proxy(runtimeContext, {
126
+ get(target, prop) {
127
+ if (prop === "get") {
128
+ return function(key) {
129
+ const value = target.get(key);
130
+ return value ?? `<${key}>`;
131
+ };
132
+ }
133
+ return Reflect.get(target, prop);
134
+ }
135
+ });
136
+ }
137
+ const instructions = await agent.getInstructions({ runtimeContext: proxyRuntimeContext });
73
138
  const llm = await agent.getLLM({ runtimeContext });
139
+ const defaultGenerateOptions = await agent.getDefaultGenerateOptions({ runtimeContext: proxyRuntimeContext });
140
+ const defaultStreamOptions = await agent.getDefaultStreamOptions({ runtimeContext: proxyRuntimeContext });
74
141
  return {
75
142
  name: agent.name,
76
143
  instructions,
77
144
  tools: serializedAgentTools,
145
+ workflows: serializedAgentWorkflows,
78
146
  provider: llm?.getProvider(),
79
- modelId: llm?.getModelId()
147
+ modelId: llm?.getModelId(),
148
+ defaultGenerateOptions,
149
+ defaultStreamOptions
80
150
  };
81
151
  } catch (error) {
82
152
  return handleError(error, "Error getting agent");
@@ -124,21 +194,27 @@ async function generateHandler({
124
194
  mastra,
125
195
  runtimeContext,
126
196
  agentId,
127
- body
197
+ body,
198
+ abortSignal
128
199
  }) {
129
200
  try {
130
201
  const agent = mastra.getAgent(agentId);
131
202
  if (!agent) {
132
203
  throw new HTTPException(404, { message: "Agent not found" });
133
204
  }
134
- const { messages, resourceId, resourceid, ...rest } = body;
205
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
135
206
  const finalResourceId = resourceId ?? resourceid;
207
+ const finalRuntimeContext = new RuntimeContext([
208
+ ...Array.from(runtimeContext.entries()),
209
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
210
+ ]);
136
211
  validateBody({ messages });
137
212
  const result = await agent.generate(messages, {
138
213
  ...rest,
139
214
  // @ts-expect-error TODO fix types
140
215
  resourceId: finalResourceId,
141
- runtimeContext
216
+ runtimeContext: finalRuntimeContext,
217
+ signal: abortSignal
142
218
  });
143
219
  return result;
144
220
  } catch (error) {
@@ -149,32 +225,45 @@ async function streamGenerateHandler({
149
225
  mastra,
150
226
  runtimeContext,
151
227
  agentId,
152
- body
228
+ body,
229
+ abortSignal
153
230
  }) {
154
231
  try {
155
232
  const agent = mastra.getAgent(agentId);
156
233
  if (!agent) {
157
234
  throw new HTTPException(404, { message: "Agent not found" });
158
235
  }
159
- const { messages, resourceId, resourceid, ...rest } = body;
236
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
160
237
  const finalResourceId = resourceId ?? resourceid;
238
+ const finalRuntimeContext = new RuntimeContext([
239
+ ...Array.from(runtimeContext.entries()),
240
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
241
+ ]);
161
242
  validateBody({ messages });
162
243
  const streamResult = await agent.stream(messages, {
163
244
  ...rest,
164
245
  // @ts-expect-error TODO fix types
165
246
  resourceId: finalResourceId,
166
- runtimeContext
247
+ runtimeContext: finalRuntimeContext,
248
+ signal: abortSignal
167
249
  });
168
- const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
250
+ const streamResponse = rest.output ? streamResult.toTextStreamResponse({
251
+ headers: {
252
+ "Transfer-Encoding": "chunked"
253
+ }
254
+ }) : streamResult.toDataStreamResponse({
169
255
  sendUsage: true,
170
256
  sendReasoning: true,
171
257
  getErrorMessage: (error) => {
172
258
  return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
259
+ },
260
+ headers: {
261
+ "Transfer-Encoding": "chunked"
173
262
  }
174
263
  });
175
264
  return streamResponse;
176
265
  } catch (error) {
177
- throw new HTTPException(error?.status ?? 500, { message: error?.message ?? "Error streaming from agent" });
266
+ return handleError(error, "error streaming agent response");
178
267
  }
179
268
  }
180
269
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
3
+ var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
4
4
 
5
5
  // src/server/handlers/utils.ts
6
6
  function validateBody(body) {
@@ -11,7 +11,7 @@ function validateBody(body) {
11
11
  return acc;
12
12
  }, {});
13
13
  if (Object.keys(errorResponse).length > 0) {
14
- throw new chunkFV45V6WC_cjs.HTTPException(400, { message: Object.values(errorResponse)[0] });
14
+ throw new chunkOCWPVYNI_cjs.HTTPException(400, { message: Object.values(errorResponse)[0] });
15
15
  }
16
16
  }
17
17