@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.
- package/package.json +18 -6
- package/api.ts +0 -16
- package/cli-metadata.ts +0 -10
- package/config-api.ts +0 -12
- package/index.test.ts +0 -1075
- package/index.ts +0 -863
- 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.test.ts +0 -12
- package/src/cli.ts +0 -866
- package/src/config-compat.test.ts +0 -130
- package/src/config-compat.ts +0 -227
- package/src/config.test.ts +0 -542
- package/src/config.ts +0 -883
- 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 -50
- package/src/manager/events.test.ts +0 -578
- package/src/manager/events.ts +0 -332
- 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 -629
- package/src/manager/outbound.ts +0 -508
- package/src/manager/state.ts +0 -48
- package/src/manager/store.ts +0 -107
- package/src/manager/timers.test.ts +0 -127
- 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 -259
- package/src/manager.inbound-allowlist.test.ts +0 -183
- package/src/manager.notify.test.ts +0 -390
- package/src/manager.restore.test.ts +0 -310
- package/src/manager.test-harness.ts +0 -127
- package/src/manager.ts +0 -441
- package/src/media-stream.test.ts +0 -953
- package/src/media-stream.ts +0 -876
- package/src/providers/base.ts +0 -99
- package/src/providers/mock.test.ts +0 -86
- 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 -127
- package/src/providers/shared/guarded-json-api.ts +0 -49
- package/src/providers/telnyx.test.ts +0 -489
- package/src/providers/telnyx.ts +0 -419
- package/src/providers/twilio/api.test.ts +0 -184
- package/src/providers/twilio/api.ts +0 -100
- 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 -607
- package/src/providers/twilio.ts +0 -861
- package/src/providers/twilio.types.ts +0 -17
- package/src/realtime-agent-context.test.ts +0 -101
- package/src/realtime-agent-context.ts +0 -149
- package/src/realtime-defaults.ts +0 -3
- package/src/realtime-fast-context.test.ts +0 -74
- package/src/realtime-fast-context.ts +0 -27
- package/src/realtime-transcription.runtime.ts +0 -4
- package/src/realtime-voice.runtime.ts +0 -5
- package/src/response-generator.test.ts +0 -385
- package/src/response-generator.ts +0 -348
- package/src/response-model.test.ts +0 -71
- package/src/response-model.ts +0 -23
- package/src/runtime.test.ts +0 -625
- package/src/runtime.ts +0 -528
- 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 -82
- package/src/tts-provider-voice.test.ts +0 -34
- package/src/tts-provider-voice.ts +0 -21
- package/src/tunnel.test.ts +0 -173
- package/src/tunnel.ts +0 -314
- package/src/types.ts +0 -311
- package/src/utils.test.ts +0 -17
- package/src/utils.ts +0 -14
- package/src/voice-mapping.test.ts +0 -32
- package/src/voice-mapping.ts +0 -65
- package/src/webhook/realtime-audio-pacer.test.ts +0 -146
- package/src/webhook/realtime-audio-pacer.ts +0 -204
- package/src/webhook/realtime-handler.test.ts +0 -1450
- package/src/webhook/realtime-handler.ts +0 -1382
- package/src/webhook/stale-call-reaper.test.ts +0 -89
- package/src/webhook/stale-call-reaper.ts +0 -38
- package/src/webhook/stream-frame-adapter.test.ts +0 -187
- package/src/webhook/stream-frame-adapter.ts +0 -219
- package/src/webhook/tailscale.test.ts +0 -216
- 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 -813
- package/src/webhook-security.ts +0 -982
- package/src/webhook.hangup-once.lifecycle.test.ts +0 -179
- package/src/webhook.test.ts +0 -1615
- package/src/webhook.ts +0 -933
- package/src/webhook.types.ts +0 -5
- package/src/websocket-test-support.ts +0 -72
- package/tsconfig.json +0 -16
|
@@ -1,127 +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
|
-
const timeoutError = await timedOut;
|
|
105
|
-
expect(timeoutError).toBeInstanceOf(Error);
|
|
106
|
-
expect((timeoutError as Error).message).toBe("Timed out waiting for transcript after 1000ms");
|
|
107
|
-
|
|
108
|
-
const toClear = waitForFinalTranscript(ctx as never, "call-4");
|
|
109
|
-
clearTranscriptWaiter(ctx as never, "call-4");
|
|
110
|
-
expect(ctx.transcriptWaiters.has("call-4")).toBe(false);
|
|
111
|
-
void toClear.catch(() => {});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("rejects duplicate transcript waiters for the same call", async () => {
|
|
115
|
-
const ctx = {
|
|
116
|
-
transcriptWaiters: new Map(),
|
|
117
|
-
config: { transcriptTimeoutMs: 1_000 },
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const pending = waitForFinalTranscript(ctx as never, "call-1");
|
|
121
|
-
await expect(waitForFinalTranscript(ctx as never, "call-1")).rejects.toThrow(
|
|
122
|
-
"Already waiting for transcript",
|
|
123
|
-
);
|
|
124
|
-
rejectTranscriptWaiter(ctx as never, "call-1", "done");
|
|
125
|
-
await expect(pending).rejects.toThrow("done");
|
|
126
|
-
});
|
|
127
|
-
});
|
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,259 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } 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
|
-
function expectTranscriptWaiter(
|
|
24
|
-
manager: Awaited<ReturnType<typeof createManagerHarness>>["manager"],
|
|
25
|
-
callId: string,
|
|
26
|
-
) {
|
|
27
|
-
const waiters = (
|
|
28
|
-
manager as unknown as {
|
|
29
|
-
transcriptWaiters: Map<string, unknown>;
|
|
30
|
-
}
|
|
31
|
-
).transcriptWaiters;
|
|
32
|
-
expect(waiters.has(callId)).toBe(true);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
describe("CallManager closed-loop turns", () => {
|
|
36
|
-
it("completes a closed-loop turn without live audio", async () => {
|
|
37
|
-
const { manager, provider } = await createManagerHarness({
|
|
38
|
-
transcriptTimeoutMs: 5000,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const started = await manager.initiateCall("+15550000003");
|
|
42
|
-
expect(started.success).toBe(true);
|
|
43
|
-
|
|
44
|
-
markCallAnswered(manager, started.callId, "evt-closed-loop-answered");
|
|
45
|
-
|
|
46
|
-
const turnPromise = manager.continueCall(started.callId, "How can I help?");
|
|
47
|
-
await vi.waitFor(() => {
|
|
48
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
49
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
manager.processEvent({
|
|
53
|
-
id: "evt-closed-loop-speech",
|
|
54
|
-
type: "call.speech",
|
|
55
|
-
callId: started.callId,
|
|
56
|
-
providerCallId: "request-uuid",
|
|
57
|
-
timestamp: Date.now(),
|
|
58
|
-
transcript: "Please check status",
|
|
59
|
-
isFinal: true,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const turn = await turnPromise;
|
|
63
|
-
expect(turn.success).toBe(true);
|
|
64
|
-
expect(turn.transcript).toBe("Please check status");
|
|
65
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
66
|
-
expect(provider.stopListeningCalls).toHaveLength(1);
|
|
67
|
-
|
|
68
|
-
const call = requireCall(manager, started.callId);
|
|
69
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual([
|
|
70
|
-
"How can I help?",
|
|
71
|
-
"Please check status",
|
|
72
|
-
]);
|
|
73
|
-
const metadata = call.metadata ?? {};
|
|
74
|
-
expect(typeof metadata.lastTurnLatencyMs).toBe("number");
|
|
75
|
-
expect(typeof metadata.lastTurnListenWaitMs).toBe("number");
|
|
76
|
-
expect(metadata.turnCount).toBe(1);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("rejects overlapping continueCall requests for the same call", async () => {
|
|
80
|
-
const { manager, provider } = await createManagerHarness({
|
|
81
|
-
transcriptTimeoutMs: 5000,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const started = await manager.initiateCall("+15550000004");
|
|
85
|
-
expect(started.success).toBe(true);
|
|
86
|
-
|
|
87
|
-
markCallAnswered(manager, started.callId, "evt-overlap-answered");
|
|
88
|
-
|
|
89
|
-
const first = manager.continueCall(started.callId, "First prompt");
|
|
90
|
-
const second = await manager.continueCall(started.callId, "Second prompt");
|
|
91
|
-
expect(second.success).toBe(false);
|
|
92
|
-
expect(second.error).toBe("Already waiting for transcript");
|
|
93
|
-
|
|
94
|
-
manager.processEvent({
|
|
95
|
-
id: "evt-overlap-speech",
|
|
96
|
-
type: "call.speech",
|
|
97
|
-
callId: started.callId,
|
|
98
|
-
providerCallId: "request-uuid",
|
|
99
|
-
timestamp: Date.now(),
|
|
100
|
-
transcript: "Done",
|
|
101
|
-
isFinal: true,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const firstResult = await first;
|
|
105
|
-
expect(firstResult.success).toBe(true);
|
|
106
|
-
expect(firstResult.transcript).toBe("Done");
|
|
107
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
108
|
-
expect(provider.stopListeningCalls).toHaveLength(1);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it("ignores speech events with mismatched turnToken while waiting for transcript", async () => {
|
|
112
|
-
const { manager, provider } = await createManagerHarness(
|
|
113
|
-
{
|
|
114
|
-
transcriptTimeoutMs: 5000,
|
|
115
|
-
},
|
|
116
|
-
new FakeProvider("twilio"),
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
const started = await manager.initiateCall("+15550000004");
|
|
120
|
-
expect(started.success).toBe(true);
|
|
121
|
-
|
|
122
|
-
markCallAnswered(manager, started.callId, "evt-turn-token-answered");
|
|
123
|
-
|
|
124
|
-
const turnPromise = manager.continueCall(started.callId, "Prompt");
|
|
125
|
-
await vi.waitFor(() => {
|
|
126
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
127
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const expectedTurnToken = requireTurnToken(provider);
|
|
131
|
-
|
|
132
|
-
manager.processEvent({
|
|
133
|
-
id: "evt-turn-token-bad",
|
|
134
|
-
type: "call.speech",
|
|
135
|
-
callId: started.callId,
|
|
136
|
-
providerCallId: "request-uuid",
|
|
137
|
-
timestamp: Date.now(),
|
|
138
|
-
transcript: "stale replay",
|
|
139
|
-
isFinal: true,
|
|
140
|
-
turnToken: "wrong-token",
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
144
|
-
|
|
145
|
-
manager.processEvent({
|
|
146
|
-
id: "evt-turn-token-good",
|
|
147
|
-
type: "call.speech",
|
|
148
|
-
callId: started.callId,
|
|
149
|
-
providerCallId: "request-uuid",
|
|
150
|
-
timestamp: Date.now(),
|
|
151
|
-
transcript: "final answer",
|
|
152
|
-
isFinal: true,
|
|
153
|
-
turnToken: expectedTurnToken,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
const turnResult = await turnPromise;
|
|
157
|
-
expect(turnResult.success).toBe(true);
|
|
158
|
-
expect(turnResult.transcript).toBe("final answer");
|
|
159
|
-
|
|
160
|
-
const call = requireCall(manager, started.callId);
|
|
161
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual(["Prompt", "final answer"]);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it("tracks latency metadata across multiple closed-loop turns", async () => {
|
|
165
|
-
const { manager, provider } = await createManagerHarness({
|
|
166
|
-
transcriptTimeoutMs: 5000,
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const started = await manager.initiateCall("+15550000005");
|
|
170
|
-
expect(started.success).toBe(true);
|
|
171
|
-
|
|
172
|
-
markCallAnswered(manager, started.callId, "evt-multi-answered");
|
|
173
|
-
|
|
174
|
-
const firstTurn = manager.continueCall(started.callId, "First question");
|
|
175
|
-
await vi.waitFor(() => {
|
|
176
|
-
expect(provider.startListeningCalls).toHaveLength(1);
|
|
177
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
178
|
-
});
|
|
179
|
-
manager.processEvent({
|
|
180
|
-
id: "evt-multi-speech-1",
|
|
181
|
-
type: "call.speech",
|
|
182
|
-
callId: started.callId,
|
|
183
|
-
providerCallId: "request-uuid",
|
|
184
|
-
timestamp: Date.now(),
|
|
185
|
-
transcript: "First answer",
|
|
186
|
-
isFinal: true,
|
|
187
|
-
});
|
|
188
|
-
await firstTurn;
|
|
189
|
-
|
|
190
|
-
const secondTurn = manager.continueCall(started.callId, "Second question");
|
|
191
|
-
await vi.waitFor(() => {
|
|
192
|
-
expect(provider.startListeningCalls).toHaveLength(2);
|
|
193
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
194
|
-
});
|
|
195
|
-
manager.processEvent({
|
|
196
|
-
id: "evt-multi-speech-2",
|
|
197
|
-
type: "call.speech",
|
|
198
|
-
callId: started.callId,
|
|
199
|
-
providerCallId: "request-uuid",
|
|
200
|
-
timestamp: Date.now(),
|
|
201
|
-
transcript: "Second answer",
|
|
202
|
-
isFinal: true,
|
|
203
|
-
});
|
|
204
|
-
const secondResult = await secondTurn;
|
|
205
|
-
|
|
206
|
-
expect(secondResult.success).toBe(true);
|
|
207
|
-
|
|
208
|
-
const call = requireCall(manager, started.callId);
|
|
209
|
-
expect(call.transcript.map((entry) => entry.text)).toEqual([
|
|
210
|
-
"First question",
|
|
211
|
-
"First answer",
|
|
212
|
-
"Second question",
|
|
213
|
-
"Second answer",
|
|
214
|
-
]);
|
|
215
|
-
const metadata = call.metadata ?? {};
|
|
216
|
-
expect(metadata.turnCount).toBe(2);
|
|
217
|
-
expect(typeof metadata.lastTurnLatencyMs).toBe("number");
|
|
218
|
-
expect(typeof metadata.lastTurnListenWaitMs).toBe("number");
|
|
219
|
-
expect(provider.startListeningCalls).toHaveLength(2);
|
|
220
|
-
expect(provider.stopListeningCalls).toHaveLength(2);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it("handles repeated closed-loop turns without waiter churn", async () => {
|
|
224
|
-
const { manager, provider } = await createManagerHarness({
|
|
225
|
-
transcriptTimeoutMs: 5000,
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
const started = await manager.initiateCall("+15550000006");
|
|
229
|
-
expect(started.success).toBe(true);
|
|
230
|
-
|
|
231
|
-
markCallAnswered(manager, started.callId, "evt-loop-answered");
|
|
232
|
-
|
|
233
|
-
for (let i = 1; i <= 5; i++) {
|
|
234
|
-
const turnPromise = manager.continueCall(started.callId, `Prompt ${i}`);
|
|
235
|
-
await vi.waitFor(() => {
|
|
236
|
-
expect(provider.startListeningCalls).toHaveLength(i);
|
|
237
|
-
expectTranscriptWaiter(manager, started.callId);
|
|
238
|
-
});
|
|
239
|
-
manager.processEvent({
|
|
240
|
-
id: `evt-loop-speech-${i}`,
|
|
241
|
-
type: "call.speech",
|
|
242
|
-
callId: started.callId,
|
|
243
|
-
providerCallId: "request-uuid",
|
|
244
|
-
timestamp: Date.now(),
|
|
245
|
-
transcript: `Answer ${i}`,
|
|
246
|
-
isFinal: true,
|
|
247
|
-
});
|
|
248
|
-
const result = await turnPromise;
|
|
249
|
-
expect(result.success).toBe(true);
|
|
250
|
-
expect(result.transcript).toBe(`Answer ${i}`);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const call = requireCall(manager, started.callId);
|
|
254
|
-
const metadata = call.metadata ?? {};
|
|
255
|
-
expect(metadata.turnCount).toBe(5);
|
|
256
|
-
expect(provider.startListeningCalls).toHaveLength(5);
|
|
257
|
-
expect(provider.stopListeningCalls).toHaveLength(5);
|
|
258
|
-
});
|
|
259
|
-
});
|