@hamsa-ai/voice-agents-sdk 0.3.1 → 0.4.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,318 @@
1
+ /**
2
+ * LiveKitConnection - WebRTC connection management for voice agent communication
3
+ *
4
+ * This class manages the core WebRTC connection to LiveKit rooms, handling connection
5
+ * lifecycle, participant management, and real-time communication state. It serves as
6
+ * the foundation for voice agent interactions by providing reliable, scalable WebRTC
7
+ * connectivity with automatic reconnection and comprehensive event handling.
8
+ *
9
+ * Key Features:
10
+ * - **Robust Connection Management**: Automatic connection establishment, monitoring, and cleanup
11
+ * - **Intelligent Reconnection**: Built-in reconnection logic with attempt tracking
12
+ * - **Participant Tracking**: Real-time monitoring of room participants and their metadata
13
+ * - **Connection Quality Optimization**: Adaptive streaming and bandwidth management
14
+ * - **Pause/Resume Functionality**: Microphone control for conversation flow management
15
+ * - **Comprehensive Event System**: Detailed events for connection state changes
16
+ * - **Performance Monitoring**: Connection timing and attempt statistics
17
+ *
18
+ * Connection Lifecycle:
19
+ * 1. **Initialization**: Room setup with optimized WebRTC configuration
20
+ * 2. **Connection**: Secure connection establishment with performance tracking
21
+ * 3. **Monitoring**: Real-time connection quality and participant management
22
+ * 4. **Reconnection**: Automatic recovery from network issues
23
+ * 5. **Cleanup**: Proper resource disposal and state management
24
+ *
25
+ * WebRTC Configuration:
26
+ * - Adaptive streaming for optimal bandwidth usage
27
+ * - Dynacast for efficient bandwidth management
28
+ * - HD video capability (720p) for future video features
29
+ * - Automatic echo cancellation and noise suppression
30
+ *
31
+ * @example Basic Connection Management
32
+ * ```typescript
33
+ * const connection = new LiveKitConnection(
34
+ * 'wss://livekit.example.com',
35
+ * 'jwt_access_token_here'
36
+ * );
37
+ *
38
+ * // Set up event listeners
39
+ * connection.on('connected', () => {
40
+ * console.log('Connected to voice agent room');
41
+ * enableVoiceInterface();
42
+ * });
43
+ *
44
+ * connection.on('disconnected', () => {
45
+ * console.log('Disconnected from room');
46
+ * disableVoiceInterface();
47
+ * });
48
+ *
49
+ * connection.on('reconnecting', () => {
50
+ * showReconnectingIndicator();
51
+ * });
52
+ *
53
+ * // Connect to room
54
+ * await connection.connect();
55
+ * ```
56
+ *
57
+ * @example Participant Management
58
+ * ```typescript
59
+ * connection.on('participantConnected', (participant) => {
60
+ * console.log(`${participant.identity} joined the conversation`);
61
+ *
62
+ * if (participant.identity.includes('agent')) {
63
+ * showAgentStatus('online');
64
+ * enableAgentFeatures();
65
+ * }
66
+ * });
67
+ *
68
+ * connection.on('participantDisconnected', (participant) => {
69
+ * console.log(`${participant.identity} left the conversation`);
70
+ *
71
+ * if (participant.identity.includes('agent')) {
72
+ * showAgentStatus('offline');
73
+ * handleAgentDisconnection();
74
+ * }
75
+ * });
76
+ *
77
+ * // Get current participants
78
+ * const participants = connection.getParticipants();
79
+ * const agentPresent = participants.some(p => p.identity.includes('agent'));
80
+ * ```
81
+ *
82
+ * @example Connection Quality Monitoring
83
+ * ```typescript
84
+ * connection.on('connectionStateChanged', (state) => {
85
+ * switch (state) {
86
+ * case 'connected':
87
+ * hideConnectionWarnings();
88
+ * break;
89
+ * case 'reconnecting':
90
+ * showConnectionIssueWarning();
91
+ * break;
92
+ * case 'disconnected':
93
+ * showConnectionLostError();
94
+ * break;
95
+ * }
96
+ * });
97
+ *
98
+ * // Monitor connection statistics
99
+ * const stats = connection.getConnectionStats();
100
+ * console.log(`Connection attempts: ${stats.connectionAttempts}`);
101
+ * console.log(`Reconnection attempts: ${stats.reconnectionAttempts}`);
102
+ * console.log(`Participants: ${stats.participantCount}`);
103
+ * ```
104
+ *
105
+ * @example Pause/Resume Functionality
106
+ * ```typescript
107
+ * // Pause conversation (mute microphone)
108
+ * pauseButton.addEventListener('click', () => {
109
+ * connection.pause();
110
+ * showPausedIndicator();
111
+ * });
112
+ *
113
+ * // Resume conversation (unmute microphone)
114
+ * resumeButton.addEventListener('click', () => {
115
+ * connection.resume();
116
+ * hidePausedIndicator();
117
+ * });
118
+ *
119
+ * // Listen for pause/resume events
120
+ * connection.on('connectionPaused', () => {
121
+ * updateUIForPausedState();
122
+ * });
123
+ *
124
+ * connection.on('connectionResumed', () => {
125
+ * updateUIForActiveState();
126
+ * });
127
+ * ```
128
+ *
129
+ * @example Error Handling
130
+ * ```typescript
131
+ * connection.on('connectionError', (error) => {
132
+ * console.error('Connection error:', error.message);
133
+ *
134
+ * // Implement retry logic
135
+ * if (error.message.includes('authentication')) {
136
+ * handleAuthenticationError();
137
+ * } else if (error.message.includes('network')) {
138
+ * handleNetworkError();
139
+ * // Automatic reconnection will be attempted
140
+ * }
141
+ * });
142
+ *
143
+ * // Monitor reconnection attempts
144
+ * connection.on('reconnecting', () => {
145
+ * const attempts = connection.getReconnectionAttempts();
146
+ * if (attempts > 3) {
147
+ * suggestNetworkTroubleshooting();
148
+ * }
149
+ * });
150
+ * ```
151
+ *
152
+ * Technical Implementation:
153
+ * - Built on LiveKit's v2.15.4 WebRTC infrastructure
154
+ * - Supports adaptive streaming for bandwidth optimization
155
+ * - Implements connection pre-warming for faster establishment
156
+ * - Provides comprehensive event forwarding from LiveKit room events
157
+ * - Maintains participant metadata and connection timing information
158
+ * - Includes fallback mechanisms for connection reliability
159
+ */
160
+ import { EventEmitter } from 'events';
161
+ import { Room } from 'livekit-client';
162
+ import type { ParticipantData } from './types';
163
+ /**
164
+ * LiveKitConnection class for managing WebRTC connections to voice agent rooms
165
+ *
166
+ * Extends EventEmitter to provide real-time connection status updates and
167
+ * participant management events for voice agent applications.
168
+ */
169
+ export declare class LiveKitConnection extends EventEmitter {
170
+ #private;
171
+ /** LiveKit room instance for WebRTC communication */
172
+ room: Room | null;
173
+ /** Current connection status to the LiveKit room */
174
+ isConnected: boolean;
175
+ /** Whether the conversation is currently paused (microphone muted) */
176
+ isPaused: boolean;
177
+ /** Map of active participants in the room with their metadata */
178
+ participants: Map<string, ParticipantData>;
179
+ /** Unix timestamp when the connection/call was initiated */
180
+ callStartTime: number | null;
181
+ /** LiveKit WebSocket URL for room connection */
182
+ private readonly lkUrl;
183
+ /** JWT access token for room authentication */
184
+ private readonly accessToken;
185
+ /** Counter for total connection attempts (including retries) */
186
+ private connectionAttempts;
187
+ /** Counter for reconnection attempts after initial connection */
188
+ private reconnectionAttempts;
189
+ /** Whether we've already emitted a 'connected' event for the current session */
190
+ private hasEmittedConnected;
191
+ /**
192
+ * Creates a new LiveKitConnection instance
193
+ *
194
+ * Initializes a LiveKit room with optimized WebRTC configuration for
195
+ * voice agent communication, including adaptive streaming and bandwidth
196
+ * management features.
197
+ *
198
+ * @param lkUrl - LiveKit WebSocket URL (e.g., 'wss://livekit.example.com')
199
+ * @param accessToken - JWT token for room authentication and authorization
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * const connection = new LiveKitConnection(
204
+ * 'wss://rtc.tryhamsa.com',
205
+ * 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
206
+ * );
207
+ *
208
+ * // Connection is ready for use
209
+ * await connection.connect();
210
+ * ```
211
+ */
212
+ constructor(lkUrl: string, accessToken: string);
213
+ /**
214
+ * Provides access to the underlying LiveKit room instance
215
+ *
216
+ * Exposes the LiveKit room for advanced operations that require direct
217
+ * access to the WebRTC room functionality. Use with caution as direct
218
+ * manipulation may interfere with connection management.
219
+ *
220
+ * @returns LiveKit Room instance or null if not initialized
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * const room = connection.getRoom();
225
+ * if (room) {
226
+ * // Access advanced LiveKit features
227
+ * console.log('Room name:', room.name);
228
+ * console.log('Local participant:', room.localParticipant?.identity);
229
+ * }
230
+ * ```
231
+ */
232
+ getRoom(): Room | null;
233
+ /**
234
+ * Establishes connection to the LiveKit room with performance optimization
235
+ *
236
+ * Initiates the WebRTC connection process with connection pre-warming for
237
+ * faster establishment times. Tracks connection performance metrics and
238
+ * handles authentication through the provided JWT token.
239
+ *
240
+ * @throws {Error} Connection failures due to network issues, authentication problems, or server unavailability
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * try {
245
+ * // Set up event listeners first
246
+ * connection.on('connected', () => {
247
+ * console.log('Successfully connected to voice agent');
248
+ * enableVoiceFeatures();
249
+ * });
250
+ *
251
+ * connection.on('connectionError', (error) => {
252
+ * console.error('Connection failed:', error.message);
253
+ * handleConnectionFailure(error);
254
+ * });
255
+ *
256
+ * // Initiate connection
257
+ * await connection.connect();
258
+ * } catch (error) {
259
+ * console.error('Failed to connect:', error);
260
+ * }
261
+ * ```
262
+ *
263
+ * @example Connection Performance Monitoring
264
+ * ```typescript
265
+ * connection.on('connectionEstablished', (timeMs) => {
266
+ * console.log(`Connection established in ${timeMs}ms`);
267
+ *
268
+ * if (timeMs > 5000) {
269
+ * logSlowConnection(timeMs);
270
+ * }
271
+ * });
272
+ *
273
+ * await connection.connect();
274
+ * ```
275
+ */
276
+ connect(): Promise<void>;
277
+ /**
278
+ * Force disconnection for testing
279
+ */
280
+ forceDisconnect(): void;
281
+ /**
282
+ * Disconnects from the LiveKit room
283
+ */
284
+ disconnect(): Promise<void>;
285
+ /**
286
+ * Pauses the connection by muting local microphone
287
+ */
288
+ pause(): void;
289
+ /**
290
+ * Resumes the connection by unmuting local microphone
291
+ */
292
+ resume(): void;
293
+ /**
294
+ * Gets connection statistics
295
+ */
296
+ getConnectionStats(): {
297
+ connectionAttempts: number;
298
+ reconnectionAttempts: number;
299
+ isConnected: boolean;
300
+ participantCount: number;
301
+ };
302
+ /**
303
+ * Gets connection attempts
304
+ */
305
+ getConnectionAttempts(): number;
306
+ /**
307
+ * Gets reconnection attempts
308
+ */
309
+ getReconnectionAttempts(): number;
310
+ /**
311
+ * Gets current participants
312
+ */
313
+ getParticipants(): ParticipantData[];
314
+ /**
315
+ * Public cleanup method for external access
316
+ */
317
+ cleanup(): void;
318
+ }