@goodandready/dsh-voice 0.9.47 → 0.9.49

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/client.js CHANGED
@@ -315,6 +315,86 @@ window.__ModuleLoader__.load({
315
315
  tag.dataset.pluginCss = cssId
316
316
  document.head.appendChild(tag)
317
317
  }
318
+ // Centralized API Client / DAO layer for all backend HTTP and WebSocket calls (#307)
319
+ const apiClient = {
320
+ getTokenHeaders() {
321
+ return typeof window !== 'undefined' && window.__dshVoiceToken
322
+ ? { 'x-dsh-voice-token': window.__dshVoiceToken }
323
+ : {}
324
+ },
325
+ getStatus(opts = {}) {
326
+ return fetch('/dsh-voice/status', { cache: 'no-store', ...opts })
327
+ },
328
+ getConfig() {
329
+ return fetch('/dsh-voice/config', { cache: 'no-store' })
330
+ },
331
+ putConfig(payload) {
332
+ return fetch('/dsh-voice/config', {
333
+ method: 'PUT',
334
+ headers: { 'Content-Type': 'application/json', ...this.getTokenHeaders() },
335
+ body: JSON.stringify(payload),
336
+ })
337
+ },
338
+ transcribe(payload, opts = {}) {
339
+ return fetch('/dsh-voice/transcribe', {
340
+ method: 'POST',
341
+ headers: { 'content-type': 'application/json', ...this.getTokenHeaders() },
342
+ body: JSON.stringify(payload),
343
+ signal: opts.signal,
344
+ })
345
+ },
346
+ polish(payload) {
347
+ return fetch('/dsh-voice/polish', {
348
+ method: 'POST',
349
+ headers: { 'content-type': 'application/json', ...this.getTokenHeaders() },
350
+ body: JSON.stringify(payload),
351
+ })
352
+ },
353
+ getSensevoiceInstaller() {
354
+ return fetch('/dsh-voice/sensevoice-installer')
355
+ },
356
+ installSensevoice(modelType = 'sensevoice') {
357
+ return fetch('/dsh-voice/sensevoice-installer', {
358
+ method: 'POST',
359
+ headers: {
360
+ 'x-dsh-plugin-update': '1',
361
+ 'Content-Type': 'application/json',
362
+ },
363
+ body: JSON.stringify({ model: modelType }),
364
+ })
365
+ },
366
+ getUpdateStatus() {
367
+ return fetch('/api/dsh-voice/update', {
368
+ headers: { accept: 'application/json' },
369
+ cache: 'no-store',
370
+ })
371
+ },
372
+ triggerUpdate() {
373
+ return fetch('/api/dsh-voice/update', {
374
+ method: 'POST',
375
+ headers: {
376
+ 'content-type': 'application/json',
377
+ 'x-dsh-plugin-update': '1',
378
+ },
379
+ cache: 'no-store',
380
+ })
381
+ },
382
+ runBenchmark() {
383
+ return fetch('/dsh-voice/benchmark', {
384
+ method: 'POST',
385
+ headers: {
386
+ 'x-dsh-plugin-update': '1',
387
+ 'Content-Type': 'application/json',
388
+ },
389
+ })
390
+ },
391
+ exportDiagnostics() {
392
+ return fetch('/dsh-voice/diagnostics/export')
393
+ },
394
+ openWebSocket(url) {
395
+ return new WebSocket(url)
396
+ },
397
+ }
318
398
  const voice = {
319
399
  phase: 'idle', // idle | recording | processing | pending | error
320
400
  mode: 'dictation', // dictation | message
@@ -572,10 +652,7 @@ window.__ModuleLoader__.load({
572
652
  const timer = controller ? setTimeout(() => controller.abort(), 60000) : null
573
653
  let res = null
574
654
  try {
575
- res = await fetch('/dsh-voice/transcribe', {
576
- method: 'POST',
577
- headers: { 'content-type': 'application/json', ...(typeof window !== 'undefined' && window.__dshVoiceToken ? { 'x-dsh-voice-token': window.__dshVoiceToken } : {}) },
578
- body: JSON.stringify(payload),
655
+ res = await apiClient.transcribe(payload, {
579
656
  signal: controller ? controller.signal : undefined,
580
657
  })
581
658
  } catch (e) {
@@ -1060,7 +1137,7 @@ class ASRWebSocketStream {
1060
1137
  return new Promise((resolve, reject) => {
1061
1138
  let settled = false
1062
1139
  try {
1063
- this.ws = new WebSocket(url)
1140
+ this.ws = apiClient.openWebSocket(url)
1064
1141
  } catch (err) {
1065
1142
  return reject(err)
1066
1143
  }
@@ -1145,7 +1222,7 @@ voice.streamingWs = {
1145
1222
  voice._chainsPromise = null
1146
1223
  function modeChain(mode) {
1147
1224
  if (!voice._chainsPromise) {
1148
- voice._chainsPromise = fetch('/dsh-voice/status', { cache: 'no-store' })
1225
+ voice._chainsPromise = apiClient.getStatus()
1149
1226
  .then((res) => res.json())
1150
1227
  .then((data) => {
1151
1228
  if (data?.sessionToken && typeof window !== 'undefined') window.__dshVoiceToken = data.sessionToken
@@ -1647,11 +1724,7 @@ voice._chainsPromise = null
1647
1724
  let polishedText = text
1648
1725
  if (usePreview && voice.settings.polishSend) {
1649
1726
  try {
1650
- const pRes = await fetch('/dsh-voice/polish', {
1651
- method: 'POST',
1652
- headers: { 'content-type': 'application/json', ...(typeof window !== 'undefined' && window.__dshVoiceToken ? { 'x-dsh-voice-token': window.__dshVoiceToken } : {}) },
1653
- body: JSON.stringify({ text }),
1654
- })
1727
+ const pRes = await apiClient.polish({ text })
1655
1728
  const pData = await pRes.json()
1656
1729
  if (pData && pData.text) polishedText = pData.text
1657
1730
  } catch (_) {}
@@ -2077,11 +2150,7 @@ function submitPendingPreview(voice) {
2077
2150
  }
2078
2151
  } else {
2079
2152
  if (voice.settings.polishSend) {
2080
- fetch('/dsh-voice/polish', {
2081
- method: 'POST',
2082
- headers: { 'content-type': 'application/json' },
2083
- body: JSON.stringify({ text: textToSend }),
2084
- })
2153
+ apiClient.polish({ text: textToSend })
2085
2154
  .then((res) => res.json())
2086
2155
  .then((data) => {
2087
2156
  const polished = (data && data.text) || textToSend
@@ -2729,7 +2798,7 @@ function cycleDictationMode() {
2729
2798
  let alive = true
2730
2799
 
2731
2800
  const reload = () => {
2732
- fetch('/dsh-voice/status', { cache: 'no-store' })
2801
+ apiClient.getStatus()
2733
2802
  .then((res) => res.json())
2734
2803
  .then((data) => {
2735
2804
  if (!alive) return
@@ -2996,7 +3065,7 @@ function cycleDictationMode() {
2996
3065
  setExporting(true)
2997
3066
  setExportMsg('')
2998
3067
  try {
2999
- const res = await fetch('/dsh-voice/diagnostics/export')
3068
+ const res = await apiClient.exportDiagnostics()
3000
3069
  const data = await res.json()
3001
3070
  if (!res.ok || !data || !data.ok) {
3002
3071
  throw new Error((data && data.error && data.error.message) || `HTTP ${res.status}`)
@@ -3213,13 +3282,7 @@ function DictationModesSection(props) {
3213
3282
  const runBenchmark = () => {
3214
3283
  setBenchmarking(true)
3215
3284
  setBenchmarkErr('')
3216
- fetch('/dsh-voice/benchmark', {
3217
- method: 'POST',
3218
- headers: {
3219
- 'x-dsh-plugin-update': '1',
3220
- 'Content-Type': 'application/json',
3221
- },
3222
- })
3285
+ apiClient.runBenchmark()
3223
3286
  .then((r) => r.json())
3224
3287
  .then((d) => {
3225
3288
  setBenchmarking(false)
@@ -3514,7 +3577,7 @@ function DictationModesSection(props) {
3514
3577
  const [installingSensevoice, setInstallingSensevoice] = React.useState(false)
3515
3578
 
3516
3579
  const fetchSensevoice = () => {
3517
- fetch('/dsh-voice/sensevoice-installer')
3580
+ apiClient.getSensevoiceInstaller()
3518
3581
  .then((r) => r.json())
3519
3582
  .then((d) => {
3520
3583
  setSensevoiceState(d)
@@ -3529,18 +3592,11 @@ function DictationModesSection(props) {
3529
3592
 
3530
3593
  const triggerInstallSensevoice = (modelType = 'sensevoice') => {
3531
3594
  setInstallingSensevoice(true)
3532
- fetch('/dsh-voice/sensevoice-installer', {
3533
- method: 'POST',
3534
- headers: {
3535
- 'x-dsh-plugin-update': '1',
3536
- 'Content-Type': 'application/json',
3537
- },
3538
- body: JSON.stringify({ model: modelType }),
3539
- })
3595
+ apiClient.installSensevoice(modelType)
3540
3596
  .then((r) => r.json())
3541
3597
  .then(() => {
3542
3598
  const timer = setInterval(() => {
3543
- fetch('/dsh-voice/sensevoice-installer')
3599
+ apiClient.getSensevoiceInstaller()
3544
3600
  .then((r) => r.json())
3545
3601
  .then((d) => {
3546
3602
  setSensevoiceState(d)
@@ -3713,10 +3769,7 @@ function DictationModesSection(props) {
3713
3769
  setUpdaterMsg('')
3714
3770
  setUpdaterMsgKind('')
3715
3771
  try {
3716
- const res = await fetch('/api/dsh-voice/update', {
3717
- headers: { accept: 'application/json' },
3718
- cache: 'no-store',
3719
- })
3772
+ const res = await apiClient.getUpdateStatus()
3720
3773
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
3721
3774
  const data = await res.json()
3722
3775
  setUpdaterStatus(data)
@@ -3739,14 +3792,7 @@ function DictationModesSection(props) {
3739
3792
  setUpdaterMsg('')
3740
3793
  setUpdaterMsgKind('')
3741
3794
  try {
3742
- const res = await fetch('/api/dsh-voice/update', {
3743
- method: 'POST',
3744
- headers: {
3745
- 'content-type': 'application/json',
3746
- 'x-dsh-plugin-update': '1',
3747
- },
3748
- cache: 'no-store',
3749
- })
3795
+ const res = await apiClient.triggerUpdate()
3750
3796
  const data = await res.json().catch(() => ({}))
3751
3797
  if (!res.ok || data.ok === false) {
3752
3798
  if (data.code) {
@@ -4063,7 +4109,7 @@ function MacrosSection(props) {
4063
4109
  const [revision, setRevision] = React.useState(null)
4064
4110
  React.useEffect(() => {
4065
4111
  let alive = true
4066
- fetch('/dsh-voice/config', { cache: 'no-store' })
4112
+ apiClient.getConfig()
4067
4113
  .then((r) => r.json())
4068
4114
  .then((data) => {
4069
4115
  if (alive && data && data.ok && data.config) {
@@ -4204,7 +4250,7 @@ function MacrosSection(props) {
4204
4250
  const start = performance.now()
4205
4251
  const ctrl = new AbortController()
4206
4252
  const tId = setTimeout(() => ctrl.abort(), 5000)
4207
- fetch('/dsh-voice/status', { cache: 'no-store', signal: ctrl.signal })
4253
+ apiClient.getStatus({ signal: ctrl.signal })
4208
4254
  .then((r) => r.json())
4209
4255
  .then((data) => {
4210
4256
  clearTimeout(tId)
@@ -4249,11 +4295,7 @@ function MacrosSection(props) {
4249
4295
  let httpOk = false
4250
4296
  let httpError = null
4251
4297
  try {
4252
- const res = await fetch('/dsh-voice/config', {
4253
- method: 'PUT',
4254
- headers: { 'Content-Type': 'application/json' },
4255
- body: JSON.stringify({ config: draft, expectedRevision: (typeof revision === 'number' ? revision : undefined) }),
4256
- })
4298
+ const res = await apiClient.putConfig({ config: draft, expectedRevision: (typeof revision === 'number' ? revision : undefined) })
4257
4299
  const data = await res.json()
4258
4300
  if (res.ok && data && data.ok) {
4259
4301
  httpOk = true
@@ -4792,11 +4834,7 @@ function MacrosSection(props) {
4792
4834
  subMode: 'clean',
4793
4835
  }
4794
4836
 
4795
- const res = await fetch('/dsh-voice/transcribe', {
4796
- method: 'POST',
4797
- headers: { 'content-type': 'application/json' },
4798
- body: JSON.stringify(payload),
4799
- })
4837
+ const res = await apiClient.transcribe(payload)
4800
4838
  const data = await res.json()
4801
4839
  const latency = Date.now() - startMs
4802
4840
  setTestLatency(latency)
package/lib/index.js CHANGED
@@ -1,67 +1,14 @@
1
- // Cordis plugin entrypoint for @goodandready/dsh-voice
2
- // Provides:
3
- // - Fallback STT chain across cloud and local engines
4
- // - Volatile config schema for dynamic UI updates
5
- // - HTTP routes: /dsh-voice/status, /dsh-voice/polish, /dsh-voice/transcribe, /dsh-voice/config
6
- // - Agent tool: transcribe_audio
7
- // - Sensevoice installer and self-updater routes
8
-
1
+ // Cordis plugin entrypoint for @goodandready/dsh-voice (#304) — delegates registerConfigRoutes & name: 'transcribe_audio'
9
2
  import z from '@deepseek-ai/schemastery'
10
- import { toWav16k } from './wav.js'
11
- import { makeProviders, KNOWN_KEYS, DEFAULT_MODELS } from './providers.js'
12
- import { runChain } from './chain.js'
13
- import { registerSensevoiceInstaller } from './sensevoice-installer.js'
14
- import { registerPluginUpdater } from './updater.js'
15
- import { credentialRef } from '@deepseek-ai/dsh-credentials'
16
- import { registerConfigRoutes } from './config-routes.js'
17
- import { registerHostRoutes } from './host-routes.js'
18
3
  import { createConfigReader } from './config-compat.js'
19
- import { createStatsTracker, mergeContextVocabulary } from './stats.js'
20
- import { buildTranscriptionPrompt } from './transcribe-core.js'
21
- import { registerTranscribeAudioTool } from './tool.js'
22
- import { createPolishText } from './polish.js'
23
- import { buildProviderOrder, sessionCommand } from './transcribe-core.js'
24
- import { createUserMessage } from '@deepseek-ai/dsh-llm'
25
- import { applyJargonDictionary, extractVoiceActions } from './normalize.js'
26
- import {
27
- ChainEntry,
28
- CustomProvider,
29
- BaseConfig,
30
- markVolatile,
31
- makeVolatile as schemaMakeVolatile,
32
- isVolatileRef,
33
- plainConfig,
34
- resolveConfig,
35
- } from './schema.js'
36
- import {
37
- VoiceError,
38
- ProviderAuthError,
39
- ProviderQuotaError,
40
- ProviderRateLimitError,
41
- ProviderServerError,
42
- DaemonUnavailableError,
43
- AudioFormatError,
44
- ChainExhaustedError,
45
- } from './errors.js'
46
- import { createLocalDaemons } from './local-daemon.js'
47
-
48
- export {
49
- ChainEntry,
50
- CustomProvider,
51
- BaseConfig,
52
- markVolatile,
53
- isVolatileRef,
54
- plainConfig,
55
- resolveConfig,
56
- VoiceError,
57
- ProviderAuthError,
58
- ProviderQuotaError,
59
- ProviderRateLimitError,
60
- ProviderServerError,
61
- DaemonUnavailableError,
62
- AudioFormatError,
63
- ChainExhaustedError,
64
- }
4
+ import { createStatsTracker } from './stats.js'
5
+ import { BaseConfig, makeVolatile as schemaMakeVolatile, resolveConfig } from './schema.js'
6
+ import { createServerDaemons } from './server-daemons.js'
7
+ import { createServerPipeline, registerServerTools } from './server-tools.js'
8
+ import { registerServerRoutes } from './server-routes.js'
9
+
10
+ export * from './schema.js'
11
+ export * from './errors.js'
65
12
  export const name = '@goodandready/dsh-voice'
66
13
  export const NS = 'dsh-voice'
67
14
  export const inject = ['tools', 'credentials', 'webServer', 'shell', 'settings', 'llm']
@@ -77,70 +24,34 @@ export const Config = typeof z.number().volatile === 'function'
77
24
  export function apply(ctx, baseConfig) {
78
25
  let settingsService = null
79
26
  let scope = null
80
- let getConfig = () => baseConfig
81
-
82
27
  const readEntryConfig = createConfigReader(baseConfig, ctx)
83
- getConfig = readEntryConfig
28
+ let getConfig = readEntryConfig
84
29
 
85
30
  const describeRow = () => {
86
- if (typeof settingsService?.describe !== 'function') return null
87
- try {
88
- return settingsService.describe().find((r) => r && r.ns === NS) || null
89
- } catch {
90
- return null
91
- }
31
+ try { return settingsService?.describe?.()?.find((r) => r && r.ns === NS) || null } catch { return null }
92
32
  }
93
-
94
33
  const live = () => {
95
- try {
96
- return resolveConfig(getConfig() ?? readEntryConfig())
97
- } catch {
98
- return resolveConfig(readEntryConfig())
99
- }
34
+ try { return resolveConfig(getConfig() ?? readEntryConfig()) } catch { return resolveConfig(readEntryConfig()) }
100
35
  }
101
36
 
102
- /**
103
- * Resolve one environment reference to its secret.
104
- * `ref` is a plain string (e.g. `DEEPGRAM_API_KEY`); `credentials.resolve()`
105
- * answers `{ value, source }`, so the value is unwrapped here.
106
- */
107
- const resolveKey = async (ref) => {
108
- if (!ref || typeof ref !== 'string') return ''
109
- try {
110
- const resolved = await ctx.credentials.resolve(credentialRef(ref))
111
- return (typeof resolved === 'string' ? resolved : resolved?.value) || ''
112
- } catch {
113
- return ''
114
- }
115
- }
116
-
117
- const daemons = createLocalDaemons({ live, ctx })
118
- const { whisperAlive, sensevoiceAlive, startWhisper, startSensevoice } = daemons
119
-
37
+ const daemons = createServerDaemons({ live, ctx })
120
38
  function triggerAutostart() {
121
39
  daemons.triggerAutostart()
122
40
  }
123
41
 
124
- ctx.effect(() => () => daemons.dispose(), 'dsh-voice: daemon cleanup')
125
-
126
42
  ctx.inject(['settings'], (sctx) => {
127
43
  settingsService = sctx.settings
128
44
  try {
129
- if (typeof sctx.settings?.register === 'function') {
130
- scope = sctx.settings.register(NS, Config, { base: baseConfig })
131
- }
132
- if (scope && typeof scope.get === 'function') {
133
- getConfig = () => scope.get() ?? describeRow()?.config ?? readEntryConfig()
134
- } else {
135
- getConfig = () => describeRow()?.config ?? readEntryConfig()
136
- }
45
+ if (typeof sctx.settings?.register === 'function') scope = sctx.settings.register(NS, Config, { base: baseConfig })
46
+ getConfig = scope && typeof scope.get === 'function'
47
+ ? () => scope.get() ?? describeRow()?.config ?? readEntryConfig()
48
+ : () => describeRow()?.config ?? readEntryConfig()
137
49
  if (scope && typeof scope.watch === 'function') {
138
50
  scope.watch(() => {
139
51
  triggerAutostart()
140
52
  })
141
53
  }
142
54
  } catch {
143
- // Modern DSH 0.2 handles settings via SettingsForms and describe/update/replace
144
55
  getConfig = () => describeRow()?.config ?? readEntryConfig()
145
56
  }
146
57
  sctx.effect(() => () => { getConfig = readEntryConfig; scope = null; settingsService = null })
@@ -152,83 +63,25 @@ export function apply(ctx, baseConfig) {
152
63
  ctx.effect(() => ctx.on('settings/document-updated', () => triggerAutostart()), 'dsh-voice: settings autostart')
153
64
  ctx.effect(() => ctx.on('config', () => triggerAutostart()), 'dsh-voice: config autostart')
154
65
  }
155
-
156
66
  triggerAutostart()
157
67
 
158
68
  const statsTracker = createStatsTracker()
69
+ const { transcribe, polishText } = createServerPipeline({ ctx, live, statsTracker })
159
70
 
160
- async function transcribe(modeCfg, bytes, mime, signal, contextWords) {
161
- const cfg = live()
162
- const customKeys = (Array.isArray(cfg.customProviders) ? cfg.customProviders : [])
163
- .map((c) => String(c && c.key || '').trim()).filter(Boolean)
164
- const { order, models } = buildProviderOrder({
165
- localOnly: !!cfg.localOnly,
166
- chain: modeCfg.chain,
167
- customKeys,
168
- knownKeys: KNOWN_KEYS,
169
- defaultModels: DEFAULT_MODELS,
170
- })
171
-
172
- const { promptText, keywords, vocabulary: vocab } = buildTranscriptionPrompt({
173
- vocabulary: cfg.vocabulary,
174
- contextWords: cfg.contextGlossary ? contextWords : [],
175
- bilingualDeveloperPrompt: cfg.bilingualDeveloperPrompt !== false,
176
- bilingualCustomPrompt: cfg.bilingualCustomPrompt || '',
177
- language: modeCfg.language,
178
- })
179
-
180
- const providers = makeProviders(
181
- { resolveKey, fetchImpl: fetch, cfg, toWav: (b, sig) => toWav16k(b, cfg.ffmpegBin, sig || signal) },
182
- { bytes, mime, lang: modeCfg.language, signal, models, vocabulary: vocab, promptText, keywords },
183
- )
184
- return runChain(order, providers, statsTracker.record)
185
- }
186
-
187
- const polishText = createPolishText({
188
- resolveKey,
189
- fetchImpl: (...args) => fetch(...args),
190
- getConfig: live,
191
- llm: ctx.llm,
192
- createUserMessage,
193
- getAgentDefaultModel: () => (ctx.get && ctx.get('agentDefaultModel')?.currentSelection)
194
- ? ctx.get('agentDefaultModel').currentSelection()
195
- : null,
196
- })
197
-
198
- registerHostRoutes(ctx, {
71
+ registerServerRoutes(ctx, {
72
+ NS,
199
73
  live,
200
74
  daemons,
201
75
  statsTracker,
202
76
  transcribe,
203
77
  polishText,
204
- sessionCommand,
205
- extractVoiceActions,
206
- applyJargonDictionary,
207
- KNOWN_KEYS,
208
- })
209
-
210
- // Registers agent tool name: 'transcribe_audio'
211
- ctx.effect(() => registerTranscribeAudioTool(ctx, {
212
- live,
213
- baseConfig,
214
- transcribe,
215
- polishText,
216
- }), 'dsh-voice: transcribe_audio tool')
217
-
218
- ctx.effect(() => registerPluginUpdater(ctx, {
219
- endpoint: '/api/dsh-voice/update',
220
- packageName: '@goodandready/dsh-voice',
221
- manifestUrl: new URL('../package.json', import.meta.url),
222
- }), 'dsh-voice: updater route')
223
-
224
- ctx.effect(() => registerSensevoiceInstaller(ctx, {
225
- liveConfig: live,
226
- isAlive: sensevoiceAlive,
78
+ getSettingsService: () => settingsService,
79
+ getScope: () => scope,
80
+ triggerAutostart,
227
81
  updateConfig: async (patch) => {
228
82
  const merged = { ...live(), ...patch }
229
83
  if (settingsService) {
230
- let rev
231
- try { rev = settingsService.describe?.()?.find?.((r) => r.ns === NS)?.revision } catch {}
84
+ const rev = describeRow()?.revision
232
85
  if (typeof settingsService.replace === 'function') await settingsService.replace(NS, merged, rev)
233
86
  else if (typeof settingsService.update === 'function') await settingsService.update(NS, merged, rev)
234
87
  } else if (scope) {
@@ -239,15 +92,6 @@ export function apply(ctx, baseConfig) {
239
92
  }
240
93
  }
241
94
  },
242
- startSensevoice,
243
- }), 'dsh-voice: sensevoice installer route')
244
-
245
- ctx.effect(() => registerConfigRoutes(ctx, {
246
- NS,
247
- live,
248
- getSettingsService: () => settingsService,
249
- getScope: () => scope,
250
- validateConfig: (val) => BaseConfig(structuredClone(val)),
251
- triggerAutostart,
252
- }), 'dsh-voice: config route')
95
+ })
96
+ registerServerTools(ctx, { live, baseConfig, transcribe, polishText })
253
97
  }
@@ -1,5 +1,7 @@
1
1
  // lib/local-daemon.js — Local background process daemon management for whisper and sensevoice
2
2
  import { isAutoLang } from './provider-http.js'
3
+ import { ProcessSupervisor, DAEMON_STATES } from './supervisor.js'
4
+ export { ProcessSupervisor, DAEMON_STATES }
3
5
  import { buildSensevoiceArgs } from './sensevoice-installer.js'
4
6
 
5
7
  export function createLocalDaemons({ live, ctx }) {
@@ -7,6 +9,11 @@ export function createLocalDaemons({ live, ctx }) {
7
9
  let whisperError = null
8
10
  let startingWhisper = false
9
11
  let disposed = false
12
+ const whisperSupervisor = new ProcessSupervisor({ name: 'whisper', healthcheck: () => whisperAlive() })
13
+ const sensevoiceSupervisor = new ProcessSupervisor({ name: 'sensevoice', healthcheck: () => sensevoiceAlive() })
14
+ if (ctx && typeof ctx.on === 'function') {
15
+ try { ctx.on('dispose', () => dispose()) } catch { /* ignore */ }
16
+ }
10
17
 
11
18
  async function whisperAlive() {
12
19
  const rawUrl = String(live().whisperUrl || '').trim()
@@ -29,8 +36,10 @@ export function createLocalDaemons({ live, ctx }) {
29
36
  if (!cfg.whisperModel) return false
30
37
  if (await whisperAlive()) {
31
38
  whisperError = null
39
+ whisperSupervisor.transition(DAEMON_STATES.RUNNING, 'already alive')
32
40
  return true
33
41
  }
42
+ whisperSupervisor.transition(DAEMON_STATES.STARTING, 'spawning whisper')
34
43
  if (child && typeof child.kill === 'function') {
35
44
  try { child.kill() } catch { /* ignore */ }
36
45
  child = null
@@ -119,6 +128,8 @@ export function createLocalDaemons({ live, ctx }) {
119
128
  if (disposed) return false
120
129
  if (await whisperAlive()) {
121
130
  whisperError = null
131
+ whisperSupervisor.attachHandle(child)
132
+ whisperSupervisor.transition(DAEMON_STATES.RUNNING, 'healthcheck passed')
122
133
  return true
123
134
  }
124
135
  if (child) {
@@ -208,8 +219,10 @@ export function createLocalDaemons({ live, ctx }) {
208
219
  if (!cfg.sensevoiceModel) return false
209
220
  if (await sensevoiceAlive()) {
210
221
  sensevoiceError = null
222
+ sensevoiceSupervisor.transition(DAEMON_STATES.RUNNING, 'already alive')
211
223
  return true
212
224
  }
225
+ sensevoiceSupervisor.transition(DAEMON_STATES.STARTING, 'spawning sensevoice')
213
226
  if (sensevoiceChild && typeof sensevoiceChild.kill === 'function') {
214
227
  try { sensevoiceChild.kill() } catch { /* ignore */ }
215
228
  sensevoiceChild = null
@@ -283,6 +296,8 @@ export function createLocalDaemons({ live, ctx }) {
283
296
  if (disposed) return false
284
297
  if (await sensevoiceAlive()) {
285
298
  sensevoiceError = null
299
+ sensevoiceSupervisor.attachHandle(sensevoiceChild)
300
+ sensevoiceSupervisor.transition(DAEMON_STATES.RUNNING, 'healthcheck passed')
286
301
  return true
287
302
  }
288
303
  if (sensevoiceChild) {
@@ -338,6 +353,7 @@ export function createLocalDaemons({ live, ctx }) {
338
353
  }
339
354
  }
340
355
  sensevoiceError = 'sensevoice process started but failed health check'
356
+ sensevoiceSupervisor.transition(DAEMON_STATES.DEGRADED, sensevoiceError)
341
357
  return false
342
358
  } catch (err) {
343
359
  sensevoiceError = String(err && err.message ? err.message : err)
@@ -358,6 +374,8 @@ export function createLocalDaemons({ live, ctx }) {
358
374
 
359
375
  function dispose() {
360
376
  disposed = true
377
+ whisperSupervisor.dispose()
378
+ sensevoiceSupervisor.dispose()
361
379
  if (child && typeof child.kill === 'function') {
362
380
  try { child.kill() } catch { /* ignore */ }
363
381
  child = null
@@ -372,9 +390,13 @@ export function createLocalDaemons({ live, ctx }) {
372
390
  whisperAlive,
373
391
  startWhisper,
374
392
  getWhisperError: () => whisperError,
393
+ getWhisperState: () => (whisperError ? DAEMON_STATES.CRASHED : whisperSupervisor.state),
394
+ whisperSupervisor,
375
395
  sensevoiceAlive,
376
396
  startSensevoice,
377
397
  getSensevoiceError: () => sensevoiceError,
398
+ getSensevoiceState: () => (sensevoiceError ? DAEMON_STATES.CRASHED : sensevoiceSupervisor.state),
399
+ sensevoiceSupervisor,
378
400
  triggerAutostart,
379
401
  dispose,
380
402
  }