UNPKG

@dexwox-labs/a2a-client

Version:

TypeScript client implementation for Google's Agent-to-Agent (A2A) protocol - includes HTTP/WebSocket communication, circuit breakers and error handling

93 lines 3.35 kB
"use strict"; /** * @module HttpUtils * @description Utility functions for HTTP communication with A2A protocol servers */ Object.defineProperty(exports, "__esModule", { value: true }); exports.sendRequest = sendRequest; /** * Sends a JSON-RPC request to an A2A protocol server * * This function handles the details of making authenticated HTTP requests * to A2A protocol servers, including setting up the proper headers, * handling authentication, and processing the response. * * @param options - Client options including base URL, headers, timeout, and authentication * @param request - JSON-RPC request object to send * @returns Promise resolving to the JSON-RPC response * @throws Error if the HTTP request fails or times out * * @example * ```typescript * // Send a simple JSON-RPC request * const response = await sendRequest<AgentCard[]>( * { * baseUrl: 'https://a2a-server.example.com', * timeout: 5000, * auth: { * type: 'bearer', * credentials: { token: 'your-token' } * } * }, * { * jsonrpc: '2.0', * method: 'discover', * params: { capability: 'chat' }, * id: '1' * } * ); * * console.log('Response:', response.result); * ``` */ async function sendRequest(options, request) { // Set up default headers and add any custom headers const headers = { 'Content-Type': 'application/json', ...options.headers }; // Handle authentication based on the specified type if (options.auth) { switch (options.auth.type) { case 'basic': // Basic authentication (username:password encoded in base64) if (options.auth.credentials.username && options.auth.credentials.password) { const encoded = Buffer.from(`${options.auth.credentials.username}:${options.auth.credentials.password}`).toString('base64'); headers['Authorization'] = `Basic ${encoded}`; } break; case 'bearer': // Bearer token authentication (typically JWT) if (options.auth.credentials.token) { headers['Authorization'] = `Bearer ${options.auth.credentials.token}`; } break; case 'apiKey': // API key authentication if (options.auth.credentials.apiKey) { headers['X-API-Key'] = options.auth.credentials.apiKey; } break; case 'custom': // Custom header-based authentication if (options.auth.credentials.headerName && options.auth.credentials.headerValue) { headers[options.auth.credentials.headerName] = options.auth.credentials.headerValue; } break; } } // Send the request with appropriate timeout const response = await fetch(options.baseUrl, { method: 'POST', headers, body: JSON.stringify(request), signal: AbortSignal.timeout(options.timeout) }); // Handle non-2xx responses if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } // Parse and return the JSON-RPC response return response.json(); } //# sourceMappingURL=http-utils.js.map