From f60c6e0c9f35d289a23e0d9775eec6b212280b16 Mon Sep 17 00:00:00 2001 From: aoife cassidy Date: Thu, 24 Apr 2025 19:17:18 +0300 Subject: [PATCH 1/2] feat(deepgram): add updateOptions --- plugins/deepgram/src/stt.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/plugins/deepgram/src/stt.ts b/plugins/deepgram/src/stt.ts index 45174b82..560b26a7 100644 --- a/plugins/deepgram/src/stt.ts +++ b/plugins/deepgram/src/stt.ts @@ -1,7 +1,14 @@ // SPDX-FileCopyrightText: 2024 LiveKit, Inc. // // SPDX-License-Identifier: Apache-2.0 -import { type AudioBuffer, AudioByteStream, AudioEnergyFilter, log, stt } from '@livekit/agents'; +import { + type AudioBuffer, + AudioByteStream, + AudioEnergyFilter, + Future, + log, + stt, +} from '@livekit/agents'; import type { AudioFrame } from '@livekit/rtc-node'; import { type RawData, WebSocket } from 'ws'; import type { STTLanguages, STTModels } from './models.js'; @@ -98,7 +105,11 @@ export class STT extends stt.STT { throw new Error('Recognize is not supported on Deepgram STT'); } - stream(): stt.SpeechStream { + updateOptions(opts: Partial) { + this.#opts = { ...this.#opts, ...opts }; + } + + stream(): SpeechStream { return new SpeechStream(this, this.#opts); } } @@ -108,6 +119,7 @@ export class SpeechStream extends stt.SpeechStream { #audioEnergyFilter: AudioEnergyFilter; #logger = log(); #speaking = false; + #resetWS = new Future(); label = 'deepgram.SpeechStream'; constructor(stt: STT, opts: STTOptions) { @@ -184,7 +196,13 @@ export class SpeechStream extends stt.SpeechStream { this.closed = true; } + updateOptions(opts: Partial) { + this.#opts = { ...this.#opts, ...opts }; + this.#resetWS.resolve(); + } + async #runWS(ws: WebSocket) { + this.#resetWS = new Future(); let closing = false; const keepalive = setInterval(() => { @@ -238,7 +256,7 @@ export class SpeechStream extends stt.SpeechStream { ); const listenTask = async () => { - while (!this.closed) { + while (!this.closed && !closing) { try { await new Promise((resolve) => { ws.once('message', (data) => resolve(data)); @@ -312,7 +330,9 @@ export class SpeechStream extends stt.SpeechStream { } }; - await Promise.all([sendTask(), listenTask(), wsMonitor]); + await Promise.race([this.#resetWS.await, Promise.all([sendTask(), listenTask(), wsMonitor])]); + closing = true; + ws.close(); clearInterval(keepalive); } } From 5a48ee56fe68a9900afa8626655d37201c727807 Mon Sep 17 00:00:00 2001 From: aoife cassidy Date: Thu, 24 Apr 2025 19:18:27 +0300 Subject: [PATCH 2/2] Create poor-suits-yawn.md --- .changeset/poor-suits-yawn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/poor-suits-yawn.md diff --git a/.changeset/poor-suits-yawn.md b/.changeset/poor-suits-yawn.md new file mode 100644 index 00000000..926e21b6 --- /dev/null +++ b/.changeset/poor-suits-yawn.md @@ -0,0 +1,5 @@ +--- +"@livekit/agents-plugin-deepgram": patch +--- + +add updateOptions