@oadank/dsh-input-tools 0.3.3 → 0.3.4

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/lib/index.js CHANGED
@@ -20,7 +20,7 @@
20
20
  */
21
21
 
22
22
  import { createHash, randomUUID } from 'node:crypto'
23
- import { mkdir, open, readFile, unlink, writeFile } from 'node:fs/promises'
23
+ import { mkdir, open, readFile, unlink, writeFile, copyFile } from 'node:fs/promises'
24
24
  import { constants, readFileSync } from 'node:fs'
25
25
  import { homedir } from 'node:os'
26
26
  import { fileURLToPath } from 'node:url'
@@ -34,6 +34,44 @@ const inject = ['tools', 'webServer']
34
34
 
35
35
  export { name, inject }
36
36
 
37
+ // ──────────────────────────────────────────────────────────────
38
+ // [0.3.4] 自带素材(下载即用):克隆样本 + VoiceDesign 示例音频打进 npm 包 assets/,
39
+ // 首次加载自动拷贝到 DSH_HOME 并注册,不再依赖"手动上传/在线生成"。
40
+ // ──────────────────────────────────────────────────────────────
41
+ const PLUGIN_ROOT = join(fileURLToPath(import.meta.url), '..', '..') // .../dsh-input-tools
42
+ const ASSETS_DIR = join(PLUGIN_ROOT, 'assets')
43
+ const BUNDLED_CLONE_ID = '8da38fcc-b041-4f5b-86b9-901956016f89'
44
+ const BUNDLED_CLONE_SAMPLE = {
45
+ id: BUNDLED_CLONE_ID,
46
+ name: '小团团(60秒长样本)',
47
+ context: '一个魔性的少女萝莉音,说话自带沙雕搞怪和无厘头气质,像在撒娇又像在耍宝,情绪起伏很大:前一句还奶声奶气地撒娇卖萌,后一句就突然拔高音量夸张卖惨耍赖,再下一秒又贱兮兮地坏笑。尾音拖长上扬,带着气音和魔性笑声,喜欢用「臭猪」「你凶我」「哼」「嘿嘿嘿」这类咋咋呼呼的用词,语速忽快忽慢、节奏跳跃,吐字软糯清晰,傻白甜又可爱,让人听了忍不住想笑',
48
+ }
49
+ const VOICE_DESIGN_SAMPLE_KEYS = ['asmr', 'docu', 'elder']
50
+
51
+ let bundledInitDone = false
52
+ /** 首次加载把自带素材落地到 DSH_HOME:克隆样本 mp3 拷贝 + 首次安装自动注册小团团。 */
53
+ async function ensureBundledAssets(config, parsed) {
54
+ if (bundledInitDone) return config
55
+ bundledInitDone = true
56
+ try {
57
+ const homeDir = process.env.DSH_HOME ?? join(homedir(), '.dsh')
58
+ const cloneDir = join(homeDir, 'voiceclone-samples')
59
+ const dstClone = join(cloneDir, BUNDLED_CLONE_ID + '.mp3')
60
+ try {
61
+ await mkdir(cloneDir, { recursive: true })
62
+ await copyFile(join(ASSETS_DIR, 'voiceclone-samples', BUNDLED_CLONE_ID + '.mp3'), dstClone)
63
+ } catch { /* 包内素材缺失或拷贝失败:跳过(不阻塞启动) */ }
64
+ // 仅"首次安装"(配置里还没有 voiceclone 键)时注册自带样本;用户删光的 [] 不强制
65
+ const parsedHasClone = parsed !== null && typeof parsed === 'object' && parsed.engines?.voiceclone !== undefined
66
+ const samples = config?.engines?.voiceclone?.samples
67
+ if (!parsedHasClone && (!Array.isArray(samples) || samples.length === 0)) {
68
+ config.engines.voiceclone = { ...config.engines.voiceclone, enabled: true, samples: [{ ...BUNDLED_CLONE_SAMPLE, path: dstClone }] }
69
+ await saveVoiceConfig(config)
70
+ }
71
+ } catch { /* 初始化失败不阻塞 */ }
72
+ return config
73
+ }
74
+
37
75
  // ──────────────────────────────────────────────────────────────
38
76
  // 配置中心:~/.dsh/voice-config.json
39
77
  // ──────────────────────────────────────────────────────────────
@@ -95,6 +133,8 @@ async function loadVoiceConfig() {
95
133
  parsed = JSON.parse(await readFile(CONFIG_PATH, 'utf8'))
96
134
  } catch { /* 首次无配置 */ }
97
135
  cachedConfig = deepMerge(defaultVoiceConfig(), parsed)
136
+ // [0.3.4] 自带素材初始化(拷贝克隆样本 + 首次安装自动注册小团团)
137
+ await ensureBundledAssets(cachedConfig, parsed)
98
138
  // 环境变量覆盖(兼容旧配置;显式配置值优先于 env)
