Ariver
2026-06-29 9d4f977026bbb4516f8cc97f715c0dc28bf2a13f
privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
@@ -14,6 +14,8 @@
    isDefault: boolean
    downloadSize?: string
    description?: string
    supportedInBuild?: boolean
    unsupportedReason?: string
  }
  interface LanguageOption {
@@ -30,6 +32,8 @@
  let languageOptions = $state<LanguageOption[]>([])
  let modelOptions = $state<ModelOption[]>([])
  let modelBusyID = $state('')
  let modelBusyKind = $state<'download' | 'select' | ''>('')
  let modelCancellingID = $state('')
  let modelProgress = $state(0)
  let modelError = $state('')
@@ -39,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()
    }
  })
@@ -50,6 +76,7 @@
      applyLanguageSettings(lang)
    } catch {}
    await loadModelOptions()
    await syncModelDownloadStatus()
    settingsLoaded = true
  }
  loadSettings()
@@ -78,7 +105,6 @@
        engineStatus.set('loading')
      } else {
        engineHardwareInfo.set('')
        engineStatus.set('need_model')
      }
      await Call.ByName('voicesnap/services.EngineService.ReloadCurrentModel')
    } catch {}
@@ -91,6 +117,37 @@
    } catch {
      modelOptions = []
    }
  }
  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 {
@@ -113,26 +170,106 @@
  }
  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')
    }
    return t('settings.modelUnavailable')
  }
  function modelActionLabel(option: ModelOption): string {
    if (!modelSupported(option)) return t('settings.modelUnavailable')
    if (modelBusyID === option.modelID) {
      if (!option.installed && modelProgress > 0) {
        return `${Math.min(100, Math.max(0, modelProgress)).toFixed(0)}%`
      if (modelBusyKind === 'download') {
        return modelCancellingID === option.modelID ? t('settings.modelCancelling') : t('settings.modelCancel')
      }
      return t('settings.modelWorking')
    }
    if (option.isCurrent) return t('settings.modelCurrent')
    if (option.isCurrent && option.installed) return t('settings.modelCurrent')
    if (option.installed) return t('settings.modelUse')
    return t('settings.modelDownload')
  }
  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}`
    }
    if (modelBusyID === option.modelID && modelBusyKind === 'download' && modelProgress > 0) {
      const pct = Math.min(100, Math.max(0, modelProgress)).toFixed(0)
      meta += ` · ${pct}%`
    }
    return meta
  }
  function isCancelError(err: any): boolean {
    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 {
    return modelBusyID === option.modelID && modelBusyKind === 'download'
  }
  async function onModelButton(option: ModelOption) {
    if (canCancelModel(option)) {
      await cancelModelDownload(option)
      return
    }
    await onModelAction(option)
  }
  async function cancelModelDownload(option: ModelOption) {
    if (!canCancelModel(option) || modelCancellingID) return
    modelCancellingID = option.modelID
    modelError = ''
    try {
      const cancelled: any = await Call.ByName('voicesnap/services.EngineService.CancelModelDownload', option.modelID)
      if (!cancelled) {
        clearModelBusy()
        await loadModelOptions()
        await syncModelDownloadStatus()
      }
    } catch (err: any) {
      modelError = errorMessage(err) || t('settings.modelActionFailed')
      clearModelBusy()
      await loadModelOptions()
      await syncModelDownloadStatus()
    }
  }
  async function onModelAction(option: ModelOption) {
    if (option.isCurrent || modelBusyID) return
    if (!modelSupported(option) || (option.isCurrent && option.installed) || modelBusyID) return
    modelBusyID = option.modelID
    modelBusyKind = option.installed ? 'select' : 'download'
    modelProgress = 0
    modelError = ''
    try {
@@ -145,11 +282,22 @@
      }
      await loadModelOptions()
    } catch (err: any) {
      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 {
      modelBusyID = ''
      modelProgress = 0
      if (modelBusyID === option.modelID) {
        const active = await syncModelDownloadStatus()
        if (!active || modelBusyID !== option.modelID) {
          clearModelBusy()
        }
      }
    }
  }
@@ -172,6 +320,7 @@
      applyLanguageSettings(settings)
      await syncEngineForCurrentModel()
      await loadModelOptions()
      await syncModelDownloadStatus()
    } catch {
      languageModeVal = prevMode
      languageIDVal = prevID
@@ -228,29 +377,25 @@
    <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>
              <span class="model-pill" class:current={option.isCurrent}>
                {option.isCurrent ? t('settings.modelCurrent') : modelTierLabel(option)}
              <span class="model-pill" class:current={option.isCurrent && option.installed}>
                {option.isCurrent && option.installed ? t('settings.modelCurrent') : modelTierLabel(option)}
              </span>
            </div>
            <span class="model-desc">{modelDescription(option)}</span>
            <span class="model-meta">
              {option.installed ? t('settings.modelInstalled') : t('settings.modelNotInstalled')}
              {#if option.downloadSize}
                · {option.downloadSize}
              {/if}
            </span>
            <span class="model-meta">{modelMeta(option)}</span>
          </div>
          {#if option.isCurrent}
          {#if option.isCurrent && option.installed}
            <span class="model-current-status">{t('settings.modelCurrent')}</span>
          {:else}
            <button
              class="model-action primary"
              disabled={!!modelBusyID}
              onclick={() => onModelAction(option)}
              class:cancel={canCancelModel(option)}
              disabled={!modelSupported(option) || (!!modelBusyID && modelBusyID !== option.modelID)}
              onclick={() => onModelButton(option)}
            >
              {modelActionLabel(option)}
            </button>
@@ -375,6 +520,10 @@
    background: rgba(0, 122, 255, 0.055);
  }
  .model-card.unsupported {
    opacity: 0.76;
  }
  .model-card.current::before {
    content: '';
    position: absolute;
@@ -456,6 +605,10 @@
    color: white;
  }
  .model-action.primary.cancel {
    background: var(--color-red);
  }
  .model-action:disabled {
    cursor: not-allowed;
    opacity: 0.72;