@vellumai/assistant 0.12.0-dev.202609111517.cc35b41 → 0.12.0-dev.202609111724.a78dcda
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/ARCHITECTURE.md +3 -1
- package/package.json +1 -1
- package/src/__tests__/config-schema.test.ts +12 -6
- package/src/__tests__/telephony-tts-guard.test.ts +38 -3
- package/src/calls/__tests__/call-controller.test.ts +450 -1
- package/src/calls/__tests__/media-stream-output.test.ts +32 -0
- package/src/{live-voice → calls}/__tests__/progress-narration.test.ts +4 -4
- package/src/{live-voice → calls}/__tests__/progress-phrases.test.ts +1 -1
- package/src/calls/__tests__/voice-leg-coordinator.test.ts +229 -0
- package/src/calls/__tests__/voice-progress-cadence.test.ts +274 -0
- package/src/calls/__tests__/voice-session-bridge.test.ts +36 -0
- package/src/calls/__tests__/voice-triage-escalate.test.ts +29 -0
- package/src/calls/call-controller.ts +452 -105
- package/src/calls/call-transport.ts +16 -0
- package/src/calls/media-stream-output.ts +32 -0
- package/src/{live-voice → calls}/progress-narration.ts +2 -2
- package/src/calls/resolve-call-tts-provider.ts +33 -2
- package/src/calls/tts-call-strategy.ts +9 -5
- package/src/calls/types.ts +2 -0
- package/src/calls/voice-leg-coordinator.ts +189 -0
- package/src/calls/voice-progress-cadence.ts +423 -0
- package/src/calls/voice-session-bridge.ts +48 -31
- package/src/calls/voice-triage-escalate.ts +34 -1
- package/src/config/default-profile-catalog.ts +1 -1
- package/src/config/feature-flag-registry.json +0 -12
- package/src/config/schema.ts +2 -0
- package/src/config/schemas/__tests__/live-voice.test.ts +0 -134
- package/src/config/schemas/__tests__/voice.test.ts +148 -0
- package/src/config/schemas/live-voice.ts +1 -153
- package/src/config/schemas/voice.ts +150 -0
- package/src/live-voice/__tests__/live-voice-progress.test.ts +15 -15
- package/src/live-voice/__tests__/live-voice-vad.test.ts +2 -2
- package/src/live-voice/live-voice-session.ts +180 -530
- package/src/util/__tests__/language-subtag.test.ts +23 -1
- package/src/util/language-subtag.ts +16 -0
- package/src/workspace/migrations/155-move-front-model-config-to-voice.ts +78 -0
- package/src/workspace/migrations/__tests__/155-move-front-model-config-to-voice.test.ts +102 -0
- package/src/workspace/migrations/registry.ts +2 -0
- /package/src/{live-voice → calls}/progress-phrases.ts +0 -0
package/ARCHITECTURE.md
CHANGED
|
@@ -612,6 +612,8 @@ Transcription mode is selected once per session in `media-stream-stt-session.ts`
|
|
|
612
612
|
- **Streaming** (default): when `calls.voice.telephonyStreaming` is enabled and the `telephony` role resolves a streaming transcriber (`resolveStreamingTranscriber({ role: "telephony" })`), inbound audio is decoded (mu-law → PCM16, resampled 8 kHz → 16 kHz) and fed to the provider's realtime adapter. Replies trigger only on utterance-boundary finals (for Deepgram, `speech_final`/`UtteranceEnd`, never mid-sentence `is_final` segments), and barge-in fires from local energy VAD, never from transcriber partials.
|
|
613
613
|
- **Batch fallback**: otherwise the session segments turns with the energy-based `MediaTurnDetector` and transcribes each completed turn via the same role's batch API. Both halves of a call read the `telephony` role, which is why a role names its consumer rather than a boundary.
|
|
614
614
|
|
|
615
|
+
Every phone turn runs the same two-leg triage as live voice through `startVoiceTurn` (`src/calls/voice-session-bridge.ts`): `call-controller.ts` opens on a toolless front-door leg (`routingLeg: "front-door"`, the `voiceFrontDoor` call site) and drives it through the shared `createFrontDoorLegCoordinator` (`src/calls/voice-leg-coordinator.ts`), which reads the stream through the verdict machine and sequences the hand-off (pause narration, abort the leg, resolve and speak the bridge, mark it as the floor holder, start the escalated leg pinned to the conversation's own model, re-arm narration); each driver supplies only a host for how text and the bridge are spoken, how a leg is started or aborted, and (live voice only) the speculative hold and commit. Phone has no partial transcripts, so the hold verdict is never taught and routing is escalate-only. The controller also passes the bridge's turn callbacks (tool activity is recorded as `tool_use_started` / `tool_use_completed` call events, persisted row ids ride the `assistant_spoke` event), `launchedAtMs` for dispatch timing, and a `voiceTelemetry` bag keyed by the call session with a `phone_inbound` / `phone_outbound` entry. Both drivers share the spoken progress narration cadence (`src/calls/voice-progress-cadence.ts`, tuned by `voice.frontModel.progress`): the cadence owns the tool-activity log, the triggers (an ops burst, a long operation completing, a full interval of audible silence with news, the `maxSilenceMs` heartbeat) and the generated or static phrase, while each driver supplies its own view of audible silence (live voice from its TTS queue and playback-tail estimate; the media-stream transport from `isPlaybackIdle()` and a running sum of sent frame durations) and how to speak a phrase.
|
|
616
|
+
|
|
615
617
|
A credential preflight (`resolveTelephonyCredentialReadiness()` in `src/calls/telephony-credential-preflight.ts`) gates every call: it requires a credentialed, telephony-capable STT provider **and** a media-stream-playable TTS provider (the configured one or a credentialed playable fallback). Inbound calls that fail the preflight receive `<Say>` setup-required copy plus `<Hangup/>` instead of a doomed stream; outbound placement fails before dialing via `preflightVoiceIngress()` with the same user-facing message.
|
|
616
618
|
|
|
617
619
|
Key modules:
|
|
@@ -2189,7 +2191,7 @@ The `TtsUseCase` discriminator (`"phone-call"` or `"message-playback"`) lets pro
|
|
|
2189
2191
|
- **`native-twilio`** — the text-token path: spoken text is sent via `sendTextToken()`, which the media-stream transport re-synthesizes through daemon TTS. Collapsing this mode into `synthesized-play` is a documented deferred follow-up.
|
|
2190
2192
|
- **`synthesized-play`** — The assistant synthesises audio via the provider's HTTP API and streams it through the audio store / `sendPlayUrl()` path.
|
|
2191
2193
|
|
|
2192
|
-
**Phone call integration:** Phone calls run on the media-stream transport, where the daemon synthesises speech via the
|
|
2194
|
+
**Phone call integration:** Phone calls run on the media-stream transport, where the daemon synthesises speech via the effective TTS provider (the configured one after managed-speech defaulting, `resolveEffectiveSpeechProviders`, the same substitution live voice makes) and transcodes it to mu-law 8 kHz frames (`media-stream-output.ts`). Each catalog entry declares `mediaStreamPlayback.outputFormat` (`pcm`, `wav`, or `none`); `resolveTelephonyTtsCapability()` (`src/calls/telephony-tts-capability.ts`) combines that field with credential availability into a playable / not-playable verdict, and the call TTS resolver falls back to a credentialed playable provider rather than producing silence.
|
|
2193
2195
|
|
|
2194
2196
|
**Adding a new TTS provider (catalog-first checklist):**
|
|
2195
2197
|
|
package/package.json
CHANGED
|
@@ -998,6 +998,18 @@ describe("AssistantConfigSchema", () => {
|
|
|
998
998
|
echoEmaHalfLifeMs: 400,
|
|
999
999
|
echoDrainSlackMs: 300,
|
|
1000
1000
|
},
|
|
1001
|
+
flux: {
|
|
1002
|
+
turnEnd: { enabled: true },
|
|
1003
|
+
eotThreshold: 0.7,
|
|
1004
|
+
eotTimeoutMs: 5000,
|
|
1005
|
+
},
|
|
1006
|
+
archiveAudio: false,
|
|
1007
|
+
});
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
test("applies voice defaults", () => {
|
|
1011
|
+
const result = AssistantConfigSchema.parse({});
|
|
1012
|
+
expect(result.voice).toEqual({
|
|
1001
1013
|
frontModel: {
|
|
1002
1014
|
endpointDecisionTimeoutMs: 1200,
|
|
1003
1015
|
endpointExtensionMs: 1500,
|
|
@@ -1012,12 +1024,6 @@ describe("AssistantConfigSchema", () => {
|
|
|
1012
1024
|
generationTimeoutMs: 1500,
|
|
1013
1025
|
},
|
|
1014
1026
|
},
|
|
1015
|
-
flux: {
|
|
1016
|
-
turnEnd: { enabled: true },
|
|
1017
|
-
eotThreshold: 0.7,
|
|
1018
|
-
eotTimeoutMs: 5000,
|
|
1019
|
-
},
|
|
1020
|
-
archiveAudio: false,
|
|
1021
1027
|
});
|
|
1022
1028
|
});
|
|
1023
1029
|
|
|
@@ -455,10 +455,11 @@ describe("resolveCallTtsProvider with requiresPcmAudio", () => {
|
|
|
455
455
|
expect(result.provider?.id).toBe("elevenlabs");
|
|
456
456
|
});
|
|
457
457
|
|
|
458
|
-
test("without requiresPcmAudio the configured provider is not swapped", async () => {
|
|
458
|
+
test("without requiresPcmAudio the configured provider is not swapped for playability", async () => {
|
|
459
459
|
testConfig.services.tts.provider = "deepgram";
|
|
460
|
-
|
|
461
|
-
// media-stream playability
|
|
460
|
+
storedKeys["deepgram"] = "dg-key";
|
|
461
|
+
// The CR transport path does not consult the media-stream playability
|
|
462
|
+
// capability.
|
|
462
463
|
registerStubProvider("deepgram", async () => {
|
|
463
464
|
return { audio: Buffer.from("x"), contentType: "audio/mpeg" };
|
|
464
465
|
});
|
|
@@ -468,6 +469,37 @@ describe("resolveCallTtsProvider with requiresPcmAudio", () => {
|
|
|
468
469
|
expect(result.useSynthesizedPath).toBe(true);
|
|
469
470
|
expect(result.audioFormat).toBe("mp3");
|
|
470
471
|
});
|
|
472
|
+
|
|
473
|
+
test("managed speech stands in for a configured provider whose credential is missing", async () => {
|
|
474
|
+
testConfig.services.tts.provider = "deepgram";
|
|
475
|
+
// No deepgram key, managed speech available: the phone speaks through
|
|
476
|
+
// managed speech, the same substitution live voice makes.
|
|
477
|
+
registerStubProvider("deepgram", async () => {
|
|
478
|
+
throw new Error("should not be used");
|
|
479
|
+
});
|
|
480
|
+
registerStubProvider("vellum", async () => {
|
|
481
|
+
return { audio: Buffer.from("x"), contentType: "audio/mpeg" };
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
const result = await resolveCallTtsProvider();
|
|
485
|
+
expect(result.provider?.id).toBe("vellum");
|
|
486
|
+
expect(result.useSynthesizedPath).toBe(true);
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
test("without managed speech a credential-less configured provider is kept", async () => {
|
|
490
|
+
mockManagedSpeechAvailable = false;
|
|
491
|
+
try {
|
|
492
|
+
testConfig.services.tts.provider = "deepgram";
|
|
493
|
+
registerStubProvider("deepgram", async () => {
|
|
494
|
+
return { audio: Buffer.from("x"), contentType: "audio/mpeg" };
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
const result = await resolveCallTtsProvider();
|
|
498
|
+
expect(result.provider?.id).toBe("deepgram");
|
|
499
|
+
} finally {
|
|
500
|
+
mockManagedSpeechAvailable = true;
|
|
501
|
+
}
|
|
502
|
+
});
|
|
471
503
|
});
|
|
472
504
|
|
|
473
505
|
// ---------------------------------------------------------------------------
|
|
@@ -584,6 +616,7 @@ describe("speakSystemPrompt mid-stream failure after audio started", () => {
|
|
|
584
616
|
test("non-PCM transport: skips the native text fallback — no full-prompt re-speak", async () => {
|
|
585
617
|
testConfig.services.tts.provider = "fish-audio";
|
|
586
618
|
testConfig.services.tts.providers["fish-audio"].referenceId = "ref-123";
|
|
619
|
+
storedKeys["fish-audio"] = "fa-key";
|
|
587
620
|
|
|
588
621
|
const fishStream = jest.fn(failAfterFirstChunk());
|
|
589
622
|
registerStreamingStubProvider("fish-audio", fishStream);
|
|
@@ -657,6 +690,7 @@ describe("speakSystemPrompt aborted synthesis", () => {
|
|
|
657
690
|
test("non-PCM transport with native-fallback provider: abort skips the text fallback and end-of-turn", async () => {
|
|
658
691
|
testConfig.services.tts.provider = "fish-audio";
|
|
659
692
|
testConfig.services.tts.providers["fish-audio"].referenceId = "ref-123";
|
|
693
|
+
storedKeys["fish-audio"] = "fa-key";
|
|
660
694
|
|
|
661
695
|
const controller = new AbortController();
|
|
662
696
|
controller.abort();
|
|
@@ -742,6 +776,7 @@ describe("speakSystemPrompt aborted synthesis", () => {
|
|
|
742
776
|
test("provider AbortError without any signal is a failure — non-PCM native fallback still runs", async () => {
|
|
743
777
|
testConfig.services.tts.provider = "fish-audio";
|
|
744
778
|
testConfig.services.tts.providers["fish-audio"].referenceId = "ref-123";
|
|
779
|
+
storedKeys["fish-audio"] = "fa-key";
|
|
745
780
|
|
|
746
781
|
const fishSynthesize = jest.fn(async () => {
|
|
747
782
|
throw new DOMException("socket torn down", "AbortError");
|
|
@@ -45,6 +45,18 @@ mock.module("../../security/secure-keys.js", () => ({
|
|
|
45
45
|
: "test-key",
|
|
46
46
|
}));
|
|
47
47
|
|
|
48
|
+
// Managed-speech defaulting reaches the platform client and the credential
|
|
49
|
+
// store. The controller tests drive the bridge mock on a microtask budget, so
|
|
50
|
+
// the configured providers stand as the effective ones here.
|
|
51
|
+
mock.module("../../config/managed-speech-defaults.js", () => ({
|
|
52
|
+
resolveEffectiveSpeechProviders: async (config?: {
|
|
53
|
+
services: { stt: { provider: string }; tts: { provider: string } };
|
|
54
|
+
}) => {
|
|
55
|
+
const services = (config ?? loadConfig()).services;
|
|
56
|
+
return { stt: services.stt.provider, tts: services.tts.provider };
|
|
57
|
+
},
|
|
58
|
+
}));
|
|
59
|
+
|
|
48
60
|
mock.module("../../security/credential-key.js", () => ({
|
|
49
61
|
credentialKey: (...args: string[]) => args.join("/"),
|
|
50
62
|
}));
|
|
@@ -213,6 +225,7 @@ import { resetDbForTesting } from "../../__tests__/db-test-helpers.js";
|
|
|
213
225
|
import { createGuardianBinding } from "../../__tests__/helpers/create-guardian-binding.js";
|
|
214
226
|
import { setConfig } from "../../__tests__/helpers/set-config.js";
|
|
215
227
|
import { loadConfig } from "../../config/loader.js";
|
|
228
|
+
import type { VoiceProgressConfig } from "../../config/schemas/voice.js";
|
|
216
229
|
import { getMessages } from "../../persistence/conversation-crud.js";
|
|
217
230
|
import { getDb } from "../../persistence/db-connection.js";
|
|
218
231
|
import { initializeDb } from "../../persistence/db-init.js";
|
|
@@ -228,7 +241,13 @@ import {
|
|
|
228
241
|
updateCallSession,
|
|
229
242
|
} from "../call-store.js";
|
|
230
243
|
import type { CallTransport } from "../call-transport.js";
|
|
244
|
+
import type { VoiceProgressNarrator } from "../progress-narration.js";
|
|
231
245
|
import { resolveCallTtsProvider } from "../resolve-call-tts-provider.js";
|
|
246
|
+
import {
|
|
247
|
+
ESCALATION_CONTINUATION_CONTENT,
|
|
248
|
+
FALLBACK_ESCALATION_BRIDGE,
|
|
249
|
+
FALLBACK_ESCALATION_BRIDGE_BY_LANGUAGE,
|
|
250
|
+
} from "../voice-triage-escalate.js";
|
|
232
251
|
|
|
233
252
|
// Disable memory so persisted call messages skip background indexing, and
|
|
234
253
|
// seed the ingress base URL used for synthesized-audio play URLs.
|
|
@@ -248,6 +267,8 @@ afterAll(() => {
|
|
|
248
267
|
|
|
249
268
|
interface MockTransport extends CallTransport {
|
|
250
269
|
sentTokens: Array<{ token: string; last: boolean }>;
|
|
270
|
+
/** Tokens sent with the system-copy flag (no caller-language hint). */
|
|
271
|
+
systemCopyTokens: string[];
|
|
251
272
|
sentPlayUrls: string[];
|
|
252
273
|
endCalled: boolean;
|
|
253
274
|
endReason: string | undefined;
|
|
@@ -257,6 +278,7 @@ interface MockTransport extends CallTransport {
|
|
|
257
278
|
function createMockTransport(): MockTransport {
|
|
258
279
|
const state = {
|
|
259
280
|
sentTokens: [] as Array<{ token: string; last: boolean }>,
|
|
281
|
+
systemCopyTokens: [] as string[],
|
|
260
282
|
sentPlayUrls: [] as string[],
|
|
261
283
|
_endCalled: false,
|
|
262
284
|
_endReason: undefined as string | undefined,
|
|
@@ -267,6 +289,9 @@ function createMockTransport(): MockTransport {
|
|
|
267
289
|
get sentTokens() {
|
|
268
290
|
return state.sentTokens;
|
|
269
291
|
},
|
|
292
|
+
get systemCopyTokens() {
|
|
293
|
+
return state.systemCopyTokens;
|
|
294
|
+
},
|
|
270
295
|
get sentPlayUrls() {
|
|
271
296
|
return state.sentPlayUrls;
|
|
272
297
|
},
|
|
@@ -279,8 +304,15 @@ function createMockTransport(): MockTransport {
|
|
|
279
304
|
get cancelPendingSpeechCount() {
|
|
280
305
|
return state._cancelPendingSpeechCount;
|
|
281
306
|
},
|
|
282
|
-
sendTextToken(
|
|
307
|
+
sendTextToken(
|
|
308
|
+
token: string,
|
|
309
|
+
last: boolean,
|
|
310
|
+
opts?: { systemCopy?: boolean },
|
|
311
|
+
) {
|
|
283
312
|
state.sentTokens.push({ token, last });
|
|
313
|
+
if (opts?.systemCopy === true) {
|
|
314
|
+
state.systemCopyTokens.push(token);
|
|
315
|
+
}
|
|
284
316
|
},
|
|
285
317
|
sendPlayUrl(url: string) {
|
|
286
318
|
state.sentPlayUrls.push(url);
|
|
@@ -382,6 +414,9 @@ function setupController(
|
|
|
382
414
|
awaitPlaybackDrained?: () => Promise<void>;
|
|
383
415
|
/** Synthesis-language resolver, as the media-stream server wires it. */
|
|
384
416
|
resolveSynthesisLanguage?: () => string | undefined;
|
|
417
|
+
/** Progress narration seam; an explicit null keeps the call silent. */
|
|
418
|
+
progressNarrator?: VoiceProgressNarrator | null;
|
|
419
|
+
progressConfig?: VoiceProgressConfig;
|
|
385
420
|
},
|
|
386
421
|
) {
|
|
387
422
|
ensureConversation("conv-ctrl-test");
|
|
@@ -406,6 +441,11 @@ function setupController(
|
|
|
406
441
|
assistantId: opts?.assistantId,
|
|
407
442
|
trustContext: opts?.trustContext,
|
|
408
443
|
resolveSynthesisLanguage: opts?.resolveSynthesisLanguage,
|
|
444
|
+
// Tests that do not exercise narration keep the default narrator out of
|
|
445
|
+
// the way: a null narrator never generates or speaks.
|
|
446
|
+
progressNarrator:
|
|
447
|
+
opts?.progressNarrator === undefined ? null : opts.progressNarrator,
|
|
448
|
+
progressConfig: opts?.progressConfig,
|
|
409
449
|
});
|
|
410
450
|
return { session, relay: transport, controller };
|
|
411
451
|
}
|
|
@@ -4194,6 +4234,415 @@ describe("call-controller", () => {
|
|
|
4194
4234
|
});
|
|
4195
4235
|
});
|
|
4196
4236
|
|
|
4237
|
+
// ── Triage-and-escalate routing ─────────────────────────────────────
|
|
4238
|
+
|
|
4239
|
+
describe("triage-and-escalate routing", () => {
|
|
4240
|
+
interface LegOpts {
|
|
4241
|
+
content: string;
|
|
4242
|
+
routingLeg?: string;
|
|
4243
|
+
spokenEscalationBridge?: string;
|
|
4244
|
+
launchedAtMs?: number;
|
|
4245
|
+
voiceTelemetry?: { sessionId: string; entry?: string };
|
|
4246
|
+
callbacks?: {
|
|
4247
|
+
persisted_user_message_id?: (id: string) => void;
|
|
4248
|
+
persisted_assistant_message_id?: (id: string) => void;
|
|
4249
|
+
tool_use_start?: (
|
|
4250
|
+
toolName: string,
|
|
4251
|
+
detail?: { toolUseId?: string },
|
|
4252
|
+
) => void;
|
|
4253
|
+
tool_result?: (event: {
|
|
4254
|
+
toolName: string;
|
|
4255
|
+
toolUseId?: string;
|
|
4256
|
+
isError?: boolean;
|
|
4257
|
+
resultPreview: string;
|
|
4258
|
+
}) => void;
|
|
4259
|
+
};
|
|
4260
|
+
onTextDelta: (t: string) => void;
|
|
4261
|
+
onComplete: () => void;
|
|
4262
|
+
signal?: AbortSignal;
|
|
4263
|
+
}
|
|
4264
|
+
|
|
4265
|
+
/**
|
|
4266
|
+
* Script the bridge one leg per call: the front-door leg's tokens first,
|
|
4267
|
+
* then the escalated leg's. Records every call's options and which legs
|
|
4268
|
+
* the controller aborted.
|
|
4269
|
+
*/
|
|
4270
|
+
function scriptLegs(
|
|
4271
|
+
legs: string[][],
|
|
4272
|
+
beforeComplete?: (opts: LegOpts, index: number) => void,
|
|
4273
|
+
) {
|
|
4274
|
+
const calls: LegOpts[] = [];
|
|
4275
|
+
const aborted: number[] = [];
|
|
4276
|
+
mockStartVoiceTurn.mockImplementation(async (opts: LegOpts) => {
|
|
4277
|
+
const index = calls.length;
|
|
4278
|
+
calls.push(opts);
|
|
4279
|
+
for (const token of legs[index] ?? []) {
|
|
4280
|
+
opts.onTextDelta(token);
|
|
4281
|
+
}
|
|
4282
|
+
beforeComplete?.(opts, index);
|
|
4283
|
+
opts.onComplete();
|
|
4284
|
+
return { turnId: `run-${index}`, abort: () => aborted.push(index) };
|
|
4285
|
+
});
|
|
4286
|
+
return { calls, aborted };
|
|
4287
|
+
}
|
|
4288
|
+
|
|
4289
|
+
function spokenText(relay: MockTransport): string {
|
|
4290
|
+
return relay.sentTokens
|
|
4291
|
+
.filter((t) => !t.last)
|
|
4292
|
+
.map((t) => t.token)
|
|
4293
|
+
.join("");
|
|
4294
|
+
}
|
|
4295
|
+
|
|
4296
|
+
function assistantSpokePayload(sessionId: string): Record<string, unknown> {
|
|
4297
|
+
const spoke = getCallEvents(sessionId).filter(
|
|
4298
|
+
(e) => e.eventType === "assistant_spoke",
|
|
4299
|
+
);
|
|
4300
|
+
expect(spoke.length).toBe(1);
|
|
4301
|
+
return JSON.parse(spoke[0].payloadJson) as Record<string, unknown>;
|
|
4302
|
+
}
|
|
4303
|
+
|
|
4304
|
+
test("a front-door answer runs one leg and is spoken verbatim", async () => {
|
|
4305
|
+
const { calls, aborted } = scriptLegs([["Hello", " there."]]);
|
|
4306
|
+
const { session, relay, controller } = setupController();
|
|
4307
|
+
|
|
4308
|
+
await controller.handleCallerUtterance("Hi");
|
|
4309
|
+
|
|
4310
|
+
expect(calls.length).toBe(1);
|
|
4311
|
+
expect(calls[0].routingLeg).toBe("front-door");
|
|
4312
|
+
expect(calls[0].content).toBe("Hi");
|
|
4313
|
+
expect(calls[0].spokenEscalationBridge).toBeUndefined();
|
|
4314
|
+
expect(aborted).toEqual([]);
|
|
4315
|
+
expect(spokenText(relay)).toContain("Hello there.");
|
|
4316
|
+
expect(assistantSpokePayload(session.id).text).toBe("Hello there.");
|
|
4317
|
+
|
|
4318
|
+
controller.destroy();
|
|
4319
|
+
});
|
|
4320
|
+
|
|
4321
|
+
test("an escalate verdict speaks the holding phrase, aborts the front-door leg, and continues on the escalated leg", async () => {
|
|
4322
|
+
const { calls, aborted } = scriptLegs([
|
|
4323
|
+
// The verdict token may arrive split across deltas; the leading
|
|
4324
|
+
// "[" is held until it classifies.
|
|
4325
|
+
["[", "1] Let me check", " that.", " Ignored past the cap."],
|
|
4326
|
+
["The answer is 42."],
|
|
4327
|
+
]);
|
|
4328
|
+
const { session, relay, controller } = setupController();
|
|
4329
|
+
|
|
4330
|
+
await controller.handleCallerUtterance("What is six times seven?");
|
|
4331
|
+
|
|
4332
|
+
expect(calls.length).toBe(2);
|
|
4333
|
+
expect(calls[0].routingLeg).toBe("front-door");
|
|
4334
|
+
expect(calls[1].routingLeg).toBe("escalated");
|
|
4335
|
+
expect(calls[1].content).toBe(ESCALATION_CONTINUATION_CONTENT);
|
|
4336
|
+
expect(calls[1].spokenEscalationBridge).toBe("Let me check that.");
|
|
4337
|
+
expect(aborted).toEqual([0]);
|
|
4338
|
+
|
|
4339
|
+
const spoken = spokenText(relay);
|
|
4340
|
+
expect(spoken).toContain("Let me check that.");
|
|
4341
|
+
expect(spoken).toContain("The answer is 42.");
|
|
4342
|
+
expect(spoken.indexOf("Let me check that.")).toBeLessThan(
|
|
4343
|
+
spoken.indexOf("The answer is 42."),
|
|
4344
|
+
);
|
|
4345
|
+
expect(spoken).not.toContain("[1]");
|
|
4346
|
+
expect(spoken).not.toContain("Ignored past the cap.");
|
|
4347
|
+
expect(assistantSpokePayload(session.id).text).toBe(
|
|
4348
|
+
"Let me check that. The answer is 42.",
|
|
4349
|
+
);
|
|
4350
|
+
// One end-of-turn signal for the whole turn, after the escalated leg.
|
|
4351
|
+
expect(relay.sentTokens.filter((t) => t.last).length).toBe(1);
|
|
4352
|
+
|
|
4353
|
+
controller.destroy();
|
|
4354
|
+
});
|
|
4355
|
+
|
|
4356
|
+
test("a bare escalate verdict speaks the canned bridge, audio-only", async () => {
|
|
4357
|
+
const { calls } = scriptLegs([["[1]"], ["Forty-two."]]);
|
|
4358
|
+
const { session, relay, controller } = setupController();
|
|
4359
|
+
|
|
4360
|
+
await controller.handleCallerUtterance("What is six times seven?");
|
|
4361
|
+
|
|
4362
|
+
expect(calls.length).toBe(2);
|
|
4363
|
+
expect(calls[1].spokenEscalationBridge).toBe(FALLBACK_ESCALATION_BRIDGE);
|
|
4364
|
+
expect(spokenText(relay)).toContain(FALLBACK_ESCALATION_BRIDGE);
|
|
4365
|
+
// The canned bridge is not the model's speech: the turn's text (and the
|
|
4366
|
+
// transcript row the bridge keeps) carry only the escalated answer.
|
|
4367
|
+
expect(assistantSpokePayload(session.id).text).toBe("Forty-two.");
|
|
4368
|
+
|
|
4369
|
+
controller.destroy();
|
|
4370
|
+
});
|
|
4371
|
+
|
|
4372
|
+
test("a leg cancelled mid-bridge never spawns the escalated leg", async () => {
|
|
4373
|
+
const calls: LegOpts[] = [];
|
|
4374
|
+
let releaseLeg: () => void = () => {};
|
|
4375
|
+
mockStartVoiceTurn.mockImplementation(async (opts: LegOpts) => {
|
|
4376
|
+
calls.push(opts);
|
|
4377
|
+
opts.onTextDelta("[1] Let me");
|
|
4378
|
+
await new Promise<void>((resolve) => {
|
|
4379
|
+
releaseLeg = resolve;
|
|
4380
|
+
opts.signal?.addEventListener("abort", () => resolve());
|
|
4381
|
+
});
|
|
4382
|
+
// The bridge reports a cancelled leg's completion too.
|
|
4383
|
+
opts.onComplete();
|
|
4384
|
+
return { turnId: "run-cancelled", abort: () => releaseLeg() };
|
|
4385
|
+
});
|
|
4386
|
+
const { relay, controller } = setupController();
|
|
4387
|
+
|
|
4388
|
+
const turnPromise = controller.handleCallerUtterance("Hi");
|
|
4389
|
+
for (let i = 0; i < 10; i++) {
|
|
4390
|
+
await Promise.resolve();
|
|
4391
|
+
}
|
|
4392
|
+
expect(calls.length).toBe(1);
|
|
4393
|
+
|
|
4394
|
+
controller.handleInterrupt();
|
|
4395
|
+
await turnPromise;
|
|
4396
|
+
|
|
4397
|
+
expect(calls.length).toBe(1);
|
|
4398
|
+
expect(spokenText(relay)).toBe("");
|
|
4399
|
+
|
|
4400
|
+
controller.destroy();
|
|
4401
|
+
});
|
|
4402
|
+
|
|
4403
|
+
test("passes the launch time, phone telemetry, and callbacks to the bridge", async () => {
|
|
4404
|
+
const before = Date.now();
|
|
4405
|
+
const { calls } = scriptLegs([["Done."]], (opts) => {
|
|
4406
|
+
opts.callbacks?.persisted_user_message_id?.("user-row-1");
|
|
4407
|
+
opts.callbacks?.tool_use_start?.("web_search", { toolUseId: "tu-1" });
|
|
4408
|
+
opts.callbacks?.tool_result?.({
|
|
4409
|
+
toolName: "web_search",
|
|
4410
|
+
toolUseId: "tu-1",
|
|
4411
|
+
isError: false,
|
|
4412
|
+
resultPreview: "three results",
|
|
4413
|
+
});
|
|
4414
|
+
opts.callbacks?.persisted_assistant_message_id?.("assistant-row-1");
|
|
4415
|
+
});
|
|
4416
|
+
const { session, controller } = setupController();
|
|
4417
|
+
|
|
4418
|
+
await controller.handleCallerUtterance("Look that up");
|
|
4419
|
+
|
|
4420
|
+
expect(calls.length).toBe(1);
|
|
4421
|
+
expect(calls[0].launchedAtMs).toBeGreaterThanOrEqual(before);
|
|
4422
|
+
expect(calls[0].launchedAtMs).toBeLessThanOrEqual(Date.now());
|
|
4423
|
+
expect(calls[0].voiceTelemetry).toEqual({
|
|
4424
|
+
sessionId: session.id,
|
|
4425
|
+
entry: "phone_inbound",
|
|
4426
|
+
});
|
|
4427
|
+
|
|
4428
|
+
const events = getCallEvents(session.id);
|
|
4429
|
+
const started = events.filter((e) => e.eventType === "tool_use_started");
|
|
4430
|
+
expect(started.length).toBe(1);
|
|
4431
|
+
expect(JSON.parse(started[0].payloadJson)).toEqual({
|
|
4432
|
+
toolName: "web_search",
|
|
4433
|
+
toolUseId: "tu-1",
|
|
4434
|
+
});
|
|
4435
|
+
const completed = events.filter(
|
|
4436
|
+
(e) => e.eventType === "tool_use_completed",
|
|
4437
|
+
);
|
|
4438
|
+
expect(completed.length).toBe(1);
|
|
4439
|
+
expect(JSON.parse(completed[0].payloadJson)).toEqual({
|
|
4440
|
+
toolName: "web_search",
|
|
4441
|
+
toolUseId: "tu-1",
|
|
4442
|
+
isError: false,
|
|
4443
|
+
resultPreview: "three results",
|
|
4444
|
+
});
|
|
4445
|
+
expect(assistantSpokePayload(session.id)).toEqual({
|
|
4446
|
+
text: "Done.",
|
|
4447
|
+
userMessageId: "user-row-1",
|
|
4448
|
+
assistantMessageId: "assistant-row-1",
|
|
4449
|
+
});
|
|
4450
|
+
|
|
4451
|
+
controller.destroy();
|
|
4452
|
+
});
|
|
4453
|
+
|
|
4454
|
+
test("the escalated leg's hidden continuation row is not the caller's utterance", async () => {
|
|
4455
|
+
const { calls } = scriptLegs(
|
|
4456
|
+
[["[1] One moment."], ["Here you go."]],
|
|
4457
|
+
(opts, index) => {
|
|
4458
|
+
opts.callbacks?.persisted_user_message_id?.(`user-row-${index}`);
|
|
4459
|
+
opts.callbacks?.persisted_assistant_message_id?.(
|
|
4460
|
+
`assistant-row-${index}`,
|
|
4461
|
+
);
|
|
4462
|
+
},
|
|
4463
|
+
);
|
|
4464
|
+
const { session, controller } = setupController();
|
|
4465
|
+
|
|
4466
|
+
await controller.handleCallerUtterance("Hi");
|
|
4467
|
+
|
|
4468
|
+
expect(calls.length).toBe(2);
|
|
4469
|
+
expect(assistantSpokePayload(session.id)).toEqual({
|
|
4470
|
+
text: "One moment. Here you go.",
|
|
4471
|
+
userMessageId: "user-row-0",
|
|
4472
|
+
assistantMessageId: "assistant-row-1",
|
|
4473
|
+
});
|
|
4474
|
+
|
|
4475
|
+
controller.destroy();
|
|
4476
|
+
});
|
|
4477
|
+
|
|
4478
|
+
test("native route: a canned bridge in a language the table lacks goes out as system copy", async () => {
|
|
4479
|
+
scriptLegs([["[1]"], ["Forty-two."]]);
|
|
4480
|
+
const { relay, controller } = setupController(undefined, {
|
|
4481
|
+
resolveSynthesisLanguage: () => "ko",
|
|
4482
|
+
});
|
|
4483
|
+
|
|
4484
|
+
await controller.handleCallerUtterance("What is six times seven?");
|
|
4485
|
+
|
|
4486
|
+
// The English fallback must not ride the caller's Korean hint.
|
|
4487
|
+
expect(relay.systemCopyTokens).toEqual([
|
|
4488
|
+
`${FALLBACK_ESCALATION_BRIDGE} `,
|
|
4489
|
+
]);
|
|
4490
|
+
expect(spokenText(relay)).toContain("Forty-two.");
|
|
4491
|
+
|
|
4492
|
+
controller.destroy();
|
|
4493
|
+
});
|
|
4494
|
+
|
|
4495
|
+
test("native route: a localized canned bridge rides the caller's language like model text", async () => {
|
|
4496
|
+
scriptLegs([["[1]"], ["Cuarenta y dos."]]);
|
|
4497
|
+
const { relay, controller } = setupController(undefined, {
|
|
4498
|
+
resolveSynthesisLanguage: () => "es",
|
|
4499
|
+
});
|
|
4500
|
+
|
|
4501
|
+
await controller.handleCallerUtterance("¿Cuánto es seis por siete?");
|
|
4502
|
+
|
|
4503
|
+
expect(relay.systemCopyTokens).toEqual([]);
|
|
4504
|
+
expect(spokenText(relay)).toContain(
|
|
4505
|
+
FALLBACK_ESCALATION_BRIDGE_BY_LANGUAGE.es,
|
|
4506
|
+
);
|
|
4507
|
+
|
|
4508
|
+
controller.destroy();
|
|
4509
|
+
});
|
|
4510
|
+
|
|
4511
|
+
test("synthesized route: the canned bridge carries its own language hint, the answer the caller's", async () => {
|
|
4512
|
+
const requests: Array<{ text: string; language: string | undefined }> =
|
|
4513
|
+
[];
|
|
4514
|
+
registerFishAudioSegmentRecorder({
|
|
4515
|
+
onSynthesizeStream: async (text, request) => {
|
|
4516
|
+
requests.push({ text, language: request.language });
|
|
4517
|
+
},
|
|
4518
|
+
});
|
|
4519
|
+
scriptLegs([["[1]"], ["Forty-two."]]);
|
|
4520
|
+
const { controller } = setupController(undefined, {
|
|
4521
|
+
resolveSynthesisLanguage: () => "ko",
|
|
4522
|
+
});
|
|
4523
|
+
|
|
4524
|
+
await controller.handleCallerUtterance("What is six times seven?");
|
|
4525
|
+
|
|
4526
|
+
expect(requests).toEqual([
|
|
4527
|
+
{ text: FALLBACK_ESCALATION_BRIDGE, language: "en" },
|
|
4528
|
+
{ text: "Forty-two.", language: "ko" },
|
|
4529
|
+
]);
|
|
4530
|
+
|
|
4531
|
+
controller.destroy();
|
|
4532
|
+
});
|
|
4533
|
+
|
|
4534
|
+
test("progress narration speaks into the escalated leg's tool loop, audio-only", async () => {
|
|
4535
|
+
const narrationConfig: VoiceProgressConfig = {
|
|
4536
|
+
enabled: true,
|
|
4537
|
+
opsThreshold: 1,
|
|
4538
|
+
idleIntervalMs: 60_000,
|
|
4539
|
+
maxSilenceMs: 60_000,
|
|
4540
|
+
longOpMs: 15_000,
|
|
4541
|
+
minGapMs: 10,
|
|
4542
|
+
generationTimeoutMs: 1_500,
|
|
4543
|
+
};
|
|
4544
|
+
const narrator: VoiceProgressNarrator = {
|
|
4545
|
+
generateProgressText: async (input) => {
|
|
4546
|
+
expect(input.transcriptSoFar).toBe("What is on my calendar?");
|
|
4547
|
+
expect(input.currentOp?.toolName).toBe("calendar_list");
|
|
4548
|
+
return "Pulling up your calendar now.";
|
|
4549
|
+
},
|
|
4550
|
+
};
|
|
4551
|
+
const { session, relay, controller } = setupController(undefined, {
|
|
4552
|
+
progressNarrator: narrator,
|
|
4553
|
+
progressConfig: narrationConfig,
|
|
4554
|
+
});
|
|
4555
|
+
const calls: LegOpts[] = [];
|
|
4556
|
+
mockStartVoiceTurn.mockImplementation(async (opts: LegOpts) => {
|
|
4557
|
+
const index = calls.length;
|
|
4558
|
+
calls.push(opts);
|
|
4559
|
+
if (index === 0) {
|
|
4560
|
+
opts.onTextDelta("[1] One moment.");
|
|
4561
|
+
opts.onComplete();
|
|
4562
|
+
return { turnId: "run-front", abort: () => {} };
|
|
4563
|
+
}
|
|
4564
|
+
// The escalated leg runs a tool; narration must land while it runs.
|
|
4565
|
+
// The bridge just spoken is a floor holder, so the tool starts after
|
|
4566
|
+
// the (test-short) minimum gap.
|
|
4567
|
+
await new Promise((r) => setTimeout(r, 25));
|
|
4568
|
+
opts.callbacks?.tool_use_start?.("calendar_list", { toolUseId: "c1" });
|
|
4569
|
+
await pollUntil(() =>
|
|
4570
|
+
relay.sentTokens.some((t) =>
|
|
4571
|
+
t.token.includes("Pulling up your calendar now."),
|
|
4572
|
+
),
|
|
4573
|
+
);
|
|
4574
|
+
opts.callbacks?.tool_result?.({
|
|
4575
|
+
toolName: "calendar_list",
|
|
4576
|
+
toolUseId: "c1",
|
|
4577
|
+
resultPreview: "two events",
|
|
4578
|
+
});
|
|
4579
|
+
opts.onTextDelta("You have two events today.");
|
|
4580
|
+
opts.onComplete();
|
|
4581
|
+
return { turnId: "run-escalated", abort: () => {} };
|
|
4582
|
+
});
|
|
4583
|
+
|
|
4584
|
+
await controller.handleCallerUtterance("What is on my calendar?");
|
|
4585
|
+
|
|
4586
|
+
const spoken = spokenText(relay);
|
|
4587
|
+
expect(spoken.indexOf("One moment.")).toBeLessThan(
|
|
4588
|
+
spoken.indexOf("Pulling up your calendar now."),
|
|
4589
|
+
);
|
|
4590
|
+
expect(spoken.indexOf("Pulling up your calendar now.")).toBeLessThan(
|
|
4591
|
+
spoken.indexOf("You have two events today."),
|
|
4592
|
+
);
|
|
4593
|
+
// Narration is audio-only: the turn's recorded text is the model's.
|
|
4594
|
+
expect(assistantSpokePayload(session.id).text).toBe(
|
|
4595
|
+
"One moment. You have two events today.",
|
|
4596
|
+
);
|
|
4597
|
+
|
|
4598
|
+
controller.destroy();
|
|
4599
|
+
});
|
|
4600
|
+
|
|
4601
|
+
test("no narration without a narrator", async () => {
|
|
4602
|
+
const { relay, controller } = setupController(undefined, {
|
|
4603
|
+
progressNarrator: null,
|
|
4604
|
+
progressConfig: {
|
|
4605
|
+
enabled: true,
|
|
4606
|
+
opsThreshold: 1,
|
|
4607
|
+
idleIntervalMs: 60_000,
|
|
4608
|
+
maxSilenceMs: 60_000,
|
|
4609
|
+
longOpMs: 15_000,
|
|
4610
|
+
minGapMs: 10,
|
|
4611
|
+
generationTimeoutMs: 1_500,
|
|
4612
|
+
},
|
|
4613
|
+
});
|
|
4614
|
+
scriptLegs([["Done."]], (opts) => {
|
|
4615
|
+
opts.callbacks?.tool_use_start?.("web_search", { toolUseId: "t1" });
|
|
4616
|
+
opts.callbacks?.tool_result?.({
|
|
4617
|
+
toolName: "web_search",
|
|
4618
|
+
toolUseId: "t1",
|
|
4619
|
+
resultPreview: "ok",
|
|
4620
|
+
});
|
|
4621
|
+
});
|
|
4622
|
+
|
|
4623
|
+
await controller.handleCallerUtterance("Look it up");
|
|
4624
|
+
|
|
4625
|
+
expect(spokenText(relay)).toBe("Done.");
|
|
4626
|
+
|
|
4627
|
+
controller.destroy();
|
|
4628
|
+
});
|
|
4629
|
+
|
|
4630
|
+
test("an outbound call reports the outbound entry", async () => {
|
|
4631
|
+
const { calls } = scriptLegs([["Hello, this is Ava."]]);
|
|
4632
|
+
const { session, controller } = setupController("Book a table for two");
|
|
4633
|
+
|
|
4634
|
+
await controller.startInitialGreeting();
|
|
4635
|
+
|
|
4636
|
+
expect(calls.length).toBe(1);
|
|
4637
|
+
expect(calls[0].voiceTelemetry).toEqual({
|
|
4638
|
+
sessionId: session.id,
|
|
4639
|
+
entry: "phone_outbound",
|
|
4640
|
+
});
|
|
4641
|
+
|
|
4642
|
+
controller.destroy();
|
|
4643
|
+
});
|
|
4644
|
+
});
|
|
4645
|
+
|
|
4197
4646
|
// ── handleBargeIn ───────────────────────────────────────────────────
|
|
4198
4647
|
|
|
4199
4648
|
describe("handleBargeIn", () => {
|