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

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Hamsa Voice Agents Web SDK
2
2
 
3
- Hamsa Voice Agents Web SDK is a JavaScript library for integrating voice agents from <https://dashboard.tryhamsa.com>. This SDK provides a seamless way to incorporate voice interactions into your web applications.
3
+ Hamsa Voice Agents Web SDK is a JavaScript library for integrating voice agents from <https://dashboard.tryhamsa.com>. This SDK provides a seamless way to incorporate voice interactions into your web applications with high-quality real-time audio communication.
4
4
 
5
5
  ## Installation
6
6
 
@@ -17,7 +17,7 @@ npm i @hamsa-ai/voice-agents-sdk
17
17
  First, import the package in your code:
18
18
 
19
19
  ```javascript
20
- import { HamsaVoiceAgent } from '@hamsa-ai/voice-agents-sdk'
20
+ import { HamsaVoiceAgent } from "@hamsa-ai/voice-agents-sdk";
21
21
  ```
22
22
 
23
23
  Initialize the SDK with your API key:
@@ -37,9 +37,11 @@ Include the script from a CDN:
37
37
  Then, you can initialize the agent like this:
38
38
 
39
39
  ```javascript
40
- const agent = new HamsaVoiceAgent('YOUR_API_KEY');
40
+ const agent = new HamsaVoiceAgent("YOUR_API_KEY");
41
41
 
42
- agent.on("callStarted", () => { console.log("Conversation has started!"); });
42
+ agent.on("callStarted", () => {
43
+ console.log("Conversation has started!");
44
+ });
43
45
 
44
46
  // Example: Start a call
45
47
  // agent.start({ agentId: 'YOUR_AGENT_ID' });
@@ -53,11 +55,11 @@ Start a conversation with an existing agent by calling the "start" function. You
53
55
 
54
56
  ```javascript
55
57
  agent.start({
56
- agentId: YOUR_AGENT_ID,
57
- params: {
58
- param1: "NAME",
59
- param2: "NAME2"
60
- }
58
+ agentId: YOUR_AGENT_ID,
59
+ params: {
60
+ param1: "NAME",
61
+ param2: "NAME2",
62
+ },
61
63
  });
62
64
  ```
63
65
 
@@ -92,29 +94,379 @@ During the conversation, the SDK emits events to update your application about t
92
94
  ### Conversation Status Events
93
95
 
94
96
  ```javascript
95
- agent.on("callStarted", () => { console.log("Conversation has started!"); });
96
- agent.on("callEnded", () => { console.log("Conversation has ended!"); });
97
- agent.on("callPaused", () => { console.log("The conversation is paused"); });
98
- agent.on("callResumed", () => { console.log("Conversation has resumed"); });
97
+ agent.on("callStarted", () => {
98
+ console.log("Conversation has started!");
99
+ });
100
+ agent.on("callEnded", () => {
101
+ console.log("Conversation has ended!");
102
+ });
103
+ agent.on("callPaused", () => {
104
+ console.log("The conversation is paused");
105
+ });
106
+ agent.on("callResumed", () => {
107
+ console.log("Conversation has resumed");
108
+ });
99
109
  ```
100
110
 
101
111
  ### Agent Status Events
102
112
 
103
113
  ```javascript
104
- agent.on("speaking", () => { console.log("The agent is speaking"); });
105
- agent.on("listening", () => { console.log("The agent is listening"); });
114
+ agent.on("speaking", () => {
115
+ console.log("The agent is speaking");
116
+ });
117
+ agent.on("listening", () => {
118
+ console.log("The agent is listening");
119
+ });
106
120
  ```
107
121
 
108
122
  ### Conversation Script Events
109
123
 
110
124
  ```javascript
111
- agent.on("transcriptionReceived", (text) => { console.log("User speech transcription received", text); });
112
- agent.on("answerReceived", (text) => { console.log("Agent answer received", text); });
125
+ agent.on("transcriptionReceived", (text) => {
126
+ console.log("User speech transcription received", text);
127
+ });
128
+ agent.on("answerReceived", (text) => {
129
+ console.log("Agent answer received", text);
130
+ });
113
131
  ```
114
132
 
115
133
  ### Error Events
116
134
 
117
135
  ```javascript
