@openclaw/voice-call 2026.5.2 → 2026.5.3-beta.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.
Files changed (126) hide show
  1. package/dist/api.js +2 -0
  2. package/dist/call-status-CXldV5o8.js +32 -0
  3. package/dist/cli-metadata.js +12 -0
  4. package/dist/config-7w04YpHh.js +548 -0
  5. package/dist/config-compat-B0me39_4.js +129 -0
  6. package/dist/guarded-json-api-Btx5EE4w.js +591 -0
  7. package/dist/http-headers-BrnxBasF.js +10 -0
  8. package/dist/index.js +1284 -0
  9. package/dist/mock-CeKvfVEd.js +135 -0
  10. package/dist/plivo-B-a7KFoT.js +393 -0
  11. package/dist/realtime-handler-B63CIDP2.js +325 -0
  12. package/dist/realtime-transcription.runtime-B2h70y2W.js +2 -0
  13. package/dist/realtime-voice.runtime-Bkh4nvLn.js +2 -0
  14. package/dist/response-generator-BrcmwDZU.js +182 -0
  15. package/dist/response-model-CyF5K80p.js +12 -0
  16. package/dist/runtime-api.js +6 -0
  17. package/dist/runtime-entry-88ytYAQa.js +3119 -0
  18. package/dist/runtime-entry.js +2 -0
  19. package/dist/setup-api.js +37 -0
  20. package/dist/telnyx-jjBE8boz.js +260 -0
  21. package/dist/twilio-1OqbcXLL.js +676 -0
  22. package/dist/voice-mapping-BYDGdWGx.js +40 -0
  23. package/package.json +14 -6
  24. package/api.ts +0 -16
  25. package/cli-metadata.ts +0 -10
  26. package/config-api.ts +0 -12
  27. package/index.test.ts +0 -943
  28. package/index.ts +0 -794
  29. package/runtime-api.ts +0 -20
  30. package/runtime-entry.ts +0 -1
  31. package/setup-api.ts +0 -47
  32. package/src/allowlist.test.ts +0 -18
  33. package/src/allowlist.ts +0 -19
  34. package/src/cli.ts +0 -845
  35. package/src/config-compat.test.ts +0 -120
  36. package/src/config-compat.ts +0 -227
  37. package/src/config.test.ts +0 -479
  38. package/src/config.ts +0 -808
  39. package/src/core-bridge.ts +0 -14
  40. package/src/deep-merge.test.ts +0 -40
  41. package/src/deep-merge.ts +0 -23
  42. package/src/gateway-continue-operation.ts +0 -200
  43. package/src/http-headers.test.ts +0 -16
  44. package/src/http-headers.ts +0 -15
  45. package/src/manager/context.ts +0 -42
  46. package/src/manager/events.test.ts +0 -581
  47. package/src/manager/events.ts +0 -288
  48. package/src/manager/lifecycle.ts +0 -53
  49. package/src/manager/lookup.test.ts +0 -52
  50. package/src/manager/lookup.ts +0 -35
  51. package/src/manager/outbound.test.ts +0 -528
  52. package/src/manager/outbound.ts +0 -486
  53. package/src/manager/state.ts +0 -48
  54. package/src/manager/store.ts +0 -106
  55. package/src/manager/timers.test.ts +0 -129
  56. package/src/manager/timers.ts +0 -113
  57. package/src/manager/twiml.test.ts +0 -13
  58. package/src/manager/twiml.ts +0 -17
  59. package/src/manager.closed-loop.test.ts +0 -236
  60. package/src/manager.inbound-allowlist.test.ts +0 -188
  61. package/src/manager.notify.test.ts +0 -377
  62. package/src/manager.restore.test.ts +0 -183
  63. package/src/manager.test-harness.ts +0 -127
  64. package/src/manager.ts +0 -392
  65. package/src/media-stream.test.ts +0 -768
  66. package/src/media-stream.ts +0 -708
  67. package/src/providers/base.ts +0 -97
  68. package/src/providers/mock.test.ts +0 -78
  69. package/src/providers/mock.ts +0 -185
  70. package/src/providers/plivo.test.ts +0 -93
  71. package/src/providers/plivo.ts +0 -601
  72. package/src/providers/shared/call-status.test.ts +0 -24
  73. package/src/providers/shared/call-status.ts +0 -24
  74. package/src/providers/shared/guarded-json-api.test.ts +0 -106
  75. package/src/providers/shared/guarded-json-api.ts +0 -42
  76. package/src/providers/telnyx.test.ts +0 -340
  77. package/src/providers/telnyx.ts +0 -394
  78. package/src/providers/twilio/api.test.ts +0 -145
  79. package/src/providers/twilio/api.ts +0 -93
  80. package/src/providers/twilio/twiml-policy.test.ts +0 -84
  81. package/src/providers/twilio/twiml-policy.ts +0 -87
  82. package/src/providers/twilio/webhook.ts +0 -34
  83. package/src/providers/twilio.test.ts +0 -591
  84. package/src/providers/twilio.ts +0 -861
  85. package/src/providers/twilio.types.ts +0 -17
  86. package/src/realtime-defaults.ts +0 -3
  87. package/src/realtime-fast-context.test.ts +0 -88
  88. package/src/realtime-fast-context.ts +0 -165
  89. package/src/realtime-transcription.runtime.ts +0 -4
  90. package/src/realtime-voice.runtime.ts +0 -5
  91. package/src/response-generator.test.ts +0 -321
  92. package/src/response-generator.ts +0 -318
  93. package/src/response-model.test.ts +0 -71
  94. package/src/response-model.ts +0 -23
  95. package/src/runtime.test.ts +0 -536
  96. package/src/runtime.ts +0 -510
  97. package/src/telephony-audio.test.ts +0 -61
  98. package/src/telephony-audio.ts +0 -12
  99. package/src/telephony-tts.test.ts +0 -196
  100. package/src/telephony-tts.ts +0 -235
  101. package/src/test-fixtures.ts +0 -73
  102. package/src/tts-provider-voice.test.ts +0 -34
  103. package/src/tts-provider-voice.ts +0 -21
  104. package/src/tunnel.test.ts +0 -166
  105. package/src/tunnel.ts +0 -314
  106. package/src/types.ts +0 -291
  107. package/src/utils.test.ts +0 -17
  108. package/src/utils.ts +0 -14
  109. package/src/voice-mapping.test.ts +0 -34
  110. package/src/voice-mapping.ts +0 -68
  111. package/src/webhook/realtime-handler.test.ts +0 -598
  112. package/src/webhook/realtime-handler.ts +0 -485
  113. package/src/webhook/stale-call-reaper.test.ts +0 -88
  114. package/src/webhook/stale-call-reaper.ts +0 -38
  115. package/src/webhook/tailscale.test.ts +0 -214
  116. package/src/webhook/tailscale.ts +0 -129
  117. package/src/webhook-exposure.test.ts +0 -33
  118. package/src/webhook-exposure.ts +0 -84
  119. package/src/webhook-security.test.ts +0 -770
  120. package/src/webhook-security.ts +0 -994
  121. package/src/webhook.hangup-once.lifecycle.test.ts +0 -135
  122. package/src/webhook.test.ts +0 -1470
  123. package/src/webhook.ts +0 -908
  124. package/src/webhook.types.ts +0 -5
  125. package/src/websocket-test-support.ts +0 -72
  126. package/tsconfig.json +0 -16
