@openclaw/voice-call 2026.5.2 → 2026.5.3-beta.1
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/api.js +2 -0
- package/dist/call-status-CXldV5o8.js +32 -0
- package/dist/cli-metadata.js +12 -0
- package/dist/config-7w04YpHh.js +548 -0
- package/dist/config-compat-B0me39_4.js +129 -0
- package/dist/guarded-json-api-Btx5EE4w.js +591 -0
- package/dist/http-headers-BrnxBasF.js +10 -0
- package/dist/index.js +1284 -0
- package/dist/mock-CeKvfVEd.js +135 -0
- package/dist/plivo-B-a7KFoT.js +393 -0
- package/dist/realtime-handler-B63CIDP2.js +325 -0
- package/dist/realtime-transcription.runtime-B2h70y2W.js +2 -0
- package/dist/realtime-voice.runtime-Bkh4nvLn.js +2 -0
- package/dist/response-generator-BrcmwDZU.js +182 -0
- package/dist/response-model-CyF5K80p.js +12 -0
- package/dist/runtime-api.js +6 -0
- package/dist/runtime-entry-88ytYAQa.js +3119 -0
- package/dist/runtime-entry.js +2 -0
- package/dist/setup-api.js +37 -0
- package/dist/telnyx-jjBE8boz.js +260 -0
- package/dist/twilio-1OqbcXLL.js +676 -0
- package/dist/voice-mapping-BYDGdWGx.js +40 -0
- package/package.json +14 -6
- package/api.ts +0 -16
- package/cli-metadata.ts +0 -10
- package/config-api.ts +0 -12
- package/index.test.ts +0 -943
- package/index.ts +0 -794
- package/runtime-api.ts +0 -20
- package/runtime-entry.ts +0 -1
- package/setup-api.ts +0 -47
- package/src/allowlist.test.ts +0 -18
- package/src/allowlist.ts +0 -19
- package/src/cli.ts +0 -845
- package/src/config-compat.test.ts +0 -120
- package/src/config-compat.ts +0 -227
- package/src/config.test.ts +0 -479
- package/src/config.ts +0 -808
- package/src/core-bridge.ts +0 -14
- package/src/deep-merge.test.ts +0 -40
- package/src/deep-merge.ts +0 -23
- package/src/gateway-continue-operation.ts +0 -200
- package/src/http-headers.test.ts +0 -16
- package/src/http-headers.ts +0 -15
- package/src/manager/context.ts +0 -42
- package/src/manager/events.test.ts +0 -581
- package/src/manager/events.ts +0 -288
- package/src/manager/lifecycle.ts +0 -53
- package/src/manager/lookup.test.ts +0 -52
- package/src/manager/lookup.ts +0 -35
- package/src/manager/outbound.test.ts +0 -528
- package/src/manager/outbound.ts +0 -486
- package/src/manager/state.ts +0 -48
- package/src/manager/store.ts +0 -106
- package/src/manager/timers.test.ts +0 -129
- package/src/manager/timers.ts +0 -113
- package/src/manager/twiml.test.ts +0 -13
- package/src/manager/twiml.ts +0 -17
- package/src/manager.closed-loop.test.ts +0 -236
- package/src/manager.inbound-allowlist.test.ts +0 -188
- package/src/manager.notify.test.ts +0 -377
- package/src/manager.restore.test.ts +0 -183
- package/src/manager.test-harness.ts +0 -127
- package/src/manager.ts +0 -392
- package/src/media-stream.test.ts +0 -768
- package/src/media-stream.ts +0 -708
- package/src/providers/base.ts +0 -97
- package/src/providers/mock.test.ts +0 -78
- package/src/providers/mock.ts +0 -185
- package/src/providers/plivo.test.ts +0 -93
- package/src/providers/plivo.ts +0 -601
- package/src/providers/shared/call-status.test.ts +0 -24
- package/src/providers/shared/call-status.ts +0 -24
- package/src/providers/shared/guarded-json-api.test.ts +0 -106
- package/src/providers/shared/guarded-json-api.ts +0 -42
- package/src/providers/telnyx.test.ts +0 -340
- package/src/providers/telnyx.ts +0 -394
- package/src/providers/twilio/api.test.ts +0 -145
- package/src/providers/twilio/api.ts +0 -93
- package/src/providers/twilio/twiml-policy.test.ts +0 -84
- package/src/providers/twilio/twiml-policy.ts +0 -87
- package/src/providers/twilio/webhook.ts +0 -34
- package/src/providers/twilio.test.ts +0 -591
- package/src/providers/twilio.ts +0 -861
- package/src/providers/twilio.types.ts +0 -17
- package/src/realtime-defaults.ts +0 -3
- package/src/realtime-fast-context.test.ts +0 -88
- package/src/realtime-fast-context.ts +0 -165
- package/src/realtime-transcription.runtime.ts +0 -4
- package/src/realtime-voice.runtime.ts +0 -5
- package/src/response-generator.test.ts +0 -321
- package/src/response-generator.ts +0 -318
- package/src/response-model.test.ts +0 -71
- package/src/response-model.ts +0 -23
- package/src/runtime.test.ts +0 -536
- package/src/runtime.ts +0 -510
- package/src/telephony-audio.test.ts +0 -61
- package/src/telephony-audio.ts +0 -12
- package/src/telephony-tts.test.ts +0 -196
- package/src/telephony-tts.ts +0 -235
- package/src/test-fixtures.ts +0 -73
- package/src/tts-provider-voice.test.ts +0 -34
- package/src/tts-provider-voice.ts +0 -21
- package/src/tunnel.test.ts +0 -166
- package/src/tunnel.ts +0 -314
- package/src/types.ts +0 -291
- package/src/utils.test.ts +0 -17
- package/src/utils.ts +0 -14
- package/src/voice-mapping.test.ts +0 -34
- package/src/voice-mapping.ts +0 -68
- package/src/webhook/realtime-handler.test.ts +0 -598
- package/src/webhook/realtime-handler.ts +0 -485
- package/src/webhook/stale-call-reaper.test.ts +0 -88
- package/src/webhook/stale-call-reaper.ts +0 -38
- package/src/webhook/tailscale.test.ts +0 -214
- package/src/webhook/tailscale.ts +0 -129
- package/src/webhook-exposure.test.ts +0 -33
- package/src/webhook-exposure.ts +0 -84
- package/src/webhook-security.test.ts +0 -770
- package/src/webhook-security.ts +0 -994
- package/src/webhook.hangup-once.lifecycle.test.ts +0 -135
- package/src/webhook.test.ts +0 -1470
- package/src/webhook.ts +0 -908
- package/src/webhook.types.ts +0 -5
- package/src/websocket-test-support.ts +0 -72
- package/tsconfig.json +0 -16
|
@@ -1,591 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type { WebhookContext } from "../types.js";
|
|
3
|
-
import { TwilioProvider } from "./twilio.js";
|
|
4
|
-
import { TwilioApiError } from "./twilio/api.js";
|
|
5
|
-
|
|
6
|
-
const STREAM_URL = "wss://example.ngrok.app/voice/stream";
|
|
7
|
-
|
|
8
|
-
function createProvider(): TwilioProvider {
|
|
9
|
-
return new TwilioProvider(
|
|
10
|
-
{ accountSid: "AC123", authToken: "secret" },
|
|
11
|
-
{ publicUrl: "https://example.ngrok.app", streamPath: "/voice/stream" },
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function createContext(rawBody: string, query?: WebhookContext["query"]): WebhookContext {
|
|
16
|
-
return {
|
|
17
|
-
headers: {},
|
|
18
|
-
rawBody,
|
|
19
|
-
url: "https://example.ngrok.app/voice/twilio",
|
|
20
|
-
method: "POST",
|
|
21
|
-
query,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function expectStreamingTwiml(body: string) {
|
|
26
|
-
expect(body).toContain(STREAM_URL);
|
|
27
|
-
expect(body).toContain('<Parameter name="token" value="');
|
|
28
|
-
expect(body).toContain("<Connect>");
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function expectQueueTwiml(body: string) {
|
|
32
|
-
expect(body).toContain("Please hold while we connect you.");
|
|
33
|
-
expect(body).toContain("<Enqueue");
|
|
34
|
-
expect(body).toContain("hold-queue");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function requireResponseBody(body: string | undefined): string {
|
|
38
|
-
if (!body) {
|
|
39
|
-
throw new Error("Twilio provider did not return a response body");
|
|
40
|
-
}
|
|
41
|
-
return body;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function requireEvent<T>(event: T | undefined, message: string): T {
|
|
45
|
-
if (!event) {
|
|
46
|
-
throw new Error(message);
|
|
47
|
-
}
|
|
48
|
-
return event;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
type TwilioApiRequest = (
|
|
52
|
-
endpoint: string,
|
|
53
|
-
params: Record<string, string | string[]>,
|
|
54
|
-
options?: { allowNotFound?: boolean },
|
|
55
|
-
) => Promise<unknown>;
|
|
56
|
-
|
|
57
|
-
function createApiRequestMock(impl?: TwilioApiRequest) {
|
|
58
|
-
return vi.fn<TwilioApiRequest>(impl ?? (async () => ({})));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function createTwilioCallStateRaceError(): TwilioApiError {
|
|
62
|
-
return new TwilioApiError(
|
|
63
|
-
400,
|
|
64
|
-
JSON.stringify({
|
|
65
|
-
code: 21220,
|
|
66
|
-
message: "Call is not in-progress. Cannot redirect.",
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function configureTelephonyTwiMlFallback(params: { providerCallId: string; streamSid?: string }) {
|
|
72
|
-
const provider = createProvider();
|
|
73
|
-
const apiRequest = createApiRequestMock();
|
|
74
|
-
(
|
|
75
|
-
provider as unknown as {
|
|
76
|
-
apiRequest: TwilioApiRequest;
|
|
77
|
-
}
|
|
78
|
-
).apiRequest = apiRequest;
|
|
79
|
-
(
|
|
80
|
-
provider as unknown as {
|
|
81
|
-
callWebhookUrls: Map<string, string>;
|
|
82
|
-
}
|
|
83
|
-
).callWebhookUrls.set(params.providerCallId, "https://example.ngrok.app/voice/twilio");
|
|
84
|
-
if (params.streamSid) {
|
|
85
|
-
provider.registerCallStream(params.providerCallId, params.streamSid);
|
|
86
|
-
}
|
|
87
|
-
return { provider, apiRequest };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
describe("TwilioProvider", () => {
|
|
91
|
-
it("sends direct initial TwiML for notify-mode outbound calls", async () => {
|
|
92
|
-
const provider = createProvider();
|
|
93
|
-
const apiRequest = createApiRequestMock(async () => ({ sid: "CA123", status: "queued" }));
|
|
94
|
-
(
|
|
95
|
-
provider as unknown as {
|
|
96
|
-
apiRequest: TwilioApiRequest;
|
|
97
|
-
}
|
|
98
|
-
).apiRequest = apiRequest;
|
|
99
|
-
|
|
100
|
-
const result = await provider.initiateCall({
|
|
101
|
-
callId: "call-1",
|
|
102
|
-
from: "+14155550100",
|
|
103
|
-
to: "+14155550123",
|
|
104
|
-
webhookUrl: "https://example.ngrok.app/voice/webhook",
|
|
105
|
-
inlineTwiml: "<Response><Say>Hello</Say></Response>",
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
expect(result).toEqual({ providerCallId: "CA123", status: "queued" });
|
|
109
|
-
expect(apiRequest).toHaveBeenCalledWith(
|
|
110
|
-
"/Calls.json",
|
|
111
|
-
expect.objectContaining({
|
|
112
|
-
To: "+14155550123",
|
|
113
|
-
From: "+14155550100",
|
|
114
|
-
Twiml: "<Response><Say>Hello</Say></Response>",
|
|
115
|
-
StatusCallback: "https://example.ngrok.app/voice/webhook?callId=call-1&type=status",
|
|
116
|
-
StatusCallbackEvent: ["initiated", "ringing", "answered", "completed"],
|
|
117
|
-
}),
|
|
118
|
-
);
|
|
119
|
-
expect(apiRequest.mock.calls[0]?.[1]).not.toHaveProperty("Url");
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("uses the webhook URL for conversation outbound calls", async () => {
|
|
123
|
-
const provider = createProvider();
|
|
124
|
-
const apiRequest = createApiRequestMock(async () => ({ sid: "CA123", status: "queued" }));
|
|
125
|
-
(
|
|
126
|
-
provider as unknown as {
|
|
127
|
-
apiRequest: TwilioApiRequest;
|
|
128
|
-
}
|
|
129
|
-
).apiRequest = apiRequest;
|
|
130
|
-
|
|
131
|
-
await provider.initiateCall({
|
|
132
|
-
callId: "call-1",
|
|
133
|
-
from: "+14155550100",
|
|
134
|
-
to: "+14155550123",
|
|
135
|
-
webhookUrl: "https://example.ngrok.app/voice/webhook",
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
expect(apiRequest).toHaveBeenCalledWith(
|
|
139
|
-
"/Calls.json",
|
|
140
|
-
expect.objectContaining({
|
|
141
|
-
Url: "https://example.ngrok.app/voice/webhook?callId=call-1",
|
|
142
|
-
StatusCallback: "https://example.ngrok.app/voice/webhook?callId=call-1&type=status",
|
|
143
|
-
}),
|
|
144
|
-
);
|
|
145
|
-
expect(apiRequest.mock.calls[0]?.[1]).not.toHaveProperty("Twiml");
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it("returns streaming TwiML for outbound conversation calls before in-progress", () => {
|
|
149
|
-
const provider = createProvider();
|
|
150
|
-
const ctx = createContext("CallStatus=initiated&Direction=outbound-api&CallSid=CA123", {
|
|
151
|
-
callId: "call-1",
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
const result = provider.parseWebhookEvent(ctx);
|
|
155
|
-
|
|
156
|
-
expectStreamingTwiml(requireResponseBody(result.providerResponseBody));
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it("serves pre-connect TwiML once before outbound streaming starts", async () => {
|
|
160
|
-
const provider = createProvider();
|
|
161
|
-
(
|
|
162
|
-
provider as unknown as {
|
|
163
|
-
apiRequest: TwilioApiRequest;
|
|
164
|
-
}
|
|
165
|
-
).apiRequest = vi.fn<TwilioApiRequest>(async () => ({
|
|
166
|
-
sid: "CA999",
|
|
167
|
-
status: "queued",
|
|
168
|
-
}));
|
|
169
|
-
const preConnectTwiml = '<Response><Play digits="ww123456#" /></Response>';
|
|
170
|
-
|
|
171
|
-
await provider.initiateCall({
|
|
172
|
-
callId: "call-1",
|
|
173
|
-
from: "+15550000001",
|
|
174
|
-
to: "+15550000002",
|
|
175
|
-
webhookUrl: "https://example.ngrok.app/voice/twilio",
|
|
176
|
-
preConnectTwiml,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
const first = provider.parseWebhookEvent(
|
|
180
|
-
createContext("CallStatus=initiated&Direction=outbound-api&CallSid=CA999", {
|
|
181
|
-
callId: "call-1",
|
|
182
|
-
}),
|
|
183
|
-
);
|
|
184
|
-
expect(requireResponseBody(first.providerResponseBody)).toBe(preConnectTwiml);
|
|
185
|
-
|
|
186
|
-
const second = provider.parseWebhookEvent(
|
|
187
|
-
createContext("CallStatus=initiated&Direction=outbound-api&CallSid=CA999", {
|
|
188
|
-
callId: "call-1",
|
|
189
|
-
}),
|
|
190
|
-
);
|
|
191
|
-
expectStreamingTwiml(requireResponseBody(second.providerResponseBody));
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("returns empty TwiML for status callbacks", () => {
|
|
195
|
-
const provider = createProvider();
|
|
196
|
-
const ctx = createContext("CallStatus=ringing&Direction=outbound-api", {
|
|
197
|
-
callId: "call-1",
|
|
198
|
-
type: "status",
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const result = provider.parseWebhookEvent(ctx);
|
|
202
|
-
|
|
203
|
-
expect(result.providerResponseBody).toBe(
|
|
204
|
-
'<?xml version="1.0" encoding="UTF-8"?><Response></Response>',
|
|
205
|
-
);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it("returns streaming TwiML for inbound calls", () => {
|
|
209
|
-
const provider = createProvider();
|
|
210
|
-
const ctx = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA456");
|
|
211
|
-
|
|
212
|
-
const result = provider.parseWebhookEvent(ctx);
|
|
213
|
-
|
|
214
|
-
expectStreamingTwiml(requireResponseBody(result.providerResponseBody));
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("returns queue TwiML for second inbound call when first call is active", () => {
|
|
218
|
-
const provider = createProvider();
|
|
219
|
-
const firstInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA111");
|
|
220
|
-
const secondInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA222");
|
|
221
|
-
|
|
222
|
-
const firstResult = provider.parseWebhookEvent(firstInbound);
|
|
223
|
-
const secondResult = provider.parseWebhookEvent(secondInbound);
|
|
224
|
-
|
|
225
|
-
expectStreamingTwiml(requireResponseBody(firstResult.providerResponseBody));
|
|
226
|
-
expectQueueTwiml(requireResponseBody(secondResult.providerResponseBody));
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it("connects next inbound call after unregisterCallStream cleanup", () => {
|
|
230
|
-
const provider = createProvider();
|
|
231
|
-
const firstInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA311");
|
|
232
|
-
const secondInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA322");
|
|
233
|
-
|
|
234
|
-
provider.parseWebhookEvent(firstInbound);
|
|
235
|
-
provider.unregisterCallStream("CA311");
|
|
236
|
-
const secondResult = provider.parseWebhookEvent(secondInbound);
|
|
237
|
-
|
|
238
|
-
const secondBody = requireResponseBody(secondResult.providerResponseBody);
|
|
239
|
-
expectStreamingTwiml(secondBody);
|
|
240
|
-
expect(secondBody).not.toContain("hold-queue");
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
it("cleans up active inbound call on completed status callback", () => {
|
|
244
|
-
const provider = createProvider();
|
|
245
|
-
const firstInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA411");
|
|
246
|
-
const completed = createContext("CallStatus=completed&Direction=inbound&CallSid=CA411", {
|
|
247
|
-
type: "status",
|
|
248
|
-
});
|
|
249
|
-
const nextInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA422");
|
|
250
|
-
|
|
251
|
-
provider.parseWebhookEvent(firstInbound);
|
|
252
|
-
provider.parseWebhookEvent(completed);
|
|
253
|
-
const nextResult = provider.parseWebhookEvent(nextInbound);
|
|
254
|
-
|
|
255
|
-
const nextBody = requireResponseBody(nextResult.providerResponseBody);
|
|
256
|
-
expectStreamingTwiml(nextBody);
|
|
257
|
-
expect(nextBody).not.toContain("hold-queue");
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
it("cleans up active inbound call on canceled status callback", () => {
|
|
261
|
-
const provider = createProvider();
|
|
262
|
-
const firstInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA511");
|
|
263
|
-
const canceled = createContext("CallStatus=canceled&Direction=inbound&CallSid=CA511", {
|
|
264
|
-
type: "status",
|
|
265
|
-
});
|
|
266
|
-
const nextInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA522");
|
|
267
|
-
|
|
268
|
-
provider.parseWebhookEvent(firstInbound);
|
|
269
|
-
provider.parseWebhookEvent(canceled);
|
|
270
|
-
const nextResult = provider.parseWebhookEvent(nextInbound);
|
|
271
|
-
|
|
272
|
-
const nextBody = requireResponseBody(nextResult.providerResponseBody);
|
|
273
|
-
expectStreamingTwiml(nextBody);
|
|
274
|
-
expect(nextBody).not.toContain("hold-queue");
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("QUEUE_TWIML references /voice/hold-music waitUrl", () => {
|
|
278
|
-
const provider = createProvider();
|
|
279
|
-
const firstInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA611");
|
|
280
|
-
const secondInbound = createContext("CallStatus=ringing&Direction=inbound&CallSid=CA622");
|
|
281
|
-
|
|
282
|
-
provider.parseWebhookEvent(firstInbound);
|
|
283
|
-
const result = provider.parseWebhookEvent(secondInbound);
|
|
284
|
-
|
|
285
|
-
expect(requireResponseBody(result.providerResponseBody)).toContain(
|
|
286
|
-
'waitUrl="/voice/hold-music"',
|
|
287
|
-
);
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
it("uses a stable fallback dedupeKey for identical request payloads", () => {
|
|
291
|
-
const provider = createProvider();
|
|
292
|
-
const rawBody = "CallSid=CA789&Direction=inbound&SpeechResult=hello";
|
|
293
|
-
const ctxA = {
|
|
294
|
-
...createContext(rawBody, { callId: "call-1", turnToken: "turn-1" }),
|
|
295
|
-
headers: { "i-twilio-idempotency-token": "idem-123" },
|
|
296
|
-
};
|
|
297
|
-
const ctxB = {
|
|
298
|
-
...createContext(rawBody, { callId: "call-1", turnToken: "turn-1" }),
|
|
299
|
-
headers: { "i-twilio-idempotency-token": "idem-123" },
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
const eventA = provider.parseWebhookEvent(ctxA).events[0];
|
|
303
|
-
const eventB = provider.parseWebhookEvent(ctxB).events[0];
|
|
304
|
-
|
|
305
|
-
const first = requireEvent(eventA, "expected first fallback Twilio event");
|
|
306
|
-
const second = requireEvent(eventB, "expected second fallback Twilio event");
|
|
307
|
-
expect(first.id).not.toBe(second.id);
|
|
308
|
-
expect(first.dedupeKey).toContain("twilio:fallback:");
|
|
309
|
-
expect(first.dedupeKey).toBe(second.dedupeKey);
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
it("uses verified request key for dedupe and ignores idempotency header changes", () => {
|
|
313
|
-
const provider = createProvider();
|
|
314
|
-
const rawBody = "CallSid=CA790&Direction=inbound&SpeechResult=hello";
|
|
315
|
-
const ctxA = {
|
|
316
|
-
...createContext(rawBody, { callId: "call-1", turnToken: "turn-1" }),
|
|
317
|
-
headers: { "i-twilio-idempotency-token": "idem-a" },
|
|
318
|
-
};
|
|
319
|
-
const ctxB = {
|
|
320
|
-
...createContext(rawBody, { callId: "call-1", turnToken: "turn-1" }),
|
|
321
|
-
headers: { "i-twilio-idempotency-token": "idem-b" },
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
const eventA = provider.parseWebhookEvent(ctxA, { verifiedRequestKey: "twilio:req:abc" })
|
|
325
|
-
.events[0];
|
|
326
|
-
const eventB = provider.parseWebhookEvent(ctxB, { verifiedRequestKey: "twilio:req:abc" })
|
|
327
|
-
.events[0];
|
|
328
|
-
|
|
329
|
-
expect(requireEvent(eventA, "expected verified first Twilio event").dedupeKey).toBe(
|
|
330
|
-
"twilio:req:abc",
|
|
331
|
-
);
|
|
332
|
-
expect(requireEvent(eventB, "expected verified second Twilio event").dedupeKey).toBe(
|
|
333
|
-
"twilio:req:abc",
|
|
334
|
-
);
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
it("keeps turnToken from query on speech events", () => {
|
|
338
|
-
const provider = createProvider();
|
|
339
|
-
const ctx = createContext("CallSid=CA222&Direction=inbound&SpeechResult=hello", {
|
|
340
|
-
callId: "call-2",
|
|
341
|
-
turnToken: "turn-xyz",
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
const event = provider.parseWebhookEvent(ctx).events[0];
|
|
345
|
-
const parsed = requireEvent(event, "expected speech event from Twilio webhook");
|
|
346
|
-
expect(parsed.type).toBe("call.speech");
|
|
347
|
-
expect(parsed.turnToken).toBe("turn-xyz");
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it("fails when an active stream exists but telephony TTS is unavailable", async () => {
|
|
351
|
-
const { provider, apiRequest } = configureTelephonyTwiMlFallback({
|
|
352
|
-
providerCallId: "CA-stream",
|
|
353
|
-
streamSid: "MZ-stream",
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
await expect(
|
|
357
|
-
provider.playTts({
|
|
358
|
-
callId: "call-stream",
|
|
359
|
-
providerCallId: "CA-stream",
|
|
360
|
-
text: "Hello stream",
|
|
361
|
-
}),
|
|
362
|
-
).rejects.toThrow("refusing TwiML fallback");
|
|
363
|
-
expect(apiRequest).not.toHaveBeenCalled();
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
it("falls back to TwiML when no active stream exists and telephony TTS is unavailable", async () => {
|
|
367
|
-
const { provider, apiRequest } = configureTelephonyTwiMlFallback({
|
|
368
|
-
providerCallId: "CA-nostream",
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
await expect(
|
|
372
|
-
provider.playTts({
|
|
373
|
-
callId: "call-nostream",
|
|
374
|
-
providerCallId: "CA-nostream",
|
|
375
|
-
text: "Hello TwiML",
|
|
376
|
-
}),
|
|
377
|
-
).resolves.toBeUndefined();
|
|
378
|
-
expect(apiRequest).toHaveBeenCalledTimes(1);
|
|
379
|
-
const call = apiRequest.mock.calls[0];
|
|
380
|
-
const endpoint = call[0];
|
|
381
|
-
const params = call[1] as { Twiml?: string };
|
|
382
|
-
expect(endpoint).toBe("/Calls/CA-nostream.json");
|
|
383
|
-
expect(params.Twiml).toContain("<Say");
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
it("retries TwiML fallback when Twilio briefly rejects a live-call update as not in progress", async () => {
|
|
387
|
-
vi.useFakeTimers();
|
|
388
|
-
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
389
|
-
try {
|
|
390
|
-
const { provider, apiRequest } = configureTelephonyTwiMlFallback({
|
|
391
|
-
providerCallId: "CA-race-play",
|
|
392
|
-
});
|
|
393
|
-
apiRequest.mockRejectedValueOnce(createTwilioCallStateRaceError()).mockResolvedValueOnce({});
|
|
394
|
-
|
|
395
|
-
const playback = provider.playTts({
|
|
396
|
-
callId: "call-race-play",
|
|
397
|
-
providerCallId: "CA-race-play",
|
|
398
|
-
text: "Hello after race",
|
|
399
|
-
});
|
|
400
|
-
await Promise.resolve();
|
|
401
|
-
expect(apiRequest).toHaveBeenCalledTimes(1);
|
|
402
|
-
|
|
403
|
-
await vi.advanceTimersByTimeAsync(250);
|
|
404
|
-
await expect(playback).resolves.toBeUndefined();
|
|
405
|
-
|
|
406
|
-
expect(apiRequest).toHaveBeenCalledTimes(2);
|
|
407
|
-
expect(apiRequest.mock.calls[0]?.[0]).toBe("/Calls/CA-race-play.json");
|
|
408
|
-
expect(apiRequest.mock.calls[1]?.[0]).toBe("/Calls/CA-race-play.json");
|
|
409
|
-
expect(warn).toHaveBeenCalledWith(
|
|
410
|
-
"[voice-call] Twilio playTts update hit call state race (21220); retrying in 250ms",
|
|
411
|
-
);
|
|
412
|
-
} finally {
|
|
413
|
-
warn.mockRestore();
|
|
414
|
-
vi.useRealTimers();
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
it("sends DTMF by updating the call and redirecting back to the webhook", async () => {
|
|
419
|
-
const { provider, apiRequest } = configureTelephonyTwiMlFallback({
|
|
420
|
-
providerCallId: "CA-dtmf",
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
await expect(
|
|
424
|
-
provider.sendDtmf({
|
|
425
|
-
callId: "call-dtmf",
|
|
426
|
-
providerCallId: "CA-dtmf",
|
|
427
|
-
digits: "ww123#",
|
|
428
|
-
}),
|
|
429
|
-
).resolves.toBeUndefined();
|
|
430
|
-
|
|
431
|
-
expect(apiRequest).toHaveBeenCalledTimes(1);
|
|
432
|
-
const call = apiRequest.mock.calls[0];
|
|
433
|
-
const endpoint = call[0];
|
|
434
|
-
const params = call[1] as { Twiml?: string };
|
|
435
|
-
expect(endpoint).toBe("/Calls/CA-dtmf.json");
|
|
436
|
-
expect(params.Twiml).toContain('<Play digits="ww123#"');
|
|
437
|
-
expect(params.Twiml).toContain("<Redirect");
|
|
438
|
-
expect(params.Twiml).toContain("https://example.ngrok.app/voice/twilio");
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
it("retries startListening when Twilio briefly rejects a live-call update as not in progress", async () => {
|
|
442
|
-
vi.useFakeTimers();
|
|
443
|
-
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
444
|
-
try {
|
|
445
|
-
const { provider, apiRequest } = configureTelephonyTwiMlFallback({
|
|
446
|
-
providerCallId: "CA-race-listen",
|
|
447
|
-
});
|
|
448
|
-
apiRequest.mockRejectedValueOnce(createTwilioCallStateRaceError()).mockResolvedValueOnce({});
|
|
449
|
-
|
|
450
|
-
const listening = provider.startListening({
|
|
451
|
-
callId: "call-race-listen",
|
|
452
|
-
providerCallId: "CA-race-listen",
|
|
453
|
-
});
|
|
454
|
-
await Promise.resolve();
|
|
455
|
-
expect(apiRequest).toHaveBeenCalledTimes(1);
|
|
456
|
-
|
|
457
|
-
await vi.advanceTimersByTimeAsync(250);
|
|
458
|
-
await expect(listening).resolves.toBeUndefined();
|
|
459
|
-
|
|
460
|
-
expect(apiRequest).toHaveBeenCalledTimes(2);
|
|
461
|
-
expect(apiRequest.mock.calls[0]?.[0]).toBe("/Calls/CA-race-listen.json");
|
|
462
|
-
expect(apiRequest.mock.calls[1]?.[0]).toBe("/Calls/CA-race-listen.json");
|
|
463
|
-
expect(warn).toHaveBeenCalledWith(
|
|
464
|
-
"[voice-call] Twilio startListening update hit call state race (21220); retrying in 250ms",
|
|
465
|
-
);
|
|
466
|
-
} finally {
|
|
467
|
-
warn.mockRestore();
|
|
468
|
-
vi.useRealTimers();
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
it("ignores stale stream unregister requests that do not match current stream SID", () => {
|
|
473
|
-
const provider = createProvider();
|
|
474
|
-
provider.registerCallStream("CA-reconnect", "MZ-new");
|
|
475
|
-
|
|
476
|
-
provider.unregisterCallStream("CA-reconnect", "MZ-old");
|
|
477
|
-
expect(provider.hasRegisteredStream("CA-reconnect")).toBe(true);
|
|
478
|
-
|
|
479
|
-
provider.unregisterCallStream("CA-reconnect", "MZ-new");
|
|
480
|
-
expect(provider.hasRegisteredStream("CA-reconnect")).toBe(false);
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
it("times out telephony synthesis in stream mode and does not send completion mark", async () => {
|
|
484
|
-
vi.useFakeTimers();
|
|
485
|
-
try {
|
|
486
|
-
const provider = createProvider();
|
|
487
|
-
provider.registerCallStream("CA-timeout", "MZ-timeout");
|
|
488
|
-
|
|
489
|
-
const sendAudio = vi.fn();
|
|
490
|
-
const sendMark = vi.fn();
|
|
491
|
-
const mediaStreamHandler = {
|
|
492
|
-
queueTts: async (
|
|
493
|
-
_streamSid: string,
|
|
494
|
-
playFn: (signal: AbortSignal) => Promise<void>,
|
|
495
|
-
): Promise<void> => {
|
|
496
|
-
await playFn(new AbortController().signal);
|
|
497
|
-
},
|
|
498
|
-
sendAudio,
|
|
499
|
-
sendMark,
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
provider.setMediaStreamHandler(mediaStreamHandler as never);
|
|
503
|
-
provider.setTTSProvider({
|
|
504
|
-
synthesisTimeoutMs: 5000,
|
|
505
|
-
synthesizeForTelephony: async () => await new Promise<Buffer>(() => {}),
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
const playExpectation = expect(
|
|
509
|
-
provider.playTts({
|
|
510
|
-
callId: "call-timeout",
|
|
511
|
-
providerCallId: "CA-timeout",
|
|
512
|
-
text: "Timeout me",
|
|
513
|
-
}),
|
|
514
|
-
).rejects.toThrow("Telephony TTS synthesis timed out after 5000ms");
|
|
515
|
-
await vi.advanceTimersByTimeAsync(5_100);
|
|
516
|
-
await playExpectation;
|
|
517
|
-
expect(sendAudio).toHaveBeenCalled();
|
|
518
|
-
expect(sendMark).not.toHaveBeenCalled();
|
|
519
|
-
} finally {
|
|
520
|
-
vi.useRealTimers();
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
it("fails stream playback when all audio sends and completion mark are dropped", async () => {
|
|
525
|
-
const provider = createProvider();
|
|
526
|
-
provider.registerCallStream("CA-dropped", "MZ-dropped");
|
|
527
|
-
|
|
528
|
-
const sendAudio = vi.fn(() => ({ sent: false }));
|
|
529
|
-
const sendMark = vi.fn(() => ({ sent: false }));
|
|
530
|
-
const mediaStreamHandler = {
|
|
531
|
-
queueTts: async (
|
|
532
|
-
_streamSid: string,
|
|
533
|
-
playFn: (signal: AbortSignal) => Promise<void>,
|
|
534
|
-
): Promise<void> => {
|
|
535
|
-
await playFn(new AbortController().signal);
|
|
536
|
-
},
|
|
537
|
-
sendAudio,
|
|
538
|
-
sendMark,
|
|
539
|
-
};
|
|
540
|
-
|
|
541
|
-
provider.setMediaStreamHandler(mediaStreamHandler as never);
|
|
542
|
-
provider.setTTSProvider({
|
|
543
|
-
synthesisTimeoutMs: 5000,
|
|
544
|
-
synthesizeForTelephony: async () => Buffer.alloc(320),
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
await expect(
|
|
548
|
-
provider.playTts({
|
|
549
|
-
callId: "call-dropped",
|
|
550
|
-
providerCallId: "CA-dropped",
|
|
551
|
-
text: "Dropped audio",
|
|
552
|
-
}),
|
|
553
|
-
).rejects.toThrow("Telephony stream playback failed");
|
|
554
|
-
expect(sendAudio).toHaveBeenCalled();
|
|
555
|
-
expect(sendMark).toHaveBeenCalledTimes(1);
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
it("fails stream playback when telephony synthesis returns empty audio", async () => {
|
|
559
|
-
const provider = createProvider();
|
|
560
|
-
provider.registerCallStream("CA-empty", "MZ-empty");
|
|
561
|
-
|
|
562
|
-
const sendAudio = vi.fn();
|
|
563
|
-
const sendMark = vi.fn();
|
|
564
|
-
const mediaStreamHandler = {
|
|
565
|
-
queueTts: async (
|
|
566
|
-
_streamSid: string,
|
|
567
|
-
playFn: (signal: AbortSignal) => Promise<void>,
|
|
568
|
-
): Promise<void> => {
|
|
569
|
-
await playFn(new AbortController().signal);
|
|
570
|
-
},
|
|
571
|
-
sendAudio,
|
|
572
|
-
sendMark,
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
provider.setMediaStreamHandler(mediaStreamHandler as never);
|
|
576
|
-
provider.setTTSProvider({
|
|
577
|
-
synthesisTimeoutMs: 5000,
|
|
578
|
-
synthesizeForTelephony: async () => Buffer.alloc(0),
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
await expect(
|
|
582
|
-
provider.playTts({
|
|
583
|
-
callId: "call-empty",
|
|
584
|
-
providerCallId: "CA-empty",
|
|
585
|
-
text: "Empty audio",
|
|
586
|
-
}),
|
|
587
|
-
).rejects.toThrow("Telephony TTS produced no audio");
|
|
588
|
-
expect(sendAudio).toHaveBeenCalled();
|
|
589
|
-
expect(sendMark).not.toHaveBeenCalled();
|
|
590
|
-
});
|
|
591
|
-
});
|