Skip to content

Commit 14900dc

Browse files
authored
doc: add realtime sip docs (#632)
1 parent b54250c commit 14900dc

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

docs/src/content/docs/guides/voice-agents/transport.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import historyUpdatedExample from '../../../../../../examples/docs/voice-agents/
2222
import updateHistoryExample from '../../../../../../examples/docs/voice-agents/updateHistory.ts?raw';
2323
import customWebRTCTransportExample from '../../../../../../examples/docs/voice-agents/customWebRTCTransport.ts?raw';
2424
import websocketSessionExample from '../../../../../../examples/docs/voice-agents/websocketSession.ts?raw';
25+
import sipTransportExample from '../../../../../../examples/docs/voice-agents/sipTransport.ts?raw';
2526
import transportEventsExample from '../../../../../../examples/docs/voice-agents/transportEvents.ts?raw';
2627
import thinClientExample from '../../../../../../examples/docs/voice-agents/thinClient.ts?raw';
2728
import cloudflareTransportExample from '../../../../../../examples/docs/extensions/cloudflare-basic.ts?raw';
@@ -47,6 +48,16 @@ building a phone agent with Twilio.
4748

4849
Use any recording/playback library to handle the raw PCM16 audio bytes.
4950

51+
### Connecting over SIP
52+
53+
Bridge SIP calls from providers such as Twilio by using the `OpenAIRealtimeSIP` transport. The transport keeps the Realtime session synchronized with SIP events emitted by your telephony provider.
54+
55+
1. Accept the incoming call by generating an initial session configuration with `OpenAIRealtimeSIP.buildInitialConfig()`. This ensures the SIP invitation and Realtime session share identical defaults.
56+
2. Attach a `RealtimeSession` that uses the `OpenAIRealtimeSIP` transport and connect with the `callId` issued by the provider webhook.
57+
3. Listen for session events to drive call analytics, transcripts, or escalation logic.
58+
59+
<Code lang="typescript" code={sipTransportExample} />
60+
5061
#### Cloudflare Workers (workerd) note
5162

5263
Cloudflare Workers and other workerd runtimes cannot open outbound WebSockets using the global `WebSocket` constructor. Use the Cloudflare transport from the extensions package, which performs the `fetch()`-based upgrade internally.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import OpenAI from 'openai';
2+
import {
3+
OpenAIRealtimeSIP,
4+
RealtimeAgent,
5+
RealtimeSession,
6+
type RealtimeSessionOptions,
7+
} from '@openai/agents/realtime';
8+
9+
const openai = new OpenAI({
10+
apiKey: process.env.OPENAI_API_KEY!,
11+
webhookSecret: process.env.OPENAI_WEBHOOK_SECRET!,
12+
});
13+
14+
const agent = new RealtimeAgent({
15+
name: 'Receptionist',
16+
instructions:
17+
'Welcome the caller, answer scheduling questions, and hand off if the caller requests a human.',
18+
});
19+
20+
const sessionOptions: Partial<RealtimeSessionOptions> = {
21+
model: 'gpt-realtime',
22+
config: {
23+
audio: {
24+
input: {
25+
turnDetection: { type: 'semantic_vad', interruptResponse: true },
26+
},
27+
},
28+
},
29+
};
30+
31+
export async function acceptIncomingCall(callId: string): Promise<void> {
32+
const initialConfig = await OpenAIRealtimeSIP.buildInitialConfig(
33+
agent,
34+
sessionOptions,
35+
);
36+
await openai.realtime.calls.accept(callId, initialConfig);
37+
}
38+
39+
export async function attachRealtimeSession(
40+
callId: string,
41+
): Promise<RealtimeSession> {
42+
const session = new RealtimeSession(agent, {
43+
transport: new OpenAIRealtimeSIP(),
44+
...sessionOptions,
45+
});
46+
47+
session.on('history_added', (item) => {
48+
console.log('Realtime update:', item.type);
49+
});
50+
51+
await session.connect({
52+
apiKey: process.env.OPENAI_API_KEY!,
53+
callId,
54+
});
55+
56+
return session;
57+
}

0 commit comments

Comments
 (0)