Ariver
2026-06-03 debe8b2b29946fdddacc5f85618b4bf7e94415b3
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
package services
 
import (
    "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 {
    app          *application.App
    initCallback func()
    mu           sync.RWMutex
    status       string
    hardwareInfo string
    lastError    string
}
 
func NewEngineService() *EngineService {
    return &EngineService{status: "loading"}
}
 
// ModelExists returns true if the ASR model files are present.
func (s *EngineService) ModelExists() bool {
    return engine.ModelExists()
}
 
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()
    resolved, err := model.ResolveModel(current.ModelID)
    installed := err == nil && resolved.IsUsable()
    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":             current.ModelID,
        "displayName":         current.Profile.DisplayName,
        "backendKind":         current.Profile.BackendKind,
        "languageID":          current.LanguageSettings.EffectiveLanguageID,
        "selectionMode":       current.SelectionMode,
        "installed":           installed,
        "installStatus":       status,
        "missing":             missing,
        "problems":            problems,
        "fallbackReason":      current.FallbackReason,
        "downloadSize":        current.Profile.ApproxSize,
        "supportedLanguages":  current.Profile.SupportedLanguageIDs,
        "recommendedLanguage": current.Profile.RecommendedFor,
    }
}
 
func (s *EngineService) SetStatus(status, hardwareInfo, lastError string) {
    s.mu.Lock()
    defer s.mu.Unlock()
    s.status = status
    s.hardwareInfo = hardwareInfo
    s.lastError = lastError
}
 
// DownloadModel downloads the ASR model with progress events.
func (s *EngineService) DownloadModel(primaryURL, fallbackURL string) error {
    err := model.Download(primaryURL, fallbackURL, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        if s.app != nil {
            s.app.Event.Emit("model:download-progress", map[string]interface{}{
                "percent":    percent,
                "downloaded": downloaded,
                "total":      total,
            })
        }
    })
    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)
    }
 
    err := model.DownloadProfile(current.Profile, current.Profile.DownloadURLs, paths.ModelsRoot(), func(percent float64, downloaded, total int64) {
        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 {
    cfg, err := config.Load()
    if err != nil {
        cfg = config.Default()
    }
    return modelselection.Resolve(cfg, language.NewSystemDetector())
}