@a2a-js/sdk 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/dist/client/index.cjs +14 -10
- package/dist/client/index.d.cts +9 -5
- package/dist/client/index.d.ts +9 -5
- package/dist/client/index.js +14 -10
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/server/index.cjs +165 -182
- package/dist/server/index.d.cts +38 -22
- package/dist/server/index.d.ts +38 -22
- package/dist/server/index.js +164 -171
- package/dist/types-DNKcmF0f.d.cts +2562 -0
- package/dist/types-DNKcmF0f.d.ts +2562 -0
- package/package.json +20 -7
- package/dist/types-CcBgkR2G.d.cts +0 -2048
- package/dist/types-CcBgkR2G.d.ts +0 -2048
package/dist/server/index.cjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
5
|
var __export = (target, all) => {
|
|
8
6
|
for (var name in all)
|
|
@@ -16,24 +14,16 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
14
|
}
|
|
17
15
|
return to;
|
|
18
16
|
};
|
|
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
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
18
|
|
|
29
19
|
// src/server/index.ts
|
|
30
20
|
var server_exports = {};
|
|
31
21
|
__export(server_exports, {
|
|
32
22
|
A2AError: () => A2AError,
|
|
33
|
-
A2AExpressApp: () => A2AExpressApp,
|
|
34
23
|
DefaultExecutionEventBus: () => DefaultExecutionEventBus,
|
|
35
24
|
DefaultExecutionEventBusManager: () => DefaultExecutionEventBusManager,
|
|
36
25
|
DefaultRequestHandler: () => DefaultRequestHandler,
|
|
26
|
+
ExecutionEventQueue: () => ExecutionEventQueue,
|
|
37
27
|
InMemoryTaskStore: () => InMemoryTaskStore,
|
|
38
28
|
JsonRpcTransportHandler: () => JsonRpcTransportHandler,
|
|
39
29
|
RequestContext: () => RequestContext,
|
|
@@ -107,6 +97,63 @@ var DefaultExecutionEventBusManager = class {
|
|
|
107
97
|
}
|
|
108
98
|
};
|
|
109
99
|
|
|
100
|
+
// src/server/events/execution_event_queue.ts
|
|
101
|
+
var ExecutionEventQueue = class {
|
|
102
|
+
eventBus;
|
|
103
|
+
eventQueue = [];
|
|
104
|
+
resolvePromise;
|
|
105
|
+
stopped = false;
|
|
106
|
+
boundHandleEvent;
|
|
107
|
+
constructor(eventBus) {
|
|
108
|
+
this.eventBus = eventBus;
|
|
109
|
+
this.eventBus.on("event", this.handleEvent);
|
|
110
|
+
this.eventBus.on("finished", this.handleFinished);
|
|
111
|
+
}
|
|
112
|
+
handleEvent = (event) => {
|
|
113
|
+
if (this.stopped) return;
|
|
114
|
+
this.eventQueue.push(event);
|
|
115
|
+
if (this.resolvePromise) {
|
|
116
|
+
this.resolvePromise();
|
|
117
|
+
this.resolvePromise = void 0;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
handleFinished = () => {
|
|
121
|
+
this.stop();
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Provides an async generator that yields events from the event bus.
|
|
125
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
126
|
+
*/
|
|
127
|
+
async *events() {
|
|
128
|
+
while (!this.stopped || this.eventQueue.length > 0) {
|
|
129
|
+
if (this.eventQueue.length > 0) {
|
|
130
|
+
const event = this.eventQueue.shift();
|
|
131
|
+
yield event;
|
|
132
|
+
if (event.kind === "message" || event.kind === "status-update" && event.final) {
|
|
133
|
+
this.handleFinished();
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
} else if (!this.stopped) {
|
|
137
|
+
await new Promise((resolve) => {
|
|
138
|
+
this.resolvePromise = resolve;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Stops the event queue from processing further events.
|
|
145
|
+
*/
|
|
146
|
+
stop() {
|
|
147
|
+
this.stopped = true;
|
|
148
|
+
if (this.resolvePromise) {
|
|
149
|
+
this.resolvePromise();
|
|
150
|
+
this.resolvePromise = void 0;
|
|
151
|
+
}
|
|
152
|
+
this.eventBus.off("event", this.handleEvent);
|
|
153
|
+
this.eventBus.off("finished", this.handleFinished);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
110
157
|
// src/server/request_handler/default_request_handler.ts
|
|
111
158
|
var import_uuid = require("uuid");
|
|
112
159
|
|
|
@@ -183,62 +230,11 @@ var A2AError = class _A2AError extends Error {
|
|
|
183
230
|
`Unsupported operation: ${operation}`
|
|
184
231
|
);
|
|
185
232
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
eventQueue = [];
|
|
192
|
-
resolvePromise;
|
|
193
|
-
stopped = false;
|
|
194
|
-
boundHandleEvent;
|
|
195
|
-
constructor(eventBus) {
|
|
196
|
-
this.eventBus = eventBus;
|
|
197
|
-
this.eventBus.on("event", this.handleEvent);
|
|
198
|
-
this.eventBus.on("finished", this.handleFinished);
|
|
199
|
-
}
|
|
200
|
-
handleEvent = (event) => {
|
|
201
|
-
if (this.stopped) return;
|
|
202
|
-
this.eventQueue.push(event);
|
|
203
|
-
if (this.resolvePromise) {
|
|
204
|
-
this.resolvePromise();
|
|
205
|
-
this.resolvePromise = void 0;
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
handleFinished = () => {
|
|
209
|
-
this.stop();
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Provides an async generator that yields events from the event bus.
|
|
213
|
-
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
214
|
-
*/
|
|
215
|
-
async *events() {
|
|
216
|
-
while (!this.stopped || this.eventQueue.length > 0) {
|
|
217
|
-
if (this.eventQueue.length > 0) {
|
|
218
|
-
const event = this.eventQueue.shift();
|
|
219
|
-
yield event;
|
|
220
|
-
if (event.kind === "message" || event.kind === "status-update" && event.final) {
|
|
221
|
-
this.handleFinished();
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
} else if (!this.stopped) {
|
|
225
|
-
await new Promise((resolve) => {
|
|
226
|
-
this.resolvePromise = resolve;
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Stops the event queue from processing further events.
|
|
233
|
-
*/
|
|
234
|
-
stop() {
|
|
235
|
-
this.stopped = true;
|
|
236
|
-
if (this.resolvePromise) {
|
|
237
|
-
this.resolvePromise();
|
|
238
|
-
this.resolvePromise = void 0;
|
|
239
|
-
}
|
|
240
|
-
this.eventBus.off("event", this.handleEvent);
|
|
241
|
-
this.eventBus.off("finished", this.handleFinished);
|
|
233
|
+
static authenticatedExtendedCardNotConfigured() {
|
|
234
|
+
return new _A2AError(
|
|
235
|
+
-32007,
|
|
236
|
+
`Extended card not configured.`
|
|
237
|
+
);
|
|
242
238
|
}
|
|
243
239
|
};
|
|
244
240
|
|
|
@@ -374,20 +370,28 @@ var ResultManager = class {
|
|
|
374
370
|
var terminalStates = ["completed", "failed", "canceled", "rejected"];
|
|
375
371
|
var DefaultRequestHandler = class {
|
|
376
372
|
agentCard;
|
|
373
|
+
extendedAgentCard;
|
|
377
374
|
taskStore;
|
|
378
375
|
agentExecutor;
|
|
379
376
|
eventBusManager;
|
|
380
377
|
// Store for push notification configurations (could be part of TaskStore or separate)
|
|
381
378
|
pushNotificationConfigs = /* @__PURE__ */ new Map();
|
|
382
|
-
constructor(agentCard, taskStore, agentExecutor, eventBusManager = new DefaultExecutionEventBusManager()) {
|
|
379
|
+
constructor(agentCard, taskStore, agentExecutor, eventBusManager = new DefaultExecutionEventBusManager(), extendedAgentCard) {
|
|
383
380
|
this.agentCard = agentCard;
|
|
384
381
|
this.taskStore = taskStore;
|
|
385
382
|
this.agentExecutor = agentExecutor;
|
|
386
383
|
this.eventBusManager = eventBusManager;
|
|
384
|
+
this.extendedAgentCard = extendedAgentCard;
|
|
387
385
|
}
|
|
388
386
|
async getAgentCard() {
|
|
389
387
|
return this.agentCard;
|
|
390
388
|
}
|
|
389
|
+
async getAuthenticatedExtendedAgentCard() {
|
|
390
|
+
if (!this.extendedAgentCard) {
|
|
391
|
+
throw A2AError.authenticatedExtendedCardNotConfigured();
|
|
392
|
+
}
|
|
393
|
+
return this.extendedAgentCard;
|
|
394
|
+
}
|
|
391
395
|
async _createRequestContext(incomingMessage, taskId, isStream) {
|
|
392
396
|
let task;
|
|
393
397
|
let referenceTasks;
|
|
@@ -411,11 +415,11 @@ var DefaultRequestHandler = class {
|
|
|
411
415
|
}
|
|
412
416
|
}
|
|
413
417
|
}
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
418
|
+
const contextId = incomingMessage.contextId || task?.contextId || (0, import_uuid.v4)();
|
|
419
|
+
const messageForContext = {
|
|
420
|
+
...incomingMessage,
|
|
421
|
+
contextId
|
|
422
|
+
};
|
|
419
423
|
return new RequestContext(
|
|
420
424
|
messageForContext,
|
|
421
425
|
taskId,
|
|
@@ -610,27 +614,78 @@ var DefaultRequestHandler = class {
|
|
|
610
614
|
if (!this.agentCard.capabilities.pushNotifications) {
|
|
611
615
|
throw A2AError.pushNotificationNotSupported();
|
|
612
616
|
}
|
|
613
|
-
const
|
|
614
|
-
if (!
|
|
617
|
+
const task = await this.taskStore.load(params.taskId);
|
|
618
|
+
if (!task) {
|
|
615
619
|
throw A2AError.taskNotFound(params.taskId);
|
|
616
620
|
}
|
|
617
|
-
|
|
621
|
+
const { taskId, pushNotificationConfig } = params;
|
|
622
|
+
if (!pushNotificationConfig.id) {
|
|
623
|
+
pushNotificationConfig.id = taskId;
|
|
624
|
+
}
|
|
625
|
+
const configs = this.pushNotificationConfigs.get(taskId) || [];
|
|
626
|
+
const updatedConfigs = configs.filter((c) => c.id !== pushNotificationConfig.id);
|
|
627
|
+
updatedConfigs.push(pushNotificationConfig);
|
|
628
|
+
this.pushNotificationConfigs.set(taskId, updatedConfigs);
|
|
618
629
|
return params;
|
|
619
630
|
}
|
|
620
631
|
async getTaskPushNotificationConfig(params) {
|
|
621
632
|
if (!this.agentCard.capabilities.pushNotifications) {
|
|
622
633
|
throw A2AError.pushNotificationNotSupported();
|
|
623
634
|
}
|
|
624
|
-
const
|
|
625
|
-
if (!
|
|
635
|
+
const task = await this.taskStore.load(params.id);
|
|
636
|
+
if (!task) {
|
|
626
637
|
throw A2AError.taskNotFound(params.id);
|
|
627
638
|
}
|
|
628
|
-
const
|
|
629
|
-
if (
|
|
639
|
+
const configs = this.pushNotificationConfigs.get(params.id) || [];
|
|
640
|
+
if (configs.length === 0) {
|
|
630
641
|
throw A2AError.internalError(`Push notification config not found for task ${params.id}.`);
|
|
631
642
|
}
|
|
643
|
+
let configId;
|
|
644
|
+
if ("pushNotificationConfigId" in params && params.pushNotificationConfigId) {
|
|
645
|
+
configId = params.pushNotificationConfigId;
|
|
646
|
+
} else {
|
|
647
|
+
configId = params.id;
|
|
648
|
+
}
|
|
649
|
+
const config = configs.find((c) => c.id === configId);
|
|
650
|
+
if (!config) {
|
|
651
|
+
throw A2AError.internalError(`Push notification config with id '${configId}' not found for task ${params.id}.`);
|
|
652
|
+
}
|
|
632
653
|
return { taskId: params.id, pushNotificationConfig: config };
|
|
633
654
|
}
|
|
655
|
+
async listTaskPushNotificationConfigs(params) {
|
|
656
|
+
if (!this.agentCard.capabilities.pushNotifications) {
|
|
657
|
+
throw A2AError.pushNotificationNotSupported();
|
|
658
|
+
}
|
|
659
|
+
const task = await this.taskStore.load(params.id);
|
|
660
|
+
if (!task) {
|
|
661
|
+
throw A2AError.taskNotFound(params.id);
|
|
662
|
+
}
|
|
663
|
+
const configs = this.pushNotificationConfigs.get(params.id) || [];
|
|
664
|
+
return configs.map((config) => ({
|
|
665
|
+
taskId: params.id,
|
|
666
|
+
pushNotificationConfig: config
|
|
667
|
+
}));
|
|
668
|
+
}
|
|
669
|
+
async deleteTaskPushNotificationConfig(params) {
|
|
670
|
+
if (!this.agentCard.capabilities.pushNotifications) {
|
|
671
|
+
throw A2AError.pushNotificationNotSupported();
|
|
672
|
+
}
|
|
673
|
+
const task = await this.taskStore.load(params.id);
|
|
674
|
+
if (!task) {
|
|
675
|
+
throw A2AError.taskNotFound(params.id);
|
|
676
|
+
}
|
|
677
|
+
const { id: taskId, pushNotificationConfigId } = params;
|
|
678
|
+
const configs = this.pushNotificationConfigs.get(taskId);
|
|
679
|
+
if (!configs) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
const updatedConfigs = configs.filter((c) => c.id !== pushNotificationConfigId);
|
|
683
|
+
if (updatedConfigs.length === 0) {
|
|
684
|
+
this.pushNotificationConfigs.delete(taskId);
|
|
685
|
+
} else if (updatedConfigs.length < configs.length) {
|
|
686
|
+
this.pushNotificationConfigs.set(taskId, updatedConfigs);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
634
689
|
async *resubscribe(params) {
|
|
635
690
|
if (!this.agentCard.capabilities.streaming) {
|
|
636
691
|
throw A2AError.unsupportedOperation("Streaming (and thus resubscription) is not supported.");
|
|
@@ -712,9 +767,21 @@ var JsonRpcTransportHandler = class {
|
|
|
712
767
|
error: a2aError.toJSONRPCError()
|
|
713
768
|
};
|
|
714
769
|
}
|
|
715
|
-
const { method,
|
|
770
|
+
const { method, id: requestId = null } = rpcRequest;
|
|
716
771
|
try {
|
|
772
|
+
if (method === "agent/getAuthenticatedExtendedCard") {
|
|
773
|
+
const result = await this.requestHandler.getAuthenticatedExtendedAgentCard();
|
|
774
|
+
return {
|
|
775
|
+
jsonrpc: "2.0",
|
|
776
|
+
id: requestId,
|
|
777
|
+
result
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
if (!rpcRequest.params) {
|
|
781
|
+
throw A2AError.invalidParams(`'params' is required for '${method}'`);
|
|
782
|
+
}
|
|
717
783
|
if (method === "message/stream" || method === "tasks/resubscribe") {
|
|
784
|
+
const params = rpcRequest.params;
|
|
718
785
|
const agentCard = await this.requestHandler.getAgentCard();
|
|
719
786
|
if (!agentCard.capabilities.streaming) {
|
|
720
787
|
throw A2AError.unsupportedOperation(`Method ${method} requires streaming capability.`);
|
|
@@ -739,22 +806,33 @@ var JsonRpcTransportHandler = class {
|
|
|
739
806
|
let result;
|
|
740
807
|
switch (method) {
|
|
741
808
|
case "message/send":
|
|
742
|
-
result = await this.requestHandler.sendMessage(params);
|
|
809
|
+
result = await this.requestHandler.sendMessage(rpcRequest.params);
|
|
743
810
|
break;
|
|
744
811
|
case "tasks/get":
|
|
745
|
-
result = await this.requestHandler.getTask(params);
|
|
812
|
+
result = await this.requestHandler.getTask(rpcRequest.params);
|
|
746
813
|
break;
|
|
747
814
|
case "tasks/cancel":
|
|
748
|
-
result = await this.requestHandler.cancelTask(params);
|
|
815
|
+
result = await this.requestHandler.cancelTask(rpcRequest.params);
|
|
749
816
|
break;
|
|
750
817
|
case "tasks/pushNotificationConfig/set":
|
|
751
818
|
result = await this.requestHandler.setTaskPushNotificationConfig(
|
|
752
|
-
params
|
|
819
|
+
rpcRequest.params
|
|
753
820
|
);
|
|
754
821
|
break;
|
|
755
822
|
case "tasks/pushNotificationConfig/get":
|
|
756
823
|
result = await this.requestHandler.getTaskPushNotificationConfig(
|
|
757
|
-
params
|
|
824
|
+
rpcRequest.params
|
|
825
|
+
);
|
|
826
|
+
break;
|
|
827
|
+
case "tasks/pushNotificationConfig/delete":
|
|
828
|
+
await this.requestHandler.deleteTaskPushNotificationConfig(
|
|
829
|
+
rpcRequest.params
|
|
830
|
+
);
|
|
831
|
+
result = null;
|
|
832
|
+
break;
|
|
833
|
+
case "tasks/pushNotificationConfig/list":
|
|
834
|
+
result = await this.requestHandler.listTaskPushNotificationConfigs(
|
|
835
|
+
rpcRequest.params
|
|
758
836
|
);
|
|
759
837
|
break;
|
|
760
838
|
default:
|
|
@@ -776,108 +854,13 @@ var JsonRpcTransportHandler = class {
|
|
|
776
854
|
}
|
|
777
855
|
}
|
|
778
856
|
};
|
|
779
|
-
|
|
780
|
-
// src/server/a2a_express_app.ts
|
|
781
|
-
var import_express = __toESM(require("express"), 1);
|
|
782
|
-
var A2AExpressApp = class {
|
|
783
|
-
requestHandler;
|
|
784
|
-
// Kept for getAgentCard
|
|
785
|
-
jsonRpcTransportHandler;
|
|
786
|
-
constructor(requestHandler) {
|
|
787
|
-
this.requestHandler = requestHandler;
|
|
788
|
-
this.jsonRpcTransportHandler = new JsonRpcTransportHandler(requestHandler);
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Adds A2A routes to an existing Express app.
|
|
792
|
-
* @param app Optional existing Express app.
|
|
793
|
-
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
794
|
-
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
795
|
-
* @returns The Express app with A2A routes.
|
|
796
|
-
*/
|
|
797
|
-
setupRoutes(app, baseUrl = "", middlewares) {
|
|
798
|
-
const router = import_express.default.Router();
|
|
799
|
-
router.use(import_express.default.json(), ...middlewares ?? []);
|
|
800
|
-
router.get("/.well-known/agent.json", async (req, res) => {
|
|
801
|
-
try {
|
|
802
|
-
const agentCard = await this.requestHandler.getAgentCard();
|
|
803
|
-
res.json(agentCard);
|
|
804
|
-
} catch (error) {
|
|
805
|
-
console.error("Error fetching agent card:", error);
|
|
806
|
-
res.status(500).json({ error: "Failed to retrieve agent card" });
|
|
807
|
-
}
|
|
808
|
-
});
|
|
809
|
-
router.post("/", async (req, res) => {
|
|
810
|
-
try {
|
|
811
|
-
const rpcResponseOrStream = await this.jsonRpcTransportHandler.handle(req.body);
|
|
812
|
-
if (typeof rpcResponseOrStream?.[Symbol.asyncIterator] === "function") {
|
|
813
|
-
const stream = rpcResponseOrStream;
|
|
814
|
-
res.setHeader("Content-Type", "text/event-stream");
|
|
815
|
-
res.setHeader("Cache-Control", "no-cache");
|
|
816
|
-
res.setHeader("Connection", "keep-alive");
|
|
817
|
-
res.flushHeaders();
|
|
818
|
-
try {
|
|
819
|
-
for await (const event of stream) {
|
|
820
|
-
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
821
|
-
`);
|
|
822
|
-
res.write(`data: ${JSON.stringify(event)}
|
|
823
|
-
|
|
824
|
-
`);
|
|
825
|
-
}
|
|
826
|
-
} catch (streamError) {
|
|
827
|
-
console.error(`Error during SSE streaming (request ${req.body?.id}):`, streamError);
|
|
828
|
-
const a2aError = streamError instanceof A2AError ? streamError : A2AError.internalError(streamError.message || "Streaming error.");
|
|
829
|
-
const errorResponse = {
|
|
830
|
-
jsonrpc: "2.0",
|
|
831
|
-
id: req.body?.id || null,
|
|
832
|
-
// Use original request ID if available
|
|
833
|
-
error: a2aError.toJSONRPCError()
|
|
834
|
-
};
|
|
835
|
-
if (!res.headersSent) {
|
|
836
|
-
res.status(500).json(errorResponse);
|
|
837
|
-
} else {
|
|
838
|
-
res.write(`id: ${(/* @__PURE__ */ new Date()).getTime()}
|
|
839
|
-
`);
|
|
840
|
-
res.write(`event: error
|
|
841
|
-
`);
|
|
842
|
-
res.write(`data: ${JSON.stringify(errorResponse)}
|
|
843
|
-
|
|
844
|
-
`);
|
|
845
|
-
}
|
|
846
|
-
} finally {
|
|
847
|
-
if (!res.writableEnded) {
|
|
848
|
-
res.end();
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
} else {
|
|
852
|
-
const rpcResponse = rpcResponseOrStream;
|
|
853
|
-
res.status(200).json(rpcResponse);
|
|
854
|
-
}
|
|
855
|
-
} catch (error) {
|
|
856
|
-
console.error("Unhandled error in A2AExpressApp POST handler:", error);
|
|
857
|
-
const a2aError = error instanceof A2AError ? error : A2AError.internalError("General processing error.");
|
|
858
|
-
const errorResponse = {
|
|
859
|
-
jsonrpc: "2.0",
|
|
860
|
-
id: req.body?.id || null,
|
|
861
|
-
error: a2aError.toJSONRPCError()
|
|
862
|
-
};
|
|
863
|
-
if (!res.headersSent) {
|
|
864
|
-
res.status(500).json(errorResponse);
|
|
865
|
-
} else if (!res.writableEnded) {
|
|
866
|
-
res.end();
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
});
|
|
870
|
-
app.use(baseUrl, router);
|
|
871
|
-
return app;
|
|
872
|
-
}
|
|
873
|
-
};
|
|
874
857
|
// Annotate the CommonJS export names for ESM import in node:
|
|
875
858
|
0 && (module.exports = {
|
|
876
859
|
A2AError,
|
|
877
|
-
A2AExpressApp,
|
|
878
860
|
DefaultExecutionEventBus,
|
|
879
861
|
DefaultExecutionEventBusManager,
|
|
880
862
|
DefaultRequestHandler,
|
|
863
|
+
ExecutionEventQueue,
|
|
881
864
|
InMemoryTaskStore,
|
|
882
865
|
JsonRpcTransportHandler,
|
|
883
866
|
RequestContext,
|
package/dist/server/index.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import {
|
|
3
|
-
import { A2AResponse } from '../index.cjs';
|
|
4
|
-
import { Express, RequestHandler, ErrorRequestHandler } from 'express';
|
|
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';
|
|
5
3
|
|
|
6
4
|
type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
7
5
|
interface ExecutionEventBus {
|
|
@@ -71,14 +69,41 @@ declare class DefaultExecutionEventBusManager implements ExecutionEventBusManage
|
|
|
71
69
|
cleanupByTaskId(taskId: string): void;
|
|
72
70
|
}
|
|
73
71
|
|
|
72
|
+
/**
|
|
73
|
+
* An async queue that subscribes to an ExecutionEventBus for events
|
|
74
|
+
* and provides an async generator to consume them.
|
|
75
|
+
*/
|
|
76
|
+
declare class ExecutionEventQueue {
|
|
77
|
+
private eventBus;
|
|
78
|
+
private eventQueue;
|
|
79
|
+
private resolvePromise?;
|
|
80
|
+
private stopped;
|
|
81
|
+
private boundHandleEvent;
|
|
82
|
+
constructor(eventBus: ExecutionEventBus);
|
|
83
|
+
private handleEvent;
|
|
84
|
+
private handleFinished;
|
|
85
|
+
/**
|
|
86
|
+
* Provides an async generator that yields events from the event bus.
|
|
87
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
88
|
+
*/
|
|
89
|
+
events(): AsyncGenerator<AgentExecutionEvent, void, undefined>;
|
|
90
|
+
/**
|
|
91
|
+
* Stops the event queue from processing further events.
|
|
92
|
+
*/
|
|
93
|
+
stop(): void;
|
|
94
|
+
}
|
|
95
|
+
|
|
74
96
|
interface A2ARequestHandler {
|
|
75
97
|
getAgentCard(): Promise<AgentCard>;
|
|
98
|
+
getAuthenticatedExtendedAgentCard(): Promise<AgentCard>;
|
|
76
99
|
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
77
100
|
sendMessageStream(params: MessageSendParams): AsyncGenerator<Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
78
101
|
getTask(params: TaskQueryParams): Promise<Task>;
|
|
79
102
|
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
80
103
|
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
81
|
-
getTaskPushNotificationConfig(params: TaskIdParams): Promise<TaskPushNotificationConfig>;
|
|
104
|
+
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig>;
|
|
105
|
+
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig[]>;
|
|
106
|
+
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams): Promise<void>;
|
|
82
107
|
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
83
108
|
}
|
|
84
109
|
|
|
@@ -109,12 +134,14 @@ declare class InMemoryTaskStore implements TaskStore {
|
|
|
109
134
|
|
|
110
135
|
declare class DefaultRequestHandler implements A2ARequestHandler {
|
|
111
136
|
private readonly agentCard;
|
|
137
|
+
private readonly extendedAgentCard?;
|
|
112
138
|
private readonly taskStore;
|
|
113
139
|
private readonly agentExecutor;
|
|
114
140
|
private readonly eventBusManager;
|
|
115
141
|
private readonly pushNotificationConfigs;
|
|
116
|
-
constructor(agentCard: AgentCard, taskStore: TaskStore, agentExecutor: AgentExecutor, eventBusManager?: ExecutionEventBusManager);
|
|
142
|
+
constructor(agentCard: AgentCard, taskStore: TaskStore, agentExecutor: AgentExecutor, eventBusManager?: ExecutionEventBusManager, extendedAgentCard?: AgentCard);
|
|
117
143
|
getAgentCard(): Promise<AgentCard>;
|
|
144
|
+
getAuthenticatedExtendedAgentCard(): Promise<AgentCard>;
|
|
118
145
|
private _createRequestContext;
|
|
119
146
|
private _processEvents;
|
|
120
147
|
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
@@ -122,7 +149,9 @@ declare class DefaultRequestHandler implements A2ARequestHandler {
|
|
|
122
149
|
getTask(params: TaskQueryParams): Promise<Task>;
|
|
123
150
|
cancelTask(params: TaskIdParams): Promise<Task>;
|
|
124
151
|
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig>;
|
|
125
|
-
getTaskPushNotificationConfig(params: TaskIdParams): Promise<TaskPushNotificationConfig>;
|
|
152
|
+
getTaskPushNotificationConfig(params: TaskIdParams | GetTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig>;
|
|
153
|
+
listTaskPushNotificationConfigs(params: ListTaskPushNotificationConfigParams): Promise<TaskPushNotificationConfig[]>;
|
|
154
|
+
deleteTaskPushNotificationConfig(params: DeleteTaskPushNotificationConfigParams): Promise<void>;
|
|
126
155
|
resubscribe(params: TaskIdParams): AsyncGenerator<Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent, void, undefined>;
|
|
127
156
|
}
|
|
128
157
|
|
|
@@ -164,21 +193,7 @@ declare class JsonRpcTransportHandler {
|
|
|
164
193
|
* For streaming methods, it returns an AsyncGenerator of JSONRPCResult.
|
|
165
194
|
* For non-streaming methods, it returns a Promise of a single JSONRPCMessage (Result or ErrorResponse).
|
|
166
195
|
*/
|
|
167
|
-
handle(requestBody: any): Promise<
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
declare class A2AExpressApp {
|
|
171
|
-
private requestHandler;
|
|
172
|
-
private jsonRpcTransportHandler;
|
|
173
|
-
constructor(requestHandler: A2ARequestHandler);
|
|
174
|
-
/**
|
|
175
|
-
* Adds A2A routes to an existing Express app.
|
|
176
|
-
* @param app Optional existing Express app.
|
|
177
|
-
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
178
|
-
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
179
|
-
* @returns The Express app with A2A routes.
|
|
180
|
-
*/
|
|
181
|
-
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler>): Express;
|
|
196
|
+
handle(requestBody: any): Promise<JSONRPCResponse | AsyncGenerator<JSONRPCResponse, void, undefined>>;
|
|
182
197
|
}
|
|
183
198
|
|
|
184
199
|
/**
|
|
@@ -202,6 +217,7 @@ declare class A2AError extends Error {
|
|
|
202
217
|
static taskNotCancelable(taskId: string): A2AError;
|
|
203
218
|
static pushNotificationNotSupported(): A2AError;
|
|
204
219
|
static unsupportedOperation(operation: string): A2AError;
|
|
220
|
+
static authenticatedExtendedCardNotConfigured(): A2AError;
|
|
205
221
|
}
|
|
206
222
|
|
|
207
|
-
export { A2AError,
|
|
223
|
+
export { A2AError, type A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|