Skip to content

feat(realtime): make websocket URL configurable #238

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
5 changes: 5 additions & 0 deletions .changeset/realtime-url-configurable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-realtime': minor
---

Expose configurable URL in OpenAIRealtimeWebSocket constructor and RealtimeSession.connect.
14 changes: 10 additions & 4 deletions packages/agents-realtime/src/openaiRealtimeWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export type OpenAIRealtimeWebSocketOptions = {
* @see https://platform.openai.com/docs/guides/realtime#creating-an-ephemeral-token
*/
useInsecureApiKey?: boolean;
/**
* The URL to use for the WebSocket connection.
*/
url?: string;
} & OpenAIRealtimeBaseOptions;

/**
Expand All @@ -59,7 +63,7 @@ export class OpenAIRealtimeWebSocket
implements RealtimeTransportLayer
{
#apiKey: string | undefined;
#url: string;
#url: string | undefined;
#state: WebSocketState = {
status: 'disconnected',
websocket: undefined,
Expand All @@ -80,7 +84,7 @@ export class OpenAIRealtimeWebSocket

constructor(options: OpenAIRealtimeWebSocketOptions = {}) {
super(options);
this.#url = `wss://api.openai.com/v1/realtime?model=${this.currentModel}`;
this.#url = options.url;
this.#useInsecureApiKey = options.useInsecureApiKey ?? false;
}

Expand Down Expand Up @@ -169,7 +173,7 @@ export class OpenAIRealtimeWebSocket
},
};

const ws = new WebSocket(this.#url, websocketArguments as any);
const ws = new WebSocket(this.#url!, websocketArguments as any);
this.#state = {
status: 'connecting',
websocket: ws,
Expand Down Expand Up @@ -260,9 +264,11 @@ export class OpenAIRealtimeWebSocket
const model = options.model ?? this.currentModel;
this.currentModel = model;
this.#apiKey = await this._getApiKey(options);
this.#url =
const url =
options.url ??
this.#url ??
`wss://api.openai.com/v1/realtime?model=${this.currentModel}`;
this.#url = url;

const sessionConfig: Partial<RealtimeSessionConfig> = {
...(options.initialSessionConfig || {}),
Expand Down
1 change: 1 addition & 0 deletions packages/agents-realtime/src/realtimeSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ export class RealtimeSession<
await this.#transport.connect({
apiKey: options.apiKey ?? this.options.apiKey,
model: this.options.model,
url: options.url,
initialSessionConfig: await this.#getSessionConfig(this.options.config),
});

Expand Down
8 changes: 8 additions & 0 deletions packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ describe('OpenAIRealtimeWebSocket', () => {
expect(statuses).toEqual(['connecting', 'connected']);
});

it('uses custom url from constructor', async () => {
const ws = new OpenAIRealtimeWebSocket({ url: 'ws://test' });
const p = ws.connect({ apiKey: 'ek_test', model: 'm' });
await vi.runAllTimersAsync();
await p;
expect(lastFakeSocket!.url).toBe('ws://test');
});

it('handles audio delta, speech started and created/done events', async () => {
const ws = new OpenAIRealtimeWebSocket();
const audioSpy = vi.fn();
Expand Down
8 changes: 8 additions & 0 deletions packages/agents-realtime/test/realtimeSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ describe('RealtimeSession', () => {
expect(transport.closeCalls).toBe(1);
});

it('forwards url in connect options to transport', async () => {
const t = new FakeTransport();
const agent = new RealtimeAgent({ name: 'A', handoffs: [] });
const s = new RealtimeSession(agent, { transport: t });
await s.connect({ apiKey: 'test', url: 'ws://example' });
expect(t.connectCalls[0]?.url).toBe('ws://example');
});

it('updateHistory accepts callback', () => {
const item = createMessage('1', 'hi');
session.updateHistory([item]);
Expand Down