@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
109 lines • 4.42 kB
TypeScript
/**
* @module MessageClient
* @description Client for sending and receiving messages between A2A agents
*/
import { EventEmitter } from 'events';
import { MessagePart } from '@dexwox-labs/a2a-core';
import { MessageClientOptions, StreamOptions } from './types';
import { TaskClient } from './task-client';
import { AgentClient } from './agent-client';
/**
* Client for sending and receiving messages between A2A agents
*
* The MessageClient provides methods for sending messages to agents and
* streaming real-time communication. It also provides access to related
* TaskClient and AgentClient instances for convenience.
*
* @example
* ```typescript
* const messageClient = new MessageClient({ baseUrl: 'https://a2a-server.example.com' });
*
* // Send a simple text message to an agent
* const messageId = await messageClient.sendMessage([
* { type: 'text', content: 'What is the weather in New York?' }
* ], 'weather-agent');
* ```
*/
export declare class MessageClient extends EventEmitter {
/** Configuration options for the client */
private readonly options;
/** Task client for managing tasks */
readonly tasks: TaskClient;
/** Agent client for discovering and interacting with agents */
readonly agents: AgentClient;
/**
* Creates a new MessageClient instance
* @param options - Configuration options for the client
*/
constructor(options: MessageClientOptions);
/**
* Sends a message synchronously to an agent
*
* This method sends a message to a specified agent and waits for the server to
* acknowledge receipt. It validates the message parts before sending and handles
* network errors appropriately.
*
* @param parts - Array of message parts to send (text, file, data, etc.)
* @param agentId - ID of the target agent to receive the message
* @returns Promise resolving to the message ID assigned by the server
* @throws {A2ANetworkError} If there's a network issue contacting the server
* @throws {A2AValidationError} If the message parts are invalid
*
* @example
* ```typescript
* // Send a text message
* const textMessageId = await messageClient.sendMessage([
* { type: 'text', content: 'Hello, agent!' }
* ], 'assistant-agent');
*
* // Send a message with multiple parts
* const multipartMessageId = await messageClient.sendMessage([
* { type: 'text', content: 'Here is the data you requested' },
* {
* type: 'data',
* content: { temperature: 72, humidity: 65 },
* schema: 'weather-data'
* }
* ], 'weather-agent');
* ```
*/
sendMessage(parts: MessagePart[], agentId: string): Promise<string>;
/**
* Streams messages to and from an agent
*
* This method establishes a real-time streaming connection with an agent,
* allowing for continuous message exchange with automatic error handling.
*
* @param parts - Initial message parts to send to the agent
* @param agentId - ID of the target agent to stream with
* @param options - Configuration options and event handlers for the stream
* @param options.onMessage - Callback function for received messages
* @param options.onError - Optional callback function for stream errors
* @param options.onComplete - Optional callback function when stream completes
* @returns Promise that resolves when the stream completes
* @throws {A2ANetworkError} If there's a network issue establishing the stream
* @throws {A2AValidationError} If the message parts are invalid
*
* @example
* ```typescript
* await messageClient.streamMessage(
* [{ type: 'text', content: 'Tell me a story about dragons' }],
* 'storyteller-agent',
* {
* onMessage: (data) => console.log('Received:', data),
* onError: (error) => console.error('Stream error:', error),
* onComplete: () => console.log('Stream completed')
* }
* );
* ```
*/
streamMessage(parts: MessagePart[], agentId: string, options: StreamOptions & {
maxRetries?: number;
retryDelay?: number;
backoffFactor?: number;
maxRetryDelay?: number;
heartbeatInterval?: number;
heartbeatTimeout?: number;
}): Promise<void>;
}
//# sourceMappingURL=message-client.d.ts.map