@kodelyth/voice-call 2026.5.42 → 2026.6.2

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.
Files changed (111) hide show
  1. package/package.json +18 -6
  2. package/api.ts +0 -16
  3. package/cli-metadata.ts +0 -10
  4. package/config-api.ts +0 -12
  5. package/index.test.ts +0 -1075
  6. package/index.ts +0 -863
  7. package/runtime-api.ts +0 -20
  8. package/runtime-entry.ts +0 -1
  9. package/setup-api.ts +0 -47
  10. package/src/allowlist.test.ts +0 -18
  11. package/src/allowlist.ts +0 -19
  12. package/src/cli.test.ts +0 -12
  13. package/src/cli.ts +0 -866
  14. package/src/config-compat.test.ts +0 -130
  15. package/src/config-compat.ts +0 -227
  16. package/src/config.test.ts +0 -542
  17. package/src/config.ts +0 -883
  18. package/src/core-bridge.ts +0 -14
  19. package/src/deep-merge.test.ts +0 -40
  20. package/src/deep-merge.ts +0 -23
  21. package/src/gateway-continue-operation.ts +0 -200
  22. package/src/http-headers.test.ts +0 -16
  23. package/src/http-headers.ts +0 -15
  24. package/src/manager/context.ts +0 -50
  25. package/src/manager/events.test.ts +0 -578
  26. package/src/manager/events.ts +0 -332
  27. package/src/manager/lifecycle.ts +0 -53
  28. package/src/manager/lookup.test.ts +0 -52
  29. package/src/manager/lookup.ts +0 -35
  30. package/src/manager/outbound.test.ts +0 -629
  31. package/src/manager/outbound.ts +0 -508
  32. package/src/manager/state.ts +0 -48
  33. package/src/manager/store.ts +0 -107
  34. package/src/manager/timers.test.ts +0 -127
  35. package/src/manager/timers.ts +0 -113
  36. package/src/manager/twiml.test.ts +0 -13
  37. package/src/manager/twiml.ts +0 -17
  38. package/src/manager.closed-loop.test.ts +0 -259
  39. package/src/manager.inbound-allowlist.test.ts +0 -183
  40. package/src/manager.notify.test.ts +0 -390
  41. package/src/manager.restore.test.ts +0 -310
  42. package/src/manager.test-harness.ts +0 -127
  43. package/src/manager.ts +0 -441
  44. package/src/media-stream.test.ts +0 -953
  45. package/src/media-stream.ts +0 -876
  46. package/src/providers/base.ts +0 -99
  47. package/src/providers/mock.test.ts +0 -86
  48. package/src/providers/mock.ts +0 -185
  49. package/src/providers/plivo.test.ts +0 -93
  50. package/src/providers/plivo.ts +0 -601
  51. package/src/providers/shared/call-status.test.ts +0 -24
  52. package/src/providers/shared/call-status.ts +0 -24
  53. package/src/providers/shared/guarded-json-api.test.ts +0 -127
  54. package/src/providers/shared/guarded-json-api.ts +0 -49
  55. package/src/providers/telnyx.test.ts +0 -489
  56. package/src/providers/telnyx.ts +0 -419
  57. package/src/providers/twilio/api.test.ts +0 -184
  58. package/src/providers/twilio/api.ts +0 -100
  59. package/src/providers/twilio/twiml-policy.test.ts +0 -84
  60. package/src/providers/twilio/twiml-policy.ts +0 -87
  61. package/src/providers/twilio/webhook.ts +0 -34
  62. package/src/providers/twilio.test.ts +0 -607
  63. package/src/providers/twilio.ts +0 -861
  64. package/src/providers/twilio.types.ts +0 -17
  65. package/src/realtime-agent-context.test.ts +0 -101
  66. package/src/realtime-agent-context.ts +0 -149
  67. package/src/realtime-defaults.ts +0 -3
  68. package/src/realtime-fast-context.test.ts +0 -74
  69. package/src/realtime-fast-context.ts +0 -27
  70. package/src/realtime-transcription.runtime.ts +0 -4
  71. package/src/realtime-voice.runtime.ts +0 -5
  72. package/src/response-generator.test.ts +0 -385
  73. package/src/response-generator.ts +0 -348
  74. package/src/response-model.test.ts +0 -71
  75. package/src/response-model.ts +0 -23
  76. package/src/runtime.test.ts +0 -625
  77. package/src/runtime.ts +0 -528
  78. package/src/telephony-audio.test.ts +0 -61
  79. package/src/telephony-audio.ts +0 -12
  80. package/src/telephony-tts.test.ts +0 -196
  81. package/src/telephony-tts.ts +0 -235
  82. package/src/test-fixtures.ts +0 -82
  83. package/src/tts-provider-voice.test.ts +0 -34
  84. package/src/tts-provider-voice.ts +0 -21
  85. package/src/tunnel.test.ts +0 -173
  86. package/src/tunnel.ts +0 -314
  87. package/src/types.ts +0 -311
  88. package/src/utils.test.ts +0 -17
  89. package/src/utils.ts +0 -14
  90. package/src/voice-mapping.test.ts +0 -32
  91. package/src/voice-mapping.ts +0 -65
  92. package/src/webhook/realtime-audio-pacer.test.ts +0 -146
  93. package/src/webhook/realtime-audio-pacer.ts +0 -204
  94. package/src/webhook/realtime-handler.test.ts +0 -1450
  95. package/src/webhook/realtime-handler.ts +0 -1382
  96. package/src/webhook/stale-call-reaper.test.ts +0 -89
  97. package/src/webhook/stale-call-reaper.ts +0 -38
  98. package/src/webhook/stream-frame-adapter.test.ts +0 -187
  99. package/src/webhook/stream-frame-adapter.ts +0 -219
  100. package/src/webhook/tailscale.test.ts +0 -216
  101. package/src/webhook/tailscale.ts +0 -129
  102. package/src/webhook-exposure.test.ts +0 -33
  103. package/src/webhook-exposure.ts +0 -84
  104. package/src/webhook-security.test.ts +0 -813
  105. package/src/webhook-security.ts +0 -982
  106. package/src/webhook.hangup-once.lifecycle.test.ts +0 -179
  107. package/src/webhook.test.ts +0 -1615
  108. package/src/webhook.ts +0 -933
  109. package/src/webhook.types.ts +0 -5
  110. package/src/websocket-test-support.ts +0 -72
  111. package/tsconfig.json +0 -16
