@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,129 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
const { persistCallRecordMock } = vi.hoisted(() => ({
|
|
4
|
-
persistCallRecordMock: vi.fn(),
|
|
5
|
-
}));
|
|
6
|
-
|
|
7
|
-
vi.mock("./store.js", () => ({
|
|
8
|
-
persistCallRecord: persistCallRecordMock,
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
clearMaxDurationTimer,
|
|
13
|
-
clearTranscriptWaiter,
|
|
14
|
-
rejectTranscriptWaiter,
|
|
15
|
-
resolveTranscriptWaiter,
|
|
16
|
-
startMaxDurationTimer,
|
|
17
|
-
waitForFinalTranscript,
|
|
18
|
-
} from "./timers.js";
|
|
19
|
-
|
|
20
|
-
describe("voice-call manager timers", () => {
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
vi.useFakeTimers();
|
|
23
|
-
vi.clearAllMocks();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
vi.useRealTimers();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("starts and clears max duration timers, persisting timeout metadata before delegation", async () => {
|
|
31
|
-
const call = { id: "call-1", state: "active" };
|
|
32
|
-
const ctx = {
|
|
33
|
-
activeCalls: new Map([["call-1", call]]),
|
|
34
|
-
maxDurationTimers: new Map(),
|
|
35
|
-
config: { maxDurationSeconds: 5 },
|
|
36
|
-
storePath: "/tmp/voice-call",
|
|
37
|
-
};
|
|
38
|
-
const onTimeout = vi.fn(async () => {});
|
|
39
|
-
|
|
40
|
-
startMaxDurationTimer({
|
|
41
|
-
ctx: ctx as never,
|
|
42
|
-
callId: "call-1",
|
|
43
|
-
onTimeout,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
expect(ctx.maxDurationTimers.has("call-1")).toBe(true);
|
|
47
|
-
|
|
48
|
-
await vi.advanceTimersByTimeAsync(5_000);
|
|
49
|
-
|
|
50
|
-
expect(call).toEqual({ id: "call-1", state: "active", endReason: "timeout" });
|
|
51
|
-
expect(persistCallRecordMock).toHaveBeenCalledWith("/tmp/voice-call", call);
|
|
52
|
-
expect(onTimeout).toHaveBeenCalledWith("call-1");
|
|
53
|
-
expect(ctx.maxDurationTimers.has("call-1")).toBe(false);
|
|
54
|
-
|
|
55
|
-
startMaxDurationTimer({
|
|
56
|
-
ctx: ctx as never,
|
|
57
|
-
callId: "call-1",
|
|
58
|
-
onTimeout,
|
|
59
|
-
});
|
|
60
|
-
clearMaxDurationTimer(ctx as never, "call-1");
|
|
61
|
-
expect(ctx.maxDurationTimers.has("call-1")).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("does not time out terminal calls", async () => {
|
|
65
|
-
const ctx = {
|
|
66
|
-
activeCalls: new Map([["call-1", { id: "call-1", state: "completed" }]]),
|
|
67
|
-
maxDurationTimers: new Map(),
|
|
68
|
-
config: { maxDurationSeconds: 5 },
|
|
69
|
-
storePath: "/tmp/voice-call",
|
|
70
|
-
};
|
|
71
|
-
const onTimeout = vi.fn(async () => {});
|
|
72
|
-
|
|
73
|
-
startMaxDurationTimer({
|
|
74
|
-
ctx: ctx as never,
|
|
75
|
-
callId: "call-1",
|
|
76
|
-
onTimeout,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
await vi.advanceTimersByTimeAsync(5_000);
|
|
80
|
-
|
|
81
|
-
expect(persistCallRecordMock).not.toHaveBeenCalled();
|
|
82
|
-
expect(onTimeout).not.toHaveBeenCalled();
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("waits for transcripts, resolves matching tokens, rejects mismatches and timeouts", async () => {
|
|
86
|
-
const ctx = {
|
|
87
|
-
transcriptWaiters: new Map(),
|
|
88
|
-
config: { transcriptTimeoutMs: 1_000 },
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const pending = waitForFinalTranscript(ctx as never, "call-1", "turn-1");
|
|
92
|
-
expect(resolveTranscriptWaiter(ctx as never, "call-1", "ignored", "turn-2")).toBe(false);
|
|
93
|
-
expect(resolveTranscriptWaiter(ctx as never, "call-1", "final transcript", "turn-1")).toBe(
|
|
94
|
-
true,
|
|
95
|
-
);
|
|
96
|
-
await expect(pending).resolves.toBe("final transcript");
|
|
97
|
-
|
|
98
|
-
const another = waitForFinalTranscript(ctx as never, "call-2");
|
|
99
|
-
rejectTranscriptWaiter(ctx as never, "call-2", "provider failed");
|
|
100
|
-
await expect(another).rejects.toThrow("provider failed");
|
|
101
|
-
|
|
102
|
-
const timedOut = waitForFinalTranscript(ctx as never, "call-3").catch((error) => error);
|
|
103
|
-
await vi.advanceTimersByTimeAsync(1_000);
|
|
104
|
-
await expect(timedOut).resolves.toEqual(
|
|
105
|
-
expect.objectContaining({
|
|
106
|
-
message: "Timed out waiting for transcript after 1000ms",
|
|
107
|
-
}),
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const toClear = waitForFinalTranscript(ctx as never, "call-4");
|
|
111
|
-
clearTranscriptWaiter(ctx as never, "call-4");
|
|
112
|
-
expect(ctx.transcriptWaiters.has("call-4")).toBe(false);
|
|
113
|
-
void toClear.catch(() => {});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it("rejects duplicate transcript waiters for the same call", async () => {
|
|
117
|
-
const ctx = {
|
|
118
|
-
transcriptWaiters: new Map(),
|
|
119
|
-
config: { transcriptTimeoutMs: 1_000 },
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const pending = waitForFinalTranscript(ctx as never, "call-1");
|
|
123
|
-
await expect(waitForFinalTranscript(ctx as never, "call-1")).rejects.toThrow(
|
|
124
|
-
"Already waiting for transcript",
|
|
125
|
-
);
|
|
126
|
-
rejectTranscriptWaiter(ctx as never, "call-1", "done");
|
|
127
|
-
await expect(pending).rejects.toThrow("done");
|
|
128
|
-
});
|
|
129
|
-
});
|
package/src/manager/timers.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { TerminalStates, type CallId } from "../types.js";
|
|
2
|
-
import type { CallManagerContext } from "./context.js";
|
|
3
|
-
import { persistCallRecord } from "./store.js";
|
|
4
|
-
|
|
5
|
-
type TimerContext = Pick<
|
|
6
|
-
CallManagerContext,
|
|
7
|
-
"activeCalls" | "maxDurationTimers" | "config" | "storePath" | "transcriptWaiters"
|
|
8
|
-
>;
|
|
9
|
-
type MaxDurationTimerContext = Pick<
|
|
10
|
-
TimerContext,
|
|
11
|
-
"activeCalls" | "maxDurationTimers" | "config" | "storePath"
|
|
12
|
-
>;
|
|
13
|
-
type TranscriptWaiterContext = Pick<TimerContext, "transcriptWaiters">;
|
|
14
|
-
|
|
15
|
-
export function clearMaxDurationTimer(
|
|
16
|
-
ctx: Pick<MaxDurationTimerContext, "maxDurationTimers">,
|
|
17
|
-
callId: CallId,
|
|
18
|
-
): void {
|
|
19
|
-
const timer = ctx.maxDurationTimers.get(callId);
|
|
20
|
-
if (timer) {
|
|
21
|
-
clearTimeout(timer);
|
|
22
|
-
ctx.maxDurationTimers.delete(callId);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function startMaxDurationTimer(params: {
|
|
27
|
-
ctx: MaxDurationTimerContext;
|
|
28
|
-
callId: CallId;
|
|
29
|
-
onTimeout: (callId: CallId) => Promise<void>;
|
|
30
|
-
timeoutMs?: number;
|
|
31
|
-
}): void {
|
|
32
|
-
clearMaxDurationTimer(params.ctx, params.callId);
|
|
33
|
-
|
|
34
|
-
const maxDurationMs = params.timeoutMs ?? params.ctx.config.maxDurationSeconds * 1000;
|
|
35
|
-
console.log(
|
|
36
|
-
`[voice-call] Starting max duration timer (${Math.ceil(maxDurationMs / 1000)}s) for call ${params.callId}`,
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const timer = setTimeout(async () => {
|
|
40
|
-
params.ctx.maxDurationTimers.delete(params.callId);
|
|
41
|
-
const call = params.ctx.activeCalls.get(params.callId);
|
|
42
|
-
if (call && !TerminalStates.has(call.state)) {
|
|
43
|
-
console.log(
|
|
44
|
-
`[voice-call] Max duration reached (${Math.ceil(maxDurationMs / 1000)}s), ending call ${params.callId}`,
|
|
45
|
-
);
|
|
46
|
-
call.endReason = "timeout";
|
|
47
|
-
persistCallRecord(params.ctx.storePath, call);
|
|
48
|
-
await params.onTimeout(params.callId);
|
|
49
|
-
}
|
|
50
|
-
}, maxDurationMs);
|
|
51
|
-
|
|
52
|
-
params.ctx.maxDurationTimers.set(params.callId, timer);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function clearTranscriptWaiter(ctx: TranscriptWaiterContext, callId: CallId): void {
|
|
56
|
-
const waiter = ctx.transcriptWaiters.get(callId);
|
|
57
|
-
if (!waiter) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
clearTimeout(waiter.timeout);
|
|
61
|
-
ctx.transcriptWaiters.delete(callId);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function rejectTranscriptWaiter(
|
|
65
|
-
ctx: TranscriptWaiterContext,
|
|
66
|
-
callId: CallId,
|
|
67
|
-
reason: string,
|
|
68
|
-
): void {
|
|
69
|
-
const waiter = ctx.transcriptWaiters.get(callId);
|
|
70
|
-
if (!waiter) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
clearTranscriptWaiter(ctx, callId);
|
|
74
|
-
waiter.reject(new Error(reason));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function resolveTranscriptWaiter(
|
|
78
|
-
ctx: TranscriptWaiterContext,
|
|
79
|
-
callId: CallId,
|
|
80
|
-
transcript: string,
|
|
81
|
-
turnToken?: string,
|
|
82
|
-
): boolean {
|
|
83
|
-
const waiter = ctx.transcriptWaiters.get(callId);
|
|
84
|
-
if (!waiter) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
if (waiter.turnToken && waiter.turnToken !== turnToken) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
clearTranscriptWaiter(ctx, callId);
|
|
91
|
-
waiter.resolve(transcript);
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function waitForFinalTranscript(
|
|
96
|
-
ctx: TimerContext,
|
|
97
|
-
callId: CallId,
|
|
98
|
-
turnToken?: string,
|
|
99
|
-
): Promise<string> {
|
|
100
|
-
if (ctx.transcriptWaiters.has(callId)) {
|
|
101
|
-
return Promise.reject(new Error("Already waiting for transcript"));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const timeoutMs = ctx.config.transcriptTimeoutMs;
|
|
105
|
-
return new Promise((resolve, reject) => {
|
|
106
|
-
const timeout = setTimeout(() => {
|
|
107
|
-
ctx.transcriptWaiters.delete(callId);
|
|
108
|
-
reject(new Error(`Timed out waiting for transcript after ${timeoutMs}ms`));
|
|
109
|
-
}, timeoutMs);
|
|
110
|
-
|
|
111
|
-
ctx.transcriptWaiters.set(callId, { resolve, reject, timeout, turnToken });
|
|
112
|
-
});
|
|
113
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { generateNotifyTwiml } from "./twiml.js";
|
|
3
|
-
|
|
4
|
-
describe("generateNotifyTwiml", () => {
|
|
5
|
-
it("renders escaped xml with the requested voice", () => {
|
|
6
|
-
expect(generateNotifyTwiml(`Call <ended> & "logged"`, "Polly.Joanna"))
|
|
7
|
-
.toBe(`<?xml version="1.0" encoding="UTF-8"?>
|
|
8
|
-
<Response>
|
|
9
|
-
<Say voice="Polly.Joanna">Call <ended> & "logged"</Say>
|
|
10
|
-
<Hangup/>
|
|
11
|
-
</Response>`);
|
|
12
|
-
});
|
|
13
|
-
});
|
package/src/manager/twiml.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { escapeXml } from "../voice-mapping.js";
|
|
2
|
-
|
|
3
|
-
export function generateNotifyTwiml(message: string, voice: string): string {
|
|
4
|
-
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
5
|
-
<Response>
|
|
6
|
-
<Say voice="${voice}">${escapeXml(message)}</Say>
|
|
7
|
-
<Hangup/>
|
|
8
|
-
</Response>`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function generateDtmfRedirectTwiml(digits: string, webhookUrl: string): string {
|
|
12
|
-
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
13
|
-
<Response>
|
|
14
|
-
<Play digits="${escapeXml(digits)}" />
|
|
15
|
-
<Redirect method="POST">${escapeXml(webhookUrl)}</Redirect>
|
|
16
|
-
</Response>`;
|
|
17
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { createManagerHarness, FakeProvider, markCallAnswered } from "./manager.test-harness.js";
|
|
3
|
-
|
|
4
|
-
function requireCall(
|
|
5
|
-
manager: Awaited<ReturnType<typeof createManagerHarness>>["manager"],
|
|
6
|
-
callId: string,
|
|
7
|
-
) {
|
|
8
|
-
const call = manager.getCall(callId);
|
|
9
|
-
if (!call) {
|
|
10
|
-
throw new Error(`expected active call ${callId}`);
|
|
11
|
-
}
|
|
12
|
-
return call;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function requireTurnToken(provider: Awaited<ReturnType<typeof createManagerHarness>>["provider"]) {
|
|
16
|
-
const firstStart = provider.startListeningCalls[0];
|
|
17
|
-
if (!firstStart?.turnToken) {
|
|
18
|
-
throw new Error("expected closed-loop turn to capture a turn token");
|
|
19
|
-
}
|
|
20
|
-
return firstStart.turnToken;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
describe("CallManager closed-loop turns", () => {
|
|
24
|
-
it("completes a closed-loop turn without live audio", async () => {
|
|
25
|
-
const { manager, provider } = await createManagerHarness({
|
|
26
|
-
transcriptTimeoutMs: 5000,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const started = await manager.initiateCall("+15550000003");
|
|
30
|
-
expect(started.success).toBe(true);
|
|
31
|
-
|
|
32
|
-
markCallAnswered(manager, started.callId, "evt-closed-loop-answered");
|
|
33
|
-
|
|
34
|
-
const turnPromise = manager.continueCall(started.callId, "How can I help?");
|
|
35
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
36
|
-
|
|
37
|
-
manager.processEvent({
|
|
38
|
-
id: "evt-closed-loop-speech",
|
|
39
|
-
type: "call.speech",
|
|
40
|
-
callId: started.callId,
|
|
41
|
-
providerCallId: "request-uuid",
|
|
42
|
-
timestamp: Date.now(),
|
|
43
|
-
transcript: "Please check status",
|
|
44
|
-
isFinal: true,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const turn = await turnPromise;
|
|
48
|
-
expect(turn.success).toBe(true);
|
|
49
|
-
expect(turn.transcript).toBe("Please check status");
|
|
50
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
51
|
-
expect(provider.stopListeningCalls).toHaveLength(1);
|
|
52
|
-
|
|
53
|
-
const call = requireCall(manager, started.callId);
|
|
54
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual([
|
|
55
|
-
"How can I help?",
|
|
56
|
-
"Please check status",
|
|
57
|
-
]);
|
|
58
|
-
const metadata = call.metadata ?? {};
|
|
59
|
-
expect(typeof metadata.lastTurnLatencyMs).toBe("number");
|
|
60
|
-
expect(typeof metadata.lastTurnListenWaitMs).toBe("number");
|
|
61
|
-
expect(metadata.turnCount).toBe(1);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("rejects overlapping continueCall requests for the same call", async () => {
|
|
65
|
-
const { manager, provider } = await createManagerHarness({
|
|
66
|
-
transcriptTimeoutMs: 5000,
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const started = await manager.initiateCall("+15550000004");
|
|
70
|
-
expect(started.success).toBe(true);
|
|
71
|
-
|
|
72
|
-
markCallAnswered(manager, started.callId, "evt-overlap-answered");
|
|
73
|
-
|
|
74
|
-
const first = manager.continueCall(started.callId, "First prompt");
|
|
75
|
-
const second = await manager.continueCall(started.callId, "Second prompt");
|
|
76
|
-
expect(second.success).toBe(false);
|
|
77
|
-
expect(second.error).toBe("Already waiting for transcript");
|
|
78
|
-
|
|
79
|
-
manager.processEvent({
|
|
80
|
-
id: "evt-overlap-speech",
|
|
81
|
-
type: "call.speech",
|
|
82
|
-
callId: started.callId,
|
|
83
|
-
providerCallId: "request-uuid",
|
|
84
|
-
timestamp: Date.now(),
|
|
85
|
-
transcript: "Done",
|
|
86
|
-
isFinal: true,
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const firstResult = await first;
|
|
90
|
-
expect(firstResult.success).toBe(true);
|
|
91
|
-
expect(firstResult.transcript).toBe("Done");
|
|
92
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
93
|
-
expect(provider.stopListeningCalls).toHaveLength(1);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it("ignores speech events with mismatched turnToken while waiting for transcript", async () => {
|
|
97
|
-
const { manager, provider } = await createManagerHarness(
|
|
98
|
-
{
|
|
99
|
-
transcriptTimeoutMs: 5000,
|
|
100
|
-
},
|
|
101
|
-
new FakeProvider("twilio"),
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const started = await manager.initiateCall("+15550000004");
|
|
105
|
-
expect(started.success).toBe(true);
|
|
106
|
-
|
|
107
|
-
markCallAnswered(manager, started.callId, "evt-turn-token-answered");
|
|
108
|
-
|
|
109
|
-
const turnPromise = manager.continueCall(started.callId, "Prompt");
|
|
110
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
111
|
-
|
|
112
|
-
const expectedTurnToken = requireTurnToken(provider);
|
|
113
|
-
|
|
114
|
-
manager.processEvent({
|
|
115
|
-
id: "evt-turn-token-bad",
|
|
116
|
-
type: "call.speech",
|
|
117
|
-
callId: started.callId,
|
|
118
|
-
providerCallId: "request-uuid",
|
|
119
|
-
timestamp: Date.now(),
|
|
120
|
-
transcript: "stale replay",
|
|
121
|
-
isFinal: true,
|
|
122
|
-
turnToken: "wrong-token",
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
const pendingState = await Promise.race([
|
|
126
|
-
turnPromise.then(() => "resolved"),
|
|
127
|
-
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 0)),
|
|
128
|
-
]);
|
|
129
|
-
expect(pendingState).toBe("pending");
|
|
130
|
-
|
|
131
|
-
manager.processEvent({
|
|
132
|
-
id: "evt-turn-token-good",
|
|
133
|
-
type: "call.speech",
|
|
134
|
-
callId: started.callId,
|
|
135
|
-
providerCallId: "request-uuid",
|
|
136
|
-
timestamp: Date.now(),
|
|
137
|
-
transcript: "final answer",
|
|
138
|
-
isFinal: true,
|
|
139
|
-
turnToken: expectedTurnToken,
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
const turnResult = await turnPromise;
|
|
143
|
-
expect(turnResult.success).toBe(true);
|
|
144
|
-
expect(turnResult.transcript).toBe("final answer");
|
|
145
|
-
|
|
146
|
-
const call = requireCall(manager, started.callId);
|
|
147
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual(["Prompt", "final answer"]);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("tracks latency metadata across multiple closed-loop turns", async () => {
|
|
151
|
-
const { manager, provider } = await createManagerHarness({
|
|
152
|
-
transcriptTimeoutMs: 5000,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
const started = await manager.initiateCall("+15550000005");
|
|
156
|
-
expect(started.success).toBe(true);
|
|
157
|
-
|
|
158
|
-
markCallAnswered(manager, started.callId, "evt-multi-answered");
|
|
159
|
-
|
|
160
|
-
const firstTurn = manager.continueCall(started.callId, "First question");
|
|
161
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
162
|
-
manager.processEvent({
|
|
163
|
-
id: "evt-multi-speech-1",
|
|
164
|
-
type: "call.speech",
|
|
165
|
-
callId: started.callId,
|
|
166
|
-
providerCallId: "request-uuid",
|
|
167
|
-
timestamp: Date.now(),
|
|
168
|
-
transcript: "First answer",
|
|
169
|
-
isFinal: true,
|
|
170
|
-
});
|
|
171
|
-
await firstTurn;
|
|
172
|
-
|
|
173
|
-
const secondTurn = manager.continueCall(started.callId, "Second question");
|
|
174
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
175
|
-
manager.processEvent({
|
|
176
|
-
id: "evt-multi-speech-2",
|
|
177
|
-
type: "call.speech",
|
|
178
|
-
callId: started.callId,
|
|
179
|
-
providerCallId: "request-uuid",
|
|
180
|
-
timestamp: Date.now(),
|
|
181
|
-
transcript: "Second answer",
|
|
182
|
-
isFinal: true,
|
|
183
|
-
});
|
|
184
|
-
const secondResult = await secondTurn;
|
|
185
|
-
|
|
186
|
-
expect(secondResult.success).toBe(true);
|
|
187
|
-
|
|
188
|
-
const call = requireCall(manager, started.callId);
|
|
189
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual([
|
|
190
|
-
"First question",
|
|
191
|
-
"First answer",
|
|
192
|
-
"Second question",
|
|
193
|
-
"Second answer",
|
|
194
|
-
]);
|
|
195
|
-
const metadata = call.metadata ?? {};
|
|
196
|
-
expect(metadata.turnCount).toBe(2);
|
|
197
|
-
expect(typeof metadata.lastTurnLatencyMs).toBe("number");
|
|
198
|
-
expect(typeof metadata.lastTurnListenWaitMs).toBe("number");
|
|
199
|
-
expect(provider.startListeningCalls).toHaveLength(2);
|
|
200
|
-
expect(provider.stopListeningCalls).toHaveLength(2);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it("handles repeated closed-loop turns without waiter churn", async () => {
|
|
204
|
-
const { manager, provider } = await createManagerHarness({
|
|
205
|
-
transcriptTimeoutMs: 5000,
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
const started = await manager.initiateCall("+15550000006");
|
|
209
|
-
expect(started.success).toBe(true);
|
|
210
|
-
|
|
211
|
-
markCallAnswered(manager, started.callId, "evt-loop-answered");
|
|
212
|
-
|
|
213
|
-
for (let i = 1; i <= 5; i++) {
|
|
214
|
-
const turnPromise = manager.continueCall(started.callId, `Prompt ${i}`);
|
|
215
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
216
|
-
manager.processEvent({
|
|
217
|
-
id: `evt-loop-speech-${i}`,
|
|
218
|
-
type: "call.speech",
|
|
219
|
-
callId: started.callId,
|
|
220
|
-
providerCallId: "request-uuid",
|
|
221
|
-
timestamp: Date.now(),
|
|
222
|
-
transcript: `Answer ${i}`,
|
|
223
|
-
isFinal: true,
|
|
224
|
-
});
|
|
225
|
-
const result = await turnPromise;
|
|
226
|
-
expect(result.success).toBe(true);
|
|
227
|
-
expect(result.transcript).toBe(`Answer ${i}`);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const call = requireCall(manager, started.callId);
|
|
231
|
-
const metadata = call.metadata ?? {};
|
|
232
|
-
expect(metadata.turnCount).toBe(5);
|
|
233
|
-
expect(provider.startListeningCalls).toHaveLength(5);
|
|
234
|
-
expect(provider.stopListeningCalls).toHaveLength(5);
|
|
235
|
-
});
|
|
236
|
-
});
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { FakeProvider, createManagerHarness } from "./manager.test-harness.js";
|
|
3
|
-
|
|
4
|
-
describe("CallManager inbound allowlist", () => {
|
|
5
|
-
it("rejects inbound calls with missing caller ID when allowlist enabled", async () => {
|
|
6
|
-
const { manager, provider } = await createManagerHarness({
|
|
7
|
-
inboundPolicy: "allowlist",
|
|
8
|
-
allowFrom: ["+15550001234"],
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
manager.processEvent({
|
|
12
|
-
id: "evt-allowlist-missing",
|
|
13
|
-
type: "call.initiated",
|
|
14
|
-
callId: "call-missing",
|
|
15
|
-
providerCallId: "provider-missing",
|
|
16
|
-
timestamp: Date.now(),
|
|
17
|
-
direction: "inbound",
|
|
18
|
-
to: "+15550000000",
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
expect(manager.getCallByProviderCallId("provider-missing")).toBeUndefined();
|
|
22
|
-
expect(provider.hangupCalls).toEqual([
|
|
23
|
-
expect.objectContaining({ providerCallId: "provider-missing" }),
|
|
24
|
-
]);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("rejects inbound calls with anonymous caller ID when allowlist enabled", async () => {
|
|
28
|
-
const { manager, provider } = await createManagerHarness({
|
|
29
|
-
inboundPolicy: "allowlist",
|
|
30
|
-
allowFrom: ["+15550001234"],
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
manager.processEvent({
|
|
34
|
-
id: "evt-allowlist-anon",
|
|
35
|
-
type: "call.initiated",
|
|
36
|
-
callId: "call-anon",
|
|
37
|
-
providerCallId: "provider-anon",
|
|
38
|
-
timestamp: Date.now(),
|
|
39
|
-
direction: "inbound",
|
|
40
|
-
from: "anonymous",
|
|
41
|
-
to: "+15550000000",
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
expect(manager.getCallByProviderCallId("provider-anon")).toBeUndefined();
|
|
45
|
-
expect(provider.hangupCalls).toEqual([
|
|
46
|
-
expect.objectContaining({ providerCallId: "provider-anon" }),
|
|
47
|
-
]);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("rejects inbound calls that only match allowlist suffixes", async () => {
|
|
51
|
-
const { manager, provider } = await createManagerHarness({
|
|
52
|
-
inboundPolicy: "allowlist",
|
|
53
|
-
allowFrom: ["+15550001234"],
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
manager.processEvent({
|
|
57
|
-
id: "evt-allowlist-suffix",
|
|
58
|
-
type: "call.initiated",
|
|
59
|
-
callId: "call-suffix",
|
|
60
|
-
providerCallId: "provider-suffix",
|
|
61
|
-
timestamp: Date.now(),
|
|
62
|
-
direction: "inbound",
|
|
63
|
-
from: "+99915550001234",
|
|
64
|
-
to: "+15550000000",
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
expect(manager.getCallByProviderCallId("provider-suffix")).toBeUndefined();
|
|
68
|
-
expect(provider.hangupCalls).toEqual([
|
|
69
|
-
expect.objectContaining({ providerCallId: "provider-suffix" }),
|
|
70
|
-
]);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("rejects duplicate inbound events with a single hangup call", async () => {
|
|
74
|
-
const { manager, provider } = await createManagerHarness({
|
|
75
|
-
inboundPolicy: "disabled",
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
manager.processEvent({
|
|
79
|
-
id: "evt-reject-init",
|
|
80
|
-
type: "call.initiated",
|
|
81
|
-
callId: "provider-dup",
|
|
82
|
-
providerCallId: "provider-dup",
|
|
83
|
-
timestamp: Date.now(),
|
|
84
|
-
direction: "inbound",
|
|
85
|
-
from: "+15552222222",
|
|
86
|
-
to: "+15550000000",
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
manager.processEvent({
|
|
90
|
-
id: "evt-reject-ring",
|
|
91
|
-
type: "call.ringing",
|
|
92
|
-
callId: "provider-dup",
|
|
93
|
-
providerCallId: "provider-dup",
|
|
94
|
-
timestamp: Date.now(),
|
|
95
|
-
direction: "inbound",
|
|
96
|
-
from: "+15552222222",
|
|
97
|
-
to: "+15550000000",
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
expect(manager.getCallByProviderCallId("provider-dup")).toBeUndefined();
|
|
101
|
-
expect(provider.hangupCalls).toEqual([
|
|
102
|
-
expect.objectContaining({ providerCallId: "provider-dup" }),
|
|
103
|
-
]);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("retries rejected inbound hangup after a transient provider failure", async () => {
|
|
107
|
-
class FlakyHangupProvider extends FakeProvider {
|
|
108
|
-
hangupFailuresRemaining = 1;
|
|
109
|
-
|
|
110
|
-
override async hangupCall(input: Parameters<FakeProvider["hangupCall"]>[0]): Promise<void> {
|
|
111
|
-
this.hangupCalls.push(input);
|
|
112
|
-
if (this.hangupFailuresRemaining > 0) {
|
|
113
|
-
this.hangupFailuresRemaining -= 1;
|
|
114
|
-
throw new Error("provider down");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const provider = new FlakyHangupProvider();
|
|
120
|
-
const { manager } = await createManagerHarness(
|
|
121
|
-
{
|
|
122
|
-
inboundPolicy: "disabled",
|
|
123
|
-
},
|
|
124
|
-
provider,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
manager.processEvent({
|
|
128
|
-
id: "evt-reject-fail-init",
|
|
129
|
-
type: "call.initiated",
|
|
130
|
-
callId: "provider-flaky",
|
|
131
|
-
providerCallId: "provider-flaky",
|
|
132
|
-
timestamp: Date.now(),
|
|
133
|
-
direction: "inbound",
|
|
134
|
-
from: "+15553333333",
|
|
135
|
-
to: "+15550000000",
|
|
136
|
-
});
|
|
137
|
-
await Promise.resolve();
|
|
138
|
-
|
|
139
|
-
manager.processEvent({
|
|
140
|
-
id: "evt-reject-fail-ring",
|
|
141
|
-
type: "call.ringing",
|
|
142
|
-
callId: "provider-flaky",
|
|
143
|
-
providerCallId: "provider-flaky",
|
|
144
|
-
timestamp: Date.now(),
|
|
145
|
-
direction: "inbound",
|
|
146
|
-
from: "+15553333333",
|
|
147
|
-
to: "+15550000000",
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
expect(manager.getCallByProviderCallId("provider-flaky")).toBeUndefined();
|
|
151
|
-
expect(provider.hangupCalls).toEqual([
|
|
152
|
-
expect.objectContaining({ providerCallId: "provider-flaky" }),
|
|
153
|
-
expect.objectContaining({ providerCallId: "provider-flaky" }),
|
|
154
|
-
]);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("accepts inbound calls that exactly match the allowlist", async () => {
|
|
158
|
-
const { manager } = await createManagerHarness({
|
|
159
|
-
inboundPolicy: "allowlist",
|
|
160
|
-
allowFrom: ["+15550001234"],
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
manager.processEvent({
|
|
164
|
-
id: "evt-allowlist-exact",
|
|
165
|
-
type: "call.initiated",
|
|
166
|
-
callId: "call-exact",
|
|
167
|
-
providerCallId: "provider-exact",
|
|
168
|
-
timestamp: Date.now(),
|
|
169
|
-
direction: "inbound",
|
|
170
|
-
from: "+15550001234",
|
|
171
|
-
to: "+15550000000",
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
const call = manager.getCallByProviderCallId("provider-exact");
|
|
175
|
-
if (!call) {
|
|
176
|
-
throw new Error("expected exact allowlist match to keep the inbound call");
|
|
177
|
-
}
|
|
178
|
-
expect(call).toMatchObject({
|
|
179
|
-
providerCallId: "provider-exact",
|
|
180
|
-
direction: "inbound",
|
|
181
|
-
from: "+15550001234",
|
|
182
|
-
to: "+15550000000",
|
|
183
|
-
});
|
|
184
|
-
expect(call.callId).toMatch(
|
|
185
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
186
|
-
);
|
|
187
|
-
});
|
|
188
|
-
});
|