@livekit/agents 1.0.9 → 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 +4 -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 +4 -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/package.json +1 -1
- package/src/voice/agent_activity.ts +5 -1
- package/src/voice/generation_tools.test.ts +268 -0
|
@@ -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
|
+
});
|