diff --git a/docs/IMPLEMENTATION.md b/docs/IMPLEMENTATION.md index 3ce8110..5dd1e33 100644 --- a/docs/IMPLEMENTATION.md +++ b/docs/IMPLEMENTATION.md @@ -825,6 +825,12 @@ ruff format --check src/ tests/ .github/scripts/ `rewrite()` adds quoted spelling guidance; STT adds multipart `prompt` only when that provider's own opt-in is enabled and the selection is nonempty. These are hints, not guarantees. + `vocabulary_delivery(cfg) -> set[str]` (`config.py`, pure) reports where a nonempty + vocabulary goes: `"stt"` when the primary (or an enabled fallback) provider prompt is on, + `"rewrite"` when the manual profile (Automatic: the default profile) is not Raw. App mappings + to other profiles do not count. When the set is empty the Vocabulary page shows a warning + pointing at Replacements and a "Turn on provider prompt" button (enables the primary toggle + and opens the Speech page); it is recomputed on the dialog's working copy as inputs change. - The STT `prompt` is `build_whisper_prompt(selection)`: a bare comma list ending with a period, shaped like earlier transcript text, so no English header pulls the transcript toward English. Whisper keeps only the last ~224 prompt tokens and drops the start diff --git a/src/config.py b/src/config.py index 7a03624..e41a6b8 100644 --- a/src/config.py +++ b/src/config.py @@ -213,6 +213,31 @@ def _validated_rewrite_selection( return profiles, mappings +def vocabulary_delivery(cfg: AppConfig) -> set[str]: + """Where the vocabulary is sent: a subset of {"stt", "rewrite"}. + + Pure and Qt-free so the settings dialog can evaluate it on its working copy. + "rewrite" means the profile that applies when no app mapping matches (the manual + choice, or the default in Automatic mode) is not Raw; app mappings to other profiles + are deliberately ignored. An empty vocabulary is never sent anywhere. + """ + if not cfg.vocabulary_entries: + return set() + delivery: set[str] = set() + if cfg.stt_prompt_primary_enabled or ( + cfg.stt_fallback_enabled and cfg.stt_prompt_fallback_enabled + ): + delivery.add("stt") + profile_id = ( + cfg.rewrite_default_profile_id + if cfg.rewrite_selection_mode == "automatic" + else cfg.rewrite_manual_profile_id + ) + if profile_id != "raw": + delivery.add("rewrite") + return delivery + + def resolve_rewrite_profile(cfg: AppConfig, executable_path: str | None) -> RewriteProfile: """Pure resolution on a session snapshot: manual choice, or Auto mapping -> default.""" profiles, mappings = _validated_rewrite_selection(cfg) diff --git a/src/settings_dialog.py b/src/settings_dialog.py index a4daf72..12ce51a 100644 --- a/src/settings_dialog.py +++ b/src/settings_dialog.py @@ -7,6 +7,7 @@ from __future__ import annotations import copy +import dataclasses import logging import threading import uuid @@ -73,6 +74,7 @@ ) import httpx +import shiboken6 from src.config import ( COMMON_LANGUAGES, @@ -106,6 +108,7 @@ save_config, select_vocabulary, validate_config, + vocabulary_delivery, ) from src import http_client, theme from src.audio import resolve_device @@ -827,6 +830,7 @@ def _build_ui(self) -> None: bottom.addStretch() bottom.addWidget(self._button_box) main.addWidget(footer) + self._connect_vocabulary_delivery() # --- General tab --------------------------------------------------- @@ -1032,6 +1036,7 @@ def _build_stt_tab(self) -> None: _section(form, "Provider", first=True) advanced = Collapsible("Advanced") + self._stt_advanced = advanced self._stt_provider = ProviderFields( form, STT_PRESETS, @@ -1575,9 +1580,21 @@ def _build_audio_tab(self) -> None: def _build_vocabulary_tab(self) -> None: tab = QWidget() form = QFormLayout(tab) + self._vocabulary_warning = _hint(QLabel(), "warning") + self._vocabulary_warning.setWordWrap(True) + self._vocabulary_warning.setVisible(False) + form.addRow(self._vocabulary_warning) + self._vocabulary_enable_btn = QPushButton("Turn on provider prompt") + self._vocabulary_enable_btn.clicked.connect(self._on_enable_provider_prompt) + self._vocabulary_enable_row = _button_row(self._vocabulary_enable_btn) + form.addRow("", self._vocabulary_enable_row) + form.setRowVisible(self._vocabulary_enable_row, False) # hides the empty label too + self._vocabulary_form = form _section(form, "Preferred spellings", first=True) hint = QLabel( - "Names, acronyms and project terms. The AI rewrite uses them as spelling hints." + "Names, acronyms and project terms. Terms are sent as hints to (1) the " + "speech-to-text provider when “Send vocabulary to this provider” is on " + "(Speech → Advanced), and (2) the AI rewrite in every profile except Raw." ) hint.setWordWrap(True) form.addRow(_hint(hint)) @@ -1693,6 +1710,61 @@ def _populate_vocabulary_table(self, entries: list[VocabularyTerm]) -> None: table.setCellWidget(row, 1, combo) self._refresh_prompt_estimate() + def _connect_vocabulary_delivery(self) -> None: + """Re-evaluate the "vocabulary unused" warning whenever its inputs change.""" + model = self._vocabulary_table.model() + for signal in (model.rowsInserted, model.rowsRemoved, model.modelReset): + signal.connect(self._refresh_vocabulary_warning) + self._vocabulary_table.itemChanged.connect(self._refresh_vocabulary_warning) + for toggle in ( + self._stt_prompt_primary_check, + self._stt_prompt_fallback_check, + self._stt_fb_check, + ): + toggle.toggled.connect(self._refresh_vocabulary_warning) + for combo in (self._rewrite_active_combo, self._rewrite_default_combo): + combo.currentIndexChanged.connect(self._refresh_vocabulary_warning) + self._refresh_vocabulary_warning() + + def _refresh_vocabulary_warning(self, *_args) -> None: + if self._profile_loading or not shiboken6.isValid(self._rewrite_active_combo): + return # mid-repopulation, or the dialog is being torn down + automatic = self._rewrite_active_combo.currentData() == "__automatic__" + profile_id = ( + self._rewrite_default_combo.currentData() + if automatic + else self._rewrite_active_combo.currentData() + ) + snapshot = dataclasses.replace( + self._working, + vocabulary_entries=self._vocabulary_values(), + stt_prompt_primary_enabled=self._stt_prompt_primary_check.isChecked(), + stt_prompt_fallback_enabled=self._stt_prompt_fallback_check.isChecked(), + stt_fallback_enabled=self._stt_fb_check.isChecked(), + rewrite_selection_mode="automatic" if automatic else "manual", + rewrite_default_profile_id=profile_id, + rewrite_manual_profile_id=profile_id, + ) + unused = bool(snapshot.vocabulary_entries) and not vocabulary_delivery(snapshot) + if unused: + text = ( + "Your vocabulary isn't used right now: the Raw profile skips AI rewrite " + "and the Speech provider prompt is off. For guaranteed fixes use " + "Replacements below." + ) + if automatic and any(m.profile_id != "raw" for m in self._rewrite_app_mappings): + text += " Apps mapped to other profiles still use the vocabulary." + self._vocabulary_warning.setText(text) + self._vocabulary_warning.setVisible(unused) + self._vocabulary_form.setRowVisible(self._vocabulary_enable_row, unused) + + def _on_enable_provider_prompt(self) -> None: + """Switch the primary provider prompt on and show it on the Speech page.""" + self._stt_advanced.toggle.setChecked(True) + self._stt_prompt_primary_check.setChecked(True) + self._tabs.setCurrentIndex(1) # STT page + self._stt_prompt_primary_check.setFocus() + def _on_vocabulary_selected(self) -> None: row = self._vocabulary_table.currentRow() if 0 <= row < self._vocabulary_table.rowCount(): @@ -1839,6 +1911,7 @@ def _populate(self, cfg: AppConfig) -> None: self._fill_vocabulary_language_combo(self._vocabulary_language, "") self._replacements = list(cfg.replacements) self._refresh_replacement_list() + self._refresh_vocabulary_warning() def _collect(self) -> None: """Write widget values back into self._working.""" diff --git a/tests/test_settings_dialog.py b/tests/test_settings_dialog.py index 9c49079..3c9f518 100644 --- a/tests/test_settings_dialog.py +++ b/tests/test_settings_dialog.py @@ -12,7 +12,7 @@ from PySide6.QtGui import QFocusEvent from PySide6.QtWidgets import QApplication, QLineEdit, QDialog -from src.config import AppConfig, load_config, save_config +from src.config import AppConfig, AppMapping, load_config, save_config from src.utils import AppError, ScreamerError from src.settings_dialog import PasswordField, SettingsDialog @@ -611,3 +611,110 @@ def test_toggle_switch_keeps_checkbox_behavior(self) -> None: dlg._sound_check.click() self.assertTrue(dlg._sound_check.isChecked()) self.assertFalse(dlg._llm_group.isHidden()) + + +class VocabularyDeliveryTests(unittest.TestCase): + def test_delivery_by_profile_and_provider_prompt(self) -> None: + from src.config import vocabulary_delivery + + def cfg(**kw) -> AppConfig: + base = {"vocabulary_entries": ["PySide6"], "rewrite_manual_profile_id": "raw"} + return AppConfig(**{**base, **kw}) + + self.assertEqual(vocabulary_delivery(cfg()), set()) + self.assertEqual(vocabulary_delivery(cfg(stt_prompt_primary_enabled=True)), {"stt"}) + self.assertEqual(vocabulary_delivery(cfg(rewrite_manual_profile_id="clean")), {"rewrite"}) + self.assertEqual( + vocabulary_delivery( + cfg(rewrite_manual_profile_id="clean", stt_prompt_primary_enabled=True) + ), + {"stt", "rewrite"}, + ) + # A fallback prompt only counts while the fallback itself is enabled. + self.assertEqual(vocabulary_delivery(cfg(stt_prompt_fallback_enabled=True)), set()) + self.assertEqual( + vocabulary_delivery(cfg(stt_prompt_fallback_enabled=True, stt_fallback_enabled=True)), + {"stt"}, + ) + # Automatic mode follows the default profile, not the hidden manual choice. + self.assertEqual( + vocabulary_delivery( + cfg( + rewrite_selection_mode="automatic", + rewrite_default_profile_id="raw", + rewrite_manual_profile_id="clean", + ) + ), + set(), + ) + empty = cfg(vocabulary_entries=[], stt_prompt_primary_enabled=True) + self.assertEqual(vocabulary_delivery(empty), set()) + + +class VocabularyWarningTests(unittest.TestCase): + def setUp(self) -> None: + tmp = self.enterContext(tempfile.TemporaryDirectory()) + self.enterContext(patch("src.config.APP_DIR", tmp)) + self.enterContext(patch("src.config._dpapi_available", return_value=False)) + self.enterContext(patch("src.settings_dialog.is_supported", return_value=False)) + + def _dialog(self, **kw) -> SettingsDialog: + cfg = AppConfig( + stt_api_key="key", + stt_base_url="https://example.test/v1", + stt_model="stt", + llm_api_key="test", + llm_base_url="https://example.test/v1", + llm_model="test", + **kw, + ) + dlg = SettingsDialog(cfg, devices=[]) + self.addCleanup(dlg.deleteLater) + return dlg + + @staticmethod + def _shown(dlg: SettingsDialog) -> bool: + return not dlg._vocabulary_warning.isHidden() + + def test_warning_shows_for_raw_without_prompt_and_clears_on_toggle(self) -> None: + dlg = self._dialog(vocabulary_entries=["PySide6"], rewrite_manual_profile_id="raw") + self.assertTrue(self._shown(dlg)) + dlg._stt_prompt_primary_check.setChecked(True) + self.assertFalse(self._shown(dlg)) + dlg._stt_prompt_primary_check.setChecked(False) + self.assertTrue(self._shown(dlg)) + + def test_warning_clears_on_profile_change_and_button_enables_prompt(self) -> None: + dlg = self._dialog(vocabulary_entries=["PySide6"], rewrite_manual_profile_id="raw") + dlg._rewrite_active_combo.setCurrentIndex(dlg._rewrite_active_combo.findData("clean")) + self.assertFalse(self._shown(dlg)) + dlg._rewrite_active_combo.setCurrentIndex(dlg._rewrite_active_combo.findData("raw")) + self.assertTrue(self._shown(dlg)) + dlg._on_enable_provider_prompt() + self.assertTrue(dlg._stt_prompt_primary_check.isChecked()) + self.assertFalse(self._shown(dlg)) + + def test_no_warning_for_empty_vocabulary_and_it_follows_edits(self) -> None: + dlg = self._dialog(rewrite_manual_profile_id="raw") + self.assertFalse(self._shown(dlg)) + dlg._vocabulary_input.setText("PySide6") + dlg._on_vocabulary_add() + self.assertTrue(self._shown(dlg)) + + def test_automatic_caveat_only_with_non_raw_app_mapping(self) -> None: + dlg = self._dialog( + vocabulary_entries=["PySide6"], + rewrite_selection_mode="automatic", + rewrite_default_profile_id="raw", + ) + self.assertTrue(self._shown(dlg)) + self.assertNotIn("Apps mapped", dlg._vocabulary_warning.text()) + dlg._rewrite_app_mappings = [AppMapping("code.exe", "clean")] + dlg._refresh_vocabulary_warning() + self.assertIn("Apps mapped", dlg._vocabulary_warning.text()) + + def test_enable_button_row_follows_warning(self) -> None: + dlg = self._dialog(vocabulary_entries=["PySide6"], rewrite_manual_profile_id="raw") + self.assertTrue(dlg._vocabulary_form.isRowVisible(dlg._vocabulary_enable_row)) + dlg._stt_prompt_primary_check.setChecked(True) + self.assertFalse(dlg._vocabulary_form.isRowVisible(dlg._vocabulary_enable_row))