@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.
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ var import_web = require("stream/web");
3
+ var import_vitest = require("vitest");
4
+ var import_zod = require("zod");
5
+ var import_llm = require("../llm/index.cjs");
6
+ var import_log = require("../log.cjs");
7
+ var import_utils = require("../utils.cjs");
8
+ var import_generation = require("./generation.cjs");
9
+ function createStringStream(chunks, delayMs = 0) {
10
+ return new import_web.ReadableStream({
11
+ async start(controller) {
12
+ for (const c of chunks) {
13
+ if (delayMs > 0) {
14
+ await (0, import_utils.delay)(delayMs);
15
+ }
16
+ controller.enqueue(c);
17
+ }
18
+ controller.close();
19
+ }
20
+ });
21
+ }
22
+ function createFunctionCallStream(fc) {
23
+ return new import_web.ReadableStream({
24
+ start(controller) {
25
+ controller.enqueue(fc);
26
+ controller.close();
27
+ }
28
+ });
29
+ }
30
+ function createFunctionCallStreamFromArray(fcs) {
31
+ return new import_web.ReadableStream({
32
+ start(controller) {
33
+ for (const fc of fcs) {
34
+ controller.enqueue(fc);
35
+ }
36
+ controller.close();
37
+ }
38
+ });
39
+ }
40
+ (0, import_vitest.describe)("Generation + Tool Execution", () => {
41
+ (0, import_log.initializeLogger)({ pretty: false, level: "silent" });
42
+ (0, import_vitest.it)("should not abort tool when preamble forwarders are cleaned up", async () => {
43
+ var _a, _b;
44
+ const replyAbortController = new AbortController();
45
+ const forwarderController = new AbortController();
46
+ const chunks = Array.from({ length: 50 }, () => `Hi.`);
47
+ const fullPreambleText = chunks.join("");
48
+ const preamble = createStringStream(chunks, 20);
49
+ const [textForwardTask, textOut] = (0, import_generation.performTextForwarding)(
50
+ preamble,
51
+ forwarderController,
52
+ null
53
+ );
54
+ let toolAborted = false;
55
+ const getWeather = (0, import_llm.tool)({
56
+ description: "weather",
57
+ parameters: import_zod.z.object({ location: import_zod.z.string() }),
58
+ execute: async ({ location }, { abortSignal }) => {
59
+ if (abortSignal) {
60
+ abortSignal.addEventListener("abort", () => {
61
+ toolAborted = true;
62
+ });
63
+ }
64
+ await (0, import_utils.delay)(6e3);
65
+ return `Sunny in ${location}`;
66
+ }
67
+ });
68
+ const fc = import_llm.FunctionCall.create({
69
+ callId: "call_1",
70
+ name: "getWeather",
71
+ args: JSON.stringify({ location: "San Francisco" })
72
+ });
73
+ const toolCallStream = createFunctionCallStream(fc);
74
+ const [execTask, toolOutput] = (0, import_generation.performToolExecutions)({
75
+ session: {},
76
+ speechHandle: { id: "speech_test", _itemAdded: () => {
77
+ } },
78
+ toolCtx: { getWeather },
79
+ toolCallStream,
80
+ controller: replyAbortController,
81
+ onToolExecutionStarted: () => {
82
+ },
83
+ onToolExecutionCompleted: () => {
84
+ }
85
+ });
86
+ await toolOutput.firstToolStartedFuture.await;
87
+ await (0, import_utils.delay)(100);
88
+ await (0, import_utils.cancelAndWait)([textForwardTask], 5e3);
89
+ await execTask.result;
90
+ (0, import_vitest.expect)(toolOutput.output.length).toBe(1);
91
+ const out = toolOutput.output[0];
92
+ (0, import_vitest.expect)((_a = out.toolCallOutput) == null ? void 0 : _a.isError).toBe(false);
93
+ (0, import_vitest.expect)((_b = out.toolCallOutput) == null ? void 0 : _b.output).toContain("Sunny in San Francisco");
94
+ (0, import_vitest.expect)(textOut.text).not.toBe(fullPreambleText);
95
+ (0, import_vitest.expect)(toolAborted).toBe(false);
96
+ }, 3e4);
97
+ (0, import_vitest.it)("should return basic tool execution output", async () => {
98
+ var _a, _b;
99
+ const replyAbortController = new AbortController();
100
+ const echo = (0, import_llm.tool)({
101
+ description: "echo",
102
+ parameters: import_zod.z.object({ msg: import_zod.z.string() }),
103
+ execute: async ({ msg }) => `echo: ${msg}`
104
+ });
105
+ const fc = import_llm.FunctionCall.create({
106
+ callId: "call_2",
107
+ name: "echo",
108
+ args: JSON.stringify({ msg: "hello" })
109
+ });
110
+ const toolCallStream = createFunctionCallStream(fc);
111
+ const [execTask, toolOutput] = (0, import_generation.performToolExecutions)({
112
+ session: {},
113
+ speechHandle: { id: "speech_test2", _itemAdded: () => {
114
+ } },
115
+ toolCtx: { echo },
116
+ toolCallStream,
117
+ controller: replyAbortController
118
+ });
119
+ await execTask.result;
120
+ (0, import_vitest.expect)(toolOutput.output.length).toBe(1);
121
+ const out = toolOutput.output[0];
122
+ (0, import_vitest.expect)((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(false);
123
+ (0, import_vitest.expect)((_b = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _b.output).toContain("echo: hello");
124
+ });
125
+ (0, import_vitest.it)("should abort tool when reply is aborted mid-execution", async () => {
126
+ var _a;
127
+ const replyAbortController = new AbortController();
128
+ let aborted = false;
129
+ const longOp = (0, import_llm.tool)({
130
+ description: "longOp",
131
+ parameters: import_zod.z.object({ ms: import_zod.z.number() }),
132
+ execute: async ({ ms }, { abortSignal }) => {
133
+ if (abortSignal) {
134
+ abortSignal.addEventListener("abort", () => {
135
+ aborted = true;
136
+ });
137
+ }
138
+ await (0, import_utils.delay)(ms);
139
+ return "done";
140
+ }
141
+ });
142
+ const fc = import_llm.FunctionCall.create({
143
+ callId: "call_abort_1",
144
+ name: "longOp",
145
+ args: JSON.stringify({ ms: 5e3 })
146
+ });
147
+ const toolCallStream = createFunctionCallStream(fc);
148
+ const [execTask, toolOutput] = (0, import_generation.performToolExecutions)({
149
+ session: {},
150
+ speechHandle: { id: "speech_abort", _itemAdded: () => {
151
+ } },
152
+ toolCtx: { longOp },
153
+ toolCallStream,
154
+ controller: replyAbortController
155
+ });
156
+ await toolOutput.firstToolStartedFuture.await;
157
+ replyAbortController.abort();
158
+ await execTask.result;
159
+ (0, import_vitest.expect)(aborted).toBe(true);
160
+ (0, import_vitest.expect)(toolOutput.output.length).toBe(1);
161
+ const out = toolOutput.output[0];
162
+ (0, import_vitest.expect)((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(true);
163
+ }, 2e4);
164
+ (0, import_vitest.it)("should return error output on invalid tool args (zod validation failure)", async () => {
165
+ var _a;
166
+ const replyAbortController = new AbortController();
167
+ const echo = (0, import_llm.tool)({
168
+ description: "echo",
169
+ parameters: import_zod.z.object({ msg: import_zod.z.string() }),
170
+ execute: async ({ msg }) => `echo: ${msg}`
171
+ });
172
+ const fc = import_llm.FunctionCall.create({
173
+ callId: "call_invalid_args",
174
+ name: "echo",
175
+ args: JSON.stringify({ msg: 123 })
176
+ });
177
+ const toolCallStream = createFunctionCallStream(fc);
178
+ const [execTask, toolOutput] = (0, import_generation.performToolExecutions)({
179
+ session: {},
180
+ speechHandle: { id: "speech_invalid", _itemAdded: () => {
181
+ } },
182
+ toolCtx: { echo },
183
+ toolCallStream,
184
+ controller: replyAbortController
185
+ });
186
+ await execTask.result;
187
+ (0, import_vitest.expect)(toolOutput.output.length).toBe(1);
188
+ const out = toolOutput.output[0];
189
+ (0, import_vitest.expect)((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(true);
190
+ });
191
+ (0, import_vitest.it)("should handle multiple tool calls within a single stream", async () => {
192
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
193
+ const replyAbortController = new AbortController();
194
+ const sum = (0, import_llm.tool)({
195
+ description: "sum",
196
+ parameters: import_zod.z.object({ a: import_zod.z.number(), b: import_zod.z.number() }),
197
+ execute: async ({ a, b }) => a + b
198
+ });
199
+ const upper = (0, import_llm.tool)({
200
+ description: "upper",
201
+ parameters: import_zod.z.object({ s: import_zod.z.string() }),
202
+ execute: async ({ s }) => s.toUpperCase()
203
+ });
204
+ const fc1 = import_llm.FunctionCall.create({
205
+ callId: "call_multi_1",
206
+ name: "sum",
207
+ args: JSON.stringify({ a: 2, b: 3 })
208
+ });
209
+ const fc2 = import_llm.FunctionCall.create({
210
+ callId: "call_multi_2",
211
+ name: "upper",
212
+ args: JSON.stringify({ s: "hey" })
213
+ });
214
+ const toolCallStream = createFunctionCallStreamFromArray([fc1, fc2]);
215
+ const [execTask, toolOutput] = (0, import_generation.performToolExecutions)({
216
+ session: {},
217
+ speechHandle: { id: "speech_multi", _itemAdded: () => {
218
+ } },
219
+ toolCtx: { sum, upper },
220
+ toolCallStream,
221
+ controller: replyAbortController
222
+ });
223
+ await execTask.result;
224
+ (0, import_vitest.expect)(toolOutput.output.length).toBe(2);
225
+ const sorted = [...toolOutput.output].sort(
226
+ (a, b) => a.toolCall.callId.localeCompare(b.toolCall.callId)
227
+ );
228
+ (0, import_vitest.expect)((_a = sorted[0]) == null ? void 0 : _a.toolCall.name).toBe("sum");
229
+ (0, import_vitest.expect)((_c = (_b = sorted[0]) == null ? void 0 : _b.toolCallOutput) == null ? void 0 : _c.isError).toBe(false);
230
+ (0, import_vitest.expect)((_e = (_d = sorted[0]) == null ? void 0 : _d.toolCallOutput) == null ? void 0 : _e.output).toBe("5");
231
+ (0, import_vitest.expect)((_f = sorted[1]) == null ? void 0 : _f.toolCall.name).toBe("upper");
232
+ (0, import_vitest.expect)((_h = (_g = sorted[1]) == null ? void 0 : _g.toolCallOutput) == null ? void 0 : _h.isError).toBe(false);
233
+ (0, import_vitest.expect)((_j = (_i = sorted[1]) == null ? void 0 : _i.toolCallOutput) == null ? void 0 : _j.output).toBe('"HEY"');
234
+ });
235
+ });
236
+ //# sourceMappingURL=generation_tools.test.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/voice/generation_tools.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { ReadableStream as NodeReadableStream } from 'stream/web';\nimport { describe, expect, it } from 'vitest';\nimport { z } from 'zod';\nimport { FunctionCall, tool } from '../llm/index.js';\nimport { initializeLogger } from '../log.js';\nimport type { Task } from '../utils.js';\nimport { cancelAndWait, delay } from '../utils.js';\nimport { type _TextOut, performTextForwarding, performToolExecutions } from './generation.js';\n\nfunction createStringStream(chunks: string[], delayMs: number = 0): NodeReadableStream<string> {\n return new NodeReadableStream<string>({\n async start(controller) {\n for (const c of chunks) {\n if (delayMs > 0) {\n await delay(delayMs);\n }\n controller.enqueue(c);\n }\n controller.close();\n },\n });\n}\n\nfunction createFunctionCallStream(fc: FunctionCall): NodeReadableStream<FunctionCall> {\n return new NodeReadableStream<FunctionCall>({\n start(controller) {\n controller.enqueue(fc);\n controller.close();\n },\n });\n}\n\nfunction createFunctionCallStreamFromArray(fcs: FunctionCall[]): NodeReadableStream<FunctionCall> {\n return new NodeReadableStream<FunctionCall>({\n start(controller) {\n for (const fc of fcs) {\n controller.enqueue(fc);\n }\n controller.close();\n },\n });\n}\n\ndescribe('Generation + Tool Execution', () => {\n initializeLogger({ pretty: false, level: 'silent' });\n\n it('should not abort tool when preamble forwarders are cleaned up', async () => {\n const replyAbortController = new AbortController();\n const forwarderController = new AbortController();\n\n const chunks = Array.from({ length: 50 }, () => `Hi.`);\n const fullPreambleText = chunks.join('');\n const preamble = createStringStream(chunks, 20);\n const [textForwardTask, textOut]: [Task<void>, _TextOut] = performTextForwarding(\n preamble,\n forwarderController,\n null,\n );\n\n // Tool that takes > 5 seconds\n let toolAborted = false;\n const getWeather = tool({\n description: 'weather',\n parameters: z.object({ location: z.string() }),\n execute: async ({ location }, { abortSignal }) => {\n if (abortSignal) {\n abortSignal.addEventListener('abort', () => {\n toolAborted = true;\n });\n }\n // 6s delay\n await delay(6000);\n return `Sunny in ${location}`;\n },\n });\n\n const fc = FunctionCall.create({\n callId: 'call_1',\n name: 'getWeather',\n args: JSON.stringify({ location: 'San Francisco' }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_test', _itemAdded: () => {} } as any,\n toolCtx: { getWeather } as any,\n toolCallStream,\n controller: replyAbortController,\n onToolExecutionStarted: () => {},\n onToolExecutionCompleted: () => {},\n });\n\n // Ensure tool has started, then cancel forwarders mid-stream (without aborting parent AbortController)\n await toolOutput.firstToolStartedFuture.await;\n await delay(100);\n await cancelAndWait([textForwardTask], 5000);\n\n await execTask.result;\n\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0]!;\n expect(out.toolCallOutput?.isError).toBe(false);\n expect(out.toolCallOutput?.output).toContain('Sunny in San Francisco');\n // Forwarder should have been cancelled before finishing all preamble chunks\n expect(textOut.text).not.toBe(fullPreambleText);\n // Tool's abort signal must not have fired\n expect(toolAborted).toBe(false);\n }, 30_000);\n\n it('should return basic tool execution output', async () => {\n const replyAbortController = new AbortController();\n\n const echo = tool({\n description: 'echo',\n parameters: z.object({ msg: z.string() }),\n execute: async ({ msg }) => `echo: ${msg}`,\n });\n\n const fc = FunctionCall.create({\n callId: 'call_2',\n name: 'echo',\n args: JSON.stringify({ msg: 'hello' }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_test2', _itemAdded: () => {} } as any,\n toolCtx: { echo } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(false);\n expect(out?.toolCallOutput?.output).toContain('echo: hello');\n });\n\n it('should abort tool when reply is aborted mid-execution', async () => {\n const replyAbortController = new AbortController();\n\n let aborted = false;\n const longOp = tool({\n description: 'longOp',\n parameters: z.object({ ms: z.number() }),\n execute: async ({ ms }, { abortSignal }) => {\n if (abortSignal) {\n abortSignal.addEventListener('abort', () => {\n aborted = true;\n });\n }\n await delay(ms);\n return 'done';\n },\n });\n\n const fc = FunctionCall.create({\n callId: 'call_abort_1',\n name: 'longOp',\n args: JSON.stringify({ ms: 5000 }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_abort', _itemAdded: () => {} } as any,\n toolCtx: { longOp } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await toolOutput.firstToolStartedFuture.await;\n replyAbortController.abort();\n await execTask.result;\n\n expect(aborted).toBe(true);\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(true);\n }, 20_000);\n\n it('should return error output on invalid tool args (zod validation failure)', async () => {\n const replyAbortController = new AbortController();\n\n const echo = tool({\n description: 'echo',\n parameters: z.object({ msg: z.string() }),\n execute: async ({ msg }) => `echo: ${msg}`,\n });\n\n // invalid: msg should be string\n const fc = FunctionCall.create({\n callId: 'call_invalid_args',\n name: 'echo',\n args: JSON.stringify({ msg: 123 }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_invalid', _itemAdded: () => {} } as any,\n toolCtx: { echo } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(true);\n });\n\n it('should handle multiple tool calls within a single stream', async () => {\n const replyAbortController = new AbortController();\n\n const sum = tool({\n description: 'sum',\n parameters: z.object({ a: z.number(), b: z.number() }),\n execute: async ({ a, b }) => a + b,\n });\n const upper = tool({\n description: 'upper',\n parameters: z.object({ s: z.string() }),\n execute: async ({ s }) => s.toUpperCase(),\n });\n\n const fc1 = FunctionCall.create({\n callId: 'call_multi_1',\n name: 'sum',\n args: JSON.stringify({ a: 2, b: 3 }),\n });\n const fc2 = FunctionCall.create({\n callId: 'call_multi_2',\n name: 'upper',\n args: JSON.stringify({ s: 'hey' }),\n });\n const toolCallStream = createFunctionCallStreamFromArray([fc1, fc2]);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_multi', _itemAdded: () => {} } as any,\n toolCtx: { sum, upper } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(2);\n\n // sort by callId to assert deterministically\n const sorted = [...toolOutput.output].sort((a, b) =>\n a.toolCall.callId.localeCompare(b.toolCall.callId),\n );\n\n expect(sorted[0]?.toolCall.name).toBe('sum');\n expect(sorted[0]?.toolCallOutput?.isError).toBe(false);\n expect(sorted[0]?.toolCallOutput?.output).toBe('5');\n expect(sorted[1]?.toolCall.name).toBe('upper');\n expect(sorted[1]?.toolCallOutput?.isError).toBe(false);\n expect(sorted[1]?.toolCallOutput?.output).toBe('\"HEY\"');\n });\n});\n"],"mappings":";AAGA,iBAAqD;AACrD,oBAAqC;AACrC,iBAAkB;AAClB,iBAAmC;AACnC,iBAAiC;AAEjC,mBAAqC;AACrC,wBAA4E;AAE5E,SAAS,mBAAmB,QAAkB,UAAkB,GAA+B;AAC7F,SAAO,IAAI,WAAAA,eAA2B;AAAA,IACpC,MAAM,MAAM,YAAY;AACtB,iBAAW,KAAK,QAAQ;AACtB,YAAI,UAAU,GAAG;AACf,oBAAM,oBAAM,OAAO;AAAA,QACrB;AACA,mBAAW,QAAQ,CAAC;AAAA,MACtB;AACA,iBAAW,MAAM;AAAA,IACnB;AAAA,EACF,CAAC;AACH;AAEA,SAAS,yBAAyB,IAAoD;AACpF,SAAO,IAAI,WAAAA,eAAiC;AAAA,IAC1C,MAAM,YAAY;AAChB,iBAAW,QAAQ,EAAE;AACrB,iBAAW,MAAM;AAAA,IACnB;AAAA,EACF,CAAC;AACH;AAEA,SAAS,kCAAkC,KAAuD;AAChG,SAAO,IAAI,WAAAA,eAAiC;AAAA,IAC1C,MAAM,YAAY;AAChB,iBAAW,MAAM,KAAK;AACpB,mBAAW,QAAQ,EAAE;AAAA,MACvB;AACA,iBAAW,MAAM;AAAA,IACnB;AAAA,EACF,CAAC;AACH;AAAA,IAEA,wBAAS,+BAA+B,MAAM;AAC5C,mCAAiB,EAAE,QAAQ,OAAO,OAAO,SAAS,CAAC;AAEnD,wBAAG,iEAAiE,YAAY;AAjDlF;AAkDI,UAAM,uBAAuB,IAAI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AACD,UAAM,MAAM,wBAAa,OAAO;AAAA,MAC9B,QAAQ;AAAA,MACR,MAAM;AAAA,MACN,MAAM,KAAK,UAAU,EAAE,GAAG,MAAM,CAAC;AAAA,IACnC,CAAC;AACD,UAAM,iBAAiB,kCAAkC,CAAC,KAAK,GAAG,CAAC;AAEnE,UAAM,CAAC,UAAU,UAAU,QAAI,yCAAsB;AAAA,MACnD,SAAS,CAAC;AAAA,MACV,cAAc,EAAE,IAAI,gBAAgB,YAAY,MAAM;AAAA,MAAC,EAAE;AAAA,MACzD,SAAS,EAAE,KAAK,MAAM;AAAA,MACtB;AAAA,MACA,YAAY;AAAA,IACd,CAAC;AAED,UAAM,SAAS;AACf,8BAAO,WAAW,OAAO,MAAM,EAAE,KAAK,CAAC;AAGvC,UAAM,SAAS,CAAC,GAAG,WAAW,MAAM,EAAE;AAAA,MAAK,CAAC,GAAG,MAC7C,EAAE,SAAS,OAAO,cAAc,EAAE,SAAS,MAAM;AAAA,IACnD;AAEA,+BAAO,YAAO,CAAC,MAAR,mBAAW,SAAS,IAAI,EAAE,KAAK,KAAK;AAC3C,+BAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,OAAO,EAAE,KAAK,KAAK;AACrD,+BAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,MAAM,EAAE,KAAK,GAAG;AAClD,+BAAO,YAAO,CAAC,MAAR,mBAAW,SAAS,IAAI,EAAE,KAAK,OAAO;AAC7C,+BAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,OAAO,EAAE,KAAK,KAAK;AACrD,+BAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,MAAM,EAAE,KAAK,OAAO;AAAA,EACxD,CAAC;AACH,CAAC;","names":["NodeReadableStream"]}
@@ -0,0 +1,235 @@
1
+ import { ReadableStream as NodeReadableStream } from "stream/web";
2
+ import { describe, expect, it } from "vitest";
3
+ import { z } from "zod";
4
+ import { FunctionCall, tool } from "../llm/index.js";
5
+ import { initializeLogger } from "../log.js";
6
+ import { cancelAndWait, delay } from "../utils.js";
7
+ import { performTextForwarding, performToolExecutions } from "./generation.js";
8
+ function createStringStream(chunks, delayMs = 0) {
9
+ return new NodeReadableStream({
10
+ async start(controller) {
11
+ for (const c of chunks) {
12
+ if (delayMs > 0) {
13
+ await delay(delayMs);
14
+ }
15
+ controller.enqueue(c);
16
+ }
17
+ controller.close();
18
+ }
19
+ });
20
+ }
21
+ function createFunctionCallStream(fc) {
22
+ return new NodeReadableStream({
23
+ start(controller) {
24
+ controller.enqueue(fc);
25
+ controller.close();
26
+ }
27
+ });
28
+ }
29
+ function createFunctionCallStreamFromArray(fcs) {
30
+ return new NodeReadableStream({
31
+ start(controller) {
32
+ for (const fc of fcs) {
33
+ controller.enqueue(fc);
34
+ }
35
+ controller.close();
36
+ }
37
+ });
38
+ }
39
+ describe("Generation + Tool Execution", () => {
40
+ initializeLogger({ pretty: false, level: "silent" });
41
+ it("should not abort tool when preamble forwarders are cleaned up", async () => {
42
+ var _a, _b;
43
+ const replyAbortController = new AbortController();
44
+ const forwarderController = new AbortController();
45
+ const chunks = Array.from({ length: 50 }, () => `Hi.`);
46
+ const fullPreambleText = chunks.join("");
47
+ const preamble = createStringStream(chunks, 20);
48
+ const [textForwardTask, textOut] = performTextForwarding(
49
+ preamble,
50
+ forwarderController,
51
+ null
52
+ );
53
+ let toolAborted = false;
54
+ const getWeather = tool({
55
+ description: "weather",
56
+ parameters: z.object({ location: z.string() }),
57
+ execute: async ({ location }, { abortSignal }) => {
58
+ if (abortSignal) {
59
+ abortSignal.addEventListener("abort", () => {
60
+ toolAborted = true;
61
+ });
62
+ }
63
+ await delay(6e3);
64
+ return `Sunny in ${location}`;
65
+ }
66
+ });
67
+ const fc = FunctionCall.create({
68
+ callId: "call_1",
69
+ name: "getWeather",
70
+ args: JSON.stringify({ location: "San Francisco" })
71
+ });
72
+ const toolCallStream = createFunctionCallStream(fc);
73
+ const [execTask, toolOutput] = performToolExecutions({
74
+ session: {},
75
+ speechHandle: { id: "speech_test", _itemAdded: () => {
76
+ } },
77
+ toolCtx: { getWeather },
78
+ toolCallStream,
79
+ controller: replyAbortController,
80
+ onToolExecutionStarted: () => {
81
+ },
82
+ onToolExecutionCompleted: () => {
83
+ }
84
+ });
85
+ await toolOutput.firstToolStartedFuture.await;
86
+ await delay(100);
87
+ await cancelAndWait([textForwardTask], 5e3);
88
+ await execTask.result;
89
+ expect(toolOutput.output.length).toBe(1);
90
+ const out = toolOutput.output[0];
91
+ expect((_a = out.toolCallOutput) == null ? void 0 : _a.isError).toBe(false);
92
+ expect((_b = out.toolCallOutput) == null ? void 0 : _b.output).toContain("Sunny in San Francisco");
93
+ expect(textOut.text).not.toBe(fullPreambleText);
94
+ expect(toolAborted).toBe(false);
95
+ }, 3e4);
96
+ it("should return basic tool execution output", async () => {
97
+ var _a, _b;
98
+ const replyAbortController = new AbortController();
99
+ const echo = tool({
100
+ description: "echo",
101
+ parameters: z.object({ msg: z.string() }),
102
+ execute: async ({ msg }) => `echo: ${msg}`
103
+ });
104
+ const fc = FunctionCall.create({
105
+ callId: "call_2",
106
+ name: "echo",
107
+ args: JSON.stringify({ msg: "hello" })
108
+ });
109
+ const toolCallStream = createFunctionCallStream(fc);
110
+ const [execTask, toolOutput] = performToolExecutions({
111
+ session: {},
112
+ speechHandle: { id: "speech_test2", _itemAdded: () => {
113
+ } },
114
+ toolCtx: { echo },
115
+ toolCallStream,
116
+ controller: replyAbortController
117
+ });
118
+ await execTask.result;
119
+ expect(toolOutput.output.length).toBe(1);
120
+ const out = toolOutput.output[0];
121
+ expect((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(false);
122
+ expect((_b = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _b.output).toContain("echo: hello");
123
+ });
124
+ it("should abort tool when reply is aborted mid-execution", async () => {
125
+ var _a;
126
+ const replyAbortController = new AbortController();
127
+ let aborted = false;
128
+ const longOp = tool({
129
+ description: "longOp",
130
+ parameters: z.object({ ms: z.number() }),
131
+ execute: async ({ ms }, { abortSignal }) => {
132
+ if (abortSignal) {
133
+ abortSignal.addEventListener("abort", () => {
134
+ aborted = true;
135
+ });
136
+ }
137
+ await delay(ms);
138
+ return "done";
139
+ }
140
+ });
141
+ const fc = FunctionCall.create({
142
+ callId: "call_abort_1",
143
+ name: "longOp",
144
+ args: JSON.stringify({ ms: 5e3 })
145
+ });
146
+ const toolCallStream = createFunctionCallStream(fc);
147
+ const [execTask, toolOutput] = performToolExecutions({
148
+ session: {},
149
+ speechHandle: { id: "speech_abort", _itemAdded: () => {
150
+ } },
151
+ toolCtx: { longOp },
152
+ toolCallStream,
153
+ controller: replyAbortController
154
+ });
155
+ await toolOutput.firstToolStartedFuture.await;
156
+ replyAbortController.abort();
157
+ await execTask.result;
158
+ expect(aborted).toBe(true);
159
+ expect(toolOutput.output.length).toBe(1);
160
+ const out = toolOutput.output[0];
161
+ expect((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(true);
162
+ }, 2e4);
163
+ it("should return error output on invalid tool args (zod validation failure)", async () => {
164
+ var _a;
165
+ const replyAbortController = new AbortController();
166
+ const echo = tool({
167
+ description: "echo",
168
+ parameters: z.object({ msg: z.string() }),
169
+ execute: async ({ msg }) => `echo: ${msg}`
170
+ });
171
+ const fc = FunctionCall.create({
172
+ callId: "call_invalid_args",
173
+ name: "echo",
174
+ args: JSON.stringify({ msg: 123 })
175
+ });
176
+ const toolCallStream = createFunctionCallStream(fc);
177
+ const [execTask, toolOutput] = performToolExecutions({
178
+ session: {},
179
+ speechHandle: { id: "speech_invalid", _itemAdded: () => {
180
+ } },
181
+ toolCtx: { echo },
182
+ toolCallStream,
183
+ controller: replyAbortController
184
+ });
185
+ await execTask.result;
186
+ expect(toolOutput.output.length).toBe(1);
187
+ const out = toolOutput.output[0];
188
+ expect((_a = out == null ? void 0 : out.toolCallOutput) == null ? void 0 : _a.isError).toBe(true);
189
+ });
190
+ it("should handle multiple tool calls within a single stream", async () => {
191
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
192
+ const replyAbortController = new AbortController();
193
+ const sum = tool({
194
+ description: "sum",
195
+ parameters: z.object({ a: z.number(), b: z.number() }),
196
+ execute: async ({ a, b }) => a + b
197
+ });
198
+ const upper = tool({
199
+ description: "upper",
200
+ parameters: z.object({ s: z.string() }),
201
+ execute: async ({ s }) => s.toUpperCase()
202
+ });
203
+ const fc1 = FunctionCall.create({
204
+ callId: "call_multi_1",
205
+ name: "sum",
206
+ args: JSON.stringify({ a: 2, b: 3 })
207
+ });
208
+ const fc2 = FunctionCall.create({
209
+ callId: "call_multi_2",
210
+ name: "upper",
211
+ args: JSON.stringify({ s: "hey" })
212
+ });
213
+ const toolCallStream = createFunctionCallStreamFromArray([fc1, fc2]);
214
+ const [execTask, toolOutput] = performToolExecutions({
215
+ session: {},
216
+ speechHandle: { id: "speech_multi", _itemAdded: () => {
217
+ } },
218
+ toolCtx: { sum, upper },
219
+ toolCallStream,
220
+ controller: replyAbortController
221
+ });
222
+ await execTask.result;
223
+ expect(toolOutput.output.length).toBe(2);
224
+ const sorted = [...toolOutput.output].sort(
225
+ (a, b) => a.toolCall.callId.localeCompare(b.toolCall.callId)
226
+ );
227
+ expect((_a = sorted[0]) == null ? void 0 : _a.toolCall.name).toBe("sum");
228
+ expect((_c = (_b = sorted[0]) == null ? void 0 : _b.toolCallOutput) == null ? void 0 : _c.isError).toBe(false);
229
+ expect((_e = (_d = sorted[0]) == null ? void 0 : _d.toolCallOutput) == null ? void 0 : _e.output).toBe("5");
230
+ expect((_f = sorted[1]) == null ? void 0 : _f.toolCall.name).toBe("upper");
231
+ expect((_h = (_g = sorted[1]) == null ? void 0 : _g.toolCallOutput) == null ? void 0 : _h.isError).toBe(false);
232
+ expect((_j = (_i = sorted[1]) == null ? void 0 : _i.toolCallOutput) == null ? void 0 : _j.output).toBe('"HEY"');
233
+ });
234
+ });
235
+ //# sourceMappingURL=generation_tools.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/voice/generation_tools.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { ReadableStream as NodeReadableStream } from 'stream/web';\nimport { describe, expect, it } from 'vitest';\nimport { z } from 'zod';\nimport { FunctionCall, tool } from '../llm/index.js';\nimport { initializeLogger } from '../log.js';\nimport type { Task } from '../utils.js';\nimport { cancelAndWait, delay } from '../utils.js';\nimport { type _TextOut, performTextForwarding, performToolExecutions } from './generation.js';\n\nfunction createStringStream(chunks: string[], delayMs: number = 0): NodeReadableStream<string> {\n return new NodeReadableStream<string>({\n async start(controller) {\n for (const c of chunks) {\n if (delayMs > 0) {\n await delay(delayMs);\n }\n controller.enqueue(c);\n }\n controller.close();\n },\n });\n}\n\nfunction createFunctionCallStream(fc: FunctionCall): NodeReadableStream<FunctionCall> {\n return new NodeReadableStream<FunctionCall>({\n start(controller) {\n controller.enqueue(fc);\n controller.close();\n },\n });\n}\n\nfunction createFunctionCallStreamFromArray(fcs: FunctionCall[]): NodeReadableStream<FunctionCall> {\n return new NodeReadableStream<FunctionCall>({\n start(controller) {\n for (const fc of fcs) {\n controller.enqueue(fc);\n }\n controller.close();\n },\n });\n}\n\ndescribe('Generation + Tool Execution', () => {\n initializeLogger({ pretty: false, level: 'silent' });\n\n it('should not abort tool when preamble forwarders are cleaned up', async () => {\n const replyAbortController = new AbortController();\n const forwarderController = new AbortController();\n\n const chunks = Array.from({ length: 50 }, () => `Hi.`);\n const fullPreambleText = chunks.join('');\n const preamble = createStringStream(chunks, 20);\n const [textForwardTask, textOut]: [Task<void>, _TextOut] = performTextForwarding(\n preamble,\n forwarderController,\n null,\n );\n\n // Tool that takes > 5 seconds\n let toolAborted = false;\n const getWeather = tool({\n description: 'weather',\n parameters: z.object({ location: z.string() }),\n execute: async ({ location }, { abortSignal }) => {\n if (abortSignal) {\n abortSignal.addEventListener('abort', () => {\n toolAborted = true;\n });\n }\n // 6s delay\n await delay(6000);\n return `Sunny in ${location}`;\n },\n });\n\n const fc = FunctionCall.create({\n callId: 'call_1',\n name: 'getWeather',\n args: JSON.stringify({ location: 'San Francisco' }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_test', _itemAdded: () => {} } as any,\n toolCtx: { getWeather } as any,\n toolCallStream,\n controller: replyAbortController,\n onToolExecutionStarted: () => {},\n onToolExecutionCompleted: () => {},\n });\n\n // Ensure tool has started, then cancel forwarders mid-stream (without aborting parent AbortController)\n await toolOutput.firstToolStartedFuture.await;\n await delay(100);\n await cancelAndWait([textForwardTask], 5000);\n\n await execTask.result;\n\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0]!;\n expect(out.toolCallOutput?.isError).toBe(false);\n expect(out.toolCallOutput?.output).toContain('Sunny in San Francisco');\n // Forwarder should have been cancelled before finishing all preamble chunks\n expect(textOut.text).not.toBe(fullPreambleText);\n // Tool's abort signal must not have fired\n expect(toolAborted).toBe(false);\n }, 30_000);\n\n it('should return basic tool execution output', async () => {\n const replyAbortController = new AbortController();\n\n const echo = tool({\n description: 'echo',\n parameters: z.object({ msg: z.string() }),\n execute: async ({ msg }) => `echo: ${msg}`,\n });\n\n const fc = FunctionCall.create({\n callId: 'call_2',\n name: 'echo',\n args: JSON.stringify({ msg: 'hello' }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_test2', _itemAdded: () => {} } as any,\n toolCtx: { echo } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(false);\n expect(out?.toolCallOutput?.output).toContain('echo: hello');\n });\n\n it('should abort tool when reply is aborted mid-execution', async () => {\n const replyAbortController = new AbortController();\n\n let aborted = false;\n const longOp = tool({\n description: 'longOp',\n parameters: z.object({ ms: z.number() }),\n execute: async ({ ms }, { abortSignal }) => {\n if (abortSignal) {\n abortSignal.addEventListener('abort', () => {\n aborted = true;\n });\n }\n await delay(ms);\n return 'done';\n },\n });\n\n const fc = FunctionCall.create({\n callId: 'call_abort_1',\n name: 'longOp',\n args: JSON.stringify({ ms: 5000 }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_abort', _itemAdded: () => {} } as any,\n toolCtx: { longOp } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await toolOutput.firstToolStartedFuture.await;\n replyAbortController.abort();\n await execTask.result;\n\n expect(aborted).toBe(true);\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(true);\n }, 20_000);\n\n it('should return error output on invalid tool args (zod validation failure)', async () => {\n const replyAbortController = new AbortController();\n\n const echo = tool({\n description: 'echo',\n parameters: z.object({ msg: z.string() }),\n execute: async ({ msg }) => `echo: ${msg}`,\n });\n\n // invalid: msg should be string\n const fc = FunctionCall.create({\n callId: 'call_invalid_args',\n name: 'echo',\n args: JSON.stringify({ msg: 123 }),\n });\n const toolCallStream = createFunctionCallStream(fc);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_invalid', _itemAdded: () => {} } as any,\n toolCtx: { echo } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(1);\n const out = toolOutput.output[0];\n expect(out?.toolCallOutput?.isError).toBe(true);\n });\n\n it('should handle multiple tool calls within a single stream', async () => {\n const replyAbortController = new AbortController();\n\n const sum = tool({\n description: 'sum',\n parameters: z.object({ a: z.number(), b: z.number() }),\n execute: async ({ a, b }) => a + b,\n });\n const upper = tool({\n description: 'upper',\n parameters: z.object({ s: z.string() }),\n execute: async ({ s }) => s.toUpperCase(),\n });\n\n const fc1 = FunctionCall.create({\n callId: 'call_multi_1',\n name: 'sum',\n args: JSON.stringify({ a: 2, b: 3 }),\n });\n const fc2 = FunctionCall.create({\n callId: 'call_multi_2',\n name: 'upper',\n args: JSON.stringify({ s: 'hey' }),\n });\n const toolCallStream = createFunctionCallStreamFromArray([fc1, fc2]);\n\n const [execTask, toolOutput] = performToolExecutions({\n session: {} as any,\n speechHandle: { id: 'speech_multi', _itemAdded: () => {} } as any,\n toolCtx: { sum, upper } as any,\n toolCallStream,\n controller: replyAbortController,\n });\n\n await execTask.result;\n expect(toolOutput.output.length).toBe(2);\n\n // sort by callId to assert deterministically\n const sorted = [...toolOutput.output].sort((a, b) =>\n a.toolCall.callId.localeCompare(b.toolCall.callId),\n );\n\n expect(sorted[0]?.toolCall.name).toBe('sum');\n expect(sorted[0]?.toolCallOutput?.isError).toBe(false);\n expect(sorted[0]?.toolCallOutput?.output).toBe('5');\n expect(sorted[1]?.toolCall.name).toBe('upper');\n expect(sorted[1]?.toolCallOutput?.isError).toBe(false);\n expect(sorted[1]?.toolCallOutput?.output).toBe('\"HEY\"');\n });\n});\n"],"mappings":"AAGA,SAAS,kBAAkB,0BAA0B;AACrD,SAAS,UAAU,QAAQ,UAAU;AACrC,SAAS,SAAS;AAClB,SAAS,cAAc,YAAY;AACnC,SAAS,wBAAwB;AAEjC,SAAS,eAAe,aAAa;AACrC,SAAwB,uBAAuB,6BAA6B;AAE5E,SAAS,mBAAmB,QAAkB,UAAkB,GAA+B;AAC7F,SAAO,IAAI,mBAA2B;AAAA,IACpC,MAAM,MAAM,YAAY;AACtB,iBAAW,KAAK,QAAQ;AACtB,YAAI,UAAU,GAAG;AACf,gBAAM,MAAM,OAAO;AAAA,QACrB;AACA,mBAAW,QAAQ,CAAC;AAAA,MACtB;AACA,iBAAW,MAAM;AAAA,IACnB;AAAA,EACF,CAAC;AACH;AAEA,SAAS,yBAAyB,IAAoD;AACpF,SAAO,IAA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AK,GAAG;AAClD,YAAO,YAAO,CAAC,MAAR,mBAAW,SAAS,IAAI,EAAE,KAAK,OAAO;AAC7C,YAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,OAAO,EAAE,KAAK,KAAK;AACrD,YAAO,kBAAO,CAAC,MAAR,mBAAW,mBAAX,mBAA2B,MAAM,EAAE,KAAK,OAAO;AAAA,EACxD,CAAC;AACH,CAAC;","names":[]}
@@ -125,6 +125,7 @@ class SpeechHandle {
125
125
  To wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead.`
126
126
  );
127
127
  }
128
+ await this.doneFut.await;
128
129
  }
129
130
  async waitIfNotInterrupted(aw) {
130
131
  const allTasksPromise = Promise.all(aw);
@@ -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":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAyC;AAEzC,mBAAkC;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,oBAAa;AAAA,EAChC,kBAAkB,IAAI,mBAAM;AAAA,EAC5B,eAAe,IAAI,oBAAa;AAAA,EAChC,UAAU,IAAI,oBAAa;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,OAAO,SAIX;AACD,UAAM,EAAE,qBAAqB,MAAM,YAAY,GAAG,OAAO,IAAI,WAAW,CAAC;AAEzE,WAAO,IAAI,iBAAa,wBAAU,SAAS,GAAG,oBAAoB,WAAW,MAAM;AAAA,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ACF;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":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAyC;AAEzC,mBAAkC;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,oBAAa;AAAA,EAChC,kBAAkB,IAAI,mBAAM;AAAA,EAC5B,eAAe,IAAI,oBAAa;AAAA,EAChC,UAAU,IAAI,oBAAa;AAAA,EAE3B,cAA8B,CAAC;AAAA;AAAA,EAEvC,SAAuB,CAAC;AAAA,EAChB,aAAyB,CAAC;AAAA,EAC1B,YAAY;AAAA,EAEZ,qBAAoD,oBAAI,IAAI;AAAA,EAC5D,gBAAi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OAAO,CAAC,IAAI,CAAC;IAa/B,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAMjE,eAAe,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,IAAI;IAIpD,kBAAkB,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,IAAI;IAIvD,gBAAgB;IAChB,OAAO,IAAI,YAAY;IAYvB,gBAAgB;IAChB,oBAAoB,IAAI,IAAI;IAM5B,gBAAgB;IAChB,mBAAmB,IAAI,IAAI;IAI3B,gBAAgB;IACV,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5C,gBAAgB;IACV,kBAAkB,CAAC,OAAO,GAAE,MAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAa7D,gBAAgB;IACV,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxC,gBAAgB;IAChB,mBAAmB,IAAI,IAAI;IAW3B,gBAAgB;IAChB,SAAS,IAAI,IAAI;IASjB,gBAAgB;IAChB,cAAc,IAAI,IAAI;IAMtB,gBAAgB;IAChB,qBAAqB,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,GAAG,IAAI;IAI/D,gBAAgB;IAChB,wBAAwB,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,GAAG,IAAI;IAIlE,gBAAgB;IAChB,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAI;CAQpC"}
@@ -102,6 +102,7 @@ class SpeechHandle {
102
102
  To wait for the assistant's spoken response prior to running this tool, use RunContext.wait_for_playout() instead.`
103
103
  );
104
104
  }
105
+ await this.doneFut.await;
105
106
  }
106
107
  async waitIfNotInterrupted(aw) {
107
108
  const allTasksPromise = Promise.all(aw);