UNPKG

@kddd/artinet-sdk

Version:

TypeScript SDK for the Agent2Agent (A2A) Protocol

64 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultCreateJSONRPCServer = void 0; exports.createJSONRPCMethod = createJSONRPCMethod; const log_js_1 = require("../../../utils/logging/log.js"); const errors_js_1 = require("../../../utils/common/errors.js"); const params_js_1 = require("../../interfaces/params.js"); const a2a_methods_js_1 = require("./a2a-methods.js"); /** * Creates a JSON-RPC method handler from a function * @param deps Dependencies for the method * @param funct The function to be used as the method handler * @param methodName The name of the method * @returns A JSON-RPC method handler */ function createJSONRPCMethod(deps, funct, methodName // Pass method name for logging ) { return async (requestParams, callback) => { try { if (!requestParams) { throw (0, errors_js_1.INVALID_PARAMS)("Missing request parameters"); } // Execute the specific logic await funct(deps, requestParams, callback); } catch (error) { // Centralized error handling ONLY for exceptions thrown by logic (0, log_js_1.logError)("createJSONRPCMethod", `Exception executing method`, error); if (error instanceof errors_js_1.SystemError) { return callback({ message: error.message, code: error.code, data: error.data, }); } else { (0, log_js_1.logError)(methodName, `Failed to process Inner Error`, error); const internalError = (0, errors_js_1.INTERNAL_ERROR)(error); return callback({ message: internalError.message, code: internalError.code, data: internalError.data, }); } } }; } const defaultCreateJSONRPCServer = (params) => { // Create handlers using the generic factory and specific logic const taskSendMethod = createJSONRPCMethod(params, a2a_methods_js_1.defaultSendTaskMethod, "tasks/send"); const taskGetMethod = createJSONRPCMethod(params, a2a_methods_js_1.defaultGetTaskMethod, "tasks/get"); const taskCancelMethod = createJSONRPCMethod(params, a2a_methods_js_1.defaultCancelTaskMethod, "tasks/cancel"); const taskPushNotificationSetMethod = createJSONRPCMethod(params, a2a_methods_js_1.defaultSetTaskPushNotificationMethod, "tasks/pushNotification/set"); const taskPushNotificationGetMethod = createJSONRPCMethod(params, a2a_methods_js_1.defaultGetTaskPushNotificationMethod, "tasks/pushNotification/get"); return new params_js_1.JSONRPCServer({ "tasks/send": taskSendMethod, "tasks/get": taskGetMethod, "tasks/cancel": taskCancelMethod, "tasks/pushNotification/set": taskPushNotificationSetMethod, "tasks/pushNotification/get": taskPushNotificationGetMethod, }, { reviver: undefined, replacer: undefined }); }; exports.defaultCreateJSONRPCServer = defaultCreateJSONRPCServer; //# sourceMappingURL=factory.js.map