Add barge-in telemetry

This commit is contained in:
Mohamed Boudra
2025-11-15 19:31:27 +01:00
parent 4bd36c0808
commit 9dcc8314b2
3 changed files with 32 additions and 2 deletions
+17 -1
View File
@@ -1,4 +1,4 @@
import { createContext, useContext, useState, ReactNode, useCallback, useEffect } from "react";
import { createContext, useContext, useState, ReactNode, useCallback, useEffect, useRef } from "react";
import { useSpeechmaticsAudio } from "@/hooks/use-speechmatics-audio";
import { useSession } from "./session-context";
import { generateMessageId } from "@/types/stream";
@@ -39,12 +39,16 @@ export function RealtimeProvider({ children }: RealtimeProviderProps) {
setVoiceDetectionFlags,
} = useSession();
const [isRealtimeMode, setIsRealtimeMode] = useState(false);
const bargeInPlaybackStopRef = useRef<number | null>(null);
const realtimeAudio = useSpeechmaticsAudio({
onSpeechStart: () => {
console.log("[Realtime] Speech detected");
// Stop audio playback if playing
if (isPlayingAudio) {
if (bargeInPlaybackStopRef.current === null) {
bargeInPlaybackStopRef.current = Date.now();
}
audioPlayer.stop();
}
@@ -112,6 +116,18 @@ export function RealtimeProvider({ children }: RealtimeProviderProps) {
setVoiceDetectionFlags(realtimeAudio.isDetecting, realtimeAudio.isSpeaking);
}, [realtimeAudio.isDetecting, realtimeAudio.isSpeaking, setVoiceDetectionFlags]);
useEffect(() => {
if (!isPlayingAudio && bargeInPlaybackStopRef.current !== null) {
const latencyMs = Date.now() - bargeInPlaybackStopRef.current;
console.log("[Telemetry] barge_in.playback_stop_latency", {
latencyMs,
startedAt: new Date(bargeInPlaybackStopRef.current).toISOString(),
completedAt: new Date().toISOString(),
});
bargeInPlaybackStopRef.current = null;
}
}, [isPlayingAudio]);
const startRealtime = useCallback(async () => {
try {
await realtimeAudio.start();
+13
View File
@@ -2067,6 +2067,10 @@ export class Session {
return;
}
const chunkReceivedAt = Date.now();
const phaseBeforeAbort = this.processingPhase;
const hadActiveStream = Boolean(this.currentStreamPromise);
this.speechInProgress = true;
console.log(
`[Session ${this.clientId}] Realtime speech chunk detected – aborting playback and LLM`
@@ -2091,6 +2095,15 @@ export class Session {
this.abortController.abort();
await this.handleAbort();
const latencyMs = Date.now() - chunkReceivedAt;
console.log("[Telemetry] barge_in.llm_abort_latency", {
latencyMs,
conversationId: this.conversationId,
phaseBeforeAbort,
hadActiveStream,
timestamp: new Date().toISOString(),
});
}
/**