dsh-voice 0.5.11 → 0.6.1
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 +10 -0
- package/README.en.md +28 -25
- package/README.md +28 -25
- package/docs/USAGE.en.md +73 -67
- package/docs/USAGE.md +72 -66
- package/lib/host-config.d.ts +111 -0
- package/lib/host-config.js +497 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +16 -9
- package/lib/stt.js +3 -3
- package/lib/tools.d.ts +2 -2
- package/lib/tools.js +34 -20
- package/lib/update-client.js +145 -53
- package/package.json +2 -2
package/lib/stt.js
CHANGED
|
@@ -32,9 +32,9 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
|
|
|
32
32
|
const abortSignal = signal ?? options.signal;
|
|
33
33
|
throwIfAborted(abortSignal);
|
|
34
34
|
if (apiKey === '')
|
|
35
|
-
throw new Error('未配置 ASR
|
|
35
|
+
throw new Error('未配置 ASR 密钥:可在 DSH「设置 → 插件 → 语音(dsh-voice)」直接填写 asrApiKey(保存即生效,无需重启),或设置环境变量 DSH_VOICE_ASR_KEY,或在 cordis.patch.yml 配置 asrApiKey。(注:文字转语音无需密钥即可直接使用。)');
|
|
36
36
|
if (baseUrl === '')
|
|
37
|
-
throw new Error('未配置 ASR
|
|
37
|
+
throw new Error('未配置 ASR 接口地址:asrEngine=custom 时必须在 DSH「设置 → 插件 → 语音」或 cordis.patch.yml 提供 asrBaseUrl(或切回 groq / openai 预设)。');
|
|
38
38
|
if (options.audio.length === 0)
|
|
39
39
|
throw new Error('音频文件为空。');
|
|
40
40
|
assertAudioSize(options.audio.length);
|
|
@@ -67,7 +67,7 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
|
|
|
67
67
|
throwIfAborted(abortSignal);
|
|
68
68
|
if (timeoutSignal.aborted)
|
|
69
69
|
throw new Error('ASR 请求超时,请重试或缩短音频。');
|
|
70
|
-
throw new Error('ASR 请求失败:' + (error instanceof Error ? error.message : String(error)) + '。若接口需要特殊代理(梯子),请在 cordis.patch.yml 配置 proxyUrl
|
|
70
|
+
throw new Error('ASR 请求失败:' + (error instanceof Error ? error.message : String(error)) + '。若接口需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」或 cordis.patch.yml 配置 proxyUrl(保存即生效)。');
|
|
71
71
|
}
|
|
72
72
|
if (response.status >= 300 && response.status < 400) {
|
|
73
73
|
try {
|
package/lib/tools.d.ts
CHANGED
|
@@ -31,6 +31,6 @@ export interface VoiceToolDeps {
|
|
|
31
31
|
stt?: typeof transcribe;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|
|
35
35
|
*/
|
|
36
|
-
export declare function buildVoiceTools(config: ResolvedVoiceConfig, deps?: VoiceToolDeps): VoiceToolDefinition[];
|
|
36
|
+
export declare function buildVoiceTools(config: ResolvedVoiceConfig | (() => ResolvedVoiceConfig), deps?: VoiceToolDeps): VoiceToolDefinition[];
|
package/lib/tools.js
CHANGED
|
@@ -137,12 +137,22 @@ export const DEFAULT_PREVIEW_TEXT = '你好,这是音色试听。Hello, this i
|
|
|
137
137
|
/** voice_preview 同时合成的音色数上限:有界并发,避免 8 个音色串行把最坏等待拖成 8 倍超时。 */
|
|
138
138
|
const PREVIEW_CONCURRENCY = 3;
|
|
139
139
|
/**
|
|
140
|
-
*
|
|
140
|
+
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|
|
141
141
|
*/
|
|
142
142
|
export function buildVoiceTools(config, deps = {}) {
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
|
|
143
|
+
const getCfg = typeof config === 'function' ? config : () => config;
|
|
144
|
+
const initialCfg = getCfg();
|
|
145
|
+
let cachedProxyUrl = '';
|
|
146
|
+
let cachedFetchImpl;
|
|
147
|
+
const getProxyFetch = (proxyUrl) => {
|
|
148
|
+
if (proxyUrl === '')
|
|
149
|
+
return undefined;
|
|
150
|
+
if (cachedFetchImpl === undefined || cachedProxyUrl !== proxyUrl) {
|
|
151
|
+
cachedProxyUrl = proxyUrl;
|
|
152
|
+
cachedFetchImpl = createProxyFetch(proxyUrl);
|
|
153
|
+
}
|
|
154
|
+
return cachedFetchImpl;
|
|
155
|
+
};
|
|
146
156
|
const voiceList = {
|
|
147
157
|
name: 'voice_list',
|
|
148
158
|
isConcurrencySafe: () => true,
|
|
@@ -183,6 +193,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
183
193
|
},
|
|
184
194
|
},
|
|
185
195
|
async execute(rawArgs, exec) {
|
|
196
|
+
const cfg = getCfg();
|
|
186
197
|
const signal = execSignal(exec);
|
|
187
198
|
throwIfAborted(signal);
|
|
188
199
|
const args = asRecord(rawArgs);
|
|
@@ -191,7 +202,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
191
202
|
throw new Error('文本超过 5000 字符,请分段合成。');
|
|
192
203
|
const voice = optionalString(args, 'voice') ?? cfg.ttsVoice;
|
|
193
204
|
if (!isValidVoiceId(voice))
|
|
194
|
-
throw new Error('音色 id 不合法:' + voice + '。请用 voice_list
|
|
205
|
+
throw new Error('音色 id 不合法:' + voice + '。请用 voice_list 查看常用音色,或在 DSH「设置 → 插件 → 语音」修改 ttsVoice(合法格式如 zh-CN-XiaoxiaoNeural)。');
|
|
195
206
|
const rate = optionalString(args, 'rate') ?? cfg.ttsRate;
|
|
196
207
|
const pitch = optionalString(args, 'pitch') ?? cfg.ttsPitch;
|
|
197
208
|
for (const [value, label] of [[rate, '语速 rate'], [pitch, '音调 pitch']]) {
|
|
@@ -200,12 +211,12 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
200
211
|
}
|
|
201
212
|
const requested = optionalString(args, 'output'), cwd = sessionCwd(exec);
|
|
202
213
|
resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd);
|
|
203
|
-
const audio = await raceAbort((deps.tts ?? synthesizeSpeech)({ text, voice, rate, pitch }, { proxyUrl: cfg.proxyUrl },
|
|
214
|
+
const audio = await raceAbort((deps.tts ?? synthesizeSpeech)({ text, voice, rate, pitch }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal), signal);
|
|
204
215
|
throwIfAborted(signal);
|
|
205
216
|
const output = writeWorkspaceFile(resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd), audio, cwd, undefined, cfg.overwrite);
|
|
206
217
|
return { output, bytes: audio.length, voice, rate, pitch, textLength: text.length };
|
|
207
218
|
},
|
|
208
|
-
timeoutMs:
|
|
219
|
+
timeoutMs: initialCfg.timeoutMs + 10000,
|
|
209
220
|
};
|
|
210
221
|
const voiceStt = {
|
|
211
222
|
name: 'voice_stt',
|
|
@@ -231,6 +242,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
231
242
|
},
|
|
232
243
|
},
|
|
233
244
|
async execute(rawArgs, exec) {
|
|
245
|
+
const cfg = getCfg();
|
|
234
246
|
const signal = execSignal(exec);
|
|
235
247
|
throwIfAborted(signal);
|
|
236
248
|
const args = asRecord(rawArgs);
|
|
@@ -241,12 +253,12 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
241
253
|
throw new Error('engine 必须是 groq / openai / custom 之一(当前:' + requestedEngine + ')。');
|
|
242
254
|
}
|
|
243
255
|
if (requestedEngine !== undefined && requestedEngine !== cfg.asrEngine) {
|
|
244
|
-
throw new Error('voice_stt 不支持用 engine 参数临时换引擎(配置 asrEngine=' + cfg.asrEngine + ',本次传入 ' + requestedEngine + '
|
|
256
|
+
throw new Error('voice_stt 不支持用 engine 参数临时换引擎(配置 asrEngine=' + cfg.asrEngine + ',本次传入 ' + requestedEngine + ')。接口地址与密钥是一组配置,混着用会把密钥发到非预期的服务;如需切换请在 DSH「设置 → 插件 → 语音」修改 asrEngine / asrBaseUrl(保存即生效)。');
|
|
245
257
|
}
|
|
246
258
|
// 接口地址一律取配置:openai/groq 预设已由 resolveConfig 回填,自定义端点原样使用。
|
|
247
259
|
const baseUrl = cfg.asrBaseUrl;
|
|
248
260
|
if (baseUrl === '')
|
|
249
|
-
throw new Error('未配置 ASR 接口地址:asrEngine=custom
|
|
261
|
+
throw new Error('未配置 ASR 接口地址:asrEngine=custom 时必须在 DSH「设置 → 插件 → 语音」或配置里提供 asrBaseUrl(保存即生效)。');
|
|
250
262
|
assertAudioSize(statSync(audioPath).size);
|
|
251
263
|
const audio = readFileSync(audioPath);
|
|
252
264
|
// 伪文件(如 procfs)stat 报 0 字节,按实际读到的长度再判一次。
|
|
@@ -258,7 +270,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
258
270
|
model,
|
|
259
271
|
language: optionalString(args, 'language'),
|
|
260
272
|
prompt: optionalString(args, 'prompt'),
|
|
261
|
-
},
|
|
273
|
+
}, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal), signal);
|
|
262
274
|
throwIfAborted(signal);
|
|
263
275
|
let transcriptFile = '';
|
|
264
276
|
const output = optionalString(args, 'output');
|
|
@@ -271,7 +283,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
271
283
|
// dsh rejects the whole tool result (issue #1).
|
|
272
284
|
return { text, model, language: optionalString(args, 'language') ?? '', audio: audioPath, transcriptFile };
|
|
273
285
|
},
|
|
274
|
-
timeoutMs:
|
|
286
|
+
timeoutMs: initialCfg.timeoutMs + 10000,
|
|
275
287
|
};
|
|
276
288
|
const voicePreview = {
|
|
277
289
|
name: 'voice_preview',
|
|
@@ -300,6 +312,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
300
312
|
},
|
|
301
313
|
},
|
|
302
314
|
async execute(rawArgs, exec) {
|
|
315
|
+
const cfg = getCfg();
|
|
303
316
|
const signal = execSignal(exec);
|
|
304
317
|
throwIfAborted(signal);
|
|
305
318
|
const args = asRecord(rawArgs);
|
|
@@ -320,7 +333,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
320
333
|
return { voice, error: '音色 id 不合法(应为 zh-CN-XXXNeural 形式的 edge 音色)' };
|
|
321
334
|
}
|
|
322
335
|
try {
|
|
323
|
-
const audio = await raceAbort((deps.tts ?? synthesizeSpeech)({ text, voice, rate: cfg.ttsRate, pitch: cfg.ttsPitch }, { proxyUrl: cfg.proxyUrl },
|
|
336
|
+
const audio = await raceAbort((deps.tts ?? synthesizeSpeech)({ text, voice, rate: cfg.ttsRate, pitch: cfg.ttsPitch }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal), signal);
|
|
324
337
|
throwIfAborted(signal);
|
|
325
338
|
const name = 'voice-preview-' + voice.replace(/[^a-zA-Z0-9-]/g, '_') + '.mp3';
|
|
326
339
|
// 复用 resolveOutputPath:overwrite=false 时改名加序号,不再直接覆盖同名样例。
|
|
@@ -343,7 +356,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
343
356
|
}
|
|
344
357
|
return { count: samples.length, samples, failed, text };
|
|
345
358
|
},
|
|
346
|
-
timeoutMs:
|
|
359
|
+
timeoutMs: initialCfg.timeoutMs + 10000,
|
|
347
360
|
};
|
|
348
361
|
const voiceHealth = {
|
|
349
362
|
name: 'voice_health',
|
|
@@ -371,6 +384,7 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
371
384
|
},
|
|
372
385
|
},
|
|
373
386
|
async execute() {
|
|
387
|
+
const cfg = getCfg();
|
|
374
388
|
const checks = [];
|
|
375
389
|
const hints = [];
|
|
376
390
|
let ok = true;
|
|
@@ -379,11 +393,11 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
379
393
|
name: 'TTS 音色',
|
|
380
394
|
ok: voiceOk,
|
|
381
395
|
detail: voiceOk ? cfg.ttsVoice : '不合法:' + cfg.ttsVoice + '(用 voice_list 查看)',
|
|
382
|
-
...(voiceOk ? {} : { hint: '运行 voice_list
|
|
396
|
+
...(voiceOk ? {} : { hint: '运行 voice_list 挑一个清单内音色,在 DSH「设置 → 插件 → 语音」修改 ttsVoice(保存即生效);合法 id 形如 zh-CN-XiaoxiaoNeural。' }),
|
|
383
397
|
});
|
|
384
398
|
if (!voiceOk) {
|
|
385
399
|
ok = false;
|
|
386
|
-
hints.push('TTS
|
|
400
|
+
hints.push('TTS 音色不合法:先用 voice_list 选音色,再在 DSH「设置 → 插件 → 语音」修改 ttsVoice(保存即生效);在此之前 voice_tts 调用会直接报错。');
|
|
387
401
|
}
|
|
388
402
|
checks.push({ name: '特殊代理', ok: true, detail: cfg.proxyUrl !== '' ? '已配置 ' + cfg.proxyUrl : '未配置' });
|
|
389
403
|
checks.push({ name: 'ASR 引擎', ok: true, detail: cfg.asrEngine });
|
|
@@ -392,22 +406,22 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
392
406
|
name: 'ASR 接口地址',
|
|
393
407
|
ok: baseUrlOk,
|
|
394
408
|
detail: baseUrlOk ? cfg.asrBaseUrl : '未配置:asrEngine=custom 时必须提供 asrBaseUrl。',
|
|
395
|
-
...(baseUrlOk ? {} : { hint: '
|
|
409
|
+
...(baseUrlOk ? {} : { hint: '在 DSH「设置 → 插件 → 语音」补上 asrBaseUrl(含 /v1 的 OpenAI 兼容地址,保存即生效),或改回 groq / openai 预设。' }),
|
|
396
410
|
});
|
|
397
411
|
if (!baseUrlOk) {
|
|
398
412
|
ok = false;
|
|
399
|
-
hints.push('ASR 接口地址缺失:只有 voice_stt 受影响,voice_tts / voice_list / voice_preview
|
|
413
|
+
hints.push('ASR 接口地址缺失:只有 voice_stt 受影响,voice_tts / voice_list / voice_preview 仍可零配置正常使用。');
|
|
400
414
|
}
|
|
401
415
|
const hasKey = cfg.asrApiKey !== '' || typeof process.env.DSH_VOICE_ASR_KEY === 'string' && process.env.DSH_VOICE_ASR_KEY !== '';
|
|
402
416
|
checks.push({
|
|
403
417
|
name: 'ASR 密钥',
|
|
404
418
|
ok: hasKey,
|
|
405
|
-
detail: hasKey ? '已配置' : '未配置:voice_stt 需要 DSH_VOICE_ASR_KEY
|
|
406
|
-
...(hasKey ? {} : { hint: '
|
|
419
|
+
detail: hasKey ? '已配置' : '未配置:voice_stt 需要 DSH_VOICE_ASR_KEY 环境变量或在「设置 → 插件 → 语音」配置 asrApiKey',
|
|
420
|
+
...(hasKey ? {} : { hint: '在 DSH「设置 → 插件 → 语音」填写 asrApiKey(自动脱敏、保存即生效),或设置环境变量 DSH_VOICE_ASR_KEY;密钥缺失只影响 voice_stt。' }),
|
|
407
421
|
});
|
|
408
422
|
if (!hasKey) {
|
|
409
423
|
ok = false;
|
|
410
|
-
hints.push('缺 ASR
|
|
424
|
+
hints.push('缺 ASR 密钥:仅 voice_stt 录音转写需要密钥;文字转语音(voice_tts / voice_list / voice_preview)无需任何密钥,现在即可直接使用。');
|
|
411
425
|
}
|
|
412
426
|
return { ok, plugin: 'dsh-voice', checks, ...(hints.length > 0 ? { hints } : {}) };
|
|
413
427
|
},
|