package/src/manager.ts DELETED
@@ -1,392 +0,0 @@
1
- import fs from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
5
- import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
6
- import type { VoiceCallConfig } from "./config.js";
7
- import type { CallManagerContext } from "./manager/context.js";
8
- import { processEvent as processManagerEvent } from "./manager/events.js";
9
- import { getCallByProviderCallId as getCallByProviderCallIdFromMaps } from "./manager/lookup.js";
10
- import {
11
- continueCall as continueCallWithContext,
12
- endCall as endCallWithContext,
13
- initiateCall as initiateCallWithContext,
14
- sendDtmf as sendDtmfWithContext,
15
- speak as speakWithContext,
16
- speakInitialMessage as speakInitialMessageWithContext,
17
- } from "./manager/outbound.js";
18
- import {
19
- getCallHistoryFromStore,
20
- loadActiveCallsFromStore,
21
- persistCallRecord,
22
- } from "./manager/store.js";
23
- import { startMaxDurationTimer } from "./manager/timers.js";
24
- import type { VoiceCallProvider } from "./providers/base.js";
25
- import {
26
- TerminalStates,
27
- type CallId,
28
- type CallRecord,
29
- type NormalizedEvent,
30
- type OutboundCallOptions,
31
- } from "./types.js";
32
- import { resolveUserPath } from "./utils.js";
33
-
34
- function markRestoredCallSkipped(call: CallRecord, endReason: "completed" | "timeout"): void {
35
- call.endedAt = Date.now();
36
- call.endReason = endReason;
37
- call.state = endReason;
38
- }
39
-
40
- function resolveDefaultStoreBase(config: VoiceCallConfig, storePath?: string): string {
41
- const rawOverride = storePath?.trim() || config.store?.trim();
42
- if (rawOverride) {
43
- return resolveUserPath(rawOverride);
44
- }
45
- const preferred = path.join(os.homedir(), ".openclaw", "voice-calls");
46
- const candidates = [preferred].map((dir) => resolveUserPath(dir));
47
- const existing =
48
- candidates.find((dir) => {
49
- try {
50
- return fs.existsSync(path.join(dir, "calls.jsonl")) || fs.existsSync(dir);
51
- } catch {
52
- return false;
53
- }
54
- }) ?? resolveUserPath(preferred);
55
- return existing;
56
- }
57
-
58
- /**
59
- * Manages voice calls: state ownership and delegation to manager helper modules.
60
- */
61
- export class CallManager {
62
- private activeCalls = new Map<CallId, CallRecord>();
63
- private providerCallIdMap = new Map<string, CallId>();
64
- private processedEventIds = new Set<string>();
65
- private rejectedProviderCallIds = new Set<string>();
66
- private provider: VoiceCallProvider | null = null;
67
- private config: VoiceCallConfig;
68
- private storePath: string;
69
- private webhookUrl: string | null = null;
70
- private activeTurnCalls = new Set<CallId>();
71
- private transcriptWaiters = new Map<
72
- CallId,
73
- {
74
- resolve: (text: string) => void;
75
- reject: (err: Error) => void;
76
- timeout: NodeJS.Timeout;
77
- }
78
- >();
79
- private maxDurationTimers = new Map<CallId, NodeJS.Timeout>();
80
- private initialMessageInFlight = new Set<CallId>();
81
-
82
- constructor(config: VoiceCallConfig, storePath?: string) {
83
- this.config = config;
84
- this.storePath = resolveDefaultStoreBase(config, storePath);
85
- }
86
-
87
- /**
88
- * Initialize the call manager with a provider.
89
- * Verifies persisted calls with the provider and restarts timers.
90
- */
91
- async initialize(provider: VoiceCallProvider, webhookUrl: string): Promise<void> {
92
- this.provider = provider;
93
- this.webhookUrl = webhookUrl;
94
-
95
- fs.mkdirSync(this.storePath, { recursive: true });
96
-
97
- const persisted = loadActiveCallsFromStore(this.storePath);
98
- this.processedEventIds = persisted.processedEventIds;
99
- this.rejectedProviderCallIds = persisted.rejectedProviderCallIds;
100
-
101
- const verified = await this.verifyRestoredCalls(provider, persisted.activeCalls);
102
- this.activeCalls = verified;
103
-
104
- // Rebuild providerCallIdMap from verified calls only
105
- this.providerCallIdMap = new Map();
106
- for (const [callId, call] of verified) {
107
- if (call.providerCallId) {
108
- this.providerCallIdMap.set(call.providerCallId, callId);
109
- }
110
- }
111
-
112
- // Restart max-duration timers for restored calls that are past the answered state
113
- for (const [callId, call] of verified) {
114
- if (call.answeredAt && !TerminalStates.has(call.state)) {
115
- const elapsed = Date.now() - call.answeredAt;
116
- const maxDurationMs = this.config.maxDurationSeconds * 1000;
117
- if (elapsed >= maxDurationMs) {
118
- // Already expired — remove instead of keeping
119
- verified.delete(callId);
120
- if (call.providerCallId) {
121
- this.providerCallIdMap.delete(call.providerCallId);
122
- }
123
- console.log(
124
- `[voice-call] Skipping restored call ${callId} (max duration already elapsed)`,
125
- );
126
- continue;
127
- }
128
- startMaxDurationTimer({
129
- ctx: this.getContext(),
130
- callId,
131
- timeoutMs: maxDurationMs - elapsed,
132
- onTimeout: async (id) => {
133
- await endCallWithContext(this.getContext(), id, { reason: "timeout" });
134
- },
135
- });
136
- console.log(`[voice-call] Restarted max-duration timer for restored call ${callId}`);
137
- }
138
- }
139
-
140
- if (verified.size > 0) {
141
- console.log(`[voice-call] Restored ${verified.size} active call(s) from store`);
142
- }
143
- }
144
-
145
- /**
146
- * Verify persisted calls with the provider before restoring.
147
- * Calls without providerCallId or older than maxDurationSeconds are skipped.
148
- * Transient provider errors keep the call (rely on timer fallback).
149
- */
150
- private async verifyRestoredCalls(
151
- provider: VoiceCallProvider,
152
- candidates: Map<CallId, CallRecord>,
153
- ): Promise<Map<CallId, CallRecord>> {
154
- if (candidates.size === 0) {
155
- return new Map();
156
- }
157
-
158
- const maxAgeMs = this.config.maxDurationSeconds * 1000;
159
- const now = Date.now();
160
- const verified = new Map<CallId, CallRecord>();
161
- const verifyTasks: Array<{ callId: CallId; call: CallRecord; promise: Promise<void> }> = [];
162
-
163
- for (const [callId, call] of candidates) {
164
- // Skip calls without a provider ID — can't verify
165
- if (!call.providerCallId) {
166
- console.log(`[voice-call] Skipping restored call ${callId} (no providerCallId)`);
167
- continue;
168
- }
169
-
170
- // Skip calls older than maxDurationSeconds (time-based fallback)
171
- if (now - call.startedAt > maxAgeMs) {
172
- console.log(
173
- `[voice-call] Skipping restored call ${callId} (older than maxDurationSeconds)`,
174
- );
175
- markRestoredCallSkipped(call, "timeout");
176
- persistCallRecord(this.storePath, call);
177
- await provider
178
- .hangupCall({
179
- callId,
180
- providerCallId: call.providerCallId,
181
- reason: "timeout",
182
- })
183
- .catch((err) => {
184
- console.warn(
185
- `[voice-call] Failed to hang up expired restored call ${callId}:`,
186
- err instanceof Error ? err.message : String(err),
187
- );
188
- });
189
- continue;
190
- }
191
-
192
- const task = {
193
- callId,
194
- call,
195
- promise: provider
196
- .getCallStatus({ providerCallId: call.providerCallId })
197
- .then((result) => {
198
- if (result.isTerminal) {
199
- console.log(
200
- `[voice-call] Skipping restored call ${callId} (provider status: ${result.status})`,
201
- );
202
- markRestoredCallSkipped(call, "completed");
203
- persistCallRecord(this.storePath, call);
204
- } else if (result.isUnknown) {
205
- console.log(
206
- `[voice-call] Keeping restored call ${callId} (provider status unknown, relying on timer)`,
207
- );
208
- verified.set(callId, call);
209
- } else {
210
- verified.set(callId, call);
211
- }
212
- })
213
- .catch(() => {
214
- // Verification failed entirely — keep the call, rely on timer
215
- console.log(
216
- `[voice-call] Keeping restored call ${callId} (verification failed, relying on timer)`,
217
- );
218
- verified.set(callId, call);
219
- }),
220
- };
221
- verifyTasks.push(task);
222
- }
223
-
224
- await Promise.allSettled(verifyTasks.map((t) => t.promise));
225
- return verified;
226
- }
227
-
228
- /**
229
- * Get the current provider.
230
- */
231
- getProvider(): VoiceCallProvider | null {
232
- return this.provider;
233
- }
234
-
235
- /**
236
- * Initiate an outbound call.
237
- */
238
- async initiateCall(
239
- to: string,
240
- sessionKey?: string,
241
- options?: OutboundCallOptions | string,
242
- ): Promise<{ callId: CallId; success: boolean; error?: string }> {
243
- return initiateCallWithContext(this.getContext(), to, sessionKey, options);
244
- }
245
-
246
- /**
247
- * Speak to user in an active call.
248
- */
249
- async speak(callId: CallId, text: string): Promise<{ success: boolean; error?: string }> {
250
- return speakWithContext(this.getContext(), callId, text);
251
- }
252
-
253
- /**
254
- * Send DTMF digits to an active call.
255
- */
256
- async sendDtmf(callId: CallId, digits: string): Promise<{ success: boolean; error?: string }> {
257
- return sendDtmfWithContext(this.getContext(), callId, digits);
258
- }
259
-
260
- /**
261
- * Speak the initial message for a call (called when media stream connects).
262
- */
263
- async speakInitialMessage(providerCallId: string): Promise<void> {
264
- return speakInitialMessageWithContext(this.getContext(), providerCallId);
265
- }
266
-
267
- /**
268
- * Continue call: speak prompt, then wait for user's final transcript.
269
- */
270
- async continueCall(
271
- callId: CallId,
272
- prompt: string,
273
- ): Promise<{ success: boolean; transcript?: string; error?: string }> {
274
- return continueCallWithContext(this.getContext(), callId, prompt);
275
- }
276
-
277
- /**
278
- * End an active call.
279
- */
280
- async endCall(callId: CallId): Promise<{ success: boolean; error?: string }> {
281
- return endCallWithContext(this.getContext(), callId);
282
- }
283
-
284
- private getContext(): CallManagerContext {
285
- return {
286
- activeCalls: this.activeCalls,
287
- providerCallIdMap: this.providerCallIdMap,
288
- processedEventIds: this.processedEventIds,
289
- rejectedProviderCallIds: this.rejectedProviderCallIds,
290
- provider: this.provider,
291
- config: this.config,
292
- storePath: this.storePath,
293
- webhookUrl: this.webhookUrl,
294
- activeTurnCalls: this.activeTurnCalls,
295
- transcriptWaiters: this.transcriptWaiters,
296
- maxDurationTimers: this.maxDurationTimers,
297
- initialMessageInFlight: this.initialMessageInFlight,
298
- onCallAnswered: (call) => {
299
- this.maybeSpeakInitialMessageOnAnswered(call);
300
- },
301
- };
302
- }
303
-
304
- /**
305
- * Process a webhook event.
306
- */
307
- processEvent(event: NormalizedEvent): void {
308
- processManagerEvent(this.getContext(), event);
309
- }
310
-
311
- private shouldDeferConversationInitialMessageUntilStreamConnect(): boolean {
312
- if (!this.provider || this.provider.name !== "twilio" || !this.config.streaming.enabled) {
313
- return false;
314
- }
315
-
316
- const streamAwareProvider = this.provider as VoiceCallProvider & {
317
- isConversationStreamConnectEnabled?: () => boolean;
318
- };
319
- if (typeof streamAwareProvider.isConversationStreamConnectEnabled !== "function") {
320
- return false;
321
- }
322
-
323
- return streamAwareProvider.isConversationStreamConnectEnabled();
324
- }
325
-
326
- private maybeSpeakInitialMessageOnAnswered(call: CallRecord): void {
327
- const initialMessage = normalizeOptionalString(call.metadata?.initialMessage) ?? "";
328
-
329
- if (!initialMessage) {
330
- return;
331
- }
332
-
333
- // Notify mode should speak as soon as the provider reports "answered".
334
- // Conversation mode should defer only when the Twilio stream-connect path
335
- // is actually available; otherwise speak immediately on answered.
336
- const mode = (call.metadata?.mode as string | undefined) ?? "conversation";
337
- if (mode === "conversation") {
338
- if (this.config.realtime.enabled) {
339
- return;
340
- }
341
- const shouldWaitForStreamConnect =
342
- this.shouldDeferConversationInitialMessageUntilStreamConnect();
343
- if (shouldWaitForStreamConnect) {
344
- return;
345
- }
346
- } else if (mode !== "notify") {
347
- return;
348
- }
349
-
350
- if (!this.provider || !call.providerCallId) {
351
- return;
352
- }
353
-
354
- void this.speakInitialMessage(call.providerCallId).catch((err) => {
355
- console.warn(
356
- `[voice-call] Failed to speak initial message for call ${call.callId}: ${formatErrorMessage(err)}`,
357
- );
358
- });
359
- }
360
-
361
- /**
362
- * Get an active call by ID.
363
- */
364
- getCall(callId: CallId): CallRecord | undefined {
365
- return this.activeCalls.get(callId);
366
- }
367
-
368
- /**
369
- * Get an active call by provider call ID (e.g., Twilio CallSid).
370
- */
371
- getCallByProviderCallId(providerCallId: string): CallRecord | undefined {
372
- return getCallByProviderCallIdFromMaps({
373
- activeCalls: this.activeCalls,
374
- providerCallIdMap: this.providerCallIdMap,
375
- providerCallId,
376
- });
377
- }
378
-
379
- /**
380
- * Get all active calls.
381
- */
382
- getActiveCalls(): CallRecord[] {
383
- return Array.from(this.activeCalls.values());
384
- }
385
-
386
- /**
387
- * Get call history (from persisted logs).
388
- */
389
- async getCallHistory(limit = 50): Promise<CallRecord[]> {
390
- return getCallHistoryFromStore(this.storePath, limit);
391
- }
392
- }