@voicethere/agent 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/agent.js +175 -5
- package/dist/host/index.d.ts +4 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +3 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/session-speak-controller.d.ts +70 -0
- package/dist/host/session-speak-controller.d.ts.map +1 -0
- package/dist/host/session-speak-controller.js +296 -0
- package/dist/host/session-speak-controller.js.map +1 -0
- package/dist/host/speak-text.d.ts +11 -0
- package/dist/host/speak-text.d.ts.map +1 -0
- package/dist/host/speak-text.js +21 -0
- package/dist/host/speak-text.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +95 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +69 -6
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +274 -7
- package/dist/runtime.js.map +1 -1
- package/dist/templates/echo/agent.js +178 -7
- package/dist/templates/echo-dc/agent.js +85 -2
- package/dist/templates/game-sync/agent.js +85 -2
- package/dist/templates/language-switch/agent.js +173 -5
- package/dist/templates/positional-tts/agent.js +173 -5
- package/dist/templates/recording-consent/agent.js +173 -5
- package/dist/templates/spatial-showcase/agent.js +173 -5
- package/dist/templates/voice-showcase/agent.js +178 -7
- package/dist/templates/voice-starter/agent.js +176 -5
- package/dist/templates/webhooks/agent.js +176 -5
- package/dist/templates/webhooks-redis/agent.js +85 -2
- package/dist/templates/world-sync/agent.js +85 -2
- package/dist/templates/world-sync-binary/agent.js +85 -2
- package/package.json +7 -1
- package/templates/mix-smoke/agent.ts +11 -2
package/README.md
CHANGED
|
@@ -336,6 +336,94 @@ onUserLanguage: async ({ sessionId, language }) => {
|
|
|
336
336
|
|
|
337
337
|
See [`templates/language-switch`](./templates/language-switch/README.md) for both modes.
|
|
338
338
|
|
|
339
|
+
## Controlling agent speech
|
|
340
|
+
|
|
341
|
+
`speak()` returns a handle. `done` never rejects and tells you how the utterance ended.
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
import { defineAgent, speak, announce, cancelSpeech } from "@voicethere/agent";
|
|
345
|
+
|
|
346
|
+
defineAgent({
|
|
347
|
+
onUserSpeechFinal: async ({ sessionId, text }) => {
|
|
348
|
+
const first = speak(sessionId, "Let me check that for you.", {
|
|
349
|
+
turnId: "reply-1",
|
|
350
|
+
});
|
|
351
|
+
const second = speak(sessionId, "Your order ships tomorrow.", {
|
|
352
|
+
turnId: "reply-1",
|
|
353
|
+
});
|
|
354
|
+
const result = await second.done;
|
|
355
|
+
// result.status: "played" | "interrupted" | "cancelled" | "failed" | "unsupported"
|
|
356
|
+
// result.playedMs: how much of the utterance the caller heard
|
|
357
|
+
if (result.status === "interrupted") first.cancel();
|
|
358
|
+
|
|
359
|
+
// Never cut by the caller, never dropped from the queue.
|
|
360
|
+
announce(sessionId, "This call may be recorded.");
|
|
361
|
+
|
|
362
|
+
// Cancel by request, by turn, or everything pending for the session.
|
|
363
|
+
cancelSpeech(sessionId, { turnId: "reply-1" });
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
`turnId` groups several speaks into one reply. `inReplyTo` names the caller turn the reply answers.
|
|
369
|
+
|
|
370
|
+
### Barge-in control
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
import { setBargeIn, withoutBargeIn, speak } from "@voicethere/agent";
|
|
374
|
+
|
|
375
|
+
await setBargeIn(sessionId, false); // { ok, reason }
|
|
376
|
+
await setBargeIn(sessionId, true);
|
|
377
|
+
|
|
378
|
+
// Barge-in is switched back on even when the body throws.
|
|
379
|
+
await withoutBargeIn(sessionId, async () => {
|
|
380
|
+
await speak(sessionId, "Please hold while I read the terms.").done;
|
|
381
|
+
});
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### Knowing what the caller heard
|
|
385
|
+
|
|
386
|
+
When the caller cuts in, the runner sends one report per interruption, in session order before the next final transcript. `playedText` is estimated from timing unless `playedTextExact` is true.
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import { defineAgent } from "@voicethere/agent";
|
|
390
|
+
|
|
391
|
+
const heard = new Map<string, string>();
|
|
392
|
+
|
|
393
|
+
defineAgent({
|
|
394
|
+
onAgentInterrupted: ({ sessionId, playedText, unplayedText }) => {
|
|
395
|
+
heard.set(
|
|
396
|
+
sessionId,
|
|
397
|
+
`You said "${playedText}" and were cut off before "${unplayedText}".`,
|
|
398
|
+
);
|
|
399
|
+
},
|
|
400
|
+
onUserSpeechFinal: async ({ sessionId, text }) => {
|
|
401
|
+
const note = heard.get(sessionId);
|
|
402
|
+
heard.delete(sessionId);
|
|
403
|
+
const prompt = note ? `${note}\n${text}` : text;
|
|
404
|
+
// pass `prompt` to your model
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Agent state
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
import { setAgentState } from "@voicethere/agent";
|
|
413
|
+
|
|
414
|
+
setAgentState(sessionId, "tool_call", { detail: "order lookup" }); // detail is cut to 64 characters
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
Keep customer content out of `detail`.
|
|
418
|
+
|
|
419
|
+
### Older runners
|
|
420
|
+
|
|
421
|
+
Runners that do not advertise speak control (`isSpeakControlAvailable(ctx)` is `false`) keep the old behaviour. `speak()` still plays, its result is `unsupported`, and `cancelSpeech`, `setAgentState` and `onAgentInterrupted` do nothing. `setBargeIn` resolves `{ ok: false, reason: "unsupported" }`. A warning is logged once per session.
|
|
422
|
+
|
|
423
|
+
### Host logic (`@voicethere/agent/host`)
|
|
424
|
+
|
|
425
|
+
`SessionSpeakController` implements the parent-side rules: speak results, cancellation, flushing queued speaks on barge-in, and the interruption report. It has no transport, so the VoiceThere runner and local tooling both drive it with the same code.
|
|
426
|
+
|
|
339
427
|
## Multiplayer / shared state
|
|
340
428
|
|
|
341
429
|
Each end-user connection must call **`startSession()`** once — one orchestrator session id per client (one signaling room per client). Do **not** join multiple browsers to the same session credentials.
|
package/dist/agent.js
CHANGED
|
@@ -151,6 +151,16 @@ var SessionSerialQueue = class {
|
|
|
151
151
|
var SESSION_START_INIT_DELAY_ENABLED_ENV = "AGENT_SESSION_START_INIT_DELAY_ENABLED";
|
|
152
152
|
var SESSION_START_INIT_DELAY_MS_ENV = "AGENT_SESSION_START_INIT_DELAY_MS";
|
|
153
153
|
var DEFAULT_SESSION_START_INIT_DELAY_MS = 500;
|
|
154
|
+
function isSpeakResultMessage(value) {
|
|
155
|
+
if (!value || typeof value !== "object") return false;
|
|
156
|
+
const msg = value;
|
|
157
|
+
return msg.type === "speak_result" && typeof msg.requestId === "string";
|
|
158
|
+
}
|
|
159
|
+
function isBargeInControlAckMessage(value) {
|
|
160
|
+
if (!value || typeof value !== "object") return false;
|
|
161
|
+
const msg = value;
|
|
162
|
+
return msg.type === "barge_in_control_ack" && typeof msg.requestId === "string";
|
|
163
|
+
}
|
|
154
164
|
function isPlayAckMessage(value) {
|
|
155
165
|
if (!value || typeof value !== "object") return false;
|
|
156
166
|
const msg = value;
|
|
@@ -242,7 +252,7 @@ function normalizeWebhookHeaders(value) {
|
|
|
242
252
|
function isParentMessage(value) {
|
|
243
253
|
if (!value || typeof value !== "object") return false;
|
|
244
254
|
const msg = value;
|
|
245
|
-
return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "webhook";
|
|
255
|
+
return msg.type === "session_start" || msg.type === "speech_event" || msg.type === "session_end" || msg.type === "data_channel_message" || msg.type === "data_channel_binary" || msg.type === "idle_timeout" || msg.type === "recording_control_ack" || msg.type === "conversation_history_control_ack" || msg.type === "play_ack" || msg.type === "play_status_ack" || msg.type === "play_stop_ack" || msg.type === "play_pose_ack" || msg.type === "mix_control_ack" || msg.type === "stt_control_ack" || msg.type === "voice_language_control_ack" || msg.type === "voice_language_prepare_result" || msg.type === "voice_replay_last_result" || msg.type === "speak_result" || msg.type === "barge_in_control_ack" || msg.type === "agent_interrupted" || msg.type === "webhook";
|
|
246
256
|
}
|
|
247
257
|
function isSessionScopedParentMessage(value) {
|
|
248
258
|
return isParentMessage(value) && !isWebhookMessage(value);
|
|
@@ -261,7 +271,57 @@ var mixAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
|
261
271
|
var voiceLanguageSwitchAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
262
272
|
var lastVoiceLanguageBySessionId = /* @__PURE__ */ new Map();
|
|
263
273
|
var ttsPoseAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
274
|
+
var speakControlAvailableBySessionId = /* @__PURE__ */ new Map();
|
|
275
|
+
var speakControlWarnedSessionIds = /* @__PURE__ */ new Set();
|
|
264
276
|
var endedSessionIds = /* @__PURE__ */ new Set();
|
|
277
|
+
var SPEAK_RESULT_SAFETY_TIMEOUT_MS = 6e5;
|
|
278
|
+
var pendingSpeakResults = /* @__PURE__ */ new Map();
|
|
279
|
+
var pendingBargeInAcks = /* @__PURE__ */ new Map();
|
|
280
|
+
function handleSpeakResult(message) {
|
|
281
|
+
const pending = pendingSpeakResults.get(message.requestId);
|
|
282
|
+
if (!pending) return;
|
|
283
|
+
clearTimeout(pending.timer);
|
|
284
|
+
pendingSpeakResults.delete(message.requestId);
|
|
285
|
+
pending.resolve({
|
|
286
|
+
requestId: message.requestId,
|
|
287
|
+
...message.turnId !== void 0 ? { turnId: message.turnId } : {},
|
|
288
|
+
status: message.status,
|
|
289
|
+
playedMs: message.playedMs,
|
|
290
|
+
...message.synthesizedMs !== void 0 ? { synthesizedMs: message.synthesizedMs } : {},
|
|
291
|
+
...message.errorCode !== void 0 ? { errorCode: message.errorCode } : {}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function handleBargeInControlAck(message) {
|
|
295
|
+
const pending = pendingBargeInAcks.get(message.requestId);
|
|
296
|
+
if (!pending) return;
|
|
297
|
+
clearTimeout(pending.timer);
|
|
298
|
+
pendingBargeInAcks.delete(message.requestId);
|
|
299
|
+
pending.resolve({
|
|
300
|
+
ok: message.ok,
|
|
301
|
+
...message.reason ? { reason: message.reason } : {}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
function clearPendingSpeakForSession(sessionId, errorCode) {
|
|
305
|
+
for (const [requestId, pending] of pendingSpeakResults) {
|
|
306
|
+
if (pending.sessionId !== sessionId) continue;
|
|
307
|
+
clearTimeout(pending.timer);
|
|
308
|
+
pendingSpeakResults.delete(requestId);
|
|
309
|
+
pending.resolve({
|
|
310
|
+
requestId,
|
|
311
|
+
...pending.turnId !== void 0 ? { turnId: pending.turnId } : {},
|
|
312
|
+
status: "cancelled",
|
|
313
|
+
playedMs: 0,
|
|
314
|
+
errorCode
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
for (const [requestId, pending] of pendingBargeInAcks) {
|
|
318
|
+
if (pending.sessionId !== sessionId) continue;
|
|
319
|
+
clearTimeout(pending.timer);
|
|
320
|
+
pendingBargeInAcks.delete(requestId);
|
|
321
|
+
pending.resolve({ ok: false, reason: "session_ended" });
|
|
322
|
+
}
|
|
323
|
+
speakControlWarnedSessionIds.delete(sessionId);
|
|
324
|
+
}
|
|
265
325
|
var PLAY_BYTES_MAX_DECODED_LENGTH = 64 * 1024;
|
|
266
326
|
var pendingRecordingAcks = /* @__PURE__ */ new Map();
|
|
267
327
|
var pendingConversationHistoryAcks = /* @__PURE__ */ new Map();
|
|
@@ -431,6 +491,9 @@ function clearPendingConversationHistoryAcksForSession(sessionId, reason) {
|
|
|
431
491
|
pending.resolve({ ok: false, reason, requestId });
|
|
432
492
|
}
|
|
433
493
|
}
|
|
494
|
+
function isVoicethereAgentChild() {
|
|
495
|
+
return typeof process.send === "function" && process.connected !== false && typeof process.env.__CHILD_BUNDLE_PATH__ === "string" && process.env.__CHILD_BUNDLE_PATH__.trim().length > 0;
|
|
496
|
+
}
|
|
434
497
|
var sessionExecutionContext = new AsyncLocalStorage();
|
|
435
498
|
var agentLogSessionContext = new AsyncLocalStorage();
|
|
436
499
|
var inboundQueueAuthority = null;
|
|
@@ -596,6 +659,10 @@ async function handleParentMessage(message, handlers) {
|
|
|
596
659
|
message.sessionId,
|
|
597
660
|
message.ttsPoseAvailable ?? false
|
|
598
661
|
);
|
|
662
|
+
speakControlAvailableBySessionId.set(
|
|
663
|
+
message.sessionId,
|
|
664
|
+
message.speakControlAvailable ?? false
|
|
665
|
+
);
|
|
599
666
|
if (message.voiceLanguageSwitch) {
|
|
600
667
|
voiceLanguageSwitchSettingsBySessionId.set(
|
|
601
668
|
message.sessionId,
|
|
@@ -617,7 +684,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
617
684
|
conversationHistoryAvailable: message.conversationHistoryAvailable ?? false,
|
|
618
685
|
mixAvailable: message.mixAvailable ?? false,
|
|
619
686
|
voiceLanguageSwitchAvailable: message.voiceLanguageSwitchAvailable ?? false,
|
|
620
|
-
ttsPoseAvailable: message.ttsPoseAvailable ?? false
|
|
687
|
+
ttsPoseAvailable: message.ttsPoseAvailable ?? false,
|
|
688
|
+
speakControlAvailable: message.speakControlAvailable ?? false
|
|
621
689
|
});
|
|
622
690
|
sendParentMessage({
|
|
623
691
|
type: "session_start_ack",
|
|
@@ -657,6 +725,11 @@ async function handleParentMessage(message, handlers) {
|
|
|
657
725
|
}
|
|
658
726
|
}
|
|
659
727
|
break;
|
|
728
|
+
case "agent_interrupted": {
|
|
729
|
+
const { type: _type, ...interrupted } = message;
|
|
730
|
+
await handlers.onAgentInterrupted?.(interrupted);
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
660
733
|
case "data_channel_message":
|
|
661
734
|
await handlers.onDataChannelMessage?.({
|
|
662
735
|
sessionId: message.sessionId,
|
|
@@ -689,6 +762,8 @@ async function handleParentMessage(message, handlers) {
|
|
|
689
762
|
voiceLanguageSwitchSettingsBySessionId.delete(message.sessionId);
|
|
690
763
|
lastVoiceLanguageBySessionId.delete(message.sessionId);
|
|
691
764
|
ttsPoseAvailableBySessionId.delete(message.sessionId);
|
|
765
|
+
speakControlAvailableBySessionId.delete(message.sessionId);
|
|
766
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
692
767
|
await (handlers.onClientLeave ?? handlers.onSessionEnd)?.({
|
|
693
768
|
sessionId: message.sessionId
|
|
694
769
|
});
|
|
@@ -712,6 +787,14 @@ function defineAgent(handlers) {
|
|
|
712
787
|
handleConversationHistoryControlAck(message);
|
|
713
788
|
return;
|
|
714
789
|
}
|
|
790
|
+
if (isSpeakResultMessage(message)) {
|
|
791
|
+
handleSpeakResult(message);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
if (isBargeInControlAckMessage(message)) {
|
|
795
|
+
handleBargeInControlAck(message);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
715
798
|
if (isPlayAckMessage(message)) {
|
|
716
799
|
handlePlayAck(message);
|
|
717
800
|
return;
|
|
@@ -755,6 +838,7 @@ function defineAgent(handlers) {
|
|
|
755
838
|
if (!isSessionScopedParentMessage(message)) return;
|
|
756
839
|
if (message.type === "session_end") {
|
|
757
840
|
endedSessionIds.add(message.sessionId);
|
|
841
|
+
clearPendingSpeakForSession(message.sessionId, "session_ended");
|
|
758
842
|
clearPendingRecordingAcksForSession(message.sessionId, "session_ended");
|
|
759
843
|
clearPendingConversationHistoryAcksForSession(
|
|
760
844
|
message.sessionId,
|
|
@@ -901,12 +985,98 @@ function buildIdleEnv(sessionId) {
|
|
|
901
985
|
...process.env.IDLE_TIMEOUT_SEC ? { IDLE_TIMEOUT_SEC: process.env.IDLE_TIMEOUT_SEC } : {}
|
|
902
986
|
};
|
|
903
987
|
}
|
|
988
|
+
function speakControlAvailableFor(sessionId) {
|
|
989
|
+
return speakControlAvailableBySessionId.get(sessionId) ?? false;
|
|
990
|
+
}
|
|
991
|
+
function warnSpeakControlUnavailable(sessionId) {
|
|
992
|
+
if (speakControlWarnedSessionIds.has(sessionId)) return;
|
|
993
|
+
speakControlWarnedSessionIds.add(sessionId);
|
|
994
|
+
agentLog(
|
|
995
|
+
"warn",
|
|
996
|
+
"speak control not available on this runner; speak results, cancel and barge-in control are disabled",
|
|
997
|
+
sessionId
|
|
998
|
+
);
|
|
999
|
+
}
|
|
904
1000
|
function speak(sessionId, text, options) {
|
|
905
|
-
|
|
906
|
-
|
|
1001
|
+
const requestId = randomUUID();
|
|
1002
|
+
const handle = (done2) => ({
|
|
1003
|
+
requestId,
|
|
1004
|
+
done: Promise.resolve(done2),
|
|
1005
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1006
|
+
});
|
|
1007
|
+
if (!isVoicethereAgentChild()) {
|
|
1008
|
+
return handle({
|
|
1009
|
+
requestId,
|
|
1010
|
+
status: "unsupported",
|
|
1011
|
+
playedMs: 0,
|
|
1012
|
+
errorCode: "no_parent"
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
if (!allowOutboundForSession(sessionId)) {
|
|
1016
|
+
return handle({
|
|
1017
|
+
requestId,
|
|
1018
|
+
status: "cancelled",
|
|
1019
|
+
playedMs: 0,
|
|
1020
|
+
errorCode: "session_ended"
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1024
|
+
sendParentMessage({
|
|
1025
|
+
type: "speak",
|
|
1026
|
+
sessionId,
|
|
1027
|
+
text,
|
|
1028
|
+
...options?.interruptible === false ? { interruptible: false } : {}
|
|
1029
|
+
});
|
|
1030
|
+
warnSpeakControlUnavailable(sessionId);
|
|
1031
|
+
return handle({ requestId, status: "unsupported", playedMs: 0 });
|
|
1032
|
+
}
|
|
1033
|
+
const turnId = options?.turnId;
|
|
1034
|
+
const inReplyTo = options?.inReplyTo;
|
|
1035
|
+
const done = new Promise((resolve) => {
|
|
1036
|
+
const timer = setTimeout(() => {
|
|
1037
|
+
pendingSpeakResults.delete(requestId);
|
|
1038
|
+
resolve({
|
|
1039
|
+
requestId,
|
|
1040
|
+
...turnId ? { turnId } : {},
|
|
1041
|
+
status: "failed",
|
|
1042
|
+
playedMs: 0,
|
|
1043
|
+
errorCode: "timeout"
|
|
1044
|
+
});
|
|
1045
|
+
}, SPEAK_RESULT_SAFETY_TIMEOUT_MS);
|
|
1046
|
+
pendingSpeakResults.set(requestId, {
|
|
1047
|
+
sessionId,
|
|
1048
|
+
...turnId ? { turnId } : {},
|
|
1049
|
+
resolve,
|
|
1050
|
+
timer
|
|
1051
|
+
});
|
|
1052
|
+
});
|
|
1053
|
+
sendParentMessage({
|
|
1054
|
+
type: "speak",
|
|
1055
|
+
sessionId,
|
|
1056
|
+
text,
|
|
1057
|
+
requestId,
|
|
1058
|
+
...options?.interruptible === false ? { interruptible: false } : {},
|
|
1059
|
+
...turnId ? { turnId } : {},
|
|
1060
|
+
...inReplyTo ? { inReplyTo } : {}
|
|
1061
|
+
});
|
|
1062
|
+
return {
|
|
1063
|
+
requestId,
|
|
1064
|
+
done,
|
|
1065
|
+
cancel: () => cancelSpeech(sessionId, { requestId })
|
|
1066
|
+
};
|
|
1067
|
+
}
|
|
1068
|
+
function cancelSpeech(sessionId, target) {
|
|
1069
|
+
if (!isVoicethereAgentChild() || !allowOutboundForSession(sessionId)) return;
|
|
1070
|
+
if (!speakControlAvailableFor(sessionId)) {
|
|
1071
|
+
warnSpeakControlUnavailable(sessionId);
|
|
907
1072
|
return;
|
|
908
1073
|
}
|
|
909
|
-
sendParentMessage({
|
|
1074
|
+
sendParentMessage({
|
|
1075
|
+
type: "speak_cancel",
|
|
1076
|
+
sessionId,
|
|
1077
|
+
...target?.requestId ? { requestId: target.requestId } : {},
|
|
1078
|
+
...target?.turnId ? { turnId: target.turnId } : {}
|
|
1079
|
+
});
|
|
910
1080
|
}
|
|
911
1081
|
function sendToClient(sessionId, payload) {
|
|
912
1082
|
sendParentMessage({ type: "send_to_client", sessionId, payload });
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { BARGE_IN_REPLY_END_WINDOW_MS, MAX_SETTLED_ENTRIES, SessionSpeakController, type SessionSpeakControllerDeps, type SessionSpeakControllerOptions, } from "./session-speak-controller.js";
|
|
2
|
+
export { DEFAULT_CHARS_PER_MS, estimatePlayedText } from "./speak-text.js";
|
|
3
|
+
export type { AgentInterruptedMessage, SpeakMessage, SpeakResultMessage, SpeakCancelMessage, SpeakStatus, BargeInControlMessage, BargeInControlAckMessage, } from "../protocol.js";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAC5B,mBAAmB,EACnB,sBAAsB,EACtB,KAAK,0BAA0B,EAC/B,KAAK,6BAA6B,GACnC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC3E,YAAY,EACV,uBAAuB,EACvB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,WAAW,EACX,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAC5B,mBAAmB,EACnB,sBAAsB,GAGvB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { AgentInterruptedMessage, SpeakMessage, SpeakResultMessage } from "../protocol.js";
|
|
2
|
+
/** A reply end with `interrupted` counts as the barge-in's cut if the barge-in follows within this window. */
|
|
3
|
+
export declare const BARGE_IN_REPLY_END_WINDOW_MS = 1000;
|
|
4
|
+
/** Settled entries kept per session; older ones are dropped. */
|
|
5
|
+
export declare const MAX_SETTLED_ENTRIES = 64;
|
|
6
|
+
export interface SessionSpeakControllerDeps {
|
|
7
|
+
send(msg: SpeakResultMessage | AgentInterruptedMessage): void;
|
|
8
|
+
now(): number;
|
|
9
|
+
}
|
|
10
|
+
export interface SessionSpeakControllerOptions {
|
|
11
|
+
/** Default true. */
|
|
12
|
+
flushQueuedOnBargeIn?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parent-side bookkeeping for one session's speaks: results, cancellation, barge-in
|
|
16
|
+
* flushing and the interruption report. Transport-free; the host plays audio and feeds
|
|
17
|
+
* events in.
|
|
18
|
+
*/
|
|
19
|
+
export declare class SessionSpeakController {
|
|
20
|
+
private readonly sessionId;
|
|
21
|
+
private readonly deps;
|
|
22
|
+
private readonly entries;
|
|
23
|
+
private readonly byId;
|
|
24
|
+
private readonly resultSent;
|
|
25
|
+
private readonly flushQueued;
|
|
26
|
+
private legacyCounter;
|
|
27
|
+
private bargeIn;
|
|
28
|
+
private pending;
|
|
29
|
+
private lastInterrupted;
|
|
30
|
+
private rateChars;
|
|
31
|
+
private rateMs;
|
|
32
|
+
constructor(sessionId: string, deps: SessionSpeakControllerDeps, options?: SessionSpeakControllerOptions);
|
|
33
|
+
get bargeInEnabled(): boolean;
|
|
34
|
+
/** @internal */
|
|
35
|
+
get sizeForTests(): number;
|
|
36
|
+
setBargeInEnabled(enabled: boolean): void;
|
|
37
|
+
accept(msg: SpeakMessage): {
|
|
38
|
+
requestId: string;
|
|
39
|
+
interruptible: boolean;
|
|
40
|
+
legacy: boolean;
|
|
41
|
+
};
|
|
42
|
+
/** True when the speak was cancelled or settled before it played. */
|
|
43
|
+
isCancelled(requestId: string): boolean;
|
|
44
|
+
onPlayStart(requestId: string): void;
|
|
45
|
+
onReplyEnd(requestId: string, info: {
|
|
46
|
+
playedMs: number;
|
|
47
|
+
interrupted: boolean;
|
|
48
|
+
synthesizedMs?: number;
|
|
49
|
+
synthesisComplete?: boolean;
|
|
50
|
+
}): void;
|
|
51
|
+
onSpeakFailed(requestId: string, errorCode: string): void;
|
|
52
|
+
cancel(target?: {
|
|
53
|
+
requestId?: string;
|
|
54
|
+
turnId?: string;
|
|
55
|
+
}): {
|
|
56
|
+
stopCurrent: boolean;
|
|
57
|
+
};
|
|
58
|
+
onBargeIn(callerPartial?: string): {
|
|
59
|
+
stopCurrent: boolean;
|
|
60
|
+
};
|
|
61
|
+
/** Call before forwarding a `user_speech_final` so the report precedes it. */
|
|
62
|
+
flushPendingInterruption(): void;
|
|
63
|
+
endSession(): void;
|
|
64
|
+
private matches;
|
|
65
|
+
private settle;
|
|
66
|
+
private prune;
|
|
67
|
+
private rate;
|
|
68
|
+
private completeInterruption;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=session-speak-controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-speak-controller.d.ts","sourceRoot":"","sources":["../../src/host/session-speak-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,uBAAuB,EACvB,YAAY,EACZ,kBAAkB,EAEnB,MAAM,gBAAgB,CAAC;AAoBxB,8GAA8G;AAC9G,eAAO,MAAM,4BAA4B,OAAO,CAAC;AACjD,gEAAgE;AAChE,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAUtC,MAAM,WAAW,0BAA0B;IACzC,IAAI,CAAC,GAAG,EAAE,kBAAkB,GAAG,uBA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