@vellumai/assistant 0.12.0-dev.202609111617.f60422f → 0.12.0-dev.202609111819.3dcbf21
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 +1 -1
- package/package.json +1 -1
- package/src/__tests__/config-schema.test.ts +12 -6
- package/src/calls/__tests__/call-controller.test.ts +106 -0
- 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/call-controller.ts +190 -113
- 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/voice-leg-coordinator.ts +189 -0
- package/src/calls/voice-progress-cadence.ts +423 -0
- package/src/config/default-profile-catalog.ts +1 -1
- 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 +178 -521
- 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
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
getRequestByPendingQuestionOrNull,
|
|
18
18
|
listGuardianRequestDeliveriesOrEmpty,
|
|
19
19
|
} from "../channels/gateway-guardian-requests.js";
|
|
20
|
+
import { loadConfig } from "../config/loader.js";
|
|
21
|
+
import type { VoiceProgressConfig } from "../config/schemas/voice.js";
|
|
20
22
|
import type { TrustContext } from "../daemon/trust-context-types.js";
|
|
21
23
|
import { DAEMON_INTERNAL_ASSISTANT_ID } from "../runtime/assistant-scope.js";
|
|
22
24
|
import { computeToolApprovalDigest } from "../security/tool-approval-digest.js";
|
|
@@ -63,6 +65,10 @@ import type { CallTransport } from "./call-transport.js";
|
|
|
63
65
|
import { finalizeCall } from "./finalize-call.js";
|
|
64
66
|
import { sendGuardianExpiryNotices } from "./guardian-action-sweep.js";
|
|
65
67
|
import { dispatchGuardianQuestion } from "./guardian-dispatch.js";
|
|
68
|
+
import {
|
|
69
|
+
createVoiceProgressNarrator,
|
|
70
|
+
type VoiceProgressNarrator,
|
|
71
|
+
} from "./progress-narration.js";
|
|
66
72
|
import {
|
|
67
73
|
findPlayableTelephonyTtsFallbackProvider,
|
|
68
74
|
resolveCallTtsProvider,
|
|
@@ -83,18 +89,19 @@ import {
|
|
|
83
89
|
extractBalancedJson,
|
|
84
90
|
stripInternalSpeechMarkers,
|
|
85
91
|
} from "./voice-control-protocol.js";
|
|
92
|
+
import {
|
|
93
|
+
createFrontDoorLegCoordinator,
|
|
94
|
+
type FrontDoorLegCoordinator,
|
|
95
|
+
type SpokenEscalationBridge,
|
|
96
|
+
} from "./voice-leg-coordinator.js";
|
|
97
|
+
import { createProgressCadence } from "./voice-progress-cadence.js";
|
|
86
98
|
import {
|
|
87
99
|
CONVERSATION_BUSY_MESSAGE,
|
|
88
100
|
startVoiceTurn,
|
|
89
101
|
type VoiceTurnCallbacks,
|
|
90
102
|
type VoiceTurnHandle,
|
|
91
103
|
} from "./voice-session-bridge.js";
|
|
92
|
-
import {
|
|
93
|
-
createFrontDoorVerdictMachine,
|
|
94
|
-
ESCALATION_CONTINUATION_CONTENT,
|
|
95
|
-
resolveSpokenEscalationBridge,
|
|
96
|
-
type VoiceRoutingLeg,
|
|
97
|
-
} from "./voice-triage-escalate.js";
|
|
104
|
+
import { type VoiceRoutingLeg } from "./voice-triage-escalate.js";
|
|
98
105
|
|
|
99
106
|
const log = getLogger("call-controller");
|
|
100
107
|
|
|
@@ -236,6 +243,11 @@ export class CallController {
|
|
|
236
243
|
* resolution only.
|
|
237
244
|
*/
|
|
238
245
|
private resolveSynthesisLanguage: () => string | undefined;
|
|
246
|
+
/** Spoken progress narration (voice.frontModel.progress), null when off. */
|
|
247
|
+
private readonly progressNarrator: VoiceProgressNarrator | null;
|
|
248
|
+
private readonly progressConfig: VoiceProgressConfig;
|
|
249
|
+
/** Rotates the static narration fallback across turns. */
|
|
250
|
+
private progressPhraseCounter = 0;
|
|
239
251
|
|
|
240
252
|
constructor(
|
|
241
253
|
callSessionId: string,
|
|
@@ -246,6 +258,9 @@ export class CallController {
|
|
|
246
258
|
assistantId?: string;
|
|
247
259
|
trustContext?: TrustContext;
|
|
248
260
|
resolveSynthesisLanguage?: () => string | undefined;
|
|
261
|
+
/** Test seam: an explicit null disables narration for the call. */
|
|
262
|
+
progressNarrator?: VoiceProgressNarrator | null;
|
|
263
|
+
progressConfig?: VoiceProgressConfig;
|
|
249
264
|
},
|
|
250
265
|
) {
|
|
251
266
|
this.callSessionId = callSessionId;
|
|
@@ -258,6 +273,12 @@ export class CallController {
|
|
|
258
273
|
this.resolveSynthesisLanguage =
|
|
259
274
|
opts?.resolveSynthesisLanguage ??
|
|
260
275
|
(() => resolveTelephonySynthesisLanguage());
|
|
276
|
+
this.progressConfig =
|
|
277
|
+
opts?.progressConfig ?? loadConfig().voice.frontModel.progress;
|
|
278
|
+
this.progressNarrator =
|
|
279
|
+
opts?.progressNarrator !== undefined
|
|
280
|
+
? opts.progressNarrator
|
|
281
|
+
: createVoiceProgressNarrator({ config: this.progressConfig });
|
|
261
282
|
|
|
262
283
|
// Resolve the conversation ID and skipDisclosure from the call session
|
|
263
284
|
const session = getCallSession(callSessionId);
|
|
@@ -765,6 +786,12 @@ export class CallController {
|
|
|
765
786
|
userMessageId: null,
|
|
766
787
|
assistantMessageId: null,
|
|
767
788
|
};
|
|
789
|
+
// Audio bookkeeping for progress narration: segments handed to synthesis
|
|
790
|
+
// but not yet emitted, text deltas seen (a narration generated while the
|
|
791
|
+
// model was already speaking is stale), and whether the turn is over.
|
|
792
|
+
let segmentsInFlight = 0;
|
|
793
|
+
let deltaEpoch = 0;
|
|
794
|
+
let turnSettled = false;
|
|
768
795
|
|
|
769
796
|
// Synthesized path: text is split at speakable boundaries as it streams
|
|
770
797
|
// and each segment is synthesized while the LLM keeps generating. The
|
|
@@ -855,15 +882,16 @@ export class CallController {
|
|
|
855
882
|
continue;
|
|
856
883
|
}
|
|
857
884
|
firstSynthSegmentEnqueued = true;
|
|
885
|
+
segmentsInFlight += 1;
|
|
858
886
|
synthesisChain = synthesisChain.then(async () => {
|
|
859
|
-
if (
|
|
860
|
-
!this.isCurrentRun(runVersion) ||
|
|
861
|
-
synthesisFailure ||
|
|
862
|
-
synthesisCancelled
|
|
863
|
-
) {
|
|
864
|
-
return;
|
|
865
|
-
}
|
|
866
887
|
try {
|
|
888
|
+
if (
|
|
889
|
+
!this.isCurrentRun(runVersion) ||
|
|
890
|
+
synthesisFailure ||
|
|
891
|
+
synthesisCancelled
|
|
892
|
+
) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
867
895
|
if (!synthesisFellBack) {
|
|
868
896
|
const status = await this.synthesizeAndStreamAudio(
|
|
869
897
|
ttsProvider,
|
|
@@ -899,6 +927,11 @@ export class CallController {
|
|
|
899
927
|
await speakSegmentViaPcmFallback(ttsProvider.id, segment, language);
|
|
900
928
|
} catch (err) {
|
|
901
929
|
synthesisFailure = { err };
|
|
930
|
+
} finally {
|
|
931
|
+
segmentsInFlight -= 1;
|
|
932
|
+
// The segment's audio has been handed off; the transport's
|
|
933
|
+
// playback tail covers what is still playing.
|
|
934
|
+
cadence.noteAudioSettled();
|
|
902
935
|
}
|
|
903
936
|
});
|
|
904
937
|
}
|
|
@@ -924,6 +957,7 @@ export class CallController {
|
|
|
924
957
|
}
|
|
925
958
|
this.beginSpeakingOnAudioStart(runVersion);
|
|
926
959
|
this.transport.sendTextToken(cleaned, false);
|
|
960
|
+
cadence.noteAudioSettled();
|
|
927
961
|
}
|
|
928
962
|
};
|
|
929
963
|
|
|
@@ -935,21 +969,62 @@ export class CallController {
|
|
|
935
969
|
* (on the native route that is the transport's system-copy contract);
|
|
936
970
|
* undefined rides the turn's language like model text.
|
|
937
971
|
*/
|
|
938
|
-
const speakFixedPhrase = (
|
|
972
|
+
const speakFixedPhrase = (
|
|
973
|
+
text: string,
|
|
974
|
+
language: string | undefined,
|
|
975
|
+
): boolean => {
|
|
939
976
|
const cleaned = sanitizeForTts(text).trim();
|
|
940
977
|
if (cleaned.length === 0) {
|
|
941
|
-
return;
|
|
978
|
+
return false;
|
|
942
979
|
}
|
|
943
980
|
if (synthProvider) {
|
|
944
981
|
enqueueSynthesisSegments(synthProvider, [cleaned], language);
|
|
945
|
-
return;
|
|
982
|
+
return true;
|
|
946
983
|
}
|
|
947
984
|
this.beginSpeakingOnAudioStart(runVersion);
|
|
948
985
|
this.transport.sendTextToken(`${cleaned} `, false, {
|
|
949
986
|
systemCopy: language !== undefined,
|
|
950
987
|
});
|
|
988
|
+
cadence.noteAudioSettled();
|
|
989
|
+
return true;
|
|
951
990
|
};
|
|
952
991
|
|
|
992
|
+
// Spoken progress narration for the turn's dead air (tool loops on the
|
|
993
|
+
// escalated leg). The shared cadence owns the timing; this host supplies
|
|
994
|
+
// the phone's view of audible silence: nothing buffered toward a
|
|
995
|
+
// sentence, no segment mid-synthesis, and the transport's queue empty
|
|
996
|
+
// with its playback-tail estimate expired.
|
|
997
|
+
const callerTranscript =
|
|
998
|
+
content === CALL_OPENING_MARKER ||
|
|
999
|
+
content === CALL_VERIFICATION_COMPLETE_MARKER
|
|
1000
|
+
? ""
|
|
1001
|
+
: content;
|
|
1002
|
+
const cadence = createProgressCadence({
|
|
1003
|
+
config: this.progressConfig,
|
|
1004
|
+
narrator: this.progressNarrator,
|
|
1005
|
+
nextFallbackPhraseIndex: () => this.progressPhraseCounter++,
|
|
1006
|
+
host: {
|
|
1007
|
+
turnId: `${this.callSessionId}#${runVersion}`,
|
|
1008
|
+
launchedAtMs,
|
|
1009
|
+
signal: runSignal,
|
|
1010
|
+
canNarrate: () => this.isCurrentRun(runVersion) && !turnSettled,
|
|
1011
|
+
isAudioIdle: () =>
|
|
1012
|
+
pendingSynthText.length === 0 &&
|
|
1013
|
+
segmentsInFlight === 0 &&
|
|
1014
|
+
(this.transport.isPlaybackIdle?.() ?? true),
|
|
1015
|
+
playbackTailUntilMs: () => this.transport.playbackTailUntilMs?.() ?? 0,
|
|
1016
|
+
transcriptSoFar: () => callerTranscript,
|
|
1017
|
+
language: () => this.resolveSynthesisLanguage(),
|
|
1018
|
+
deltaEpoch: () => deltaEpoch,
|
|
1019
|
+
speak: speakFixedPhrase,
|
|
1020
|
+
},
|
|
1021
|
+
});
|
|
1022
|
+
const settleTurn = (): void => {
|
|
1023
|
+
turnSettled = true;
|
|
1024
|
+
cadence.clear();
|
|
1025
|
+
};
|
|
1026
|
+
runSignal.addEventListener("abort", settleTurn, { once: true });
|
|
1027
|
+
|
|
953
1028
|
// Speech goes out through the shared control-marker holdback: text up to
|
|
954
1029
|
// a possibly-streaming marker flushes, the marker itself is stripped, and
|
|
955
1030
|
// ordinary bracketed text ("[A]", "[note]") never stalls TTS. Marker
|
|
@@ -964,8 +1039,13 @@ export class CallController {
|
|
|
964
1039
|
|
|
965
1040
|
// Transcript row ids reported by the bridge. The escalated leg persists
|
|
966
1041
|
// a hidden continuation prompt rather than the caller's words, so only
|
|
967
|
-
// the front-door leg's user row is the utterance
|
|
968
|
-
|
|
1042
|
+
// the front-door leg's user row is the utterance; and a front-door leg
|
|
1043
|
+
// that handed off owns the bridge row, not the answer, so its assistant
|
|
1044
|
+
// row is not the turn's.
|
|
1045
|
+
const legCallbacks = (
|
|
1046
|
+
routingLeg: VoiceRoutingLeg,
|
|
1047
|
+
handedOff: () => boolean,
|
|
1048
|
+
): VoiceTurnCallbacks => ({
|
|
969
1049
|
persisted_user_message_id: (messageId) => {
|
|
970
1050
|
if (!this.isCurrentRun(runVersion) || routingLeg === "escalated") {
|
|
971
1051
|
return;
|
|
@@ -973,7 +1053,7 @@ export class CallController {
|
|
|
973
1053
|
outcome.userMessageId = messageId;
|
|
974
1054
|
},
|
|
975
1055
|
persisted_assistant_message_id: (messageId) => {
|
|
976
|
-
if (!this.isCurrentRun(runVersion)) {
|
|
1056
|
+
if (!this.isCurrentRun(runVersion) || handedOff()) {
|
|
977
1057
|
return;
|
|
978
1058
|
}
|
|
979
1059
|
outcome.assistantMessageId = messageId;
|
|
@@ -991,6 +1071,7 @@ export class CallController {
|
|
|
991
1071
|
? { toolUseId: detail.toolUseId }
|
|
992
1072
|
: {}),
|
|
993
1073
|
});
|
|
1074
|
+
cadence.toolStarted(toolName, detail?.toolUseId);
|
|
994
1075
|
},
|
|
995
1076
|
tool_result: (event) => {
|
|
996
1077
|
if (!this.isCurrentRun(runVersion)) {
|
|
@@ -1004,15 +1085,43 @@ export class CallController {
|
|
|
1004
1085
|
...(event.isError !== undefined ? { isError: event.isError } : {}),
|
|
1005
1086
|
resultPreview: event.resultPreview,
|
|
1006
1087
|
});
|
|
1088
|
+
cadence.toolFinished(event);
|
|
1007
1089
|
},
|
|
1008
1090
|
});
|
|
1009
1091
|
|
|
1092
|
+
// Speak the escalation bridge so the strong-model call has no dead air.
|
|
1093
|
+
// The model's own holding phrase is real assistant speech and stays in
|
|
1094
|
+
// the turn's text, matching the row the bridge's transcript hygiene
|
|
1095
|
+
// keeps for it; the canned fallback is audio-only, matching the row it
|
|
1096
|
+
// deletes.
|
|
1097
|
+
const speakEscalationBridge = (bridge: SpokenEscalationBridge): void => {
|
|
1098
|
+
if (bridge.usesFallback) {
|
|
1099
|
+
speakFixedPhrase(bridge.spokenBridge, bridge.language);
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
fullResponseText += `${bridge.spokenBridge} `;
|
|
1103
|
+
flushSafeText(fullResponseText, { force: true });
|
|
1104
|
+
// Force-synthesize the bridge now. On the synthesized-TTS path text is
|
|
1105
|
+
// held in pendingSynthText until a sentence boundary, so an
|
|
1106
|
+
// unpunctuated bridge would otherwise sit unspoken until the post-leg
|
|
1107
|
+
// drain, leaving the caller in silence during the escalated model's
|
|
1108
|
+
// call: the exact gap the bridge exists to mask.
|
|
1109
|
+
if (synthProvider) {
|
|
1110
|
+
const { segments } = extractSpeakableSegments(pendingSynthText, true);
|
|
1111
|
+
pendingSynthText = "";
|
|
1112
|
+
enqueueSynthesisSegments(synthProvider, segments);
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
|
|
1116
|
+
// The escalated leg, once the front-door leg hands off. Started from
|
|
1117
|
+
// inside the hand-off; awaited by the turn after the front-door leg
|
|
1118
|
+
// settles.
|
|
1119
|
+
let escalatedLeg: Promise<void> | null = null;
|
|
1120
|
+
|
|
1010
1121
|
// Run one leg through the session bridge, streaming its deltas through
|
|
1011
|
-
// the shared TTS closures above.
|
|
1012
|
-
// when a front-door leg handed off (empty when the model gave none), or
|
|
1013
|
-
// null when the leg answered. Errors cancel and drain this turn's
|
|
1122
|
+
// the shared TTS closures above. Errors cancel and drain this turn's
|
|
1014
1123
|
// synthesis before propagating.
|
|
1015
|
-
const runVoiceLeg = async (leg: PhoneVoiceLeg): Promise<
|
|
1124
|
+
const runVoiceLeg = async (leg: PhoneVoiceLeg): Promise<void> => {
|
|
1016
1125
|
// Reasoning models can inline <think> spans in the content stream when
|
|
1017
1126
|
// a profile has not opted into parseThinkTags. Neither the spoken path
|
|
1018
1127
|
// nor the post-turn consumers of fullResponseText (transcripts,
|
|
@@ -1020,50 +1129,54 @@ export class CallController {
|
|
|
1020
1129
|
// are fed only filtered text. One filter per leg: a span never straddles
|
|
1021
1130
|
// the hand-off.
|
|
1022
1131
|
const reasoningFilter = createReasoningTagFilter();
|
|
1132
|
+
let legHandle: VoiceTurnHandle | null = null;
|
|
1023
1133
|
// Verdict-first: a front-door leg's leading tokens decide the turn's
|
|
1024
|
-
// fate, so its raw stream is a control plane until they classify.
|
|
1025
|
-
//
|
|
1026
|
-
|
|
1134
|
+
// fate, so its raw stream is a control plane until they classify. The
|
|
1135
|
+
// shared coordinator reads it and sequences the hand-off; only text it
|
|
1136
|
+
// releases is spoken or recorded. Phone has no partial transcripts,
|
|
1137
|
+
// so the hold verdict is never taught.
|
|
1138
|
+
const coordinator: FrontDoorLegCoordinator | null =
|
|
1027
1139
|
leg.routingLeg === "front-door"
|
|
1028
|
-
?
|
|
1140
|
+
? createFrontDoorLegCoordinator({
|
|
1141
|
+
holdEnabled: false,
|
|
1142
|
+
host: {
|
|
1143
|
+
isLive: () =>
|
|
1144
|
+
this.isCurrentRun(runVersion) && !runSignal.aborted,
|
|
1145
|
+
language: () => this.resolveSynthesisLanguage(),
|
|
1146
|
+
progress: cadence,
|
|
1147
|
+
onAnswerText: (text) => {
|
|
1148
|
+
fullResponseText += text;
|
|
1149
|
+
flushSafeText(fullResponseText);
|
|
1150
|
+
},
|
|
1151
|
+
abortLeg: () => legHandle?.abort(),
|
|
1152
|
+
speakBridge: speakEscalationBridge,
|
|
1153
|
+
startEscalatedLeg: (escalated) => {
|
|
1154
|
+
const started = runVoiceLeg(escalated);
|
|
1155
|
+
// Awaited by the turn; the rejection is observed there.
|
|
1156
|
+
started.catch(() => {});
|
|
1157
|
+
escalatedLeg = started;
|
|
1158
|
+
},
|
|
1159
|
+
},
|
|
1160
|
+
})
|
|
1029
1161
|
: null;
|
|
1030
|
-
let escalationBridge: string | null = null;
|
|
1031
|
-
let legHandle: VoiceTurnHandle | null = null;
|
|
1032
1162
|
|
|
1033
1163
|
const legComplete = new Promise<void>((resolve, reject) => {
|
|
1034
|
-
// Once the front-door leg hands off, nothing more from it is spoken,
|
|
1035
|
-
// recorded, or acted on. Abort it so a model that keeps generating
|
|
1036
|
-
// past the bridge cap adds no latency before the escalated leg
|
|
1037
|
-
// starts.
|
|
1038
|
-
const handOff = (bridge: string): void => {
|
|
1039
|
-
if (escalationBridge !== null) {
|
|
1040
|
-
return;
|
|
1041
|
-
}
|
|
1042
|
-
escalationBridge = bridge;
|
|
1043
|
-
legHandle?.abort();
|
|
1044
|
-
resolve();
|
|
1045
|
-
};
|
|
1046
|
-
|
|
1047
1164
|
const ingest = (speakable: string): void => {
|
|
1048
|
-
if (
|
|
1049
|
-
|
|
1050
|
-
flushSafeText(fullResponseText);
|
|
1051
|
-
return;
|
|
1165
|
+
if (speakable.length > 0) {
|
|
1166
|
+
deltaEpoch += 1;
|
|
1052
1167
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
flushSafeText(fullResponseText);
|
|
1057
|
-
} else if (
|
|
1058
|
-
(step.kind === "escalate" || step.kind === "bridge") &&
|
|
1059
|
-
step.bridge !== null
|
|
1060
|
-
) {
|
|
1061
|
-
handOff(step.bridge);
|
|
1168
|
+
if (coordinator !== null) {
|
|
1169
|
+
coordinator.push(speakable);
|
|
1170
|
+
return;
|
|
1062
1171
|
}
|
|
1172
|
+
fullResponseText += speakable;
|
|
1173
|
+
flushSafeText(fullResponseText);
|
|
1063
1174
|
};
|
|
1064
1175
|
|
|
1065
1176
|
const onTextDelta = (text: string): void => {
|
|
1066
|
-
|
|
1177
|
+
// Once the front-door leg hands off, nothing more from it is
|
|
1178
|
+
// spoken, recorded, or acted on.
|
|
1179
|
+
if (!this.isCurrentRun(runVersion) || coordinator?.handedOff) {
|
|
1067
1180
|
return;
|
|
1068
1181
|
}
|
|
1069
1182
|
ingest(reasoningFilter.push(text));
|
|
@@ -1076,7 +1189,7 @@ export class CallController {
|
|
|
1076
1189
|
if (
|
|
1077
1190
|
!this.isCurrentRun(runVersion) ||
|
|
1078
1191
|
runSignal.aborted ||
|
|
1079
|
-
|
|
1192
|
+
coordinator?.handedOff
|
|
1080
1193
|
) {
|
|
1081
1194
|
resolve();
|
|
1082
1195
|
return;
|
|
@@ -1084,17 +1197,7 @@ export class CallController {
|
|
|
1084
1197
|
// A held "[..." tail that never completed a marker is real text,
|
|
1085
1198
|
// released to both consumers before the leg settles.
|
|
1086
1199
|
ingest(reasoningFilter.flush());
|
|
1087
|
-
|
|
1088
|
-
// A front-door leg that stopped mid-bridge (a bare escalate
|
|
1089
|
-
// verdict, or a holding phrase with no sentence terminator) hands
|
|
1090
|
-
// off with whatever arrived; the canned fallback covers an empty
|
|
1091
|
-
// bridge.
|
|
1092
|
-
const step = verdict.finish();
|
|
1093
|
-
if (step.kind === "bridge") {
|
|
1094
|
-
handOff(step.bridge);
|
|
1095
|
-
return;
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1200
|
+
coordinator?.complete();
|
|
1098
1201
|
resolve();
|
|
1099
1202
|
};
|
|
1100
1203
|
|
|
@@ -1126,12 +1229,15 @@ export class CallController {
|
|
|
1126
1229
|
onTextDelta,
|
|
1127
1230
|
onComplete,
|
|
1128
1231
|
onError,
|
|
1129
|
-
callbacks: legCallbacks(
|
|
1232
|
+
callbacks: legCallbacks(
|
|
1233
|
+
leg.routingLeg,
|
|
1234
|
+
() => coordinator?.handedOff === true,
|
|
1235
|
+
),
|
|
1130
1236
|
signal: runSignal,
|
|
1131
1237
|
})
|
|
1132
1238
|
.then((handle) => {
|
|
1133
1239
|
legHandle = handle;
|
|
1134
|
-
if (this.isCurrentRun(runVersion) &&
|
|
1240
|
+
if (this.isCurrentRun(runVersion) && !coordinator?.handedOff) {
|
|
1135
1241
|
this.currentTurnHandle = handle;
|
|
1136
1242
|
} else {
|
|
1137
1243
|
// Superseded, or the front-door leg handed off before its
|
|
@@ -1171,72 +1277,43 @@ export class CallController {
|
|
|
1171
1277
|
synthesisCancelled = true;
|
|
1172
1278
|
this.abortActiveSynthesis();
|
|
1173
1279
|
}
|
|
1280
|
+
settleTurn();
|
|
1174
1281
|
await synthesisChain.catch(() => {});
|
|
1175
1282
|
throw err;
|
|
1176
1283
|
}
|
|
1177
|
-
return escalationBridge;
|
|
1178
1284
|
};
|
|
1179
1285
|
|
|
1180
1286
|
// Superseded mid-stream (barge-in): drain the segment chain (queued
|
|
1181
1287
|
// links short-circuit on staleness) so this turn's synthesis fully
|
|
1182
1288
|
// settles instead of racing the next turn.
|
|
1183
1289
|
const settleSuperseded = async (): Promise<VoiceTurnOutcome> => {
|
|
1290
|
+
settleTurn();
|
|
1184
1291
|
await synthesisChain.catch(() => {});
|
|
1185
1292
|
outcome.text = fullResponseText;
|
|
1186
1293
|
return outcome;
|
|
1187
1294
|
};
|
|
1188
1295
|
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1296
|
+
// Narration speaks into the turn's audible dead air on a cadence wherever
|
|
1297
|
+
// in the turn it occurs; the front-door leg is toolless and quick, so in
|
|
1298
|
+
// practice the escalated leg's tool loops are what it covers.
|
|
1299
|
+
cadence.arm();
|
|
1300
|
+
await runVoiceLeg({ content, routingLeg: "front-door" });
|
|
1193
1301
|
if (!this.isCurrentRun(runVersion)) {
|
|
1194
1302
|
return settleSuperseded();
|
|
1195
1303
|
}
|
|
1196
1304
|
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
// transcript hygiene keeps for it; the canned fallback is audio-only,
|
|
1202
|
-
// matching the row it deletes.
|
|
1203
|
-
const {
|
|
1204
|
-
spokenBridge,
|
|
1205
|
-
usesFallback: usesFallbackBridge,
|
|
1206
|
-
language: bridgeLanguage,
|
|
1207
|
-
} = resolveSpokenEscalationBridge(
|
|
1208
|
-
escalationBridge,
|
|
1209
|
-
this.resolveSynthesisLanguage(),
|
|
1210
|
-
);
|
|
1211
|
-
if (usesFallbackBridge) {
|
|
1212
|
-
speakFixedPhrase(spokenBridge, bridgeLanguage);
|
|
1213
|
-
} else {
|
|
1214
|
-
fullResponseText += `${spokenBridge} `;
|
|
1215
|
-
flushSafeText(fullResponseText, { force: true });
|
|
1216
|
-
// Force-synthesize the bridge now. On the synthesized-TTS path text
|
|
1217
|
-
// is held in pendingSynthText until a sentence boundary, so an
|
|
1218
|
-
// unpunctuated bridge would otherwise sit unspoken until the
|
|
1219
|
-
// post-leg drain, leaving the caller in silence during the escalated
|
|
1220
|
-
// model's call: the exact gap the bridge exists to mask.
|
|
1221
|
-
if (synthProvider) {
|
|
1222
|
-
const { segments } = extractSpeakableSegments(pendingSynthText, true);
|
|
1223
|
-
pendingSynthText = "";
|
|
1224
|
-
enqueueSynthesisSegments(synthProvider, segments);
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
// The bridge phrase the caller just heard is handed along so the
|
|
1228
|
-
// escalated continuation rule can quote it and ban a re-announcing
|
|
1229
|
-
// echo. The bridge pins this leg to the conversation's own profile.
|
|
1230
|
-
await runVoiceLeg({
|
|
1231
|
-
content: ESCALATION_CONTINUATION_CONTENT,
|
|
1232
|
-
routingLeg: "escalated",
|
|
1233
|
-
spokenEscalationBridge: spokenBridge,
|
|
1234
|
-
});
|
|
1305
|
+
// The front-door model handed off: the coordinator spoke the bridge and
|
|
1306
|
+
// started the escalated leg, which answers for real.
|
|
1307
|
+
if (escalatedLeg !== null) {
|
|
1308
|
+
await escalatedLeg;
|
|
1235
1309
|
if (!this.isCurrentRun(runVersion)) {
|
|
1236
1310
|
return settleSuperseded();
|
|
1237
1311
|
}
|
|
1238
1312
|
}
|
|
1239
1313
|
|
|
1314
|
+
// The turn's legs are done: no narration past this point.
|
|
1315
|
+
settleTurn();
|
|
1316
|
+
|
|
1240
1317
|
// Final sweep: a held "[..." tail that never completed a marker is real
|
|
1241
1318
|
// text, so the forced flush speaks it instead of dropping it.
|
|
1242
1319
|
flushSafeText(fullResponseText, { force: true });
|
|
@@ -98,4 +98,20 @@ export interface CallTransport {
|
|
|
98
98
|
* this — the controller then treats playback as already drained.
|
|
99
99
|
*/
|
|
100
100
|
awaitPlaybackDrained?(): Promise<void>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether no assistant speech is buffered, queued, in flight, or
|
|
104
|
+
* (estimatedly) still playing to the caller. Progress narration speaks
|
|
105
|
+
* only into audible silence, so it asks before every update. Transports
|
|
106
|
+
* that play speech synchronously may omit this; the controller then
|
|
107
|
+
* treats the transport as idle.
|
|
108
|
+
*/
|
|
109
|
+
isPlaybackIdle?(): boolean;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Wall-clock estimate of when audio already sent finishes playing to the
|
|
113
|
+
* caller, 0 when nothing is playing. Anchors the dead-air countdown for
|
|
114
|
+
* progress narration to when the caller last heard something.
|
|
115
|
+
*/
|
|
116
|
+
playbackTailUntilMs?(): number;
|
|
101
117
|
}
|
|
@@ -327,6 +327,14 @@ export class MediaStreamOutput implements CallTransport {
|
|
|
327
327
|
/** True when the queue drain loop is actively running. */
|
|
328
328
|
private draining = false;
|
|
329
329
|
|
|
330
|
+
/**
|
|
331
|
+
* Estimated wall-clock instant the audio already sent to Twilio finishes
|
|
332
|
+
* playing to the caller. Frames leave faster than real time and Twilio
|
|
333
|
+
* buffers them, so the only local view of "still audible" is this running
|
|
334
|
+
* sum of sent frame durations; a `clear` drops the buffer and resets it.
|
|
335
|
+
*/
|
|
336
|
+
private playbackTailAtMs = 0;
|
|
337
|
+
|
|
330
338
|
/** Abort controller for the currently in-flight synthesis/fetch. */
|
|
331
339
|
private activePlaybackAbort: AbortController | null = null;
|
|
332
340
|
|
|
@@ -659,6 +667,7 @@ export class MediaStreamOutput implements CallTransport {
|
|
|
659
667
|
return;
|
|
660
668
|
}
|
|
661
669
|
this.sendClearCommand();
|
|
670
|
+
this.playbackTailAtMs = 0;
|
|
662
671
|
// Twilio drops its buffered audio (and the marks within it) on `clear`, so
|
|
663
672
|
// any end-of-turn mark already sent will never echo. Treat those as drained
|
|
664
673
|
// so a pending end-call drain wait resolves instead of stalling on the cap.
|
|
@@ -716,6 +725,24 @@ export class MediaStreamOutput implements CallTransport {
|
|
|
716
725
|
return this.playbackQueue.length;
|
|
717
726
|
}
|
|
718
727
|
|
|
728
|
+
/**
|
|
729
|
+
* Whether no speech is buffered toward a sentence, queued, being
|
|
730
|
+
* synthesized or fetched, or (estimatedly) still playing at Twilio.
|
|
731
|
+
*/
|
|
732
|
+
isPlaybackIdle(): boolean {
|
|
733
|
+
return (
|
|
734
|
+
this.textBuffer.length === 0 &&
|
|
735
|
+
this.playbackQueue.length === 0 &&
|
|
736
|
+
!this.draining &&
|
|
737
|
+
Date.now() >= this.playbackTailAtMs
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** See {@link CallTransport.playbackTailUntilMs}. */
|
|
742
|
+
playbackTailUntilMs(): number {
|
|
743
|
+
return this.playbackTailAtMs;
|
|
744
|
+
}
|
|
745
|
+
|
|
719
746
|
/**
|
|
720
747
|
* Runtime check for closed state. Used instead of direct property access
|
|
721
748
|
* in async methods because TypeScript's control flow analysis cannot
|
|
@@ -772,6 +799,7 @@ export class MediaStreamOutput implements CallTransport {
|
|
|
772
799
|
this.playbackQueue.length = 0;
|
|
773
800
|
this.playbackVersion++;
|
|
774
801
|
this.audioStartCallback = null;
|
|
802
|
+
this.playbackTailAtMs = 0;
|
|
775
803
|
if (this.activePlaybackAbort) {
|
|
776
804
|
this.activePlaybackAbort.abort();
|
|
777
805
|
this.activePlaybackAbort = null;
|
|
@@ -854,6 +882,10 @@ export class MediaStreamOutput implements CallTransport {
|
|
|
854
882
|
for (const frame of frames) {
|
|
855
883
|
this.sendAudioPayload(frame);
|
|
856
884
|
}
|
|
885
|
+
const durationMs =
|
|
886
|
+
(frames.length * MULAW_FRAME_SIZE * 1000) / TELEPHONY_SAMPLE_RATE_HZ;
|
|
887
|
+
this.playbackTailAtMs =
|
|
888
|
+
Math.max(Date.now(), this.playbackTailAtMs) + durationMs;
|
|
857
889
|
}
|
|
858
890
|
|
|
859
891
|
/**
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* The voice front door owns hold, escalation, and direct-response behavior.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {
|
|
8
|
+
import type { VoiceProgressConfig } from "../config/schemas/voice.js";
|
|
9
9
|
import {
|
|
10
10
|
extractToolUse,
|
|
11
11
|
getConfiguredProvider,
|
|
@@ -216,7 +216,7 @@ export function effectiveSpokenTextMaxChars(
|
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
export function createVoiceProgressNarrator(options: {
|
|
219
|
-
config:
|
|
219
|
+
config: VoiceProgressConfig;
|
|
220
220
|
/** Provider resolver, injectable for tests. */
|
|
221
221
|
getProvider?: () => Promise<Provider | null>;
|
|
222
222
|
}): VoiceProgressNarrator {
|