@openclaw/voice-call 2026.3.13 → 2026.5.2-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/README.md +27 -5
- package/api.ts +16 -0
- package/cli-metadata.ts +10 -0
- package/config-api.ts +12 -0
- package/index.test.ts +943 -0
- package/index.ts +379 -149
- package/openclaw.plugin.json +384 -157
- package/package.json +35 -5
- package/runtime-api.ts +20 -0
- package/runtime-entry.ts +1 -0
- package/setup-api.ts +47 -0
- package/src/allowlist.test.ts +18 -0
- package/src/cli.ts +533 -68
- package/src/config-compat.test.ts +120 -0
- package/src/config-compat.ts +227 -0
- package/src/config.test.ts +273 -12
- package/src/config.ts +355 -72
- package/src/core-bridge.ts +2 -147
- package/src/deep-merge.test.ts +40 -0
- package/src/gateway-continue-operation.ts +200 -0
- package/src/http-headers.ts +6 -3
- package/src/manager/context.ts +6 -5
- package/src/manager/events.test.ts +243 -19
- package/src/manager/events.ts +61 -31
- package/src/manager/lifecycle.ts +53 -0
- package/src/manager/lookup.test.ts +52 -0
- package/src/manager/outbound.test.ts +528 -0
- package/src/manager/outbound.ts +163 -57
- package/src/manager/store.ts +18 -6
- package/src/manager/timers.test.ts +129 -0
- package/src/manager/timers.ts +4 -3
- package/src/manager/twiml.test.ts +13 -0
- package/src/manager/twiml.ts +8 -0
- package/src/manager.closed-loop.test.ts +30 -12
- package/src/manager.inbound-allowlist.test.ts +77 -10
- package/src/manager.notify.test.ts +344 -20
- package/src/manager.restore.test.ts +95 -8
- package/src/manager.test-harness.ts +8 -6
- package/src/manager.ts +79 -5
- package/src/media-stream.test.ts +578 -81
- package/src/media-stream.ts +235 -54
- package/src/providers/base.ts +19 -0
- package/src/providers/mock.ts +7 -1
- package/src/providers/plivo.test.ts +50 -6
- package/src/providers/plivo.ts +14 -6
- package/src/providers/shared/call-status.ts +2 -1
- package/src/providers/shared/guarded-json-api.test.ts +106 -0
- package/src/providers/shared/guarded-json-api.ts +1 -1
- package/src/providers/telnyx.test.ts +178 -6
- package/src/providers/telnyx.ts +40 -3
- package/src/providers/twilio/api.test.ts +145 -0
- package/src/providers/twilio/api.ts +67 -16
- package/src/providers/twilio/twiml-policy.ts +6 -10
- package/src/providers/twilio/webhook.ts +1 -1
- package/src/providers/twilio.test.ts +425 -25
- package/src/providers/twilio.ts +230 -77
- package/src/providers/twilio.types.ts +17 -0
- package/src/realtime-defaults.ts +3 -0
- package/src/realtime-fast-context.test.ts +88 -0
- package/src/realtime-fast-context.ts +165 -0
- package/src/realtime-transcription.runtime.ts +4 -0
- package/src/realtime-voice.runtime.ts +5 -0
- package/src/response-generator.test.ts +321 -0
- package/src/response-generator.ts +213 -53
- package/src/response-model.test.ts +71 -0
- package/src/response-model.ts +23 -0
- package/src/runtime.test.ts +429 -0
- package/src/runtime.ts +270 -24
- package/src/telephony-audio.test.ts +61 -0
- package/src/telephony-audio.ts +1 -79
- package/src/telephony-tts.test.ts +133 -12
- package/src/telephony-tts.ts +155 -2
- package/src/test-fixtures.ts +28 -7
- package/src/tts-provider-voice.test.ts +34 -0
- package/src/tts-provider-voice.ts +21 -0
- package/src/tunnel.test.ts +166 -0
- package/src/tunnel.ts +1 -1
- package/src/types.ts +24 -37
- package/src/utils.test.ts +17 -0
- package/src/voice-mapping.test.ts +34 -0
- package/src/voice-mapping.ts +3 -2
- package/src/webhook/realtime-handler.test.ts +598 -0
- package/src/webhook/realtime-handler.ts +485 -0
- package/src/webhook/stale-call-reaper.test.ts +88 -0
- package/src/webhook/stale-call-reaper.ts +5 -0
- package/src/webhook/tailscale.test.ts +214 -0
- package/src/webhook/tailscale.ts +19 -5
- package/src/webhook-exposure.test.ts +33 -0
- package/src/webhook-exposure.ts +84 -0
- package/src/webhook-security.test.ts +172 -21
- package/src/webhook-security.ts +43 -29
- package/src/webhook.hangup-once.lifecycle.test.ts +135 -0
- package/src/webhook.test.ts +1145 -27
- package/src/webhook.ts +523 -102
- package/src/webhook.types.ts +5 -0
- package/src/websocket-test-support.ts +72 -0
- package/tsconfig.json +16 -0
- package/CHANGELOG.md +0 -121
- package/src/providers/index.ts +0 -10
- package/src/providers/stt-openai-realtime.test.ts +0 -42
- package/src/providers/stt-openai-realtime.ts +0 -311
- package/src/providers/tts-openai.test.ts +0 -43
- package/src/providers/tts-openai.ts +0 -221
|
@@ -0,0 +1,528 @@
|
|
|
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).toEqual({
|
|
158
|
+
callId: expect.any(String),
|
|
159
|
+
success: true,
|
|
160
|
+
});
|
|
161
|
+
const callId = result.callId;
|
|
162
|
+
|
|
163
|
+
expect(mapVoiceToPollyMock).toHaveBeenCalledWith("nova");
|
|
164
|
+
expect(generateNotifyTwimlMock).toHaveBeenCalledWith("hello there", "Polly.Joanna");
|
|
165
|
+
expect(initiateProviderCall).toHaveBeenCalledWith({
|
|
166
|
+
callId,
|
|
167
|
+
from: "+14155550100",
|
|
168
|
+
to: "+14155550123",
|
|
169
|
+
webhookUrl: "https://example.com/webhook",
|
|
170
|
+
inlineTwiml: "<Response />",
|
|
171
|
+
});
|
|
172
|
+
expect(ctx.providerCallIdMap.get("provider-1")).toBe(callId);
|
|
173
|
+
expect(ctx.activeCalls.get(callId)?.sessionKey).toBe("session-1");
|
|
174
|
+
expect(persistCallRecordMock).toHaveBeenCalledTimes(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("assigns per-call session keys to outbound calls when configured", async () => {
|
|
178
|
+
const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
|
|
179
|
+
const ctx = {
|
|
180
|
+
activeCalls: new Map(),
|
|
181
|
+
providerCallIdMap: new Map(),
|
|
182
|
+
provider: { name: "twilio", initiateCall: initiateProviderCall },
|
|
183
|
+
config: {
|
|
184
|
+
maxConcurrentCalls: 3,
|
|
185
|
+
outbound: { defaultMode: "conversation" },
|
|
186
|
+
fromNumber: "+14155550100",
|
|
187
|
+
sessionScope: "per-call",
|
|
188
|
+
},
|
|
189
|
+
storePath: "/tmp/voice-call.json",
|
|
190
|
+
webhookUrl: "https://example.com/webhook",
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const result = await initiateCall(ctx as never, "+14155550123");
|
|
194
|
+
|
|
195
|
+
expect(result).toEqual({
|
|
196
|
+
callId: expect.any(String),
|
|
197
|
+
success: true,
|
|
198
|
+
});
|
|
199
|
+
expect(ctx.activeCalls.get(result.callId)?.sessionKey).toBe(`voice:call:${result.callId}`);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("initiates conversation calls with pre-connect DTMF TwiML", async () => {
|
|
203
|
+
const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
|
|
204
|
+
const ctx = {
|
|
205
|
+
activeCalls: new Map(),
|
|
206
|
+
providerCallIdMap: new Map(),
|
|
207
|
+
provider: { name: "twilio", initiateCall: initiateProviderCall },
|
|
208
|
+
config: {
|
|
209
|
+
maxConcurrentCalls: 3,
|
|
210
|
+
outbound: { defaultMode: "conversation" },
|
|
211
|
+
fromNumber: "+14155550100",
|
|
212
|
+
},
|
|
213
|
+
storePath: "/tmp/voice-call.json",
|
|
214
|
+
webhookUrl: "https://example.com/webhook",
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const result = await initiateCall(ctx as never, "+14155550123", "session-1", {
|
|
218
|
+
mode: "conversation",
|
|
219
|
+
message: "hello meet",
|
|
220
|
+
dtmfSequence: "ww123456#",
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
expect(result).toEqual({
|
|
224
|
+
callId: expect.any(String),
|
|
225
|
+
success: true,
|
|
226
|
+
});
|
|
227
|
+
const callId = result.callId;
|
|
228
|
+
|
|
229
|
+
expect(generateDtmfRedirectTwimlMock).toHaveBeenCalledWith(
|
|
230
|
+
"ww123456#",
|
|
231
|
+
"https://example.com/webhook",
|
|
232
|
+
);
|
|
233
|
+
expect(initiateProviderCall).toHaveBeenCalledWith({
|
|
234
|
+
callId,
|
|
235
|
+
from: "+14155550100",
|
|
236
|
+
to: "+14155550123",
|
|
237
|
+
webhookUrl: "https://example.com/webhook",
|
|
238
|
+
inlineTwiml: undefined,
|
|
239
|
+
preConnectTwiml: "<DtmfRedirect />",
|
|
240
|
+
});
|
|
241
|
+
expect(ctx.activeCalls.get(callId)?.metadata).toMatchObject({
|
|
242
|
+
initialMessage: "hello meet",
|
|
243
|
+
mode: "conversation",
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("rejects DTMF sequences outside conversation mode", async () => {
|
|
248
|
+
const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
|
|
249
|
+
const ctx = {
|
|
250
|
+
activeCalls: new Map(),
|
|
251
|
+
providerCallIdMap: new Map(),
|
|
252
|
+
provider: { name: "twilio", initiateCall: initiateProviderCall },
|
|
253
|
+
config: {
|
|
254
|
+
maxConcurrentCalls: 3,
|
|
255
|
+
outbound: { defaultMode: "notify" },
|
|
256
|
+
fromNumber: "+14155550100",
|
|
257
|
+
},
|
|
258
|
+
storePath: "/tmp/voice-call.json",
|
|
259
|
+
webhookUrl: "https://example.com/webhook",
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
await expect(
|
|
263
|
+
initiateCall(ctx as never, "+14155550123", "session-1", {
|
|
264
|
+
message: "hello",
|
|
265
|
+
dtmfSequence: "123456#",
|
|
266
|
+
}),
|
|
267
|
+
).resolves.toEqual({
|
|
268
|
+
callId: "",
|
|
269
|
+
success: false,
|
|
270
|
+
error: "dtmfSequence requires conversation mode",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
expect(initiateProviderCall).not.toHaveBeenCalled();
|
|
274
|
+
expect(ctx.activeCalls.size).toBe(0);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("fails initiateCall cleanly when provider initiation throws", async () => {
|
|
278
|
+
const ctx = {
|
|
279
|
+
activeCalls: new Map(),
|
|
280
|
+
providerCallIdMap: new Map(),
|
|
281
|
+
provider: {
|
|
282
|
+
name: "mock",
|
|
283
|
+
initiateCall: vi.fn(async () => {
|
|
284
|
+
throw new Error("provider down");
|
|
285
|
+
}),
|
|
286
|
+
},
|
|
287
|
+
config: {
|
|
288
|
+
maxConcurrentCalls: 3,
|
|
289
|
+
outbound: { defaultMode: "conversation" },
|
|
290
|
+
},
|
|
291
|
+
storePath: "/tmp/voice-call.json",
|
|
292
|
+
webhookUrl: "https://example.com/webhook",
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
await expect(initiateCall(ctx as never, "+14155550123")).resolves.toEqual({
|
|
296
|
+
callId: expect.any(String),
|
|
297
|
+
success: false,
|
|
298
|
+
error: "provider down",
|
|
299
|
+
});
|
|
300
|
+
expect(ctx.activeCalls.size).toBe(0);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("speaks through connected calls and rolls back to listening on provider errors", async () => {
|
|
304
|
+
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
|
|
305
|
+
const playTts = vi.fn(async () => {});
|
|
306
|
+
const ctx = {
|
|
307
|
+
activeCalls: new Map([["call-1", call]]),
|
|
308
|
+
providerCallIdMap: new Map(),
|
|
309
|
+
provider: { name: "twilio", playTts },
|
|
310
|
+
config: { tts: { provider: "openai", providers: { openai: { voice: "alloy" } } } },
|
|
311
|
+
storePath: "/tmp/voice-call.json",
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
|
|
315
|
+
expect(transitionStateMock).toHaveBeenCalledWith(call, "speaking");
|
|
316
|
+
expect(playTts).toHaveBeenCalledWith({
|
|
317
|
+
callId: "call-1",
|
|
318
|
+
providerCallId: "provider-1",
|
|
319
|
+
text: "hello",
|
|
320
|
+
voice: "alloy",
|
|
321
|
+
});
|
|
322
|
+
expect(addTranscriptEntryMock).toHaveBeenCalledWith(call, "bot", "hello");
|
|
323
|
+
|
|
324
|
+
playTts.mockImplementationOnce(async () => {
|
|
325
|
+
throw new Error("tts failed");
|
|
326
|
+
});
|
|
327
|
+
await expect(speak(ctx as never, "call-1", "hello again")).resolves.toEqual({
|
|
328
|
+
success: false,
|
|
329
|
+
error: "tts failed",
|
|
330
|
+
});
|
|
331
|
+
expect(transitionStateMock).toHaveBeenLastCalledWith(call, "listening");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("passes configured voice ids through to Telnyx speak", async () => {
|
|
335
|
+
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
|
|
336
|
+
const playTts = vi.fn(async () => {});
|
|
337
|
+
const ctx = {
|
|
338
|
+
activeCalls: new Map([["call-1", call]]),
|
|
339
|
+
providerCallIdMap: new Map(),
|
|
340
|
+
provider: { name: "telnyx", playTts },
|
|
341
|
+
config: {
|
|
342
|
+
tts: {
|
|
343
|
+
provider: "telnyx",
|
|
344
|
+
providers: {
|
|
345
|
+
telnyx: {
|
|
346
|
+
voiceId: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
storePath: "/tmp/voice-call.json",
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
|
|
355
|
+
|
|
356
|
+
expect(playTts).toHaveBeenCalledWith({
|
|
357
|
+
callId: "call-1",
|
|
358
|
+
providerCallId: "provider-1",
|
|
359
|
+
text: "hello",
|
|
360
|
+
voice: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("uses per-number route TTS voice for routed inbound calls", async () => {
|
|
365
|
+
const call = {
|
|
366
|
+
callId: "call-1",
|
|
367
|
+
providerCallId: "provider-1",
|
|
368
|
+
state: "active",
|
|
369
|
+
to: "+15550002222",
|
|
370
|
+
metadata: { numberRouteKey: "+15550002222" },
|
|
371
|
+
};
|
|
372
|
+
const playTts = vi.fn(async () => {});
|
|
373
|
+
const ctx = {
|
|
374
|
+
activeCalls: new Map([["call-1", call]]),
|
|
375
|
+
providerCallIdMap: new Map(),
|
|
376
|
+
provider: { name: "twilio", playTts },
|
|
377
|
+
config: {
|
|
378
|
+
tts: { provider: "openai", providers: { openai: { voice: "coral" } } },
|
|
379
|
+
numbers: {
|
|
380
|
+
"+15550002222": {
|
|
381
|
+
tts: {
|
|
382
|
+
providers: {
|
|
383
|
+
openai: { voice: "alloy" },
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
storePath: "/tmp/voice-call.json",
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
|
|
393
|
+
|
|
394
|
+
expect(playTts).toHaveBeenCalledWith({
|
|
395
|
+
callId: "call-1",
|
|
396
|
+
providerCallId: "provider-1",
|
|
397
|
+
text: "hello",
|
|
398
|
+
voice: "alloy",
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it("sends DTMF through connected provider calls", async () => {
|
|
403
|
+
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
|
|
404
|
+
const sendDtmfProvider = vi.fn(async () => {});
|
|
405
|
+
const ctx = {
|
|
406
|
+
activeCalls: new Map([["call-1", call]]),
|
|
407
|
+
providerCallIdMap: new Map(),
|
|
408
|
+
provider: { name: "twilio", sendDtmf: sendDtmfProvider },
|
|
409
|
+
config: {},
|
|
410
|
+
storePath: "/tmp/voice-call.json",
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
await expect(sendDtmf(ctx as never, "call-1", "ww123#")).resolves.toEqual({
|
|
414
|
+
success: true,
|
|
415
|
+
});
|
|
416
|
+
expect(sendDtmfProvider).toHaveBeenCalledWith({
|
|
417
|
+
callId: "call-1",
|
|
418
|
+
providerCallId: "provider-1",
|
|
419
|
+
digits: "ww123#",
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("rejects invalid or unsupported outbound DTMF", async () => {
|
|
424
|
+
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
|
|
425
|
+
const ctx = {
|
|
426
|
+
activeCalls: new Map([["call-1", call]]),
|
|
427
|
+
providerCallIdMap: new Map(),
|
|
428
|
+
provider: { name: "telnyx" },
|
|
429
|
+
config: {},
|
|
430
|
+
storePath: "/tmp/voice-call.json",
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
await expect(sendDtmf(ctx as never, "call-1", "abc")).resolves.toEqual({
|
|
434
|
+
success: false,
|
|
435
|
+
error: "digits may only contain digits, *, #, comma, w, p",
|
|
436
|
+
});
|
|
437
|
+
await expect(sendDtmf(ctx as never, "call-1", "123#")).resolves.toEqual({
|
|
438
|
+
success: false,
|
|
439
|
+
error: "telnyx does not support outbound DTMF",
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("ends connected calls, clears timers, and rejects pending transcripts", async () => {
|
|
444
|
+
const { call, ctx, hangupCall } = createActiveCallContext();
|
|
445
|
+
|
|
446
|
+
await expect(endCall(ctx as never, "call-1")).resolves.toEqual({ success: true });
|
|
447
|
+
expect(hangupCall).toHaveBeenCalledWith({
|
|
448
|
+
callId: "call-1",
|
|
449
|
+
providerCallId: "provider-1",
|
|
450
|
+
reason: "hangup-bot",
|
|
451
|
+
});
|
|
452
|
+
expect(call).toEqual(
|
|
453
|
+
expect.objectContaining({
|
|
454
|
+
endReason: "hangup-bot",
|
|
455
|
+
endedAt: expect.any(Number),
|
|
456
|
+
}),
|
|
457
|
+
);
|
|
458
|
+
expect(transitionStateMock).toHaveBeenCalledWith(call, "hangup-bot");
|
|
459
|
+
expect(clearMaxDurationTimerMock).toHaveBeenCalledWith(
|
|
460
|
+
{ maxDurationTimers: ctx.maxDurationTimers },
|
|
461
|
+
"call-1",
|
|
462
|
+
);
|
|
463
|
+
expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
|
|
464
|
+
{ transcriptWaiters: ctx.transcriptWaiters },
|
|
465
|
+
"call-1",
|
|
466
|
+
"Call ended: hangup-bot",
|
|
467
|
+
);
|
|
468
|
+
expect(ctx.activeCalls.size).toBe(0);
|
|
469
|
+
expect(ctx.providerCallIdMap.size).toBe(0);
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it("preserves timeout reasons when ending timed out calls", async () => {
|
|
473
|
+
const { call, ctx, hangupCall } = createActiveCallContext();
|
|
474
|
+
|
|
475
|
+
await expect(endCall(ctx as never, "call-1", { reason: "timeout" })).resolves.toEqual({
|
|
476
|
+
success: true,
|
|
477
|
+
});
|
|
478
|
+
expect(hangupCall).toHaveBeenCalledWith({
|
|
479
|
+
callId: "call-1",
|
|
480
|
+
providerCallId: "provider-1",
|
|
481
|
+
reason: "timeout",
|
|
482
|
+
});
|
|
483
|
+
expect(call).toEqual(
|
|
484
|
+
expect.objectContaining({
|
|
485
|
+
endReason: "timeout",
|
|
486
|
+
endedAt: expect.any(Number),
|
|
487
|
+
}),
|
|
488
|
+
);
|
|
489
|
+
expect(transitionStateMock).toHaveBeenCalledWith(call, "timeout");
|
|
490
|
+
expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
|
|
491
|
+
{ transcriptWaiters: ctx.transcriptWaiters },
|
|
492
|
+
"call-1",
|
|
493
|
+
"Call ended: timeout",
|
|
494
|
+
);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it("handles missing, disconnected, and already-ended calls", async () => {
|
|
498
|
+
await expect(
|
|
499
|
+
speak(
|
|
500
|
+
{
|
|
501
|
+
activeCalls: new Map(),
|
|
502
|
+
providerCallIdMap: new Map(),
|
|
503
|
+
provider: { name: "twilio", playTts: vi.fn() },
|
|
504
|
+
config: {},
|
|
505
|
+
storePath: "/tmp/voice-call.json",
|
|
506
|
+
} as never,
|
|
507
|
+
"missing",
|
|
508
|
+
"hello",
|
|
509
|
+
),
|
|
510
|
+
).resolves.toEqual({ success: false, error: "Call not found" });
|
|
511
|
+
|
|
512
|
+
await expect(
|
|
513
|
+
endCall(
|
|
514
|
+
{
|
|
515
|
+
activeCalls: new Map([
|
|
516
|
+
["call-1", { callId: "call-1", state: "completed", providerCallId: "provider-1" }],
|
|
517
|
+
]),
|
|
518
|
+
providerCallIdMap: new Map(),
|
|
519
|
+
provider: { hangupCall: vi.fn() },
|
|
520
|
+
storePath: "/tmp/voice-call.json",
|
|
521
|
+
transcriptWaiters: new Map(),
|
|
522
|
+
maxDurationTimers: new Map(),
|
|
523
|
+
} as never,
|
|
524
|
+
"call-1",
|
|
525
|
+
),
|
|
526
|
+
).resolves.toEqual({ success: true });
|
|
527
|
+
});
|
|
528
|
+
});
|