Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/selfish-geckos-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openai/agents-realtime": patch
---

Realtime: expose Call ID in OpenAIRealtimeWebRTC
18 changes: 18 additions & 0 deletions packages/agents-realtime/src/openaiRealtimeWebRtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ export type WebRTCState =
status: 'disconnected';
peerConnection: undefined;
dataChannel: undefined;
callId: string | undefined;
}
| {
status: 'connecting';
peerConnection: RTCPeerConnection;
dataChannel: RTCDataChannel;
callId: string | undefined;
}
| {
status: 'connected';
peerConnection: RTCPeerConnection;
dataChannel: RTCDataChannel;
callId: string | undefined;
};

/**
Expand Down Expand Up @@ -88,6 +91,7 @@ export class OpenAIRealtimeWebRTC
status: 'disconnected',
peerConnection: undefined,
dataChannel: undefined,
callId: undefined,
};
#useInsecureApiKey: boolean;
#ongoingResponse: boolean = false;
Expand All @@ -102,6 +106,13 @@ export class OpenAIRealtimeWebRTC
this.#useInsecureApiKey = options.useInsecureApiKey ?? false;
}

/**
* The current call ID of the WebRTC connection.
*/
get callId() {
return this.#state.callId;
}

/**
* The current status of the WebRTC connection.
*/
Expand Down Expand Up @@ -168,11 +179,13 @@ export class OpenAIRealtimeWebRTC

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

this.#state = {
status: 'connecting',
peerConnection,
dataChannel,
callId,
};
this.emit('connection_change', this.#state.status);

Expand All @@ -181,6 +194,7 @@ export class OpenAIRealtimeWebRTC
status: 'connected',
peerConnection,
dataChannel,
callId,
};
// Sending the session config again here once the channel is connected to ensure
// that the session config is sent to the server before the first response is received
Expand Down Expand Up @@ -257,6 +271,9 @@ export class OpenAIRealtimeWebRTC
},
});

callId = sdpResponse.headers?.get('Location')?.split('/').pop();
this.#state = { ...this.#state, callId };

const answer: RTCSessionDescriptionInit = {
type: 'answer',
sdp: await sdpResponse.text(),
Expand Down Expand Up @@ -326,6 +343,7 @@ export class OpenAIRealtimeWebRTC
status: 'disconnected',
peerConnection: undefined,
dataChannel: undefined,
callId: undefined,
};
this.emit('connection_change', this.#state.status);
this._onClose();
Expand Down
87 changes: 86 additions & 1 deletion packages/agents-realtime/test/openaiRealtimeWebRtc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {
writable: true,
});
Object.defineProperty(globalThis, 'fetch', {
value: async () => ({ text: async () => 'answer' }),
value: async () => ({
text: async () => 'answer',
headers: {
get: (headerKey: string) => {
if (headerKey === 'Location') {
return 'https://api.openai.com/v1/calls/rtc_u1_1234567890';
}
return null;
},
},
}),
configurable: true,
writable: true,
});
Expand Down Expand Up @@ -143,6 +153,7 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {

await expect(rtc.connect({ apiKey: 'ek_test' })).rejects.toThrow();
expect(rtc.status).toBe('disconnected');
expect(rtc.callId).toBeUndefined();
expect(events).toEqual(['connecting', 'disconnected']);
});

Expand Down Expand Up @@ -173,6 +184,7 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {

expect(stop).toHaveBeenCalled();
expect(rtc.status).toBe('disconnected');
expect(rtc.callId).toBeUndefined();
});

it('mute toggles sender tracks', async () => {
Expand Down Expand Up @@ -206,3 +218,76 @@ describe('OpenAIRealtimeWebRTC.interrupt', () => {
expect(rtc.connectionState.peerConnection).toBe(custom as any);
});
});

describe('OpenAIRealtimeWebRTC.callId', () => {
const originals: Record<string, any> = {};
const callId = 'rtc_u1_1234567890';
beforeEach(() => {
originals.RTCPeerConnection = (global as any).RTCPeerConnection;
originals.navigator = (global as any).navigator;
originals.document = (global as any).document;
originals.fetch = (global as any).fetch;

(global as any).RTCPeerConnection = FakeRTCPeerConnection as any;
Object.defineProperty(globalThis, 'navigator', {
value: {
mediaDevices: {
getUserMedia: async () => ({
getAudioTracks: () => [{ enabled: true }],
}),
},
},
configurable: true,
writable: true,
});
Object.defineProperty(globalThis, 'document', {
value: { createElement: () => ({ autoplay: true }) },
configurable: true,
writable: true,
});
Object.defineProperty(globalThis, 'fetch', {
value: async () => ({
text: async () => 'answer',
headers: {
get: (headerName: string) => {
if (headerName === 'Location') {
return 'https://api.openai.com/v1/calls/' + callId;
}
return null;
},
},
}),
configurable: true,
writable: true,
});
});

afterEach(() => {
(global as any).RTCPeerConnection = originals.RTCPeerConnection;
Object.defineProperty(globalThis, 'navigator', {
value: originals.navigator,
configurable: true,
writable: true,
});
Object.defineProperty(globalThis, 'document', {
value: originals.document,
configurable: true,
writable: true,
});
Object.defineProperty(globalThis, 'fetch', {
value: originals.fetch,
configurable: true,
writable: true,
});
lastChannel = null;
});

it('returns the callId', async () => {
const rtc = new OpenAIRealtimeWebRTC();
expect(rtc.callId).toBeUndefined();
await rtc.connect({ apiKey: 'ek_test' });
expect(rtc.callId).toBe(callId);
rtc.close();
expect(rtc.callId).toBeUndefined();
});
});