@@ -8,16 +8,21 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
88 socket : WS . WebSocket ;
99
1010 constructor (
11- props : { model : string ; options ?: WS . ClientOptions | undefined } ,
11+ props : {
12+ model : string ;
13+ options ?: WS . ClientOptions | undefined ;
14+ /** @internal */ __resolvedApiKey ?: boolean ;
15+ } ,
1216 client ?: Pick < OpenAI , 'apiKey' | 'baseURL' > ,
1317 ) {
1418 super ( ) ;
1519 client ??= new OpenAI ( ) ;
16- if ( typeof ( client as any ) . _options . apiKey !== 'string' ) {
20+ const hasProvider = typeof ( client as any ) ?. _options ?. apiKey === 'function' ;
21+ if ( hasProvider && ! props . __resolvedApiKey ) {
1722 throw new Error (
1823 [
1924 'Cannot open Realtime WebSocket with a function-based apiKey.' ,
20- 'Use the factory so the key is resolved before connecting:' ,
25+ 'Use the factory so the key is resolved just before connecting:' ,
2126 '- OpenAIRealtimeWS.create(client, { model })' ,
2227 ] . join ( '\n' ) ,
2328 ) ;
@@ -27,7 +32,7 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
2732 ...props . options ,
2833 headers : {
2934 ...props . options ?. headers ,
30- ...( isAzure ( client ) ? { } : { Authorization : `Bearer ${ client . apiKey } ` } ) ,
35+ ...( isAzure ( client ) && ! props . __resolvedApiKey ? { } : { Authorization : `Bearer ${ client . apiKey } ` } ) ,
3136 'OpenAI-Beta' : 'realtime=v1' ,
3237 } ,
3338 } ) ;
@@ -63,20 +68,30 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
6368 client : Pick < OpenAI , 'apiKey' | 'baseURL' | '_callApiKey' > ,
6469 props : { model : string ; options ?: WS . ClientOptions | undefined } ,
6570 ) : Promise < OpenAIRealtimeWS > {
66- await client . _callApiKey ( ) ;
67- return new OpenAIRealtimeWS ( props , client ) ;
71+ return new OpenAIRealtimeWS ( { ...props , __resolvedApiKey : await client . _callApiKey ( ) } , client ) ;
6872 }
6973
7074 static async azure (
7175 client : Pick < AzureOpenAI , '_callApiKey' | 'apiVersion' | 'apiKey' | 'baseURL' | 'deploymentName' > ,
72- options : { deploymentName ?: string ; options ?: WS . ClientOptions | undefined } = { } ,
76+ props : { deploymentName ?: string ; options ?: WS . ClientOptions | undefined } = { } ,
7377 ) : Promise < OpenAIRealtimeWS > {
74- const deploymentName = options . deploymentName ?? client . deploymentName ;
78+ const isToken = await client . _callApiKey ( ) ;
79+ const deploymentName = props . deploymentName ?? client . deploymentName ;
7580 if ( ! deploymentName ) {
7681 throw new Error ( 'No deployment name provided' ) ;
7782 }
7883 return new OpenAIRealtimeWS (
79- { model : deploymentName , options : { headers : await getAzureHeaders ( client ) } } ,
84+ {
85+ model : deploymentName ,
86+ options : {
87+ ...props . options ,
88+ headers : {
89+ ...props . options ?. headers ,
90+ ...( isToken ? { } : { 'api-key' : client . apiKey } ) ,
91+ } ,
92+ } ,
93+ __resolvedApiKey : isToken ,
94+ } ,
8095 client ,
8196 ) ;
8297 }
@@ -98,11 +113,4 @@ export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
98113 }
99114}
100115
101- async function getAzureHeaders ( client : Pick < AzureOpenAI , '_callApiKey' | 'apiKey' > ) {
102- const isToken = await client . _callApiKey ( ) ;
103- if ( isToken ) {
104- return { Authorization : `Bearer ${ isToken } ` } ;
105- } else {
106- return { 'api-key' : client . apiKey } ;
107- }
108- }
116+ // getAzureHeaders inlined into azure()
0 commit comments