@a2alite/sdk
Version:
A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management
77 lines (76 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHonoApp = createHonoApp;
const hono_1 = require("hono");
const jsonRpcBodyParser_ts_1 = require("../../jsonRPC/jsonRpcBodyParser.js");
const types_ts_1 = require("../../../types/types.js");
const errors_ts_1 = require("../../../utils/errors.js");
const streaming_1 = require("hono/streaming");
/**
* Creates a Hono HTTP application configured for A2A protocol communication
*
* This function sets up the necessary HTTP endpoints for the A2A protocol:
* - POST /a2a - Main JSON-RPC endpoint for agent communication
* - GET /.well-known/agent.json - Agent discovery endpoint
*
* The application handles both synchronous and streaming responses using Server-Sent Events.
*
* @param params - Configuration object
* @param params.a2aServer - The A2A server instance to handle requests
* @returns Promise resolving to a configured Hono application instance
*/
async function createHonoApp({ a2aServer }) {
const app = new hono_1.Hono();
// Start the a2a server
await a2aServer.start();
// JSON-RPC endpoint
app.post("/a2a", async (c) => {
try {
const bodyText = await c.req.text();
const parsed = await (0, jsonRpcBodyParser_ts_1.jsonRpcBodyParser)(bodyText);
// If parser returns a JSONRPCError (has code/message), send JSON-RPC error response
if ((0, types_ts_1.isJSONRPCError)(parsed)) {
return c.json({
jsonrpc: "2.0",
id: null,
error: parsed,
});
}
// Handle request
const result = await a2aServer.handleRequest(parsed);
if (result.response) {
return c.json(result.response);
}
else if (result.stream) {
const stream = result.stream;
// Use Hono's StreamSSE helper for SSE
return (0, streaming_1.streamSSE)(c, async (sse) => {
for await (const jsonRpcResponse of stream) {
await sse.writeSSE({ data: JSON.stringify(jsonRpcResponse) });
}
});
}
else {
// Unexpected result
return c.json({
jsonrpc: "2.0",
id: parsed?.id || null,
error: (0, errors_ts_1.internalError)(),
});
}
}
catch (err) {
console.error(err);
return c.json({
jsonrpc: "2.0",
id: null,
error: (0, errors_ts_1.internalError)(),
});
}
});
// Agent card handler
app.get("/.well-known/agent.json", async (c) => {
return c.json(a2aServer.agentCard);
});
return app;
}