import EventEmitter from 'eventemitter3'; import { type AnycastAgentOptions, type AnycastAgentEvents, ConnectionState } from './types.js'; /** Shared memory client interface */ export interface AgentMemoryClient { get(key: string, options?: { scope?: 'agent' | 'tenant'; }): Promise; set(key: string, value: unknown, options?: { scope?: 'agent' | 'tenant'; ttl?: number; }): Promise; delete(key: string, options?: { scope?: 'agent' | 'tenant'; }): Promise; list(prefix?: string, options?: { scope?: 'agent' | 'tenant'; limit?: number; }): Promise>; } /** Session context client interface */ export interface AgentContextClient { save(data: Record): Promise; restore(): Promise | null>; clear(): Promise; } /** * AnycastAgent — connects to the Anycast Agents rendezvous server * for P2P connectivity, signaling, and relay fallback. * * @example * ```typescript * const agent = new AnycastAgent({ * rendezvousUrl: 'wss://agents.anycast.com/rendezvous', * token: 'agt_...', * }); * * agent.on('connect', ({ agentId }) => { * console.log(`Connected as ${agentId}`); * }); * * agent.on('peer', (peerId, status) => { * console.log(`Peer ${peerId} is ${status}`); * }); * * await agent.connect(); * ``` */ export declare class AnycastAgent extends EventEmitter { private ws; private state; private heartbeatTimer; private reconnectTimer; private reconnectAttempt; private intentionalClose; /** Agent ID assigned by the server after registration */ agentId: string | null; /** Tenant ID from the server */ tenantId: string | null; /** Agent name from the server */ agentName: string | null; /** PoP IATA code the agent is connected through */ popIata: string | null; /** WAN IP (NAT gateway / public IP as seen by portal) */ wanIp: string | null; /** Internal IP (agent's own network interface) */ internalIp: string | null; /** Shared memory client for persistent key-value storage */ readonly memory: AgentMemoryClient; /** Session context client for save/restore across restarts */ readonly context: AgentContextClient; private _contextData; private _persistContext; /** WireGuard Curve25519 keypair for tunnel negotiation */ private wgKeyPair; private readonly opts; constructor(options: AnycastAgentOptions); /** * Set in-memory context data that will be auto-saved on disconnect * (only effective when persistContext: true). */ setContext(data: Record): void; /** * Get the WireGuard public key for this agent (null if WireGuard is disabled). */ getWireguardPublicKey(): string | null; /** * Request a WireGuard tunnel to a target agent. * Creates a tunnel record via the portal API and returns tunnel metadata. * The actual WireGuard interface is brought up when the peer accepts (tunnel_accepted). */ requestTunnel(targetAgentId: string): Promise<{ tunnelId: string; localIP: string; peerIP: string; active: boolean; close: () => Promise; }>; /** Current connection state */ get connectionState(): ConnectionState; /** Whether the agent is connected */ get connected(): boolean; /** * Connect to the rendezvous server. * Resolves when registered, rejects on timeout or fatal error. */ connect(): Promise; /** * Disconnect from the rendezvous server. */ disconnect(): Promise; /** * Request a connection to a peer agent. * @returns The session ID for this connection */ requestConnection(targetAgentId: string): string; /** * Send a WebRTC signal to a peer via the rendezvous server. */ sendSignal(sessionId: string, targetAgentId: string, signal: { type: 'offer' | 'answer' | 'ice'; data: string; }): void; /** * Disconnect a session. */ disconnectSession(sessionId: string): void; /** * Reply to a portal_message received via the 'message' event. * Must be called within the timeout window (default 30s). */ replyToMessage(messageId: string, text: string): void; /** * Track LLM token usage for this agent. Reports to the portal for cost tracking. */ trackUsage(usage: { model: string; promptTokens: number; completionTokens: number; triggeredBy?: string; triggeredByName?: string; context?: string; }): Promise; /** * Wrap an Anthropic SDK client to auto-report token usage after every call. * Does NOT require @anthropic-ai/sdk as a dependency — works via Proxy. */ wrapAnthropic(client: T, options?: { triggeredBy?: string; triggeredByName?: string; context?: string; }): T; /** * Wrap an OpenAI SDK client to auto-report token usage after every call. * Does NOT require openai as a dependency — works via Proxy. */ wrapOpenAI(client: T, options?: { triggeredBy?: string; triggeredByName?: string; context?: string; }): T; /** * Generic wrapper for any async function — extracts usage via a callback. */ tracked(fn: () => Promise, options: { model: string; extractUsage: (result: R) => { promptTokens: number; completionTokens: number; }; triggeredBy?: string; triggeredByName?: string; context?: string; }): Promise; private handleMessage; private send; private startHeartbeat; private stopHeartbeat; private scheduleReconnect; private cleanup; private setState; private assertConnected; } //# sourceMappingURL=agent.d.ts.map