Skip to content

feat(deepgram): add updateOptions #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-suits-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/agents-plugin-deepgram": patch
---

add updateOptions
28 changes: 24 additions & 4 deletions plugins/deepgram/src/stt.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<STTOptions>) {
this.#opts = { ...this.#opts, ...opts };
}

stream(): SpeechStream {
return new SpeechStream(this, this.#opts);
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -184,7 +196,13 @@ export class SpeechStream extends stt.SpeechStream {
this.closed = true;
}

updateOptions(opts: Partial<STTOptions>) {
this.#opts = { ...this.#opts, ...opts };
this.#resetWS.resolve();
}

async #runWS(ws: WebSocket) {
this.#resetWS = new Future();
let closing = false;

const keepalive = setInterval(() => {
Expand Down Expand Up @@ -238,7 +256,7 @@ export class SpeechStream extends stt.SpeechStream {
);

const listenTask = async () => {
while (!this.closed) {
while (!this.closed && !closing) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JC: was this causing an issue? If so - how did you notice it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this wasn't causing an issue, i just added this so that when the old websocket closes the loop breaks as opposed to listening indefinitely to new outputs from a closed websocket

try {
await new Promise<RawData>((resolve) => {
ws.once('message', (data) => resolve(data));
Expand Down Expand Up @@ -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])]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we propagate exceptions from the underlying tasks? In python we have something like

for task in done:
          if task != wait_reconnect_task:
                  task.result()

closing = true;
ws.close();
clearInterval(keepalive);
}
}
Expand Down