@eslaykey/dsh-breeze-tts 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +84 -2
- package/client.js +1466 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +12 -0
- package/index.js +986 -0
- package/locale/en.json +35 -0
- package/locale/zh.json +35 -0
- package/package.json +49 -6
package/index.js
ADDED
|
@@ -0,0 +1,986 @@
|
|
|
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
|
+
voiceId: s.voiceId,
|
|
691
|
+
modelId: s.modelId,
|
|
692
|
+
moodId: s.moodId ?? 'normal',
|
|
693
|
+
tones: (s.tones ?? []).map((tone) => ({
|
|
694
|
+
id: tone.id,
|
|
695
|
+
name: tone.name,
|
|
696
|
+
instructions: tone.instructions,
|
|
697
|
+
builtin: Boolean(tone.builtin),
|
|
698
|
+
})),
|
|
699
|
+
customMood: s.customMood ?? '',
|
|
700
|
+
emotionMode: s.emotionMode ?? 'fixed',
|
|
701
|
+
voicePresets: VOICE_PRESETS,
|
|
702
|
+
savedVoices: Array.isArray(s.savedVoices) ? s.savedVoices : [],
|
|
703
|
+
autoSpeak: Boolean(s.autoSpeak),
|
|
704
|
+
autoSpeakMaxChars: Number(s.autoSpeakMaxChars ?? 120),
|
|
705
|
+
backgroundPlay: Boolean(s.backgroundPlay),
|
|
706
|
+
previewText: s.previewText,
|
|
707
|
+
today: { chars: current.chars, calls: current.calls, limit: Number(s.dailyChars ?? 0) },
|
|
708
|
+
balance,
|
|
709
|
+
error,
|
|
710
|
+
})
|
|
711
|
+
}
|
|
712
|
+
if (route === '/voices' && req.method === 'GET') {
|
|
713
|
+
const s = await settings()
|
|
714
|
+
if (!s.apiKey) return json(400, { ok: false, error: 'missing api key' })
|
|
715
|
+
const data = await callApi('/voices', {
|
|
716
|
+
query: { language_code: url.searchParams.get('language') ?? 'zh' },
|
|
717
|
+
apiKey: s.apiKey,
|
|
718
|
+
})
|
|
719
|
+
const rows = (data.voices ?? data.items ?? []).map((voice) => ({
|
|
720
|
+
voiceId: voice.voice_id ?? voice.id,
|
|
721
|
+
name: voice.name,
|
|
722
|
+
language: voice.language ?? voice.language_code,
|
|
723
|
+
gender: voice.gender ?? null,
|
|
724
|
+
category: voice.primary_category_code ?? voice.category ?? null,
|
|
725
|
+
description: String(voice.description ?? '').slice(0, 120),
|
|
726
|
+
designed: (voice.origin ?? '') === 'designed',
|
|
727
|
+
private: (voice.visibility ?? '') === 'private',
|
|
728
|
+
}))
|
|
729
|
+
return json(200, { ok: true, voices: rows })
|
|
730
|
+
}
|
|
731
|
+
if (route === '/speak' && req.method === 'POST') {
|
|
732
|
+
const body = await readBody()
|
|
733
|
+
const result = await synth(body.text, {
|
|
734
|
+
toneId: body.toneId || body.mood || '',
|
|
735
|
+
voiceId: body.voiceId,
|
|
736
|
+
format: 'mp3_24000_96',
|
|
737
|
+
})
|
|
738
|
+
res.writeHead(200, {
|
|
739
|
+
'content-type': 'audio/mpeg',
|
|
740
|
+
'x-tts-chars': String(result.chars),
|
|
741
|
+
'x-tts-credits': String(result.credits),
|
|
742
|
+
})
|
|
743
|
+
return res.end(result.audio)
|
|
744
|
+
}
|
|
745
|
+
if (route === '/play' && req.method === 'POST') {
|
|
746
|
+
const body = await readBody()
|
|
747
|
+
if (body.replay === true) {
|
|
748
|
+
if (!lastAudio) return json(404, { ok: false, error: 'nothing to replay' })
|
|
749
|
+
playFile(lastAudio, ctx.get('subprocess'))
|
|
750
|
+
return json(200, { ok: true, replay: true, audio: lastAudio })
|
|
751
|
+
}
|
|
752
|
+
const result = await synth(body.text, {
|
|
753
|
+
toneId: body.toneId || body.mood || '',
|
|
754
|
+
voiceId: body.voiceId,
|
|
755
|
+
format: 'wav_24000',
|
|
756
|
+
})
|
|
757
|
+
await mkdir(CACHE_DIR, { recursive: true })
|
|
758
|
+
const file = path.join(CACHE_DIR, `${Date.now()}-${randomUUID().slice(0, 8)}.wav`)
|
|
759
|
+
await writeFile(file, result.audio)
|
|
760
|
+
lastAudio = file
|
|
761
|
+
playFile(file, ctx.get('subprocess'))
|
|
762
|
+
return json(200, {
|
|
763
|
+
ok: true,
|
|
764
|
+
chars: result.chars,
|
|
765
|
+
credits: result.credits,
|
|
766
|
+
audio: file,
|
|
767
|
+
})
|
|
768
|
+
}
|
|
769
|
+
if (route === '/synth' && req.method === 'POST') {
|
|
770
|
+
const body = await readBody()
|
|
771
|
+
const result = await synth(body.text, {
|
|
772
|
+
toneId: body.toneId || body.mood || '',
|
|
773
|
+
voiceId: body.voiceId,
|
|
774
|
+
format: 'wav_24000',
|
|
775
|
+
})
|
|
776
|
+
await mkdir(CACHE_DIR, { recursive: true })
|
|
777
|
+
const name = `${Date.now()}-${randomUUID().slice(0, 8)}.wav`
|
|
778
|
+
const file = path.join(CACHE_DIR, name)
|
|
779
|
+
await writeFile(file, result.audio)
|
|
780
|
+
lastAudio = file
|
|
781
|
+
return json(200, {
|
|
782
|
+
ok: true,
|
|
783
|
+
url: `${ROUTE_PREFIX}/audio/${name}`,
|
|
784
|
+
langs: result.langs,
|
|
785
|
+
chars: result.chars,
|
|
786
|
+
credits: result.credits,
|
|
787
|
+
file,
|
|
788
|
+
})
|
|
789
|
+
}
|
|
790
|
+
if (route === '/play-file' && req.method === 'POST') {
|
|
791
|
+
const body = await readBody()
|
|
792
|
+
const raw = typeof body.file === 'string' && body.file ? body.file
|
|
793
|
+
: String(body.url ?? '').split('/').pop()
|
|
794
|
+
const name = path.basename(raw)
|
|
795
|
+
if (!/^[A-Za-z0-9._-]+$/.test(name)) return json(400, { ok: false, error: 'bad file name' })
|
|
796
|
+
const file = path.join(CACHE_DIR, name)
|
|
797
|
+
playFile(file, ctx.get('subprocess'))
|
|
798
|
+
return json(200, { ok: true, file })
|
|
799
|
+
}
|
|
800
|
+
if (route.startsWith('/audio/') && req.method === 'GET') {
|
|
801
|
+
const name = path.basename(route.slice('/audio/'.length))
|
|
802
|
+
if (!/^[A-Za-z0-9._-]+$/.test(name)) return json(400, { ok: false, error: 'bad file name' })
|
|
803
|
+
let bytes
|
|
804
|
+
try {
|
|
805
|
+
bytes = await readFile(path.join(CACHE_DIR, name))
|
|
806
|
+
} catch {
|
|
807
|
+
return json(404, { ok: false, error: 'no such audio' })
|
|
808
|
+
}
|
|
809
|
+
const type = name.endsWith('.wav') ? 'audio/wav' : name.endsWith('.mp3') ? 'audio/mpeg' : 'application/octet-stream'
|
|
810
|
+
res.writeHead(200, { 'content-type': type, 'content-length': String(bytes.length), 'cache-control': 'no-store' })
|
|
811
|
+
return res.end(bytes)
|
|
812
|
+
}
|
|
813
|
+
if (route === '/design' && req.method === 'POST') {
|
|
814
|
+
const body = await readBody()
|
|
815
|
+
const s2 = await settings()
|
|
816
|
+
if (!s2.apiKey) return json(400, { ok: false, error: 'missing api key' })
|
|
817
|
+
const description = String(body.description ?? '').trim()
|
|
818
|
+
if (description.length < 3) return json(400, { ok: false, error: 'description too short' })
|
|
819
|
+
const sample = String(body.text ?? '').trim().slice(0, 500) || '你好,这是一段试听。'
|
|
820
|
+
const data = await callApi('/voice-previews/design', {
|
|
821
|
+
method: 'POST',
|
|
822
|
+
body: {
|
|
823
|
+
voice_description: description.slice(0, 500),
|
|
824
|
+
text: sample,
|
|
825
|
+
model_id: s2.modelId,
|
|
826
|
+
language_code: 'zh',
|
|
827
|
+
preview_count: 3,
|
|
828
|
+
},
|
|
829
|
+
query: { output_format: 'mp3' },
|
|
830
|
+
apiKey: s2.apiKey,
|
|
831
|
+
})
|
|
832
|
+
const previews = Array.isArray(data.previews) ? data.previews : []
|
|
833
|
+
const candidates = []
|
|
834
|
+
await mkdir(CACHE_DIR, { recursive: true })
|
|
835
|
+
for (const preview of previews) {
|
|
836
|
+
const generatedVoiceId = preview.generated_voice_id
|
|
837
|
+
const encoded = preview.audio_base_64 || preview.audio_base64
|
|
838
|
+
if (!generatedVoiceId || !encoded) continue
|
|
839
|
+
const name = `${Date.now()}-${randomUUID().slice(0, 8)}.mp3`
|
|
840
|
+
await writeFile(path.join(CACHE_DIR, name), Buffer.from(String(encoded), 'base64'))
|
|
841
|
+
candidates.push({ generatedVoiceId, url: `${ROUTE_PREFIX}/audio/${name}` })
|
|
842
|
+
}
|
|
843
|
+
return json(200, { ok: true, candidates })
|
|
844
|
+
}
|
|
845
|
+
if (route === '/save-voice' && req.method === 'POST') {
|
|
846
|
+
const body = await readBody()
|
|
847
|
+
const s3 = await settings()
|
|
848
|
+
if (!s3.apiKey) return json(400, { ok: false, error: 'missing api key' })
|
|
849
|
+
const generatedVoiceId = String(body.generatedVoiceId ?? '')
|
|
850
|
+
if (!/^[A-Za-z0-9_-]+$/.test(generatedVoiceId)) return json(400, { ok: false, error: 'bad voice id' })
|
|
851
|
+
const payload = {
|
|
852
|
+
voice_name: String(body.name ?? 'voice').slice(0, 60),
|
|
853
|
+
language_code: 'zh',
|
|
854
|
+
}
|
|
855
|
+
const desc = String(body.description ?? '').trim()
|
|
856
|
+
if (desc) payload.voice_description = desc.slice(0, 500)
|
|
857
|
+
const saved = await callApi(`/voice-previews/${generatedVoiceId}/save`, {
|
|
858
|
+
method: 'POST',
|
|
859
|
+
body: payload,
|
|
860
|
+
apiKey: s3.apiKey,
|
|
861
|
+
})
|
|
862
|
+
const savedId = saved.voice_id || (saved.voice && saved.voice.voice_id)
|
|
863
|
+
if (savedId) await writeStore({ voiceId: savedId })
|
|
864
|
+
return json(200, { ok: true, voiceId: savedId || null })
|
|
865
|
+
}
|
|
866
|
+
if (route === '/log' && req.method === 'POST') {
|
|
867
|
+
const body = await readBody()
|
|
868
|
+
const line = `${new Date().toISOString()} ${String(body.msg ?? '').slice(0, 400)}`
|
|
869
|
+
await mkdir(CACHE_DIR, { recursive: true })
|
|
870
|
+
await appendFile(path.join(CACHE_DIR, 'autospeak.log'), line + String.fromCharCode(10)).catch(() => {})
|
|
871
|
+
return json(200, { ok: true })
|
|
872
|
+
}
|
|
873
|
+
if (route === '/rename-voice' && req.method === 'POST') {
|
|
874
|
+
const body = await readBody()
|
|
875
|
+
const s6 = await settings()
|
|
876
|
+
if (!s6.apiKey) return json(400, { ok: false, error: 'missing api key' })
|
|
877
|
+
const voiceId = String(body.voiceId ?? '')
|
|
878
|
+
const name = String(body.name ?? '').trim().slice(0, 60)
|
|
879
|
+
if (!/^[A-Za-z0-9_-]+$/.test(voiceId)) return json(400, { ok: false, error: 'bad voice id' })
|
|
880
|
+
if (!name) return json(400, { ok: false, error: 'name required' })
|
|
881
|
+
if (VOICE_PRESETS.some((preset) => preset.voiceId === voiceId)) {
|
|
882
|
+
return json(400, { ok: false, error: 'builtin voice cannot be renamed' })
|
|
883
|
+
}
|
|
884
|
+
const form = new FormData()
|
|
885
|
+
form.append('name', name)
|
|
886
|
+
await callApi(`/voices/${voiceId}`, { method: 'PATCH', body: form, apiKey: s6.apiKey })
|
|
887
|
+
return json(200, { ok: true, voiceId, name })
|
|
888
|
+
}
|
|
889
|
+
if (route === '/delete-voice' && req.method === 'POST') {
|
|
890
|
+
const body = await readBody()
|
|
891
|
+
const s4 = await settings()
|
|
892
|
+
const voiceId = String(body.voiceId ?? '')
|
|
893
|
+
if (!s4.apiKey) return json(400, { ok: false, error: 'missing api key' })
|
|
894
|
+
if (!/^[A-Za-z0-9_-]+$/.test(voiceId)) return json(400, { ok: false, error: 'bad voice id' })
|
|
895
|
+
if (VOICE_PRESETS.some((preset) => preset.voiceId === voiceId)) {
|
|
896
|
+
return json(400, { ok: false, error: 'builtin voice cannot be removed' })
|
|
897
|
+
}
|
|
898
|
+
const saved = Array.isArray(s4.savedVoices) ? s4.savedVoices : []
|
|
899
|
+
if (saved.some((item) => item.voiceId === voiceId)) {
|
|
900
|
+
await writeStore({ savedVoices: saved.filter((item) => item.voiceId !== voiceId) })
|
|
901
|
+
if ((await readStore()).voiceId === voiceId) await writeStore({ voiceId: VOICE_PRESETS[0].voiceId })
|
|
902
|
+
return json(200, { ok: true, removed: 'saved' })
|
|
903
|
+
}
|
|
904
|
+
await callApi(`/voices/${voiceId}`, { method: 'DELETE', apiKey: s4.apiKey })
|
|
905
|
+
const st5 = await readStore()
|
|
906
|
+
if (st5.voiceId === voiceId) await writeStore({ voiceId: 'voc_rc894j8q2auu' })
|
|
907
|
+
return json(200, { ok: true, voiceId })
|
|
908
|
+
}
|
|
909
|
+
if (route === '/settings' && req.method === 'POST') {
|
|
910
|
+
const body = await readBody()
|
|
911
|
+
const patch = {}
|
|
912
|
+
if (typeof body.voiceId === 'string' && body.voiceId) patch.voiceId = body.voiceId
|
|
913
|
+
if (typeof body.moodId === 'string' && body.moodId) patch.moodId = body.moodId
|
|
914
|
+
if (typeof body.customMood === 'string') patch.customMood = body.customMood.slice(0, 500)
|
|
915
|
+
if (typeof body.emotionMode === 'string' && ['fixed', 'infer', 'marker'].includes(body.emotionMode)) {
|
|
916
|
+
patch.emotionMode = body.emotionMode
|
|
917
|
+
}
|
|
918
|
+
if (Array.isArray(body.savedVoices)) {
|
|
919
|
+
patch.savedVoices = body.savedVoices
|
|
920
|
+
.filter((item) => item && typeof item.voiceId === 'string')
|
|
921
|
+
.map((item) => ({
|
|
922
|
+
voiceId: item.voiceId,
|
|
923
|
+
name: String(item.name ?? item.voiceId).slice(0, 60),
|
|
924
|
+
language: String(item.language ?? '').slice(0, 8),
|
|
925
|
+
description: String(item.description ?? '').slice(0, 120),
|
|
926
|
+
}))
|
|
927
|
+
.slice(0, 50)
|
|
928
|
+
}
|
|
929
|
+
if (Array.isArray(body.tones)) {
|
|
930
|
+
patch.tones = body.tones
|
|
931
|
+
.filter((tone) => tone && typeof tone.id === 'string' && typeof tone.instructions === 'string')
|
|
932
|
+
.map((tone) => ({
|
|
933
|
+
id: String(tone.id).slice(0, 40),
|
|
934
|
+
name: String(tone.name ?? tone.id).slice(0, 40),
|
|
935
|
+
instructions: String(tone.instructions).slice(0, 500),
|
|
936
|
+
builtin: false,
|
|
937
|
+
}))
|
|
938
|
+
.slice(0, 40)
|
|
939
|
+
}
|
|
940
|
+
if (typeof body.previewText === 'string' && body.previewText) patch.previewText = body.previewText
|
|
941
|
+
if (typeof body.dailyChars === 'number' && body.dailyChars > 0) patch.dailyChars = Math.floor(body.dailyChars)
|
|
942
|
+
if (typeof body.autoSpeak === 'boolean') patch.autoSpeak = body.autoSpeak
|
|
943
|
+
if (typeof body.backgroundPlay === 'boolean') patch.backgroundPlay = body.backgroundPlay
|
|
944
|
+
if (typeof body.autoSpeakMaxChars === 'number' && body.autoSpeakMaxChars >= 0) {
|
|
945
|
+
patch.autoSpeakMaxChars = Math.max(0, Math.min(99999, Math.floor(body.autoSpeakMaxChars)))
|
|
946
|
+
}
|
|
947
|
+
const written = await writeStore(patch)
|
|
948
|
+
if (typeof written.emotionMode === 'string') emotionModeCache = written.emotionMode
|
|
949
|
+
return json(200, { ok: true, saved: patch })
|
|
950
|
+
}
|
|
951
|
+
if (route === '/health') {
|
|
952
|
+
let lastPlay = ''
|
|
953
|
+
try {
|
|
954
|
+
const lines = (await readFile(PLAY_LOG, 'utf8')).trim().split(String.fromCharCode(10))
|
|
955
|
+
lastPlay = lines.slice(-6).join(' | ')
|
|
956
|
+
} catch {
|
|
957
|
+
lastPlay = ''
|
|
958
|
+
}
|
|
959
|
+
return json(200, { ok: true, busy: fleet.busy, lastPlay })
|
|
960
|
+
}
|
|
961
|
+
return json(404, { ok: false, error: 'unknown route', route })
|
|
962
|
+
} catch (caught) {
|
|
963
|
+
return json(500, {
|
|
964
|
+
ok: false,
|
|
965
|
+
error: caught instanceof Error ? caught.message : String(caught),
|
|
966
|
+
code: caught && caught.code ? String(caught.code) : '',
|
|
967
|
+
status: caught && caught.status ? Number(caught.status) : 0,
|
|
968
|
+
})
|
|
969
|
+
}
|
|
970
|
+
},
|
|
971
|
+
}))
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
return () => {
|
|
975
|
+
for (const dispose of disposers) {
|
|
976
|
+
try {
|
|
977
|
+
dispose()
|
|
978
|
+
} catch {
|
|
979
|
+
fleet = { ...fleet }
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}, 'dsh-breeze-tts: tools and routes')
|
|
984
|
+
|
|
985
|
+
ctx.logger?.info?.(`breeze-tts ready (voice ${cfg.voiceId}, ${clip(cfg.modelId)})`)
|
|
986
|
+
}
|