Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ On first real run Whisper downloads its model (`base` is about 150 MB).
| Action | Default |
| --- | --- |
| Suggest replies to the last sentence | `Ctrl+Alt+S` |
| Suggest replies to one of their recent sentences | `Ctrl+Alt+1` (newest) … `Ctrl+Alt+4` |
| Show / hide the window | `Ctrl+Alt+H` |
| Lock / unlock following the latest sentence | `Ctrl+Alt+L` |

Inside the window: `Ctrl+Enter` suggests, `Esc` hides. Click a suggestion to copy it.

Each of the other person's finished sentences that a numbered hotkey can reach is outlined
in orange, with its hotkey in the top-right corner: `1` is the newest, `2` the one before,
and so on. Settings → Window sets how many sentences get one (1 to 10, following the
number row: `1`–`9`, then `0`) and which keys are held with the number.

The transcript follows the newest sentence (🔒, next to Suggest). Scrolling by hand
unlocks it (🔓) so the view stays where you are reading; it follows again only when you
click the lock button or press `Ctrl+Alt+L`, which also jumps to the latest sentence.
Expand Down
2 changes: 2 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ hide_from_capture = true
hotkey_suggest = "<ctrl>+<alt>+s"
hotkey_toggle = "<ctrl>+<alt>+h"
hotkey_lock = "<ctrl>+<alt>+l" # lock/unlock following the latest sentence
reply_hotkeys = 4 # their latest N sentences (1-10) get modifiers + 1, 2, ... 0
reply_hotkey_modifiers = "<ctrl>+<alt>" # empty turns the numbered reply hotkeys off

[privacy]
log_content = false
Expand Down
14 changes: 11 additions & 3 deletions src/polyglot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path
from typing import Any

from polyglot.config import AppConfig, ConfigStore, Secrets
from polyglot.config import AppConfig, ConfigStore, Secrets, UiConfig, hotkey_label
from polyglot.controller import PIPELINE_SECTIONS, AppController
from polyglot.core.errors import ConfigError
from polyglot.core.events import EventBus
Expand Down Expand Up @@ -115,9 +115,17 @@ def start_stop() -> None:

hotkeys = GlobalHotkeys()
hotkeys.suggest.connect(presenter.on_suggest)
hotkeys.suggest_slot.connect(presenter.on_suggest_slot)
hotkeys.toggle_window.connect(window.toggle_visible)
hotkeys.toggle_lock.connect(window.toggle_lock)
hotkeys.bind(config.ui.hotkey_suggest, config.ui.hotkey_toggle, config.ui.hotkey_lock)

def bind_hotkeys(ui: UiConfig) -> None:
slots = ui.reply_hotkey_combos()
bound = hotkeys.bind(ui.hotkey_suggest, ui.hotkey_toggle, ui.hotkey_lock, slots)
# Only label the bubbles with hotkeys that actually work.
presenter.set_reply_keys(tuple(hotkey_label(c) for c in slots) if bound else ())

bind_hotkeys(config.ui)

model_catalog = OpenAIModelCatalog() # caches the account's model list per session
mic_tester = SoundDeviceMicTester()
Expand All @@ -143,7 +151,7 @@ def open_settings() -> None:
refresh_flags()
presenter.on_user_language(new.translation.target_language)
window.setWindowOpacity(new.ui.opacity)
hotkeys.bind(new.ui.hotkey_suggest, new.ui.hotkey_toggle, new.ui.hotkey_lock)
bind_hotkeys(new.ui)
if new.ui.hide_from_capture and shield_filter is None:
shield_filter = CaptureShieldFilter(platform.window_shield())
shield_filter.status_changed.connect(presenter.on_shield_status)
Expand Down
22 changes: 22 additions & 0 deletions src/polyglot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ class UiConfig(_Section):
hotkey_suggest: str = "<ctrl>+<alt>+s"
hotkey_toggle: str = "<ctrl>+<alt>+h"
hotkey_lock: str = "<ctrl>+<alt>+l" # lock/unlock following the latest sentence
#: How many of the other person's latest sentences get a reply hotkey of their own:
#: modifiers + 1 for the newest, + 2 for the one before, ... + 0 for the tenth.
reply_hotkeys: int = Field(4, ge=1, le=10)
#: The keys held with the digit for those hotkeys. Empty turns them off.
reply_hotkey_modifiers: str = "<ctrl>+<alt>"

