openai / openai-realtime-api-beta

Node.js + JavaScript reference client for the Realtime API (beta)
MIT License
548 stars 113 forks source link

Websocket handlers shouldn't throw exceptions #12

Open Stevenic opened 2 weeks ago

Stevenic commented 2 weeks ago

My relay server instance sometimes dies with this error...

file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/conversation.js:202
        throw new Error(`response.audio.delta: Item "${item_id}" not found`);
        ^

Error: response.audio.delta: Item "item_AEjLKjxXWZnPmwwdAm9UY" not found
    at response.audio.delta (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/conversation.js:202:15)
    at RealtimeConversation.processEvent (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/conversation.js:292:27)
    at handler (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/client.js:278:49)
    at handlerWithDispatch (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/client.js:282:31)
    at RealtimeAPI.dispatch (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/event_handler.js:134:7)
    at RealtimeAPI.receive (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/api.js:178:10)
    at WebSocket.<anonymous> (file:///c:/source/stevenic/synthos/node_modules/@openai/realtime-api-beta/lib/api.js:129:14)
    at WebSocket.emit (node:events:517:28)
    at Receiver.receiverOnMessage (c:\source\stevenic\synthos\node_modules\ws\lib\websocket.js:1220:20)
    at Receiver.emit (node:events:517:28)

Node.js v18.20.2
error Command failed with exit code 1.

The issue is that internal socket handlers are throwing exceptions which can't easily be caught. If you hit an error like this you should map it to a error event of some sort...

khorwood-openai commented 2 weeks ago

The reason for throwing errors here is that this sort of thing should never happen. Can you reliably reproduce so I can fix?

sam-cormack commented 2 weeks ago

I'm seeing the same thing using a relay server. I can reproduce it consistently by:

From what I can tell, calling client.disconnect() in the browser causes a call to client.disconnect() on the relay server (I'm using the relay server from the openai-realtime-console repo). This calls conversation.clear() on the relay server RealtimeClient. If the relay server client is already processing a response.audio.delta event, the event_id lookup will fail causing the error. I've confirmed that removing the line this.conversation.clear(); from the disconnect() method in RealtimeClient on server stops the error from occurring (though is obviously not a real fix).

Stevenic commented 2 weeks ago

Yeah I periodically get them for transcription deltas as well. It's very annoying because it means i can't prevent the relay server from crashing

Error: response.audio_transcript.delta: Item "item_AGBPgXAeI4ng5ST0WGzc5" not found