Skip to content

Commit de2f722

Browse files
committed
Add ability to set realtime model
# Conflicts: # dist/lib/client.d.ts.map
1 parent 2e27289 commit de2f722

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

dist/lib/api.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
export class RealtimeAPI extends RealtimeEventHandler {
22
/**
33
* Create a new RealtimeAPI instance
4-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
4+
* @param {{url?: string, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
55
* @returns {RealtimeAPI}
66
*/
7-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
7+
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug }?: {
88
url?: string;
9+
model?: string;
910
apiKey?: string;
1011
dangerouslyAllowAPIKeyInBrowser?: boolean;
1112
debug?: boolean;
1213
});
1314
defaultUrl: string;
15+
defaultModel: string;
1416
url: string;
1517
apiKey: string;
18+
model: string;
1619
debug: boolean;
1720
ws: any;
1821
/**

dist/lib/api.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/client.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@
163163
export class RealtimeClient extends RealtimeEventHandler {
164164
/**
165165
* Create a new RealtimeClient instance
166-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
166+
* @param {{url?: string, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
167167
*/
168-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug }?: {
168+
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug }?: {
169169
url?: string;
170+
model?: string;
170171
apiKey?: string;
171172
dangerouslyAllowAPIKeyInBrowser?: boolean;
172173
debug?: boolean;
@@ -226,7 +227,7 @@ export class RealtimeClient extends RealtimeEventHandler {
226227
* Updates session config and conversation config
227228
* @returns {Promise<true>}
228229
*/
229-
connect(): Promise<true>;
230+
connect({ model }?: {}): Promise<true>;
230231
/**
231232
* Waits for a session.created event to be executed before proceeding
232233
* @returns {Promise<true>}

dist/lib/client.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { RealtimeUtils } from './utils.js';
44
export 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, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
88
* @returns {RealtimeAPI}
99
*/
10-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
10+
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
1111
super();
1212
this.defaultUrl = 'wss://api.openai.com/v1/realtime';
13+
this.defaultModel = 'gpt-4o-realtime-preview-2024-10-01';
1314
this.url = url || this.defaultUrl;
1415
this.apiKey = apiKey || null;
16+
this.model = model || this.defaultModel;
1517
this.debug = !!debug;
1618
this.ws = null;
1719
if (globalThis.document && this.apiKey) {
@@ -56,7 +58,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
5658
* @param {{model?: string}} [settings]
5759
* @returns {Promise<true>}
5860
*/
59-
async connect({ model } = { model: 'gpt-4o-realtime-preview-2024-10-01' }) {
61+
async connect({ model = this.model } = {}) {
6062
if (!this.apiKey && this.url === this.defaultUrl) {
6163
console.warn(`No apiKey provided for connection to "${this.url}"`);
6264
}
@@ -73,7 +75,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
7375
);
7476
}
7577
const WebSocket = globalThis.WebSocket;
76-
const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
78+
const ws = new WebSocket(`${this.url}?model=${model}`, [
7779
'realtime',
7880
`openai-insecure-api-key.${this.apiKey}`,
7981
'openai-beta.realtime-v1',
@@ -113,7 +115,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
113115
const wsModule = await import(/* webpackIgnore: true */ moduleName);
114116
const WebSocket = wsModule.default;
115117
const ws = new WebSocket(
116-
'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01',
118+
`wss://api.openai.com/v1/realtime?model=${model}`,
117119
[],
118120
{
119121
finishRequest: (request) => {

lib/client.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ import { RealtimeUtils } from './utils.js';
192192
export class RealtimeClient extends RealtimeEventHandler {
193193
/**
194194
* Create a new RealtimeClient instance
195-
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
195+
* @param {{url?: string, model?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings]
196196
*/
197-
constructor({ url, apiKey, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
197+
constructor({ url, apiKey, model, dangerouslyAllowAPIKeyInBrowser, debug } = {}) {
198198
super();
199199
this.defaultSessionConfig = {
200200
modalities: ['text', 'audio'],
@@ -224,6 +224,7 @@ export class RealtimeClient extends RealtimeEventHandler {
224224
this.realtime = new RealtimeAPI({
225225
url,
226226
apiKey,
227+
model,
227228
dangerouslyAllowAPIKeyInBrowser,
228229
debug,
229230
});
@@ -392,11 +393,11 @@ export class RealtimeClient extends RealtimeEventHandler {
392393
* Updates session config and conversation config
393394
* @returns {Promise<true>}
394395
*/
395-
async connect() {
396+
async connect({ model } = {}) {
396397
if (this.isConnected()) {
397398
throw new Error(`Already connected, use .disconnect() first`);
398399
}
399-
await this.realtime.connect();
400+
await this.realtime.connect({ model });
400401
this.updateSession();
401402
return true;
402403
}

0 commit comments

Comments
 (0)