Ariver
2026-07-03 a075aec123e1f7118f138738f196c2995aa96f98
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package services
 
import (
    "context"
    "fmt"
    "sync"
    "voicesnap/internal/config"
    "voicesnap/internal/engine"
    "voicesnap/internal/language"
    "voicesnap/internal/model"
    "voicesnap/internal/modelselection"
    "voicesnap/internal/paths"
 
    "github.com/wailsapp/wails/v3/pkg/application"
)
 
// EngineService provides engine status and model management to the frontend.
type EngineService struct {
    cfg                *config.Config
    app                *application.App
    initCallback       func()
    mu                 sync.RWMutex
    status             string
    hardwareInfo       string
    lastError          string
    downloadMu         sync.Mutex
    downloadID         string
    downloadCancel     context.CancelFunc
    downloadCancelling bool
    downloadProgress   float64
    downloadDownloaded int64
    downloadTotal      int64
}
 
func NewEngineService(cfg *config.Config) *EngineService {
    if cfg == nil {
        cfg = config.Default()
    }
    return &EngineService{cfg: cfg, status: "loading"}
}
 
// ModelExists returns true if the ASR model files are present.
func (s *EngineService) ModelExists() bool {
    return engine.ModelExists()
}
 
func (s *EngineService) HasAnyInstalledModel() bool {
    return model.HasAnyUsableModel()
}
 
func (s *EngineService) GetStatus() map[string]interface{} {
    s.mu.RLock()
    defer s.mu.RUnlock()
    return map[string]interface{}{
        "status":       s.status,
        "hardwareInfo": s.hardwareInfo,
        "error":        s.lastError,
    }
}
 
func (s *EngineService) GetCurrentModelStatus() map[string]interface{} {
    current := s.currentModel()
    return s.modelStatusMap(current.Profile, current)
}
 
func (s *EngineService) ListModelOptions() []map[string]interface{} {
    current := s.currentModel()
    return s.listModelOptionsForCurrent(current)
}
 
func (s *EngineService) ListModelOptionsForLanguage(languageID string) []map[string]interface{} {
    current := s.currentModel()
    activeLanguageID := current.LanguageSettings.EffectiveLanguageID
    current.LanguageSettings = language.Resolve(config.LanguageModeManual, model.NormalizeLanguageID(languageID), nil)
    if current.LanguageSettings.EffectiveLanguageID != activeLanguageID {
        current.ModelID = ""
    }
    return s.listModelOptionsForCurrent(current)
}
 
func (s *EngineService) listModelOptionsForCurrent(current modelselection.CurrentModel) []map[string]interface{} {
    languageProfile, err := model.GetLanguageProfile(current.LanguageSettings.EffectiveLanguageID)
    if err != nil {
        return nil
    }
 
    ids := make([]string, 0, 1+len(languageProfile.UpgradeModelIDs))
    ids = append(ids, languageProfile.DefaultModelID)
    ids = append(ids, languageProfile.UpgradeModelIDs...)
 
    options := make([]map[string]interface{}, 0, len(ids))
    for _, id := range ids {
        profile, err := model.GetModelProfile(id)
        if err != nil {
            continue
        }
        options = append(options, s.modelStatusMap(profile, current))
    }
    return options
}
 
func (s *EngineService) SelectModel(modelID string) error {
    profile, err := s.allowedModelProfile(modelID)
    if err != nil {
        return err
    }
    resolved, err := model.ResolveModel(profile.ID)
    if err != nil {
        return err
    }
    if !resolved.IsUsable() {
        return fmt.Errorf("model %s is not installed", profile.ID)
    }
 
    s.cfg.ModelSelectionMode = config.ModelSelectionModeManual
    s.cfg.SelectedModelID = profile.ID
    config.Save(s.cfg)
    s.ReloadCurrentModel()
    return nil
}
 
