Skip to content

Rework the examples around a fast check and a small model parse - #22

Merged
nigelgilbert merged 1 commit into
mainfrom
examples/model-parsed-hooks
Nov 22, 2026
Merged

nigelgilbert merged 1 commit into
mainfrom
examples/model-parsed-hooks

Conversation

@nigelgilbert

Copy link
Copy Markdown
Owner

What this changes

The examples are reworked so that each one makes a fast check for its word, and only then asks a small local model to work out what was meant. A dictation without the word is returned untouched and costs no model call.

File Replaces Say What happens
translate_ja_es.py translate.py "In Japanese, where is the train station?" or "See you at noon, in Spanish." It is pasted in that language
web_search.py ask.py "Search for ramen in Tokyo", or "Search Firefox for ramen in Tokyo" The Google search opens, nothing is pasted
mac_commands.py voice_commands.py "Mac, dark mode", "Macintosh, silence the speakers", "Mac, open Notes" The command runs, nothing is pasted
terminal.py (reworked) "Terminal", "command line", "shell" or "bash", then the request The zsh command is pasted, never run

talk_like_a_pirate.py and hud.py are unchanged.

How each one works

  • Translate. The regex only checks that "Japanese" or "Spanish" is mentioned. The model then replies {"language": ...} or null, which is how "The menu was written in Japanese." is left alone. The code removes the request from whichever end of the sentence it is on. The old version only accepted the request at the front and was limited to an exact "in " opening.
  • Web search. The word "search" anywhere triggers it. The model returns {"browser", "query"}, so the browser can be named anywhere in the sentence. If the reply does not parse, the whole sentence is searched in the default browser.
  • Mac commands. The dictation must open with "Mac" or "Macintosh". The model replies with one label from the COMMANDS dict, open <app>, or none. none hands the text back as an ordinary dictation. New commands: light mode, Night Shift on and off. "dark mode" now sets dark instead of toggling.
  • Terminal. One on_cleaned hook and no module state. It uses mlx-community/Qwen2.5-Coder-7B-Instruct-4bit instead of the 4B model, and strips backticks the model sometimes wraps the command in.

Every hook now returns a string on every path (return t to change nothing) instead of None.

Things a reviewer should know

  • web_search.py maps "Firefox" to Waterfox. That matches the author's setup. Anyone without Waterfox gets an error when they name Firefox, until they edit BROWSERS. The examples README says so.
  • Night Shift uses a private framework. There is no command-line switch for it, so the hook calls CoreBrightness through osascript. It works on macOS 26.4 and may break in a later release.
  • Dark and light mode need Automation permission for System Events, asked once.
  • The hooks can clash when stacked. web_search.py acts in on_transcribed, so "Terminal, search for files named foo" opens a browser before terminal.py sees it.
  • Python 3.14 syntax. web_search.py uses except A, B: without parentheses, as ruff format writes it for this project.
  • Models. terminal.py downloads a second model of about 4 GB the first time.

Testing

Each file was run against the real models on sample dictations, with browser launches and Mac commands replaced by prints:

  • translate: 18 of 19. The miss is "In Spanish the word for bread is pan.", which is translated when it should be left alone.
  • web search: all sample phrases chose the right browser and query.
  • Mac commands: 31 of 31, including phrases that should match nothing.
  • terminal: commands came back in under a second each. Two of nine were wrong for macOS (rename is not installed by default, and an lsof | awk pipeline passes the header line to kill).

Night Shift on and off was run for real. The other Mac commands and the browser launches were not run for real by these checks. There are no automated tests for the examples, and nothing under sidecar/ changes. ruff check and ruff format --check pass on examples/.

The two README edits bring the examples table and the one-line summary in the root README in line with the new files.

Each example now checks for its word first and returns the text untouched when it is absent, so the model is only asked when a dictation is meant for that file.

- translate_ja_es.py replaces translate.py: the request can open or end the sentence, and the model decides whether a mention of the language is a request.
- web_search.py replaces ask.py: say "search" and the model picks out the browser and the query.
- mac_commands.py replaces voice_commands.py: the model classifies free phrasing into a fixed list of commands, with light mode and Night Shift added.
- terminal.py takes four opening words, keeps no state between hooks and uses a 7B coder model.
@nigelgilbert
nigelgilbert merged commit a9d040c into main Nov 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant