File size: 10,677 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import BluetoothSdk from "@mentra/bluetooth-sdk/internal"
import {ISLAND_SETTINGS_KEYS} from "../runtime/config"
import {useSettingsStore} from "../stores/settings"
import {ModelRetry} from "./ModelRetry"
import offlineSpeechModelService from "./OfflineSpeechModelService"
import sttModelManager from "./STTModelManager"

/**

 * State of the on-device speech-to-text model. `idle` means no miniapp needs

 * on-device transcription right now, so no model is loaded.

 */
export interface LocalSttModelStatus {
  /** Speech-page model code, e.g. "fr". */
  code: string
  /** BCP-47 language of that model, e.g. "en-US". */
  language: string
  state: "idle" | "downloading" | "loading" | "ready" | "unavailable"
  /** 0–100 while downloading. */
  progress?: number
  /** Why the model is unavailable. */
  reason?: string
}

/**

 * Runs on-device Sherpa STT for local miniapps while LocalMiniappRuntime says

 * a transcription listener needs it (`setDemand`): a `localOnly` listener, a

 * `preferLocal` one the picked model can serve, any listener while the cloud

 * is unreachable, or one still finishing an on-device utterance.

 *

 * There is one recognizer and it always runs the model picked on the Speech

 * page. When on-device transcription is needed and that model is missing, it

 * is downloaded through the Speech-page system first. A failed download or

 * load retries on the next attempt and on the shared model retry schedule.

 *

 * Activation flips `localSttFallbackActive` in the settings store, which

 * the bluetooth-sdk native side reads to decide whether to feed PCM into

 * the SherpaOnnxTranscriber. Local transcripts then flow back through the

 * `local_transcription` Bridge event; the host wires that event to

 * `localMiniappRuntime.forwardEvent`, with this coordinator providing the

 * subscription-active gate so we never publish transcripts to miniapps that

 * aren't asking for them. LocalMiniappRuntime routes local and cloud

 * transcripts to each listener.

 */
class LocalSttFallbackCoordinator {
  private static instance: LocalSttFallbackCoordinator

  /** A miniapp transcription listener needs on-device transcripts. */
  private demand = false
  private selectionAttached = false
  private hydration?: Promise<unknown>
  private hydrated = false
  private localActive = false
  /** Model code the native recognizer was last confirmed to have loaded for us. */
  private loadedCode: string | null = null
  /** Bumped by every new start and every stop, so a superseded start stops at its next step. */
  private generation = 0
  private running?: Promise<void>
  private runningCode: string | null = null
  /** Model code whose last download or load failed; its retry waits for the schedule. */
  private failedCode: string | null = null
  private readonly retry = new ModelRetry()
  private status: LocalSttModelStatus = {code: "", language: "", state: "idle"}
  private readonly statusListeners = new Set<(status: LocalSttModelStatus) => void>()

  private constructor() {
    // Reset the persisted mirror flag on boot — the in-memory state in this
    // coordinator is the source of truth, and a stale "true" left from the
    // previous session would cause native to feed Sherpa before any miniapp
    // registered a subscription.
    useSettingsStore.getState().setSetting(ISLAND_SETTINGS_KEYS.localSttFallbackActive, false)
  }

  static getInstance(): LocalSttFallbackCoordinator {
    if (!LocalSttFallbackCoordinator.instance) {
      LocalSttFallbackCoordinator.instance = new LocalSttFallbackCoordinator()
    }
    return LocalSttFallbackCoordinator.instance
  }

  /** A model change on the Speech page reloads the recognizer while it is in use. */
  private attachSelectionIfReady(): void {
    if (this.selectionAttached) return
    this.selectionAttached = true
    sttModelManager.onSelectionChanged(() => this.reconcile())
  }

  /** True while local transcripts should reach miniapps (the picked model is loaded). */
  isActive(): boolean {
    return this.localActive
  }

  /** BCP-47 language of the loaded model, or null when local STT is inactive. */
  getActiveLanguage(): string | null {
    return this.localActive && this.loadedCode ? sttModelManager.getBcp47ForLanguage(this.loadedCode) : null
  }

  getModelStatus(): LocalSttModelStatus {
    return {...this.status}
  }

  /** Follow the model status. The listener gets the current status immediately, then every change. */
  onModelStatusChanged(listener: (status: LocalSttModelStatus) => void): () => void {
    this.statusListeners.add(listener)
    listener(this.getModelStatus())
    return () => {
      this.statusListeners.delete(listener)
    }
  }

  /** Whether a miniapp transcription listener needs on-device transcripts. */
  setDemand(demand: boolean): void {
    if (this.demand !== demand) this.log(`on-device demand -> ${demand}`)
    this.demand = demand
    this.reconcile()
  }

