dsh-voice 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.en.md +17 -15
- package/README.md +17 -15
- package/docs/USAGE.en.md +39 -16
- package/docs/USAGE.md +41 -18
- package/lib/client.js +5067 -0
- package/lib/config.d.ts +7 -0
- package/lib/config.js +21 -0
- package/lib/edge-tts.d.ts +4 -0
- package/lib/edge-tts.js +38 -1
- package/lib/host-config.js +7 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +4 -1
- package/lib/openai-tts.d.ts +19 -6
- package/lib/openai-tts.js +163 -2
- package/lib/stt.d.ts +19 -0
- package/lib/stt.js +157 -9
- package/lib/tools.d.ts +4 -2
- package/lib/tools.js +380 -70
- package/lib/tts-text.d.ts +46 -3
- package/lib/tts-text.js +345 -16
- package/lib/web.d.ts +66 -0
- package/lib/web.js +677 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +10 -5
- package/skills/voice-workflow/SKILL.md +51 -19
- package/lib/update-client.js +0 -745
package/lib/web.js
ADDED
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { isLoopbackBaseUrl, isValidProsody, resolveCustomVoice, } from './config.js';
|
|
4
|
+
import { fetchEdgeVoices, synthesizeSpeech } from './edge-tts.js';
|
|
5
|
+
import { rateToSpeed, synthesizeOpenAiSpeech } from './openai-tts.js';
|
|
6
|
+
import { createProxyFetch } from './proxy-fetch.js';
|
|
7
|
+
import { hasFfmpeg, transcribe } from './stt.js';
|
|
8
|
+
import { DEFAULT_PREVIEW_TEXT } from './tools.js';
|
|
9
|
+
import { applyPronunciationsPlain, inspectMp3Buffer } from './tts-text.js';
|
|
10
|
+
import { filterVoices, isValidVoiceId, resolvePersona, VOICE_PERSONAS, VOICES } from './voices.js';
|
|
11
|
+
export const VOICE_STUDIO_SETTINGS_ROUTE = '/_dsh/dsh-voice/studio';
|
|
12
|
+
export const VOICE_STUDIO_API_ROUTE = '/api/dsh-voice/studio';
|
|
13
|
+
const AUTHENTICATED_CARRIER = Symbol('DSH authenticated Voice Studio connection');
|
|
14
|
+
const MAX_AUDIO_BYTES = 10 * 1024 * 1024;
|
|
15
|
+
const MAX_BODY = 16 * 1024 * 1024;
|
|
16
|
+
const MAX_PREVIEW_CHARS = 300;
|
|
17
|
+
const loopback = (host) => ['127.0.0.1', 'localhost', '::1', '[::1]', '::ffff:127.0.0.1'].includes(host.toLowerCase());
|
|
18
|
+
const record = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
19
|
+
const text = (args, key) => typeof args[key] === 'string' ? args[key].trim() : '';
|
|
20
|
+
export class VoiceStudioError extends Error {
|
|
21
|
+
status;
|
|
22
|
+
code;
|
|
23
|
+
constructor(message, status = 400, code = 'invalid') {
|
|
24
|
+
super(message);
|
|
25
|
+
this.status = status;
|
|
26
|
+
this.code = code;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function requestGuard(req) {
|
|
30
|
+
const host = req.headers.host;
|
|
31
|
+
if (!host || !loopback(req.socket.remoteAddress ?? '')) {
|
|
32
|
+
throw new VoiceStudioError('Voice Studio 面板仅允许本机访问。', 403, 'local_only');
|
|
33
|
+
}
|
|
34
|
+
let target;
|
|
35
|
+
try {
|
|
36
|
+
target = new URL('http://' + host);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
throw new VoiceStudioError('无效的请求地址。', 403);
|
|
40
|
+
}
|
|
41
|
+
if (!loopback(target.hostname) || target.username || target.password || target.pathname !== '/' || target.search || target.hash) {
|
|
42
|
+
throw new VoiceStudioError('Voice Studio 面板仅允许本机访问。', 403, 'local_only');
|
|
43
|
+
}
|
|
44
|
+
const protocol = req.socket.encrypted ? 'https:' : 'http:';
|
|
45
|
+
target.protocol = protocol;
|
|
46
|
+
const origin = req.headers.origin;
|
|
47
|
+
const desktop = origin === 'dsh-app://app' && req.headers['x-dsh-voice'] === '1';
|
|
48
|
+
if (!desktop && ((req.method === 'POST' && !origin) || (origin !== undefined && origin !== target.origin))) {
|
|
49
|
+
throw new VoiceStudioError('请从当前 DSH 设置页面操作。', 403, 'origin');
|
|
50
|
+
}
|
|
51
|
+
if (req.method === 'POST' && !/^application\/json(?:\s*;|$)/i.test(req.headers['content-type'] ?? '')) {
|
|
52
|
+
throw new VoiceStudioError('请求必须使用 JSON。', 415);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** 宿主载体受鉴权后仍校验来源、方法与请求头标记,防止跨站请求。 */
|
|
56
|
+
function carrierGuard(request) {
|
|
57
|
+
if (!['GET', 'POST'].includes(request.method)) {
|
|
58
|
+
throw new VoiceStudioError('仅支持 GET 和 POST。', 405, 'method');
|
|
59
|
+
}
|
|
60
|
+
if (request.headers.get('x-dsh-voice') !== '1') {
|
|
61
|
+
throw new VoiceStudioError('请从当前 DSH 设置页面操作。', 403, 'origin');
|
|
62
|
+
}
|
|
63
|
+
const site = request.headers.get('sec-fetch-site');
|
|
64
|
+
if (site && !['same-origin', 'none'].includes(site)) {
|
|
65
|
+
throw new VoiceStudioError('请从当前 DSH 设置页面操作。', 403, 'origin');
|
|
66
|
+
}
|
|
67
|
+
const origin = request.headers.get('origin');
|
|
68
|
+
if (origin && origin !== 'dsh-app://app') {
|
|
69
|
+
try {
|
|
70
|
+
const source = new URL(origin);
|
|
71
|
+
const expectedHost = request.headers.get('host') || new URL(request.url).host;
|
|
72
|
+
if (!['http:', 'https:'].includes(source.protocol) ||
|
|
73
|
+
source.username ||
|
|
74
|
+
source.password ||
|
|
75
|
+
source.origin !== origin ||
|
|
76
|
+
source.host !== expectedHost) {
|
|
77
|
+
throw new Error('origin mismatch');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
throw new VoiceStudioError('请从当前 DSH 设置页面操作。', 403, 'origin');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (request.method === 'POST' &&
|
|
85
|
+
request.headers.get('content-type')?.split(';')[0].trim().toLowerCase() !== 'application/json') {
|
|
86
|
+
throw new VoiceStudioError('请求必须使用 JSON。', 415);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function readBody(req, signal) {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
let length = 0;
|
|
92
|
+
const chunks = [];
|
|
93
|
+
const cleanup = () => {
|
|
94
|
+
req.off('data', data);
|
|
95
|
+
req.off('end', end);
|
|
96
|
+
req.off('error', error);
|
|
97
|
+
signal.removeEventListener('abort', abort);
|
|
98
|
+
};
|
|
99
|
+
const fail = (reason) => {
|
|
100
|
+
cleanup();
|
|
101
|
+
reject(reason);
|
|
102
|
+
};
|
|
103
|
+
const data = (chunk) => {
|
|
104
|
+
length += chunk.length;
|
|
105
|
+
if (length > MAX_BODY) {
|
|
106
|
+
chunks.length = 0;
|
|
107
|
+
fail(new VoiceStudioError('请求体超过 16 MiB 上限。', 413, 'bodyLarge'));
|
|
108
|
+
req.resume();
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
chunks.push(chunk);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const error = (reason) => fail(reason);
|
|
115
|
+
const abort = () => fail(signal.reason);
|
|
116
|
+
const end = () => {
|
|
117
|
+
cleanup();
|
|
118
|
+
try {
|
|
119
|
+
const value = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
120
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
121
|
+
throw new Error();
|
|
122
|
+
resolve(value);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
reject(new VoiceStudioError('请求内容不是有效的 JSON。'));
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
if (signal.aborted) {
|
|
129
|
+
abort();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
req.on('data', data);
|
|
133
|
+
req.on('end', end);
|
|
134
|
+
req.on('error', error);
|
|
135
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function readPronunciations(cfg) {
|
|
139
|
+
const raw = cfg.pronunciations;
|
|
140
|
+
if (!Array.isArray(raw))
|
|
141
|
+
return [];
|
|
142
|
+
const out = [];
|
|
143
|
+
for (const item of raw) {
|
|
144
|
+
if (!item || typeof item !== 'object')
|
|
145
|
+
continue;
|
|
146
|
+
const rec = item;
|
|
147
|
+
const word = typeof rec.word === 'string' ? rec.word.trim() : '';
|
|
148
|
+
const alias = typeof rec.alias === 'string' ? rec.alias.trim() : '';
|
|
149
|
+
if (word !== '' && alias !== '')
|
|
150
|
+
out.push({ word, alias });
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
function sanitizeCustomVoicesPatch(raw) {
|
|
155
|
+
if (!Array.isArray(raw)) {
|
|
156
|
+
throw new VoiceStudioError('customVoices 必须是数组。', 400);
|
|
157
|
+
}
|
|
158
|
+
const out = [];
|
|
159
|
+
for (let i = 0; i < raw.length; i++) {
|
|
160
|
+
const item = record(raw[i]);
|
|
161
|
+
const name = text(item, 'name');
|
|
162
|
+
if (!name) {
|
|
163
|
+
throw new VoiceStudioError(`customVoices[${i}].name 不能为空。`, 400);
|
|
164
|
+
}
|
|
165
|
+
const entry = { name };
|
|
166
|
+
const voice = text(item, 'voice');
|
|
167
|
+
if (voice)
|
|
168
|
+
entry.voice = voice;
|
|
169
|
+
const persona = text(item, 'persona');
|
|
170
|
+
if (persona)
|
|
171
|
+
entry.persona = persona;
|
|
172
|
+
const rate = text(item, 'rate');
|
|
173
|
+
if (rate) {
|
|
174
|
+
if (!isValidProsody(rate)) {
|
|
175
|
+
throw new VoiceStudioError(`customVoices[${i}].rate 格式不合法(如 +0%、+15%、-10%)。`, 400);
|
|
176
|
+
}
|
|
177
|
+
entry.rate = rate;
|
|
178
|
+
}
|
|
179
|
+
const pitch = text(item, 'pitch');
|
|
180
|
+
if (pitch) {
|
|
181
|
+
if (!isValidProsody(pitch)) {
|
|
182
|
+
throw new VoiceStudioError(`customVoices[${i}].pitch 格式不合法(如 +0Hz、+2Hz、-1st)。`, 400);
|
|
183
|
+
}
|
|
184
|
+
entry.pitch = pitch;
|
|
185
|
+
}
|
|
186
|
+
const instructions = text(item, 'instructions');
|
|
187
|
+
if (instructions)
|
|
188
|
+
entry.instructions = instructions;
|
|
189
|
+
const referenceAudio = text(item, 'referenceAudio');
|
|
190
|
+
if (referenceAudio)
|
|
191
|
+
entry.referenceAudio = referenceAudio;
|
|
192
|
+
const referenceText = text(item, 'referenceText');
|
|
193
|
+
if (referenceText)
|
|
194
|
+
entry.referenceText = referenceText;
|
|
195
|
+
out.push(entry);
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
function sanitizePronunciationsPatch(raw) {
|
|
200
|
+
if (!Array.isArray(raw)) {
|
|
201
|
+
throw new VoiceStudioError('pronunciations 必须是数组。', 400);
|
|
202
|
+
}
|
|
203
|
+
const out = [];
|
|
204
|
+
const seen = new Set();
|
|
205
|
+
for (let i = 0; i < raw.length; i++) {
|
|
206
|
+
const item = record(raw[i]);
|
|
207
|
+
const word = text(item, 'word');
|
|
208
|
+
const alias = text(item, 'alias');
|
|
209
|
+
if (!word || !alias) {
|
|
210
|
+
throw new VoiceStudioError(`pronunciations[${i}] 的 word 与 alias 均不能为空。`, 400);
|
|
211
|
+
}
|
|
212
|
+
const key = word.toLowerCase();
|
|
213
|
+
if (!seen.has(key)) {
|
|
214
|
+
seen.add(key);
|
|
215
|
+
out.push({ word, alias });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
function sanitizeConfigPatch(rawPatch) {
|
|
221
|
+
const raw = record(rawPatch);
|
|
222
|
+
const sanitized = {};
|
|
223
|
+
if ('ttsVoice' in raw) {
|
|
224
|
+
const ttsVoice = text(raw, 'ttsVoice');
|
|
225
|
+
if (!ttsVoice)
|
|
226
|
+
throw new VoiceStudioError('ttsVoice 不能为空。', 400);
|
|
227
|
+
sanitized.ttsVoice = ttsVoice;
|
|
228
|
+
}
|
|
229
|
+
if ('ttsRate' in raw) {
|
|
230
|
+
const ttsRate = text(raw, 'ttsRate');
|
|
231
|
+
if (!isValidProsody(ttsRate)) {
|
|
232
|
+
throw new VoiceStudioError('ttsRate 格式不合法(如 +0%、+15%、-10%)。', 400);
|
|
233
|
+
}
|
|
234
|
+
sanitized.ttsRate = ttsRate;
|
|
235
|
+
}
|
|
236
|
+
if ('ttsPitch' in raw) {
|
|
237
|
+
const ttsPitch = text(raw, 'ttsPitch');
|
|
238
|
+
if (!isValidProsody(ttsPitch)) {
|
|
239
|
+
throw new VoiceStudioError('ttsPitch 格式不合法(如 +0Hz、+2Hz、-1st)。', 400);
|
|
240
|
+
}
|
|
241
|
+
sanitized.ttsPitch = ttsPitch;
|
|
242
|
+
}
|
|
243
|
+
if ('ttsEngine' in raw) {
|
|
244
|
+
const ttsEngine = text(raw, 'ttsEngine');
|
|
245
|
+
if (!['edge', 'openai', 'siliconflow', 'custom'].includes(ttsEngine)) {
|
|
246
|
+
throw new VoiceStudioError('ttsEngine 不合法。', 400);
|
|
247
|
+
}
|
|
248
|
+
sanitized.ttsEngine = ttsEngine;
|
|
249
|
+
}
|
|
250
|
+
if ('asrEngine' in raw) {
|
|
251
|
+
const asrEngine = text(raw, 'asrEngine');
|
|
252
|
+
if (!['groq', 'openai', 'siliconflow', 'custom'].includes(asrEngine)) {
|
|
253
|
+
throw new VoiceStudioError('asrEngine 不合法。', 400);
|
|
254
|
+
}
|
|
255
|
+
sanitized.asrEngine = asrEngine;
|
|
256
|
+
}
|
|
257
|
+
if ('overwrite' in raw) {
|
|
258
|
+
if (typeof raw.overwrite !== 'boolean') {
|
|
259
|
+
throw new VoiceStudioError('overwrite 必须是布尔值。', 400);
|
|
260
|
+
}
|
|
261
|
+
sanitized.overwrite = raw.overwrite;
|
|
262
|
+
}
|
|
263
|
+
if ('customVoices' in raw) {
|
|
264
|
+
sanitized.customVoices = sanitizeCustomVoicesPatch(raw.customVoices);
|
|
265
|
+
}
|
|
266
|
+
if ('pronunciations' in raw) {
|
|
267
|
+
sanitized.pronunciations = sanitizePronunciationsPatch(raw.pronunciations);
|
|
268
|
+
}
|
|
269
|
+
if (Object.keys(sanitized).length === 0) {
|
|
270
|
+
throw new VoiceStudioError('未提供可保存的有效配置项。', 400);
|
|
271
|
+
}
|
|
272
|
+
return sanitized;
|
|
273
|
+
}
|
|
274
|
+
export function createVoiceStudioBackend(readCfg, options = {}) {
|
|
275
|
+
let cachedProxyUrl = '';
|
|
276
|
+
let cachedFetchImpl;
|
|
277
|
+
const getProxyFetch = (proxyUrl) => {
|
|
278
|
+
if (proxyUrl === '')
|
|
279
|
+
return undefined;
|
|
280
|
+
if (cachedFetchImpl === undefined || cachedProxyUrl !== proxyUrl) {
|
|
281
|
+
cachedProxyUrl = proxyUrl;
|
|
282
|
+
cachedFetchImpl = createProxyFetch(proxyUrl);
|
|
283
|
+
}
|
|
284
|
+
return cachedFetchImpl;
|
|
285
|
+
};
|
|
286
|
+
const writable = () => options.writable !== undefined ? options.writable() !== false : typeof options.updateSettings === 'function';
|
|
287
|
+
const snapshot = () => {
|
|
288
|
+
const cfg = readCfg();
|
|
289
|
+
const pronunciations = readPronunciations(cfg);
|
|
290
|
+
const hasTtsKey = cfg.ttsApiKey !== '';
|
|
291
|
+
const hasAsrKey = cfg.asrApiKey !== '' || (cfg.asrEngine === 'custom' && cfg.asrBaseUrl !== '' && isLoopbackBaseUrl(cfg.asrBaseUrl));
|
|
292
|
+
const hasProxy = cfg.proxyUrl !== '';
|
|
293
|
+
const ttsReady = cfg.ttsEngine === 'edge' ||
|
|
294
|
+
(cfg.ttsBaseUrl !== '' && (cfg.ttsApiKey !== '' || isLoopbackBaseUrl(cfg.ttsBaseUrl)));
|
|
295
|
+
const sttReady = cfg.asrBaseUrl !== '' && hasAsrKey;
|
|
296
|
+
return {
|
|
297
|
+
ttsEngine: cfg.ttsEngine,
|
|
298
|
+
ttsVoice: cfg.ttsVoice,
|
|
299
|
+
ttsRate: cfg.ttsRate,
|
|
300
|
+
ttsPitch: cfg.ttsPitch,
|
|
301
|
+
ttsModel: cfg.ttsModel,
|
|
302
|
+
hasTtsKey,
|
|
303
|
+
asrEngine: cfg.asrEngine,
|
|
304
|
+
asrModel: cfg.asrModel,
|
|
305
|
+
hasAsrKey,
|
|
306
|
+
hasProxy,
|
|
307
|
+
overwrite: cfg.overwrite,
|
|
308
|
+
customVoices: cfg.customVoices,
|
|
309
|
+
pronunciations,
|
|
310
|
+
voices: VOICES,
|
|
311
|
+
personas: VOICE_PERSONAS,
|
|
312
|
+
writable: writable(),
|
|
313
|
+
hasFfmpeg: hasFfmpeg(),
|
|
314
|
+
ttsReady,
|
|
315
|
+
sttReady,
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
async function dispatch(args, signal) {
|
|
319
|
+
const action = text(args, 'action');
|
|
320
|
+
const cfg = readCfg();
|
|
321
|
+
if (action === 'voices') {
|
|
322
|
+
const online = args.online === true;
|
|
323
|
+
const locale = text(args, 'locale') || undefined;
|
|
324
|
+
const gender = text(args, 'gender') || undefined;
|
|
325
|
+
const tag = text(args, 'tag') || undefined;
|
|
326
|
+
let sourceVoices = VOICES;
|
|
327
|
+
let source = 'builtin';
|
|
328
|
+
let fallbackReason = '';
|
|
329
|
+
if (online) {
|
|
330
|
+
try {
|
|
331
|
+
const fetchVoicesFn = options.fetchVoices ?? fetchEdgeVoices;
|
|
332
|
+
const fetched = await fetchVoicesFn({ fetchImpl: getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, signal }, cfg.timeoutMs);
|
|
333
|
+
signal.throwIfAborted();
|
|
334
|
+
if (Array.isArray(fetched) && fetched.length > 0) {
|
|
335
|
+
sourceVoices = fetched;
|
|
336
|
+
source = 'edge-online';
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
source = 'builtin-fallback';
|
|
340
|
+
fallbackReason = '在线音色目录返回为空';
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
signal.throwIfAborted();
|
|
345
|
+
source = 'builtin-fallback';
|
|
346
|
+
fallbackReason = error instanceof Error ? error.message : String(error);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const voices = locale || gender || tag ? filterVoices({ locale, gender, tag }, sourceVoices) : sourceVoices;
|
|
350
|
+
return {
|
|
351
|
+
voices,
|
|
352
|
+
source,
|
|
353
|
+
...(fallbackReason !== '' ? { fallbackReason } : {}),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
if (action === 'preview') {
|
|
357
|
+
const rawText = typeof args.text === 'string' ? args.text.trim() : '';
|
|
358
|
+
const previewText = rawText !== '' ? rawText : DEFAULT_PREVIEW_TEXT;
|
|
359
|
+
if (previewText.length > MAX_PREVIEW_CHARS) {
|
|
360
|
+
throw new VoiceStudioError(`试听文本不能超过 ${MAX_PREVIEW_CHARS} 个字符。`, 400, 'textTooLong');
|
|
361
|
+
}
|
|
362
|
+
const rawVoice = text(args, 'voice') || undefined;
|
|
363
|
+
const rawPersona = text(args, 'persona') || undefined;
|
|
364
|
+
const rawRate = text(args, 'rate') || undefined;
|
|
365
|
+
const rawPitch = text(args, 'pitch') || undefined;
|
|
366
|
+
const rawInstructions = text(args, 'instructions') || undefined;
|
|
367
|
+
let customAlias;
|
|
368
|
+
let voiceConsumedAsAlias = false;
|
|
369
|
+
let personaConsumedAsAlias = false;
|
|
370
|
+
if (rawPersona !== undefined) {
|
|
371
|
+
customAlias = resolveCustomVoice(cfg.customVoices, rawPersona);
|
|
372
|
+
if (customAlias !== undefined)
|
|
373
|
+
personaConsumedAsAlias = true;
|
|
374
|
+
}
|
|
375
|
+
if (customAlias === undefined && rawVoice !== undefined) {
|
|
376
|
+
customAlias = resolveCustomVoice(cfg.customVoices, rawVoice);
|
|
377
|
+
if (customAlias !== undefined)
|
|
378
|
+
voiceConsumedAsAlias = true;
|
|
379
|
+
}
|
|
380
|
+
let defaultAlias;
|
|
381
|
+
if (rawVoice === undefined && rawPersona === undefined) {
|
|
382
|
+
defaultAlias = resolveCustomVoice(cfg.customVoices, cfg.ttsVoice);
|
|
383
|
+
}
|
|
384
|
+
const activeAlias = customAlias ?? defaultAlias;
|
|
385
|
+
let voiceConsumedAsPersona = false;
|
|
386
|
+
let personaSpec = !personaConsumedAsAlias && rawPersona !== undefined ? resolvePersona(rawPersona) : undefined;
|
|
387
|
+
if (personaSpec === undefined && activeAlias?.persona !== undefined) {
|
|
388
|
+
personaSpec = resolvePersona(activeAlias.persona);
|
|
389
|
+
}
|
|
390
|
+
if (personaSpec === undefined && !voiceConsumedAsAlias && rawVoice !== undefined && !isValidVoiceId(rawVoice)) {
|
|
391
|
+
personaSpec = resolvePersona(rawVoice);
|
|
392
|
+
if (personaSpec !== undefined)
|
|
393
|
+
voiceConsumedAsPersona = true;
|
|
394
|
+
}
|
|
395
|
+
let defaultPersona;
|
|
396
|
+
if (rawVoice === undefined && rawPersona === undefined && activeAlias === undefined && !isValidVoiceId(cfg.ttsVoice)) {
|
|
397
|
+
defaultPersona = resolvePersona(cfg.ttsVoice);
|
|
398
|
+
}
|
|
399
|
+
const activePersona = personaSpec ?? defaultPersona;
|
|
400
|
+
const explicitRawVoice = !voiceConsumedAsAlias && !voiceConsumedAsPersona ? rawVoice : undefined;
|
|
401
|
+
const resolvedVoice = explicitRawVoice ?? activeAlias?.voice ?? activePersona?.voice ?? cfg.ttsVoice;
|
|
402
|
+
const resolvedRate = rawRate ?? activeAlias?.rate ?? activePersona?.rate ?? cfg.ttsRate;
|
|
403
|
+
const resolvedPitch = rawPitch ?? activeAlias?.pitch ?? activePersona?.pitch ?? cfg.ttsPitch;
|
|
404
|
+
const resolvedInstructions = rawInstructions ?? activeAlias?.instructions;
|
|
405
|
+
if (!isValidProsody(resolvedRate)) {
|
|
406
|
+
throw new VoiceStudioError('语速格式不合法(如 +0%、+15%、-10%)。', 400);
|
|
407
|
+
}
|
|
408
|
+
if (!isValidProsody(resolvedPitch)) {
|
|
409
|
+
throw new VoiceStudioError('音调格式不合法(如 +0Hz、+2Hz、-1st)。', 400);
|
|
410
|
+
}
|
|
411
|
+
const useEdge = options.tts !== undefined
|
|
412
|
+
? true
|
|
413
|
+
: options.openaiTts !== undefined && cfg.ttsEngine !== 'edge'
|
|
414
|
+
? false
|
|
415
|
+
: cfg.ttsEngine === 'edge';
|
|
416
|
+
const pronunciations = readPronunciations(cfg);
|
|
417
|
+
let audio;
|
|
418
|
+
if (useEdge) {
|
|
419
|
+
if (!isValidVoiceId(resolvedVoice)) {
|
|
420
|
+
throw new VoiceStudioError('音色 ID 不合法:' + resolvedVoice, 400);
|
|
421
|
+
}
|
|
422
|
+
const ttsFn = options.tts ?? synthesizeSpeech;
|
|
423
|
+
const ttsOpts = {
|
|
424
|
+
text: previewText,
|
|
425
|
+
voice: resolvedVoice,
|
|
426
|
+
rate: resolvedRate,
|
|
427
|
+
pitch: resolvedPitch,
|
|
428
|
+
...(pronunciations.length > 0 ? { pronunciations } : {}),
|
|
429
|
+
};
|
|
430
|
+
audio = await ttsFn(ttsOpts, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal);
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
if (cfg.ttsBaseUrl === '') {
|
|
434
|
+
throw new VoiceStudioError('未配置 TTS 接口地址(ttsBaseUrl)。', 400, 'config');
|
|
435
|
+
}
|
|
436
|
+
if (cfg.ttsApiKey === '' && !isLoopbackBaseUrl(cfg.ttsBaseUrl)) {
|
|
437
|
+
throw new VoiceStudioError('未配置 TTS 密钥(ttsApiKey)。', 400, 'config');
|
|
438
|
+
}
|
|
439
|
+
if (resolvedVoice.trim() === '') {
|
|
440
|
+
throw new VoiceStudioError('音色 ID 不能为空。', 400);
|
|
441
|
+
}
|
|
442
|
+
const spokenText = pronunciations.length > 0 ? applyPronunciationsPlain(previewText, pronunciations) : previewText;
|
|
443
|
+
const openaiFn = options.openaiTts ?? synthesizeOpenAiSpeech;
|
|
444
|
+
audio = await openaiFn(cfg.ttsBaseUrl, cfg.ttsApiKey, {
|
|
445
|
+
text: spokenText,
|
|
446
|
+
voice: resolvedVoice,
|
|
447
|
+
model: cfg.ttsModel !== '' ? cfg.ttsModel : 'gpt-4o-mini-tts',
|
|
448
|
+
speed: rateToSpeed(resolvedRate),
|
|
449
|
+
...(resolvedInstructions ? { instructions: resolvedInstructions } : {}),
|
|
450
|
+
signal,
|
|
451
|
+
}, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal);
|
|
452
|
+
}
|
|
453
|
+
const info = inspectMp3Buffer(audio);
|
|
454
|
+
return {
|
|
455
|
+
preview: {
|
|
456
|
+
audioBase64: audio.toString('base64'),
|
|
457
|
+
mime: 'audio/mpeg',
|
|
458
|
+
bytes: audio.length,
|
|
459
|
+
durationMs: info.durationMs,
|
|
460
|
+
durationSec: Math.round(info.durationMs / 100) / 10,
|
|
461
|
+
voice: resolvedVoice,
|
|
462
|
+
},
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
if (action === 'transcribeMic') {
|
|
466
|
+
const rawB64 = typeof args.audioBase64 === 'string' ? args.audioBase64.trim() : '';
|
|
467
|
+
const cleanedB64 = rawB64.replace(/^data:[^;]+;base64,/i, '');
|
|
468
|
+
if (!cleanedB64) {
|
|
469
|
+
throw new VoiceStudioError('请提供录音音频数据(audioBase64)。', 400);
|
|
470
|
+
}
|
|
471
|
+
const audioBuf = Buffer.from(cleanedB64, 'base64');
|
|
472
|
+
if (audioBuf.length === 0) {
|
|
473
|
+
throw new VoiceStudioError('录音音频数据为空或格式无效。', 400);
|
|
474
|
+
}
|
|
475
|
+
if (audioBuf.length > MAX_AUDIO_BYTES) {
|
|
476
|
+
throw new VoiceStudioError('录音音频超过 10 MiB 上限。', 413, 'bodyLarge');
|
|
477
|
+
}
|
|
478
|
+
const rawFilename = text(args, 'filename');
|
|
479
|
+
const filename = (rawFilename || 'mic-recording.webm').replace(/[/\\]/g, '_');
|
|
480
|
+
const language = text(args, 'language') || undefined;
|
|
481
|
+
const prompt = text(args, 'prompt') || undefined;
|
|
482
|
+
const sttFn = options.stt ?? transcribe;
|
|
483
|
+
const res = await sttFn(cfg.asrBaseUrl, cfg.asrApiKey, {
|
|
484
|
+
audio: audioBuf,
|
|
485
|
+
filename,
|
|
486
|
+
model: cfg.asrModel !== '' ? cfg.asrModel : 'whisper-1',
|
|
487
|
+
...(language ? { language } : {}),
|
|
488
|
+
...(prompt ? { prompt } : {}),
|
|
489
|
+
allowEmptyApiKey: cfg.asrEngine === 'custom' && isLoopbackBaseUrl(cfg.asrBaseUrl),
|
|
490
|
+
signal,
|
|
491
|
+
}, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal);
|
|
492
|
+
return {
|
|
493
|
+
transcript: {
|
|
494
|
+
text: res.text,
|
|
495
|
+
...(res.language !== undefined ? { language: res.language } : {}),
|
|
496
|
+
...(res.duration !== undefined ? { duration: res.duration } : {}),
|
|
497
|
+
},
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
if (action === 'updateConfig') {
|
|
501
|
+
if (!writable() || typeof options.updateSettings !== 'function') {
|
|
502
|
+
throw new VoiceStudioError('当前配置是只读的,无法保存设置。', 403, 'readonly');
|
|
503
|
+
}
|
|
504
|
+
const sanitizedPatch = sanitizeConfigPatch(args.patch);
|
|
505
|
+
await options.updateSettings(sanitizedPatch);
|
|
506
|
+
return { saved: Object.keys(sanitizedPatch) };
|
|
507
|
+
}
|
|
508
|
+
throw new VoiceStudioError('未知的操作。', 400);
|
|
509
|
+
}
|
|
510
|
+
return {
|
|
511
|
+
snapshot,
|
|
512
|
+
/** 仅由宿主已鉴权的 connection.fetch 路由调用。 */
|
|
513
|
+
async fetch(request) {
|
|
514
|
+
try {
|
|
515
|
+
carrierGuard(request);
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
return new Response(JSON.stringify({
|
|
519
|
+
error: error instanceof Error ? error.message : String(error),
|
|
520
|
+
code: error instanceof VoiceStudioError ? error.code : 'invalid',
|
|
521
|
+
}), {
|
|
522
|
+
status: error instanceof VoiceStudioError ? error.status : 400,
|
|
523
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
request.signal.throwIfAborted();
|
|
527
|
+
const req = (request.body ? Readable.fromWeb(request.body) : Readable.from([]));
|
|
528
|
+
req.method = request.method;
|
|
529
|
+
req.headers = Object.fromEntries(request.headers);
|
|
530
|
+
const headers = new Headers();
|
|
531
|
+
let status = 200;
|
|
532
|
+
let bytes = Buffer.alloc(0);
|
|
533
|
+
const res = Object.assign(new EventEmitter(), {
|
|
534
|
+
destroyed: false,
|
|
535
|
+
writableFinished: false,
|
|
536
|
+
setHeader(name, value) {
|
|
537
|
+
headers.set(name, value);
|
|
538
|
+
},
|
|
539
|
+
writeHead(value, entries) {
|
|
540
|
+
status = value;
|
|
541
|
+
for (const [name, entry] of Object.entries(entries || {}))
|
|
542
|
+
headers.set(name, entry);
|
|
543
|
+
},
|
|
544
|
+
end(value) {
|
|
545
|
+
bytes = Buffer.from(value);
|
|
546
|
+
this.writableFinished = true;
|
|
547
|
+
},
|
|
548
|
+
});
|
|
549
|
+
const abort = () => req.emit('aborted');
|
|
550
|
+
request.signal.addEventListener('abort', abort, { once: true });
|
|
551
|
+
try {
|
|
552
|
+
await this.handle(req, res, AUTHENTICATED_CARRIER);
|
|
553
|
+
}
|
|
554
|
+
finally {
|
|
555
|
+
request.signal.removeEventListener('abort', abort);
|
|
556
|
+
req.destroy();
|
|
557
|
+
}
|
|
558
|
+
request.signal.throwIfAborted();
|
|
559
|
+
return new Response(new Uint8Array(bytes), { status, headers });
|
|
560
|
+
},
|
|
561
|
+
async handle(req, res, transport) {
|
|
562
|
+
const controller = new AbortController();
|
|
563
|
+
const abort = () => {
|
|
564
|
+
if (!res.writableFinished)
|
|
565
|
+
controller.abort(new Error('请求已取消'));
|
|
566
|
+
};
|
|
567
|
+
req.on('aborted', abort);
|
|
568
|
+
res.on('close', abort);
|
|
569
|
+
const send = (status, value) => {
|
|
570
|
+
if (res.destroyed)
|
|
571
|
+
return;
|
|
572
|
+
res.writeHead(status, {
|
|
573
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
574
|
+
'Cache-Control': 'no-store',
|
|
575
|
+
'X-Content-Type-Options': 'nosniff',
|
|
576
|
+
});
|
|
577
|
+
res.end(JSON.stringify(value));
|
|
578
|
+
};
|
|
579
|
+
try {
|
|
580
|
+
if (transport !== AUTHENTICATED_CARRIER)
|
|
581
|
+
requestGuard(req);
|
|
582
|
+
if (req.method === 'GET') {
|
|
583
|
+
send(200, { snapshot: snapshot() });
|
|
584
|
+
}
|
|
585
|
+
else if (req.method === 'POST') {
|
|
586
|
+
const result = await dispatch(await readBody(req, controller.signal), controller.signal);
|
|
587
|
+
send(200, { ...result, snapshot: snapshot() });
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
res.setHeader('Allow', 'GET, POST');
|
|
591
|
+
send(405, { error: '仅支持 GET 和 POST。', code: 'method' });
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
catch (error) {
|
|
595
|
+
if (!controller.signal.aborted) {
|
|
596
|
+
let state;
|
|
597
|
+
try {
|
|
598
|
+
state = snapshot();
|
|
599
|
+
}
|
|
600
|
+
catch {
|
|
601
|
+
/* 忽略 snapshot 读取异常,避免掩盖原始错误。 */
|
|
602
|
+
}
|
|
603
|
+
send(error instanceof VoiceStudioError ? error.status : 400, {
|
|
604
|
+
error: error instanceof Error ? error.message : String(error),
|
|
605
|
+
code: error instanceof VoiceStudioError ? error.code : 'invalid',
|
|
606
|
+
snapshot: state,
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
finally {
|
|
611
|
+
req.off('aborted', abort);
|
|
612
|
+
res.off('close', abort);
|
|
613
|
+
}
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
export function installVoiceStudioWeb(ctx, readCfg, deps = {}) {
|
|
618
|
+
let updateSettings = deps.updateSettings;
|
|
619
|
+
const backend = createVoiceStudioBackend(readCfg, {
|
|
620
|
+
...deps,
|
|
621
|
+
writable: deps.writable ?? (() => typeof updateSettings === 'function'),
|
|
622
|
+
updateSettings: (patch) => {
|
|
623
|
+
if (typeof updateSettings !== 'function') {
|
|
624
|
+
return Promise.reject(new VoiceStudioError('当前配置是只读的,无法保存设置。', 403, 'readonly'));
|
|
625
|
+
}
|
|
626
|
+
return updateSettings(patch);
|
|
627
|
+
},
|
|
628
|
+
});
|
|
629
|
+
if (typeof ctx.inject !== 'function')
|
|
630
|
+
return backend;
|
|
631
|
+
if (!deps.updateSettings) {
|
|
632
|
+
try {
|
|
633
|
+
ctx.inject(['settings'], (host) => {
|
|
634
|
+
const pluginId = ctx.fiber?.entry?.options?.id || 'dsh-voice';
|
|
635
|
+
if (typeof host?.settings?.update === 'function') {
|
|
636
|
+
updateSettings = (patch) => host.settings.update(pluginId, patch);
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
catch {
|
|
641
|
+
/* 宿主未提供 settings 服务时回退只读状态。 */
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
ctx.inject(['connection'], (host) => {
|
|
645
|
+
const mount = () => host.connection?.fetch?.register?.({
|
|
646
|
+
path: VOICE_STUDIO_API_ROUTE,
|
|
647
|
+
methods: ['GET', 'POST'],
|
|
648
|
+
requestBody: 'buffered',
|
|
649
|
+
fetch: (request) => backend.fetch(request),
|
|
650
|
+
});
|
|
651
|
+
if (typeof host?.effect === 'function') {
|
|
652
|
+
host.effect(() => {
|
|
653
|
+
const off = mount();
|
|
654
|
+
return typeof off === 'function' ? off : undefined;
|
|
655
|
+
}, 'dsh-voice: studio api route');
|
|
656
|
+
}
|
|
657
|
+
else {
|
|
658
|
+
mount();
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
ctx.inject(['webServer'], (webCtx) => {
|
|
662
|
+
if (!webCtx?.webServer?.register)
|
|
663
|
+
return;
|
|
664
|
+
const mount = () => webCtx.webServer.register({
|
|
665
|
+
kind: 'exact',
|
|
666
|
+
path: VOICE_STUDIO_SETTINGS_ROUTE,
|
|
667
|
+
handler: (req, res) => backend.handle(req, res),
|
|
668
|
+
});
|
|
669
|
+
if (typeof webCtx.effect === 'function') {
|
|
670
|
+
webCtx.effect(() => mount(), 'dsh-voice: studio settings route');
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
mount();
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
return backend;
|
|
677
|
+
}
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Voice",
|
|
4
|
-
"description": "
|
|
4
|
+
"description": "5-theme Voice Studio sidebar workbench (Native Airy / Warm Cream / Claude Warmth / Bright 8-Bit / Airy Mint with online 300+ voice & 10-persona auditioning, browser mic dictation, customVoices & pronunciation lexicon): keyless Edge & OpenAI/SiliconFlow TTS, exact MP3 duration feedback, reverse SRT/VTT video dubbing, podcast BGM auto-ducking & -16 LUFS mastering, SHA-256 cached voice cloning, pronunciation overrides, and speaker-diarized multi-engine STT."
|
|
5
5
|
}
|
|
6
6
|
}
|