Skip to content

feat(realtime): add changePeerConnection option #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 6 additions & 0 deletions .changeset/change-peer-connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@openai/agents-realtime': patch
---

Add `changePeerConnection` option to `OpenAIRealtimeWebRTC` allowing interception
and replacement of the created `RTCPeerConnection` before the offer is made.
16 changes: 15 additions & 1 deletion packages/agents-realtime/src/openaiRealtimeWebRtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export type OpenAIRealtimeWebRTCOptions = {
* @see https://platform.openai.com/docs/guides/realtime#creating-an-ephemeral-token
*/
useInsecureApiKey?: boolean;
/**
* Optional hook invoked with the freshly created peer connection. Returning a
* different connection will override the one created by the transport layer.
* This is called right before the offer is created and can be asynchronous.
*/
changePeerConnection?: (
peerConnection: RTCPeerConnection,
) => RTCPeerConnection | Promise<RTCPeerConnection>;
} & OpenAIRealtimeBaseOptions;

/**
Expand Down Expand Up @@ -158,7 +166,7 @@ export class OpenAIRealtimeWebRTC

const connectionUrl = new URL(baseUrl);

const peerConnection = new RTCPeerConnection();
let peerConnection: RTCPeerConnection = new RTCPeerConnection();
const dataChannel = peerConnection.createDataChannel('oai-events');

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

if (this.options.changePeerConnection) {
peerConnection =
await this.options.changePeerConnection(peerConnection);
this.#state = { ...this.#state, peerConnection };
}

const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);

Expand Down
10 changes: 10 additions & 0 deletions packages/agents-realtime/test/openaiRealtimeWebRtc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {
const rtc = new OpenAIRealtimeWebRTC();
expect(() => rtc.sendEvent({ type: 'test' } as any)).toThrow();
});

it('allows overriding the peer connection', async () => {
class NewPeerConnection extends FakeRTCPeerConnection {}
const custom = new NewPeerConnection();
const rtc = new OpenAIRealtimeWebRTC({
changePeerConnection: async () => custom as any,
});
await rtc.connect({ apiKey: 'ek_test' });
expect(rtc.connectionState.peerConnection).toBe(custom as any);
});
});