@artinet/sdk 0.5.17 → 0.5.18

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.
Files changed (151) hide show
  1. package/README.md +101 -26
  2. package/dist/browser/browser.d.ts +9 -0
  3. package/dist/browser/browser.js +10 -0
  4. package/dist/browser/client/a2a-client.d.ts +126 -0
  5. package/dist/browser/client/a2a-client.js +221 -0
  6. package/dist/browser/client/index.d.ts +1 -0
  7. package/dist/browser/client/index.js +1 -0
  8. package/dist/browser/services/a2a/helpers/message-builder.d.ts +12 -0
  9. package/dist/browser/services/a2a/helpers/message-builder.js +61 -0
  10. package/dist/browser/transport/rpc/parser.d.ts +15 -0
  11. package/dist/browser/transport/rpc/parser.js +48 -0
  12. package/dist/browser/transport/rpc/rpc-client.d.ts +80 -0
  13. package/dist/browser/transport/rpc/rpc-client.js +189 -0
  14. package/dist/browser/transport/streaming/event-stream.d.ts +25 -0
  15. package/dist/browser/transport/streaming/event-stream.js +99 -0
  16. package/dist/browser/types/ext.d.ts +13 -0
  17. package/dist/browser/types/ext.js +10 -0
  18. package/dist/browser/types/index.d.ts +4 -0
  19. package/dist/browser/types/index.js +4 -0
  20. package/dist/browser/types/interfaces/client.d.ts +135 -0
  21. package/dist/browser/types/interfaces/client.js +5 -0
  22. package/dist/browser/types/interfaces/index.d.ts +3 -0
  23. package/dist/browser/types/interfaces/index.js +3 -0
  24. package/dist/browser/types/interfaces/services/a2a/builder.d.ts +37 -0
  25. package/dist/browser/types/interfaces/services/a2a/builder.js +5 -0
  26. package/dist/browser/types/interfaces/services/a2a/context.d.ts +162 -0
  27. package/dist/browser/types/interfaces/services/a2a/context.js +5 -0
  28. package/dist/browser/types/interfaces/services/a2a/engine.d.ts +7 -0
  29. package/dist/browser/types/interfaces/services/a2a/engine.js +5 -0
  30. package/dist/browser/types/interfaces/services/a2a/index.d.ts +5 -0
  31. package/dist/browser/types/interfaces/services/a2a/index.js +5 -0
  32. package/dist/browser/types/interfaces/services/a2a/legacy.d.ts +93 -0
  33. package/dist/browser/types/interfaces/services/a2a/legacy.js +5 -0
  34. package/dist/browser/types/interfaces/services/a2a/service.d.ts +413 -0
  35. package/dist/browser/types/interfaces/services/a2a/service.js +5 -0
  36. package/dist/browser/types/interfaces/services/core/context/command.d.ts +25 -0
  37. package/dist/browser/types/interfaces/services/core/context/command.js +5 -0
  38. package/dist/browser/types/interfaces/services/core/context/context.d.ts +207 -0
  39. package/dist/browser/types/interfaces/services/core/context/context.js +5 -0
  40. package/dist/browser/types/interfaces/services/core/context/index.d.ts +3 -0
  41. package/dist/browser/types/interfaces/services/core/context/index.js +3 -0
  42. package/dist/browser/types/interfaces/services/core/context/types.d.ts +11 -0
  43. package/dist/browser/types/interfaces/services/core/context/types.js +5 -0
  44. package/dist/browser/types/interfaces/services/core/execution/engine.d.ts +106 -0
  45. package/dist/browser/types/interfaces/services/core/execution/engine.js +5 -0
  46. package/dist/browser/types/interfaces/services/core/execution/environment.d.ts +11 -0
  47. package/dist/browser/types/interfaces/services/core/execution/environment.js +5 -0
  48. package/dist/browser/types/interfaces/services/core/execution/execute.d.ts +7 -0
  49. package/dist/browser/types/interfaces/services/core/execution/execute.js +5 -0
  50. package/dist/browser/types/interfaces/services/core/execution/index.d.ts +3 -0
  51. package/dist/browser/types/interfaces/services/core/execution/index.js +3 -0
  52. package/dist/browser/types/interfaces/services/core/index.d.ts +4 -0
  53. package/dist/browser/types/interfaces/services/core/index.js +4 -0
  54. package/dist/browser/types/interfaces/services/core/managers/cancellation.d.ts +9 -0
  55. package/dist/browser/types/interfaces/services/core/managers/cancellation.js +5 -0
  56. package/dist/browser/types/interfaces/services/core/managers/connection.d.ts +9 -0
  57. package/dist/browser/types/interfaces/services/core/managers/connection.js +5 -0
  58. package/dist/browser/types/interfaces/services/core/managers/context.d.ts +17 -0
  59. package/dist/browser/types/interfaces/services/core/managers/context.js +5 -0
  60. package/dist/browser/types/interfaces/services/core/managers/event.d.ts +328 -0
  61. package/dist/browser/types/interfaces/services/core/managers/event.js +5 -0
  62. package/dist/browser/types/interfaces/services/core/managers/index.d.ts +6 -0
  63. package/dist/browser/types/interfaces/services/core/managers/index.js +6 -0
  64. package/dist/browser/types/interfaces/services/core/managers/stream.d.ts +217 -0
  65. package/dist/browser/types/interfaces/services/core/managers/stream.js +5 -0
  66. package/dist/browser/types/interfaces/services/core/managers/task.d.ts +9 -0
  67. package/dist/browser/types/interfaces/services/core/managers/task.js +1 -0
  68. package/dist/browser/types/interfaces/services/core/service.d.ts +115 -0
  69. package/dist/browser/types/interfaces/services/core/service.js +5 -0
  70. package/dist/browser/types/interfaces/services/index.d.ts +4 -0
  71. package/dist/browser/types/interfaces/services/index.js +4 -0
  72. package/dist/browser/types/interfaces/services/mcp/index.d.ts +1 -0
  73. package/dist/browser/types/interfaces/services/mcp/index.js +1 -0
  74. package/dist/browser/types/interfaces/services/mcp/service.d.ts +49 -0
  75. package/dist/browser/types/interfaces/services/mcp/service.js +5 -0
  76. package/dist/browser/types/interfaces/services/protocol.d.ts +33 -0
  77. package/dist/browser/types/interfaces/services/protocol.js +34 -0
  78. package/dist/browser/types/interfaces/storage.d.ts +8 -0
  79. package/dist/browser/types/interfaces/storage.js +5 -0
  80. package/dist/browser/types/schemas/a2a/agent.d.ts +2583 -0
  81. package/dist/browser/types/schemas/a2a/agent.js +323 -0
  82. package/dist/browser/types/schemas/a2a/auth.d.ts +908 -0
  83. package/dist/browser/types/schemas/a2a/auth.js +283 -0
  84. package/dist/browser/types/schemas/a2a/error.d.ts +396 -0
  85. package/dist/browser/types/schemas/a2a/error.js +163 -0
  86. package/dist/browser/types/schemas/a2a/index.d.ts +11 -0
  87. package/dist/browser/types/schemas/a2a/index.js +11 -0
  88. package/dist/browser/types/schemas/a2a/kind.d.ts +11 -0
  89. package/dist/browser/types/schemas/a2a/kind.js +20 -0
  90. package/dist/browser/types/schemas/a2a/message.d.ts +10343 -0
  91. package/dist/browser/types/schemas/a2a/message.js +130 -0
  92. package/dist/browser/types/schemas/a2a/notification.d.ts +1517 -0
  93. package/dist/browser/types/schemas/a2a/notification.js +203 -0
  94. package/dist/browser/types/schemas/a2a/parameters.d.ts +956 -0
  95. package/dist/browser/types/schemas/a2a/parameters.js +241 -0
  96. package/dist/browser/types/schemas/a2a/protocol.d.ts +14363 -0
  97. package/dist/browser/types/schemas/a2a/protocol.js +59 -0
  98. package/dist/browser/types/schemas/a2a/rpc.d.ts +182 -0
  99. package/dist/browser/types/schemas/a2a/rpc.js +126 -0
  100. package/dist/browser/types/schemas/a2a/task.d.ts +5886 -0
  101. package/dist/browser/types/schemas/a2a/task.js +134 -0
  102. package/dist/browser/types/schemas/a2a/transport.d.ts +31 -0
  103. package/dist/browser/types/schemas/a2a/transport.js +28 -0
  104. package/dist/browser/types/schemas/index.d.ts +1 -0
  105. package/dist/browser/types/schemas/index.js +1 -0
  106. package/dist/browser/types/utils/index.d.ts +1 -0
  107. package/dist/browser/types/utils/index.js +1 -0
  108. package/dist/browser/types/utils/transform.d.ts +64 -0
  109. package/dist/browser/types/utils/transform.js +35 -0
  110. package/dist/browser/utils/common/constants.d.ts +11 -0
  111. package/dist/browser/utils/common/constants.js +38 -0
  112. package/dist/browser/utils/common/errors.d.ts +24 -0
  113. package/dist/browser/utils/common/errors.js +42 -0
  114. package/dist/browser/utils/common/utils.d.ts +9 -0
  115. package/dist/browser/utils/common/utils.js +11 -0
  116. package/dist/browser/utils/logging/index.d.ts +2 -0
  117. package/dist/browser/utils/logging/index.js +2 -0
  118. package/dist/browser/utils/logging/log.d.ts +33 -0
  119. package/dist/browser/utils/logging/log.js +75 -0
  120. package/dist/browser/utils/logging/logger.d.ts +18 -0
  121. package/dist/browser/utils/logging/logger.js +18 -0
  122. package/dist/client/a2a-client.d.ts +2 -1
  123. package/dist/client/a2a-client.js +13 -4
  124. package/dist/server/express/errors.js +1 -1
  125. package/dist/server/express/middeware.d.ts +2 -2
  126. package/dist/server/express/middeware.js +26 -6
  127. package/dist/server/express/server.d.ts +2 -1
  128. package/dist/server/express/server.js +32 -6
  129. package/dist/services/a2a/factory/builder.js +6 -1
  130. package/dist/services/a2a/factory/service.js +1 -1
  131. package/dist/services/a2a/helpers/agentcard-builder.js +1 -0
  132. package/dist/services/a2a/helpers/history.d.ts +2 -0
  133. package/dist/services/a2a/helpers/history.js +3 -0
  134. package/dist/services/a2a/helpers/index.d.ts +2 -0
  135. package/dist/services/a2a/helpers/index.js +2 -0
  136. package/dist/services/a2a/methods/get-task.js +2 -0
  137. package/dist/services/a2a/methods/send-message.js +18 -1
  138. package/dist/services/a2a/service.d.ts +2 -1
  139. package/dist/services/a2a/service.js +20 -6
  140. package/dist/services/mcp/service.js +0 -1
  141. package/dist/transport/rpc/parser.js +2 -2
  142. package/dist/transport/rpc/rpc-client.js +2 -2
  143. package/dist/types/interfaces/services/a2a/service.d.ts +4 -0
  144. package/dist/utils/common/constants.js +1 -1
  145. package/dist/utils/common/errors.d.ts +2 -1
  146. package/dist/utils/common/errors.js +2 -1
  147. package/dist/utils/common/schema-validation.d.ts +2 -0
  148. package/dist/utils/common/schema-validation.js +12 -0
  149. package/dist/utils/index.d.ts +1 -0
  150. package/dist/utils/index.js +1 -0
  151. package/package.json +20 -17
