Skip to content

Commit 52f9f33

Browse files
fix(realtime-ws): stop accidental cancellation error
1 parent fe5fb97 commit 52f9f33

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/agents-realtime/src/openaiRealtimeWebsocket.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ export class OpenAIRealtimeWebSocket
327327
_cancelResponse() {
328328
// cancel the ongoing response
329329
if (this.#ongoingResponse) {
330-
this.sendEvent({
331-
type: 'response.cancel',
332-
});
330+
// this.sendEvent({
331+
// type: 'response.cancel',
332+
// event_id: 'test',
333+
// });
333334
this.#ongoingResponse = false;
334335
}
335336
}
@@ -344,7 +345,12 @@ export class OpenAIRealtimeWebSocket
344345
* @param elapsedTime - The elapsed time since the response started.
345346
*/
346347
_interrupt(elapsedTime: number) {
348+
if (elapsedTime >= 0 && elapsedTime < this._audioLengthMs) {
349+
return;
350+
}
351+
347352
// immediately emit this event so the client can stop playing audio
353+
this._cancelResponse();
348354
this.emit('audio_interrupted');
349355
this.sendEvent({
350356
type: 'conversation.item.truncate',
@@ -367,10 +373,9 @@ export class OpenAIRealtimeWebSocket
367373
return;
368374
}
369375

370-
this._cancelResponse();
371-
372376
const elapsedTime = Date.now() - this._firstAudioTimestamp;
373-
if (elapsedTime >= 0 && elapsedTime < this._audioLengthMs) {
377+
378+
if (elapsedTime >= 0) {
374379
this._interrupt(elapsedTime);
375380
}
376381

0 commit comments

Comments
 (0)