@livekit/agents-plugin-openai 1.5.2 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/realtime/realtime_model.cjs +197 -159
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +4 -0
- package/dist/realtime/realtime_model.d.ts +4 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +196 -159
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +176 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +177 -0
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/dist/ws/llm.cjs +5 -2
- package/dist/ws/llm.cjs.map +1 -1
- package/dist/ws/llm.d.cts +4 -5
- package/dist/ws/llm.d.ts +4 -5
- package/dist/ws/llm.d.ts.map +1 -1
- package/dist/ws/llm.js +5 -2
- package/dist/ws/llm.js.map +1 -1
- package/dist/ws/llm.test.cjs +22 -2
- package/dist/ws/llm.test.cjs.map +1 -1
- package/dist/ws/llm.test.js +22 -2
- package/dist/ws/llm.test.js.map +1 -1
- package/package.json +5 -5
- package/src/realtime/realtime_model.test.ts +210 -0
- package/src/realtime/realtime_model.ts +207 -163
- package/src/ws/llm.test.ts +28 -2
- package/src/ws/llm.ts +12 -8
|
@@ -86,6 +86,22 @@ class DiscardedGeneration {
|
|
|
86
86
|
readonly discarded = true;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
// These server-side errors cannot be fixed by reconnecting; retrying only loops forever.
|
|
90
|
+
const FATAL_ERROR_CODES = new Set([
|
|
91
|
+
'insufficient_quota',
|
|
92
|
+
'invalid_api_key',
|
|
93
|
+
'account_deactivated',
|
|
94
|
+
'billing_hard_limit_reached',
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
/** @internal Exported for testing purposes */
|
|
98
|
+
export function isFatalError(error: unknown): boolean {
|
|
99
|
+
if (error === null || typeof error !== 'object') return false;
|
|
100
|
+
const { code, type } = error as { code?: unknown; type?: unknown };
|
|
101
|
+
const errorCode = typeof code === 'string' && code.length > 0 ? code : type;
|
|
102
|
+
return typeof errorCode === 'string' && FATAL_ERROR_CODES.has(errorCode);
|
|
103
|
+
}
|
|
104
|
+
|
|
89
105
|
class CreateResponseHandle {
|
|
90
106
|
instructions?: string;
|
|
91
107
|
doneFut: Future<llm.GenerationCreatedEvent>;
|
|
@@ -969,6 +985,41 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
969
985
|
return untypedEvent;
|
|
970
986
|
}
|
|
971
987
|
|
|
988
|
+
private closeCurrentGeneration(reason: string): void {
|
|
989
|
+
if (this.currentGeneration instanceof DiscardedGeneration) {
|
|
990
|
+
this.currentGeneration = undefined;
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
if (!this.currentGeneration) return;
|
|
995
|
+
|
|
996
|
+
for (const gen of this.currentGeneration.messages.values()) {
|
|
997
|
+
gen.textChannel.close();
|
|
998
|
+
gen.audioChannel.close();
|
|
999
|
+
if (!gen.modalities.done) {
|
|
1000
|
+
gen.modalities.resolve(this._options.modalities);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
this.currentGeneration.messages.clear();
|
|
1004
|
+
this.currentGeneration.messageChannel.close();
|
|
1005
|
+
this.currentGeneration.functionChannel.close();
|
|
1006
|
+
if (!this.currentGeneration._doneFut.done) {
|
|
1007
|
+
this.currentGeneration._doneFut.resolve();
|
|
1008
|
+
}
|
|
1009
|
+
this.#logger.warn(`In-progress generation discarded due to ${reason}`);
|
|
1010
|
+
this.currentGeneration = undefined;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
private rejectResponseCreatedFutures(reason: string): void {
|
|
1014
|
+
for (const handle of Object.values(this.responseCreatedFutures)) {
|
|
1015
|
+
if (!handle.doneFut.done) {
|
|
1016
|
+
handle.doneFut.reject(new Error(reason));
|
|
1017
|
+
}
|
|
1018
|
+
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1019
|
+
}
|
|
1020
|
+
this.responseCreatedFutures = {};
|
|
1021
|
+
}
|
|
1022
|
+
|
|
972
1023
|
private async createWsConn(): Promise<WebSocket> {
|
|
973
1024
|
const headers: Record<string, string> = {
|
|
974
1025
|
'User-Agent': 'LiveKit-Agents-JS',
|
|
@@ -1056,14 +1107,9 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1056
1107
|
}
|
|
1057
1108
|
this.itemDeleteFutures = {};
|
|
1058
1109
|
|
|
1059
|
-
|
|
1060
|
-
if (!handle.doneFut.done) {
|
|
1061
|
-
handle.doneFut.reject(new Error('Session reconnected'));
|
|
1062
|
-
}
|
|
1063
|
-
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1064
|
-
}
|
|
1065
|
-
this.responseCreatedFutures = {};
|
|
1110
|
+
this.rejectResponseCreatedFutures('Session reconnected');
|
|
1066
1111
|
this.discardedEventIds.clear();
|
|
1112
|
+
this.closeCurrentGeneration('session reconnection');
|
|
1067
1113
|
|
|
1068
1114
|
// Clear audio-capable item tracking - restored items are text-only on the server
|
|
1069
1115
|
this.audioCapableItemIds.clear();
|
|
@@ -1111,61 +1157,66 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1111
1157
|
};
|
|
1112
1158
|
|
|
1113
1159
|
reconnecting = false;
|
|
1114
|
-
|
|
1115
|
-
this.#
|
|
1116
|
-
|
|
1117
|
-
|
|
1160
|
+
try {
|
|
1161
|
+
while (!this.#closed && !signal.aborted) {
|
|
1162
|
+
this.#logger.debug('Creating WebSocket connection to OpenAI Realtime API');
|
|
1163
|
+
wsConn = await this.createWsConn();
|
|
1164
|
+
if (signal.aborted) break;
|
|
1118
1165
|
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1166
|
+
try {
|
|
1167
|
+
if (reconnecting) {
|
|
1168
|
+
await reconnect();
|
|
1169
|
+
if (signal.aborted) break;
|
|
1170
|
+
numRetries = 0;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
await this.runWs(wsConn);
|
|
1122
1174
|
if (signal.aborted) break;
|
|
1123
|
-
|
|
1124
|
-
|
|
1175
|
+
} catch (error) {
|
|
1176
|
+
if (!isAPIError(error)) {
|
|
1177
|
+
this.emitError({ error: error as Error, recoverable: false });
|
|
1178
|
+
throw error;
|
|
1179
|
+
}
|
|
1125
1180
|
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
this.emitError({ error: error as Error, recoverable: false });
|
|
1131
|
-
throw error;
|
|
1132
|
-
}
|
|
1181
|
+
if (maxRetries === 0 || !error.retryable) {
|
|
1182
|
+
this.emitError({ error: error as Error, recoverable: false });
|
|
1183
|
+
throw error;
|
|
1184
|
+
}
|
|
1133
1185
|
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1186
|
+
if (numRetries === maxRetries) {
|
|
1187
|
+
this.emitError({ error: error as Error, recoverable: false });
|
|
1188
|
+
throw new APIConnectionError({
|
|
1189
|
+
message: `OpenAI Realtime API connection failed after ${numRetries} attempts`,
|
|
1190
|
+
options: {
|
|
1191
|
+
body: error,
|
|
1192
|
+
retryable: false,
|
|
1193
|
+
},
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1138
1196
|
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1197
|
+
this.emitError({ error: error as Error, recoverable: true });
|
|
1198
|
+
const retryInterval =
|
|
1199
|
+
numRetries === 0
|
|
1200
|
+
? DEFAULT_FIRST_RETRY_INTERVAL_MS
|
|
1201
|
+
: this._options.connOptions.retryIntervalMs;
|
|
1202
|
+
this.#logger.warn(
|
|
1203
|
+
{
|
|
1204
|
+
attempt: numRetries,
|
|
1205
|
+
maxRetries,
|
|
1206
|
+
error,
|
|
1146
1207
|
},
|
|
1147
|
-
|
|
1148
|
-
|
|
1208
|
+
`OpenAI Realtime API connection failed, retrying in ${retryInterval / 1000}s`,
|
|
1209
|
+
);
|
|
1149
1210
|
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
? DEFAULT_FIRST_RETRY_INTERVAL_MS
|
|
1154
|
-
: this._options.connOptions.retryIntervalMs;
|
|
1155
|
-
this.#logger.warn(
|
|
1156
|
-
{
|
|
1157
|
-
attempt: numRetries,
|
|
1158
|
-
maxRetries,
|
|
1159
|
-
error,
|
|
1160
|
-
},
|
|
1161
|
-
`OpenAI Realtime API connection failed, retrying in ${retryInterval / 1000}s`,
|
|
1162
|
-
);
|
|
1211
|
+
await delay(retryInterval);
|
|
1212
|
+
numRetries++;
|
|
1213
|
+
}
|
|
1163
1214
|
|
|
1164
|
-
|
|
1165
|
-
numRetries++;
|
|
1215
|
+
reconnecting = true;
|
|
1166
1216
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1217
|
+
} finally {
|
|
1218
|
+
this.closeCurrentGeneration('session close');
|
|
1219
|
+
this.rejectResponseCreatedFutures('Realtime session closed');
|
|
1169
1220
|
}
|
|
1170
1221
|
}
|
|
1171
1222
|
|
|
@@ -1216,79 +1267,88 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1216
1267
|
this.#logger.debug(this.loggableEvent(event), `(server) <- ${event.type}`);
|
|
1217
1268
|
}
|
|
1218
1269
|
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1270
|
+
try {
|
|
1271
|
+
switch (event.type) {
|
|
1272
|
+
case 'input_audio_buffer.speech_started':
|
|
1273
|
+
this.handleInputAudioBufferSpeechStarted(event);
|
|
1274
|
+
break;
|
|
1275
|
+
case 'input_audio_buffer.speech_stopped':
|
|
1276
|
+
this.handleInputAudioBufferSpeechStopped(event);
|
|
1277
|
+
break;
|
|
1278
|
+
case 'response.created':
|
|
1279
|
+
this.handleResponseCreated(event);
|
|
1280
|
+
break;
|
|
1281
|
+
case 'response.output_item.added':
|
|
1282
|
+
this.handleResponseOutputItemAdded(event);
|
|
1283
|
+
break;
|
|
1284
|
+
case 'conversation.item.added':
|
|
1285
|
+
case 'conversation.item.created': // Beta: kept for backward compatibility
|
|
1286
|
+
this.handleConversationItemCreated(event);
|
|
1287
|
+
break;
|
|
1288
|
+
case 'conversation.item.deleted':
|
|
1289
|
+
this.handleConversationItemDeleted(event);
|
|
1290
|
+
break;
|
|
1291
|
+
case 'conversation.item.input_audio_transcription.delta':
|
|
1292
|
+
this.handleConversationItemInputAudioTranscriptionDelta(event);
|
|
1293
|
+
break;
|
|
1294
|
+
case 'conversation.item.input_audio_transcription.completed':
|
|
1295
|
+
this.handleConversationItemInputAudioTranscriptionCompleted(event);
|
|
1296
|
+
break;
|
|
1297
|
+
case 'conversation.item.input_audio_transcription.failed':
|
|
1298
|
+
this.handleConversationItemInputAudioTranscriptionFailed(event);
|
|
1299
|
+
break;
|
|
1300
|
+
case 'response.content_part.added':
|
|
1301
|
+
this.handleResponseContentPartAdded(event);
|
|
1302
|
+
break;
|
|
1303
|
+
case 'response.content_part.done':
|
|
1304
|
+
this.handleResponseContentPartDone(event);
|
|
1305
|
+
break;
|
|
1306
|
+
case 'response.output_text.delta':
|
|
1307
|
+
case 'response.text.delta': // Beta: kept for backward compatibility
|
|
1308
|
+
this.handleResponseTextDelta(event);
|
|
1309
|
+
break;
|
|
1310
|
+
case 'response.output_text.done':
|
|
1311
|
+
case 'response.text.done': // Beta: kept for backward compatibility
|
|
1312
|
+
this.handleResponseTextDone(event);
|
|
1313
|
+
break;
|
|
1314
|
+
case 'response.output_audio_transcript.delta':
|
|
1315
|
+
case 'response.audio_transcript.delta': // Beta: kept for backward compatibility
|
|
1316
|
+
this.handleResponseAudioTranscriptDelta(event);
|
|
1317
|
+
break;
|
|
1318
|
+
case 'response.output_audio.delta':
|
|
1319
|
+
case 'response.audio.delta': // Beta: kept for backward compatibility
|
|
1320
|
+
this.handleResponseAudioDelta(event);
|
|
1321
|
+
break;
|
|
1322
|
+
case 'response.output_audio_transcript.done':
|
|
1323
|
+
case 'response.audio_transcript.done': // Beta: kept for backward compatibility
|
|
1324
|
+
this.handleResponseAudioTranscriptDone(event);
|
|
1325
|
+
break;
|
|
1326
|
+
case 'response.output_audio.done':
|
|
1327
|
+
case 'response.audio.done': // Beta: kept for backward compatibility
|
|
1328
|
+
this.handleResponseAudioDone(event);
|
|
1329
|
+
break;
|
|
1330
|
+
case 'response.output_item.done':
|
|
1331
|
+
this.handleResponseOutputItemDone(event);
|
|
1332
|
+
break;
|
|
1333
|
+
case 'response.done':
|
|
1334
|
+
this.handleResponseDone(event);
|
|
1335
|
+
break;
|
|
1336
|
+
case 'error':
|
|
1337
|
+
this.handleError(event);
|
|
1338
|
+
break;
|
|
1339
|
+
default:
|
|
1340
|
+
if (lkOaiDebug) {
|
|
1341
|
+
this.#logger.debug(`unhandled event: ${event.type}`);
|
|
1342
|
+
}
|
|
1343
|
+
break;
|
|
1344
|
+
}
|
|
1345
|
+
} catch (error) {
|
|
1346
|
+
if (isAPIError(error) && !error.retryable) {
|
|
1347
|
+
wsCloseFuture.resolve(error);
|
|
1348
|
+
wsConn.close();
|
|
1349
|
+
return;
|
|
1350
|
+
}
|
|
1351
|
+
this.#logger.error({ error, event }, 'Failed to handle OpenAI Realtime API event');
|
|
1292
1352
|
}
|
|
1293
1353
|
};
|
|
1294
1354
|
|
|
@@ -1338,13 +1398,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1338
1398
|
await this.#task;
|
|
1339
1399
|
|
|
1340
1400
|
// Clean up pending futures to prevent memory leaks
|
|
1341
|
-
|
|
1342
|
-
if (!handle.doneFut.done) {
|
|
1343
|
-
handle.doneFut.reject(new Error('Session closed'));
|
|
1344
|
-
}
|
|
1345
|
-
if (handle.timeout) clearTimeout(handle.timeout);
|
|
1346
|
-
}
|
|
1347
|
-
this.responseCreatedFutures = {};
|
|
1401
|
+
this.rejectResponseCreatedFutures('Session closed');
|
|
1348
1402
|
|
|
1349
1403
|
for (const fut of Object.values(this.itemCreateFutures)) {
|
|
1350
1404
|
if (!fut.done) {
|
|
@@ -1363,24 +1417,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1363
1417
|
this.inputTranscriptAccumulators.clear();
|
|
1364
1418
|
|
|
1365
1419
|
// Clean up current generation if exists
|
|
1366
|
-
|
|
1367
|
-
this.currentGeneration = undefined;
|
|
1368
|
-
} else if (this.currentGeneration) {
|
|
1369
|
-
for (const gen of this.currentGeneration.messages.values()) {
|
|
1370
|
-
gen.textChannel.close();
|
|
1371
|
-
gen.audioChannel.close();
|
|
1372
|
-
if (!gen.modalities.done) {
|
|
1373
|
-
gen.modalities.resolve(this._options.modalities);
|
|
1374
|
-
}
|
|
1375
|
-
}
|
|
1376
|
-
this.currentGeneration.messages.clear();
|
|
1377
|
-
this.currentGeneration.messageChannel.close();
|
|
1378
|
-
this.currentGeneration.functionChannel.close();
|
|
1379
|
-
if (!this.currentGeneration._doneFut.done) {
|
|
1380
|
-
this.currentGeneration._doneFut.resolve();
|
|
1381
|
-
}
|
|
1382
|
-
this.currentGeneration = undefined;
|
|
1383
|
-
}
|
|
1420
|
+
this.closeCurrentGeneration('Session closed');
|
|
1384
1421
|
|
|
1385
1422
|
// Clear the message queue
|
|
1386
1423
|
this.messageChannel.items.length = 0;
|
|
@@ -1924,13 +1961,18 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1924
1961
|
const errorTypeValue = errorBody && 'type' in errorBody ? errorBody.type : undefined;
|
|
1925
1962
|
const errorType = typeof errorTypeValue === 'string' ? errorTypeValue : 'unknown';
|
|
1926
1963
|
|
|
1927
|
-
|
|
1928
|
-
|
|
1964
|
+
const recoverable = !isFatalError(errorBody);
|
|
1965
|
+
const error = new APIError(
|
|
1966
|
+
`OpenAI Realtime API response failed with error type: ${errorType}`,
|
|
1967
|
+
{
|
|
1929
1968
|
body: errorBody ?? null,
|
|
1930
|
-
retryable:
|
|
1931
|
-
}
|
|
1932
|
-
|
|
1933
|
-
|
|
1969
|
+
retryable: recoverable,
|
|
1970
|
+
},
|
|
1971
|
+
);
|
|
1972
|
+
if (!recoverable) {
|
|
1973
|
+
throw error;
|
|
1974
|
+
}
|
|
1975
|
+
this.emitError({ error, recoverable: true });
|
|
1934
1976
|
} else if (event.response.status === 'cancelled' || event.response.status === 'incomplete') {
|
|
1935
1977
|
let statusType: string | undefined;
|
|
1936
1978
|
let statusReason: string | undefined;
|
|
@@ -1974,13 +2016,15 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
1974
2016
|
}
|
|
1975
2017
|
}
|
|
1976
2018
|
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
}),
|
|
1982
|
-
recoverable: true,
|
|
2019
|
+
const recoverable = !isFatalError(event.error);
|
|
2020
|
+
const error = new APIError(event.error.message, {
|
|
2021
|
+
body: event.error,
|
|
2022
|
+
retryable: recoverable,
|
|
1983
2023
|
});
|
|
2024
|
+
if (!recoverable) {
|
|
2025
|
+
throw error;
|
|
2026
|
+
}
|
|
2027
|
+
this.emitError({ error, recoverable: true });
|
|
1984
2028
|
}
|
|
1985
2029
|
|
|
1986
2030
|
private emitError({ error, recoverable }: { error: Error; recoverable: boolean }): void {
|
package/src/ws/llm.test.ts
CHANGED
|
@@ -12,13 +12,13 @@ initializeLogger({ level: 'silent', pretty: false });
|
|
|
12
12
|
const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
|
|
13
13
|
|
|
14
14
|
describe('buildResponsesWsUrl', () => {
|
|
15
|
-
it('points at the OpenAI Responses WS endpoint
|
|
15
|
+
it('points at the OpenAI Responses WS endpoint without model when no baseURL is set', () => {
|
|
16
16
|
const url = new URL(buildResponsesWsUrl(undefined, 'gpt-4.1'));
|
|
17
17
|
|
|
18
18
|
expect(url.protocol).toBe('wss:');
|
|
19
19
|
expect(url.host).toBe('api.openai.com');
|
|
20
20
|
expect(url.pathname).toBe('/v1/responses');
|
|
21
|
-
expect(url.searchParams.get('model')).toBe(
|
|
21
|
+
expect(url.searchParams.get('model')).toBe(null);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('rewrites https baseURL to wss and appends /responses with the model', () => {
|
|
@@ -30,6 +30,32 @@ describe('buildResponsesWsUrl', () => {
|
|
|
30
30
|
expect(url.searchParams.get('model')).toBe('gpt-4o');
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
+
it('rewrites full https Responses endpoint to wss without duplicating /responses', () => {
|
|
34
|
+
const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1/responses', 'gpt-4o'));
|
|
35
|
+
|
|
36
|
+
expect(url.protocol).toBe('wss:');
|
|
37
|
+
expect(url.host).toBe('gateway.example.com');
|
|
38
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
39
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('uses full wss Responses endpoint with the model', () => {
|
|
43
|
+
const url = new URL(buildResponsesWsUrl('wss://gateway.example.com/v1/responses', 'gpt-4o'));
|
|
44
|
+
|
|
45
|
+
expect(url.protocol).toBe('wss:');
|
|
46
|
+
expect(url.host).toBe('gateway.example.com');
|
|
47
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
48
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('omits the model when an explicit baseURL still points at api.openai.com', () => {
|
|
52
|
+
const url = new URL(buildResponsesWsUrl('https://api.openai.com/v1', 'gpt-4.1'));
|
|
53
|
+
|
|
54
|
+
expect(url.host).toBe('api.openai.com');
|
|
55
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
56
|
+
expect(url.searchParams.get('model')).toBe(null);
|
|
57
|
+
});
|
|
58
|
+
|
|
33
59
|
it('strips a trailing slash on baseURL before appending /responses', () => {
|
|
34
60
|
const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1/', 'gpt-4o-mini'));
|
|
35
61
|
|
package/src/ws/llm.ts
CHANGED
|
@@ -36,12 +36,11 @@ const WS_MAX_SESSION_DURATION = 3_600_000;
|
|
|
36
36
|
/**
|
|
37
37
|
* Build the Responses-API WebSocket URL.
|
|
38
38
|
*
|
|
39
|
-
* Includes the model on the upgrade URL
|
|
39
|
+
* Includes the model on the upgrade URL for OpenAI-compatible gateways
|
|
40
40
|
* (which can only see the URL at the WebSocket upgrade, not the subsequent
|
|
41
|
-
* `response.create` frame) can route by model.
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* parameter, so this is a no-op for direct connections.
|
|
41
|
+
* `response.create` frame) so they can route by model. OpenAI's native
|
|
42
|
+
* endpoint gets the model in the `response.create` frame, so the query
|
|
43
|
+
* parameter is intentionally omitted for direct connections.
|
|
45
44
|
*
|
|
46
45
|
* The scheme of `baseURL` is respected: `http://` maps to `ws://`
|
|
47
46
|
* and `https://` maps to `wss://`.
|
|
@@ -49,11 +48,16 @@ const WS_MAX_SESSION_DURATION = 3_600_000;
|
|
|
49
48
|
* @internal
|
|
50
49
|
*/
|
|
51
50
|
export function buildResponsesWsUrl(baseURL: string | undefined, model: string): string {
|
|
52
|
-
const
|
|
53
|
-
|
|
51
|
+
const normalizedBaseURL = baseURL?.replace(/^http(s?):/, 'ws$1:').replace(/\/+$/, '');
|
|
52
|
+
const base = normalizedBaseURL
|
|
53
|
+
? normalizedBaseURL.endsWith('/responses')
|
|
54
|
+
? normalizedBaseURL
|
|
55
|
+
: `${normalizedBaseURL}/responses`
|
|
54
56
|
: OPENAI_RESPONSES_WS_URL;
|
|
55
57
|
const url = new URL(base);
|
|
56
|
-
url.
|
|
58
|
+
if (url.hostname !== 'api.openai.com') {
|
|
59
|
+
url.searchParams.set('model', model);
|
|
60
|
+
}
|
|
57
61
|
return url.toString();
|
|
58
62
|
}
|
|
59
63
|
|