Skip to content

Commit 77b69d1

Browse files
authored
feat(deepgram): add updateOptions (#374)
1 parent f2105e8 commit 77b69d1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

.changeset/poor-suits-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/agents-plugin-deepgram": patch
3+
---
4+
5+
add updateOptions

plugins/deepgram/src/stt.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
4-
import { type AudioBuffer, AudioByteStream, AudioEnergyFilter, log, stt } from '@livekit/agents';
4+
import {
5+
type AudioBuffer,
6+
AudioByteStream,
7+
AudioEnergyFilter,
8+
Future,
9+
log,
10+
stt,
11+
} from '@livekit/agents';
512
import type { AudioFrame } from '@livekit/rtc-node';
613
import { type RawData, WebSocket } from 'ws';
714
import type { STTLanguages, STTModels } from './models.js';
@@ -98,7 +105,11 @@ export class STT extends stt.STT {
98105
throw new Error('Recognize is not supported on Deepgram STT');
99106
}
100107

101-
stream(): stt.SpeechStream {
108+
updateOptions(opts: Partial<STTOptions>) {
109+
this.#opts = { ...this.#opts, ...opts };
110+
}
111+
112+
stream(): SpeechStream {
102113
return new SpeechStream(this, this.#opts);
103114
}
104115
}
@@ -108,6 +119,7 @@ export class SpeechStream extends stt.SpeechStream {
108119
#audioEnergyFilter: AudioEnergyFilter;
109120
#logger = log();
110121
#speaking = false;
122+
#resetWS = new Future();
111123
label = 'deepgram.SpeechStream';
112124

113125
constructor(stt: STT, opts: STTOptions) {
@@ -184,7 +196,13 @@ export class SpeechStream extends stt.SpeechStream {
184196
this.closed = true;
185197
}
186198

199+
updateOptions(opts: Partial<STTOptions>) {
200+
this.#opts = { ...this.#opts, ...opts };
201+
this.#resetWS.resolve();
202+
}
203+
187204
async #runWS(ws: WebSocket) {
205+
this.#resetWS = new Future();
188206
let closing = false;
189207

190208
const keepalive = setInterval(() => {
@@ -238,7 +256,7 @@ export class SpeechStream extends stt.SpeechStream {
238256
);
239257

240258
const listenTask = async () => {
241-
while (!this.closed) {
259+
while (!this.closed && !closing) {
242260
try {
243261
await new Promise<RawData>((resolve) => {
244262
ws.once('message', (data) => resolve(data));
@@ -312,7 +330,9 @@ export class SpeechStream extends stt.SpeechStream {
312330
}
313331
};
314332

315-
await Promise.all([sendTask(), listenTask(), wsMonitor]);
333+
await Promise.race([this.#resetWS.await, Promise.all([sendTask(), listenTask(), wsMonitor])]);
334+
closing = true;
335+
ws.close();
316336
clearInterval(keepalive);
317337
}
318338
}

0 commit comments

Comments
 (0)