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/frontend/src/components/settings/LanguagePage.svelte |  136 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 116 insertions(+), 20 deletions(-)

diff --git a/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte b/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
index 24c435c..d486394 100644
--- a/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
+++ b/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
@@ -14,6 +14,8 @@
     isDefault: boolean
     downloadSize?: string
     description?: string
+    supportedInBuild?: boolean
+    unsupportedReason?: string
   }
 
   interface LanguageOption {
@@ -41,8 +43,30 @@
 
   Events.On('model:download-progress', (ev: any) => {
     const data = ev?.data
-    if (data?.modelID && data.modelID === modelBusyID && typeof data.percent === 'number') {
+    if (data?.modelID && typeof data.percent === 'number') {
+      modelBusyID = data.modelID
+      modelBusyKind = 'download'
+      if (modelCancellingID !== data.modelID) {
+        modelCancellingID = ''
+      }
       modelProgress = data.percent
+    }
+  })
+
+  Events.On('model:download-cancelled', (ev: any) => {
+    const data = ev?.data
+    if (data?.modelID) {
+      modelBusyID = data.modelID
+      modelBusyKind = 'download'
+      modelCancellingID = data.modelID
+    }
+  })
+
+  Events.On('model:download-finished', (ev: any) => {
+    const data = ev?.data
+    if (!data?.modelID || data.modelID === modelBusyID) {
+      clearModelBusy()
+      loadModelOptions()
     }
   })
 
@@ -52,6 +76,7 @@
       applyLanguageSettings(lang)
     } catch {}
     await loadModelOptions()
+    await syncModelDownloadStatus()
     settingsLoaded = true
   }
   loadSettings()
@@ -94,6 +119,37 @@
     }
   }
 