def reply_hotkey_combos(self) -> tuple[str, ...]:
"""The reply hotkeys, newest sentence first, in number-row order (1-9, then 0)."""
mods = self.reply_hotkey_modifiers.strip().strip("+")
if not mods:
return ()
return tuple(f"{mods}+{d}" for d in REPLY_DIGITS[: self.reply_hotkeys])


#: The number row, left to right: the order of the reply hotkeys.
REPLY_DIGITS = "1234567890"


def hotkey_label(combo: str) -> str:
"""``"<ctrl>+<alt>+1"`` -> ``"Ctrl+Alt+1"``: how a hotkey is shown to the user."""
parts = [p.strip().strip("<>") for p in combo.split("+") if p.strip()]
return "+".join(p.upper() if len(p) == 1 else p.capitalize() for p in parts)


class PrivacyConfig(_Section):
Expand Down
5 changes: 3 additions & 2 deletions src/polyglot/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ def toggle(self) -> None:
else:
self.start()

def request_suggestion(self) -> None:
def request_suggestion(self, utterance_id: int | None = None) -> None:
"""Replies to the latest sentence, or to ``utterance_id`` (a reply hotkey)."""
built = self.ensure_built()
if built is not None:
built.pipeline.request_suggestion()
built.pipeline.request_suggestion(utterance_id)

def set_language(self, language: str | None) -> None:
built = self.ensure_built()
Expand Down
22 changes: 13 additions & 9 deletions src/polyglot/core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,17 +738,20 @@ def _on_translation(self, msg: _TranslationDone) -> None:
log.info("utt=%d translated translate_ms=%.0f", msg.uid, msg.ms)

# ------------------------------------------------------------------ suggestions
def _request_suggestion(self) -> None:
def _request_suggestion(self, utterance_id: int | None = None) -> None:
if self._suggester is None:
self._bus.publish(
StageError("suggest", ErrorKind.NOT_CONFIGURED, "suggestions are not enabled")
)
return
target = self.session.latest_answerable()
if utterance_id is None:
target = self.session.latest_answerable()
missing = "nothing has been said yet"
else:
target = self.session.answerable(utterance_id)
missing = f"utterance {utterance_id} is not something to reply to"
if target is None:
self._bus.publish(
StageError("suggest", ErrorKind.NOTHING_TO_ANSWER, "nothing has been said yet")
)
self._bus.publish(StageError("suggest", ErrorKind.NOTHING_TO_ANSWER, missing))
return
self._suggest_gen += 1
gen = self._suggest_gen
Expand Down Expand Up @@ -781,7 +784,7 @@ def _on_suggestion(self, msg: _SuggestionDone) -> None:
# ------------------------------------------------------------------ commands
def _on_command(self, cmd: _Command) -> None:
if cmd.name == "suggest":
self._request_suggestion()
self._request_suggestion(cmd.arg)
elif cmd.name == "set_language":
self.session.set_language(cmd.arg)
self._bus.publish(LanguageChanged(cmd.arg, locked=cmd.arg is not None))
Expand Down Expand Up @@ -1126,9 +1129,10 @@ def wait_idle(self, timeout: float = 30.0) -> bool:
return False

# ------------------------------------------------------------------ commands
def request_suggestion(self) -> None:
"""Ask for replies to the latest final utterance (bound to the suggest hotkey)."""
self._coord.post(_Command("suggest"))
def request_suggestion(self, utterance_id: int | None = None) -> None:
"""Ask for replies to one of the other person's sentences: the latest by default
(the suggest hotkey), or a given one (the numbered reply hotkeys)."""
self._coord.post(_Command("suggest", utterance_id))

def set_language(self, language: str | None) -> None:
"""Force the source language, or ``None`` to unlock and auto-detect again."""
Expand Down
24 changes: 18 additions & 6 deletions src/polyglot/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
from polyglot.core.models import Speaker, Utterance, UtteranceStatus


