@mastra/server 2.0.3 → 2.0.4-alpha.1
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/dist/_tsup-dts-rollup.d.cts +152 -10
- package/dist/_tsup-dts-rollup.d.ts +152 -10
- package/dist/{chunk-2FJURXCL.cjs → chunk-24EGIVT7.cjs} +34 -33
- package/dist/{chunk-6Q7UXAYJ.cjs → chunk-3HQNCTZ2.cjs} +21 -20
- package/dist/{chunk-M56ECCHK.cjs → chunk-4YZ3U35L.cjs} +18 -17
- package/dist/{chunk-QN4KF3BH.cjs → chunk-57CJTIPW.cjs} +2 -2
- package/dist/chunk-5SN4U5AC.cjs +508 -0
- package/dist/{chunk-ZLBRQFDD.cjs → chunk-64U3UDTH.cjs} +2 -2
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/{chunk-QJ3AHN64.js → chunk-C7564HUT.js} +4 -3
- package/dist/{chunk-3XTEV33Q.js → chunk-EJO45KYT.js} +19 -16
- package/dist/{chunk-L7XE5QTW.js → chunk-H5PTF3Y4.js} +1 -1
- package/dist/{chunk-Q6SHQECN.js → chunk-HFWCEP5S.js} +4 -3
- package/dist/{chunk-3EJZQ6TQ.js → chunk-HWZVAG3H.js} +3 -3
- package/dist/{chunk-CHFORQ7J.cjs → chunk-I2B73Y4I.cjs} +60 -57
- package/dist/{chunk-4JINXASC.js → chunk-IU5VO2I2.js} +3 -2
- package/dist/chunk-LIVAK2DM.js +2001 -0
- package/dist/{chunk-3AHQ5RGN.js → chunk-M5ABIP7D.js} +1 -1
- package/dist/{chunk-M2RXDCPV.cjs → chunk-M6MC2QMH.cjs} +78 -59
- package/dist/{chunk-AMVOS7YB.cjs → chunk-MHKNLNAN.cjs} +9 -8
- package/dist/chunk-MLKGABMK.js +7 -0
- package/dist/{chunk-TRDNDNGQ.js → chunk-NYN7KFXL.js} +1 -7
- package/dist/{chunk-FV45V6WC.cjs → chunk-OCWPVYNI.cjs} +0 -7
- package/dist/{chunk-SKBVVI24.cjs → chunk-OGCNNUHF.cjs} +9 -9
- package/dist/{chunk-BPL2CBLV.js → chunk-OR3CIE2H.js} +3 -2
- package/dist/chunk-P6SCPDYW.js +500 -0
- package/dist/{chunk-GVBJ5I2S.js → chunk-Q6KMBIAN.js} +5 -4
- package/dist/{chunk-5JNVY6DU.js → chunk-TJKLBTFB.js} +20 -8
- package/dist/{chunk-5YGDYMRB.cjs → chunk-VPNDC2DI.cjs} +14 -13
- package/dist/{chunk-OWNA6I2H.js → chunk-WJU67W7F.js} +30 -11
- package/dist/{chunk-RBQASTUP.js → chunk-X3ZDCS52.js} +4 -3
- package/dist/{chunk-55HTWX4C.cjs → chunk-YBVOQN4M.cjs} +20 -19
- 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/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/vNextWorkflows.cjs +11 -11
- package/dist/server/handlers/vNextWorkflows.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 +4 -4
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers/workflows.cjs +11 -11
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +28 -23
- package/dist/server/handlers.d.cts +1 -0
- package/dist/server/handlers.d.ts +1 -0
- package/dist/server/handlers.js +11 -10
- package/package.json +5 -3
- package/dist/chunk-5SWCVTNL.cjs +0 -5579
- package/dist/chunk-OMN3UI6X.js +0 -5576
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
|
|
4
|
+
var a2a = require('@mastra/core/a2a');
|
|
5
|
+
var zod = require('zod');
|
|
6
|
+
|
|
7
|
+
// src/server/handlers/a2a.ts
|
|
8
|
+
var a2a_exports = {};
|
|
9
|
+
chunk75ZPJI57_cjs.__export(a2a_exports, {
|
|
10
|
+
getAgentCardByIdHandler: () => getAgentCardByIdHandler,
|
|
11
|
+
getAgentExecutionHandler: () => getAgentExecutionHandler,
|
|
12
|
+
handleTaskCancel: () => handleTaskCancel,
|
|
13
|
+
handleTaskGet: () => handleTaskGet,
|
|
14
|
+
handleTaskSend: () => handleTaskSend,
|
|
15
|
+
handleTaskSendSubscribe: () => handleTaskSendSubscribe
|
|
16
|
+
});
|
|
17
|
+
function normalizeError(error, reqId, taskId, logger) {
|
|
18
|
+
let a2aError;
|
|
19
|
+
if (error instanceof a2a.A2AError) {
|
|
20
|
+
a2aError = error;
|
|
21
|
+
} else if (error instanceof Error) {
|
|
22
|
+
a2aError = a2a.A2AError.internalError(error.message, { stack: error.stack });
|
|
23
|
+
} else {
|
|
24
|
+
a2aError = a2a.A2AError.internalError("An unknown error occurred.", error);
|
|
25
|
+
}
|
|
26
|
+
if (taskId && !a2aError.taskId) {
|
|
27
|
+
a2aError.taskId = taskId;
|
|
28
|
+
}
|
|
29
|
+
logger?.error(`Error processing request (Task: ${a2aError.taskId ?? "N/A"}, ReqID: ${reqId ?? "N/A"}):`, a2aError);
|
|
30
|
+
return createErrorResponse(reqId, a2aError.toJSONRPCError());
|
|
31
|
+
}
|
|
32
|
+
function createErrorResponse(id, error) {
|
|
33
|
+
return {
|
|
34
|
+
jsonrpc: "2.0",
|
|
35
|
+
id,
|
|
36
|
+
// Can be null if request ID was invalid/missing
|
|
37
|
+
error
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function createSuccessResponse(id, result) {
|
|
41
|
+
if (!id) {
|
|
42
|
+
throw a2a.A2AError.internalError("Cannot create success response for null ID.");
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
jsonrpc: "2.0",
|
|
46
|
+
id,
|
|
47
|
+
result
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function convertToCoreMessage(message) {
|
|
51
|
+
return {
|
|
52
|
+
role: message.role === "user" ? "user" : "assistant",
|
|
53
|
+
content: message.parts.map((msg) => convertToCoreMessagePart(msg))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function convertToCoreMessagePart(part) {
|
|
57
|
+
switch (part.type) {
|
|
58
|
+
case "text":
|
|
59
|
+
return {
|
|
60
|
+
type: "text",
|
|
61
|
+
text: part.text
|
|
62
|
+
};
|
|
63
|
+
case "file":
|
|
64
|
+
return {
|
|
65
|
+
type: "file",
|
|
66
|
+
data: new URL(part.file.uri),
|
|
67
|
+
mimeType: part.file.mimeType
|
|
68
|
+
};
|
|
69
|
+
case "data":
|
|
70
|
+
throw new Error("Data parts are not supported in core messages");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/server/a2a/store.ts
|
|
75
|
+
var InMemoryTaskStore = class {
|
|
76
|
+
store = /* @__PURE__ */ new Map();
|
|
77
|
+
activeCancellations = /* @__PURE__ */ new Set();
|
|
78
|
+
async load({ agentId, taskId }) {
|
|
79
|
+
const entry = this.store.get(`${agentId}-${taskId}`);
|
|
80
|
+
if (!entry) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return { task: { ...entry.task }, history: [...entry.history] };
|
|
84
|
+
}
|
|
85
|
+
async save({ agentId, data }) {
|
|
86
|
+
const key = `${agentId}-${data.task.id}`;
|
|
87
|
+
if (!data.task.id) {
|
|
88
|
+
throw new Error("Task ID is required");
|
|
89
|
+
}
|
|
90
|
+
this.store.set(key, {
|
|
91
|
+
task: { ...data.task },
|
|
92
|
+
history: [...data.history]
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// src/server/a2a/tasks.ts
|
|
98
|
+
function isTaskStatusUpdate(update) {
|
|
99
|
+
return "state" in update && !("parts" in update);
|
|
100
|
+
}
|
|
101
|
+
function isArtifactUpdate(update) {
|
|
102
|
+
return "parts" in update;
|
|
103
|
+
}
|
|
104
|
+
function applyUpdateToTaskAndHistory(current, update) {
|
|
105
|
+
let newTask = structuredClone(current.task);
|
|
106
|
+
let newHistory = structuredClone(current.history);
|
|
107
|
+
if (isTaskStatusUpdate(update)) {
|
|
108
|
+
newTask.status = {
|
|
109
|
+
...newTask.status,
|
|
110
|
+
// Keep existing properties if not overwritten
|
|
111
|
+
...update,
|
|
112
|
+
// Apply updates
|
|
113
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
114
|
+
};
|
|
115
|
+
if (update.message?.role === "agent") {
|
|
116
|
+
newHistory.push(update.message);
|
|
117
|
+
}
|
|
118
|
+
} else if (isArtifactUpdate(update)) {
|
|
119
|
+
if (!newTask.artifacts) {
|
|
120
|
+
newTask.artifacts = [];
|
|
121
|
+
} else {
|
|
122
|
+
newTask.artifacts = [...newTask.artifacts];
|
|
123
|
+
}
|
|
124
|
+
const existingIndex = update.index ?? -1;
|
|
125
|
+
let replaced = false;
|
|
126
|
+
if (existingIndex >= 0 && existingIndex < newTask.artifacts.length) {
|
|
127
|
+
const existingArtifact = newTask.artifacts[existingIndex];
|
|
128
|
+
if (update.append) {
|
|
129
|
+
const appendedArtifact = JSON.parse(JSON.stringify(existingArtifact));
|
|
130
|
+
appendedArtifact.parts.push(...update.parts);
|
|
131
|
+
if (update.metadata) {
|
|
132
|
+
appendedArtifact.metadata = {
|
|
133
|
+
...appendedArtifact.metadata || {},
|
|
134
|
+
...update.metadata
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (update.lastChunk !== void 0) appendedArtifact.lastChunk = update.lastChunk;
|
|
138
|
+
if (update.description) appendedArtifact.description = update.description;
|
|
139
|
+
newTask.artifacts[existingIndex] = appendedArtifact;
|
|
140
|
+
replaced = true;
|
|
141
|
+
} else {
|
|
142
|
+
newTask.artifacts[existingIndex] = { ...update };
|
|
143
|
+
replaced = true;
|
|
144
|
+
}
|
|
145
|
+
} else if (update.name) {
|
|
146
|
+
const namedIndex = newTask.artifacts.findIndex((a) => a.name === update.name);
|
|
147
|
+
if (namedIndex >= 0) {
|
|
148
|
+
newTask.artifacts[namedIndex] = { ...update };
|
|
149
|
+
replaced = true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!replaced) {
|
|
153
|
+
newTask.artifacts.push({ ...update });
|
|
154
|
+
if (newTask.artifacts.some((a) => a.index !== void 0)) {
|
|
155
|
+
newTask.artifacts.sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return { task: newTask, history: newHistory };
|
|
160
|
+
}
|
|
161
|
+
async function loadOrCreateTaskAndHistory({
|
|
162
|
+
agentId,
|
|
163
|
+
taskId,
|
|
164
|
+
taskStore,
|
|
165
|
+
message,
|
|
166
|
+
sessionId,
|
|
167
|
+
metadata,
|
|
168
|
+
logger
|
|
169
|
+
}) {
|
|
170
|
+
const data = await taskStore.load({ agentId, taskId });
|
|
171
|
+
if (!data) {
|
|
172
|
+
const initialTask = {
|
|
173
|
+
id: taskId,
|
|
174
|
+
sessionId,
|
|
175
|
+
status: {
|
|
176
|
+
state: "submitted",
|
|
177
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
178
|
+
message: null
|
|
179
|
+
},
|
|
180
|
+
artifacts: [],
|
|
181
|
+
metadata
|
|
182
|
+
};
|
|
183
|
+
const initialData = {
|
|
184
|
+
task: initialTask,
|
|
185
|
+
history: [message]
|
|
186
|
+
};
|
|
187
|
+
logger?.info(`[Task ${taskId}] Created new task and history.`);
|
|
188
|
+
await taskStore.save({ agentId, data: initialData });
|
|
189
|
+
return initialData;
|
|
190
|
+
}
|
|
191
|
+
logger?.info(`[Task ${taskId}] Loaded existing task and history.`);
|
|
192
|
+
let updatedData = {
|
|
193
|
+
task: data.task,
|
|
194
|
+
history: [...data.history, message]
|
|
195
|
+
};
|
|
196
|
+
const { status } = data.task;
|
|
197
|
+
const finalStates = ["completed", "failed", "canceled"];
|
|
198
|
+
if (finalStates.includes(status.state)) {
|
|
199
|
+
logger?.warn(`[Task ${taskId}] Received message for task in final state ${status.state}. Restarting.`);
|
|
200
|
+
updatedData = applyUpdateToTaskAndHistory(updatedData, {
|
|
201
|
+
state: "submitted",
|
|
202
|
+
message: null
|
|
203
|
+
});
|
|
204
|
+
} else if (status.state === "input-required") {
|
|
205
|
+
logger?.info(`[Task ${taskId}] Changing state from 'input-required' to 'working'.`);
|
|
206
|
+
updatedData = applyUpdateToTaskAndHistory(updatedData, { state: "working" });
|
|
207
|
+
} else if (status.state === "working") {
|
|
208
|
+
logger?.warn(`[Task ${taskId}] Received message while already 'working'. Proceeding.`);
|
|
209
|
+
}
|
|
210
|
+
await taskStore.save({ agentId, data: updatedData });
|
|
211
|
+
return {
|
|
212
|
+
task: { ...updatedData.task },
|
|
213
|
+
history: [...updatedData.history]
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function createTaskContext({
|
|
217
|
+
task,
|
|
218
|
+
userMessage,
|
|
219
|
+
history,
|
|
220
|
+
activeCancellations
|
|
221
|
+
}) {
|
|
222
|
+
return {
|
|
223
|
+
task: structuredClone(task),
|
|
224
|
+
userMessage,
|
|
225
|
+
history: structuredClone(history),
|
|
226
|
+
isCancelled: () => activeCancellations.has(task.id)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// src/server/handlers/a2a.ts
|
|
231
|
+
var taskSendParamsSchema = zod.z.object({
|
|
232
|
+
id: zod.z.string().min(1, "Invalid or missing task ID (params.id)."),
|
|
233
|
+
message: zod.z.object({
|
|
234
|
+
parts: zod.z.array(
|
|
235
|
+
zod.z.object({
|
|
236
|
+
type: zod.z.enum(["text"]),
|
|
237
|
+
text: zod.z.string()
|
|
238
|
+
})
|
|
239
|
+
)
|
|
240
|
+
})
|
|
241
|
+
});
|
|
242
|
+
async function getAgentCardByIdHandler({
|
|
243
|
+
mastra,
|
|
244
|
+
agentId,
|
|
245
|
+
executionUrl = `/a2a/${agentId}`,
|
|
246
|
+
provider = {
|
|
247
|
+
organization: "Mastra",
|
|
248
|
+
url: "https://mastra.ai"
|
|
249
|
+
},
|
|
250
|
+
version = "1.0",
|
|
251
|
+
runtimeContext
|
|
252
|
+
}) {
|
|
253
|
+
const agent = mastra.getAgent(agentId);
|
|
254
|
+
if (!agent) {
|
|
255
|
+
throw new Error(`Agent with ID ${agentId} not found`);
|
|
256
|
+
}
|
|
257
|
+
const [instructions, tools] = await Promise.all([
|
|
258
|
+
agent.getInstructions({ runtimeContext }),
|
|
259
|
+
agent.getTools({ runtimeContext })
|
|
260
|
+
]);
|
|
261
|
+
const agentCard = {
|
|
262
|
+
name: agent.id || agentId,
|
|
263
|
+
description: instructions,
|
|
264
|
+
url: executionUrl,
|
|
265
|
+
provider,
|
|
266
|
+
version,
|
|
267
|
+
capabilities: {
|
|
268
|
+
streaming: true,
|
|
269
|
+
// All agents support streaming
|
|
270
|
+
pushNotifications: false,
|
|
271
|
+
stateTransitionHistory: false
|
|
272
|
+
},
|
|
273
|
+
defaultInputModes: ["text"],
|
|
274
|
+
defaultOutputModes: ["text"],
|
|
275
|
+
// Convert agent tools to skills format for A2A protocol
|
|
276
|
+
skills: Object.entries(tools).map(([toolId, tool]) => ({
|
|
277
|
+
id: toolId,
|
|
278
|
+
name: toolId,
|
|
279
|
+
description: tool.description || `Tool: ${toolId}`,
|
|
280
|
+
// Optional fields
|
|
281
|
+
tags: ["tool"]
|
|
282
|
+
}))
|
|
283
|
+
};
|
|
284
|
+
return agentCard;
|
|
285
|
+
}
|
|
286
|
+
function validateTaskSendParams(params) {
|
|
287
|
+
try {
|
|
288
|
+
taskSendParamsSchema.parse(params);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error instanceof zod.z.ZodError) {
|
|
291
|
+
throw a2a.A2AError.invalidParams(error.errors[0].message);
|
|
292
|
+
}
|
|
293
|
+
throw error;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
async function handleTaskSend({
|
|
297
|
+
requestId,
|
|
298
|
+
params,
|
|
299
|
+
taskStore,
|
|
300
|
+
agent,
|
|
301
|
+
logger,
|
|
302
|
+
runtimeContext
|
|
303
|
+
}) {
|
|
304
|
+
validateTaskSendParams(params);
|
|
305
|
+
const agentId = agent.id;
|
|
306
|
+
const { id: taskId, message, sessionId, metadata } = params;
|
|
307
|
+
let currentData = await loadOrCreateTaskAndHistory({
|
|
308
|
+
taskId,
|
|
309
|
+
taskStore,
|
|
310
|
+
agentId,
|
|
311
|
+
message,
|
|
312
|
+
sessionId,
|
|
313
|
+
metadata
|
|
314
|
+
});
|
|
315
|
+
createTaskContext({
|
|
316
|
+
task: currentData.task,
|
|
317
|
+
userMessage: message,
|
|
318
|
+
history: currentData.history,
|
|
319
|
+
activeCancellations: taskStore.activeCancellations
|
|
320
|
+
});
|
|
321
|
+
try {
|
|
322
|
+
const { text } = await agent.generate([convertToCoreMessage(message)], {
|
|
323
|
+
runId: taskId,
|
|
324
|
+
runtimeContext
|
|
325
|
+
});
|
|
326
|
+
currentData = applyUpdateToTaskAndHistory(currentData, {
|
|
327
|
+
state: "completed",
|
|
328
|
+
message: {
|
|
329
|
+
role: "agent",
|
|
330
|
+
parts: [
|
|
331
|
+
{
|
|
332
|
+
type: "text",
|
|
333
|
+
text
|
|
334
|
+
}
|
|
335
|
+
]
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
await taskStore.save({ agentId, data: currentData });
|
|
339
|
+
} catch (handlerError) {
|
|
340
|
+
const failureStatusUpdate = {
|
|
341
|
+
state: "failed",
|
|
342
|
+
message: {
|
|
343
|
+
role: "agent",
|
|
344
|
+
parts: [
|
|
345
|
+
{
|
|
346
|
+
type: "text",
|
|
347
|
+
text: `Handler failed: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`
|
|
348
|
+
}
|
|
349
|
+
]
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
currentData = applyUpdateToTaskAndHistory(currentData, failureStatusUpdate);
|
|
353
|
+
try {
|
|
354
|
+
await taskStore.save({ agentId, data: currentData });
|
|
355
|
+
} catch (saveError) {
|
|
356
|
+
logger?.error(`Failed to save task ${taskId} after handler error:`, saveError?.message);
|
|
357
|
+
}
|
|
358
|
+
return normalizeError(handlerError, requestId, taskId, logger);
|
|
359
|
+
}
|
|
360
|
+
return createSuccessResponse(requestId, currentData.task);
|
|
361
|
+
}
|
|
362
|
+
async function handleTaskGet({
|
|
363
|
+
requestId,
|
|
364
|
+
taskStore,
|
|
365
|
+
agentId,
|
|
366
|
+
taskId
|
|
367
|
+
}) {
|
|
368
|
+
const task = await taskStore.load({ agentId, taskId });
|
|
369
|
+
if (!task) {
|
|
370
|
+
throw a2a.A2AError.taskNotFound(taskId);
|
|
371
|
+
}
|
|
372
|
+
return createSuccessResponse(requestId, task);
|
|
373
|
+
}
|
|
374
|
+
async function* handleTaskSendSubscribe({
|
|
375
|
+
requestId,
|
|
376
|
+
params,
|
|
377
|
+
taskStore,
|
|
378
|
+
agent,
|
|
379
|
+
logger,
|
|
380
|
+
runtimeContext
|
|
381
|
+
}) {
|
|
382
|
+
yield createSuccessResponse(requestId, {
|
|
383
|
+
state: "working",
|
|
384
|
+
message: {
|
|
385
|
+
role: "agent",
|
|
386
|
+
parts: [{ type: "text", text: "Generating response..." }]
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
let result;
|
|
390
|
+
try {
|
|
391
|
+
result = await handleTaskSend({
|
|
392
|
+
requestId,
|
|
393
|
+
params,
|
|
394
|
+
taskStore,
|
|
395
|
+
agent,
|
|
396
|
+
runtimeContext,
|
|
397
|
+
logger
|
|
398
|
+
});
|
|
399
|
+
} catch (err) {
|
|
400
|
+
if (!(err instanceof a2a.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 a2a.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
|
+
runtimeContext
|
|
461
|
+
});
|
|
462
|
+
return result2;
|
|
463
|
+
}
|
|
464
|
+
case "tasks/sendSubscribe":
|
|
465
|
+
const result = await handleTaskSendSubscribe({
|
|
466
|
+
requestId,
|
|
467
|
+
taskStore,
|
|
468
|
+
params,
|
|
469
|
+
agent,
|
|
470
|
+
runtimeContext
|
|
471
|
+
});
|
|
472
|
+
return result;
|
|
473
|
+
case "tasks/get": {
|
|
474
|
+
const result2 = await handleTaskGet({
|
|
475
|
+
requestId,
|
|
476
|
+
taskStore,
|
|
477
|
+
agentId,
|
|
478
|
+
taskId
|
|
479
|
+
});
|
|
480
|
+
return result2;
|
|
481
|
+
}
|
|
482
|
+
case "tasks/cancel": {
|
|
483
|
+
const result2 = await handleTaskCancel({
|
|
484
|
+
requestId,
|
|
485
|
+
taskStore,
|
|
486
|
+
agentId,
|
|
487
|
+
taskId
|
|
488
|
+
});
|
|
489
|
+
return result2;
|
|
490
|
+
}
|
|
491
|
+
default:
|
|
492
|
+
throw a2a.A2AError.methodNotFound(method);
|
|
493
|
+
}
|
|
494
|
+
} catch (error) {
|
|
495
|
+
if (error instanceof a2a.A2AError && taskId && !error.taskId) {
|
|
496
|
+
error.taskId = taskId;
|
|
497
|
+
}
|
|
498
|
+
return normalizeError(error, requestId, taskId, logger);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
exports.a2a_exports = a2a_exports;
|
|
503
|
+
exports.getAgentCardByIdHandler = getAgentCardByIdHandler;
|
|
504
|
+
exports.getAgentExecutionHandler = getAgentExecutionHandler;
|
|
505
|
+
exports.handleTaskCancel = handleTaskCancel;
|
|
506
|
+
exports.handleTaskGet = handleTaskGet;
|
|
507
|
+
exports.handleTaskSend = handleTaskSend;
|
|
508
|
+
exports.handleTaskSendSubscribe = handleTaskSendSubscribe;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
|
|
4
4
|
|
|
5
5
|
// src/server/handlers/error.ts
|
|
6
6
|
function handleError(error, defaultMessage) {
|
|
7
7
|
const apiError = error;
|
|
8
|
-
throw new
|
|
8
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(apiError.status || 500, {
|
|
9
9
|
message: apiError.message || defaultMessage
|
|
10
10
|
});
|
|
11
11
|
}
|
|
@@ -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/network.ts
|
|
6
7
|
var network_exports = {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { stringify, esm_default } from './chunk-
|
|
2
|
-
import { handleError } from './chunk-
|
|
3
|
-
import {
|
|
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
7
|
// src/server/handlers/workflows.ts
|
|
@@ -96,14 +97,14 @@ async function startAsyncWorkflowHandler({
|
|
|
96
97
|
throw new HTTPException(404, { message: "Workflow not found" });
|
|
97
98
|
}
|
|
98
99
|
if (!runId) {
|
|
99
|
-
const
|
|
100
|
-
const result2 = await start({
|
|
100
|
+
const newRun = workflow.createRun();
|
|
101
|
+
const result2 = await newRun.start({
|
|
101
102
|
triggerData,
|
|
102
103
|
runtimeContext
|
|
103
104
|
});
|
|
104
105
|
return result2;
|
|
105
106
|
}
|
|
106
|
-
const run = workflow.
|
|
107
|
+
const run = workflow.getMemoryRun(runId);
|
|
107
108
|
if (!run) {
|
|
108
109
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
109
110
|
}
|
|
@@ -132,7 +133,7 @@ async function getWorkflowRunHandler({
|
|
|
132
133
|
if (!workflow) {
|
|
133
134
|
throw new HTTPException(404, { message: "Workflow not found" });
|
|
134
135
|
}
|
|
135
|
-
const run = workflow.getRun(runId);
|
|
136
|
+
const run = await workflow.getRun(runId);
|
|
136
137
|
if (!run) {
|
|
137
138
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
138
139
|
}
|
|
@@ -154,8 +155,8 @@ async function createRunHandler({
|
|
|
154
155
|
if (!workflow) {
|
|
155
156
|
throw new HTTPException(404, { message: "Workflow not found" });
|
|
156
157
|
}
|
|
157
|
-
const
|
|
158
|
-
return { runId };
|
|
158
|
+
const newRun = workflow.createRun({ runId: prevRunId });
|
|
159
|
+
return { runId: newRun.runId };
|
|
159
160
|
} catch (error) {
|
|
160
161
|
throw new HTTPException(500, { message: error?.message || "Error creating workflow run" });
|
|
161
162
|
}
|
|
@@ -175,11 +176,11 @@ async function startWorkflowRunHandler({
|
|
|
175
176
|
throw new HTTPException(400, { message: "runId required to start run" });
|
|
176
177
|
}
|
|
177
178
|
const workflow = mastra.getWorkflow(workflowId);
|
|
178
|
-
const run = workflow.
|
|
179
|
+
const run = workflow.getMemoryRun(runId);
|
|
179
180
|
if (!run) {
|
|
180
181
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
181
182
|
}
|
|
182
|
-
|
|
183
|
+
void run.start({
|
|
183
184
|
triggerData,
|
|
184
185
|
runtimeContext
|
|
185
186
|
});
|
|
@@ -201,7 +202,7 @@ async function watchWorkflowHandler({
|
|
|
201
202
|
throw new HTTPException(400, { message: "runId required to watch workflow" });
|
|
202
203
|
}
|
|
203
204
|
const workflow = mastra.getWorkflow(workflowId);
|
|
204
|
-
const run = workflow.
|
|
205
|
+
const run = workflow.getMemoryRun(runId);
|
|
205
206
|
if (!run) {
|
|
206
207
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
207
208
|
}
|
|
@@ -217,8 +218,10 @@ async function watchWorkflowHandler({
|
|
|
217
218
|
asyncRef = null;
|
|
218
219
|
}
|
|
219
220
|
asyncRef = setImmediate(() => {
|
|
220
|
-
|
|
221
|
+
const runDone = Object.values(activePathsObj).every((value) => value.status !== "executing");
|
|
222
|
+
if (runDone) {
|
|
221
223
|
controller.close();
|
|
224
|
+
unwatch?.();
|
|
222
225
|
}
|
|
223
226
|
});
|
|
224
227
|
});
|
|
@@ -247,7 +250,7 @@ async function resumeAsyncWorkflowHandler({
|
|
|
247
250
|
throw new HTTPException(400, { message: "runId required to resume workflow" });
|
|
248
251
|
}
|
|
249
252
|
const workflow = mastra.getWorkflow(workflowId);
|
|
250
|
-
const run = workflow.
|
|
253
|
+
const run = workflow.getMemoryRun(runId);
|
|
251
254
|
if (!run) {
|
|
252
255
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
253
256
|
}
|
|
@@ -276,11 +279,11 @@ async function resumeWorkflowHandler({
|
|
|
276
279
|
throw new HTTPException(400, { message: "runId required to resume workflow" });
|
|
277
280
|
}
|
|
278
281
|
const workflow = mastra.getWorkflow(workflowId);
|
|
279
|
-
const run = workflow.
|
|
282
|
+
const run = workflow.getMemoryRun(runId);
|
|
280
283
|
if (!run) {
|
|
281
284
|
throw new HTTPException(404, { message: "Workflow run not found" });
|
|
282
285
|
}
|
|
283
|
-
|
|
286
|
+
void run.resume({
|
|
284
287
|
stepId: body.stepId,
|
|
285
288
|
context: body.context,
|
|
286
289
|
runtimeContext
|
|
@@ -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
|
import { Readable } from 'stream';
|
|
5
6
|
|
|
6
7
|
// src/server/handlers/voice.ts
|
|
@@ -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 = {};
|