@a2alite/sdk
Version:
A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management
44 lines (43 loc) • 1.46 kB
JavaScript
import { ErrorType, } from "../../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: 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: ErrorType.InternalError,
message: "Internal error",
},
},
};
}
}
}
export { JSONRPCServer };