From 5df683eba7c103010a7c110d97a5fd808b39fe46 Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Thu, 02 Jul 2026 01:47:41 +0800
Subject: [PATCH] Fix local universal macOS package assembly

---
 privatevoice.src/services/engine_service.go |   82 ++++++++++++++++++++++++++++++++++++-----
 1 files changed, 72 insertions(+), 10 deletions(-)

diff --git a/privatevoice.src/services/engine_service.go b/privatevoice.src/services/engine_service.go
index 4dbfeb3..ecfb3ac 100755
--- a/privatevoice.src/services/engine_service.go
+++ b/privatevoice.src/services/engine_service.go
@@ -16,16 +16,20 @@
 
 // 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
+	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 {
@@ -117,6 +121,7 @@
 	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,
@@ -145,6 +150,7 @@
 		s.downloadMu.Unlock()
 		return false
 	}
+	s.downloadCancelling = true
 	s.downloadCancel()
 	s.downloadMu.Unlock()
 
@@ -156,9 +162,16 @@
 	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
@@ -186,6 +199,8 @@
 		"supportedLanguages":  profile.SupportedLanguageIDs,
 		"recommendedLanguage": profile.RecommendedFor,
 		"description":         profile.Description,
+		"supportedInBuild":    supportedInBuild,
+		"unsupportedReason":   model.ModelUnsupportedReason(profile.ID),
 	}
 }
 
@@ -215,6 +230,7 @@
 	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,
@@ -248,6 +264,7 @@
 	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,
@@ -304,15 +321,51 @@
 	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) {
@@ -320,6 +373,15 @@
 	if err != nil {
 		return model.ModelProfile{}, err
 	}
+	switch model.ModelUnsupportedReason(profile.ID) {
+	case "":
+	case model.UnsupportedReasonRequiresMacOS14:
+		return model.ModelProfile{}, fmt.Errorf("model %s requires macOS 14 or later", profile.ID)
+	case model.UnsupportedReasonWindowsPreview:
+		return model.ModelProfile{}, fmt.Errorf("model %s is not supported in the Windows preview build; use SenseVoice", profile.ID)
+	default:
+		return model.ModelProfile{}, fmt.Errorf("model %s is not supported in this build", profile.ID)
+	}
 
 	current := s.currentModel()
 	languageProfile, err := model.GetLanguageProfile(current.LanguageSettings.EffectiveLanguageID)

--
Gitblit v1.9.3