SIP call termination in OpenAI Realtime API with Agents SDK?

Hi guys

I’m implementing a post-call routine for OpenAI Realtime API SIP calls using the @openai/agents SDK (TypeScript) with openai transport, but I cannot detect when calls end.

I looked around a bit in open source projects and documentation, but I don’t understand why I can’t detect the end of the websocket connection.

My Setup:

import { RealtimeSession, OpenAIRealtimeSIP } from '@openai/agents/realtime'

const session = new RealtimeSession(agent, {
  transport: new OpenAIRealtimeSIP(),
  ...sessionOptions,
})

await session.connect({ apiKey, callId })

What I’ve Tried:

  1. Listening to multiple event names - None fire when caller hangs up:
const closeEvents = ['close', 'disconnect', 'end', 'finish', 'completed', 'terminated']
for (const eventName of closeEvents) {
  session.on(eventName, () => {
    console.log(`${eventName} triggered`) // Never logs
  })
}

  1. Intercepting all session events - No events emitted on hangup:
const originalEmit = session.emit?.bind(session)
session.emit = (event, ...args) => {
  console.log(`Event emitted: ${event}`) // Nothing on call end
  return originalEmit(event, ...args)
}


What I Need: When a caller hangs up, I need to trigger a post-call routine that:

  • Extracts conversation transcript, do some custom code like xhr call etc

Thank you for your help!

Hi man, I managed as the example in the documentation. You must suscribe to the event session.transport.on(‘disconnected’).

See you.

 try {
      await session.connect({ apiKey, callId });
      console.info(`[${timestamp()}] Attached to realtime call ${callId}`);

      session.transport.sendEvent({
        type: 'response.create',
        response: {
          //instructions: `Say exactly '${SALUDO_INICIAL}' now before continuing the conversation.`,
          instructions: `Say exactly '${SALUDO_INICIAL_HRF}' now before continuing the conversation.`,
        },
      });

      await new Promise<void>((resolve) => {
        const handleDisconnect = () => {
          session.transport.off('disconnected', handleDisconnect);
          resolve();
        };
        session.transport.on('disconnected', handleDisconnect);
      });
    } catch (error) {
      console.error(`Error while observing call ${callId}:`, error);
    } finally {
      session.close();
      console.info(`[${timestamp()}] Call ${callId} ended`);
    }
Summary

This text will be hidden