@livekit/agents 1.0.45 → 1.0.47
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/cli.cjs +14 -20
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -20
- package/dist/cli.js.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +14 -5
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +14 -5
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/llm/chat_context.cjs +19 -0
- package/dist/llm/chat_context.cjs.map +1 -1
- package/dist/llm/chat_context.d.cts +4 -0
- package/dist/llm/chat_context.d.ts +4 -0
- package/dist/llm/chat_context.d.ts.map +1 -1
- package/dist/llm/chat_context.js +19 -0
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/provider_format/index.cjs +2 -0
- package/dist/llm/provider_format/index.cjs.map +1 -1
- package/dist/llm/provider_format/index.d.cts +1 -1
- package/dist/llm/provider_format/index.d.ts +1 -1
- package/dist/llm/provider_format/index.d.ts.map +1 -1
- package/dist/llm/provider_format/index.js +6 -1
- package/dist/llm/provider_format/index.js.map +1 -1
- package/dist/llm/provider_format/openai.cjs +82 -2
- package/dist/llm/provider_format/openai.cjs.map +1 -1
- package/dist/llm/provider_format/openai.d.cts +1 -0
- package/dist/llm/provider_format/openai.d.ts +1 -0
- package/dist/llm/provider_format/openai.d.ts.map +1 -1
- package/dist/llm/provider_format/openai.js +80 -1
- package/dist/llm/provider_format/openai.js.map +1 -1
- package/dist/llm/provider_format/openai.test.cjs +326 -0
- package/dist/llm/provider_format/openai.test.cjs.map +1 -1
- package/dist/llm/provider_format/openai.test.js +327 -1
- package/dist/llm/provider_format/openai.test.js.map +1 -1
- package/dist/llm/provider_format/utils.cjs +4 -3
- package/dist/llm/provider_format/utils.cjs.map +1 -1
- package/dist/llm/provider_format/utils.d.ts.map +1 -1
- package/dist/llm/provider_format/utils.js +4 -3
- package/dist/llm/provider_format/utils.js.map +1 -1
- package/dist/llm/realtime.cjs.map +1 -1
- package/dist/llm/realtime.d.cts +1 -0
- package/dist/llm/realtime.d.ts +1 -0
- package/dist/llm/realtime.d.ts.map +1 -1
- package/dist/llm/realtime.js.map +1 -1
- package/dist/log.cjs +5 -2
- package/dist/log.cjs.map +1 -1
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +5 -2
- package/dist/log.js.map +1 -1
- package/dist/stream/deferred_stream.cjs +15 -6
- package/dist/stream/deferred_stream.cjs.map +1 -1
- package/dist/stream/deferred_stream.d.ts.map +1 -1
- package/dist/stream/deferred_stream.js +15 -6
- package/dist/stream/deferred_stream.js.map +1 -1
- package/dist/stream/index.cjs +3 -0
- package/dist/stream/index.cjs.map +1 -1
- package/dist/stream/index.d.cts +1 -0
- package/dist/stream/index.d.ts +1 -0
- package/dist/stream/index.d.ts.map +1 -1
- package/dist/stream/index.js +2 -0
- package/dist/stream/index.js.map +1 -1
- package/dist/stream/multi_input_stream.cjs +139 -0
- package/dist/stream/multi_input_stream.cjs.map +1 -0
- package/dist/stream/multi_input_stream.d.cts +55 -0
- package/dist/stream/multi_input_stream.d.ts +55 -0
- package/dist/stream/multi_input_stream.d.ts.map +1 -0
- package/dist/stream/multi_input_stream.js +115 -0
- package/dist/stream/multi_input_stream.js.map +1 -0
- package/dist/stream/multi_input_stream.test.cjs +340 -0
- package/dist/stream/multi_input_stream.test.cjs.map +1 -0
- package/dist/stream/multi_input_stream.test.js +339 -0
- package/dist/stream/multi_input_stream.test.js.map +1 -0
- package/dist/telemetry/trace_types.cjs +42 -0
- package/dist/telemetry/trace_types.cjs.map +1 -1
- package/dist/telemetry/trace_types.d.cts +14 -0
- package/dist/telemetry/trace_types.d.ts +14 -0
- package/dist/telemetry/trace_types.d.ts.map +1 -1
- package/dist/telemetry/trace_types.js +28 -0
- package/dist/telemetry/trace_types.js.map +1 -1
- package/dist/utils.cjs +44 -2
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +44 -2
- package/dist/utils.js.map +1 -1
- package/dist/utils.test.cjs +71 -0
- package/dist/utils.test.cjs.map +1 -1
- package/dist/utils.test.js +71 -0
- package/dist/utils.test.js.map +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.cjs.map +1 -1
- package/dist/version.d.cts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/voice/agent.cjs +144 -12
- package/dist/voice/agent.cjs.map +1 -1
- package/dist/voice/agent.d.cts +29 -4
- package/dist/voice/agent.d.ts +29 -4
- package/dist/voice/agent.d.ts.map +1 -1
- package/dist/voice/agent.js +140 -11
- package/dist/voice/agent.js.map +1 -1
- package/dist/voice/agent.test.cjs +120 -0
- package/dist/voice/agent.test.cjs.map +1 -1
- package/dist/voice/agent.test.js +122 -2
- package/dist/voice/agent.test.js.map +1 -1
- package/dist/voice/agent_activity.cjs +402 -292
- package/dist/voice/agent_activity.cjs.map +1 -1
- package/dist/voice/agent_activity.d.cts +35 -7
- package/dist/voice/agent_activity.d.ts +35 -7
- package/dist/voice/agent_activity.d.ts.map +1 -1
- package/dist/voice/agent_activity.js +402 -287
- package/dist/voice/agent_activity.js.map +1 -1
- package/dist/voice/agent_session.cjs +156 -44
- package/dist/voice/agent_session.cjs.map +1 -1
- package/dist/voice/agent_session.d.cts +22 -9
- package/dist/voice/agent_session.d.ts +22 -9
- package/dist/voice/agent_session.d.ts.map +1 -1
- package/dist/voice/agent_session.js +156 -44
- package/dist/voice/agent_session.js.map +1 -1
- package/dist/voice/audio_recognition.cjs +89 -36
- package/dist/voice/audio_recognition.cjs.map +1 -1
- package/dist/voice/audio_recognition.d.cts +22 -1
- package/dist/voice/audio_recognition.d.ts +22 -1
- package/dist/voice/audio_recognition.d.ts.map +1 -1
- package/dist/voice/audio_recognition.js +93 -36
- package/dist/voice/audio_recognition.js.map +1 -1
- package/dist/voice/audio_recognition_span.test.cjs +233 -0
- package/dist/voice/audio_recognition_span.test.cjs.map +1 -0
- package/dist/voice/audio_recognition_span.test.js +232 -0
- package/dist/voice/audio_recognition_span.test.js.map +1 -0
- package/dist/voice/generation.cjs +39 -19
- package/dist/voice/generation.cjs.map +1 -1
- package/dist/voice/generation.d.ts.map +1 -1
- package/dist/voice/generation.js +44 -20
- package/dist/voice/generation.js.map +1 -1
- package/dist/voice/index.cjs +2 -0
- package/dist/voice/index.cjs.map +1 -1
- package/dist/voice/index.d.cts +1 -1
- package/dist/voice/index.d.ts +1 -1
- package/dist/voice/index.d.ts.map +1 -1
- package/dist/voice/index.js +2 -1
- package/dist/voice/index.js.map +1 -1
- package/dist/voice/io.cjs +6 -3
- package/dist/voice/io.cjs.map +1 -1
- package/dist/voice/io.d.cts +3 -2
- package/dist/voice/io.d.ts +3 -2
- package/dist/voice/io.d.ts.map +1 -1
- package/dist/voice/io.js +6 -3
- package/dist/voice/io.js.map +1 -1
- package/dist/voice/recorder_io/recorder_io.cjs +3 -1
- package/dist/voice/recorder_io/recorder_io.cjs.map +1 -1
- package/dist/voice/recorder_io/recorder_io.d.ts.map +1 -1
- package/dist/voice/recorder_io/recorder_io.js +3 -1
- package/dist/voice/recorder_io/recorder_io.js.map +1 -1
- package/dist/voice/room_io/_input.cjs +17 -17
- package/dist/voice/room_io/_input.cjs.map +1 -1
- package/dist/voice/room_io/_input.d.cts +2 -2
- package/dist/voice/room_io/_input.d.ts +2 -2
- package/dist/voice/room_io/_input.d.ts.map +1 -1
- package/dist/voice/room_io/_input.js +7 -6
- package/dist/voice/room_io/_input.js.map +1 -1
- package/dist/voice/room_io/room_io.cjs +9 -0
- package/dist/voice/room_io/room_io.cjs.map +1 -1
- package/dist/voice/room_io/room_io.d.cts +3 -1
- package/dist/voice/room_io/room_io.d.ts +3 -1
- package/dist/voice/room_io/room_io.d.ts.map +1 -1
- package/dist/voice/room_io/room_io.js +9 -0
- package/dist/voice/room_io/room_io.js.map +1 -1
- package/dist/voice/speech_handle.cjs +7 -1
- package/dist/voice/speech_handle.cjs.map +1 -1
- package/dist/voice/speech_handle.d.cts +2 -0
- package/dist/voice/speech_handle.d.ts +2 -0
- package/dist/voice/speech_handle.d.ts.map +1 -1
- package/dist/voice/speech_handle.js +8 -2
- package/dist/voice/speech_handle.js.map +1 -1
- package/dist/voice/testing/run_result.cjs +66 -15
- package/dist/voice/testing/run_result.cjs.map +1 -1
- package/dist/voice/testing/run_result.d.cts +14 -3
- package/dist/voice/testing/run_result.d.ts +14 -3
- package/dist/voice/testing/run_result.d.ts.map +1 -1
- package/dist/voice/testing/run_result.js +66 -15
- package/dist/voice/testing/run_result.js.map +1 -1
- package/dist/voice/utils.cjs +47 -0
- package/dist/voice/utils.cjs.map +1 -0
- package/dist/voice/utils.d.cts +4 -0
- package/dist/voice/utils.d.ts +4 -0
- package/dist/voice/utils.d.ts.map +1 -0
- package/dist/voice/utils.js +23 -0
- package/dist/voice/utils.js.map +1 -0
- package/package.json +1 -1
- package/src/cli.ts +20 -33
- package/src/ipc/job_proc_lazy_main.ts +16 -5
- package/src/llm/chat_context.ts +35 -0
- package/src/llm/provider_format/index.ts +7 -2
- package/src/llm/provider_format/openai.test.ts +385 -1
- package/src/llm/provider_format/openai.ts +103 -0
- package/src/llm/provider_format/utils.ts +6 -4
- package/src/llm/realtime.ts +1 -0
- package/src/log.ts +5 -2
- package/src/stream/deferred_stream.ts +17 -6
- package/src/stream/index.ts +1 -0
- package/src/stream/multi_input_stream.test.ts +540 -0
- package/src/stream/multi_input_stream.ts +172 -0
- package/src/telemetry/trace_types.ts +18 -0
- package/src/utils.test.ts +87 -0
- package/src/utils.ts +52 -2
- package/src/version.ts +1 -1
- package/src/voice/agent.test.ts +140 -2
- package/src/voice/agent.ts +189 -10
- package/src/voice/agent_activity.ts +449 -286
- package/src/voice/agent_session.ts +195 -51
- package/src/voice/audio_recognition.ts +118 -38
- package/src/voice/audio_recognition_span.test.ts +261 -0
- package/src/voice/generation.ts +52 -23
- package/src/voice/index.ts +1 -1
- package/src/voice/io.ts +7 -4
- package/src/voice/recorder_io/recorder_io.ts +2 -1
- package/src/voice/room_io/_input.ts +11 -7
- package/src/voice/room_io/room_io.ts +12 -0
- package/src/voice/speech_handle.ts +9 -2
- package/src/voice/testing/run_result.ts +81 -23
- package/src/voice/utils.ts +29 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { ReadableStream } from 'node:stream/web';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { delay } from '../utils.js';
|
|
7
|
+
import { MultiInputStream } from './multi_input_stream.js';
|
|
8
|
+
|
|
9
|
+
function streamFrom<T>(values: T[]): ReadableStream<T> {
|
|
10
|
+
return new ReadableStream<T>({
|
|
11
|
+
start(controller) {
|
|
12
|
+
for (const v of values) controller.enqueue(v);
|
|
13
|
+
controller.close();
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('MultiInputStream', () => {
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Basic functionality
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
it('should create a readable output stream', () => {
|
|
24
|
+
const multi = new MultiInputStream<string>();
|
|
25
|
+
expect(multi.stream).toBeInstanceOf(ReadableStream);
|
|
26
|
+
expect(multi.inputCount).toBe(0);
|
|
27
|
+
expect(multi.isClosed).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should read data from a single input stream', async () => {
|
|
31
|
+
const multi = new MultiInputStream<string>();
|
|
32
|
+
const reader = multi.stream.getReader();
|
|
33
|
+
|
|
34
|
+
multi.addInputStream(streamFrom(['a', 'b', 'c']));
|
|
35
|
+
|
|
36
|
+
const results: string[] = [];
|
|
37
|
+
// Read three values then close manually (output stays open after input ends).
|
|
38
|
+
for (let i = 0; i < 3; i++) {
|
|
39
|
+
const { value } = await reader.read();
|
|
40
|
+
results.push(value!);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
expect(results).toEqual(['a', 'b', 'c']);
|
|
44
|
+
reader.releaseLock();
|
|
45
|
+
await multi.close();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should merge data from multiple input streams', async () => {
|
|
49
|
+
const multi = new MultiInputStream<number>();
|
|
50
|
+
const reader = multi.stream.getReader();
|
|
51
|
+
|
|
52
|
+
multi.addInputStream(streamFrom([1, 2]));
|
|
53
|
+
multi.addInputStream(streamFrom([3, 4]));
|
|
54
|
+
|
|
55
|
+
const results: number[] = [];
|
|
56
|
+
for (let i = 0; i < 4; i++) {
|
|
57
|
+
const { value } = await reader.read();
|
|
58
|
+
results.push(value!);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Order is non-deterministic but all values must arrive.
|
|
62
|
+
expect(results.sort()).toEqual([1, 2, 3, 4]);
|
|
63
|
+
reader.releaseLock();
|
|
64
|
+
await multi.close();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Dynamic add / remove
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
it('should allow adding inputs dynamically while reading', async () => {
|
|
72
|
+
const multi = new MultiInputStream<string>();
|
|
73
|
+
const reader = multi.stream.getReader();
|
|
74
|
+
|
|
75
|
+
multi.addInputStream(streamFrom(['first']));
|
|
76
|
+
|
|
77
|
+
const r1 = await reader.read();
|
|
78
|
+
expect(r1.value).toBe('first');
|
|
79
|
+
|
|
80
|
+
// Add a second input after reading from the first.
|
|
81
|
+
multi.addInputStream(streamFrom(['second']));
|
|
82
|
+
|
|
83
|
+
const r2 = await reader.read();
|
|
84
|
+
expect(r2.value).toBe('second');
|
|
85
|
+
|
|
86
|
+
reader.releaseLock();
|
|
87
|
+
await multi.close();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should continue reading from remaining inputs after removing one', async () => {
|
|
91
|
+
const multi = new MultiInputStream<string>();
|
|
92
|
+
const reader = multi.stream.getReader();
|
|
93
|
+
|
|
94
|
+
// A slow stream that emits over time.
|
|
95
|
+
const slowSource = new ReadableStream<string>({
|
|
96
|
+
async start(controller) {
|
|
97
|
+
controller.enqueue('slow-1');
|
|
98
|
+
await delay(50);
|
|
99
|
+
controller.enqueue('slow-2');
|
|
100
|
+
await delay(50);
|
|
101
|
+
controller.enqueue('slow-3');
|
|
102
|
+
controller.close();
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const slowId = multi.addInputStream(slowSource);
|
|
107
|
+
|
|
108
|
+
// Read first value from slow source.
|
|
109
|
+
const r1 = await reader.read();
|
|
110
|
+
expect(r1.value).toBe('slow-1');
|
|
111
|
+
|
|
112
|
+
// Remove the slow source and add a fast one.
|
|
113
|
+
await multi.removeInputStream(slowId);
|
|
114
|
+
|
|
115
|
+
multi.addInputStream(streamFrom(['fast-1', 'fast-2']));
|
|
116
|
+
|
|
117
|
+
const r2 = await reader.read();
|
|
118
|
+
expect(r2.value).toBe('fast-1');
|
|
119
|
+
|
|
120
|
+
const r3 = await reader.read();
|
|
121
|
+
expect(r3.value).toBe('fast-2');
|
|
122
|
+
|
|
123
|
+
reader.releaseLock();
|
|
124
|
+
await multi.close();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should handle swapping inputs (remove then add)', async () => {
|
|
128
|
+
const multi = new MultiInputStream<string>();
|
|
129
|
+
const reader = multi.stream.getReader();
|
|
130
|
+
|
|
131
|
+
const id1 = multi.addInputStream(streamFrom(['from-A']));
|
|
132
|
+
|
|
133
|
+
const r1 = await reader.read();
|
|
134
|
+
expect(r1.value).toBe('from-A');
|
|
135
|
+
|
|
136
|
+
await multi.removeInputStream(id1);
|
|
137
|
+
|
|
138
|
+
const id2 = multi.addInputStream(streamFrom(['from-B']));
|
|
139
|
+
|
|
140
|
+
const r2 = await reader.read();
|
|
141
|
+
expect(r2.value).toBe('from-B');
|
|
142
|
+
|
|
143
|
+
await multi.removeInputStream(id2);
|
|
144
|
+
reader.releaseLock();
|
|
145
|
+
await multi.close();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// Reading before any input is added
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
it('should keep reader awaiting until an input is added', async () => {
|
|
153
|
+
const multi = new MultiInputStream<string>();
|
|
154
|
+
const reader = multi.stream.getReader();
|
|
155
|
+
|
|
156
|
+
let readCompleted = false;
|
|
157
|
+
const readPromise = reader.read().then((result) => {
|
|
158
|
+
readCompleted = true;
|
|
159
|
+
return result;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await delay(50);
|
|
163
|
+
expect(readCompleted).toBe(false);
|
|
164
|
+
|
|
165
|
+
// Now add an input to unblock the read.
|
|
166
|
+
multi.addInputStream(streamFrom(['hello']));
|
|
167
|
+
|
|
168
|
+
const result = await readPromise;
|
|
169
|
+
expect(readCompleted).toBe(true);
|
|
170
|
+
expect(result.value).toBe('hello');
|
|
171
|
+
|
|
172
|
+
reader.releaseLock();
|
|
173
|
+
await multi.close();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
// Empty input streams
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
it('should handle empty input streams without closing the output', async () => {
|
|
181
|
+
const multi = new MultiInputStream<string>();
|
|
182
|
+
const reader = multi.stream.getReader();
|
|
183
|
+
|
|
184
|
+
// Add an empty stream — it should end immediately without affecting the output.
|
|
185
|
+
multi.addInputStream(streamFrom([]));
|
|
186
|
+
|
|
187
|
+
await delay(20);
|
|
188
|
+
|
|
189
|
+
// The output should still be open. Adding a real input should work.
|
|
190
|
+
multi.addInputStream(streamFrom(['data']));
|
|
191
|
+
|
|
192
|
+
const result = await reader.read();
|
|
193
|
+
expect(result.value).toBe('data');
|
|
194
|
+
|
|
195
|
+
reader.releaseLock();
|
|
196
|
+
await multi.close();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Error handling
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
it('should remove errored input without killing the output', async () => {
|
|
204
|
+
const multi = new MultiInputStream<string>();
|
|
205
|
+
const reader = multi.stream.getReader();
|
|
206
|
+
|
|
207
|
+
// An input that errors after emitting one value.
|
|
208
|
+
const errorSource = new ReadableStream<string>({
|
|
209
|
+
async start(controller) {
|
|
210
|
+
controller.enqueue('before-error');
|
|
211
|
+
await delay(20);
|
|
212
|
+
controller.error(new Error('boom'));
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
multi.addInputStream(errorSource);
|
|
217
|
+
|
|
218
|
+
const r1 = await reader.read();
|
|
219
|
+
expect(r1.value).toBe('before-error');
|
|
220
|
+
|
|
221
|
+
// Wait for the error to propagate and the input to be removed.
|
|
222
|
+
await delay(50);
|
|
223
|
+
|
|
224
|
+
expect(multi.inputCount).toBe(0);
|
|
225
|
+
|
|
226
|
+
// The output is still alive — we can add another input.
|
|
227
|
+
multi.addInputStream(streamFrom(['after-error']));
|
|
228
|
+
|
|
229
|
+
const r2 = await reader.read();
|
|
230
|
+
expect(r2.value).toBe('after-error');
|
|
231
|
+
|
|
232
|
+
reader.releaseLock();
|
|
233
|
+
await multi.close();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('should keep other inputs alive when one errors', async () => {
|
|
237
|
+
const multi = new MultiInputStream<string>();
|
|
238
|
+
const reader = multi.stream.getReader();
|
|
239
|
+
|
|
240
|
+
const goodSource = new ReadableStream<string>({
|
|
241
|
+
async start(controller) {
|
|
242
|
+
await delay(60);
|
|
243
|
+
controller.enqueue('good');
|
|
244
|
+
controller.close();
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const badSource = new ReadableStream<string>({
|
|
249
|
+
async start(controller) {
|
|
250
|
+
controller.error(new Error('bad'));
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
multi.addInputStream(goodSource);
|
|
255
|
+
multi.addInputStream(badSource);
|
|
256
|
+
|
|
257
|
+
// Wait a bit for the bad source to error and be removed.
|
|
258
|
+
await delay(10);
|
|
259
|
+
|
|
260
|
+
// The good source should still be pumping.
|
|
261
|
+
const result = await reader.read();
|
|
262
|
+
expect(result.value).toBe('good');
|
|
263
|
+
|
|
264
|
+
reader.releaseLock();
|
|
265
|
+
await multi.close();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
// Close semantics
|
|
270
|
+
// ---------------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
it('should end the output stream with done:true when close is called', async () => {
|
|
273
|
+
const multi = new MultiInputStream<string>();
|
|
274
|
+
const reader = multi.stream.getReader();
|
|
275
|
+
|
|
276
|
+
multi.addInputStream(streamFrom(['data']));
|
|
277
|
+
|
|
278
|
+
const r1 = await reader.read();
|
|
279
|
+
expect(r1.value).toBe('data');
|
|
280
|
+
|
|
281
|
+
await multi.close();
|
|
282
|
+
|
|
283
|
+
const r2 = await reader.read();
|
|
284
|
+
expect(r2.done).toBe(true);
|
|
285
|
+
expect(r2.value).toBeUndefined();
|
|
286
|
+
|
|
287
|
+
reader.releaseLock();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should resolve pending reads as done when close is called', async () => {
|
|
291
|
+
const multi = new MultiInputStream<string>();
|
|
292
|
+
const reader = multi.stream.getReader();
|
|
293
|
+
|
|
294
|
+
// No inputs — read will be pending.
|
|
295
|
+
const readPromise = reader.read();
|
|
296
|
+
|
|
297
|
+
await delay(10);
|
|
298
|
+
await multi.close();
|
|
299
|
+
|
|
300
|
+
const result = await readPromise;
|
|
301
|
+
expect(result.done).toBe(true);
|
|
302
|
+
expect(result.value).toBeUndefined();
|
|
303
|
+
|
|
304
|
+
reader.releaseLock();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('should be idempotent for multiple close calls', async () => {
|
|
308
|
+
const multi = new MultiInputStream<string>();
|
|
309
|
+
|
|
310
|
+
await multi.close();
|
|
311
|
+
await multi.close();
|
|
312
|
+
|
|
313
|
+
expect(multi.isClosed).toBe(true);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('should throw when adding input after close', async () => {
|
|
317
|
+
const multi = new MultiInputStream<string>();
|
|
318
|
+
await multi.close();
|
|
319
|
+
|
|
320
|
+
expect(() => multi.addInputStream(streamFrom(['x']))).toThrow('MultiInputStream is closed');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// ---------------------------------------------------------------------------
|
|
324
|
+
// removeInputStream edge cases
|
|
325
|
+
// ---------------------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
it('should no-op when removing a non-existent input', async () => {
|
|
328
|
+
const multi = new MultiInputStream<string>();
|
|
329
|
+
|
|
330
|
+
// Should not throw.
|
|
331
|
+
await multi.removeInputStream('does-not-exist');
|
|
332
|
+
|
|
333
|
+
await multi.close();
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('should release the source reader lock so the source can be reused', async () => {
|
|
337
|
+
const multi = new MultiInputStream<string>();
|
|
338
|
+
const reader = multi.stream.getReader();
|
|
339
|
+
|
|
340
|
+
const source = new ReadableStream<string>({
|
|
341
|
+
async start(controller) {
|
|
342
|
+
controller.enqueue('chunk-0');
|
|
343
|
+
await delay(30);
|
|
344
|
+
controller.enqueue('chunk-1');
|
|
345
|
+
controller.close();
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const id = multi.addInputStream(source);
|
|
350
|
+
|
|
351
|
+
const r1 = await reader.read();
|
|
352
|
+
expect(r1.value).toBe('chunk-0');
|
|
353
|
+
|
|
354
|
+
await multi.removeInputStream(id);
|
|
355
|
+
|
|
356
|
+
// The source's reader lock should be released — we can get a new reader.
|
|
357
|
+
const sourceReader = source.getReader();
|
|
358
|
+
const sr = await sourceReader.read();
|
|
359
|
+
expect(sr.value).toBe('chunk-1');
|
|
360
|
+
sourceReader.releaseLock();
|
|
361
|
+
|
|
362
|
+
reader.releaseLock();
|
|
363
|
+
await multi.close();
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// ---------------------------------------------------------------------------
|
|
367
|
+
// Input count tracking
|
|
368
|
+
// ---------------------------------------------------------------------------
|
|
369
|
+
|
|
370
|
+
it('should track inputCount correctly through add / remove / natural end', async () => {
|
|
371
|
+
const multi = new MultiInputStream<string>();
|
|
372
|
+
|
|
373
|
+
expect(multi.inputCount).toBe(0);
|
|
374
|
+
|
|
375
|
+
const id1 = multi.addInputStream(streamFrom(['a']));
|
|
376
|
+
const id2 = multi.addInputStream(streamFrom(['b']));
|
|
377
|
+
|
|
378
|
+
expect(multi.inputCount).toBe(2);
|
|
379
|
+
|
|
380
|
+
await multi.removeInputStream(id1);
|
|
381
|
+
expect(multi.inputCount).toBeLessThanOrEqual(1);
|
|
382
|
+
|
|
383
|
+
// Let the remaining stream finish.
|
|
384
|
+
await delay(20);
|
|
385
|
+
expect(multi.inputCount).toBe(0);
|
|
386
|
+
|
|
387
|
+
await multi.removeInputStream(id2); // already gone, no-op
|
|
388
|
+
expect(multi.inputCount).toBe(0);
|
|
389
|
+
|
|
390
|
+
await multi.close();
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// ---------------------------------------------------------------------------
|
|
394
|
+
// Concurrent reads and writes
|
|
395
|
+
// ---------------------------------------------------------------------------
|
|
396
|
+
|
|
397
|
+
it('should handle concurrent reads and slow writes', async () => {
|
|
398
|
+
const multi = new MultiInputStream<string>();
|
|
399
|
+
const reader = multi.stream.getReader();
|
|
400
|
+
|
|
401
|
+
const chunks = ['a', 'b', 'c', 'd', 'e'];
|
|
402
|
+
let idx = 0;
|
|
403
|
+
|
|
404
|
+
const source = new ReadableStream<string>({
|
|
405
|
+
start(controller) {
|
|
406
|
+
const writeNext = () => {
|
|
407
|
+
if (idx < chunks.length) {
|
|
408
|
+
controller.enqueue(chunks[idx++]);
|
|
409
|
+
setTimeout(writeNext, 5);
|
|
410
|
+
} else {
|
|
411
|
+
controller.close();
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
writeNext();
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
multi.addInputStream(source);
|
|
419
|
+
|
|
420
|
+
const results: string[] = [];
|
|
421
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
422
|
+
const { value } = await reader.read();
|
|
423
|
+
results.push(value!);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
expect(results).toEqual(chunks);
|
|
427
|
+
|
|
428
|
+
reader.releaseLock();
|
|
429
|
+
await multi.close();
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// ---------------------------------------------------------------------------
|
|
433
|
+
// Backpressure
|
|
434
|
+
// ---------------------------------------------------------------------------
|
|
435
|
+
|
|
436
|
+
it('should handle backpressure with large data', async () => {
|
|
437
|
+
const multi = new MultiInputStream<string>();
|
|
438
|
+
|
|
439
|
+
const largeChunks = Array.from({ length: 1000 }, (_, i) => `chunk-${i}`);
|
|
440
|
+
multi.addInputStream(streamFrom(largeChunks));
|
|
441
|
+
|
|
442
|
+
const reader = multi.stream.getReader();
|
|
443
|
+
const results: string[] = [];
|
|
444
|
+
|
|
445
|
+
let result = await reader.read();
|
|
446
|
+
while (!result.done) {
|
|
447
|
+
results.push(result.value);
|
|
448
|
+
// Check if we've collected all expected values before reading again,
|
|
449
|
+
// to avoid hanging on the output which stays open after input ends.
|
|
450
|
+
if (results.length === largeChunks.length) break;
|
|
451
|
+
result = await reader.read();
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
expect(results).toEqual(largeChunks);
|
|
455
|
+
|
|
456
|
+
reader.releaseLock();
|
|
457
|
+
await multi.close();
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// ---------------------------------------------------------------------------
|
|
461
|
+
// Multiple tee / concurrent consumers
|
|
462
|
+
// ---------------------------------------------------------------------------
|
|
463
|
+
|
|
464
|
+
it('should support tee on the output stream', async () => {
|
|
465
|
+
const multi = new MultiInputStream<number>();
|
|
466
|
+
|
|
467
|
+
const [s1, s2] = multi.stream.tee();
|
|
468
|
+
const r1 = s1.getReader();
|
|
469
|
+
const r2 = s2.getReader();
|
|
470
|
+
|
|
471
|
+
multi.addInputStream(streamFrom([10, 20]));
|
|
472
|
+
|
|
473
|
+
const [a1, a2] = await Promise.all([r1.read(), r2.read()]);
|
|
474
|
+
expect(a1.value).toBe(10);
|
|
475
|
+
expect(a2.value).toBe(10);
|
|
476
|
+
|
|
477
|
+
const [b1, b2] = await Promise.all([r1.read(), r2.read()]);
|
|
478
|
+
expect(b1.value).toBe(20);
|
|
479
|
+
expect(b2.value).toBe(20);
|
|
480
|
+
|
|
481
|
+
r1.releaseLock();
|
|
482
|
+
r2.releaseLock();
|
|
483
|
+
await multi.close();
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// ---------------------------------------------------------------------------
|
|
487
|
+
// Return value of addInputStream
|
|
488
|
+
// ---------------------------------------------------------------------------
|
|
489
|
+
|
|
490
|
+
it('should return unique IDs from addInputStream', () => {
|
|
491
|
+
const multi = new MultiInputStream<string>();
|
|
492
|
+
|
|
493
|
+
const id1 = multi.addInputStream(streamFrom(['a']));
|
|
494
|
+
const id2 = multi.addInputStream(streamFrom(['b']));
|
|
495
|
+
const id3 = multi.addInputStream(streamFrom(['c']));
|
|
496
|
+
|
|
497
|
+
expect(id1).not.toBe(id2);
|
|
498
|
+
expect(id2).not.toBe(id3);
|
|
499
|
+
expect(id1).not.toBe(id3);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// ---------------------------------------------------------------------------
|
|
503
|
+
// close() while pumps are actively writing
|
|
504
|
+
// ---------------------------------------------------------------------------
|
|
505
|
+
|
|
506
|
+
it('should cleanly close while pumps are actively writing', async () => {
|
|
507
|
+
const multi = new MultiInputStream<string>();
|
|
508
|
+
const reader = multi.stream.getReader();
|
|
509
|
+
|
|
510
|
+
// A source that never stops on its own.
|
|
511
|
+
const infiniteSource = new ReadableStream<string>({
|
|
512
|
+
async start(controller) {
|
|
513
|
+
let i = 0;
|
|
514
|
+
while (true) {
|
|
515
|
+
try {
|
|
516
|
+
controller.enqueue(`tick-${i++}`);
|
|
517
|
+
} catch {
|
|
518
|
+
// controller.enqueue throws after stream is canceled
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
await delay(5);
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
multi.addInputStream(infiniteSource);
|
|
527
|
+
|
|
528
|
+
// Read a couple of values.
|
|
529
|
+
const r1 = await reader.read();
|
|
530
|
+
expect(r1.done).toBe(false);
|
|
531
|
+
|
|
532
|
+
// Close while the infinite source is still pumping.
|
|
533
|
+
await multi.close();
|
|
534
|
+
|
|
535
|
+
const r2 = await reader.read();
|
|
536
|
+
expect(r2.done).toBe(true);
|
|
537
|
+
|
|
538
|
+
reader.releaseLock();
|
|
539
|
+
});
|
|
540
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import type {
|
|
5
|
+
ReadableStream,
|
|
6
|
+
ReadableStreamDefaultReader,
|
|
7
|
+
WritableStreamDefaultWriter,
|
|
8
|
+
} from 'node:stream/web';
|
|
9
|
+
import { log } from '../log.js';
|
|
10
|
+
import { isStreamReaderReleaseError } from './deferred_stream.js';
|
|
11
|
+
import { IdentityTransform } from './identity_transform.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A fan-in multiplexer that merges multiple {@link ReadableStream} inputs into
|
|
15
|
+
* a single output {@link ReadableStream}. Inputs can be dynamically added and
|
|
16
|
+
* removed at any time while the stream is open.
|
|
17
|
+
*
|
|
18
|
+
* Unlike {@link DeferredReadableStream} which supports a single readable source,
|
|
19
|
+
* `MultiInputStream` allows N concurrent input streams to pump data into one output.
|
|
20
|
+
*
|
|
21
|
+
* Key behaviors:
|
|
22
|
+
* - An error in one input removes that input but does **not** kill the output.
|
|
23
|
+
* - When all inputs end or are removed, the output stays open (waiting for new inputs).
|
|
24
|
+
* - The output only closes when {@link close} is called explicitly.
|
|
25
|
+
* - {@link removeInputStream} releases the reader lock so the source can be reused.
|
|
26
|
+
*/
|
|
27
|
+
export class MultiInputStream<T> {
|
|
28
|
+
private transform: IdentityTransform<T>;
|
|
29
|
+
private writer: WritableStreamDefaultWriter<T>;
|
|
30
|
+
private inputs: Map<string, ReadableStreamDefaultReader<T>> = new Map();
|
|
31
|
+
private pumpPromises: Map<string, Promise<void>> = new Map();
|
|
32
|
+
private nextId = 0;
|
|
33
|
+
private _closed = false;
|
|
34
|
+
private logger = log();
|
|
35
|
+
|
|
36
|
+
constructor() {
|
|
37
|
+
this.transform = new IdentityTransform<T>();
|
|
38
|
+
this.writer = this.transform.writable.getWriter();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The single output stream that consumers read from. */
|
|
42
|
+
get stream(): ReadableStream<T> {
|
|
43
|
+
return this.transform.readable;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Number of currently active input streams. */
|
|
47
|
+
get inputCount(): number {
|
|
48
|
+
return this.inputs.size;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether {@link close} has been called. */
|
|
52
|
+
get isClosed(): boolean {
|
|
53
|
+
return this._closed;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Add an input {@link ReadableStream} that will be pumped into the output.
|
|
58
|
+
*
|
|
59
|
+
* @returns A unique identifier that can be passed to {@link removeInputStream}.
|
|
60
|
+
* @throws If the stream has already been closed.
|
|
61
|
+
*/
|
|
62
|
+
addInputStream(source: ReadableStream<T>): string {
|
|
63
|
+
if (this._closed) {
|
|
64
|
+
throw new Error('MultiInputStream is closed');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const id = `input-${this.nextId++}`;
|
|
68
|
+
const reader = source.getReader();
|
|
69
|
+
this.inputs.set(id, reader);
|
|
70
|
+
|
|
71
|
+
const pumpDone = this.pumpInput(id, reader);
|
|
72
|
+
this.pumpPromises.set(id, pumpDone);
|
|
73
|
+
|
|
74
|
+
return id;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Detach an input stream by its ID and release the reader lock so the
|
|
79
|
+
* source stream can be reused elsewhere.
|
|
80
|
+
*
|
|
81
|
+
* No-op if the ID does not exist (e.g. the input already ended or was removed).
|
|
82
|
+
*/
|
|
83
|
+
async removeInputStream(id: string): Promise<void> {
|
|
84
|
+
const reader = this.inputs.get(id);
|
|
85
|
+
if (!reader) return;
|
|
86
|
+
|
|
87
|
+
// Delete first so the pump's finally-block is a harmless no-op.
|
|
88
|
+
this.inputs.delete(id);
|
|
89
|
+
|
|
90
|
+
// Releasing the lock causes any pending reader.read() inside pump to throw
|
|
91
|
+
// a TypeError, which is caught by isStreamReaderReleaseError.
|
|
92
|
+
reader.releaseLock();
|
|
93
|
+
|
|
94
|
+
// Wait for the pump to finish so the caller knows cleanup is complete.
|
|
95
|
+
const pump = this.pumpPromises.get(id);
|
|
96
|
+
if (pump) {
|
|
97
|
+
await pump;
|
|
98
|
+
this.pumpPromises.delete(id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Close the output stream and detach all inputs.
|
|
104
|
+
*
|
|
105
|
+
* Idempotent — calling more than once is a no-op.
|
|
106
|
+
*/
|
|
107
|
+
async close(): Promise<void> {
|
|
108
|
+
if (this._closed) return;
|
|
109
|
+
this._closed = true;
|
|
110
|
+
|
|
111
|
+
// Release every input reader to unblock pending reads inside pumps.
|
|
112
|
+
for (const reader of this.inputs.values()) {
|
|
113
|
+
reader.releaseLock();
|
|
114
|
+
}
|
|
115
|
+
this.inputs.clear();
|
|
116
|
+
|
|
117
|
+
// Wait for every pump loop to finish before touching the writer.
|
|
118
|
+
await Promise.allSettled([...this.pumpPromises.values()]);
|
|
119
|
+
this.pumpPromises.clear();
|
|
120
|
+
|
|
121
|
+
// Close the output writer + writable side of the transform.
|
|
122
|
+
try {
|
|
123
|
+
this.writer.releaseLock();
|
|
124
|
+
} catch {
|
|
125
|
+
// ignore if already released
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
await this.transform.writable.close();
|
|
130
|
+
} catch {
|
|
131
|
+
// ignore if already closed
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private shouldStopPumping(id: string): boolean {
|
|
136
|
+
return this._closed || !this.inputs.has(id);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private async pumpInput(id: string, reader: ReadableStreamDefaultReader<T>): Promise<void> {
|
|
140
|
+
try {
|
|
141
|
+
while (true) {
|
|
142
|
+
// If the stream was closed or the input was removed while we were
|
|
143
|
+
// awaiting the previous write, bail out immediately.
|
|
144
|
+
if (this.shouldStopPumping(id)) break;
|
|
145
|
+
|
|
146
|
+
const { done, value } = await reader.read();
|
|
147
|
+
if (done) break;
|
|
148
|
+
|
|
149
|
+
// Double-check after the (potentially long) read.
|
|
150
|
+
if (this.shouldStopPumping(id)) break;
|
|
151
|
+
|
|
152
|
+
await this.writer.write(value);
|
|
153
|
+
}
|
|
154
|
+
} catch (e) {
|
|
155
|
+
// TypeErrors from releaseLock() during removeInputStream / close are expected.
|
|
156
|
+
if (isStreamReaderReleaseError(e)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.logger.error({ error: e }, 'Error pumping input stream from MultiInputStream');
|
|
161
|
+
} finally {
|
|
162
|
+
try {
|
|
163
|
+
reader.releaseLock();
|
|
164
|
+
} catch {
|
|
165
|
+
// ignore if already released
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
this.inputs.delete(id);
|
|
169
|
+
this.pumpPromises.delete(id);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|