@a2a-js/sdk 0.2.1 → 0.2.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 +106 -89
- package/package.json +26 -8
- package/build/src/a2a_response.d.ts +0 -5
- package/build/src/a2a_response.js +0 -2
- package/build/src/client/client.d.ts +0 -118
- package/build/src/client/client.js +0 -409
- package/build/src/index.d.ts +0 -21
- package/build/src/index.js +0 -18
- package/build/src/samples/agents/movie-agent/genkit.d.ts +0 -2
- package/build/src/samples/agents/movie-agent/genkit.js +0 -11
- package/build/src/samples/agents/movie-agent/index.d.ts +0 -1
- package/build/src/samples/agents/movie-agent/index.js +0 -252
- package/build/src/samples/agents/movie-agent/tmdb.d.ts +0 -7
- package/build/src/samples/agents/movie-agent/tmdb.js +0 -32
- package/build/src/samples/agents/movie-agent/tools.d.ts +0 -15
- package/build/src/samples/agents/movie-agent/tools.js +0 -74
- package/build/src/samples/cli.d.ts +0 -2
- package/build/src/samples/cli.js +0 -289
- package/build/src/server/a2a_express_app.d.ts +0 -14
- package/build/src/server/a2a_express_app.js +0 -98
- package/build/src/server/agent_execution/agent_executor.d.ts +0 -18
- package/build/src/server/agent_execution/agent_executor.js +0 -2
- package/build/src/server/agent_execution/request_context.d.ts +0 -9
- package/build/src/server/agent_execution/request_context.js +0 -15
- package/build/src/server/error.d.ts +0 -23
- package/build/src/server/error.js +0 -57
- package/build/src/server/events/execution_event_bus.d.ts +0 -16
- package/build/src/server/events/execution_event_bus.js +0 -13
- package/build/src/server/events/execution_event_bus_manager.d.ts +0 -27
- package/build/src/server/events/execution_event_bus_manager.js +0 -36
- package/build/src/server/events/execution_event_queue.d.ts +0 -24
- package/build/src/server/events/execution_event_queue.js +0 -63
- package/build/src/server/request_handler/a2a_request_handler.d.ts +0 -11
- package/build/src/server/request_handler/a2a_request_handler.js +0 -2
- package/build/src/server/request_handler/default_request_handler.d.ts +0 -22
- package/build/src/server/request_handler/default_request_handler.js +0 -296
- package/build/src/server/result_manager.d.ts +0 -29
- package/build/src/server/result_manager.js +0 -149
- package/build/src/server/store.d.ts +0 -25
- package/build/src/server/store.js +0 -17
- package/build/src/server/transports/jsonrpc_transport_handler.d.ts +0 -15
- package/build/src/server/transports/jsonrpc_transport_handler.js +0 -114
- package/build/src/server/utils.d.ts +0 -22
- package/build/src/server/utils.js +0 -34
- package/build/src/types-old.d.ts +0 -832
- package/build/src/types-old.js +0 -20
- package/build/src/types.d.ts +0 -2046
- package/build/src/types.js +0 -8
package/README.md
CHANGED
|
@@ -28,19 +28,21 @@ You can also find JavaScript samples [here](https://github.com/google-a2a/a2a-sa
|
|
|
28
28
|
This directory contains a TypeScript server implementation for the Agent-to-Agent (A2A) communication protocol, built using Express.js.
|
|
29
29
|
|
|
30
30
|
### 1. Define Agent Card
|
|
31
|
+
|
|
31
32
|
```typescript
|
|
32
|
-
import { AgentCard } from "@a2a-js/sdk";
|
|
33
|
+
import type { AgentCard } from "@a2a-js/sdk";
|
|
33
34
|
|
|
34
35
|
const movieAgentCard: AgentCard = {
|
|
35
|
-
name:
|
|
36
|
-
description:
|
|
36
|
+
name: "Movie Agent",
|
|
37
|
+
description:
|
|
38
|
+
"An agent that can answer questions about movies and actors using TMDB.",
|
|
37
39
|
// Adjust the base URL and port as needed.
|
|
38
|
-
url:
|
|
40
|
+
url: "http://localhost:41241/",
|
|
39
41
|
provider: {
|
|
40
|
-
organization:
|
|
41
|
-
url:
|
|
42
|
+
organization: "A2A Agents",
|
|
43
|
+
url: "https://example.com/a2a-agents", // Added provider URL
|
|
42
44
|
},
|
|
43
|
-
version:
|
|
45
|
+
version: "0.0.2", // Incremented version
|
|
44
46
|
capabilities: {
|
|
45
47
|
streaming: true, // Supports streaming
|
|
46
48
|
pushNotifications: false, // Assuming not implemented for this agent yet
|
|
@@ -48,24 +50,25 @@ const movieAgentCard: AgentCard = {
|
|
|
48
50
|
},
|
|
49
51
|
securitySchemes: undefined, // Or define actual security schemes if any
|
|
50
52
|
security: undefined,
|
|
51
|
-
defaultInputModes: [
|
|
52
|
-
defaultOutputModes: [
|
|
53
|
+
defaultInputModes: ["text/plain"],
|
|
54
|
+
defaultOutputModes: ["text/plain"],
|
|
53
55
|
skills: [
|
|
54
56
|
{
|
|
55
|
-
id:
|
|
56
|
-
name:
|
|
57
|
-
description:
|
|
58
|
-
|
|
57
|
+
id: "general_movie_chat",
|
|
58
|
+
name: "General Movie Chat",
|
|
59
|
+
description:
|
|
60
|
+
"Answer general questions or chat about movies, actors, directors.",
|
|
61
|
+
tags: ["movies", "actors", "directors"],
|
|
59
62
|
examples: [
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
"Tell me about the plot of Inception.",
|
|
64
|
+
"Recommend a good sci-fi movie.",
|
|
65
|
+
"Who directed The Matrix?",
|
|
66
|
+
"What other movies has Scarlett Johansson been in?",
|
|
67
|
+
"Find action movies starring Keanu Reeves",
|
|
68
|
+
"Which came out first, Jurassic Park or Terminator 2?",
|
|
66
69
|
],
|
|
67
|
-
inputModes: [
|
|
68
|
-
outputModes: [
|
|
70
|
+
inputModes: ["text/plain"], // Explicitly defining for skill
|
|
71
|
+
outputModes: ["text/plain"], // Explicitly defining for skill
|
|
69
72
|
},
|
|
70
73
|
],
|
|
71
74
|
supportsAuthenticatedExtendedCard: false,
|
|
@@ -73,6 +76,7 @@ const movieAgentCard: AgentCard = {
|
|
|
73
76
|
```
|
|
74
77
|
|
|
75
78
|
### 2. Define Agent Executor
|
|
79
|
+
|
|
76
80
|
```typescript
|
|
77
81
|
import {
|
|
78
82
|
InMemoryTaskStore,
|
|
@@ -82,19 +86,19 @@ import {
|
|
|
82
86
|
RequestContext,
|
|
83
87
|
ExecutionEventBus,
|
|
84
88
|
DefaultRequestHandler,
|
|
85
|
-
} from "@a2a-js/sdk";
|
|
89
|
+
} from "@a2a-js/sdk/server";
|
|
86
90
|
|
|
87
91
|
// 1. Define your agent's logic as a AgentExecutor
|
|
88
92
|
class MyAgentExecutor implements AgentExecutor {
|
|
89
93
|
private cancelledTasks = new Set<string>();
|
|
90
94
|
|
|
91
95
|
public cancelTask = async (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
taskId: string,
|
|
97
|
+
eventBus: ExecutionEventBus
|
|
98
|
+
): Promise<void> => {
|
|
99
|
+
this.cancelledTasks.add(taskId);
|
|
100
|
+
// The execute loop is responsible for publishing the final state
|
|
101
|
+
};
|
|
98
102
|
|
|
99
103
|
async execute(
|
|
100
104
|
requestContext: RequestContext,
|
|
@@ -114,11 +118,11 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
114
118
|
// 1. Publish initial Task event if it's a new task
|
|
115
119
|
if (!existingTask) {
|
|
116
120
|
const initialTask: Task = {
|
|
117
|
-
kind:
|
|
121
|
+
kind: "task",
|
|
118
122
|
id: taskId,
|
|
119
123
|
contextId: contextId,
|
|
120
124
|
status: {
|
|
121
|
-
state:
|
|
125
|
+
state: "submitted",
|
|
122
126
|
timestamp: new Date().toISOString(),
|
|
123
127
|
},
|
|
124
128
|
history: [userMessage],
|
|
@@ -130,16 +134,16 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
130
134
|
|
|
131
135
|
// 2. Publish "working" status update
|
|
132
136
|
const workingStatusUpdate: TaskStatusUpdateEvent = {
|
|
133
|
-
kind:
|
|
137
|
+
kind: "status-update",
|
|
134
138
|
taskId: taskId,
|
|
135
139
|
contextId: contextId,
|
|
136
140
|
status: {
|
|
137
|
-
state:
|
|
141
|
+
state: "working",
|
|
138
142
|
message: {
|
|
139
|
-
kind:
|
|
140
|
-
role:
|
|
143
|
+
kind: "message",
|
|
144
|
+
role: "agent",
|
|
141
145
|
messageId: uuidv4(),
|
|
142
|
-
parts: [{ kind:
|
|
146
|
+
parts: [{ kind: "text", text: "Generating code..." }],
|
|
143
147
|
taskId: taskId,
|
|
144
148
|
contextId: contextId,
|
|
145
149
|
},
|
|
@@ -156,11 +160,11 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
156
160
|
if (this.cancelledTasks.has(taskId)) {
|
|
157
161
|
console.log(`[MyAgentExecutor] Request cancelled for task: ${taskId}`);
|
|
158
162
|
const cancelledUpdate: TaskStatusUpdateEvent = {
|
|
159
|
-
kind:
|
|
163
|
+
kind: "status-update",
|
|
160
164
|
taskId: taskId,
|
|
161
165
|
contextId: contextId,
|
|
162
166
|
status: {
|
|
163
|
-
state:
|
|
167
|
+
state: "canceled",
|
|
164
168
|
timestamp: new Date().toISOString(),
|
|
165
169
|
},
|
|
166
170
|
final: true,
|
|
@@ -172,7 +176,7 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
172
176
|
|
|
173
177
|
// 3. Publish artifact update
|
|
174
178
|
const artifactUpdate: TaskArtifactUpdateEvent = {
|
|
175
|
-
kind:
|
|
179
|
+
kind: "artifact-update",
|
|
176
180
|
taskId: taskId,
|
|
177
181
|
contextId: contextId,
|
|
178
182
|
artifact: {
|
|
@@ -187,14 +191,14 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
187
191
|
|
|
188
192
|
// 4. Publish final status update
|
|
189
193
|
const finalUpdate: TaskStatusUpdateEvent = {
|
|
190
|
-
kind:
|
|
194
|
+
kind: "status-update",
|
|
191
195
|
taskId: taskId,
|
|
192
196
|
contextId: contextId,
|
|
193
197
|
status: {
|
|
194
|
-
state:
|
|
198
|
+
state: "completed",
|
|
195
199
|
message: {
|
|
196
|
-
kind:
|
|
197
|
-
role:
|
|
200
|
+
kind: "message",
|
|
201
|
+
role: "agent",
|
|
198
202
|
messageId: uuidv4(),
|
|
199
203
|
taskId: taskId,
|
|
200
204
|
contextId: contextId,
|
|
@@ -210,6 +214,7 @@ class MyAgentExecutor implements AgentExecutor {
|
|
|
210
214
|
```
|
|
211
215
|
|
|
212
216
|
### 3. Start the server
|
|
217
|
+
|
|
213
218
|
```typescript
|
|
214
219
|
const taskStore: TaskStore = new InMemoryTaskStore();
|
|
215
220
|
const agentExecutor: AgentExecutor = new MyAgentExecutor();
|
|
@@ -221,19 +226,26 @@ const requestHandler = new DefaultRequestHandler(
|
|
|
221
226
|
);
|
|
222
227
|
|
|
223
228
|
const appBuilder = new A2AExpressApp(requestHandler);
|
|
224
|
-
const expressApp = appBuilder.setupRoutes(express(),
|
|
229
|
+
const expressApp = appBuilder.setupRoutes(express(), "");
|
|
225
230
|
|
|
226
231
|
const PORT = process.env.CODER_AGENT_PORT || 41242; // Different port for coder agent
|
|
227
232
|
expressApp.listen(PORT, () => {
|
|
228
|
-
console.log(
|
|
229
|
-
|
|
230
|
-
|
|
233
|
+
console.log(
|
|
234
|
+
`[MyAgent] Server using new framework started on http://localhost:${PORT}`
|
|
235
|
+
);
|
|
236
|
+
console.log(
|
|
237
|
+
`[MyAgent] Agent Card: http://localhost:${PORT}/.well-known/agent.json`
|
|
238
|
+
);
|
|
239
|
+
console.log("[MyAgent] Press Ctrl+C to stop the server");
|
|
231
240
|
});
|
|
232
241
|
```
|
|
242
|
+
|
|
233
243
|
### Agent Executor
|
|
244
|
+
|
|
234
245
|
Developers are expected to implement this interface and provide two methods: `execute` and `cancelTask`.
|
|
235
246
|
|
|
236
247
|
#### `execute`
|
|
248
|
+
|
|
237
249
|
- This method is provided with a `RequestContext` and an `EventBus` to publish execution events.
|
|
238
250
|
- Executor can either respond by publishing a Message or Task.
|
|
239
251
|
- For a task, check if there's an existing task in `RequestContext`. If not, publish an initial Task event using `taskId` & `contextId` from `RequestContext`.
|
|
@@ -242,9 +254,10 @@ Developers are expected to implement this interface and provide two methods: `ex
|
|
|
242
254
|
- Executor should also check if an ongoing task has been cancelled. If yes, cancel the execution and emit an `TaskStatusUpdateEvent` with cancelled state.
|
|
243
255
|
|
|
244
256
|
#### `cancelTask`
|
|
257
|
+
|
|
245
258
|
Executors should implement cancellation mechanism for an ongoing task.
|
|
246
259
|
|
|
247
|
-
## A2A Client
|
|
260
|
+
## A2A Client
|
|
248
261
|
|
|
249
262
|
There's a `A2AClient` class, which provides methods for interacting with an A2A server over HTTP using JSON-RPC.
|
|
250
263
|
|
|
@@ -259,8 +272,8 @@ There's a `A2AClient` class, which provides methods for interacting with an A2A
|
|
|
259
272
|
### Basic Usage
|
|
260
273
|
|
|
261
274
|
```typescript
|
|
262
|
-
import {
|
|
263
|
-
|
|
275
|
+
import { A2AClient } from "@a2a-js/sdk/client";
|
|
276
|
+
import type {
|
|
264
277
|
Message,
|
|
265
278
|
MessageSendParams,
|
|
266
279
|
Task,
|
|
@@ -268,7 +281,7 @@ import {
|
|
|
268
281
|
SendMessageResponse,
|
|
269
282
|
GetTaskResponse,
|
|
270
283
|
SendMessageSuccessResponse,
|
|
271
|
-
GetTaskSuccessResponse
|
|
284
|
+
GetTaskSuccessResponse,
|
|
272
285
|
} from "@a2a-js/sdk";
|
|
273
286
|
import { v4 as uuidv4 } from "uuid";
|
|
274
287
|
|
|
@@ -285,50 +298,50 @@ async function run() {
|
|
|
285
298
|
messageId: messageId,
|
|
286
299
|
role: "user",
|
|
287
300
|
parts: [{ kind: "text", text: "Hello, agent!" }],
|
|
288
|
-
kind: "message"
|
|
301
|
+
kind: "message",
|
|
289
302
|
},
|
|
290
303
|
configuration: {
|
|
291
304
|
blocking: true,
|
|
292
|
-
acceptedOutputModes: [
|
|
293
|
-
}
|
|
305
|
+
acceptedOutputModes: ["text/plain"],
|
|
306
|
+
},
|
|
294
307
|
};
|
|
295
|
-
|
|
296
|
-
const sendResponse: SendMessageResponse =
|
|
308
|
+
|
|
309
|
+
const sendResponse: SendMessageResponse =
|
|
310
|
+
await client.sendMessage(sendParams);
|
|
297
311
|
|
|
298
312
|
if (sendResponse.error) {
|
|
299
|
-
|
|
300
|
-
|
|
313
|
+
console.error("Error sending message:", sendResponse.error);
|
|
314
|
+
return;
|
|
301
315
|
}
|
|
302
316
|
|
|
303
317
|
// On success, the result can be a Task or a Message. Check which one it is.
|
|
304
318
|
const result = (sendResponse as SendMessageSuccessResponse).result;
|
|
305
319
|
|
|
306
|
-
if (result.kind ===
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
} else if (result.kind ===
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
320
|
+
if (result.kind === "task") {
|
|
321
|
+
// The agent created a task.
|
|
322
|
+
const taskResult = result as Task;
|
|
323
|
+
console.log("Send Message Result (Task):", taskResult);
|
|
324
|
+
taskId = taskResult.id; // Save the task ID for the next call
|
|
325
|
+
} else if (result.kind === "message") {
|
|
326
|
+
// The agent responded with a direct message.
|
|
327
|
+
const messageResult = result as Message;
|
|
328
|
+
console.log("Send Message Result (Direct Message):", messageResult);
|
|
329
|
+
// No task was created, so we can't get task status.
|
|
316
330
|
}
|
|
317
331
|
|
|
318
332
|
// 2. If a task was created, get its status.
|
|
319
333
|
if (taskId) {
|
|
320
|
-
|
|
321
|
-
|
|
334
|
+
const getParams: TaskQueryParams = { id: taskId };
|
|
335
|
+
const getResponse: GetTaskResponse = await client.getTask(getParams);
|
|
322
336
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const getTaskResult = (getResponse as GetTaskSuccessResponse).result;
|
|
329
|
-
console.log("Get Task Result:", getTaskResult);
|
|
330
|
-
}
|
|
337
|
+
if (getResponse.error) {
|
|
338
|
+
console.error(`Error getting task ${taskId}:`, getResponse.error);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
331
341
|
|
|
342
|
+
const getTaskResult = (getResponse as GetTaskSuccessResponse).result;
|
|
343
|
+
console.log("Get Task Result:", getTaskResult);
|
|
344
|
+
}
|
|
332
345
|
} catch (error) {
|
|
333
346
|
console.error("A2A Client Communication Error:", error);
|
|
334
347
|
}
|
|
@@ -340,8 +353,8 @@ run();
|
|
|
340
353
|
### Streaming Usage
|
|
341
354
|
|
|
342
355
|
```typescript
|
|
343
|
-
import {
|
|
344
|
-
|
|
356
|
+
import { A2AClient } from "@a2a-js/sdk/client";
|
|
357
|
+
import type {
|
|
345
358
|
TaskStatusUpdateEvent,
|
|
346
359
|
TaskArtifactUpdateEvent,
|
|
347
360
|
MessageSendParams,
|
|
@@ -356,31 +369,33 @@ async function streamTask() {
|
|
|
356
369
|
const messageId = uuidv4();
|
|
357
370
|
try {
|
|
358
371
|
console.log(`\n--- Starting streaming task for message ${messageId} ---`);
|
|
359
|
-
|
|
372
|
+
|
|
360
373
|
// Construct the `MessageSendParams` object.
|
|
361
374
|
const streamParams: MessageSendParams = {
|
|
362
375
|
message: {
|
|
363
376
|
messageId: messageId,
|
|
364
377
|
role: "user",
|
|
365
378
|
parts: [{ kind: "text", text: "Stream me some updates!" }],
|
|
366
|
-
kind: "message"
|
|
379
|
+
kind: "message",
|
|
367
380
|
},
|
|
368
381
|
};
|
|
369
|
-
|
|
382
|
+
|
|
370
383
|
// Use the `sendMessageStream` method.
|
|
371
384
|
const stream = client.sendMessageStream(streamParams);
|
|
372
385
|
let currentTaskId: string | undefined;
|
|
373
386
|
|
|
374
387
|
for await (const event of stream) {
|
|
375
388
|
// The first event is often the Task object itself, establishing the ID.
|
|
376
|
-
if ((event as Task).kind ===
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
389
|
+
if ((event as Task).kind === "task") {
|
|
390
|
+
currentTaskId = (event as Task).id;
|
|
391
|
+
console.log(
|
|
392
|
+
`[${currentTaskId}] Task created. Status: ${(event as Task).status.state}`
|
|
393
|
+
);
|
|
394
|
+
continue;
|
|
380
395
|
}
|
|
381
|
-
|
|
396
|
+
|
|
382
397
|
// Differentiate subsequent stream events.
|
|
383
|
-
if ((event as TaskStatusUpdateEvent).kind ===
|
|
398
|
+
if ((event as TaskStatusUpdateEvent).kind === "status-update") {
|
|
384
399
|
const statusEvent = event as TaskStatusUpdateEvent;
|
|
385
400
|
console.log(
|
|
386
401
|
`[${statusEvent.taskId}] Status Update: ${statusEvent.status.state} - ${
|
|
@@ -391,7 +406,9 @@ async function streamTask() {
|
|
|
391
406
|
console.log(`[${statusEvent.taskId}] Stream marked as final.`);
|
|
392
407
|
break; // Exit loop when server signals completion
|
|
393
408
|
}
|
|
394
|
-
} else if (
|
|
409
|
+
} else if (
|
|
410
|
+
(event as TaskArtifactUpdateEvent).kind === "artifact-update"
|
|
411
|
+
) {
|
|
395
412
|
const artifactEvent = event as TaskArtifactUpdateEvent;
|
|
396
413
|
// Use artifact.name or artifact.artifactId for identification
|
|
397
414
|
console.log(
|
package/package.json
CHANGED
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a2a-js/sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Server & Client SDK for Agent2Agent protocol",
|
|
5
|
-
"repository":"google-a2a/a2a-js.git",
|
|
5
|
+
"repository": "google-a2a/a2a-js.git",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=18"
|
|
8
8
|
},
|
|
9
|
-
"main": "
|
|
10
|
-
"types": "
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
11
|
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./server": {
|
|
19
|
+
"types": "./dist/server/index.d.ts",
|
|
20
|
+
"import": "./dist/server/index.js",
|
|
21
|
+
"require": "./dist/server/index.cjs"
|
|
22
|
+
},
|
|
23
|
+
"./client": {
|
|
24
|
+
"types": "./dist/client/index.d.ts",
|
|
25
|
+
"import": "./dist/client/index.js",
|
|
26
|
+
"require": "./dist/client/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
12
29
|
"files": [
|
|
13
|
-
"
|
|
14
|
-
"!
|
|
30
|
+
"dist",
|
|
31
|
+
"!dist/**/*.map",
|
|
15
32
|
"README.md"
|
|
16
33
|
],
|
|
17
34
|
"devDependencies": {
|
|
@@ -28,12 +45,13 @@
|
|
|
28
45
|
"json-schema-to-typescript": "^15.0.4",
|
|
29
46
|
"mocha": "^11.6.0",
|
|
30
47
|
"sinon": "^20.0.0",
|
|
48
|
+
"tsup": "^8.5.0",
|
|
31
49
|
"tsx": "^4.19.3",
|
|
32
50
|
"typescript": "^5.8.2"
|
|
33
51
|
},
|
|
34
52
|
"scripts": {
|
|
35
53
|
"clean": "gts clean",
|
|
36
|
-
"build": "
|
|
54
|
+
"build": "tsup",
|
|
37
55
|
"pretest": "npm run build",
|
|
38
56
|
"test": "mocha build/test/**/*.js",
|
|
39
57
|
"coverage": "c8 npm run test",
|
|
@@ -43,7 +61,7 @@
|
|
|
43
61
|
},
|
|
44
62
|
"dependencies": {
|
|
45
63
|
"@types/cors": "^2.8.17",
|
|
46
|
-
"@types/express": "^
|
|
64
|
+
"@types/express": "^4.17.23",
|
|
47
65
|
"body-parser": "^2.2.0",
|
|
48
66
|
"cors": "^2.8.5",
|
|
49
67
|
"express": "^4.21.2",
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { SendMessageResponse, SendStreamingMessageResponse, GetTaskResponse, CancelTaskResponse, SetTaskPushNotificationConfigResponse, GetTaskPushNotificationConfigResponse, JSONRPCErrorResponse } from "./types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Represents any valid JSON-RPC response defined in the A2A protocol.
|
|
4
|
-
*/
|
|
5
|
-
export type A2AResponse = SendMessageResponse | SendStreamingMessageResponse | GetTaskResponse | CancelTaskResponse | SetTaskPushNotificationConfigResponse | GetTaskPushNotificationConfigResponse | JSONRPCErrorResponse;
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { AgentCard, JSONRPCResponse, JSONRPCErrorResponse, Message, Task, TaskStatusUpdateEvent, TaskArtifactUpdateEvent, MessageSendParams, SendMessageResponse, TaskQueryParams, GetTaskResponse, TaskIdParams, CancelTaskResponse, TaskPushNotificationConfig, SetTaskPushNotificationConfigResponse, GetTaskPushNotificationConfigResponse } from '../types.js';
|
|
2
|
-
type A2AStreamEventData = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent;
|
|
3
|
-
/**
|
|
4
|
-
* A2AClient is a TypeScript HTTP client for interacting with A2A-compliant agents.
|
|
5
|
-
*/
|
|
6
|
-
export declare class A2AClient {
|
|
7
|
-
private agentBaseUrl;
|
|
8
|
-
private agentCardPromise;
|
|
9
|
-
private requestIdCounter;
|
|
10
|
-
private serviceEndpointUrl?;
|
|
11
|
-
/**
|
|
12
|
-
* Constructs an A2AClient instance.
|
|
13
|
-
* It initiates fetching the agent card from the provided agent baseUrl.
|
|
14
|
-
* The Agent Card is expected at `${agentBaseUrl}/.well-known/agent.json`.
|
|
15
|
-
* The `url` field from the Agent Card will be used as the RPC service endpoint.
|
|
16
|
-
* @param agentBaseUrl The base URL of the A2A agent (e.g., https://agent.example.com).
|
|
17
|
-
*/
|
|
18
|
-
constructor(agentBaseUrl: string);
|
|
19
|
-
/**
|
|
20
|
-
* Fetches the Agent Card from the agent's well-known URI and caches its service endpoint URL.
|
|
21
|
-
* This method is called by the constructor.
|
|
22
|
-
* @returns A Promise that resolves to the AgentCard.
|
|
23
|
-
*/
|
|
24
|
-
private _fetchAndCacheAgentCard;
|
|
25
|
-
/**
|
|
26
|
-
* Retrieves the Agent Card.
|
|
27
|
-
* If an `agentBaseUrl` is provided, it fetches the card from that specific URL.
|
|
28
|
-
* Otherwise, it returns the card fetched and cached during client construction.
|
|
29
|
-
* @param agentBaseUrl Optional. The base URL of the agent to fetch the card from.
|
|
30
|
-
* If provided, this will fetch a new card, not use the cached one from the constructor's URL.
|
|
31
|
-
* @returns A Promise that resolves to the AgentCard.
|
|
32
|
-
*/
|
|
33
|
-
getAgentCard(agentBaseUrl?: string): Promise<AgentCard>;
|
|
34
|
-
/**
|
|
35
|
-
* Gets the RPC service endpoint URL. Ensures the agent card has been fetched first.
|
|
36
|
-
* @returns A Promise that resolves to the service endpoint URL string.
|
|
37
|
-
*/
|
|
38
|
-
private _getServiceEndpoint;
|
|
39
|
-
/**
|
|
40
|
-
* Helper method to make a generic JSON-RPC POST request.
|
|
41
|
-
* @param method The RPC method name.
|
|
42
|
-
* @param params The parameters for the RPC method.
|
|
43
|
-
* @returns A Promise that resolves to the RPC response.
|
|
44
|
-
*/
|
|
45
|
-
private _postRpcRequest;
|
|
46
|
-
/**
|
|
47
|
-
* Sends a message to the agent.
|
|
48
|
-
* The behavior (blocking/non-blocking) and push notification configuration
|
|
49
|
-
* are specified within the `params.configuration` object.
|
|
50
|
-
* Optionally, `params.message.contextId` or `params.message.taskId` can be provided.
|
|
51
|
-
* @param params The parameters for sending the message, including the message content and configuration.
|
|
52
|
-
* @returns A Promise resolving to SendMessageResponse, which can be a Message, Task, or an error.
|
|
53
|
-
*/
|
|
54
|
-
sendMessage(params: MessageSendParams): Promise<SendMessageResponse>;
|
|
55
|
-
/**
|
|
56
|
-
* Sends a message to the agent and streams back responses using Server-Sent Events (SSE).
|
|
57
|
-
* Push notification configuration can be specified in `params.configuration`.
|
|
58
|
-
* Optionally, `params.message.contextId` or `params.message.taskId` can be provided.
|
|
59
|
-
* Requires the agent to support streaming (`capabilities.streaming: true` in AgentCard).
|
|
60
|
-
* @param params The parameters for sending the message.
|
|
61
|
-
* @returns An AsyncGenerator yielding A2AStreamEventData (Message, Task, TaskStatusUpdateEvent, or TaskArtifactUpdateEvent).
|
|
62
|
-
* The generator throws an error if streaming is not supported or if an HTTP/SSE error occurs.
|
|
63
|
-
*/
|
|
64
|
-
sendMessageStream(params: MessageSendParams): AsyncGenerator<A2AStreamEventData, void, undefined>;
|
|
65
|
-
/**
|
|
66
|
-
* Sets or updates the push notification configuration for a given task.
|
|
67
|
-
* Requires the agent to support push notifications (`capabilities.pushNotifications: true` in AgentCard).
|
|
68
|
-
* @param params Parameters containing the taskId and the TaskPushNotificationConfig.
|
|
69
|
-
* @returns A Promise resolving to SetTaskPushNotificationConfigResponse.
|
|
70
|
-
*/
|
|
71
|
-
setTaskPushNotificationConfig(params: TaskPushNotificationConfig): Promise<SetTaskPushNotificationConfigResponse>;
|
|
72
|
-
/**
|
|
73
|
-
* Gets the push notification configuration for a given task.
|
|
74
|
-
* @param params Parameters containing the taskId.
|
|
75
|
-
* @returns A Promise resolving to GetTaskPushNotificationConfigResponse.
|
|
76
|
-
*/
|
|
77
|
-
getTaskPushNotificationConfig(params: TaskIdParams): Promise<GetTaskPushNotificationConfigResponse>;
|
|
78
|
-
/**
|
|
79
|
-
* Retrieves a task by its ID.
|
|
80
|
-
* @param params Parameters containing the taskId and optional historyLength.
|
|
81
|
-
* @returns A Promise resolving to GetTaskResponse, which contains the Task object or an error.
|
|
82
|
-
*/
|
|
83
|
-
getTask(params: TaskQueryParams): Promise<GetTaskResponse>;
|
|
84
|
-
/**
|
|
85
|
-
* Cancels a task by its ID.
|
|
86
|
-
* @param params Parameters containing the taskId.
|
|
87
|
-
* @returns A Promise resolving to CancelTaskResponse, which contains the updated Task object or an error.
|
|
88
|
-
*/
|
|
89
|
-
cancelTask(params: TaskIdParams): Promise<CancelTaskResponse>;
|
|
90
|
-
/**
|
|
91
|
-
* Resubscribes to a task's event stream using Server-Sent Events (SSE).
|
|
92
|
-
* This is used if a previous SSE connection for an active task was broken.
|
|
93
|
-
* Requires the agent to support streaming (`capabilities.streaming: true` in AgentCard).
|
|
94
|
-
* @param params Parameters containing the taskId.
|
|
95
|
-
* @returns An AsyncGenerator yielding A2AStreamEventData (Message, Task, TaskStatusUpdateEvent, or TaskArtifactUpdateEvent).
|
|
96
|
-
*/
|
|
97
|
-
resubscribeTask(params: TaskIdParams): AsyncGenerator<A2AStreamEventData, void, undefined>;
|
|
98
|
-
/**
|
|
99
|
-
* Parses an HTTP response body as an A2A Server-Sent Event stream.
|
|
100
|
-
* Each 'data' field of an SSE event is expected to be a JSON-RPC 2.0 Response object,
|
|
101
|
-
* specifically a SendStreamingMessageResponse (or similar structure for resubscribe).
|
|
102
|
-
* @param response The HTTP Response object whose body is the SSE stream.
|
|
103
|
-
* @param originalRequestId The ID of the client's JSON-RPC request that initiated this stream.
|
|
104
|
-
* Used to validate the `id` in the streamed JSON-RPC responses.
|
|
105
|
-
* @returns An AsyncGenerator yielding the `result` field of each valid JSON-RPC success response from the stream.
|
|
106
|
-
*/
|
|
107
|
-
private _parseA2ASseStream;
|
|
108
|
-
/**
|
|
109
|
-
* Processes a single SSE event's data string, expecting it to be a JSON-RPC response.
|
|
110
|
-
* @param jsonData The string content from one or more 'data:' lines of an SSE event.
|
|
111
|
-
* @param originalRequestId The ID of the client's request that initiated the stream.
|
|
112
|
-
* @returns The `result` field of the parsed JSON-RPC success response.
|
|
113
|
-
* @throws Error if data is not valid JSON, not a valid JSON-RPC response, an error response, or ID mismatch.
|
|
114
|
-
*/
|
|
115
|
-
private _processSseEventData;
|
|
116
|
-
isErrorResponse(response: JSONRPCResponse): response is JSONRPCErrorResponse;
|
|
117
|
-
}
|
|
118
|
-
export {};
|