@a2a-js/sdk 0.3.2 → 0.3.3
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 +406 -315
- package/dist/client/index.cjs +138 -71
- package/dist/client/index.d.cts +45 -33
- package/dist/client/index.d.ts +45 -33
- package/dist/client/index.js +138 -71
- package/package.json +4 -4
package/dist/client/index.js
CHANGED
|
@@ -3,94 +3,80 @@ import {
|
|
|
3
3
|
} from "../chunk-67JNQ6TZ.js";
|
|
4
4
|
|
|
5
5
|
// src/client/client.ts
|
|
6
|
-
var A2AClient = class {
|
|
6
|
+
var A2AClient = class _A2AClient {
|
|
7
7
|
agentCardPromise;
|
|
8
8
|
requestIdCounter = 1;
|
|
9
9
|
serviceEndpointUrl;
|
|
10
10
|
// To be populated from AgentCard after fetching
|
|
11
|
-
|
|
11
|
+
customFetchImpl;
|
|
12
12
|
/**
|
|
13
|
-
* Constructs an A2AClient instance.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* 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)
|
|
18
|
-
* @param options Optional. The options for the A2AClient including the fetch implementation, agent card path, and authentication handler.
|
|
19
|
-
*/
|
|
20
|
-
constructor(agentBaseUrl, options) {
|
|
21
|
-
this.fetchImpl = options?.fetchImpl ?? fetch;
|
|
22
|
-
this.agentCardPromise = this._fetchAndCacheAgentCard(agentBaseUrl, options?.agentCardPath);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
26
|
-
* This method is called by the constructor.
|
|
27
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
28
|
-
* @param agentCardPath path to the agent card, defaults to .well-known/agent-card.json
|
|
29
|
-
* @returns A Promise that resolves to the AgentCard.
|
|
13
|
+
* Constructs an A2AClient instance from an AgentCard.
|
|
14
|
+
* @param agentCard The AgentCard object.
|
|
15
|
+
* @param options Optional. The options for the A2AClient including the fetch/auth implementation.
|
|
30
16
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!response.ok) {
|
|
38
|
-
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
39
|
-
}
|
|
40
|
-
const agentCard = await response.json();
|
|
17
|
+
constructor(agentCard, options) {
|
|
18
|
+
this.customFetchImpl = options?.fetchImpl;
|
|
19
|
+
if (typeof agentCard === "string") {
|
|
20
|
+
console.warn("Warning: Constructing A2AClient with a URL is deprecated. Please use A2AClient.fromCardUrl() instead.");
|
|
21
|
+
this.agentCardPromise = this._fetchAndCacheAgentCard(agentCard, options?.agentCardPath);
|
|
22
|
+
} else {
|
|
41
23
|
if (!agentCard.url) {
|
|
42
|
-
throw new Error("
|
|
24
|
+
throw new Error("Provided Agent Card does not contain a valid 'url' for the service endpoint.");
|
|
43
25
|
}
|
|
44
26
|
this.serviceEndpointUrl = agentCard.url;
|
|
45
|
-
|
|
46
|
-
} catch (error) {
|
|
47
|
-
console.error("Error fetching or parsing Agent Card:", error);
|
|
48
|
-
throw error;
|
|
27
|
+
this.agentCardPromise = Promise.resolve(agentCard);
|
|
49
28
|
}
|
|
50
29
|
}
|
|
51
30
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
56
|
-
* @
|
|
57
|
-
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
58
|
-
* @returns A Promise that resolves to the AgentCard.
|
|
31
|
+
* Dynamically resolves the fetch implementation to use for requests.
|
|
32
|
+
* Prefers a custom implementation if provided, otherwise falls back to the global fetch.
|
|
33
|
+
* @returns The fetch implementation.
|
|
34
|
+
* @param args Arguments to pass to the fetch implementation.
|
|
35
|
+
* @throws If no fetch implementation is available.
|
|
59
36
|
*/
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
const response = await this.fetchImpl(agentCardUrl, {
|
|
64
|
-
headers: { "Accept": "application/json" }
|
|
65
|
-
});
|
|
66
|
-
if (!response.ok) {
|
|
67
|
-
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
68
|
-
}
|
|
69
|
-
return await response.json();
|
|
37
|
+
_fetch(...args) {
|
|
38
|
+
if (this.customFetchImpl) {
|
|
39
|
+
return this.customFetchImpl(...args);
|
|
70
40
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*/
|
|
78
|
-
resolveAgentCardUrl(agentBaseUrl, agentCardPath = AGENT_CARD_PATH) {
|
|
79
|
-
return `${agentBaseUrl.replace(/\/$/, "")}/${agentCardPath.replace(/^\//, "")}`;
|
|
41
|
+
if (typeof fetch === "function") {
|
|
42
|
+
return fetch(...args);
|
|
43
|
+
}
|
|
44
|
+
throw new Error(
|
|
45
|
+
"A `fetch` implementation was not provided and is not available in the global scope. Please provide a `fetchImpl` in the A2AClientOptions. For earlier Node.js versions (pre-v18), you can use a library like `node-fetch`."
|
|
46
|
+
);
|
|
80
47
|
}
|
|
81
48
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
49
|
+
* Creates an A2AClient instance by fetching the AgentCard from a URL then constructing the A2AClient.
|
|
50
|
+
* @param agentCardUrl The URL of the agent card.
|
|
51
|
+
* @param options Optional. The options for the A2AClient including the fetch/auth implementation.
|
|
52
|
+
* @returns A Promise that resolves to a new A2AClient instance.
|
|
84
53
|
*/
|
|
85
|
-
async
|
|
86
|
-
|
|
87
|
-
|
|
54
|
+
static async fromCardUrl(agentCardUrl, options) {
|
|
55
|
+
const fetchImpl = options?.fetchImpl;
|
|
56
|
+
const requestInit = {
|
|
57
|
+
headers: { "Accept": "application/json" }
|
|
58
|
+
};
|
|
59
|
+
let response;
|
|
60
|
+
if (fetchImpl) {
|
|
61
|
+
response = await fetchImpl(agentCardUrl, requestInit);
|
|
62
|
+
} else if (typeof fetch === "function") {
|
|
63
|
+
response = await fetch(agentCardUrl, requestInit);
|
|
64
|
+
} else {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"A `fetch` implementation was not provided and is not available in the global scope. Please provide a `fetchImpl` in the A2AClientOptions. For earlier Node.js versions (pre-v18), you can use a library like `node-fetch`."
|
|
67
|
+
);
|
|
88
68
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
throw new Error("Agent Card URL for RPC endpoint is not available. Fetching might have failed.");
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
92
71
|
}
|
|
93
|
-
|
|
72
|
+
let agentCard;
|
|
73
|
+
try {
|
|
74
|
+
agentCard = await response.json();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error("Failed to parse Agent Card JSON:", error);
|
|
77
|
+
throw new Error(`Failed to parse Agent Card JSON from ${agentCardUrl}. Original error: ${error.message}`);
|
|
78
|
+
}
|
|
79
|
+
return new _A2AClient(agentCard, options);
|
|
94
80
|
}
|
|
95
81
|
/**
|
|
96
82
|
* Helper method to make a generic JSON-RPC POST request.
|
|
@@ -149,7 +135,7 @@ var A2AClient = class {
|
|
|
149
135
|
},
|
|
150
136
|
body: JSON.stringify(rpcRequest)
|
|
151
137
|
};
|
|
152
|
-
return this.
|
|
138
|
+
return this._fetch(url, requestInit);
|
|
153
139
|
}
|
|
154
140
|
/**
|
|
155
141
|
* Sends a message to the agent.
|
|
@@ -269,7 +255,7 @@ var A2AClient = class {
|
|
|
269
255
|
params,
|
|
270
256
|
id: clientRequestId
|
|
271
257
|
};
|
|
272
|
-
const response = await this.
|
|
258
|
+
const response = await this._fetch(endpoint, {
|
|
273
259
|
method: "POST",
|
|
274
260
|
headers: {
|
|
275
261
|
"Content-Type": "application/json",
|
|
@@ -384,6 +370,87 @@ var A2AClient = class {
|
|
|
384
370
|
isErrorResponse(response) {
|
|
385
371
|
return "error" in response;
|
|
386
372
|
}
|
|
373
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
374
|
+
// Functions used to support old A2AClient Constructor to be deprecated soon
|
|
375
|
+
// TODOs:
|
|
376
|
+
// * remove `agentCardPromise`, and just use agentCard initialized
|
|
377
|
+
// * _getServiceEndpoint can be made synchronous or deleted and accessed via
|
|
378
|
+
// agentCard.url
|
|
379
|
+
// * getAgentCard changed to this.agentCard
|
|
380
|
+
// * delete resolveAgentCardUrl(), _fetchAndCacheAgentCard(),
|
|
381
|
+
// agentCardPath from A2AClientOptions
|
|
382
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
383
|
+
/**
|
|
384
|
+
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
385
|
+
* This method is called by the constructor.
|
|
386
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
387
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent-card.json
|
|
388
|
+
* @returns A Promise that resolves to the AgentCard.
|
|
389
|
+
*/
|
|
390
|
+
async _fetchAndCacheAgentCard(agentBaseUrl, agentCardPath) {
|
|
391
|
+
try {
|
|
392
|
+
const agentCardUrl = this.resolveAgentCardUrl(agentBaseUrl, agentCardPath);
|
|
393
|
+
const response = await this._fetch(agentCardUrl, {
|
|
394
|
+
headers: { "Accept": "application/json" }
|
|
395
|
+
});
|
|
396
|
+
if (!response.ok) {
|
|
397
|
+
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
398
|
+
}
|
|
399
|
+
const agentCard = await response.json();
|
|
400
|
+
if (!agentCard.url) {
|
|
401
|
+
throw new Error("Fetched Agent Card does not contain a valid 'url' for the service endpoint.");
|
|
402
|
+
}
|
|
403
|
+
this.serviceEndpointUrl = agentCard.url;
|
|
404
|
+
return agentCard;
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.error("Error fetching or parsing Agent Card:", error);
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Retrieves the Agent Card.
|
|
412
|
+
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
413
|
+
* Otherwise, it returns the card fetched and cached during client construction.
|
|
414
|
+
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
415
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent-card.json
|
|
416
|
+
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
417
|
+
* @returns A Promise that resolves to the AgentCard.
|
|
418
|
+
*/
|
|
419
|
+
async getAgentCard(agentBaseUrl, agentCardPath) {
|
|
420
|
+
if (agentBaseUrl) {
|
|
421
|
+
const agentCardUrl = this.resolveAgentCardUrl(agentBaseUrl, agentCardPath);
|
|
422
|
+
const response = await this._fetch(agentCardUrl, {
|
|
423
|
+
headers: { "Accept": "application/json" }
|
|
424
|
+
});
|
|
425
|
+
if (!response.ok) {
|
|
426
|
+
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
427
|
+
}
|
|
428
|
+
return await response.json();
|
|
429
|
+
}
|
|
430
|
+
return this.agentCardPromise;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Determines the agent card URL based on the agent URL.
|
|
434
|
+
* @param agentBaseUrl The agent URL.
|
|
435
|
+
* @param agentCardPath Optional relative path to the agent card, defaults to .well-known/agent-card.json
|
|
436
|
+
*/
|
|
437
|
+
resolveAgentCardUrl(agentBaseUrl, agentCardPath = AGENT_CARD_PATH) {
|
|
438
|
+
return `${agentBaseUrl.replace(/\/$/, "")}/${agentCardPath.replace(/^\//, "")}`;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
442
|
+
* @returns A Promise that resolves to the service endpoint URL string.
|
|
443
|
+
*/
|
|
444
|
+
async _getServiceEndpoint() {
|
|
445
|
+
if (this.serviceEndpointUrl) {
|
|
446
|
+
return this.serviceEndpointUrl;
|
|
447
|
+
}
|
|
448
|
+
await this.agentCardPromise;
|
|
449
|
+
if (!this.serviceEndpointUrl) {
|
|
450
|
+
throw new Error("Agent Card URL for RPC endpoint is not available. Fetching might have failed.");
|
|
451
|
+
}
|
|
452
|
+
return this.serviceEndpointUrl;
|
|
453
|
+
}
|
|
387
454
|
};
|
|
388
455
|
|
|
389
456
|
// src/client/auth-handler.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a2a-js/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Server & Client SDK for Agent2Agent protocol",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"@genkit-ai/googleai": "^1.8.0",
|
|
44
44
|
"@genkit-ai/vertexai": "^1.8.0",
|
|
45
45
|
"@types/chai": "^5.2.2",
|
|
46
|
-
"@types/express": "^
|
|
46
|
+
"@types/express": "^5.0.3",
|
|
47
47
|
"@types/mocha": "^10.0.10",
|
|
48
48
|
"@types/node": "^22.13.14",
|
|
49
49
|
"@types/sinon": "^17.0.4",
|
|
50
50
|
"c8": "^10.1.3",
|
|
51
51
|
"chai": "^5.2.0",
|
|
52
|
-
"express": "^
|
|
52
|
+
"express": "^5.1.0",
|
|
53
53
|
"genkit": "^1.8.0",
|
|
54
54
|
"gts": "^6.0.2",
|
|
55
55
|
"json-schema-to-typescript": "^15.0.4",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"uuid": "^11.1.0"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
|
-
"express": "^4.21.2"
|
|
76
|
+
"express": "^4.21.2 || ^5.1.0"
|
|
77
77
|
},
|
|
78
78
|
"peerDependenciesMeta": {
|
|
79
79
|
"express": {
|