Skip to content
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/bumpy-cats-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-realtime': patch
---

fix: #552 WebSocket Realtime Agent: invalid_request_error with decimal audio_end_ms data
3 changes: 2 additions & 1 deletion packages/agents-realtime/src/openaiRealtimeWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ export class OpenAIRealtimeWebSocket
}

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

this.emit('audio_interrupted');
this.sendEvent({
Expand Down
16 changes: 16 additions & 0 deletions packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ describe('OpenAIRealtimeWebSocket', () => {
expect(baseSpy).toHaveBeenCalled();
});

it('_interrupt floors fractional audio length when clamping', () => {
const ws = new OpenAIRealtimeWebSocket();
const sendSpy = vi
.spyOn(OpenAIRealtimeWebSocket.prototype as any, 'sendEvent')
.mockImplementation(() => {});
// @ts-expect-error - testing protected field.
ws._audioLengthMs = 42.8;
ws._interrupt(100, false);
const call = sendSpy.mock.calls.find(
(c: unknown[]) => (c[0] as any).type === 'conversation.item.truncate',
);
expect((call?.[0] as any).audio_end_ms).toBe(42);
expect(Number.isInteger((call?.[0] as any).audio_end_ms)).toBe(true);
sendSpy.mockRestore();
});

it('_interrupt quantizes and clamps elapsedTime', () => {
const ws = new OpenAIRealtimeWebSocket();
const sendSpy = vi
Expand Down