dsh-mimo-speak 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +197 -2
- package/README.zh.md +166 -0
- package/cordis.patch.yml +20 -0
- package/icon.svg +6 -0
- package/lib/client.js +3607 -0
- package/lib/client.js.map +1 -0
- package/lib/node/brief.d.ts +23 -0
- package/lib/node/brief.js +110 -0
- package/lib/node/brief.js.map +1 -0
- package/lib/node/client/BundleConfigCard.d.ts +15 -0
- package/lib/node/client/BundleConfigCard.js +22 -0
- package/lib/node/client/BundleConfigCard.js.map +1 -0
- package/lib/node/client/PlayerBar.d.ts +45 -0
- package/lib/node/client/PlayerBar.js +83 -0
- package/lib/node/client/PlayerBar.js.map +1 -0
- package/lib/node/client/SettingsSectionCard.d.ts +14 -0
- package/lib/node/client/SettingsSectionCard.js +19 -0
- package/lib/node/client/SettingsSectionCard.js.map +1 -0
- package/lib/node/client/SpeakAction.d.ts +32 -0
- package/lib/node/client/SpeakAction.js +651 -0
- package/lib/node/client/SpeakAction.js.map +1 -0
- package/lib/node/client/SpeakerIcon.d.ts +11 -0
- package/lib/node/client/SpeakerIcon.js +5 -0
- package/lib/node/client/SpeakerIcon.js.map +1 -0
- package/lib/node/client/TtsSettingsForm.d.ts +27 -0
- package/lib/node/client/TtsSettingsForm.js +164 -0
- package/lib/node/client/TtsSettingsForm.js.map +1 -0
- package/lib/node/client/api.d.ts +89 -0
- package/lib/node/client/api.js +247 -0
- package/lib/node/client/api.js.map +1 -0
- package/lib/node/client/audio-bus.d.ts +29 -0
- package/lib/node/client/audio-bus.js +91 -0
- package/lib/node/client/audio-bus.js.map +1 -0
- package/lib/node/client/chat-text.d.ts +32 -0
- package/lib/node/client/chat-text.js +90 -0
- package/lib/node/client/chat-text.js.map +1 -0
- package/lib/node/client/highlight.d.ts +45 -0
- package/lib/node/client/highlight.js +198 -0
- package/lib/node/client/highlight.js.map +1 -0
- package/lib/node/client/index.d.ts +26 -0
- package/lib/node/client/index.js +89 -0
- package/lib/node/client/index.js.map +1 -0
- package/lib/node/client/locales.d.ts +19 -0
- package/lib/node/client/locales.js +180 -0
- package/lib/node/client/locales.js.map +1 -0
- package/lib/node/client/panel-visibility.d.ts +24 -0
- package/lib/node/client/panel-visibility.js +16 -0
- package/lib/node/client/panel-visibility.js.map +1 -0
- package/lib/node/client/settings-controller.d.ts +116 -0
- package/lib/node/client/settings-controller.js +201 -0
- package/lib/node/client/settings-controller.js.map +1 -0
- package/lib/node/client/settings-registration.d.ts +24 -0
- package/lib/node/client/settings-registration.js +21 -0
- package/lib/node/client/settings-registration.js.map +1 -0
- package/lib/node/client/slots.d.ts +48 -0
- package/lib/node/client/slots.js +15 -0
- package/lib/node/client/slots.js.map +1 -0
- package/lib/node/client/speech-cache.d.ts +59 -0
- package/lib/node/client/speech-cache.js +68 -0
- package/lib/node/client/speech-cache.js.map +1 -0
- package/lib/node/client/split-speech.d.ts +20 -0
- package/lib/node/client/split-speech.js +100 -0
- package/lib/node/client/split-speech.js.map +1 -0
- package/lib/node/client/status.d.ts +28 -0
- package/lib/node/client/status.js +60 -0
- package/lib/node/client/status.js.map +1 -0
- package/lib/node/client/stream-dispose.d.ts +119 -0
- package/lib/node/client/stream-dispose.js +249 -0
- package/lib/node/client/stream-dispose.js.map +1 -0
- package/lib/node/client/styles.d.ts +19 -0
- package/lib/node/client/styles.js +176 -0
- package/lib/node/client/styles.js.map +1 -0
- package/lib/node/client/use-audio-player.d.ts +44 -0
- package/lib/node/client/use-audio-player.js +102 -0
- package/lib/node/client/use-audio-player.js.map +1 -0
- package/lib/node/client/use-audio-queue.d.ts +44 -0
- package/lib/node/client/use-audio-queue.js +151 -0
- package/lib/node/client/use-audio-queue.js.map +1 -0
- package/lib/node/client/use-stream-player.d.ts +41 -0
- package/lib/node/client/use-stream-player.js +60 -0
- package/lib/node/client/use-stream-player.js.map +1 -0
- package/lib/node/edge-tts.d.ts +21 -0
- package/lib/node/edge-tts.js +111 -0
- package/lib/node/edge-tts.js.map +1 -0
- package/lib/node/index.d.ts +97 -0
- package/lib/node/index.js +463 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/normalize.d.ts +30 -0
- package/lib/node/normalize.js +72 -0
- package/lib/node/normalize.js.map +1 -0
- package/lib/node/settings.d.ts +186 -0
- package/lib/node/settings.js +74 -0
- package/lib/node/settings.js.map +1 -0
- package/lib/node/summarize.d.ts +29 -0
- package/lib/node/summarize.js +131 -0
- package/lib/node/summarize.js.map +1 -0
- package/lib/node/tool.d.ts +54 -0
- package/lib/node/tool.js +123 -0
- package/lib/node/tool.js.map +1 -0
- package/lib/node/tts.d.ts +127 -0
- package/lib/node/tts.js +361 -0
- package/lib/node/tts.js.map +1 -0
- package/lib/node/wav.d.ts +32 -0
- package/lib/node/wav.js +63 -0
- package/lib/node/wav.js.map +1 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +122 -4
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser → Host calls for speech synthesis.
|
|
3
|
+
*
|
|
4
|
+
* Both halves share their route vocabulary through `src/settings.ts`; the
|
|
5
|
+
* browser sends text and receives WAV bytes, so the MiMo API key never enters
|
|
6
|
+
* the page. Failures arrive as stable codes the UI maps to localized copy.
|
|
7
|
+
* @module dsh-mimo-speak/client/api
|
|
8
|
+
*/
|
|
9
|
+
import { MIMOSPEAK_STATUS_PATH, MIMOSPEAK_SUMMARY_PATH, MIMOSPEAK_SYNTHESIZE_PATH, MIMOSPEAK_SYNTHESIZE_STREAM_PATH, MIMOSPEAK_TEST_PATH, } from "../settings.js";
|
|
10
|
+
/** A synthesize failure carrying the Host's stable code. */
|
|
11
|
+
export class TtsClientError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
/**
|
|
14
|
+
* @param code - stable failure code; `generic` when the Host gave none.
|
|
15
|
+
* @param message - Host-supplied detail, safe to show.
|
|
16
|
+
*/
|
|
17
|
+
constructor(code, message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.name = 'TtsClientError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Read the answering engine off an audio response.
|
|
25
|
+
*
|
|
26
|
+
* The Host answers MiMo synthesis as `audio/wav` and the Edge fallback as
|
|
27
|
+
* `audio/mpeg`, so the content type is the honest signal: the client can say
|
|
28
|
+
* "the fallback voice is reading this" instead of switching voices silently.
|
|
29
|
+
* @param blob - the audio response body.
|
|
30
|
+
* @returns the engine that produced it.
|
|
31
|
+
*/
|
|
32
|
+
export function speechEngineOf(blob) {
|
|
33
|
+
return blob.type.includes('mpeg') ? 'edge' : 'mimo';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Ask the Host for a short spoken summary of one reply.
|
|
37
|
+
*
|
|
38
|
+
* Used by the brief read scope: summarizing first and speaking the summary is
|
|
39
|
+
* what "简要播报" should mean, instead of mechanically picking sentences.
|
|
40
|
+
* @param text - the reply text to condense.
|
|
41
|
+
* @param signal - aborts the request when the reader stops.
|
|
42
|
+
* @returns the summary text.
|
|
43
|
+
*/
|
|
44
|
+
export async function requestSummary(text, signal) {
|
|
45
|
+
let response;
|
|
46
|
+
try {
|
|
47
|
+
response = await fetch(MIMOSPEAK_SUMMARY_PATH, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: { 'content-type': 'application/json' },
|
|
50
|
+
body: JSON.stringify({ text }),
|
|
51
|
+
...(signal === undefined ? {} : { signal }),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (cause) {
|
|
55
|
+
throw new TtsClientError('generic', cause instanceof Error ? cause.message : String(cause));
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
// The Host explains refusals in the shared error envelope.
|
|
59
|
+
let code = 'generic';
|
|
60
|
+
let message = `summary request failed with HTTP ${response.status}`;
|
|
61
|
+
try {
|
|
62
|
+
const body = await response.json();
|
|
63
|
+
if (typeof body.error?.code === 'string')
|
|
64
|
+
code = body.error.code;
|
|
65
|
+
if (typeof body.error?.message === 'string')
|
|
66
|
+
message = body.error.message;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Non-JSON failure body: keep the generic HTTP message.
|
|
70
|
+
}
|
|
71
|
+
throw new TtsClientError(code, message);
|
|
72
|
+
}
|
|
73
|
+
const body = await response.json();
|
|
74
|
+
if (typeof body.summary !== 'string' || body.summary.trim() === '') {
|
|
75
|
+
throw new TtsClientError('generic', 'The summary route returned no text.');
|
|
76
|
+
}
|
|
77
|
+
return body.summary.trim();
|
|
78
|
+
}
|
|
79
|
+
export async function fetchTtsStatus() {
|
|
80
|
+
const response = await fetch(MIMOSPEAK_STATUS_PATH, { method: 'GET', cache: 'no-store' });
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
throw new TtsClientError('generic', `status request failed with HTTP ${response.status}`);
|
|
83
|
+
}
|
|
84
|
+
return await response.json();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Synthesize one text into playable WAV audio.
|
|
88
|
+
* @param request - text and optional style/voice overrides (settings-page
|
|
89
|
+
* audition previews unsaved values through these).
|
|
90
|
+
* @param signal - aborts the request when the reader stops.
|
|
91
|
+
* @returns the WAV payload as a Blob ready for `URL.createObjectURL`.
|
|
92
|
+
* @throws {TtsClientError} when the Host refuses or the transport fails.
|
|
93
|
+
*/
|
|
94
|
+
export async function requestSpeech(request, signal) {
|
|
95
|
+
let response;
|
|
96
|
+
try {
|
|
97
|
+
response = await fetch(MIMOSPEAK_SYNTHESIZE_PATH, {
|
|
98
|
+
method: 'POST',
|
|
99
|
+
headers: { 'content-type': 'application/json' },
|
|
100
|
+
body: JSON.stringify(request),
|
|
101
|
+
...(signal === undefined ? {} : { signal }),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
throw new TtsClientError('generic', error instanceof Error ? error.message : String(error));
|
|
106
|
+
}
|
|
107
|
+
if (response.ok)
|
|
108
|
+
return await response.blob();
|
|
109
|
+
let code = 'generic';
|
|
110
|
+
let message = `synthesize request failed with HTTP ${response.status}`;
|
|
111
|
+
try {
|
|
112
|
+
const body = await response.json();
|
|
113
|
+
if (typeof body.error?.code === 'string')
|
|
114
|
+
code = body.error.code;
|
|
115
|
+
if (typeof body.error?.message === 'string')
|
|
116
|
+
message = body.error.message;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
// Non-JSON failure body: keep the generic HTTP message.
|
|
120
|
+
}
|
|
121
|
+
throw new TtsClientError(code, message);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Stream one text as raw PCM chunks and feed each chunk to `onChunk`.
|
|
125
|
+
*
|
|
126
|
+
* The Host answers with `text/event-stream`; every `data:` line carries one
|
|
127
|
+
* JSON object with either a `pcm` base64 payload, a terminal `done`, or an
|
|
128
|
+
* `error`. The browser decodes and schedules each chunk as it arrives, so the
|
|
129
|
+
* first chunk is playable before the rest of the utterance exists.
|
|
130
|
+
* @param request - text and optional style/voice overrides.
|
|
131
|
+
* @param onChunk - invoked once per decoded PCM chunk, in arrival order.
|
|
132
|
+
* @param signal - aborts the request mid-stream when the reader stops.
|
|
133
|
+
* @returns the terminal stats event.
|
|
134
|
+
* @throws {TtsClientError} when the Host refuses or the transport fails.
|
|
135
|
+
*/
|
|
136
|
+
export async function requestSpeechStream(request, onChunk, signal) {
|
|
137
|
+
let response;
|
|
138
|
+
try {
|
|
139
|
+
response = await fetch(MIMOSPEAK_SYNTHESIZE_STREAM_PATH, {
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: { 'content-type': 'application/json' },
|
|
142
|
+
body: JSON.stringify(request),
|
|
143
|
+
...(signal === undefined ? {} : { signal }),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
throw new TtsClientError('generic', error instanceof Error ? error.message : String(error));
|
|
148
|
+
}
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
// A refused stream answers with the same JSON error envelope as
|
|
151
|
+
// /synthesize; dropping it would turn "no API key" into a bare HTTP code.
|
|
152
|
+
let code = 'generic';
|
|
153
|
+
let message = `stream request failed with HTTP ${response.status}`;
|
|
154
|
+
try {
|
|
155
|
+
const failure = await response.json();
|
|
156
|
+
if (typeof failure.error?.code === 'string')
|
|
157
|
+
code = failure.error.code;
|
|
158
|
+
if (typeof failure.error?.message === 'string')
|
|
159
|
+
message = failure.error.message;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// Non-JSON failure body: keep the generic HTTP message.
|
|
163
|
+
}
|
|
164
|
+
throw new TtsClientError(code, message);
|
|
165
|
+
}
|
|
166
|
+
const body = response.body;
|
|
167
|
+
if (body === null) {
|
|
168
|
+
throw new TtsClientError('generic', 'stream request returned no body.');
|
|
169
|
+
}
|
|
170
|
+
const reader = body.getReader();
|
|
171
|
+
const decoder = new TextDecoder();
|
|
172
|
+
let buffer = '';
|
|
173
|
+
try {
|
|
174
|
+
for (;;) {
|
|
175
|
+
const { done, value } = await reader.read();
|
|
176
|
+
if (done)
|
|
177
|
+
break;
|
|
178
|
+
buffer += decoder.decode(value, { stream: true });
|
|
179
|
+
let newlineAt = buffer.indexOf('\n');
|
|
180
|
+
while (newlineAt !== -1) {
|
|
181
|
+
const line = buffer.slice(0, newlineAt).trim();
|
|
182
|
+
buffer = buffer.slice(newlineAt + 1);
|
|
183
|
+
newlineAt = buffer.indexOf('\n');
|
|
184
|
+
if (!line.startsWith('data:'))
|
|
185
|
+
continue;
|
|
186
|
+
const payloadText = line.slice(5).trim();
|
|
187
|
+
if (payloadText === '')
|
|
188
|
+
continue;
|
|
189
|
+
let payload;
|
|
190
|
+
try {
|
|
191
|
+
payload = JSON.parse(payloadText);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (payload.error !== undefined) {
|
|
197
|
+
const code = typeof payload.error.code === 'string' ? payload.error.code : 'generic';
|
|
198
|
+
const message = typeof payload.error.message === 'string' ? payload.error.message : 'stream failed';
|
|
199
|
+
throw new TtsClientError(code, message);
|
|
200
|
+
}
|
|
201
|
+
if (typeof payload.pcm === 'string' && payload.pcm !== '') {
|
|
202
|
+
const binary = atob(payload.pcm);
|
|
203
|
+
const bytes = new Uint8Array(binary.length);
|
|
204
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
205
|
+
bytes[index] = binary.charCodeAt(index);
|
|
206
|
+
}
|
|
207
|
+
onChunk(bytes);
|
|
208
|
+
}
|
|
209
|
+
if (payload.done === true)
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
reader.releaseLock();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Run the Host-side round-trip health check.
|
|
220
|
+
* @returns which engine answered, its latency, and its voice.
|
|
221
|
+
* @throws {TtsClientError} when the Host refuses or both engines fail.
|
|
222
|
+
*/
|
|
223
|
+
export async function requestSpeechTest() {
|
|
224
|
+
let response;
|
|
225
|
+
try {
|
|
226
|
+
response = await fetch(MIMOSPEAK_TEST_PATH, { method: 'POST' });
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
throw new TtsClientError('generic', error instanceof Error ? error.message : String(error));
|
|
230
|
+
}
|
|
231
|
+
if (response.ok)
|
|
232
|
+
return await response.json();
|
|
233
|
+
let code = 'generic';
|
|
234
|
+
let message = `test request failed with HTTP ${response.status}`;
|
|
235
|
+
try {
|
|
236
|
+
const body = await response.json();
|
|
237
|
+
if (typeof body.error?.code === 'string')
|
|
238
|
+
code = body.error.code;
|
|
239
|
+
if (typeof body.error?.message === 'string')
|
|
240
|
+
message = body.error.message;
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
// Non-JSON failure body: keep the generic HTTP message.
|
|
244
|
+
}
|
|
245
|
+
throw new TtsClientError(code, message);
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/client/api.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,qBAAqB,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,gCAAgC,EAAE,mBAAmB,GAChI,MAAM,gBAAgB,CAAA;AAKvB,4DAA4D;AAC5D,MAAM,OAAO,cAAe,SAAQ,KAAK;IAKlB;IAJrB;;;OAGG;IACH,YAAqB,IAA8B,EAAE,OAAe;QAClE,KAAK,CAAC,OAAO,CAAC,CAAA;QADK,SAAI,GAAJ,IAAI,CAA0B;QAEjD,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;IAC9B,CAAC;CACF;AASD;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,IAAU;IACvC,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAA;AACrD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,IAAY,EAAE,MAAoB;IACrE,IAAI,QAAkB,CAAA;IACtB,IAAI,CAAC;QACH,QAAQ,GAAG,MAAM,KAAK,CAAC,sBAAsB,EAAE;YAC7C,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,CAAC;YAC9B,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;SAC5C,CAAC,CAAA;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,cAAc,CAAC,SAAS,EA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ugin owns.
|
|
3
|
+
*
|
|
4
|
+
* The plugin plays audio through three different engines — the chunk queue
|
|
5
|
+
* (`<audio>` elements), the progressive PCM stream (`AudioContext`), and the
|
|
6
|
+
* settings-page sample player. Each used to keep its own module-local registry,
|
|
7
|
+
* so a stream starting while a queued reply played left both audible. Routing
|
|
8
|
+
* every engine through this one bus means starting audio anywhere pauses
|
|
9
|
+
* everything else, whichever engine produced it.
|
|
10
|
+
* @module dsh-mimo-speak/client/audio-bus
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Register one player's pause control.
|
|
14
|
+
* @param token - the player's stable identity (a ref object or Audio element).
|
|
15
|
+
* @param pause - stops that player without toggling its state.
|
|
16
|
+
* @returns a disposer that unregisters the player.
|
|
17
|
+
*/
|
|
18
|
+
export declare function registerAudioPauser(token: object, pause: () => void): () => void;
|
|
19
|
+
/**
|
|
20
|
+
* Pause every registered player except the caller.
|
|
21
|
+
* @param self - the caller's identity token, left untouched.
|
|
22
|
+
*/
|
|
23
|
+
export declare function pauseOtherAudio(self: object): void;
|
|
24
|
+
/**
|
|
25
|
+
* Subscribe to microphone capture starts.
|
|
26
|
+
* @param listener - called once per successful audio capture request.
|
|
27
|
+
* @returns the unsubscribe function.
|
|
28
|
+
*/
|
|
29
|
+
export declare function onAudioCaptureStart(listener: () => void): () => void;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One pause registry for every player this plugin owns.
|
|
3
|
+
*
|
|
4
|
+
* The plugin plays audio through three different engines — the chunk queue
|
|
5
|
+
* (`<audio>` elements), the progressive PCM stream (`AudioContext`), and the
|
|
6
|
+
* settings-page sample player. Each used to keep its own module-local registry,
|
|
7
|
+
* so a stream starting while a queued reply played left both audible. Routing
|
|
8
|
+
* every engine through this one bus means starting audio anywhere pauses
|
|
9
|
+
* everything else, whichever engine produced it.
|
|
10
|
+
* @module dsh-mimo-speak/client/audio-bus
|
|
11
|
+
*/
|
|
12
|
+
/** Pause callbacks keyed by the owning player's identity token. */
|
|
13
|
+
const pausers = new Map();
|
|
14
|
+
/**
|
|
15
|
+
* Register one player's pause control.
|
|
16
|
+
* @param token - the player's stable identity (a ref object or Audio element).
|
|
17
|
+
* @param pause - stops that player without toggling its state.
|
|
18
|
+
* @returns a disposer that unregisters the player.
|
|
19
|
+
*/
|
|
20
|
+
export function registerAudioPauser(token, pause) {
|
|
21
|
+
pausers.set(token, pause);
|
|
22
|
+
return () => {
|
|
23
|
+
pausers.delete(token);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Pause every registered player except the caller.
|
|
28
|
+
* @param self - the caller's identity token, left untouched.
|
|
29
|
+
*/
|
|
30
|
+
export function pauseOtherAudio(self) {
|
|
31
|
+
for (const [token, pause] of pausers) {
|
|
32
|
+
if (token === self)
|
|
33
|
+
continue;
|
|
34
|
+
try {
|
|
35
|
+
pause();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// A disposed player must never break the one taking over.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Listeners told when page code starts capturing microphone audio.
|
|
44
|
+
*
|
|
45
|
+
* Reading aloud while the microphone is open is self-defeating: the recorder
|
|
46
|
+
* captures the assistant's own voice and the transcript comes back as the
|
|
47
|
+
* reply it just spoke. The official voice-input bundle records through
|
|
48
|
+
* `getUserMedia`, so watching that one call covers any recorder — no coupling
|
|
49
|
+
* to another plugin's internals, and no cooperation needed from it.
|
|
50
|
+
*/
|
|
51
|
+
const captureListeners = new Set();
|
|
52
|
+
/** Whether `getUserMedia` is already wrapped in this page. */
|
|
53
|
+
let captureWatchInstalled = false;
|
|
54
|
+
/**
|
|
55
|
+
* Subscribe to microphone capture starts.
|
|
56
|
+
* @param listener - called once per successful audio capture request.
|
|
57
|
+
* @returns the unsubscribe function.
|
|
58
|
+
*/
|
|
59
|
+
export function onAudioCaptureStart(listener) {
|
|
60
|
+
captureListeners.add(listener);
|
|
61
|
+
installCaptureWatch();
|
|
62
|
+
return () => {
|
|
63
|
+
captureListeners.delete(listener);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Wrap `getUserMedia` once per page, leaving its behavior untouched. */
|
|
67
|
+
function installCaptureWatch() {
|
|
68
|
+
if (captureWatchInstalled)
|
|
69
|
+
return;
|
|
70
|
+
const devices = navigator.mediaDevices;
|
|
71
|
+
if (devices === undefined || typeof devices.getUserMedia !== 'function')
|
|
72
|
+
return;
|
|
73
|
+
captureWatchInstalled = true;
|
|
74
|
+
const original = devices.getUserMedia.bind(devices);
|
|
75
|
+
devices.getUserMedia = async (constraints) => {
|
|
76
|
+
const stream = await original(constraints);
|
|
77
|
+
const wantsAudio = constraints?.audio !== undefined && constraints.audio !== false;
|
|
78
|
+
if (!wantsAudio)
|
|
79
|
+
return stream;
|
|
80
|
+
for (const listener of captureListeners) {
|
|
81
|
+
try {
|
|
82
|
+
listener();
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// A reader that cannot stop must not break the microphone request.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return stream;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=audio-bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-bus.js","sourceRoot":"","sources":["../../../src/client/audio-bus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,mEAAmE;AACnE,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsB,CAAA;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa,EAAE,KAAiB;IAClE,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IACzB,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACvB,CAAC,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eA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BAAgB,EAAE,CAAC;YACxC,IAAI,CAAC;gBACH,QAAQ,EAAE,CAAA;YACZ,CAAC;YAAC,MAAM,CAAC;gBACP,mEAAmE;YACrE,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locate one finalized assistant message's speakable text in the Chat snapshot.
|
|
3
|
+
*
|
|
4
|
+
* The `conversation.chat.assistant-actions` seat receives only the durable
|
|
5
|
+
* `messageId`, so the entry derives the words it speaks from the current
|
|
6
|
+
* Conversation binding: the settled assistant node carrying that message id
|
|
7
|
+
* supplies its text blocks. Reasoning and tool-call blocks are never spoken —
|
|
8
|
+
* the message's prose is what the action row belongs to.
|
|
9
|
+
* @module dsh-mimo-speak/client/chat-text
|
|
10
|
+
*/
|
|
11
|
+
import type { ChatSnapshot } from '@deepseek-ai/dsh-client-ui-chat/client';
|
|
12
|
+
import type { MessageId } from '@deepseek-ai/dsh-api-remotes/client';
|
|
13
|
+
import type { ReadScope } from '../settings.ts';
|
|
14
|
+
/**
|
|
15
|
+
* Extract the speakable text of one finalized assistant message.
|
|
16
|
+
* @param snapshot - current Chat snapshot of the viewed Session.
|
|
17
|
+
* @param messageId - durable identity of the target assistant message.
|
|
18
|
+
* @param scope - which parts of the reply are read aloud.
|
|
19
|
+
* @returns the message's text blocks joined in order, or undefined when the
|
|
20
|
+
* message is not in the loaded window.
|
|
21
|
+
*/
|
|
22
|
+
export declare function speakableTextFor(snapshot: ChatSnapshot, messageId: MessageId, scope?: ReadScope): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Identify the newest finalized assistant message in the Chat snapshot.
|
|
25
|
+
*
|
|
26
|
+
* Automatic read-aloud only ever speaks this message, so a reply that
|
|
27
|
+
* finished while the user scrolled up, or an older reply re-rendered for any
|
|
28
|
+
* other reason, never steals the speaker.
|
|
29
|
+
* @param snapshot - current Chat snapshot of the viewed Session.
|
|
30
|
+
* @returns the message id of the newest assistant step, or an empty string.
|
|
31
|
+
*/
|
|
32
|
+
export declare function latestAssistantMessageId(snapshot: ChatSnapshot): string;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locate one finalized assistant message's speakable text in the Chat snapshot.
|
|
3
|
+
*
|
|
4
|
+
* The `conversation.chat.assistant-actions` seat receives only the durable
|
|
5
|
+
* `messageId`, so the entry derives the words it speaks from the current
|
|
6
|
+
* Conversation binding: the settled assistant node carrying that message id
|
|
7
|
+
* supplies its text blocks. Reasoning and tool-call blocks are never spoken —
|
|
8
|
+
* the message's prose is what the action row belongs to.
|
|
9
|
+
* @module dsh-mimo-speak/client/chat-text
|
|
10
|
+
*/
|
|
11
|
+
/** Code blocks shorter than this many lines are worth speaking in smart mode. */
|
|
12
|
+
const SMART_CODE_LINE_LIMIT = 24;
|
|
13
|
+
/** Whether a block contributes text under the active read scope. */
|
|
14
|
+
function blockIncluded(block, scope) {
|
|
15
|
+
if (block.kind === 'text')
|
|
16
|
+
return true;
|
|
17
|
+
// Both text-only scopes skip everything that is not prose; `brief` condenses
|
|
18
|
+
// the prose afterwards, in the speak entry (model summary, else the offline rule).
|
|
19
|
+
if (scope === 'text' || scope === 'brief')
|
|
20
|
+
return false;
|
|
21
|
+
if (block.kind !== 'code')
|
|
22
|
+
return false;
|
|
23
|
+
if (scope === 'all')
|
|
24
|
+
return true;
|
|
25
|
+
// Smart: speak short code snippets only; long listings are skipped.
|
|
26
|
+
const lines = block.text.split('\n').length;
|
|
27
|
+
return lines <= SMART_CODE_LINE_LIMIT && block.text.length <= 1200;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract the speakable text of one finalized assistant message.
|
|
31
|
+
* @param snapshot - current Chat snapshot of the viewed Session.
|
|
32
|
+
* @param messageId - durable identity of the target assistant message.
|
|
33
|
+
* @param scope - which parts of the reply are read aloud.
|
|
34
|
+
* @returns the message's text blocks joined in order, or undefined when the
|
|
35
|
+
* message is not in the loaded window.
|
|
36
|
+
*/
|
|
37
|
+
export function speakableTextFor(snapshot, messageId, scope = 'text') {
|
|
38
|
+
const matches = [];
|
|
39
|
+
for (const node of snapshot.nodes.values()) {
|
|
40
|
+
if (node.kind !== 'assistant-step')
|
|
41
|
+
continue;
|
|
42
|
+
// One narrowing cast: the store's rows are the kind-payload pairs the
|
|
43
|
+
// ui-chat contract declares, keyed exactly as this check reads them.
|
|
44
|
+
const candidate = node;
|
|
45
|
+
const data = candidate.data;
|
|
46
|
+
const blocks = data.finalNode?.blocks ?? data.blocks;
|
|
47
|
+
if (data.finalNode?.messageId !== messageId)
|
|
48
|
+
continue;
|
|
49
|
+
const text = blocks
|
|
50
|
+
.filter(block => blockIncluded(block, scope))
|
|
51
|
+
.map(block => block.text)
|
|
52
|
+
.join('\n')
|
|
53
|
+
.trim();
|
|
54
|
+
matches.push({ anchorSeq: candidate.anchorSeq, visibility: candidate.visibility, text });
|
|
55
|
+
}
|
|
56
|
+
if (matches.length === 0)
|
|
57
|
+
return undefined;
|
|
58
|
+
// Prefer the visible rendering of the message; hidden duplicates (folded
|
|
59
|
+
// process views) carry the same words.
|
|
60
|
+
const preferred = matches.find(match => match.visibility === 'visible') ?? matches[0];
|
|
61
|
+
return preferred.text;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Identify the newest finalized assistant message in the Chat snapshot.
|
|
65
|
+
*
|
|
66
|
+
* Automatic read-aloud only ever speaks this message, so a reply that
|
|
67
|
+
* finished while the user scrolled up, or an older reply re-rendered for any
|
|
68
|
+
* other reason, never steals the speaker.
|
|
69
|
+
* @param snapshot - current Chat snapshot of the viewed Session.
|
|
70
|
+
* @returns the message id of the newest assistant step, or an empty string.
|
|
71
|
+
*/
|
|
72
|
+
export function latestAssistantMessageId(snapshot) {
|
|
73
|
+
let latestSeq = -1;
|
|
74
|
+
let latestId = '';
|
|
75
|
+
for (const node of snapshot.nodes.values()) {
|
|
76
|
+
if (node.kind !== 'assistant-step')
|
|
77
|
+
continue;
|
|
78
|
+
const candidate = node;
|
|
79
|
+
const data = candidate.data;
|
|
80
|
+
const id = data.finalNode?.messageId;
|
|
81
|
+
if (typeof id !== 'string' || id === '')
|
|
82
|
+
continue;
|
|
83
|
+
if (candidate.anchorSeq > latestSeq) {
|
|
84
|
+
latestSeq = candidate.anchorSeq;
|
|
85
|
+
latestId = id;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return latestId;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=chat-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-text.js","sourceRoot":"","sources":["../../../src/client/chat-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAeH,iFAAiF;AACjF,MAAM,qBAAqB,GAAG,EAAE,CAAA;AAEhC,oEAAoE;AACpE,SAAS,aAAa,CAAC,KAAqB,EAAE,KAAgB;IAC5D,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,CAAA;IACtC,6EAA6E;IAC7E,mFAAmF;IACnF,IAAI,KAAK,KAAK,MAAM,IAAI,KAAK,KAAK,OAAO;QAAE,OAAO,KAAK,CAAA;IACvD,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,KAAK,CAAA;IACvC,IAAI,KAAK,KAAK,KAAK;QAAE,OAAO,IAAI,CAAA;IAChC,oEAAoE;IACpE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAA;IAC3C,OAAO,KAAK,IAAI,qBAAqB,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,CAAA;AACpE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAAsB,EACtB,SAAoB,EACpB,QAAmB,MAAM;IAEzB,MAAM,OAAO,GAA8D,EAAE,CAAA;IAC7E,KAAK,MAA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,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;QAC3C,IAAI,IAAI,CAAC,IAAI,KAAK,gBAAgB;YAAE,SAAQ;QAC5C,MAAM,SAAS,GAAG,IAAkC,CAAA;QACpD,MAAM,IAAI,GAAG,SAAS,CAAC,IAAyB,CAAA;QAChD,MAAM,EAAE,GAAG,IAAI,CAAC,SAAS,EAAE,SAAS,CAAA;QACpC,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,KAAK,EAAE;YAAE,SAAQ;QACjD,IAAI,SAAS,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC;YACpC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAA;YAC/B,QAAQ,GAAG,EAAE,CAAA;QACf,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentence highlighting for the speak entry.
|
|
3
|
+
*
|
|
4
|
+
* While a chunk plays, the sentence it came from is tinted inside the message
|
|
5
|
+
* bubble. The highlighter walks the message's text nodes once, indexes their
|
|
6
|
+
* letters/digits (a normalization that survives Markdown and inline styles),
|
|
7
|
+
* and maps each chunk text back to DOM ranges through the CSS Custom
|
|
8
|
+
* Highlight API — so no DOM is mutated and scroll position never jumps.
|
|
9
|
+
*
|
|
10
|
+
* When the API is unavailable the highlighter silently no-ops.
|
|
11
|
+
* @module dsh-mimo-speak/client/highlight
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Locate the message bubble containing the speak button, by walking up from
|
|
15
|
+
* the button until an ancestor's text carries the opening of the spoken text.
|
|
16
|
+
* @param fromEl - the action row element (or any descendant of the bubble).
|
|
17
|
+
* @param probeText - the speakable text whose opening identifies the bubble.
|
|
18
|
+
* @returns the closest matching ancestor, or null.
|
|
19
|
+
*/
|
|
20
|
+
export declare function findMessageContainer(fromEl: Element | null, probeText: string): Element | null;
|
|
21
|
+
/** Highlight one sentence of one message while it plays. */
|
|
22
|
+
export declare class SentenceHighlighter {
|
|
23
|
+
private readonly container;
|
|
24
|
+
private readonly token;
|
|
25
|
+
private readonly nodes;
|
|
26
|
+
private readonly map;
|
|
27
|
+
private key;
|
|
28
|
+
/** Text length the index was built from; a re-render invalidates it. */
|
|
29
|
+
private indexedSignature;
|
|
30
|
+
constructor(container: Element | null);
|
|
31
|
+
/** Whether any highlight can actually be painted. */
|
|
32
|
+
get supported(): boolean;
|
|
33
|
+
/** Index the message's text nodes, rebuilding when the text changed. */
|
|
34
|
+
private index;
|
|
35
|
+
/**
|
|
36
|
+
* Paint the sentence `text` onto the message, searching from the key offset
|
|
37
|
+
* `from` so repeated phrases highlight their later occurrence.
|
|
38
|
+
* @param text - one chunk (a substring of the message).
|
|
39
|
+
* @param from - key offset to search from.
|
|
40
|
+
* @returns the key offset just past the painted range (feed it back next).
|
|
41
|
+
*/
|
|
42
|
+
show(text: string, from: number): number;
|
|
43
|
+
/** Remove the painted sentence, unless another instance owns it now. */
|
|
44
|
+
clear(): void;
|
|
45
|
+
}
|