@kodelyth/voice-call 2026.5.42 → 2026.6.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.
- package/package.json +18 -6
- package/api.ts +0 -16
- package/cli-metadata.ts +0 -10
- package/config-api.ts +0 -12
- package/index.test.ts +0 -1075
- package/index.ts +0 -863
- package/runtime-api.ts +0 -20
- package/runtime-entry.ts +0 -1
- package/setup-api.ts +0 -47
- package/src/allowlist.test.ts +0 -18
- package/src/allowlist.ts +0 -19
- package/src/cli.test.ts +0 -12
- package/src/cli.ts +0 -866
- package/src/config-compat.test.ts +0 -130
- package/src/config-compat.ts +0 -227
- package/src/config.test.ts +0 -542
- package/src/config.ts +0 -883
- package/src/core-bridge.ts +0 -14
- package/src/deep-merge.test.ts +0 -40
- package/src/deep-merge.ts +0 -23
- package/src/gateway-continue-operation.ts +0 -200
- package/src/http-headers.test.ts +0 -16
- package/src/http-headers.ts +0 -15
- package/src/manager/context.ts +0 -50
- package/src/manager/events.test.ts +0 -578
- package/src/manager/events.ts +0 -332
- package/src/manager/lifecycle.ts +0 -53
- package/src/manager/lookup.test.ts +0 -52
- package/src/manager/lookup.ts +0 -35
- package/src/manager/outbound.test.ts +0 -629
- package/src/manager/outbound.ts +0 -508
- package/src/manager/state.ts +0 -48
- package/src/manager/store.ts +0 -107
- package/src/manager/timers.test.ts +0 -127
- package/src/manager/timers.ts +0 -113
- package/src/manager/twiml.test.ts +0 -13
- package/src/manager/twiml.ts +0 -17
- package/src/manager.closed-loop.test.ts +0 -259
- package/src/manager.inbound-allowlist.test.ts +0 -183
- package/src/manager.notify.test.ts +0 -390
- package/src/manager.restore.test.ts +0 -310
- package/src/manager.test-harness.ts +0 -127
- package/src/manager.ts +0 -441
- package/src/media-stream.test.ts +0 -953
- package/src/media-stream.ts +0 -876
- package/src/providers/base.ts +0 -99
- package/src/providers/mock.test.ts +0 -86
- package/src/providers/mock.ts +0 -185
- package/src/providers/plivo.test.ts +0 -93
- package/src/providers/plivo.ts +0 -601
- package/src/providers/shared/call-status.test.ts +0 -24
- package/src/providers/shared/call-status.ts +0 -24
- package/src/providers/shared/guarded-json-api.test.ts +0 -127
- package/src/providers/shared/guarded-json-api.ts +0 -49
- package/src/providers/telnyx.test.ts +0 -489
- package/src/providers/telnyx.ts +0 -419
- package/src/providers/twilio/api.test.ts +0 -184
- package/src/providers/twilio/api.ts +0 -100
- package/src/providers/twilio/twiml-policy.test.ts +0 -84
- package/src/providers/twilio/twiml-policy.ts +0 -87
- package/src/providers/twilio/webhook.ts +0 -34
- package/src/providers/twilio.test.ts +0 -607
- package/src/providers/twilio.ts +0 -861
- package/src/providers/twilio.types.ts +0 -17
- package/src/realtime-agent-context.test.ts +0 -101
- package/src/realtime-agent-context.ts +0 -149
- package/src/realtime-defaults.ts +0 -3
- package/src/realtime-fast-context.test.ts +0 -74
- package/src/realtime-fast-context.ts +0 -27
- package/src/realtime-transcription.runtime.ts +0 -4
- package/src/realtime-voice.runtime.ts +0 -5
- package/src/response-generator.test.ts +0 -385
- package/src/response-generator.ts +0 -348
- package/src/response-model.test.ts +0 -71
- package/src/response-model.ts +0 -23
- package/src/runtime.test.ts +0 -625
- package/src/runtime.ts +0 -528
- package/src/telephony-audio.test.ts +0 -61
- package/src/telephony-audio.ts +0 -12
- package/src/telephony-tts.test.ts +0 -196
- package/src/telephony-tts.ts +0 -235
- package/src/test-fixtures.ts +0 -82
- package/src/tts-provider-voice.test.ts +0 -34
- package/src/tts-provider-voice.ts +0 -21
- package/src/tunnel.test.ts +0 -173
- package/src/tunnel.ts +0 -314
- package/src/types.ts +0 -311
- package/src/utils.test.ts +0 -17
- package/src/utils.ts +0 -14
- package/src/voice-mapping.test.ts +0 -32
- package/src/voice-mapping.ts +0 -65
- package/src/webhook/realtime-audio-pacer.test.ts +0 -146
- package/src/webhook/realtime-audio-pacer.ts +0 -204
- package/src/webhook/realtime-handler.test.ts +0 -1450
- package/src/webhook/realtime-handler.ts +0 -1382
- package/src/webhook/stale-call-reaper.test.ts +0 -89
- package/src/webhook/stale-call-reaper.ts +0 -38
- package/src/webhook/stream-frame-adapter.test.ts +0 -187
- package/src/webhook/stream-frame-adapter.ts +0 -219
- package/src/webhook/tailscale.test.ts +0 -216
- package/src/webhook/tailscale.ts +0 -129
- package/src/webhook-exposure.test.ts +0 -33
- package/src/webhook-exposure.ts +0 -84
- package/src/webhook-security.test.ts +0 -813
- package/src/webhook-security.ts +0 -982
- package/src/webhook.hangup-once.lifecycle.test.ts +0 -179
- package/src/webhook.test.ts +0 -1615
- package/src/webhook.ts +0 -933
- package/src/webhook.types.ts +0 -5
- package/src/websocket-test-support.ts +0 -72
- package/tsconfig.json +0 -16
package/src/runtime.test.ts
DELETED
|
@@ -1,625 +0,0 @@
|
|
|
1
|
-
import type { KlawConfig } from "klaw/plugin-sdk/core";
|
|
2
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import type { VoiceCallConfig } from "./config.js";
|
|
4
|
-
import type { CoreConfig } from "./core-bridge.js";
|
|
5
|
-
import { createVoiceCallBaseConfig } from "./test-fixtures.js";
|
|
6
|
-
|
|
7
|
-
const mocks = vi.hoisted(() => ({
|
|
8
|
-
resolveVoiceCallConfig: vi.fn(),
|
|
9
|
-
resolveTwilioAuthToken: vi.fn(),
|
|
10
|
-
validateProviderConfig: vi.fn(),
|
|
11
|
-
managerInitialize: vi.fn(),
|
|
12
|
-
managerGetCall: vi.fn(),
|
|
13
|
-
webhookStart: vi.fn(),
|
|
14
|
-
webhookStop: vi.fn(),
|
|
15
|
-
webhookSetRealtimeHandler: vi.fn(),
|
|
16
|
-
webhookGetRealtimeHandler: vi.fn(),
|
|
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
|
-
resolveRealtimeFastContextConsult: vi.fn(),
|
|
24
|
-
startTunnel: vi.fn(),
|
|
25
|
-
setupTailscaleExposure: vi.fn(),
|
|
26
|
-
cleanupTailscaleExposure: vi.fn(),
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
|
-
vi.mock("./config.js", () => ({
|
|
30
|
-
resolveVoiceCallSessionKey: (params: {
|
|
31
|
-
config: Pick<VoiceCallConfig, "sessionScope">;
|
|
32
|
-
callId: string;
|
|
33
|
-
phone?: string;
|
|
34
|
-
explicitSessionKey?: string;
|
|
35
|
-
}) => {
|
|
36
|
-
const explicit = params.explicitSessionKey?.trim();
|
|
37
|
-
if (explicit) {
|
|
38
|
-
return explicit;
|
|
39
|
-
}
|
|
40
|
-
if (params.config.sessionScope === "per-call") {
|
|
41
|
-
return `voice:call:${params.callId}`;
|
|
42
|
-
}
|
|
43
|
-
const normalizedPhone = params.phone?.replace(/\D/g, "");
|
|
44
|
-
return normalizedPhone ? `voice:${normalizedPhone}` : `voice:${params.callId}`;
|
|
45
|
-
},
|
|
46
|
-
resolveVoiceCallEffectiveConfig: (config: VoiceCallConfig) => ({ config }),
|
|
47
|
-
resolveVoiceCallConfig: mocks.resolveVoiceCallConfig,
|
|
48
|
-
resolveTwilioAuthToken: mocks.resolveTwilioAuthToken,
|
|
49
|
-
validateProviderConfig: mocks.validateProviderConfig,
|
|
50
|
-
}));
|
|
51
|
-
|
|
52
|
-
vi.mock("./manager.js", () => ({
|
|
53
|
-
CallManager: class {
|
|
54
|
-
initialize = mocks.managerInitialize;
|
|
55
|
-
getCall = mocks.managerGetCall;
|
|
56
|
-
},
|
|
57
|
-
}));
|
|
58
|
-
|
|
59
|
-
vi.mock("./webhook.js", () => ({
|
|
60
|
-
VoiceCallWebhookServer: class {
|
|
61
|
-
constructor(...args: unknown[]) {
|
|
62
|
-
mocks.webhookCtorArgs.push(args);
|
|
63
|
-
}
|
|
64
|
-
start = mocks.webhookStart;
|
|
65
|
-
stop = mocks.webhookStop;
|
|
66
|
-
setRealtimeHandler = mocks.webhookSetRealtimeHandler;
|
|
67
|
-
getRealtimeHandler = mocks.webhookGetRealtimeHandler;
|
|
68
|
-
getMediaStreamHandler = mocks.webhookGetMediaStreamHandler;
|
|
69
|
-
},
|
|
70
|
-
}));
|
|
71
|
-
|
|
72
|
-
vi.mock("./realtime-voice.runtime.js", () => ({
|
|
73
|
-
resolveConfiguredRealtimeVoiceProvider: mocks.resolveConfiguredRealtimeVoiceProvider,
|
|
74
|
-
}));
|
|
75
|
-
|
|
76
|
-
vi.mock("./realtime-fast-context.js", () => ({
|
|
77
|
-
resolveRealtimeFastContextConsult: mocks.resolveRealtimeFastContextConsult,
|
|
78
|
-
}));
|
|
79
|
-
|
|
80
|
-
vi.mock("./webhook/realtime-handler.js", () => ({
|
|
81
|
-
RealtimeCallHandler: class {
|
|
82
|
-
constructor(...args: unknown[]) {
|
|
83
|
-
mocks.realtimeHandlerCtorArgs.push(args);
|
|
84
|
-
}
|
|
85
|
-
registerToolHandler = mocks.realtimeHandlerRegisterToolHandler;
|
|
86
|
-
setPublicUrl = mocks.realtimeHandlerSetPublicUrl;
|
|
87
|
-
},
|
|
88
|
-
}));
|
|
89
|
-
|
|
90
|
-
vi.mock("./tunnel.js", () => ({
|
|
91
|
-
startTunnel: mocks.startTunnel,
|
|
92
|
-
}));
|
|
93
|
-
|
|
94
|
-
vi.mock("./webhook/tailscale.js", () => ({
|
|
95
|
-
setupTailscaleExposure: mocks.setupTailscaleExposure,
|
|
96
|
-
cleanupTailscaleExposure: mocks.cleanupTailscaleExposure,
|
|
97
|
-
}));
|
|
98
|
-
|
|
99
|
-
import { createVoiceCallRuntime } from "./runtime.js";
|
|
100
|
-
|
|
101
|
-
function createBaseConfig(): VoiceCallConfig {
|
|
102
|
-
return createVoiceCallBaseConfig({ tunnelProvider: "ngrok" });
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function createExternalProviderConfig(params: {
|
|
106
|
-
provider: "twilio" | "telnyx" | "plivo";
|
|
107
|
-
publicUrl?: string;
|
|
108
|
-
}): VoiceCallConfig {
|
|
109
|
-
const config = createVoiceCallBaseConfig({
|
|
110
|
-
provider: params.provider,
|
|
111
|
-
tunnelProvider: "none",
|
|
112
|
-
});
|
|
113
|
-
config.twilio = {
|
|
114
|
-
accountSid: "AC123",
|
|
115
|
-
authToken: "secret",
|
|
116
|
-
};
|
|
117
|
-
config.telnyx = {
|
|
118
|
-
apiKey: "key",
|
|
119
|
-
connectionId: "conn",
|
|
120
|
-
publicKey: "pub",
|
|
121
|
-
};
|
|
122
|
-
config.plivo = {
|
|
123
|
-
authId: "MA123",
|
|
124
|
-
authToken: "secret",
|
|
125
|
-
};
|
|
126
|
-
if (params.publicUrl) {
|
|
127
|
-
config.publicUrl = params.publicUrl;
|
|
128
|
-
}
|
|
129
|
-
return config;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
type RealtimeConsultToolHandler = (
|
|
133
|
-
args: unknown,
|
|
134
|
-
callId: string,
|
|
135
|
-
context?: { partialUserTranscript?: string },
|
|
136
|
-
) => Promise<unknown>;
|
|
137
|
-
|
|
138
|
-
function firstMockCall(calls: readonly unknown[][], label: string): unknown[] {
|
|
139
|
-
const call = calls.at(0);
|
|
140
|
-
if (!call) {
|
|
141
|
-
throw new Error(`expected ${label} call`);
|
|
142
|
-
}
|
|
143
|
-
return call;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function firstCallParam(calls: readonly unknown[][], label: string) {
|
|
147
|
-
const call = firstMockCall(calls, label);
|
|
148
|
-
return call[0];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
152
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
153
|
-
throw new Error(`expected ${label} to be a record`);
|
|
154
|
-
}
|
|
155
|
-
return value as Record<string, unknown>;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function requireRealtimeConsultToolHandler(): RealtimeConsultToolHandler {
|
|
159
|
-
const registeredToolHandler = firstMockCall(
|
|
160
|
-
mocks.realtimeHandlerRegisterToolHandler.mock.calls,
|
|
161
|
-
"realtime tool handler registration",
|
|
162
|
-
);
|
|
163
|
-
expect(registeredToolHandler[0]).toBe("klaw_agent_consult");
|
|
164
|
-
if (typeof registeredToolHandler[1] !== "function") {
|
|
165
|
-
throw new Error("expected realtime tool handler callback");
|
|
166
|
-
}
|
|
167
|
-
return registeredToolHandler[1] as RealtimeConsultToolHandler;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
describe("createVoiceCallRuntime lifecycle", () => {
|
|
171
|
-
beforeEach(() => {
|
|
172
|
-
vi.clearAllMocks();
|
|
173
|
-
mocks.resolveVoiceCallConfig.mockImplementation((cfg: VoiceCallConfig) => cfg);
|
|
174
|
-
mocks.resolveTwilioAuthToken.mockImplementation(
|
|
175
|
-
(cfg: VoiceCallConfig) => cfg.twilio?.authToken,
|
|
176
|
-
);
|
|
177
|
-
mocks.validateProviderConfig.mockReturnValue({ valid: true, errors: [] });
|
|
178
|
-
mocks.managerInitialize.mockResolvedValue(undefined);
|
|
179
|
-
mocks.managerGetCall.mockReset();
|
|
180
|
-
mocks.webhookStart.mockResolvedValue("http://127.0.0.1:3334/voice/webhook");
|
|
181
|
-
mocks.webhookStop.mockResolvedValue(undefined);
|
|
182
|
-
mocks.webhookSetRealtimeHandler.mockReset();
|
|
183
|
-
mocks.webhookGetRealtimeHandler.mockReturnValue({
|
|
184
|
-
setPublicUrl: mocks.realtimeHandlerSetPublicUrl,
|
|
185
|
-
});
|
|
186
|
-
mocks.webhookGetMediaStreamHandler.mockReturnValue(undefined);
|
|
187
|
-
mocks.webhookCtorArgs.length = 0;
|
|
188
|
-
mocks.realtimeHandlerCtorArgs.length = 0;
|
|
189
|
-
mocks.realtimeHandlerRegisterToolHandler.mockReset();
|
|
190
|
-
mocks.realtimeHandlerSetPublicUrl.mockReset();
|
|
191
|
-
mocks.resolveConfiguredRealtimeVoiceProvider.mockResolvedValue({
|
|
192
|
-
provider: { id: "openai" },
|
|
193
|
-
providerConfig: { model: "gpt-realtime" },
|
|
194
|
-
});
|
|
195
|
-
mocks.resolveRealtimeFastContextConsult.mockReset();
|
|
196
|
-
mocks.resolveRealtimeFastContextConsult.mockResolvedValue({ handled: false });
|
|
197
|
-
mocks.startTunnel.mockResolvedValue(null);
|
|
198
|
-
mocks.setupTailscaleExposure.mockResolvedValue(null);
|
|
199
|
-
mocks.cleanupTailscaleExposure.mockResolvedValue(undefined);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it("cleans up tunnel, tailscale, and webhook server when init fails after start", async () => {
|
|
203
|
-
const tunnelStop = vi.fn().mockResolvedValue(undefined);
|
|
204
|
-
mocks.startTunnel.mockResolvedValue({
|
|
205
|
-
publicUrl: "https://public.example/voice/webhook",
|
|
206
|
-
provider: "ngrok",
|
|
207
|
-
stop: tunnelStop,
|
|
208
|
-
});
|
|
209
|
-
mocks.managerInitialize.mockRejectedValue(new Error("init failed"));
|
|
210
|
-
|
|
211
|
-
await expect(
|
|
212
|
-
createVoiceCallRuntime({
|
|
213
|
-
config: createBaseConfig(),
|
|
214
|
-
coreConfig: {},
|
|
215
|
-
agentRuntime: {} as never,
|
|
216
|
-
}),
|
|
217
|
-
).rejects.toThrow("init failed");
|
|
218
|
-
|
|
219
|
-
expect(tunnelStop).toHaveBeenCalledTimes(1);
|
|
220
|
-
expect(mocks.cleanupTailscaleExposure).toHaveBeenCalledTimes(1);
|
|
221
|
-
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("returns an idempotent stop handler", async () => {
|
|
225
|
-
const tunnelStop = vi.fn().mockResolvedValue(undefined);
|
|
226
|
-
mocks.startTunnel.mockResolvedValue({
|
|
227
|
-
publicUrl: "https://public.example/voice/webhook",
|
|
228
|
-
provider: "ngrok",
|
|
229
|
-
stop: tunnelStop,
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
const runtime = await createVoiceCallRuntime({
|
|
233
|
-
config: createBaseConfig(),
|
|
234
|
-
coreConfig: {} as CoreConfig,
|
|
235
|
-
agentRuntime: {} as never,
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
await runtime.stop();
|
|
239
|
-
await runtime.stop();
|
|
240
|
-
|
|
241
|
-
expect(tunnelStop).toHaveBeenCalledTimes(1);
|
|
242
|
-
expect(mocks.cleanupTailscaleExposure).toHaveBeenCalledTimes(1);
|
|
243
|
-
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
it("passes fullConfig to the webhook server for streaming provider resolution", async () => {
|
|
247
|
-
const coreConfig = { messages: { tts: { provider: "openai" } } } as CoreConfig;
|
|
248
|
-
const fullConfig = {
|
|
249
|
-
plugins: {
|
|
250
|
-
entries: {
|
|
251
|
-
openai: { enabled: true },
|
|
252
|
-
},
|
|
253
|
-
},
|
|
254
|
-
} as KlawConfig;
|
|
255
|
-
|
|
256
|
-
await createVoiceCallRuntime({
|
|
257
|
-
config: createBaseConfig(),
|
|
258
|
-
coreConfig,
|
|
259
|
-
fullConfig,
|
|
260
|
-
agentRuntime: {} as never,
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
expect(mocks.webhookCtorArgs[0]?.[3]).toBe(coreConfig);
|
|
264
|
-
expect(mocks.webhookCtorArgs[0]?.[4]).toBe(fullConfig);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
it.each(["twilio", "telnyx", "plivo"] as const)(
|
|
268
|
-
"fails closed when %s falls back to a local-only webhook",
|
|
269
|
-
async (provider) => {
|
|
270
|
-
await expect(
|
|
271
|
-
createVoiceCallRuntime({
|
|
272
|
-
config: createExternalProviderConfig({ provider }),
|
|
273
|
-
coreConfig: {} as CoreConfig,
|
|
274
|
-
agentRuntime: {} as never,
|
|
275
|
-
}),
|
|
276
|
-
).rejects.toThrow(`${provider} requires a publicly reachable webhook URL`);
|
|
277
|
-
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
278
|
-
},
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
it.each([
|
|
282
|
-
"http://127.0.0.1:3334/voice/webhook",
|
|
283
|
-
"http://[::1]:3334/voice/webhook",
|
|
284
|
-
"http://[fd00::1]/voice/webhook",
|
|
285
|
-
])("fails closed when Twilio publicUrl %s points at a local-only webhook", async (publicUrl) => {
|
|
286
|
-
await expect(
|
|
287
|
-
createVoiceCallRuntime({
|
|
288
|
-
config: createExternalProviderConfig({
|
|
289
|
-
provider: "twilio",
|
|
290
|
-
publicUrl,
|
|
291
|
-
}),
|
|
292
|
-
coreConfig: {} as CoreConfig,
|
|
293
|
-
agentRuntime: {} as never,
|
|
294
|
-
}),
|
|
295
|
-
).rejects.toThrow("twilio requires a publicly reachable webhook URL");
|
|
296
|
-
expect(mocks.webhookStop).toHaveBeenCalledTimes(1);
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it("accepts an explicit public URL for external voice providers", async () => {
|
|
300
|
-
const runtime = await createVoiceCallRuntime({
|
|
301
|
-
config: createExternalProviderConfig({
|
|
302
|
-
provider: "twilio",
|
|
303
|
-
publicUrl: "https://voice.example.com/voice/webhook",
|
|
304
|
-
}),
|
|
305
|
-
coreConfig: {} as CoreConfig,
|
|
306
|
-
agentRuntime: {} as never,
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
expect(runtime.webhookUrl).toBe("https://voice.example.com/voice/webhook");
|
|
310
|
-
expect(runtime.publicUrl).toBe("https://voice.example.com/voice/webhook");
|
|
311
|
-
|
|
312
|
-
await runtime.stop();
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it("does not log duplicate webhook and public URLs when they match", async () => {
|
|
316
|
-
const logger = {
|
|
317
|
-
info: vi.fn(),
|
|
318
|
-
warn: vi.fn(),
|
|
319
|
-
error: vi.fn(),
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
const runtime = await createVoiceCallRuntime({
|
|
323
|
-
config: createExternalProviderConfig({
|
|
324
|
-
provider: "twilio",
|
|
325
|
-
publicUrl: "https://voice.example.com/voice/webhook",
|
|
326
|
-
}),
|
|
327
|
-
coreConfig: {} as CoreConfig,
|
|
328
|
-
agentRuntime: {} as never,
|
|
329
|
-
logger,
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
expect(logger.info).toHaveBeenCalledWith(
|
|
333
|
-
"[voice-call] Webhook URL: https://voice.example.com/voice/webhook",
|
|
334
|
-
);
|
|
335
|
-
expect(logger.info).not.toHaveBeenCalledWith(
|
|
336
|
-
"[voice-call] Public URL: https://voice.example.com/voice/webhook",
|
|
337
|
-
);
|
|
338
|
-
|
|
339
|
-
await runtime.stop();
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it("wires the shared realtime agent consult tool and handler", async () => {
|
|
343
|
-
const config = createBaseConfig();
|
|
344
|
-
config.inboundPolicy = "allowlist";
|
|
345
|
-
config.realtime.enabled = true;
|
|
346
|
-
config.realtime.tools = [
|
|
347
|
-
{
|
|
348
|
-
type: "function",
|
|
349
|
-
name: "custom_tool",
|
|
350
|
-
description: "Custom tool",
|
|
351
|
-
parameters: { type: "object", properties: {} },
|
|
352
|
-
},
|
|
353
|
-
];
|
|
354
|
-
const sessionStore: Record<string, unknown> = {};
|
|
355
|
-
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
356
|
-
payloads: [{ text: "Use the shipment status." }],
|
|
357
|
-
meta: {},
|
|
358
|
-
}));
|
|
359
|
-
const agentRuntime = {
|
|
360
|
-
defaults: { provider: "openai", model: "gpt-5.4" },
|
|
361
|
-
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
362
|
-
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
363
|
-
resolveAgentIdentity: vi.fn(),
|
|
364
|
-
resolveThinkingDefault: vi.fn(() => "high"),
|
|
365
|
-
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
366
|
-
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
367
|
-
session: {
|
|
368
|
-
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
369
|
-
loadSessionStore: vi.fn(() => sessionStore),
|
|
370
|
-
saveSessionStore: vi.fn(async () => {}),
|
|
371
|
-
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
372
|
-
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
373
|
-
},
|
|
374
|
-
runEmbeddedPiAgent,
|
|
375
|
-
};
|
|
376
|
-
mocks.managerGetCall.mockReturnValue({
|
|
377
|
-
callId: "call-1",
|
|
378
|
-
direction: "outbound",
|
|
379
|
-
from: "+15550001234",
|
|
380
|
-
to: "+15550009999",
|
|
381
|
-
metadata: { requesterSessionKey: "agent:main:discord:channel:general" },
|
|
382
|
-
transcript: [{ speaker: "user", text: "Can you check shipment status?" }],
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
await createVoiceCallRuntime({
|
|
386
|
-
config,
|
|
387
|
-
coreConfig: {} as CoreConfig,
|
|
388
|
-
agentRuntime: agentRuntime as never,
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
const realtimeHandlerOptions = requireRecord(
|
|
392
|
-
mocks.realtimeHandlerCtorArgs[0]?.[0],
|
|
393
|
-
"realtime handler options",
|
|
394
|
-
);
|
|
395
|
-
const tools = realtimeHandlerOptions.tools;
|
|
396
|
-
if (!Array.isArray(tools)) {
|
|
397
|
-
throw new Error("expected realtime handler tools to be an array");
|
|
398
|
-
}
|
|
399
|
-
expect(tools.map((tool) => requireRecord(tool, "realtime tool").name)).toEqual([
|
|
400
|
-
"klaw_agent_consult",
|
|
401
|
-
"custom_tool",
|
|
402
|
-
]);
|
|
403
|
-
const handler = requireRealtimeConsultToolHandler();
|
|
404
|
-
await expect(
|
|
405
|
-
handler({ question: "What should I say?" }, "call-1", {
|
|
406
|
-
partialUserTranscript: "Also check the ETA.",
|
|
407
|
-
}),
|
|
408
|
-
).resolves.toEqual({
|
|
409
|
-
text: "Use the shipment status.",
|
|
410
|
-
});
|
|
411
|
-
expect(runEmbeddedPiAgent).toHaveBeenCalledOnce();
|
|
412
|
-
const consultParams = requireRecord(
|
|
413
|
-
firstCallParam(runEmbeddedPiAgent.mock.calls as unknown[][], "embedded PI consult"),
|
|
414
|
-
"embedded PI consult params",
|
|
415
|
-
);
|
|
416
|
-
expect(consultParams.sessionKey).toBe("voice:15550009999");
|
|
417
|
-
expect(consultParams.spawnedBy).toBe("agent:main:discord:channel:general");
|
|
418
|
-
expect(consultParams.messageProvider).toBe("voice");
|
|
419
|
-
expect(consultParams.lane).toBe("voice");
|
|
420
|
-
expect(consultParams.provider).toBe("openai");
|
|
421
|
-
expect(consultParams.model).toBe("gpt-5.4");
|
|
422
|
-
expect(consultParams.toolsAllow).toEqual([
|
|
423
|
-
"read",
|
|
424
|
-
"web_search",
|
|
425
|
-
"web_fetch",
|
|
426
|
-
"x_search",
|
|
427
|
-
"memory_search",
|
|
428
|
-
"memory_get",
|
|
429
|
-
]);
|
|
430
|
-
expect(consultParams.extraSystemPrompt).toContain("one or two bounded read-only queries");
|
|
431
|
-
expect(consultParams.prompt).toContain("Caller: Can you check shipment status?");
|
|
432
|
-
expect(consultParams.prompt).toContain("Caller: Also check the ETA.");
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
it("uses persisted per-call session keys for realtime consults", async () => {
|
|
436
|
-
const config = createBaseConfig();
|
|
437
|
-
config.inboundPolicy = "allowlist";
|
|
438
|
-
config.realtime.enabled = true;
|
|
439
|
-
config.sessionScope = "per-call";
|
|
440
|
-
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
441
|
-
payloads: [{ text: "Per-call consult answer." }],
|
|
442
|
-
meta: {},
|
|
443
|
-
}));
|
|
444
|
-
const sessionStore: Record<string, unknown> = {};
|
|
445
|
-
const agentRuntime = {
|
|
446
|
-
defaults: { provider: "openai", model: "gpt-5.4" },
|
|
447
|
-
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
448
|
-
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
449
|
-
resolveAgentIdentity: vi.fn(),
|
|
450
|
-
resolveThinkingDefault: vi.fn(() => "high"),
|
|
451
|
-
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
452
|
-
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
453
|
-
session: {
|
|
454
|
-
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
455
|
-
loadSessionStore: vi.fn(() => sessionStore),
|
|
456
|
-
saveSessionStore: vi.fn(async () => {}),
|
|
457
|
-
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
458
|
-
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
459
|
-
},
|
|
460
|
-
runEmbeddedPiAgent,
|
|
461
|
-
};
|
|
462
|
-
mocks.managerGetCall.mockReturnValue({
|
|
463
|
-
callId: "call-1",
|
|
464
|
-
sessionKey: "voice:call:call-1",
|
|
465
|
-
direction: "inbound",
|
|
466
|
-
from: "+15550001234",
|
|
467
|
-
to: "+15550009999",
|
|
468
|
-
transcript: [],
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
await createVoiceCallRuntime({
|
|
472
|
-
config,
|
|
473
|
-
coreConfig: {} as CoreConfig,
|
|
474
|
-
agentRuntime: agentRuntime as never,
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
const handler = requireRealtimeConsultToolHandler();
|
|
478
|
-
await expect(handler({ question: "What should I say?" }, "call-1")).resolves.toEqual({
|
|
479
|
-
text: "Per-call consult answer.",
|
|
480
|
-
});
|
|
481
|
-
expect(runEmbeddedPiAgent).toHaveBeenCalledOnce();
|
|
482
|
-
const consultParams = requireRecord(
|
|
483
|
-
firstCallParam(runEmbeddedPiAgent.mock.calls as unknown[][], "per-call embedded PI consult"),
|
|
484
|
-
"per-call embedded PI consult params",
|
|
485
|
-
);
|
|
486
|
-
expect(consultParams.sessionKey).toBe("voice:call:call-1");
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
it("answers realtime consults from fast memory context before starting the full agent", async () => {
|
|
490
|
-
const config = createBaseConfig();
|
|
491
|
-
config.realtime.enabled = true;
|
|
492
|
-
config.realtime.fastContext = {
|
|
493
|
-
enabled: true,
|
|
494
|
-
timeoutMs: 800,
|
|
495
|
-
maxResults: 2,
|
|
496
|
-
sources: ["memory"],
|
|
497
|
-
fallbackToConsult: false,
|
|
498
|
-
};
|
|
499
|
-
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
500
|
-
payloads: [{ text: "slow answer" }],
|
|
501
|
-
meta: {},
|
|
502
|
-
}));
|
|
503
|
-
const sessionStore: Record<string, unknown> = {};
|
|
504
|
-
const agentRuntime = {
|
|
505
|
-
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
506
|
-
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
507
|
-
resolveAgentIdentity: vi.fn(),
|
|
508
|
-
resolveThinkingDefault: vi.fn(() => "high"),
|
|
509
|
-
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
510
|
-
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
511
|
-
session: {
|
|
512
|
-
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
513
|
-
loadSessionStore: vi.fn(() => sessionStore),
|
|
514
|
-
saveSessionStore: vi.fn(async () => {}),
|
|
515
|
-
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore as never)),
|
|
516
|
-
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
517
|
-
},
|
|
518
|
-
runEmbeddedPiAgent,
|
|
519
|
-
};
|
|
520
|
-
mocks.managerGetCall.mockReturnValue({
|
|
521
|
-
callId: "call-1",
|
|
522
|
-
direction: "inbound",
|
|
523
|
-
from: "+15550001234",
|
|
524
|
-
to: "+15550009999",
|
|
525
|
-
transcript: [],
|
|
526
|
-
});
|
|
527
|
-
mocks.resolveRealtimeFastContextConsult.mockResolvedValue({
|
|
528
|
-
handled: true,
|
|
529
|
-
result: {
|
|
530
|
-
text: "Fast Klaw memory or session context found.\nThe caller's basement lights are on.",
|
|
531
|
-
},
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
await createVoiceCallRuntime({
|
|
535
|
-
config,
|
|
536
|
-
coreConfig: {} as CoreConfig,
|
|
537
|
-
agentRuntime: agentRuntime as never,
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
const handler = requireRealtimeConsultToolHandler();
|
|
541
|
-
const fastContextResult = await handler({ question: "Are the basement lights on?" }, "call-1");
|
|
542
|
-
const fastContextRecord = requireRecord(fastContextResult, "fast context result");
|
|
543
|
-
expect(fastContextRecord.text).toContain("The caller's basement lights are on.");
|
|
544
|
-
expect(mocks.resolveRealtimeFastContextConsult).toHaveBeenCalledWith({
|
|
545
|
-
cfg: {},
|
|
546
|
-
agentId: "main",
|
|
547
|
-
args: { question: "Are the basement lights on?" },
|
|
548
|
-
config: {
|
|
549
|
-
enabled: true,
|
|
550
|
-
fallbackToConsult: false,
|
|
551
|
-
maxResults: 2,
|
|
552
|
-
sources: ["memory"],
|
|
553
|
-
timeoutMs: 800,
|
|
554
|
-
},
|
|
555
|
-
logger: {
|
|
556
|
-
info: console.log,
|
|
557
|
-
warn: console.warn,
|
|
558
|
-
error: console.error,
|
|
559
|
-
debug: console.debug,
|
|
560
|
-
},
|
|
561
|
-
sessionKey: "voice:15550001234",
|
|
562
|
-
});
|
|
563
|
-
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
it("uses the configured realtime consult thinking level when set", async () => {
|
|
567
|
-
const config = createBaseConfig();
|
|
568
|
-
config.inboundPolicy = "allowlist";
|
|
569
|
-
config.realtime.enabled = true;
|
|
570
|
-
config.realtime.consultThinkingLevel = "low";
|
|
571
|
-
config.realtime.consultFastMode = true;
|
|
572
|
-
const sessionStore: Record<string, unknown> = {};
|
|
573
|
-
const runEmbeddedPiAgent = vi.fn(async () => ({
|
|
574
|
-
payloads: [{ text: "Done." }],
|
|
575
|
-
meta: {},
|
|
576
|
-
}));
|
|
577
|
-
const agentRuntime = {
|
|
578
|
-
defaults: { provider: "openai", model: "gpt-5.4" },
|
|
579
|
-
resolveAgentDir: vi.fn(() => "/tmp/agent"),
|
|
580
|
-
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace"),
|
|
581
|
-
resolveAgentIdentity: vi.fn(),
|
|
582
|
-
resolveThinkingDefault: vi.fn(() => "high"),
|
|
583
|
-
resolveAgentTimeoutMs: vi.fn(() => 30_000),
|
|
584
|
-
ensureAgentWorkspace: vi.fn(async () => {}),
|
|
585
|
-
session: {
|
|
586
|
-
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
587
|
-
loadSessionStore: vi.fn(() => sessionStore),
|
|
588
|
-
saveSessionStore: vi.fn(async () => {}),
|
|
589
|
-
updateSessionStore: vi.fn(async (_storePath, mutator) => mutator(sessionStore)),
|
|
590
|
-
resolveSessionFilePath: vi.fn(() => "/tmp/session.json"),
|
|
591
|
-
},
|
|
592
|
-
runEmbeddedPiAgent,
|
|
593
|
-
};
|
|
594
|
-
mocks.managerGetCall.mockReturnValue({
|
|
595
|
-
callId: "call-1",
|
|
596
|
-
direction: "outbound",
|
|
597
|
-
from: "+15550001234",
|
|
598
|
-
to: "+15550009999",
|
|
599
|
-
transcript: [],
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
await createVoiceCallRuntime({
|
|
603
|
-
config,
|
|
604
|
-
coreConfig: {} as CoreConfig,
|
|
605
|
-
agentRuntime: agentRuntime as never,
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
const handler = requireRealtimeConsultToolHandler();
|
|
609
|
-
await expect(handler({ question: "Turn on the lights." }, "call-1")).resolves.toEqual({
|
|
610
|
-
text: "Done.",
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
expect(agentRuntime.resolveThinkingDefault).not.toHaveBeenCalled();
|
|
614
|
-
expect(runEmbeddedPiAgent).toHaveBeenCalledOnce();
|
|
615
|
-
const consultParams = requireRecord(
|
|
616
|
-
firstCallParam(
|
|
617
|
-
runEmbeddedPiAgent.mock.calls as unknown[][],
|
|
618
|
-
"configured embedded PI consult",
|
|
619
|
-
),
|
|
620
|
-
"configured embedded PI consult params",
|
|
621
|
-
);
|
|
622
|
-
expect(consultParams.thinkLevel).toBe("low");
|
|
623
|
-
expect(consultParams.fastMode).toBe(true);
|
|
624
|
-
});
|
|
625
|
-
});
|