package/README.md CHANGED
@@ -8,26 +8,44 @@
8
8
 
9
9
  # artinet-sdk
10
10
 
11
- Give your AI agent the power to communicate with any other agent, no matter the framework.
11
+ Create Agents that communicate across framework.
12
12
 
13
13
  The artinet-sdk is a TypeScript library that adds a standardized, interoperable communication layer to your agents.
14
14
 
15
15
  ## Features
16
16
 
17
- - **Easy To Use:** The AgentBuilder lets you quickly create an Agent2Agent API, while handling all of the communication complexity.
18
- - **No Vendor Lock-In:** Create agents that can communicate with other agents across frameworks and ecosystems with just a few lines of code.
17
+ - **Hassle Free:** Use the AgentBuilder to quickly setup an AgentServer.
18
+ - **No Vendor Lock-In:** Let your agents communicate with other agents across frameworks and ecosystems.
19
19
  - **Flexible Design:** Everything you need to build collaborative agents while remaining modular enough for advanced configuration.
20
20
 
21
21
  ## Quick Start
22
22
 
23
- Use the [`create-agent`](https://www.npmjs.com/package/@artinet/create-agent) command:
23
+ **Use the [`create-agent`](https://www.npmjs.com/package/@artinet/create-agent) command:**
24
24
 
25
25
  ```bash
26
26
  npx @artinet/create-agent@latest
27
27
  ```
28
-
29
28
  It has [serveral template projects](https://github.com/the-artinet-project/create-agent/tree/main/templates) that you can use to jump right into agent building.
30
29
 
30
+ **Or use [`easy-a2a`](https://github.com/the-artinet-project/easy-a2a):**
31
+
32
+ ```typescript
33
+ const agent = a2a({
34
+ baseURL: "https://your-api.com/api/v1",
35
+ apiKey: "your-api-key",
36
+ })
37
+ .ai("You are a helpful assistant.")
38
+ .createAgent({
39
+ agentCard: "MyAgent",
40
+ });
41
+ ```
42
+
43
+ To build agents backed by OpenAI API compatible endpoints.
44
+
45
+ ```bash
46
+ npm install easy-a2a
47
+ ```
48
+
31
49
  ## Table of Contents
32
50
 
33
51
  - [artinet-sdk](#artinet-sdk)
@@ -41,6 +59,7 @@ It has [serveral template projects](https://github.com/the-artinet-project/creat
41
59
  - [Usage](#usage)
42
60
  - [Client](#client)
43
61
  - [Basic Client Usage](#basic-client-usage)
62
+ - [Browser Usage](#browser-usage)
44
63
  - [Streaming Updates](#streaming-updates)
45
64
  - [Authentication](#authentication)
46
65
  - [Server](#server)
@@ -51,31 +70,11 @@ It has [serveral template projects](https://github.com/the-artinet-project/creat
51
70
  - [Persistent Storage](#persistent-storage)
52
71
  - [Advanced Server Customization](#advanced-server-customization)
53
72
  - [Cross Protocol Support](#cross-protocol-support)
73
+ - [**Migration Changes**](#migration-changes)
54
74
  - [Contributing](#contributing)
55
75
  - [License](#license)
56
76
  - [Acknowledgements](#acknowledgements)
57
77
 
58
- **Breaking Changes since v0.5.8**
59
-
60
- - Pino has been removed and replaced with console for better portability and is set to silent by default.
61
- - The default handler for streamMessage no longer automatically emits an initial `submitted` and `working` event.
62
- - Agent Registration, Bundling and Deployment utils have been removed (email us: humans@artinet.io for support).
63
- - `@artinet/metadata-validator` has been removed due to build issues.
64
- - getTask now correctly takes TaskQueryParams as an argument vs TaskIdParams in accordance with the A2A spec.
65
- - AgentBuilder now returns a unique messageId for each status update instead of the original user provided messageId.
66
- - AgentBuilder now prefers the contextId & taskId from the calling context.
67
- - In a future release the following packages will be set as peer dependancies to reduce the size of the build: `@modelcontextprotocol/sdk`, `@trpc/server`, `cors`, `express`
68
- - The `history` object from `TaskAndHistory` is deprecated and no longer being updated. Use `Task.history` instead.
69
- - The `A2AClient` now checks `/.well-known/agent-card.json` as a opposed to `/.well-known/agent.json` in-line with the A2A spec.
70
- - The `A2AClient` now uses uses the `AgentCard`.url if an `AgentCard` has been successfully retrieved, else it will default to the `baseUrl`.
71
- - The examples folder will be removed in favor of [`create-agent`](https://github.com/the-artinet-project/create-agent).
72
- - In `Task` the `contextId` field is now required (inline with the A2A spec).
73
- - In `AgentSkill` the `tag` field is now required (inline with the A2A spec).
74
- - ~~Optional fields in Agent2Agent Zod schemas are now nullable for better interoperability.~~ **Nullable Schemas have been reverted.**
75
- - The `EngineBuilder` constructor is now protected and open for extension.
76
- - `AgentBuilder` will now throw an error if it recieves an invalid `FilePart`.
77
- - `createAgent`/`createService` can now take a single string (i.e. agentName) as valid value for the AgentCard and will populate the rest of the required fields with placeholder values (see `src/services/a2a/helpers/agentcard-builder.ts` for reference).
78
-
79
78
  ## Installation
80
79
 
81
80
  ```bash
@@ -169,6 +168,43 @@ const client = new A2AClient("https://your-a2a-server.com/a2a");
169
168
  const task: Task = await client.sendMessage("What is the capital of France?");
170
169
  ```
171
170
 
171
+ #### Browser Usage
172
+
173
+ _Experimental_
174
+
175
+ The Client can be used directly in browsers. You'll need to load the required external dependencies: `zod`, `uuid`, and `eventsource-parser`.
176
+
177
+ ```html
178
+ <!DOCTYPE html>
179
+ <html>
180
+ <head>
181
+ <!-- Required external dependencies -->
182
+ <script type="importmap">
183
+ {
184
+ "imports": {
185
+ "zod": "https://esm.sh/zod@3.23.8",
186
+ "uuid": "https://esm.sh/uuid@11.1.0",
187
+ "eventsource-parser": "https://esm.sh/eventsource-parser@3.0.1"
188
+ }
189
+ }
190
+ </script>
191
+ </head>
192
+ <body>
193
+ <script type="module">
194
+ const { A2AClient } = await import("@artinet/sdk");
195
+ const client = new A2AClient("http://localhost:4000/a2a");
196
+
197
+ const stream = await client.sendStreamingMessage("Hello!");
198
+ for await (const update of stream) {
199
+ console.log(update);
200
+ }
201
+ </script>
202
+ </body>
203
+ </html>
204
+ ```
205
+
206
+ > **Note:** Uses [esm.sh](https://esm.sh) as a CDN. See [`examples/browser-example.html`](examples/browser-example.html) for a complete example.
207
+
172
208
  #### Streaming Updates
173
209
 
174
210
  Receive real-time updates via SSE using `message/stream`.
@@ -625,6 +661,45 @@ const result = await client.callTool({
625
661
  - `send-streaming-message`, `task-resubscribe` & `push-notifications` etc are currently not supported by default.
626
662
  - Leverage the A2A Zod Schemas to manually implement your own tools.
627
663
 
664
+ ## **Migration Changes**
665
+
666
+ \*_since v0.5.8_
667
+
668
+ - Pino has been removed and replaced with console for better portability and is set to silent by default.
669
+ - The default handler for streamMessage no longer automatically emits an initial `submitted` and `working` event.
670
+ - Agent Registration, Bundling and Deployment utils have been removed (email us: humans@artinet.io for support).
671
+ - `@artinet/metadata-validator` has been removed due to build issues.
672
+ - getTask now correctly takes TaskQueryParams as an argument vs TaskIdParams in accordance with the A2A spec.
673
+ - AgentBuilder now returns a unique messageId for each status update instead of the original user provided messageId.
674
+ - AgentBuilder now prefers the contextId & taskId from the calling context.
675
+ - In a future release the following packages will be set as peer dependancies to reduce the size of the build: `@modelcontextprotocol/sdk`, `@trpc/server`, `cors`, `express`
676
+ - The `history` object from `TaskAndHistory` is deprecated and no longer being updated. Use `Task.history` instead.
677
+ - The `A2AClient` now checks `/.well-known/agent-card.json` as a opposed to `/.well-known/agent.json` in-line with the A2A spec.
678
+ - The `A2AClient` now uses uses the `AgentCard`.url if an `AgentCard` has been successfully retrieved, else it will default to the `baseUrl`.
679
+ - The examples folder will be removed in favor of [`create-agent`](https://github.com/the-artinet-project/create-agent).
680
+ - In `Task` the `contextId` field is now required (inline with the A2A spec).
681
+ - In `AgentSkill` the `tag` field is now required (inline with the A2A spec).
682
+ - ~~Optional fields in Agent2Agent Zod schemas are now nullable for better interoperability.~~ **Nullable Schemas have been reverted.**
683
+ - The `EngineBuilder` constructor is now protected and open for extension.
684
+ - `AgentBuilder` will now throw an error if it recieves an invalid `FilePart`.
685
+ - `createAgent`/`createService` can now take a single string (i.e. agentName) as valid value for the AgentCard and will populate the rest of the required fields with placeholder values (see `src/services/a2a/helpers/agentcard-builder.ts` for reference).
686
+ - `createAgentServer` no longer adds `express.json()` to the root of the express server and now uses the utility function `rpcParser` only on the agents `basePath` and has stricter JSON-RPC validation measures.
687
+ - `A2AClient` now exposes `mergePath` making it easier to access `AgentCards` that are not exposed at the root.
688
+ - `AgentBuilder` now checks for cancellations after each step.
689
+ - `createAgent` exposes the `enforceParamValidation` flag which triggers stricter enforcement of `A2ASchemas` (This will be enabled by default in a future release).
690
+ - `AgentCardBuilder` now sets the `preferredTransport` field to the default (`JSONRPC`) if none is provided (inline with the A2A spec).
691
+ - The default `sendMessage` implementation now supports the `MessageSendConfiguration`.`blocking` toggle.
692
+ - The default `sendMessage` and `getTask` implementations now support the `MessageSendConfiguration`.`historyLength` parameter.
693
+ - The Express Server now provides support for `AuthenticatedExtendedCard`.
694
+
695
+ > **Note:** The Official A2A Javascript SDK is now more stable. So over the course of future releases @artinet/sdk will be merging in utilities directly from `@a2a-js/sdk` as a peer-dependancy.
696
+
697
+ > This will **NOT** change the core architecture or design of @artinet/sdk, but it will make integration with the emerging Agent2Agent ecosystem easier while allowing us to focus on the adoption of additional communication protocols. We aim to complete this migration by v0.6 which will be our first LTS release.
698
+
699
+ > This will **NOT** require the modification of existing `AgentEngine`'s, the current architecture was designed with this shift in mind and is the reason behind our use of loosely typed Interfaces, MPSC & SPMC queues vs EventBus and the design of the `CoreExecute` contract (onStart, onUpdate, onError, onCancel & onComplete).
700
+
701
+ > This will mean that the more concrete implementations in `src/services/a2a` will become more generic implementations in `src/services/core`.
702
+
628
703
  ## Contributing
629
704
 
630
705
  Contributions are welcome! Please open an issue or submit a Pull Request on [GitHub](https://github.com/the-artinet-project/artinet-sdk).
@@ -0,0 +1,9 @@
1
+ export * from "./types/index.js";
2
+ export * from "./client/index.js";
3
+ export { executeJsonRpcRequest, executeGetRequest, createJsonRpcRequest, sendJsonRpcRequest, } from "./transport/rpc/rpc-client.js";
4
+ export { executeStreamEvents } from "./transport/streaming/event-stream.js";
5
+ export * from "./utils/common/constants.js";
6
+ export * from "./utils/common/errors.js";
7
+ export * from "./utils/common/utils.js";
8
+ export * from "./utils/logging/log.js";
9
+ export { A2AClient } from "./client/a2a-client.js";
@@ -0,0 +1,10 @@
1
+ //browser only entry point
2
+ export * from "./types/index.js";
3
+ export * from "./client/index.js";
4
+ export { executeJsonRpcRequest, executeGetRequest, createJsonRpcRequest, sendJsonRpcRequest, } from "./transport/rpc/rpc-client.js";
5
+ export { executeStreamEvents } from "./transport/streaming/event-stream.js";
6
+ export * from "./utils/common/constants.js";
7
+ export * from "./utils/common/errors.js";
8
+ export * from "./utils/common/utils.js";
9
+ export * from "./utils/logging/log.js";
10
+ export { A2AClient } from "./client/a2a-client.js";
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import type { AgentCard, MessageSendParams, TaskQueryParams, TaskIdParams, TaskPushNotificationConfig, Task, Message, UpdateEvent } from "../types/index.js";
6
+ import type { Client } from "../types/interfaces/client.js";
7
+ /**
8
+ * A2AClient is the main client class for interacting with Agent2Agent (A2A) protocol-compliant services.
9
+ * It provides methods for sending tasks, retrieving statuses, canceling operations, and handling streaming responses.
10
+ */
11
+ export declare class A2AClient implements Client {
12
+ private baseUrl;
13
+ private cachedAgentCard;
14
+ private customHeaders;
15
+ private fallbackPath;
16
+ private agentUrl;
17
+ private mergePath;
18
+ /**
19
+ * Creates a new A2AClient instance.
20
+ * @param baseUrl The base URL for the A2A server.
21
+ * @param headers Optional custom headers to include in all requests.
22
+ * @param fallbackPath Optional fallback path to use if the agent card is not found at the base URL.
23
+ * @example
24
+ * const client = new A2AClient("http://localhost:4000/a2a");
25
+ * const card = await client.agentCard();
26
+ * console.log(card);
27
+ * @example
28
+ * const client = new A2AClient("http://localhost:4000/a2a", {}, "/agent-card");
29
+ * const card = await client.agentCard();
30
+ * console.log(card);
31
+ */
32
+ constructor(baseUrl: URL | string, headers?: Record<string, string>, fallbackPath?: string, mergePath?: boolean);
33
+ /**
34
+ * Retrieves the AgentCard from the A2A server.
35
+ * Caches the result after the first successful fetch.
36
+ * @returns A promise resolving to the AgentCard.
37
+ */
38
+ agentCard(): Promise<AgentCard>;
39
+ /**
40
+ * Refreshes the cached AgentCard by fetching it again from the server.
41
+ * @returns A promise resolving to the updated AgentCard.
42
+ */
43
+ refreshAgentCard(): Promise<AgentCard>;
44
+ /**
45
+ * Sends a Message to an agent server.
46
+ * @param params The parameters for the message/send method.
47
+ * @returns A promise resolving to Message/Task response from the agent server or null.
48
+ */
49
+ sendMessage(params: MessageSendParams | string): Promise<Message | Task | null>;
50
+ /**
51
+ * @deprecated Use sendMessage instead.
52
+ * Sends a task request to the agent (non-streaming).
53
+ * @param params The parameters for the message/send method.
54
+ * @returns A promise resolving to the Task object or null.
55
+ */
56
+ sendTask(params: MessageSendParams): Promise<Message | Task | null>;
57
+ /**
58
+ * Sends a Message and returns a stream of status and artifact updates.
59
+ * @param params Task parameters for the request
60
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent/TaskArtifactUpdateEvent/Task/Message payloads.
61
+ */
62
+ sendStreamingMessage(params: MessageSendParams | string): AsyncIterable<UpdateEvent>;
63
+ /**
64
+ * @deprecated Use sendStreamingMessage instead.
65
+ * Sends a task and returns a subscription to status and artifact updates.
66
+ * @param params Task parameters for the request
67
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent or TaskArtifactUpdateEvent payloads.
68
+ */
69
+ sendTaskSubscribe(params: MessageSendParams): AsyncIterable<UpdateEvent>;
70
+ /**
71
+ * Retrieves the current state of a task.
72
+ * @param params The parameters for the tasks/get method.
73
+ * @returns A promise resolving to the Task object or null.
74
+ */
75
+ getTask(params: TaskQueryParams): Promise<Task | null>;
76
+ /**
77
+ * Cancels a currently running task.
78
+ * @param params The parameters for the tasks/cancel method.
79
+ * @returns A promise resolving to the updated Task object (usually canceled state) or null.
80
+ */
81
+ cancelTask(params: TaskIdParams): Promise<Task | null>;
82
+ /**
83
+ * Sets or updates the push notification config for a task.
84
+ * @param params The parameters for the tasks/pushNotificationConfig/set method (which is TaskPushNotificationConfig).
85
+ * @returns A promise resolving to the confirmed TaskPushNotificationConfig or null.
86
+ */
87
+ setTaskPushNotification(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig | null>;
88
+ /**
89
+ * Retrieves the currently configured push notification config for a task.
90
+ * @param params The parameters for the tasks/pushNotificationConfig/get method.
91
+ * @returns A promise resolving to the TaskPushNotificationConfig or null.
92
+ */
93
+ getTaskPushNotification(params: TaskIdParams): Promise<TaskPushNotificationConfig | null>;
94
+ /**
95
+ * Resubscribes to an existing task's update stream.
96
+ * @param params Parameters identifying the task to resubscribe to
97
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent or TaskArtifactUpdateEvent payloads.
98
+ */
99
+ resubscribeTask(params: TaskQueryParams): AsyncIterable<UpdateEvent>;
100
+ /**
101
+ * Checks if the server supports a specific capability based on the agent card.
102
+ * @param capability The capability to check (e.g., 'streaming', 'pushNotifications').
103
+ * @returns A promise resolving to true if the capability is supported.
104
+ */
105
+ supports(capability: "streaming" | "pushNotifications" | "stateTransitionHistory"): Promise<boolean>;
106
+ /**
107
+ * Sets custom headers to be included in all requests.
108
+ * @param headers A record of header name/value pairs.
109
+ */
110
+ setHeaders(headers: Record<string, string>): void;
111
+ /**
112
+ * Adds a single custom header to be included in all requests.
113
+ * @param name The header name.
114
+ * @param value The header value.
115
+ */
116
+ addHeader(name: string, value: string): void;
117
+ /**
118
+ * Removes a custom header.
119
+ * @param name The header name to remove.
120
+ */
121
+ removeHeader(name: string): void;
122
+ /**
123
+ * Clears all custom headers.
124
+ */
125
+ clearHeaders(): void;
126
+ }
@@ -0,0 +1,221 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { executeJsonRpcRequest, executeGetRequest, } from "../transport/rpc/rpc-client.js";
6
+ import { executeStreamEvents } from "../transport/streaming/event-stream.js";
7
+ import { INTERNAL_ERROR } from "../utils/common/errors.js";
8
+ import { logError } from "../utils/logging/log.js";
9
+ import { createMessageSendParams } from "../services/a2a/helpers/message-builder.js";
10
+ /**
11
+ * A2AClient is the main client class for interacting with Agent2Agent (A2A) protocol-compliant services.
12
+ * It provides methods for sending tasks, retrieving statuses, canceling operations, and handling streaming responses.
13
+ */
14
+ export class A2AClient {
15
+ /**
16
+ * Creates a new A2AClient instance.
17
+ * @param baseUrl The base URL for the A2A server.
18
+ * @param headers Optional custom headers to include in all requests.
19
+ * @param fallbackPath Optional fallback path to use if the agent card is not found at the base URL.
20
+ * @example
21
+ * const client = new A2AClient("http://localhost:4000/a2a");
22
+ * const card = await client.agentCard();
23
+ * console.log(card);
24
+ * @example
25
+ * const client = new A2AClient("http://localhost:4000/a2a", {}, "/agent-card");
26
+ * const card = await client.agentCard();
27
+ * console.log(card);
28
+ */
29
+ constructor(baseUrl, headers = {}, fallbackPath, mergePath = false) {
30
+ this.cachedAgentCard = null;
31
+ this.customHeaders = {};
32
+ this.baseUrl = typeof baseUrl === "string" ? new URL(baseUrl) : baseUrl;
33
+ this.customHeaders = headers;
34
+ this.fallbackPath = fallbackPath ?? "/agent-card";
35
+ this.agentUrl = this.baseUrl;
36
+ this.mergePath = mergePath;
37
+ }
38
+ /**
39
+ * Retrieves the AgentCard from the A2A server.
40
+ * Caches the result after the first successful fetch.
41
+ * @returns A promise resolving to the AgentCard.
42
+ */
43
+ async agentCard() {
44
+ if (this.cachedAgentCard) {
45
+ return this.cachedAgentCard;
46
+ }
47
+ // Standard location for agent cards
48
+ const wellKnownUrl = new URL("/.well-known/agent-card.json", this.baseUrl);
49
+ if (this.mergePath) {
50
+ wellKnownUrl.pathname = this.baseUrl.pathname + wellKnownUrl.pathname;
51
+ }
52
+ try {
53
+ try {
54
+ if (!URL.canParse(wellKnownUrl)) {
55
+ throw new Error("Invalid well-known URL");
56
+ }
57
+ const card = await executeGetRequest(wellKnownUrl, this.customHeaders, "agent card (well-known)");
58
+ if (!card.name || card.name === null || card.name === undefined) {
59
+ throw new Error("No agent card found");
60
+ }
61
+ this.cachedAgentCard = card;
62
+ }
63
+ catch (error) {
64
+ const fallbackUrl = new URL(this.fallbackPath, this.baseUrl);
65
+ if (this.mergePath) {
66
+ fallbackUrl.pathname = this.baseUrl.pathname + fallbackUrl.pathname;
67
+ }
68
+ const fallbackCard = await executeGetRequest(fallbackUrl, this.customHeaders, "agent card (fallback)");
69
+ if (!fallbackCard.name ||
70
+ fallbackCard.name === null ||
71
+ fallbackCard.name === undefined) {
72
+ throw new Error("No fallback agent card found");
73
+ }
74
+ this.cachedAgentCard = fallbackCard;
75
+ }
76
+ }
77
+ catch (error) {
78
+ logError("A2AClient:agentCard", "Failed to fetch or parse agent card:", error);
79
+ throw INTERNAL_ERROR(`Could not retrieve agent card: ${error instanceof Error ? error.message : String(error)}`);
80
+ }
81
+ this.agentUrl = new URL(this.cachedAgentCard.url, this.baseUrl);
82
+ return this.cachedAgentCard;
83
+ }
84
+ /**
85
+ * Refreshes the cached AgentCard by fetching it again from the server.
86
+ * @returns A promise resolving to the updated AgentCard.
87
+ */
88
+ async refreshAgentCard() {
89
+ this.cachedAgentCard = null;
90
+ return this.agentCard();
91
+ }
92
+ /**
93
+ * Sends a Message to an agent server.
94
+ * @param params The parameters for the message/send method.
95
+ * @returns A promise resolving to Message/Task response from the agent server or null.
96
+ */
97
+ async sendMessage(params) {
98
+ return await executeJsonRpcRequest(this.agentUrl, "message/send", createMessageSendParams(params), this.customHeaders);
99
+ }
100
+ /**
101
+ * @deprecated Use sendMessage instead.
102
+ * Sends a task request to the agent (non-streaming).
103
+ * @param params The parameters for the message/send method.
104
+ * @returns A promise resolving to the Task object or null.
105
+ */
106
+ async sendTask(params) {
107
+ return await this.sendMessage(params);
108
+ }
109
+ /**
110
+ * Sends a Message and returns a stream of status and artifact updates.
111
+ * @param params Task parameters for the request
112
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent/TaskArtifactUpdateEvent/Task/Message payloads.
113
+ */
114
+ sendStreamingMessage(params) {
115
+ return executeStreamEvents(this.agentUrl, "message/stream", createMessageSendParams(params), this.customHeaders);
116
+ }
117
+ /**
118
+ * @deprecated Use sendStreamingMessage instead.
119
+ * Sends a task and returns a subscription to status and artifact updates.
120
+ * @param params Task parameters for the request
121
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent or TaskArtifactUpdateEvent payloads.
122
+ */
123
+ sendTaskSubscribe(params) {
124
+ return this.sendStreamingMessage(params);
125
+ }
126
+ /**
127
+ * Retrieves the current state of a task.
128
+ * @param params The parameters for the tasks/get method.
129
+ * @returns A promise resolving to the Task object or null.
130
+ */
131
+ async getTask(params) {
132
+ return await executeJsonRpcRequest(this.agentUrl, "tasks/get", params, this.customHeaders);
133
+ }
134
+ /**
135
+ * Cancels a currently running task.
136
+ * @param params The parameters for the tasks/cancel method.
137
+ * @returns A promise resolving to the updated Task object (usually canceled state) or null.
138
+ */
139
+ async cancelTask(params) {
140
+ return await executeJsonRpcRequest(this.agentUrl, "tasks/cancel", params, this.customHeaders);
141
+ }
142
+ /**
143
+ * Sets or updates the push notification config for a task.
144
+ * @param params The parameters for the tasks/pushNotificationConfig/set method (which is TaskPushNotificationConfig).
145
+ * @returns A promise resolving to the confirmed TaskPushNotificationConfig or null.
146
+ */
147
+ async setTaskPushNotification(params) {
148
+ return await executeJsonRpcRequest(this.agentUrl, "tasks/pushNotificationConfig/set", params, this.customHeaders);
149
+ }
150
+ /**
151
+ * Retrieves the currently configured push notification config for a task.
152
+ * @param params The parameters for the tasks/pushNotificationConfig/get method.
153
+ * @returns A promise resolving to the TaskPushNotificationConfig or null.
154
+ */
155
+ async getTaskPushNotification(params) {
156
+ return await executeJsonRpcRequest(this.agentUrl, "tasks/pushNotificationConfig/get", params, this.customHeaders);
157
+ }
158
+ /**
159
+ * Resubscribes to an existing task's update stream.
160
+ * @param params Parameters identifying the task to resubscribe to
161
+ * @returns An AsyncIterable that yields TaskStatusUpdateEvent or TaskArtifactUpdateEvent payloads.
162
+ */
163
+ resubscribeTask(params) {
164
+ return executeStreamEvents(this.agentUrl, "tasks/resubscribe", params, this.customHeaders);
165
+ }
166
+ /**
167
+ * Checks if the server supports a specific capability based on the agent card.
168
+ * @param capability The capability to check (e.g., 'streaming', 'pushNotifications').
169
+ * @returns A promise resolving to true if the capability is supported.
170
+ */
171
+ async supports(capability) {
172
+ try {
173
+ const card = await this.agentCard();
174
+ if (!card.capabilities) {
175
+ return false;
176
+ }
177
+ switch (capability) {
178
+ case "streaming":
179
+ return !!card.capabilities.streaming;
180
+ case "pushNotifications":
181
+ return !!card.capabilities.pushNotifications;
182
+ case "stateTransitionHistory":
183
+ return !!card.capabilities.stateTransitionHistory;
184
+ default:
185
+ return false;
186
+ }
187
+ }
188
+ catch (error) {
189
+ logError("A2AClient:supports", `Failed to determine support for capability '${capability}':`, error);
190
+ return false; // Assume not supported if card fetch fails
191
+ }
192
+ }
193
+ /**
194
+ * Sets custom headers to be included in all requests.
195
+ * @param headers A record of header name/value pairs.
196
+ */
197
+ setHeaders(headers) {
198
+ this.customHeaders = { ...headers };
199
+ }
200
+ /**
201
+ * Adds a single custom header to be included in all requests.
202
+ * @param name The header name.
203
+ * @param value The header value.
204
+ */
205
+ addHeader(name, value) {
206
+ this.customHeaders[name] = value;
207
+ }
208
+ /**
209
+ * Removes a custom header.
210
+ * @param name The header name to remove.
211
+ */
212
+ removeHeader(name) {
213
+ delete this.customHeaders[name];
214
+ }
215
+ /**
216
+ * Clears all custom headers.
217
+ */
218
+ clearHeaders() {
219
+ this.customHeaders = {};
220
+ }
221
+ }
@@ -0,0 +1 @@
1
+ export * from "./a2a-client.js";
@@ -0,0 +1 @@
1
+ export * from "./a2a-client.js";
@@ -0,0 +1,12 @@
1
+ import { MessageSendParams, Message as MessageType, MessageSendConfiguration } from "../../../types/index.js";
2
+ export declare class MessageBuilder {
3
+ message: MessageType;
4
+ constructor(message?: Partial<MessageType>);
5
+ valueOf(): MessageType;
6
+ }
7
+ export declare class MessageSendConfigurationBuilder {
8
+ configuration: MessageSendConfiguration;
9
+ constructor(configuration?: Partial<MessageSendConfiguration> | null);
10
+ valueOf(): MessageSendConfiguration;
11
+ }
12
+ export declare const createMessageSendParams: (messageSendParams: Partial<MessageSendParams> | string) => MessageSendParams;
@@ -0,0 +1,61 @@
1
+ import { v4 as uuidv4 } from "uuid";
2
+ export class MessageBuilder {
3
+ constructor(message = {}) {
4
+ this.message = {
5
+ role: "user",
6
+ parts: [],
7
+ metadata: undefined,
8
+ extensions: undefined,
9
+ referenceTaskIds: undefined,
10
+ messageId: uuidv4(),
11
+ taskId: undefined,
12
+ contextId: undefined,
13
+ kind: "message",
14
+ };
15
+ this.message.role = message.role ?? this.message.role;
16
+ this.message.parts = message.parts ?? this.message.parts;
17
+ this.message.metadata = message.metadata ?? this.message.metadata;
18
+ this.message.extensions = message.extensions ?? this.message.extensions;
19
+ this.message.referenceTaskIds =
20
+ message.referenceTaskIds ?? this.message.referenceTaskIds;
21
+ this.message.messageId = message.messageId ?? this.message.messageId;
22
+ this.message.taskId = message.taskId ?? this.message.taskId;
23
+ this.message.contextId = message.contextId ?? this.message.contextId;
24
+ }
25
+ valueOf() {
26
+ return this.message;
27
+ }
28
+ }
29
+ export class MessageSendConfigurationBuilder {
30
+ constructor(configuration = {}) {
31
+ this.configuration = {
32
+ acceptedOutputModes: undefined,
33
+ historyLength: undefined,
34
+ pushNotificationConfig: undefined,
35
+ blocking: undefined,
36
+ };
37
+ this.configuration.acceptedOutputModes =
38
+ configuration?.acceptedOutputModes ??
39
+ this.configuration.acceptedOutputModes;
40
+ this.configuration.historyLength =
41
+ configuration?.historyLength ?? this.configuration.historyLength;
42
+ this.configuration.pushNotificationConfig =
43
+ configuration?.pushNotificationConfig ??
44
+ this.configuration.pushNotificationConfig;
45
+ this.configuration.blocking =
46
+ configuration?.blocking ?? this.configuration.blocking;
47
+ }
48
+ valueOf() {
49
+ return this.configuration;
50
+ }
51
+ }
52
+ export const createMessageSendParams = (messageSendParams) => {
53
+ const isString = typeof messageSendParams === "string";
54
+ return {
55
+ message: new MessageBuilder(isString
56
+ ? { parts: [{ text: messageSendParams, kind: "text" }] }
57
+ : messageSendParams.message).valueOf(),
58
+ configuration: new MessageSendConfigurationBuilder(isString ? undefined : messageSendParams.configuration).valueOf(),
59
+ metadata: isString ? undefined : messageSendParams.metadata,
60
+ };
61
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { JSONRPCResponse } from "../../types/index.js";
6
+ /**
7
+ * Parses a JSON-RPC response string and validates its structure.
8
+ * If the response contains an error, it is thrown as an A2AError.
9
+ * If the response contains neither a result nor an error, a validation error is thrown.
10
+ *
11
+ * @param data Response data as string
12
+ * @returns The parsed and validated response object
13
+ * @throws A2AError if the response contains an error or is invalid
14
+ */
15
+ export declare function parseResponse<Res extends JSONRPCResponse>(data: string): Res;