@artinet/sdk 0.5.16 → 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.
- package/README.md +108 -58
- package/dist/browser/browser.d.ts +9 -0
- package/dist/browser/browser.js +10 -0
- package/dist/browser/client/a2a-client.d.ts +126 -0
- package/dist/browser/client/a2a-client.js +221 -0
- package/dist/browser/client/index.d.ts +1 -0
- package/dist/browser/client/index.js +1 -0
- package/dist/browser/services/a2a/helpers/message-builder.d.ts +12 -0
- package/dist/browser/services/a2a/helpers/message-builder.js +61 -0
- package/dist/browser/transport/rpc/parser.d.ts +15 -0
- package/dist/browser/transport/rpc/parser.js +48 -0
- package/dist/browser/transport/rpc/rpc-client.d.ts +80 -0
- package/dist/browser/transport/rpc/rpc-client.js +189 -0
- package/dist/browser/transport/streaming/event-stream.d.ts +25 -0
- package/dist/browser/transport/streaming/event-stream.js +99 -0
- package/dist/browser/types/ext.d.ts +13 -0
- package/dist/browser/types/ext.js +10 -0
- package/dist/browser/types/index.d.ts +4 -0
- package/dist/browser/types/index.js +4 -0
- package/dist/browser/types/interfaces/client.d.ts +135 -0
- package/dist/browser/types/interfaces/client.js +5 -0
- package/dist/browser/types/interfaces/index.d.ts +3 -0
- package/dist/browser/types/interfaces/index.js +3 -0
- package/dist/browser/types/interfaces/services/a2a/builder.d.ts +37 -0
- package/dist/browser/types/interfaces/services/a2a/builder.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/context.d.ts +162 -0
- package/dist/browser/types/interfaces/services/a2a/context.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/engine.d.ts +7 -0
- package/dist/browser/types/interfaces/services/a2a/engine.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/index.d.ts +5 -0
- package/dist/browser/types/interfaces/services/a2a/index.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/legacy.d.ts +93 -0
- package/dist/browser/types/interfaces/services/a2a/legacy.js +5 -0
- package/dist/browser/types/interfaces/services/a2a/service.d.ts +413 -0
- package/dist/browser/types/interfaces/services/a2a/service.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/command.d.ts +25 -0
- package/dist/browser/types/interfaces/services/core/context/command.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/context.d.ts +207 -0
- package/dist/browser/types/interfaces/services/core/context/context.js +5 -0
- package/dist/browser/types/interfaces/services/core/context/index.d.ts +3 -0
- package/dist/browser/types/interfaces/services/core/context/index.js +3 -0
- package/dist/browser/types/interfaces/services/core/context/types.d.ts +11 -0
- package/dist/browser/types/interfaces/services/core/context/types.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/engine.d.ts +106 -0
- package/dist/browser/types/interfaces/services/core/execution/engine.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/environment.d.ts +11 -0
- package/dist/browser/types/interfaces/services/core/execution/environment.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/execute.d.ts +7 -0
- package/dist/browser/types/interfaces/services/core/execution/execute.js +5 -0
- package/dist/browser/types/interfaces/services/core/execution/index.d.ts +3 -0
- package/dist/browser/types/interfaces/services/core/execution/index.js +3 -0
- package/dist/browser/types/interfaces/services/core/index.d.ts +4 -0
- package/dist/browser/types/interfaces/services/core/index.js +4 -0
- package/dist/browser/types/interfaces/services/core/managers/cancellation.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/cancellation.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/connection.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/connection.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/context.d.ts +17 -0
- package/dist/browser/types/interfaces/services/core/managers/context.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/event.d.ts +328 -0
- package/dist/browser/types/interfaces/services/core/managers/event.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/index.d.ts +6 -0
- package/dist/browser/types/interfaces/services/core/managers/index.js +6 -0
- package/dist/browser/types/interfaces/services/core/managers/stream.d.ts +217 -0
- package/dist/browser/types/interfaces/services/core/managers/stream.js +5 -0
- package/dist/browser/types/interfaces/services/core/managers/task.d.ts +9 -0
- package/dist/browser/types/interfaces/services/core/managers/task.js +1 -0
- package/dist/browser/types/interfaces/services/core/service.d.ts +115 -0
- package/dist/browser/types/interfaces/services/core/service.js +5 -0
- package/dist/browser/types/interfaces/services/index.d.ts +4 -0
- package/dist/browser/types/interfaces/services/index.js +4 -0
- package/dist/browser/types/interfaces/services/mcp/index.d.ts +1 -0
- package/dist/browser/types/interfaces/services/mcp/index.js +1 -0
- package/dist/browser/types/interfaces/services/mcp/service.d.ts +49 -0
- package/dist/browser/types/interfaces/services/mcp/service.js +5 -0
- package/dist/browser/types/interfaces/services/protocol.d.ts +33 -0
- package/dist/browser/types/interfaces/services/protocol.js +34 -0
- package/dist/browser/types/interfaces/storage.d.ts +8 -0
- package/dist/browser/types/interfaces/storage.js +5 -0
- package/dist/browser/types/schemas/a2a/agent.d.ts +2583 -0
- package/dist/browser/types/schemas/a2a/agent.js +323 -0
- package/dist/browser/types/schemas/a2a/auth.d.ts +908 -0
- package/dist/browser/types/schemas/a2a/auth.js +283 -0
- package/dist/browser/types/schemas/a2a/error.d.ts +396 -0
- package/dist/browser/types/schemas/a2a/error.js +163 -0
- package/dist/browser/types/schemas/a2a/index.d.ts +11 -0
- package/dist/browser/types/schemas/a2a/index.js +11 -0
- package/dist/browser/types/schemas/a2a/kind.d.ts +11 -0
- package/dist/browser/types/schemas/a2a/kind.js +20 -0
- package/dist/browser/types/schemas/a2a/message.d.ts +10343 -0
- package/dist/browser/types/schemas/a2a/message.js +130 -0
- package/dist/browser/types/schemas/a2a/notification.d.ts +1517 -0
- package/dist/browser/types/schemas/a2a/notification.js +203 -0
- package/dist/browser/types/schemas/a2a/parameters.d.ts +956 -0
- package/dist/browser/types/schemas/a2a/parameters.js +241 -0
- package/dist/browser/types/schemas/a2a/protocol.d.ts +14363 -0
- package/dist/browser/types/schemas/a2a/protocol.js +59 -0
- package/dist/browser/types/schemas/a2a/rpc.d.ts +182 -0
- package/dist/browser/types/schemas/a2a/rpc.js +126 -0
- package/dist/browser/types/schemas/a2a/task.d.ts +5886 -0
- package/dist/browser/types/schemas/a2a/task.js +134 -0
- package/dist/browser/types/schemas/a2a/transport.d.ts +31 -0
- package/dist/browser/types/schemas/a2a/transport.js +28 -0
- package/dist/browser/types/schemas/index.d.ts +1 -0
- package/dist/browser/types/schemas/index.js +1 -0
- package/dist/browser/types/utils/index.d.ts +1 -0
- package/dist/browser/types/utils/index.js +1 -0
- package/dist/browser/types/utils/transform.d.ts +64 -0
- package/dist/browser/types/utils/transform.js +35 -0
- package/dist/browser/utils/common/constants.d.ts +11 -0
- package/dist/browser/utils/common/constants.js +38 -0
- package/dist/browser/utils/common/errors.d.ts +24 -0
- package/dist/browser/utils/common/errors.js +42 -0
- package/dist/browser/utils/common/utils.d.ts +9 -0
- package/dist/browser/utils/common/utils.js +11 -0
- package/dist/browser/utils/logging/index.d.ts +2 -0
- package/dist/browser/utils/logging/index.js +2 -0
- package/dist/browser/utils/logging/log.d.ts +33 -0
- package/dist/browser/utils/logging/log.js +75 -0
- package/dist/browser/utils/logging/logger.d.ts +18 -0
- package/dist/browser/utils/logging/logger.js +18 -0
- package/dist/client/a2a-client.d.ts +2 -1
- package/dist/client/a2a-client.js +13 -4
- package/dist/server/express/errors.js +1 -1
- package/dist/server/express/middeware.d.ts +2 -2
- package/dist/server/express/middeware.js +26 -6
- package/dist/server/express/server.d.ts +26 -25
- package/dist/server/express/server.js +32 -6
- package/dist/services/a2a/factory/builder.d.ts +24 -24
- package/dist/services/a2a/factory/builder.js +6 -1
- package/dist/services/a2a/factory/service.js +2 -1
- package/dist/services/a2a/helpers/agentcard-builder.d.ts +7 -0
- package/dist/services/a2a/helpers/agentcard-builder.js +23 -0
- package/dist/services/a2a/helpers/history.d.ts +2 -0
- package/dist/services/a2a/helpers/history.js +3 -0
- package/dist/services/a2a/helpers/index.d.ts +3 -0
- package/dist/services/a2a/helpers/index.js +3 -0
- package/dist/services/a2a/methods/get-task.d.ts +46 -46
- package/dist/services/a2a/methods/get-task.js +2 -0
- package/dist/services/a2a/methods/send-message.js +18 -1
- package/dist/services/a2a/service.d.ts +154 -153
- package/dist/services/a2a/service.js +20 -6
- package/dist/services/mcp/service.js +0 -1
- package/dist/transport/rpc/parser.js +2 -2
- package/dist/transport/rpc/rpc-client.js +2 -2
- package/dist/transport/trpc/a2a/factory/router.d.ts +1084 -1084
- package/dist/transport/trpc/a2a/routes/info.d.ts +36 -36
- package/dist/transport/trpc/a2a/routes/message/route.d.ts +225 -225
- package/dist/transport/trpc/a2a/routes/tasks/route.d.ts +257 -257
- package/dist/transport/trpc/a2a/trpc.d.ts +120 -120
- package/dist/types/interfaces/services/a2a/service.d.ts +6 -1
- package/dist/types/schemas/a2a/agent.d.ts +818 -818
- package/dist/types/schemas/a2a/agent.js +1 -23
- package/dist/types/schemas/a2a/auth.d.ts +197 -197
- package/dist/types/schemas/a2a/auth.js +4 -19
- package/dist/types/schemas/a2a/error.d.ts +24 -24
- package/dist/types/schemas/a2a/message.d.ts +4499 -4499
- package/dist/types/schemas/a2a/message.js +2 -10
- package/dist/types/schemas/a2a/notification.d.ts +403 -403
- package/dist/types/schemas/a2a/notification.js +3 -7
- package/dist/types/schemas/a2a/parameters.d.ts +264 -264
- package/dist/types/schemas/a2a/parameters.js +1 -14
- package/dist/types/schemas/a2a/protocol.d.ts +5988 -5988
- package/dist/types/schemas/a2a/rpc.d.ts +20 -20
- package/dist/types/schemas/a2a/rpc.js +0 -5
- package/dist/types/schemas/a2a/task.d.ts +2513 -2513
- package/dist/types/schemas/a2a/task.js +10 -11
- package/dist/utils/common/constants.js +1 -1
- package/dist/utils/common/errors.d.ts +2 -1
- package/dist/utils/common/errors.js +2 -1
- package/dist/utils/common/schema-validation.d.ts +2 -0
- package/dist/utils/common/schema-validation.js +12 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +20 -17
|
@@ -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;
|
|
@@ -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";
|