@eslaykey/dsh-breeze-tts 0.0.0-stage → 1.0.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/index.js ADDED
@@ -0,0 +1,1011 @@
1
+ import { spawn } from 'node:child_process'
2
+ import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises'
3
+ import { homedir } from 'node:os'
4
+ import path from 'node:path'
5
+ import { createHash, randomUUID } from 'node:crypto'
6
+
7
+ const API_BASE = 'https://api.breeze.blue/v1'
8
+ const ROUTE_PREFIX = '/breeze-tts'
9
+ const SETTINGS_PATH = path.join(homedir(), '.dsh', 'breeze.json')
10
+ const CACHE_DIR = path.join(homedir(), '.dsh', 'breeze-tts')
11
+
12
+ const VOICE_PRESETS = [
13
+ { voiceId: 'voc_rc894j8q2auu', name: '知绘小姐', tag: '女 · 年轻 · 明亮干脆', gender: 'female' },
14
+ { voiceId: 'voc_reys5un5t4ed', name: '深思先生', tag: '男 · 成年 · 标准普通话', gender: 'male' },
15
+ { voiceId: 'voc_u3nxkd5578wc', name: '大肥鱼(机灵版)', tag: '女 · 少女 · 活泼', gender: 'female' },
16
+ { voiceId: 'voc_8yud7hz7c9q2', name: '大肥鱼(迷糊版)', tag: '女 · 少女 · 慵懒', gender: 'female' },
17
+ ]
18
+
19
+ const DEFAULT_TONES = [
20
+ { id: 'normal', name: '自然', instructions: '像平时聊天一样自然地说话,语速适中,不要播音腔。', builtin: true },
21
+ { id: 'warm', name: '温和', instructions: '语气温和亲切,语速稍慢,句尾柔和,像在耐心安慰对方。', builtin: true },
22
+ { id: 'formal', name: '正式', instructions: '语气正式清晰,吐字清楚,节奏平稳,像在做正式说明。', builtin: true },
23
+ { id: 'lively', name: '活泼', instructions: '语气轻快有活力,语速稍快,句尾上扬,带一点兴奋。', builtin: true },
24
+ ]
25
+
26
+ const DEFAULTS = {
27
+ voiceId: 'voc_rc894j8q2auu',
28
+ savedVoices: [],
29
+ modelId: 'breeze-tts-2',
30
+ languageCode: 'zh',
31
+ languageMode: 'auto',
32
+ dailyChars: 1500,
33
+ previewText: '你好,这是一段语音试听。',
34
+ moodId: 'normal',
35
+ customMood: '',
36
+ emotionMode: 'fixed',
37
+ autoSpeak: false,
38
+ autoSpeakMaxChars: 120,
39
+ backgroundPlay: false,
40
+ tones: DEFAULT_TONES,
41
+ }
42
+
43
+ export const inject = ['tools', 'webServer']
44
+
45
+ function parseJson(text) {
46
+ try {
47
+ return JSON.parse(text)
48
+ } catch {
49
+ return null
50
+ }
51
+ }
52
+
53
+ async function readStore() {
54
+ try {
55
+ const raw = await readFile(SETTINGS_PATH, 'utf8')
56
+ const trimmed = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw
57
+ return parseJson(trimmed) ?? {}
58
+ } catch {
59
+ return {}
60
+ }
61
+ }
62
+
63
+ async function writeStore(patch) {
64
+ const current = await readStore()
65
+ const next = { ...current, ...patch }
66
+ await mkdir(path.dirname(SETTINGS_PATH), { recursive: true })
67
+ await writeFile(SETTINGS_PATH, JSON.stringify(next, null, 2), 'utf8')
68
+ return next
69
+ }
70
+
71
+ function usageKey() {
72
+ const now = new Date()
73
+ const month = String(now.getMonth() + 1).padStart(2, '0')
74
+ const day = String(now.getDate()).padStart(2, '0')
75
+ return `${now.getFullYear()}-${month}-${day}`
76
+ }
77
+
78
+ function creditsOf(chars) {
79
+ return Math.round(chars * 0.19 * 10) / 10
80
+ }
81
+
82
+ const CJK = /[\u3400-\u4DBF\u4E00-\u9FFF\u3040-\u30FF\uAC00-\uD7AF]/g
83
+ const LATIN = /[A-Za-z]/g
84
+
85
+ const SENTENCE_MARKS = [0x3002, 0xFF01, 0xFF1F, 0x2026, 0xFF1B, 0x21, 0x3F, 0x3B, 0x0A].map((code) => String.fromCharCode(code)).join('')
86
+
87
+ function countMatches(text, pattern) {
88
+ const found = String(text).match(pattern)
89
+ return found ? found.length : 0
90
+ }
91
+
92
+ function stripEmoji(text) {
93
+ let out = ''
94
+ for (const ch of String(text ?? '')) {
95
+ const cp = ch.codePointAt(0)
96
+ if (cp >= 0x1f000 && cp <= 0x1faff) continue
97
+ if (cp >= 0x2600 && cp <= 0x27bf) continue
98
+ if (cp === 0xfe0f || cp === 0x200d || cp === 0x2b50) continue
99
+ out += ch
100
+ }
101
+ return out
102
+ }
103
+
104
+ function cleanSpeech(text, keepTags) {
105
+ let value = String(text ?? '')
106
+ if (!keepTags) value = value.replace(/<!--[\s\S]*?-->/g, ' ')
107
+ value = value.replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
108
+ value = value.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
109
+ value = value.replace(/https?:\/\/[^\s)]+/g, ' ')
110
+ value = value.replace(/`{1,3}[^`]*`{1,3}/g, ' ')
111
+ value = value.replace(/[*_~#>|]+/g, ' ')
112
+ value = stripEmoji(value)
113
+ value = value.replace(/\s+([:,。!?;、)】」》])/g, '$1')
114
+ value = value.replace(/([(【「《])\s+/g, '$1')
115
+ value = value.replace(/\s+/g, ' ')
116
+ return value.trim()
117
+ }
118
+
119
+ function detectLanguage(text) {
120
+ const cjk = countMatches(text, CJK)
121
+ const latin = countMatches(text, LATIN)
122
+ if (cjk === 0) return latin > 0 ? "en" : ""
123
+ return cjk * 2 >= latin ? "zh" : "en"
124
+ }
125
+
126
+ function splitSentences(text) {
127
+ const value = String(text ?? '').trim()
128
+ if (!value) return []
129
+ const parts = []
130
+ let buffer = ''
131
+ for (let index = 0; index < value.length; index += 1) {
132
+ const ch = value[index]
133
+ buffer += ch
134
+ const next = value[index + 1]
135
+ const asciiStop = ch === '.' && (next === undefined || next === ' ' || next === String.fromCharCode(10) || next === String.fromCharCode(9) || next === '"' || next === "'" || next === ')' || next === ']')
136
+ if (SENTENCE_MARKS.includes(ch) || asciiStop) {
137
+ parts.push(buffer)
138
+ buffer = ''
139
+ }
140
+ }
141
+ if (buffer.trim()) parts.push(buffer)
142
+ return parts.map((part) => part.trim()).filter(Boolean)
143
+ }
144
+
145
+ const EMOTIONS = {
146
+ curious: 'TONE_CURIOUS',
147
+ excited: 'TONE_EXCITED',
148
+ sad: 'TONE_SAD',
149
+ gentle: 'TONE_GENTLE',
150
+ }
151
+
152
+ function emotionInstruction(emotion) {
153
+ if (emotion === 'curious') return '语气带着好奇和疑问,句尾略微上扬。'
154
+ if (emotion === 'excited') return '语气兴奋,语速稍快,音调偏高。'
155
+ if (emotion === 'happy') return '语气愉快,带着笑意,句尾轻轻上扬。'
156
+ if (emotion === 'angry') return '语气生气,音量略高,语速偏快,带着一点压迫感。'
157
+ if (emotion === 'down') return '语气沮丧,声音发闷,语速放慢。'
158
+ if (emotion === 'sad') return '语气低落,语速放慢,尾音下沉。'
159
+ if (emotion === 'whisper') return '压低声音像耳语,气息多,音量小。'
160
+ if (emotion === 'surprised') return '语气惊讶,语调突然抬高,语速略快。'
161
+ if (emotion === 'afraid') return '语气带着紧张和不安,声音发抖,语速不稳。'
162
+ if (emotion === 'serious') return '语气严肃克制,吐字清楚,节奏稳定。'
163
+ if (emotion === 'tired') return '语气疲惫,声音有气无力,语速偏慢。'
164
+ if (emotion === 'gentle') return '语气轻柔,像在轻声安慰对方。'
165
+ return ''
166
+ }
167
+
168
+ function guessEmotion(sentence) {
169
+ const value = String(sentence).trim()
170
+ if (!value) return ''
171
+ if (/[??]\s*$/.test(value)) return 'curious'
172
+ if (/[!!]\s*$/.test(value)) return 'excited'
173
+ if (/(…|\.\.\.)\s*$/.test(value)) return 'sad'
174
+ return ''
175
+ }
176
+
177
+ function splitByMarker(text) {
178
+ const parts = []
179
+ const pattern = /<!--\s*mood:\s*([a-z-]+)\s*-->/gi
180
+ let lastIndex = 0
181
+ let current = ''
182
+ let match = pattern.exec(text)
183
+ while (match) {
184
+ const before = text.slice(lastIndex, match.index)
185
+ if (before.trim()) parts.push({ text: before, emotion: current })
186
+ current = String(match[1] || '').toLowerCase()
187
+ lastIndex = match.index + match[0].length
188
+ match = pattern.exec(text)
189
+ }
190
+ const tail = text.slice(lastIndex)
191
+ if (tail.trim()) parts.push({ text: tail, emotion: current })
192
+ return parts
193
+ }
194
+
195
+ function planSegments(text, mode, baseInstructions) {
196
+ const sentences = splitSentences(text)
197
+ const segments = []
198
+ let pendingEmotion = ''
199
+
200
+ let carry = ''
201
+ for (const sentence of sentences) {
202
+ let emotion = mode === 'infer' ? guessEmotion(sentence) : ''
203
+ if (!emotion && carry && sentence.length <= 40) emotion = carry
204
+ if (emotion) carry = emotion
205
+ const instructions = emotion ? emotionInstruction(emotion) : baseInstructions
206
+ const last = segments[segments.length - 1]
207
+ if (last && last.instructions === instructions && last.text.length + sentence.length <= 900) {
208
+ last.text += sentence
209
+ continue
210
+ }
211
+ segments.push({ text: sentence, instructions, emotion: emotion || pendingEmotion })
212
+ }
213
+ if (mode === 'marker') {
214
+ const parts = splitByMarker(text)
215
+ if (parts.length > 1) {
216
+ const merged = []
217
+ for (const part of parts) {
218
+ const piece = cleanSpeech(part.text)
219
+ if (!piece) continue
220
+ const instructions = part.emotion ? (emotionInstruction(part.emotion) || baseInstructions) : baseInstructions
221
+ const last = merged[merged.length - 1]
222
+ if (last && last.instructions === instructions && last.text.length + piece.length <= 900) {
223
+ last.text += piece
224
+ continue
225
+ }
226
+ merged.push({ text: piece, instructions, emotion: part.emotion })
227
+ }
228
+ return merged
229
+ }
230
+ }
231
+ return segments
232
+ }
233
+
234
+ function chunkByLanguage(text, limit = 900) {
235
+ const chunks = []
236
+ for (const part of splitSentences(text)) {
237
+ const lang = detectLanguage(part)
238
+ const last = chunks[chunks.length - 1]
239
+ if (last && last.lang === lang && last.text.length + part.length <= limit) {
240
+ last.text += part
241
+ continue
242
+ }
243
+ chunks.push({ text: part, lang })
244
+ }
245
+ return chunks
246
+ }
247
+
248
+ function concatWav(buffers) {
249
+ let fmt = null
250
+ const frames = []
251
+ for (const buffer of buffers) {
252
+ if (buffer.toString('ascii', 0, 4) !== 'RIFF') throw new Error('not a wav payload')
253
+ let pos = 12
254
+ let localFmt = null
255
+ while (pos + 8 <= buffer.length) {
256
+ const tag = buffer.toString('ascii', pos, pos + 4)
257
+ const size = buffer.readUInt32LE(pos + 4)
258
+ const body = buffer.subarray(pos + 8, pos + 8 + size)
259
+ if (tag === 'fmt ') localFmt = body
260
+ else if (tag === 'data') frames.push(body)
261
+ pos += 8 + size + (size % 2)
262
+ }
263
+ if (!localFmt) throw new Error('wav without fmt chunk')
264
+ if (!fmt) fmt = localFmt
265
+ else if (!fmt.equals(localFmt)) throw new Error('wav format mismatch')
266
+ }
267
+ const body = Buffer.concat(frames)
268
+ const header = Buffer.alloc(12)
269
+ header.write('RIFF', 0, 'ascii')
270
+ header.writeUInt32LE(4 + (8 + fmt.length) + (8 + body.length), 4)
271
+ header.write('WAVE', 8, 'ascii')
272
+ const fmtChunk = Buffer.alloc(8)
273
+ fmtChunk.write('fmt ', 0, 'ascii')
274
+ fmtChunk.writeUInt32LE(fmt.length, 4)
275
+ const dataChunk = Buffer.alloc(8)
276
+ dataChunk.write('data', 0, 'ascii')
277
+ dataChunk.writeUInt32LE(body.length, 4)
278
+ return Buffer.concat([header, fmtChunk, fmt, dataChunk, body])
279
+ }
280
+
281
+ async function callApi(pathname, options = {}) {
282
+ const { method = 'GET', body, query, apiKey, binary = false } = options
283
+ const url = new URL(API_BASE + pathname)
284
+ for (const [key, value] of Object.entries(query ?? {})) {
285
+ if (value !== undefined && value !== null && value !== '') url.searchParams.set(key, String(value))
286
+ }
287
+ const headers = { accept: 'application/json', 'user-agent': 'dsh-breeze-tts/1.0' }
288
+ if (apiKey) headers['xi-api-key'] = apiKey
289
+ const isForm = typeof FormData !== 'undefined' && body instanceof FormData
290
+ if (body !== undefined && !isForm) headers['content-type'] = 'application/json'
291
+ const response = await fetch(url, {
292
+ method,
293
+ headers,
294
+ body: body === undefined ? undefined : isForm ? body : JSON.stringify(body),
295
+ })
296
+ if (!response.ok) {
297
+ const text = await response.text()
298
+ const parsed = parseJson(text) ?? {}
299
+ const failure = new Error(parsed.detail || parsed.error || `HTTP ${response.status}`)
300
+ failure.code = parsed.code || ''
301
+ failure.status = response.status
302
+ throw failure
303
+ }
304
+ if (binary) return Buffer.from(await response.arrayBuffer())
305
+ return response.json()
306
+ }
307
+
308
+ const PLAY_LOG = path.join(CACHE_DIR, 'last-play.log')
309
+ const POWERSHELL = path.join(process.env.SystemRoot || 'C:\Windows',
310
+ 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe')
311
+
312
+ function logPlay(line) {
313
+ appendFile(PLAY_LOG, `${new Date().toISOString()} ${line}
314
+ `).catch(() => {})
315
+ }
316
+
317
+ function playerScript(file) {
318
+ const escaped = String(file).replace(/'/g, "''")
319
+ const member = '[DllImport("winmm.dll", CharSet=CharSet.Unicode)] public static extern bool PlaySound(string ps, IntPtr h, uint f);'
320
+ return [
321
+ `Add-Type -MemberDefinition '${member}' -Name W -Namespace BreezeTts;`,
322
+ `$ok=[BreezeTts.W]::PlaySound('${escaped}',[IntPtr]::Zero,0x20000);`,
323
+ 'exit([int](-not $ok))',
324
+ ].join('')
325
+ }
326
+
327
+ function warmPlayer(spawner) {
328
+ const args = ['-NoProfile', '-NonInteractive', '-Command', 'exit 0']
329
+ try {
330
+ if (spawner && typeof spawner.spawn === 'function') {
331
+ const handle = spawner.spawn({
332
+ argv: [POWERSHELL, ...args],
333
+ cwd: CACHE_DIR,
334
+ stdio: { stdin: 'ignore', stdout: 'ignore', stderr: 'ignore' },
335
+ graceMs: 2000,
336
+ })
337
+ handle.done.catch(() => {})
338
+ return
339
+ }
340
+ const child = spawn(POWERSHELL, args, { stdio: 'ignore', windowsHide: true })
341
+ child.on('error', () => {})
342
+ } catch {
343
+ /* warm-up is best effort */
344
+ }
345
+ }
346
+
347
+ function playFile(file, spawner) {
348
+ const script = playerScript(file)
349
+ const args = ['-NoProfile', '-NonInteractive', '-Command', script]
350
+ logPlay(`start ${path.basename(file)} via=${spawner ? 'subprocess' : 'node'}`)
351
+
352
+ if (spawner && typeof spawner.spawn === 'function') {
353
+ try {
354
+ const handle = spawner.spawn({
355
+ argv: [POWERSHELL, ...args],
356
+ cwd: CACHE_DIR,
357
+ stdio: { stdin: 'ignore', stdout: 'ignore', stderr: 'ignore' },
358
+ graceMs: 2000,
359
+ })
360
+ handle.done
361
+ .then((outcome) => {
362
+ logPlay(`done exit=${outcome.exitCode} signal=${outcome.signal}`)
363
+ warmPlayer(spawner)
364
+ })
365
+ .catch((error) => logPlay(`failed ${error instanceof Error ? error.message : String(error)}`))
366
+ return
367
+ } catch (error) {
368
+ logPlay(`subprocess-threw ${error instanceof Error ? error.message : String(error)}`)
369
+ }
370
+ }
371
+
372
+ try {
373
+ const child = spawn(POWERSHELL, args, { stdio: 'ignore', windowsHide: true })
374
+ child.on('error', (error) => logPlay(`spawn-error ${error.message}`))
375
+ child.on('exit', (code) => logPlay(`exit ${code}`))
376
+ } catch (error) {
377
+ logPlay(`spawn-threw ${error instanceof Error ? error.message : String(error)}`)
378
+ }
379
+ }
380
+
381
+ function clip(text, limit = 90) {
382
+ const value = String(text ?? '').replace(/\s+/g, ' ').trim()
383
+ return value.length > limit ? `${value.slice(0, limit)}…` : value
384
+ }
385
+
386
+ export function apply(ctx, config) {
387
+ const cfg = { ...DEFAULTS, ...(config ?? {}) }
388
+ let fleet = { busy: 0 }
389
+ let emotionModeCache = 'fixed'
390
+ readStore()
391
+ .then((store) => {
392
+ if (typeof store.emotionMode === 'string') emotionModeCache = store.emotionMode
393
+ })
394
+ .catch(() => {})
395
+ let lastAudio = ''
396
+
397
+ async function settings() {
398
+ const store = await readStore()
399
+ const custom = Array.isArray(store.tones) ? store.tones.filter((tone) => tone && !tone.builtin) : []
400
+ const tones = [...DEFAULT_TONES, ...custom]
401
+ return {
402
+ ...cfg,
403
+ ...store,
404
+ tones,
405
+ apiKey: store.apiKey ?? store.api_key ?? process.env.BREEZE_API_KEY ?? '',
406
+ }
407
+ }
408
+
409
+ async function usage() {
410
+ const store = await readStore()
411
+ const today = usageKey()
412
+ if (store.usageDate !== today) return { chars: 0, calls: 0 }
413
+ return { chars: Number(store.usageChars ?? 0), calls: Number(store.usageCalls ?? 0) }
414
+ }
415
+
416
+ async function accountUsage(chars) {
417
+ const current = await usage()
418
+ await writeStore({
419
+ usageDate: usageKey(),
420
+ usageChars: current.chars + chars,
421
+ usageCalls: current.calls + 1,
422
+ })
423
+ return { chars: current.chars + chars, calls: current.calls + 1 }
424
+ }
425
+
426
+ async function synth(text, options = {}) {
427
+ const s = await settings()
428
+ if (!s.apiKey) throw new Error('missing Breeze API key: write it into ~/.dsh/breeze.json')
429
+ const voiceId = options.voiceId || s.voiceId
430
+ if (!voiceId) throw new Error('missing voice id')
431
+ const moodId = options.toneId || s.moodId || 'normal'
432
+ const tone = (s.tones ?? []).find((item) => item.id === moodId)
433
+ const mood = moodId
434
+ const customTone = String(s.customMood || '').trim()
435
+ const instructions = options.instructions ?? (customTone || (tone ? tone.instructions : ''))
436
+ const format = options.format || 'wav_24000'
437
+ const planSource = (s.emotionMode === 'marker') ? String(text ?? '') : cleanSpeech(text)
438
+ const chunks = chunkByLanguage(cleanSpeech(text))
439
+ if (chunks.length === 0) throw new Error('empty text')
440
+ const cacheKey = createHash('sha1').update([voiceId, mood, s.emotionMode || 'fixed', instructions, planSource].join('|')).digest('hex').slice(0, 16)
441
+ const cachePath = path.join(CACHE_DIR, `cache-${cacheKey}.wav`)
442
+ if (format.startsWith('wav')) {
443
+ try {
444
+ const hit = await readFile(cachePath)
445
+ if (hit.length > 44) {
446
+ return {
447
+ audio: hit,
448
+ langs: ['cache'],
449
+ chars: 0,
450
+ credits: 0,
451
+ mood,
452
+ voiceId,
453
+ format: 'wav',
454
+ cached: true,
455
+ }
456
+ }
457
+ } catch {
458
+ /* cache miss */
459
+ }
460
+ }
461
+ const current = await usage()
462
+ if (current.chars + chunks.map((piece) => piece.text).join('').length > Number(s.dailyChars ?? 1500)) {
463
+ throw new Error(`daily limit reached (${current.chars}/${s.dailyChars} chars)`)
464
+ }
465
+ const buffers = []
466
+ const fixed = s.languageMode && s.languageMode !== 'auto' ? String(s.languageMode) : ''
467
+ const segments = planSegments(planSource, s.emotionMode || 'fixed', instructions)
468
+ for (const piece of segments) {
469
+ const chunk = piece.text
470
+ const segInstructions = piece.instructions || instructions
471
+ const segLang = detectLanguage(chunk) || s.languageCode
472
+ const body = {
473
+ text: chunk,
474
+ model_id: s.modelId,
475
+ language_code: fixed || segLang,
476
+ }
477
+ if (segInstructions) body.instructions = segInstructions
478
+ const audio = await callApi(`/text-to-speech/${encodeURIComponent(voiceId)}`, {
479
+ method: 'POST',
480
+ body,
481
+ query: { output_format: format, delivery: 'sync' },
482
+ apiKey: s.apiKey,
483
+ binary: true,
484
+ })
485
+ buffers.push(audio)
486
+ }
487
+ const chars = segments.map((piece) => piece.text).join('').length
488
+ const merged = format.startsWith('wav') && buffers.length > 1 ? concatWav(buffers) : Buffer.concat(buffers)
489
+ await accountUsage(chars)
490
+ if (format.startsWith('wav')) {
491
+ await mkdir(CACHE_DIR, { recursive: true }).catch(() => {})
492
+ await writeFile(cachePath, merged).catch(() => {})
493
+ }
494
+ return {
495
+ audio: merged,
496
+ langs: segments.map((piece) => piece.emotion || detectLanguage(piece.text) || ''),
497
+ chars,
498
+ credits: creditsOf(chars),
499
+ mood,
500
+ voiceId,
501
+ format: format.startsWith('wav') ? 'wav' : 'mp3',
502
+ }
503
+ }
504
+
505
+ async function saveAudio(result, file) {
506
+ await mkdir(path.dirname(file), { recursive: true })
507
+ await writeFile(file, result.audio)
508
+ return file
509
+ }
510
+
511
+ const promptService = ctx.get('systemPrompt')
512
+ if (promptService && typeof promptService.section === 'function') {
513
+ ctx.effect(() => promptService.section({
514
+ name: 'breeze-tts-emotion',
515
+ order: 950,
516
+ text: () => {
517
+ if (emotionModeCache !== 'marker') return ''
518
+ return [
519
+ 'Voice emotion tags: to make part of a reply speak with a different tone,',
520
+ 'put an HTML comment tag at the start of that segment:',
521
+ '<!--mood:curious-->, <!--mood:excited-->, <!--mood:happy-->, <!--mood:angry-->,',
522
+ '<!--mood:down-->, <!--mood:sad-->, <!--mood:whisper-->, <!--mood:gentle-->,',
523
+ '<!--mood:surprised-->, <!--mood:afraid-->, <!--mood:serious-->, <!--mood:tired-->.',
524
+ 'The tag only affects speech synthesis. Use them sparingly, only where the',
525
+ 'emotional shift matters.',
526
+ ].join(' ')
527
+ },
528
+ }), 'breeze-tts: emotion prompt section')
529
+ }
530
+
531
+ ctx.effect(() => {
532
+ const disposers = []
533
+
534
+ disposers.push(ctx.tools.register({
535
+ name: 'tts_speak',
536
+ description: 'Speak a text aloud through this machine\'s speakers with Breeze Blue TTS, optionally also writing the audio to a file.',
537
+ parameters: {
538
+ type: 'object',
539
+ properties: {
540
+ text: { type: 'string', description: 'Text to synthesize. Long text is split on sentence marks.' },
541
+ mood: { type: 'string', description: 'Delivery preset: normal, lazy, silly or eager. Defaults to the configured mood.' },
542
+ voice_id: { type: 'string', description: 'Voice id override. Defaults to the configured voice.' },
543
+ play: { type: 'boolean', description: 'Play through the speakers. Set false to only produce the file.' },
544
+ save_path: { type: 'string', description: 'Absolute path to also write the audio to.' },
545
+ },
546
+ required: ['text'],
547
+ },
548
+ output: {
549
+ schema: {
550
+ type: 'object',
551
+ properties: {
552
+ ok: { type: 'boolean' },
553
+ chars: { type: 'number' },
554
+ credits: { type: 'number' },
555
+ mood: { type: 'string' },
556
+ audio: { type: 'string' },
557
+ played: { type: 'boolean' },
558
+ },
559
+ },
560
+ render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
561
+ },
562
+ async execute(args) {
563
+ const input = args ?? {}
564
+ const savePath = typeof input.save_path === 'string' ? input.save_path : ''
565
+ const wantsFile = savePath !== ''
566
+ const plays = input.play !== false
567
+ const format = !plays && wantsFile && /\.mp3$/i.test(savePath) ? 'mp3_24000_96' : 'wav_24000'
568
+ const result = await synth(input.text, { mood: input.mood, voiceId: input.voice_id, format })
569
+ await mkdir(CACHE_DIR, { recursive: true })
570
+ const file = path.join(CACHE_DIR, `${Date.now()}-${randomUUID().slice(0, 8)}.${format.startsWith('wav') ? 'wav' : 'mp3'}`)
571
+ await saveAudio(result, file)
572
+ const target = typeof input.save_path === 'string' && input.save_path
573
+ ? await saveAudio(result, input.save_path)
574
+ : file
575
+ const played = input.play !== false
576
+ if (played) playFile(target, ctx.get('subprocess'))
577
+ return {
578
+ ok: true,
579
+ chars: result.chars,
580
+ credits: result.credits,
581
+ mood: result.mood,
582
+ audio: target,
583
+ played,
584
+ }
585
+ },
586
+ }))
587
+
588
+ disposers.push(ctx.tools.register({
589
+ name: 'tts_voices',
590
+ description: 'List the Breeze voices available to this account, filtered by language or a search term.',
591
+ parameters: {
592
+ type: 'object',
593
+ properties: {
594
+ search: { type: 'string', description: 'Free-text search over name and description.' },
595
+ language: { type: 'string', description: 'ISO 639-1 language code such as zh or en.' },
596
+ limit: { type: 'number', description: 'Maximum rows to return, default 20.' },
597
+ },
598
+ },
599
+ output: {
600
+ schema: { type: 'object', properties: { total: { type: 'number' }, voices: { type: 'array' } } },
601
+ render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
602
+ },
603
+ async execute(args) {
604
+ const input = args ?? {}
605
+ const s = await settings()
606
+ if (!s.apiKey) throw new Error('missing Breeze API key')
607
+ const data = await callApi('/voices', {
608
+ query: { search: input.search, language_code: input.language },
609
+ apiKey: s.apiKey,
610
+ })
611
+ const rows = (data.voices ?? data.items ?? []).map((voice) => ({
612
+ voice_id: voice.voice_id ?? voice.id,
613
+ name: voice.name,
614
+ language: voice.language ?? voice.language_code,
615
+ gender: voice.gender ?? null,
616
+ category: voice.primary_category_code ?? voice.category ?? null,
617
+ }))
618
+ const limit = Number(input.limit ?? 20)
619
+ return { total: rows.length, voices: rows.slice(0, limit) }
620
+ },
621
+ }))
622
+
623
+ disposers.push(ctx.tools.register({
624
+ name: 'tts_status',
625
+ description: 'Report Breeze TTS readiness: configured voice, today\'s character budget, credit balance and remaining quota.',
626
+ parameters: { type: 'object', properties: {} },
627
+ output: {
628
+ schema: { type: 'object', properties: { ok: { type: 'boolean' } } },
629
+ render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
630
+ },
631
+ async execute() {
632
+ const s = await settings()
633
+ const current = await usage()
634
+ let balance = null
635
+ let error = null
636
+ if (s.apiKey) {
637
+ try {
638
+ const data = await callApi('/balance', { apiKey: s.apiKey })
639
+ balance = data.balance ?? null
640
+ } catch (caught) {
641
+ error = caught instanceof Error ? caught.message : String(caught)
642
+ }
643
+ }
644
+ return {
645
+ ok: Boolean(s.apiKey) && !error,
646
+ voiceId: s.voiceId,
647
+ modelId: s.modelId,
648
+ moodId: s.moodId,
649
+ tones: (s.tones ?? []).map((tone) => tone.id),
650
+ today: { chars: current.chars, calls: current.calls, limit: Number(s.dailyChars ?? 0) },
651
+ balance,
652
+ error,
653
+ }
654
+ },
655
+ }))
656
+
657
+ const web = ctx.get('webServer')
658
+ if (web) {
659
+ disposers.push(web.register({
660
+ kind: 'prefix',
661
+ path: ROUTE_PREFIX,
662
+ handler: async (req, res) => {
663
+ const url = new URL(req.url ?? '/', 'http://localhost')
664
+ const route = url.pathname.slice(ROUTE_PREFIX.length) || '/'
665
+ const json = (status, payload) => {
666
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
667
+ res.end(JSON.stringify(payload))
668
+ }
669
+ const readBody = async () => {
670
+ const chunks = []
671
+ for await (const chunk of req) chunks.push(chunk)
672
+ return parseJson(Buffer.concat(chunks).toString('utf8')) ?? {}
673
+ }
674
+ try {
675
+ if (route === '/status' && req.method === 'GET') {
676
+ const s = await settings()
677
+ const current = await usage()
678
+ let balance = null
679
+ let error = null
680
+ if (s.apiKey) {
681
+ try {
682
+ const data = await callApi('/balance', { apiKey: s.apiKey })
683
+ balance = data.balance ?? null
684
+ } catch (caught) {
685
+ error = caught instanceof Error ? caught.message : String(caught)
686
+ }
687
+ }
688
+ return json(200, {
689
+ ok: Boolean(s.apiKey) && !error,
690
+ hasKey: Boolean(s.apiKey),
691
+ voiceId: s.voiceId,
692
+ modelId: s.modelId,
693
+ moodId: s.moodId ?? 'normal',
694
+ tones: (s.tones ?? []).map((tone) => ({
695
+ id: tone.id,
696
+ name: tone.name,
697
+ instructions: tone.instructions,
698
+ builtin: Boolean(tone.builtin),
699
+ })),
700
+ customMood: s.customMood ?? '',
701
+ emotionMode: s.emotionMode ?? 'fixed',
702
+ voicePresets: VOICE_PRESETS,
703
+ savedVoices: Array.isArray(s.savedVoices) ? s.savedVoices : [],
704
+ autoSpeak: Boolean(s.autoSpeak),
705
+ autoSpeakMaxChars: Number(s.autoSpeakMaxChars ?? 120),
706
+ backgroundPlay: Boolean(s.backgroundPlay),
707
+ previewText: s.previewText,
708
+ today: { chars: current.chars, calls: current.calls, limit: Number(s.dailyChars ?? 0) },
709
+ balance,
710
+ error,
711
+ })
712
+ }
713
+ if (route === '/voices' && req.method === 'GET') {
714
+ const s = await settings()
715
+ if (!s.apiKey) return json(400, { ok: false, error: 'missing api key' })
716
+ const data = await callApi('/voices', {
717
+ query: { language_code: url.searchParams.get('language') ?? 'zh' },
718
+ apiKey: s.apiKey,
719
+ })
720
+ const rows = (data.voices ?? data.items ?? []).map((voice) => ({
721
+ voiceId: voice.voice_id ?? voice.id,
722
+ name: voice.name,
723
+ language: voice.language ?? voice.language_code,
724
+ gender: voice.gender ?? null,
725
+ category: voice.primary_category_code ?? voice.category ?? null,
726
+ description: String(voice.description ?? '').slice(0, 120),
727
+ designed: (voice.origin ?? '') === 'designed',
728
+ private: (voice.visibility ?? '') === 'private',
729
+ }))
730
+ return json(200, { ok: true, voices: rows })
731
+ }
732
+ if (route === '/speak' && req.method === 'POST') {
733
+ const body = await readBody()
734
+ const result = await synth(body.text, {
735
+ toneId: body.toneId || body.mood || '',
736
+ voiceId: body.voiceId,
737
+ format: 'mp3_24000_96',
738
+ })
739
+ res.writeHead(200, {
740
+ 'content-type': 'audio/mpeg',
741
+ 'x-tts-chars': String(result.chars),
742
+ 'x-tts-credits': String(result.credits),
743
+ })
744
+ return res.end(result.audio)
745
+ }
746
+ if (route === '/play' && req.method === 'POST') {
747
+ const body = await readBody()
748
+ if (body.replay === true) {
749
+ if (!lastAudio) return json(404, { ok: false, error: 'nothing to replay' })
750
+ playFile(lastAudio, ctx.get('subprocess'))
751
+ return json(200, { ok: true, replay: true, audio: lastAudio })
752
+ }
753
+ const result = await synth(body.text, {
754
+ toneId: body.toneId || body.mood || '',
755
+ voiceId: body.voiceId,
756
+ format: 'wav_24000',
757
+ })
758
+ await mkdir(CACHE_DIR, { recursive: true })
759
+ const file = path.join(CACHE_DIR, `${Date.now()}-${randomUUID().slice(0, 8)}.wav`)
760
+ await writeFile(file, result.audio)
761
+ lastAudio = file
762
+ playFile(file, ctx.get('subprocess'))
763
+ return json(200, {
764
+ ok: true,
765
+ chars: result.chars,
766
+ credits: result.credits,
767
+ audio: file,
768
+ })
769
+ }
770
+ if (route === '/synth' && req.method === 'POST') {
771
+ const body = await readBody()
772
+ const result = await synth(body.text, {
773
+ toneId: body.toneId || body.mood || '',
774
+ voiceId: body.voiceId,
775
+ format: 'wav_24000',
776
+ })
777
+ await mkdir(CACHE_DIR, { recursive: true })
778
+ const name = `${Date.now()}-${randomUUID().slice(0, 8)}.wav`
779
+ const file = path.join(CACHE_DIR, name)
780
+ await writeFile(file, result.audio)
781
+ lastAudio = file
782
+ return json(200, {
783
+ ok: true,
784
+ url: `${ROUTE_PREFIX}/audio/${name}`,
785
+ langs: result.langs,
786
+ chars: result.chars,
787
+ credits: result.credits,
788
+ file,
789
+ })
790
+ }
791
+ if (route === '/play-file' && req.method === 'POST') {
792
+ const body = await readBody()
793
+ const raw = typeof body.file === 'string' && body.file ? body.file
794
+ : String(body.url ?? '').split('/').pop()
795
+ const name = path.basename(raw)
796
+ if (!/^[A-Za-z0-9._-]+$/.test(name)) return json(400, { ok: false, error: 'bad file name' })
797
+ const file = path.join(CACHE_DIR, name)
798
+ playFile(file, ctx.get('subprocess'))
799
+ return json(200, { ok: true, file })
800
+ }
801
+ if (route.startsWith('/audio/') && req.method === 'GET') {
802
+ const name = path.basename(route.slice('/audio/'.length))
803
+ if (!/^[A-Za-z0-9._-]+$/.test(name)) return json(400, { ok: false, error: 'bad file name' })
804
+ let bytes
805
+ try {
806
+ bytes = await readFile(path.join(CACHE_DIR, name))
807
+ } catch {
808
+ return json(404, { ok: false, error: 'no such audio' })
809
+ }
810
+ const type = name.endsWith('.wav') ? 'audio/wav' : name.endsWith('.mp3') ? 'audio/mpeg' : 'application/octet-stream'
811
+ res.writeHead(200, { 'content-type': type, 'content-length': String(bytes.length), 'cache-control': 'no-store' })
812
+ return res.end(bytes)
813
+ }
814
+ if (route === '/design' && req.method === 'POST') {
815
+ const body = await readBody()
816
+ const s2 = await settings()
817
+ if (!s2.apiKey) return json(400, { ok: false, error: 'missing api key' })
818
+ const description = String(body.description ?? '').trim()
819
+ if (description.length < 3) return json(400, { ok: false, error: 'description too short' })
820
+ const sample = String(body.text ?? '').trim().slice(0, 500) || '你好,这是一段试听。'
821
+ const data = await callApi('/voice-previews/design', {
822
+ method: 'POST',
823
+ body: {
824
+ voice_description: description.slice(0, 500),
825
+ text: sample,
826
+ model_id: s2.modelId,
827
+ language_code: 'zh',
828
+ preview_count: 3,
829
+ },
830
+ query: { output_format: 'mp3' },
831
+ apiKey: s2.apiKey,
832
+ })
833
+ const previews = Array.isArray(data.previews) ? data.previews : []
834
+ const candidates = []
835
+ await mkdir(CACHE_DIR, { recursive: true })
836
+ for (const preview of previews) {
837
+ const generatedVoiceId = preview.generated_voice_id
838
+ const encoded = preview.audio_base_64 || preview.audio_base64
839
+ if (!generatedVoiceId || !encoded) continue
840
+ const name = `${Date.now()}-${randomUUID().slice(0, 8)}.mp3`
841
+ await writeFile(path.join(CACHE_DIR, name), Buffer.from(String(encoded), 'base64'))
842
+ candidates.push({ generatedVoiceId, url: `${ROUTE_PREFIX}/audio/${name}` })
843
+ }
844
+ return json(200, { ok: true, candidates })
845
+ }
846
+ if (route === '/save-voice' && req.method === 'POST') {
847
+ const body = await readBody()
848
+ const s3 = await settings()
849
+ if (!s3.apiKey) return json(400, { ok: false, error: 'missing api key' })
850
+ const generatedVoiceId = String(body.generatedVoiceId ?? '')
851
+ if (!/^[A-Za-z0-9_-]+$/.test(generatedVoiceId)) return json(400, { ok: false, error: 'bad voice id' })
852
+ const payload = {
853
+ voice_name: String(body.name ?? 'voice').slice(0, 60),
854
+ language_code: 'zh',
855
+ }
856
+ const desc = String(body.description ?? '').trim()
857
+ if (desc) payload.voice_description = desc.slice(0, 500)
858
+ const saved = await callApi(`/voice-previews/${generatedVoiceId}/save`, {
859
+ method: 'POST',
860
+ body: payload,
861
+ apiKey: s3.apiKey,
862
+ })
863
+ const savedId = saved.voice_id || (saved.voice && saved.voice.voice_id)
864
+ if (savedId) await writeStore({ voiceId: savedId })
865
+ return json(200, { ok: true, voiceId: savedId || null })
866
+ }
867
+ if (route === '/log' && req.method === 'POST') {
868
+ const body = await readBody()
869
+ const line = `${new Date().toISOString()} ${String(body.msg ?? '').slice(0, 400)}`
870
+ await mkdir(CACHE_DIR, { recursive: true })
871
+ await appendFile(path.join(CACHE_DIR, 'autospeak.log'), line + String.fromCharCode(10)).catch(() => {})
872
+ return json(200, { ok: true })
873
+ }
874
+ if (route === '/rename-voice' && req.method === 'POST') {
875
+ const body = await readBody()
876
+ const s6 = await settings()
877
+ if (!s6.apiKey) return json(400, { ok: false, error: 'missing api key' })
878
+ const voiceId = String(body.voiceId ?? '')
879
+ const name = String(body.name ?? '').trim().slice(0, 60)
880
+ if (!/^[A-Za-z0-9_-]+$/.test(voiceId)) return json(400, { ok: false, error: 'bad voice id' })
881
+ if (!name) return json(400, { ok: false, error: 'name required' })
882
+ if (VOICE_PRESETS.some((preset) => preset.voiceId === voiceId)) {
883
+ return json(400, { ok: false, error: 'builtin voice cannot be renamed' })
884
+ }
885
+ const form = new FormData()
886
+ form.append('name', name)
887
+ await callApi(`/voices/${voiceId}`, { method: 'PATCH', body: form, apiKey: s6.apiKey })
888
+ return json(200, { ok: true, voiceId, name })
889
+ }
890
+ if (route === '/delete-voice' && req.method === 'POST') {
891
+ const body = await readBody()
892
+ const s4 = await settings()
893
+ const voiceId = String(body.voiceId ?? '')
894
+ if (!s4.apiKey) return json(400, { ok: false, error: 'missing api key' })
895
+ if (!/^[A-Za-z0-9_-]+$/.test(voiceId)) return json(400, { ok: false, error: 'bad voice id' })
896
+ if (VOICE_PRESETS.some((preset) => preset.voiceId === voiceId)) {
897
+ return json(400, { ok: false, error: 'builtin voice cannot be removed' })
898
+ }
899
+ const saved = Array.isArray(s4.savedVoices) ? s4.savedVoices : []
900
+ if (saved.some((item) => item.voiceId === voiceId)) {
901
+ await writeStore({ savedVoices: saved.filter((item) => item.voiceId !== voiceId) })
902
+ if ((await readStore()).voiceId === voiceId) await writeStore({ voiceId: VOICE_PRESETS[0].voiceId })
903
+ return json(200, { ok: true, removed: 'saved' })
904
+ }
905
+ await callApi(`/voices/${voiceId}`, { method: 'DELETE', apiKey: s4.apiKey })
906
+ const st5 = await readStore()
907
+ if (st5.voiceId === voiceId) await writeStore({ voiceId: 'voc_rc894j8q2auu' })
908
+ return json(200, { ok: true, voiceId })
909
+ }
910
+ if (route === '/connect' && req.method === 'POST') {
911
+ const body = await readBody()
912
+ const key = String(body.apiKey ?? '').trim()
913
+ if (!key) return json(400, { ok: false, error: 'api key required' })
914
+ try {
915
+ const balance = await callApi('/balance', { apiKey: key })
916
+ await writeStore({ apiKey: key })
917
+ return json(200, { ok: true, balance: balance.balance ?? null })
918
+ } catch (caught) {
919
+ return json(400, {
920
+ ok: false,
921
+ error: caught instanceof Error ? caught.message : String(caught),
922
+ code: caught && caught.code ? String(caught.code) : '',
923
+ status: caught && caught.status ? Number(caught.status) : 0,
924
+ })
925
+ }
926
+ }
927
+ if (route === '/disconnect' && req.method === 'POST') {
928
+ const store = await readStore()
929
+ delete store.apiKey
930
+ delete store.api_key
931
+ await writeFile(SETTINGS_PATH, JSON.stringify(store, null, 2), 'utf8')
932
+ return json(200, { ok: true })
933
+ }
934
+ if (route === '/settings' && req.method === 'POST') {
935
+ const body = await readBody()
936
+ const patch = {}
937
+ if (typeof body.voiceId === 'string' && body.voiceId) patch.voiceId = body.voiceId
938
+ if (typeof body.moodId === 'string' && body.moodId) patch.moodId = body.moodId
939
+ if (typeof body.customMood === 'string') patch.customMood = body.customMood.slice(0, 500)
940
+ if (typeof body.emotionMode === 'string' && ['fixed', 'infer', 'marker'].includes(body.emotionMode)) {
941
+ patch.emotionMode = body.emotionMode
942
+ }
943
+ if (Array.isArray(body.savedVoices)) {
944
+ patch.savedVoices = body.savedVoices
945
+ .filter((item) => item && typeof item.voiceId === 'string')
946
+ .map((item) => ({
947
+ voiceId: item.voiceId,
948
+ name: String(item.name ?? item.voiceId).slice(0, 60),
949
+ language: String(item.language ?? '').slice(0, 8),
950
+ description: String(item.description ?? '').slice(0, 120),
951
+ }))
952
+ .slice(0, 50)
953
+ }
954
+ if (Array.isArray(body.tones)) {
955
+ patch.tones = body.tones
956
+ .filter((tone) => tone && typeof tone.id === 'string' && typeof tone.instructions === 'string')
957
+ .map((tone) => ({
958
+ id: String(tone.id).slice(0, 40),
959
+ name: String(tone.name ?? tone.id).slice(0, 40),
960
+ instructions: String(tone.instructions).slice(0, 500),
961
+ builtin: false,
962
+ }))
963
+ .slice(0, 40)
964
+ }
965
+ if (typeof body.previewText === 'string' && body.previewText) patch.previewText = body.previewText
966
+ if (typeof body.dailyChars === 'number' && body.dailyChars > 0) patch.dailyChars = Math.floor(body.dailyChars)
967
+ if (typeof body.autoSpeak === 'boolean') patch.autoSpeak = body.autoSpeak
968
+ if (typeof body.backgroundPlay === 'boolean') patch.backgroundPlay = body.backgroundPlay
969
+ if (typeof body.autoSpeakMaxChars === 'number' && body.autoSpeakMaxChars >= 0) {
970
+ patch.autoSpeakMaxChars = Math.max(0, Math.min(99999, Math.floor(body.autoSpeakMaxChars)))
971
+ }
972
+ const written = await writeStore(patch)
973
+ if (typeof written.emotionMode === 'string') emotionModeCache = written.emotionMode
974
+ return json(200, { ok: true, saved: patch })
975
+ }
976
+ if (route === '/health') {
977
+ let lastPlay = ''
978
+ try {
979
+ const lines = (await readFile(PLAY_LOG, 'utf8')).trim().split(String.fromCharCode(10))
980
+ lastPlay = lines.slice(-6).join(' | ')
981
+ } catch {
982
+ lastPlay = ''
983
+ }
984
+ return json(200, { ok: true, busy: fleet.busy, lastPlay })
985
+ }
986
+ return json(404, { ok: false, error: 'unknown route', route })
987
+ } catch (caught) {
988
+ return json(500, {
989
+ ok: false,
990
+ error: caught instanceof Error ? caught.message : String(caught),
991
+ code: caught && caught.code ? String(caught.code) : '',
992
+ status: caught && caught.status ? Number(caught.status) : 0,
993
+ })
994
+ }
995
+ },
996
+ }))
997
+ }
998
+
999
+ return () => {
1000
+ for (const dispose of disposers) {
1001
+ try {
1002
+ dispose()
1003
+ } catch {
1004
+ fleet = { ...fleet }
1005
+ }
1006
+ }
1007
+ }
1008
+ }, 'dsh-breeze-tts: tools and routes')
1009
+
1010
+ ctx.logger?.info?.(`breeze-tts ready (voice ${cfg.voiceId}, ${clip(cfg.modelId)})`)
1011
+ }