@@ -1,629 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- const {
4
- addTranscriptEntryMock,
5
- clearMaxDurationTimerMock,
6
- generateDtmfRedirectTwimlMock,
7
- generateNotifyTwimlMock,
8
- getCallByProviderCallIdMock,
9
- mapVoiceToPollyMock,
10
- persistCallRecordMock,
11
- rejectTranscriptWaiterMock,
12
- transitionStateMock,
13
- } = vi.hoisted(() => ({
14
- addTranscriptEntryMock: vi.fn(),
15
- clearMaxDurationTimerMock: vi.fn(),
16
- generateDtmfRedirectTwimlMock: vi.fn(),
17
- generateNotifyTwimlMock: vi.fn(),
18
- getCallByProviderCallIdMock: vi.fn(),
19
- mapVoiceToPollyMock: vi.fn(),
20
- persistCallRecordMock: vi.fn(),
21
- rejectTranscriptWaiterMock: vi.fn(),
22
- transitionStateMock: vi.fn(),
23
- }));
24
-
25
- vi.mock("./state.js", () => ({
26
- addTranscriptEntry: addTranscriptEntryMock,
27
- transitionState: transitionStateMock,
28
- }));
29
-
30
- vi.mock("./store.js", () => ({
31
- persistCallRecord: persistCallRecordMock,
32
- }));
33
-
34
- vi.mock("./timers.js", () => ({
35
- clearMaxDurationTimer: clearMaxDurationTimerMock,
36
- clearTranscriptWaiter: vi.fn(),
37
- rejectTranscriptWaiter: rejectTranscriptWaiterMock,
38
- waitForFinalTranscript: vi.fn(),
39
- }));
40
-
41
- vi.mock("./lookup.js", () => ({
42
- getCallByProviderCallId: getCallByProviderCallIdMock,
43
- }));
44
-
45
- vi.mock("../voice-mapping.js", () => ({
46
- mapVoiceToPolly: mapVoiceToPollyMock,
47
- }));
48
-
49
- vi.mock("./twiml.js", () => ({
50
- generateDtmfRedirectTwiml: generateDtmfRedirectTwimlMock,
51
- generateNotifyTwiml: generateNotifyTwimlMock,
52
- }));
53
-
54
- import { endCall, initiateCall, sendDtmf, speak } from "./outbound.js";
55
-
56
- function createActiveCallContext(params: { hangupCall?: ReturnType<typeof vi.fn> } = {}) {
57
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
58
- const hangupCall = params.hangupCall ?? vi.fn(async () => {});
59
- const ctx = {
60
- activeCalls: new Map([["call-1", call]]),
61
- providerCallIdMap: new Map([["provider-1", "call-1"]]),
62
- provider: { hangupCall },
63
- storePath: "/tmp/voice-call.json",
64
- transcriptWaiters: new Map(),
65
- maxDurationTimers: new Map(),
66
- };
67
-
68
- return { call, ctx, hangupCall };
69
- }
70
-
71
- describe("voice-call outbound helpers", () => {
72
- beforeEach(() => {
73
- vi.clearAllMocks();
74
- mapVoiceToPollyMock.mockReturnValue("Polly.Joanna");
75
- generateDtmfRedirectTwimlMock.mockReturnValue("<DtmfRedirect />");
76
- generateNotifyTwimlMock.mockReturnValue("<Response />");
77
- });
78
-
79
- it("guards initiateCall when provider, webhook, capacity, or fromNumber are missing", async () => {
80
- const base = {
81
- activeCalls: new Map(),
82
- providerCallIdMap: new Map(),
83
- config: {
84
- maxConcurrentCalls: 1,
85
- outbound: { defaultMode: "conversation", notifyHangupDelaySec: 0 },
86
- },
87
- storePath: "/tmp/voice-call.json",
88
- webhookUrl: "https://example.com/webhook",
89
- };
90
-
91
- await expect(
92
- initiateCall({ ...base, provider: undefined } as never, "+14155550123"),
93
- ).resolves.toEqual({
94
- callId: "",
95
- success: false,
96
- error: "Provider not initialized",
97
- });
98
-
99
- await expect(
100
- initiateCall(
101
- { ...base, provider: { name: "twilio" }, webhookUrl: undefined } as never,
102
- "+14155550123",
103
- ),
104
- ).resolves.toEqual({
105
- callId: "",
106
- success: false,
107
- error: "Webhook URL not configured",
108
- });
109
-
110
- const saturated = {
111
- ...base,
112
- activeCalls: new Map([["existing", {}]]),
113
- provider: { name: "twilio" },
114
- };
115
- await expect(initiateCall(saturated as never, "+14155550123")).resolves.toEqual({
116
- callId: "",
117
- success: false,
118
- error: "Maximum concurrent calls (1) reached",
119
- });
120
-
121
- await expect(
122
- initiateCall(
123
- {
124
- ...base,
125
- provider: { name: "twilio" },
126
- config: { ...base.config, fromNumber: "" },
127
- } as never,
128
- "+14155550123",
129
- ),
130
- ).resolves.toEqual({
131
- callId: "",
132
- success: false,
133
- error: "fromNumber not configured",
134
- });
135
- });
136
-
137
- it("initiates notify-mode calls with inline TwiML and records provider ids", async () => {
138
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
139
- const ctx = {
140
- activeCalls: new Map(),
141
- providerCallIdMap: new Map(),
142
- provider: { name: "twilio", initiateCall: initiateProviderCall },
143
- config: {
144
- maxConcurrentCalls: 3,
145
- outbound: { defaultMode: "conversation" },
146
- fromNumber: "+14155550100",
147
- tts: { provider: "openai", providers: { openai: { voice: "nova" } } },
148
- },
149
- storePath: "/tmp/voice-call.json",
150
- webhookUrl: "https://example.com/webhook",
151
- };
152
-
153
- const result = await initiateCall(ctx as never, "+14155550123", "session-1", {
154
- mode: "notify",
155
- message: "hello there",
156
- });
157
- expect(result.success).toBe(true);
158
- expect(result.callId).toBeTypeOf("string");
159
- expect(result.callId).not.toBe("");
160
- const callId = result.callId;
161
-
162
- expect(mapVoiceToPollyMock).toHaveBeenCalledWith("nova");
163
- expect(generateNotifyTwimlMock).toHaveBeenCalledWith("hello there", "Polly.Joanna");
164
- expect(initiateProviderCall).toHaveBeenCalledWith({
165
- callId,
166
- from: "+14155550100",
167
- to: "+14155550123",
168
- webhookUrl: "https://example.com/webhook",
169
- inlineTwiml: "<Response />",
170
- });
171
- expect(ctx.providerCallIdMap.get("provider-1")).toBe(callId);
172
- expect(ctx.activeCalls.get(callId)?.sessionKey).toBe("session-1");
173
- expect(persistCallRecordMock).toHaveBeenCalledTimes(2);
174
- });
175
-
176
- it("assigns per-call session keys to outbound calls when configured", async () => {
177
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
178
- const ctx = {
179
- activeCalls: new Map(),
180
- providerCallIdMap: new Map(),
181
- provider: { name: "twilio", initiateCall: initiateProviderCall },
182
- config: {
183
- maxConcurrentCalls: 3,
184
- outbound: { defaultMode: "conversation" },
185
- fromNumber: "+14155550100",
186
- sessionScope: "per-call",
187
- },
188
- storePath: "/tmp/voice-call.json",
189
- webhookUrl: "https://example.com/webhook",
190
- };
191
-
192
- const result = await initiateCall(ctx as never, "+14155550123");
193
-
194
- expect(result.success).toBe(true);
195
- expect(result.callId).toBeTypeOf("string");
196
- expect(result.callId).not.toBe("");
197
- expect(ctx.activeCalls.get(result.callId)?.sessionKey).toBe(`voice:call:${result.callId}`);
198
- });
199
-
200
- it("initiates conversation calls with pre-connect DTMF TwiML", async () => {
201
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
202
- const ctx = {
203
- activeCalls: new Map(),
204
- providerCallIdMap: new Map(),
205
- provider: { name: "twilio", initiateCall: initiateProviderCall },
206
- config: {
207
- maxConcurrentCalls: 3,
208
- outbound: { defaultMode: "conversation" },
209
- fromNumber: "+14155550100",
210
- },
211
- storePath: "/tmp/voice-call.json",
212
- webhookUrl: "https://example.com/webhook",
213
- };
214
-
215
- const result = await initiateCall(ctx as never, "+14155550123", "session-1", {
216
- mode: "conversation",
217
- message: "hello meet",
218
- dtmfSequence: "ww123456#",
219
- });
220
-
221
- expect(result.success).toBe(true);
222
- expect(result.callId).toBeTypeOf("string");
223
- expect(result.callId).not.toBe("");
224
- const callId = result.callId;
225
-
226
- expect(generateDtmfRedirectTwimlMock).toHaveBeenCalledWith(
227
- "ww123456#",
228
- "https://example.com/webhook",
229
- );
230
- expect(initiateProviderCall).toHaveBeenCalledWith({
231
- callId,
232
- from: "+14155550100",
233
- to: "+14155550123",
234
- webhookUrl: "https://example.com/webhook",
235
- inlineTwiml: undefined,
236
- preConnectTwiml: "<DtmfRedirect />",
237
- });
238
- const metadata = (
239
- ctx.activeCalls.get(callId) as { metadata?: Record<string, unknown> } | undefined
240
- )?.metadata;
241
- expect(metadata?.initialMessage).toBe("hello meet");
242
- expect(metadata?.mode).toBe("conversation");
243
- });
244
-
245
- it("rejects DTMF sequences outside conversation mode", async () => {
246
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
247
- const ctx = {
248
- activeCalls: new Map(),
249
- providerCallIdMap: new Map(),
250
- provider: { name: "twilio", initiateCall: initiateProviderCall },
251
- config: {
252
- maxConcurrentCalls: 3,
253
- outbound: { defaultMode: "notify" },
254
- fromNumber: "+14155550100",
255
- },
256
- storePath: "/tmp/voice-call.json",
257
- webhookUrl: "https://example.com/webhook",
258
- };
259
-
260
- await expect(
261
- initiateCall(ctx as never, "+14155550123", "session-1", {
262
- message: "hello",
263
- dtmfSequence: "123456#",
264
- }),
265
- ).resolves.toEqual({
266
- callId: "",
267
- success: false,
268
- error: "dtmfSequence requires conversation mode",
269
- });
270
-
271
- expect(initiateProviderCall).not.toHaveBeenCalled();
272
- expect(ctx.activeCalls.size).toBe(0);
273
- });
274
-
275
- it("fails initiateCall cleanly when provider initiation throws", async () => {
276
- const ctx = {
277
- activeCalls: new Map(),
278
- providerCallIdMap: new Map(),
279
- provider: {
280
- name: "mock",
281
- initiateCall: vi.fn(async () => {
282
- throw new Error("provider down");
283
- }),
284
- },
285
- config: {
286
- maxConcurrentCalls: 3,
287
- outbound: { defaultMode: "conversation" },
288
- },
289
- storePath: "/tmp/voice-call.json",
290
- webhookUrl: "https://example.com/webhook",
291
- };
292
-
293
- const result = await initiateCall(ctx as never, "+14155550123");
294
- expect(result.success).toBe(false);
295
- expect(result.error).toBe("provider down");
296
- expect(result.callId).toBeTypeOf("string");
297
- expect(result.callId).not.toBe("");
298
- expect(ctx.activeCalls.size).toBe(0);
299
- });
300
-
301
- it("speaks through connected calls and rolls back to listening on provider errors", async () => {
302
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
303
- const playTts = vi.fn(async () => {});
304
- const ctx = {
305
- activeCalls: new Map([["call-1", call]]),
306
- providerCallIdMap: new Map(),
307
- provider: { name: "twilio", playTts },
308
- config: { tts: { provider: "openai", providers: { openai: { voice: "alloy" } } } },
309
- storePath: "/tmp/voice-call.json",
310
- };
311
-
312
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
313
- expect(transitionStateMock).toHaveBeenCalledWith(call, "speaking");
314
- expect(playTts).toHaveBeenCalledWith({
315
- callId: "call-1",
316
- providerCallId: "provider-1",
317
- text: "hello",
318
- voice: "alloy",
319
- });
320
- expect(addTranscriptEntryMock).toHaveBeenCalledWith(call, "bot", "hello");
321
-
322
- playTts.mockImplementationOnce(async () => {
323
- throw new Error("tts failed");
324
- });
325
- await expect(speak(ctx as never, "call-1", "hello again")).resolves.toEqual({
326
- success: false,
327
- error: "tts failed",
328
- });
329
- expect(transitionStateMock).toHaveBeenLastCalledWith(call, "listening");
330
- });
331
-
332
- it("passes configured voice ids through to Telnyx speak", async () => {
333
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
334
- const playTts = vi.fn(async () => {});
335
- const ctx = {
336
- activeCalls: new Map([["call-1", call]]),
337
- providerCallIdMap: new Map(),
338
- provider: { name: "telnyx", playTts },
339
- config: {
340
- tts: {
341
- provider: "telnyx",
342
- providers: {
343
- telnyx: {
344
- voiceId: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
345
- },
346
- },
347
- },
348
- },
349
- storePath: "/tmp/voice-call.json",
350
- };
351
-
352
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
353
-
354
- expect(playTts).toHaveBeenCalledWith({
355
- callId: "call-1",
356
- providerCallId: "provider-1",
357
- text: "hello",
358
- voice: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
359
- });
360
- });
361
-
362
- it("uses per-number route TTS voice for routed inbound calls", async () => {
363
- const call = {
364
- callId: "call-1",
365
- providerCallId: "provider-1",
366
- state: "active",
367
- to: "+15550002222",
368
- metadata: { numberRouteKey: "+15550002222" },
369
- };
370
- const playTts = vi.fn(async () => {});
371
- const ctx = {
372
- activeCalls: new Map([["call-1", call]]),
373
- providerCallIdMap: new Map(),
374
- provider: { name: "twilio", playTts },
375
- config: {
376
- tts: { provider: "openai", providers: { openai: { voice: "coral" } } },
377
- numbers: {
378
- "+15550002222": {
379
- tts: {
380
- providers: {
381
- openai: { voice: "alloy" },
382
- },
383
- },
384
- },
385
- },
386
- },
387
- storePath: "/tmp/voice-call.json",
388
- };
389
-
390
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
391
-
392
- expect(playTts).toHaveBeenCalledWith({
393
- callId: "call-1",
394
- providerCallId: "provider-1",
395
- text: "hello",
396
- voice: "alloy",
397
- });
398
- });
399
-
400
- it("sends DTMF through connected provider calls", async () => {
401
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
402
- const sendDtmfProvider = vi.fn(async () => {});
403
- const ctx = {
404
- activeCalls: new Map([["call-1", call]]),
405
- providerCallIdMap: new Map(),
406
- provider: { name: "twilio", sendDtmf: sendDtmfProvider },
407
- config: {},
408
- storePath: "/tmp/voice-call.json",
409
- };
410
-
411
- await expect(sendDtmf(ctx as never, "call-1", "ww123#")).resolves.toEqual({
412
- success: true,
413
- });
414
- expect(sendDtmfProvider).toHaveBeenCalledWith({
415
- callId: "call-1",
416
- providerCallId: "provider-1",
417
- digits: "ww123#",
418
- });
419
- });
420
-
421
- it("rejects invalid or unsupported outbound DTMF", async () => {
422
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
423
- const ctx = {
424
- activeCalls: new Map([["call-1", call]]),
425
- providerCallIdMap: new Map(),
426
- provider: { name: "telnyx" },
427
- config: {},
428
- storePath: "/tmp/voice-call.json",
429
- };
430
-
431
- await expect(sendDtmf(ctx as never, "call-1", "abc")).resolves.toEqual({
432
- success: false,
433
- error: "digits may only contain digits, *, #, comma, w, p",
434
- });
435
- await expect(sendDtmf(ctx as never, "call-1", "123#")).resolves.toEqual({
436
- success: false,
437
- error: "telnyx does not support outbound DTMF",
438
- });
439
- });
440
-
441
- it("ends connected calls, clears timers, and rejects pending transcripts", async () => {
442
- const { call, ctx, hangupCall } = createActiveCallContext();
443
-
444
- const beforeEndMs = Date.now();
445
- await expect(endCall(ctx as never, "call-1")).resolves.toEqual({ success: true });
446
- const afterEndMs = Date.now();
447
- expect(hangupCall).toHaveBeenCalledWith({
448
- callId: "call-1",
449
- providerCallId: "provider-1",
450
- reason: "hangup-bot",
451
- });
452
- expect((call as { endReason?: string }).endReason).toBe("hangup-bot");
453
- const endedAt = (call as { endedAt?: unknown }).endedAt;
454
- expect(endedAt).toBeTypeOf("number");
455
- if (typeof endedAt === "number") {
456
- expect(endedAt).toBeGreaterThanOrEqual(beforeEndMs);
457
- expect(endedAt).toBeLessThanOrEqual(afterEndMs);
458
- }
459
- expect(transitionStateMock).toHaveBeenCalledWith(call, "hangup-bot");
460
- expect(clearMaxDurationTimerMock).toHaveBeenCalledWith(
461
- { maxDurationTimers: ctx.maxDurationTimers },
462
- "call-1",
463
- );
464
- expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
465
- { transcriptWaiters: ctx.transcriptWaiters },
466
- "call-1",
467
- "Call ended: hangup-bot",
468
- );
469
- expect(ctx.activeCalls.size).toBe(0);
470
- expect(ctx.providerCallIdMap.size).toBe(0);
471
- });
472
-
473
- it("preserves timeout reasons when ending timed out calls", async () => {
474
- const { call, ctx, hangupCall } = createActiveCallContext();
475
-
476
- const beforeEndMs = Date.now();
477
- await expect(endCall(ctx as never, "call-1", { reason: "timeout" })).resolves.toEqual({
478
- success: true,
479
- });
480
- const afterEndMs = Date.now();
481
- expect(hangupCall).toHaveBeenCalledWith({
482
- callId: "call-1",
483
- providerCallId: "provider-1",
484
- reason: "timeout",
485
- });
486
- expect((call as { endReason?: string }).endReason).toBe("timeout");
487
- const endedAt = (call as { endedAt?: unknown }).endedAt;
488
- expect(endedAt).toBeTypeOf("number");
489
- if (typeof endedAt === "number") {
490
- expect(endedAt).toBeGreaterThanOrEqual(beforeEndMs);
491
- expect(endedAt).toBeLessThanOrEqual(afterEndMs);
492
- }
493
- expect(transitionStateMock).toHaveBeenCalledWith(call, "timeout");
494
- expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
495
- { transcriptWaiters: ctx.transcriptWaiters },
496
- "call-1",
497
- "Call ended: timeout",
498
- );
499
- });
500
-
501
- it("handles missing, disconnected, and already-ended calls", async () => {
502
- await expect(
503
- speak(
504
- {
505
- activeCalls: new Map(),
506
- providerCallIdMap: new Map(),
507
- provider: { name: "twilio", playTts: vi.fn() },
508
- config: {},
509
- storePath: "/tmp/voice-call.json",
510
- } as never,
511
- "missing",
512
- "hello",
513
- ),
514
- ).resolves.toEqual({ success: false, error: "Call not found" });
515
-
516
- await expect(
517
- endCall(
518
- {
519
- activeCalls: new Map([
520
- ["call-1", { callId: "call-1", state: "completed", providerCallId: "provider-1" }],
521
- ]),
522
- providerCallIdMap: new Map(),
523
- provider: { hangupCall: vi.fn() },
524
- storePath: "/tmp/voice-call.json",
525
- transcriptWaiters: new Map(),
526
- maxDurationTimers: new Map(),
527
- } as never,
528
- "call-1",
529
- ),
530
- ).resolves.toEqual({ success: true });
531
- });
532
-
533
- it("issues a stream session and threads streamUrl + streamAuthToken through for Telnyx realtime", async () => {
534
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "call-control-1" }));
535
- const streamSessionIssuer = vi.fn(() => ({
536
- token: "token-xyz",
537
- streamUrl: "wss://example.test/voice/stream/realtime/token-xyz",
538
- }));
539
- const ctx = {
540
- activeCalls: new Map(),
541
- providerCallIdMap: new Map(),
542
- provider: { name: "telnyx", initiateCall: initiateProviderCall },
543
- config: {
544
- maxConcurrentCalls: 3,
545
- outbound: { defaultMode: "conversation" },
546
- fromNumber: "+14155550100",
547
- realtime: { enabled: true },
548
- },
549
- storePath: "/tmp/voice-call.json",
550
- webhookUrl: "https://example.com/webhook",
551
- streamSessionIssuer,
552
- };
553
-
554
- const result = await initiateCall(ctx as never, "+14155550123");
555
-
556
- expect(result.success).toBe(true);
557
- expect(streamSessionIssuer).toHaveBeenCalledTimes(1);
558
- const issuerCall = (
559
- streamSessionIssuer.mock.calls as unknown as Array<
560
- [{ providerName: string; direction: string; to: string }]
561
- >
562
- )[0]?.[0];
563
- expect(issuerCall?.providerName).toBe("telnyx");
564
- expect(issuerCall?.direction).toBe("outbound");
565
- expect(issuerCall?.to).toBe("+14155550123");
566
- const providerCall = (
567
- initiateProviderCall.mock.calls as unknown as Array<
568
- [{ streamUrl?: string; streamAuthToken?: string }]
569
- >
570
- )[0]?.[0];
571
- expect(providerCall?.streamUrl).toBe("wss://example.test/voice/stream/realtime/token-xyz");
572
- expect(providerCall?.streamAuthToken).toBe("token-xyz");
573
- });
574
-
575
- it("skips the stream session for Twilio realtime (Twilio learns the URL from TwiML)", async () => {
576
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
577
- const streamSessionIssuer = vi.fn(() => ({
578
- token: "should-not-be-used",
579
- streamUrl: "wss://example.test/should-not-be-used",
580
- }));
581
- const ctx = {
582
- activeCalls: new Map(),
583
- providerCallIdMap: new Map(),
584
- provider: { name: "twilio", initiateCall: initiateProviderCall },
585
- config: {
586
- maxConcurrentCalls: 3,
587
- outbound: { defaultMode: "conversation" },
588
- fromNumber: "+14155550100",
589
- realtime: { enabled: true },
590
- },
591
- storePath: "/tmp/voice-call.json",
592
- webhookUrl: "https://example.com/webhook",
593
- streamSessionIssuer,
594
- };
595
-
596
- const result = await initiateCall(ctx as never, "+14155550123");
597
-
598
- expect(result.success).toBe(true);
599
- expect(streamSessionIssuer).not.toHaveBeenCalled();
600
- const providerCall = (
601
- initiateProviderCall.mock.calls as unknown as Array<[Record<string, unknown>]>
602
- )[0]?.[0];
603
- expect(providerCall?.streamUrl).toBeUndefined();
604
- expect(providerCall?.streamAuthToken).toBeUndefined();
605
- });
606
-
607
- it("does not issue a stream session when realtime is disabled", async () => {
608
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "call-control-1" }));
609
- const streamSessionIssuer = vi.fn();
610
- const ctx = {
611
- activeCalls: new Map(),
612
- providerCallIdMap: new Map(),
613
- provider: { name: "telnyx", initiateCall: initiateProviderCall },
614
- config: {
615
- maxConcurrentCalls: 3,
616
- outbound: { defaultMode: "conversation" },
617
- fromNumber: "+14155550100",
618
- realtime: { enabled: false },
619
- },
620
- storePath: "/tmp/voice-call.json",
621
- webhookUrl: "https://example.com/webhook",
622
- streamSessionIssuer,
623
- };
624
-
625
- await initiateCall(ctx as never, "+14155550123");
626
-
627
- expect(streamSessionIssuer).not.toHaveBeenCalled();
628
- });
629
- });