diff --git a/.changeset/realtime-url-configurable.md b/.changeset/realtime-url-configurable.md deleted file mode 100644 index aa7bb5cc..00000000 --- a/.changeset/realtime-url-configurable.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@openai/agents-realtime': minor ---- - -Expose configurable URL in OpenAIRealtimeWebSocket constructor and RealtimeSession.connect. diff --git a/packages/agents-realtime/src/openaiRealtimeWebsocket.ts b/packages/agents-realtime/src/openaiRealtimeWebsocket.ts index d548b361..0ce09c43 100644 --- a/packages/agents-realtime/src/openaiRealtimeWebsocket.ts +++ b/packages/agents-realtime/src/openaiRealtimeWebsocket.ts @@ -47,10 +47,6 @@ 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; /** @@ -63,7 +59,7 @@ export class OpenAIRealtimeWebSocket implements RealtimeTransportLayer { #apiKey: string | undefined; - #url: string | undefined; + #url: string; #state: WebSocketState = { status: 'disconnected', websocket: undefined, @@ -84,7 +80,7 @@ export class OpenAIRealtimeWebSocket constructor(options: OpenAIRealtimeWebSocketOptions = {}) { super(options); - this.#url = options.url; + this.#url = `wss://api.openai.com/v1/realtime?model=${this.currentModel}`; this.#useInsecureApiKey = options.useInsecureApiKey ?? false; } @@ -173,7 +169,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, @@ -264,11 +260,9 @@ export class OpenAIRealtimeWebSocket const model = options.model ?? this.currentModel; this.currentModel = model; this.#apiKey = await this._getApiKey(options); - const url = + this.#url = options.url ?? - this.#url ?? `wss://api.openai.com/v1/realtime?model=${this.currentModel}`; - this.#url = url; const sessionConfig: Partial = { ...(options.initialSessionConfig || {}), diff --git a/packages/agents-realtime/src/realtimeSession.ts b/packages/agents-realtime/src/realtimeSession.ts index e05e5148..62738ac7 100644 --- a/packages/agents-realtime/src/realtimeSession.ts +++ b/packages/agents-realtime/src/realtimeSession.ts @@ -672,7 +672,6 @@ 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), }); diff --git a/packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts b/packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts index 91572a49..3403a6b6 100644 --- a/packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts +++ b/packages/agents-realtime/test/openaiRealtimeWebsocket.test.ts @@ -58,14 +58,6 @@ 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(); diff --git a/packages/agents-realtime/test/realtimeSession.test.ts b/packages/agents-realtime/test/realtimeSession.test.ts index 648b5197..dfb4e6d5 100644 --- a/packages/agents-realtime/test/realtimeSession.test.ts +++ b/packages/agents-realtime/test/realtimeSession.test.ts @@ -113,14 +113,6 @@ 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]);