dsh-voice 0.9.0 → 0.10.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 +18 -0
- package/README.en.md +23 -21
- package/README.md +23 -21
- package/docs/USAGE.en.md +64 -63
- package/docs/USAGE.md +64 -63
- package/lib/client.js +720 -146
- package/lib/edge-tts.d.ts +11 -2
- package/lib/edge-tts.js +384 -124
- package/lib/index.d.ts +9 -10
- package/lib/index.js +78 -1
- package/lib/stt.d.ts +4 -0
- package/lib/stt.js +19 -0
- package/lib/tools.d.ts +2 -1
- package/lib/tools.js +329 -48
- package/lib/tts-text.d.ts +77 -4
- package/lib/tts-text.js +497 -8
- package/lib/web.d.ts +5 -2
- package/lib/web.js +626 -112
- package/package.json +1 -1
- package/skills/voice-workflow/SKILL.md +81 -55
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-voice —— 语音双件套工具插件(node 半身,配置走 cordis.patch.yml)。
|
|
3
|
-
*
|
|
4
|
-
* 插件导出 apply(ctx, config):注册五个面向模型的工具(voice_tts / voice_stt /
|
|
5
|
-
* voice_list / voice_preview / voice_health)。TTS 走 edge-tts 协议(原生 WebSocket,
|
|
6
|
-
* 零 API 成本);STT 走 OpenAI 兼容 ASR 接口(Groq/OpenAI/自定义端点)。
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-voice
|
|
9
|
-
*/
|
|
10
1
|
import { type VoiceConfig } from './config.js';
|
|
11
2
|
import { type VoiceToolDefinition } from './tools.js';
|
|
12
3
|
/** cordis 服务注入:apply 里要用 ctx.tools,必须显式声明。 */
|
|
13
4
|
export declare const Config: import("./host-config.js").HostSchemaNode<VoiceConfig>;
|
|
14
5
|
export declare const name = "voice";
|
|
15
6
|
export declare const inject: string[];
|
|
7
|
+
/** 随包分发的技能目录绝对路径。 */
|
|
8
|
+
export declare function bundledSkillsDir(): string;
|
|
9
|
+
/** 解析 SKILL.md 的 YAML frontmatter(零外部依赖)。 */
|
|
10
|
+
export declare function parseSkillFile(text: string): {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
content: string;
|
|
14
|
+
};
|
|
16
15
|
/** 插件所需的最小 ctx 面。 */
|
|
17
16
|
export interface VoicePluginContext {
|
|
18
17
|
logger?: {
|
|
@@ -26,7 +25,7 @@ export interface VoicePluginContext {
|
|
|
26
25
|
on?(event: string, listener: () => void): () => void;
|
|
27
26
|
}
|
|
28
27
|
/**
|
|
29
|
-
*
|
|
28
|
+
* 插件入口:解析配置、注册五个语音工具、按需注入运行时工作流技能并挂载 Voice Studio Web 路由。
|
|
30
29
|
*/
|
|
31
30
|
export declare function apply(ctx: VoicePluginContext, config?: VoiceConfig | null): void;
|
|
32
31
|
export * from './config.js';
|
package/lib/index.js
CHANGED
|
@@ -8,6 +8,9 @@ import { registerUpdates } from '@stardustlc/dsh-updates';
|
|
|
8
8
|
*
|
|
9
9
|
* @module dsh-voice
|
|
10
10
|
*/
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
11
14
|
import { resolveConfig } from './config.js';
|
|
12
15
|
import { Config as VoiceConfigSchema, liveConfig } from './host-config.js';
|
|
13
16
|
import { buildVoiceTools } from './tools.js';
|
|
@@ -16,8 +19,81 @@ import { installVoiceStudioWeb } from './web.js';
|
|
|
16
19
|
export const Config = VoiceConfigSchema;
|
|
17
20
|
export const name = 'voice';
|
|
18
21
|
export const inject = ['tools'];
|
|
22
|
+
/** 随包分发的技能目录绝对路径。 */
|
|
23
|
+
export function bundledSkillsDir() {
|
|
24
|
+
return fileURLToPath(new URL('../skills/', import.meta.url));
|
|
25
|
+
}
|
|
26
|
+
function unquoteYamlScalar(value) {
|
|
27
|
+
const trimmed = value.trim();
|
|
28
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
29
|
+
return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\n/g, '\n').trim();
|
|
30
|
+
}
|
|
31
|
+
return trimmed;
|
|
32
|
+
}
|
|
33
|
+
/** 解析 SKILL.md 的 YAML frontmatter(零外部依赖)。 */
|
|
34
|
+
export function parseSkillFile(text) {
|
|
35
|
+
const normalized = text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
|
|
36
|
+
const match = /^---[ \t]*\n([\s\S]*?)\n---[ \t]*\n?/.exec(normalized);
|
|
37
|
+
if (!match)
|
|
38
|
+
return { name: '', description: '', content: normalized };
|
|
39
|
+
let skillName = '';
|
|
40
|
+
let description = '';
|
|
41
|
+
for (const rawLine of match[1].split('\n')) {
|
|
42
|
+
const keyMatch = /^([A-Za-z][A-Za-z0-9_-]*):[ \t]*(.*)$/.exec(rawLine.trim());
|
|
43
|
+
if (!keyMatch)
|
|
44
|
+
continue;
|
|
45
|
+
if (keyMatch[1] === 'name')
|
|
46
|
+
skillName = unquoteYamlScalar(keyMatch[2]);
|
|
47
|
+
else if (keyMatch[1] === 'description')
|
|
48
|
+
description = unquoteYamlScalar(keyMatch[2]);
|
|
49
|
+
}
|
|
50
|
+
return { name: skillName, description, content: normalized.slice(match[0].length).trimStart() };
|
|
51
|
+
}
|
|
52
|
+
function installVoiceWorkflowSkill(ctx, warn, disposers) {
|
|
53
|
+
if (typeof ctx.inject !== 'function')
|
|
54
|
+
return;
|
|
55
|
+
try {
|
|
56
|
+
ctx.inject(['skills'], (skillCtx) => {
|
|
57
|
+
if (typeof skillCtx?.skills?.register !== 'function')
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
const dir = join(bundledSkillsDir(), 'voice-workflow');
|
|
61
|
+
const parsed = parseSkillFile(readFileSync(join(dir, 'SKILL.md'), 'utf8'));
|
|
62
|
+
if (!parsed.name || !parsed.description || !parsed.content) {
|
|
63
|
+
throw new Error('voice-workflow 的 frontmatter 不完整');
|
|
64
|
+
}
|
|
65
|
+
const off = skillCtx.skills.register({
|
|
66
|
+
name: parsed.name,
|
|
67
|
+
description: parsed.description,
|
|
68
|
+
content: parsed.content,
|
|
69
|
+
resourceBase: { kind: 'directory', path: dir },
|
|
70
|
+
source: 'runtime',
|
|
71
|
+
});
|
|
72
|
+
if (typeof off === 'function') {
|
|
73
|
+
let cleaned = false;
|
|
74
|
+
const cleanup = () => {
|
|
75
|
+
if (cleaned)
|
|
76
|
+
return;
|
|
77
|
+
cleaned = true;
|
|
78
|
+
off();
|
|
79
|
+
};
|
|
80
|
+
disposers.push(cleanup);
|
|
81
|
+
if (typeof skillCtx.effect === 'function') {
|
|
82
|
+
skillCtx.effect(() => cleanup, 'dsh-voice: voice-workflow skill');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
warn('[dsh-voice] 技能加载失败:' + (error instanceof Error ? error.message : String(error)));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
warn('[dsh-voice] 技能注入未启用:' + (error instanceof Error ? error.message : String(error)));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
19
95
|
/**
|
|
20
|
-
*
|
|
96
|
+
* 插件入口:解析配置、注册五个语音工具、按需注入运行时工作流技能并挂载 Voice Studio Web 路由。
|
|
21
97
|
*/
|
|
22
98
|
export function apply(ctx, config) {
|
|
23
99
|
registerUpdates(ctx, { name: "dsh-voice" });
|
|
@@ -43,6 +119,7 @@ export function apply(ctx, config) {
|
|
|
43
119
|
for (const definition of buildVoiceTools(readCfg)) {
|
|
44
120
|
disposers.push(ctx.tools.register(definition));
|
|
45
121
|
}
|
|
122
|
+
installVoiceWorkflowSkill(ctx, warn, disposers);
|
|
46
123
|
installVoiceStudioWeb(ctx, readCfg);
|
|
47
124
|
const teardown = () => {
|
|
48
125
|
for (const dispose of disposers)
|
package/lib/stt.d.ts
CHANGED
|
@@ -39,6 +39,10 @@ export interface OversizeAudioPreparation {
|
|
|
39
39
|
chunks: PreparedAudioChunk[];
|
|
40
40
|
cleanup: () => void;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* 判断文件路径是否为常见视频封装容器(故意不含 `.webm`,确保浏览器麦克风录音无需本地 ffmpeg 即可直接转写)。
|
|
44
|
+
*/
|
|
45
|
+
export declare function isVideoContainerFile(filePath: string): boolean;
|
|
42
46
|
/** 字节数超过 25MB 时抛错:调用方可在读文件前先按 statSync 大小快速拒绝,避免白读整文件。 */
|
|
43
47
|
export declare function assertAudioSize(bytes: number): void;
|
|
44
48
|
/** 传给 transcribe 的 fetch:可以像代理那样一并带上自己的 FormData/Blob 构造器。 */
|
package/lib/stt.js
CHANGED
|
@@ -11,6 +11,25 @@ import { isLoopbackBaseUrl } from './config.js';
|
|
|
11
11
|
const MAX_AUDIO_BYTES = 25 * 1024 * 1024;
|
|
12
12
|
const COMPRESSED_CHUNK_LIMIT_BYTES = 24 * 1024 * 1024;
|
|
13
13
|
const SEGMENT_DURATION_SECONDS = 1200;
|
|
14
|
+
const VIDEO_CONTAINER_EXTENSIONS = new Set([
|
|
15
|
+
'.mp4',
|
|
16
|
+
'.mkv',
|
|
17
|
+
'.mov',
|
|
18
|
+
'.avi',
|
|
19
|
+
'.flv',
|
|
20
|
+
'.wmv',
|
|
21
|
+
'.m4v',
|
|
22
|
+
'.ts',
|
|
23
|
+
'.mts',
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* 判断文件路径是否为常见视频封装容器(故意不含 `.webm`,确保浏览器麦克风录音无需本地 ffmpeg 即可直接转写)。
|
|
27
|
+
*/
|
|
28
|
+
export function isVideoContainerFile(filePath) {
|
|
29
|
+
if (typeof filePath !== 'string' || filePath.trim() === '')
|
|
30
|
+
return false;
|
|
31
|
+
return VIDEO_CONTAINER_EXTENSIONS.has(extname(filePath.trim()).toLowerCase());
|
|
32
|
+
}
|
|
14
33
|
/** 字节数超过 25MB 时抛错:调用方可在读文件前先按 statSync 大小快速拒绝,避免白读整文件。 */
|
|
15
34
|
export function assertAudioSize(bytes) {
|
|
16
35
|
if (bytes > MAX_AUDIO_BYTES) {
|
package/lib/tools.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type ResolvedVoiceConfig } from './config.js';
|
|
|
2
2
|
import { fetchEdgeVoices, synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
|
|
3
3
|
import { synthesizeOpenAiSpeech } from './openai-tts.js';
|
|
4
4
|
import { prepareAudioForAsr, splitStereoChannelsForDiarization, transcribe } from './stt.js';
|
|
5
|
-
import { mixAudioWithBgm, normalizeAudioLoudness } from './tts-text.js';
|
|
5
|
+
import { fitAudioToWindow, mixAudioWithBgm, normalizeAudioLoudness } from './tts-text.js';
|
|
6
6
|
/** 模型可见的内容块。 */
|
|
7
7
|
export interface ContentBlock {
|
|
8
8
|
type: 'text';
|
|
@@ -38,6 +38,7 @@ export interface VoiceToolDeps {
|
|
|
38
38
|
normalizeAudio?: typeof normalizeAudioLoudness;
|
|
39
39
|
mixBgm?: typeof mixAudioWithBgm;
|
|
40
40
|
splitStereo?: typeof splitStereoChannelsForDiarization;
|
|
41
|
+
fitWindow?: typeof fitAudioToWindow;
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
43
44
|
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|