@clawdbot/voice-call 0.1.0

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/src/runtime.ts ADDED
@@ -0,0 +1,194 @@
1
+ import type { CoreConfig } from "./core-bridge.js";
2
+ import type { VoiceCallConfig } from "./config.js";
3
+ import { validateProviderConfig } from "./config.js";
4
+ import { CallManager } from "./manager.js";
5
+ import type { VoiceCallProvider } from "./providers/base.js";
6
+ import { MockProvider } from "./providers/mock.js";
7
+ import { TelnyxProvider } from "./providers/telnyx.js";
8
+ import { OpenAITTSProvider } from "./providers/tts-openai.js";
9
+ import { TwilioProvider } from "./providers/twilio.js";
10
+ import { startTunnel, type TunnelResult } from "./tunnel.js";
11
+ import {
12
+ cleanupTailscaleExposure,
13
+ setupTailscaleExposure,
14
+ VoiceCallWebhookServer,
15
+ } from "./webhook.js";
16
+
17
+ export type VoiceCallRuntime = {
18
+ config: VoiceCallConfig;
19
+ provider: VoiceCallProvider;
20
+ manager: CallManager;
21
+ webhookServer: VoiceCallWebhookServer;
22
+ webhookUrl: string;
23
+ publicUrl: string | null;
24
+ stop: () => Promise<void>;
25
+ };
26
+
27
+ type Logger = {
28
+ info: (message: string) => void;
29
+ warn: (message: string) => void;
30
+ error: (message: string) => void;
31
+ debug: (message: string) => void;
32
+ };
33
+
34
+ function resolveProvider(config: VoiceCallConfig): VoiceCallProvider {
35
+ switch (config.provider) {
36
+ case "telnyx":
37
+ return new TelnyxProvider({
38
+ apiKey: config.telnyx?.apiKey ?? process.env.TELNYX_API_KEY,
39
+ connectionId:
40
+ config.telnyx?.connectionId ?? process.env.TELNYX_CONNECTION_ID,
41
+ publicKey: config.telnyx?.publicKey ?? process.env.TELNYX_PUBLIC_KEY,
42
+ });
43
+ case "twilio":
44
+ return new TwilioProvider(
45
+ {
46
+ accountSid:
47
+ config.twilio?.accountSid ?? process.env.TWILIO_ACCOUNT_SID,
48
+ authToken: config.twilio?.authToken ?? process.env.TWILIO_AUTH_TOKEN,
49
+ },
50
+ {
51
+ allowNgrokFreeTier: config.tunnel?.allowNgrokFreeTier ?? true,
52
+ publicUrl: config.publicUrl,
53
+ skipVerification: config.skipSignatureVerification,
54
+ streamPath: config.streaming?.enabled
55
+ ? config.streaming.streamPath
56
+ : undefined,
57
+ },
58
+ );
59
+ case "mock":
60
+ return new MockProvider();
61
+ default:
62
+ throw new Error(
63
+ `Unsupported voice-call provider: ${String(config.provider)}`,
64
+ );
65
+ }
66
+ }
67
+
68
+ export async function createVoiceCallRuntime(params: {
69
+ config: VoiceCallConfig;
70
+ coreConfig: CoreConfig;
71
+ logger?: Logger;
72
+ }): Promise<VoiceCallRuntime> {
73
+ const { config, coreConfig, logger } = params;
74
+ const log = logger ?? {
75
+ info: console.log,
76
+ warn: console.warn,
77
+ error: console.error,
78
+ debug: console.debug,
79
+ };
80
+
81
+ if (!config.enabled) {
82
+ throw new Error(
83
+ "Voice call disabled. Enable the plugin entry in config.",
84
+ );
85
+ }
86
+
87
+ const validation = validateProviderConfig(config);
88
+ if (!validation.valid) {
89
+ throw new Error(`Invalid voice-call config: ${validation.errors.join("; ")}`);
90
+ }
91
+
92
+ const provider = resolveProvider(config);
93
+ const manager = new CallManager(config);
94
+ const webhookServer = new VoiceCallWebhookServer(
95
+ config,
96
+ manager,
97
+ provider,
98
+ coreConfig,
99
+ );
100
+
101
+ const localUrl = await webhookServer.start();
102
+
103
+ // Determine public URL - priority: config.publicUrl > tunnel > legacy tailscale
104
+ let publicUrl: string | null = config.publicUrl ?? null;
105
+ let tunnelResult: TunnelResult | null = null;
106
+
107
+ if (!publicUrl && config.tunnel?.provider && config.tunnel.provider !== "none") {
108
+ try {
109
+ tunnelResult = await startTunnel({
110
+ provider: config.tunnel.provider,
111
+ port: config.serve.port,
112
+ path: config.serve.path,
113
+ ngrokAuthToken:
114
+ config.tunnel.ngrokAuthToken ?? process.env.NGROK_AUTHTOKEN,
115
+ ngrokDomain: config.tunnel.ngrokDomain ?? process.env.NGROK_DOMAIN,
116
+ });
117
+ publicUrl = tunnelResult?.publicUrl ?? null;
118
+ } catch (err) {
119
+ log.error(
120
+ `[voice-call] Tunnel setup failed: ${
121
+ err instanceof Error ? err.message : String(err)
122
+ }`,
123
+ );
124
+ }
125
+ }
126
+
127
+ if (!publicUrl && config.tailscale?.mode !== "off") {
128
+ publicUrl = await setupTailscaleExposure(config);
129
+ }
130
+
131
+ const webhookUrl = publicUrl ?? localUrl;
132
+
133
+ if (publicUrl && provider.name === "twilio") {
134
+ (provider as TwilioProvider).setPublicUrl(publicUrl);
135
+ }
136
+
137
+ if (provider.name === "twilio" && config.streaming?.enabled) {
138
+ const twilioProvider = provider as TwilioProvider;
139
+ const openaiApiKey =
140
+ config.streaming.openaiApiKey || process.env.OPENAI_API_KEY;
141
+ if (openaiApiKey) {
142
+ try {
143
+ const ttsProvider = new OpenAITTSProvider({
144
+ apiKey: openaiApiKey,
145
+ voice: config.tts.voice,
146
+ model: config.tts.model,
147
+ instructions: config.tts.instructions,
148
+ });
149
+ twilioProvider.setTTSProvider(ttsProvider);
150
+ log.info("[voice-call] OpenAI TTS provider configured");
151
+ } catch (err) {
152
+ log.warn(
153
+ `[voice-call] Failed to initialize OpenAI TTS: ${
154
+ err instanceof Error ? err.message : String(err)
155
+ }`,
156
+ );
157
+ }
158
+ } else {
159
+ log.warn("[voice-call] OpenAI TTS key missing; streaming TTS disabled");
160
+ }
161
+
162
+ const mediaHandler = webhookServer.getMediaStreamHandler();
163
+ if (mediaHandler) {
164
+ twilioProvider.setMediaStreamHandler(mediaHandler);
165
+ log.info("[voice-call] Media stream handler wired to provider");
166
+ }
167
+ }
168
+
169
+ manager.initialize(provider, webhookUrl);
170
+
171
+ const stop = async () => {
172
+ if (tunnelResult) {
173
+ await tunnelResult.stop();
174
+ }
175
+ await cleanupTailscaleExposure(config);
176
+ await webhookServer.stop();
177
+ };
178
+
179
+ log.info("[voice-call] Runtime initialized");
180
+ log.info(`[voice-call] Webhook URL: ${webhookUrl}`);
181
+ if (publicUrl) {
182
+ log.info(`[voice-call] Public URL: ${publicUrl}`);
183
+ }
184
+
185
+ return {
186
+ config,
187
+ provider,
188
+ manager,
189
+ webhookServer,
190
+ webhookUrl,
191
+ publicUrl,
192
+ stop,
193
+ };
194
+ }
package/src/tunnel.ts ADDED
@@ -0,0 +1,330 @@
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 localUrl = `http://127.0.0.1:${config.port}`;
234
+
235
+ return new Promise((resolve, reject) => {
236
+ const proc = spawn(
237
+ "tailscale",
238
+ [config.mode, "--bg", "--yes", "--set-path", config.path, localUrl],
239
+ { stdio: ["ignore", "pipe", "pipe"] },
240
+ );
241
+
242
+ const timeout = setTimeout(() => {
243
+ proc.kill("SIGKILL");
244
+ reject(new Error(`Tailscale ${config.mode} timed out`));
245
+ }, 10000);
246
+
247
+ proc.on("close", (code) => {
248
+ clearTimeout(timeout);
249
+ if (code === 0) {
250
+ const publicUrl = `https://${dnsName}${config.path}`;
251
+ console.log(
252
+ `[voice-call] Tailscale ${config.mode} active: ${publicUrl}`,
253
+ );
254
+
255
+ resolve({
256
+ publicUrl,
257
+ provider: `tailscale-${config.mode}`,
258
+ stop: async () => {
259
+ await stopTailscaleTunnel(config.mode, config.path);
260
+ },
261
+ });
262
+ } else {
263
+ reject(new Error(`Tailscale ${config.mode} failed with code ${code}`));
264
+ }
265
+ });
266
+
267
+ proc.on("error", (err) => {
268
+ clearTimeout(timeout);
269
+ reject(err);
270
+ });
271
+ });
272
+ }
273
+
274
+ /**
275
+ * Stop a Tailscale serve/funnel tunnel.
276
+ */
277
+ async function stopTailscaleTunnel(
278
+ mode: "serve" | "funnel",
279
+ path: string,
280
+ ): Promise<void> {
281
+ return new Promise((resolve) => {
282
+ const proc = spawn("tailscale", [mode, "off", path], {
283
+ stdio: "ignore",
284
+ });
285
+
286
+ const timeout = setTimeout(() => {
287
+ proc.kill("SIGKILL");
288
+ resolve();
289
+ }, 5000);
290
+
291
+ proc.on("close", () => {
292
+ clearTimeout(timeout);
293
+ resolve();
294
+ });
295
+ });
296
+ }
297
+
298
+ /**
299
+ * Start a tunnel based on configuration.
300
+ */
301
+ export async function startTunnel(
302
+ config: TunnelConfig,
303
+ ): Promise<TunnelResult | null> {
304
+ switch (config.provider) {
305
+ case "ngrok":
306
+ return startNgrokTunnel({
307
+ port: config.port,
308
+ path: config.path,
309
+ authToken: config.ngrokAuthToken,
310
+ domain: config.ngrokDomain,
311
+ });
312
+
313
+ case "tailscale-serve":
314
+ return startTailscaleTunnel({
315
+ mode: "serve",
316
+ port: config.port,
317
+ path: config.path,
318
+ });
319
+
320
+ case "tailscale-funnel":
321
+ return startTailscaleTunnel({
322
+ mode: "funnel",
323
+ port: config.port,
324
+ path: config.path,
325
+ });
326
+
327
+ default:
328
+ return null;
329
+ }
330
+ }