@kuralle-syrinx/server-websocket 4.2.0 → 4.4.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.
@@ -1,51 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
-
3
- import { describe, expect, it, vi } from "vitest";
4
- import type { IncomingMessage } from "node:http";
5
- import { WebSocket } from "ws";
6
- import { VoiceAgentSession } from "@kuralle-syrinx/core";
7
- import { runWebSocketConnection, type TransportAdapter } from "./transport-host.js";
8
-
9
- const hostConfig = {
10
- heartbeatIntervalMs: 30_000,
11
- startupTimeoutMs: 500,
12
- maxSessionDurationMs: 60_000,
13
- maxBufferedAmountBytes: 1_000_000,
14
- maxInboundMessageBytes: 1_000_000,
15
- };
16
-
17
- function closedSocket(): WebSocket {
18
- return {
19
- readyState: WebSocket.CLOSED,
20
- on: () => undefined,
21
- close: () => undefined,
22
- } as unknown as WebSocket;
23
- }
24
-
25
- describe("runWebSocketConnection startup", () => {
26
- it("does not acquire a session when the socket is already closed before startup", async () => {
27
- const acquireSession = vi.fn(async () => ({
28
- session: new VoiceAgentSession({ plugins: {} }),
29
- resumed: false,
30
- }));
31
- const adapter: TransportAdapter<null> = {
32
- createState: () => null,
33
- acquireSession,
34
- wireSession: () => () => undefined,
35
- processMessage: () => undefined,
36
- onDisconnect: () => undefined,
37
- onStartupTimeout: () => undefined,
38
- sendError: () => undefined,
39
- sendStartupError: () => undefined,
40
- };
41
-
42
- await runWebSocketConnection(
43
- closedSocket(),
44
- { url: "/ws" } as IncomingMessage,
45
- hostConfig,
46
- adapter,
47
- );
48
-
49
- expect(acquireSession).not.toHaveBeenCalled();
50
- });
51
- });
@@ -1,327 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
-
3
- import { describe, expect, it, vi } from "vitest";
4
- import { Route, VoiceAgentSession } from "@kuralle-syrinx/core";
5
- import {
6
- buildBrowserMetricsMessage,
7
- TurnMetricsTracker,
8
- } from "./turn-metrics.js";
9
- import { waitForCondition } from "./test-helpers.js";
10
-
11
- describe("turn metrics", () => {
12
- it("computes stage latencies from synthetic timestamps", () => {
13
- const message = buildBrowserMetricsMessage("turn-a", {
14
- speechEndMs: 1000,
15
- sttFinalMs: 1200,
16
- eosMs: 0,
17
- vadStopHangoverMs: 0,
18
- textReadyMs: 1500,
19
- firstAudioByteMs: 1700,
20
- firstAudioPlayedMs: 1900,
21
- lastAudioPlayedMs: 2500,
22
- });
23
-
24
- expect(message).toEqual({
25
- type: "metrics",
26
- turnId: "turn-a",
27
- correlationId: "turn-a",
28
- speechEndMs: 1000,
29
- textReadyMs: 1500,
30
- firstAudioByteMs: 1700,
31
- firstAudioPlayedMs: 1900,
32
- lastAudioPlayedMs: 2500,
33
- sttMs: 200,
34
- llmTTFTMs: 300,
35
- ttsTTFBMs: 200,
36
- e2eMs: 900,
37
- eouBudgetMs: {
38
- sttFinalDelayMs: 200,
39
- totalMs: 200,
40
- },
41
- });
42
- });
43
-
44
- it("buildBrowserMetricsMessage eou budget sums hangover, stt-final, and endpoint delays", () => {
45
- const message = buildBrowserMetricsMessage("turn-eou-unit", {
46
- speechEndMs: 1000,
47
- sttFinalMs: 1250,
48
- eosMs: 1300,
49
- vadStopHangoverMs: 80,
50
- textReadyMs: 0,
51
- firstAudioByteMs: 0,
52
- firstAudioPlayedMs: 0,
53
- lastAudioPlayedMs: 0,
54
- });
55
-
56
- expect(message.eouBudgetMs).toEqual({
57
- vadStopHangoverMs: 80,
58
- sttFinalDelayMs: 250,
59
- endpointDelayMs: 50,
60
- totalMs: 380,
61
- });
62
- expect(message.sttMs).toBe(250);
63
- });
64
-
65
- it("keeps correlation id stable for the turn context", async () => {
66
- const session = new VoiceAgentSession({ plugins: {} });
67
- const emitted: unknown[] = [];
68
- const tracker = new TurnMetricsTracker(session.bus, (message) => emitted.push(message));
69
- const disposers: Array<() => void> = [];
70
- tracker.wire(disposers);
71
- void session.start();
72
-
73
- session.bus.push(Route.Main, {
74
- kind: "vad.speech_ended",
75
- contextId: "turn-correlation",
76
- timestampMs: 500,
77
- });
78
- session.bus.push(Route.Main, {
79
- kind: "stt.result",
80
- contextId: "turn-correlation",
81
- timestampMs: 700,
82
- text: "hello",
83
- confidence: 0.99,
84
- });
85
- session.bus.push(Route.Main, {
86
- kind: "llm.delta",
87
- contextId: "turn-correlation",
88
- timestampMs: 900,
89
- text: "hi",
90
- });
91
- session.bus.push(Route.Main, {
92
- kind: "tts.audio",
93
- contextId: "turn-correlation",
94
- timestampMs: 1100,
95
- audio: new Uint8Array(640),
96
- sampleRateHz: 16000,
97
- });
98
- session.bus.push(Route.Main, {
99
- kind: "tts.playout_started",
100
- contextId: "turn-correlation",
101
- timestampMs: 1100,
102
- });
103
- session.bus.push(Route.Main, {
104
- kind: "tts.playout_progress",
105
- contextId: "turn-correlation",
106
- timestampMs: 1300,
107
- playedOutMs: 200,
108
- complete: false,
109
- });
110
- session.bus.push(Route.Main, {
111
- kind: "tts.playout_progress",
112
- contextId: "turn-correlation",
113
- timestampMs: 1800,
114
- playedOutMs: 120,
115
- complete: true,
116
- });
117
-
118
- await waitForCondition(() => emitted.length === 1);
119
- expect(emitted[0]).toMatchObject({
120
- type: "metrics",
121
- turnId: "turn-correlation",
122
- correlationId: "turn-correlation",
123
- sttMs: 200,
124
- llmTTFTMs: 200,
125
- ttsTTFBMs: 200,
126
- e2eMs: 600,
127
- });
128
-
129
- for (const dispose of disposers) dispose();
130
- await session.close();
131
- });
132
-
133
- it("records firstAudioPlayedMs from playout_started, not throttled progress", async () => {
134
- const session = new VoiceAgentSession({ plugins: {} });
135
- const emitted: unknown[] = [];
136
- const tracker = new TurnMetricsTracker(session.bus, (message) => emitted.push(message));
137
- const disposers: Array<() => void> = [];
138
- tracker.wire(disposers);
139
- void session.start();
140
-
141
- session.bus.push(Route.Main, {
142
- kind: "vad.speech_ended",
143
- contextId: "turn-throttle",
144
- timestampMs: 1000,
145
- });
146
- session.bus.push(Route.Main, {
147
- kind: "tts.playout_started",
148
- contextId: "turn-throttle",
149
- timestampMs: 1100,
150
- });
151
- session.bus.push(Route.Main, {
152
- kind: "tts.playout_progress",
153
- contextId: "turn-throttle",
154
- timestampMs: 1300,
155
- playedOutMs: 200,
156
- complete: true,
157
- });
158
-
159
- await waitForCondition(() => emitted.length === 1);
160
- expect(emitted[0]).toMatchObject({
161
- firstAudioPlayedMs: 1100,
162
- e2eMs: 100,
163
- });
164
-
165
- for (const dispose of disposers) dispose();
166
- await session.close();
167
- });
168
-
169
- it("emits metrics once when playout completes for a wired browser session", async () => {
170
- const session = new VoiceAgentSession({ plugins: {} });
171
- const emitted: unknown[] = [];
172
- const tracker = new TurnMetricsTracker(session.bus, (message) => emitted.push(message));
173
- const disposers: Array<() => void> = [];
174
- tracker.wire(disposers);
175
- void session.start();
176
-
177
- session.bus.push(Route.Main, {
178
- kind: "vad.speech_ended",
179
- contextId: "turn-live",
180
- timestampMs: 10_000,
181
- });
182
- session.bus.push(Route.Main, {
183
- kind: "stt.result",
184
- contextId: "turn-live",
185
- timestampMs: 10_200,
186
- text: "hello",
187
- confidence: 0.99,
188
- });
189
- session.bus.push(Route.Main, {
190
- kind: "llm.delta",
191
- contextId: "turn-live",
192
- timestampMs: 10_450,
193
- text: "hi",
194
- });
195
- session.bus.push(Route.Main, {
196
- kind: "tts.audio",
197
- contextId: "turn-live",
198
- timestampMs: 10_600,
199
- audio: new Uint8Array(640),
200
- sampleRateHz: 16000,
201
- });
202
- session.bus.push(Route.Main, {
203
- kind: "tts.playout_started",
204
- contextId: "turn-live",
205
- timestampMs: 10_600,
206
- });
207
- session.bus.push(Route.Main, {
208
- kind: "tts.playout_progress",
209
- contextId: "turn-live",
210
- timestampMs: 10_800,
211
- playedOutMs: 200,
212
- complete: false,
213
- });
214
- session.bus.push(Route.Main, {
215
- kind: "tts.playout_progress",
216
- contextId: "turn-live",
217
- timestampMs: 11_000,
218
- playedOutMs: 120,
219
- complete: true,
220
- });
221
-
222
- await waitForCondition(() => emitted.length === 1);
223
- expect(emitted[0]).toMatchObject({
224
- type: "metrics",
225
- turnId: "turn-live",
226
- correlationId: "turn-live",
227
- sttMs: 200,
228
- llmTTFTMs: 250,
229
- ttsTTFBMs: 150,
230
- e2eMs: 600,
231
- });
232
-
233
- for (const dispose of disposers) dispose();
234
- await session.close();
235
- });
236
-
237
- it("eou_budget_breakdown: vad hangover, stt-final delay, endpoint delay, and total", async () => {
238
- const session = new VoiceAgentSession({ plugins: {} });
239
- const emitted: unknown[] = [];
240
- const tracker = new TurnMetricsTracker(session.bus, (message) => emitted.push(message));
241
- const disposers: Array<() => void> = [];
242
- tracker.wire(disposers);
243
- void session.start();
244
-
245
- session.bus.push(Route.Main, {
246
- kind: "vad.speech_ended",
247
- contextId: "turn-eou",
248
- timestampMs: 1000,
249
- });
250
- session.bus.push(Route.Main, {
251
- kind: "metric.conversation",
252
- contextId: "turn-eou",
253
- timestampMs: 1005,
254
- name: "vad.stop_hangover_ms",
255
- value: "80",
256
- });
257
- session.bus.push(Route.Main, {
258
- kind: "stt.result",
259
- contextId: "turn-eou",
260
- timestampMs: 1250,
261
- text: "hello",
262
- confidence: 0.99,
263
- });
264
- session.bus.push(Route.Main, {
265
- kind: "eos.turn_complete",
266
- contextId: "turn-eou",
267
- timestampMs: 1300,
268
- text: "hello",
269
- transcripts: [],
270
- });
271
- session.bus.push(Route.Main, {
272
- kind: "tts.playout_progress",
273
- contextId: "turn-eou",
274
- timestampMs: 1500,
275
- playedOutMs: 100,
276
- complete: true,
277
- });
278
-
279
- await waitForCondition(() => emitted.length === 1);
280
- expect(emitted[0]).toMatchObject({
281
- type: "metrics",
282
- turnId: "turn-eou",
283
- correlationId: "turn-eou",
284
- sttMs: 250,
285
- eouBudgetMs: {
286
- vadStopHangoverMs: 80,
287
- sttFinalDelayMs: 250,
288
- endpointDelayMs: 50,
289
- totalMs: 380,
290
- },
291
- });
292
-
293
- for (const dispose of disposers) dispose();
294
- await session.close();
295
- });
296
-
297
- it("drops partial turn state on interrupt without emitting metrics", async () => {
298
- const session = new VoiceAgentSession({ plugins: {} });
299
- const emit = vi.fn();
300
- const tracker = new TurnMetricsTracker(session.bus, emit);
301
- tracker.wire([]);
302
- void session.start();
303
-
304
- session.bus.push(Route.Main, {
305
- kind: "vad.speech_ended",
306
- contextId: "turn-interrupted",
307
- timestampMs: 1000,
308
- });
309
- session.bus.push(Route.Critical, {
310
- kind: "interrupt.tts",
311
- contextId: "turn-interrupted",
312
- timestampMs: 1100,
313
- });
314
- session.bus.push(Route.Main, {
315
- kind: "tts.playout_progress",
316
- contextId: "turn-interrupted",
317
- timestampMs: 1200,
318
- playedOutMs: 20,
319
- complete: true,
320
- });
321
-
322
- await waitForCondition(() => emit.mock.calls.length === 0, 200);
323
- expect(emit).not.toHaveBeenCalled();
324
-
325
- await session.close();
326
- });
327
- });
@@ -1,36 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
-
3
- import { describe, expect, it } from "vitest";
4
- import { validateTwilioSignature } from "./twilio-auth.js";
5
-
6
- // Vector computed with the canonical HMAC-SHA1 algorithm (verified against
7
- // node:crypto createHmac("sha1")) for these exact inputs — this pins that our
8
- // Web Crypto implementation matches Twilio's documented signing scheme.
9
- describe("validateTwilioSignature", () => {
10
- const authToken = "12345";
11
- const url = "https://mycompany.com/myapp.php?foo=1&bar=2";
12
- const params = {
13
- CallSid: "CA1234567890ABCDE",
14
- Caller: "+14158675310",
15
- Digits: "1234",
16
- From: "+14158675310",
17
- To: "+18005551212",
18
- };
19
- const validSignature = "GvWf1cFY/Q7PnoempGyD5oXAezc=";
20
-
21
- it("accepts a correct signature", async () => {
22
- expect(await validateTwilioSignature({ authToken, signature: validSignature, url, params })).toBe(true);
23
- });
24
-
25
- it("rejects a tampered signature", async () => {
26
- expect(await validateTwilioSignature({ authToken, signature: "AAAA" + validSignature.slice(4), url, params })).toBe(false);
27
- });
28
-
29
- it("rejects a wrong auth token", async () => {
30
- expect(await validateTwilioSignature({ authToken: "wrong", signature: validSignature, url, params })).toBe(false);
31
- });
32
-
33
- it("rejects a missing signature", async () => {
34
- expect(await validateTwilioSignature({ authToken, signature: null, url, params })).toBe(false);
35
- });
36
- });