func (s *EngineService) SelectModelForLanguage(languageID, modelID string) error {
    languageID = model.NormalizeLanguageID(languageID)
    profile, languageProfile, err := s.allowedModelProfileForLanguage(languageID, modelID)
    if err != nil {
        return err
    }
    resolved, err := model.ResolveModel(profile.ID)
    if err != nil {
        return err
    }
    if !resolved.IsUsable() {
        return fmt.Errorf("model %s is not installed", profile.ID)
    }
 
    s.persistLanguageModelSelection(languageID, profile, languageProfile)
    s.ReloadCurrentModel()
    return nil
}
 
func (s *EngineService) DownloadModelByID(modelID string) error {
    profile, err := s.allowedModelProfile(modelID)
    if err != nil {
        return err
    }
    if len(profile.DownloadURLs) == 0 {
        return fmt.Errorf("no download URL configured for model %s", profile.ID)
    }
 
    ctx, finish, err := s.beginModelDownload(profile.ID)
    if err != nil {
        return err
    }
    defer finish()
 
    err = model.DownloadProfileWithContext(ctx, profile, profile.DownloadURLs, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        s.updateModelDownloadProgress(profile.ID, percent, downloaded, total)
        if s.app != nil {
            s.app.Event.Emit("model:download-progress", map[string]interface{}{
                "percent":    percent,
                "downloaded": downloaded,
                "total":      total,
                "modelID":    profile.ID,
                "modelName":  profile.DisplayName,
            })
        }
    })
    if err != nil {
        return err
    }
 
    s.cfg.ModelSelectionMode = config.ModelSelectionModeManual
    s.cfg.SelectedModelID = profile.ID
    config.Save(s.cfg)
    s.ReloadCurrentModel()
    return nil
}
 
func (s *EngineService) DownloadModelByIDForLanguage(languageID, modelID string) error {
    languageID = model.NormalizeLanguageID(languageID)
    profile, languageProfile, err := s.allowedModelProfileForLanguage(languageID, modelID)
    if err != nil {
        return err
    }
    if len(profile.DownloadURLs) == 0 {
        return fmt.Errorf("no download URL configured for model %s", profile.ID)
    }
 
    ctx, finish, err := s.beginModelDownload(profile.ID)
    if err != nil {
        return err
    }
    defer finish()
 
    err = model.DownloadProfileWithContext(ctx, profile, profile.DownloadURLs, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        s.updateModelDownloadProgress(profile.ID, percent, downloaded, total)
        if s.app != nil {
            s.app.Event.Emit("model:download-progress", map[string]interface{}{
                "percent":    percent,
                "downloaded": downloaded,
                "total":      total,
                "modelID":    profile.ID,
                "modelName":  profile.DisplayName,
            })
        }
    })
    if err != nil {
        return err
    }
 
    s.persistLanguageModelSelection(languageID, profile, languageProfile)
    s.ReloadCurrentModel()
    return nil
}
 
func (s *EngineService) persistLanguageModelSelection(languageID string, profile model.ModelProfile, languageProfile model.LanguageProfile) {
    s.cfg.LanguageMode = config.LanguageModeManual
    s.cfg.LanguageID = languageID
    if profile.ID == languageProfile.DefaultModelID {
        s.cfg.ModelSelectionMode = config.ModelSelectionModeAuto
        s.cfg.SelectedModelID = ""
    } else {
        s.cfg.ModelSelectionMode = config.ModelSelectionModeManual
        s.cfg.SelectedModelID = profile.ID
    }
    config.Save(s.cfg)
}
 
func (s *EngineService) CancelModelDownload(modelID string) bool {
    s.downloadMu.Lock()
    activeID := s.downloadID
    if s.downloadCancel == nil || (modelID != "" && activeID != modelID) {
        s.downloadMu.Unlock()
        return false
    }
    s.downloadCancelling = true
    s.downloadCancel()
    s.downloadMu.Unlock()
 
    if s.app != nil {
        s.app.Event.Emit("model:download-cancelled", map[string]interface{}{
            "modelID": activeID,
        })
    }
    return true
}
 
func (s *EngineService) GetModelDownloadStatus() map[string]interface{} {
    s.downloadMu.Lock()
    defer s.downloadMu.Unlock()
    return s.modelDownloadStatusLocked()
}
 
