@livekit/agents-plugin-openai 1.8.1 → 1.9.1
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/gpt_live_model.cjs +34 -34
- package/dist/realtime/gpt_live_model.cjs.map +1 -1
- package/dist/realtime/gpt_live_model.d.cts +3 -2
- package/dist/realtime/gpt_live_model.d.ts +3 -2
- package/dist/realtime/gpt_live_model.d.ts.map +1 -1
- package/dist/realtime/gpt_live_model.js +34 -34
- package/dist/realtime/gpt_live_model.js.map +1 -1
- package/dist/realtime/gpt_live_model.test.cjs +39 -0
- package/dist/realtime/gpt_live_model.test.cjs.map +1 -1
- package/dist/realtime/gpt_live_model.test.js +39 -0
- package/dist/realtime/gpt_live_model.test.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +17 -8
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +17 -8
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +11 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +11 -0
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/package.json +7 -7
- package/src/realtime/gpt_live_model.test.ts +51 -0
- package/src/realtime/gpt_live_model.ts +48 -40
- package/src/realtime/realtime_model.test.ts +12 -0
- package/src/realtime/realtime_model.ts +20 -10
|
@@ -171,11 +171,6 @@ interface Speech {
|
|
|
171
171
|
startedAt: number;
|
|
172
172
|
quietMs: number;
|
|
173
173
|
}
|
|
174
|
-
interface DelegatedResponse {
|
|
175
|
-
callIds: Set<string>;
|
|
176
|
-
returned: Set<string>;
|
|
177
|
-
completed: boolean;
|
|
178
|
-
}
|
|
179
174
|
|
|
180
175
|
/**
|
|
181
176
|
* GPT-Live WebSocket session, accessible through Agent.duplexSession.
|
|
@@ -194,8 +189,12 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
194
189
|
private readonly debug = Number(process.env.LK_OPENAI_DEBUG ?? 0) !== 0;
|
|
195
190
|
private readonly history = llm.ChatContext.empty();
|
|
196
191
|
private readonly speech = new Map<Role, Speech>();
|
|
197
|
-
|
|
198
|
-
|
|
192
|
+
// delegation id -> call ids of its response that has not completed yet; on completion the
|
|
193
|
+
// call ids move to the open set, and stay there until each output is sent to the backend.
|
|
194
|
+
// backendResponsePending is set once an output is sent and cleared by the response.create
|
|
195
|
+
private readonly backendRunningResponses = new Map<string | null, Set<string>>();
|
|
196
|
+
private readonly backendOpenCalls = new Set<string>();
|
|
197
|
+
private backendResponsePending = false;
|
|
199
198
|
private readonly delegationIds = new Set<string>();
|
|
200
199
|
private readonly bstream = new AudioByteStream(SAMPLE_RATE, 1, SAMPLE_RATE / 10);
|
|
201
200
|
private inputResampler?: AudioResampler;
|
|
@@ -385,8 +384,9 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
385
384
|
this.resetInputAudio();
|
|
386
385
|
this.endSpeech('user');
|
|
387
386
|
this.speech.clear();
|
|
388
|
-
this.
|
|
389
|
-
this.
|
|
387
|
+
this.backendRunningResponses.clear();
|
|
388
|
+
this.backendOpenCalls.clear();
|
|
389
|
+
this.backendResponsePending = false;
|
|
390
390
|
this.delegationIds.clear();
|
|
391
391
|
this.usageSeconds = 0;
|
|
392
392
|
this._sessionId = undefined;
|
|
@@ -674,11 +674,7 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
674
674
|
private handleResponseEvent(delegationId: string | null, event: ResponsesEvent): void {
|
|
675
675
|
switch (event.type) {
|
|
676
676
|
case 'response.created':
|
|
677
|
-
this.
|
|
678
|
-
callIds: new Set(),
|
|
679
|
-
returned: new Set(),
|
|
680
|
-
completed: false,
|
|
681
|
-
});
|
|
677
|
+
this.backendRunningResponses.set(delegationId, new Set());
|
|
682
678
|
break;
|
|
683
679
|
case 'response.output_item.done': {
|
|
684
680
|
const item = event.item;
|
|
@@ -690,17 +686,15 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
690
686
|
);
|
|
691
687
|
return;
|
|
692
688
|
}
|
|
693
|
-
let
|
|
694
|
-
if (!
|
|
689
|
+
let calls = this.backendRunningResponses.get(delegationId);
|
|
690
|
+
if (!calls) {
|
|
695
691
|
this.logger.warn(
|
|
696
692
|
{ 'lk.pii.call_id': item.call_id, 'lk.pii.delegation_id': delegationId },
|
|
697
693
|
'GPT-Live function call outside a known response',
|
|
698
694
|
);
|
|
699
|
-
|
|
700
|
-
this.delegatedResponses.set(delegationId, pending);
|
|
695
|
+
calls = this.backendOpenCalls;
|
|
701
696
|
}
|
|
702
|
-
|
|
703
|
-
this.callToDelegation.set(item.call_id, delegationId);
|
|
697
|
+
calls.add(item.call_id);
|
|
704
698
|
const call = new llm.FunctionCall({
|
|
705
699
|
id: item.id ?? shortuuid('fc_'),
|
|
706
700
|
callId: item.call_id,
|
|
@@ -713,11 +707,12 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
713
707
|
}
|
|
714
708
|
case 'response.completed': {
|
|
715
709
|
this.handleResponseUsage(event.response);
|
|
716
|
-
const
|
|
717
|
-
if (
|
|
718
|
-
|
|
719
|
-
this.
|
|
710
|
+
const calls = this.backendRunningResponses.get(delegationId);
|
|
711
|
+
if (calls) {
|
|
712
|
+
this.backendRunningResponses.delete(delegationId);
|
|
713
|
+
for (const callId of calls) this.backendOpenCalls.add(callId);
|
|
720
714
|
}
|
|
715
|
+
this.maybeContinueResponse();
|
|
721
716
|
break;
|
|
722
717
|
}
|
|
723
718
|
case 'response.failed':
|
|
@@ -732,9 +727,10 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
732
727
|
},
|
|
733
728
|
'GPT-Live backend response did not complete',
|
|
734
729
|
);
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
this.
|
|
730
|
+
// the service discards a failed response's calls: an output for one is refused, so it
|
|
731
|
+
// goes to the voice model as context instead
|
|
732
|
+
this.backendRunningResponses.delete(delegationId);
|
|
733
|
+
this.maybeContinueResponse();
|
|
738
734
|
break;
|
|
739
735
|
}
|
|
740
736
|
}
|
|
@@ -743,7 +739,7 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
743
739
|
private handleResponseUsage(response: ResponsesEvent['response']): void {
|
|
744
740
|
const usage = response?.usage;
|
|
745
741
|
if (usage) {
|
|
746
|
-
const metric: metrics.LLMMetrics
|
|
742
|
+
const metric: metrics.LLMMetrics = {
|
|
747
743
|
type: 'llm_metrics',
|
|
748
744
|
label: this.duplexModel.label(),
|
|
749
745
|
requestId: response?.id ?? '',
|
|
@@ -767,12 +763,16 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
767
763
|
}
|
|
768
764
|
}
|
|
769
765
|
|
|
770
|
-
private maybeContinueResponse(
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
766
|
+
private maybeContinueResponse(): void {
|
|
767
|
+
// one response.create continues the chain, and only once nothing is still asking and every
|
|
768
|
+
// call in the conversation has its answer: the service refuses a partial batch
|
|
769
|
+
if (
|
|
770
|
+
this.backendRunningResponses.size ||
|
|
771
|
+
this.backendOpenCalls.size ||
|
|
772
|
+
!this.backendResponsePending
|
|
773
|
+
)
|
|
774
|
+
return;
|
|
775
|
+
this.backendResponsePending = false;
|
|
776
776
|
this.queueEvent(
|
|
777
777
|
{
|
|
778
778
|
type: 'response.create',
|
|
@@ -952,7 +952,7 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
952
952
|
async _appendItems(items: llm.ChatItem[]): Promise<void> {
|
|
953
953
|
this.history.insert(items);
|
|
954
954
|
if (!this.sessionStartSent) return;
|
|
955
|
-
const outputs:
|
|
955
|
+
const outputs: llm.FunctionCallOutput[] = [];
|
|
956
956
|
const lines: string[] = [];
|
|
957
957
|
for (const item of items) {
|
|
958
958
|
if (item.type === 'message' && (item.role === 'system' || item.role === 'developer')) {
|
|
@@ -960,8 +960,12 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
960
960
|
this.append('session.instructions.append', item.textContent, null, {
|
|
961
961
|
replayOnReconnect: false,
|
|
962
962
|
});
|
|
963
|
-
} else if (
|
|
964
|
-
|
|
963
|
+
} else if (
|
|
964
|
+
item.type === 'function_call_output' &&
|
|
965
|
+
(this.backendOpenCalls.has(item.callId) ||
|
|
966
|
+
[...this.backendRunningResponses.values()].some((calls) => calls.has(item.callId)))
|
|
967
|
+
) {
|
|
968
|
+
outputs.push(item);
|
|
965
969
|
} else {
|
|
966
970
|
const rendered = renderItem(item);
|
|
967
971
|
if (rendered) lines.push(`${rendered[0]}: ${rendered[1]}`);
|
|
@@ -969,7 +973,7 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
969
973
|
}
|
|
970
974
|
if (lines.length)
|
|
971
975
|
this.append('session.thinking.append', lines.join('\n'), null, { replayOnReconnect: false });
|
|
972
|
-
for (const
|
|
976
|
+
for (const output of outputs) {
|
|
973
977
|
this.queueEvent(
|
|
974
978
|
{
|
|
975
979
|
type: 'response.item.create',
|
|
@@ -978,8 +982,12 @@ export class GPTLiveSession extends llm.DuplexSession<{
|
|
|
978
982
|
} satisfies ClientEvent,
|
|
979
983
|
false,
|
|
980
984
|
);
|
|
981
|
-
this.
|
|
982
|
-
this.
|
|
985
|
+
this.backendOpenCalls.delete(output.callId);
|
|
986
|
+
for (const calls of this.backendRunningResponses.values()) calls.delete(output.callId);
|
|
987
|
+
}
|
|
988
|
+
if (outputs.length) {
|
|
989
|
+
this.backendResponsePending = true;
|
|
990
|
+
this.maybeContinueResponse();
|
|
983
991
|
}
|
|
984
992
|
}
|
|
985
993
|
/** Prompt an immediate spoken reply to instructions or the newest typed user message. */
|
|
@@ -1252,6 +1252,18 @@ describe('processBaseURL', () => {
|
|
|
1252
1252
|
expect(url.pathname).toBe('/v1/realtime');
|
|
1253
1253
|
});
|
|
1254
1254
|
|
|
1255
|
+
it('preserves custom wss baseURL paths and query params while adding model', () => {
|
|
1256
|
+
expect(
|
|
1257
|
+
processBaseURL({
|
|
1258
|
+
baseURL: 'wss://livekit.ai/voice/v1/chat/voice?client=oai&enable_noise_suppression=true',
|
|
1259
|
+
model: 'gpt-4',
|
|
1260
|
+
isAzure: false,
|
|
1261
|
+
}),
|
|
1262
|
+
).toBe(
|
|
1263
|
+
'wss://livekit.ai/voice/v1/chat/voice?client=oai&enable_noise_suppression=true&model=gpt-4',
|
|
1264
|
+
);
|
|
1265
|
+
});
|
|
1266
|
+
|
|
1255
1267
|
it('passes through an already-ws baseURL unchanged', () => {
|
|
1256
1268
|
const url = new URL(
|
|
1257
1269
|
processBaseURL({
|
|
@@ -396,9 +396,7 @@ export function processBaseURL({
|
|
|
396
396
|
azureDeployment?: string;
|
|
397
397
|
apiVersion?: string;
|
|
398
398
|
}): string {
|
|
399
|
-
|
|
400
|
-
const realtimePath = isAzure && !apiVersion ? 'v1/realtime' : 'realtime';
|
|
401
|
-
const url = new URL([baseURL, realtimePath].join('/'));
|
|
399
|
+
const url = new URL(baseURL);
|
|
402
400
|
|
|
403
401
|
if (url.protocol === 'https:') {
|
|
404
402
|
url.protocol = 'wss:';
|
|
@@ -406,11 +404,21 @@ export function processBaseURL({
|
|
|
406
404
|
url.protocol = 'ws:';
|
|
407
405
|
}
|
|
408
406
|
|
|
409
|
-
|
|
410
|
-
if (
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
407
|
+
const pathStripped = url.pathname.replace(/\/$/, '');
|
|
408
|
+
if (isAzure) {
|
|
409
|
+
if (['', '/openai'].includes(pathStripped)) {
|
|
410
|
+
// Azure GA (no apiVersion) uses /v1/realtime; legacy preview uses /realtime.
|
|
411
|
+
url.pathname = pathStripped + (apiVersion ? '/realtime' : '/v1/realtime');
|
|
412
|
+
} else if (pathStripped === '/openai/v1') {
|
|
413
|
+
url.pathname = '/openai/v1/realtime';
|
|
414
|
+
}
|
|
415
|
+
} else if (!url.pathname || ['', '/v1', '/openai', '/openai/v1'].includes(pathStripped)) {
|
|
416
|
+
url.pathname = pathStripped + '/realtime';
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (isAzure) {
|
|
420
|
+
// remove from endpoint URL if present
|
|
421
|
+
url.searchParams.delete('api-version');
|
|
414
422
|
}
|
|
415
423
|
|
|
416
424
|
const queryParams: Record<string, string> = {};
|
|
@@ -422,12 +430,14 @@ export function processBaseURL({
|
|
|
422
430
|
}
|
|
423
431
|
} else if (isAzure) {
|
|
424
432
|
// GA Azure: /v1/realtime?model=<deployment>
|
|
425
|
-
if (azureDeployment) {
|
|
433
|
+
if (!url.searchParams.has('model') && azureDeployment) {
|
|
426
434
|
queryParams['model'] = azureDeployment;
|
|
427
435
|
}
|
|
428
436
|
} else {
|
|
429
437
|
// Standard OpenAI: /realtime?model=<model>
|
|
430
|
-
|
|
438
|
+
if (!url.searchParams.has('model')) {
|
|
439
|
+
queryParams['model'] = model;
|
|
440
|
+
}
|
|
431
441
|
}
|
|
432
442
|
|
|
433
443
|
for (const [key, value] of Object.entries(queryParams)) {
|