  private reconcile(): void {
    this.attachSelectionIfReady()
    if (!this.demand) {
      this.stop("no listener needs it")
      return
    }
    if (!this.hydrated) {
      // Read the saved Speech-page choice first, so a miniapp that subscribes
      // during boot never loads (or downloads) the default model instead.
      this.hydration ??= sttModelManager.getCurrentLanguageFromPreferences().finally(() => {
        this.hydrated = true
        this.reconcile()
      })
      return
    }
    const code = sttModelManager.getCurrentLanguage()
    if (this.localActive && this.loadedCode === code) return
    // A failed model waits for its retry; a different model or new demand tries now.
    if (this.failedCode === code && this.retry.pending) return
    if (this.running && this.runningCode === code) return
    // Anything else supersedes the current run, which bails out at its next step.
    const run = this.start(code)
    this.running = run
    this.runningCode = code
    void run
      .catch((error) => this.log(`start failed unexpectedly: ${error}`))
      .finally(() => {
        if (this.running !== run) return
        this.running = undefined
        this.runningCode = null
      })
  }

  private async start(code: string): Promise<void> {
    const generation = ++this.generation
    const current = () => generation === this.generation && this.demand && sttModelManager.getCurrentLanguage() === code

    if (this.localActive) {
      // Switching models: hold transcripts until the new model is loaded.
      this.log(`switching local stt model ${this.loadedCode} -> ${code}`)
      this.localActive = false
    }
    this.failedCode = null
    this.retry.cancel()

    if (!(await sttModelManager.isModelAvailable(code))) {
      this.log(`local stt model ${code} is missing — downloading`)
      this.setStatus(code, {state: "downloading", progress: 0})
      try {
        await offlineSpeechModelService.downloadSttModel(code, (progress) => {
          if (current()) this.setStatus(code, {state: "downloading", progress})
        })
        if (!current()) return
        // Persist the picked model natively so the recognizer loads it.
        await sttModelManager.activateLanguage(code)
      } catch (error) {
        if (!current()) return
        this.fail(code, `Could not download the speech model: ${errorMessage(error)}`)
        return
      }
    }
    if (!current()) return

    this.log(`loading local stt model ${code}`)
    this.setStatus(code, {state: "loading"})
    try {
      // Native loads only while on-device transcription is in demand, so the
      // demand flag must reach it before the restart, not after a debounce.
      await this.setNativeDemand(true)
      await BluetoothSdk.restartTranscriber()
      // Confirm what native loaded rather than trusting promise completion.
      if (!(await BluetoothSdk.isSttModelLoaded(sttModelManager.getModelPath(code)))) {
        throw new Error("the native recognizer did not load it")
      }
    } catch (error) {
      if (!current()) return
      this.log(`loading ${code} failed: ${error}`)
      void this.setNativeDemand(false)
      this.fail(code, `Could not load the speech model: ${errorMessage(error)}`)
      return
    }
    if (!current()) return

    this.localActive = true
    this.loadedCode = code
    this.retry.reset()
    this.setStatus(code, {state: "ready"})
    this.log(`local stt ready on ${code}`)
  }

  private fail(code: string, reason: string): void {
    this.failedCode = code
    const delay = this.retry.schedule(() => this.reconcile())
    this.log(`${reason}; retrying in ${Math.round(delay / 1000)}s`)
    this.setStatus(code, {state: "unavailable", reason})
  }

  private stop(reason: string): void {
    this.generation++
    this.running = undefined
    this.runningCode = null
    this.retry.reset()
    this.failedCode = null
    if (this.localActive || this.loadedCode || this.status.state !== "idle") {
      this.log(`stopping local stt: ${reason}`)
      void this.setNativeDemand(false)
    }
    this.localActive = false
    this.loadedCode = null
    this.setStatus(sttModelManager.getCurrentLanguage(), {state: "idle"})
  }

  private async setNativeDemand(active: boolean): Promise<void> {
    useSettingsStore.getState().setSetting(ISLAND_SETTINGS_KEYS.localSttFallbackActive, active)
    if (active) {
      await BluetoothSdk.updateBluetoothSettings({[ISLAND_SETTINGS_KEYS.localSttFallbackActive]: true})
    }
  }

  private setStatus(code: string, next: Omit<LocalSttModelStatus, "code" | "language">): void {
    const status: LocalSttModelStatus = {code, language: sttModelManager.getBcp47ForLanguage(code), ...next}
    const previous = this.status
    if (
      previous.code === status.code &&
      previous.state === status.state &&
      previous.progress === status.progress &&
      previous.reason === status.reason
    ) {
      return
    }
    this.status = status
    for (const listener of this.statusListeners) {
      try {
        listener(this.getModelStatus())
      } catch (error) {
        this.log(`model status listener threw: ${error}`)
      }
    }
  }

  private log(msg: string): void {
    console.log(`[LocalSttFallback] ${msg}`)
  }
}

function errorMessage(error: unknown): string {
  return error instanceof Error ? error.message : String(error)
}

const localSttFallbackCoordinator = LocalSttFallbackCoordinator.getInstance()
export default localSttFallbackCoordinator