@goodandready/dsh-voice 0.9.46 → 0.9.48

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.
@@ -0,0 +1,502 @@
1
+ // lib/provider-registry.js — Strategy pattern and extensible registry for STT providers (#306)
2
+ import { readHttpError, safeJson, isAutoLang, fileName, chatAudioFormat } from './provider-http.js'
3
+
4
+ const CHAT_AUDIO_PROMPT =
5
+ 'Transcribe the audio verbatim. Reply with the transcript text only, '
6
+ + 'without comments, quotes or formatting.'
7
+
8
+ function bindNamedRunner(name, fn) {
9
+ Object.defineProperty(fn, 'name', { value: name, configurable: true })
10
+ return fn
11
+ }
12
+
13
+ /**
14
+ * Abstract base strategy for Speech-to-Text providers.
15
+ */
16
+ export class SpeechToTextProvider {
17
+ /**
18
+ * @param {{ key: string, kind?: 'browser' | 'cloud' | 'local' | 'custom', defaultModel?: string, requiresWav?: boolean }} meta
19
+ */
20
+ constructor(meta) {
21
+ if (!meta || !meta.key) throw new Error('SpeechToTextProvider requires a non-empty key')
22
+ this.key = String(meta.key)
23
+ this.kind = meta.kind || 'cloud'
24
+ this.defaultModel = meta.defaultModel || ''
25
+ this.requiresWav = Boolean(meta.requiresWav)
26
+ }
27
+
28
+ pickModel(models) {
29
+ const chosen = models && typeof models[this.key] === 'string' ? models[this.key].trim() : ''
30
+ return chosen || this.defaultModel
31
+ }
32
+
33
+ /**
34
+ * Validate configuration for this provider.
35
+ * @param {Record<string, any>} _cfg
36
+ * @returns {{ valid: boolean, reason?: string }}
37
+ */
38
+ validateConfig(_cfg) {
39
+ return { valid: true }
40
+ }
41
+
42
+ /**
43
+ * Check provider readiness/health.
44
+ * @param {Record<string, any>} _deps
45
+ * @returns {Promise<{ ok: boolean, reason?: string }>}
46
+ */
47
+ async healthcheck(_deps) {
48
+ return { ok: true }
49
+ }
50
+
51
+ /**
52
+ * Execute speech recognition strategy.
53
+ * @param {Record<string, any>} _deps
54
+ * @param {Record<string, any>} _req
55
+ * @returns {Promise<{ ok: boolean, provider: string, text?: string, reason?: string }>}
56
+ */
57
+ async transcribe(_deps, _req) {
58
+ return { ok: false, provider: this.key, reason: `${this.key}: not implemented` }
59
+ }
60
+
61
+ /**
62
+ * Create a zero-arg chain runner bound to deps and request context.
63
+ */
64
+ createRunner(deps, req) {
65
+ return bindNamedRunner(this.key, async () => this.transcribe(deps, req))
66
+ }
67
+ }
68
+
69
+ export class BrowserStubProvider extends SpeechToTextProvider {
70
+ constructor(key = 'browser', reasonSuffix = 'recognition runs in the page, the host has no browser engine') {
71
+ super({ key, kind: 'browser' })
72
+ this.reasonSuffix = reasonSuffix
73
+ }
74
+
75
+ async transcribe() {
76
+ return {
77
+ ok: false,
78
+ provider: this.key,
79
+ reason: `${this.key}: ${this.reasonSuffix}`,
80
+ }
81
+ }
82
+ }
83
+
84
+ export class DeepgramProvider extends SpeechToTextProvider {
85
+ constructor(defaultModel = 'nova-2') {
86
+ super({ key: 'deepgram', kind: 'cloud', defaultModel })
87
+ }
88
+
89
+ validateConfig(cfg) {
90
+ if (!cfg?.deepgramKeyEnv) return { valid: false, reason: 'missing deepgramKeyEnv' }
91
+ return { valid: true }
92
+ }
93
+
94
+ async healthcheck({ resolveKey, cfg }) {
95
+ const key = await resolveKey?.(cfg?.deepgramKeyEnv)
96
+ return key ? { ok: true } : { ok: false, reason: `no ${cfg?.deepgramKeyEnv || 'DEEPGRAM_API_KEY'}` }
97
+ }
98
+
99
+ async transcribe(deps, req) {
100
+ const { resolveKey, fetchImpl, cfg } = deps
101
+ const { bytes, mime, lang, signal, models, effectiveKeywords } = req
102
+ const key = await resolveKey(cfg.deepgramKeyEnv)
103
+ if (!key) return { ok: false, provider: 'deepgram', reason: `no ${cfg.deepgramKeyEnv}` }
104
+ const model = this.pickModel(models)
105
+ const base = String(cfg.deepgramBaseUrl || 'https://api.deepgram.com').replace(/\/+$/, '')
106
+ const url = `${base}/v1/listen?model=${encodeURIComponent(model)}`
107
+ + (!isAutoLang(lang) ? `&language=${encodeURIComponent(lang)}` : '')
108
+ + (effectiveKeywords ? `&keywords=${encodeURIComponent(effectiveKeywords)}` : '')
109
+ + '&smart_format=true'
110
+ const res = await fetchImpl(url, {
111
+ method: 'POST',
112
+ headers: { authorization: `Token ${key}`, 'content-type': mime },
113
+ body: bytes,
114
+ signal,
115
+ keepalive: true,
116
+ })
117
+ if (!res.ok) throw await readHttpError(res, 'Deepgram', 'deepgram')
118
+ let data
119
+ try { data = await safeJson(res, 'Deepgram') } catch (e) {
120
+ return { ok: false, provider: 'deepgram', reason: e.message }
121
+ }
122
+ const text = (data?.results?.channels?.[0]?.alternatives?.[0]?.transcript || '').trim()
123
+ return { ok: text.length > 0, provider: 'deepgram', text, reason: text ? '' : 'empty transcript' }
124
+ }
125
+ }
126
+
127
+ export class GroqProvider extends SpeechToTextProvider {
128
+ constructor(defaultModel = 'whisper-large-v3-turbo') {
129
+ super({ key: 'groq', kind: 'cloud', defaultModel })
130
+ }
131
+
132
+ validateConfig(cfg) {
133
+ if (!cfg?.groqKeyEnv) return { valid: false, reason: 'missing groqKeyEnv' }
134
+ return { valid: true }
135
+ }
136
+
137
+ async healthcheck({ resolveKey, cfg }) {
138
+ const key = await resolveKey?.(cfg?.groqKeyEnv)
139
+ return key ? { ok: true } : { ok: false, reason: `no ${cfg?.groqKeyEnv || 'GROQ_API_KEY'}` }
140
+ }
141
+
142
+ async transcribe(deps, req) {
143
+ const { resolveKey, fetchImpl, cfg } = deps
144
+ const { bytes, mime, lang, signal, models, effectivePrompt } = req
145
+ const key = await resolveKey(cfg.groqKeyEnv)
146
+ if (!key) return { ok: false, provider: 'groq', reason: `no ${cfg.groqKeyEnv}` }
147
+ const form = new FormData()
148
+ form.append('file', new Blob([bytes], { type: mime }), fileName(mime))
149
+ form.append('model', this.pickModel(models))
150
+ if (!isAutoLang(lang)) form.append('language', lang)
151
+ if (effectivePrompt) form.append('prompt', effectivePrompt)
152
+ form.append('response_format', 'json')
153
+ const res = await fetchImpl('https://api.groq.com/openai/v1/audio/transcriptions', {
154
+ method: 'POST',
155
+ headers: { authorization: `Bearer ${key}` },
156
+ body: form,
157
+ signal,
158
+ keepalive: true,
159
+ })
160
+ if (!res.ok) throw await readHttpError(res, 'Groq', 'groq')
161
+ let data
162
+ try { data = await safeJson(res, 'Groq') } catch (e) {
163
+ return { ok: false, provider: 'groq', reason: e.message }
164
+ }
165
+ const text = (data?.text || '').trim()
166
+ return { ok: text.length > 0, provider: 'groq', text, reason: text ? '' : 'empty transcript' }
167
+ }
168
+ }
169
+
170
+ export class HuggingFaceProvider extends SpeechToTextProvider {
171
+ constructor(defaultModel = 'openai/whisper-large-v3') {
172
+ super({ key: 'hf', kind: 'cloud', defaultModel })
173
+ }
174
+
175
+ validateConfig(cfg) {
176
+ if (!cfg?.hfTokenEnv) return { valid: false, reason: 'missing hfTokenEnv' }
177
+ return { valid: true }
178
+ }
179
+
180
+ async healthcheck({ resolveKey, cfg }) {
181
+ const token = await resolveKey?.(cfg?.hfTokenEnv)
182
+ return token ? { ok: true } : { ok: false, reason: `no ${cfg?.hfTokenEnv || 'HF_TOKEN'}` }
183
+ }
184
+
185
+ async transcribe(deps, req) {
186
+ const { resolveKey, fetchImpl, cfg } = deps
187
+ const { bytes, mime, signal, models } = req
188
+ const token = await resolveKey(cfg.hfTokenEnv)
189
+ if (!token) return { ok: false, provider: 'hf', reason: `no ${cfg.hfTokenEnv}` }
190
+ const model = this.pickModel(models)
191
+ const res = await fetchImpl(`https://router.huggingface.co/hf-inference/models/${model}`, {
192
+ method: 'POST',
193
+ headers: { authorization: `Bearer ${token}`, 'content-type': mime, 'x-wait-for-model': 'true' },
194
+ body: bytes,
195
+ signal,
196
+ keepalive: true,
197
+ })
198
+ if (!res.ok) throw await readHttpError(res, 'HF', 'hf')
199
+ let data
200
+ try { data = await safeJson(res, 'HF') } catch (e) {
201
+ return { ok: false, provider: 'hf', reason: e.message }
202
+ }
203
+ const text = (data?.text || '').trim()
204
+ return { ok: text.length > 0, provider: 'hf', text, reason: text ? '' : 'empty transcript' }
205
+ }
206
+ }
207
+
208
+ export class LocalWhisperProvider extends SpeechToTextProvider {
209
+ constructor() {
210
+ super({ key: 'local-whisper', kind: 'local', defaultModel: '', requiresWav: true })
211
+ }
212
+
213
+ validateConfig(cfg) {
214
+ if (!cfg?.whisperUrl) return { valid: false, reason: 'missing whisperUrl' }
215
+ return { valid: true }
216
+ }
217
+
218
+ async transcribe(deps, req) {
219
+ const { fetchImpl, cfg } = deps
220
+ const { bytes, mime, lang, signal, effectivePrompt } = req
221
+ let sendBytes = bytes
222
+ let sendMime = mime
223
+ if (!mime.includes('wav')) {
224
+ if (typeof deps.toWav !== 'function') {
225
+ return { ok: false, provider: 'local-whisper', reason: 'local whisper needs WAV, no converter configured' }
226
+ }
227
+ try {
228
+ sendBytes = await deps.toWav(bytes, signal)
229
+ sendMime = 'audio/wav'
230
+ } catch (e) {
231
+ return { ok: false, provider: 'local-whisper', reason: `local whisper: ${String(e && e.message || e)}` }
232
+ }
233
+ }
234
+ const form = new FormData()
235
+ form.append('file', new Blob([sendBytes], { type: sendMime }), fileName(sendMime))
236
+ form.append('language', isAutoLang(lang) ? 'auto' : lang)
237
+ form.append('translate', 'false')
238
+ if (effectivePrompt) form.append('prompt', effectivePrompt)
239
+ form.append('response_format', 'json')
240
+ const res = await fetchImpl(cfg.whisperUrl, { method: 'POST', body: form, signal })
241
+ if (!res.ok) {
242
+ let detail = `HTTP ${res.status}`
243
+ try { const e = await res.json(); if (e?.error) detail = e.error } catch { /* body is not json */ }
244
+ return { ok: false, provider: 'local-whisper', reason: `local whisper: ${detail}` }
245
+ }
246
+ let data
247
+ try { data = await safeJson(res, 'local whisper') } catch (e) {
248
+ return { ok: false, provider: 'local-whisper', reason: e.message }
249
+ }
250
+ const text = (data?.text || '').trim()
251
+ return { ok: text.length > 0, provider: 'local-whisper', text, reason: text ? '' : 'empty transcript' }
252
+ }
253
+ }
254
+
255
+ export class SenseVoiceProvider extends SpeechToTextProvider {
256
+ constructor(defaultModel = 'SenseVoiceSmall') {
257
+ super({ key: 'sensevoice', kind: 'local', defaultModel, requiresWav: true })
258
+ }
259
+
260
+ validateConfig(cfg) {
261
+ if (!cfg?.sensevoiceUrl) return { valid: false, reason: 'missing sensevoiceUrl' }
262
+ return { valid: true }
263
+ }
264
+
265
+ async transcribe(deps, req) {
266
+ const { fetchImpl, cfg } = deps
267
+ const { bytes, mime, lang, signal, models, effectivePrompt } = req
268
+ const url = String(cfg.sensevoiceUrl || 'http://127.0.0.1:6006/api/v1/asr').trim()
269
+ let sendBytes = bytes
270
+ let sendMime = mime
271
+ if (!mime.includes('wav')) {
272
+ if (typeof deps.toWav !== 'function') {
273
+ return { ok: false, provider: 'sensevoice', reason: 'sensevoice needs WAV, no converter configured' }
274
+ }
275
+ try {
276
+ sendBytes = await deps.toWav(bytes, signal)
277
+ sendMime = 'audio/wav'
278
+ } catch (e) {
279
+ return { ok: false, provider: 'sensevoice', reason: `sensevoice: ${String(e && e.message || e)}` }
280
+ }
281
+ }
282
+ const form = new FormData()
283
+ form.append('file', new Blob([sendBytes], { type: sendMime }), fileName(sendMime))
284
+ const isOpenAI = url.includes('/transcriptions')
285
+ if (isOpenAI) {
286
+ form.append('model', this.pickModel(models))
287
+ if (!isAutoLang(lang)) form.append('language', lang)
288
+ if (effectivePrompt) form.append('prompt', effectivePrompt)
289
+ form.append('response_format', 'json')
290
+ }
291
+
292
+ let res
293
+ try {
294
+ res = await fetchImpl(url, { method: 'POST', body: form, signal })
295
+ } catch (e) {
296
+ return { ok: false, provider: 'sensevoice', reason: `sensevoice: ${String(e && e.message || e)}` }
297
+ }
298
+
299
+ if (!res.ok) {
300
+ let detail = `HTTP ${res.status}`
301
+ try { const e = await res.json(); if (e?.error) detail = e.error } catch { /* body is not json */ }
302
+ return { ok: false, provider: 'sensevoice', reason: `sensevoice: ${detail}` }
303
+ }
304
+
305
+ let data
306
+ try {
307
+ data = await res.json()
308
+ } catch {
309
+ return { ok: false, provider: 'sensevoice', reason: 'sensevoice: invalid json response' }
310
+ }
311
+
312
+ let text = String(data?.text || data?.transcript || '').trim()
313
+ text = text.replace(/<\|[^|>]+\|>/g, '').trim()
314
+ return {
315
+ ok: text.length > 0,
316
+ provider: 'sensevoice',
317
+ text,
318
+ reason: text ? '' : 'empty transcript',
319
+ }
320
+ }
321
+ }
322
+
323
+ export class OpenAICompatibleProvider extends SpeechToTextProvider {
324
+ /**
325
+ * @param {{ key: string, template?: string, baseURL?: string, model?: string, keyEnv?: string, prompt?: string }} spec
326
+ */
327
+ constructor(spec) {
328
+ super({
329
+ key: spec.key,
330
+ kind: 'custom',
331
+ defaultModel: spec.model || '',
332
+ })
333
+ this.spec = spec
334
+ }
335
+
336
+ validateConfig() {
337
+ if (!this.spec?.baseURL) return { valid: false, reason: `${this.key}: no baseURL` }
338
+ if (!this.spec?.model) return { valid: false, reason: `${this.key}: no model` }
339
+ return { valid: true }
340
+ }
341
+
342
+ async transcribe(deps, req) {
343
+ const { resolveKey, fetchImpl } = deps
344
+ const { bytes, mime, lang, signal, models, effectivePrompt } = req
345
+ const label = this.key
346
+ const base = String(this.spec.baseURL || '').replace(/\/+$/, '')
347
+ const chosen = models && typeof models[label] === 'string' ? models[label].trim() : ''
348
+ const model = chosen || this.spec.model
349
+
350
+ if (!base) return { ok: false, provider: label, reason: `${label}: no baseURL` }
351
+ if (!model) return { ok: false, provider: label, reason: `${label}: no model` }
352
+
353
+ let headers = {}
354
+ if (this.spec.keyEnv) {
355
+ const key = await resolveKey(this.spec.keyEnv)
356
+ if (!key) return { ok: false, provider: label, reason: `no ${this.spec.keyEnv}` }
357
+ headers = { authorization: `Bearer ${key}` }
358
+ }
359
+
360
+ const viaTranscriptions = async () => {
361
+ const form = new FormData()
362
+ form.append('file', new Blob([bytes], { type: mime }), fileName(mime))
363
+ form.append('model', model)
364
+ if (lang && lang !== 'auto') form.append('language', lang)
365
+ if (effectivePrompt) form.append('prompt', effectivePrompt)
366
+ form.append('response_format', 'json')
367
+ const res = await fetchImpl(`${base}/audio/transcriptions`, {
368
+ method: 'POST', headers, body: form, signal,
369
+ })
370
+ if (!res.ok) throw await readHttpError(res, label, label)
371
+ const data = await safeJson(res, label)
372
+ return (data?.text || '').trim()
373
+ }
374
+
375
+ const viaChatAudio = async () => {
376
+ let sendBytes = bytes
377
+ let format = chatAudioFormat(mime)
378
+ if (!format) {
379
+ if (typeof deps.toWav !== 'function') {
380
+ throw new Error(`${label}: chat template needs wav or mp3, unsupported format ${mime}`)
381
+ }
382
+ sendBytes = await deps.toWav(bytes, signal)
383
+ format = 'wav'
384
+ }
385
+ const ask = (this.spec.prompt || CHAT_AUDIO_PROMPT)
386
+ + (effectivePrompt ? ` Vocabulary hints (spell these correctly): ${effectivePrompt}.` : '')
387
+ + (lang && lang !== 'auto' ? ` The audio language is ${lang}.` : '')
388
+ const res = await fetchImpl(`${base}/chat/completions`, {
389
+ method: 'POST',
390
+ headers: { ...headers, 'content-type': 'application/json' },
391
+ body: JSON.stringify({
392
+ model,
393
+ messages: [{
394
+ role: 'user',
395
+ content: [
396
+ { type: 'text', text: ask },
397
+ { type: 'input_audio', input_audio: { data: Buffer.from(sendBytes).toString('base64'), format } },
398
+ ],
399
+ }],
400
+ }),
401
+ signal,
402
+ })
403
+ if (!res.ok) throw await readHttpError(res, label, label)
404
+ const data = await safeJson(res, label)
405
+ return String(data?.choices?.[0]?.message?.content || '').trim()
406
+ }
407
+
408
+ let text
409
+ try {
410
+ text = this.spec.template === 'openai-chat-audio'
411
+ ? await viaChatAudio()
412
+ : await viaTranscriptions()
413
+ } catch (e) {
414
+ return { ok: false, provider: label, reason: `${label}: ${String(e && e.message || e)}` }
415
+ }
416
+ return { ok: text.length > 0, provider: label, text, reason: text ? '' : 'empty transcript' }
417
+ }
418
+ }
419
+
420
+ /**
421
+ * Registry managing STT provider strategies and instantiating chain runners.
422
+ */
423
+ export class ProviderRegistry {
424
+ constructor() {
425
+ /** @type {Map<string, SpeechToTextProvider>} */
426
+ this.providers = new Map()
427
+ /** @type {Set<string>} */
428
+ this.builtinKeys = new Set()
429
+ }
430
+
431
+ /**
432
+ * Register a provider strategy.
433
+ * @param {SpeechToTextProvider} strategy
434
+ * @param {{ builtin?: boolean }} [options]
435
+ */
436
+ register(strategy, options = {}) {
437
+ if (!(strategy instanceof SpeechToTextProvider)) {
438
+ throw new TypeError('ProviderRegistry.register requires a SpeechToTextProvider instance')
439
+ }
440
+ this.providers.set(strategy.key, strategy)
441
+ if (options.builtin) {
442
+ this.builtinKeys.add(strategy.key)
443
+ }
444
+ return this
445
+ }
446
+
447
+ get(key) {
448
+ return this.providers.get(key)
449
+ }
450
+
451
+ has(key) {
452
+ return this.providers.has(key)
453
+ }
454
+
455
+ keys() {
456
+ return Array.from(this.providers.keys())
457
+ }
458
+
459
+ /**
460
+ * Build execution runners for runChain(), applying presets and customProviders.
461
+ */
462
+ buildRunners(deps, req) {
463
+ const vocab = Array.isArray(req.vocabulary)
464
+ ? req.vocabulary.map((w) => String(w || '').trim()).filter(Boolean).join(', ')
465
+ : ''
466
+ const effectivePrompt = (typeof req.promptText === 'string' && req.promptText.trim()) ? req.promptText.trim() : vocab
467
+ const effectiveKeywords = (typeof req.keywords === 'string' && req.keywords.trim()) ? req.keywords.trim() : vocab
468
+ const enrichedReq = { ...req, effectivePrompt, effectiveKeywords }
469
+
470
+ const out = {}
471
+ for (const [key, strategy] of this.providers.entries()) {
472
+ out[key] = strategy.createRunner(deps, enrichedReq)
473
+ }
474
+
475
+ const customList = Array.isArray(deps?.cfg?.customProviders) ? deps.cfg.customProviders : []
476
+ for (const spec of customList) {
477
+ const key = String(spec && spec.key || '').trim()
478
+ if (!key || this.builtinKeys.has(key)) continue
479
+ const customStrategy = new OpenAICompatibleProvider({ ...spec, key })
480
+ out[key] = customStrategy.createRunner(deps, enrichedReq)
481
+ }
482
+ return out
483
+ }
484
+ }
485
+
486
+ export function createDefaultProviderRegistry(presetProviders = {}) {
487
+ const registry = new ProviderRegistry()
488
+ registry.register(new BrowserStubProvider('browser', 'recognition runs in the page, the host has no browser engine'), { builtin: true })
489
+ registry.register(new BrowserStubProvider('browser-webgpu', 'recognition runs in the page via WebGPU/WASM, the host has no browser engine'), { builtin: true })
490
+ registry.register(new DeepgramProvider(), { builtin: true })
491
+ registry.register(new GroqProvider(), { builtin: true })
492
+ registry.register(new HuggingFaceProvider(), { builtin: true })
493
+ registry.register(new LocalWhisperProvider(), { builtin: true })
494
+ registry.register(new SenseVoiceProvider(), { builtin: true })
495
+
496
+ for (const [key, spec] of Object.entries(presetProviders)) {
497
+ if (!registry.has(key)) {
498
+ registry.register(new OpenAICompatibleProvider({ ...spec, key }), { builtin: false })
499
+ }
500
+ }
501
+ return registry
502
+ }