@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.
Files changed (44) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/README.md +135 -0
  3. package/index.ts +497 -0
  4. package/openclaw.plugin.json +601 -0
  5. package/package.json +16 -0
  6. package/src/cli.ts +312 -0
  7. package/src/config.test.ts +204 -0
  8. package/src/config.ts +502 -0
  9. package/src/core-bridge.ts +198 -0
  10. package/src/manager/context.ts +21 -0
  11. package/src/manager/events.ts +177 -0
  12. package/src/manager/lookup.ts +33 -0
  13. package/src/manager/outbound.ts +248 -0
  14. package/src/manager/state.ts +50 -0
  15. package/src/manager/store.ts +88 -0
  16. package/src/manager/timers.ts +86 -0
  17. package/src/manager/twiml.ts +9 -0
  18. package/src/manager.test.ts +108 -0
  19. package/src/manager.ts +888 -0
  20. package/src/media-stream.test.ts +97 -0
  21. package/src/media-stream.ts +393 -0
  22. package/src/providers/base.ts +67 -0
  23. package/src/providers/index.ts +10 -0
  24. package/src/providers/mock.ts +168 -0
  25. package/src/providers/plivo.test.ts +28 -0
  26. package/src/providers/plivo.ts +504 -0
  27. package/src/providers/stt-openai-realtime.ts +311 -0
  28. package/src/providers/telnyx.ts +364 -0
  29. package/src/providers/tts-openai.ts +264 -0
  30. package/src/providers/twilio/api.ts +45 -0
  31. package/src/providers/twilio/webhook.ts +30 -0
  32. package/src/providers/twilio.test.ts +64 -0
  33. package/src/providers/twilio.ts +595 -0
  34. package/src/response-generator.ts +171 -0
  35. package/src/runtime.ts +217 -0
  36. package/src/telephony-audio.ts +88 -0
  37. package/src/telephony-tts.ts +95 -0
  38. package/src/tunnel.ts +331 -0
  39. package/src/types.ts +273 -0
  40. package/src/utils.ts +12 -0
  41. package/src/voice-mapping.ts +65 -0
  42. package/src/webhook-security.test.ts +260 -0
  43. package/src/webhook-security.ts +469 -0
  44. package/src/webhook.ts +491 -0
