@telnyx/ai-agent-lib 0.2.1 → 0.2.3

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
@@ -340,7 +340,8 @@ The library automatically measures round-trip latency using client-side Voice Ac
340
340
 
341
341
  **How it works:**
342
342
  1. **Local VAD (User's microphone)**: Monitors the user's audio stream. After detecting 1 second of silence following speech, the library records `thinkingStartedAt` timestamp and transitions to "thinking" state.
343
- 2. **Remote VAD (Agent's audio)**: Monitors the agent's audio stream. When audio volume crosses the threshold, the library calculates `latencyMs` as the time elapsed since `thinkingStartedAt` and transitions to "speaking" state.
343
+ 2. **Remote VAD (Agent's audio)**: Monitors the agent's audio stream. When audio volume crosses the threshold, the library calculates `userPerceivedLatencyMs` as the time elapsed since user went silent and transitions to "speaking" state.
344
+ 3. **Greeting Latency**: For the first agent speech (greeting), the library calculates `greetingLatencyMs` from when the audio stream monitoring started.
344
345
 
345
346
  **Configuration constants:**
346
347
  - Volume threshold: 10 (frequency data average)
@@ -352,8 +353,11 @@ const agentState = useAgentState();
352
353
 
353
354
  // Access latency when agent starts speaking
354
355
  useEffect(() => {
355
- if (agentState.latencyMs !== undefined) {
356
- console.log(`Response latency: ${agentState.latencyMs}ms`);
356
+ if (agentState.greetingLatencyMs !== undefined) {
357
+ console.log(`Greeting latency: ${agentState.greetingLatencyMs}ms`);
358
+ }
359
+ if (agentState.userPerceivedLatencyMs !== undefined) {
360
+ console.log(`Response latency: ${agentState.userPerceivedLatencyMs}ms`);
357
361
  }
358
362
  if (agentState.thinkingStartedAt) {
359
363
  console.log(`Started thinking at: ${agentState.thinkingStartedAt}`);
@@ -446,9 +450,12 @@ type TranscriptItem = {
446
450
  // Agent state with optional latency information
447
451
  type AgentStateData = {
448
452
  state: "speaking" | "listening" | "thinking";
449
- // Round-trip latency in ms from when user stopped speaking until agent response began.
453
+ // Latency in ms from when user stopped speaking until agent response began.
450
454
  // Only present when state is "speaking"
451
- latencyMs?: number;
455
+ userPerceivedLatencyMs?: number;
456
+ // Latency in ms for the initial agent greeting (first speech).
457
+ // Only present on first "speaking" state
458
+ greetingLatencyMs?: number;
452
459
  // UTC timestamp (ISO 8601) when user stopped speaking and thinking state began.
453
460
  // Only present when state is "thinking"
454
461
  thinkingStartedAt?: string;
@@ -501,8 +508,11 @@ agent.on('conversation.agent.state', (data) => {
501
508
  break;
502
509
  case 'speaking':
503
510
  // Show speaking indicator (e.g., animated waveform)
504
- if (data.latencyMs !== undefined) {
505
- console.log(`Response latency: ${data.latencyMs}ms`);
511
+ if (data.greetingLatencyMs !== undefined) {
512
+ console.log(`Greeting latency: ${data.greetingLatencyMs}ms`);
513
+ }
514
+ if (data.userPerceivedLatencyMs !== undefined) {
515
+ console.log(`Response latency: ${data.userPerceivedLatencyMs}ms`);
506
516
  // Track latency for analytics or display to user
507
517
  }
508
518
  break;
@@ -585,8 +595,8 @@ function ConversationMonitor() {
585
595
  // Subscribe to events for additional handling beyond the built-in hooks
586
596
  useEffect(() => {
587
597
  const handleAgentState = (data: AgentStateData) => {
588
- if (data.latencyMs !== undefined) {
589
- setLatencyHistory(prev => [...prev, data.latencyMs!]);
598
+ if (data.userPerceivedLatencyMs !== undefined) {
599
+ setLatencyHistory(prev => [...prev, data.userPerceivedLatencyMs!]);
590
600
  }
591
601
  };
592
602
 
@@ -13,6 +13,9 @@ export declare class AudioStreamMonitor {
13
13
  private lastState;
14
14
  private userIsSpeaking;
15
15
  private lastUserAudioTime;
16
+ private userSilenceStartTime;
17
+ private isFirstAgentSpeech;
18
+ private monitorStartTime;
16
19
  constructor();
17
20
  private updateAgentState;
18
21
  /**