99
139
  const env = process.env
100
140
  if (env.TTS_XIAOMI_KEY !== undefined && cachedConfig.engines.xiaomi.apiKey === '') cachedConfig.engines.xiaomi.apiKey = env.TTS_XIAOMI_KEY
@@ -961,8 +1001,8 @@ async function apply(ctx) {
961
1001
  return sendJson(res, 404, { ok: false, error: '安装脚本不存在' })
962
1002
  }
963
1003
  }
964
- // VoiceDesign 官方示例音频:用真实的 MiMo-V2.5-TTS-VoiceDesign 模型生成(instruct=音色描述 / text=正文),缓存到 ~/.dsh/voice-design-samples/
965
- // 注意:必须走 voicedesign 模型(user=音色描述,无 voice 参数)才是真音色,绝不能降级到 edge 假造
1004
+ // VoiceDesign 官方示例音频:[0.3.4] 优先读插件包自带素材(assets/,mp3 下载即用),
1005
+ // 包内缺失才回退到"小米模型在线生成 + 缓存到 ~/.dsh/voice-design-samples/ wav"。
966
1006
  if (url.pathname === '/asr/voice-design-samples' && req.method === 'GET') {
967
1007
  const cfg = await loadVoiceConfig()
968
1008
  const homeDir = process.env.DSH_HOME ?? join(homedir(), '.dsh')
@@ -986,20 +1026,29 @@ async function apply(ctx) {
986
1026
  ]
987
1027
  const results = []
988
1028
  for (const s of samples) {
989
- const wavPath = join(sampleDir, `${s.key}.wav`)
990
- let wavBytes = null
991
- try { wavBytes = await readFile(wavPath) } catch { /* 首次/缓存失效用真实模型重新生成 */ }
992
- if (wavBytes === null) {
1029
+ // 1) 包内自带 mp3(首选,下载即用)
1030
+ let bytes = null
1031
+ try { bytes = await readFile(join(ASSETS_DIR, 'voice-design-samples', `${s.key}.mp3`)) } catch { /* 包内无回退 */ }
1032
+ let mediaType = 'audio/mpeg'
1033
+ if (bytes === null) {
1034
+ // 2) 缓存 wav(在线生成过)
1035
+ const wavPath = join(sampleDir, `${s.key}.wav`)
1036
+ try { bytes = await readFile(wavPath) } catch { /* 继续回退 */ }
1037
+ mediaType = 'audio/wav'
1038
+ }
1039
+ if (bytes === null) {
1040
+ // 3) 在线生成(key 缺失会失败,示例跳过)
993
1041
  try {
994
1042
  const syn = await synthesizeXiaomiVoiceDesign(s.text, { context: s.instruct }, cfg)
995
1043
  if (syn === null) throw new Error('voicedesign synth failed')
996
- wavBytes = Buffer.from(syn.data)
1044
+ bytes = Buffer.from(syn.data)
1045
+ mediaType = 'audio/wav'
997
1046
  await mkdir(sampleDir, { recursive: true })
998
- await writeFile(wavPath, wavBytes)
999
- } catch { /* 模型生成失败(如 key 缺失)→ 该示例跳过,绝不降级假音频 */ }
1047
+ await writeFile(join(sampleDir, `${s.key}.wav`), bytes)
1048
+ } catch { /* 模型生成失败 跳过 */ }
1000
1049
  }
1001
- if (wavBytes !== null) {
1002
- results.push({ key: s.key, title: s.title, mediaType: 'audio/wav', data: wavBytes.toString('base64') })
1050
+ if (bytes !== null) {
1051
+ results.push({ key: s.key, title: s.title, mediaType, data: bytes.toString('base64') })
1003
1052
  }
1004
1053
  }
1005
1054
  return sendJson(res, 200, { ok: results.length > 0, samples: results })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oadank/dsh-input-tools",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -13,6 +13,7 @@
13
13
  "files": [
14
14
  "lib",
15
15
  "scripts",
16
+ "assets",
16
17
  "README.md",
17
18
  "cordis.patch.yml"
18
19
  ],
@@ -168,7 +168,7 @@ const server = http.createServer(async (req, res) => {
168
168
  res.writeHead(404); res.end();
169
169
  }
170
170
  });
171
- server.listen(PORT, () => console.log('[ASR] listening on ' + PORT));
171
+ server.listen(PORT, '127.0.0.1', () => console.log('[ASR] listening on 127.0.0.1:' + PORT));
172
172
  "@
173
173
  # 服务脚本用 .cjs:插件包 package.json 声明了 "type":"module",.js 会被当 ESM 解析导致 require 报错
174
174
  # (2026-08-21 XDN 实测:asr-service.js 抛 ReferenceError: require is not defined)