@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
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { JSONRPCErrorSchema, } from "../../types/index.js";
6
+ import { SystemError, PARSE_ERROR } from "../../utils/common/errors.js";
7
+ import { logError } from "../../utils/logging/log.js";
8
+ /**
9
+ * Parses a JSON-RPC response string and validates its structure.
10
+ * If the response contains an error, it is thrown as an A2AError.
11
+ * If the response contains neither a result nor an error, a validation error is thrown.
12
+ *
13
+ * @param data Response data as string
14
+ * @returns The parsed and validated response object
15
+ * @throws A2AError if the response contains an error or is invalid
16
+ */
17
+ export function parseResponse(data) {
18
+ if (!data) {
19
+ throw PARSE_ERROR("Invalid response data");
20
+ }
21
+ try {
22
+ const parsed = JSON.parse(data); //todo: leverage safe parse
23
+ if (parsed.error) {
24
+ const parsedError = JSONRPCErrorSchema.safeParse(parsed.error);
25
+ if (!parsedError.success) {
26
+ throw PARSE_ERROR(parsedError.error);
27
+ }
28
+ throw new SystemError(parsedError.data.message, parsedError.data.code, parsedError.data.data);
29
+ }
30
+ if (typeof parsed !== "object" ||
31
+ parsed === null ||
32
+ parsed.jsonrpc !== "2.0") {
33
+ throw PARSE_ERROR("invalid jsonrpc");
34
+ }
35
+ if (parsed.result === undefined) {
36
+ throw PARSE_ERROR("result is undefined");
37
+ }
38
+ return parsed;
39
+ }
40
+ catch (error) {
41
+ if (error instanceof SystemError) {
42
+ logError("parseResponse", "SystemError:", error.message);
43
+ throw error;
44
+ }
45
+ logError("parseResponse", "Error parsing response:", data);
46
+ throw PARSE_ERROR(error);
47
+ }
48
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import type { JSONRPCRequest, JSONRPCResponse, A2ARequest } from "../../types/index.js";
6
+ /**
7
+ * Creates a JSON-RPC request body with the specified method and parameters.
8
+ *, ErrorCodeParseError
9
+ * @param method The JSON-RPC method name
10
+ * @param params The parameters for the method
11
+ * @param requestId Optional request ID (generates a UUID v4 if not provided)
12
+ * @returns A properly formatted JSON-RPC request object
13
+ */
14
+ export declare function createJsonRpcRequest<Req extends A2ARequest>(method: Req["method"], params: Req["params"], requestId?: string | number): JSONRPCRequest;
15
+ /**
16
+ * Sends a JSON-RPC request to the specified endpoint.
17
+ *
18
+ * @param baseUrl The API endpoint URL
19
+ * @param method The JSON-RPC method name
20
+ * @param params The parameters for the method
21
+ * @param headers Custom headers to include in the request
22
+ * @param acceptHeader The desired Accept header ('application/json' or 'text/event-stream')
23
+ * @returns A Promise resolving to the fetch Response object
24
+ * @throws RpcError if there's a network error
25
+ */
26
+ export declare function sendJsonRpcRequest<Req extends A2ARequest>(baseUrl: URL, method: Req["method"], params: Req["params"], headers?: Record<string, string>, acceptHeader?: "application/json" | "text/event-stream"): Promise<Response>;
27
+ /**
28
+ * Sends a GET request to the specified endpoint.
29
+ * This is used for non-JSON-RPC calls like agent card retrieval.
30
+ *
31
+ * @param url The endpoint URL
32
+ * @param headers Custom headers to include in the request
33
+ * @returns A Promise resolving to the fetch Response object
34
+ * @throws RpcError if there's a network error
35
+ */
36
+ export declare function sendGetRequest(url: URL, headers?: Record<string, string>): Promise<Response>;
37
+ /**
38
+ * Processes a standard JSON-RPC response (non-streaming).
39
+ * Parses the response, validates it, and returns the result payload.
40
+ *
41
+ * @param response The fetch Response object
42
+ * @param expectedMethod Optional method name for logging purposes
43
+ * @returns A promise resolving to the result payload
44
+ * @throws RpcError if there's an error in the response
45
+ */
46
+ export declare function handleJsonRpcResponse<Res extends JSONRPCResponse>(response: Response, expectedMethod?: string): Promise<NonNullable<Res["result"]>>;
47
+ /**
48
+ * Processes a JSON response from a regular GET request.
49
+ * Handles error checking and returns the parsed JSON.
50
+ *
51
+ * @param response The fetch Response object
52
+ * @param endpoint Optional endpoint description for logging purposes
53
+ * @returns A promise resolving to the parsed JSON
54
+ * @throws RpcError if there's a response error
55
+ */
56
+ export declare function handleJsonResponse<T>(response: Response, endpoint?: string): Promise<T>;
57
+ /**
58
+ * Sends a JSON-RPC request and processes the response in a single operation.
59
+ * This combines sendJsonRpcRequest and handleJsonRpcResponse into one call.
60
+ *
61
+ * @param baseUrl The API endpoint URL
62
+ * @param method The JSON-RPC method name
63
+ * @param params The parameters for the method
64
+ * @param headers Custom headers to include in the request
65
+ * @param acceptHeader The desired Accept header ('application/json' or 'text/event-stream')
66
+ * @returns A Promise resolving to the result payload
67
+ * @throws RpcError if there's a network error or error in the response
68
+ */
69
+ export declare function executeJsonRpcRequest<Req extends A2ARequest, Res extends JSONRPCResponse>(baseUrl: URL, method: Req["method"], params: Req["params"], headers?: Record<string, string>, acceptHeader?: "application/json" | "text/event-stream"): Promise<NonNullable<Res["result"]>>;
70
+ /**
71
+ * Sends a GET request and processes the JSON response.
72
+ * Helper for non-RPC REST endpoints.
73
+ *
74
+ * @param url The endpoint URL
75
+ * @param headers Custom headers to include in the request
76
+ * @param endpoint Optional endpoint description for logging
77
+ * @returns A Promise resolving to the parsed JSON
78
+ * @throws RpcError if there's a network error or error in the response
79
+ */
80
+ export declare function executeGetRequest<T>(url: URL, headers?: Record<string, string>, endpoint?: string): Promise<T>;
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ /**
6
+ * HTTP JSON-RPC client utilities.
7
+ * Handles the common pattern of sending JSON-RPC requests and processing responses.
8
+ */
9
+ import { v4 as uuidv4 } from "uuid";
10
+ import { SystemError, INTERNAL_ERROR, PARSE_ERROR, } from "../../utils/common/errors.js";
11
+ import { parseResponse } from "./parser.js";
12
+ import { logError, logWarn } from "../../utils/logging/log.js";
13
+ /**
14
+ * Creates a JSON-RPC request body with the specified method and parameters.
15
+ *, ErrorCodeParseError
16
+ * @param method The JSON-RPC method name
17
+ * @param params The parameters for the method
18
+ * @param requestId Optional request ID (generates a UUID v4 if not provided)
19
+ * @returns A properly formatted JSON-RPC request object
20
+ */
21
+ export function createJsonRpcRequest(method, params, requestId = uuidv4()) {
22
+ return {
23
+ jsonrpc: "2.0",
24
+ id: requestId,
25
+ method,
26
+ params,
27
+ };
28
+ }
29
+ /**
30
+ * Sends a JSON-RPC request to the specified endpoint.
31
+ *
32
+ * @param baseUrl The API endpoint URL
33
+ * @param method The JSON-RPC method name
34
+ * @param params The parameters for the method
35
+ * @param headers Custom headers to include in the request
36
+ * @param acceptHeader The desired Accept header ('application/json' or 'text/event-stream')
37
+ * @returns A Promise resolving to the fetch Response object
38
+ * @throws RpcError if there's a network error
39
+ */
40
+ export async function sendJsonRpcRequest(baseUrl, method, params, headers = {}, acceptHeader = "application/json") {
41
+ const requestBody = createJsonRpcRequest(method, params);
42
+ try {
43
+ return await fetch(baseUrl, {
44
+ method: "POST",
45
+ headers: {
46
+ "Content-Type": "application/json",
47
+ "Access-Control-Allow-Origin": "*",
48
+ Accept: acceptHeader,
49
+ ...headers,
50
+ },
51
+ priority: "high",
52
+ mode: "cors",
53
+ keepalive: true,
54
+ body: JSON.stringify(requestBody),
55
+ });
56
+ }
57
+ catch (networkError) {
58
+ logError("SendJsonRpcRequest", "Network error during RPC call:", networkError);
59
+ // Wrap network errors into a standard error format
60
+ throw INTERNAL_ERROR(networkError);
61
+ }
62
+ }
63
+ /**
64
+ * Sends a GET request to the specified endpoint.
65
+ * This is used for non-JSON-RPC calls like agent card retrieval.
66
+ *
67
+ * @param url The endpoint URL
68
+ * @param headers Custom headers to include in the request
69
+ * @returns A Promise resolving to the fetch Response object
70
+ * @throws RpcError if there's a network error
71
+ */
72
+ export async function sendGetRequest(url, headers = {}) {
73
+ try {
74
+ return await fetch(url, {
75
+ method: "GET",
76
+ headers: {
77
+ Accept: "application/json",
78
+ ...headers,
79
+ },
80
+ });
81
+ }
82
+ catch (networkError) {
83
+ logError("SendGetRequest", "Network error during GET request:", networkError);
84
+ throw INTERNAL_ERROR(networkError);
85
+ }
86
+ }
87
+ /**
88
+ * Processes a standard JSON-RPC response (non-streaming).
89
+ * Parses the response, validates it, and returns the result payload.
90
+ *
91
+ * @param response The fetch Response object
92
+ * @param expectedMethod Optional method name for logging purposes
93
+ * @returns A promise resolving to the result payload
94
+ * @throws RpcError if there's an error in the response
95
+ */
96
+ export async function handleJsonRpcResponse(response, expectedMethod) {
97
+ let responseBody = null;
98
+ try {
99
+ responseBody = await response.text();
100
+ if (!response.ok) {
101
+ try {
102
+ // Try to parse error as JSON-RPC
103
+ parseResponse(responseBody);
104
+ // If we get here, it means there was no error in the response
105
+ // But the HTTP status was not OK, so we throw a generic error
106
+ }
107
+ catch (parseError) {
108
+ logWarn("handleJsonRpcResponse", "Error parsing JSON-RPC response:", parseError);
109
+ }
110
+ // Throw a generic HTTP error if we couldn't extract an RPC error
111
+ throw new Error(`HTTP error ${response.status}: ${response.statusText}${responseBody ? ` - ${responseBody}` : ""}`);
112
+ }
113
+ // Parse and validate the response
114
+ // If it has an error, parseResponse will throw
115
+ // If it doesn't have a result, parseResponse will also throw
116
+ const jsonResponse = parseResponse(responseBody);
117
+ // At this point, we know we have a valid result
118
+ // NonNullable is used in the return type to ensure TypeScript knows this
119
+ return jsonResponse.result;
120
+ }
121
+ catch (error) {
122
+ logError("handleJsonRpcResponse", `Error processing response [${expectedMethod}]:`, error);
123
+ // Re-throw RpcError instances directly, wrap others
124
+ if (error instanceof SystemError) {
125
+ throw error;
126
+ }
127
+ else {
128
+ throw INTERNAL_ERROR(error);
129
+ }
130
+ }
131
+ }
132
+ /**
133
+ * Processes a JSON response from a regular GET request.
134
+ * Handles error checking and returns the parsed JSON.
135
+ *
136
+ * @param response The fetch Response object
137
+ * @param endpoint Optional endpoint description for logging purposes
138
+ * @returns A promise resolving to the parsed JSON
139
+ * @throws RpcError if there's a response error
140
+ */
141
+ export async function handleJsonResponse(response, endpoint) {
142
+ let responseBody = null;
143
+ try {
144
+ responseBody = await response.text();
145
+ if (!response.ok) {
146
+ throw new Error(`HTTP error ${response.status}: ${response.statusText}${responseBody ? ` - ${responseBody}` : ""}`);
147
+ }
148
+ return JSON.parse(responseBody);
149
+ }
150
+ catch (error) {
151
+ logError("handleJsonResponse", `Error processing response for ${endpoint || "unknown endpoint"}:`, error);
152
+ if (error instanceof SystemError) {
153
+ throw error;
154
+ }
155
+ else {
156
+ throw PARSE_ERROR(error);
157
+ }
158
+ }
159
+ }
160
+ /**
161
+ * Sends a JSON-RPC request and processes the response in a single operation.
162
+ * This combines sendJsonRpcRequest and handleJsonRpcResponse into one call.
163
+ *
164
+ * @param baseUrl The API endpoint URL
165
+ * @param method The JSON-RPC method name
166
+ * @param params The parameters for the method
167
+ * @param headers Custom headers to include in the request
168
+ * @param acceptHeader The desired Accept header ('application/json' or 'text/event-stream')
169
+ * @returns A Promise resolving to the result payload
170
+ * @throws RpcError if there's a network error or error in the response
171
+ */
172
+ export async function executeJsonRpcRequest(baseUrl, method, params, headers = {}, acceptHeader = "application/json") {
173
+ const response = await sendJsonRpcRequest(baseUrl, method, params, headers, acceptHeader);
174
+ return handleJsonRpcResponse(response, method);
175
+ }
176
+ /**
177
+ * Sends a GET request and processes the JSON response.
178
+ * Helper for non-RPC REST endpoints.
179
+ *
180
+ * @param url The endpoint URL
181
+ * @param headers Custom headers to include in the request
182
+ * @param endpoint Optional endpoint description for logging
183
+ * @returns A Promise resolving to the parsed JSON
184
+ * @throws RpcError if there's a network error or error in the response
185
+ */
186
+ export async function executeGetRequest(url, headers = {}, endpoint) {
187
+ const response = await sendGetRequest(url, headers);
188
+ return handleJsonResponse(response, endpoint);
189
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import type { JSONRPCResponse, A2ARequest } from "../../types/index.js";
6
+ /**
7
+ * Creates an async generator for processing task events from an SSE stream
8
+ *
9
+ * @template T The type of task event to process (TaskStatusUpdateEvent or TaskArtifactUpdateEvent)
10
+ * @param response The fetch Response object containing the event stream
11
+ * @returns An async generator yielding the specified type of task events from StreamingResponse
12
+ */
13
+ export declare function handleEventStream<StreamRes extends JSONRPCResponse>(response: Response): AsyncGenerator<NonNullable<StreamRes["result"]>>;
14
+ /**
15
+ * Executes a streaming JSON-RPC request and yields events as they arrive
16
+ *
17
+ * @template Req The type of request being made
18
+ * @template StreamRes The type of streaming response expected
19
+ * @param baseUrl The base URL for the API
20
+ * @param method The JSON-RPC method to call
21
+ * @param params The parameters for the method
22
+ * @param customHeaders Any custom headers to include in the request
23
+ * @returns An AsyncIterable yielding events from the stream
24
+ */
25
+ export declare function executeStreamEvents<Req extends A2ARequest, StreamRes extends JSONRPCResponse>(baseUrl: URL, method: Req["method"], params: Req["params"], customHeaders: Record<string, string>): AsyncIterable<NonNullable<StreamRes["result"]>>;
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ /**
6
+ * Event stream utilities for handling Server-Sent Events (SSE).
7
+ */
8
+ import { parseResponse } from "../rpc/parser.js";
9
+ import { createParser, } from "eventsource-parser";
10
+ import { sendJsonRpcRequest } from "../rpc/rpc-client.js";
11
+ import { logError, logWarn, logDebug } from "../../utils/logging/index.js";
12
+ /**
13
+ * Creates an async generator for processing task events from an SSE stream
14
+ *
15
+ * @template T The type of task event to process (TaskStatusUpdateEvent or TaskArtifactUpdateEvent)
16
+ * @param response The fetch Response object containing the event stream
17
+ * @returns An async generator yielding the specified type of task events from StreamingResponse
18
+ */
19
+ export async function* handleEventStream(response) {
20
+ if (!response.ok || !response.body) {
21
+ let errorText = null;
22
+ try {
23
+ errorText = await response.text();
24
+ }
25
+ catch (_) { }
26
+ logError("handleEventStream", `HTTP error [${response.status}:${response.statusText}] - ${errorText}`, new Error(`HTTP error [${response.status}:${response.statusText}] - ${errorText}`));
27
+ throw new Error(`HTTP error [${response.status}:${response.statusText}] - ${errorText}`);
28
+ }
29
+ // Use eventsource-parser to process the SSE stream
30
+ const reader = response.body.getReader();
31
+ const decoder = new TextDecoder();
32
+ const events = [];
33
+ const parser = createParser({
34
+ onEvent: (event) => {
35
+ if (event.data) {
36
+ if (event.event === "close") {
37
+ logDebug("handleEventStream", "Stream closed");
38
+ return;
39
+ }
40
+ try {
41
+ const parsedData = parseResponse(event.data);
42
+ const eventResult = parsedData.result;
43
+ if (eventResult) {
44
+ events.push(eventResult);
45
+ }
46
+ else {
47
+ logWarn("handleEventStream", "Failed to parse SSE data", parsedData);
48
+ }
49
+ // if (parsedData.final && parsedData.final === true) {
50
+ // logDebug("handleEventStream", "Stream completed");
51
+ // return;
52
+ // }
53
+ }
54
+ catch (e) {
55
+ logWarn("handleEventStream", "Failed to parse SSE data", e);
56
+ }
57
+ }
58
+ },
59
+ onError: (error) => {
60
+ logError("handleEventStream", "Error parsing SSE data", error);
61
+ },
62
+ onRetry: (retry) => {
63
+ logWarn("handleEventStream", "Retrying SSE connection", retry);
64
+ },
65
+ });
66
+ try {
67
+ while (true) {
68
+ const { done, value } = await reader.read();
69
+ if (done)
70
+ break;
71
+ const chunk = decoder.decode(value, { stream: true });
72
+ parser.feed(chunk);
73
+ while (events.length > 0) {
74
+ yield events.shift();
75
+ }
76
+ }
77
+ }
78
+ finally {
79
+ reader.releaseLock();
80
+ }
81
+ }
82
+ /**
83
+ * Executes a streaming JSON-RPC request and yields events as they arrive
84
+ *
85
+ * @template Req The type of request being made
86
+ * @template StreamRes The type of streaming response expected
87
+ * @param baseUrl The base URL for the API
88
+ * @param method The JSON-RPC method to call
89
+ * @param params The parameters for the method
90
+ * @param customHeaders Any custom headers to include in the request
91
+ * @returns An AsyncIterable yielding events from the stream
92
+ */
93
+ export async function* executeStreamEvents(baseUrl, method, params, customHeaders) {
94
+ logDebug("executeStreamEvents", `Sending streaming request to: ${baseUrl.toString()}, method: ${method}`);
95
+ const responsePromise = sendJsonRpcRequest(baseUrl, method, params, customHeaders, "text/event-stream");
96
+ const response = await responsePromise;
97
+ logDebug("executeStreamEvents", "Response", response);
98
+ yield* handleEventStream(response);
99
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ /**
6
+ * For artinet specific conventions
7
+ * AgentInfo will expand to incorporate additional details required from other protocols
8
+ * (e.g. AgentFAQ(NANDA), AgentCard(A2A), etc.)
9
+ */
10
+ export { AgentCardSchema as AgentInfoSchema } from "./schemas/a2a/index.js";
11
+ export type { AgentCard as AgentInfo } from "./schemas/a2a/index.js";
12
+ export type { A2AEngine as AgentEngine } from "./interfaces/services/a2a/index.js";
13
+ export type { A2AServiceInterface as Agent } from "./interfaces/services/a2a/index.js";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ /**
6
+ * For artinet specific conventions
7
+ * AgentInfo will expand to incorporate additional details required from other protocols
8
+ * (e.g. AgentFAQ(NANDA), AgentCard(A2A), etc.)
9
+ */
10
+ export { AgentCardSchema as AgentInfoSchema } from "./schemas/a2a/index.js";
@@ -0,0 +1,4 @@
1
+ export * from "./ext.js";
2
+ export * from "./schemas/index.js";
3
+ export * from "./interfaces/index.js";
4
+ export * from "./utils/index.js";
@@ -0,0 +1,4 @@
1
+ export * from "./ext.js";
2
+ export * from "./schemas/index.js";
3
+ export * from "./interfaces/index.js";
4
+ export * from "./utils/index.js";
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { TaskPushNotificationConfig, TaskIdParams, Task } from "../schemas/a2a/index.js";
6
+ import type { AgentCard, TaskQueryParams, MessageSendParams, Message } from "../schemas/a2a/index.js";
7
+ import { UpdateEvent } from "./services/a2a/index.js";
8
+ /**
9
+ * @interface Client
10
+ * @description Defines the standard contract for an A2A (Agent-to-Agent) client implementation.
11
+ * This interface allows consumers to create custom client solutions while ensuring
12
+ * compatibility with the A2A protocol. It outlines the core functionalities
13
+ * required for interacting with an A2A agent server, including task management,
14
+ * capability discovery, and communication configuration.
15
+ */
16
+ export interface Client {
17
+ /**
18
+ * @description Fetches the agent's metadata (AgentCard) from a well-known path or a fallback path.
19
+ * The AgentCard contains essential information about the agent, such as its capabilities and endpoints.
20
+ * @param {string} wellKnownPath The standard path (e.g., "/.well-known/a2a") to look for the AgentCard.
21
+ * @param {string} fallbackPath An alternative path to check if the well-known path fails.
22
+ * @returns {Promise<AgentCard>} A promise that resolves with the AgentCard object.
23
+ * @async
24
+ */
25
+ agentCard(): Promise<AgentCard>;
26
+ /**
27
+ * @description Refreshes the cached AgentCard information by re-fetching it from the server.
28
+ * Useful when the agent's capabilities or configuration might have changed.
29
+ * @returns {Promise<AgentCard>} A promise that resolves with the updated AgentCard object.
30
+ * @async
31
+ */
32
+ refreshAgentCard(): Promise<AgentCard>;
33
+ /**
34
+ * @description Sends a task request to the agent server.
35
+ * @param {MessageSendParams} params Parameters required to send the task, including the task definition and metadata.
36
+ * @returns {Promise<Task | null>} A promise that resolves with the initial Task object representing the submitted task, or null if the submission failed.
37
+ * @async
38
+ */
39
+ sendMessage(params: MessageSendParams): Promise<Message | Task | null>;
40
+ /**
41
+ * @deprecated Will be removed in v6.Use sendMessage instead.
42
+ * @description Sends a task request to the agent server.
43
+ * @param {MessageSendParams} params Parameters required to send the task, including the task definition and metadata.
44
+ * @returns {Promise<Task | null>} A promise that resolves with the initial Task object representing the submitted task, or null if the submission failed.
45
+ * @async
46
+ */
47
+ sendTask(params: MessageSendParams): Promise<Message | Task | null>;
48
+ /**
49
+ * @description Sends a task request and subscribes to real-time updates (status changes, artifact updates) for that task.
50
+ * This uses a streaming connection if available.
51
+ * @param {MessageSendParams} params Parameters required to send the task.
52
+ * @returns {AsyncIterable<UpdateEvent>} An async iterable that yields task status and artifact updates.
53
+ * @async
54
+ */
55
+ sendStreamingMessage(params: MessageSendParams): AsyncIterable<UpdateEvent>;
56
+ /**
57
+ * @deprecated Will be removed in v6Use sendStreamingMessage instead.
58
+ * @description Sends a task request and subscribes to real-time updates (status changes, artifact updates) for that task.
59
+ * This uses a streaming connection if available.
60
+ * @param {MessageSendParams} params Parameters required to send the task.
61
+ * @returns {AsyncIterable<UpdateEvent>} An async iterable that yields task status and artifact updates.
62
+ * @async
63
+ */
64
+ sendTaskSubscribe(params: MessageSendParams): AsyncIterable<UpdateEvent>;
65
+ /**
66
+ * @description Retrieves the current state of a specific task by its ID.
67
+ * @param {TaskQueryParams} params Parameters containing the task ID to query.
68
+ * @returns {Promise<Task | null>} A promise that resolves with the Task object, or null if the task is not found.
69
+ * @async
70
+ */
71
+ getTask(params: TaskQueryParams): Promise<Task | null>;
72
+ /**
73
+ * @description Sends a request to cancel an ongoing task.
74
+ * @param {TaskIdParams} params Parameters containing the ID of the task to cancel.
75
+ * @returns {Promise<Task | null>} A promise that resolves with the final state of the cancelled Task object, or null if cancellation failed or the task wasn't found.
76
+ * @async
77
+ */
78
+ cancelTask(params: TaskIdParams): Promise<Task | null>;
79
+ /**
80
+ * @description Configures push notifications for a specific task. This allows the client
81
+ * to receive updates asynchronously via a specified webhook URL.
82
+ * Requires the agent to support the 'pushNotifications' capability.
83
+ * @param {TaskPushNotificationConfig} params The configuration details for the push notification, including the task ID and webhook URL.
84
+ * @returns {Promise<TaskPushNotificationConfig | null>} A promise that resolves with the applied configuration, or null if the configuration failed.
85
+ * @async
86
+ */
87
+ setTaskPushNotification(params: TaskPushNotificationConfig): Promise<TaskPushNotificationConfig | null>;
88
+ /**
89
+ * @description Retrieves the current push notification configuration for a specific task.
90
+ * @param {TaskIdParams} params Parameters containing the task ID.
91
+ * @returns {Promise<TaskPushNotificationConfig | null>} A promise that resolves with the push notification configuration, or null if not configured or not found.
92
+ * @async
93
+ */
94
+ getTaskPushNotification(params: TaskIdParams): Promise<TaskPushNotificationConfig | null>;
95
+ /**
96
+ * @description Resubscribes to updates for an existing task, potentially after a connection drop or client restart.
97
+ * This allows resuming the stream of status and artifact updates.
98
+ * @param {TaskQueryParams} params Parameters containing the task ID to resubscribe to.
99
+ * @returns {AsyncIterable<UpdateEvent>} An async iterable that yields task status and artifact updates.
100
+ * @async
101
+ */
102
+ resubscribeTask(params: TaskQueryParams): AsyncIterable<UpdateEvent>;
103
+ /**
104
+ * @description Checks if the agent server supports a specific capability.
105
+ * @param {"streaming" | "pushNotifications" | "stateTransitionHistory"} capability The capability to check for.
106
+ * @returns {Promise<boolean>} A promise that resolves to true if the capability is supported, false otherwise.
107
+ * @async
108
+ */
109
+ supports(capability: "streaming" | "pushNotifications" | "stateTransitionHistory"): Promise<boolean>;
110
+ /**
111
+ * @description Sets multiple HTTP headers to be included in all subsequent requests made by the client instance.
112
+ * Replaces any previously set headers.
113
+ * @param {Record<string, string>} headers An object containing header names and values.
114
+ * @returns {void}
115
+ */
116
+ setHeaders(headers: Record<string, string>): void;
117
+ /**
118
+ * @description Adds or updates a single HTTP header for subsequent requests.
119
+ * @param {string} name The name of the header.
120
+ * @param {string} value The value of the header.
121
+ * @returns {void}
122
+ */
123
+ addHeader(name: string, value: string): void;
124
+ /**
125
+ * @description Removes a specific HTTP header from subsequent requests.
126
+ * @param {string} name The name of the header to remove.
127
+ * @returns {void}
128
+ */
129
+ removeHeader(name: string): void;
130
+ /**
131
+ * @description Clears all custom HTTP headers previously set on the client instance.
132
+ * @returns {void}
133
+ */
134
+ clearHeaders(): void;
135
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Copyright 2025 The Artinet Project
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from "./services/index.js";
2
+ export * from "./storage.js";
3
+ export * from "./client.js";
@@ -0,0 +1,3 @@
1
+ export * from "./services/index.js";
2
+ export * from "./storage.js";
3
+ export * from "./client.js";