def is_answerable(u: Utterance) -> bool:
"""Something to reply to: a finished sentence with text, said by the other person.
The user's own turns and failed transcripts never are. The presenter uses the same
rule to decide which sentences get a reply hotkey."""
return (
u.speaker is Speaker.OTHER
and u.is_final
and bool(u.text)
and u.status is not UtteranceStatus.FAILED
)


@dataclass(frozen=True)
class LanguagePolicy:
"""Lock the source language once ``agree`` consecutive finals agree confidently."""
Expand Down Expand Up @@ -53,15 +65,15 @@ def latest_answerable(self) -> Utterance | None:
"""The most recent final utterance by the other person that has text (failed ones
are skipped). The user's own turns are never something to reply to."""
for u in reversed(self.ordered()):
if (
u.speaker is Speaker.OTHER
and u.is_final
and u.text
and u.status is not UtteranceStatus.FAILED
):
if is_answerable(u):
return u
return None

def answerable(self, utterance_id: int) -> Utterance | None:
"""That utterance, if it is one replies can be suggested for; otherwise ``None``."""
u = self.utterances.get(utterance_id)
return u if u is not None and is_answerable(u) else None

def ordered(self) -> list[Utterance]:
return [self.utterances[k] for k in sorted(self.utterances)]

Expand Down
66 changes: 61 additions & 5 deletions src/polyglot/ui/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from polyglot.core.models import ErrorKind, Speaker, Utterance, UtteranceStatus
from polyglot.core.ports import ShieldStatus
from polyglot.core.session import is_answerable

