@livekit/agents-plugin-inworld 1.9.4 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/tts.cjs +58 -14
- package/dist/tts.cjs.map +1 -1
- package/dist/tts.d.cts +10 -1
- package/dist/tts.d.ts +10 -1
- package/dist/tts.d.ts.map +1 -1
- package/dist/tts.js +58 -14
- package/dist/tts.js.map +1 -1
- package/dist/tts_auto_mode.test.cjs +85 -0
- package/dist/tts_auto_mode.test.cjs.map +1 -0
- package/dist/tts_auto_mode.test.js +84 -0
- package/dist/tts_auto_mode.test.js.map +1 -0
- package/dist/tts_context.test.cjs +163 -0
- package/dist/tts_context.test.cjs.map +1 -0
- package/dist/tts_context.test.js +140 -0
- package/dist/tts_context.test.js.map +1 -0
- package/package.json +7 -7
- package/src/tts.ts +86 -16
- package/src/tts_auto_mode.test.ts +105 -0
- package/src/tts_context.test.ts +173 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { initializeLogger } from '@livekit/agents';
|
|
5
|
+
import { EventEmitter, once } from 'node:events';
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
7
|
+
import * as ws from 'ws';
|
|
8
|
+
import { TTS } from './tts.js';
|
|
9
|
+
|
|
10
|
+
const endpoint = vi.hoisted(() => ({ url: '' }));
|
|
11
|
+
vi.mock('ws', async (importOriginal) => {
|
|
12
|
+
const original = await importOriginal<typeof ws>();
|
|
13
|
+
return {
|
|
14
|
+
...original,
|
|
15
|
+
WebSocket: class extends original.WebSocket {
|
|
16
|
+
constructor(_url: string, options: ws.ClientOptions) {
|
|
17
|
+
super(endpoint.url, options);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
type Message = { contextId: string } & Record<string, unknown>;
|
|
24
|
+
|
|
25
|
+
let server: ws.WebSocketServer;
|
|
26
|
+
let received: Message[];
|
|
27
|
+
const events = new EventEmitter();
|
|
28
|
+
|
|
29
|
+
function kinds() {
|
|
30
|
+
return received.map((m) => Object.keys(m).find((k) => k !== 'contextId'));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function waitFor(kind: string) {
|
|
34
|
+
while (!kinds().includes(kind)) await once(events, 'message');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function startServer({ answerClose }: { answerClose: boolean }) {
|
|
38
|
+
received = [];
|
|
39
|
+
server = new ws.WebSocketServer({ host: '127.0.0.1', port: 0 });
|
|
40
|
+
server.on('connection', (socket) => {
|
|
41
|
+
socket.on('message', (data) => {
|
|
42
|
+
const msg = JSON.parse(data.toString()) as Message;
|
|
43
|
+
received.push(msg);
|
|
44
|
+
events.emit('message');
|
|
45
|
+
const reply = (result: object) =>
|
|
46
|
+
socket.send(JSON.stringify({ result: { contextId: msg.contextId, ...result } }));
|
|
47
|
+
if ('create' in msg) reply({ contextCreated: {} });
|
|
48
|
+
if ('cancel_context' in msg || ('close_context' in msg && answerClose)) {
|
|
49
|
+
reply({ contextClosed: {} });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
await once(server, 'listening');
|
|
54
|
+
const address = server.address();
|
|
55
|
+
if (address === null || typeof address === 'string') throw new Error('expected TCP address');
|
|
56
|
+
endpoint.url = `ws://127.0.0.1:${address.port}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function startStream() {
|
|
60
|
+
const provider = new TTS({ apiKey: 'test', wsURL: endpoint.url });
|
|
61
|
+
return provider.stream({ connOptions: { maxRetry: 0, retryIntervalMs: 1, timeoutMs: 1000 } });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
initializeLogger({ pretty: false, level: 'silent' });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(async () => {
|
|
69
|
+
vi.restoreAllMocks();
|
|
70
|
+
for (const client of server.clients) client.terminate();
|
|
71
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
type SendCallback = (err?: Error) => void;
|
|
75
|
+
|
|
76
|
+
// Holds the client's send callback for the first payload of `kind`, so a test can
|
|
77
|
+
// settle that send after closing the stream.
|
|
78
|
+
function holdSend(kind: string, forward: boolean) {
|
|
79
|
+
const original = ws.WebSocket.prototype.send;
|
|
80
|
+
let release!: (cb: SendCallback) => void;
|
|
81
|
+
const held = new Promise<SendCallback>((resolve) => (release = resolve));
|
|
82
|
+
let holding = true;
|
|
83
|
+
vi.spyOn(ws.WebSocket.prototype, 'send').mockImplementation(function (
|
|
84
|
+
this: ws.WebSocket,
|
|
85
|
+
data: unknown,
|
|
86
|
+
...rest: unknown[]
|
|
87
|
+
) {
|
|
88
|
+
const cb = rest.find((r): r is SendCallback => typeof r === 'function');
|
|
89
|
+
if (holding && cb && String(data).includes(`"${kind}"`)) {
|
|
90
|
+
holding = false;
|
|
91
|
+
if (forward) original.call(this, data as string);
|
|
92
|
+
release(cb);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
return (original as (...args: unknown[]) => void).call(this, data, ...rest);
|
|
96
|
+
});
|
|
97
|
+
return held;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
describe('inworld TTS context lifecycle', () => {
|
|
101
|
+
it('closes the context when the input ends', async () => {
|
|
102
|
+
await startServer({ answerClose: true });
|
|
103
|
+
const stream = startStream();
|
|
104
|
+
stream.pushText('Hello there.');
|
|
105
|
+
stream.endInput();
|
|
106
|
+
for await (const _ of stream) {
|
|
107
|
+
/* drain */
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
expect(kinds()).toEqual(['create', 'send_text', 'flush_context', 'close_context']);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('cancels the context when the stream is closed mid-reply', async () => {
|
|
114
|
+
await startServer({ answerClose: true });
|
|
115
|
+
const stream = startStream();
|
|
116
|
+
stream.pushText('Hello there. How are you doing today? ');
|
|
117
|
+
stream.flush();
|
|
118
|
+
await waitFor('send_text');
|
|
119
|
+
stream.close();
|
|
120
|
+
await waitFor('cancel_context');
|
|
121
|
+
|
|
122
|
+
expect(kinds()).not.toContain('close_context');
|
|
123
|
+
expect(new Set(received.map((m) => m.contextId)).size).toBe(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('cancels a closing context when the stream is closed', async () => {
|
|
127
|
+
await startServer({ answerClose: false });
|
|
128
|
+
const stream = startStream();
|
|
129
|
+
stream.pushText('Hello there. How are you doing today?');
|
|
130
|
+
stream.endInput();
|
|
131
|
+
await waitFor('close_context');
|
|
132
|
+
stream.close();
|
|
133
|
+
await waitFor('cancel_context');
|
|
134
|
+
|
|
135
|
+
expect(kinds().slice(-2)).toEqual(['close_context', 'cancel_context']);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('cancels without closing when the stream is closed during the flush', async () => {
|
|
139
|
+
await startServer({ answerClose: false });
|
|
140
|
+
const flushSent = holdSend('flush_context', true);
|
|
141
|
+
const stream = startStream();
|
|
142
|
+
stream.pushText('Hello there. How are you doing today?');
|
|
143
|
+
stream.endInput();
|
|
144
|
+
const settleFlush = await flushSent;
|
|
145
|
+
stream.close();
|
|
146
|
+
settleFlush();
|
|
147
|
+
await waitFor('cancel_context');
|
|
148
|
+
|
|
149
|
+
expect(kinds()).toContain('flush_context');
|
|
150
|
+
expect(kinds()).not.toContain('close_context');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('ignores a text send that fails after the stream is closed', async () => {
|
|
154
|
+
await startServer({ answerClose: true });
|
|
155
|
+
const unhandled = vi.fn();
|
|
156
|
+
process.on('unhandledRejection', unhandled);
|
|
157
|
+
try {
|
|
158
|
+
const textSent = holdSend('send_text', false);
|
|
159
|
+
const stream = startStream();
|
|
160
|
+
stream.pushText('Hello there. How are you doing today? ');
|
|
161
|
+
stream.flush();
|
|
162
|
+
const failText = await textSent;
|
|
163
|
+
stream.close();
|
|
164
|
+
await waitFor('cancel_context');
|
|
165
|
+
failText(new Error('send failed'));
|
|
166
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
167
|
+
|
|
168
|
+
expect(unhandled).not.toHaveBeenCalled();
|
|
169
|
+
} finally {
|
|
170
|
+
process.off('unhandledRejection', unhandled);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
});
|