@a2a-js/sdk 0.3.0 → 0.3.2
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 +2 -1
- package/dist/a2a_request_handler-B5t-IxgA.d.ts +17 -0
- package/dist/a2a_request_handler-DUvKWfix.d.cts +17 -0
- package/dist/chunk-67JNQ6TZ.js +6 -0
- package/dist/chunk-JA52GYRU.js +207 -0
- package/dist/client/index.cjs +90 -40
- package/dist/client/index.d.cts +88 -8
- package/dist/client/index.d.ts +88 -8
- package/dist/client/index.js +89 -39
- package/dist/index.cjs +14 -0
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +6 -0
- package/dist/server/express/index.cjs +342 -0
- package/dist/server/express/index.d.cts +20 -0
- package/dist/server/express/index.d.ts +20 -0
- package/dist/server/express/index.js +106 -0
- package/dist/server/index.d.cts +2 -15
- package/dist/server/index.d.ts +2 -15
- package/dist/server/index.js +5 -203
- package/package.json +5 -2
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/server/express/index.ts
|
|
30
|
+
var express_exports = {};
|
|
31
|
+
__export(express_exports, {
|
|
32
|
+
A2AExpressApp: () => A2AExpressApp
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(express_exports);
|
|
35
|
+
|
|
36
|
+
// src/server/express/a2a_express_app.ts
|
|
37
|
+
var import_express = __toESM(require("express"), 1);
|
|
38
|
+
|
|
39
|
+
// src/server/error.ts
|
|
40
|
+
var A2AError = class _A2AError extends Error {
|
|
41
|
+
code;
|
|
42
|
+
data;
|
|
43
|
+
taskId;
|
|
44
|
+
// Optional task ID context
|
|
45
|
+
constructor(code, message, data, taskId) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "A2AError";
|
|
48
|
+
this.code = code;
|
|
49
|
+
this.data = data;
|
|
50
|
+
this.taskId = taskId;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Formats the error into a standard JSON-RPC error object structure.
|
|
54
|
+
*/
|
|
55
|
+
toJSONRPCError() {
|
|
56
|
+
const errorObject = {
|
|
57
|
+
code: this.code,
|
|
58
|
+
message: this.message
|
|
59
|
+
};
|
|
60
|
+
if (this.data !== void 0) {
|
|
61
|
+
errorObject.data = this.data;
|
|
62
|
+
}
|
|
63
|
+
return errorObject;
|
|
64
|
+
}
|
|
65
|
+
// Static factory methods for common errors
|
|
66
|
+
static parseError(message, data) {
|
|
67
|
+
return new _A2AError(-32700, message, data);
|
|
68
|
+
}
|
|
69
|
+
static invalidRequest(message, data) {
|
|
70
|
+
return new _A2AError(-32600, message, data);
|
|
71
|
+
}
|
|
72
|
+
static methodNotFound(method) {
|
|
73
|
+
return new _A2AError(
|
|
74
|
+
-32601,
|
|
75
|
+
`Method not found: ${method}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
static invalidParams(message, data) {
|
|
79
|
+
return new _A2AError(-32602, message, data);
|
|
80
|
+
}
|
|
81
|
+
static internalError(message, data) {
|
|
82
|
+
return new _A2AError(-32603, message, data);
|
|
83
|
+
}
|
|
84
|
+
static taskNotFound(taskId) {
|
|
85
|
+
return new _A2AError(
|
|
86
|
+
-32001,
|
|
87
|
+
`Task not found: ${taskId}`,
|
|
88
|
+
void 0,
|
|
89
|
+
taskId
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
static taskNotCancelable(taskId) {
|
|
93
|
+
return new _A2AError(
|
|
94
|
+
-32002,
|
|
95
|
+
`Task not cancelable: ${taskId}`,
|
|
96
|
+
void 0,
|
|
97
|
+
taskId
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
static pushNotificationNotSupported() {
|
|
101
|
+
return new _A2AError(
|
|
102
|
+
-32003,
|
|
103
|
+
"Push Notification is not supported"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
static unsupportedOperation(operation) {
|
|
107
|
+
return new _A2AError(
|
|
108
|
+
-32004,
|
|
109
|
+
`Unsupported operation: ${operation}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
static authenticatedExtendedCardNotConfigured() {
|
|
113
|
+
return new _A2AError(
|
|
114
|
+
-32007,
|
|
115
|
+
`Extended card not configured.`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/server/transports/jsonrpc_transport_handler.ts
|
|
121
|
+
var JsonRpcTransportHandler = class {
|
|
122
|
+
requestHandler;
|
|
123
|
+
constructor(requestHandler) {
|
|
124
|
+
this.requestHandler = requestHandler;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Handles an incoming JSON-RPC request.
|
|
128
|
+
* For streaming methods, it returns an AsyncGenerator of JSONRPCResult.
|
|
129
|
+
* For non-streaming methods, it returns a Promise of a single JSONRPCMessage (Result or ErrorResponse).
|
|
130
|
+
*/
|
|
131
|
+
async handle(requestBody) {
|
|
132
|
+
let rpcRequest;
|
|
133
|
+
try {
|
|
134
|
+
if (typeof requestBody === "string") {
|
|
135
|
+
rpcRequest = JSON.parse(requestBody);
|
|
136
|
+
} else if (typeof requestBody === "object" && requestBody !== null) {
|
|
137
|
+
rpcRequest = requestBody;
|
|
138
|
+
} else {
|
|
139
|
+
throw A2AError.parseError("Invalid request body type.");
|
|
140
|
+
}
|
|
141
|
+
if (rpcRequest.jsonrpc !== "2.0" || !rpcRequest.method || typeof rpcRequest.method !== "string") {
|
|
142
|
+
throw A2AError.invalidRequest(
|
|
143
|
+
"Invalid JSON-RPC request structure."
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
const a2aError = error instanceof A2AError ? error : A2AError.parseError(error.message || "Failed to parse JSON request.");
|
|
148
|
+
return {
|
|
149
|
+
jsonrpc: "2.0",
|
|
150
|
+
id: typeof rpcRequest?.id !== "undefined" ? rpcRequest.id : null,
|
|
151
|
+
error: a2aError.toJSONRPCError()
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const { method, id: requestId = null } = rpcRequest;
|
|
155
|
+
try {
|
|
156
|
+
if (method === "agent/getAuthenticatedExtendedCard") {
|
|
157
|
+
const result = await this.requestHandler.getAuthenticatedExtendedAgentCard();
|
|
158
|
+
return {
|
|
159
|
+
jsonrpc: "2.0",
|
|
160
|
+
id: requestId,
|
|
161
|
+
result
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (!rpcRequest.params) {
|
|
165
|
+
throw A2AError.invalidParams(`'params' is required for '${method}'`);
|
|
166
|
+
}
|
|
167
|
+
if (method === "message/stream" || method === "tasks/resubscribe") {
|
|
168
|
+
const params = rpcRequest.params;
|
|
169
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
170
|
+
if (!agentCard.capabilities.streaming) {
|
|
171
|
+
throw A2AError.unsupportedOperation(`Method ${method} requires streaming capability.`);
|
|
172
|
+
}
|
|
173
|
+
const agentEventStream = method === "message/stream" ? this.requestHandler.sendMessageStream(params) : this.requestHandler.resubscribe(params);
|
|
174
|
+
return async function* jsonRpcEventStream() {
|
|
175
|
+
try {
|
|
176
|
+
for await (const event of agentEventStream) {
|
|
177
|
+
yield {
|
|
178
|
+
jsonrpc: "2.0",
|
|
179
|
+
id: requestId,
|
|
180
|
+
// Use the original request ID for all streamed responses
|
|
181
|
+
result: event
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
} catch (streamError) {
|
|
185
|
+
console.error(`Error in agent event stream for ${method} (request ${requestId}):`, streamError);
|
|
186
|
+
throw streamError;
|
|
187
|
+
}
|
|
188
|
+
}();
|
|
189
|
+
} else {
|
|
190
|
+
let result;
|
|
191
|
+
switch (method) {
|
|
192
|
+
case "message/send":
|
|
193
|
+
result = await this.requestHandler.sendMessage(rpcRequest.params);
|
|
194
|
+
break;
|
|
195
|
+
case "tasks/get":
|
|
196
|
+
result = await this.requestHandler.getTask(rpcRequest.params);
|
|
197
|
+
break;
|
|
198
|
+
case "tasks/cancel":
|
|
199
|
+
result = await this.requestHandler.cancelTask(rpcRequest.params);
|
|
200
|
+
break;
|
|
201
|
+
case "tasks/pushNotificationConfig/set":
|
|
202
|
+
result = await this.requestHandler.setTaskPushNotificationConfig(
|
|
203
|
+
rpcRequest.params
|
|
204
|
+
);
|
|
205
|
+
break;
|
|
206
|
+
case "tasks/pushNotificationConfig/get":
|
|
207
|
+
result = await this.requestHandler.getTaskPushNotificationConfig(
|
|
208
|
+
rpcRequest.params
|
|
209
|
+
);
|
|
210
|
+
break;
|
|
211
|
+
case "tasks/pushNotificationConfig/delete":
|
|
212
|
+
await this.requestHandler.deleteTaskPushNotificationConfig(
|
|
213
|
+
rpcRequest.params
|
|
214
|
+
);
|
|
215
|
+
result = null;
|
|
216
|
+
break;
|
|
217
|
+
case "tasks/pushNotificationConfig/list":
|
|
218
|
+
result = await this.requestHandler.listTaskPushNotificationConfigs(
|
|
219
|
+
rpcRequest.params
|
|
220
|
+
);
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
throw A2AError.methodNotFound(method);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
jsonrpc: "2.0",
|
|
227
|
+
id: requestId,
|
|
228
|
+
result
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
} catch (error) {
|
|
232
|
+
const a2aError = error instanceof A2AError ? error : A2AError.internalError(error.message || "An unexpected error occurred.");
|
|
233
|
+
return {
|
|
234
|
+
jsonrpc: "2.0",
|
|
235
|
+
id: requestId,
|
|
236
|
+
error: a2aError.toJSONRPCError()
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// src/constants.ts
|
|
243
|
+
var AGENT_CARD_PATH = ".well-known/agent-card.json";
|
|
244
|
+
|
|
245
|
+
// src/server/express/a2a_express_app.ts
|
|
246
|
+
var A2AExpressApp = class {
|
|
247
|
+
requestHandler;
|
|
248
|
+
// Kept for getAgentCard
|
|
249
|
+
jsonRpcTransportHandler;
|
|
250
|
+
constructor(requestHandler) {
|
|
251
|
+
this.requestHandler = requestHandler;
|
|
252
|
+
this.jsonRpcTransportHandler = new JsonRpcTransportHandler(requestHandler);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Adds A2A routes to an existing Express app.
|
|
256
|
+
* @param app Optional existing Express app.
|
|
257
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
258
|
+
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
259
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
260
|
+
* @returns The Express app with A2A routes.
|
|
261
|
+
*/
|
|
262
|
+
setupRoutes(app, baseUrl = "", middlewares, agentCardPath = AGENT_CARD_PATH) {
|
|
263
|
+
const router = import_express.default.Router();
|
|
264
|
+
router.use(import_express.default.json(), ...middlewares ?? []);
|
|
265
|
+
router.get(`/${agentCardPath}`, async (req, res) => {
|
|
266
|
+
try {
|
|
267
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
268
|
+
res.json(agentCard);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
console.error("Error fetching agent card:", error);
|
|
271
|
+
res.status(500).json({ error: "Failed to retrieve agent card" });
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
router.post("/", async (req, res) => {
|
|
275
|
+
try {
|
|
276
|
+
const rpcResponseOrStream = await this.jsonRpcTransportHandler.handle(req.body);
|
|
277
|
+
if (typeof rpcResponseOrStream?.[Symbol.asyncIterator] === "function") {
|
|
278
|
+
const stream = rpcResponseOrStream;
|
|
279
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
280
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
281
|
+
res.setHeader("Connection", "keep-alive");
|
|
282
|
+
res.flushHeaders();
|
|
283
|
+
try {
|
|
284
|
+
for await (const event of stream) {
|
|
285
|
+
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
286
|
+
`);
|
|
287
|
+
res.write(`data: ${JSON.stringify(event)}
|
|
288
|
+
|
|
289
|
+
`);
|
|
290
|
+
}
|
|
291
|
+
} catch (streamError) {
|
|
292
|
+
console.error(`Error during SSE streaming (request ${req.body?.id}):`, streamError);
|
|
293
|
+
const a2aError = streamError instanceof A2AError ? streamError : A2AError.internalError(streamError.message || "Streaming error.");
|
|
294
|
+
const errorResponse = {
|
|
295
|
+
jsonrpc: "2.0",
|
|
296
|
+
id: req.body?.id || null,
|
|
297
|
+
// Use original request ID if available
|
|
298
|
+
error: a2aError.toJSONRPCError()
|
|
299
|
+
};
|
|
300
|
+
if (!res.headersSent) {
|
|
301
|
+
res.status(500).json(errorResponse);
|
|
302
|
+
} else {
|
|
303
|
+
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
304
|
+
`);
|
|
305
|
+
res.write(`event: error
|
|
306
|
+
`);
|
|
307
|
+
res.write(`data: ${JSON.stringify(errorResponse)}
|
|
308
|
+
|
|
309
|
+
`);
|
|
310
|
+
}
|
|
311
|
+
} finally {
|
|
312
|
+
if (!res.writableEnded) {
|
|
313
|
+
res.end();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
const rpcResponse = rpcResponseOrStream;
|
|
318
|
+
res.status(200).json(rpcResponse);
|
|
319
|
+
}
|
|
320
|
+
} catch (error) {
|
|
321
|
+
console.error("Unhandled error in A2AExpressApp POST handler:", error);
|
|
322
|
+
const a2aError = error instanceof A2AError ? error : A2AError.internalError("General processing error.");
|
|
323
|
+
const errorResponse = {
|
|
324
|
+
jsonrpc: "2.0",
|
|
325
|
+
id: req.body?.id || null,
|
|
326
|
+
error: a2aError.toJSONRPCError()
|
|
327
|
+
};
|
|
328
|
+
if (!res.headersSent) {
|
|
329
|
+
res.status(500).json(errorResponse);
|
|
330
|
+
} else if (!res.writableEnded) {
|
|
331
|
+
res.end();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
app.use(baseUrl, router);
|
|
336
|
+
return app;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
340
|
+
0 && (module.exports = {
|
|
341
|
+
A2AExpressApp
|
|
342
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Express, RequestHandler, ErrorRequestHandler } from 'express';
|
|
2
|
+
import { A as A2ARequestHandler } from '../../a2a_request_handler-DUvKWfix.cjs';
|
|
3
|
+
import '../../types-DNKcmF0f.cjs';
|
|
4
|
+
|
|
5
|
+
declare class A2AExpressApp {
|
|
6
|
+
private requestHandler;
|
|
7
|
+
private jsonRpcTransportHandler;
|
|
8
|
+
constructor(requestHandler: A2ARequestHandler);
|
|
9
|
+
/**
|
|
10
|
+
* Adds A2A routes to an existing Express app.
|
|
11
|
+
* @param app Optional existing Express app.
|
|
12
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
13
|
+
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
14
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
15
|
+
* @returns The Express app with A2A routes.
|
|
16
|
+
*/
|
|
17
|
+
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler>, agentCardPath?: string): Express;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { A2AExpressApp };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Express, RequestHandler, ErrorRequestHandler } from 'express';
|
|
2
|
+
import { A as A2ARequestHandler } from '../../a2a_request_handler-B5t-IxgA.js';
|
|
3
|
+
import '../../types-DNKcmF0f.js';
|
|
4
|
+
|
|
5
|
+
declare class A2AExpressApp {
|
|
6
|
+
private requestHandler;
|
|
7
|
+
private jsonRpcTransportHandler;
|
|
8
|
+
constructor(requestHandler: A2ARequestHandler);
|
|
9
|
+
/**
|
|
10
|
+
* Adds A2A routes to an existing Express app.
|
|
11
|
+
* @param app Optional existing Express app.
|
|
12
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
13
|
+
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
14
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
15
|
+
* @returns The Express app with A2A routes.
|
|
16
|
+
*/
|
|
17
|
+
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler>, agentCardPath?: string): Express;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { A2AExpressApp };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AGENT_CARD_PATH
|
|
3
|
+
} from "../../chunk-67JNQ6TZ.js";
|
|
4
|
+
import {
|
|
5
|
+
A2AError,
|
|
6
|
+
JsonRpcTransportHandler
|
|
7
|
+
} from "../../chunk-JA52GYRU.js";
|
|
8
|
+
|
|
9
|
+
// src/server/express/a2a_express_app.ts
|
|
10
|
+
import express from "express";
|
|
11
|
+
var A2AExpressApp = class {
|
|
12
|
+
requestHandler;
|
|
13
|
+
// Kept for getAgentCard
|
|
14
|
+
jsonRpcTransportHandler;
|
|
15
|
+
constructor(requestHandler) {
|
|
16
|
+
this.requestHandler = requestHandler;
|
|
17
|
+
this.jsonRpcTransportHandler = new JsonRpcTransportHandler(requestHandler);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Adds A2A routes to an existing Express app.
|
|
21
|
+
* @param app Optional existing Express app.
|
|
22
|
+
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
23
|
+
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
24
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
25
|
+
* @returns The Express app with A2A routes.
|
|
26
|
+
*/
|
|
27
|
+
setupRoutes(app, baseUrl = "", middlewares, agentCardPath = AGENT_CARD_PATH) {
|
|
28
|
+
const router = express.Router();
|
|
29
|
+
router.use(express.json(), ...middlewares ?? []);
|
|
30
|
+
router.get(`/${agentCardPath}`, async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const agentCard = await this.requestHandler.getAgentCard();
|
|
33
|
+
res.json(agentCard);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("Error fetching agent card:", error);
|
|
36
|
+
res.status(500).json({ error: "Failed to retrieve agent card" });
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
router.post("/", async (req, res) => {
|
|
40
|
+
try {
|
|
41
|
+
const rpcResponseOrStream = await this.jsonRpcTransportHandler.handle(req.body);
|
|
42
|
+
if (typeof rpcResponseOrStream?.[Symbol.asyncIterator] === "function") {
|
|
43
|
+
const stream = rpcResponseOrStream;
|
|
44
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
45
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
46
|
+
res.setHeader("Connection", "keep-alive");
|
|
47
|
+
res.flushHeaders();
|
|
48
|
+
try {
|
|
49
|
+
for await (const event of stream) {
|
|
50
|
+
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
51
|
+
`);
|
|
52
|
+
res.write(`data: ${JSON.stringify(event)}
|
|
53
|
+
|
|
54
|
+
`);
|
|
55
|
+
}
|
|
56
|
+
} catch (streamError) {
|
|
57
|
+
console.error(`Error during SSE streaming (request ${req.body?.id}):`, streamError);
|
|
58
|
+
const a2aError = streamError instanceof A2AError ? streamError : A2AError.internalError(streamError.message || "Streaming error.");
|
|
59
|
+
const errorResponse = {
|
|
60
|
+
jsonrpc: "2.0",
|
|
61
|
+
id: req.body?.id || null,
|
|
62
|
+
// Use original request ID if available
|
|
63
|
+
error: a2aError.toJSONRPCError()
|
|
64
|
+
};
|
|
65
|
+
if (!res.headersSent) {
|
|
66
|
+
res.status(500).json(errorResponse);
|
|
67
|
+
} else {
|
|
68
|
+
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
69
|
+
`);
|
|
70
|
+
res.write(`event: error
|
|
71
|
+
`);
|
|
72
|
+
res.write(`data: ${JSON.stringify(errorResponse)}
|
|
73
|
+
|
|
74
|
+
`);
|
|
75
|
+
}
|
|
76
|
+
} finally {
|
|
77
|
+
if (!res.writableEnded) {
|
|
78
|
+
res.end();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
const rpcResponse = rpcResponseOrStream;
|
|
83
|
+
res.status(200).json(rpcResponse);
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error("Unhandled error in A2AExpressApp POST handler:", error);
|
|
87
|
+
const a2aError = error instanceof A2AError ? error : A2AError.internalError("General processing error.");
|
|
88
|
+
const errorResponse = {
|
|
89
|
+
jsonrpc: "2.0",
|
|
90
|
+
id: req.body?.id || null,
|
|
91
|
+
error: a2aError.toJSONRPCError()
|
|
92
|
+
};
|
|
93
|
+
if (!res.headersSent) {
|
|
94
|
+
res.status(500).json(errorResponse);
|
|
95
|
+
} else if (!res.writableEnded) {
|
|
96
|
+
res.end();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
app.use(baseUrl, router);
|
|
101
|
+
return app;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
export {
|
|
105
|
+
A2AExpressApp
|
|
106
|
+
};
|
package/dist/server/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
2
|
import { B as Message, aw as Task, aO as TaskStatusUpdateEvent, aQ as TaskArtifactUpdateEvent, ac as AgentCard, w as MessageSendParams, V as TaskQueryParams, X as TaskIdParams, Z as TaskPushNotificationConfig, a1 as GetTaskPushNotificationConfigParams, a5 as ListTaskPushNotificationConfigParams, a7 as DeleteTaskPushNotificationConfigParams, i as JSONRPCResponse, au as JSONRPCError } from '../types-DNKcmF0f.cjs';
|
|
3
|
+
import { A as A2ARequestHandler } from '../a2a_request_handler-DUvKWfix.cjs';
|
|
3
4
|
|
|
4
5
|
type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
5
6
|
interface ExecutionEventBus {
|
|
@@ -93,20 +94,6 @@ declare class ExecutionEventQueue {
|
|
|
93
94
|
stop(): void;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
interface A2ARequestHandler {
|
|
97
|
-
getAgentCard(): Promise<AgentCard>;
|
|
98
|
-
getAuthenticatedExtendedAgentCard(): Promise<AgentCard>;
|
|
99
|
-
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
100
|
-
sendMessageStream(params: MessageSendParams): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
101
|
-
getTask(params: TaskQueryParams): Promise<Task>;
|
|
102
|
-
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
103
|
-
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
104
|
-
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig>;
|
|
105
|
-
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig[]>;
|
|
106
|
-
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams): Promise<void>;
|
|
107
|
-
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
97
|
/**
|
|
111
98
|
* Simplified interface for task storage providers.
|
|
112
99
|
* Stores and retrieves the task.
|
|
@@ -220,4 +207,4 @@ declare class A2AError extends Error {
|
|
|
220
207
|
static authenticatedExtendedCardNotConfigured(): A2AError;
|
|
221
208
|
}
|
|
222
209
|
|
|
223
|
-
export { A2AError,
|
|
210
|
+
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
2
|
import { B as Message, aw as Task, aO as TaskStatusUpdateEvent, aQ as TaskArtifactUpdateEvent, ac as AgentCard, w as MessageSendParams, V as TaskQueryParams, X as TaskIdParams, Z as TaskPushNotificationConfig, a1 as GetTaskPushNotificationConfigParams, a5 as ListTaskPushNotificationConfigParams, a7 as DeleteTaskPushNotificationConfigParams, i as JSONRPCResponse, au as JSONRPCError } from '../types-DNKcmF0f.js';
|
|
3
|
+
import { A as A2ARequestHandler } from '../a2a_request_handler-B5t-IxgA.js';
|
|
3
4
|
|
|
4
5
|
type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
5
6
|
interface ExecutionEventBus {
|
|
@@ -93,20 +94,6 @@ declare class ExecutionEventQueue {
|
|
|
93
94
|
stop(): void;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
interface A2ARequestHandler {
|
|
97
|
-
getAgentCard(): Promise<AgentCard>;
|
|
98
|
-
getAuthenticatedExtendedAgentCard(): Promise<AgentCard>;
|
|
99
|
-
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
100
|
-
sendMessageStream(params: MessageSendParams): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
101
|
-
getTask(params: TaskQueryParams): Promise<Task>;
|
|
102
|
-
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
103
|
-
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
104
|
-
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig>;
|
|
105
|
-
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig[]>;
|
|
106
|
-
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams): Promise<void>;
|
|
107
|
-
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
97
|
/**
|
|
111
98
|
* Simplified interface for task storage providers.
|
|
112
99
|
* Stores and retrieves the task.
|
|
@@ -220,4 +207,4 @@ declare class A2AError extends Error {
|
|
|
220
207
|
static authenticatedExtendedCardNotConfigured(): A2AError;
|
|
221
208
|
}
|
|
222
209
|
|
|
223
|
-
export { A2AError,
|
|
210
|
+
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|