From 62f9a3b1e84455c32971272c03163ca543e2f68f Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Wed, 03 Jun 2026 14:44:26 +0800
Subject: [PATCH] Implement Round 4 Parakeet model selection

---
 privatevoice.src/services/engine_service.go |  136 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 123 insertions(+), 13 deletions(-)

diff --git a/privatevoice.src/services/engine_service.go b/privatevoice.src/services/engine_service.go
index f44fe18..d40c951 100755
--- a/privatevoice.src/services/engine_service.go
+++ b/privatevoice.src/services/engine_service.go
@@ -15,6 +15,7 @@
 
 // EngineService provides engine status and model management to the frontend.
 type EngineService struct {
+	cfg          *config.Config
 	app          *application.App
 	initCallback func()
 	mu           sync.RWMutex
@@ -23,8 +24,11 @@
 	lastError    string
 }
 
-func NewEngineService() *EngineService {
-	return &EngineService{status: "loading"}
+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.
@@ -44,7 +48,84 @@
 
 func (s *EngineService) GetCurrentModelStatus() map[string]interface{} {
 	current := s.currentModel()
-	resolved, err := model.ResolveModel(current.ModelID)
+	return s.modelStatusMap(current.Profile, current)
+}
+
+func (s *EngineService) ListModelOptions() []map[string]interface{} {
+	current := s.currentModel()
+	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) 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)
+	}
+
+	err = model.DownloadProfile(profile, 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":    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) modelStatusMap(profile model.ModelProfile, current modelselection.CurrentModel) map[string]interface{} {
+	resolved, err := model.ResolveModel(profile.ID)
 	installed := err == nil && resolved.IsUsable()
 	status := model.ModelNotInstalled
 	var missing []string
@@ -56,19 +137,23 @@
 	}
 
 	return map[string]interface{}{
-		"modelID":             current.ModelID,
-		"displayName":         current.Profile.DisplayName,
-		"backendKind":         current.Profile.BackendKind,
+		"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":        current.Profile.ApproxSize,
-		"supportedLanguages":  current.Profile.SupportedLanguageIDs,
-		"recommendedLanguage": current.Profile.RecommendedFor,
+		"downloadSize":        profile.ApproxSize,
+		"supportedLanguages":  profile.SupportedLanguageIDs,
+		"recommendedLanguage": profile.RecommendedFor,
+		"description":         profile.Description,
 	}
 }
 
@@ -145,9 +230,34 @@
 }
 
 func (s *EngineService) currentModel() modelselection.CurrentModel {
-	cfg, err := config.Load()
-	if err != nil {
-		cfg = config.Default()
+	if s.cfg == nil {
+		cfg, err := config.Load()
+		if err != nil {
+			cfg = config.Default()
+		}
+		s.cfg = cfg
 	}
-	return modelselection.Resolve(cfg, language.NewSystemDetector())
+	return modelselection.Resolve(s.cfg, language.NewSystemDetector())
+}
+
+func (s *EngineService) allowedModelProfile(modelID string) (model.ModelProfile, error) {
+	profile, err := model.GetModelProfile(modelID)
+	if err != nil {
+		return model.ModelProfile{}, err
+	}
+
+	current := s.currentModel()
+	languageProfile, err := model.GetLanguageProfile(current.LanguageSettings.EffectiveLanguageID)
+	if err != nil {
+		return model.ModelProfile{}, err
+	}
+	if profile.ID == languageProfile.DefaultModelID {
+		return profile, nil
+	}
+	for _, id := range languageProfile.UpgradeModelIDs {
+		if profile.ID == id {
+			return profile, nil
+		}
+	}
+	return model.ModelProfile{}, fmt.Errorf("model %s is not available for language %s", profile.ID, current.LanguageSettings.EffectiveLanguageID)
 }

--
Gitblit v1.9.3