@a2a-js/sdk 0.3.1 → 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/chunk-67JNQ6TZ.js +6 -0
- package/dist/client/index.cjs +200 -83
- package/dist/client/index.d.cts +120 -28
- package/dist/client/index.d.ts +120 -28
- package/dist/client/index.js +199 -82
- package/dist/index.cjs +14 -0
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +6 -0
- package/dist/server/express/index.cjs +6 -2
- package/dist/server/express/index.d.cts +2 -1
- package/dist/server/express/index.d.ts +2 -1
- package/dist/server/express/index.js +6 -2
- package/package.json +4 -4
package/dist/client/index.d.ts
CHANGED
|
@@ -1,46 +1,39 @@
|
|
|
1
1
|
import { ac as AgentCard, w as MessageSendParams, S as SendMessageResponse, B as Message, aw as Task, aO as TaskStatusUpdateEvent, aQ as TaskArtifactUpdateEvent, Z as TaskPushNotificationConfig, b as SetTaskPushNotificationConfigResponse, X as TaskIdParams, c as GetTaskPushNotificationConfigResponse, V as TaskQueryParams, G as GetTaskResponse, C as CancelTaskResponse, i as JSONRPCResponse, J as JSONRPCErrorResponse } from '../types-DNKcmF0f.js';
|
|
2
2
|
|
|
3
3
|
type A2AStreamEventData = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
4
|
+
interface A2AClientOptions {
|
|
5
|
+
agentCardPath?: string;
|
|
6
|
+
fetchImpl?: typeof fetch;
|
|
7
|
+
}
|
|
4
8
|
/**
|
|
5
9
|
* A2AClient is a TypeScript HTTP client for interacting with A2A-compliant agents.
|
|
6
10
|
*/
|
|
7
11
|
declare class A2AClient {
|
|
8
|
-
private agentBaseUrl;
|
|
9
|
-
private agentCardPath;
|
|
10
12
|
private agentCardPromise;
|
|
11
|
-
private static readonly DEFAULT_AGENT_CARD_PATH;
|
|
12
13
|
private requestIdCounter;
|
|
13
14
|
private serviceEndpointUrl?;
|
|
15
|
+
private customFetchImpl?;
|
|
14
16
|
/**
|
|
15
|
-
* Constructs an A2AClient instance.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
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
|
|
17
|
+
* Constructs an A2AClient instance from an AgentCard.
|
|
18
|
+
* @param agentCard The AgentCard object.
|
|
19
|
+
* @param options Optional. The options for the A2AClient including the fetch/auth implementation.
|
|
21
20
|
*/
|
|
22
|
-
constructor(
|
|
21
|
+
constructor(agentCard: AgentCard | string, options?: A2AClientOptions);
|
|
23
22
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @returns
|
|
23
|
+
* Dynamically resolves the fetch implementation to use for requests.
|
|
24
|
+
* Prefers a custom implementation if provided, otherwise falls back to the global fetch.
|
|
25
|
+
* @returns The fetch implementation.
|
|
26
|
+
* @param args Arguments to pass to the fetch implementation.
|
|
27
|
+
* @throws If no fetch implementation is available.
|
|
27
28
|
*/
|
|
28
|
-
private
|
|
29
|
+
private _fetch;
|
|
29
30
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
34
|
-
* @param agentCardPath path to the agent card, defaults to .well-known/agent.json
|
|
35
|
-
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
36
|
-
* @returns A Promise that resolves to the AgentCard.
|
|
37
|
-
*/
|
|
38
|
-
getAgentCard(agentBaseUrl?: string, agentCardPath?: string): Promise<AgentCard>;
|
|
39
|
-
/**
|
|
40
|
-
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
41
|
-
* @returns A Promise that resolves to the service endpoint URL string.
|
|
31
|
+
* Creates an A2AClient instance by fetching the AgentCard from a URL then constructing the A2AClient.
|
|
32
|
+
* @param agentCardUrl The URL of the agent card.
|
|
33
|
+
* @param options Optional. The options for the A2AClient including the fetch/auth implementation.
|
|
34
|
+
* @returns A Promise that resolves to a new A2AClient instance.
|
|
42
35
|
*/
|
|
43
|
-
|
|
36
|
+
static fromCardUrl(agentCardUrl: string, options?: A2AClientOptions): Promise<A2AClient>;
|
|
44
37
|
/**
|
|
45
38
|
* Helper method to make a generic JSON-RPC POST request.
|
|
46
39
|
* @param method The RPC method name.
|
|
@@ -48,6 +41,14 @@ declare class A2AClient {
|
|
|
48
41
|
* @returns A Promise that resolves to the RPC response.
|
|
49
42
|
*/
|
|
50
43
|
private _postRpcRequest;
|
|
44
|
+
/**
|
|
45
|
+
* Internal helper method to fetch the RPC service endpoint.
|
|
46
|
+
* @param url The URL to fetch.
|
|
47
|
+
* @param rpcRequest The JSON-RPC request to send.
|
|
48
|
+
* @param acceptHeader The Accept header to use. Defaults to "application/json".
|
|
49
|
+
* @returns A Promise that resolves to the fetch HTTP response.
|
|
50
|
+
*/
|
|
51
|
+
private _fetchRpc;
|
|
51
52
|
/**
|
|
52
53
|
* Sends a message to the agent.
|
|
53
54
|
* The behavior (blocking/non-blocking) and push notification configuration
|
|
@@ -119,6 +120,97 @@ declare class A2AClient {
|
|
|
119
120
|
*/
|
|
120
121
|
private _processSseEventData;
|
|
121
122
|
isErrorResponse(response: JSONRPCResponse): response is JSONRPCErrorResponse;
|
|
123
|
+
/**
|
|
124
|
+
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
125
|
+
* This method is called by the constructor.
|
|
126
|
+
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com)
|
|
127
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent-card.json
|
|
128
|
+
* @returns A Promise that resolves to the AgentCard.
|
|
129
|
+
*/
|
|
130
|
+
private _fetchAndCacheAgentCard;
|
|
131
|
+
/**
|
|
132
|
+
* Retrieves the Agent Card.
|
|
133
|
+
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
134
|
+
* Otherwise, it returns the card fetched and cached during client construction.
|
|
135
|
+
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
136
|
+
* @param agentCardPath path to the agent card, defaults to .well-known/agent-card.json
|
|
137
|
+
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
138
|
+
* @returns A Promise that resolves to the AgentCard.
|
|
139
|
+
*/
|
|
140
|
+
getAgentCard(agentBaseUrl?: string, agentCardPath?: string): Promise<AgentCard>;
|
|
141
|
+
/**
|
|
142
|
+
* Determines the agent card URL based on the agent URL.
|
|
143
|
+
* @param agentBaseUrl The agent URL.
|
|
144
|
+
* @param agentCardPath Optional relative path to the agent card, defaults to .well-known/agent-card.json
|
|
145
|
+
*/
|
|
146
|
+
private resolveAgentCardUrl;
|
|
147
|
+
/**
|
|
148
|
+
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
149
|
+
* @returns A Promise that resolves to the service endpoint URL string.
|
|
150
|
+
*/
|
|
151
|
+
private _getServiceEndpoint;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface HttpHeaders {
|
|
155
|
+
[key: string]: string;
|
|
122
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Generic interface for handling authentication for HTTP requests.
|
|
159
|
+
*
|
|
160
|
+
* - For each HTTP request, this handler is called to provide additional headers to the request through
|
|
161
|
+
* the headers() function.
|
|
162
|
+
* - After the server returns a response, the shouldRetryWithHeaders() function is called. Usually this
|
|
163
|
+
* function responds to a 401 or 403 response or JSON-RPC codes, but can respond to any other signal -
|
|
164
|
+
* that is an implementation detail of the AuthenticationHandler.
|
|
165
|
+
* - If the shouldRetryWithHeaders() function returns new headers, then the request should retried with the provided
|
|
166
|
+
* revised headers. These provisional headers may, or may not, be optimistically stored for subsequent requests -
|
|
167
|
+
* that is an implementation detail of the AuthenticationHandler.
|
|
168
|
+
* - If the request is successful and the onSuccessfulRetry() is defined, then the onSuccessfulRetry() function is
|
|
169
|
+
* called with the headers that were used to successfully complete the request. This callback provides an
|
|
170
|
+
* opportunity to save the headers for subsequent requests if they were not already saved.
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
173
|
+
interface AuthenticationHandler {
|
|
174
|
+
/**
|
|
175
|
+
* Provides additional HTTP request headers.
|
|
176
|
+
* @returns HTTP headers which may include Authorization if available.
|
|
177
|
+
*/
|
|
178
|
+
headers: () => Promise<HttpHeaders>;
|
|
179
|
+
/**
|
|
180
|
+
* For every HTTP response (even 200s) the shouldRetryWithHeaders() method is called.
|
|
181
|
+
* This method is supposed to check if the request needs to be retried and if, yes,
|
|
182
|
+
* return a set of headers. An A2A server might indicate auth failures in its response
|
|
183
|
+
* by JSON-rpc codes, HTTP codes like 401, 403 or headers like WWW-Authenticate.
|
|
184
|
+
*
|
|
185
|
+
* @param req The RequestInit object used to invoke fetch()
|
|
186
|
+
* @param res The fetch Response object
|
|
187
|
+
* @returns If the HTTP request should be retried then returns the HTTP headers to use,
|
|
188
|
+
* or returns undefined if no retry should be made.
|
|
189
|
+
*/
|
|
190
|
+
shouldRetryWithHeaders: (req: RequestInit, res: Response) => Promise<HttpHeaders | undefined>;
|
|
191
|
+
/**
|
|
192
|
+
* If the last HTTP request using the headers from shouldRetryWithHeaders() was successful, and
|
|
193
|
+
* this function is implemented, then it will be called with the headers provided from
|
|
194
|
+
* shouldRetryWithHeaders().
|
|
195
|
+
*
|
|
196
|
+
* This callback allows transient headers to be saved for subsequent requests only when they
|
|
197
|
+
* are validated by the server.
|
|
198
|
+
*/
|
|
199
|
+
onSuccessfulRetry?: (headers: HttpHeaders) => Promise<void>;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Higher-order function that wraps fetch with authentication handling logic.
|
|
203
|
+
* Returns a new fetch function that automatically handles authentication retries for 401/403 responses.
|
|
204
|
+
*
|
|
205
|
+
* @param fetchImpl The underlying fetch implementation to wrap
|
|
206
|
+
* @param authHandler Authentication handler for managing auth headers and retries
|
|
207
|
+
* @returns A new fetch function with authentication handling capabilities
|
|
208
|
+
*
|
|
209
|
+
* Usage examples:
|
|
210
|
+
* - const authFetch = createAuthHandlingFetch(fetch, authHandler);
|
|
211
|
+
* - const response = await authFetch(url, options);
|
|
212
|
+
* - const response = await authFetch(url); // Direct function call
|
|
213
|
+
*/
|
|
214
|
+
declare function createAuthenticatingFetchWithRetry(fetchImpl: typeof fetch, authHandler: AuthenticationHandler): typeof fetch;
|
|
123
215
|
|
|
124
|
-
export { A2AClient };
|
|
216
|
+
export { A2AClient, type A2AClientOptions, type AuthenticationHandler, type HttpHeaders, createAuthenticatingFetchWithRetry };
|
package/dist/client/index.js
CHANGED
|
@@ -1,85 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AGENT_CARD_PATH
|
|
3
|
+
} from "../chunk-67JNQ6TZ.js";
|
|
4
|
+
|
|
1
5
|
// src/client/client.ts
|
|
2
6
|
var A2AClient = class _A2AClient {
|
|
3
|
-
agentBaseUrl;
|
|
4
|
-
agentCardPath;
|
|
5
7
|
agentCardPromise;
|
|
6
|
-
static DEFAULT_AGENT_CARD_PATH = ".well-known/agent.json";
|
|
7
8
|
requestIdCounter = 1;
|
|
8
9
|
serviceEndpointUrl;
|
|
9
10
|
// To be populated from AgentCard after fetching
|
|
11
|
+
customFetchImpl;
|
|
10
12
|
/**
|
|
11
|
-
* Constructs an A2AClient instance.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
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
|
|
17
|
-
*/
|
|
18
|
-
constructor(agentBaseUrl, agentCardPath = _A2AClient.DEFAULT_AGENT_CARD_PATH) {
|
|
19
|
-
this.agentBaseUrl = agentBaseUrl.replace(/\/$/, "");
|
|
20
|
-
this.agentCardPath = agentCardPath.replace(/^\//, "");
|
|
21
|
-
this.agentCardPromise = this._fetchAndCacheAgentCard();
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
25
|
-
* This method is called by the constructor.
|
|
26
|
-
* @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.
|
|
27
16
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!response.ok) {
|
|
35
|
-
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
36
|
-
}
|
|
37
|
-
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 {
|
|
38
23
|
if (!agentCard.url) {
|
|
39
|
-
throw new Error("
|
|
24
|
+
throw new Error("Provided Agent Card does not contain a valid 'url' for the service endpoint.");
|
|
40
25
|
}
|
|
41
26
|
this.serviceEndpointUrl = agentCard.url;
|
|
42
|
-
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.error("Error fetching or parsing Agent Card:");
|
|
45
|
-
throw error;
|
|
27
|
+
this.agentCardPromise = Promise.resolve(agentCard);
|
|
46
28
|
}
|
|
47
29
|
}
|
|
48
30
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* @param
|
|
53
|
-
* @
|
|
54
|
-
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
55
|
-
* @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.
|
|
56
36
|
*/
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
const response = await fetch(agentCardUrl, {
|
|
61
|
-
headers: { "Accept": "application/json" }
|
|
62
|
-
});
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
throw new Error(`Failed to fetch Agent Card from ${agentCardUrl}: ${response.status} ${response.statusText}`);
|
|
65
|
-
}
|
|
66
|
-
return await response.json();
|
|
37
|
+
_fetch(...args) {
|
|
38
|
+
if (this.customFetchImpl) {
|
|
39
|
+
return this.customFetchImpl(...args);
|
|
67
40
|
}
|
|
68
|
-
|
|
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
|
+
);
|
|
69
47
|
}
|
|
70
48
|
/**
|
|
71
|
-
*
|
|
72
|
-
* @
|
|
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.
|
|
73
53
|
*/
|
|
74
|
-
async
|
|
75
|
-
|
|
76
|
-
|
|
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
|
+
);
|
|
77
68
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
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}`);
|
|
81
71
|
}
|
|
82
|
-
|
|
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);
|
|
83
80
|
}
|
|
84
81
|
/**
|
|
85
82
|
* Helper method to make a generic JSON-RPC POST request.
|
|
@@ -97,21 +94,15 @@ var A2AClient = class _A2AClient {
|
|
|
97
94
|
// Cast because TParams structure varies per method
|
|
98
95
|
id: requestId
|
|
99
96
|
};
|
|
100
|
-
const httpResponse = await
|
|
101
|
-
method: "POST",
|
|
102
|
-
headers: {
|
|
103
|
-
"Content-Type": "application/json",
|
|
104
|
-
"Accept": "application/json"
|
|
105
|
-
// Expect JSON response for non-streaming requests
|
|
106
|
-
},
|
|
107
|
-
body: JSON.stringify(rpcRequest)
|
|
108
|
-
});
|
|
97
|
+
const httpResponse = await this._fetchRpc(endpoint, rpcRequest);
|
|
109
98
|
if (!httpResponse.ok) {
|
|
110
99
|
let errorBodyText = "(empty or non-JSON response)";
|
|
111
100
|
try {
|
|
112
101
|
errorBodyText = await httpResponse.text();
|
|
113
102
|
const errorJson = JSON.parse(errorBodyText);
|
|
114
|
-
if (
|
|
103
|
+
if (errorJson.jsonrpc && errorJson.error) {
|
|
104
|
+
return errorJson;
|
|
105
|
+
} else if (!errorJson.jsonrpc && errorJson.error) {
|
|
115
106
|
throw new Error(`RPC error for ${method}: ${errorJson.error.message} (Code: ${errorJson.error.code}, HTTP Status: ${httpResponse.status}) Data: ${JSON.stringify(errorJson.error.data || {})}`);
|
|
116
107
|
} else if (!errorJson.jsonrpc) {
|
|
117
108
|
throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
|
|
@@ -127,6 +118,25 @@ var A2AClient = class _A2AClient {
|
|
|
127
118
|
}
|
|
128
119
|
return rpcResponse;
|
|
129
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Internal helper method to fetch the RPC service endpoint.
|
|
123
|
+
* @param url The URL to fetch.
|
|
124
|
+
* @param rpcRequest The JSON-RPC request to send.
|
|
125
|
+
* @param acceptHeader The Accept header to use. Defaults to "application/json".
|
|
126
|
+
* @returns A Promise that resolves to the fetch HTTP response.
|
|
127
|
+
*/
|
|
128
|
+
async _fetchRpc(url, rpcRequest, acceptHeader = "application/json") {
|
|
129
|
+
const requestInit = {
|
|
130
|
+
method: "POST",
|
|
131
|
+
headers: {
|
|
132
|
+
"Content-Type": "application/json",
|
|
133
|
+
"Accept": acceptHeader
|
|
134
|
+
// Expect JSON response for non-streaming requests
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify(rpcRequest)
|
|
137
|
+
};
|
|
138
|
+
return this._fetch(url, requestInit);
|
|
139
|
+
}
|
|
130
140
|
/**
|
|
131
141
|
* Sends a message to the agent.
|
|
132
142
|
* The behavior (blocking/non-blocking) and push notification configuration
|
|
@@ -161,15 +171,7 @@ var A2AClient = class _A2AClient {
|
|
|
161
171
|
params,
|
|
162
172
|
id: clientRequestId
|
|
163
173
|
};
|
|
164
|
-
const response = await
|
|
165
|
-
method: "POST",
|
|
166
|
-
headers: {
|
|
167
|
-
"Content-Type": "application/json",
|
|
168
|
-
"Accept": "text/event-stream"
|
|
169
|
-
// Crucial for SSE
|
|
170
|
-
},
|
|
171
|
-
body: JSON.stringify(rpcRequest)
|
|
172
|
-
});
|
|
174
|
+
const response = await this._fetchRpc(endpoint, rpcRequest, "text/event-stream");
|
|
173
175
|
if (!response.ok) {
|
|
174
176
|
let errorBody = "";
|
|
175
177
|
try {
|
|
@@ -253,7 +255,7 @@ var A2AClient = class _A2AClient {
|
|
|
253
255
|
params,
|
|
254
256
|
id: clientRequestId
|
|
255
257
|
};
|
|
256
|
-
const response = await
|
|
258
|
+
const response = await this._fetch(endpoint, {
|
|
257
259
|
method: "POST",
|
|
258
260
|
headers: {
|
|
259
261
|
"Content-Type": "application/json",
|
|
@@ -368,7 +370,122 @@ var A2AClient = class _A2AClient {
|
|
|
368
370
|
isErrorResponse(response) {
|
|
369
371
|
return "error" in response;
|
|
370
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
|
+
}
|
|
371
454
|
};
|
|
455
|
+
|
|
456
|
+
// src/client/auth-handler.ts
|
|
457
|
+
function createAuthenticatingFetchWithRetry(fetchImpl, authHandler) {
|
|
458
|
+
async function authFetch(url, init) {
|
|
459
|
+
const authHeaders = await authHandler.headers() || {};
|
|
460
|
+
const mergedInit = {
|
|
461
|
+
...init || {},
|
|
462
|
+
headers: {
|
|
463
|
+
...authHeaders,
|
|
464
|
+
...init?.headers || {}
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
let response = await fetchImpl(url, mergedInit);
|
|
468
|
+
const updatedHeaders = await authHandler.shouldRetryWithHeaders(mergedInit, response);
|
|
469
|
+
if (updatedHeaders) {
|
|
470
|
+
const retryInit = {
|
|
471
|
+
...init || {},
|
|
472
|
+
headers: {
|
|
473
|
+
...updatedHeaders,
|
|
474
|
+
...init?.headers || {}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
response = await fetchImpl(url, retryInit);
|
|
478
|
+
if (response.ok && authHandler.onSuccessfulRetry) {
|
|
479
|
+
await authHandler.onSuccessfulRetry(updatedHeaders);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return response;
|
|
483
|
+
}
|
|
484
|
+
Object.setPrototypeOf(authFetch, Object.getPrototypeOf(fetchImpl));
|
|
485
|
+
Object.defineProperties(authFetch, Object.getOwnPropertyDescriptors(fetchImpl));
|
|
486
|
+
return authFetch;
|
|
487
|
+
}
|
|
372
488
|
export {
|
|
373
|
-
A2AClient
|
|
489
|
+
A2AClient,
|
|
490
|
+
createAuthenticatingFetchWithRetry
|
|
374
491
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,10 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
5
9
|
var __copyProps = (to, from, except, desc) => {
|
|
6
10
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
11
|
for (let key of __getOwnPropNames(from))
|
|
@@ -14,4 +18,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
14
18
|
|
|
15
19
|
// src/index.ts
|
|
16
20
|
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
AGENT_CARD_PATH: () => AGENT_CARD_PATH
|
|
23
|
+
});
|
|
17
24
|
module.exports = __toCommonJS(index_exports);
|
|
25
|
+
|
|
26
|
+
// src/constants.ts
|
|
27
|
+
var AGENT_CARD_PATH = ".well-known/agent-card.json";
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
AGENT_CARD_PATH
|
|
31
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -6,4 +6,12 @@ export { A as A2AError, e as A2ARequest, a9 as APIKeySecurityScheme, aa as Agent
|
|
|
6
6
|
*/
|
|
7
7
|
type A2AResponse = SendMessageResponse | SendStreamingMessageResponse | GetTaskResponse | CancelTaskResponse | SetTaskPushNotificationConfigResponse | GetTaskPushNotificationConfigResponse | ListTaskPushNotificationConfigSuccessResponse | DeleteTaskPushNotificationConfigSuccessResponse | GetAuthenticatedExtendedCardSuccessResponse | JSONRPCErrorResponse;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Shared constants for the A2A library
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The well-known path for the agent card
|
|
14
|
+
*/
|
|
15
|
+
declare const AGENT_CARD_PATH = ".well-known/agent-card.json";
|
|
16
|
+
|
|
17
|
+
export { type A2AResponse, AGENT_CARD_PATH, CancelTaskResponse, DeleteTaskPushNotificationConfigSuccessResponse, GetAuthenticatedExtendedCardSuccessResponse, GetTaskPushNotificationConfigResponse, GetTaskResponse, JSONRPCErrorResponse, ListTaskPushNotificationConfigSuccessResponse, SendMessageResponse, SendStreamingMessageResponse, SetTaskPushNotificationConfigResponse };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,12 @@ export { A as A2AError, e as A2ARequest, a9 as APIKeySecurityScheme, aa as Agent
|
|
|
6
6
|
*/
|
|
7
7
|
type A2AResponse = SendMessageResponse | SendStreamingMessageResponse | GetTaskResponse | CancelTaskResponse | SetTaskPushNotificationConfigResponse | GetTaskPushNotificationConfigResponse | ListTaskPushNotificationConfigSuccessResponse | DeleteTaskPushNotificationConfigSuccessResponse | GetAuthenticatedExtendedCardSuccessResponse | JSONRPCErrorResponse;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Shared constants for the A2A library
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The well-known path for the agent card
|
|
14
|
+
*/
|
|
15
|
+
declare const AGENT_CARD_PATH = ".well-known/agent-card.json";
|
|
16
|
+
|
|
17
|
+
export { type A2AResponse, AGENT_CARD_PATH, CancelTaskResponse, DeleteTaskPushNotificationConfigSuccessResponse, GetAuthenticatedExtendedCardSuccessResponse, GetTaskPushNotificationConfigResponse, GetTaskResponse, JSONRPCErrorResponse, ListTaskPushNotificationConfigSuccessResponse, SendMessageResponse, SendStreamingMessageResponse, SetTaskPushNotificationConfigResponse };
|
package/dist/index.js
CHANGED
|
@@ -239,6 +239,9 @@ var JsonRpcTransportHandler = class {
|
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
241
|
|
|
242
|
+
// src/constants.ts
|
|
243
|
+
var AGENT_CARD_PATH = ".well-known/agent-card.json";
|
|
244
|
+
|
|
242
245
|
// src/server/express/a2a_express_app.ts
|
|
243
246
|
var A2AExpressApp = class {
|
|
244
247
|
requestHandler;
|
|
@@ -253,12 +256,13 @@ var A2AExpressApp = class {
|
|
|
253
256
|
* @param app Optional existing Express app.
|
|
254
257
|
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
255
258
|
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
259
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
256
260
|
* @returns The Express app with A2A routes.
|
|
257
261
|
*/
|
|
258
|
-
setupRoutes(app, baseUrl = "", middlewares) {
|
|
262
|
+
setupRoutes(app, baseUrl = "", middlewares, agentCardPath = AGENT_CARD_PATH) {
|
|
259
263
|
const router = import_express.default.Router();
|
|
260
264
|
router.use(import_express.default.json(), ...middlewares ?? []);
|
|
261
|
-
router.get(
|
|
265
|
+
router.get(`/${agentCardPath}`, async (req, res) => {
|
|
262
266
|
try {
|
|
263
267
|
const agentCard = await this.requestHandler.getAgentCard();
|
|
264
268
|
res.json(agentCard);
|
|
@@ -11,9 +11,10 @@ declare class A2AExpressApp {
|
|
|
11
11
|
* @param app Optional existing Express app.
|
|
12
12
|
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
13
13
|
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
14
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
14
15
|
* @returns The Express app with A2A routes.
|
|
15
16
|
*/
|
|
16
|
-
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler
|
|
17
|
+
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler>, agentCardPath?: string): Express;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export { A2AExpressApp };
|
|
@@ -11,9 +11,10 @@ declare class A2AExpressApp {
|
|
|
11
11
|
* @param app Optional existing Express app.
|
|
12
12
|
* @param baseUrl The base URL for A2A endpoints (e.g., "/a2a/api").
|
|
13
13
|
* @param middlewares Optional array of Express middlewares to apply to the A2A routes.
|
|
14
|
+
* @param agentCardPath Optional custom path for the agent card endpoint (defaults to /.well-known/agent-card.json).
|
|
14
15
|
* @returns The Express app with A2A routes.
|
|
15
16
|
*/
|
|
16
|
-
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler
|
|
17
|
+
setupRoutes(app: Express, baseUrl?: string, middlewares?: Array<RequestHandler | ErrorRequestHandler>, agentCardPath?: string): Express;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export { A2AExpressApp };
|