+  async function syncModelDownloadStatus(): Promise<boolean> {
+    try {
+      const status: any = await Call.ByName('voicesnap/services.EngineService.GetModelDownloadStatus')
+      return applyModelDownloadStatus(status)
+    } catch {
+      return false
+    }
+  }
+
+  function applyModelDownloadStatus(status: any): boolean {
+    if (status?.active && status?.modelID) {
+      modelBusyID = status.modelID
+      modelBusyKind = 'download'
+      modelCancellingID = status.cancelling ? status.modelID : ''
+      modelProgress = typeof status.percent === 'number' ? status.percent : 0
+      modelError = ''
+      return true
+    }
+    if (modelBusyKind === 'download') {
+      clearModelBusy()
+    }
+    return false
+  }
+
+  function clearModelBusy() {
+    modelBusyID = ''
+    modelBusyKind = ''
+    modelCancellingID = ''
+    modelProgress = 0
+  }
+
   function modelDescription(option: ModelOption): string {
     switch (option.modelID) {
       case 'sensevoice-zh': return t('models.sensevoiceDesc')
@@ -114,12 +170,28 @@
   }
 
   function modelTierLabel(option: ModelOption): string {
+    if (!modelSupported(option)) return t('settings.modelUnavailable')
     if (option.isDefault) return t('settings.modelDefault')
     if (option.tier === 'advanced') return t('settings.modelAdvanced')
     return option.tier || ''
   }
 
+  function modelSupported(option: ModelOption): boolean {
+    return option.supportedInBuild !== false
+  }
+
+  function modelUnsupportedText(option: ModelOption): string {
+    if (option.unsupportedReason === 'requires_macos_14') {
+      return t('models.qwen3RequiresMacOS14')
+    }
+    if (option.unsupportedReason === 'windows_preview_unsupported') {
+      return t('models.windowsPreviewUnsupported')
+    }
+    return t('settings.modelUnavailable')
+  }
+
   function modelActionLabel(option: ModelOption): string {
+    if (!modelSupported(option)) return t('settings.modelUnavailable')
     if (modelBusyID === option.modelID) {
       if (modelBusyKind === 'download') {
         return modelCancellingID === option.modelID ? t('settings.modelCancelling') : t('settings.modelCancel')
@@ -133,6 +205,9 @@
 
   function modelMeta(option: ModelOption): string {
     let meta = option.installed ? t('settings.modelInstalled') : t('settings.modelNotInstalled')
+    if (!modelSupported(option)) {
+      return `${meta} · ${modelUnsupportedText(option)}`
+    }
     if (option.downloadSize) {
       meta += ` · ${option.downloadSize}`
     }
@@ -144,8 +219,23 @@
   }
 
   function isCancelError(err: any): boolean {
-    const text = String(err?.message || err || '').toLowerCase()
+    const text = errorMessage(err).toLowerCase()
     return text.includes('context canceled') || text.includes('cancelled') || text.includes('canceled')
+  }
+
+  function isAlreadyDownloadingError(err: any): boolean {
+    return errorMessage(err).toLowerCase().includes('already downloading')
+  }
+
+  function errorMessage(err: any): string {
+    const raw = String(err?.message || err || '')
+    if (!raw.startsWith('{')) return raw
+    try {
+      const parsed = JSON.parse(raw)
+      return String(parsed?.message || raw)
+    } catch {
+      return raw
+    }
   }
 
   function canCancelModel(option: ModelOption): boolean {
@@ -167,24 +257,20 @@
     try {
       const cancelled: any = await Call.ByName('voicesnap/services.EngineService.CancelModelDownload', option.modelID)
       if (!cancelled) {
-        modelBusyID = ''
-        modelBusyKind = ''
-        modelCancellingID = ''
-        modelProgress = 0
+        clearModelBusy()
         await loadModelOptions()
+        await syncModelDownloadStatus()
       }
     } catch (err: any) {
-      modelError = err?.message || String(err || t('settings.modelActionFailed'))
-      modelBusyID = ''
-      modelBusyKind = ''
-      modelCancellingID = ''
-      modelProgress = 0
+      modelError = errorMessage(err) || t('settings.modelActionFailed')
+      clearModelBusy()
       await loadModelOptions()
+      await syncModelDownloadStatus()
     }
   }
 
   async function onModelAction(option: ModelOption) {
-    if ((option.isCurrent && option.installed) || modelBusyID) return
+    if (!modelSupported(option) || (option.isCurrent && option.installed) || modelBusyID) return
     modelBusyID = option.modelID
     modelBusyKind = option.installed ? 'select' : 'download'
     modelProgress = 0
@@ -199,16 +285,21 @@
       }
       await loadModelOptions()
     } catch (err: any) {
-      if (!isCancelError(err)) {
-        modelError = err?.message || String(err || t('settings.modelActionFailed'))
+      if (isAlreadyDownloadingError(err)) {
+        const active = await syncModelDownloadStatus()
+        if (!active) {
+          modelError = errorMessage(err)
+        }
+      } else if (!isCancelError(err)) {
+        modelError = errorMessage(err) || t('settings.modelActionFailed')
       }
       await loadModelOptions()
     } finally {
       if (modelBusyID === option.modelID) {
-        modelBusyID = ''
-        modelBusyKind = ''
-        modelCancellingID = ''
-        modelProgress = 0
+        const active = await syncModelDownloadStatus()
+        if (!active || modelBusyID !== option.modelID) {
+          clearModelBusy()
+        }
       }
     }
   }
@@ -232,6 +323,7 @@
       applyLanguageSettings(settings)
       await syncEngineForCurrentModel()
       await loadModelOptions()
+      await syncModelDownloadStatus()
     } catch {
       languageModeVal = prevMode
       languageIDVal = prevID
@@ -288,7 +380,7 @@
 
     <div class="model-list">
       {#each modelOptions as option}
-        <div class="model-card" class:current={option.isCurrent}>
+        <div class="model-card" class:current={option.isCurrent} class:unsupported={!modelSupported(option)}>
           <div class="model-main">
             <div class="model-title-row">
               <span class="model-title">{option.displayName}</span>
@@ -305,7 +397,7 @@
             <button
               class="model-action primary"
               class:cancel={canCancelModel(option)}
-              disabled={!!modelBusyID && modelBusyID !== option.modelID}
+              disabled={!modelSupported(option) || (!!modelBusyID && modelBusyID !== option.modelID)}
               onclick={() => onModelButton(option)}
             >
               {modelActionLabel(option)}
@@ -431,6 +523,10 @@
     background: rgba(0, 122, 255, 0.055);
   }
 
+  .model-card.unsupported {
+    opacity: 0.76;
+  }
+
   .model-card.current::before {
     content: '';
     position: absolute;

--
Gitblit v1.9.3