@mastra/server 0.0.0-separate-trace-data-from-component-20250501141108 → 0.0.0-taofeeqInngest-20250603090617
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.
- package/README.md +48 -135
- package/dist/_tsup-dts-rollup.d.cts +282 -92
- package/dist/_tsup-dts-rollup.d.ts +282 -92
- package/dist/{chunk-4JINXASC.js → chunk-55DOQLP6.js} +5 -4
- package/dist/{chunk-QN4KF3BH.cjs → chunk-57CJTIPW.cjs} +2 -2
- package/dist/{chunk-ZLBRQFDD.cjs → chunk-64U3UDTH.cjs} +2 -2
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/{chunk-4BIX6GMY.cjs → chunk-A3KDUGS7.cjs} +78 -28
- package/dist/{chunk-7IWQE76Z.cjs → chunk-B4MQFJ7G.cjs} +16 -10
- package/dist/{chunk-WTHDCRMY.js → chunk-BFOA2QQY.js} +12 -6
- package/dist/{chunk-M56ECCHK.cjs → chunk-BNEY4P4P.cjs} +20 -19
- package/dist/{chunk-QJ3AHN64.js → chunk-C7564HUT.js} +4 -3
- package/dist/{chunk-55HTWX4C.cjs → chunk-CMMOIUFC.cjs} +40 -19
- package/dist/{chunk-Q6SHQECN.js → chunk-CP55EVBK.js} +24 -4
- package/dist/{chunk-RBQASTUP.js → chunk-DJJIUEL2.js} +13 -5
- package/dist/{chunk-3EJZQ6TQ.js → chunk-DQLE3DVM.js} +5 -5
- package/dist/chunk-GHC4YV6R.js +504 -0
- package/dist/{chunk-L7XE5QTW.js → chunk-H5PTF3Y4.js} +1 -1
- package/dist/{chunk-Y3SV5XK4.js → chunk-J3PKLB3A.js} +60 -10
- package/dist/{chunk-JPB6RPGB.js → chunk-KUNQFY2W.js} +112 -51
- package/dist/chunk-LIVAK2DM.js +2001 -0
- package/dist/{chunk-3AHQ5RGN.js → chunk-M5ABIP7D.js} +1 -1
- package/dist/chunk-MLKGABMK.js +7 -0
- package/dist/chunk-MMO2HDM6.cjs +378 -0
- package/dist/{chunk-SKBVVI24.cjs → chunk-NEOOQUKW.cjs} +11 -11
- package/dist/{chunk-TRDNDNGQ.js → chunk-NYN7KFXL.js} +1 -7
- package/dist/{chunk-FV45V6WC.cjs → chunk-OCWPVYNI.cjs} +0 -7
- package/dist/chunk-SDPGVWQJ.cjs +512 -0
- package/dist/{chunk-5JNVY6DU.js → chunk-TJKLBTFB.js} +20 -8
- package/dist/{chunk-5YGDYMRB.cjs → chunk-VPNDC2DI.cjs} +14 -13
- package/dist/{chunk-M3YJLWTU.js → chunk-W7VCKPAD.js} +62 -51
- package/dist/{chunk-2FJURXCL.cjs → chunk-Y7UWRW5X.cjs} +43 -35
- package/dist/chunk-YIOVBYZH.cjs +332 -0
- package/dist/chunk-YWLUOY3D.cjs +2004 -0
- package/dist/{chunk-D3G23FP3.cjs → chunk-ZE5AAC4I.cjs} +37 -25
- package/dist/server/handlers/a2a.cjs +30 -0
- package/dist/server/handlers/a2a.d.cts +6 -0
- package/dist/server/handlers/a2a.d.ts +6 -0
- package/dist/server/handlers/a2a.js +1 -0
- package/dist/server/handlers/agents.cjs +7 -7
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/error.cjs +2 -2
- package/dist/server/handlers/error.js +1 -1
- package/dist/server/handlers/legacyWorkflows.cjs +46 -0
- package/dist/server/handlers/legacyWorkflows.d.cts +10 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +10 -0
- package/dist/server/handlers/legacyWorkflows.js +1 -0
- package/dist/server/handlers/logs.cjs +4 -4
- package/dist/server/handlers/logs.js +1 -1
- package/dist/server/handlers/memory.cjs +9 -9
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/network.cjs +5 -5
- package/dist/server/handlers/network.js +1 -1
- package/dist/server/handlers/telemetry.cjs +3 -3
- package/dist/server/handlers/telemetry.js +1 -1
- package/dist/server/handlers/tools.cjs +5 -5
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/utils.cjs +2 -2
- package/dist/server/handlers/utils.js +1 -1
- package/dist/server/handlers/vector.cjs +7 -7
- package/dist/server/handlers/vector.js +1 -1
- package/dist/server/handlers/voice.cjs +8 -4
- package/dist/server/handlers/voice.d.cts +1 -0
- package/dist/server/handlers/voice.d.ts +1 -0
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers/workflows.cjs +17 -13
- package/dist/server/handlers/workflows.d.cts +3 -2
- package/dist/server/handlers/workflows.d.ts +3 -2
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +32 -27
- package/dist/server/handlers.d.cts +2 -1
- package/dist/server/handlers.d.ts +2 -1
- package/dist/server/handlers.js +11 -10
- package/package.json +7 -5
- package/dist/chunk-5SWCVTNL.cjs +0 -5579
- package/dist/chunk-AELYAUEE.cjs +0 -316
- package/dist/chunk-EVCC233P.cjs +0 -321
- package/dist/chunk-OMN3UI6X.js +0 -5576
- package/dist/server/handlers/vNextWorkflows.cjs +0 -46
- package/dist/server/handlers/vNextWorkflows.d.cts +0 -10
- package/dist/server/handlers/vNextWorkflows.d.ts +0 -10
- package/dist/server/handlers/vNextWorkflows.js +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { validateBody } from './chunk-
|
|
2
|
-
import { handleError } from './chunk-
|
|
3
|
-
import {
|
|
1
|
+
import { validateBody } from './chunk-H5PTF3Y4.js';
|
|
2
|
+
import { handleError } from './chunk-M5ABIP7D.js';
|
|
3
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
4
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
4
5
|
|
|
5
6
|
// src/server/handlers/memory.ts
|
|
6
7
|
var memory_exports = {};
|
|
@@ -181,8 +182,12 @@ async function deleteThreadHandler({
|
|
|
181
182
|
async function getMessagesHandler({
|
|
182
183
|
mastra,
|
|
183
184
|
agentId,
|
|
184
|
-
threadId
|
|
185
|
+
threadId,
|
|
186
|
+
limit
|
|
185
187
|
}) {
|
|
188
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
|
|
189
|
+
throw new HTTPException(400, { message: "Invalid limit: must be a positive integer" });
|
|
190
|
+
}
|
|
186
191
|
try {
|
|
187
192
|
validateBody({ threadId });
|
|
188
193
|
const memory = getMemoryFromContext({ mastra, agentId });
|
|
@@ -193,7 +198,10 @@ async function getMessagesHandler({
|
|
|
193
198
|
if (!thread) {
|
|
194
199
|
throw new HTTPException(404, { message: "Thread not found" });
|
|
195
200
|
}
|
|
196
|
-
const result = await memory.query({
|
|
201
|
+
const result = await memory.query({
|
|
202
|
+
threadId,
|
|
203
|
+
...limit && { selectBy: { last: limit } }
|
|
204
|
+
});
|
|
197
205
|
return result;
|
|
198
206
|
} catch (error) {
|
|
199
207
|
return handleError(error, "Error getting messages");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { validateBody } from './chunk-
|
|
2
|
-
import { handleError } from './chunk-
|
|
3
|
-
import { __export } from './chunk-
|
|
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 = {};
|
|
@@ -37,9 +37,9 @@ async function getLogsByRunIdHandler({
|
|
|
37
37
|
async function getLogTransports({ mastra }) {
|
|
38
38
|
try {
|
|
39
39
|
const logger = mastra.getLogger();
|
|
40
|
-
const transports = logger.
|
|
40
|
+
const transports = logger.getTransports();
|
|
41
41
|
return {
|
|
42
|
-
transports: transports ?
|
|
42
|
+
transports: transports ? [...transports.keys()] : []
|
|
43
43
|
};
|
|
44
44
|
} catch (error) {
|
|
45
45
|
return handleError(error, "Error getting log Transports");
|
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
2
|
+
import { A2AError } from '@mastra/core/a2a';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
// src/server/handlers/a2a.ts
|
|
6
|
+
var a2a_exports = {};
|
|
7
|
+
__export(a2a_exports, {
|
|
8
|
+
getAgentCardByIdHandler: () => getAgentCardByIdHandler,
|
|
9
|
+
getAgentExecutionHandler: () => getAgentExecutionHandler,
|
|
10
|
+
handleTaskCancel: () => handleTaskCancel,
|
|
11
|
+
handleTaskGet: () => handleTaskGet,
|
|
12
|
+
handleTaskSend: () => handleTaskSend,
|
|
13
|
+
handleTaskSendSubscribe: () => handleTaskSendSubscribe
|
|
14
|
+
});
|
|
15
|
+
function normalizeError(error, reqId, taskId, logger) {
|
|
16
|
+
let a2aError;
|
|
17
|
+
if (error instanceof A2AError) {
|
|
18
|
+
a2aError = error;
|
|
19
|
+
} else if (error instanceof Error) {
|
|
20
|
+
a2aError = A2AError.internalError(error.message, { stack: error.stack });
|
|
21
|
+
} else {
|
|
22
|
+
a2aError = A2AError.internalError("An unknown error occurred.", error);
|
|
23
|
+
}
|
|
24
|
+
if (taskId && !a2aError.taskId) {
|
|
25
|
+
a2aError.taskId = taskId;
|
|
26
|
+
}
|
|
27
|
+
logger?.error(`Error processing request (Task: ${a2aError.taskId ?? "N/A"}, ReqID: ${reqId ?? "N/A"}):`, a2aError);
|
|
28
|
+
return createErrorResponse(reqId, a2aError.toJSONRPCError());
|
|
29
|
+
}
|
|
30
|
+
function createErrorResponse(id, error) {
|
|
31
|
+
return {
|
|
32
|
+
jsonrpc: "2.0",
|
|
33
|
+
id,
|
|
34
|
+
// Can be null if request ID was invalid/missing
|
|
35
|
+
error
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createSuccessResponse(id, result) {
|
|
39
|
+
if (!id) {
|
|
40
|
+
throw A2AError.internalError("Cannot create success response for null ID.");
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
jsonrpc: "2.0",
|
|
44
|
+
id,
|
|
45
|
+
result
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function convertToCoreMessage(message) {
|
|
49
|
+
return {
|
|
50
|
+
role: message.role === "user" ? "user" : "assistant",
|
|
51
|
+
content: message.parts.map((msg) => convertToCoreMessagePart(msg))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function convertToCoreMessagePart(part) {
|
|
55
|
+
switch (part.type) {
|
|
56
|
+
case "text":
|
|
57
|
+
return {
|
|
58
|
+
type: "text",
|
|
59
|
+
text: part.text
|
|
60
|
+
};
|
|
61
|
+
case "file":
|
|
62
|
+
return {
|
|
63
|
+
type: "file",
|
|
64
|
+
data: new URL(part.file.uri),
|
|
65
|
+
mimeType: part.file.mimeType
|
|
66
|
+
};
|
|
67
|
+
case "data":
|
|
68
|
+
throw new Error("Data parts are not supported in core messages");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/server/a2a/store.ts
|
|
73
|
+
var InMemoryTaskStore = class {
|
|
74
|
+
store = /* @__PURE__ */ new Map();
|
|
75
|
+
activeCancellations = /* @__PURE__ */ new Set();
|
|
76
|
+
async load({ agentId, taskId }) {
|
|
77
|
+
const entry = this.store.get(`${agentId}-${taskId}`);
|
|
78
|
+
if (!entry) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
return { task: { ...entry.task }, history: [...entry.history] };
|
|
82
|
+
}
|
|
83
|
+
async save({ agentId, data }) {
|
|
84
|
+
const key = `${agentId}-${data.task.id}`;
|
|
85
|
+
if (!data.task.id) {
|
|
86
|
+
throw new Error("Task ID is required");
|
|
87
|
+
}
|
|
88
|
+
this.store.set(key, {
|
|
89
|
+
task: { ...data.task },
|
|
90
|
+
history: [...data.history]
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/server/a2a/tasks.ts
|
|
96
|
+
function isTaskStatusUpdate(update) {
|
|
97
|
+
return "state" in update && !("parts" in update);
|
|
98
|
+
}
|
|
99
|
+
function isArtifactUpdate(update) {
|
|
100
|
+
return "parts" in update;
|
|
101
|
+
}
|
|
102
|
+
function applyUpdateToTaskAndHistory(current, update) {
|
|
103
|
+
let newTask = structuredClone(current.task);
|
|
104
|
+
let newHistory = structuredClone(current.history);
|
|
105
|
+
if (isTaskStatusUpdate(update)) {
|
|
106
|
+
newTask.status = {
|
|
107
|
+
...newTask.status,
|
|
108
|
+
// Keep existing properties if not overwritten
|
|
109
|
+
...update,
|
|
110
|
+
// Apply updates
|
|
111
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
112
|
+
};
|
|
113
|
+
if (update.message?.role === "agent") {
|
|
114
|
+
newHistory.push(update.message);
|
|
115
|
+
}
|
|
116
|
+
} else if (isArtifactUpdate(update)) {
|
|
117
|
+
if (!newTask.artifacts) {
|
|
118
|
+
newTask.artifacts = [];
|
|
119
|
+
} else {
|
|
120
|
+
newTask.artifacts = [...newTask.artifacts];
|
|
121
|
+
}
|
|
122
|
+
const existingIndex = update.index ?? -1;
|
|
123
|
+
let replaced = false;
|
|
124
|
+
if (existingIndex >= 0 && existingIndex < newTask.artifacts.length) {
|
|
125
|
+
const existingArtifact = newTask.artifacts[existingIndex];
|
|
126
|
+
if (update.append) {
|
|
127
|
+
const appendedArtifact = JSON.parse(JSON.stringify(existingArtifact));
|
|
128
|
+
appendedArtifact.parts.push(...update.parts);
|
|
129
|
+
if (update.metadata) {
|
|
130
|
+
appendedArtifact.metadata = {
|
|
131
|
+
...appendedArtifact.metadata || {},
|
|
132
|
+
...update.metadata
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (update.lastChunk !== void 0) appendedArtifact.lastChunk = update.lastChunk;
|
|
136
|
+
if (update.description) appendedArtifact.description = update.description;
|
|
137
|
+
newTask.artifacts[existingIndex] = appendedArtifact;
|
|
138
|
+
replaced = true;
|
|
139
|
+
} else {
|
|
140
|
+
newTask.artifacts[existingIndex] = { ...update };
|
|
141
|
+
replaced = true;
|
|
142
|
+
}
|
|
143
|
+
} else if (update.name) {
|
|
144
|
+
const namedIndex = newTask.artifacts.findIndex((a) => a.name === update.name);
|
|
145
|
+
if (namedIndex >= 0) {
|
|
146
|
+
newTask.artifacts[namedIndex] = { ...update };
|
|
147
|
+
replaced = true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!replaced) {
|
|
151
|
+
newTask.artifacts.push({ ...update });
|
|
152
|
+
if (newTask.artifacts.some((a) => a.index !== void 0)) {
|
|
153
|
+
newTask.artifacts.sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return { task: newTask, history: newHistory };
|
|
158
|
+
}
|
|
159
|
+
async function loadOrCreateTaskAndHistory({
|
|
160
|
+
agentId,
|
|
161
|
+
taskId,
|
|
162
|
+
taskStore,
|
|
163
|
+
message,
|
|
164
|
+
sessionId,
|
|
165
|
+
metadata,
|
|
166
|
+
logger
|
|
167
|
+
}) {
|
|
168
|
+
const data = await taskStore.load({ agentId, taskId });
|
|
169
|
+
if (!data) {
|
|
170
|
+
const initialTask = {
|
|
171
|
+
id: taskId,
|
|
172
|
+
sessionId,
|
|
173
|
+
status: {
|
|
174
|
+
state: "submitted",
|
|
175
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
176
|
+
message: null
|
|
177
|
+
},
|
|
178
|
+
artifacts: [],
|
|
179
|
+
metadata
|
|
180
|
+
};
|
|
181
|
+
const initialData = {
|
|
182
|
+
task: initialTask,
|
|
183
|
+
history: [message]
|
|
184
|
+
};
|
|
185
|
+
logger?.info(`[Task ${taskId}] Created new task and history.`);
|
|
186
|
+
await taskStore.save({ agentId, data: initialData });
|
|
187
|
+
return initialData;
|
|
188
|
+
}
|
|
189
|
+
logger?.info(`[Task ${taskId}] Loaded existing task and history.`);
|
|
190
|
+
let updatedData = {
|
|
191
|
+
task: data.task,
|
|
192
|
+
history: [...data.history, message]
|
|
193
|
+
};
|
|
194
|
+
const { status } = data.task;
|
|
195
|
+
const finalStates = ["completed", "failed", "canceled"];
|
|
196
|
+
if (finalStates.includes(status.state)) {
|
|
197
|
+
logger?.warn(`[Task ${taskId}] Received message for task in final state ${status.state}. Restarting.`);
|
|
198
|
+
updatedData = applyUpdateToTaskAndHistory(updatedData, {
|
|
199
|
+
state: "submitted",
|
|
200
|
+
message: null
|
|
201
|
+
});
|
|
202
|
+
} else if (status.state === "input-required") {
|
|
203
|
+
logger?.info(`[Task ${taskId}] Changing state from 'input-required' to 'working'.`);
|
|
204
|
+
updatedData = applyUpdateToTaskAndHistory(updatedData, { state: "working" });
|
|
205
|
+
} else if (status.state === "working") {
|
|
206
|
+
logger?.warn(`[Task ${taskId}] Received message while already 'working'. Proceeding.`);
|
|
207
|
+
}
|
|
208
|
+
await taskStore.save({ agentId, data: updatedData });
|
|
209
|
+
return {
|
|
210
|
+
task: { ...updatedData.task },
|
|
211
|
+
history: [...updatedData.history]
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function createTaskContext({
|
|
215
|
+
task,
|
|
216
|
+
userMessage,
|
|
217
|
+
history,
|
|
218
|
+
activeCancellations
|
|
219
|
+
}) {
|
|
220
|
+
return {
|
|
221
|
+
task: structuredClone(task),
|
|
222
|
+
userMessage,
|
|
223
|
+
history: structuredClone(history),
|
|
224
|
+
isCancelled: () => activeCancellations.has(task.id)
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/server/handlers/a2a.ts
|
|
229
|
+
var taskSendParamsSchema = z.object({
|
|
230
|
+
id: z.string().min(1, "Invalid or missing task ID (params.id)."),
|
|
231
|
+
message: z.object({
|
|
232
|
+
parts: z.array(
|
|
233
|
+
z.object({
|
|
234
|
+
type: z.enum(["text"]),
|
|
235
|
+
text: z.string()
|
|
236
|
+
})
|
|
237
|
+
)
|
|
238
|
+
})
|
|
239
|
+
});
|
|
240
|
+
async function getAgentCardByIdHandler({
|
|
241
|
+
mastra,
|
|
242
|
+
agentId,
|
|
243
|
+
executionUrl = `/a2a/${agentId}`,
|
|
244
|
+
provider = {
|
|
245
|
+
organization: "Mastra",
|
|
246
|
+
url: "https://mastra.ai"
|
|
247
|
+
},
|
|
248
|
+
version = "1.0",
|
|
249
|
+
runtimeContext
|
|
250
|
+
}) {
|
|
251
|
+
const agent = mastra.getAgent(agentId);
|
|
252
|
+
if (!agent) {
|
|
253
|
+
throw new Error(`Agent with ID ${agentId} not found`);
|
|
254
|
+
}
|
|
255
|
+
const [instructions, tools] = await Promise.all([
|
|
256
|
+
agent.getInstructions({ runtimeContext }),
|
|
257
|
+
agent.getTools({ runtimeContext })
|
|
258
|
+
]);
|
|
259
|
+
const agentCard = {
|
|
260
|
+
name: agent.id || agentId,
|
|
261
|
+
description: instructions,
|
|
262
|
+
url: executionUrl,
|
|
263
|
+
provider,
|
|
264
|
+
version,
|
|
265
|
+
capabilities: {
|
|
266
|
+
streaming: true,
|
|
267
|
+
// All agents support streaming
|
|
268
|
+
pushNotifications: false,
|
|
269
|
+
stateTransitionHistory: false
|
|
270
|
+
},
|
|
271
|
+
defaultInputModes: ["text"],
|
|
272
|
+
defaultOutputModes: ["text"],
|
|
273
|
+
// Convert agent tools to skills format for A2A protocol
|
|
274
|
+
skills: Object.entries(tools).map(([toolId, tool]) => ({
|
|
275
|
+
id: toolId,
|
|
276
|
+
name: toolId,
|
|
277
|
+
description: tool.description || `Tool: ${toolId}`,
|
|
278
|
+
// Optional fields
|
|
279
|
+
tags: ["tool"]
|
|
280
|
+
}))
|
|
281
|
+
};
|
|
282
|
+
return agentCard;
|
|
283
|
+
}
|
|
284
|
+
function validateTaskSendParams(params) {
|
|
285
|
+
try {
|
|
286
|
+
taskSendParamsSchema.parse(params);
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (error instanceof z.ZodError) {
|
|
289
|
+
throw A2AError.invalidParams(error.errors[0].message);
|
|
290
|
+
}
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async function handleTaskSend({
|
|
295
|
+
requestId,
|
|
296
|
+
params,
|
|
297
|
+
taskStore,
|
|
298
|
+
agent,
|
|
299
|
+
agentId,
|
|
300
|
+
logger,
|
|
301
|
+
runtimeContext
|
|
302
|
+
}) {
|
|
303
|
+
validateTaskSendParams(params);
|
|
304
|
+
const { id: taskId, message, sessionId, metadata } = params;
|
|
305
|
+
let currentData = await loadOrCreateTaskAndHistory({
|
|
306
|
+
taskId,
|
|
307
|
+
taskStore,
|
|
308
|
+
agentId,
|
|
309
|
+
message,
|
|
310
|
+
sessionId,
|
|
311
|
+
metadata
|
|
312
|
+
});
|
|
313
|
+
createTaskContext({
|
|
314
|
+
task: currentData.task,
|
|
315
|
+
userMessage: message,
|
|
316
|
+
history: currentData.history,
|
|
317
|
+
activeCancellations: taskStore.activeCancellations
|
|
318
|
+
});
|
|
319
|
+
try {
|
|
320
|
+
const { text } = await agent.generate([convertToCoreMessage(message)], {
|
|
321
|
+
runId: taskId,
|
|
322
|
+
runtimeContext
|
|
323
|
+
});
|
|
324
|
+
currentData = applyUpdateToTaskAndHistory(currentData, {
|
|
325
|
+
state: "completed",
|
|
326
|
+
message: {
|
|
327
|
+
role: "agent",
|
|
328
|
+
parts: [
|
|
329
|
+
{
|
|
330
|
+
type: "text",
|
|
331
|
+
text
|
|
332
|
+
}
|
|
333
|
+
]
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
await taskStore.save({ agentId, data: currentData });
|
|
337
|
+
} catch (handlerError) {
|
|
338
|
+
const failureStatusUpdate = {
|
|
339
|
+
state: "failed",
|
|
340
|
+
message: {
|
|
341
|
+
role: "agent",
|
|
342
|
+
parts: [
|
|
343
|
+
{
|
|
344
|
+
type: "text",
|
|
345
|
+
text: `Handler failed: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
currentData = applyUpdateToTaskAndHistory(currentData, failureStatusUpdate);
|
|
351
|
+
try {
|
|
352
|
+
await taskStore.save({ agentId, data: currentData });
|
|
353
|
+
} catch (saveError) {
|
|
354
|
+
logger?.error(`Failed to save task ${taskId} after handler error:`, saveError?.message);
|
|
355
|
+
}
|
|
356
|
+
return normalizeError(handlerError, requestId, taskId, logger);
|
|
357
|
+
}
|
|
358
|
+
return createSuccessResponse(requestId, currentData.task);
|
|
359
|
+
}
|
|
360
|
+
async function handleTaskGet({
|
|
361
|
+
requestId,
|
|
362
|
+
taskStore,
|
|
363
|
+
agentId,
|
|
364
|
+
taskId
|
|
365
|
+
}) {
|
|
366
|
+
const task = await taskStore.load({ agentId, taskId });
|
|
367
|
+
if (!task) {
|
|
368
|
+
throw A2AError.taskNotFound(taskId);
|
|
369
|
+
}
|
|
370
|
+
return createSuccessResponse(requestId, task);
|
|
371
|
+
}
|
|
372
|
+
async function* handleTaskSendSubscribe({
|
|
373
|
+
requestId,
|
|
374
|
+
params,
|
|
375
|
+
taskStore,
|
|
376
|
+
agent,
|
|
377
|
+
agentId,
|
|
378
|
+
logger,
|
|
379
|
+
runtimeContext
|
|
380
|
+
}) {
|
|
381
|
+
yield createSuccessResponse(requestId, {
|
|
382
|
+
state: "working",
|
|
383
|
+
message: {
|
|
384
|
+
role: "agent",
|
|
385
|
+
parts: [{ type: "text", text: "Generating response..." }]
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
let result;
|
|
389
|
+
try {
|
|
390
|
+
result = await handleTaskSend({
|
|
391
|
+
requestId,
|
|
392
|
+
params,
|
|
393
|
+
taskStore,
|
|
394
|
+
agent,
|
|
395
|
+
agentId,
|
|
396
|
+
runtimeContext,
|
|
397
|
+
logger
|
|
398
|
+
});
|
|
399
|
+
} catch (err) {
|
|
400
|
+
if (!(err instanceof A2AError)) {
|
|
401
|
+
throw err;
|
|
402
|
+
}
|
|
403
|
+
result = createErrorResponse(requestId, err.toJSONRPCError());
|
|
404
|
+
}
|
|
405
|
+
yield result;
|
|
406
|
+
}
|
|
407
|
+
async function handleTaskCancel({
|
|
408
|
+
requestId,
|
|
409
|
+
taskStore,
|
|
410
|
+
agentId,
|
|
411
|
+
taskId,
|
|
412
|
+
logger
|
|
413
|
+
}) {
|
|
414
|
+
let data = await taskStore.load({
|
|
415
|
+
agentId,
|
|
416
|
+
taskId
|
|
417
|
+
});
|
|
418
|
+
if (!data) {
|
|
419
|
+
throw A2AError.taskNotFound(taskId);
|
|
420
|
+
}
|
|
421
|
+
const finalStates = ["completed", "failed", "canceled"];
|
|
422
|
+
if (finalStates.includes(data.task.status.state)) {
|
|
423
|
+
logger?.info(`Task ${taskId} already in final state ${data.task.status.state}, cannot cancel.`);
|
|
424
|
+
return createSuccessResponse(requestId, data.task);
|
|
425
|
+
}
|
|
426
|
+
taskStore.activeCancellations.add(taskId);
|
|
427
|
+
const cancelUpdate = {
|
|
428
|
+
state: "canceled",
|
|
429
|
+
message: {
|
|
430
|
+
role: "agent",
|
|
431
|
+
parts: [{ type: "text", text: "Task cancelled by request." }]
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
data = applyUpdateToTaskAndHistory(data, cancelUpdate);
|
|
435
|
+
await taskStore.save({ agentId, data });
|
|
436
|
+
taskStore.activeCancellations.delete(taskId);
|
|
437
|
+
return createSuccessResponse(requestId, data.task);
|
|
438
|
+
}
|
|
439
|
+
async function getAgentExecutionHandler({
|
|
440
|
+
requestId,
|
|
441
|
+
mastra,
|
|
442
|
+
agentId,
|
|
443
|
+
runtimeContext,
|
|
444
|
+
method,
|
|
445
|
+
params,
|
|
446
|
+
taskStore = new InMemoryTaskStore(),
|
|
447
|
+
logger
|
|
448
|
+
}) {
|
|
449
|
+
const agent = mastra.getAgent(agentId);
|
|
450
|
+
let taskId;
|
|
451
|
+
try {
|
|
452
|
+
taskId = params.id;
|
|
453
|
+
switch (method) {
|
|
454
|
+
case "tasks/send": {
|
|
455
|
+
const result2 = await handleTaskSend({
|
|
456
|
+
requestId,
|
|
457
|
+
params,
|
|
458
|
+
taskStore,
|
|
459
|
+
agent,
|
|
460
|
+
agentId,
|
|
461
|
+
runtimeContext
|
|
462
|
+
});
|
|
463
|
+
return result2;
|
|
464
|
+
}
|
|
465
|
+
case "tasks/sendSubscribe":
|
|
466
|
+
const result = await handleTaskSendSubscribe({
|
|
467
|
+
requestId,
|
|
468
|
+
taskStore,
|
|
469
|
+
params,
|
|
470
|
+
agent,
|
|
471
|
+
agentId,
|
|
472
|
+
runtimeContext
|
|
473
|
+
});
|
|
474
|
+
return result;
|
|
475
|
+
case "tasks/get": {
|
|
476
|
+
const result2 = await handleTaskGet({
|
|
477
|
+
requestId,
|
|
478
|
+
taskStore,
|
|
479
|
+
agentId,
|
|
480
|
+
taskId
|
|
481
|
+
});
|
|
482
|
+
return result2;
|
|
483
|
+
}
|
|
484
|
+
case "tasks/cancel": {
|
|
485
|
+
const result2 = await handleTaskCancel({
|
|
486
|
+
requestId,
|
|
487
|
+
taskStore,
|
|
488
|
+
agentId,
|
|
489
|
+
taskId
|
|
490
|
+
});
|
|
491
|
+
return result2;
|
|
492
|
+
}
|
|
493
|
+
default:
|
|
494
|
+
throw A2AError.methodNotFound(method);
|
|
495
|
+
}
|
|
496
|
+
} catch (error) {
|
|
497
|
+
if (error instanceof A2AError && taskId && !error.taskId) {
|
|
498
|
+
error.taskId = taskId;
|
|
499
|
+
}
|
|
500
|
+
return normalizeError(error, requestId, taskId, logger);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
export { a2a_exports, getAgentCardByIdHandler, getAgentExecutionHandler, handleTaskCancel, handleTaskGet, handleTaskSend, handleTaskSendSubscribe };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { stringify, esm_default } from './chunk-
|
|
2
|
-
import { validateBody } from './chunk-
|
|
3
|
-
import { handleError } from './chunk-
|
|
4
|
-
import {
|
|
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';
|
|
5
7
|
|
|
6
8
|
// src/server/handlers/agents.ts
|
|
7
9
|
var agents_exports = {};
|
|
@@ -30,13 +32,33 @@ async function getAgentsHandler({ mastra, runtimeContext }) {
|
|
|
30
32
|
};
|
|
31
33
|
return acc;
|
|
32
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
|
+
}
|
|
33
52
|
return {
|
|
34
53
|
id,
|
|
35
54
|
name: agent.name,
|
|
36
55
|
instructions,
|
|
37
56
|
tools: serializedAgentTools,
|
|
57
|
+
workflows: serializedAgentWorkflows,
|
|
38
58
|
provider: llm?.getProvider(),
|
|
39
|
-
modelId: llm?.getModelId()
|
|
59
|
+
modelId: llm?.getModelId(),
|
|
60
|
+
defaultGenerateOptions: agent.getDefaultGenerateOptions(),
|
|
61
|
+
defaultStreamOptions: agent.getDefaultStreamOptions()
|
|
40
62
|
};
|
|
41
63
|
})
|
|
42
64
|
);
|
|
@@ -69,14 +91,34 @@ async function getAgentByIdHandler({
|
|
|
69
91
|
};
|
|
70
92
|
return acc;
|
|
71
93
|
}, {});
|
|
94
|
+
let serializedAgentWorkflows = {};
|
|
95
|
+
if ("getWorkflows" in agent) {
|
|
96
|
+
const logger = mastra.getLogger();
|
|
97
|
+
try {
|
|
98
|
+
const workflows = await agent.getWorkflows({ runtimeContext });
|
|
99
|
+
serializedAgentWorkflows = Object.entries(workflows || {}).reduce((acc, [key, workflow]) => {
|
|
100
|
+
return {
|
|
101
|
+
...acc,
|
|
102
|
+
[key]: {
|
|
103
|
+
name: workflow.name
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}, {});
|
|
107
|
+
} catch (error) {
|
|
108
|
+
logger.error("Error getting workflows for agent", { agentName: agent.name, error });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
72
111
|
const instructions = await agent.getInstructions({ runtimeContext });
|
|
73
112
|
const llm = await agent.getLLM({ runtimeContext });
|
|
74
113
|
return {
|
|
75
114
|
name: agent.name,
|
|
76
115
|
instructions,
|
|
77
116
|
tools: serializedAgentTools,
|
|
117
|
+
workflows: serializedAgentWorkflows,
|
|
78
118
|
provider: llm?.getProvider(),
|
|
79
|
-
modelId: llm?.getModelId()
|
|
119
|
+
modelId: llm?.getModelId(),
|
|
120
|
+
defaultGenerateOptions: agent.getDefaultGenerateOptions(),
|
|
121
|
+
defaultStreamOptions: agent.getDefaultStreamOptions()
|
|
80
122
|
};
|
|
81
123
|
} catch (error) {
|
|
82
124
|
return handleError(error, "Error getting agent");
|
|
@@ -131,14 +173,18 @@ async function generateHandler({
|
|
|
131
173
|
if (!agent) {
|
|
132
174
|
throw new HTTPException(404, { message: "Agent not found" });
|
|
133
175
|
}
|
|
134
|
-
const { messages, resourceId, resourceid, ...rest } = body;
|
|
176
|
+
const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
|
|
135
177
|
const finalResourceId = resourceId ?? resourceid;
|
|
178
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
179
|
+
...Array.from(runtimeContext.entries()),
|
|
180
|
+
...Array.from(Object.entries(agentRuntimeContext ?? {}))
|
|
181
|
+
]);
|
|
136
182
|
validateBody({ messages });
|
|
137
183
|
const result = await agent.generate(messages, {
|
|
138
184
|
...rest,
|
|
139
185
|
// @ts-expect-error TODO fix types
|
|
140
186
|
resourceId: finalResourceId,
|
|
141
|
-
runtimeContext
|
|
187
|
+
runtimeContext: finalRuntimeContext
|
|
142
188
|
});
|
|
143
189
|
return result;
|
|
144
190
|
} catch (error) {
|
|
@@ -156,14 +202,18 @@ async function streamGenerateHandler({
|
|
|
156
202
|
if (!agent) {
|
|
157
203
|
throw new HTTPException(404, { message: "Agent not found" });
|
|
158
204
|
}
|
|
159
|
-
const { messages, resourceId, resourceid, ...rest } = body;
|
|
205
|
+
const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
|
|
160
206
|
const finalResourceId = resourceId ?? resourceid;
|
|
207
|
+
const finalRuntimeContext = new RuntimeContext([
|
|
208
|
+
...Array.from(runtimeContext.entries()),
|
|
209
|
+
...Array.from(Object.entries(agentRuntimeContext ?? {}))
|
|
210
|
+
]);
|
|
161
211
|
validateBody({ messages });
|
|
162
212
|
const streamResult = await agent.stream(messages, {
|
|
163
213
|
...rest,
|
|
164
214
|
// @ts-expect-error TODO fix types
|
|
165
215
|
resourceId: finalResourceId,
|
|
166
|
-
runtimeContext
|
|
216
|
+
runtimeContext: finalRuntimeContext
|
|
167
217
|
});
|
|
168
218
|
const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
|
|
169
219
|
sendUsage: true,
|