@@ -4,15 +4,16 @@ import { RealtimeUtils } from './utils.js';
44export class RealtimeAPI extends RealtimeEventHandler {
55 /**
66 * Create a new RealtimeAPI instance
7- * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean} } [settings]
7+ * @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean, model?: string } } [settings]
88 * @returns {RealtimeAPI }
99 */
10- constructor ( { url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = { } ) {
10+ constructor ( { url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug, model } = { } ) {
1111 super ( ) ;
1212 this . defaultUrl = 'wss://api.openai.com/v1/realtime' ;
1313 this . url = url || this . defaultUrl ;
1414 this . apiKey = apiKey || null ;
1515 this . debug = ! ! debug ;
16+ this . model = model || 'gpt-4o-realtime-preview' ;
1617 this . ws = null ;
1718 if ( globalThis . document && this . apiKey ) {
1819 if ( ! dangerouslyAllowAPIKeyInBrowser ) {
@@ -56,13 +57,15 @@ export class RealtimeAPI extends RealtimeEventHandler {
5657 * @param {{model?: string} } [settings]
5758 * @returns {Promise<true> }
5859 */
59- async connect ( { model } = { model : 'gpt-4o-realtime-preview-2024-10-01' } ) {
60+ async connect ( { model } = { } ) {
6061 if ( ! this . apiKey && this . url === this . defaultUrl ) {
6162 console . warn ( `No apiKey provided for connection to "${ this . url } "` ) ;
6263 }
6364 if ( this . isConnected ( ) ) {
6465 throw new Error ( `Already connected` ) ;
6566 }
67+ const useModel = model || this . model ;
68+
6669 if ( globalThis . WebSocket ) {
6770 /**
6871 * Web browser
@@ -73,7 +76,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
7376 ) ;
7477 }
7578 const WebSocket = globalThis . WebSocket ;
76- const ws = new WebSocket ( `${ this . url } ${ model ? `?model=${ model } ` : '' } ` , [
79+ const ws = new WebSocket ( `${ this . url } ${ useModel ? `?model=${ useModel } ` : '' } ` , [
7780 'realtime' ,
7881 `openai-insecure-api-key.${ this . apiKey } ` ,
7982 'openai-beta.realtime-v1' ,
@@ -113,7 +116,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
113116 const wsModule = await import ( /* webpackIgnore: true */ moduleName ) ;
114117 const WebSocket = wsModule . default ;
115118 const ws = new WebSocket (
116- 'wss://api.openai.com/v1/realtime? model=gpt-4o-realtime-preview-2024-10-01' ,
119+ ` ${ this . url } ${ useModel ? `? model=${ useModel } ` : '' } ` ,
117120 [ ] ,
118121 {
119122 finishRequest : ( request ) => {
0 commit comments