118
- agent.on("closed", () => { console.log("Conversation was closed"); });
119
- agent.on("error", (e) => { console.log("Error was received", e); });
136
+ agent.on("closed", () => {
137
+ console.log("Conversation was closed");
138
+ });
139
+ agent.on("error", (e) => {
140
+ console.log("Error was received", e);
141
+ });
142
+ ```
143
+
144
+ ### Advanced Analytics Events
145
+
146
+ The SDK provides comprehensive analytics for monitoring call quality, performance, and custom agent events:
147
+
148
+ ```javascript
149
+ // Real-time connection quality updates
150
+ agent.on("connectionQualityChanged", ({ quality, participant, metrics }) => {
151
+ console.log(`Connection quality: ${quality}`, metrics);
152
+ });
153
+
154
+ // Periodic analytics updates (every second during calls)
155
+ agent.on("analyticsUpdated", (analytics) => {
156
+ console.log("Call analytics:", analytics);
157
+ // Contains: connectionStats, audioMetrics, performanceMetrics, etc.
158
+ });
159
+
160
+ // Participant events
161
+ agent.on("participantConnected", (participant) => {
162
+ console.log("Participant joined:", participant.identity);
163
+ });
164
+
165
+ agent.on("participantDisconnected", (participant) => {
166
+ console.log("Participant left:", participant.identity);
167
+ });
168
+
169
+ // Track subscription events (audio/video streams)
170
+ agent.on("trackSubscribed", ({ track, participant, trackStats }) => {
171
+ console.log("New track:", track.kind, "from", participant);
172
+ });
173
+
174
+ agent.on("trackUnsubscribed", ({ track, participant }) => {
175
+ console.log("Track ended:", track.kind, "from", participant);
176
+ });
177
+
178
+ // Connection state changes
179
+ agent.on("reconnecting", () => {
180
+ console.log("Attempting to reconnect...");
181
+ });
182
+
183
+ agent.on("reconnected", () => {
184
+ console.log("Successfully reconnected");
185
+ });
186
+
187
+ // Custom events from agents
188
+ agent.on("customEvent", (eventType, eventData, metadata) => {
189
+ console.log(`Custom event: ${eventType}`, eventData);
190
+ // Examples: flow_navigation, tool_execution, agent_state_change
191
+ });
192
+ ```
193
+
194
+ ## Analytics & Monitoring
195
+
196
+ The SDK provides comprehensive real-time analytics for monitoring call quality, performance metrics, and custom agent events. Access analytics data through both synchronous methods and event-driven updates.
197
+
198
+ ### Synchronous Analytics Methods
199
+
200
+ Get real-time analytics data instantly for dashboards and monitoring:
201
+
202
+ ```javascript
203
+ // Connection quality and network statistics
204
+ const connectionStats = agent.getConnectionStats();
205
+ console.log(connectionStats);
206
+ /*
207
+ {
208
+ latency: 45, // Network latency in ms
209
+ packetLoss: 0.1, // Packet loss percentage
210
+ bandwidth: 128000, // Current bandwidth usage
211
+ quality: 'good', // Connection quality: excellent/good/poor/lost
212
+ jitter: 2, // Network jitter
213
+ connectionAttempts: 1, // Total connection attempts
214
+ reconnectionAttempts: 0, // Reconnection attempts
215
+ isConnected: true // Current connection status
216
+ }
217
+ */
218
+
219
+ // Audio levels and quality metrics
220
+ const audioLevels = agent.getAudioLevels();
221
+ console.log(audioLevels);
222
+ /*
223
+ {
224
+ userAudioLevel: 0.8, // Current user audio level
225
+ agentAudioLevel: 0.3, // Current agent audio level
226
+ userSpeakingTime: 30000, // User speaking duration (ms)
227
+ agentSpeakingTime: 20000, // Agent speaking duration (ms)
228
+ audioDropouts: 0, // Audio interruption count
229
+ echoCancellationActive: true,// Echo cancellation status
230
+ volume: 1.0, // Current volume setting
231
+ isPaused: false // Pause state
232
+ }
233
+ */
234
+
235
+ // Performance metrics
236
+ const performance = agent.getPerformanceMetrics();
237
+ console.log(performance);
238
+ /*
239
+ {
240
+ responseTime: 1200, // Total response time
241
+ networkLatency: 45, // Network round-trip time
242
+ callDuration: 60000, // Current call duration (ms)
243
+ connectionEstablishedTime: 250, // Time to establish connection
244
+ reconnectionCount: 0 // Number of reconnections
245
+ }
246
+ */
247
+
248
+ // Participant information
249
+ const participants = agent.getParticipants();
250
+ console.log(participants);
251
+ /*
252
+ [
253
+ {
254
+ identity: "agent",
255
+ sid: "participant-sid",
256
+ connectionTime: 1638360000000,
257
+ metadata: "agent-metadata"
258
+ }
259
+ ]
260
+ */
261
+
262
+ // Track statistics (audio/video streams)
263
+ const trackStats = agent.getTrackStats();
264
+ console.log(trackStats);
265
+ /*
266
+ {
267
+ totalTracks: 2,
268
+ activeTracks: 2,
269
+ audioElements: 1,
270
+ trackDetails: [
271
+ ["track-id", { trackId: "track-id", kind: "audio", participant: "agent" }]
272
+ ]
273
+ }
274
+ */
275
+
276
+ // Complete analytics snapshot
277
+ const analytics = agent.getCallAnalytics();
278
+ console.log(analytics);
279
+ /*
280
+ {
281
+ callDuration: 60000,
282
+ callStartTime: 1638360000000,
283
+ isConnected: true,
284
+ isPaused: false,
285
+ connectionStats: { ... },
286
+ audioMetrics: { ... },
287
+ performanceMetrics: { ... },
288
+ participants: [ ... ],
289
+ trackStats: { ... },
290
+ callStats: { ... }
291
+ }
292
+ */
293
+ ```
294
+
295
+ ### Real-time Dashboard Example
296
+
297
+ Build live monitoring dashboards using the analytics data:
298
+
299
+ ```javascript
300
+ // Update dashboard every second
301
+ const updateDashboard = () => {
302
+ const stats = agent.getConnectionStats();
303
+ const audio = agent.getAudioLevels();
304
+ const performance = agent.getPerformanceMetrics();
305
+
306
+ // Update UI elements
307
+ document.getElementById('latency').textContent = `${stats.latency}ms`;
308
+ document.getElementById('quality').textContent = stats.quality;
309
+ document.getElementById('duration').textContent = `${Math.floor(performance.callDuration / 1000)}s`;
310
+ document.getElementById('user-audio').style.width = `${audio.userAudioLevel * 100}%`;
311
+ document.getElementById('agent-audio').style.width = `${audio.agentAudioLevel * 100}%`;
312
+ };
313
+
314
+ // Start dashboard updates when call begins
315
+ agent.on('callStarted', () => {
316
+ const dashboardInterval = setInterval(updateDashboard, 1000);
317
+
318
+ agent.on('callEnded', () => {
319
+ clearInterval(dashboardInterval);
320
+ });
321
+ });
322
+ ```
323
+
324
+ ### Custom Event Tracking
325
+
326
+ Track custom events from your voice agents:
327
+
328
+ ```javascript
329
+ agent.on("customEvent", (eventType, eventData, metadata) => {
330
+ switch(eventType) {
331
+ case "flow_navigation":
332
+ console.log("Agent navigated:", eventData.from, "->", eventData.to);
333
+ // Track conversation flow
334
+ break;
335
+
336
+ case "tool_execution":
337
+ console.log("Tool called:", eventData.toolName, "Result:", eventData.success);
338
+ // Monitor tool usage
339
+ break;
340
+
341
+ case "agent_state_change":
342
+ console.log("Agent state:", eventData.state);
343
+ // Track agent behavior
344
+ break;
345
+
346
+ case "user_intent_detected":
347
+ console.log("User intent:", eventData.intent, "Confidence:", eventData.confidence);
348
+ // Analyze user intent
349
+ break;
350
+
351
+ default:
352
+ console.log("Custom event:", eventType, eventData);
353
+ }
354
+ });
120
355
  ```
356
+
357
+ ## Configuration Options
358
+
359
+ The SDK accepts optional configuration parameters:
360
+
361
+ ```javascript
362
+ const agent = new HamsaVoiceAgent("YOUR_API_KEY", {
363
+ API_URL: "https://api.tryhamsa.com", // API endpoint (default)
364
+ });
365
+ ```
366
+
367
+ ## Client-Side Tools
368
+
369
+ You can register client-side tools that the agent can call during conversations:
370
+
371
+ ```javascript
372
+ const tools = [
373
+ {
374
+ function_name: "getUserInfo",
375
+ description: "Get user information",
376
+ parameters: [
377
+ {
378
+ name: "userId",
379
+ type: "string",
380
+ description: "User ID to look up",
381
+ },
382
+ ],
383
+ required: ["userId"],
384
+ fn: async (userId) => {
385
+ // Your tool implementation
386
+ const userInfo = await fetchUserInfo(userId);
387
+ return userInfo;
388
+ },
389
+ },
390
+ ];
391
+
392
+ agent.start({
393
+ agentId: "YOUR_AGENT_ID",
394
+ tools: tools,
395
+ voiceEnablement: true,
396
+ });
397
+ ```
398
+
399
+ ## Migration from Previous Versions
400
+
401
+ If you're upgrading from a previous version, see the [Migration Guide](./MIGRATION_GUIDE.md) for detailed instructions. Connection details are now automatically managed and no longer need to be configured.
402
+
403
+ ## Browser Compatibility
404
+
405
+ This SDK supports modern browsers with WebRTC capabilities:
406
+
407
+ - Chrome 60+
408
+ - Firefox 60+
409
+ - Safari 12+
410
+ - Edge 79+
411
+
412
+ ## TypeScript Support
413
+
414
+ The SDK includes comprehensive TypeScript definitions with detailed analytics interfaces:
415
+
416
+ ```typescript
417
+ import { HamsaVoiceAgent } from "@hamsa-ai/voice-agents-sdk";
418
+
419
+ // All analytics methods return strongly typed data
420
+ const agent = new HamsaVoiceAgent("API_KEY");
421
+
422
+ // TypeScript will provide autocomplete and type checking
423
+ agent.on("analyticsUpdated", (analytics: CallAnalytics) => {
424
+ console.log(analytics.connectionStats.latency); // number
425
+ console.log(analytics.audioMetrics.userAudioLevel); // number
426
+ console.log(analytics.performanceMetrics.callDuration); // number
427
+ });
428
+
429
+ // Strongly typed custom events
430
+ agent.on("customEvent", (eventType: string, eventData: any, metadata: CustomEventMetadata) => {
431
+ console.log(metadata.timestamp); // number
432
+ console.log(metadata.participant); // string
433
+ });
434
+ ```
435
+
436
+ ## Use Cases
437
+
438
+ ### Real-time Call Quality Monitoring
439
+ ```javascript
440
+ agent.on("connectionQualityChanged", ({ quality, metrics }) => {
441
+ if (quality === 'poor') {
442
+ showNetworkWarning();
443
+ logQualityIssue(metrics);
444
+ }
445
+ });
446
+ ```
447
+
448
+ ### Analytics Dashboard
449
+ ```javascript
450
+ const analytics = agent.getCallAnalytics();
451
+ sendToAnalytics({
452
+ callDuration: analytics.callDuration,
453
+ audioQuality: analytics.audioMetrics,
454
+ participantCount: analytics.participants.length,
455
+ performance: analytics.performanceMetrics
456
+ });
457
+ ```
458
+
459
+ ### Conversation Flow Analysis
460
+ ```javascript
461
+ agent.on("customEvent", (eventType, data) => {
462
+ if (eventType === "flow_navigation") {
463
+ trackConversationFlow(data.from, data.to);
464
+ optimizeAgentResponses(data);
465
+ }
466
+ });
467
+ ```
468
+
469
+ ## Dependencies
470
+
471
+ - **livekit-client**: Real-time communication infrastructure
472
+ - **events**: EventEmitter for browser compatibility