@jskit-ai/assistant-voice 0.2.9 → 0.2.10
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/examples/conversation/main.js +1 -1
- package/examples/conversation/package.json +8 -8
- package/package.json +3 -3
- package/src/client/ConversationDialog.vue +2 -1
- package/src/client/VoiceConversation.vue +12 -6
- package/src/client/VoiceConversationControls.vue +6 -6
- package/src/client/VoiceConversationHost.vue +2 -1
- package/src/client/VoiceConversationSettings.vue +2 -1
- package/src/client/VoiceSpeakingVoiceSelect.vue +1 -1
- package/src/client/audioCapture.js +57 -36
- package/src/client/browser.js +89 -0
- package/src/client/conversationNarration.js +4 -1
- package/src/client/conversationVoiceState.js +6 -2
- package/src/client/i18n.js +7 -5
- package/src/client/types.js +104 -0
- package/src/client/voiceAvatar.js +13 -5
- package/src/client/voiceController.js +31 -7
- package/src/client/voiceConversation.js +66 -355
- package/src/client/voiceLauncher.js +18 -5
- package/src/client/voiceReplies.js +389 -0
- package/src/client/voiceSpeech.js +520 -0
- package/src/client/voiceTransport.js +55 -464
- package/src/server/voiceAccessToken.js +4 -1
- package/src/server/voiceDaemon.js +105 -47
- package/src/server/voiceProxy.js +3 -1
- package/src/server/voiceProxyConfig.js +5 -1
- package/src/shared/protocol.js +27 -5
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { onScopeDispose, ref, toValue } from "vue";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/** Reports nothing when an application leaves out onError. */
|
|
4
|
+
function ignoreLauncherError() {}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Optional hold gesture for a launcher; the shared host still owns capture.
|
|
8
|
+
* @param {{
|
|
9
|
+
* open: () => import("./types.js").VoiceHeldRecording | false | null | Promise<import("./types.js").VoiceHeldRecording | false | null>,
|
|
10
|
+
* enabled?: import("vue").MaybeRefOrGetter<boolean>,
|
|
11
|
+
* onTap?: (() => unknown) | null,
|
|
12
|
+
* onError?: (error: unknown) => void
|
|
13
|
+
* }} options
|
|
14
|
+
*/
|
|
15
|
+
export function useVoiceLauncher({ open, enabled = true, onTap, onError = ignoreLauncherError }) {
|
|
16
|
+
// readable-check: allow method-probe - optional operation: a launcher without onTap has no tap action, so a key press starts its hold at once
|
|
17
|
+
const tappable = typeof onTap === "function";
|
|
5
18
|
const holding = ref(false);
|
|
6
19
|
let input = null;
|
|
7
20
|
let timer;
|
|
@@ -30,7 +43,7 @@ export function useVoiceLauncher({ open, enabled = true, onTap = null, onError =
|
|
|
30
43
|
}
|
|
31
44
|
function keyDown(event) {
|
|
32
45
|
if (!toValue(enabled) || event.repeat || input !== null || ![" ", "Enter"].includes(event.key)) return;
|
|
33
|
-
begin(event.key,
|
|
46
|
+
begin(event.key, tappable ? 350 : 0);
|
|
34
47
|
}
|
|
35
48
|
function finish(discard) {
|
|
36
49
|
clearTimeout(timer);
|
|
@@ -38,7 +51,7 @@ export function useVoiceLauncher({ open, enabled = true, onTap = null, onError =
|
|
|
38
51
|
input = null;
|
|
39
52
|
++revision;
|
|
40
53
|
if (!holding.value) {
|
|
41
|
-
if (!discard && keyboard &&
|
|
54
|
+
if (!discard && keyboard && tappable) report(onTap());
|
|
42
55
|
return;
|
|
43
56
|
}
|
|
44
57
|
holding.value = false;
|
|
@@ -56,7 +69,7 @@ export function useVoiceLauncher({ open, enabled = true, onTap = null, onError =
|
|
|
56
69
|
const suppressed = suppressClick && event.detail !== 0;
|
|
57
70
|
suppressClick = false;
|
|
58
71
|
if (suppressed) { event.preventDefault(); event.stopImmediatePropagation(); return; }
|
|
59
|
-
if (toValue(enabled) &&
|
|
72
|
+
if (toValue(enabled) && tappable) report(onTap());
|
|
60
73
|
}
|
|
61
74
|
onScopeDispose(cancel);
|
|
62
75
|
return { holding, pointerDown, pointerUp, keyDown, keyUp, cancel, click };
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { computed, ref } from "vue";
|
|
2
|
+
import { createVoiceTurnId } from "./browser.js";
|
|
3
|
+
import { speechTextFromAssistant, takeStreamingSpeech, VOICE_MAX_SPEECH_TEXT_CHARACTERS } from "../shared/protocol.js";
|
|
4
|
+
import { createConversationNarrationTracker } from "./conversationNarration.js";
|
|
5
|
+
import { useJskitText, voiceErrorText, voiceSpeechWording } from "./i18n.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What a voice session says aloud: the conversation's assistant replies, streamed into speech as they arrive, and the
|
|
9
|
+
* optional narration of the agent's activity, with the read-aloud choice and sound unlocking that govern both. The
|
|
10
|
+
* session's watchers call into it, so their order stays the session's.
|
|
11
|
+
* @param {{
|
|
12
|
+
* binding: import("./types.js").VoiceConversationBinding,
|
|
13
|
+
* voice: import("./types.js").VoiceTransport,
|
|
14
|
+
* readAloud: import("vue").Ref<boolean>,
|
|
15
|
+
* error: import("vue").Ref<string>,
|
|
16
|
+
* starting: import("vue").Ref<boolean>,
|
|
17
|
+
* capturing: import("vue").ComputedRef<boolean>,
|
|
18
|
+
* speechActive: import("vue").ComputedRef<boolean>,
|
|
19
|
+
* narrationPageVisible: import("vue").Ref<boolean>,
|
|
20
|
+
* hiddenNarrationPage: import("vue").ComputedRef<boolean>,
|
|
21
|
+
* closed: () => boolean
|
|
22
|
+
* }} dependencies
|
|
23
|
+
*/
|
|
24
|
+
function useVoiceReplies({ binding, voice, readAloud, error, starting, capturing, speechActive, narrationPageVisible, hiddenNarrationPage, closed }) {
|
|
25
|
+
const { t } = useJskitText();
|
|
26
|
+
const readAloudChangePending = computed(() => binding.readAloudChangePending === true);
|
|
27
|
+
const soundPreparing = ref(false);
|
|
28
|
+
let soundRevision = 0;
|
|
29
|
+
const conversationId = binding.conversationId || binding.id;
|
|
30
|
+
let primed = false;
|
|
31
|
+
let draining = false;
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
const speechQueue = [];
|
|
34
|
+
const replies = new Map();
|
|
35
|
+
let speechSuppressed = false;
|
|
36
|
+
let requestedMessageId = "";
|
|
37
|
+
let speechInvitation = "";
|
|
38
|
+
let lastStreamingId = "";
|
|
39
|
+
const narrationTracker = createConversationNarrationTracker();
|
|
40
|
+
let narrationSettleTimer;
|
|
41
|
+
let thinkingMurmurTimer;
|
|
42
|
+
let narrationEpoch = 0;
|
|
43
|
+
|
|
44
|
+
function clearNarrationTimers() {
|
|
45
|
+
++narrationEpoch;
|
|
46
|
+
clearTimeout(narrationSettleTimer);
|
|
47
|
+
clearTimeout(thinkingMurmurTimer);
|
|
48
|
+
}
|
|
49
|
+
function retireNarration(selected = () => true) {
|
|
50
|
+
for (let index = speechQueue.length - 1; index >= 0; --index) {
|
|
51
|
+
const reply = speechQueue[index];
|
|
52
|
+
if (!reply.narrationKind || !selected(reply.narrationKind)) continue;
|
|
53
|
+
cancelReply(reply, "narration-retired");
|
|
54
|
+
speechQueue.splice(index, 1);
|
|
55
|
+
if (reply.turnId && voice.activeSpeechTurnId.value === reply.turnId) voice.stopSpeaking("narration-retired");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function narrationEnabled() {
|
|
59
|
+
const narration = binding.narration;
|
|
60
|
+
return Boolean(narration && narration.eligible && !narration.loading && readAloud.value
|
|
61
|
+
&& binding.state.available !== false && narrationPageVisible.value && !capturing.value
|
|
62
|
+
&& !starting.value && !speechSuppressed && !closed());
|
|
63
|
+
}
|
|
64
|
+
function queueNarration(kind, text) {
|
|
65
|
+
// Optional activity uses the original queue but never impersonates a canonical output.
|
|
66
|
+
if (!narrationEnabled()) return;
|
|
67
|
+
// Only filler yields to queued speech. Actual messages keep arrival order.
|
|
68
|
+
if (kind === "murmur" && (speechQueue.length || speechActive.value)) return;
|
|
69
|
+
speechQueue.push({ narrationKind: kind, raw: text, consumed: 0, final: true, started: false });
|
|
70
|
+
void drainSpeech();
|
|
71
|
+
}
|
|
72
|
+
function observeNarration(settled = false, enabled = narrationEnabled()) {
|
|
73
|
+
const narration = binding.narration;
|
|
74
|
+
if (!narration) return;
|
|
75
|
+
const entries = narrationTracker.observe(narration.turns, {
|
|
76
|
+
includeFinals: false, settled, enabled,
|
|
77
|
+
vocalizeThinking: narration.vocalizeThinking,
|
|
78
|
+
vocalizeInterimTurns: narration.vocalizeInterimTurns
|
|
79
|
+
});
|
|
80
|
+
for (const entry of entries) queueNarration(entry.kind, entry.text);
|
|
81
|
+
}
|
|
82
|
+
function scheduleThinkingMurmur(epoch) {
|
|
83
|
+
const narration = binding.narration;
|
|
84
|
+
if (!narrationEnabled() || !narration.working || !narration.thinkingSounds) return;
|
|
85
|
+
thinkingMurmurTimer = setTimeout(() => {
|
|
86
|
+
if (epoch !== narrationEpoch || !narrationEnabled() || !binding.narration?.working || !binding.narration.thinkingSounds) return;
|
|
87
|
+
if (!speechQueue.length && !draining && !speechActive.value && voice.state.value === "idle") {
|
|
88
|
+
queueNarration("murmur", t("jskit.assistant.voice.narration.murmur"));
|
|
89
|
+
}
|
|
90
|
+
scheduleThinkingMurmur(epoch);
|
|
91
|
+
}, 20_000);
|
|
92
|
+
}
|
|
93
|
+
function refreshNarration(settled = false) {
|
|
94
|
+
clearNarrationTimers();
|
|
95
|
+
observeNarration(settled);
|
|
96
|
+
const narration = binding.narration;
|
|
97
|
+
if (!narrationEnabled() || !narration.working) return;
|
|
98
|
+
const epoch = narrationEpoch;
|
|
99
|
+
if (narration.vocalizeThinking || narration.vocalizeInterimTurns) {
|
|
100
|
+
narrationSettleTimer = setTimeout(() => {
|
|
101
|
+
if (epoch === narrationEpoch && narrationEnabled()) observeNarration(true);
|
|
102
|
+
}, 700);
|
|
103
|
+
}
|
|
104
|
+
scheduleThinkingMurmur(epoch);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function observe(callback, value) {
|
|
108
|
+
try {
|
|
109
|
+
// readable-check: allow optional-call - optional operation: a binding may leave out onPlayback or onReadAloudChange, and is then not told
|
|
110
|
+
Promise.resolve(callback?.(value)).catch(() => {});
|
|
111
|
+
} catch {
|
|
112
|
+
// Observers cannot take ownership of scheduling or cleanup.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function emitReply(reply, phase, reason) {
|
|
116
|
+
if (reply.terminal || (phase === "started" && reply.audible)) return;
|
|
117
|
+
if (phase === "started") reply.audible = true;
|
|
118
|
+
else reply.terminal = true;
|
|
119
|
+
if (reply.narrationKind) return;
|
|
120
|
+
observe(binding.onPlayback, {
|
|
121
|
+
conversationId, outputId: reply.outputId, phase, ...(reason ? { reason } : {})
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function receivePlayback(event) {
|
|
125
|
+
const reply = [...replies.values()].find(reply => reply.turnId === event.turnId)
|
|
126
|
+
|| speechQueue.find(reply => reply.narrationKind && reply.turnId === event.turnId);
|
|
127
|
+
if (!reply) return;
|
|
128
|
+
if (event.phase === "completed") {
|
|
129
|
+
reply.drained = true;
|
|
130
|
+
if (reply.canonicalFinal || reply.narrationKind) emitReply(reply, "completed");
|
|
131
|
+
} else emitReply(reply, event.phase, event.reason);
|
|
132
|
+
}
|
|
133
|
+
function cancelReply(reply, reason = "cancelled", phase = "interrupted") {
|
|
134
|
+
reply.cancelled = true;
|
|
135
|
+
emitReply(reply, phase, reason);
|
|
136
|
+
}
|
|
137
|
+
function stopSpeech() { cancelSpeech(); }
|
|
138
|
+
function cancelSpeech(reason = "cancelled", phase = "interrupted") {
|
|
139
|
+
clearNarrationTimers();
|
|
140
|
+
retireNarration();
|
|
141
|
+
for (const reply of replies.values()) {
|
|
142
|
+
if (!reply.terminal) cancelReply(reply, reason, phase);
|
|
143
|
+
}
|
|
144
|
+
speechQueue.length = 0;
|
|
145
|
+
speechSuppressed = true;
|
|
146
|
+
speechInvitation = "";
|
|
147
|
+
voice.stopSpeaking(reason);
|
|
148
|
+
}
|
|
149
|
+
function inviteSpeech(messageId) {
|
|
150
|
+
// Only a new request from this page can lift silence. A delayed admission,
|
|
151
|
+
// history refresh or retry of the same request is not a new invitation.
|
|
152
|
+
if (!messageId || messageId === requestedMessageId || seen.has(messageId)) return;
|
|
153
|
+
requestedMessageId = messageId;
|
|
154
|
+
speechInvitation = messageId;
|
|
155
|
+
}
|
|
156
|
+
async function drainSpeech() {
|
|
157
|
+
if (draining || closed() || !readAloud.value || speechSuppressed || soundPreparing.value || voice.playbackBlocked.value) return;
|
|
158
|
+
draining = true;
|
|
159
|
+
const wording = voiceSpeechWording();
|
|
160
|
+
try {
|
|
161
|
+
while (speechQueue.length) {
|
|
162
|
+
const reply = speechQueue[0];
|
|
163
|
+
if (reply.cancelled) { speechQueue.shift(); continue; }
|
|
164
|
+
if (reply.started && !speechActive.value) { speechQueue.shift(); continue; }
|
|
165
|
+
if (speechActive.value && !reply.started) return;
|
|
166
|
+
if (reply.started && !voice.canAppendSpeech.value) return;
|
|
167
|
+
const previousSpeech = reply.narrationKind ? "" : speechTextFromAssistant(reply.raw.slice(0, reply.consumed), undefined, wording);
|
|
168
|
+
const maximumCharacters = reply.narrationKind ? (reply.started ? 140 : 64) : Math.max(1, Math.min(
|
|
169
|
+
reply.started ? 140 : 64,
|
|
170
|
+
VOICE_MAX_SPEECH_TEXT_CHARACTERS - previousSpeech.length,
|
|
171
|
+
VOICE_MAX_SPEECH_TEXT_CHARACTERS - reply.spokenCharacters
|
|
172
|
+
));
|
|
173
|
+
const chunk = reply.speechCapped ? null : takeStreamingSpeech(reply.raw.slice(reply.consumed), reply.final, maximumCharacters, wording);
|
|
174
|
+
if (!chunk) {
|
|
175
|
+
if (reply.final && reply.started) voice.endSpeech();
|
|
176
|
+
else if (reply.final) {
|
|
177
|
+
cancelReply(reply, "no-audio");
|
|
178
|
+
speechQueue.shift();
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
reply.consumed += chunk.consumed;
|
|
184
|
+
let text = chunk.text;
|
|
185
|
+
if (!reply.narrationKind) {
|
|
186
|
+
// Keep offsets in raw Markdown. The original limit applies after its
|
|
187
|
+
// cleanup, including whitespace between separately streamed phrases.
|
|
188
|
+
const speech = speechTextFromAssistant(reply.raw.slice(0, reply.consumed), undefined, wording);
|
|
189
|
+
const boundary = speech.startsWith(previousSpeech)
|
|
190
|
+
? speech.slice(previousSpeech.length).match(/^\s*/u)[0].length : 0;
|
|
191
|
+
text = text.slice(0, Math.max(0, Math.min(
|
|
192
|
+
VOICE_MAX_SPEECH_TEXT_CHARACTERS - previousSpeech.length - boundary,
|
|
193
|
+
VOICE_MAX_SPEECH_TEXT_CHARACTERS - reply.spokenCharacters
|
|
194
|
+
)));
|
|
195
|
+
reply.spokenCharacters += text.length;
|
|
196
|
+
reply.speechCapped = speech.length >= VOICE_MAX_SPEECH_TEXT_CHARACTERS
|
|
197
|
+
|| reply.spokenCharacters >= VOICE_MAX_SPEECH_TEXT_CHARACTERS;
|
|
198
|
+
reply.final ||= reply.speechCapped;
|
|
199
|
+
}
|
|
200
|
+
if (!text) continue;
|
|
201
|
+
if (!reply.started) {
|
|
202
|
+
reply.started = true;
|
|
203
|
+
reply.turnId = createVoiceTurnId("speak");
|
|
204
|
+
const accepted = await voice.speak(text, reply.turnId, { stream: true });
|
|
205
|
+
if (closed()) return;
|
|
206
|
+
if (reply.cancelled) {
|
|
207
|
+
if (reply.narrationKind) continue;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (accepted === false) {
|
|
211
|
+
cancelReply(reply, "no-audio");
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
} else if (!voice.appendSpeech(text)) return;
|
|
215
|
+
if (reply.speechCapped || (reply.final && reply.consumed >= reply.raw.length)) { voice.endSpeech(); return; }
|
|
216
|
+
}
|
|
217
|
+
} catch (cause) { error.value = voiceErrorText(cause); stopSpeech(); }
|
|
218
|
+
finally { draining = false; }
|
|
219
|
+
}
|
|
220
|
+
function queueReply(message, final) {
|
|
221
|
+
let reply = replies.get(message.id);
|
|
222
|
+
if (!reply) {
|
|
223
|
+
if (!primed || !readAloud.value || speechSuppressed || seen.has(message.id) || (!final && message.autonomous)) return;
|
|
224
|
+
retireNarration(kind => kind === "murmur");
|
|
225
|
+
reply = {
|
|
226
|
+
id: message.id, outputId: message.outputId || message.id, streamId: message.streamId,
|
|
227
|
+
raw: "", consumed: 0, spokenCharacters: 0, speechCapped: false,
|
|
228
|
+
final: false, canonicalFinal: false, started: false
|
|
229
|
+
};
|
|
230
|
+
replies.set(message.id, reply);
|
|
231
|
+
speechQueue.push(reply);
|
|
232
|
+
}
|
|
233
|
+
if (reply.cancelled) return;
|
|
234
|
+
if (hiddenNarrationPage.value && !reply.canonicalFinal && (final || !reply.raw)) {
|
|
235
|
+
// Consume hidden outputs without replay; a streaming answer cannot claim
|
|
236
|
+
// completion when its first canonical final was not admitted for speech.
|
|
237
|
+
cancelReply(reply, "page-hidden");
|
|
238
|
+
if (reply.turnId && voice.activeSpeechTurnId.value === reply.turnId) voice.stopSpeaking("page-hidden");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (final) retireNarration(kind => kind === "murmur");
|
|
242
|
+
const text = String(message.text || "").slice(0, VOICE_MAX_SPEECH_TEXT_CHARACTERS * 2);
|
|
243
|
+
const spokenPrefix = reply.raw.slice(0, reply.consumed);
|
|
244
|
+
if (!text.startsWith(spokenPrefix) && !(final && text.trimEnd() === spokenPrefix.trimEnd())) {
|
|
245
|
+
// A model revision cannot unsay words. Retire that projection; never splice
|
|
246
|
+
// its old prefix onto a different answer or replay it as a second answer.
|
|
247
|
+
cancelReply(reply);
|
|
248
|
+
if (speechQueue[0] === reply) voice.stopSpeaking();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
reply.consumed = Math.min(reply.consumed, text.length);
|
|
252
|
+
reply.raw = text;
|
|
253
|
+
reply.canonicalFinal ||= final;
|
|
254
|
+
reply.final = final || reply.speechCapped || text.length >= VOICE_MAX_SPEECH_TEXT_CHARACTERS * 2;
|
|
255
|
+
if (reply.canonicalFinal && reply.drained) emitReply(reply, "completed");
|
|
256
|
+
}
|
|
257
|
+
async function enableSound() {
|
|
258
|
+
const revision = ++soundRevision;
|
|
259
|
+
soundPreparing.value = true;
|
|
260
|
+
try {
|
|
261
|
+
await voice.preparePlayback();
|
|
262
|
+
} catch {
|
|
263
|
+
// Requested sound stays on; its recovery control remains available.
|
|
264
|
+
} finally {
|
|
265
|
+
if (revision === soundRevision && !closed()) {
|
|
266
|
+
soundPreparing.value = false;
|
|
267
|
+
void drainSpeech();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async function toggleReadAloud() {
|
|
272
|
+
if (readAloudChangePending.value) return;
|
|
273
|
+
error.value = "";
|
|
274
|
+
readAloud.value = !readAloud.value;
|
|
275
|
+
observe(binding.onReadAloudChange, readAloud.value);
|
|
276
|
+
await applyReadAloud();
|
|
277
|
+
}
|
|
278
|
+
async function applyReadAloud() {
|
|
279
|
+
if (!readAloud.value) {
|
|
280
|
+
++soundRevision;
|
|
281
|
+
soundPreparing.value = false;
|
|
282
|
+
cancelSpeech("muted");
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
speechSuppressed = false;
|
|
286
|
+
speechInvitation = "";
|
|
287
|
+
await enableSound();
|
|
288
|
+
if (!closed()) refreshNarration();
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function currentSpokenText() {
|
|
292
|
+
const reply = speechQueue[0];
|
|
293
|
+
return reply ? reply.raw.slice(0, reply.consumed).slice(-2000) : "";
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Queues the assistant replies of the conversation's latest history for speech, once each, and forgets what the
|
|
297
|
+
* history no longer holds.
|
|
298
|
+
* @param {import("./types.js").VoiceConversationMessage[]} messages
|
|
299
|
+
* @param {import("./types.js").VoiceConversationMessage | null | undefined} reply The reply streaming now.
|
|
300
|
+
*/
|
|
301
|
+
function observeHistory(messages, reply) {
|
|
302
|
+
let invited = false;
|
|
303
|
+
for (const message of messages) {
|
|
304
|
+
// Unadmitted local records do not acknowledge a request or lift speech silence.
|
|
305
|
+
if (message.role === "user" && message.receipt === false) continue;
|
|
306
|
+
if (seen.has(message.id)) continue;
|
|
307
|
+
if (message.role === "user" && message.id === speechInvitation) {
|
|
308
|
+
speechSuppressed = false;
|
|
309
|
+
speechInvitation = "";
|
|
310
|
+
invited = true;
|
|
311
|
+
}
|
|
312
|
+
if (message.role === "assistant") queueReply(message, true);
|
|
313
|
+
seen.add(message.id);
|
|
314
|
+
}
|
|
315
|
+
if (lastStreamingId && lastStreamingId !== reply?.id && !messages.some(message => message.id === lastStreamingId)) {
|
|
316
|
+
const retired = replies.get(lastStreamingId);
|
|
317
|
+
if (retired && !retired.canonicalFinal && !retired.cancelled) {
|
|
318
|
+
cancelReply(retired);
|
|
319
|
+
if (speechQueue[0] === retired) voice.finishCurrentPhrase();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
lastStreamingId = reply?.id || "";
|
|
323
|
+
if (reply?.text) {
|
|
324
|
+
queueReply(reply, reply.status === "completed");
|
|
325
|
+
if (!readAloud.value || !primed || reply.autonomous || hiddenNarrationPage.value) seen.add(reply.id);
|
|
326
|
+
}
|
|
327
|
+
// Keep only the visible history plus active playback receipts in memory.
|
|
328
|
+
const retained = new Set([...messages.map((message) => message.id), reply?.id, ...speechQueue.map((item) => item.id),
|
|
329
|
+
...[...replies.values()].filter(item => !item.terminal).map(item => item.id)]);
|
|
330
|
+
for (const id of seen) if (!retained.has(id)) seen.delete(id);
|
|
331
|
+
for (const id of replies.keys()) if (!retained.has(id)) replies.delete(id);
|
|
332
|
+
primed = true;
|
|
333
|
+
if (invited) refreshNarration();
|
|
334
|
+
void drainSpeech();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The narration settings the session watches, with whether narration may speak now.
|
|
339
|
+
* @returns {Record<string, unknown>}
|
|
340
|
+
*/
|
|
341
|
+
function narrationSettings() {
|
|
342
|
+
const narration = binding.narration;
|
|
343
|
+
return {
|
|
344
|
+
id: binding.id, conversationId: binding.conversationId, narration,
|
|
345
|
+
loading: narration?.loading, eligible: narration?.eligible, working: narration?.working,
|
|
346
|
+
thinking: narration?.vocalizeThinking, interim: narration?.vocalizeInterimTurns,
|
|
347
|
+
sounds: narration?.thinkingSounds, turns: narration?.turns, enabled: narrationEnabled()
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Retires narration the new settings no longer allow and observes what they now allow, without replaying history.
|
|
353
|
+
* @param {Record<string, any>} current
|
|
354
|
+
* @param {Record<string, any> | undefined} previous
|
|
355
|
+
*/
|
|
356
|
+
function applyNarrationSettings(current, previous) {
|
|
357
|
+
clearNarrationTimers();
|
|
358
|
+
if (!current.narration || current.loading || current.id !== previous?.id || current.conversationId !== previous?.conversationId) {
|
|
359
|
+
narrationTracker.reset();
|
|
360
|
+
retireNarration();
|
|
361
|
+
if (!current.narration || current.loading) return;
|
|
362
|
+
}
|
|
363
|
+
if (!current.enabled) retireNarration();
|
|
364
|
+
else retireNarration(kind => kind === "thinking" ? !current.thinking
|
|
365
|
+
: kind === "commentary" ? !current.interim : !current.sounds || !current.working);
|
|
366
|
+
// Retire disabled tails before enabling a flag; settings are not a request to replay history.
|
|
367
|
+
if (previous && (current.enabled !== previous.enabled || current.thinking !== previous.thinking || current.interim !== previous.interim)) observeNarration(true, false);
|
|
368
|
+
refreshNarration(!current.enabled);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
applyNarrationSettings,
|
|
373
|
+
applyReadAloud,
|
|
374
|
+
cancelSpeech,
|
|
375
|
+
currentSpokenText,
|
|
376
|
+
drainSpeech,
|
|
377
|
+
enableSound,
|
|
378
|
+
inviteSpeech,
|
|
379
|
+
narrationSettings,
|
|
380
|
+
observeHistory,
|
|
381
|
+
readAloudChangePending,
|
|
382
|
+
receivePlayback,
|
|
383
|
+
soundPreparing,
|
|
384
|
+
stopSpeech,
|
|
385
|
+
toggleReadAloud
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export { useVoiceReplies };
|