func (s *EngineService) modelStatusMap(profile model.ModelProfile, current modelselection.CurrentModel) map[string]interface{} {
    resolved, err := model.ResolveModel(profile.ID)
    installed := err == nil && resolved.IsUsable()
    supportedInBuild := model.IsModelSupportedInCurrentBuild(profile.ID)
    status := model.ModelNotInstalled
    var missing []string
    var problems []string
    if err == nil {
        status = resolved.Status
        missing = resolved.Missing
        problems = resolved.Problems
    }
 
    return map[string]interface{}{
        "modelID":             profile.ID,
        "displayName":         profile.DisplayName,
        "backendKind":         profile.BackendKind,
        "tier":                profile.Tier,
        "languageID":          current.LanguageSettings.EffectiveLanguageID,
        "selectionMode":       current.SelectionMode,
        "isCurrent":           profile.ID == current.ModelID,
        "isDefault":           profile.ID == current.LanguageSettings.DefaultModelID,
        "installed":           installed,
        "installStatus":       status,
        "missing":             missing,
        "problems":            problems,
        "fallbackReason":      current.FallbackReason,
        "downloadSize":        profile.ApproxSize,
        "supportedLanguages":  profile.SupportedLanguageIDs,
        "recommendedLanguage": profile.RecommendedFor,
        "description":         profile.Description,
        "supportedInBuild":    supportedInBuild,
        "unsupportedReason":   model.ModelUnsupportedReason(profile.ID),
    }
}
 
func (s *EngineService) SetStatus(status, hardwareInfo, lastError string) {
    s.mu.Lock()
    s.status = status
    s.hardwareInfo = hardwareInfo
    s.lastError = lastError
    s.mu.Unlock()
 
    if s.app != nil {
        s.app.Event.Emit("engine:status", map[string]interface{}{
            "status":       status,
            "hardwareInfo": hardwareInfo,
            "error":        lastError,
        })
    }
}
 
// DownloadModel downloads the ASR model with progress events.
func (s *EngineService) DownloadModel(primaryURL, fallbackURL string) error {
    profile := model.DefaultModelProfile()
    ctx, finish, err := s.beginModelDownload(profile.ID)
    if err != nil {
        return err
    }
    defer finish()
 
    err = model.DownloadProfileWithContext(ctx, profile, []string{primaryURL, fallbackURL}, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        s.updateModelDownloadProgress(profile.ID, percent, downloaded, total)
        if s.app != nil {
            s.app.Event.Emit("model:download-progress", map[string]interface{}{
                "percent":    percent,
                "downloaded": downloaded,
                "total":      total,
                "modelID":    profile.ID,
                "modelName":  profile.DisplayName,
            })
        }
    })
    if err != nil {
        return err
    }
 
    if s.initCallback != nil {
        go s.initCallback()
    }
    return nil
}
 
func (s *EngineService) DownloadCurrentModel() error {
    current := s.currentModel()
    if len(current.Profile.DownloadURLs) == 0 {
        return fmt.Errorf("no download URL configured for model %s", current.ModelID)
    }
 
    ctx, finish, err := s.beginModelDownload(current.ModelID)
    if err != nil {
        return err
    }
    defer finish()
 
    err = model.DownloadProfileWithContext(ctx, current.Profile, current.Profile.DownloadURLs, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        s.updateModelDownloadProgress(current.ModelID, percent, downloaded, total)
        if s.app != nil {
            s.app.Event.Emit("model:download-progress", map[string]interface{}{
                "percent":    percent,
                "downloaded": downloaded,
                "total":      total,
                "modelID":    current.ModelID,
                "modelName":  current.Profile.DisplayName,
            })
        }
    })
    if err != nil {
        return err
    }
 
    if s.initCallback != nil {
        go s.initCallback()
    }
    return nil
}
 
func (s *EngineService) ReloadCurrentModel() {
    if s.initCallback != nil {
        go s.initCallback()
    }
}
 
// SetInitCallback sets the callback to re-initialize the engine after model download.
func (s *EngineService) SetInitCallback(cb func()) {
    s.initCallback = cb
}
 
