Skip to content

Commit 9bad1c5

Browse files
committed
fix: #552 WebSocket Realtime Agent: invalid_request_error with decimal audio_end_ms data
1 parent f3d1ff8 commit 9bad1c5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/bumpy-cats-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-realtime': patch
3+
---
4+
5+
fix: #552 WebSocket Realtime Agent: invalid_request_error with decimal audio_end_ms data

packages/agents-realtime/src/openaiRealtimeWebsocket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ export class OpenAIRealtimeWebSocket
426426
}
427427

428428
const length = this._audioLengthMs ?? Number.POSITIVE_INFINITY;
429-
const audio_end_ms = Math.max(0, Math.min(Math.floor(elapsedTime), length));
429+
// audio_end_ms must be an integer
430+
const audio_end_ms = Math.max(0, Math.floor(Math.min(elapsedTime, length)));
430431

431432
this.emit('audio_interrupted');
432433
this.sendEvent({

packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ describe('OpenAIRealtimeWebSocket', () => {
205205
expect(baseSpy).toHaveBeenCalled();
206206
});
207207

208+
it('_interrupt floors fractional audio length when clamping', () => {
209+
const ws = new OpenAIRealtimeWebSocket();
210+
const sendSpy = vi
211+
.spyOn(OpenAIRealtimeWebSocket.prototype as any, 'sendEvent')
212+
.mockImplementation(() => {});
213+
// @ts-expect-error - testing protected field.
214+
ws._audioLengthMs = 42.8;
215+
ws._interrupt(100, false);
216+
const call = sendSpy.mock.calls.find(
217+
(c: unknown[]) => (c[0] as any).type === 'conversation.item.truncate',
218+
);
219+
expect((call?.[0] as any).audio_end_ms).toBe(42);
220+
expect(Number.isInteger((call?.[0] as any).audio_end_ms)).toBe(true);
221+
sendSpy.mockRestore();
222+
});
223+
208224
it('_interrupt quantizes and clamps elapsedTime', () => {
209225
const ws = new OpenAIRealtimeWebSocket();
210226
const sendSpy = vi

0 commit comments

Comments
 (0)