@@ -0,0 +1,21 @@
1
+ import type { CallId, CallRecord } from "../types.js";
2
+ import type { VoiceCallConfig } from "../config.js";
3
+ import type { VoiceCallProvider } from "../providers/base.js";
4
+
5
+ export type TranscriptWaiter = {
6
+ resolve: (text: string) => void;
7
+ reject: (err: Error) => void;
8
+ timeout: NodeJS.Timeout;
9
+ };
10
+
11
+ export type CallManagerContext = {
12
+ activeCalls: Map<CallId, CallRecord>;
13
+ providerCallIdMap: Map<string, CallId>;
14
+ processedEventIds: Set<string>;
15
+ provider: VoiceCallProvider | null;
16
+ config: VoiceCallConfig;
17
+ storePath: string;
18
+ webhookUrl: string | null;
19
+ transcriptWaiters: Map<CallId, TranscriptWaiter>;
20
+ maxDurationTimers: Map<CallId, NodeJS.Timeout>;
21
+ };
@@ -0,0 +1,177 @@
1
+ import crypto from "node:crypto";
2
+
3
+ import type { CallId, CallRecord, CallState, NormalizedEvent } from "../types.js";
4
+ import { TerminalStates } from "../types.js";
5
+ import type { CallManagerContext } from "./context.js";
6
+ import { findCall } from "./lookup.js";
7
+ import { addTranscriptEntry, transitionState } from "./state.js";
8
+ import { persistCallRecord } from "./store.js";
9
+ import {
10
+ clearMaxDurationTimer,
11
+ rejectTranscriptWaiter,
12
+ resolveTranscriptWaiter,
13
+ startMaxDurationTimer,
14
+ } from "./timers.js";
15
+ import { endCall } from "./outbound.js";
16
+
17
+ function shouldAcceptInbound(config: CallManagerContext["config"], from: string | undefined): boolean {
18
+ const { inboundPolicy: policy, allowFrom } = config;
19
+
20
+ switch (policy) {
21
+ case "disabled":
22
+ console.log("[voice-call] Inbound call rejected: policy is disabled");
23
+ return false;
24
+
25
+ case "open":
26
+ console.log("[voice-call] Inbound call accepted: policy is open");
27
+ return true;
28
+
29
+ case "allowlist":
30
+ case "pairing": {
31
+ const normalized = from?.replace(/\D/g, "") || "";
32
+ const allowed = (allowFrom || []).some((num) => {
33
+ const normalizedAllow = num.replace(/\D/g, "");
34
+ return normalized.endsWith(normalizedAllow) || normalizedAllow.endsWith(normalized);
35
+ });
36
+ const status = allowed ? "accepted" : "rejected";
37
+ console.log(
38
+ `[voice-call] Inbound call ${status}: ${from} ${allowed ? "is in" : "not in"} allowlist`,
39
+ );
40
+ return allowed;
41
+ }
42
+
43
+ default:
44
+ return false;
45
+ }
46
+ }
47
+
48
+ function createInboundCall(params: {
49
+ ctx: CallManagerContext;
50
+ providerCallId: string;
51
+ from: string;
52
+ to: string;
53
+ }): CallRecord {
54
+ const callId = crypto.randomUUID();
55
+
56
+ const callRecord: CallRecord = {
57
+ callId,
58
+ providerCallId: params.providerCallId,
59
+ provider: params.ctx.provider?.name || "twilio",
60
+ direction: "inbound",
61
+ state: "ringing",
62
+ from: params.from,
63
+ to: params.to,
64
+ startedAt: Date.now(),
65
+ transcript: [],
66
+ processedEventIds: [],
67
+ metadata: {
68
+ initialMessage: params.ctx.config.inboundGreeting || "Hello! How can I help you today?",
69
+ },
70
+ };
71
+
72
+ params.ctx.activeCalls.set(callId, callRecord);
73
+ params.ctx.providerCallIdMap.set(params.providerCallId, callId);
74
+ persistCallRecord(params.ctx.storePath, callRecord);
75
+
76
+ console.log(`[voice-call] Created inbound call record: ${callId} from ${params.from}`);
77
+ return callRecord;
78
+ }
79
+
80
+ export function processEvent(ctx: CallManagerContext, event: NormalizedEvent): void {
81
+ if (ctx.processedEventIds.has(event.id)) return;
82
+ ctx.processedEventIds.add(event.id);
83
+
84
+ let call = findCall({
85
+ activeCalls: ctx.activeCalls,
86
+ providerCallIdMap: ctx.providerCallIdMap,
87
+ callIdOrProviderCallId: event.callId,
88
+ });
89
+
90
+ if (!call && event.direction === "inbound" && event.providerCallId) {
91
+ if (!shouldAcceptInbound(ctx.config, event.from)) {
92
+ // TODO: Could hang up the call here.
93
+ return;
94
+ }
95
+
96
+ call = createInboundCall({
97
+ ctx,
98
+ providerCallId: event.providerCallId,
99
+ from: event.from || "unknown",
100
+ to: event.to || ctx.config.fromNumber || "unknown",
101
+ });
102
+
103
+ // Normalize event to internal ID for downstream consumers.
104
+ event.callId = call.callId;
105
+ }
106
+
107
+ if (!call) return;
108
+
109
+ if (event.providerCallId && !call.providerCallId) {
110
+ call.providerCallId = event.providerCallId;
111
+ ctx.providerCallIdMap.set(event.providerCallId, call.callId);
112
+ }
113
+
114
+ call.processedEventIds.push(event.id);
115
+
116
+ switch (event.type) {
117
+ case "call.initiated":
118
+ transitionState(call, "initiated");
119
+ break;
120
+
121
+ case "call.ringing":
122
+ transitionState(call, "ringing");
123
+ break;
124
+
125
+ case "call.answered":
126
+ call.answeredAt = event.timestamp;
127
+ transitionState(call, "answered");
128
+ startMaxDurationTimer({
129
+ ctx,
130
+ callId: call.callId,
131
+ onTimeout: async (callId) => {
132
+ await endCall(ctx, callId);
133
+ },
134
+ });
135
+ break;
136
+
137
+ case "call.active":
138
+ transitionState(call, "active");
139
+ break;
140
+
141
+ case "call.speaking":
142
+ transitionState(call, "speaking");
143
+ break;
144
+
145
+ case "call.speech":
146
+ if (event.isFinal) {
147
+ addTranscriptEntry(call, "user", event.transcript);
148
+ resolveTranscriptWaiter(ctx, call.callId, event.transcript);
149
+ }
150
+ transitionState(call, "listening");
151
+ break;
152
+
153
+ case "call.ended":
154
+ call.endedAt = event.timestamp;
155
+ call.endReason = event.reason;
156
+ transitionState(call, event.reason as CallState);
157
+ clearMaxDurationTimer(ctx, call.callId);
158
+ rejectTranscriptWaiter(ctx, call.callId, `Call ended: ${event.reason}`);
159
+ ctx.activeCalls.delete(call.callId);
160
+ if (call.providerCallId) ctx.providerCallIdMap.delete(call.providerCallId);
161
+ break;
162
+
163
+ case "call.error":
164
+ if (!event.retryable) {
165
+ call.endedAt = event.timestamp;
166
+ call.endReason = "error";
167
+ transitionState(call, "error");
168
+ clearMaxDurationTimer(ctx, call.callId);
169
+ rejectTranscriptWaiter(ctx, call.callId, `Call error: ${event.error}`);
170
+ ctx.activeCalls.delete(call.callId);
171
+ if (call.providerCallId) ctx.providerCallIdMap.delete(call.providerCallId);
172
+ }
173
+ break;
174
+ }
175
+
176
+ persistCallRecord(ctx.storePath, call);
177
+ }
@@ -0,0 +1,33 @@
1
+ import type { CallId, CallRecord } from "../types.js";
2
+
3
+ export function getCallByProviderCallId(params: {
4
+ activeCalls: Map<CallId, CallRecord>;
5
+ providerCallIdMap: Map<string, CallId>;
6
+ providerCallId: string;
7
+ }): CallRecord | undefined {
8
+ const callId = params.providerCallIdMap.get(params.providerCallId);
9
+ if (callId) {
10
+ return params.activeCalls.get(callId);
11
+ }
12
+
13
+ for (const call of params.activeCalls.values()) {
14
+ if (call.providerCallId === params.providerCallId) {
15
+ return call;
16
+ }
17
+ }
18
+ return undefined;
19
+ }
20
+
21
+ export function findCall(params: {
22
+ activeCalls: Map<CallId, CallRecord>;
23
+ providerCallIdMap: Map<string, CallId>;
24
+ callIdOrProviderCallId: string;
25
+ }): CallRecord | undefined {
26
+ const directCall = params.activeCalls.get(params.callIdOrProviderCallId);
27
+ if (directCall) return directCall;
28
+ return getCallByProviderCallId({
29
+ activeCalls: params.activeCalls,
30
+ providerCallIdMap: params.providerCallIdMap,
31
+ providerCallId: params.callIdOrProviderCallId,
32
+ });
33
+ }
@@ -0,0 +1,248 @@
1
+ import crypto from "node:crypto";
2
+
3
+ import { TerminalStates, type CallId, type CallRecord, type OutboundCallOptions } from "../types.js";
4
+ import type { CallMode } from "../config.js";
5
+ import { mapVoiceToPolly } from "../voice-mapping.js";
6
+ import type { CallManagerContext } from "./context.js";
7
+ import { getCallByProviderCallId } from "./lookup.js";
8
+ import { generateNotifyTwiml } from "./twiml.js";
9
+ import { addTranscriptEntry, transitionState } from "./state.js";
10
+ import { persistCallRecord } from "./store.js";
11
+ import { clearMaxDurationTimer, clearTranscriptWaiter, rejectTranscriptWaiter, waitForFinalTranscript } from "./timers.js";
12
+
13
+ export async function initiateCall(
14
+ ctx: CallManagerContext,
15
+ to: string,
16
+ sessionKey?: string,
17
+ options?: OutboundCallOptions | string,
18
+ ): Promise<{ callId: CallId; success: boolean; error?: string }> {
19
+ const opts: OutboundCallOptions =
20
+ typeof options === "string" ? { message: options } : (options ?? {});
21
+ const initialMessage = opts.message;
22
+ const mode = opts.mode ?? ctx.config.outbound.defaultMode;
23
+
24
+ if (!ctx.provider) {
25
+ return { callId: "", success: false, error: "Provider not initialized" };
26
+ }
27
+ if (!ctx.webhookUrl) {
28
+ return { callId: "", success: false, error: "Webhook URL not configured" };
29
+ }
30
+
31
+ if (ctx.activeCalls.size >= ctx.config.maxConcurrentCalls) {
32
+ return {
33
+ callId: "",
34
+ success: false,
35
+ error: `Maximum concurrent calls (${ctx.config.maxConcurrentCalls}) reached`,
36
+ };
37
+ }
38
+
39
+ const callId = crypto.randomUUID();
40
+ const from =
41
+ ctx.config.fromNumber ||
42
+ (ctx.provider?.name === "mock" ? "+15550000000" : undefined);
43
+ if (!from) {
44
+ return { callId: "", success: false, error: "fromNumber not configured" };
45
+ }
46
+
47
+ const callRecord: CallRecord = {
48
+ callId,
49
+ provider: ctx.provider.name,
50
+ direction: "outbound",
51
+ state: "initiated",
52
+ from,
53
+ to,
54
+ sessionKey,
55
+ startedAt: Date.now(),
56
+ transcript: [],
57
+ processedEventIds: [],
58
+ metadata: {
59
+ ...(initialMessage && { initialMessage }),
60
+ mode,
61
+ },
62
+ };
63
+
64
+ ctx.activeCalls.set(callId, callRecord);
65
+ persistCallRecord(ctx.storePath, callRecord);
66
+
67
+ try {
68
+ // For notify mode with a message, use inline TwiML with <Say>.
69
+ let inlineTwiml: string | undefined;
70
+ if (mode === "notify" && initialMessage) {
71
+ const pollyVoice = mapVoiceToPolly(ctx.config.tts?.openai?.voice);
72
+ inlineTwiml = generateNotifyTwiml(initialMessage, pollyVoice);
73
+ console.log(`[voice-call] Using inline TwiML for notify mode (voice: ${pollyVoice})`);
74
+ }
75
+
76
+ const result = await ctx.provider.initiateCall({
77
+ callId,
78
+ from,
79
+ to,
80
+ webhookUrl: ctx.webhookUrl,
81
+ inlineTwiml,
82
+ });
83
+
84
+ callRecord.providerCallId = result.providerCallId;
85
+ ctx.providerCallIdMap.set(result.providerCallId, callId);
86
+ persistCallRecord(ctx.storePath, callRecord);
87
+
88
+ return { callId, success: true };
89
+ } catch (err) {
90
+ callRecord.state = "failed";
91
+ callRecord.endedAt = Date.now();
92
+ callRecord.endReason = "failed";
93
+ persistCallRecord(ctx.storePath, callRecord);
94
+ ctx.activeCalls.delete(callId);
95
+ if (callRecord.providerCallId) {
96
+ ctx.providerCallIdMap.delete(callRecord.providerCallId);
97
+ }
98
+
99
+ return {
100
+ callId,
101
+ success: false,
102
+ error: err instanceof Error ? err.message : String(err),
103
+ };
104
+ }
105
+ }
106
+
107
+ export async function speak(
108
+ ctx: CallManagerContext,
109
+ callId: CallId,
110
+ text: string,
111
+ ): Promise<{ success: boolean; error?: string }> {
112
+ const call = ctx.activeCalls.get(callId);
113
+ if (!call) return { success: false, error: "Call not found" };
114
+ if (!ctx.provider || !call.providerCallId) return { success: false, error: "Call not connected" };
115
+ if (TerminalStates.has(call.state)) return { success: false, error: "Call has ended" };
116
+
117
+ try {
118
+ transitionState(call, "speaking");
119
+ persistCallRecord(ctx.storePath, call);
120
+
121
+ addTranscriptEntry(call, "bot", text);
122
+
123
+ const voice =
124
+ ctx.provider?.name === "twilio" ? ctx.config.tts?.openai?.voice : undefined;
125
+ await ctx.provider.playTts({
126
+ callId,
127
+ providerCallId: call.providerCallId,
128
+ text,
129
+ voice,
130
+ });
131
+
132
+ return { success: true };
133
+ } catch (err) {
134
+ return { success: false, error: err instanceof Error ? err.message : String(err) };
135
+ }
136
+ }
137
+
138
+ export async function speakInitialMessage(
139
+ ctx: CallManagerContext,
140
+ providerCallId: string,
141
+ ): Promise<void> {
142
+ const call = getCallByProviderCallId({
143
+ activeCalls: ctx.activeCalls,
144
+ providerCallIdMap: ctx.providerCallIdMap,
145
+ providerCallId,
146
+ });
147
+ if (!call) {
148
+ console.warn(`[voice-call] speakInitialMessage: no call found for ${providerCallId}`);
149
+ return;
150
+ }
151
+
152
+ const initialMessage = call.metadata?.initialMessage as string | undefined;
153
+ const mode = (call.metadata?.mode as CallMode) ?? "conversation";
154
+
155
+ if (!initialMessage) {
156
+ console.log(`[voice-call] speakInitialMessage: no initial message for ${call.callId}`);
157
+ return;
158
+ }
159
+
160
+ // Clear so we don't speak it again if the provider reconnects.
161
+ if (call.metadata) {
162
+ delete call.metadata.initialMessage;
163
+ persistCallRecord(ctx.storePath, call);
164
+ }
165
+
166
+ console.log(`[voice-call] Speaking initial message for call ${call.callId} (mode: ${mode})`);
167
+ const result = await speak(ctx, call.callId, initialMessage);
168
+ if (!result.success) {
169
+ console.warn(`[voice-call] Failed to speak initial message: ${result.error}`);
170
+ return;
171
+ }
172
+
173
+ if (mode === "notify") {
174
+ const delaySec = ctx.config.outbound.notifyHangupDelaySec;
175
+ console.log(`[voice-call] Notify mode: auto-hangup in ${delaySec}s for call ${call.callId}`);
176
+ setTimeout(async () => {
177
+ const currentCall = ctx.activeCalls.get(call.callId);
178
+ if (currentCall && !TerminalStates.has(currentCall.state)) {
179
+ console.log(`[voice-call] Notify mode: hanging up call ${call.callId}`);
180
+ await endCall(ctx, call.callId);
181
+ }
182
+ }, delaySec * 1000);
183
+ }
184
+ }
185
+
186
+ export async function continueCall(
187
+ ctx: CallManagerContext,
188
+ callId: CallId,
189
+ prompt: string,
190
+ ): Promise<{ success: boolean; transcript?: string; error?: string }> {
191
+ const call = ctx.activeCalls.get(callId);
192
+ if (!call) return { success: false, error: "Call not found" };
193
+ if (!ctx.provider || !call.providerCallId) return { success: false, error: "Call not connected" };
194
+ if (TerminalStates.has(call.state)) return { success: false, error: "Call has ended" };
195
+
196
+ try {
197
+ await speak(ctx, callId, prompt);
198
+
199
+ transitionState(call, "listening");
200
+ persistCallRecord(ctx.storePath, call);
201
+
202
+ await ctx.provider.startListening({ callId, providerCallId: call.providerCallId });
203
+
204
+ const transcript = await waitForFinalTranscript(ctx, callId);
205
+
206
+ // Best-effort: stop listening after final transcript.
207
+ await ctx.provider.stopListening({ callId, providerCallId: call.providerCallId });
208
+
209
+ return { success: true, transcript };
210
+ } catch (err) {
211
+ return { success: false, error: err instanceof Error ? err.message : String(err) };
212
+ } finally {
213
+ clearTranscriptWaiter(ctx, callId);
214
+ }
215
+ }
216
+
217
+ export async function endCall(
218
+ ctx: CallManagerContext,
219
+ callId: CallId,
220
+ ): Promise<{ success: boolean; error?: string }> {
221
+ const call = ctx.activeCalls.get(callId);
222
+ if (!call) return { success: false, error: "Call not found" };
223
+ if (!ctx.provider || !call.providerCallId) return { success: false, error: "Call not connected" };
224
+ if (TerminalStates.has(call.state)) return { success: true };
225
+
226
+ try {
227
+ await ctx.provider.hangupCall({
228
+ callId,
229
+ providerCallId: call.providerCallId,
230
+ reason: "hangup-bot",
231
+ });
232
+
233
+ call.state = "hangup-bot";
234
+ call.endedAt = Date.now();
235
+ call.endReason = "hangup-bot";
236
+ persistCallRecord(ctx.storePath, call);
237
+
238
+ clearMaxDurationTimer(ctx, callId);
239
+ rejectTranscriptWaiter(ctx, callId, "Call ended: hangup-bot");
240
+
241
+ ctx.activeCalls.delete(callId);
242
+ if (call.providerCallId) ctx.providerCallIdMap.delete(call.providerCallId);
243
+
244
+ return { success: true };
245
+ } catch (err) {
246
+ return { success: false, error: err instanceof Error ? err.message : String(err) };
247
+ }
248
+ }
@@ -0,0 +1,50 @@
1
+ import { TerminalStates, type CallRecord, type CallState, type TranscriptEntry } from "../types.js";
2
+
3
+ const ConversationStates = new Set<CallState>(["speaking", "listening"]);
4
+
5
+ const StateOrder: readonly CallState[] = [
6
+ "initiated",
7
+ "ringing",
8
+ "answered",
9
+ "active",
10
+ "speaking",
11
+ "listening",
12
+ ];
13
+
14
+ export function transitionState(call: CallRecord, newState: CallState): void {
15
+ // No-op for same state or already terminal.
16
+ if (call.state === newState || TerminalStates.has(call.state)) return;
17
+
18
+ // Terminal states can always be reached from non-terminal.
19
+ if (TerminalStates.has(newState)) {
20
+ call.state = newState;
21
+ return;
22
+ }
23
+
24
+ // Allow cycling between speaking and listening (multi-turn conversations).
25
+ if (ConversationStates.has(call.state) && ConversationStates.has(newState)) {
26
+ call.state = newState;
27
+ return;
28
+ }
29
+
30
+ // Only allow forward transitions in state order.
31
+ const currentIndex = StateOrder.indexOf(call.state);
32
+ const newIndex = StateOrder.indexOf(newState);
33
+ if (newIndex > currentIndex) {
34
+ call.state = newState;
35
+ }
36
+ }
37
+
38
+ export function addTranscriptEntry(
39
+ call: CallRecord,
40
+ speaker: "bot" | "user",
41
+ text: string,
42
+ ): void {
43
+ const entry: TranscriptEntry = {
44
+ timestamp: Date.now(),
45
+ speaker,
46
+ text,
47
+ isFinal: true,
48
+ };
49
+ call.transcript.push(entry);
50
+ }
@@ -0,0 +1,88 @@
1
+ import fs from "node:fs";
2
+ import fsp from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ import { CallRecordSchema, TerminalStates, type CallId, type CallRecord } from "../types.js";
6
+
7
+ export function persistCallRecord(storePath: string, call: CallRecord): void {
8
+ const logPath = path.join(storePath, "calls.jsonl");
9
+ const line = `${JSON.stringify(call)}\n`;
10
+ // Fire-and-forget async write to avoid blocking event loop.
11
+ fsp.appendFile(logPath, line).catch((err) => {
12
+ console.error("[voice-call] Failed to persist call record:", err);
13
+ });
14
+ }
15
+
16
+ export function loadActiveCallsFromStore(storePath: string): {
17
+ activeCalls: Map<CallId, CallRecord>;
18
+ providerCallIdMap: Map<string, CallId>;
19
+ processedEventIds: Set<string>;
20
+ } {
21
+ const logPath = path.join(storePath, "calls.jsonl");
22
+ if (!fs.existsSync(logPath)) {
23
+ return {
24
+ activeCalls: new Map(),
25
+ providerCallIdMap: new Map(),
26
+ processedEventIds: new Set(),
27
+ };
28
+ }
29
+
30
+ const content = fs.readFileSync(logPath, "utf-8");
31
+ const lines = content.split("\n");
32
+
33
+ const callMap = new Map<CallId, CallRecord>();
34
+ for (const line of lines) {
35
+ if (!line.trim()) continue;
36
+ try {
37
+ const call = CallRecordSchema.parse(JSON.parse(line));
38
+ callMap.set(call.callId, call);
39
+ } catch {
40
+ // Skip invalid lines.
41
+ }
42
+ }
43
+
44
+ const activeCalls = new Map<CallId, CallRecord>();
45
+ const providerCallIdMap = new Map<string, CallId>();
46
+ const processedEventIds = new Set<string>();
47
+
48
+ for (const [callId, call] of callMap) {
49
+ if (TerminalStates.has(call.state)) continue;
50
+ activeCalls.set(callId, call);
51
+ if (call.providerCallId) {
52
+ providerCallIdMap.set(call.providerCallId, callId);
53
+ }
54
+ for (const eventId of call.processedEventIds) {
55
+ processedEventIds.add(eventId);
56
+ }
57
+ }
58
+
59
+ return { activeCalls, providerCallIdMap, processedEventIds };
60
+ }
61
+
62
+ export async function getCallHistoryFromStore(
63
+ storePath: string,
64
+ limit = 50,
65
+ ): Promise<CallRecord[]> {
66
+ const logPath = path.join(storePath, "calls.jsonl");
67
+
68
+ try {
69
+ await fsp.access(logPath);
70
+ } catch {
71
+ return [];
72
+ }
73
+
74
+ const content = await fsp.readFile(logPath, "utf-8");
75
+ const lines = content.trim().split("\n").filter(Boolean);
76
+ const calls: CallRecord[] = [];
77
+
78
+ for (const line of lines.slice(-limit)) {
79
+ try {
80
+ const parsed = CallRecordSchema.parse(JSON.parse(line));
81
+ calls.push(parsed);
82
+ } catch {
83
+ // Skip invalid lines.
84
+ }
85
+ }
86
+
87
+ return calls;
88
+ }
@@ -0,0 +1,86 @@
1
+ import { TerminalStates, type CallId } from "../types.js";
2
+ import type { CallManagerContext } from "./context.js";
3
+ import { persistCallRecord } from "./store.js";
4
+
5
+ export function clearMaxDurationTimer(ctx: CallManagerContext, callId: CallId): void {
6
+ const timer = ctx.maxDurationTimers.get(callId);
7
+ if (timer) {
8
+ clearTimeout(timer);
9
+ ctx.maxDurationTimers.delete(callId);
10
+ }
11
+ }
12
+
13
+ export function startMaxDurationTimer(params: {
14
+ ctx: CallManagerContext;
15
+ callId: CallId;
16
+ onTimeout: (callId: CallId) => Promise<void>;
17
+ }): void {
18
+ clearMaxDurationTimer(params.ctx, params.callId);
19
+
20
+ const maxDurationMs = params.ctx.config.maxDurationSeconds * 1000;
21
+ console.log(
22
+ `[voice-call] Starting max duration timer (${params.ctx.config.maxDurationSeconds}s) for call ${params.callId}`,
23
+ );
24
+
25
+ const timer = setTimeout(async () => {
26
+ params.ctx.maxDurationTimers.delete(params.callId);
27
+ const call = params.ctx.activeCalls.get(params.callId);
28
+ if (call && !TerminalStates.has(call.state)) {
29
+ console.log(
30
+ `[voice-call] Max duration reached (${params.ctx.config.maxDurationSeconds}s), ending call ${params.callId}`,
31
+ );
32
+ call.endReason = "timeout";
33
+ persistCallRecord(params.ctx.storePath, call);
34
+ await params.onTimeout(params.callId);
35
+ }
36
+ }, maxDurationMs);
37
+
38
+ params.ctx.maxDurationTimers.set(params.callId, timer);
39
+ }
40
+
41
+ export function clearTranscriptWaiter(ctx: CallManagerContext, callId: CallId): void {
42
+ const waiter = ctx.transcriptWaiters.get(callId);
43
+ if (!waiter) return;
44
+ clearTimeout(waiter.timeout);
45
+ ctx.transcriptWaiters.delete(callId);
46
+ }
47
+
48
+ export function rejectTranscriptWaiter(
49
+ ctx: CallManagerContext,
50
+ callId: CallId,
51
+ reason: string,
52
+ ): void {
53
+ const waiter = ctx.transcriptWaiters.get(callId);
54
+ if (!waiter) return;
55
+ clearTranscriptWaiter(ctx, callId);
56
+ waiter.reject(new Error(reason));
57
+ }
58
+
59
+ export function resolveTranscriptWaiter(
60
+ ctx: CallManagerContext,
61
+ callId: CallId,
62
+ transcript: string,
63
+ ): void {
64
+ const waiter = ctx.transcriptWaiters.get(callId);
65
+ if (!waiter) return;
66
+ clearTranscriptWaiter(ctx, callId);
67
+ waiter.resolve(transcript);
68
+ }
69
+
70
+ export function waitForFinalTranscript(
71
+ ctx: CallManagerContext,
72
+ callId: CallId,
73
+ ): Promise<string> {
74
+ // Only allow one in-flight waiter per call.
75
+ rejectTranscriptWaiter(ctx, callId, "Transcript waiter replaced");
76
+
77
+ const timeoutMs = ctx.config.transcriptTimeoutMs;
78
+ return new Promise((resolve, reject) => {
79
+ const timeout = setTimeout(() => {
80
+ ctx.transcriptWaiters.delete(callId);
81
+ reject(new Error(`Timed out waiting for transcript after ${timeoutMs}ms`));
82
+ }, timeoutMs);
83
+
84
+ ctx.transcriptWaiters.set(callId, { resolve, reject, timeout });
85
+ });
86
+ }