@a2a-js/sdk 0.3.6 → 0.3.7
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 +7 -7
- package/dist/chunk-SJNAG4AL.js +122 -0
- package/dist/client/index.cjs +289 -51
- package/dist/client/index.d.cts +60 -2
- package/dist/client/index.d.ts +60 -2
- package/dist/client/index.js +245 -95
- package/dist/server/express/index.cjs +19 -16
- package/dist/server/express/index.js +9 -38
- package/dist/server/index.cjs +184 -6
- package/dist/server/index.d.cts +68 -8
- package/dist/server/index.d.ts +68 -8
- package/dist/server/index.js +184 -6
- package/package.json +9 -15
package/dist/server/index.cjs
CHANGED
|
@@ -54,16 +54,194 @@ var RequestContext = class {
|
|
|
54
54
|
};
|
|
55
55
|
|
|
56
56
|
// src/server/events/execution_event_bus.ts
|
|
57
|
-
var
|
|
58
|
-
|
|
59
|
-
constructor() {
|
|
60
|
-
super();
|
|
57
|
+
var CustomEventImpl = typeof CustomEvent !== "undefined" ? CustomEvent : class CustomEventPolyfill extends Event {
|
|
58
|
+
detail;
|
|
59
|
+
constructor(type, eventInitDict) {
|
|
60
|
+
super(type, eventInitDict);
|
|
61
|
+
this.detail = eventInitDict?.detail ?? null;
|
|
61
62
|
}
|
|
63
|
+
};
|
|
64
|
+
function isAgentExecutionCustomEvent(e) {
|
|
65
|
+
return e instanceof CustomEventImpl;
|
|
66
|
+
}
|
|
67
|
+
var DefaultExecutionEventBus = class extends EventTarget {
|
|
68
|
+
// Separate storage for each event type - both use the interface's Listener type
|
|
69
|
+
// but are invoked differently (with event payload vs. no arguments)
|
|
70
|
+
eventListeners = /* @__PURE__ */ new Map();
|
|
71
|
+
finishedListeners = /* @__PURE__ */ new Map();
|
|
62
72
|
publish(event) {
|
|
63
|
-
this.
|
|
73
|
+
this.dispatchEvent(new CustomEventImpl("event", { detail: event }));
|
|
64
74
|
}
|
|
65
75
|
finished() {
|
|
66
|
-
this.
|
|
76
|
+
this.dispatchEvent(new Event("finished"));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* EventEmitter-compatible 'on' method.
|
|
80
|
+
* Wraps the listener to extract event detail from CustomEvent.
|
|
81
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
82
|
+
* @param eventName The event name to listen for.
|
|
83
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
84
|
+
* @returns This instance for method chaining.
|
|
85
|
+
*/
|
|
86
|
+
on(eventName, listener) {
|
|
87
|
+
if (eventName === "event") {
|
|
88
|
+
this.addEventListenerInternal(listener);
|
|
89
|
+
} else {
|
|
90
|
+
this.addFinishedListenerInternal(listener);
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* EventEmitter-compatible 'off' method.
|
|
96
|
+
* Uses the stored wrapped listener for proper removal.
|
|
97
|
+
* Removes at most one instance of a listener per call (like EventEmitter).
|
|
98
|
+
* @param eventName The event name to stop listening for.
|
|
99
|
+
* @param listener The callback function to remove.
|
|
100
|
+
* @returns This instance for method chaining.
|
|
101
|
+
*/
|
|
102
|
+
off(eventName, listener) {
|
|
103
|
+
if (eventName === "event") {
|
|
104
|
+
this.removeEventListenerInternal(listener);
|
|
105
|
+
} else {
|
|
106
|
+
this.removeFinishedListenerInternal(listener);
|
|
107
|
+
}
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* EventEmitter-compatible 'once' method.
|
|
112
|
+
* Listener is automatically removed after first invocation.
|
|
113
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
114
|
+
* @param eventName The event name to listen for once.
|
|
115
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
116
|
+
* @returns This instance for method chaining.
|
|
117
|
+
*/
|
|
118
|
+
once(eventName, listener) {
|
|
119
|
+
if (eventName === "event") {
|
|
120
|
+
this.addEventListenerOnceInternal(listener);
|
|
121
|
+
} else {
|
|
122
|
+
this.addFinishedListenerOnceInternal(listener);
|
|
123
|
+
}
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* EventEmitter-compatible 'removeAllListeners' method.
|
|
128
|
+
* Removes all listeners for a specific event or all events.
|
|
129
|
+
* @param eventName Optional event name to remove listeners for. If omitted, removes all.
|
|
130
|
+
* @returns This instance for method chaining.
|
|
131
|
+
*/
|
|
132
|
+
removeAllListeners(eventName) {
|
|
133
|
+
if (eventName === void 0 || eventName === "event") {
|
|
134
|
+
for (const wrappedListeners of this.eventListeners.values()) {
|
|
135
|
+
for (const wrapped of wrappedListeners) {
|
|
136
|
+
this.removeEventListener("event", wrapped);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
this.eventListeners.clear();
|
|
140
|
+
}
|
|
141
|
+
if (eventName === void 0 || eventName === "finished") {
|
|
142
|
+
for (const wrappedListeners of this.finishedListeners.values()) {
|
|
143
|
+
for (const wrapped of wrappedListeners) {
|
|
144
|
+
this.removeEventListener("finished", wrapped);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
this.finishedListeners.clear();
|
|
148
|
+
}
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
// ========================
|
|
152
|
+
// Helper methods for listener tracking
|
|
153
|
+
// ========================
|
|
154
|
+
/**
|
|
155
|
+
* Adds a wrapped listener to the tracking map.
|
|
156
|
+
*/
|
|
157
|
+
trackListener(listenerMap, listener, wrapped) {
|
|
158
|
+
const existing = listenerMap.get(listener);
|
|
159
|
+
if (existing) {
|
|
160
|
+
existing.push(wrapped);
|
|
161
|
+
} else {
|
|
162
|
+
listenerMap.set(listener, [wrapped]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Removes a wrapped listener from the tracking map (for once cleanup).
|
|
167
|
+
*/
|
|
168
|
+
untrackWrappedListener(listenerMap, listener, wrapped) {
|
|
169
|
+
const wrappedList = listenerMap.get(listener);
|
|
170
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
171
|
+
const index = wrappedList.indexOf(wrapped);
|
|
172
|
+
if (index !== -1) {
|
|
173
|
+
wrappedList.splice(index, 1);
|
|
174
|
+
if (wrappedList.length === 0) {
|
|
175
|
+
listenerMap.delete(listener);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// ========================
|
|
181
|
+
// Internal methods for 'event' listeners
|
|
182
|
+
// ========================
|
|
183
|
+
addEventListenerInternal(listener) {
|
|
184
|
+
const wrapped = (e) => {
|
|
185
|
+
if (!isAgentExecutionCustomEvent(e)) {
|
|
186
|
+
throw new Error('Internal error: expected CustomEvent for "event" type');
|
|
187
|
+
}
|
|
188
|
+
listener.call(this, e.detail);
|
|
189
|
+
};
|
|
190
|
+
this.trackListener(this.eventListeners, listener, wrapped);
|
|
191
|
+
this.addEventListener("event", wrapped);
|
|
192
|
+
}
|
|
193
|
+
removeEventListenerInternal(listener) {
|
|
194
|
+
const wrappedList = this.eventListeners.get(listener);
|
|
195
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
196
|
+
const wrapped = wrappedList.pop();
|
|
197
|
+
if (wrappedList.length === 0) {
|
|
198
|
+
this.eventListeners.delete(listener);
|
|
199
|
+
}
|
|
200
|
+
this.removeEventListener("event", wrapped);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
addEventListenerOnceInternal(listener) {
|
|
204
|
+
const wrapped = (e) => {
|
|
205
|
+
if (!isAgentExecutionCustomEvent(e)) {
|
|
206
|
+
throw new Error('Internal error: expected CustomEvent for "event" type');
|
|
207
|
+
}
|
|
208
|
+
this.untrackWrappedListener(this.eventListeners, listener, wrapped);
|
|
209
|
+
listener.call(this, e.detail);
|
|
210
|
+
};
|
|
211
|
+
this.trackListener(this.eventListeners, listener, wrapped);
|
|
212
|
+
this.addEventListener("event", wrapped, { once: true });
|
|
213
|
+
}
|
|
214
|
+
// ========================
|
|
215
|
+
// Internal methods for 'finished' listeners
|
|
216
|
+
// ========================
|
|
217
|
+
// The interface declares listeners as (event: AgentExecutionEvent) => void,
|
|
218
|
+
// but for 'finished' events they are invoked with no arguments (EventEmitter behavior).
|
|
219
|
+
// We use Function.prototype.call to invoke with `this` as the event bus (matching
|
|
220
|
+
// EventEmitter semantics) and no arguments, which is type-safe.
|
|
221
|
+
addFinishedListenerInternal(listener) {
|
|
222
|
+
const wrapped = () => {
|
|
223
|
+
listener.call(this);
|
|
224
|
+
};
|
|
225
|
+
this.trackListener(this.finishedListeners, listener, wrapped);
|
|
226
|
+
this.addEventListener("finished", wrapped);
|
|
227
|
+
}
|
|
228
|
+
removeFinishedListenerInternal(listener) {
|
|
229
|
+
const wrappedList = this.finishedListeners.get(listener);
|
|
230
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
231
|
+
const wrapped = wrappedList.pop();
|
|
232
|
+
if (wrappedList.length === 0) {
|
|
233
|
+
this.finishedListeners.delete(listener);
|
|
234
|
+
}
|
|
235
|
+
this.removeEventListener("finished", wrapped);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
addFinishedListenerOnceInternal(listener) {
|
|
239
|
+
const wrapped = () => {
|
|
240
|
+
this.untrackWrappedListener(this.finishedListeners, listener, wrapped);
|
|
241
|
+
listener.call(this);
|
|
242
|
+
};
|
|
243
|
+
this.trackListener(this.finishedListeners, listener, wrapped);
|
|
244
|
+
this.addEventListener("finished", wrapped, { once: true });
|
|
67
245
|
}
|
|
68
246
|
};
|
|
69
247
|
|
package/dist/server/index.d.cts
CHANGED
|
@@ -1,21 +1,81 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
1
|
import { F as Message, ay as Task, aQ as TaskStatusUpdateEvent, aS as TaskArtifactUpdateEvent, z as PushNotificationConfig, ae as AgentCard, x as MessageSendParams, X as TaskQueryParams, Z as TaskIdParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams, j as JSONRPCResponse, aw as JSONRPCError } from '../extensions-DvruCIzw.cjs';
|
|
3
2
|
import { S as ServerCallContext, A as A2ARequestHandler } from '../a2a_request_handler-B3LxMq3P.cjs';
|
|
4
3
|
export { a as UnauthenticatedUser, U as User } from '../a2a_request_handler-B3LxMq3P.cjs';
|
|
5
4
|
|
|
6
5
|
type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
6
|
+
/**
|
|
7
|
+
* Event names supported by ExecutionEventBus.
|
|
8
|
+
*/
|
|
9
|
+
type ExecutionEventName = 'event' | 'finished';
|
|
7
10
|
interface ExecutionEventBus {
|
|
8
11
|
publish(event: AgentExecutionEvent): void;
|
|
9
|
-
on(eventName:
|
|
10
|
-
off(eventName:
|
|
11
|
-
once(eventName:
|
|
12
|
-
removeAllListeners(eventName?:
|
|
12
|
+
on(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
13
|
+
off(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
14
|
+
once(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
15
|
+
removeAllListeners(eventName?: ExecutionEventName): this;
|
|
13
16
|
finished(): void;
|
|
14
17
|
}
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Web-compatible ExecutionEventBus using EventTarget.
|
|
20
|
+
* Works across all modern runtimes: Node.js 15+, browsers, Cloudflare Workers, Deno, Bun.
|
|
21
|
+
*
|
|
22
|
+
* This implementation provides the subset of EventEmitter methods defined in the
|
|
23
|
+
* ExecutionEventBus interface. Users extending DefaultExecutionEventBus should note
|
|
24
|
+
* that other EventEmitter methods (e.g., listenerCount, rawListeners) are not available.
|
|
25
|
+
*/
|
|
26
|
+
declare class DefaultExecutionEventBus extends EventTarget implements ExecutionEventBus {
|
|
27
|
+
private readonly eventListeners;
|
|
28
|
+
private readonly finishedListeners;
|
|
17
29
|
publish(event: AgentExecutionEvent): void;
|
|
18
30
|
finished(): void;
|
|
31
|
+
/**
|
|
32
|
+
* EventEmitter-compatible 'on' method.
|
|
33
|
+
* Wraps the listener to extract event detail from CustomEvent.
|
|
34
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
35
|
+
* @param eventName The event name to listen for.
|
|
36
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
37
|
+
* @returns This instance for method chaining.
|
|
38
|
+
*/
|
|
39
|
+
on(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
40
|
+
/**
|
|
41
|
+
* EventEmitter-compatible 'off' method.
|
|
42
|
+
* Uses the stored wrapped listener for proper removal.
|
|
43
|
+
* Removes at most one instance of a listener per call (like EventEmitter).
|
|
44
|
+
* @param eventName The event name to stop listening for.
|
|
45
|
+
* @param listener The callback function to remove.
|
|
46
|
+
* @returns This instance for method chaining.
|
|
47
|
+
*/
|
|
48
|
+
off(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
49
|
+
/**
|
|
50
|
+
* EventEmitter-compatible 'once' method.
|
|
51
|
+
* Listener is automatically removed after first invocation.
|
|
52
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
53
|
+
* @param eventName The event name to listen for once.
|
|
54
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
55
|
+
* @returns This instance for method chaining.
|
|
56
|
+
*/
|
|
57
|
+
once(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
58
|
+
/**
|
|
59
|
+
* EventEmitter-compatible 'removeAllListeners' method.
|
|
60
|
+
* Removes all listeners for a specific event or all events.
|
|
61
|
+
* @param eventName Optional event name to remove listeners for. If omitted, removes all.
|
|
62
|
+
* @returns This instance for method chaining.
|
|
63
|
+
*/
|
|
64
|
+
removeAllListeners(eventName?: ExecutionEventName): this;
|
|
65
|
+
/**
|
|
66
|
+
* Adds a wrapped listener to the tracking map.
|
|
67
|
+
*/
|
|
68
|
+
private trackListener;
|
|
69
|
+
/**
|
|
70
|
+
* Removes a wrapped listener from the tracking map (for once cleanup).
|
|
71
|
+
*/
|
|
72
|
+
private untrackWrappedListener;
|
|
73
|
+
private addEventListenerInternal;
|
|
74
|
+
private removeEventListenerInternal;
|
|
75
|
+
private addEventListenerOnceInternal;
|
|
76
|
+
private addFinishedListenerInternal;
|
|
77
|
+
private removeFinishedListenerInternal;
|
|
78
|
+
private addFinishedListenerOnceInternal;
|
|
19
79
|
}
|
|
20
80
|
|
|
21
81
|
declare class RequestContext {
|
|
@@ -253,4 +313,4 @@ declare class DefaultPushNotificationSender implements PushNotificationSender {
|
|
|
253
313
|
private _dispatchNotification;
|
|
254
314
|
}
|
|
255
315
|
|
|
256
|
-
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultPushNotificationSender, type DefaultPushNotificationSenderOptions, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, type ExtendedAgentCardProvider, InMemoryPushNotificationStore, InMemoryTaskStore, JsonRpcTransportHandler, type PushNotificationSender, type PushNotificationStore, RequestContext, ResultManager, ServerCallContext, type TaskStore };
|
|
316
|
+
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultPushNotificationSender, type DefaultPushNotificationSenderOptions, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, type ExecutionEventName, ExecutionEventQueue, type ExtendedAgentCardProvider, InMemoryPushNotificationStore, InMemoryTaskStore, JsonRpcTransportHandler, type PushNotificationSender, type PushNotificationStore, RequestContext, ResultManager, ServerCallContext, type TaskStore };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,21 +1,81 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
1
|
import { F as Message, ay as Task, aQ as TaskStatusUpdateEvent, aS as TaskArtifactUpdateEvent, z as PushNotificationConfig, ae as AgentCard, x as MessageSendParams, X as TaskQueryParams, Z as TaskIdParams, $ as TaskPushNotificationConfig, a3 as GetTaskPushNotificationConfigParams, a7 as ListTaskPushNotificationConfigParams, a9 as DeleteTaskPushNotificationConfigParams, j as JSONRPCResponse, aw as JSONRPCError } from '../extensions-DvruCIzw.js';
|
|
3
2
|
import { S as ServerCallContext, A as A2ARequestHandler } from '../a2a_request_handler-BuP9LgXH.js';
|
|
4
3
|
export { a as UnauthenticatedUser, U as User } from '../a2a_request_handler-BuP9LgXH.js';
|
|
5
4
|
|
|
6
5
|
type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
6
|
+
/**
|
|
7
|
+
* Event names supported by ExecutionEventBus.
|
|
8
|
+
*/
|
|
9
|
+
type ExecutionEventName = 'event' | 'finished';
|
|
7
10
|
interface ExecutionEventBus {
|
|
8
11
|
publish(event: AgentExecutionEvent): void;
|
|
9
|
-
on(eventName:
|
|
10
|
-
off(eventName:
|
|
11
|
-
once(eventName:
|
|
12
|
-
removeAllListeners(eventName?:
|
|
12
|
+
on(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
13
|
+
off(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
14
|
+
once(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
15
|
+
removeAllListeners(eventName?: ExecutionEventName): this;
|
|
13
16
|
finished(): void;
|
|
14
17
|
}
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Web-compatible ExecutionEventBus using EventTarget.
|
|
20
|
+
* Works across all modern runtimes: Node.js 15+, browsers, Cloudflare Workers, Deno, Bun.
|
|
21
|
+
*
|
|
22
|
+
* This implementation provides the subset of EventEmitter methods defined in the
|
|
23
|
+
* ExecutionEventBus interface. Users extending DefaultExecutionEventBus should note
|
|
24
|
+
* that other EventEmitter methods (e.g., listenerCount, rawListeners) are not available.
|
|
25
|
+
*/
|
|
26
|
+
declare class DefaultExecutionEventBus extends EventTarget implements ExecutionEventBus {
|
|
27
|
+
private readonly eventListeners;
|
|
28
|
+
private readonly finishedListeners;
|
|
17
29
|
publish(event: AgentExecutionEvent): void;
|
|
18
30
|
finished(): void;
|
|
31
|
+
/**
|
|
32
|
+
* EventEmitter-compatible 'on' method.
|
|
33
|
+
* Wraps the listener to extract event detail from CustomEvent.
|
|
34
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
35
|
+
* @param eventName The event name to listen for.
|
|
36
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
37
|
+
* @returns This instance for method chaining.
|
|
38
|
+
*/
|
|
39
|
+
on(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
40
|
+
/**
|
|
41
|
+
* EventEmitter-compatible 'off' method.
|
|
42
|
+
* Uses the stored wrapped listener for proper removal.
|
|
43
|
+
* Removes at most one instance of a listener per call (like EventEmitter).
|
|
44
|
+
* @param eventName The event name to stop listening for.
|
|
45
|
+
* @param listener The callback function to remove.
|
|
46
|
+
* @returns This instance for method chaining.
|
|
47
|
+
*/
|
|
48
|
+
off(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
49
|
+
/**
|
|
50
|
+
* EventEmitter-compatible 'once' method.
|
|
51
|
+
* Listener is automatically removed after first invocation.
|
|
52
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
53
|
+
* @param eventName The event name to listen for once.
|
|
54
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
55
|
+
* @returns This instance for method chaining.
|
|
56
|
+
*/
|
|
57
|
+
once(eventName: ExecutionEventName, listener: (event: AgentExecutionEvent) => void): this;
|
|
58
|
+
/**
|
|
59
|
+
* EventEmitter-compatible 'removeAllListeners' method.
|
|
60
|
+
* Removes all listeners for a specific event or all events.
|
|
61
|
+
* @param eventName Optional event name to remove listeners for. If omitted, removes all.
|
|
62
|
+
* @returns This instance for method chaining.
|
|
63
|
+
*/
|
|
64
|
+
removeAllListeners(eventName?: ExecutionEventName): this;
|
|
65
|
+
/**
|
|
66
|
+
* Adds a wrapped listener to the tracking map.
|
|
67
|
+
*/
|
|
68
|
+
private trackListener;
|
|
69
|
+
/**
|
|
70
|
+
* Removes a wrapped listener from the tracking map (for once cleanup).
|
|
71
|
+
*/
|
|
72
|
+
private untrackWrappedListener;
|
|
73
|
+
private addEventListenerInternal;
|
|
74
|
+
private removeEventListenerInternal;
|
|
75
|
+
private addEventListenerOnceInternal;
|
|
76
|
+
private addFinishedListenerInternal;
|
|
77
|
+
private removeFinishedListenerInternal;
|
|
78
|
+
private addFinishedListenerOnceInternal;
|
|
19
79
|
}
|
|
20
80
|
|
|
21
81
|
declare class RequestContext {
|
|
@@ -253,4 +313,4 @@ declare class DefaultPushNotificationSender implements PushNotificationSender {
|
|
|
253
313
|
private _dispatchNotification;
|
|
254
314
|
}
|
|
255
315
|
|
|
256
|
-
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultPushNotificationSender, type DefaultPushNotificationSenderOptions, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, type ExtendedAgentCardProvider, InMemoryPushNotificationStore, InMemoryTaskStore, JsonRpcTransportHandler, type PushNotificationSender, type PushNotificationStore, RequestContext, ResultManager, ServerCallContext, type TaskStore };
|
|
316
|
+
export { A2AError, A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultPushNotificationSender, type DefaultPushNotificationSenderOptions, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, type ExecutionEventName, ExecutionEventQueue, type ExtendedAgentCardProvider, InMemoryPushNotificationStore, InMemoryTaskStore, JsonRpcTransportHandler, type PushNotificationSender, type PushNotificationStore, RequestContext, ResultManager, ServerCallContext, type TaskStore };
|
package/dist/server/index.js
CHANGED
|
@@ -25,16 +25,194 @@ var RequestContext = class {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
// src/server/events/execution_event_bus.ts
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
constructor() {
|
|
31
|
-
super();
|
|
28
|
+
var CustomEventImpl = typeof CustomEvent !== "undefined" ? CustomEvent : class CustomEventPolyfill extends Event {
|
|
29
|
+
detail;
|
|
30
|
+
constructor(type, eventInitDict) {
|
|
31
|
+
super(type, eventInitDict);
|
|
32
|
+
this.detail = eventInitDict?.detail ?? null;
|
|
32
33
|
}
|
|
34
|
+
};
|
|
35
|
+
function isAgentExecutionCustomEvent(e) {
|
|
36
|
+
return e instanceof CustomEventImpl;
|
|
37
|
+
}
|
|
38
|
+
var DefaultExecutionEventBus = class extends EventTarget {
|
|
39
|
+
// Separate storage for each event type - both use the interface's Listener type
|
|
40
|
+
// but are invoked differently (with event payload vs. no arguments)
|
|
41
|
+
eventListeners = /* @__PURE__ */ new Map();
|
|
42
|
+
finishedListeners = /* @__PURE__ */ new Map();
|
|
33
43
|
publish(event) {
|
|
34
|
-
this.
|
|
44
|
+
this.dispatchEvent(new CustomEventImpl("event", { detail: event }));
|
|
35
45
|
}
|
|
36
46
|
finished() {
|
|
37
|
-
this.
|
|
47
|
+
this.dispatchEvent(new Event("finished"));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* EventEmitter-compatible 'on' method.
|
|
51
|
+
* Wraps the listener to extract event detail from CustomEvent.
|
|
52
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
53
|
+
* @param eventName The event name to listen for.
|
|
54
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
55
|
+
* @returns This instance for method chaining.
|
|
56
|
+
*/
|
|
57
|
+
on(eventName, listener) {
|
|
58
|
+
if (eventName === "event") {
|
|
59
|
+
this.addEventListenerInternal(listener);
|
|
60
|
+
} else {
|
|
61
|
+
this.addFinishedListenerInternal(listener);
|
|
62
|
+
}
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* EventEmitter-compatible 'off' method.
|
|
67
|
+
* Uses the stored wrapped listener for proper removal.
|
|
68
|
+
* Removes at most one instance of a listener per call (like EventEmitter).
|
|
69
|
+
* @param eventName The event name to stop listening for.
|
|
70
|
+
* @param listener The callback function to remove.
|
|
71
|
+
* @returns This instance for method chaining.
|
|
72
|
+
*/
|
|
73
|
+
off(eventName, listener) {
|
|
74
|
+
if (eventName === "event") {
|
|
75
|
+
this.removeEventListenerInternal(listener);
|
|
76
|
+
} else {
|
|
77
|
+
this.removeFinishedListenerInternal(listener);
|
|
78
|
+
}
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* EventEmitter-compatible 'once' method.
|
|
83
|
+
* Listener is automatically removed after first invocation.
|
|
84
|
+
* Supports multiple registrations of the same listener (like EventEmitter).
|
|
85
|
+
* @param eventName The event name to listen for once.
|
|
86
|
+
* @param listener The callback function to invoke when the event is emitted.
|
|
87
|
+
* @returns This instance for method chaining.
|
|
88
|
+
*/
|
|
89
|
+
once(eventName, listener) {
|
|
90
|
+
if (eventName === "event") {
|
|
91
|
+
this.addEventListenerOnceInternal(listener);
|
|
92
|
+
} else {
|
|
93
|
+
this.addFinishedListenerOnceInternal(listener);
|
|
94
|
+
}
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* EventEmitter-compatible 'removeAllListeners' method.
|
|
99
|
+
* Removes all listeners for a specific event or all events.
|
|
100
|
+
* @param eventName Optional event name to remove listeners for. If omitted, removes all.
|
|
101
|
+
* @returns This instance for method chaining.
|
|
102
|
+
*/
|
|
103
|
+
removeAllListeners(eventName) {
|
|
104
|
+
if (eventName === void 0 || eventName === "event") {
|
|
105
|
+
for (const wrappedListeners of this.eventListeners.values()) {
|
|
106
|
+
for (const wrapped of wrappedListeners) {
|
|
107
|
+
this.removeEventListener("event", wrapped);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this.eventListeners.clear();
|
|
111
|
+
}
|
|
112
|
+
if (eventName === void 0 || eventName === "finished") {
|
|
113
|
+
for (const wrappedListeners of this.finishedListeners.values()) {
|
|
114
|
+
for (const wrapped of wrappedListeners) {
|
|
115
|
+
this.removeEventListener("finished", wrapped);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
this.finishedListeners.clear();
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
// ========================
|
|
123
|
+
// Helper methods for listener tracking
|
|
124
|
+
// ========================
|
|
125
|
+
/**
|
|
126
|
+
* Adds a wrapped listener to the tracking map.
|
|
127
|
+
*/
|
|
128
|
+
trackListener(listenerMap, listener, wrapped) {
|
|
129
|
+
const existing = listenerMap.get(listener);
|
|
130
|
+
if (existing) {
|
|
131
|
+
existing.push(wrapped);
|
|
132
|
+
} else {
|
|
133
|
+
listenerMap.set(listener, [wrapped]);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Removes a wrapped listener from the tracking map (for once cleanup).
|
|
138
|
+
*/
|
|
139
|
+
untrackWrappedListener(listenerMap, listener, wrapped) {
|
|
140
|
+
const wrappedList = listenerMap.get(listener);
|
|
141
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
142
|
+
const index = wrappedList.indexOf(wrapped);
|
|
143
|
+
if (index !== -1) {
|
|
144
|
+
wrappedList.splice(index, 1);
|
|
145
|
+
if (wrappedList.length === 0) {
|
|
146
|
+
listenerMap.delete(listener);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// ========================
|
|
152
|
+
// Internal methods for 'event' listeners
|
|
153
|
+
// ========================
|
|
154
|
+
addEventListenerInternal(listener) {
|
|
155
|
+
const wrapped = (e) => {
|
|
156
|
+
if (!isAgentExecutionCustomEvent(e)) {
|
|
157
|
+
throw new Error('Internal error: expected CustomEvent for "event" type');
|
|
158
|
+
}
|
|
159
|
+
listener.call(this, e.detail);
|
|
160
|
+
};
|
|
161
|
+
this.trackListener(this.eventListeners, listener, wrapped);
|
|
162
|
+
this.addEventListener("event", wrapped);
|
|
163
|
+
}
|
|
164
|
+
removeEventListenerInternal(listener) {
|
|
165
|
+
const wrappedList = this.eventListeners.get(listener);
|
|
166
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
167
|
+
const wrapped = wrappedList.pop();
|
|
168
|
+
if (wrappedList.length === 0) {
|
|
169
|
+
this.eventListeners.delete(listener);
|
|
170
|
+
}
|
|
171
|
+
this.removeEventListener("event", wrapped);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
addEventListenerOnceInternal(listener) {
|
|
175
|
+
const wrapped = (e) => {
|
|
176
|
+
if (!isAgentExecutionCustomEvent(e)) {
|
|
177
|
+
throw new Error('Internal error: expected CustomEvent for "event" type');
|
|
178
|
+
}
|
|
179
|
+
this.untrackWrappedListener(this.eventListeners, listener, wrapped);
|
|
180
|
+
listener.call(this, e.detail);
|
|
181
|
+
};
|
|
182
|
+
this.trackListener(this.eventListeners, listener, wrapped);
|
|
183
|
+
this.addEventListener("event", wrapped, { once: true });
|
|
184
|
+
}
|
|
185
|
+
// ========================
|
|
186
|
+
// Internal methods for 'finished' listeners
|
|
187
|
+
// ========================
|
|
188
|
+
// The interface declares listeners as (event: AgentExecutionEvent) => void,
|
|
189
|
+
// but for 'finished' events they are invoked with no arguments (EventEmitter behavior).
|
|
190
|
+
// We use Function.prototype.call to invoke with `this` as the event bus (matching
|
|
191
|
+
// EventEmitter semantics) and no arguments, which is type-safe.
|
|
192
|
+
addFinishedListenerInternal(listener) {
|
|
193
|
+
const wrapped = () => {
|
|
194
|
+
listener.call(this);
|
|
195
|
+
};
|
|
196
|
+
this.trackListener(this.finishedListeners, listener, wrapped);
|
|
197
|
+
this.addEventListener("finished", wrapped);
|
|
198
|
+
}
|
|
199
|
+
removeFinishedListenerInternal(listener) {
|
|
200
|
+
const wrappedList = this.finishedListeners.get(listener);
|
|
201
|
+
if (wrappedList && wrappedList.length > 0) {
|
|
202
|
+
const wrapped = wrappedList.pop();
|
|
203
|
+
if (wrappedList.length === 0) {
|
|
204
|
+
this.finishedListeners.delete(listener);
|
|
205
|
+
}
|
|
206
|
+
this.removeEventListener("finished", wrapped);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
addFinishedListenerOnceInternal(listener) {
|
|
210
|
+
const wrapped = () => {
|
|
211
|
+
this.untrackWrappedListener(this.finishedListeners, listener, wrapped);
|
|
212
|
+
listener.call(this);
|
|
213
|
+
};
|
|
214
|
+
this.trackListener(this.finishedListeners, listener, wrapped);
|
|
215
|
+
this.addEventListener("finished", wrapped, { once: true });
|
|
38
216
|
}
|
|
39
217
|
};
|
|
40
218
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a2a-js/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "Server & Client SDK for Agent2Agent protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -41,15 +41,12 @@
|
|
|
41
41
|
"README.md"
|
|
42
42
|
],
|
|
43
43
|
"devDependencies": {
|
|
44
|
+
"@cloudflare/vitest-pool-workers": "^0.10.15",
|
|
44
45
|
"@eslint/js": "^9.39.1",
|
|
45
|
-
"@types/chai": "^5.2.2",
|
|
46
46
|
"@types/express": "^5.0.3",
|
|
47
|
-
"@types/mocha": "^10.0.10",
|
|
48
47
|
"@types/node": "^22.13.14",
|
|
49
|
-
"@types/sinon": "^17.0.4",
|
|
50
48
|
"@types/supertest": "^6.0.3",
|
|
51
|
-
"
|
|
52
|
-
"chai": "^5.2.0",
|
|
49
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
53
50
|
"esbuild": "^0.27.0",
|
|
54
51
|
"eslint": "^9.39.1",
|
|
55
52
|
"eslint-config-prettier": "^10.1.8",
|
|
@@ -57,25 +54,25 @@
|
|
|
57
54
|
"express": "^5.1.0",
|
|
58
55
|
"gts": "^6.0.2",
|
|
59
56
|
"json-schema-to-typescript": "^15.0.4",
|
|
60
|
-
"mocha": "^11.6.0",
|
|
61
57
|
"prettier": "^3.6.2",
|
|
62
|
-
"sinon": "^20.0.0",
|
|
63
58
|
"supertest": "^7.1.4",
|
|
64
59
|
"tsup": "^8.5.0",
|
|
65
60
|
"tsx": "^4.19.3",
|
|
66
61
|
"typescript": "^5.9.3",
|
|
67
|
-
"typescript-eslint": "^8.46.4"
|
|
62
|
+
"typescript-eslint": "^8.46.4",
|
|
63
|
+
"vitest": "^3.2.4"
|
|
68
64
|
},
|
|
69
65
|
"scripts": {
|
|
70
66
|
"clean": "gts clean",
|
|
71
67
|
"build": "tsup",
|
|
72
|
-
"test": "
|
|
68
|
+
"test": "vitest run",
|
|
69
|
+
"test:edge": "vitest run --config vitest.edge.config.ts",
|
|
73
70
|
"lint": "tsc --noEmit && npx eslint .",
|
|
74
71
|
"format:readme": "prettier --write ./README.md",
|
|
75
72
|
"lint:fix": "npx eslint . --fix",
|
|
76
|
-
"coverage": "
|
|
73
|
+
"coverage": "vitest run --coverage",
|
|
77
74
|
"generate": "curl https://raw.githubusercontent.com/google-a2a/A2A/refs/heads/main/specification/json/a2a.json > spec.json && node scripts/generateTypes.js && rm spec.json",
|
|
78
|
-
"test-build
|
|
75
|
+
"test-build": "esbuild ./dist/client/index.js ./dist/server/index.js ./dist/index.js --bundle --platform=neutral --outdir=dist/tmp-checks --outbase=./dist"
|
|
79
76
|
},
|
|
80
77
|
"dependencies": {
|
|
81
78
|
"uuid": "^11.1.0"
|
|
@@ -87,8 +84,5 @@
|
|
|
87
84
|
"express": {
|
|
88
85
|
"optional": true
|
|
89
86
|
}
|
|
90
|
-
},
|
|
91
|
-
"mocha": {
|
|
92
|
-
"require": "tsx"
|
|
93
87
|
}
|
|
94
88
|
}
|