// SetApp sets the Wails app reference for event emission.
func (s *EngineService) SetApp(app *application.App) {
    s.app = app
}
 
func (s *EngineService) currentModel() modelselection.CurrentModel {
    if s.cfg == nil {
        cfg, err := config.Load()
        if err != nil {
            cfg = config.Default()
        }
        s.cfg = cfg
    }
    return modelselection.Resolve(s.cfg, language.NewSystemDetector())
}
 
func (s *EngineService) beginModelDownload(modelID string) (context.Context, func(), error) {
    s.downloadMu.Lock()
    defer s.downloadMu.Unlock()
    if s.downloadCancel != nil {
        return nil, nil, fmt.Errorf("model %s is already downloading", s.downloadID)
    }
    ctx, cancel := context.WithCancel(context.Background())
    s.downloadID = modelID
    s.downloadCancel = cancel
    s.downloadCancelling = false
    s.downloadProgress = 0
    s.downloadDownloaded = 0
    s.downloadTotal = 0
    finish := func() {
        emitFinished := false
        s.downloadMu.Lock()
        if s.downloadID == modelID {
            s.downloadID = ""
            s.downloadCancel = nil
            s.downloadCancelling = false
            s.downloadProgress = 0
            s.downloadDownloaded = 0
            s.downloadTotal = 0
            emitFinished = true
        }
        s.downloadMu.Unlock()
        if emitFinished && s.app != nil {
            s.app.Event.Emit("model:download-finished", map[string]interface{}{
                "modelID": modelID,
            })
        }
    }
    return ctx, finish, nil
}
 
func (s *EngineService) updateModelDownloadProgress(modelID string, percent float64, downloaded, total int64) {
    s.downloadMu.Lock()
    if s.downloadID == modelID && s.downloadCancel != nil {
        s.downloadProgress = percent
        s.downloadDownloaded = downloaded
        s.downloadTotal = total
    }
    s.downloadMu.Unlock()
}
 
func (s *EngineService) modelDownloadStatusLocked() map[string]interface{} {
    return map[string]interface{}{
        "active":     s.downloadCancel != nil,
        "modelID":    s.downloadID,
        "cancelling": s.downloadCancelling,
        "percent":    s.downloadProgress,
        "downloaded": s.downloadDownloaded,
        "total":      s.downloadTotal,
    }
}
 
func (s *EngineService) allowedModelProfile(modelID string) (model.ModelProfile, error) {
    current := s.currentModel()
    profile, _, err := s.allowedModelProfileForLanguage(current.LanguageSettings.EffectiveLanguageID, modelID)
    return profile, err
}
 
func (s *EngineService) allowedModelProfileForLanguage(languageID, modelID string) (model.ModelProfile, model.LanguageProfile, error) {
    profile, err := model.GetModelProfile(modelID)
    if err != nil {
        return model.ModelProfile{}, model.LanguageProfile{}, err
    }
    switch model.ModelUnsupportedReason(profile.ID) {
    case "":
    case model.UnsupportedReasonRequiresMacOS14:
        return model.ModelProfile{}, model.LanguageProfile{}, fmt.Errorf("model %s requires macOS 14 or later", profile.ID)
    case model.UnsupportedReasonWindowsPreview:
        return model.ModelProfile{}, model.LanguageProfile{}, fmt.Errorf("model %s is not supported in the Windows preview build; use SenseVoice", profile.ID)
    default:
        return model.ModelProfile{}, model.LanguageProfile{}, fmt.Errorf("model %s is not supported in this build", profile.ID)
    }
 
    languageProfile, err := model.GetLanguageProfile(model.NormalizeLanguageID(languageID))
    if err != nil {
        return model.ModelProfile{}, model.LanguageProfile{}, err
    }
    if profile.ID == languageProfile.DefaultModelID {
        return profile, languageProfile, nil
    }
    for _, id := range languageProfile.UpgradeModelIDs {
        if profile.ID == id {
            return profile, languageProfile, nil
        }
    }
    return model.ModelProfile{}, model.LanguageProfile{}, fmt.Errorf("model %s is not available for language %s", profile.ID, languageProfile.ID)
}