ERROR_TEXT: dict[ErrorKind, str] = {
ErrorKind.TIMEOUT: "timed out",
Expand All @@ -51,6 +52,9 @@ class RowVM:
partial: bool = False
failed: bool = False
mine: bool = False # said by the user (from their microphone), not the other person
#: The hotkey that suggests replies to this sentence ("Ctrl+Alt+1"), shown on the
#: bubble, which is highlighted while it has one. ``None``: no hotkey reaches it.
reply_key: str | None = None


@dataclass(frozen=True)
Expand Down Expand Up @@ -123,7 +127,7 @@ def set_capture(self, vm: CaptureVM) -> None:

class Commands(Protocol):
def toggle(self) -> None: ...
def request_suggestion(self) -> None: ...
def request_suggestion(self, utterance_id: int | None = None) -> None: ...
def set_language(self, language: str | None) -> None: ...
def clear(self) -> None: ...

Expand All @@ -136,8 +140,15 @@ class Presenter:
user_language: str = "en"
translation_enabled: bool = False
rows: dict[int, Utterance] = field(default_factory=dict)
#: Labels of the reply hotkeys, for the newest sentence first ("Ctrl+Alt+1", ...).
reply_keys: tuple[str, ...] = ()
suggestion_for: int | None = None
_suggestion: SuggestionVM | None = None
#: The newest sentence when the shown suggestion was asked for: anything they say
#: after it makes the suggestion stale (not merely being older than the latest, since
#: a reply hotkey can deliberately answer an earlier sentence).
_asked_at: int | None = None
_slots: dict[int, str] = field(default_factory=dict) # utterance ID -> its reply hotkey
_state: PipelineState = PipelineState.STOPPED
_cleared_upto: int = 0 # events about utterances <= this belong to a cleared conversation

Expand All @@ -148,23 +159,30 @@ def handle(self, event: Event) -> None:
if isinstance(event, ConversationCleared):
self._cleared_upto = max(self._cleared_upto, event.upto)
self.rows.clear()
self._slots = {}
self.suggestion_for = None
self._suggestion = None
self._asked_at = None
self.view.clear_rows()
self.view.show_suggestions(SuggestionVM(visible=False))
elif isinstance(event, UtteranceUpdated):
self._on_utterance(event.utterance)
elif isinstance(event, UtteranceDiscarded):
self.rows.pop(event.utterance_id, None)
self._slots.pop(event.utterance_id, None)
self.view.remove_row(event.utterance_id)
self._refresh_slots()
elif isinstance(event, SuggestionStarted):
self.suggestion_for = event.utterance_id
self._asked_at = max(self._latest_final(), event.utterance_id)
self._show_suggestion(
SuggestionVM(reply_to=self._text(event.utterance_id), loading=True)
)
elif isinstance(event, SuggestionsReady):
s = event.suggestion
self.suggestion_for = s.utterance_id
if self._asked_at is None:
self._asked_at = s.utterance_id
if s.error is not None:
vm = SuggestionVM(
reply_to=self._text(s.utterance_id),
Expand All @@ -185,12 +203,37 @@ def handle(self, event: Event) -> None:

def _on_utterance(self, u: Utterance) -> None:
self.rows[u.id] = u
self.view.upsert_row(self.row_vm(u))
if self._suggestion is not None and self.suggestion_for is not None:
stale = self._latest_final() > self.suggestion_for
self._refresh_slots(always=u.id)
if self._suggestion is not None and self._asked_at is not None:
stale = self._is_stale()
if stale != self._suggestion.stale:
self._show_suggestion(self._suggestion, stale=stale)

# ------------------------------------------------------------ reply hotkeys
def set_reply_keys(self, labels: tuple[str, ...]) -> None:
"""The reply hotkeys now bound, newest sentence first. Re-labels the bubbles."""
self.reply_keys = tuple(labels)
self._refresh_slots()

def reply_targets(self) -> list[int]:
"""The sentences the reply hotkeys reach, newest first: the other person's latest
finished sentences, one per hotkey."""
ids = sorted((uid for uid, u in self.rows.items() if is_answerable(u)), reverse=True)
return ids[: len(self.reply_keys)]

def _refresh_slots(self, *, always: int | None = None) -> None:
"""Give each reachable sentence its hotkey, and redraw the bubbles whose hotkey
changed (a new sentence shifts every label down by one), plus ``always``."""
new = dict(zip(self.reply_targets(), self.reply_keys, strict=False)) # fewer sentences
old, self._slots = self._slots, new
changed = {uid for uid in old.keys() | new.keys() if old.get(uid) != new.get(uid)}
if always is not None:
changed.add(always)
for uid in sorted(changed):
row = self.rows.get(uid)
if row is not None:
self.view.upsert_row(self.row_vm(row))

def _on_status(self, event: PipelineStatus) -> None:
self._state = event.state
busy = event.state is PipelineState.LOADING
Expand Down Expand Up @@ -262,6 +305,7 @@ def row_vm(self, u: Utterance) -> RowVM:
partial=u.status is UtteranceStatus.PARTIAL,
failed=failed,
mine=u.speaker is Speaker.USER,
reply_key=self._slots.get(u.id),
)

def language_label(self, language: str | None, locked: bool) -> str:
Expand Down Expand Up @@ -290,9 +334,12 @@ def _latest_final(self) -> int:
]
return max(finals, default=0)

def _is_stale(self) -> bool:
return self._asked_at is not None and self._latest_final() > self._asked_at

def _show_suggestion(self, vm: SuggestionVM, *, stale: bool | None = None) -> None:
if stale is None:
stale = self.suggestion_for is not None and self._latest_final() > self.suggestion_for
stale = self._is_stale()
vm = SuggestionVM(vm.reply_to, vm.options, vm.loading, vm.message, stale, True)
self._suggestion = vm
self.view.show_suggestions(vm)
Expand All @@ -304,6 +351,15 @@ def on_start_stop(self) -> None:
def on_suggest(self) -> None:
self.commands.request_suggestion()

def on_suggest_slot(self, index: int) -> None:
"""A reply hotkey: 0 is the newest of their sentences, 1 the one before, ..."""
targets = self.reply_targets()
if 0 <= index < len(targets):
self.commands.request_suggestion(targets[index])
return
key = self.reply_keys[index] if 0 <= index < len(self.reply_keys) else str(index + 1)
self._show_suggestion(SuggestionVM(message=f"Nothing to reply to with {key} yet."))

def on_clear(self) -> None:
# The view clears when the core confirms (ConversationCleared), so results
# already on their way cannot bring old rows back.
Expand Down
Loading
Loading