Skip to content

Commit 08ac275

Browse files
feat(realtime): add changePeerConnection option (#66)
* feat(realtime): add changePeerConnection option * feat(realtime): finalize changePeerConnection hook * Update change-peer-connection.md
1 parent 861ebe7 commit 08ac275

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openai/agents-realtime': patch
3+
---
4+
5+
Add `changePeerConnection` option to `OpenAIRealtimeWebRTC` allowing interception
6+
and replacement of the created `RTCPeerConnection` before the offer is made.

packages/agents-realtime/src/openaiRealtimeWebRtc.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export type OpenAIRealtimeWebRTCOptions = {
6161
* @see https://platform.openai.com/docs/guides/realtime#creating-an-ephemeral-token
6262
*/
6363
useInsecureApiKey?: boolean;
64+
/**
65+
* Optional hook invoked with the freshly created peer connection. Returning a
66+
* different connection will override the one created by the transport layer.
67+
* This is called right before the offer is created and can be asynchronous.
68+
*/
69+
changePeerConnection?: (
70+
peerConnection: RTCPeerConnection,
71+
) => RTCPeerConnection | Promise<RTCPeerConnection>;
6472
} & OpenAIRealtimeBaseOptions;
6573

6674
/**
@@ -158,7 +166,7 @@ export class OpenAIRealtimeWebRTC
158166

159167
const connectionUrl = new URL(baseUrl);
160168

161-
const peerConnection = new RTCPeerConnection();
169+
let peerConnection: RTCPeerConnection = new RTCPeerConnection();
162170
const dataChannel = peerConnection.createDataChannel('oai-events');
163171

164172
this.#state = {
@@ -226,6 +234,12 @@ export class OpenAIRealtimeWebRTC
226234
}));
227235
peerConnection.addTrack(stream.getAudioTracks()[0]);
228236

237+
if (this.options.changePeerConnection) {
238+
peerConnection =
239+
await this.options.changePeerConnection(peerConnection);
240+
this.#state = { ...this.#state, peerConnection };
241+
}
242+
229243
const offer = await peerConnection.createOffer();
230244
await peerConnection.setLocalDescription(offer);
231245

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,14 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {
195195
const rtc = new OpenAIRealtimeWebRTC();
196196
expect(() => rtc.sendEvent({ type: 'test' } as any)).toThrow();
197197
});
198+
199+
it('allows overriding the peer connection', async () => {
200+
class NewPeerConnection extends FakeRTCPeerConnection {}
201+
const custom = new NewPeerConnection();
202+
const rtc = new OpenAIRealtimeWebRTC({
203+
changePeerConnection: async () => custom as any,
204+
});
205+
await rtc.connect({ apiKey: 'ek_test' });
206+
expect(rtc.connectionState.peerConnection).toBe(custom as any);
207+
});
198208
});

0 commit comments

Comments
 (0)