dsh-voice-mode 0.7.44 → 0.7.45
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.en.md +9 -7
- package/README.md +9 -7
- package/SECURITY.md +1 -1
- package/THIRD_PARTY_NOTICES.md +31 -2
- package/lib/client.js +316 -191
- package/lib/index.js +1304 -609
- package/lib/tts-vits-worker.cjs +1 -1
- package/package.json +3 -2
- package/scripts/check-anchors.mjs +42 -3
package/lib/index.js
CHANGED
|
@@ -7,48 +7,1182 @@ import { rm } from "node:fs/promises";
|
|
|
7
7
|
// src/asr-host.ts
|
|
8
8
|
import { statSync } from "node:fs";
|
|
9
9
|
|
|
10
|
-
//
|
|
10
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/segmenter.js
|
|
11
|
+
var DEFAULT_TABLE_PLACEHOLDER = "\u6B64\u5904\u7701\u7565\u4E00\u4E2A\u8868\u683C\u3002";
|
|
12
|
+
var SKIP_PREFIX = /^[\s.,,、::;;!?!?)\])"'”’〉》】]+$/;
|
|
13
|
+
var INLINE_SENTENCE_END_RE = /[。!?!?;;…]/;
|
|
14
|
+
var TABLE_DELIM_CELL_RE = /^:?-+:?$/;
|
|
15
|
+
var TABLE_DELIM_CHARS_RE = /^[\s|:-]*$/;
|
|
16
|
+
var UNESCAPED_PIPE_RE = /(?:^|[^\\])\|/;
|
|
17
|
+
var FENCE_LANG_PREFIX_RE = /^[ \t]*[A-Za-z0-9_+.#-]{1,32}[ \t]*\r?\n/;
|
|
18
|
+
var MAX_FRONTMATTER_KEY_CHARS = 120;
|
|
19
|
+
var YAML_CLOSE = /^(?:---|\.\.\.)[ \t]*$/;
|
|
20
|
+
var YAML_HEADER = /^[ \t]*(?:-[ \t]+)?(?:[\p{L}\p{N}_.-]+|"(?:[^"\\]|\\.)*"|'(?:[^']|'')*')[ \t]*:/u;
|
|
21
|
+
var YAML_KEY_PREFIX = /^[ \t]*(?:-[ \t]+)?(?:[\p{L}\p{N}_.-]+[ \t]*|"(?:[^"\\]|\\.)*(?:\\|"[ \t]*)?|'(?:[^']|'')*(?:'[ \t]*)?)$/u;
|
|
22
|
+
function classifyFirstLine(line, complete) {
|
|
23
|
+
if (/^[ \t]*#/.test(line))
|
|
24
|
+
return "metadata";
|
|
25
|
+
if (/^[ \t]*$/.test(line))
|
|
26
|
+
return "pending";
|
|
27
|
+
const header = YAML_HEADER.exec(line.slice(0, MAX_FRONTMATTER_KEY_CHARS + 1));
|
|
28
|
+
if (header) {
|
|
29
|
+
const next = line[header[0].length];
|
|
30
|
+
if (next === void 0)
|
|
31
|
+
return complete ? "metadata" : "pending";
|
|
32
|
+
return next === " " || next === " " ? "metadata" : "body";
|
|
33
|
+
}
|
|
34
|
+
if (!complete && YAML_CLOSE.test(line))
|
|
35
|
+
return "pending";
|
|
36
|
+
if (!complete && line.length <= MAX_FRONTMATTER_KEY_CHARS) {
|
|
37
|
+
if (isHighSurrogate(line.charCodeAt(line.length - 1)) || YAML_KEY_PREFIX.test(line))
|
|
38
|
+
return "pending";
|
|
39
|
+
}
|
|
40
|
+
return "body";
|
|
41
|
+
}
|
|
42
|
+
var LeadingFrontmatter = class {
|
|
43
|
+
phase = "opening";
|
|
44
|
+
partial = "";
|
|
45
|
+
held = "";
|
|
46
|
+
release(body) {
|
|
47
|
+
this.phase = "body";
|
|
48
|
+
this.partial = "";
|
|
49
|
+
this.held = "";
|
|
50
|
+
return body;
|
|
51
|
+
}
|
|
52
|
+
read(chunk, final = false) {
|
|
53
|
+
if (this.phase === "body")
|
|
54
|
+
return chunk;
|
|
55
|
+
let start = 0;
|
|
56
|
+
for (; ; ) {
|
|
57
|
+
const nl = chunk.indexOf("\n", start);
|
|
58
|
+
const complete = nl !== -1 || final;
|
|
59
|
+
this.partial += nl === -1 ? chunk.slice(start) : chunk.slice(start, nl);
|
|
60
|
+
if (!complete && this.phase === "metadata")
|
|
61
|
+
return "";
|
|
62
|
+
const line = this.partial.endsWith("\r") ? this.partial.slice(0, -1) : this.partial;
|
|
63
|
+
const rest = nl === -1 ? "" : chunk.slice(nl + 1);
|
|
64
|
+
if (this.phase === "opening") {
|
|
65
|
+
if (!complete && /^\uFEFF?(?:-{0,2}|---[ \t]*)$/.test(line))
|
|
66
|
+
return "";
|
|
67
|
+
if (nl === -1 || !/^\uFEFF?---[ \t]*$/.test(line)) {
|
|
68
|
+
return this.release(this.partial + (nl === -1 ? "" : "\n" + rest));
|
|
69
|
+
}
|
|
70
|
+
this.phase = "first";
|
|
71
|
+
} else {
|
|
72
|
+
if (complete && YAML_CLOSE.test(line))
|
|
73
|
+
return this.release(rest);
|
|
74
|
+
if (this.phase === "first") {
|
|
75
|
+
const kind = classifyFirstLine(line, complete);
|
|
76
|
+
if (kind === "body") {
|
|
77
|
+
return this.release(this.held + this.partial + (nl === -1 ? "" : "\n" + rest));
|
|
78
|
+
}
|
|
79
|
+
if (kind === "metadata")
|
|
80
|
+
this.phase = "metadata";
|
|
81
|
+
}
|
|
82
|
+
if (nl === -1)
|
|
83
|
+
return final ? this.release(this.held + this.partial) : "";
|
|
84
|
+
this.held += this.partial + "\n";
|
|
85
|
+
}
|
|
86
|
+
this.partial = "";
|
|
87
|
+
start = nl + 1;
|
|
88
|
+
if (start === chunk.length && !final)
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function hasNonDecimalPeriodEnd(text) {
|
|
94
|
+
for (let i = 0; i < text.length; i++) {
|
|
95
|
+
if (text.charCodeAt(i) !== 46)
|
|
96
|
+
continue;
|
|
97
|
+
const prev = i > 0 ? text.charCodeAt(i - 1) : -1;
|
|
98
|
+
if (prev >= 48 && prev <= 57)
|
|
99
|
+
continue;
|
|
100
|
+
if (i + 1 === text.length || /\s/.test(text[i + 1]))
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
function hasOpenInlineConstruct(text, maxOpenWindow) {
|
|
106
|
+
const limit = Math.max(16, Math.min(maxOpenWindow, 160));
|
|
107
|
+
const openWiki = text.lastIndexOf("[[");
|
|
108
|
+
if (openWiki !== -1 && text.indexOf("]]", openWiki + 2) === -1 && text.length - openWiki < limit && !/[。!?\n]/.test(text.slice(openWiki + 2))) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
const openLinkParen = text.lastIndexOf("](");
|
|
112
|
+
if (openLinkParen !== -1 && text.indexOf(")", openLinkParen + 2) === -1 && text.length - openLinkParen < limit && !/[\s\n]/.test(text.slice(openLinkParen + 2))) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
const openBracket = text.lastIndexOf("[");
|
|
116
|
+
if (openBracket !== -1 && text.indexOf("]", openBracket + 1) === -1 && text.length - openBracket < limit && !/[。!?\n]/.test(text.slice(openBracket + 1)) && !hasNonDecimalPeriodEnd(text.slice(openBracket + 1))) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
const openAngleUrl = text.lastIndexOf("<http");
|
|
120
|
+
if (openAngleUrl !== -1 && text.indexOf(">", openAngleUrl + 5) === -1 && text.length - openAngleUrl < limit && !/[\s\n]/.test(text.slice(openAngleUrl + 5))) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
const bareUrlMatch = /https?:\/\/[^\s<>()[\]"'”’),。!?、;:]*$/.exec(text);
|
|
124
|
+
if (bareUrlMatch && bareUrlMatch[0].length < limit)
|
|
125
|
+
return true;
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
function isTableDelimiterLine(line) {
|
|
129
|
+
const trimmed = line.trim();
|
|
130
|
+
if (!trimmed || !trimmed.includes("|"))
|
|
131
|
+
return false;
|
|
132
|
+
const stripped = trimmed.replace(/^\|/, "").replace(/\|$/, "");
|
|
133
|
+
const cells = stripped.split("|").map((c) => c.trim());
|
|
134
|
+
if (cells.length === 0)
|
|
135
|
+
return false;
|
|
136
|
+
return cells.every((cell) => TABLE_DELIM_CELL_RE.test(cell));
|
|
137
|
+
}
|
|
138
|
+
function isTableRowLine(line) {
|
|
139
|
+
const trimmed = line.trim();
|
|
140
|
+
if (!trimmed)
|
|
141
|
+
return false;
|
|
142
|
+
const withoutWikilinks = trimmed.replace(/\[\[[^\]]*\]\]/g, "");
|
|
143
|
+
return UNESCAPED_PIPE_RE.test(withoutWikilinks);
|
|
144
|
+
}
|
|
145
|
+
function stripMarkdownTables(text, options = {}) {
|
|
146
|
+
const policy = options.policy ?? "skip";
|
|
147
|
+
const placeholder = options.placeholder ?? DEFAULT_TABLE_PLACEHOLDER;
|
|
148
|
+
const lines = String(text).split(/\r?\n/);
|
|
149
|
+
const out = [];
|
|
150
|
+
let fenceChar = null;
|
|
151
|
+
for (let i = 0; i < lines.length; i++) {
|
|
152
|
+
const line = lines[i];
|
|
153
|
+
const trimmed = line.trimStart();
|
|
154
|
+
const fenceMatch = /^(`{3,}|~{3,})/.exec(trimmed);
|
|
155
|
+
if (fenceMatch) {
|
|
156
|
+
const ch = fenceMatch[1][0];
|
|
157
|
+
if (fenceChar === null)
|
|
158
|
+
fenceChar = ch;
|
|
159
|
+
else if (fenceChar === ch)
|
|
160
|
+
fenceChar = null;
|
|
161
|
+
out.push(line);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (fenceChar !== null) {
|
|
165
|
+
out.push(line);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (isTableRowLine(line) && i + 1 < lines.length && isTableDelimiterLine(lines[i + 1])) {
|
|
169
|
+
i += 1;
|
|
170
|
+
while (i + 1 < lines.length && isTableRowLine(lines[i + 1])) {
|
|
171
|
+
i += 1;
|
|
172
|
+
}
|
|
173
|
+
if (policy === "placeholder" && placeholder.trim()) {
|
|
174
|
+
out.push(placeholder);
|
|
175
|
+
} else {
|
|
176
|
+
out.push("");
|
|
177
|
+
}
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (isTableDelimiterLine(line)) {
|
|
181
|
+
out.push("");
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
out.push(line);
|
|
185
|
+
}
|
|
186
|
+
return out.join("\n");
|
|
187
|
+
}
|
|
188
|
+
function cleanMarkdownBody(text, options) {
|
|
189
|
+
const withoutFences = text.replace(/`{3,}[\s\S]*?`{3,}/g, " ").replace(/~{3,}[\s\S]*?~{3,}/g, " ");
|
|
190
|
+
const withoutMath = withoutFences.replace(/\$\$[\s\S]*?\$\$/g, " ");
|
|
191
|
+
const withoutTables = stripMarkdownTables(withoutMath, options);
|
|
192
|
+
return withoutTables.replace(/`([^`]*)`/g, "$1").replace(/!\[\[[^\]]*\]\]/g, " ").replace(/\[\[([^\]|]+)\|([^\]]+)\]\]/g, "$2").replace(/\[\[([^\]]+)\]\]/g, "$1").replace(/!\[[^\]]*\]\([^)]*\)/g, " ").replace(/\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/<https?:\/\/[^\s<>]+>/gi, " ").replace(/https?:\/\/[^\s<>()[\]"'”’),。!?、;:]+/gi, " ").replace(/^[ \t]*>[ \t]*\[![A-Za-z0-9_-]+\][+-]?(?:[ \t]+(.*))?$/gm, "$1").replace(/^#{1,6}\s+/gm, "").replace(/^(?:[ \t]*>[ \t]?)+/gm, "").replace(/\*\*([^*]+)\*\*/g, "$1").replace(/\*([^*]+)\*/g, "$1").replace(/^[-*+]\s+/gm, "").replace(/^\d+\.\s+/gm, "").replace(/^[-*_]{3,}\s*$/gm, " ").replace(/<\/?(?:b|i|u|br|p|span|div|strong|em|s|sub|sup|h[1-6]|ul|ol|li|a|img|code|pre|blockquote|hr|table|tr|td|th)\b[^>]*>/gi, " ");
|
|
193
|
+
}
|
|
194
|
+
function sanitizeForTts(text) {
|
|
195
|
+
return String(text).replace(/[*_#|^=+~`]/g, " ").replace(/\s{2,}/g, " ").replace(/([\u3400-\u9fff])\s+(?=[\u3400-\u9fff])/g, "$1").trim();
|
|
196
|
+
}
|
|
197
|
+
function splitSentences(chunk) {
|
|
198
|
+
const sentences = [];
|
|
199
|
+
let start = 0;
|
|
200
|
+
const re = /[。!?!?;;…\n]+|\.(?=\s|$)/g;
|
|
201
|
+
let m;
|
|
202
|
+
let lastEnd = 0;
|
|
203
|
+
while ((m = re.exec(chunk)) !== null) {
|
|
204
|
+
const end = m.index + m[0].length;
|
|
205
|
+
if (m[0] === "." && end === chunk.length && /\d/.test(chunk[m.index - 1] ?? ""))
|
|
206
|
+
continue;
|
|
207
|
+
sentences.push(chunk.slice(start, end));
|
|
208
|
+
start = end;
|
|
209
|
+
lastEnd = end;
|
|
210
|
+
}
|
|
211
|
+
return { sentences, tail: chunk.slice(lastEnd) };
|
|
212
|
+
}
|
|
213
|
+
function isHighSurrogate(code) {
|
|
214
|
+
return code >= 55296 && code <= 56319;
|
|
215
|
+
}
|
|
216
|
+
function isLowSurrogate(code) {
|
|
217
|
+
return code >= 56320 && code <= 57343;
|
|
218
|
+
}
|
|
219
|
+
function cutHead(text, max) {
|
|
220
|
+
if (!text || !Number.isFinite(max) || max <= 0)
|
|
221
|
+
return ["", text];
|
|
222
|
+
if (text.length <= max)
|
|
223
|
+
return [text, ""];
|
|
224
|
+
let idx = -1;
|
|
225
|
+
for (let i = Math.min(Math.floor(max), text.length) - 1; i > 0; i--) {
|
|
226
|
+
if (/[,,、\s]/.test(text[i])) {
|
|
227
|
+
idx = i + 1;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (idx <= 0)
|
|
232
|
+
idx = Math.max(1, Math.floor(max / 2));
|
|
233
|
+
if (idx > 0 && idx < text.length && isHighSurrogate(text.charCodeAt(idx - 1)) && isLowSurrogate(text.charCodeAt(idx))) {
|
|
234
|
+
idx = idx > 1 ? idx - 1 : Math.min(2, text.length);
|
|
235
|
+
}
|
|
236
|
+
return [text.slice(0, idx), text.slice(idx)];
|
|
237
|
+
}
|
|
238
|
+
var SentenceSegmenter = class {
|
|
239
|
+
rawBuffer = "";
|
|
240
|
+
maxChars;
|
|
241
|
+
tablePolicy;
|
|
242
|
+
tablePlaceholder;
|
|
243
|
+
// YAML frontmatter state (document start only)
|
|
244
|
+
frontmatter = new LeadingFrontmatter();
|
|
245
|
+
// Cross-delta code fence state (` ``` ` and `~~~`)
|
|
246
|
+
fenceChar = null;
|
|
247
|
+
hangChars = "";
|
|
248
|
+
fenced = "";
|
|
249
|
+
// Cross-delta block math state (`$$...$$`)
|
|
250
|
+
inMath = false;
|
|
251
|
+
hangDollar = "";
|
|
252
|
+
mathBuf = "";
|
|
253
|
+
// Cross-delta Markdown pipe table state
|
|
254
|
+
inTable = false;
|
|
255
|
+
tableHintEmitted = false;
|
|
256
|
+
pendingHeader = null;
|
|
257
|
+
lineBuf = "";
|
|
258
|
+
lineCommitted = false;
|
|
259
|
+
constructor(options = {}) {
|
|
260
|
+
const requested = options.maxSentenceChars ?? 200;
|
|
261
|
+
this.maxChars = Number.isFinite(requested) ? Math.max(2, Math.floor(requested)) : 200;
|
|
262
|
+
this.tablePolicy = options.tablePolicy ?? "skip";
|
|
263
|
+
this.tablePlaceholder = options.tablePlaceholder ?? DEFAULT_TABLE_PLACEHOLDER;
|
|
264
|
+
}
|
|
265
|
+
emitTableBoundary() {
|
|
266
|
+
if (this.tablePolicy === "placeholder" && !this.tableHintEmitted && this.tablePlaceholder.trim()) {
|
|
267
|
+
this.tableHintEmitted = true;
|
|
268
|
+
return `${this.tablePlaceholder}
|
|
269
|
+
`;
|
|
270
|
+
}
|
|
271
|
+
return "\n";
|
|
272
|
+
}
|
|
273
|
+
/** Cross-delta fenced code block filter supporting both ` ``` ` and `~~~`. */
|
|
274
|
+
dropFences(chunk) {
|
|
275
|
+
let s = this.hangChars + chunk;
|
|
276
|
+
this.hangChars = "";
|
|
277
|
+
const trail = /(`+|~+)$/.exec(s);
|
|
278
|
+
const hang = trail ? trail[0] : "";
|
|
279
|
+
if (hang.length > 0 && hang.length <= 2) {
|
|
280
|
+
this.hangChars = hang;
|
|
281
|
+
s = s.slice(0, -hang.length);
|
|
282
|
+
}
|
|
283
|
+
let out = "";
|
|
284
|
+
for (; ; ) {
|
|
285
|
+
const m = this.fenceChar === null ? /(`{3,}|~{3,})/.exec(s) : this.fenceChar === "`" ? /`{3,}/.exec(s) : /~{3,}/.exec(s);
|
|
286
|
+
if (!m) {
|
|
287
|
+
if (this.fenceChar !== null)
|
|
288
|
+
this.fenced += s;
|
|
289
|
+
else
|
|
290
|
+
out += s;
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
if (this.fenceChar !== null) {
|
|
294
|
+
this.fenced += s.slice(0, m.index);
|
|
295
|
+
this.fenceChar = null;
|
|
296
|
+
this.fenced = "";
|
|
297
|
+
} else {
|
|
298
|
+
out += s.slice(0, m.index) + "\n";
|
|
299
|
+
this.fenceChar = m[0][0];
|
|
300
|
+
}
|
|
301
|
+
s = s.slice(m.index + m[0].length);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** Cross-delta block math (`$$...$$`) filter. */
|
|
305
|
+
dropBlockMath(chunk) {
|
|
306
|
+
let s = this.hangDollar + chunk;
|
|
307
|
+
this.hangDollar = "";
|
|
308
|
+
if (s.endsWith("$") && !s.endsWith("$$")) {
|
|
309
|
+
this.hangDollar = "$";
|
|
310
|
+
s = s.slice(0, -1);
|
|
311
|
+
}
|
|
312
|
+
let out = "";
|
|
313
|
+
for (; ; ) {
|
|
314
|
+
const idx = s.indexOf("$$");
|
|
315
|
+
if (idx === -1) {
|
|
316
|
+
if (this.inMath)
|
|
317
|
+
this.mathBuf += s;
|
|
318
|
+
else
|
|
319
|
+
out += s;
|
|
320
|
+
return out;
|
|
321
|
+
}
|
|
322
|
+
if (this.inMath) {
|
|
323
|
+
this.mathBuf = "";
|
|
324
|
+
this.inMath = false;
|
|
325
|
+
out += " ";
|
|
326
|
+
} else {
|
|
327
|
+
out += s.slice(0, idx) + " ";
|
|
328
|
+
this.inMath = true;
|
|
329
|
+
}
|
|
330
|
+
s = s.slice(idx + 2);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/** Cross-delta Markdown pipe table filter with zero latency for regular prose lines. */
|
|
334
|
+
filterTables(chunk) {
|
|
335
|
+
const normalized = chunk.replace(/\r\n/g, "\n");
|
|
336
|
+
let out = "";
|
|
337
|
+
let pos = 0;
|
|
338
|
+
while (pos < normalized.length) {
|
|
339
|
+
const nl = normalized.indexOf("\n", pos);
|
|
340
|
+
if (nl !== -1) {
|
|
341
|
+
const segment = normalized.slice(pos, nl);
|
|
342
|
+
pos = nl + 1;
|
|
343
|
+
if (this.lineCommitted) {
|
|
344
|
+
out += segment + "\n";
|
|
345
|
+
this.lineCommitted = false;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const line = this.lineBuf + segment;
|
|
349
|
+
this.lineBuf = "";
|
|
350
|
+
out += this.handleCompleteLine(line);
|
|
351
|
+
} else {
|
|
352
|
+
const rest = normalized.slice(pos);
|
|
353
|
+
pos = normalized.length;
|
|
354
|
+
if (this.lineCommitted) {
|
|
355
|
+
out += rest;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
this.lineBuf += rest;
|
|
359
|
+
if (this.inTable) {
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (this.pendingHeader !== null) {
|
|
363
|
+
if (!TABLE_DELIM_CHARS_RE.test(this.lineBuf)) {
|
|
364
|
+
const releasedHeader = this.pendingHeader;
|
|
365
|
+
this.pendingHeader = null;
|
|
366
|
+
out += releasedHeader + "\n";
|
|
367
|
+
this.tryFlushRegularLineBuf((chunkOut) => {
|
|
368
|
+
out += chunkOut;
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
this.tryFlushRegularLineBuf((chunkOut) => {
|
|
374
|
+
out += chunkOut;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
tryFlushRegularLineBuf(emit) {
|
|
381
|
+
if (hasOpenInlineConstruct(this.lineBuf, this.maxChars))
|
|
382
|
+
return;
|
|
383
|
+
if (isTableRowLine(this.lineBuf))
|
|
384
|
+
return;
|
|
385
|
+
const shouldFlush = INLINE_SENTENCE_END_RE.test(this.lineBuf) || hasNonDecimalPeriodEnd(this.lineBuf) || this.lineBuf.length >= this.maxChars;
|
|
386
|
+
if (shouldFlush) {
|
|
387
|
+
emit(this.lineBuf);
|
|
388
|
+
this.lineBuf = "";
|
|
389
|
+
this.lineCommitted = true;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
handleCompleteLine(line) {
|
|
393
|
+
if (this.inTable) {
|
|
394
|
+
if (isTableRowLine(line) || isTableDelimiterLine(line)) {
|
|
395
|
+
return "";
|
|
396
|
+
}
|
|
397
|
+
this.inTable = false;
|
|
398
|
+
this.tableHintEmitted = false;
|
|
399
|
+
if (!line.trim())
|
|
400
|
+
return "\n";
|
|
401
|
+
return line + "\n";
|
|
402
|
+
}
|
|
403
|
+
if (this.pendingHeader !== null) {
|
|
404
|
+
const header = this.pendingHeader;
|
|
405
|
+
this.pendingHeader = null;
|
|
406
|
+
if (isTableDelimiterLine(line)) {
|
|
407
|
+
this.inTable = true;
|
|
408
|
+
this.tableHintEmitted = false;
|
|
409
|
+
return this.emitTableBoundary();
|
|
410
|
+
}
|
|
411
|
+
const emitted = header + "\n";
|
|
412
|
+
if (isTableRowLine(line)) {
|
|
413
|
+
this.pendingHeader = line;
|
|
414
|
+
return emitted;
|
|
415
|
+
}
|
|
416
|
+
return emitted + line + "\n";
|
|
417
|
+
}
|
|
418
|
+
if (isTableDelimiterLine(line)) {
|
|
419
|
+
return "\n";
|
|
420
|
+
}
|
|
421
|
+
if (isTableRowLine(line)) {
|
|
422
|
+
this.pendingHeader = line;
|
|
423
|
+
return "";
|
|
424
|
+
}
|
|
425
|
+
return line + "\n";
|
|
426
|
+
}
|
|
427
|
+
flushTableState() {
|
|
428
|
+
let out = "";
|
|
429
|
+
if (this.inTable) {
|
|
430
|
+
if (this.lineBuf && !isTableRowLine(this.lineBuf) && !isTableDelimiterLine(this.lineBuf)) {
|
|
431
|
+
out += this.lineBuf;
|
|
432
|
+
}
|
|
433
|
+
} else if (this.pendingHeader !== null) {
|
|
434
|
+
if (this.lineBuf && isTableDelimiterLine(this.lineBuf)) {
|
|
435
|
+
out += this.emitTableBoundary();
|
|
436
|
+
} else {
|
|
437
|
+
out += this.pendingHeader + "\n";
|
|
438
|
+
if (this.lineBuf && !isTableDelimiterLine(this.lineBuf)) {
|
|
439
|
+
out += this.lineBuf;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
} else if (this.lineBuf && !isTableDelimiterLine(this.lineBuf)) {
|
|
443
|
+
out += this.lineBuf;
|
|
444
|
+
}
|
|
445
|
+
this.inTable = false;
|
|
446
|
+
this.tableHintEmitted = false;
|
|
447
|
+
this.pendingHeader = null;
|
|
448
|
+
this.lineBuf = "";
|
|
449
|
+
this.lineCommitted = false;
|
|
450
|
+
return out;
|
|
451
|
+
}
|
|
452
|
+
cleanCompleteText(raw) {
|
|
453
|
+
return cleanMarkdownBody(raw, {
|
|
454
|
+
policy: this.tablePolicy,
|
|
455
|
+
placeholder: this.tablePlaceholder
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
capSentence(s, out) {
|
|
459
|
+
let rest = s;
|
|
460
|
+
while (rest.length > this.maxChars) {
|
|
461
|
+
const [head, tail] = cutHead(rest, this.maxChars);
|
|
462
|
+
if (!head)
|
|
463
|
+
break;
|
|
464
|
+
rest = tail;
|
|
465
|
+
const t2 = sanitizeForTts(head).trim();
|
|
466
|
+
if (t2 && !SKIP_PREFIX.test(t2))
|
|
467
|
+
out.push(t2);
|
|
468
|
+
}
|
|
469
|
+
const t = sanitizeForTts(rest).trim();
|
|
470
|
+
if (t && !SKIP_PREFIX.test(t))
|
|
471
|
+
out.push(t);
|
|
472
|
+
}
|
|
473
|
+
/** Split `rawBuffer` at complete sentence boundaries, running `plainText()` per complete sentence. */
|
|
474
|
+
drainRawBuffer(out, isFinal) {
|
|
475
|
+
if (!this.rawBuffer)
|
|
476
|
+
return;
|
|
477
|
+
if (!isFinal && hasOpenInlineConstruct(this.rawBuffer, this.maxChars)) {
|
|
478
|
+
const lastNl = this.rawBuffer.lastIndexOf("\n");
|
|
479
|
+
if (lastNl === -1)
|
|
480
|
+
return;
|
|
481
|
+
const readyPrefix = this.rawBuffer.slice(0, lastNl + 1);
|
|
482
|
+
this.rawBuffer = this.rawBuffer.slice(lastNl + 1);
|
|
483
|
+
const cleaned = this.cleanCompleteText(readyPrefix);
|
|
484
|
+
const { sentences: sentences2, tail: tail2 } = splitSentences(cleaned);
|
|
485
|
+
for (const s of sentences2)
|
|
486
|
+
this.capSentence(s, out);
|
|
487
|
+
if (tail2.trim())
|
|
488
|
+
this.capSentence(tail2, out);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const { sentences, tail } = splitSentences(this.rawBuffer);
|
|
492
|
+
this.rawBuffer = tail;
|
|
493
|
+
for (const rawSentence of sentences) {
|
|
494
|
+
const cleaned = this.cleanCompleteText(rawSentence);
|
|
495
|
+
if (!cleaned)
|
|
496
|
+
continue;
|
|
497
|
+
const sub = splitSentences(cleaned);
|
|
498
|
+
for (const s of sub.sentences)
|
|
499
|
+
this.capSentence(s, out);
|
|
500
|
+
if (sub.tail.trim())
|
|
501
|
+
this.capSentence(sub.tail, out);
|
|
502
|
+
}
|
|
503
|
+
if (!isFinal) {
|
|
504
|
+
while (this.rawBuffer.length > this.maxChars && !hasOpenInlineConstruct(this.rawBuffer, this.maxChars)) {
|
|
505
|
+
const cleaned = this.cleanCompleteText(this.rawBuffer);
|
|
506
|
+
if (cleaned.length <= this.maxChars) {
|
|
507
|
+
this.rawBuffer = cleaned;
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
const [head, rest] = cutHead(cleaned, this.maxChars);
|
|
511
|
+
if (!head)
|
|
512
|
+
break;
|
|
513
|
+
this.rawBuffer = rest;
|
|
514
|
+
const t = sanitizeForTts(head).trim();
|
|
515
|
+
if (t && !SKIP_PREFIX.test(t))
|
|
516
|
+
out.push(t);
|
|
517
|
+
}
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (this.rawBuffer) {
|
|
521
|
+
const cleaned = this.cleanCompleteText(this.rawBuffer);
|
|
522
|
+
this.rawBuffer = "";
|
|
523
|
+
if (cleaned) {
|
|
524
|
+
const sub = splitSentences(cleaned);
|
|
525
|
+
for (const s of sub.sentences)
|
|
526
|
+
this.capSentence(s, out);
|
|
527
|
+
if (sub.tail.trim())
|
|
528
|
+
this.capSentence(sub.tail, out);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/** Feed a raw streaming delta and return all completed sentences. */
|
|
533
|
+
feed(chunk) {
|
|
534
|
+
if (!chunk)
|
|
535
|
+
return [];
|
|
536
|
+
const noFrontmatter = this.frontmatter.read(chunk);
|
|
537
|
+
const noFence = noFrontmatter ? this.dropFences(noFrontmatter) : "";
|
|
538
|
+
const noMath = noFence ? this.dropBlockMath(noFence) : "";
|
|
539
|
+
const noTable = noMath ? this.filterTables(noMath) : "";
|
|
540
|
+
if (!noTable)
|
|
541
|
+
return [];
|
|
542
|
+
this.rawBuffer += noTable;
|
|
543
|
+
const out = [];
|
|
544
|
+
this.drainRawBuffer(out, false);
|
|
545
|
+
return out;
|
|
546
|
+
}
|
|
547
|
+
/** Flush remaining buffered text at end-of-stream and reset internal state. */
|
|
548
|
+
flush() {
|
|
549
|
+
let tailRaw = this.frontmatter.read("", true);
|
|
550
|
+
this.frontmatter = new LeadingFrontmatter();
|
|
551
|
+
if (this.hangChars) {
|
|
552
|
+
if (this.fenceChar !== null)
|
|
553
|
+
this.fenced += this.hangChars;
|
|
554
|
+
else
|
|
555
|
+
tailRaw += this.hangChars;
|
|
556
|
+
this.hangChars = "";
|
|
557
|
+
}
|
|
558
|
+
if (this.hangDollar) {
|
|
559
|
+
if (this.inMath)
|
|
560
|
+
this.mathBuf += this.hangDollar;
|
|
561
|
+
else
|
|
562
|
+
tailRaw += this.hangDollar;
|
|
563
|
+
this.hangDollar = "";
|
|
564
|
+
}
|
|
565
|
+
if (this.inMath) {
|
|
566
|
+
tailRaw += this.mathBuf;
|
|
567
|
+
this.inMath = false;
|
|
568
|
+
this.mathBuf = "";
|
|
569
|
+
}
|
|
570
|
+
if (tailRaw) {
|
|
571
|
+
const filtered = this.filterTables(tailRaw);
|
|
572
|
+
if (filtered)
|
|
573
|
+
this.rawBuffer += filtered;
|
|
574
|
+
}
|
|
575
|
+
const trailingTable = this.flushTableState();
|
|
576
|
+
if (trailingTable)
|
|
577
|
+
this.rawBuffer += trailingTable;
|
|
578
|
+
if (this.fenceChar !== null) {
|
|
579
|
+
const restored = this.fenced.replace(FENCE_LANG_PREFIX_RE, "");
|
|
580
|
+
this.rawBuffer += restored;
|
|
581
|
+
this.fenceChar = null;
|
|
582
|
+
this.fenced = "";
|
|
583
|
+
}
|
|
584
|
+
const out = [];
|
|
585
|
+
this.drainRawBuffer(out, true);
|
|
586
|
+
this.rawBuffer = "";
|
|
587
|
+
return out;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/asr-replacements.js
|
|
592
|
+
var UTF8_ENCODER = new TextEncoder();
|
|
593
|
+
var ASCII_WORD = /[A-Za-z0-9]/;
|
|
594
|
+
var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
595
|
+
function compilePattern(heard) {
|
|
596
|
+
const trimmedStr = heard.replace(/\s+/g, " ").trim();
|
|
597
|
+
const chars = [...trimmedStr];
|
|
598
|
+
if (chars.length === 0)
|
|
599
|
+
return null;
|
|
600
|
+
let body = "";
|
|
601
|
+
let prev = "";
|
|
602
|
+
let spaced = false;
|
|
603
|
+
for (const ch of chars) {
|
|
604
|
+
if (ch === " ") {
|
|
605
|
+
spaced = true;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (prev && (spaced || ASCII_WORD.test(prev) !== ASCII_WORD.test(ch)))
|
|
609
|
+
body += "\\s*";
|
|
610
|
+
body += escapeRegExp(ch);
|
|
611
|
+
prev = ch;
|
|
612
|
+
spaced = false;
|
|
613
|
+
}
|
|
614
|
+
const first = chars[0];
|
|
615
|
+
const last = chars[chars.length - 1];
|
|
616
|
+
const requireLeftBound = ASCII_WORD.test(first);
|
|
617
|
+
const tail = ASCII_WORD.test(last) ? "(?![A-Za-z0-9])" : "";
|
|
618
|
+
return { pattern: `${body}${tail}`, requireLeftBound };
|
|
619
|
+
}
|
|
620
|
+
function createAsrReplacer(rules, onHit) {
|
|
621
|
+
const entries = [];
|
|
622
|
+
rules.forEach((rule, ruleIndex) => {
|
|
623
|
+
if (!rule.to)
|
|
624
|
+
return;
|
|
625
|
+
for (const heard of rule.from) {
|
|
626
|
+
const compiled = compilePattern(heard);
|
|
627
|
+
if (!compiled)
|
|
628
|
+
continue;
|
|
629
|
+
const compact = heard.replace(/\s+/g, "");
|
|
630
|
+
entries.push({
|
|
631
|
+
pattern: compiled.pattern,
|
|
632
|
+
key: compact.toLowerCase(),
|
|
633
|
+
to: rule.to,
|
|
634
|
+
length: [...compact].length,
|
|
635
|
+
ruleIndex,
|
|
636
|
+
requireLeftBound: compiled.requireLeftBound
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
if (entries.length === 0)
|
|
641
|
+
return (text) => text;
|
|
642
|
+
entries.sort((a, b) => b.length - a.length);
|
|
643
|
+
const targets = /* @__PURE__ */ new Map();
|
|
644
|
+
for (const e of entries) {
|
|
645
|
+
if (!targets.has(e.key)) {
|
|
646
|
+
targets.set(e.key, {
|
|
647
|
+
to: e.to,
|
|
648
|
+
ruleIndex: e.ruleIndex,
|
|
649
|
+
requireLeftBound: e.requireLeftBound
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const source = entries.map((e) => `(?:${e.pattern})`).join("|");
|
|
654
|
+
return (text, countHit = true) => {
|
|
655
|
+
if (!text)
|
|
656
|
+
return text;
|
|
657
|
+
const regex = new RegExp(source, "giu");
|
|
658
|
+
let out = "";
|
|
659
|
+
let cursor = 0;
|
|
660
|
+
let m;
|
|
661
|
+
while ((m = regex.exec(text)) !== null) {
|
|
662
|
+
const match = m[0];
|
|
663
|
+
const offset = m.index;
|
|
664
|
+
if (match.length === 0) {
|
|
665
|
+
regex.lastIndex = offset + 1;
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
const hit = targets.get(match.replace(/\s+/g, "").toLowerCase());
|
|
669
|
+
if (!hit) {
|
|
670
|
+
regex.lastIndex = offset + 1;
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if (hit.requireLeftBound && offset > 0 && ASCII_WORD.test(text[offset - 1])) {
|
|
674
|
+
regex.lastIndex = offset + 1;
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
out += text.slice(cursor, offset) + hit.to;
|
|
678
|
+
if (onHit && countHit)
|
|
679
|
+
onHit(hit.ruleIndex);
|
|
680
|
+
cursor = offset + match.length;
|
|
681
|
+
regex.lastIndex = cursor;
|
|
682
|
+
}
|
|
683
|
+
return cursor === 0 ? text : out + text.slice(cursor);
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/emotion.js
|
|
688
|
+
var EMOTION_TAG_PATTERN = "<\\s*(break\\s+(?<ms>\\d+)\\s*ms|whisper|\\/whisper|laugh|sigh|emphasis)\\s*>";
|
|
689
|
+
var TAG_RE = new RegExp(EMOTION_TAG_PATTERN, "gi");
|
|
690
|
+
var STRIP_RE = new RegExp(EMOTION_TAG_PATTERN, "gi");
|
|
691
|
+
var MAX_BREAK_MS = 5e3;
|
|
692
|
+
var MAX_TOTAL_BREAK_MS = 1e4;
|
|
693
|
+
function stripEmotionTags(raw) {
|
|
694
|
+
return String(raw).replace(STRIP_RE, " ");
|
|
695
|
+
}
|
|
696
|
+
function parseEmotionTags(raw) {
|
|
697
|
+
const input = String(raw);
|
|
698
|
+
const out = [];
|
|
699
|
+
let buf = "";
|
|
700
|
+
let whisper = false;
|
|
701
|
+
let pendingBreakMs = 0;
|
|
702
|
+
let totalBreakMs = 0;
|
|
703
|
+
let sawInlineTag = false;
|
|
704
|
+
const appendText = (chunk) => {
|
|
705
|
+
if (chunk.length === 0)
|
|
706
|
+
return;
|
|
707
|
+
if (sawInlineTag && /[A-Za-z0-9]$/.test(buf) && /^[A-Za-z0-9]/.test(chunk)) {
|
|
708
|
+
buf += " ";
|
|
709
|
+
}
|
|
710
|
+
sawInlineTag = false;
|
|
711
|
+
buf += chunk;
|
|
712
|
+
};
|
|
713
|
+
const flush = () => {
|
|
714
|
+
const text = buf.trim();
|
|
715
|
+
buf = "";
|
|
716
|
+
sawInlineTag = false;
|
|
717
|
+
if (text.length === 0)
|
|
718
|
+
return;
|
|
719
|
+
out.push({ text, whisper });
|
|
720
|
+
};
|
|
721
|
+
let lastEnd = 0;
|
|
722
|
+
for (const m of input.matchAll(TAG_RE)) {
|
|
723
|
+
const idx = m.index ?? 0;
|
|
724
|
+
appendText(input.slice(lastEnd, idx));
|
|
725
|
+
lastEnd = idx + m[0].length;
|
|
726
|
+
const tag = (m[1] ?? "").toLowerCase().trim();
|
|
727
|
+
if (tag.startsWith("break")) {
|
|
728
|
+
const rawMs = Number(m.groups?.ms ?? 0);
|
|
729
|
+
if (!Number.isFinite(rawMs) || rawMs <= 0) {
|
|
730
|
+
sawInlineTag = true;
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
const remaining = Math.max(0, MAX_TOTAL_BREAK_MS - totalBreakMs);
|
|
734
|
+
const ms = Math.min(rawMs, MAX_BREAK_MS, remaining);
|
|
735
|
+
if (ms <= 0) {
|
|
736
|
+
if (buf.trim().length > 0)
|
|
737
|
+
flush();
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
totalBreakMs += ms;
|
|
741
|
+
if (buf.trim().length > 0) {
|
|
742
|
+
flush();
|
|
743
|
+
}
|
|
744
|
+
const last = out[out.length - 1];
|
|
745
|
+
if (last) {
|
|
746
|
+
last.preBreakMs = (last.preBreakMs ?? 0) + ms;
|
|
747
|
+
} else {
|
|
748
|
+
pendingBreakMs += ms;
|
|
749
|
+
}
|
|
750
|
+
} else if (tag === "whisper") {
|
|
751
|
+
flush();
|
|
752
|
+
whisper = true;
|
|
753
|
+
} else if (tag === "/whisper") {
|
|
754
|
+
flush();
|
|
755
|
+
whisper = false;
|
|
756
|
+
} else {
|
|
757
|
+
sawInlineTag = true;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
appendText(input.slice(lastEnd));
|
|
761
|
+
flush();
|
|
762
|
+
if (pendingBreakMs > 0) {
|
|
763
|
+
const first = out[0];
|
|
764
|
+
if (first) {
|
|
765
|
+
first.preBreakMs = (first.preBreakMs ?? 0) + pendingBreakMs;
|
|
766
|
+
}
|
|
767
|
+
pendingBreakMs = 0;
|
|
768
|
+
}
|
|
769
|
+
if (whisper) {
|
|
770
|
+
for (const seg of out)
|
|
771
|
+
seg.whisper = false;
|
|
772
|
+
}
|
|
773
|
+
return out;
|
|
774
|
+
}
|
|
775
|
+
var ANY_EMOTION_CUE_RE = new RegExp(EMOTION_TAG_PATTERN, "i");
|
|
776
|
+
|
|
777
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/redact.js
|
|
11
778
|
var PREFIXED = [
|
|
12
779
|
{ re: /\b(Bearer\s+)[A-Za-z0-9._~+/=-]{8,}/gi, to: "$1<redacted>" },
|
|
13
780
|
{ re: /\b(Basic\s+)[A-Za-z0-9+/=-]{8,}/gi, to: "$1<redacted>" },
|
|
14
781
|
{ re: /\b((?:api[-_]?key|x-api-key)\s*[:=]\s*)[A-Za-z0-9._~+/=-]{8,}/gi, to: "$1<redacted>" },
|
|
15
|
-
// URL 内嵌凭据(https://user:pass@host)与查询参数密钥(?api_key=... / &token=... / &key=...)
|
|
16
782
|
{ re: /(https?:\/\/)([^:@/\s]+:[^@/\s]+)(@)/gi, to: "$1<redacted>$3" },
|
|
17
783
|
{ re: /([?&](?:api[_-]?key|access_token|token|key)=)([^&\s#]+)/gi, to: "$1<redacted>" },
|
|
18
|
-
// JSON
|
|
784
|
+
// Matches both complete JSON string values and values truncated before the closing quote.
|
|
19
785
|
{
|
|
20
|
-
re: /("(?:[a-zA-Z0-9_-]*?(?:api[_-]?key|token|secret|password))"\s*:\s*")([^"\\]{6,})(")/gi,
|
|
786
|
+
re: /("(?:[a-zA-Z0-9_-]*?(?:api[_-]?key|token|secret|password))"\s*:\s*")([^"\\]{6,})("|$)/gi,
|
|
21
787
|
to: "$1<redacted>$3"
|
|
22
788
|
}
|
|
23
|
-
];
|
|
24
|
-
var BARE = [
|
|
25
|
-
/\bsk
|
|
26
|
-
|
|
27
|
-
/\bgsk_[A-Za-z0-9._-]{8,}/g,
|
|
28
|
-
/\
|
|
29
|
-
/\
|
|
30
|
-
/\
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
789
|
+
];
|
|
790
|
+
var BARE = [
|
|
791
|
+
/\bsk-(?:proj|live|test|ant)-[A-Za-z0-9._-]{8,}/g,
|
|
792
|
+
/\bsk[-_][A-Za-z0-9._-]{8,}/g,
|
|
793
|
+
/\bgsk_[A-Za-z0-9._-]{8,}/g,
|
|
794
|
+
/\brk_(?:live|test)_[A-Za-z0-9._-]{8,}/g,
|
|
795
|
+
/\bAIza[A-Za-z0-9._-]{10,}/g,
|
|
796
|
+
/\bgh[pousr]_[A-Za-z0-9_]{10,}/g,
|
|
797
|
+
// Mixed-case + digit high-entropy token (>=20 chars); excludes 64-hex SHA256 digests and '/' paths.
|
|
798
|
+
/\b(?![0-9a-fA-F]{64}\b)(?=[A-Za-z0-9+=_-]*[A-Z])(?=[A-Za-z0-9+=_-]*[a-z])(?=[A-Za-z0-9+=_-]*[0-9])[A-Za-z0-9+=_-]{20,}\b/g,
|
|
799
|
+
// 32-hex keys (MD5-shaped secrets); 64-hex SHA256 digests remain intact.
|
|
800
|
+
/\b[0-9a-f]{32}\b/gi
|
|
801
|
+
];
|
|
802
|
+
function coerceText(value) {
|
|
803
|
+
if (typeof value === "string")
|
|
804
|
+
return value;
|
|
805
|
+
if (value === null || value === void 0)
|
|
806
|
+
return "";
|
|
807
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
808
|
+
return String(value);
|
|
809
|
+
}
|
|
810
|
+
if (value instanceof Error)
|
|
811
|
+
return value.message;
|
|
812
|
+
try {
|
|
813
|
+
return JSON.stringify(value) ?? "";
|
|
814
|
+
} catch {
|
|
815
|
+
return "";
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
function redactSecrets(text) {
|
|
819
|
+
let out = coerceText(text);
|
|
820
|
+
for (const { re, to } of PREFIXED)
|
|
821
|
+
out = out.replace(re, to);
|
|
822
|
+
for (const re of BARE)
|
|
823
|
+
out = out.replace(re, "<redacted>");
|
|
824
|
+
return out;
|
|
825
|
+
}
|
|
826
|
+
function redactBodySnippet(body, maxLength = 200) {
|
|
827
|
+
const raw = coerceText(body);
|
|
828
|
+
if (maxLength <= 0)
|
|
829
|
+
return "";
|
|
830
|
+
const preWindow = Math.max(maxLength * 4, 2048);
|
|
831
|
+
return redactSecrets(raw.slice(0, preWindow)).slice(0, maxLength);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/engine-kind.js
|
|
835
|
+
var LOCAL_TTS_ENGINE_KINDS = ["system", "vits-wasm", "kokoro-daemon"];
|
|
836
|
+
function isLocalTtsEngine(value) {
|
|
837
|
+
return LOCAL_TTS_ENGINE_KINDS.includes(value);
|
|
838
|
+
}
|
|
839
|
+
function usesBilingualSample(value) {
|
|
840
|
+
return value === "kokoro-daemon" || value === "openai-compat" || value === "mimo";
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/voice-catalog.js
|
|
844
|
+
var VITS_SPEAKERS = [
|
|
845
|
+
{ name: "suyingxue", sid: 0, zh: "\u7D20\u6620\u96EA", en: "Su Yingxue", gender: "F" },
|
|
846
|
+
{ name: "gunian", sid: 1, zh: "\u987E\u5FF5", en: "Gu Nian", gender: "M" },
|
|
847
|
+
{ name: "fushiyu", sid: 2, zh: "\u5085\u65AF\u9047", en: "Fu Siyu", gender: "F" },
|
|
848
|
+
{ name: "bingjiao", sid: 3, zh: "\u51B0\u5A07", en: "Bing Jiao", gender: "M" },
|
|
849
|
+
{ name: "bazong", sid: 4, zh: "\u9738\u603B", en: "Ba Zong", gender: "M" }
|
|
850
|
+
];
|
|
851
|
+
var KOKORO_F0 = [
|
|
852
|
+
224,
|
|
853
|
+
189,
|
|
854
|
+
154,
|
|
855
|
+
261,
|
|
856
|
+
226,
|
|
857
|
+
222,
|
|
858
|
+
220,
|
|
859
|
+
229,
|
|
860
|
+
198,
|
|
861
|
+
186,
|
|
862
|
+
212,
|
|
863
|
+
293,
|
|
864
|
+
233,
|
|
865
|
+
161,
|
|
866
|
+
247,
|
|
867
|
+
207,
|
|
868
|
+
218,
|
|
869
|
+
216,
|
|
870
|
+
220,
|
|
871
|
+
238,
|
|
872
|
+
242,
|
|
873
|
+
229,
|
|
874
|
+
198,
|
|
875
|
+
286,
|
|
876
|
+
211,
|
|
877
|
+
190,
|
|
878
|
+
264,
|
|
879
|
+
261,
|
|
880
|
+
226,
|
|
881
|
+
147,
|
|
882
|
+
216,
|
|
883
|
+
240,
|
|
884
|
+
233,
|
|
885
|
+
188,
|
|
886
|
+
222,
|
|
887
|
+
247,
|
|
888
|
+
253,
|
|
889
|
+
270,
|
|
890
|
+
276,
|
|
891
|
+
276,
|
|
892
|
+
279,
|
|
893
|
+
320,
|
|
894
|
+
247,
|
|
895
|
+
296,
|
|
896
|
+
276,
|
|
897
|
+
235,
|
|
898
|
+
139,
|
|
899
|
+
240,
|
|
900
|
+
282,
|
|
901
|
+
282,
|
|
902
|
+
238,
|
|
903
|
+
226,
|
|
904
|
+
273,
|
|
905
|
+
216,
|
|
906
|
+
286,
|
|
907
|
+
270,
|
|
908
|
+
198,
|
|
909
|
+
179,
|
|
910
|
+
117,
|
|
911
|
+
130,
|
|
912
|
+
114,
|
|
913
|
+
128,
|
|
914
|
+
108,
|
|
915
|
+
106,
|
|
916
|
+
122,
|
|
917
|
+
136,
|
|
918
|
+
190,
|
|
919
|
+
112,
|
|
920
|
+
108,
|
|
921
|
+
128,
|
|
922
|
+
131,
|
|
923
|
+
111,
|
|
924
|
+
110,
|
|
925
|
+
132,
|
|
926
|
+
138,
|
|
927
|
+
189,
|
|
928
|
+
137,
|
|
929
|
+
148,
|
|
930
|
+
151,
|
|
931
|
+
127,
|
|
932
|
+
135,
|
|
933
|
+
111,
|
|
934
|
+
138,
|
|
935
|
+
114,
|
|
936
|
+
125,
|
|
937
|
+
158,
|
|
938
|
+
128,
|
|
939
|
+
156,
|
|
940
|
+
132,
|
|
941
|
+
162,
|
|
942
|
+
131,
|
|
943
|
+
136,
|
|
944
|
+
142,
|
|
945
|
+
124,
|
|
946
|
+
129,
|
|
947
|
+
136,
|
|
948
|
+
126,
|
|
949
|
+
135,
|
|
950
|
+
161,
|
|
951
|
+
150,
|
|
952
|
+
124,
|
|
953
|
+
104,
|
|
954
|
+
124
|
|
955
|
+
];
|
|
956
|
+
var KOKORO_NAMED = {
|
|
957
|
+
48: { name: "zf_xiaobei", zh: "\u5C0F\u5317", en: "Xiaobei" },
|
|
958
|
+
49: { name: "zf_xiaoni", zh: "\u5C0F\u59AE", en: "Xiaoni" },
|
|
959
|
+
50: { name: "zf_xiaoxiao", zh: "\u5C0F\u5C0F", en: "Xiaoxiao" },
|
|
960
|
+
51: { name: "zf_xiaoyi", zh: "\u5C0F\u827A", en: "Xiaoyi" }
|
|
961
|
+
};
|
|
962
|
+
var KOKORO_PINNED = [62, 68, 75, 76];
|
|
963
|
+
var KOKORO_VOICES = [
|
|
964
|
+
...KOKORO_PINNED.map((sid) => ({ name: KOKORO_NAMED[sid]?.name ?? String(sid), sid })),
|
|
965
|
+
...KOKORO_F0.map((_, sid) => ({ name: KOKORO_NAMED[sid]?.name ?? String(sid), sid })).filter((v) => !KOKORO_PINNED.includes(v.sid))
|
|
966
|
+
];
|
|
967
|
+
var SILICONFLOW_SYSTEM_VOICES = [
|
|
968
|
+
"alex",
|
|
969
|
+
"benjamin",
|
|
970
|
+
"charles",
|
|
971
|
+
"david",
|
|
972
|
+
"anna",
|
|
973
|
+
"bella",
|
|
974
|
+
"claire",
|
|
975
|
+
"diana"
|
|
976
|
+
];
|
|
977
|
+
var OPENAI_API_VOICES = [
|
|
978
|
+
"alloy",
|
|
979
|
+
"ash",
|
|
980
|
+
"ballad",
|
|
981
|
+
"coral",
|
|
982
|
+
"echo",
|
|
983
|
+
"fable",
|
|
984
|
+
"marin",
|
|
985
|
+
"cedar",
|
|
986
|
+
"nova",
|
|
987
|
+
"onyx",
|
|
988
|
+
"sage",
|
|
989
|
+
"shimmer"
|
|
990
|
+
];
|
|
991
|
+
var TTS_OPENAI_PRESETS = {
|
|
992
|
+
"siliconflow-cosyvoice2": {
|
|
993
|
+
id: "siliconflow-cosyvoice2",
|
|
994
|
+
label: "SiliconFlow \xB7 CosyVoice2",
|
|
995
|
+
region: "cn",
|
|
996
|
+
baseUrl: "https://api.siliconflow.cn/v1",
|
|
997
|
+
model: "FunAudioLLM/CosyVoice2-0.5B",
|
|
998
|
+
voice: "FunAudioLLM/CosyVoice2-0.5B:alex",
|
|
999
|
+
voices: SILICONFLOW_SYSTEM_VOICES.map((v) => `FunAudioLLM/CosyVoice2-0.5B:${v}`),
|
|
1000
|
+
requiresKey: true
|
|
1001
|
+
},
|
|
1002
|
+
"siliconflow-fish": {
|
|
1003
|
+
id: "siliconflow-fish",
|
|
1004
|
+
label: "SiliconFlow \xB7 Fish Speech 1.5",
|
|
1005
|
+
region: "cn",
|
|
1006
|
+
baseUrl: "https://api.siliconflow.cn/v1",
|
|
1007
|
+
model: "fishaudio/fish-speech-1.5",
|
|
1008
|
+
voice: "fishaudio/fish-speech-1.5:anna",
|
|
1009
|
+
voices: SILICONFLOW_SYSTEM_VOICES.map((v) => `fishaudio/fish-speech-1.5:${v}`),
|
|
1010
|
+
requiresKey: true
|
|
1011
|
+
},
|
|
1012
|
+
"openrouter-openai": {
|
|
1013
|
+
id: "openrouter-openai",
|
|
1014
|
+
label: "OpenRouter \xB7 GPT-4o Mini TTS",
|
|
1015
|
+
region: "global",
|
|
1016
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
1017
|
+
model: "openai/gpt-4o-mini-tts-2025-12-15",
|
|
1018
|
+
voice: "alloy",
|
|
1019
|
+
voices: [...OPENAI_API_VOICES],
|
|
1020
|
+
requiresKey: true
|
|
1021
|
+
},
|
|
1022
|
+
"openai-official": {
|
|
1023
|
+
id: "openai-official",
|
|
1024
|
+
label: "OpenAI \xB7 GPT-4o Mini TTS",
|
|
1025
|
+
region: "global",
|
|
1026
|
+
baseUrl: "https://api.openai.com/v1",
|
|
1027
|
+
model: "gpt-4o-mini-tts",
|
|
1028
|
+
voice: "marin",
|
|
1029
|
+
voices: [...OPENAI_API_VOICES],
|
|
1030
|
+
requiresKey: true
|
|
1031
|
+
},
|
|
1032
|
+
"groq-playai": {
|
|
1033
|
+
id: "groq-playai",
|
|
1034
|
+
label: "Groq \xB7 PlayAI TTS",
|
|
1035
|
+
region: "global",
|
|
1036
|
+
baseUrl: "https://api.groq.com/openai/v1",
|
|
1037
|
+
model: "playai-tts",
|
|
1038
|
+
voice: "Fritz-PlayAI",
|
|
1039
|
+
voices: ["Fritz-PlayAI"],
|
|
1040
|
+
requiresKey: true
|
|
1041
|
+
},
|
|
1042
|
+
"selfhosted-kokoro": {
|
|
1043
|
+
id: "selfhosted-kokoro",
|
|
1044
|
+
label: "Self-hosted \xB7 Kokoro-FastAPI",
|
|
1045
|
+
region: "local",
|
|
1046
|
+
baseUrl: "http://127.0.0.1:8880/v1",
|
|
1047
|
+
model: "kokoro",
|
|
1048
|
+
voice: "zf_xiaobei",
|
|
1049
|
+
voices: ["zf_xiaobei", "zf_xiaoni", "zf_xiaoxiao", "zf_xiaoyi"],
|
|
1050
|
+
requiresKey: false
|
|
1051
|
+
},
|
|
1052
|
+
"selfhosted-vllm": {
|
|
1053
|
+
id: "selfhosted-vllm",
|
|
1054
|
+
label: "Self-hosted \xB7 vLLM-Omni",
|
|
1055
|
+
region: "local",
|
|
1056
|
+
baseUrl: "http://127.0.0.1:8000/v1",
|
|
1057
|
+
model: "tts-1",
|
|
1058
|
+
voice: "default",
|
|
1059
|
+
voices: ["default"],
|
|
1060
|
+
requiresKey: false
|
|
1061
|
+
}
|
|
1062
|
+
};
|
|
1063
|
+
|
|
1064
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/llm-router.js
|
|
1065
|
+
var p = (id, label, protocol, defaultBaseUrl, defaultModel, requiresKey) => ({ id, label, protocol, defaultBaseUrl, defaultModel, requiresKey });
|
|
1066
|
+
var LLM_PRESETS = {
|
|
1067
|
+
deepseek: p("deepseek", "DeepSeek", "openai-compat", "https://api.deepseek.com/v1", "deepseek-chat", true),
|
|
1068
|
+
"dashscope-qwen": p("dashscope-qwen", "DashScope (Qwen)", "openai-compat", "https://dashscope.aliyuncs.com/compatible-mode/v1", "qwen-plus", true),
|
|
1069
|
+
"volcengine-doubao": p("volcengine-doubao", "Volcengine Ark (Doubao)", "openai-compat", "https://ark.cn-beijing.volces.com/api/v3", "doubao-seed-1-6-250615", true),
|
|
1070
|
+
"moonshot-kimi": p("moonshot-kimi", "Moonshot (Kimi)", "openai-compat", "https://api.moonshot.cn/v1", "moonshot-v1-8k", true),
|
|
1071
|
+
siliconflow: p("siliconflow", "SiliconFlow", "openai-compat", "https://api.siliconflow.cn/v1", "Qwen/Qwen2.5-7B-Instruct", true),
|
|
1072
|
+
openrouter: p("openrouter", "OpenRouter", "openai-compat", "https://openrouter.ai/api/v1", "openai/gpt-4o-mini", true),
|
|
1073
|
+
openai: p("openai", "OpenAI", "openai-compat", "https://api.openai.com/v1", "gpt-4o-mini", true),
|
|
1074
|
+
anthropic: p("anthropic", "Anthropic", "anthropic-messages", "https://api.anthropic.com/v1", "claude-sonnet-4-5", true),
|
|
1075
|
+
ollama: p("ollama", "Ollama (Local)", "openai-compat", "http://127.0.0.1:11434/v1", "qwen2.5:7b", false),
|
|
1076
|
+
lmstudio: p("lmstudio", "LM Studio (Local)", "openai-compat", "http://127.0.0.1:1234/v1", "local-model", false),
|
|
1077
|
+
custom: p("custom", "Custom", "openai-compat", "", "", false)
|
|
1078
|
+
};
|
|
1079
|
+
var MAX_ERROR_BYTES = 8 * 1024;
|
|
1080
|
+
var MAX_FALLBACK_CONTENT_LENGTH = 1024 * 1024;
|
|
1081
|
+
var CORRECTION_PREAMBLE_RE = /^(?:以下是|这是|已|文本)?(?:修正|纠正|更正|纠错)|^(?:here(?:'s| is) (?:the |an |a )?|i have |text after )?(?:corrected|correction)\b/i;
|
|
1082
|
+
var CORRECTION_FILLER_WORDS = "\u5F53\u7136\u53EF\u4EE5|\u5F53\u7136|\u597D\u5427|\u597D\u7684?|\u884C\u5427|\u884C|\u662F\u7684|\u55EF|\u6536\u5230|\u660E\u767D\u4E86|\u77E5\u9053\u4E86|Thank you|Of course|Got it|OK|Okay|Sure|Certainly|Alright|Understood|Hello|Hi|Yes|Thanks";
|
|
1083
|
+
var CORRECTION_FILLER_PREFIX_RE = new RegExp(`^(?:(?:${CORRECTION_FILLER_WORDS})(?:\\s*[,\uFF0C\u3001:\uFF1A.\u3002!\uFF01?\uFF1F)\uFF09]|\\s))+`, "i");
|
|
1084
|
+
var CORRECTION_PURE_FILLER_RE = new RegExp(`^(?:(?:${CORRECTION_FILLER_WORDS})(?:\\s*[,\uFF0C\u3001:\uFF1A.\u3002!\uFF01?\uFF1F)\uFF09]\\s*|\\s+))*(?:${CORRECTION_FILLER_WORDS})\\s*$`, "i");
|
|
1085
|
+
var CORRECTION_MIN_SIMILARITY = 0.45;
|
|
1086
|
+
var SIMILARITY_MAX_CHARS = 1500;
|
|
1087
|
+
function normalizedSimilarity(a, b) {
|
|
1088
|
+
const norm = (s) => Array.from(s.toLowerCase().replace(/[\s\p{P}\p{S}]+/gu, ""));
|
|
1089
|
+
const x = norm(a);
|
|
1090
|
+
const y = norm(b);
|
|
1091
|
+
const n = Math.max(x.length, y.length);
|
|
1092
|
+
if (n === 0 || n > SIMILARITY_MAX_CHARS)
|
|
1093
|
+
return 1;
|
|
1094
|
+
let prev = Array.from({ length: y.length + 1 }, (_, j) => j);
|
|
1095
|
+
for (let i = 1; i <= x.length; i++) {
|
|
1096
|
+
const cur = [i];
|
|
1097
|
+
for (let j = 1; j <= y.length; j++) {
|
|
1098
|
+
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (x[i - 1] === y[j - 1] ? 0 : 1));
|
|
1099
|
+
}
|
|
1100
|
+
prev = cur;
|
|
1101
|
+
}
|
|
1102
|
+
return 1 - prev[y.length] / n;
|
|
1103
|
+
}
|
|
1104
|
+
function correctionSystem(lang) {
|
|
1105
|
+
return lang === "en" ? "You are a speech-recognition corrector. Make only minimal fixes: correct typos/homophones and punctuation, preserving original meaning and style. Do not rewrite, add content, or answer questions. Output only the corrected text." : "\u4F60\u662F\u4E2D\u6587\u8BED\u97F3\u8BC6\u522B\u7EA0\u9519\u5668\u3002\u8BF7\u53EA\u505A\u6700\u5C0F\u4FEE\u6B63\uFF1A\u6539\u9519\u522B\u5B57/\u540C\u97F3\u5B57\u3001\u8865\u6807\u70B9\uFF0C\u4FDD\u6301\u539F\u610F\u4E0E\u53E3\u8BED\u98CE\u683C\uFF1B\u4E0D\u8981\u6539\u5199\u3001\u8865\u5145\u6216\u56DE\u7B54\u95EE\u9898\u3002\u53EA\u8F93\u51FA\u4FEE\u6B63\u540E\u7684\u6587\u672C\uFF0C\u4E0D\u8981\u4EFB\u4F55\u89E3\u91CA\u3002";
|
|
47
1106
|
}
|
|
48
|
-
function
|
|
49
|
-
const
|
|
50
|
-
|
|
1107
|
+
function applyCorrectionGuards(original, candidate) {
|
|
1108
|
+
const trimmed = String(original ?? "").trim();
|
|
1109
|
+
if (!trimmed)
|
|
1110
|
+
return original;
|
|
1111
|
+
const corrected = String(candidate ?? "").trim();
|
|
1112
|
+
if (!corrected)
|
|
1113
|
+
return original;
|
|
1114
|
+
if (corrected.length > Math.max(trimmed.length * 3, trimmed.length + 24))
|
|
1115
|
+
return original;
|
|
1116
|
+
if (normalizedSimilarity(trimmed, corrected) < CORRECTION_MIN_SIMILARITY)
|
|
1117
|
+
return original;
|
|
1118
|
+
if (CORRECTION_PREAMBLE_RE.test(corrected))
|
|
1119
|
+
return original;
|
|
1120
|
+
let noFiller = corrected;
|
|
1121
|
+
for (; ; ) {
|
|
1122
|
+
const next = noFiller.replace(CORRECTION_FILLER_PREFIX_RE, "").trimStart();
|
|
1123
|
+
const after = CORRECTION_PURE_FILLER_RE.test(next) ? "" : next;
|
|
1124
|
+
if (after === noFiller)
|
|
1125
|
+
break;
|
|
1126
|
+
noFiller = after;
|
|
1127
|
+
}
|
|
1128
|
+
if (noFiller !== corrected && (noFiller === "" || CORRECTION_PREAMBLE_RE.test(noFiller)))
|
|
1129
|
+
return original;
|
|
1130
|
+
return corrected;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// node_modules/.pnpm/@voice-mode+core@file+vendor+voice-mode-core-0.1.0.tgz/node_modules/@voice-mode/core/dist/tts-engines.js
|
|
1134
|
+
var MAX_AUDIO_BYTES = 16 * 1024 * 1024;
|
|
1135
|
+
var MAX_MIMO_JSON_BYTES = 24 * 1024 * 1024;
|
|
1136
|
+
var MAX_TTS_ERROR_BYTES = 8 * 1024;
|
|
1137
|
+
var UTF8_DECODER = new TextDecoder("utf-8");
|
|
1138
|
+
function sniffAudioMime(bytes, contentTypeHeader) {
|
|
1139
|
+
if (!(bytes instanceof Uint8Array) || bytes.byteLength === 0)
|
|
1140
|
+
return null;
|
|
1141
|
+
if (bytes.byteLength >= 12 && bytes[0] === 82 && bytes[1] === 73 && bytes[2] === 70 && bytes[3] === 70 && bytes[8] === 87 && bytes[9] === 65 && bytes[10] === 86 && bytes[11] === 69) {
|
|
1142
|
+
return "audio/wav";
|
|
1143
|
+
}
|
|
1144
|
+
if (bytes.byteLength >= 3 && bytes[0] === 73 && bytes[1] === 68 && bytes[2] === 51) {
|
|
1145
|
+
return "audio/mpeg";
|
|
1146
|
+
}
|
|
1147
|
+
if (bytes.byteLength >= 2 && bytes[0] === 255) {
|
|
1148
|
+
const b1 = bytes[1];
|
|
1149
|
+
if ((b1 & 224) === 224 && (b1 & 24) !== 8 && (b1 & 6) !== 0) {
|
|
1150
|
+
return "audio/mpeg";
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
if (bytes.byteLength >= 4 && bytes[0] === 79 && bytes[1] === 103 && bytes[2] === 103 && bytes[3] === 83) {
|
|
1154
|
+
return "audio/ogg";
|
|
1155
|
+
}
|
|
1156
|
+
if (bytes.byteLength >= 4 && bytes[0] === 102 && bytes[1] === 76 && bytes[2] === 97 && bytes[3] === 67) {
|
|
1157
|
+
return "audio/flac";
|
|
1158
|
+
}
|
|
1159
|
+
if (bytes.byteLength >= 4 && bytes[0] === 26 && bytes[1] === 69 && bytes[2] === 223 && bytes[3] === 163) {
|
|
1160
|
+
return "audio/webm";
|
|
1161
|
+
}
|
|
1162
|
+
if (bytes.byteLength >= 8 && bytes[4] === 102 && bytes[5] === 116 && bytes[6] === 121 && bytes[7] === 112) {
|
|
1163
|
+
return "audio/mp4";
|
|
1164
|
+
}
|
|
1165
|
+
if (!contentTypeHeader || typeof contentTypeHeader !== "string")
|
|
1166
|
+
return null;
|
|
1167
|
+
const mediaType = (contentTypeHeader.split(";")[0] ?? "").trim().toLowerCase();
|
|
1168
|
+
if (!/^audio\/[a-z0-9][a-z0-9!#$&^_.+-]*$/.test(mediaType))
|
|
1169
|
+
return null;
|
|
1170
|
+
const prefix = UTF8_DECODER.decode(bytes.subarray(0, 64)).trimStart();
|
|
1171
|
+
if (prefix.startsWith("<") || prefix.startsWith("{") || prefix.startsWith("[")) {
|
|
1172
|
+
return null;
|
|
1173
|
+
}
|
|
1174
|
+
return mediaType;
|
|
51
1175
|
}
|
|
1176
|
+
var B64_TABLE = (() => {
|
|
1177
|
+
const table = new Int8Array(128).fill(-1);
|
|
1178
|
+
const std = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1179
|
+
for (let i = 0; i < std.length; i += 1) {
|
|
1180
|
+
table[std.charCodeAt(i)] = i;
|
|
1181
|
+
}
|
|
1182
|
+
table[45] = 62;
|
|
1183
|
+
table[95] = 63;
|
|
1184
|
+
return table;
|
|
1185
|
+
})();
|
|
52
1186
|
|
|
53
1187
|
// src/asr-host.ts
|
|
54
1188
|
import { join as join2 } from "node:path";
|
|
@@ -73,29 +1207,29 @@ function createSenseWorkerClient(worker) {
|
|
|
73
1207
|
}
|
|
74
1208
|
};
|
|
75
1209
|
worker.on?.("message", (msg) => {
|
|
76
|
-
const
|
|
77
|
-
if (!
|
|
1210
|
+
const p2 = pending.get(msg?.id);
|
|
1211
|
+
if (!p2) return;
|
|
78
1212
|
pending.delete(msg.id);
|
|
79
1213
|
if (!msg.ok) {
|
|
80
|
-
if (
|
|
81
|
-
|
|
1214
|
+
if (p2.op === "create") {
|
|
1215
|
+
p2.reject(new Error(msg.error ?? "sense worker create failed"));
|
|
82
1216
|
} else {
|
|
83
|
-
|
|
1217
|
+
p2.resolve(null);
|
|
84
1218
|
}
|
|
85
1219
|
return;
|
|
86
1220
|
}
|
|
87
|
-
|
|
1221
|
+
p2.resolve(p2.op === "create" ? true : msg.text ?? "");
|
|
88
1222
|
});
|
|
89
1223
|
worker.on?.("error", (e) => {
|
|
90
1224
|
die();
|
|
91
1225
|
const err = new Error("sense worker error: " + String(e?.message ?? e));
|
|
92
|
-
for (const [,
|
|
1226
|
+
for (const [, p2] of pending) p2.reject(err);
|
|
93
1227
|
pending.clear();
|
|
94
1228
|
});
|
|
95
1229
|
worker.on?.("exit", () => {
|
|
96
1230
|
die();
|
|
97
1231
|
const err = new Error("sense worker exited");
|
|
98
|
-
for (const [,
|
|
1232
|
+
for (const [, p2] of pending) p2.reject(err);
|
|
99
1233
|
pending.clear();
|
|
100
1234
|
});
|
|
101
1235
|
const request = (op, samples) => {
|
|
@@ -121,7 +1255,7 @@ function createSenseWorkerClient(worker) {
|
|
|
121
1255
|
terminate: async () => {
|
|
122
1256
|
dead = true;
|
|
123
1257
|
const err = new Error("sense worker terminated");
|
|
124
|
-
for (const [,
|
|
1258
|
+
for (const [, p2] of pending) p2.reject(err);
|
|
125
1259
|
pending.clear();
|
|
126
1260
|
try {
|
|
127
1261
|
await worker.terminate?.();
|
|
@@ -1332,338 +2466,64 @@ function handleAsrRequest(asr, activeSessionId, req, res) {
|
|
|
1332
2466
|
respondJson(res, 400, { code: "bad_request", error: "invalid offset" });
|
|
1333
2467
|
return;
|
|
1334
2468
|
}
|
|
1335
|
-
if (!epochOK) {
|
|
1336
|
-
respondJson(res, 400, { code: "bad_request", error: "invalid epoch" });
|
|
1337
|
-
return;
|
|
1338
|
-
}
|
|
1339
|
-
const epoch = epochParam === null ? 0 : Math.floor(epochN);
|
|
1340
|
-
const offset = offsetParam === null ? 0 : Math.floor(Number(offsetParam));
|
|
1341
|
-
if (!sessionId || sessionId !== activeSessionId) {
|
|
1342
|
-
respondJson(res, 403, { code: "unknown_session", error: "not the active voice session" });
|
|
1343
|
-
return;
|
|
1344
|
-
}
|
|
1345
|
-
if (reset) {
|
|
1346
|
-
asr.reset(sessionId);
|
|
1347
|
-
respondJson(res, 200, { ok: true });
|
|
1348
|
-
return;
|
|
1349
|
-
}
|
|
1350
|
-
const raw = Buffer.concat(chunks);
|
|
1351
|
-
const samples = raw.length === 0 ? final ? new Float32Array(0) : null : pcmToSamples(raw);
|
|
1352
|
-
if (!samples) {
|
|
1353
|
-
respondJson(res, 400, { code: "bad_request", error: "invalid pcm payload" });
|
|
1354
|
-
return;
|
|
1355
|
-
}
|
|
1356
|
-
if (url.searchParams.get("vadOnly") === "1") {
|
|
1357
|
-
void asr.detect(sessionId, samples).then((out) => {
|
|
1358
|
-
respondJson(res, 200, { isSpeech: out.isSpeech });
|
|
1359
|
-
}).catch((e) => {
|
|
1360
|
-
console.warn(`[dsh-voice-mode] asr detect failed: ${redactSecrets(e)}`);
|
|
1361
|
-
respondJson(res, 500, { code: "internal", error: "internal error" });
|
|
1362
|
-
});
|
|
1363
|
-
return;
|
|
1364
|
-
}
|
|
1365
|
-
void asr.feed(sessionId, samples, final, offset, epoch, url.searchParams.get("manual") === "1").then((out) => {
|
|
1366
|
-
if (out.loading) {
|
|
1367
|
-
respondJson(res, 202, { loading: true });
|
|
1368
|
-
return;
|
|
1369
|
-
}
|
|
1370
|
-
const body = { text: out.text };
|
|
1371
|
-
if (out.endpoint) body.endpoint = true;
|
|
1372
|
-
if (out.isSpeech !== void 0) body.isSpeech = out.isSpeech;
|
|
1373
|
-
respondJson(res, 200, body);
|
|
1374
|
-
}).catch((e) => {
|
|
1375
|
-
console.warn(`[dsh-voice-mode] asr decode failed: ${redactSecrets(e)}`);
|
|
1376
|
-
respondJson(res, 500, { code: "internal", error: "internal error" });
|
|
1377
|
-
});
|
|
1378
|
-
});
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
// src/asr-replacements.ts
|
|
1382
|
-
import z from "@deepseek-ai/schemastery";
|
|
1383
|
-
var AsrReplacementSchema = z.object({
|
|
1384
|
-
from: z.array(z.string()).description('Spellings the recognizer produces, e.g. ["sakeena"]; case-insensitive, whitespace between Latin and CJK text ignored.'),
|
|
1385
|
-
to: z.string().description('The spelling you want, e.g. "Sakina".')
|
|
1386
|
-
});
|
|
1387
|
-
var ASCII_WORD = /[A-Za-z0-9]/;
|
|
1388
|
-
var escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1389
|
-
function patternOf(heard) {
|
|
1390
|
-
const chars = [...heard.replace(/\s+/g, " ").trim()];
|
|
1391
|
-
if (chars.length === 0) return null;
|
|
1392
|
-
let body = "";
|
|
1393
|
-
let prev = "";
|
|
1394
|
-
let spaced = false;
|
|
1395
|
-
for (const ch of chars) {
|
|
1396
|
-
if (ch === " ") {
|
|
1397
|
-
spaced = true;
|
|
1398
|
-
continue;
|
|
1399
|
-
}
|
|
1400
|
-
if (prev && (spaced || ASCII_WORD.test(prev) !== ASCII_WORD.test(ch))) body += "\\s*";
|
|
1401
|
-
body += escapeRegExp(ch);
|
|
1402
|
-
prev = ch;
|
|
1403
|
-
spaced = false;
|
|
1404
|
-
}
|
|
1405
|
-
const first = [...heard.trim()][0];
|
|
1406
|
-
const trimmed = [...heard.trim()];
|
|
1407
|
-
const last = trimmed[trimmed.length - 1];
|
|
1408
|
-
const head = ASCII_WORD.test(first) ? "(?<![A-Za-z0-9])" : "";
|
|
1409
|
-
const tail = ASCII_WORD.test(last) ? "(?![A-Za-z0-9])" : "";
|
|
1410
|
-
return `${head}${body}${tail}`;
|
|
1411
|
-
}
|
|
1412
|
-
function createAsrReplacer(rules, onHit) {
|
|
1413
|
-
const entries = [];
|
|
1414
|
-
rules.forEach((rule, ruleIndex) => {
|
|
1415
|
-
if (!rule.to) return;
|
|
1416
|
-
for (const heard of rule.from) {
|
|
1417
|
-
const pattern = patternOf(heard);
|
|
1418
|
-
if (!pattern) continue;
|
|
1419
|
-
entries.push({
|
|
1420
|
-
pattern,
|
|
1421
|
-
key: heard.replace(/\s+/g, "").toLowerCase(),
|
|
1422
|
-
to: rule.to,
|
|
1423
|
-
length: heard.replace(/\s+/g, "").length,
|
|
1424
|
-
ruleIndex
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
});
|
|
1428
|
-
if (entries.length === 0) return (text) => text;
|
|
1429
|
-
entries.sort((a, b) => b.length - a.length);
|
|
1430
|
-
const targets = /* @__PURE__ */ new Map();
|
|
1431
|
-
for (const e of entries) if (!targets.has(e.key)) targets.set(e.key, { to: e.to, ruleIndex: e.ruleIndex });
|
|
1432
|
-
const regex = new RegExp(entries.map((e) => `(?:${e.pattern})`).join("|"), "giu");
|
|
1433
|
-
return (text, countHit = true) => {
|
|
1434
|
-
if (!text) return text;
|
|
1435
|
-
return text.replace(regex, (match) => {
|
|
1436
|
-
const hit = targets.get(match.replace(/\s+/g, "").toLowerCase());
|
|
1437
|
-
if (!hit) return match;
|
|
1438
|
-
if (onHit && countHit) onHit(hit.ruleIndex);
|
|
1439
|
-
return hit.to;
|
|
1440
|
-
});
|
|
1441
|
-
};
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
// src/segmenter.ts
|
|
1445
|
-
var SKIP_PREFIX = /^[\s.,,、::;;!?!?)\])"'”’〉》】]+$/;
|
|
1446
|
-
function plainText(text) {
|
|
1447
|
-
return String(text).replace(/```[\s\S]*?```/g, " ").replace(/`([^`]*)`/g, "$1").replace(/!\[[^\]]*\]\([^)]*\)/g, " ").replace(/\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/^#{1,6}\s+/gm, "").replace(/\*\*([^*]+)\*\*/g, "$1").replace(/\*([^*]+)\*/g, "$1").replace(/^[-*+]\s+/gm, "").replace(/^\d+\.\s+/gm, "").replace(/<\/?(?:b|i|u|br|p|span|div|strong|em|s|sub|sup|h[1-6]|ul|ol|li|a|img|code|pre|blockquote|hr|table|tr|td|th)\b[^>]*>/gi, " ");
|
|
1448
|
-
}
|
|
1449
|
-
function sanitizeForTts(text) {
|
|
1450
|
-
return String(text).replace(/[*_#|^=+~`]/g, " ").replace(/\s{2,}/g, " ").replace(/([\u3400-\u9fff])\s+(?=[\u3400-\u9fff])/g, "$1").trim();
|
|
1451
|
-
}
|
|
1452
|
-
function splitSentences(chunk) {
|
|
1453
|
-
const sentences = [];
|
|
1454
|
-
let start = 0;
|
|
1455
|
-
const re = /[。!?!?;;…\n]+|\.(?=\s|$)/g;
|
|
1456
|
-
let m;
|
|
1457
|
-
let lastEnd = 0;
|
|
1458
|
-
while ((m = re.exec(chunk)) !== null) {
|
|
1459
|
-
const end = m.index + m[0].length;
|
|
1460
|
-
if (m[0] === "." && end === chunk.length && /\d/.test(chunk[m.index - 1] ?? "")) continue;
|
|
1461
|
-
sentences.push(chunk.slice(start, end));
|
|
1462
|
-
start = end;
|
|
1463
|
-
lastEnd = end;
|
|
1464
|
-
}
|
|
1465
|
-
return { sentences, tail: chunk.slice(lastEnd) };
|
|
1466
|
-
}
|
|
1467
|
-
function cutHead(text, max) {
|
|
1468
|
-
let idx = -1;
|
|
1469
|
-
for (let i = Math.min(max, text.length) - 1; i > 0; i--) {
|
|
1470
|
-
if (/[,,、\s]/.test(text[i])) {
|
|
1471
|
-
idx = i + 1;
|
|
1472
|
-
break;
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
if (idx <= 0) idx = Math.max(1, Math.floor(max / 2));
|
|
1476
|
-
return [text.slice(0, idx), text.slice(idx)];
|
|
1477
|
-
}
|
|
1478
|
-
var SentenceSegmenter = class {
|
|
1479
|
-
buffer = "";
|
|
1480
|
-
maxChars;
|
|
1481
|
-
/** 是否处在 ``` 代码块内(跨 delta 有效:流式 delta 只有几个 token,代码块总是被切碎送达,
|
|
1482
|
-
* plainText 的整块正则匹配不到,代码会被逐句念出——「总是朗读」覆盖打字会话后尤其明显)。 */
|
|
1483
|
-
inFence = false;
|
|
1484
|
-
/** delta 末尾悬挂的 1-2 个反引号(被切开的 ``` 前半;连续 3 个及以上是完整围栏,不悬挂)。 */
|
|
1485
|
-
ticks = "";
|
|
1486
|
-
/** 围栏内被抑制的内容暂存:流结束时若仍未闭合则回退输出(宁可念代码,也不吞掉整回合正文)。 */
|
|
1487
|
-
fenced = "";
|
|
1488
|
-
constructor(options = {}) {
|
|
1489
|
-
this.maxChars = options.maxSentenceChars ?? 200;
|
|
1490
|
-
}
|
|
1491
|
-
/** 去掉 ``` 代码块内容(状态跨 delta 保持);进入代码块处补一个换行,让前面的文字先成句。 */
|
|
1492
|
-
dropFences(chunk) {
|
|
1493
|
-
let s = this.ticks + chunk;
|
|
1494
|
-
this.ticks = "";
|
|
1495
|
-
const trail = /`+$/.exec(s);
|
|
1496
|
-
const hang = trail ? trail[0].length : 0;
|
|
1497
|
-
if (hang > 0 && hang <= 2) {
|
|
1498
|
-
this.ticks = s.slice(-hang);
|
|
1499
|
-
s = s.slice(0, -hang);
|
|
1500
|
-
}
|
|
1501
|
-
let out = "";
|
|
1502
|
-
for (; ; ) {
|
|
1503
|
-
const m = /`{3,}/.exec(s);
|
|
1504
|
-
if (!m) {
|
|
1505
|
-
if (this.inFence) this.fenced += s;
|
|
1506
|
-
else out += s;
|
|
1507
|
-
return out;
|
|
1508
|
-
}
|
|
1509
|
-
if (this.inFence) this.fenced += s.slice(0, m.index);
|
|
1510
|
-
else out += s.slice(0, m.index) + "\n";
|
|
1511
|
-
this.inFence = !this.inFence;
|
|
1512
|
-
if (!this.inFence) this.fenced = "";
|
|
1513
|
-
s = s.slice(m.index + m[0].length);
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1516
|
-
/**
|
|
1517
|
-
* 安全阀:把超过 maxChars 的文字墙切下一段(feed 与 flush 共用同一口径)。
|
|
1518
|
-
* 切点取 ≤ maxChars 内**最后一个**「,,、\s」——取第一个会把无标点的英文/代码逐词切成
|
|
1519
|
-
* 几十个小段;超长段必须 ≤ maxChars,否则阀形同虚设。没有可用分隔符时取一半。
|
|
1520
|
-
*/
|
|
1521
|
-
cutWall() {
|
|
1522
|
-
const [head, rest] = cutHead(this.buffer, this.maxChars);
|
|
1523
|
-
this.buffer = rest;
|
|
1524
|
-
return sanitizeForTts(head).trim();
|
|
1525
|
-
}
|
|
1526
|
-
/** 成句本身超过 maxChars(长逗号句)同样过安全阀,逐段输出。 */
|
|
1527
|
-
capSentence(s, out) {
|
|
1528
|
-
let rest = s;
|
|
1529
|
-
while (rest.length > this.maxChars) {
|
|
1530
|
-
const [head, tail] = cutHead(rest, this.maxChars);
|
|
1531
|
-
rest = tail;
|
|
1532
|
-
const t2 = sanitizeForTts(head).trim();
|
|
1533
|
-
if (t2 && !SKIP_PREFIX.test(t2)) out.push(t2);
|
|
1534
|
-
}
|
|
1535
|
-
const t = sanitizeForTts(rest).trim();
|
|
1536
|
-
if (t && !SKIP_PREFIX.test(t)) out.push(t);
|
|
1537
|
-
}
|
|
1538
|
-
/** 喂入一段 raw delta,返回它补全的完整句子。 */
|
|
1539
|
-
feed(chunk) {
|
|
1540
|
-
const cleaned = plainText(this.dropFences(chunk));
|
|
1541
|
-
if (!cleaned) return [];
|
|
1542
|
-
this.buffer += cleaned;
|
|
1543
|
-
const { sentences, tail } = splitSentences(this.buffer);
|
|
1544
|
-
this.buffer = tail;
|
|
1545
|
-
const out = [];
|
|
1546
|
-
for (const s of sentences) this.capSentence(s, out);
|
|
1547
|
-
while (this.buffer.length > this.maxChars) {
|
|
1548
|
-
const head = this.cutWall();
|
|
1549
|
-
if (head && !SKIP_PREFIX.test(head)) out.push(head);
|
|
1550
|
-
}
|
|
1551
|
-
return out;
|
|
1552
|
-
}
|
|
1553
|
-
/**
|
|
1554
|
-
* 收尾:flush 剩余缓冲(流结束)。
|
|
1555
|
-
* 整回合未见闭合围栏 → 把围栏内被抑制的内容回退输出(v0.7.27 之前未配对围栏零损失,
|
|
1556
|
-
* 全靠这一条守住;否则一个孤立的 ``` 会静默吞掉本回合剩余正文)。
|
|
1557
|
-
*
|
|
1558
|
-
* 过安全阀后的多段仍按「代码内容」朗读:既有取舍是「宁念代码不吞正文」,切分只改朗读粒度
|
|
1559
|
-
* (每段一次合成请求)、不改取舍;段序 = 原文字序,段内容 = 原文字符的连续切片(切点沿用
|
|
1560
|
-
* feed() 同一口径)。不回退成「整体不念」——那等于回到吞正文的老问题。
|
|
1561
|
-
*/
|
|
1562
|
-
flush() {
|
|
1563
|
-
if (this.inFence) this.buffer = plainText(this.buffer + this.fenced);
|
|
1564
|
-
const out = [];
|
|
1565
|
-
while (this.buffer.length > this.maxChars) {
|
|
1566
|
-
const head = this.cutWall();
|
|
1567
|
-
if (head && !SKIP_PREFIX.test(head)) out.push(head);
|
|
1568
|
-
}
|
|
1569
|
-
const t = sanitizeForTts(this.buffer).trim();
|
|
1570
|
-
this.buffer = "";
|
|
1571
|
-
this.inFence = false;
|
|
1572
|
-
this.ticks = "";
|
|
1573
|
-
this.fenced = "";
|
|
1574
|
-
if (t && !SKIP_PREFIX.test(t)) out.push(t);
|
|
1575
|
-
return out;
|
|
1576
|
-
}
|
|
1577
|
-
};
|
|
1578
|
-
|
|
1579
|
-
// src/tts-queue.ts
|
|
1580
|
-
import { MsEdgeTTS, OUTPUT_FORMAT } from "./msedge-tts.cjs";
|
|
1581
|
-
|
|
1582
|
-
// src/emotion.ts
|
|
1583
|
-
var EMOTION_TAG_PATTERN = "<\\s*(break\\s+(?<ms>\\d+)\\s*ms|whisper|\\/whisper|laugh|sigh|emphasis)\\s*>";
|
|
1584
|
-
var TAG_RE = new RegExp(EMOTION_TAG_PATTERN, "gi");
|
|
1585
|
-
var STRIP_RE = new RegExp(EMOTION_TAG_PATTERN, "gi");
|
|
1586
|
-
function stripEmotionTags(raw) {
|
|
1587
|
-
return raw.replace(STRIP_RE, " ");
|
|
1588
|
-
}
|
|
1589
|
-
var MAX_BREAK_MS = 5e3;
|
|
1590
|
-
var MAX_TOTAL_BREAK_MS = 1e4;
|
|
1591
|
-
function parseEmotionTags(raw) {
|
|
1592
|
-
const out = [];
|
|
1593
|
-
let buf = "";
|
|
1594
|
-
let whisper = false;
|
|
1595
|
-
let pendingBreakMs = 0;
|
|
1596
|
-
let totalBreakMs = 0;
|
|
1597
|
-
let sawInlineTag = false;
|
|
1598
|
-
const appendText = (chunk) => {
|
|
1599
|
-
if (chunk.length === 0) return;
|
|
1600
|
-
if (sawInlineTag && /[A-Za-z0-9]$/.test(buf) && /^[A-Za-z0-9]/.test(chunk)) {
|
|
1601
|
-
buf += " ";
|
|
1602
|
-
}
|
|
1603
|
-
sawInlineTag = false;
|
|
1604
|
-
buf += chunk;
|
|
1605
|
-
};
|
|
1606
|
-
const flush = () => {
|
|
1607
|
-
const t = buf.trim();
|
|
1608
|
-
buf = "";
|
|
1609
|
-
sawInlineTag = false;
|
|
1610
|
-
if (t.length === 0) return;
|
|
1611
|
-
const seg = { text: t, whisper };
|
|
1612
|
-
out.push(seg);
|
|
1613
|
-
};
|
|
1614
|
-
let lastEnd = 0;
|
|
1615
|
-
for (const m of raw.matchAll(TAG_RE)) {
|
|
1616
|
-
const idx = m.index ?? 0;
|
|
1617
|
-
appendText(raw.slice(lastEnd, idx));
|
|
1618
|
-
lastEnd = idx + m[0].length;
|
|
1619
|
-
const tag = (m[1] ?? "").toLowerCase().trim();
|
|
1620
|
-
if (tag.startsWith("break")) {
|
|
1621
|
-
const rawMs = Number(m.groups?.ms ?? 0);
|
|
1622
|
-
if (!Number.isFinite(rawMs) || rawMs <= 0) continue;
|
|
1623
|
-
const remaining = Math.max(0, MAX_TOTAL_BREAK_MS - totalBreakMs);
|
|
1624
|
-
const ms = Math.min(rawMs, MAX_BREAK_MS, remaining);
|
|
1625
|
-
if (ms <= 0) {
|
|
1626
|
-
if (buf.trim().length > 0) flush();
|
|
1627
|
-
continue;
|
|
1628
|
-
}
|
|
1629
|
-
totalBreakMs += ms;
|
|
1630
|
-
if (buf.trim().length > 0) {
|
|
1631
|
-
flush();
|
|
1632
|
-
}
|
|
1633
|
-
const last = out[out.length - 1];
|
|
1634
|
-
if (last) {
|
|
1635
|
-
;
|
|
1636
|
-
last.preBreakMs = (last.preBreakMs ?? 0) + ms;
|
|
1637
|
-
} else {
|
|
1638
|
-
pendingBreakMs += ms;
|
|
1639
|
-
}
|
|
1640
|
-
} else if (tag === "whisper") {
|
|
1641
|
-
flush();
|
|
1642
|
-
whisper = true;
|
|
1643
|
-
} else if (tag === "/whisper") {
|
|
1644
|
-
flush();
|
|
1645
|
-
whisper = false;
|
|
1646
|
-
} else {
|
|
1647
|
-
sawInlineTag = true;
|
|
2469
|
+
if (!epochOK) {
|
|
2470
|
+
respondJson(res, 400, { code: "bad_request", error: "invalid epoch" });
|
|
2471
|
+
return;
|
|
1648
2472
|
}
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
if (last) {
|
|
1655
|
-
;
|
|
1656
|
-
last.preBreakMs = (last.preBreakMs ?? 0) + pendingBreakMs;
|
|
2473
|
+
const epoch = epochParam === null ? 0 : Math.floor(epochN);
|
|
2474
|
+
const offset = offsetParam === null ? 0 : Math.floor(Number(offsetParam));
|
|
2475
|
+
if (!sessionId || sessionId !== activeSessionId) {
|
|
2476
|
+
respondJson(res, 403, { code: "unknown_session", error: "not the active voice session" });
|
|
2477
|
+
return;
|
|
1657
2478
|
}
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
2479
|
+
if (reset) {
|
|
2480
|
+
asr.reset(sessionId);
|
|
2481
|
+
respondJson(res, 200, { ok: true });
|
|
2482
|
+
return;
|
|
2483
|
+
}
|
|
2484
|
+
const raw = Buffer.concat(chunks);
|
|
2485
|
+
const samples = raw.length === 0 ? final ? new Float32Array(0) : null : pcmToSamples(raw);
|
|
2486
|
+
if (!samples) {
|
|
2487
|
+
respondJson(res, 400, { code: "bad_request", error: "invalid pcm payload" });
|
|
2488
|
+
return;
|
|
2489
|
+
}
|
|
2490
|
+
if (url.searchParams.get("vadOnly") === "1") {
|
|
2491
|
+
void asr.detect(sessionId, samples).then((out) => {
|
|
2492
|
+
respondJson(res, 200, { isSpeech: out.isSpeech });
|
|
2493
|
+
}).catch((e) => {
|
|
2494
|
+
console.warn(`[dsh-voice-mode] asr detect failed: ${redactSecrets(e)}`);
|
|
2495
|
+
respondJson(res, 500, { code: "internal", error: "internal error" });
|
|
2496
|
+
});
|
|
2497
|
+
return;
|
|
2498
|
+
}
|
|
2499
|
+
void asr.feed(sessionId, samples, final, offset, epoch, url.searchParams.get("manual") === "1").then((out) => {
|
|
2500
|
+
if (out.loading) {
|
|
2501
|
+
respondJson(res, 202, { loading: true });
|
|
2502
|
+
return;
|
|
2503
|
+
}
|
|
2504
|
+
const body = { text: out.text };
|
|
2505
|
+
if (out.endpoint) body.endpoint = true;
|
|
2506
|
+
if (out.isSpeech !== void 0) body.isSpeech = out.isSpeech;
|
|
2507
|
+
respondJson(res, 200, body);
|
|
2508
|
+
}).catch((e) => {
|
|
2509
|
+
console.warn(`[dsh-voice-mode] asr decode failed: ${redactSecrets(e)}`);
|
|
2510
|
+
respondJson(res, 500, { code: "internal", error: "internal error" });
|
|
2511
|
+
});
|
|
2512
|
+
});
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
// src/asr-replacements.ts
|
|
2516
|
+
import z from "@deepseek-ai/schemastery";
|
|
2517
|
+
var AsrReplacementSchema = z.object({
|
|
2518
|
+
from: z.array(z.string()).description('Spellings the recognizer produces, e.g. ["sakeena"]; case-insensitive, whitespace between Latin and CJK text ignored.'),
|
|
2519
|
+
to: z.string().description('The spelling you want, e.g. "Sakina".')
|
|
2520
|
+
});
|
|
2521
|
+
function createAsrReplacer2(rules, onHit) {
|
|
2522
|
+
return createAsrReplacer(rules, onHit);
|
|
1664
2523
|
}
|
|
1665
2524
|
|
|
1666
2525
|
// src/tts-queue.ts
|
|
2526
|
+
import { MsEdgeTTS, OUTPUT_FORMAT } from "./msedge-tts.cjs";
|
|
1667
2527
|
var MP3_MAGIC = 255;
|
|
1668
2528
|
var TTS_METADATA = { wordBoundaryEnabled: false, sentenceBoundaryEnabled: false };
|
|
1669
2529
|
var EDGE_VOICES_TIMEOUT_MS = 3e4;
|
|
@@ -2269,135 +3129,6 @@ function resolveNativeRuntime(opts = {}) {
|
|
|
2269
3129
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
2270
3130
|
import { join as join3 } from "node:path";
|
|
2271
3131
|
import { statSync as statSync2 } from "node:fs";
|
|
2272
|
-
|
|
2273
|
-
// src/voice-catalog.ts
|
|
2274
|
-
var VITS_SPEAKERS = [
|
|
2275
|
-
{ name: "suyingxue", sid: 0, zh: "\u7D20\u6620\u96EA", en: "Su Yingxue", gender: "F" },
|
|
2276
|
-
{ name: "gunian", sid: 1, zh: "\u987E\u5FF5", en: "Gu Nian", gender: "M" },
|
|
2277
|
-
{ name: "fushiyu", sid: 2, zh: "\u5085\u65AF\u9047", en: "Fu Siyu", gender: "F" },
|
|
2278
|
-
{ name: "bingjiao", sid: 3, zh: "\u51B0\u5A07", en: "Bing Jiao", gender: "M" },
|
|
2279
|
-
{ name: "bazong", sid: 4, zh: "\u9738\u603B", en: "Ba Zong", gender: "M" }
|
|
2280
|
-
];
|
|
2281
|
-
var KOKORO_F0 = [
|
|
2282
|
-
224,
|
|
2283
|
-
189,
|
|
2284
|
-
154,
|
|
2285
|
-
261,
|
|
2286
|
-
226,
|
|
2287
|
-
222,
|
|
2288
|
-
220,
|
|
2289
|
-
229,
|
|
2290
|
-
198,
|
|
2291
|
-
186,
|
|
2292
|
-
212,
|
|
2293
|
-
293,
|
|
2294
|
-
233,
|
|
2295
|
-
161,
|
|
2296
|
-
247,
|
|
2297
|
-
207,
|
|
2298
|
-
218,
|
|
2299
|
-
216,
|
|
2300
|
-
220,
|
|
2301
|
-
238,
|
|
2302
|
-
242,
|
|
2303
|
-
229,
|
|
2304
|
-
198,
|
|
2305
|
-
286,
|
|
2306
|
-
211,
|
|
2307
|
-
190,
|
|
2308
|
-
264,
|
|
2309
|
-
261,
|
|
2310
|
-
226,
|
|
2311
|
-
147,
|
|
2312
|
-
216,
|
|
2313
|
-
240,
|
|
2314
|
-
233,
|
|
2315
|
-
188,
|
|
2316
|
-
222,
|
|
2317
|
-
247,
|
|
2318
|
-
253,
|
|
2319
|
-
270,
|
|
2320
|
-
276,
|
|
2321
|
-
276,
|
|
2322
|
-
279,
|
|
2323
|
-
320,
|
|
2324
|
-
247,
|
|
2325
|
-
296,
|
|
2326
|
-
276,
|
|
2327
|
-
235,
|
|
2328
|
-
139,
|
|
2329
|
-
240,
|
|
2330
|
-
282,
|
|
2331
|
-
282,
|
|
2332
|
-
238,
|
|
2333
|
-
226,
|
|
2334
|
-
273,
|
|
2335
|
-
216,
|
|
2336
|
-
286,
|
|
2337
|
-
270,
|
|
2338
|
-
198,
|
|
2339
|
-
179,
|
|
2340
|
-
117,
|
|
2341
|
-
130,
|
|
2342
|
-
114,
|
|
2343
|
-
128,
|
|
2344
|
-
108,
|
|
2345
|
-
106,
|
|
2346
|
-
122,
|
|
2347
|
-
136,
|
|
2348
|
-
190,
|
|
2349
|
-
112,
|
|
2350
|
-
108,
|
|
2351
|
-
128,
|
|
2352
|
-
131,
|
|
2353
|
-
111,
|
|
2354
|
-
110,
|
|
2355
|
-
132,
|
|
2356
|
-
138,
|
|
2357
|
-
189,
|
|
2358
|
-
137,
|
|
2359
|
-
148,
|
|
2360
|
-
151,
|
|
2361
|
-
127,
|
|
2362
|
-
135,
|
|
2363
|
-
111,
|
|
2364
|
-
138,
|
|
2365
|
-
114,
|
|
2366
|
-
125,
|
|
2367
|
-
158,
|
|
2368
|
-
128,
|
|
2369
|
-
156,
|
|
2370
|
-
132,
|
|
2371
|
-
162,
|
|
2372
|
-
131,
|
|
2373
|
-
136,
|
|
2374
|
-
142,
|
|
2375
|
-
124,
|
|
2376
|
-
129,
|
|
2377
|
-
136,
|
|
2378
|
-
126,
|
|
2379
|
-
135,
|
|
2380
|
-
161,
|
|
2381
|
-
150,
|
|
2382
|
-
124,
|
|
2383
|
-
104,
|
|
2384
|
-
124
|
|
2385
|
-
];
|
|
2386
|
-
var KOKORO_NAMED = {
|
|
2387
|
-
48: { name: "zf_xiaobei", zh: "\u5C0F\u5317", en: "Xiaobei" },
|
|
2388
|
-
49: { name: "zf_xiaoni", zh: "\u5C0F\u59AE", en: "Xiaoni" },
|
|
2389
|
-
50: { name: "zf_xiaoxiao", zh: "\u5C0F\u5C0F", en: "Xiaoxiao" },
|
|
2390
|
-
51: { name: "zf_xiaoyi", zh: "\u5C0F\u827A", en: "Xiaoyi" }
|
|
2391
|
-
};
|
|
2392
|
-
var KOKORO_PINNED = [62, 68, 75, 76];
|
|
2393
|
-
var KOKORO_VOICES = [
|
|
2394
|
-
...KOKORO_PINNED.map((sid) => ({ name: KOKORO_NAMED[sid]?.name ?? String(sid), sid })),
|
|
2395
|
-
...KOKORO_F0.map((_, sid) => ({ name: KOKORO_NAMED[sid]?.name ?? String(sid), sid })).filter(
|
|
2396
|
-
(v) => !KOKORO_PINNED.includes(v.sid)
|
|
2397
|
-
)
|
|
2398
|
-
];
|
|
2399
|
-
|
|
2400
|
-
// src/tts-local.ts
|
|
2401
3132
|
var TTS_MODEL_REPO = "csukuangfj/sherpa-onnx-vits-zh-ll";
|
|
2402
3133
|
var KOKORO_MODEL_DIR_INT8 = "csukuangfj/kokoro-int8-multi-lang-v1_1";
|
|
2403
3134
|
var KOKORO_MODEL_DIR_FP32 = "csukuangfj/kokoro-multi-lang-v1_1";
|
|
@@ -2557,10 +3288,10 @@ function createSherpaLocalEngine(options) {
|
|
|
2557
3288
|
child.send({ id, ...msg });
|
|
2558
3289
|
});
|
|
2559
3290
|
const rejectAll = (e, owner) => {
|
|
2560
|
-
for (const [id,
|
|
2561
|
-
if (owner &&
|
|
3291
|
+
for (const [id, p2] of pending) {
|
|
3292
|
+
if (owner && p2.owner !== owner) continue;
|
|
2562
3293
|
pending.delete(id);
|
|
2563
|
-
|
|
3294
|
+
p2.reject(e);
|
|
2564
3295
|
}
|
|
2565
3296
|
};
|
|
2566
3297
|
let initFailUntil = 0;
|
|
@@ -2634,11 +3365,11 @@ function createSherpaLocalEngine(options) {
|
|
|
2634
3365
|
}
|
|
2635
3366
|
});
|
|
2636
3367
|
child.on("message", (m) => {
|
|
2637
|
-
const
|
|
2638
|
-
if (!
|
|
3368
|
+
const p2 = pending.get(m.id);
|
|
3369
|
+
if (!p2) return;
|
|
2639
3370
|
pending.delete(m.id);
|
|
2640
|
-
if (m.ok)
|
|
2641
|
-
else
|
|
3371
|
+
if (m.ok) p2.resolve(m);
|
|
3372
|
+
else p2.reject(new Error(m.error ?? "tts child error"));
|
|
2642
3373
|
});
|
|
2643
3374
|
child.on("error", (e) => {
|
|
2644
3375
|
rejectAll(e instanceof Error ? e : new Error(String(e)), me);
|
|
@@ -2698,11 +3429,11 @@ function createSherpaLocalEngine(options) {
|
|
|
2698
3429
|
},
|
|
2699
3430
|
status() {
|
|
2700
3431
|
const files = spec.files.map((f) => {
|
|
2701
|
-
const
|
|
3432
|
+
const p2 = join3(repoDir, f.file);
|
|
2702
3433
|
let exists = false;
|
|
2703
3434
|
let size = 0;
|
|
2704
3435
|
try {
|
|
2705
|
-
const st = statSync2(
|
|
3436
|
+
const st = statSync2(p2);
|
|
2706
3437
|
exists = st.isFile();
|
|
2707
3438
|
size = st.size;
|
|
2708
3439
|
} catch {
|
|
@@ -3299,38 +4030,6 @@ var en = {
|
|
|
3299
4030
|
};
|
|
3300
4031
|
|
|
3301
4032
|
// src/llm-correct.ts
|
|
3302
|
-
function correctionSystem(lang) {
|
|
3303
|
-
return lang === "en" ? "You are a speech-recognition corrector. The user text comes from ASR and may contain homophone errors, typos, or missing/wrong punctuation. Make only minimal fixes: correct typos/homophones and fix punctuation, preserving the original meaning and colloquial style. Do not rewrite sentences, do not add content, do not answer questions, and do not alter numbers or proper nouns. Output only the corrected text, with no explanation. If the input has no obvious errors, return it unchanged." : "\u4F60\u662F\u4E2D\u6587\u8BED\u97F3\u8BC6\u522B\u7EA0\u9519\u5668\u3002\u7528\u6237\u7684\u8BDD\u6765\u81EA\u8BED\u97F3\u8BC6\u522B\uFF08ASR\uFF09\uFF0C\u53EF\u80FD\u6709\u540C\u97F3\u5B57\u3001\u9519\u522B\u5B57\u3001\u6807\u70B9\u7F3A\u5931\u6216\u9519\u8BEF\u3002\u8BF7\u53EA\u505A\u6700\u5C0F\u4FEE\u6B63\uFF1A\u6539\u9519\u522B\u5B57/\u540C\u97F3\u5B57\u3001\u8865\u6807\u70B9\uFF0C\u4FDD\u6301\u539F\u610F\u4E0E\u53E3\u8BED\u98CE\u683C\uFF1B\u4E0D\u8981\u6539\u5199\u53E5\u5B50\u3001\u4E0D\u8981\u8865\u5145\u5185\u5BB9\u3001\u4E0D\u8981\u56DE\u7B54\u95EE\u9898\u3001\u4E0D\u8981\u6539\u53D8\u6570\u5B57\u548C\u4E13\u4E1A\u672F\u8BED\u3002\u53EA\u8F93\u51FA\u4FEE\u6B63\u540E\u7684\u6587\u672C\uFF0C\u4E0D\u8981\u4EFB\u4F55\u89E3\u91CA\u3002\u5982\u679C\u539F\u6587\u6CA1\u6709\u660E\u663E\u9519\u8BEF\uFF0C\u539F\u6837\u8FD4\u56DE\u3002";
|
|
3304
|
-
}
|
|
3305
|
-
var CORRECTION_PREAMBLE_RE = /^(?:以下是|这是|已|文本)?(?:修正|纠正|更正|纠错)|^(?:here(?:'s| is) (?:the |an |a )?|i have |text after )?(?:corrected|correction)\b/i;
|
|
3306
|
-
var CORRECTION_MIN_SIMILARITY = 0.45;
|
|
3307
|
-
var SIMILARITY_MAX_CHARS = 1500;
|
|
3308
|
-
function normalizedSimilarity(a, b) {
|
|
3309
|
-
const norm = (s) => Array.from(s.toLowerCase().replace(/[\s\p{P}\p{S}]+/gu, ""));
|
|
3310
|
-
const x = norm(a);
|
|
3311
|
-
const y = norm(b);
|
|
3312
|
-
const n = Math.max(x.length, y.length);
|
|
3313
|
-
if (n === 0) return 1;
|
|
3314
|
-
if (n > SIMILARITY_MAX_CHARS) return 1;
|
|
3315
|
-
let prev = Array.from({ length: y.length + 1 }, (_, j) => j);
|
|
3316
|
-
for (let i = 1; i <= x.length; i++) {
|
|
3317
|
-
const cur = [i];
|
|
3318
|
-
for (let j = 1; j <= y.length; j++) {
|
|
3319
|
-
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (x[i - 1] === y[j - 1] ? 0 : 1));
|
|
3320
|
-
}
|
|
3321
|
-
prev = cur;
|
|
3322
|
-
}
|
|
3323
|
-
return 1 - prev[y.length] / n;
|
|
3324
|
-
}
|
|
3325
|
-
var CORRECTION_FILLER_WORDS = "\u5F53\u7136\u53EF\u4EE5|\u5F53\u7136|\u597D\u5427|\u597D\u7684?|\u884C\u5427|\u884C|\u662F\u7684|\u55EF|\u6536\u5230|\u660E\u767D\u4E86|\u77E5\u9053\u4E86|Thank you|Of course|Got it|OK|Okay|Sure|Certainly|Alright|Understood|Hello|Hi|Yes|Thanks";
|
|
3326
|
-
var CORRECTION_FILLER_PREFIX_RE = new RegExp(
|
|
3327
|
-
"^(?:(?:" + CORRECTION_FILLER_WORDS + ")(?:\\s*[,\uFF0C\u3001:\uFF1A.\u3002!\uFF01?\uFF1F)\uFF09]|\\s))+",
|
|
3328
|
-
"i"
|
|
3329
|
-
);
|
|
3330
|
-
var CORRECTION_PURE_FILLER_RE = new RegExp(
|
|
3331
|
-
"^(?:(?:" + CORRECTION_FILLER_WORDS + ")(?:\\s*[,\uFF0C\u3001:\uFF1A.\u3002!\uFF01?\uFF1F)\uFF09]\\s*|\\s+))*(?:" + CORRECTION_FILLER_WORDS + ")\\s*$",
|
|
3332
|
-
"i"
|
|
3333
|
-
);
|
|
3334
4033
|
async function correctText(ctx, text, lang) {
|
|
3335
4034
|
const trimmed = String(text ?? "").trim();
|
|
3336
4035
|
if (!trimmed) return text;
|
|
@@ -3348,7 +4047,6 @@ async function correctText(ctx, text, lang) {
|
|
|
3348
4047
|
provider: sel.provider,
|
|
3349
4048
|
model: sel.model,
|
|
3350
4049
|
system: correctionSystem(lang),
|
|
3351
|
-
// host 侧自弃 10s(早于客户端 12s,审查 M3):超时或输出失控超长均立即 abort。
|
|
3352
4050
|
signal: ac.signal,
|
|
3353
4051
|
messages: [{ role: "user", content: [{ type: "text", text: trimmed }] }]
|
|
3354
4052
|
});
|
|
@@ -3364,22 +4062,7 @@ async function correctText(ctx, text, lang) {
|
|
|
3364
4062
|
} finally {
|
|
3365
4063
|
clearTimeout(timer);
|
|
3366
4064
|
}
|
|
3367
|
-
|
|
3368
|
-
if (corrected.length > Math.max(trimmed.length * 3, trimmed.length + 24)) return text;
|
|
3369
|
-
if (normalizedSimilarity(trimmed, corrected) < CORRECTION_MIN_SIMILARITY) return text;
|
|
3370
|
-
if (CORRECTION_PREAMBLE_RE.test(corrected)) return text;
|
|
3371
|
-
let noFiller = corrected;
|
|
3372
|
-
for (; ; ) {
|
|
3373
|
-
const next = noFiller.replace(CORRECTION_FILLER_PREFIX_RE, "").trimStart();
|
|
3374
|
-
const after = CORRECTION_PURE_FILLER_RE.test(next) ? "" : next;
|
|
3375
|
-
if (after === noFiller) break;
|
|
3376
|
-
noFiller = after;
|
|
3377
|
-
}
|
|
3378
|
-
if (noFiller !== corrected) {
|
|
3379
|
-
if (noFiller === "") return text;
|
|
3380
|
-
if (CORRECTION_PREAMBLE_RE.test(noFiller)) return text;
|
|
3381
|
-
}
|
|
3382
|
-
return corrected || text;
|
|
4065
|
+
return applyCorrectionGuards(text, out);
|
|
3383
4066
|
} catch (e) {
|
|
3384
4067
|
console.warn(`[dsh-voice-mode] llm correction failed: ${redactSecrets(e)}`);
|
|
3385
4068
|
return text;
|
|
@@ -3387,18 +4070,16 @@ async function correctText(ctx, text, lang) {
|
|
|
3387
4070
|
}
|
|
3388
4071
|
|
|
3389
4072
|
// src/audio-format.ts
|
|
3390
|
-
function
|
|
3391
|
-
if (buf.length
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
if (buf.length >= 3 && buf[0] === 255 && (buf[1] & 224) === 224 && (buf[1] & 24) !== 8 && ((buf[1] & 6) === 2 || (buf[1] & 6) === 4) && (buf[2] & 240) !== 240 && (buf[2] & 12) !== 12) {
|
|
3399
|
-
return "audio/mpeg";
|
|
4073
|
+
function sniffAudioMime2(buf, fallback) {
|
|
4074
|
+
if (buf.length === 0) return fallback;
|
|
4075
|
+
if (buf[0] === 255) {
|
|
4076
|
+
if (buf.length >= 7 && (buf[1] & 246) === 240) return "audio/aac";
|
|
4077
|
+
if (buf.length >= 3 && (buf[1] & 224) === 224 && (buf[1] & 24) !== 8 && ((buf[1] & 6) === 2 || (buf[1] & 6) === 4) && (buf[2] & 240) !== 240 && (buf[2] & 12) !== 12) {
|
|
4078
|
+
return "audio/mpeg";
|
|
4079
|
+
}
|
|
4080
|
+
return fallback;
|
|
3400
4081
|
}
|
|
3401
|
-
return fallback;
|
|
4082
|
+
return sniffAudioMime(buf) ?? fallback;
|
|
3402
4083
|
}
|
|
3403
4084
|
|
|
3404
4085
|
// src/online-tts.ts
|
|
@@ -3446,8 +4127,8 @@ function resolveOnlineTts(overrides, config) {
|
|
|
3446
4127
|
|
|
3447
4128
|
// src/tts-openai.ts
|
|
3448
4129
|
var MIME = { mp3: "audio/mpeg", wav: "audio/wav" };
|
|
3449
|
-
var
|
|
3450
|
-
var
|
|
4130
|
+
var MAX_AUDIO_BYTES2 = 16 * 1024 * 1024;
|
|
4131
|
+
var MAX_ERROR_BYTES2 = 8 * 1024;
|
|
3451
4132
|
async function readResponseBytes(res, maxBytes, truncate) {
|
|
3452
4133
|
const lenHeader = Number(res.headers.get("content-length") ?? 0);
|
|
3453
4134
|
if (!truncate && Number.isFinite(lenHeader) && lenHeader > maxBytes) {
|
|
@@ -3609,14 +4290,14 @@ var OpenAiTtsEngine = class {
|
|
|
3609
4290
|
redirect: "error"
|
|
3610
4291
|
});
|
|
3611
4292
|
if (!res.ok) {
|
|
3612
|
-
const rawErr = await readResponseBytes(res,
|
|
4293
|
+
const rawErr = await readResponseBytes(res, MAX_ERROR_BYTES2, true).then((b) => b.toString("utf8"), () => "");
|
|
3613
4294
|
const detail = redactBodySnippet(rawErr);
|
|
3614
4295
|
throw new OpenAiTtsError("openai_http_error", `HTTP ${res.status}${detail ? `: ${detail}` : ""}`);
|
|
3615
4296
|
}
|
|
3616
4297
|
const ctype = (res.headers.get("content-type") ?? "").toLowerCase();
|
|
3617
|
-
const buf = await readResponseBytes(res,
|
|
4298
|
+
const buf = await readResponseBytes(res, MAX_AUDIO_BYTES2, false);
|
|
3618
4299
|
if (buf.length === 0) throw new OpenAiTtsError("openai_empty_audio", "empty audio");
|
|
3619
|
-
const sniffed =
|
|
4300
|
+
const sniffed = sniffAudioMime2(buf, "");
|
|
3620
4301
|
let mime;
|
|
3621
4302
|
if (sniffed) {
|
|
3622
4303
|
mime = sniffed;
|
|
@@ -3683,20 +4364,20 @@ function presetRequiresKey(presetId) {
|
|
|
3683
4364
|
if (!Object.prototype.hasOwnProperty.call(OPENAI_PRESETS, presetId)) return false;
|
|
3684
4365
|
return !presetId.startsWith("selfhosted-");
|
|
3685
4366
|
}
|
|
3686
|
-
var
|
|
3687
|
-
var
|
|
4367
|
+
var SILICONFLOW_SYSTEM_VOICES2 = ["alex", "benjamin", "charles", "david", "anna", "bella", "claire", "diana"];
|
|
4368
|
+
var OPENAI_API_VOICES2 = ["alloy", "ash", "ballad", "coral", "echo", "fable", "marin", "cedar", "nova", "onyx", "sage", "shimmer"];
|
|
3688
4369
|
var OPENAI_PRESETS = {
|
|
3689
4370
|
"siliconflow-cosyvoice2": {
|
|
3690
4371
|
baseUrl: "https://api.siliconflow.cn/v1",
|
|
3691
4372
|
model: "FunAudioLLM/CosyVoice2-0.5B",
|
|
3692
4373
|
voice: "FunAudioLLM/CosyVoice2-0.5B:alex",
|
|
3693
|
-
voices:
|
|
4374
|
+
voices: SILICONFLOW_SYSTEM_VOICES2.map((v) => `FunAudioLLM/CosyVoice2-0.5B:${v}`)
|
|
3694
4375
|
},
|
|
3695
4376
|
"siliconflow-fish": {
|
|
3696
4377
|
baseUrl: "https://api.siliconflow.cn/v1",
|
|
3697
4378
|
model: "fishaudio/fish-speech-1.5",
|
|
3698
4379
|
voice: "fishaudio/fish-speech-1.5:anna",
|
|
3699
|
-
voices:
|
|
4380
|
+
voices: SILICONFLOW_SYSTEM_VOICES2.map((v) => `fishaudio/fish-speech-1.5:${v}`)
|
|
3700
4381
|
},
|
|
3701
4382
|
// OpenRouter 官方 TTS 文档(2026-05-01 公告):POST /api/v1/audio/speech,
|
|
3702
4383
|
// 示例模型 slug 为带日期的 `openai/gpt-4o-mini-tts-2025-12-15`、voice `alloy`、response_format mp3。
|
|
@@ -3705,14 +4386,14 @@ var OPENAI_PRESETS = {
|
|
|
3705
4386
|
baseUrl: "https://openrouter.ai/api/v1",
|
|
3706
4387
|
model: "openai/gpt-4o-mini-tts-2025-12-15",
|
|
3707
4388
|
voice: "alloy",
|
|
3708
|
-
voices: [...
|
|
4389
|
+
voices: [...OPENAI_API_VOICES2]
|
|
3709
4390
|
},
|
|
3710
4391
|
// OpenAI 官方(docs: /v1/audio/speech,模型 gpt-4o-mini-tts,官方推荐 marin/cedar 音质最佳)。
|
|
3711
4392
|
"openai-official": {
|
|
3712
4393
|
baseUrl: "https://api.openai.com/v1",
|
|
3713
4394
|
model: "gpt-4o-mini-tts",
|
|
3714
4395
|
voice: "marin",
|
|
3715
|
-
voices: [...
|
|
4396
|
+
voices: [...OPENAI_API_VOICES2]
|
|
3716
4397
|
},
|
|
3717
4398
|
// 自建示例(本机地址为占位;无鉴权时 key 留空即可):
|
|
3718
4399
|
// Kokoro-FastAPI / LocalAI 的 OpenAI 兼容面,Kokoro 音色名取自本插件本地目录(zf_xiaobei 等)。
|
|
@@ -3851,16 +4532,24 @@ function buildSettingsGetView(vset, config) {
|
|
|
3851
4532
|
}
|
|
3852
4533
|
|
|
3853
4534
|
// src/engine-kind.ts
|
|
3854
|
-
var
|
|
3855
|
-
var
|
|
3856
|
-
|
|
3857
|
-
|
|
4535
|
+
var TTS_ENGINE_KINDS2 = ["vits", "kokoro", "edge", "openai", "mimo"];
|
|
4536
|
+
var DSH_TO_CORE_TTS = {
|
|
4537
|
+
vits: "vits-wasm",
|
|
4538
|
+
kokoro: "kokoro-daemon",
|
|
4539
|
+
openai: "openai-compat",
|
|
4540
|
+
mimo: "mimo"
|
|
4541
|
+
};
|
|
4542
|
+
function toCoreTtsKind(value) {
|
|
4543
|
+
return typeof value === "string" && Object.hasOwn(DSH_TO_CORE_TTS, value) ? DSH_TO_CORE_TTS[value] : void 0;
|
|
4544
|
+
}
|
|
4545
|
+
function asTtsEngineKind2(value) {
|
|
4546
|
+
return TTS_ENGINE_KINDS2.includes(value) ? value : "vits";
|
|
3858
4547
|
}
|
|
3859
4548
|
function isLocalEngine(value) {
|
|
3860
|
-
return
|
|
4549
|
+
return isLocalTtsEngine(toCoreTtsKind(value));
|
|
3861
4550
|
}
|
|
3862
|
-
function
|
|
3863
|
-
return value
|
|
4551
|
+
function usesBilingualSample2(value) {
|
|
4552
|
+
return usesBilingualSample(toCoreTtsKind(value));
|
|
3864
4553
|
}
|
|
3865
4554
|
|
|
3866
4555
|
// src/prompts.ts
|
|
@@ -3889,7 +4578,7 @@ function mimoSpeedInstruction(rate) {
|
|
|
3889
4578
|
return rate > 1 ? `\u8BED\u901F\u52A0\u5FEB\u5230\u7EA6 ${rate} \u500D` : `\u8BED\u901F\u653E\u6162\u5230\u7EA6 ${rate} \u500D`;
|
|
3890
4579
|
}
|
|
3891
4580
|
function previewSample(engine, voice) {
|
|
3892
|
-
if (
|
|
4581
|
+
if (usesBilingualSample2(engine)) return `${SAMPLE_ZH}${SAMPLE_EN}`;
|
|
3893
4582
|
if (engine === "vits") return SAMPLE_ZH;
|
|
3894
4583
|
const m = /^([a-z]{2,3})-/i.exec(voice);
|
|
3895
4584
|
return m && SAMPLE_BY_LANG[m[1].toLowerCase()] || SAMPLE_EN;
|
|
@@ -3901,7 +4590,7 @@ var MIME2 = {
|
|
|
3901
4590
|
mp3: "audio/mpeg"
|
|
3902
4591
|
};
|
|
3903
4592
|
var MAX_JSON_BYTES = 24 * 1024 * 1024;
|
|
3904
|
-
var
|
|
4593
|
+
var MAX_ERROR_BYTES3 = 8 * 1024;
|
|
3905
4594
|
async function readMimoBytes(res, maxBytes, truncate) {
|
|
3906
4595
|
const lenHeader = Number(res.headers.get("content-length") ?? 0);
|
|
3907
4596
|
if (!truncate && Number.isFinite(lenHeader) && lenHeader > maxBytes) {
|
|
@@ -3950,9 +4639,9 @@ var MimoTtsError = class extends Error {
|
|
|
3950
4639
|
this.code = code;
|
|
3951
4640
|
}
|
|
3952
4641
|
};
|
|
3953
|
-
var
|
|
3954
|
-
var
|
|
3955
|
-
var
|
|
4642
|
+
var MIMO_DEFAULT_BASE_URL2 = "https://api.xiaomimimo.com/v1";
|
|
4643
|
+
var MIMO_DEFAULT_MODEL2 = "mimo-v2.5-tts";
|
|
4644
|
+
var MIMO_DEFAULT_VOICE2 = "mimo_default";
|
|
3956
4645
|
function mimoChatUrl(baseUrl) {
|
|
3957
4646
|
const url = new URL(baseUrl.trim());
|
|
3958
4647
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
@@ -3990,7 +4679,7 @@ function decodeBase64Audio(data) {
|
|
|
3990
4679
|
}
|
|
3991
4680
|
return Buffer.from(clean, "base64");
|
|
3992
4681
|
}
|
|
3993
|
-
var
|
|
4682
|
+
var MimoTtsEngine2 = class {
|
|
3994
4683
|
voice;
|
|
3995
4684
|
rate;
|
|
3996
4685
|
url;
|
|
@@ -4014,13 +4703,13 @@ var MimoTtsEngine = class {
|
|
|
4014
4703
|
if (!options.apiKey?.trim()) {
|
|
4015
4704
|
throw new MimoTtsError("mimo_not_configured", "mimo apiKey is not configured");
|
|
4016
4705
|
}
|
|
4017
|
-
url = mimoChatUrl(options.baseUrl?.trim() ||
|
|
4706
|
+
url = mimoChatUrl(options.baseUrl?.trim() || MIMO_DEFAULT_BASE_URL2);
|
|
4018
4707
|
} catch (e) {
|
|
4019
4708
|
this.configError = e instanceof MimoTtsError ? e : new MimoTtsError("mimo_bad_url", "bad baseUrl");
|
|
4020
4709
|
url = null;
|
|
4021
4710
|
}
|
|
4022
4711
|
this.url = url;
|
|
4023
|
-
this.voice = (options.voice ?? "").trim() ||
|
|
4712
|
+
this.voice = (options.voice ?? "").trim() || MIMO_DEFAULT_VOICE2;
|
|
4024
4713
|
this.format = options.responseFormat ?? "wav";
|
|
4025
4714
|
this.timeoutMs = options.timeoutMs ?? 8e3;
|
|
4026
4715
|
this.cooldownMs = options.cooldownMs ?? 3e4;
|
|
@@ -4032,7 +4721,7 @@ var MimoTtsEngine = class {
|
|
|
4032
4721
|
return this.lastMime;
|
|
4033
4722
|
}
|
|
4034
4723
|
updateVoice(voice, rate) {
|
|
4035
|
-
this.voice = voice.trim() || this.options.voice?.trim() ||
|
|
4724
|
+
this.voice = voice.trim() || this.options.voice?.trim() || MIMO_DEFAULT_VOICE2;
|
|
4036
4725
|
if (rate !== void 0 && Number.isFinite(rate)) this.rate = rate;
|
|
4037
4726
|
}
|
|
4038
4727
|
/** 合成并**随结果返回**本句专属 MIME(B6:不从共享 this.lastMime 事后回读,并发异构容器不串味)。 */
|
|
@@ -4078,7 +4767,7 @@ var MimoTtsEngine = class {
|
|
|
4078
4767
|
if (instruction) messages.push({ role: "user", content: instruction });
|
|
4079
4768
|
messages.push({ role: "assistant", content: input });
|
|
4080
4769
|
const body = {
|
|
4081
|
-
model: (this.options.model ?? "").trim() ||
|
|
4770
|
+
model: (this.options.model ?? "").trim() || MIMO_DEFAULT_MODEL2,
|
|
4082
4771
|
messages,
|
|
4083
4772
|
audio: { voice: (options.voice || this.voice).trim(), format: this.format },
|
|
4084
4773
|
stream: false
|
|
@@ -4101,7 +4790,7 @@ var MimoTtsEngine = class {
|
|
|
4101
4790
|
redirect: "error"
|
|
4102
4791
|
});
|
|
4103
4792
|
if (!res.ok) {
|
|
4104
|
-
const rawErr = await readMimoBytes(res,
|
|
4793
|
+
const rawErr = await readMimoBytes(res, MAX_ERROR_BYTES3, true).then((b) => b.toString("utf8"), () => "");
|
|
4105
4794
|
const detail = redactBodySnippet(rawErr);
|
|
4106
4795
|
throw new MimoTtsError("mimo_http_error", `HTTP ${res.status}${detail ? `: ${detail}` : ""}`);
|
|
4107
4796
|
}
|
|
@@ -4117,7 +4806,7 @@ var MimoTtsEngine = class {
|
|
|
4117
4806
|
const buf = decodeBase64Audio(data);
|
|
4118
4807
|
if (buf.length === 0) throw new MimoTtsError("mimo_empty_audio", "empty audio");
|
|
4119
4808
|
const ctype = (res.headers.get("content-type") ?? "").toLowerCase();
|
|
4120
|
-
const sniffed =
|
|
4809
|
+
const sniffed = sniffAudioMime2(buf, "");
|
|
4121
4810
|
let mime;
|
|
4122
4811
|
if (sniffed) mime = sniffed;
|
|
4123
4812
|
else if (ctype.startsWith("audio/")) mime = ctype.split(";")[0].trim();
|
|
@@ -4302,6 +4991,7 @@ var respondJson2 = (res, status, payload) => {
|
|
|
4302
4991
|
res.end(JSON.stringify(payload));
|
|
4303
4992
|
};
|
|
4304
4993
|
var VOICE_SPOKEN_SECTION = "voice-mode:spoken-format";
|
|
4994
|
+
var VOICE_SPOKEN_SECTION_ORDER = 10300;
|
|
4305
4995
|
var inject = ["webServer", "settings", "sessions"];
|
|
4306
4996
|
var defaultModelCacheDir = () => process.platform === "win32" ? join5(process.env.LOCALAPPDATA ?? join5(homedir2(), "AppData", "Local"), "dsh-voice-mode", "models") : join5(homedir2(), ".cache", "dsh-voice-mode", "models");
|
|
4307
4997
|
var VOICE_SETTINGS_DEFAULTS = {
|
|
@@ -4394,7 +5084,7 @@ var DESC = {
|
|
|
4394
5084
|
function createVoiceSettingsSchema(defs) {
|
|
4395
5085
|
const d = { ...VOICE_SETTINGS_DEFAULTS, ...defs };
|
|
4396
5086
|
return z2.object({
|
|
4397
|
-
ttsEngine: z2.union(
|
|
5087
|
+
ttsEngine: z2.union(TTS_ENGINE_KINDS2.map((k) => z2.const(k))).default(d.ttsEngine).description(
|
|
4398
5088
|
DESC.ttsEngine
|
|
4399
5089
|
),
|
|
4400
5090
|
kokoroModel: z2.union([z2.const("int8"), z2.const("fp32")]).default(d.kokoroModel).description(
|
|
@@ -4494,7 +5184,7 @@ var Config = z2.object({
|
|
|
4494
5184
|
enabled: z2.boolean().default(true),
|
|
4495
5185
|
cacheDir: z2.string().default(defaultModelCacheDir()),
|
|
4496
5186
|
modelHost: z2.string().default("https://huggingface.co"),
|
|
4497
|
-
ttsEngine: z2.union(
|
|
5187
|
+
ttsEngine: z2.union(TTS_ENGINE_KINDS2.map((k) => z2.const(k))).default("edge"),
|
|
4498
5188
|
openaiTts: OpenAiTtsConfig,
|
|
4499
5189
|
mimoTts: MimoTtsConfig,
|
|
4500
5190
|
kokoroModel: z2.union([z2.const("int8"), z2.const("fp32")]).default("int8"),
|
|
@@ -4790,7 +5480,7 @@ function apply(ctx, config) {
|
|
|
4790
5480
|
const rebuildReplacer = (rules) => {
|
|
4791
5481
|
replacementRuleCount = rules.length;
|
|
4792
5482
|
replacementHits = new Array(rules.length).fill(0);
|
|
4793
|
-
replacerBox.fn =
|
|
5483
|
+
replacerBox.fn = createAsrReplacer2(rules, (i) => {
|
|
4794
5484
|
replacementHits[i] = (replacementHits[i] ?? 0) + 1;
|
|
4795
5485
|
});
|
|
4796
5486
|
};
|
|
@@ -4841,7 +5531,7 @@ function apply(ctx, config) {
|
|
|
4841
5531
|
if (kind === "mimo") {
|
|
4842
5532
|
const m = config.mimoTts;
|
|
4843
5533
|
const fb = m.fallback;
|
|
4844
|
-
return new
|
|
5534
|
+
return new MimoTtsEngine2({
|
|
4845
5535
|
// 设置卡片里填的 key 优先(留空则回落到配置文件 mimoTts.apiKey);key 只在宿主侧。
|
|
4846
5536
|
apiKey: (vset.mimoApiKey ?? "").trim() || m.apiKey,
|
|
4847
5537
|
baseUrl: m.baseUrl,
|
|
@@ -4903,7 +5593,7 @@ function apply(ctx, config) {
|
|
|
4903
5593
|
broadcast
|
|
4904
5594
|
});
|
|
4905
5595
|
};
|
|
4906
|
-
let engineKind =
|
|
5596
|
+
let engineKind = asTtsEngineKind2(vset.ttsEngine ?? config.ttsEngine);
|
|
4907
5597
|
let activeKokoroModel = vset.kokoroModel;
|
|
4908
5598
|
const queue = new TtsQueue({
|
|
4909
5599
|
engine: makeEngine(engineKind),
|
|
@@ -5022,7 +5712,12 @@ function apply(ctx, config) {
|
|
|
5022
5712
|
if (!config.enabled || !vset.spokenFormat) return next();
|
|
5023
5713
|
const agentId = context.agent?.id;
|
|
5024
5714
|
if (agentId !== void 0 && agentId === activeVoiceSession) {
|
|
5025
|
-
|
|
5715
|
+
const spokenSection = {
|
|
5716
|
+
name: VOICE_SPOKEN_SECTION,
|
|
5717
|
+
text: spokenPrompt(activeVoiceLang),
|
|
5718
|
+
order: VOICE_SPOKEN_SECTION_ORDER
|
|
5719
|
+
};
|
|
5720
|
+
assembly.sections.push(spokenSection);
|
|
5026
5721
|
}
|
|
5027
5722
|
return next();
|
|
5028
5723
|
});
|
|
@@ -5389,10 +6084,10 @@ function apply(ctx, config) {
|
|
|
5389
6084
|
collectBody(req, res, MAX_JSON_BODY, (body) => {
|
|
5390
6085
|
let kind = "asr";
|
|
5391
6086
|
try {
|
|
5392
|
-
const
|
|
5393
|
-
if (
|
|
5394
|
-
} else if (
|
|
5395
|
-
kind =
|
|
6087
|
+
const p2 = JSON.parse(body || "{}");
|
|
6088
|
+
if (p2.kind === void 0) {
|
|
6089
|
+
} else if (p2.kind === "vad" || p2.kind === "sense" || p2.kind === "asr") {
|
|
6090
|
+
kind = p2.kind;
|
|
5396
6091
|
} else {
|
|
5397
6092
|
respondJson2(res, 400, { code: "bad_request", error: "invalid kind" });
|
|
5398
6093
|
return;
|
|
@@ -5438,8 +6133,8 @@ function apply(ctx, config) {
|
|
|
5438
6133
|
collectBody(req, res, MAX_JSON_BODY, (body) => {
|
|
5439
6134
|
let engine = "vits";
|
|
5440
6135
|
try {
|
|
5441
|
-
const
|
|
5442
|
-
if (isLocalEngine(
|
|
6136
|
+
const p2 = JSON.parse(body || "{}");
|
|
6137
|
+
if (isLocalEngine(p2.engine)) engine = p2.engine;
|
|
5443
6138
|
else {
|
|
5444
6139
|
respondJson2(res, 400, { code: "bad_request", error: "invalid engine" });
|
|
5445
6140
|
return;
|
|
@@ -5490,8 +6185,8 @@ function apply(ctx, config) {
|
|
|
5490
6185
|
collectBody(req, res, MAX_JSON_BODY, (body) => {
|
|
5491
6186
|
let engine = "vits";
|
|
5492
6187
|
try {
|
|
5493
|
-
const
|
|
5494
|
-
if (isLocalEngine(
|
|
6188
|
+
const p2 = JSON.parse(body || "{}");
|
|
6189
|
+
if (isLocalEngine(p2.engine)) engine = p2.engine;
|
|
5495
6190
|
else {
|
|
5496
6191
|
respondJson2(res, 400, { code: "bad_request", error: "invalid engine" });
|
|
5497
6192
|
return;
|