@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 { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
|
|
4
|
-
fetchWithSsrFGuardMock: vi.fn(),
|
|
5
|
-
}));
|
|
6
|
-
|
|
7
|
-
vi.mock("../../../api.js", () => ({
|
|
8
|
-
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
import { guardedJsonApiRequest } from "./guarded-json-api.js";
|
|
12
|
-
|
|
13
|
-
describe("guardedJsonApiRequest", () => {
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
vi.clearAllMocks();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("uses the SSRF-guarded fetch and parses json responses", async () => {
|
|
19
|
-
const release = vi.fn(async () => {});
|
|
20
|
-
fetchWithSsrFGuardMock.mockResolvedValue({
|
|
21
|
-
response: new Response(JSON.stringify({ ok: true }), { status: 200 }),
|
|
22
|
-
release,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
await expect(
|
|
26
|
-
guardedJsonApiRequest({
|
|
27
|
-
url: "https://api.example.com/v1/calls",
|
|
28
|
-
method: "POST",
|
|
29
|
-
headers: { Authorization: "Bearer token" },
|
|
30
|
-
body: { hello: "world" },
|
|
31
|
-
allowedHostnames: ["api.example.com"],
|
|
32
|
-
auditContext: "voice-call:test",
|
|
33
|
-
errorPrefix: "request failed",
|
|
34
|
-
}),
|
|
35
|
-
).resolves.toEqual({ ok: true });
|
|
36
|
-
|
|
37
|
-
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith({
|
|
38
|
-
url: "https://api.example.com/v1/calls",
|
|
39
|
-
init: {
|
|
40
|
-
method: "POST",
|
|
41
|
-
headers: { Authorization: "Bearer token" },
|
|
42
|
-
body: JSON.stringify({ hello: "world" }),
|
|
43
|
-
},
|
|
44
|
-
policy: { allowedHostnames: ["api.example.com"] },
|
|
45
|
-
auditContext: "voice-call:test",
|
|
46
|
-
});
|
|
47
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("returns undefined for empty bodies and allowed 404s", async () => {
|
|
51
|
-
const release = vi.fn(async () => {});
|
|
52
|
-
fetchWithSsrFGuardMock.mockResolvedValueOnce({
|
|
53
|
-
response: new Response(null, { status: 204 }),
|
|
54
|
-
release,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
await expect(
|
|
58
|
-
guardedJsonApiRequest({
|
|
59
|
-
url: "https://api.example.com/v1/calls/1",
|
|
60
|
-
method: "GET",
|
|
61
|
-
headers: {},
|
|
62
|
-
allowedHostnames: ["api.example.com"],
|
|
63
|
-
auditContext: "voice-call:test",
|
|
64
|
-
errorPrefix: "request failed",
|
|
65
|
-
}),
|
|
66
|
-
).resolves.toBeUndefined();
|
|
67
|
-
|
|
68
|
-
fetchWithSsrFGuardMock.mockResolvedValueOnce({
|
|
69
|
-
response: new Response("missing", { status: 404 }),
|
|
70
|
-
release,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
await expect(
|
|
74
|
-
guardedJsonApiRequest({
|
|
75
|
-
url: "https://api.example.com/v1/calls/2",
|
|
76
|
-
method: "GET",
|
|
77
|
-
headers: {},
|
|
78
|
-
allowNotFound: true,
|
|
79
|
-
allowedHostnames: ["api.example.com"],
|
|
80
|
-
auditContext: "voice-call:test",
|
|
81
|
-
errorPrefix: "request failed",
|
|
82
|
-
}),
|
|
83
|
-
).resolves.toBeUndefined();
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("throws prefixed errors and still releases the response handle", async () => {
|
|
87
|
-
const release = vi.fn(async () => {});
|
|
88
|
-
fetchWithSsrFGuardMock.mockResolvedValue({
|
|
89
|
-
response: new Response("boom", { status: 500 }),
|
|
90
|
-
release,
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
await expect(
|
|
94
|
-
guardedJsonApiRequest({
|
|
95
|
-
url: "https://api.example.com/v1/calls/3",
|
|
96
|
-
method: "DELETE",
|
|
97
|
-
headers: {},
|
|
98
|
-
allowedHostnames: ["api.example.com"],
|
|
99
|
-
auditContext: "voice-call:test",
|
|
100
|
-
errorPrefix: "provider error",
|
|
101
|
-
}),
|
|
102
|
-
).rejects.toThrow("provider error: 500 boom");
|
|
103
|
-
|
|
104
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("throws prefixed errors for malformed json success responses", async () => {
|
|
108
|
-
const release = vi.fn(async () => {});
|
|
109
|
-
fetchWithSsrFGuardMock.mockResolvedValue({
|
|
110
|
-
response: new Response("{not json", { status: 200 }),
|
|
111
|
-
release,
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
await expect(
|
|
115
|
-
guardedJsonApiRequest({
|
|
116
|
-
url: "https://api.example.com/v1/calls/4",
|
|
117
|
-
method: "GET",
|
|
118
|
-
headers: {},
|
|
119
|
-
allowedHostnames: ["api.example.com"],
|
|
120
|
-
auditContext: "voice-call:test",
|
|
121
|
-
errorPrefix: "provider error",
|
|
122
|
-
}),
|
|
123
|
-
).rejects.toThrow("provider error: malformed JSON response");
|
|
124
|
-
|
|
125
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
126
|
-
});
|
|
127
|
-
});
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { fetchWithSsrFGuard } from "../../../api.js";
|
|
2
|
-
|
|
3
|
-
type GuardedJsonApiRequestParams = {
|
|
4
|
-
url: string;
|
|
5
|
-
method: "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
|
|
6
|
-
headers: Record<string, string>;
|
|
7
|
-
body?: Record<string, unknown>;
|
|
8
|
-
allowNotFound?: boolean;
|
|
9
|
-
allowedHostnames: string[];
|
|
10
|
-
auditContext: string;
|
|
11
|
-
errorPrefix: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export async function guardedJsonApiRequest<T = unknown>(
|
|
15
|
-
params: GuardedJsonApiRequestParams,
|
|
16
|
-
): Promise<T> {
|
|
17
|
-
const { response, release } = await fetchWithSsrFGuard({
|
|
18
|
-
url: params.url,
|
|
19
|
-
init: {
|
|
20
|
-
method: params.method,
|
|
21
|
-
headers: params.headers,
|
|
22
|
-
body: params.body ? JSON.stringify(params.body) : undefined,
|
|
23
|
-
},
|
|
24
|
-
policy: { allowedHostnames: params.allowedHostnames },
|
|
25
|
-
auditContext: params.auditContext,
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
if (params.allowNotFound && response.status === 404) {
|
|
31
|
-
return undefined as T;
|
|
32
|
-
}
|
|
33
|
-
const errorText = await response.text();
|
|
34
|
-
throw new Error(`${params.errorPrefix}: ${response.status} ${errorText}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const text = await response.text();
|
|
38
|
-
if (!text) {
|
|
39
|
-
return undefined as T;
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(text) as T;
|
|
43
|
-
} catch {
|
|
44
|
-
throw new Error(`${params.errorPrefix}: malformed JSON response`);
|
|
45
|
-
}
|
|
46
|
-
} finally {
|
|
47
|
-
await release();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,489 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import type { WebhookContext } from "../types.js";
|
|
4
|
-
import { TelnyxProvider } from "./telnyx.js";
|
|
5
|
-
|
|
6
|
-
const apiMocks = vi.hoisted(() => ({
|
|
7
|
-
fetchWithSsrFGuard: vi.fn(),
|
|
8
|
-
}));
|
|
9
|
-
|
|
10
|
-
vi.mock("../../api.js", () => ({
|
|
11
|
-
fetchWithSsrFGuard: apiMocks.fetchWithSsrFGuard,
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
apiMocks.fetchWithSsrFGuard.mockReset();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
function createCtx(params?: Partial<WebhookContext>): WebhookContext {
|
|
19
|
-
return {
|
|
20
|
-
headers: {},
|
|
21
|
-
rawBody: "{}",
|
|
22
|
-
url: "http://localhost/voice/webhook",
|
|
23
|
-
method: "POST",
|
|
24
|
-
query: {},
|
|
25
|
-
remoteAddress: "127.0.0.1",
|
|
26
|
-
...params,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function requireFetchRequest() {
|
|
31
|
-
const [call] = apiMocks.fetchWithSsrFGuard.mock.calls;
|
|
32
|
-
if (!call) {
|
|
33
|
-
throw new Error("expected Telnyx provider to call fetchWithSsrFGuard");
|
|
34
|
-
}
|
|
35
|
-
const [request] = call;
|
|
36
|
-
if (!request || typeof request !== "object" || Array.isArray(request)) {
|
|
37
|
-
throw new Error("expected Telnyx provider to call fetchWithSsrFGuard");
|
|
38
|
-
}
|
|
39
|
-
return request as {
|
|
40
|
-
url?: string;
|
|
41
|
-
auditContext?: string;
|
|
42
|
-
policy?: unknown;
|
|
43
|
-
init?: {
|
|
44
|
-
method?: string;
|
|
45
|
-
body?: unknown;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function decodeBase64Url(input: string): Buffer {
|
|
51
|
-
const normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
52
|
-
const padLen = (4 - (normalized.length % 4)) % 4;
|
|
53
|
-
const padded = normalized + "=".repeat(padLen);
|
|
54
|
-
return Buffer.from(padded, "base64");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function createSignedTelnyxCtx(params: {
|
|
58
|
-
privateKey: crypto.KeyObject;
|
|
59
|
-
rawBody: string;
|
|
60
|
-
}): WebhookContext {
|
|
61
|
-
const timestamp = String(Math.floor(Date.now() / 1000));
|
|
62
|
-
const signedPayload = `${timestamp}|${params.rawBody}`;
|
|
63
|
-
const signature = crypto
|
|
64
|
-
.sign(null, Buffer.from(signedPayload), params.privateKey)
|
|
65
|
-
.toString("base64");
|
|
66
|
-
|
|
67
|
-
return createCtx({
|
|
68
|
-
rawBody: params.rawBody,
|
|
69
|
-
headers: {
|
|
70
|
-
"telnyx-signature-ed25519": signature,
|
|
71
|
-
"telnyx-timestamp": timestamp,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function expectReplayVerification(
|
|
77
|
-
results: Array<{ ok: boolean; isReplay?: boolean; verifiedRequestKey?: string }>,
|
|
78
|
-
) {
|
|
79
|
-
expect(results.map((result) => result.ok)).toEqual([true, true]);
|
|
80
|
-
expect(results.map((result) => Boolean(result.isReplay))).toEqual([false, true]);
|
|
81
|
-
const firstResult = results[0];
|
|
82
|
-
if (!firstResult?.verifiedRequestKey) {
|
|
83
|
-
throw new Error("expected Telnyx verification to produce a request key");
|
|
84
|
-
}
|
|
85
|
-
const secondResult = results[1];
|
|
86
|
-
if (!secondResult?.verifiedRequestKey) {
|
|
87
|
-
throw new Error("expected replayed Telnyx verification to preserve the request key");
|
|
88
|
-
}
|
|
89
|
-
const firstKey = firstResult.verifiedRequestKey;
|
|
90
|
-
const secondKey = secondResult.verifiedRequestKey;
|
|
91
|
-
expect(firstKey.length).toBeGreaterThan(0);
|
|
92
|
-
expect(secondKey).toBe(firstKey);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function requireJwkX(jwk: JsonWebKey) {
|
|
96
|
-
if (typeof jwk.x !== "string" || jwk.x.length === 0) {
|
|
97
|
-
throw new Error("expected Ed25519 JWK export to expose x");
|
|
98
|
-
}
|
|
99
|
-
return jwk.x;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function expectWebhookVerificationSucceeds(params: {
|
|
103
|
-
publicKey: string;
|
|
104
|
-
privateKey: crypto.KeyObject;
|
|
105
|
-
}) {
|
|
106
|
-
const provider = new TelnyxProvider(
|
|
107
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: params.publicKey },
|
|
108
|
-
{ skipVerification: false },
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const rawBody = JSON.stringify({
|
|
112
|
-
event_type: "call.initiated",
|
|
113
|
-
payload: { call_control_id: "x" },
|
|
114
|
-
});
|
|
115
|
-
const result = provider.verifyWebhook(
|
|
116
|
-
createSignedTelnyxCtx({ privateKey: params.privateKey, rawBody }),
|
|
117
|
-
);
|
|
118
|
-
expect(result.ok).toBe(true);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
describe("TelnyxProvider.verifyWebhook", () => {
|
|
122
|
-
it("fails closed when public key is missing and skipVerification is false", () => {
|
|
123
|
-
const provider = new TelnyxProvider(
|
|
124
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: undefined },
|
|
125
|
-
{ skipVerification: false },
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
const result = provider.verifyWebhook(createCtx());
|
|
129
|
-
expect(result.ok).toBe(false);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it("allows requests when skipVerification is true (development only)", () => {
|
|
133
|
-
const provider = new TelnyxProvider(
|
|
134
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: undefined },
|
|
135
|
-
{ skipVerification: true },
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
const result = provider.verifyWebhook(createCtx());
|
|
139
|
-
expect(result.ok).toBe(true);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it("fails when signature headers are missing (with public key configured)", () => {
|
|
143
|
-
const provider = new TelnyxProvider(
|
|
144
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: "public-key" },
|
|
145
|
-
{ skipVerification: false },
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
const result = provider.verifyWebhook(createCtx({ headers: {} }));
|
|
149
|
-
expect(result.ok).toBe(false);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it("verifies a valid signature with a raw Ed25519 public key (Base64)", () => {
|
|
153
|
-
const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
|
|
154
|
-
|
|
155
|
-
const jwk = publicKey.export({ format: "jwk" }) as JsonWebKey;
|
|
156
|
-
expect(jwk.kty).toBe("OKP");
|
|
157
|
-
expect(jwk.crv).toBe("Ed25519");
|
|
158
|
-
|
|
159
|
-
const rawPublicKey = decodeBase64Url(requireJwkX(jwk));
|
|
160
|
-
const rawPublicKeyBase64 = rawPublicKey.toString("base64");
|
|
161
|
-
expectWebhookVerificationSucceeds({ publicKey: rawPublicKeyBase64, privateKey });
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it("verifies a valid signature with a DER SPKI public key (Base64)", () => {
|
|
165
|
-
const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
|
|
166
|
-
const spkiDer = publicKey.export({ format: "der", type: "spki" }) as Buffer;
|
|
167
|
-
const spkiDerBase64 = spkiDer.toString("base64");
|
|
168
|
-
expectWebhookVerificationSucceeds({ publicKey: spkiDerBase64, privateKey });
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it("returns replay status when the same signed request is seen twice", () => {
|
|
172
|
-
const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
|
|
173
|
-
const spkiDer = publicKey.export({ format: "der", type: "spki" }) as Buffer;
|
|
174
|
-
const provider = new TelnyxProvider(
|
|
175
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: spkiDer.toString("base64") },
|
|
176
|
-
{ skipVerification: false },
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
const rawBody = JSON.stringify({
|
|
180
|
-
event_type: "call.initiated",
|
|
181
|
-
payload: { call_control_id: "call-replay-test" },
|
|
182
|
-
nonce: crypto.randomUUID(),
|
|
183
|
-
});
|
|
184
|
-
const ctx = createSignedTelnyxCtx({ privateKey, rawBody });
|
|
185
|
-
|
|
186
|
-
const first = provider.verifyWebhook(ctx);
|
|
187
|
-
const second = provider.verifyWebhook(ctx);
|
|
188
|
-
|
|
189
|
-
expectReplayVerification([first, second]);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe("TelnyxProvider.parseWebhookEvent", () => {
|
|
194
|
-
it("uses verified request key for manager dedupe", () => {
|
|
195
|
-
const provider = new TelnyxProvider({
|
|
196
|
-
apiKey: "KEY123",
|
|
197
|
-
connectionId: "CONN456",
|
|
198
|
-
publicKey: undefined,
|
|
199
|
-
});
|
|
200
|
-
const result = provider.parseWebhookEvent(
|
|
201
|
-
createCtx({
|
|
202
|
-
rawBody: JSON.stringify({
|
|
203
|
-
data: {
|
|
204
|
-
id: "evt-123",
|
|
205
|
-
event_type: "call.initiated",
|
|
206
|
-
payload: { call_control_id: "call-1" },
|
|
207
|
-
},
|
|
208
|
-
}),
|
|
209
|
-
}),
|
|
210
|
-
{ verifiedRequestKey: "telnyx:req:abc" },
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
expect(result.events).toHaveLength(1);
|
|
214
|
-
const event = result.events[0];
|
|
215
|
-
if (!event) {
|
|
216
|
-
throw new Error("expected Telnyx parseWebhookEvent to produce one event");
|
|
217
|
-
}
|
|
218
|
-
expect(event.dedupeKey).toBe("telnyx:req:abc");
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("maps call direction and phone numbers from Call Control callbacks", () => {
|
|
222
|
-
const provider = new TelnyxProvider({
|
|
223
|
-
apiKey: "KEY123",
|
|
224
|
-
connectionId: "CONN456",
|
|
225
|
-
publicKey: undefined,
|
|
226
|
-
});
|
|
227
|
-
const result = provider.parseWebhookEvent(
|
|
228
|
-
createCtx({
|
|
229
|
-
rawBody: JSON.stringify({
|
|
230
|
-
data: {
|
|
231
|
-
id: "evt-inbound",
|
|
232
|
-
event_type: "call.initiated",
|
|
233
|
-
payload: {
|
|
234
|
-
call_control_id: "call-1",
|
|
235
|
-
direction: "incoming",
|
|
236
|
-
from: "+15551111111",
|
|
237
|
-
to: "+15550000000",
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
}),
|
|
241
|
-
}),
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
expect(result.events).toHaveLength(1);
|
|
245
|
-
const event = result.events[0];
|
|
246
|
-
expect(event?.type).toBe("call.initiated");
|
|
247
|
-
expect(event?.direction).toBe("inbound");
|
|
248
|
-
expect(event?.from).toBe("+15551111111");
|
|
249
|
-
expect(event?.to).toBe("+15550000000");
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it("uses raw client_state fallback when client_state is malformed base64", () => {
|
|
253
|
-
const provider = new TelnyxProvider({
|
|
254
|
-
apiKey: "KEY123",
|
|
255
|
-
connectionId: "CONN456",
|
|
256
|
-
publicKey: undefined,
|
|
257
|
-
});
|
|
258
|
-
const result = provider.parseWebhookEvent(
|
|
259
|
-
createCtx({
|
|
260
|
-
rawBody: JSON.stringify({
|
|
261
|
-
data: {
|
|
262
|
-
id: "evt-client-state",
|
|
263
|
-
event_type: "call.initiated",
|
|
264
|
-
payload: {
|
|
265
|
-
call_control_id: "call-fallback",
|
|
266
|
-
client_state: "call-1@@@",
|
|
267
|
-
},
|
|
268
|
-
},
|
|
269
|
-
}),
|
|
270
|
-
}),
|
|
271
|
-
);
|
|
272
|
-
|
|
273
|
-
expect(result.events).toHaveLength(1);
|
|
274
|
-
expect(result.events[0]?.callId).toBe("call-1@@@");
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("reads transcription text from Telnyx transcription_data payloads", () => {
|
|
278
|
-
const provider = new TelnyxProvider({
|
|
279
|
-
apiKey: "KEY123",
|
|
280
|
-
connectionId: "CONN456",
|
|
281
|
-
publicKey: undefined,
|
|
282
|
-
});
|
|
283
|
-
const result = provider.parseWebhookEvent(
|
|
284
|
-
createCtx({
|
|
285
|
-
rawBody: JSON.stringify({
|
|
286
|
-
data: {
|
|
287
|
-
id: "evt-transcription",
|
|
288
|
-
event_type: "call.transcription",
|
|
289
|
-
payload: {
|
|
290
|
-
call_control_id: "call-1",
|
|
291
|
-
transcription_data: {
|
|
292
|
-
transcript: "hello this is a test speech",
|
|
293
|
-
is_final: false,
|
|
294
|
-
confidence: 0.977219,
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
},
|
|
298
|
-
}),
|
|
299
|
-
}),
|
|
300
|
-
);
|
|
301
|
-
|
|
302
|
-
expect(result.events).toHaveLength(1);
|
|
303
|
-
const event = result.events[0];
|
|
304
|
-
expect(event?.type).toBe("call.speech");
|
|
305
|
-
if (event?.type !== "call.speech") {
|
|
306
|
-
throw new Error("expected Telnyx transcription callback to produce a speech event");
|
|
307
|
-
}
|
|
308
|
-
expect(event?.transcript).toBe("hello this is a test speech");
|
|
309
|
-
expect(event?.isFinal).toBe(false);
|
|
310
|
-
expect(event?.confidence).toBe(0.977219);
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
describe("TelnyxProvider answer control", () => {
|
|
315
|
-
it("answers inbound call-control legs with a deterministic command id", async () => {
|
|
316
|
-
const release = vi.fn(async () => {});
|
|
317
|
-
apiMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
318
|
-
response: new Response(JSON.stringify({ data: {} }), { status: 200 }),
|
|
319
|
-
release,
|
|
320
|
-
});
|
|
321
|
-
const provider = new TelnyxProvider({
|
|
322
|
-
apiKey: "KEY123",
|
|
323
|
-
connectionId: "CONN456",
|
|
324
|
-
publicKey: undefined,
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
await provider.answerCall({
|
|
328
|
-
callId: "call-1",
|
|
329
|
-
providerCallId: "call-control-1",
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
expect(apiMocks.fetchWithSsrFGuard).toHaveBeenCalledTimes(1);
|
|
333
|
-
const request = requireFetchRequest();
|
|
334
|
-
expect(request.url).toBe("https://api.telnyx.com/v2/calls/call-control-1/actions/answer");
|
|
335
|
-
expect(request.auditContext).toBe("voice-call.telnyx.api");
|
|
336
|
-
expect(request.policy).toEqual({ allowedHostnames: ["api.telnyx.com"] });
|
|
337
|
-
expect(request.init?.method).toBe("POST");
|
|
338
|
-
expect(request.init?.body).toBe(JSON.stringify({ command_id: "klaw-answer-call-1" }));
|
|
339
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
describe("TelnyxProvider Media Streaming (PCMU)", () => {
|
|
344
|
-
it("embeds streaming fields in the dial payload when streamUrl is provided", async () => {
|
|
345
|
-
const release = vi.fn(async () => {});
|
|
346
|
-
apiMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
347
|
-
response: new Response(JSON.stringify({ data: { call_control_id: "call-control-1" } }), {
|
|
348
|
-
status: 200,
|
|
349
|
-
}),
|
|
350
|
-
release,
|
|
351
|
-
});
|
|
352
|
-
const provider = new TelnyxProvider({
|
|
353
|
-
apiKey: "KEY123",
|
|
354
|
-
connectionId: "CONN456",
|
|
355
|
-
publicKey: undefined,
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
await provider.initiateCall({
|
|
359
|
-
callId: "call-1",
|
|
360
|
-
from: "+15550000001",
|
|
361
|
-
to: "+15550000002",
|
|
362
|
-
webhookUrl: "https://example.test/voice/webhook",
|
|
363
|
-
streamUrl: "wss://example.test/voice/stream/realtime/token-xyz",
|
|
364
|
-
streamAuthToken: "token-xyz",
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
const request = requireFetchRequest();
|
|
368
|
-
const body = JSON.parse(request.init?.body as string) as Record<string, unknown>;
|
|
369
|
-
expect(body.stream_url).toBe("wss://example.test/voice/stream/realtime/token-xyz");
|
|
370
|
-
expect(body.stream_track).toBe("inbound_track");
|
|
371
|
-
expect(body.stream_codec).toBe("PCMU");
|
|
372
|
-
expect(body.stream_bidirectional_mode).toBe("rtp");
|
|
373
|
-
expect(body.stream_bidirectional_codec).toBe("PCMU");
|
|
374
|
-
expect(body.stream_bidirectional_sampling_rate).toBe(8000);
|
|
375
|
-
expect(body.stream_bidirectional_target_legs).toBe("self");
|
|
376
|
-
expect(body.stream_auth_token).toBe("token-xyz");
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it("omits streaming fields from the dial payload when streamUrl is absent", async () => {
|
|
380
|
-
apiMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
381
|
-
response: new Response(JSON.stringify({ data: { call_control_id: "call-control-1" } }), {
|
|
382
|
-
status: 200,
|
|
383
|
-
}),
|
|
384
|
-
release: vi.fn(async () => {}),
|
|
385
|
-
});
|
|
386
|
-
const provider = new TelnyxProvider({
|
|
387
|
-
apiKey: "KEY123",
|
|
388
|
-
connectionId: "CONN456",
|
|
389
|
-
publicKey: undefined,
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
await provider.initiateCall({
|
|
393
|
-
callId: "call-1",
|
|
394
|
-
from: "+15550000001",
|
|
395
|
-
to: "+15550000002",
|
|
396
|
-
webhookUrl: "https://example.test/voice/webhook",
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
const body = JSON.parse(requireFetchRequest().init?.body as string) as Record<string, unknown>;
|
|
400
|
-
expect(body.stream_url).toBeUndefined();
|
|
401
|
-
expect(body.stream_codec).toBeUndefined();
|
|
402
|
-
expect(body.stream_bidirectional_codec).toBeUndefined();
|
|
403
|
-
expect(body.stream_auth_token).toBeUndefined();
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it("embeds streaming fields in the answer action when streamUrl is provided", async () => {
|
|
407
|
-
apiMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
408
|
-
response: new Response(JSON.stringify({ data: {} }), { status: 200 }),
|
|
409
|
-
release: vi.fn(async () => {}),
|
|
410
|
-
});
|
|
411
|
-
const provider = new TelnyxProvider({
|
|
412
|
-
apiKey: "KEY123",
|
|
413
|
-
connectionId: "CONN456",
|
|
414
|
-
publicKey: undefined,
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
await provider.answerCall({
|
|
418
|
-
callId: "call-1",
|
|
419
|
-
providerCallId: "call-control-1",
|
|
420
|
-
streamUrl: "wss://example.test/voice/stream/realtime/token-xyz",
|
|
421
|
-
streamAuthToken: "token-xyz",
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
const body = JSON.parse(requireFetchRequest().init?.body as string) as Record<string, unknown>;
|
|
425
|
-
expect(body.command_id).toBe("klaw-answer-call-1");
|
|
426
|
-
expect(body.stream_url).toBe("wss://example.test/voice/stream/realtime/token-xyz");
|
|
427
|
-
expect(body.stream_codec).toBe("PCMU");
|
|
428
|
-
expect(body.stream_bidirectional_target_legs).toBe("self");
|
|
429
|
-
expect(body.stream_auth_token).toBe("token-xyz");
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
it("silently acknowledges streaming.started and streaming.stopped webhooks", () => {
|
|
433
|
-
const provider = new TelnyxProvider(
|
|
434
|
-
{ apiKey: "KEY123", connectionId: "CONN456", publicKey: undefined },
|
|
435
|
-
{ skipVerification: true },
|
|
436
|
-
);
|
|
437
|
-
// Telnyx documents stream lifecycle webhooks as `streaming.started` and
|
|
438
|
-
// `streaming.stopped` (no `call.` prefix). The bridge tracks its own
|
|
439
|
-
// lifecycle on the WebSocket; we ack the carrier webhook with 200 and
|
|
440
|
-
// emit nothing to avoid duplicate signal at the manager.
|
|
441
|
-
for (const eventType of ["streaming.started", "streaming.stopped"]) {
|
|
442
|
-
const rawBody = JSON.stringify({
|
|
443
|
-
data: {
|
|
444
|
-
event_type: eventType,
|
|
445
|
-
id: `evt-${eventType}`,
|
|
446
|
-
payload: { call_control_id: "call-control-1" },
|
|
447
|
-
},
|
|
448
|
-
});
|
|
449
|
-
const result = provider.parseWebhookEvent(createCtx({ rawBody }), {
|
|
450
|
-
verifiedRequestKey: "key-1",
|
|
451
|
-
});
|
|
452
|
-
expect(result.events).toHaveLength(0);
|
|
453
|
-
expect(result.statusCode).toBe(200);
|
|
454
|
-
}
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
describe("TelnyxProvider speak control", () => {
|
|
459
|
-
it("passes custom Telnyx voice ids to the speak action", async () => {
|
|
460
|
-
const release = vi.fn(async () => {});
|
|
461
|
-
apiMocks.fetchWithSsrFGuard.mockResolvedValue({
|
|
462
|
-
response: new Response(JSON.stringify({ data: {} }), { status: 200 }),
|
|
463
|
-
release,
|
|
464
|
-
});
|
|
465
|
-
const provider = new TelnyxProvider({
|
|
466
|
-
apiKey: "KEY123",
|
|
467
|
-
connectionId: "CONN456",
|
|
468
|
-
publicKey: undefined,
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
await provider.playTts({
|
|
472
|
-
callId: "call-1",
|
|
473
|
-
providerCallId: "call-control-1",
|
|
474
|
-
text: "hello",
|
|
475
|
-
voice: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
expect(apiMocks.fetchWithSsrFGuard).toHaveBeenCalledTimes(1);
|
|
479
|
-
const request = requireFetchRequest();
|
|
480
|
-
expect(request.url).toBe("https://api.telnyx.com/v2/calls/call-control-1/actions/speak");
|
|
481
|
-
expect(request.auditContext).toBe("voice-call.telnyx.api");
|
|
482
|
-
expect(request.policy).toEqual({ allowedHostnames: ["api.telnyx.com"] });
|
|
483
|
-
expect(request.init?.method).toBe("POST");
|
|
484
|
-
expect(typeof request.init?.body).toBe("string");
|
|
485
|
-
const body = JSON.parse(request.init?.body as string) as { voice?: string };
|
|
486
|
-
expect(body.voice).toBe("Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc");
|
|
487
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
488
|
-
});
|
|
489
|
-
});
|