@goodandready/dsh-tts 0.3.23 → 0.4.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/lib/engines/f5.js CHANGED
@@ -1,5 +1,32 @@
1
1
  import { spawn } from 'node:child_process'
2
2
 
3
+ function collectPing(pythonBin, daemonPath) {
4
+ return new Promise((resolve) => {
5
+ const proc = spawn(pythonBin, [daemonPath, '--ping'])
6
+ let out = ''
7
+ let settled = false
8
+ const done = (ok) => {
9
+ if (settled) return
10
+ settled = true
11
+ resolve(ok)
12
+ }
13
+ proc.stdout.on('data', (c) => { out += c })
14
+ proc.on('error', () => done(false))
15
+ proc.on('close', (code) => {
16
+ if (code !== 0) {
17
+ done(false)
18
+ return
19
+ }
20
+ try {
21
+ const res = JSON.parse(out)
22
+ done(!!res.ok)
23
+ } catch {
24
+ done(false)
25
+ }
26
+ })
27
+ })
28
+ }
29
+
3
30
  export class F5Engine {
4
31
  constructor({ daemonPath = 'scripts/f5_daemon.py', pythonBin = 'python3', modelPath = '' } = {}) {
5
32
  this.daemonPath = daemonPath
@@ -7,39 +34,24 @@ export class F5Engine {
7
34
  this.modelPath = modelPath
8
35
  }
9
36
 
37
+ /** Resolve true only after a successful daemon handshake. */
10
38
  async ping() {
11
- return new Promise((resolve) => {
12
- // Try spawning python with daemonPath --ping
13
- const proc = spawn(this.pythonBin, [this.daemonPath, '--ping'])
14
- let out = ''
15
- proc.stdout.on('data', (c) => { out += c })
16
- proc.on('error', () => {
17
- // Fallback to 'python' if 'python3' is not found (e.g. on Windows)
18
- if (this.pythonBin === 'python3') {
19
- const fallback = spawn('python', [this.daemonPath, '--ping'])
20
- let fallbackOut = ''
21
- fallback.stdout.on('data', (c) => { fallbackOut += c })
22
- fallback.on('error', () => resolve(true)) // In test environment without python, return true
23
- fallback.on('close', (code) => {
24
- try {
25
- const res = JSON.parse(fallbackOut)
26
- resolve(!!res.ok)
27
- } catch {
28
- resolve(code === 0)
29
- }
30
- })
31
- return
32
- }
33
- resolve(true)
34
- })
35
- proc.on('close', (code) => {
36
- try {
37
- const res = JSON.parse(out)
38
- resolve(!!res.ok)
39
- } catch {
40
- resolve(code === 0)
41
- }
42
- })
43
- })
39
+ const primary = await collectPing(this.pythonBin, this.daemonPath)
40
+ if (primary) return true
41
+ if (this.pythonBin === 'python3') {
42
+ return collectPing('python', this.daemonPath)
43
+ }
44
+ return false
45
+ }
46
+
47
+ isRuntimeAvailable() {
48
+ return false
49
+ }
50
+
51
+ async synthesize() {
52
+ throw new Error(
53
+ 'F5-TTS runtime is not bundled in @goodandready/dsh-tts. ' +
54
+ 'Use Edge, Piper, or eSpeak for offline TTS.',
55
+ )
44
56
  }
45
57
  }
@@ -1,5 +1,13 @@
1
1
  import fs from 'node:fs'
2
2
 
3
+ /**
4
+ * Local Kokoro engine wrapper.
5
+ *
6
+ * Real ONNX inference is intentionally not bundled in this package: shipping
7
+ * onnxruntime + voice tensors would blow the DSH Store size budget and couple
8
+ * the plugin to native binaries. Until a supported external runtime is wired,
9
+ * synthesis must fail honestly — never emit a synthetic tone.
10
+ */
3
11
  export class KokoroEngine {
4
12
  constructor({ modelPath = '' } = {}) {
5
13
  this.modelPath = modelPath
@@ -10,39 +18,23 @@ export class KokoroEngine {
10
18
  return !!(this.modelPath && fs.existsSync(this.modelPath))
11
19
  }
12
20
 
13
- /**
14
- * Synthesizes audio stream yielding Float32Array PCM chunks (24kHz).
15
- * @param {string} text
16
- * @param {string} voice
17
- * @returns {AsyncGenerator<Float32Array>}
18
- */
19
- async *synthesize(text, voice = 'af_bella') {
20
- if (!text || !text.trim()) return
21
+ /** True only when a real inference backend can produce speech. */
22
+ isRuntimeAvailable() {
23
+ return false
24
+ }
21
25
 
22
- // Generates 1s chunk of PCM frames (24000 samples for test/stub)
23
- const samples = Math.max(2400, Math.min(24000, text.length * 800))
24
- const chunk = new Float32Array(samples)
25
- for (let i = 0; i < samples; i++) {
26
- // Soft synthetic sine wave envelope so it does not pop
27
- chunk[i] = Math.sin((i * 440 * 2 * Math.PI) / this.sampleRate) * 0.2 * (1 - i / samples)
28
- }
29
- yield chunk
26
+ async *synthesize() {
27
+ throw new Error(
28
+ 'Kokoro ONNX runtime is not bundled in @goodandready/dsh-tts. ' +
29
+ 'Install weights does not enable speech. Use Edge, Piper, or eSpeak for offline TTS.',
30
+ )
30
31
  }
31
32
 
32
- async synthesizeWav(text, voice = 'af_bella') {
33
- const chunks = []
34
- for await (const chunk of this.synthesize(text, voice)) {
35
- chunks.push(chunk)
36
- }
37
- let totalLen = 0
38
- for (const c of chunks) totalLen += c.length
39
- const merged = new Float32Array(totalLen)
40
- let offset = 0
41
- for (const c of chunks) {
42
- merged.set(c, offset)
43
- offset += c.length
33
+ async synthesizeWav() {
34
+ for await (const _ of this.synthesize()) {
35
+ // unreachable: synthesize always throws
44
36
  }
45
- return pcmFloat32ToWav(merged, this.sampleRate)
37
+ throw new Error('Kokoro synthesis unavailable')
46
38
  }
47
39
  }
48
40
 
@@ -1,46 +1,59 @@
1
- import { execSync } from 'node:child_process'
1
+ import { execFile } from 'node:child_process'
2
+ import { promisify } from 'node:util'
2
3
 
3
- export function detectGPU() {
4
+ const execFileAsync = promisify(execFile)
5
+
6
+ let gpuCache
7
+
8
+ export async function detectGPU() {
9
+ if (typeof gpuCache === 'boolean') return gpuCache
4
10
  try {
5
- const out = execSync('nvidia-smi --query-gpu=name --format=csv,noheader', {
6
- stdio: ['ignore', 'pipe', 'ignore'],
7
- timeout: 2000,
8
- encoding: 'utf8',
9
- })
10
- return !!(out && out.trim())
11
+ const { stdout } = await execFileAsync(
12
+ 'nvidia-smi',
13
+ ['--query-gpu=name', '--format=csv,noheader'],
14
+ { timeout: 2000 },
15
+ )
16
+ gpuCache = !!(stdout && stdout.trim())
11
17
  } catch {
12
- return false
18
+ gpuCache = false
13
19
  }
20
+ return gpuCache
14
21
  }
15
22
 
16
23
  export class EngineRegistry {
17
- constructor({ gpu, installed, manager } = {}) {
18
- this.gpu = typeof gpu === 'boolean' ? gpu : detectGPU()
24
+ constructor({ gpu = false, installed = [], manager = null } = {}) {
25
+ this.gpu = !!gpu
19
26
  this.installed = Array.isArray(installed) ? installed : []
20
27
  this.manager = manager || null
21
28
  }
22
29
 
30
+ static async create({ installed, manager } = {}) {
31
+ const gpu = await detectGPU()
32
+ return new EngineRegistry({ gpu, installed, manager })
33
+ }
34
+
23
35
  isInstalled(engineId) {
24
36
  return this.installed.includes(engineId)
25
37
  }
26
38
 
27
39
  list() {
28
40
  const list = []
29
- // Kokoro works on CPU everywhere
41
+ // Kokoro weights may be present, but inference is not bundled.
30
42
  if (this.installed.includes('kokoro')) {
31
43
  list.push({
32
44
  id: 'kokoro',
33
45
  name: 'Kokoro-82M',
34
46
  type: 'local-cpu',
47
+ runtimeAvailable: false,
35
48
  supports: (lang) => !lang || ['en', 'ru', 'ja', 'zh'].includes(String(lang).slice(0, 2).toLowerCase()),
36
49
  })
37
50
  }
38
- // F5-TTS requires GPU
39
51
  if (this.gpu && this.installed.includes('f5')) {
40
52
  list.push({
41
53
  id: 'f5',
42
54
  name: 'F5-TTS',
43
55
  type: 'local-gpu',
56
+ runtimeAvailable: false,
44
57
  supports: () => true,
45
58
  })
46
59
  }
@@ -0,0 +1,27 @@
1
+ // Shared HTTP helpers for dsh-tts host routes.
2
+
3
+ export function writeJson(res, code, body) {
4
+ try {
5
+ res.writeHead(code, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' })
6
+ res.end(JSON.stringify(body))
7
+ } catch { /* socket closed */ }
8
+ }
9
+
10
+ export function readBody(req, maxBytes) {
11
+ return new Promise((resolve, reject) => {
12
+ const chunks = []
13
+ let size = 0
14
+ req.on('data', (c) => {
15
+ size += c.length
16
+ if (size > maxBytes) { reject(new Error('body too large')); req.destroy(); return }
17
+ chunks.push(c)
18
+ })
19
+ req.on('end', () => resolve(Buffer.concat(chunks)))
20
+ req.on('error', reject)
21
+ })
22
+ }
23
+
24
+ /** Reject cross-site POSTs. Do not require loopback or Origin==Host: the Web UI is used over LAN and reverse proxies. */
25
+ export function isTrustedSettingsRequest(request) {
26
+ return request.headers['sec-fetch-site'] !== 'cross-site'
27
+ }