@openclaw/voice-call 2026.3.13 → 2026.5.2-beta.1
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/README.md +27 -5
- package/api.ts +16 -0
- package/cli-metadata.ts +10 -0
- package/config-api.ts +12 -0
- package/index.test.ts +943 -0
- package/index.ts +379 -149
- package/openclaw.plugin.json +384 -157
- package/package.json +35 -5
- package/runtime-api.ts +20 -0
- package/runtime-entry.ts +1 -0
- package/setup-api.ts +47 -0
- package/src/allowlist.test.ts +18 -0
- package/src/cli.ts +533 -68
- package/src/config-compat.test.ts +120 -0
- package/src/config-compat.ts +227 -0
- package/src/config.test.ts +273 -12
- package/src/config.ts +355 -72
- package/src/core-bridge.ts +2 -147
- package/src/deep-merge.test.ts +40 -0
- package/src/gateway-continue-operation.ts +200 -0
- package/src/http-headers.ts +6 -3
- package/src/manager/context.ts +6 -5
- package/src/manager/events.test.ts +243 -19
- package/src/manager/events.ts +61 -31
- package/src/manager/lifecycle.ts +53 -0
- package/src/manager/lookup.test.ts +52 -0
- package/src/manager/outbound.test.ts +528 -0
- package/src/manager/outbound.ts +163 -57
- package/src/manager/store.ts +18 -6
- package/src/manager/timers.test.ts +129 -0
- package/src/manager/timers.ts +4 -3
- package/src/manager/twiml.test.ts +13 -0
- package/src/manager/twiml.ts +8 -0
- package/src/manager.closed-loop.test.ts +30 -12
- package/src/manager.inbound-allowlist.test.ts +77 -10
- package/src/manager.notify.test.ts +344 -20
- package/src/manager.restore.test.ts +95 -8
- package/src/manager.test-harness.ts +8 -6
- package/src/manager.ts +79 -5
- package/src/media-stream.test.ts +578 -81
- package/src/media-stream.ts +235 -54
- package/src/providers/base.ts +19 -0
- package/src/providers/mock.ts +7 -1
- package/src/providers/plivo.test.ts +50 -6
- package/src/providers/plivo.ts +14 -6
- package/src/providers/shared/call-status.ts +2 -1
- package/src/providers/shared/guarded-json-api.test.ts +106 -0
- package/src/providers/shared/guarded-json-api.ts +1 -1
- package/src/providers/telnyx.test.ts +178 -6
- package/src/providers/telnyx.ts +40 -3
- package/src/providers/twilio/api.test.ts +145 -0
- package/src/providers/twilio/api.ts +67 -16
- package/src/providers/twilio/twiml-policy.ts +6 -10
- package/src/providers/twilio/webhook.ts +1 -1
- package/src/providers/twilio.test.ts +425 -25
- package/src/providers/twilio.ts +230 -77
- package/src/providers/twilio.types.ts +17 -0
- package/src/realtime-defaults.ts +3 -0
- package/src/realtime-fast-context.test.ts +88 -0
- package/src/realtime-fast-context.ts +165 -0
- package/src/realtime-transcription.runtime.ts +4 -0
- package/src/realtime-voice.runtime.ts +5 -0
- package/src/response-generator.test.ts +321 -0
- package/src/response-generator.ts +213 -53
- package/src/response-model.test.ts +71 -0
- package/src/response-model.ts +23 -0
- package/src/runtime.test.ts +429 -0
- package/src/runtime.ts +270 -24
- package/src/telephony-audio.test.ts +61 -0
- package/src/telephony-audio.ts +1 -79
- package/src/telephony-tts.test.ts +133 -12
- package/src/telephony-tts.ts +155 -2
- package/src/test-fixtures.ts +28 -7
- package/src/tts-provider-voice.test.ts +34 -0
- package/src/tts-provider-voice.ts +21 -0
- package/src/tunnel.test.ts +166 -0
- package/src/tunnel.ts +1 -1
- package/src/types.ts +24 -37
- package/src/utils.test.ts +17 -0
- package/src/voice-mapping.test.ts +34 -0
- package/src/voice-mapping.ts +3 -2
- package/src/webhook/realtime-handler.test.ts +598 -0
- package/src/webhook/realtime-handler.ts +485 -0
- package/src/webhook/stale-call-reaper.test.ts +88 -0
- package/src/webhook/stale-call-reaper.ts +5 -0
- package/src/webhook/tailscale.test.ts +214 -0
- package/src/webhook/tailscale.ts +19 -5
- package/src/webhook-exposure.test.ts +33 -0
- package/src/webhook-exposure.ts +84 -0
- package/src/webhook-security.test.ts +172 -21
- package/src/webhook-security.ts +43 -29
- package/src/webhook.hangup-once.lifecycle.test.ts +135 -0
- package/src/webhook.test.ts +1145 -27
- package/src/webhook.ts +523 -102
- package/src/webhook.types.ts +5 -0
- package/src/websocket-test-support.ts +72 -0
- package/tsconfig.json +16 -0
- package/CHANGELOG.md +0 -121
- package/src/providers/index.ts +0 -10
- package/src/providers/stt-openai-realtime.test.ts +0 -42
- package/src/providers/stt-openai-realtime.ts +0 -311
- package/src/providers/tts-openai.test.ts +0 -43
- package/src/providers/tts-openai.ts +0 -221
package/src/runtime.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
1
2
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
3
|
import type { VoiceCallConfig } from "./config.js";
|
|
3
4
|
import type { CoreConfig } from "./core-bridge.js";
|
|
@@ -5,35 +6,88 @@ import { createVoiceCallBaseConfig } from "./test-fixtures.js";
|
|
|
5
6
|
|
|
6
7
|
const mocks = vi.hoisted(() => ({
|
|
7
8
|
resolveVoiceCallConfig: vi.fn(),
|
|
9
|
+
resolveTwilioAuthToken: vi.fn(),
|
|
8
10
|
validateProviderConfig: vi.fn(),
|
|
9
11
|
managerInitialize: vi.fn(),
|
|
12
|
+
managerGetCall: vi.fn(),
|
|
10
13
|
webhookStart: vi.fn(),
|
|
11
14
|
webhookStop: vi.fn(),
|
|
15
|
+
webhookSetRealtimeHandler: vi.fn(),
|
|
16
|
+
webhookGetRealtimeHandler: vi.fn(),
|
|
12
17
|
webhookGetMediaStreamHandler: vi.fn(),
|
|
18
|
+
webhookCtorArgs: [] as unknown[][],
|
|
19
|
+
realtimeHandlerCtorArgs: [] as unknown[][],
|
|
20
|
+
realtimeHandlerRegisterToolHandler: vi.fn(),
|
|
21
|
+
realtimeHandlerSetPublicUrl: vi.fn(),
|
|
22
|
+
resolveConfiguredRealtimeVoiceProvider: vi.fn(),
|
|
23
|
+
getActiveMemorySearchManager: vi.fn(),
|
|
24
|
+
memorySearch: vi.fn(),
|
|
13
25
|
startTunnel: vi.fn(),
|
|
14
26
|
setupTailscaleExposure: vi.fn(),
|
|
15
27
|
cleanupTailscaleExposure: vi.fn(),
|
|
16
28
|
}));
|
|
17
29
|
|
|
18
30
|
vi.mock("./config.js", () => ({
|
|
31
|
+
resolveVoiceCallSessionKey: (params: {
|
|
32
|
+
config: Pick<VoiceCallConfig, "sessionScope">;
|
|
33
|
+
callId: string;
|
|
34
|
+
phone?: string;
|
|
35
|
+
explicitSessionKey?: string;
|
|
36
|
+
}) => {
|
|
37
|
+
const explicit = params.explicitSessionKey?.trim();
|
|
38
|
+
if (explicit) {
|
|
39
|
+
return explicit;
|
|
40
|
+
}
|
|
41
|
+
if (params.config.sessionScope === "per-call") {
|
|
42
|
+
return `voice:call:${params.callId}`;
|
|
43
|
+
}
|
|
44
|
+
const normalizedPhone = params.phone?.replace(/\D/g, "");
|
|
45
|
+
return normalizedPhone ? `voice:${normalizedPhone}` : `voice:${params.callId}`;
|
|
46
|
+
},
|
|
47
|
+
resolveVoiceCallEffectiveConfig: (config: VoiceCallConfig) => ({ config }),
|
|
19
48
|
resolveVoiceCallConfig: mocks.resolveVoiceCallConfig,
|
|
49
|
+
resolveTwilioAuthToken: mocks.resolveTwilioAuthToken,
|
|
20
50
|
validateProviderConfig: mocks.validateProviderConfig,
|
|
21
51
|
}));
|
|
22
52
|
|
|
23
53
|
vi.mock("./manager.js", () => ({
|
|
24
54
|
CallManager: class {
|
|
25
55
|
initialize = mocks.managerInitialize;
|
|
56
|
+
getCall = mocks.managerGetCall;
|
|
26
57
|
},
|
|
27
58
|
}));
|
|
28
59
|
|
|
29
60
|
vi.mock("./webhook.js", () => ({
|
|
30
61
|
VoiceCallWebhookServer: class {
|
|
62
|
+
constructor(...args: unknown[]) {
|
|
63
|
+
mocks.webhookCtorArgs.push(args);
|
|
64
|
+
}
|
|
31
65
|
start = mocks.webhookStart;
|
|
32
66
|
stop = mocks.webhookStop;
|
|
67
|
+
setRealtimeHandler = mocks.webhookSetRealtimeHandler;
|
|
68
|
+
getRealtimeHandler = mocks.webhookGetRealtimeHandler;
|
|
33
69
|
getMediaStreamHandler = mocks.webhookGetMediaStreamHandler;
|
|
34
70
|
},
|
|
35
71
|
}));
|
|
36
72
|
|
|
73
|
+
vi.mock("./realtime-voice.runtime.js", () => ({
|
|
74
|
+
resolveConfiguredRealtimeVoiceProvider: mocks.resolveConfiguredRealtimeVoiceProvider,
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
vi.mock("./webhook/realtime-handler.js", () => ({
|
|
78
|
+
RealtimeCallHandler: class {
|
|
79
|
+
constructor(...args: unknown[]) {
|
|
80
|
+
mocks.realtimeHandlerCtorArgs.push(args);
|
|
81
|
+
}
|
|
82
|
+
registerToolHandler = mocks.realtimeHandlerRegisterToolHandler;
|
|
83
|
+
setPublicUrl = mocks.realtimeHandlerSetPublicUrl;
|
|
84
|
+
},
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
vi.mock("openclaw/plugin-sdk/memory-host-search", () => ({
|
|
88
|
+
getActiveMemorySearchManager: mocks.getActiveMemorySearchManager,
|
|
89
|
+
}));
|
|
90
|
+
|
|
37
91
|
vi.mock("./tunnel.js", () => ({
|
|
38
92
|
startTunnel: mocks.startTunnel,
|
|
39
93
|
}));
|
|
@@ -49,15 +103,66 @@ function createBaseConfig(): VoiceCallConfig {
|
|
|
49
103
|
return createVoiceCallBaseConfig({ tunnelProvider: "ngrok" });
|
|
50
104
|
}
|
|
51
105
|
|
|
106
|
+
function createExternalProviderConfig(params: {
|
|
107
|
+
provider: "twilio" | "telnyx" | "plivo";
|
|
108
|
+
publicUrl?: string;
|
|
109
|
+
}): VoiceCallConfig {
|
|
110
|
+
const config = createVoiceCallBaseConfig({
|
|
111
|
+
provider: params.provider,
|
|
112
|
+
tunnelProvider: "none",
|
|
113
|
+
});
|
|
114
|
+
config.twilio = {
|
|
115
|
+
accountSid: "AC123",
|
|
116
|
+
authToken: "secret",
|
|
117
|
+
};
|
|
118
|
+
config.telnyx = {
|
|
119
|
+
apiKey: "key",
|
|
120
|
+
connectionId: "conn",
|
|
121
|
+
publicKey: "pub",
|
|
122
|
+
};
|
|
123
|
+
config.plivo = {
|
|
124
|
+
authId: "MA123",
|
|
125
|
+
authToken: "secret",
|
|
126
|
+
};
|
|
127
|
+
if (params.publicUrl) {
|
|
128
|
+
config.publicUrl = params.publicUrl;
|
|
129
|
+
}
|
|
130
|
+
return config;
|
|
131
|
+
}
|
|
132
|
+
|
|
52
133
|
describe("createVoiceCallRuntime lifecycle", () => {
|
|
53
134
|
beforeEach(() => {
|
|
54
135
|
vi.clearAllMocks();
|
|
55
136
|
mocks.resolveVoiceCallConfig.mockImplementation((cfg: VoiceCallConfig) => cfg);
|
|
137
|
+
mocks.resolveTwilioAuthToken.mockImplementation(
|
|
138
|
+
(cfg: VoiceCallConfig) => cfg.twilio?.authToken,
|
|
139
|
+
);
|
|
56
140
|
mocks.validateProviderConfig.mockReturnValue({ valid: true, errors: [] });
|
|
57
141
|
mocks.managerInitialize.mockResolvedValue(undefined);
|
|
142
|
+
mocks.managerGetCall.mockReset();
|
|
58
143
|
mocks.webhookStart.mockResolvedValue("http://127.0.0.1:3334/voice/webhook");
|
|
59
144
|
mocks.webhookStop.mockResolvedValue(undefined);
|
|
145
|
+
mocks.webhookSetRealtimeHandler.mockReset();
|
|
146
|
+
mocks.webhookGetRealtimeHandler.mockReturnValue({
|
|
147
|
+
setPublicUrl: mocks.realtimeHandlerSetPublicUrl,
|
|
148
|
+
});
|
|
60
149
|
mocks.webhookGetMediaStreamHandler.mockReturnValue(undefined);
|
|
150
|
+
mocks.webhookCtorArgs.length = 0;
|
|
151
|
+
mocks.realtimeHandlerCtorArgs.length = 0;
|
|
152
|
+
mocks.realtimeHandlerRegisterToolHandler.mockReset();
|
|
153
|
+
mocks.realtimeHandlerSetPublicUrl.mockReset();
|
|
154
|
+
mocks.resolveConfiguredRealtimeVoiceProvider.mockResolvedValue({
|
|
155
|
+
provider: { id: "openai" },
|
|
156
|
+
providerConfig: { model: "gpt-realtime" },
|
|
157
|
+
});
|
|
158
|
+
mocks.getActiveMemorySearchManager.mockReset();
|
|
159
|
+
mocks.memorySearch.mockReset();
|
|
160
|
+
mocks.getActiveMemorySearchManager.mockResolvedValue({
|
|
161
|
+
manager: {
|
|
162
|
+
search: mocks.memorySearch,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
mocks.memorySearch.mockResolvedValue([]);
|
|
61
166
|
mocks.startTunnel.mockResolvedValue(null);
|
|
62
167
|
mocks.setupTailscaleExposure.mockResolvedValue(null);
|
|
63
168
|
mocks.cleanupTailscaleExposure.mockResolvedValue(undefined);
|
|
@@ -76,6 +181,7 @@ describe("createVoiceCallRuntime lifecycle", () => {
|
|
|
76
181
|
createVoiceCallRuntime({
|
|
77
182
|
config: createBaseConfig(),
|
|
78
183
|
coreConfig: {},
|
|
184
|
+
agentRuntime: {} as never,
|
|
79
185
|
}),
|
|
80
186
|
).rejects.toThrow("init failed");
|
|
81
187
|
|
|
@@ -95,6 +201,7 @@ describe("createVoiceCallRuntime lifecycle", () => {
|
|
|
95
201
|
const runtime = await createVoiceCallRuntime({
|
|
96
202
|
config: createBaseConfig(),
|
|
97
203
|
coreConfig: {} as CoreConfig,
|
|
204
|
+
agentRuntime: {} as never,
|
|
98
205
|
});
|
|
99
206
|
|
|
100
207
|
await runtime.stop();
|
|
@@ -104,4 +211,326 @@ describe("createVoiceCallRuntime lifecycle", () => {
|
|
|
104
211
|
expect(mocks.cleanupTailscaleExposure).toHaveBeenCalledTimes(1);
|
|
105
212
|
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
106
213
|
});
|
|
214
|
+
|
|
215
|
+
it("passes fullConfig to the webhook server for streaming provider resolution", async () => {
|
|
216
|
+
const coreConfig = { messages: { tts: { provider: "openai" } } } as CoreConfig;
|
|
217
|
+
const fullConfig = {
|
|
218
|
+
plugins: {
|
|
219
|
+
entries: {
|
|
220
|
+
openai: { enabled: true },
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
} as OpenClawConfig;
|
|
224
|
+
|
|
225
|
+
await createVoiceCallRuntime({
|
|
226
|
+
config: createBaseConfig(),
|
|
227
|
+
coreConfig,
|
|
228
|
+
fullConfig,
|
|
229
|
+
agentRuntime: {} as never,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
expect(mocks.webhookCtorArgs[0]?.[3]).toBe(coreConfig);
|
|
233
|
+
expect(mocks.webhookCtorArgs[0]?.[4]).toBe(fullConfig);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it.each(["twilio", "telnyx", "plivo"] as const)(
|
|
237
|
+
"fails closed when %s falls back to a local-only webhook",
|
|
238
|
+
async (provider) => {
|
|
239
|
+
await expect(
|
|
240
|
+
createVoiceCallRuntime({
|
|
241
|
+
config: createExternalProviderConfig({ provider }),
|
|
242
|
+
coreConfig: {} as CoreConfig,
|
|
243
|
+
agentRuntime: {} as never,
|
|
244
|
+
}),
|
|
245
|
+
).rejects.toThrow(`${provider} requires a publicly reachable webhook URL`);
|
|
246
|
+
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
it.each([
|
|
251
|
+
"http://127.0.0.1:3334/voice/webhook",
|
|
252
|
+
"http://[::1]:3334/voice/webhook",
|
|
253
|
+
"http://[fd00::1]/voice/webhook",
|
|
254
|
+
])("fails closed when Twilio publicUrl %s points at a local-only webhook", async (publicUrl) => {
|
|
255
|
+
await expect(
|
|
256
|
+
createVoiceCallRuntime({
|
|
257
|
+
config: createExternalProviderConfig({
|
|
258
|
+
provider: "twilio",
|
|
259
|
+
publicUrl,
|
|
260
|
+
}),
|
|
261
|
+
coreConfig: {} as CoreConfig,
|
|
262
|
+
agentRuntime: {} as never,
|
|
263
|
+
}),
|
|
264
|
+
).rejects.toThrow("twilio requires a publicly reachable webhook URL");
|
|
265
|
+
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("accepts an explicit public URL for external voice providers", async () => {
|
|
269
|
+
const runtime = await createVoiceCallRuntime({
|
|
270
|
+
config: createExternalProviderConfig({
|
|
271
|
+
provider: "twilio",
|
|
272
|
+
publicUrl: "https://voice.example.com/voice/webhook",
|
|
273
|
+
}),
|
|
274
|
+
coreConfig: {} as CoreConfig,
|
|
275
|
+
agentRuntime: {} as never,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
expect(runtime.webhookUrl).toBe("https://voice.example.com/voice/webhook");
|
|
279
|
+
expect(runtime.publicUrl).toBe("https://voice.example.com/voice/webhook");
|
|
280
|
+
|
|
281
|
+
await runtime.stop();
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("does not log duplicate webhook and public URLs when they match", async () => {
|
|
285
|
+
const logger = {
|
|
286
|
+
info: vi.fn(),
|
|
287
|
+
warn: vi.fn(),
|
|
288
|
+
error: vi.fn(),
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const runtime = await createVoiceCallRuntime({
|
|
292
|
+
config: createExternalProviderConfig({
|
|
293
|
+
provider: "twilio",
|
|
294
|
+
publicUrl: "https://voice.example.com/voice/webhook",
|
|
295
|
+
}),
|
|
296
|
+
coreConfig: {} as CoreConfig,
|
|
297
|
+
agentRuntime: {} as never,
|
|
298
|
+
logger,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(logger.info).toHaveBeenCalledWith(
|
|
302
|
+
"[voice-call] Webhook URL: https://voice.example.com/voice/webhook",
|
|
303
|
+
);
|
|
304
|
+
expect(logger.info).not.toHaveBeenCalledWith(
|
|
305
|
+
"[voice-call] Public URL: https://voice.example.com/voice/webhook",
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
await runtime.stop();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("wires the shared realtime agent consult tool and handler", async () => {
|
|
312
|
+
const config = createBaseConfig();
|
|
313
|
+
config.inboundPolicy = "allowlist";
|
|
314
|
+
config.realtime.enabled = true;
|
|
315
|
+
config.realtime.tools = [
|
|
316
|
+
{
|
|
317
|
+
type: "function",
|
|
318
|
+
name: "custom_tool",
|
|
319
|
+
description: "Custom tool",
|
|
320
|
+
parameters: { type: "object", properties: {} },
|
|
321
|
+
},
|
|
322
|
+
];
|
|
323
|
+
const sessionStore: Record<string, unknown> = {};
|
|
324
|
+
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
325
|
+
payloads: [{ text: "Use the shipment status." }],
|
|
326
|
+
meta: {},
|
|
327
|
+
}));
|
|
328
|
+
const agentRuntime = {
|
|
329
|
+
defaults: { provider: "openai", model: "gpt-5.4" },
|
|
330
|
+
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
331
|
+
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
332
|
+
resolveAgentIdentity: vi.fn(),
|
|
333
|
+
resolveThinkingDefault: vi.fn(() => "high"),
|
|
334
|
+
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
335
|
+
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
336
|
+
session: {
|
|
337
|
+
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
338
|
+
loadSessionStore: vi.fn(() => sessionStore),
|
|
339
|
+
saveSessionStore: vi.fn(async () => {}),
|
|
340
|
+
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
341
|
+
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
342
|
+
},
|
|
343
|
+
runEmbeddedPiAgent,
|
|
344
|
+
};
|
|
345
|
+
mocks.managerGetCall.mockReturnValue({
|
|
346
|
+
callId: "call-1",
|
|
347
|
+
direction: "outbound",
|
|
348
|
+
from: "+15550001234",
|
|
349
|
+
to: "+15550009999",
|
|
350
|
+
transcript: [{ speaker: "user", text: "Can you check shipment status?" }],
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
await createVoiceCallRuntime({
|
|
354
|
+
config,
|
|
355
|
+
coreConfig: {} as CoreConfig,
|
|
356
|
+
agentRuntime: agentRuntime as never,
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
expect(mocks.realtimeHandlerCtorArgs[0]?.[0]).toMatchObject({
|
|
360
|
+
tools: [
|
|
361
|
+
expect.objectContaining({ name: "openclaw_agent_consult" }),
|
|
362
|
+
expect.objectContaining({ name: "custom_tool" }),
|
|
363
|
+
],
|
|
364
|
+
});
|
|
365
|
+
expect(mocks.realtimeHandlerRegisterToolHandler).toHaveBeenCalledWith(
|
|
366
|
+
"openclaw_agent_consult",
|
|
367
|
+
expect.any(Function),
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
const handler = mocks.realtimeHandlerRegisterToolHandler.mock.calls[0]?.[1] as
|
|
371
|
+
| ((
|
|
372
|
+
args: unknown,
|
|
373
|
+
callId: string,
|
|
374
|
+
context?: { partialUserTranscript?: string },
|
|
375
|
+
) => Promise<unknown>)
|
|
376
|
+
| undefined;
|
|
377
|
+
await expect(
|
|
378
|
+
handler?.({ question: "What should I say?" }, "call-1", {
|
|
379
|
+
partialUserTranscript: "Also check the ETA.",
|
|
380
|
+
}),
|
|
381
|
+
).resolves.toEqual({
|
|
382
|
+
text: "Use the shipment status.",
|
|
383
|
+
});
|
|
384
|
+
expect(runEmbeddedPiAgent).toHaveBeenCalledWith(
|
|
385
|
+
expect.objectContaining({
|
|
386
|
+
sessionKey: "voice:15550009999",
|
|
387
|
+
messageProvider: "voice",
|
|
388
|
+
lane: "voice",
|
|
389
|
+
provider: "openai",
|
|
390
|
+
model: "gpt-5.4",
|
|
391
|
+
toolsAllow: ["read", "web_search", "web_fetch", "x_search", "memory_search", "memory_get"],
|
|
392
|
+
extraSystemPrompt: expect.stringContaining("one or two bounded read-only queries"),
|
|
393
|
+
prompt: expect.stringContaining("Caller: Can you check shipment status?"),
|
|
394
|
+
}),
|
|
395
|
+
);
|
|
396
|
+
expect(runEmbeddedPiAgent).toHaveBeenCalledWith(
|
|
397
|
+
expect.objectContaining({
|
|
398
|
+
prompt: expect.stringContaining("Caller: Also check the ETA."),
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it("uses persisted per-call session keys for realtime consults", async () => {
|
|
404
|
+
const config = createBaseConfig();
|
|
405
|
+
config.inboundPolicy = "allowlist";
|
|
406
|
+
config.realtime.enabled = true;
|
|
407
|
+
config.sessionScope = "per-call";
|
|
408
|
+
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
409
|
+
payloads: [{ text: "Per-call consult answer." }],
|
|
410
|
+
meta: {},
|
|
411
|
+
}));
|
|
412
|
+
const sessionStore: Record<string, unknown> = {};
|
|
413
|
+
const agentRuntime = {
|
|
414
|
+
defaults: { provider: "openai", model: "gpt-5.4" },
|
|
415
|
+
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
416
|
+
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
417
|
+
resolveAgentIdentity: vi.fn(),
|
|
418
|
+
resolveThinkingDefault: vi.fn(() => "high"),
|
|
419
|
+
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
420
|
+
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
421
|
+
session: {
|
|
422
|
+
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
423
|
+
loadSessionStore: vi.fn(() => sessionStore),
|
|
424
|
+
saveSessionStore: vi.fn(async () => {}),
|
|
425
|
+
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
426
|
+
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
427
|
+
},
|
|
428
|
+
runEmbeddedPiAgent,
|
|
429
|
+
};
|
|
430
|
+
mocks.managerGetCall.mockReturnValue({
|
|
431
|
+
callId: "call-1",
|
|
432
|
+
sessionKey: "voice:call:call-1",
|
|
433
|
+
direction: "inbound",
|
|
434
|
+
from: "+15550001234",
|
|
435
|
+
to: "+15550009999",
|
|
436
|
+
transcript: [],
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
await createVoiceCallRuntime({
|
|
440
|
+
config,
|
|
441
|
+
coreConfig: {} as CoreConfig,
|
|
442
|
+
agentRuntime: agentRuntime as never,
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
const handler = mocks.realtimeHandlerRegisterToolHandler.mock.calls[0]?.[1] as
|
|
446
|
+
| ((
|
|
447
|
+
args: unknown,
|
|
448
|
+
callId: string,
|
|
449
|
+
context?: { partialUserTranscript?: string },
|
|
450
|
+
) => Promise<unknown>)
|
|
451
|
+
| undefined;
|
|
452
|
+
await expect(handler?.({ question: "What should I say?" }, "call-1")).resolves.toEqual({
|
|
453
|
+
text: "Per-call consult answer.",
|
|
454
|
+
});
|
|
455
|
+
expect(runEmbeddedPiAgent).toHaveBeenCalledWith(
|
|
456
|
+
expect.objectContaining({
|
|
457
|
+
sessionKey: "voice:call:call-1",
|
|
458
|
+
}),
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("answers realtime consults from fast memory context before starting the full agent", async () => {
|
|
463
|
+
const config = createBaseConfig();
|
|
464
|
+
config.realtime.enabled = true;
|
|
465
|
+
config.realtime.fastContext = {
|
|
466
|
+
enabled: true,
|
|
467
|
+
timeoutMs: 800,
|
|
468
|
+
maxResults: 2,
|
|
469
|
+
sources: ["memory"],
|
|
470
|
+
fallbackToConsult: false,
|
|
471
|
+
};
|
|
472
|
+
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
473
|
+
payloads: [{ text: "slow answer" }],
|
|
474
|
+
meta: {},
|
|
475
|
+
}));
|
|
476
|
+
const sessionStore: Record<string, unknown> = {};
|
|
477
|
+
const agentRuntime = {
|
|
478
|
+
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
479
|
+
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
480
|
+
resolveAgentIdentity: vi.fn(),
|
|
481
|
+
resolveThinkingDefault: vi.fn(() => "high"),
|
|
482
|
+
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
483
|
+
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
484
|
+
session: {
|
|
485
|
+
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
486
|
+
loadSessionStore: vi.fn(() => sessionStore),
|
|
487
|
+
saveSessionStore: vi.fn(async () => {}),
|
|
488
|
+
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
489
|
+
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
490
|
+
},
|
|
491
|
+
runEmbeddedPiAgent,
|
|
492
|
+
};
|
|
493
|
+
mocks.managerGetCall.mockReturnValue({
|
|
494
|
+
callId: "call-1",
|
|
495
|
+
direction: "inbound",
|
|
496
|
+
from: "+15550001234",
|
|
497
|
+
to: "+15550009999",
|
|
498
|
+
transcript: [],
|
|
499
|
+
});
|
|
500
|
+
mocks.memorySearch.mockResolvedValue([
|
|
501
|
+
{
|
|
502
|
+
source: "memory",
|
|
503
|
+
path: "MEMORY.md",
|
|
504
|
+
startLine: 12,
|
|
505
|
+
endLine: 14,
|
|
506
|
+
score: 0.91,
|
|
507
|
+
snippet: "The caller's basement lights are on.",
|
|
508
|
+
},
|
|
509
|
+
]);
|
|
510
|
+
|
|
511
|
+
await createVoiceCallRuntime({
|
|
512
|
+
config,
|
|
513
|
+
coreConfig: {} as CoreConfig,
|
|
514
|
+
agentRuntime: agentRuntime as never,
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
const handler = mocks.realtimeHandlerRegisterToolHandler.mock.calls[0]?.[1] as
|
|
518
|
+
| ((
|
|
519
|
+
args: unknown,
|
|
520
|
+
callId: string,
|
|
521
|
+
context?: { partialUserTranscript?: string },
|
|
522
|
+
) => Promise<unknown>)
|
|
523
|
+
| undefined;
|
|
524
|
+
await expect(handler?.({ question: "Are the basement lights on?" }, "call-1")).resolves.toEqual(
|
|
525
|
+
{
|
|
526
|
+
text: expect.stringContaining("The caller's basement lights are on."),
|
|
527
|
+
},
|
|
528
|
+
);
|
|
529
|
+
expect(mocks.memorySearch).toHaveBeenCalledWith("Are the basement lights on?", {
|
|
530
|
+
maxResults: 2,
|
|
531
|
+
sessionKey: "voice:15550001234",
|
|
532
|
+
sources: ["memory"],
|
|
533
|
+
});
|
|
534
|
+
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
|
|
535
|
+
});
|
|
107
536
|
});
|