@openclaw/voice-call 2026.1.29
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/CHANGELOG.md +78 -0
- package/README.md +135 -0
- package/index.ts +497 -0
- package/openclaw.plugin.json +601 -0
- package/package.json +16 -0
- package/src/cli.ts +312 -0
- package/src/config.test.ts +204 -0
- package/src/config.ts +502 -0
- package/src/core-bridge.ts +198 -0
- package/src/manager/context.ts +21 -0
- package/src/manager/events.ts +177 -0
- package/src/manager/lookup.ts +33 -0
- package/src/manager/outbound.ts +248 -0
- package/src/manager/state.ts +50 -0
- package/src/manager/store.ts +88 -0
- package/src/manager/timers.ts +86 -0
- package/src/manager/twiml.ts +9 -0
- package/src/manager.test.ts +108 -0
- package/src/manager.ts +888 -0
- package/src/media-stream.test.ts +97 -0
- package/src/media-stream.ts +393 -0
- package/src/providers/base.ts +67 -0
- package/src/providers/index.ts +10 -0
- package/src/providers/mock.ts +168 -0
- package/src/providers/plivo.test.ts +28 -0
- package/src/providers/plivo.ts +504 -0
- package/src/providers/stt-openai-realtime.ts +311 -0
- package/src/providers/telnyx.ts +364 -0
- package/src/providers/tts-openai.ts +264 -0
- package/src/providers/twilio/api.ts +45 -0
- package/src/providers/twilio/webhook.ts +30 -0
- package/src/providers/twilio.test.ts +64 -0
- package/src/providers/twilio.ts +595 -0
- package/src/response-generator.ts +171 -0
- package/src/runtime.ts +217 -0
- package/src/telephony-audio.ts +88 -0
- package/src/telephony-tts.ts +95 -0
- package/src/tunnel.ts +331 -0
- package/src/types.ts +273 -0
- package/src/utils.ts +12 -0
- package/src/voice-mapping.ts +65 -0
- package/src/webhook-security.test.ts +260 -0
- package/src/webhook-security.ts +469 -0
- package/src/webhook.ts +491 -0
package/src/tunnel.ts
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
import { getTailscaleDnsName } from "./webhook.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Tunnel configuration for exposing the webhook server.
|
|
7
|
+
*/
|
|
8
|
+
export interface TunnelConfig {
|
|
9
|
+
/** Tunnel provider: ngrok, tailscale-serve, or tailscale-funnel */
|
|
10
|
+
provider: "ngrok" | "tailscale-serve" | "tailscale-funnel" | "none";
|
|
11
|
+
/** Local port to tunnel */
|
|
12
|
+
port: number;
|
|
13
|
+
/** Path prefix for the tunnel (e.g., /voice/webhook) */
|
|
14
|
+
path: string;
|
|
15
|
+
/** ngrok auth token (optional, enables longer sessions) */
|
|
16
|
+
ngrokAuthToken?: string;
|
|
17
|
+
/** ngrok custom domain (paid feature) */
|
|
18
|
+
ngrokDomain?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Result of starting a tunnel.
|
|
23
|
+
*/
|
|
24
|
+
export interface TunnelResult {
|
|
25
|
+
/** The public URL */
|
|
26
|
+
publicUrl: string;
|
|
27
|
+
/** Function to stop the tunnel */
|
|
28
|
+
stop: () => Promise<void>;
|
|
29
|
+
/** Tunnel provider name */
|
|
30
|
+
provider: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Start an ngrok tunnel to expose the local webhook server.
|
|
35
|
+
*
|
|
36
|
+
* Uses the ngrok CLI which must be installed: https://ngrok.com/download
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const tunnel = await startNgrokTunnel({ port: 3334, path: '/voice/webhook' });
|
|
40
|
+
* console.log('Public URL:', tunnel.publicUrl);
|
|
41
|
+
* // Later: await tunnel.stop();
|
|
42
|
+
*/
|
|
43
|
+
export async function startNgrokTunnel(config: {
|
|
44
|
+
port: number;
|
|
45
|
+
path: string;
|
|
46
|
+
authToken?: string;
|
|
47
|
+
domain?: string;
|
|
48
|
+
}): Promise<TunnelResult> {
|
|
49
|
+
// Set auth token if provided
|
|
50
|
+
if (config.authToken) {
|
|
51
|
+
await runNgrokCommand(["config", "add-authtoken", config.authToken]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Build ngrok command args
|
|
55
|
+
const args = [
|
|
56
|
+
"http",
|
|
57
|
+
String(config.port),
|
|
58
|
+
"--log",
|
|
59
|
+
"stdout",
|
|
60
|
+
"--log-format",
|
|
61
|
+
"json",
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// Add custom domain if provided (paid ngrok feature)
|
|
65
|
+
if (config.domain) {
|
|
66
|
+
args.push("--domain", config.domain);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const proc = spawn("ngrok", args, {
|
|
71
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
let resolved = false;
|
|
75
|
+
let publicUrl: string | null = null;
|
|
76
|
+
let outputBuffer = "";
|
|
77
|
+
|
|
78
|
+
const timeout = setTimeout(() => {
|
|
79
|
+
if (!resolved) {
|
|
80
|
+
resolved = true;
|
|
81
|
+
proc.kill("SIGTERM");
|
|
82
|
+
reject(new Error("ngrok startup timed out (30s)"));
|
|
83
|
+
}
|
|
84
|
+
}, 30000);
|
|
85
|
+
|
|
86
|
+
const processLine = (line: string) => {
|
|
87
|
+
try {
|
|
88
|
+
const log = JSON.parse(line);
|
|
89
|
+
|
|
90
|
+
// ngrok logs the public URL in a 'started tunnel' message
|
|
91
|
+
if (log.msg === "started tunnel" && log.url) {
|
|
92
|
+
publicUrl = log.url;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Also check for the URL field directly
|
|
96
|
+
if (log.addr && log.url && !publicUrl) {
|
|
97
|
+
publicUrl = log.url;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Check for ready state
|
|
101
|
+
if (publicUrl && !resolved) {
|
|
102
|
+
resolved = true;
|
|
103
|
+
clearTimeout(timeout);
|
|
104
|
+
|
|
105
|
+
// Add path to the public URL
|
|
106
|
+
const fullUrl = publicUrl + config.path;
|
|
107
|
+
|
|
108
|
+
console.log(`[voice-call] ngrok tunnel active: ${fullUrl}`);
|
|
109
|
+
|
|
110
|
+
resolve({
|
|
111
|
+
publicUrl: fullUrl,
|
|
112
|
+
provider: "ngrok",
|
|
113
|
+
stop: async () => {
|
|
114
|
+
proc.kill("SIGTERM");
|
|
115
|
+
await new Promise<void>((res) => {
|
|
116
|
+
proc.on("close", () => res());
|
|
117
|
+
setTimeout(res, 2000); // Fallback timeout
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
} catch {
|
|
123
|
+
// Not JSON, might be startup message
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
proc.stdout.on("data", (data: Buffer) => {
|
|
128
|
+
outputBuffer += data.toString();
|
|
129
|
+
const lines = outputBuffer.split("\n");
|
|
130
|
+
outputBuffer = lines.pop() || "";
|
|
131
|
+
|
|
132
|
+
for (const line of lines) {
|
|
133
|
+
if (line.trim()) {
|
|
134
|
+
processLine(line);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
proc.stderr.on("data", (data: Buffer) => {
|
|
140
|
+
const msg = data.toString();
|
|
141
|
+
// Check for common errors
|
|
142
|
+
if (msg.includes("ERR_NGROK")) {
|
|
143
|
+
if (!resolved) {
|
|
144
|
+
resolved = true;
|
|
145
|
+
clearTimeout(timeout);
|
|
146
|
+
reject(new Error(`ngrok error: ${msg}`));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
proc.on("error", (err) => {
|
|
152
|
+
if (!resolved) {
|
|
153
|
+
resolved = true;
|
|
154
|
+
clearTimeout(timeout);
|
|
155
|
+
reject(new Error(`Failed to start ngrok: ${err.message}`));
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
proc.on("close", (code) => {
|
|
160
|
+
if (!resolved) {
|
|
161
|
+
resolved = true;
|
|
162
|
+
clearTimeout(timeout);
|
|
163
|
+
reject(new Error(`ngrok exited unexpectedly with code ${code}`));
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Run an ngrok command and wait for completion.
|
|
171
|
+
*/
|
|
172
|
+
async function runNgrokCommand(args: string[]): Promise<string> {
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
const proc = spawn("ngrok", args, {
|
|
175
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
let stdout = "";
|
|
179
|
+
let stderr = "";
|
|
180
|
+
|
|
181
|
+
proc.stdout.on("data", (data) => {
|
|
182
|
+
stdout += data.toString();
|
|
183
|
+
});
|
|
184
|
+
proc.stderr.on("data", (data) => {
|
|
185
|
+
stderr += data.toString();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
proc.on("close", (code) => {
|
|
189
|
+
if (code === 0) {
|
|
190
|
+
resolve(stdout);
|
|
191
|
+
} else {
|
|
192
|
+
reject(new Error(`ngrok command failed: ${stderr || stdout}`));
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
proc.on("error", reject);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Check if ngrok is installed and available.
|
|
202
|
+
*/
|
|
203
|
+
export async function isNgrokAvailable(): Promise<boolean> {
|
|
204
|
+
return new Promise((resolve) => {
|
|
205
|
+
const proc = spawn("ngrok", ["version"], {
|
|
206
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
proc.on("close", (code) => {
|
|
210
|
+
resolve(code === 0);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
proc.on("error", () => {
|
|
214
|
+
resolve(false);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Start a Tailscale serve/funnel tunnel.
|
|
221
|
+
*/
|
|
222
|
+
export async function startTailscaleTunnel(config: {
|
|
223
|
+
mode: "serve" | "funnel";
|
|
224
|
+
port: number;
|
|
225
|
+
path: string;
|
|
226
|
+
}): Promise<TunnelResult> {
|
|
227
|
+
// Get Tailscale DNS name
|
|
228
|
+
const dnsName = await getTailscaleDnsName();
|
|
229
|
+
if (!dnsName) {
|
|
230
|
+
throw new Error("Could not get Tailscale DNS name. Is Tailscale running?");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const path = config.path.startsWith("/") ? config.path : `/${config.path}`;
|
|
234
|
+
const localUrl = `http://127.0.0.1:${config.port}${path}`;
|
|
235
|
+
|
|
236
|
+
return new Promise((resolve, reject) => {
|
|
237
|
+
const proc = spawn(
|
|
238
|
+
"tailscale",
|
|
239
|
+
[config.mode, "--bg", "--yes", "--set-path", path, localUrl],
|
|
240
|
+
{ stdio: ["ignore", "pipe", "pipe"] },
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const timeout = setTimeout(() => {
|
|
244
|
+
proc.kill("SIGKILL");
|
|
245
|
+
reject(new Error(`Tailscale ${config.mode} timed out`));
|
|
246
|
+
}, 10000);
|
|
247
|
+
|
|
248
|
+
proc.on("close", (code) => {
|
|
249
|
+
clearTimeout(timeout);
|
|
250
|
+
if (code === 0) {
|
|
251
|
+
const publicUrl = `https://${dnsName}${path}`;
|
|
252
|
+
console.log(
|
|
253
|
+
`[voice-call] Tailscale ${config.mode} active: ${publicUrl}`,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
resolve({
|
|
257
|
+
publicUrl,
|
|
258
|
+
provider: `tailscale-${config.mode}`,
|
|
259
|
+
stop: async () => {
|
|
260
|
+
await stopTailscaleTunnel(config.mode, path);
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
} else {
|
|
264
|
+
reject(new Error(`Tailscale ${config.mode} failed with code ${code}`));
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
proc.on("error", (err) => {
|
|
269
|
+
clearTimeout(timeout);
|
|
270
|
+
reject(err);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Stop a Tailscale serve/funnel tunnel.
|
|
277
|
+
*/
|
|
278
|
+
async function stopTailscaleTunnel(
|
|
279
|
+
mode: "serve" | "funnel",
|
|
280
|
+
path: string,
|
|
281
|
+
): Promise<void> {
|
|
282
|
+
return new Promise((resolve) => {
|
|
283
|
+
const proc = spawn("tailscale", [mode, "off", path], {
|
|
284
|
+
stdio: "ignore",
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const timeout = setTimeout(() => {
|
|
288
|
+
proc.kill("SIGKILL");
|
|
289
|
+
resolve();
|
|
290
|
+
}, 5000);
|
|
291
|
+
|
|
292
|
+
proc.on("close", () => {
|
|
293
|
+
clearTimeout(timeout);
|
|
294
|
+
resolve();
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Start a tunnel based on configuration.
|
|
301
|
+
*/
|
|
302
|
+
export async function startTunnel(
|
|
303
|
+
config: TunnelConfig,
|
|
304
|
+
): Promise<TunnelResult | null> {
|
|
305
|
+
switch (config.provider) {
|
|
306
|
+
case "ngrok":
|
|
307
|
+
return startNgrokTunnel({
|
|
308
|
+
port: config.port,
|
|
309
|
+
path: config.path,
|
|
310
|
+
authToken: config.ngrokAuthToken,
|
|
311
|
+
domain: config.ngrokDomain,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
case "tailscale-serve":
|
|
315
|
+
return startTailscaleTunnel({
|
|
316
|
+
mode: "serve",
|
|
317
|
+
port: config.port,
|
|
318
|
+
path: config.path,
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
case "tailscale-funnel":
|
|
322
|
+
return startTailscaleTunnel({
|
|
323
|
+
mode: "funnel",
|
|
324
|
+
port: config.port,
|
|
325
|
+
path: config.path,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
default:
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import type { CallMode } from "./config.js";
|
|
4
|
+
|
|
5
|
+
// -----------------------------------------------------------------------------
|
|
6
|
+
// Provider Identifiers
|
|
7
|
+
// -----------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
export const ProviderNameSchema = z.enum(["telnyx", "twilio", "plivo", "mock"]);
|
|
10
|
+
export type ProviderName = z.infer<typeof ProviderNameSchema>;
|
|
11
|
+
|
|
12
|
+
// -----------------------------------------------------------------------------
|
|
13
|
+
// Core Call Identifiers
|
|
14
|
+
// -----------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/** Internal call identifier (UUID) */
|
|
17
|
+
export type CallId = string;
|
|
18
|
+
|
|
19
|
+
/** Provider-specific call identifier */
|
|
20
|
+
export type ProviderCallId = string;
|
|
21
|
+
|
|
22
|
+
// -----------------------------------------------------------------------------
|
|
23
|
+
// Call Lifecycle States
|
|
24
|
+
// -----------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
export const CallStateSchema = z.enum([
|
|
27
|
+
// Non-terminal states
|
|
28
|
+
"initiated",
|
|
29
|
+
"ringing",
|
|
30
|
+
"answered",
|
|
31
|
+
"active",
|
|
32
|
+
"speaking",
|
|
33
|
+
"listening",
|
|
34
|
+
// Terminal states
|
|
35
|
+
"completed",
|
|
36
|
+
"hangup-user",
|
|
37
|
+
"hangup-bot",
|
|
38
|
+
"timeout",
|
|
39
|
+
"error",
|
|
40
|
+
"failed",
|
|
41
|
+
"no-answer",
|
|
42
|
+
"busy",
|
|
43
|
+
"voicemail",
|
|
44
|
+
]);
|
|
45
|
+
export type CallState = z.infer<typeof CallStateSchema>;
|
|
46
|
+
|
|
47
|
+
export const TerminalStates = new Set<CallState>([
|
|
48
|
+
"completed",
|
|
49
|
+
"hangup-user",
|
|
50
|
+
"hangup-bot",
|
|
51
|
+
"timeout",
|
|
52
|
+
"error",
|
|
53
|
+
"failed",
|
|
54
|
+
"no-answer",
|
|
55
|
+
"busy",
|
|
56
|
+
"voicemail",
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
export const EndReasonSchema = z.enum([
|
|
60
|
+
"completed",
|
|
61
|
+
"hangup-user",
|
|
62
|
+
"hangup-bot",
|
|
63
|
+
"timeout",
|
|
64
|
+
"error",
|
|
65
|
+
"failed",
|
|
66
|
+
"no-answer",
|
|
67
|
+
"busy",
|
|
68
|
+
"voicemail",
|
|
69
|
+
]);
|
|
70
|
+
export type EndReason = z.infer<typeof EndReasonSchema>;
|
|
71
|
+
|
|
72
|
+
// -----------------------------------------------------------------------------
|
|
73
|
+
// Normalized Call Events
|
|
74
|
+
// -----------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
const BaseEventSchema = z.object({
|
|
77
|
+
id: z.string(),
|
|
78
|
+
callId: z.string(),
|
|
79
|
+
providerCallId: z.string().optional(),
|
|
80
|
+
timestamp: z.number(),
|
|
81
|
+
// Optional fields for inbound call detection
|
|
82
|
+
direction: z.enum(["inbound", "outbound"]).optional(),
|
|
83
|
+
from: z.string().optional(),
|
|
84
|
+
to: z.string().optional(),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const NormalizedEventSchema = z.discriminatedUnion("type", [
|
|
88
|
+
BaseEventSchema.extend({
|
|
89
|
+
type: z.literal("call.initiated"),
|
|
90
|
+
}),
|
|
91
|
+
BaseEventSchema.extend({
|
|
92
|
+
type: z.literal("call.ringing"),
|
|
93
|
+
}),
|
|
94
|
+
BaseEventSchema.extend({
|
|
95
|
+
type: z.literal("call.answered"),
|
|
96
|
+
}),
|
|
97
|
+
BaseEventSchema.extend({
|
|
98
|
+
type: z.literal("call.active"),
|
|
99
|
+
}),
|
|
100
|
+
BaseEventSchema.extend({
|
|
101
|
+
type: z.literal("call.speaking"),
|
|
102
|
+
text: z.string(),
|
|
103
|
+
}),
|
|
104
|
+
BaseEventSchema.extend({
|
|
105
|
+
type: z.literal("call.speech"),
|
|
106
|
+
transcript: z.string(),
|
|
107
|
+
isFinal: z.boolean(),
|
|
108
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
109
|
+
}),
|
|
110
|
+
BaseEventSchema.extend({
|
|
111
|
+
type: z.literal("call.silence"),
|
|
112
|
+
durationMs: z.number(),
|
|
113
|
+
}),
|
|
114
|
+
BaseEventSchema.extend({
|
|
115
|
+
type: z.literal("call.dtmf"),
|
|
116
|
+
digits: z.string(),
|
|
117
|
+
}),
|
|
118
|
+
BaseEventSchema.extend({
|
|
119
|
+
type: z.literal("call.ended"),
|
|
120
|
+
reason: EndReasonSchema,
|
|
121
|
+
}),
|
|
122
|
+
BaseEventSchema.extend({
|
|
123
|
+
type: z.literal("call.error"),
|
|
124
|
+
error: z.string(),
|
|
125
|
+
retryable: z.boolean().optional(),
|
|
126
|
+
}),
|
|
127
|
+
]);
|
|
128
|
+
export type NormalizedEvent = z.infer<typeof NormalizedEventSchema>;
|
|
129
|
+
|
|
130
|
+
// -----------------------------------------------------------------------------
|
|
131
|
+
// Call Direction
|
|
132
|
+
// -----------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
export const CallDirectionSchema = z.enum(["outbound", "inbound"]);
|
|
135
|
+
export type CallDirection = z.infer<typeof CallDirectionSchema>;
|
|
136
|
+
|
|
137
|
+
// -----------------------------------------------------------------------------
|
|
138
|
+
// Call Record
|
|
139
|
+
// -----------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
export const TranscriptEntrySchema = z.object({
|
|
142
|
+
timestamp: z.number(),
|
|
143
|
+
speaker: z.enum(["bot", "user"]),
|
|
144
|
+
text: z.string(),
|
|
145
|
+
isFinal: z.boolean().default(true),
|
|
146
|
+
});
|
|
147
|
+
export type TranscriptEntry = z.infer<typeof TranscriptEntrySchema>;
|
|
148
|
+
|
|
149
|
+
export const CallRecordSchema = z.object({
|
|
150
|
+
callId: z.string(),
|
|
151
|
+
providerCallId: z.string().optional(),
|
|
152
|
+
provider: ProviderNameSchema,
|
|
153
|
+
direction: CallDirectionSchema,
|
|
154
|
+
state: CallStateSchema,
|
|
155
|
+
from: z.string(),
|
|
156
|
+
to: z.string(),
|
|
157
|
+
sessionKey: z.string().optional(),
|
|
158
|
+
startedAt: z.number(),
|
|
159
|
+
answeredAt: z.number().optional(),
|
|
160
|
+
endedAt: z.number().optional(),
|
|
161
|
+
endReason: EndReasonSchema.optional(),
|
|
162
|
+
transcript: z.array(TranscriptEntrySchema).default([]),
|
|
163
|
+
processedEventIds: z.array(z.string()).default([]),
|
|
164
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
165
|
+
});
|
|
166
|
+
export type CallRecord = z.infer<typeof CallRecordSchema>;
|
|
167
|
+
|
|
168
|
+
// -----------------------------------------------------------------------------
|
|
169
|
+
// Webhook Types
|
|
170
|
+
// -----------------------------------------------------------------------------
|
|
171
|
+
|
|
172
|
+
export type WebhookVerificationResult = {
|
|
173
|
+
ok: boolean;
|
|
174
|
+
reason?: string;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export type WebhookContext = {
|
|
178
|
+
headers: Record<string, string | string[] | undefined>;
|
|
179
|
+
rawBody: string;
|
|
180
|
+
url: string;
|
|
181
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
182
|
+
query?: Record<string, string | string[] | undefined>;
|
|
183
|
+
remoteAddress?: string;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export type ProviderWebhookParseResult = {
|
|
187
|
+
events: NormalizedEvent[];
|
|
188
|
+
providerResponseBody?: string;
|
|
189
|
+
providerResponseHeaders?: Record<string, string>;
|
|
190
|
+
statusCode?: number;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// -----------------------------------------------------------------------------
|
|
194
|
+
// Provider Method Types
|
|
195
|
+
// -----------------------------------------------------------------------------
|
|
196
|
+
|
|
197
|
+
export type InitiateCallInput = {
|
|
198
|
+
callId: CallId;
|
|
199
|
+
from: string;
|
|
200
|
+
to: string;
|
|
201
|
+
webhookUrl: string;
|
|
202
|
+
clientState?: Record<string, string>;
|
|
203
|
+
/** Inline TwiML to execute (skips webhook, used for notify mode) */
|
|
204
|
+
inlineTwiml?: string;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export type InitiateCallResult = {
|
|
208
|
+
providerCallId: ProviderCallId;
|
|
209
|
+
status: "initiated" | "queued";
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export type HangupCallInput = {
|
|
213
|
+
callId: CallId;
|
|
214
|
+
providerCallId: ProviderCallId;
|
|
215
|
+
reason: EndReason;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export type PlayTtsInput = {
|
|
219
|
+
callId: CallId;
|
|
220
|
+
providerCallId: ProviderCallId;
|
|
221
|
+
text: string;
|
|
222
|
+
voice?: string;
|
|
223
|
+
locale?: string;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
export type StartListeningInput = {
|
|
227
|
+
callId: CallId;
|
|
228
|
+
providerCallId: ProviderCallId;
|
|
229
|
+
language?: string;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export type StopListeningInput = {
|
|
233
|
+
callId: CallId;
|
|
234
|
+
providerCallId: ProviderCallId;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// -----------------------------------------------------------------------------
|
|
238
|
+
// Outbound Call Options
|
|
239
|
+
// -----------------------------------------------------------------------------
|
|
240
|
+
|
|
241
|
+
export type OutboundCallOptions = {
|
|
242
|
+
/** Message to speak when call connects */
|
|
243
|
+
message?: string;
|
|
244
|
+
/** Call mode (overrides config default) */
|
|
245
|
+
mode?: CallMode;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
// -----------------------------------------------------------------------------
|
|
249
|
+
// Tool Result Types
|
|
250
|
+
// -----------------------------------------------------------------------------
|
|
251
|
+
|
|
252
|
+
export type InitiateCallToolResult = {
|
|
253
|
+
success: boolean;
|
|
254
|
+
callId?: string;
|
|
255
|
+
status?: "initiated" | "queued" | "no-answer" | "busy" | "failed";
|
|
256
|
+
error?: string;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export type ContinueCallToolResult = {
|
|
260
|
+
success: boolean;
|
|
261
|
+
transcript?: string;
|
|
262
|
+
error?: string;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export type SpeakToUserToolResult = {
|
|
266
|
+
success: boolean;
|
|
267
|
+
error?: string;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export type EndCallToolResult = {
|
|
271
|
+
success: boolean;
|
|
272
|
+
error?: string;
|
|
273
|
+
};
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function resolveUserPath(input: string): string {
|
|
5
|
+
const trimmed = input.trim();
|
|
6
|
+
if (!trimmed) return trimmed;
|
|
7
|
+
if (trimmed.startsWith("~")) {
|
|
8
|
+
const expanded = trimmed.replace(/^~(?=$|[\\/])/, os.homedir());
|
|
9
|
+
return path.resolve(expanded);
|
|
10
|
+
}
|
|
11
|
+
return path.resolve(trimmed);
|
|
12
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Voice mapping and XML utilities for voice call providers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Escape XML special characters for TwiML and other XML responses.
|
|
7
|
+
*/
|
|
8
|
+
export function escapeXml(text: string): string {
|
|
9
|
+
return text
|
|
10
|
+
.replace(/&/g, "&")
|
|
11
|
+
.replace(/</g, "<")
|
|
12
|
+
.replace(/>/g, ">")
|
|
13
|
+
.replace(/"/g, """)
|
|
14
|
+
.replace(/'/g, "'");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Map of OpenAI voice names to similar Twilio Polly voices.
|
|
19
|
+
*/
|
|
20
|
+
const OPENAI_TO_POLLY_MAP: Record<string, string> = {
|
|
21
|
+
alloy: "Polly.Joanna", // neutral, warm
|
|
22
|
+
echo: "Polly.Matthew", // male, warm
|
|
23
|
+
fable: "Polly.Amy", // British, expressive
|
|
24
|
+
onyx: "Polly.Brian", // deep male
|
|
25
|
+
nova: "Polly.Salli", // female, friendly
|
|
26
|
+
shimmer: "Polly.Kimberly", // female, clear
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Default Polly voice when no mapping is found.
|
|
31
|
+
*/
|
|
32
|
+
export const DEFAULT_POLLY_VOICE = "Polly.Joanna";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Map OpenAI voice names to Twilio Polly equivalents.
|
|
36
|
+
* Falls through if already a valid Polly/Google voice.
|
|
37
|
+
*
|
|
38
|
+
* @param voice - OpenAI voice name (alloy, echo, etc.) or Polly voice name
|
|
39
|
+
* @returns Polly voice name suitable for Twilio TwiML
|
|
40
|
+
*/
|
|
41
|
+
export function mapVoiceToPolly(voice: string | undefined): string {
|
|
42
|
+
if (!voice) return DEFAULT_POLLY_VOICE;
|
|
43
|
+
|
|
44
|
+
// Already a Polly/Google voice - pass through
|
|
45
|
+
if (voice.startsWith("Polly.") || voice.startsWith("Google.")) {
|
|
46
|
+
return voice;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Map OpenAI voices to Polly equivalents
|
|
50
|
+
return OPENAI_TO_POLLY_MAP[voice.toLowerCase()] || DEFAULT_POLLY_VOICE;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Check if a voice name is a known OpenAI voice.
|
|
55
|
+
*/
|
|
56
|
+
export function isOpenAiVoice(voice: string): boolean {
|
|
57
|
+
return voice.toLowerCase() in OPENAI_TO_POLLY_MAP;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get all supported OpenAI voice names.
|
|
62
|
+
*/
|
|
63
|
+
export function getOpenAiVoiceNames(): string[] {
|
|
64
|
+
return Object.keys(OPENAI_TO_POLLY_MAP);
|
|
65
|
+
}
|