From e4aa7ceded24165db971d75917688c02b1fa4775 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 8 May 2026 16:43:42 +0000 Subject: [PATCH] fix(voice): clean up turn variables when committing a user turn manually Port of livekit/agents#5671 to agents-js. Resets last final-transcript time, speech-start time, and last-speaking time alongside the existing transcript/confidence/committed reset, and ends any in-progress user_turn span (clearing collected STT request ids) so the next speech starts a fresh span and metrics window. --- .changeset/clear-user-turn-cleanup.md | 5 +++++ agents/src/voice/audio_recognition.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 .changeset/clear-user-turn-cleanup.md diff --git a/.changeset/clear-user-turn-cleanup.md b/.changeset/clear-user-turn-cleanup.md new file mode 100644 index 000000000..da37a8652 --- /dev/null +++ b/.changeset/clear-user-turn-cleanup.md @@ -0,0 +1,5 @@ +--- +'@livekit/agents': patch +--- + +fix(voice): clean up turn variables and end user_turn span when committing a user turn manually diff --git a/agents/src/voice/audio_recognition.ts b/agents/src/voice/audio_recognition.ts index 2584432bc..1cad3b474 100644 --- a/agents/src/voice/audio_recognition.ts +++ b/agents/src/voice/audio_recognition.ts @@ -1404,8 +1404,18 @@ export class AudioRecognition { this.audioInterimTranscript = ''; this.audioPreflightTranscript = ''; this.finalTranscriptConfidence = []; + this.lastFinalTranscriptTime = 0; + this.speechStartTime = undefined; + this.lastSpeakingTime = undefined; this.userTurnCommitted = false; + // end any in-progress user_turn span so the next speech starts a fresh one + if (this.userTurnSpan && this.userTurnSpan.isRecording()) { + this.userTurnSpan.end(); + } + this.userTurnSpan = undefined; + this.sttRequestIds = []; + const restartStt = async () => { const unlock = await this.sttLifecycleLock.lock(); try {