Skip to content

refactor: extract settings menu from telegram bot - #161

Merged
QueryPlanner merged 1 commit into
mainfrom
refactor/extract-settings-menu
Aug 19, 2026
Merged

refactor: extract settings menu from telegram bot#161
QueryPlanner merged 1 commit into
mainfrom
refactor/extract-settings-menu

Conversation

@QueryPlanner

Copy link
Copy Markdown
Owner

What

Extracts the /model and /thinking inline-keyboard settings UI out of TelegramBot into a standalone SettingsMenu class in a new telegram/settings_menu.py. Second of the planned cleanup series for telegram/bot.py (stacked on #160, the pure-dedup PR — please review/merge that one first).

Why

The original audit flagged bot.py's settings-menu code (~300 lines: model/thinking menu building, callback parsing, capability resolution, reasoning-option logic) as a textbook "unrelated UI logic bolted onto a transport class" violation — none of it touches polling, session routing, or ADK runtime state, it only needs a Telegram API client and a way to load a chat's inference profile.

Discovered while scoping this PR: the existing test suite for this subsystem (tests/test_telegram_bot_model_override.py, 908 lines) pokes TelegramBot's private methods and attributes directly (bot._build_model_menu, bot._resolve_capabilities, bot._capabilities_resolver, patch("blacki.telegram.bot.MODEL_CHOICES", ...), etc.) rather than through a public interface — so a clean extraction meant rewriting most of that test file, not just moving code. Flagged this tradeoff and got explicit sign-off before doing the full rewrite rather than either leaving compatibility-shim methods on TelegramBot or silently taking on a much bigger diff than initially scoped.

This is a relocation for separation of concerns, not a line-count reduction — bot.py shrinks by 391 lines, but the extracted code and its tests now carry their own module/fixture overhead, so total repo lines grow slightly. That's expected; PR #160 was the line-reduction pass, this one is the bloat/separation-of-concerns pass.

How

  • New telegram/settings_menu.py: SettingsMenu class taking an api_provider callable (since TelegramBot.api is a lazy property) and a load_profile callable (since _load_chat_profile is shared with turn-handling and stays on TelegramBot). Owns MODEL_CHOICES, the s:* callback-data constants, reasoning labels, and all the menu-building/callback-handling/capability-resolution methods verbatim from the old TelegramBot.
  • bot.py: TelegramBot.__init__ constructs self._settings_menu = SettingsMenu(...); stop() calls self._settings_menu.aclose() instead of managing _capabilities_resolver directly; _handle_command's /model//thinking branches and _handle_callback_query's non-health branch delegate to self._settings_menu. _load_chat_profile stays on TelegramBot unchanged.
  • Removed ~450 lines of settings-menu code and constants from bot.py (1891 → 1500 lines); no behavior change to any of it, verified by porting every existing test case.
  • Test rewrite: tests/test_telegram_settings_menu.py (new, 856 lines) tests SettingsMenu directly via a mock API provider and profile loader — this ports essentially all prior settings-behavior test coverage (menu building, callback dispatch for model/reasoning/reset/thinking/back, capability resolution and caching, legacy-model migration race conditions, Telegram callback-data byte-limit check). tests/test_telegram_bot_model_override.py is trimmed from 908 to 154 lines, keeping only true TelegramBot-level tests: command dispatch delegates to _settings_menu, callback routing splits health vs. settings correctly, and _load_chat_profile's environment-fallback behavior.

Tests

  • pytest tests/test_telegram_settings_menu.py tests/test_telegram_bot_model_override.py tests/test_telegram_bot.py tests/test_telegram_health.py -q — 300 passed
  • pytest -q (full suite) — 1560 passed, 1 skipped
  • ruff check / ruff format --check on all changed/new files — clean
  • mypy on src/blacki/telegram/ — clean

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

- Move the /model and /thinking inline-keyboard settings UI
  (menu building, callback parsing, capability resolution,
  reasoning options) out of TelegramBot into a standalone
  SettingsMenu class in telegram/settings_menu.py
- TelegramBot now owns a SettingsMenu instance and delegates
  command dispatch and non-health callback routing to it;
  _load_chat_profile stays on TelegramBot since turn handling
  needs it too
- bot.py drops from 1891 to 1500 lines; the extracted ~450
  lines gain their own module instead of sharing an unrelated
  God class
- Rewrite the coupled test suite: settings-menu behavior now
  tests SettingsMenu directly in
  tests/test_telegram_settings_menu.py; the old
  test_telegram_bot_model_override.py is trimmed to the thin
  TelegramBot-level delegation tests
- No behavior change; full test suite (1560 tests) passes
@QueryPlanner
QueryPlanner force-pushed the refactor/extract-settings-menu branch from 02ebfdb to 18dcb67 Compare August 19, 2026 05:32
@QueryPlanner
QueryPlanner merged commit 2086eae into main Aug 19, 2026
3 checks passed
@QueryPlanner
QueryPlanner deleted the refactor/extract-settings-menu branch August 19, 2026 05:41
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