Declare `clai2` plugin settings menus with `SettingsMenu` and ask through `Dialogs`

Merged
MP
2mo ago
+738-171

Part 15 of the clai2 web UI stack (… → #10289 → this PR). Based on #10289. No linked issue: requested by the CLAI maintainer.

Why. A plugin's settings menu today is code that draws termflow widgets in configure(), so only a terminal can show it. The web UI's plugins panel (#10289) can only send people back to the terminal for sign-ins and /keys entries. This PR lets a plugin declare its menu once, so the terminal and the web UI can each render it. The web rendering comes in the next PR.

New public plugin API (pydantic_clai2.plugins.menus)

  • Plugin.settings_menu() -> SettingsMenu | None. The base configure() now shows it in the terminal, so a plugin that declares a menu stops writing configure(). A plugin that overrides configure() keeps working unchanged, terminal-only.
  • SettingsMenu(source=..., actions=..., searchable=...).
    • source is the existing FieldSource, unchanged.
    • actions maps a row key to async (dialogs) -> messages, for rows that need more than one typed or picked value (a /keys entry, a sign-in).
  • Dialogs, a protocol with four async methods:
    • choose(title, options, current, description);
    • text(title, label, placeholder, initial, secret, check);
    • confirm(title, yes, no, description);
    • wait(title, description, task), which runs task(show) behind a cancellable screen.
    • Backing out returns None/False. These four cover every built-in plugin's menu: Coder's folder browser and Workspace's service list are loops of choose.
  • Moved: FieldRow, FieldSource and ChoicePreview now live in plugins.menus. field_menu still imports them, so existing imports keep working.
  • Plugin.has_configure is now true for either override.

Terminal implementation (ui/menus/dialogs.py)

  • TerminalDialogs shows each dialog as a termflow widget in its own run_worker, and takes scripted Runners in tests, as menus already do.
  • run_settings_menu runs the menu with run_flow_async, its actions bound to TerminalDialogs. It holds the live panel off the screen for the whole menu, not just per widget.
  • sign_in.wait_for_sign_in now uses TerminalDialogs.wait, so the sign-in waiting screen isn't duplicated. The one visible change is that its button says "Cancel" instead of "Cancel sign-in".
  • First adopter: compaction now declares settings_menu() instead of overriding configure(). The menu looks and behaves the same, except that the message when nothing changed now reads "Compaction settings: nothing changed."

Flicker prototype (done before this design). Running one run_worker per dialog means the panel is released between dialogs. I recorded raw terminal output (tmux pipe-pane) while editing compaction's menu three times, comparing today's single-worker flow, one worker per dialog, and one worker per dialog with a 250 ms pause between dialogs. In all three, 0 bytes reached the main screen between one widget leaving the alternate screen and the next entering it, and the per-dialog version added about 1 ms per switch. A turn streaming output during a slow action could still paint in that gap, so run_settings_menu holds the panel for the whole menu, which makes that impossible.

Verification.

  • tests/clai2/test_dialogs.py covers each dialog's widget (labels, current marker, search past 8, preview text, mask, validator, initial text, cancel paths), the wait results, a declared menu running an action under one hold, the base configure(), and has_configure.
  • The sign-in and compaction tests are updated. 100% branch coverage on plugins/menus.py, ui/menus/dialogs.py, plugins/sign_in.py and compaction.py.
  • Pyright and ruff are clean, and the affected suites pass (2,489 tests). One test, test_system_prompt_menu, failed once under xdist; it is terminal-size dependent and passes alone, with or without this change.
  • Real terminal in tmux: /plugins configure compaction → edited Threshold → Esc → "Saved Threshold.", with the same screens as before.

What changes for existing users. Nothing visible except the two small text changes above. Plugin authors get settings_menu(). Overriding configure() still works and stays terminal-only.

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.