UNPKG

@a2alite/sdk

Version:

A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management

47 lines (46 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JSONRPCServer = void 0; const types_ts_1 = require("../../types/types.js"); class JSONRPCServer { constructor() { this._handlers = new Map(); } setRequestHandler(methodName, handler) { // as any is fine since the handler is called with the correct type this._handlers.set(methodName, handler); } async handleRequest(request, requestAbortSignal, extension) { try { const handler = this._handlers.get(request.method); // If no handler is found for the requested method, return a MethodNotFoundError if (!handler) { return { response: { jsonrpc: "2.0", id: request.id, error: { code: types_ts_1.ErrorType.MethodNotFoundError, message: "Method not found", }, }, }; } // Run the handler and return the result return handler(request, requestAbortSignal, extension); } catch (error) { return { response: { jsonrpc: "2.0", id: request.id, error: { code: types_ts_1.ErrorType.InternalError, message: "Internal error", }, }, }; } } } exports.JSONRPCServer = JSONRPCServer;