@livekit/agents 1.0.8 → 1.0.10
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/voice/agent_activity.cjs +11 -1
- package/dist/voice/agent_activity.cjs.map +1 -1
- package/dist/voice/agent_activity.d.ts.map +1 -1
- package/dist/voice/agent_activity.js +11 -1
- package/dist/voice/agent_activity.js.map +1 -1
- package/dist/voice/generation_tools.test.cjs +236 -0
- package/dist/voice/generation_tools.test.cjs.map +1 -0
- package/dist/voice/generation_tools.test.js +235 -0
- package/dist/voice/generation_tools.test.js.map +1 -0
- package/dist/voice/speech_handle.cjs +1 -0
- package/dist/voice/speech_handle.cjs.map +1 -1
- package/dist/voice/speech_handle.d.ts.map +1 -1
- package/dist/voice/speech_handle.js +1 -0
- package/dist/voice/speech_handle.js.map +1 -1
- package/package.json +1 -1
- package/src/voice/agent_activity.ts +19 -1
- package/src/voice/generation_tools.test.ts +268 -0
- package/src/voice/speech_handle.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/voice/speech_handle.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatItem } from '../llm/index.js';\nimport { Event, Future, shortuuid } from '../utils.js';\nimport type { Task } from '../utils.js';\nimport { asyncLocalStorage } from './agent.js';\n\nexport class SpeechHandle {\n /** Priority for messages that should be played after all other messages in the queue */\n static SPEECH_PRIORITY_LOW = 0;\n /** Every speech generates by the VoiceAgent defaults to this priority. */\n static SPEECH_PRIORITY_NORMAL = 5;\n /** Priority for important messages that should be played before others. */\n static SPEECH_PRIORITY_HIGH = 10;\n\n private interruptFut = new Future<void>();\n private authorizedEvent = new Event();\n private scheduledFut = new Future<void>();\n private doneFut = new Future<void>();\n\n private generations: Future<void>[] = [];\n /** @internal */\n _tasks: Task<void>[] = [];\n private _chatItems: ChatItem[] = [];\n private _numSteps = 1;\n\n private itemAddedCallbacks: Set<(item: ChatItem) => void> = new Set();\n private doneCallbacks: Set<(sh: SpeechHandle) => void> = new Set();\n\n constructor(\n private _id: string,\n private _allowInterruptions: boolean,\n /** @internal */\n public _stepIndex: number,\n readonly parent?: SpeechHandle,\n ) {\n this.doneFut.await.finally(() => {\n for (const callback of this.doneCallbacks) {\n callback(this);\n }\n });\n }\n\n static create(options?: {\n allowInterruptions?: boolean;\n stepIndex?: number;\n parent?: SpeechHandle;\n }) {\n const { allowInterruptions = true, stepIndex = 0, parent } = options ?? {};\n\n return new SpeechHandle(shortuuid('speech_'), allowInterruptions, stepIndex, parent);\n }\n\n get interrupted(): boolean {\n return this.interruptFut.done;\n }\n\n get numSteps(): number {\n return this._numSteps;\n }\n\n get id(): string {\n return this._id;\n }\n\n get scheduled(): boolean {\n return this.scheduledFut.done;\n }\n\n get allowInterruptions(): boolean {\n return this._allowInterruptions;\n }\n\n /**\n * Allow or disallow interruptions on this SpeechHandle.\n *\n * When set to false, the SpeechHandle will no longer accept any incoming\n * interruption requests until re-enabled. If the handle is already\n * interrupted, clearing interruptions is not allowed.\n *\n * @param value - true to allow interruptions, false to disallow\n * @throws Error If attempting to disable interruptions when already interrupted\n */\n set allowInterruptions(value: boolean) {\n if (this.interrupted && !value) {\n throw new Error(\n 'Cannot set allow_interruptions to False, the SpeechHandle is already interrupted',\n );\n }\n this._allowInterruptions = value;\n }\n\n done(): boolean {\n return this.doneFut.done;\n }\n\n get chatItems(): ChatItem[] {\n return this._chatItems;\n }\n\n /**\n * Interrupt the current speech generation.\n *\n * @throws Error If this speech handle does not allow interruptions.\n *\n * @returns The same speech handle that was interrupted.\n */\n interrupt(force: boolean = false): SpeechHandle {\n if (!force && !this.allowInterruptions) {\n throw new Error('This generation handle does not allow interruptions');\n }\n\n this._cancel();\n return this;\n }\n\n /**\n * Waits for the entire assistant turn to complete playback.\n *\n * This method waits until the assistant has fully finished speaking,\n * including any finalization steps beyond initial response generation.\n * This is appropriate to call when you want to ensure the speech output\n * has entirely played out, including any tool calls and response follow-ups.\n */\n async waitForPlayout(): Promise<void> {\n const store = asyncLocalStorage.getStore();\n if (store && store?.functionCall) {\n throw new Error(\n `Cannot call 'SpeechHandle.waitForPlayout()' from inside the function tool '${store.functionCall.name}'. ` +\n 'This creates a circular wait: the speech handle is waiting for the function tool to complete, ' +\n 'while the function tool is simultaneously waiting for the speech handle.\\n' +\n \"To wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead.\",\n );\n }\n }\n\n async waitIfNotInterrupted(aw: Promise<unknown>[]): Promise<void> {\n const allTasksPromise = Promise.all(aw);\n const fs: Promise<unknown>[] = [allTasksPromise, this.interruptFut.await];\n await Promise.race(fs);\n }\n\n addDoneCallback(callback: (sh: SpeechHandle) => void) {\n this.doneCallbacks.add(callback);\n }\n\n removeDoneCallback(callback: (sh: SpeechHandle) => void) {\n this.doneCallbacks.delete(callback);\n }\n\n /** @internal */\n _cancel(): SpeechHandle {\n if (this.done()) {\n return this;\n }\n\n if (!this.interruptFut.done) {\n this.interruptFut.resolve();\n }\n\n return this;\n }\n\n /** @internal */\n _authorizeGeneration(): void {\n const fut = new Future<void>();\n this.generations.push(fut);\n this.authorizedEvent.set();\n }\n\n /** @internal */\n _clearAuthorization(): void {\n this.authorizedEvent.clear();\n }\n\n /** @internal */\n async _waitForAuthorization(): Promise<void> {\n await this.authorizedEvent.wait();\n }\n\n /** @internal */\n async _waitForGeneration(stepIdx: number = -1): Promise<void> {\n if (this.generations.length === 0) {\n throw new Error('cannot use wait_for_generation: no active generation is running.');\n }\n\n const index = stepIdx === -1 ? this.generations.length - 1 : stepIdx;\n const generation = this.generations[index];\n if (!generation) {\n throw new Error(`Generation at index ${index} not found.`);\n }\n return generation.await;\n }\n\n /** @internal */\n async _waitForScheduled(): Promise<void> {\n return this.scheduledFut.await;\n }\n\n /** @internal */\n _markGenerationDone(): void {\n if (this.generations.length === 0) {\n throw new Error('cannot use mark_generation_done: no active generation is running.');\n }\n\n const lastGeneration = this.generations[this.generations.length - 1];\n if (lastGeneration && !lastGeneration.done) {\n lastGeneration.resolve();\n }\n }\n\n /** @internal */\n _markDone(): void {\n if (!this.doneFut.done) {\n this.doneFut.resolve();\n if (this.generations.length > 0) {\n this._markGenerationDone(); // preemptive generation could be cancelled before being scheduled\n }\n }\n }\n\n /** @internal */\n _markScheduled(): void {\n if (!this.scheduledFut.done) {\n this.scheduledFut.resolve();\n }\n }\n\n /** @internal */\n _addItemAddedCallback(callback: (item: ChatItem) => void): void {\n this.itemAddedCallbacks.add(callback);\n }\n\n /** @internal */\n _removeItemAddedCallback(callback: (item: ChatItem) => void): void {\n this.itemAddedCallbacks.delete(callback);\n }\n\n /** @internal */\n _itemAdded(items: ChatItem[]): void {\n for (const item of items) {\n for (const cb of this.itemAddedCallbacks) {\n cb(item);\n }\n this._chatItems.push(item);\n }\n }\n}\n"],"mappings":"AAIA,SAAS,OAAO,QAAQ,iBAAiB;AAEzC,SAAS,yBAAyB;AAE3B,MAAM,aAAa;AAAA,EAsBxB,YACU,KACA,qBAED,YACE,QACT;AALQ;AACA;AAED;AACE;AAET,SAAK,QAAQ,MAAM,QAAQ,MAAM;AAC/B,iBAAW,YAAY,KAAK,eAAe;AACzC,iBAAS,IAAI;AAAA,MACf;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA,EAhCA,OAAO,sBAAsB;AAAA;AAAA,EAE7B,OAAO,yBAAyB;AAAA;AAAA,EAEhC,OAAO,uBAAuB;AAAA,EAEtB,eAAe,IAAI,OAAa;AAAA,EAChC,kBAAkB,IAAI,MAAM;AAAA,EAC5B,eAAe,IAAI,OAAa;AAAA,EAChC,UAAU,IAAI,OAAa;AAAA,EAE3B,cAA8B,CAAC;AAAA;AAAA,EAEvC,SAAuB,CAAC;AAAA,EAChB,aAAyB,CAAC;AAAA,EAC1B,YAAY;AAAA,EAEZ,qBAAoD,oBAAI,IAAI;AAAA,EAC5D,gBAAiD,oBAAI,IAAI;AAAA,EAgBjE,OAAO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;AAAA,IACrF;AAEA,UAAM,iBAAiB,KAAK,YAAY,KAAK,YAAY,SAAS,CAAC;AACnE,QAAI,kBAAkB,CAAC,eAAe,MAAM;AAC1C,qBAAe,QAAQ;AAAA,IACzB;AAAA,EACF;AAAA;AAAA,EAGA,YAAkB;AAChB,QAAI,CAAC,KAAK,QAAQ,MAAM;AACtB,WAAK,QAAQ,QAAQ;AACrB,UAAI,KAAK,YAAY,SAAS,GAAG;AAC/B,aAAK,oBAAoB;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,iBAAuB;AACrB,QAAI,CAAC,KAAK,aAAa,MAAM;AAC3B,WAAK,aAAa,QAAQ;AAAA,IAC5B;AAAA,EACF;AAAA;AAAA,EAGA,sBAAsB,UAA0C;AAC9D,SAAK,mBAAmB,IAAI,QAAQ;AAAA,EACtC;AAAA;AAAA,EAGA,yBAAyB,UAA0C;AACjE,SAAK,mBAAmB,OAAO,QAAQ;AAAA,EACzC;AAAA;AAAA,EAGA,WAAW,OAAyB;AAClC,eAAW,QAAQ,OAAO;AACxB,iBAAW,MAAM,KAAK,oBAAoB;AACxC,WAAG,IAAI;AAAA,MACT;AACA,WAAK,WAAW,KAAK,IAAI;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/voice/speech_handle.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatItem } from '../llm/index.js';\nimport { Event, Future, shortuuid } from '../utils.js';\nimport type { Task } from '../utils.js';\nimport { asyncLocalStorage } from './agent.js';\n\nexport class SpeechHandle {\n /** Priority for messages that should be played after all other messages in the queue */\n static SPEECH_PRIORITY_LOW = 0;\n /** Every speech generates by the VoiceAgent defaults to this priority. */\n static SPEECH_PRIORITY_NORMAL = 5;\n /** Priority for important messages that should be played before others. */\n static SPEECH_PRIORITY_HIGH = 10;\n\n private interruptFut = new Future<void>();\n private authorizedEvent = new Event();\n private scheduledFut = new Future<void>();\n private doneFut = new Future<void>();\n\n private generations: Future<void>[] = [];\n /** @internal */\n _tasks: Task<void>[] = [];\n private _chatItems: ChatItem[] = [];\n private _numSteps = 1;\n\n private itemAddedCallbacks: Set<(item: ChatItem) => void> = new Set();\n private doneCallbacks: Set<(sh: SpeechHandle) => void> = new Set();\n\n constructor(\n private _id: string,\n private _allowInterruptions: boolean,\n /** @internal */\n public _stepIndex: number,\n readonly parent?: SpeechHandle,\n ) {\n this.doneFut.await.finally(() => {\n for (const callback of this.doneCallbacks) {\n callback(this);\n }\n });\n }\n\n static create(options?: {\n allowInterruptions?: boolean;\n stepIndex?: number;\n parent?: SpeechHandle;\n }) {\n const { allowInterruptions = true, stepIndex = 0, parent } = options ?? {};\n\n return new SpeechHandle(shortuuid('speech_'), allowInterruptions, stepIndex, parent);\n }\n\n get interrupted(): boolean {\n return this.interruptFut.done;\n }\n\n get numSteps(): number {\n return this._numSteps;\n }\n\n get id(): string {\n return this._id;\n }\n\n get scheduled(): boolean {\n return this.scheduledFut.done;\n }\n\n get allowInterruptions(): boolean {\n return this._allowInterruptions;\n }\n\n /**\n * Allow or disallow interruptions on this SpeechHandle.\n *\n * When set to false, the SpeechHandle will no longer accept any incoming\n * interruption requests until re-enabled. If the handle is already\n * interrupted, clearing interruptions is not allowed.\n *\n * @param value - true to allow interruptions, false to disallow\n * @throws Error If attempting to disable interruptions when already interrupted\n */\n set allowInterruptions(value: boolean) {\n if (this.interrupted && !value) {\n throw new Error(\n 'Cannot set allow_interruptions to False, the SpeechHandle is already interrupted',\n );\n }\n this._allowInterruptions = value;\n }\n\n done(): boolean {\n return this.doneFut.done;\n }\n\n get chatItems(): ChatItem[] {\n return this._chatItems;\n }\n\n /**\n * Interrupt the current speech generation.\n *\n * @throws Error If this speech handle does not allow interruptions.\n *\n * @returns The same speech handle that was interrupted.\n */\n interrupt(force: boolean = false): SpeechHandle {\n if (!force && !this.allowInterruptions) {\n throw new Error('This generation handle does not allow interruptions');\n }\n\n this._cancel();\n return this;\n }\n\n /**\n * Waits for the entire assistant turn to complete playback.\n *\n * This method waits until the assistant has fully finished speaking,\n * including any finalization steps beyond initial response generation.\n * This is appropriate to call when you want to ensure the speech output\n * has entirely played out, including any tool calls and response follow-ups.\n */\n async waitForPlayout(): Promise<void> {\n const store = asyncLocalStorage.getStore();\n if (store && store?.functionCall) {\n throw new Error(\n `Cannot call 'SpeechHandle.waitForPlayout()' from inside the function tool '${store.functionCall.name}'. ` +\n 'This creates a circular wait: the speech handle is waiting for the function tool to complete, ' +\n 'while the function tool is simultaneously waiting for the speech handle.\\n' +\n \"To wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead.\",\n );\n }\n await this.doneFut.await;\n }\n\n async waitIfNotInterrupted(aw: Promise<unknown>[]): Promise<void> {\n const allTasksPromise = Promise.all(aw);\n const fs: Promise<unknown>[] = [allTasksPromise, this.interruptFut.await];\n await Promise.race(fs);\n }\n\n addDoneCallback(callback: (sh: SpeechHandle) => void) {\n this.doneCallbacks.add(callback);\n }\n\n removeDoneCallback(callback: (sh: SpeechHandle) => void) {\n this.doneCallbacks.delete(callback);\n }\n\n /** @internal */\n _cancel(): SpeechHandle {\n if (this.done()) {\n return this;\n }\n\n if (!this.interruptFut.done) {\n this.interruptFut.resolve();\n }\n\n return this;\n }\n\n /** @internal */\n _authorizeGeneration(): void {\n const fut = new Future<void>();\n this.generations.push(fut);\n this.authorizedEvent.set();\n }\n\n /** @internal */\n _clearAuthorization(): void {\n this.authorizedEvent.clear();\n }\n\n /** @internal */\n async _waitForAuthorization(): Promise<void> {\n await this.authorizedEvent.wait();\n }\n\n /** @internal */\n async _waitForGeneration(stepIdx: number = -1): Promise<void> {\n if (this.generations.length === 0) {\n throw new Error('cannot use wait_for_generation: no active generation is running.');\n }\n\n const index = stepIdx === -1 ? this.generations.length - 1 : stepIdx;\n const generation = this.generations[index];\n if (!generation) {\n throw new Error(`Generation at index ${index} not found.`);\n }\n return generation.await;\n }\n\n /** @internal */\n async _waitForScheduled(): Promise<void> {\n return this.scheduledFut.await;\n }\n\n /** @internal */\n _markGenerationDone(): void {\n if (this.generations.length === 0) {\n throw new Error('cannot use mark_generation_done: no active generation is running.');\n }\n\n const lastGeneration = this.generations[this.generations.length - 1];\n if (lastGeneration && !lastGeneration.done) {\n lastGeneration.resolve();\n }\n }\n\n /** @internal */\n _markDone(): void {\n if (!this.doneFut.done) {\n this.doneFut.resolve();\n if (this.generations.length > 0) {\n this._markGenerationDone(); // preemptive generation could be cancelled before being scheduled\n }\n }\n }\n\n /** @internal */\n _markScheduled(): void {\n if (!this.scheduledFut.done) {\n this.scheduledFut.resolve();\n }\n }\n\n /** @internal */\n _addItemAddedCallback(callback: (item: ChatItem) => void): void {\n this.itemAddedCallbacks.add(callback);\n }\n\n /** @internal */\n _removeItemAddedCallback(callback: (item: ChatItem) => void): void {\n this.itemAddedCallbacks.delete(callback);\n }\n\n /** @internal */\n _itemAdded(items: ChatItem[]): void {\n for (const item of items) {\n for (const cb of this.itemAddedCallbacks) {\n cb(item);\n }\n this._chatItems.push(item);\n }\n }\n}\n"],"mappings":"AAIA,SAAS,OAAO,QAAQ,iBAAiB;AAEzC,SAAS,yBAAyB;AAE3B,MAAM,aAAa;AAAA,EAsBxB,YACU,KACA,qBAED,YACE,QACT;AALQ;AACA;AAED;AACE;AAET,SAAK,QAAQ,MAAM,QAAQ,MAAM;AAC/B,iBAAW,YAAY,KAAK,eAAe;AACzC,iBAAS,IAAI;AAAA,MACf;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA,EAhCA,OAAO,sBAAsB;AAAA;AAAA,EAE7B,OAAO,yBAAyB;AAAA;AAAA,EAEhC,OAAO,uBAAuB;AAAA,EAEtB,eAAe,IAAI,OAAa;AAAA,EAChC,kBAAkB,IAAI,MAAM;AAAA,EAC5B,eAAe,IAAI,OAAa;AAAA,EAChC,UAAU,IAAI,OAAa;AAAA,EAE3B,cAA8B,CAAC;AAAA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AO,WAAW;AAAA,EACpB;AAAA;AAAA,EAGA,MAAM,oBAAmC;AACvC,WAAO,KAAK,aAAa;AAAA,EAC3B;AAAA;AAAA,EAGA,sBAA4B;AAC1B,QAAI,KAAK,YAAY,WAAW,GAAG;AACjC,YAAM,IAAI,MAAM,mEAAmE;AAAA,IACrF;AAEA,UAAM,iBAAiB,KAAK,YAAY,KAAK,YAAY,SAAS,CAAC;AACnE,QAAI,kBAAkB,CAAC,eAAe,MAAM;AAC1C,qBAAe,QAAQ;AAAA,IACzB;AAAA,EACF;AAAA;AAAA,EAGA,YAAkB;AAChB,QAAI,CAAC,KAAK,QAAQ,MAAM;AACtB,WAAK,QAAQ,QAAQ;AACrB,UAAI,KAAK,YAAY,SAAS,GAAG;AAC/B,aAAK,oBAAoB;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,iBAAuB;AACrB,QAAI,CAAC,KAAK,aAAa,MAAM;AAC3B,WAAK,aAAa,QAAQ;AAAA,IAC5B;AAAA,EACF;AAAA;AAAA,EAGA,sBAAsB,UAA0C;AAC9D,SAAK,mBAAmB,IAAI,QAAQ;AAAA,EACtC;AAAA;AAAA,EAGA,yBAAyB,UAA0C;AACjE,SAAK,mBAAmB,OAAO,QAAQ;AAAA,EACzC;AAAA;AAAA,EAGA,WAAW,OAAyB;AAClC,eAAW,QAAQ,OAAO;AACxB,iBAAW,MAAM,KAAK,oBAAoB;AACxC,WAAG,IAAI;AAAA,MACT;AACA,WAAK,WAAW,KAAK,IAAI;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1506,6 +1506,10 @@ export class AgentActivity implements RecognitionHooks {
|
|
|
1506
1506
|
abortController: AbortController,
|
|
1507
1507
|
outputs: Array<[string, _TextOut | null, _AudioOut | null]>,
|
|
1508
1508
|
) => {
|
|
1509
|
+
replyAbortController.signal.addEventListener('abort', () => abortController.abort(), {
|
|
1510
|
+
once: true,
|
|
1511
|
+
});
|
|
1512
|
+
|
|
1509
1513
|
const forwardTasks: Array<Task<void>> = [];
|
|
1510
1514
|
try {
|
|
1511
1515
|
for await (const msg of ev.messageStream) {
|
|
@@ -1563,7 +1567,7 @@ export class AgentActivity implements RecognitionHooks {
|
|
|
1563
1567
|
const tasks = [
|
|
1564
1568
|
Task.from(
|
|
1565
1569
|
(controller) => readMessages(controller, messageOutputs),
|
|
1566
|
-
|
|
1570
|
+
undefined,
|
|
1567
1571
|
'AgentActivity.realtime_generation.read_messages',
|
|
1568
1572
|
),
|
|
1569
1573
|
];
|
|
@@ -1775,6 +1779,20 @@ export class AgentActivity implements RecognitionHooks {
|
|
|
1775
1779
|
}
|
|
1776
1780
|
|
|
1777
1781
|
if (functionToolsExecutedEvent.functionCallOutputs.length > 0) {
|
|
1782
|
+
// wait all speeches played before updating the tool output and generating the response
|
|
1783
|
+
// most realtime models dont support generating multiple responses at the same time
|
|
1784
|
+
while (this.currentSpeech || this.speechQueue.size() > 0) {
|
|
1785
|
+
if (
|
|
1786
|
+
this.currentSpeech &&
|
|
1787
|
+
!this.currentSpeech.done() &&
|
|
1788
|
+
this.currentSpeech !== speechHandle
|
|
1789
|
+
) {
|
|
1790
|
+
await this.currentSpeech.waitForPlayout();
|
|
1791
|
+
} else {
|
|
1792
|
+
// Don't block the event loop
|
|
1793
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1778
1796
|
const chatCtx = this.realtimeSession.chatCtx.copy();
|
|
1779
1797
|
chatCtx.items.push(...functionToolsExecutedEvent.functionCallOutputs);
|
|
1780
1798
|
try {
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { ReadableStream as NodeReadableStream } from 'stream/web';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { FunctionCall, tool } from '../llm/index.js';
|
|
8
|
+
import { initializeLogger } from '../log.js';
|
|
9
|
+
import type { Task } from '../utils.js';
|
|
10
|
+
import { cancelAndWait, delay } from '../utils.js';
|
|
11
|
+
import { type _TextOut, performTextForwarding, performToolExecutions } from './generation.js';
|
|
12
|
+
|
|
13
|
+
function createStringStream(chunks: string[], delayMs: number = 0): NodeReadableStream<string> {
|
|
14
|
+
return new NodeReadableStream<string>({
|
|
15
|
+
async start(controller) {
|
|
16
|
+
for (const c of chunks) {
|
|
17
|
+
if (delayMs > 0) {
|
|
18
|
+
await delay(delayMs);
|
|
19
|
+
}
|
|
20
|
+
controller.enqueue(c);
|
|
21
|
+
}
|
|
22
|
+
controller.close();
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function createFunctionCallStream(fc: FunctionCall): NodeReadableStream<FunctionCall> {
|
|
28
|
+
return new NodeReadableStream<FunctionCall>({
|
|
29
|
+
start(controller) {
|
|
30
|
+
controller.enqueue(fc);
|
|
31
|
+
controller.close();
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function createFunctionCallStreamFromArray(fcs: FunctionCall[]): NodeReadableStream<FunctionCall> {
|
|
37
|
+
return new NodeReadableStream<FunctionCall>({
|
|
38
|
+
start(controller) {
|
|
39
|
+
for (const fc of fcs) {
|
|
40
|
+
controller.enqueue(fc);
|
|
41
|
+
}
|
|
42
|
+
controller.close();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('Generation + Tool Execution', () => {
|
|
48
|
+
initializeLogger({ pretty: false, level: 'silent' });
|
|
49
|
+
|
|
50
|
+
it('should not abort tool when preamble forwarders are cleaned up', async () => {
|
|
51
|
+
const replyAbortController = new AbortController();
|
|
52
|
+
const forwarderController = new AbortController();
|
|
53
|
+
|
|
54
|
+
const chunks = Array.from({ length: 50 }, () => `Hi.`);
|
|
55
|
+
const fullPreambleText = chunks.join('');
|
|
56
|
+
const preamble = createStringStream(chunks, 20);
|
|
57
|
+
const [textForwardTask, textOut]: [Task<void>, _TextOut] = performTextForwarding(
|
|
58
|
+
preamble,
|
|
59
|
+
forwarderController,
|
|
60
|
+
null,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
// Tool that takes > 5 seconds
|
|
64
|
+
let toolAborted = false;
|
|
65
|
+
const getWeather = tool({
|
|
66
|
+
description: 'weather',
|
|
67
|
+
parameters: z.object({ location: z.string() }),
|
|
68
|
+
execute: async ({ location }, { abortSignal }) => {
|
|
69
|
+
if (abortSignal) {
|
|
70
|
+
abortSignal.addEventListener('abort', () => {
|
|
71
|
+
toolAborted = true;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// 6s delay
|
|
75
|
+
await delay(6000);
|
|
76
|
+
return `Sunny in ${location}`;
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const fc = FunctionCall.create({
|
|
81
|
+
callId: 'call_1',
|
|
82
|
+
name: 'getWeather',
|
|
83
|
+
args: JSON.stringify({ location: 'San Francisco' }),
|
|
84
|
+
});
|
|
85
|
+
const toolCallStream = createFunctionCallStream(fc);
|
|
86
|
+
|
|
87
|
+
const [execTask, toolOutput] = performToolExecutions({
|
|
88
|
+
session: {} as any,
|
|
89
|
+
speechHandle: { id: 'speech_test', _itemAdded: () => {} } as any,
|
|
90
|
+
toolCtx: { getWeather } as any,
|
|
91
|
+
toolCallStream,
|
|
92
|
+
controller: replyAbortController,
|
|
93
|
+
onToolExecutionStarted: () => {},
|
|
94
|
+
onToolExecutionCompleted: () => {},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Ensure tool has started, then cancel forwarders mid-stream (without aborting parent AbortController)
|
|
98
|
+
await toolOutput.firstToolStartedFuture.await;
|
|
99
|
+
await delay(100);
|
|
100
|
+
await cancelAndWait([textForwardTask], 5000);
|
|
101
|
+
|
|
102
|
+
await execTask.result;
|
|
103
|
+
|
|
104
|
+
expect(toolOutput.output.length).toBe(1);
|
|
105
|
+
const out = toolOutput.output[0]!;
|
|
106
|
+
expect(out.toolCallOutput?.isError).toBe(false);
|
|
107
|
+
expect(out.toolCallOutput?.output).toContain('Sunny in San Francisco');
|
|
108
|
+
// Forwarder should have been cancelled before finishing all preamble chunks
|
|
109
|
+
expect(textOut.text).not.toBe(fullPreambleText);
|
|
110
|
+
// Tool's abort signal must not have fired
|
|
111
|
+
expect(toolAborted).toBe(false);
|
|
112
|
+
}, 30_000);
|
|
113
|
+
|
|
114
|
+
it('should return basic tool execution output', async () => {
|
|
115
|
+
const replyAbortController = new AbortController();
|
|
116
|
+
|
|
117
|
+
const echo = tool({
|
|
118
|
+
description: 'echo',
|
|
119
|
+
parameters: z.object({ msg: z.string() }),
|
|
120
|
+
execute: async ({ msg }) => `echo: ${msg}`,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const fc = FunctionCall.create({
|
|
124
|
+
callId: 'call_2',
|
|
125
|
+
name: 'echo',
|
|
126
|
+
args: JSON.stringify({ msg: 'hello' }),
|
|
127
|
+
});
|
|
128
|
+
const toolCallStream = createFunctionCallStream(fc);
|
|
129
|
+
|
|
130
|
+
const [execTask, toolOutput] = performToolExecutions({
|
|
131
|
+
session: {} as any,
|
|
132
|
+
speechHandle: { id: 'speech_test2', _itemAdded: () => {} } as any,
|
|
133
|
+
toolCtx: { echo } as any,
|
|
134
|
+
toolCallStream,
|
|
135
|
+
controller: replyAbortController,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await execTask.result;
|
|
139
|
+
expect(toolOutput.output.length).toBe(1);
|
|
140
|
+
const out = toolOutput.output[0];
|
|
141
|
+
expect(out?.toolCallOutput?.isError).toBe(false);
|
|
142
|
+
expect(out?.toolCallOutput?.output).toContain('echo: hello');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should abort tool when reply is aborted mid-execution', async () => {
|
|
146
|
+
const replyAbortController = new AbortController();
|
|
147
|
+
|
|
148
|
+
let aborted = false;
|
|
149
|
+
const longOp = tool({
|
|
150
|
+
description: 'longOp',
|
|
151
|
+
parameters: z.object({ ms: z.number() }),
|
|
152
|
+
execute: async ({ ms }, { abortSignal }) => {
|
|
153
|
+
if (abortSignal) {
|
|
154
|
+
abortSignal.addEventListener('abort', () => {
|
|
155
|
+
aborted = true;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
await delay(ms);
|
|
159
|
+
return 'done';
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const fc = FunctionCall.create({
|
|
164
|
+
callId: 'call_abort_1',
|
|
165
|
+
name: 'longOp',
|
|
166
|
+
args: JSON.stringify({ ms: 5000 }),
|
|
167
|
+
});
|
|
168
|
+
const toolCallStream = createFunctionCallStream(fc);
|
|
169
|
+
|
|
170
|
+
const [execTask, toolOutput] = performToolExecutions({
|
|
171
|
+
session: {} as any,
|
|
172
|
+
speechHandle: { id: 'speech_abort', _itemAdded: () => {} } as any,
|
|
173
|
+
toolCtx: { longOp } as any,
|
|
174
|
+
toolCallStream,
|
|
175
|
+
controller: replyAbortController,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await toolOutput.firstToolStartedFuture.await;
|
|
179
|
+
replyAbortController.abort();
|
|
180
|
+
await execTask.result;
|
|
181
|
+
|
|
182
|
+
expect(aborted).toBe(true);
|
|
183
|
+
expect(toolOutput.output.length).toBe(1);
|
|
184
|
+
const out = toolOutput.output[0];
|
|
185
|
+
expect(out?.toolCallOutput?.isError).toBe(true);
|
|
186
|
+
}, 20_000);
|
|
187
|
+
|
|
188
|
+
it('should return error output on invalid tool args (zod validation failure)', async () => {
|
|
189
|
+
const replyAbortController = new AbortController();
|
|
190
|
+
|
|
191
|
+
const echo = tool({
|
|
192
|
+
description: 'echo',
|
|
193
|
+
parameters: z.object({ msg: z.string() }),
|
|
194
|
+
execute: async ({ msg }) => `echo: ${msg}`,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// invalid: msg should be string
|
|
198
|
+
const fc = FunctionCall.create({
|
|
199
|
+
callId: 'call_invalid_args',
|
|
200
|
+
name: 'echo',
|
|
201
|
+
args: JSON.stringify({ msg: 123 }),
|
|
202
|
+
});
|
|
203
|
+
const toolCallStream = createFunctionCallStream(fc);
|
|
204
|
+
|
|
205
|
+
const [execTask, toolOutput] = performToolExecutions({
|
|
206
|
+
session: {} as any,
|
|
207
|
+
speechHandle: { id: 'speech_invalid', _itemAdded: () => {} } as any,
|
|
208
|
+
toolCtx: { echo } as any,
|
|
209
|
+
toolCallStream,
|
|
210
|
+
controller: replyAbortController,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
await execTask.result;
|
|
214
|
+
expect(toolOutput.output.length).toBe(1);
|
|
215
|
+
const out = toolOutput.output[0];
|
|
216
|
+
expect(out?.toolCallOutput?.isError).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should handle multiple tool calls within a single stream', async () => {
|
|
220
|
+
const replyAbortController = new AbortController();
|
|
221
|
+
|
|
222
|
+
const sum = tool({
|
|
223
|
+
description: 'sum',
|
|
224
|
+
parameters: z.object({ a: z.number(), b: z.number() }),
|
|
225
|
+
execute: async ({ a, b }) => a + b,
|
|
226
|
+
});
|
|
227
|
+
const upper = tool({
|
|
228
|
+
description: 'upper',
|
|
229
|
+
parameters: z.object({ s: z.string() }),
|
|
230
|
+
execute: async ({ s }) => s.toUpperCase(),
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const fc1 = FunctionCall.create({
|
|
234
|
+
callId: 'call_multi_1',
|
|
235
|
+
name: 'sum',
|
|
236
|
+
args: JSON.stringify({ a: 2, b: 3 }),
|
|
237
|
+
});
|
|
238
|
+
const fc2 = FunctionCall.create({
|
|
239
|
+
callId: 'call_multi_2',
|
|
240
|
+
name: 'upper',
|
|
241
|
+
args: JSON.stringify({ s: 'hey' }),
|
|
242
|
+
});
|
|
243
|
+
const toolCallStream = createFunctionCallStreamFromArray([fc1, fc2]);
|
|
244
|
+
|
|
245
|
+
const [execTask, toolOutput] = performToolExecutions({
|
|
246
|
+
session: {} as any,
|
|
247
|
+
speechHandle: { id: 'speech_multi', _itemAdded: () => {} } as any,
|
|
248
|
+
toolCtx: { sum, upper } as any,
|
|
249
|
+
toolCallStream,
|
|
250
|
+
controller: replyAbortController,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
await execTask.result;
|
|
254
|
+
expect(toolOutput.output.length).toBe(2);
|
|
255
|
+
|
|
256
|
+
// sort by callId to assert deterministically
|
|
257
|
+
const sorted = [...toolOutput.output].sort((a, b) =>
|
|
258
|
+
a.toolCall.callId.localeCompare(b.toolCall.callId),
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
expect(sorted[0]?.toolCall.name).toBe('sum');
|
|
262
|
+
expect(sorted[0]?.toolCallOutput?.isError).toBe(false);
|
|
263
|
+
expect(sorted[0]?.toolCallOutput?.output).toBe('5');
|
|
264
|
+
expect(sorted[1]?.toolCall.name).toBe('upper');
|
|
265
|
+
expect(sorted[1]?.toolCallOutput?.isError).toBe(false);
|
|
266
|
+
expect(sorted[1]?.toolCallOutput?.output).toBe('"HEY"');
|
|
267
|
+
});
|
|
268
|
+
});
|
|
@@ -133,6 +133,7 @@ export class SpeechHandle {
|
|
|
133
133
|
"To wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead.",
|
|
134
134
|
);
|
|
135
135
|
}
|
|
136
|
+
await this.doneFut.await;
|
|
136
137
|
}
|
|
137
138
|
|
|
138
139
|
async waitIfNotInterrupted(aw: Promise<unknown>[]): Promise<void> {
|