@a2a-js/sdk 0.2.4 → 0.2.5
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 +1 -1
- package/dist/client/index.cjs +14 -10
- package/dist/client/index.d.cts +8 -4
- package/dist/client/index.d.ts +8 -4
- package/dist/client/index.js +14 -10
- package/dist/server/index.cjs +64 -62
- package/dist/server/index.d.cts +25 -1
- package/dist/server/index.d.ts +25 -1
- package/dist/server/index.js +63 -62
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -182,7 +182,7 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
182
182
|
artifact: {
|
|
183
183
|
artifactId: "artifact-1",
|
|
184
184
|
name: "artifact-1",
|
|
185
|
-
parts: [{ text: `Task ${
|
|
185
|
+
parts: [{ kind: "text", text: `Task ${taskId} completed.` }],
|
|
186
186
|
},
|
|
187
187
|
append: false, // Each emission is a complete file snapshot
|
|
188
188
|
lastChunk: true, // True for this file artifact
|
package/dist/client/index.cjs
CHANGED
|
@@ -24,21 +24,25 @@ __export(client_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(client_exports);
|
|
25
25
|
|
|
26
26
|
// src/client/client.ts
|
|
27
|
-
var A2AClient = class {
|
|
27
|
+
var A2AClient = class _A2AClient {
|
|
28
28
|
agentBaseUrl;
|
|
29
|
+
agentCardPath;
|
|
29
30
|
agentCardPromise;
|
|
31
|
+
static DEFAULT_AGENT_CARD_PATH = ".well-known/agent.json";
|
|
30
32
|
requestIdCounter = 1;
|
|
31
33
|
serviceEndpointUrl;
|
|
32
34
|
// To be populated from AgentCard after fetching
|
|
33
35
|
/**
|
|
34
36
|
* Constructs an A2AClient instance.
|
|
35
37
|
* It initiates fetching the agent card from the provided agent baseUrl.
|
|
36
|
-
* The Agent Card is
|
|
38
|
+
* The Agent Card is fetched from a path relative to the agentBaseUrl, which defaults to '.well-known/agent.json'.
|
|
37
39
|
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
38
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
40
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
41
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
39
42
|
*/
|
|
40
|
-
constructor(agentBaseUrl) {
|
|
43
|
+
constructor(agentBaseUrl, agentCardPath = _A2AClient.DEFAULT_AGENT_CARD_PATH) {
|
|
41
44
|
this.agentBaseUrl = agentBaseUrl.replace(/\/$/, "");
|
|
45
|
+
this.agentCardPath = agentCardPath.replace(/^\//, "");
|
|
42
46
|
this.agentCardPromise = this._fetchAndCacheAgentCard();
|
|
43
47
|
}
|
|
44
48
|
/**
|
|
@@ -47,7 +51,7 @@ var A2AClient = class {
|
|
|
47
51
|
* @returns A Promise that resolves to the AgentCard.
|
|
48
52
|
*/
|
|
49
53
|
async _fetchAndCacheAgentCard() {
|
|
50
|
-
const agentCardUrl = `${this.agentBaseUrl}
|
|
54
|
+
const agentCardUrl = `${this.agentBaseUrl}/${this.agentCardPath}`;
|
|
51
55
|
try {
|
|
52
56
|
const response = await fetch(agentCardUrl, {
|
|
53
57
|
headers: { "Accept": "application/json" }
|
|
@@ -71,13 +75,13 @@ var A2AClient = class {
|
|
|
71
75
|
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
72
76
|
* Otherwise, it returns the card fetched and cached during client construction.
|
|
73
77
|
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
78
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
74
79
|
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
75
80
|
* @returns A Promise that resolves to the AgentCard.
|
|
76
81
|
*/
|
|
77
|
-
async getAgentCard(agentBaseUrl) {
|
|
82
|
+
async getAgentCard(agentBaseUrl, agentCardPath = _A2AClient.DEFAULT_AGENT_CARD_PATH) {
|
|
78
83
|
if (agentBaseUrl) {
|
|
79
|
-
const
|
|
80
|
-
const agentCardUrl = `${specificAgentBaseUrl}/.well-known/agent.json`;
|
|
84
|
+
const agentCardUrl = `${agentBaseUrl.replace(/\/$/, "")}/${agentCardPath.replace(/^\//, "")}`;
|
|
81
85
|
const response = await fetch(agentCardUrl, {
|
|
82
86
|
headers: { "Accept": "application/json" }
|
|
83
87
|
});
|
|
@@ -133,7 +137,7 @@ var A2AClient = class {
|
|
|
133
137
|
errorBodyText = await httpResponse.text();
|
|
134
138
|
const errorJson = JSON.parse(errorBodyText);
|
|
135
139
|
if (!errorJson.jsonrpc && errorJson.error) {
|
|
136
|
-
throw new Error(`RPC error for ${method}: ${errorJson.error.message} (Code: ${errorJson.error.code}, HTTP Status: ${httpResponse.status}) Data: ${JSON.stringify(errorJson.error.data)}`);
|
|
140
|
+
throw new Error(`RPC error for ${method}: ${errorJson.error.message} (Code: ${errorJson.error.code}, HTTP Status: ${httpResponse.status}) Data: ${JSON.stringify(errorJson.error.data || {})}`);
|
|
137
141
|
} else if (!errorJson.jsonrpc) {
|
|
138
142
|
throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
|
|
139
143
|
}
|
|
@@ -371,7 +375,7 @@ var A2AClient = class {
|
|
|
371
375
|
}
|
|
372
376
|
if (this.isErrorResponse(a2aStreamResponse)) {
|
|
373
377
|
const err = a2aStreamResponse.error;
|
|
374
|
-
throw new Error(`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data)}`);
|
|
378
|
+
throw new Error(`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data || {})}`);
|
|
375
379
|
}
|
|
376
380
|
if (!("result" in a2aStreamResponse) || typeof a2aStreamResponse.result === "undefined") {
|
|
377
381
|
throw new Error(`SSE event JSON-RPC response is missing 'result' field. Data: ${jsonData}`);
|
package/dist/client/index.d.cts
CHANGED
|
@@ -6,17 +6,20 @@ type A2AStreamEventData = Message | Task | TaskStatusUpdateEvent | TaskArtifactU
|
|
|
6
6
|
*/
|
|
7
7
|
declare class A2AClient {
|
|
8
8
|
private agentBaseUrl;
|
|
9
|
+
private agentCardPath;
|
|
9
10
|
private agentCardPromise;
|
|
11
|
+
private static readonly DEFAULT_AGENT_CARD_PATH;
|
|
10
12
|
private requestIdCounter;
|
|
11
13
|
private serviceEndpointUrl?;
|
|
12
14
|
/**
|
|
13
15
|
* Constructs an A2AClient instance.
|
|
14
16
|
* It initiates fetching the agent card from the provided agent baseUrl.
|
|
15
|
-
* The Agent Card is
|
|
17
|
+
* The Agent Card is fetched from a path relative to the agentBaseUrl, which defaults to '.well-known/agent.json'.
|
|
16
18
|
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
17
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
19
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
20
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
18
21
|
*/
|
|
19
|
-
constructor(agentBaseUrl: string);
|
|
22
|
+
constructor(agentBaseUrl: string, agentCardPath?: string);
|
|
20
23
|
/**
|
|
21
24
|
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
22
25
|
* This method is called by the constructor.
|
|
@@ -28,10 +31,11 @@ declare class A2AClient {
|
|
|
28
31
|
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
29
32
|
* Otherwise, it returns the card fetched and cached during client construction.
|
|
30
33
|
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
34
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
31
35
|
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
32
36
|
* @returns A Promise that resolves to the AgentCard.
|
|
33
37
|
*/
|
|
34
|
-
getAgentCard(agentBaseUrl?: string): Promise<AgentCard>;
|
|
38
|
+
getAgentCard(agentBaseUrl?: string, agentCardPath?: string): Promise<AgentCard>;
|
|
35
39
|
/**
|
|
36
40
|
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
37
41
|
* @returns A Promise that resolves to the service endpoint URL string.
|
package/dist/client/index.d.ts
CHANGED
|
@@ -6,17 +6,20 @@ type A2AStreamEventData = Message | Task | TaskStatusUpdateEvent | TaskArtifactU
|
|
|
6
6
|
*/
|
|
7
7
|
declare class A2AClient {
|
|
8
8
|
private agentBaseUrl;
|
|
9
|
+
private agentCardPath;
|
|
9
10
|
private agentCardPromise;
|
|
11
|
+
private static readonly DEFAULT_AGENT_CARD_PATH;
|
|
10
12
|
private requestIdCounter;
|
|
11
13
|
private serviceEndpointUrl?;
|
|
12
14
|
/**
|
|
13
15
|
* Constructs an A2AClient instance.
|
|
14
16
|
* It initiates fetching the agent card from the provided agent baseUrl.
|
|
15
|
-
* The Agent Card is
|
|
17
|
+
* The Agent Card is fetched from a path relative to the agentBaseUrl, which defaults to '.well-known/agent.json'.
|
|
16
18
|
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
17
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
19
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
20
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
18
21
|
*/
|
|
19
|
-
constructor(agentBaseUrl: string);
|
|
22
|
+
constructor(agentBaseUrl: string, agentCardPath?: string);
|
|
20
23
|
/**
|
|
21
24
|
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
22
25
|
* This method is called by the constructor.
|
|
@@ -28,10 +31,11 @@ declare class A2AClient {
|
|
|
28
31
|
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
29
32
|
* Otherwise, it returns the card fetched and cached during client construction.
|
|
30
33
|
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
34
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
31
35
|
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
32
36
|
* @returns A Promise that resolves to the AgentCard.
|
|
33
37
|
*/
|
|
34
|
-
getAgentCard(agentBaseUrl?: string): Promise<AgentCard>;
|
|
38
|
+
getAgentCard(agentBaseUrl?: string, agentCardPath?: string): Promise<AgentCard>;
|
|
35
39
|
/**
|
|
36
40
|
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
37
41
|
* @returns A Promise that resolves to the service endpoint URL string.
|
package/dist/client/index.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
// src/client/client.ts
|
|
2
|
-
var A2AClient = class {
|
|
2
|
+
var A2AClient = class _A2AClient {
|
|
3
3
|
agentBaseUrl;
|
|
4
|
+
agentCardPath;
|
|
4
5
|
agentCardPromise;
|
|
6
|
+
static DEFAULT_AGENT_CARD_PATH = ".well-known/agent.json";
|
|
5
7
|
requestIdCounter = 1;
|
|
6
8
|
serviceEndpointUrl;
|
|
7
9
|
// To be populated from AgentCard after fetching
|
|
8
10
|
/**
|
|
9
11
|
* Constructs an A2AClient instance.
|
|
10
12
|
* It initiates fetching the agent card from the provided agent baseUrl.
|
|
11
|
-
* The Agent Card is
|
|
13
|
+
* The Agent Card is fetched from a path relative to the agentBaseUrl, which defaults to '.well-known/agent.json'.
|
|
12
14
|
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
13
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
15
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
16
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
14
17
|
*/
|
|
15
|
-
constructor(agentBaseUrl) {
|
|
18
|
+
constructor(agentBaseUrl, agentCardPath = _A2AClient.DEFAULT_AGENT_CARD_PATH) {
|
|
16
19
|
this.agentBaseUrl = agentBaseUrl.replace(/\/$/, "");
|
|
20
|
+
this.agentCardPath = agentCardPath.replace(/^\//, "");
|
|
17
21
|
this.agentCardPromise = this._fetchAndCacheAgentCard();
|
|
18
22
|
}
|
|
19
23
|
/**
|
|
@@ -22,7 +26,7 @@ var A2AClient = class {
|
|
|
22
26
|
* @returns A Promise that resolves to the AgentCard.
|
|
23
27
|
*/
|
|
24
28
|
async _fetchAndCacheAgentCard() {
|
|
25
|
-
const agentCardUrl = `${this.agentBaseUrl}
|
|
29
|
+
const agentCardUrl = `${this.agentBaseUrl}/${this.agentCardPath}`;
|
|
26
30
|
try {
|
|
27
31
|
const response = await fetch(agentCardUrl, {
|
|
28
32
|
headers: { "Accept": "application/json" }
|
|
@@ -46,13 +50,13 @@ var A2AClient = class {
|
|
|
46
50
|
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
47
51
|
* Otherwise, it returns the card fetched and cached during client construction.
|
|
48
52
|
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
53
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
49
54
|
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
50
55
|
* @returns A Promise that resolves to the AgentCard.
|
|
51
56
|
*/
|
|
52
|
-
async getAgentCard(agentBaseUrl) {
|
|
57
|
+
async getAgentCard(agentBaseUrl, agentCardPath = _A2AClient.DEFAULT_AGENT_CARD_PATH) {
|
|
53
58
|
if (agentBaseUrl) {
|
|
54
|
-
const
|
|
55
|
-
const agentCardUrl = `${specificAgentBaseUrl}/.well-known/agent.json`;
|
|
59
|
+
const agentCardUrl = `${agentBaseUrl.replace(/\/$/, "")}/${agentCardPath.replace(/^\//, "")}`;
|
|
56
60
|
const response = await fetch(agentCardUrl, {
|
|
57
61
|
headers: { "Accept": "application/json" }
|
|
58
62
|
});
|
|
@@ -108,7 +112,7 @@ var A2AClient = class {
|
|
|
108
112
|
errorBodyText = await httpResponse.text();
|
|
109
113
|
const errorJson = JSON.parse(errorBodyText);
|
|
110
114
|
if (!errorJson.jsonrpc && errorJson.error) {
|
|
111
|
-
throw new Error(`RPC error for ${method}: ${errorJson.error.message} (Code: ${errorJson.error.code}, HTTP Status: ${httpResponse.status}) Data: ${JSON.stringify(errorJson.error.data)}`);
|
|
115
|
+
throw new Error(`RPC error for ${method}: ${errorJson.error.message} (Code: ${errorJson.error.code}, HTTP Status: ${httpResponse.status}) Data: ${JSON.stringify(errorJson.error.data || {})}`);
|
|
112
116
|
} else if (!errorJson.jsonrpc) {
|
|
113
117
|
throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
|
|
114
118
|
}
|
|
@@ -346,7 +350,7 @@ var A2AClient = class {
|
|
|
346
350
|
}
|
|
347
351
|
if (this.isErrorResponse(a2aStreamResponse)) {
|
|
348
352
|
const err = a2aStreamResponse.error;
|
|
349
|
-
throw new Error(`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data)}`);
|
|
353
|
+
throw new Error(`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data || {})}`);
|
|
350
354
|
}
|
|
351
355
|
if (!("result" in a2aStreamResponse) || typeof a2aStreamResponse.result === "undefined") {
|
|
352
356
|
throw new Error(`SSE event JSON-RPC response is missing 'result' field. Data: ${jsonData}`);
|
package/dist/server/index.cjs
CHANGED
|
@@ -34,6 +34,7 @@ __export(server_exports, {
|
|
|
34
34
|
DefaultExecutionEventBus: () => DefaultExecutionEventBus,
|
|
35
35
|
DefaultExecutionEventBusManager: () => DefaultExecutionEventBusManager,
|
|
36
36
|
DefaultRequestHandler: () => DefaultRequestHandler,
|
|
37
|
+
ExecutionEventQueue: () => ExecutionEventQueue,
|
|
37
38
|
InMemoryTaskStore: () => InMemoryTaskStore,
|
|
38
39
|
JsonRpcTransportHandler: () => JsonRpcTransportHandler,
|
|
39
40
|
RequestContext: () => RequestContext,
|
|
@@ -107,6 +108,63 @@ var DefaultExecutionEventBusManager = class {
|
|
|
107
108
|
}
|
|
108
109
|
};
|
|
109
110
|
|
|
111
|
+
// src/server/events/execution_event_queue.ts
|
|
112
|
+
var ExecutionEventQueue = class {
|
|
113
|
+
eventBus;
|
|
114
|
+
eventQueue = [];
|
|
115
|
+
resolvePromise;
|
|
116
|
+
stopped = false;
|
|
117
|
+
boundHandleEvent;
|
|
118
|
+
constructor(eventBus) {
|
|
119
|
+
this.eventBus = eventBus;
|
|
120
|
+
this.eventBus.on("event", this.handleEvent);
|
|
121
|
+
this.eventBus.on("finished", this.handleFinished);
|
|
122
|
+
}
|
|
123
|
+
handleEvent = (event) => {
|
|
124
|
+
if (this.stopped) return;
|
|
125
|
+
this.eventQueue.push(event);
|
|
126
|
+
if (this.resolvePromise) {
|
|
127
|
+
this.resolvePromise();
|
|
128
|
+
this.resolvePromise = void 0;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
handleFinished = () => {
|
|
132
|
+
this.stop();
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Provides an async generator that yields events from the event bus.
|
|
136
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
137
|
+
*/
|
|
138
|
+
async *events() {
|
|
139
|
+
while (!this.stopped || this.eventQueue.length > 0) {
|
|
140
|
+
if (this.eventQueue.length > 0) {
|
|
141
|
+
const event = this.eventQueue.shift();
|
|
142
|
+
yield event;
|
|
143
|
+
if (event.kind === "message" || event.kind === "status-update" && event.final) {
|
|
144
|
+
this.handleFinished();
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
} else if (!this.stopped) {
|
|
148
|
+
await new Promise((resolve) => {
|
|
149
|
+
this.resolvePromise = resolve;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Stops the event queue from processing further events.
|
|
156
|
+
*/
|
|
157
|
+
stop() {
|
|
158
|
+
this.stopped = true;
|
|
159
|
+
if (this.resolvePromise) {
|
|
160
|
+
this.resolvePromise();
|
|
161
|
+
this.resolvePromise = void 0;
|
|
162
|
+
}
|
|
163
|
+
this.eventBus.off("event", this.handleEvent);
|
|
164
|
+
this.eventBus.off("finished", this.handleFinished);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
110
168
|
// src/server/request_handler/default_request_handler.ts
|
|
111
169
|
var import_uuid = require("uuid");
|
|
112
170
|
|
|
@@ -185,63 +243,6 @@ var A2AError = class _A2AError extends Error {
|
|
|
185
243
|
}
|
|
186
244
|
};
|
|
187
245
|
|
|
188
|
-
// src/server/events/execution_event_queue.ts
|
|
189
|
-
var ExecutionEventQueue = class {
|
|
190
|
-
eventBus;
|
|
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);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
246
|
// src/server/result_manager.ts
|
|
246
247
|
var ResultManager = class {
|
|
247
248
|
taskStore;
|
|
@@ -411,11 +412,11 @@ var DefaultRequestHandler = class {
|
|
|
411
412
|
}
|
|
412
413
|
}
|
|
413
414
|
}
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
415
|
+
const contextId = incomingMessage.contextId || task?.contextId || (0, import_uuid.v4)();
|
|
416
|
+
const messageForContext = {
|
|
417
|
+
...incomingMessage,
|
|
418
|
+
contextId
|
|
419
|
+
};
|
|
419
420
|
return new RequestContext(
|
|
420
421
|
messageForContext,
|
|
421
422
|
taskId,
|
|
@@ -878,6 +879,7 @@ var A2AExpressApp = class {
|
|
|
878
879
|
DefaultExecutionEventBus,
|
|
879
880
|
DefaultExecutionEventBusManager,
|
|
880
881
|
DefaultRequestHandler,
|
|
882
|
+
ExecutionEventQueue,
|
|
881
883
|
InMemoryTaskStore,
|
|
882
884
|
JsonRpcTransportHandler,
|
|
883
885
|
RequestContext,
|
package/dist/server/index.d.cts
CHANGED
|
@@ -71,6 +71,30 @@ declare class DefaultExecutionEventBusManager implements ExecutionEventBusManage
|
|
|
71
71
|
cleanupByTaskId(taskId: string): void;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* An async queue that subscribes to an ExecutionEventBus for events
|
|
76
|
+
* and provides an async generator to consume them.
|
|
77
|
+
*/
|
|
78
|
+
declare class ExecutionEventQueue {
|
|
79
|
+
private eventBus;
|
|
80
|
+
private eventQueue;
|
|
81
|
+
private resolvePromise?;
|
|
82
|
+
private stopped;
|
|
83
|
+
private boundHandleEvent;
|
|
84
|
+
constructor(eventBus: ExecutionEventBus);
|
|
85
|
+
private handleEvent;
|
|
86
|
+
private handleFinished;
|
|
87
|
+
/**
|
|
88
|
+
* Provides an async generator that yields events from the event bus.
|
|
89
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
90
|
+
*/
|
|
91
|
+
events(): AsyncGenerator<AgentExecutionEvent, void, undefined>;
|
|
92
|
+
/**
|
|
93
|
+
* Stops the event queue from processing further events.
|
|
94
|
+
*/
|
|
95
|
+
stop(): void;
|
|
96
|
+
}
|
|
97
|
+
|
|
74
98
|
interface A2ARequestHandler {
|
|
75
99
|
getAgentCard(): Promise<AgentCard>;
|
|
76
100
|
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
@@ -204,4 +228,4 @@ declare class A2AError extends Error {
|
|
|
204
228
|
static unsupportedOperation(operation: string): A2AError;
|
|
205
229
|
}
|
|
206
230
|
|
|
207
|
-
export { A2AError, A2AExpressApp, type A2ARequestHandler, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|
|
231
|
+
export { A2AError, A2AExpressApp, type 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
|
@@ -71,6 +71,30 @@ declare class DefaultExecutionEventBusManager implements ExecutionEventBusManage
|
|
|
71
71
|
cleanupByTaskId(taskId: string): void;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* An async queue that subscribes to an ExecutionEventBus for events
|
|
76
|
+
* and provides an async generator to consume them.
|
|
77
|
+
*/
|
|
78
|
+
declare class ExecutionEventQueue {
|
|
79
|
+
private eventBus;
|
|
80
|
+
private eventQueue;
|
|
81
|
+
private resolvePromise?;
|
|
82
|
+
private stopped;
|
|
83
|
+
private boundHandleEvent;
|
|
84
|
+
constructor(eventBus: ExecutionEventBus);
|
|
85
|
+
private handleEvent;
|
|
86
|
+
private handleFinished;
|
|
87
|
+
/**
|
|
88
|
+
* Provides an async generator that yields events from the event bus.
|
|
89
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
90
|
+
*/
|
|
91
|
+
events(): AsyncGenerator<AgentExecutionEvent, void, undefined>;
|
|
92
|
+
/**
|
|
93
|
+
* Stops the event queue from processing further events.
|
|
94
|
+
*/
|
|
95
|
+
stop(): void;
|
|
96
|
+
}
|
|
97
|
+
|
|
74
98
|
interface A2ARequestHandler {
|
|
75
99
|
getAgentCard(): Promise<AgentCard>;
|
|
76
100
|
sendMessage(params: MessageSendParams): Promise<Message | Task>;
|
|
@@ -204,4 +228,4 @@ declare class A2AError extends Error {
|
|
|
204
228
|
static unsupportedOperation(operation: string): A2AError;
|
|
205
229
|
}
|
|
206
230
|
|
|
207
|
-
export { A2AError, A2AExpressApp, type A2ARequestHandler, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|
|
231
|
+
export { A2AError, A2AExpressApp, type A2ARequestHandler, type AgentExecutionEvent, type AgentExecutor, DefaultExecutionEventBus, DefaultExecutionEventBusManager, DefaultRequestHandler, type ExecutionEventBus, type ExecutionEventBusManager, ExecutionEventQueue, InMemoryTaskStore, JsonRpcTransportHandler, RequestContext, ResultManager, type TaskStore };
|
package/dist/server/index.js
CHANGED
|
@@ -64,6 +64,63 @@ var DefaultExecutionEventBusManager = class {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
// src/server/events/execution_event_queue.ts
|
|
68
|
+
var ExecutionEventQueue = class {
|
|
69
|
+
eventBus;
|
|
70
|
+
eventQueue = [];
|
|
71
|
+
resolvePromise;
|
|
72
|
+
stopped = false;
|
|
73
|
+
boundHandleEvent;
|
|
74
|
+
constructor(eventBus) {
|
|
75
|
+
this.eventBus = eventBus;
|
|
76
|
+
this.eventBus.on("event", this.handleEvent);
|
|
77
|
+
this.eventBus.on("finished", this.handleFinished);
|
|
78
|
+
}
|
|
79
|
+
handleEvent = (event) => {
|
|
80
|
+
if (this.stopped) return;
|
|
81
|
+
this.eventQueue.push(event);
|
|
82
|
+
if (this.resolvePromise) {
|
|
83
|
+
this.resolvePromise();
|
|
84
|
+
this.resolvePromise = void 0;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
handleFinished = () => {
|
|
88
|
+
this.stop();
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Provides an async generator that yields events from the event bus.
|
|
92
|
+
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
93
|
+
*/
|
|
94
|
+
async *events() {
|
|
95
|
+
while (!this.stopped || this.eventQueue.length > 0) {
|
|
96
|
+
if (this.eventQueue.length > 0) {
|
|
97
|
+
const event = this.eventQueue.shift();
|
|
98
|
+
yield event;
|
|
99
|
+
if (event.kind === "message" || event.kind === "status-update" && event.final) {
|
|
100
|
+
this.handleFinished();
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
} else if (!this.stopped) {
|
|
104
|
+
await new Promise((resolve) => {
|
|
105
|
+
this.resolvePromise = resolve;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Stops the event queue from processing further events.
|
|
112
|
+
*/
|
|
113
|
+
stop() {
|
|
114
|
+
this.stopped = true;
|
|
115
|
+
if (this.resolvePromise) {
|
|
116
|
+
this.resolvePromise();
|
|
117
|
+
this.resolvePromise = void 0;
|
|
118
|
+
}
|
|
119
|
+
this.eventBus.off("event", this.handleEvent);
|
|
120
|
+
this.eventBus.off("finished", this.handleFinished);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
67
124
|
// src/server/request_handler/default_request_handler.ts
|
|
68
125
|
import { v4 as uuidv4 } from "uuid";
|
|
69
126
|
|
|
@@ -142,63 +199,6 @@ var A2AError = class _A2AError extends Error {
|
|
|
142
199
|
}
|
|
143
200
|
};
|
|
144
201
|
|
|
145
|
-
// src/server/events/execution_event_queue.ts
|
|
146
|
-
var ExecutionEventQueue = class {
|
|
147
|
-
eventBus;
|
|
148
|
-
eventQueue = [];
|
|
149
|
-
resolvePromise;
|
|
150
|
-
stopped = false;
|
|
151
|
-
boundHandleEvent;
|
|
152
|
-
constructor(eventBus) {
|
|
153
|
-
this.eventBus = eventBus;
|
|
154
|
-
this.eventBus.on("event", this.handleEvent);
|
|
155
|
-
this.eventBus.on("finished", this.handleFinished);
|
|
156
|
-
}
|
|
157
|
-
handleEvent = (event) => {
|
|
158
|
-
if (this.stopped) return;
|
|
159
|
-
this.eventQueue.push(event);
|
|
160
|
-
if (this.resolvePromise) {
|
|
161
|
-
this.resolvePromise();
|
|
162
|
-
this.resolvePromise = void 0;
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
handleFinished = () => {
|
|
166
|
-
this.stop();
|
|
167
|
-
};
|
|
168
|
-
/**
|
|
169
|
-
* Provides an async generator that yields events from the event bus.
|
|
170
|
-
* Stops when a Message event is received or a TaskStatusUpdateEvent with final=true is received.
|
|
171
|
-
*/
|
|
172
|
-
async *events() {
|
|
173
|
-
while (!this.stopped || this.eventQueue.length > 0) {
|
|
174
|
-
if (this.eventQueue.length > 0) {
|
|
175
|
-
const event = this.eventQueue.shift();
|
|
176
|
-
yield event;
|
|
177
|
-
if (event.kind === "message" || event.kind === "status-update" && event.final) {
|
|
178
|
-
this.handleFinished();
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
} else if (!this.stopped) {
|
|
182
|
-
await new Promise((resolve) => {
|
|
183
|
-
this.resolvePromise = resolve;
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Stops the event queue from processing further events.
|
|
190
|
-
*/
|
|
191
|
-
stop() {
|
|
192
|
-
this.stopped = true;
|
|
193
|
-
if (this.resolvePromise) {
|
|
194
|
-
this.resolvePromise();
|
|
195
|
-
this.resolvePromise = void 0;
|
|
196
|
-
}
|
|
197
|
-
this.eventBus.off("event", this.handleEvent);
|
|
198
|
-
this.eventBus.off("finished", this.handleFinished);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
202
|
// src/server/result_manager.ts
|
|
203
203
|
var ResultManager = class {
|
|
204
204
|
taskStore;
|
|
@@ -368,11 +368,11 @@ var DefaultRequestHandler = class {
|
|
|
368
368
|
}
|
|
369
369
|
}
|
|
370
370
|
}
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
371
|
+
const contextId = incomingMessage.contextId || task?.contextId || uuidv4();
|
|
372
|
+
const messageForContext = {
|
|
373
|
+
...incomingMessage,
|
|
374
|
+
contextId
|
|
375
|
+
};
|
|
376
376
|
return new RequestContext(
|
|
377
377
|
messageForContext,
|
|
378
378
|
taskId,
|
|
@@ -834,6 +834,7 @@ export {
|
|
|
834
834
|
DefaultExecutionEventBus,
|
|
835
835
|
DefaultExecutionEventBusManager,
|
|
836
836
|
DefaultRequestHandler,
|
|
837
|
+
ExecutionEventQueue,
|
|
837
838
|
InMemoryTaskStore,
|
|
838
839
|
JsonRpcTransportHandler,
|
|
839
840
|
RequestContext,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a2a-js/sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Server & Client SDK for Agent2Agent protocol",
|
|
5
5
|
"repository": "google-a2a/a2a-js.git",
|
|
6
6
|
"engines": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"clean": "gts clean",
|
|
54
54
|
"build": "tsup",
|
|
55
55
|
"pretest": "npm run build",
|
|
56
|
-
"test": "mocha
|
|
56
|
+
"test": "mocha test/**/*.spec.ts",
|
|
57
57
|
"coverage": "c8 npm run test",
|
|
58
58
|
"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",
|
|
59
59
|
"sample:cli": "tsx src/samples/cli.ts",
|
|
@@ -66,5 +66,8 @@
|
|
|
66
66
|
"cors": "^2.8.5",
|
|
67
67
|
"express": "^4.21.2",
|
|
68
68
|
"uuid": "^11.1.0"
|
|
69
|
+
},
|
|
70
|
+
"mocha": {
|
|
71
|
+
"require": "tsx"
|
|
69
72
|
}
|
|
70
73
|
}
|