Skip to content

refactor(model): extract shared switch_model() from CLI and gateway handlers#2799

Merged
teknium1 merged 1 commit intomainfrom
hermes/hermes-3c0e357d
Mar 24, 2026
Merged

refactor(model): extract shared switch_model() from CLI and gateway handlers#2799
teknium1 merged 1 commit intomainfrom
hermes/hermes-3c0e357d

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

Summary

Phase 4 of the /model command overhaul — the refactor that unifies CLI and gateway model-switching logic.

What changed

Both handlers had ~50 lines of duplicated core logic (parsing, provider detection, credential resolution, model validation). Extracted into hermes_cli/model_switch.py:

Export Purpose
ModelSwitchResult Dataclass with all fields both handlers need
CustomAutoResult Dataclass for bare /model custom results
switch_model() Core pipeline: parse → detect → resolve → validate
switch_to_custom_provider() Resolve endpoint + auto-detect model

The shared functions are pure (no I/O side effects). Each caller handles its own platform-specific concerns:

  • CLI: sets self.model/provider/etc, calls save_config_value(), prints with ASCII emoticons
  • Gateway: writes config.yaml directly, sets env vars, returns markdown

Impact

cli.py                     | 176 +++-------  (~80 lines of model handler, down from ~150)
gateway/run.py             | 179 +++-------  (~80 lines of model handler, down from ~150)
hermes_cli/model_switch.py | 234 ++++++++++  (new shared module)

Net: -244 lines from handlers, +234 in shared module. The handlers can't drift apart on core logic anymore.

Test plan

Full suite: 6090 passed, 0 failures. All existing /model tests pass unchanged — the refactor preserves exact behavior.

…andlers

Phase 4 of the /model command overhaul.

Both the CLI (cli.py) and gateway (gateway/run.py) /model handlers
had ~50 lines of duplicated core logic: parsing, provider detection,
credential resolution, and model validation. This extracts that
pipeline into hermes_cli/model_switch.py.

New module exports:
- ModelSwitchResult: dataclass with all fields both handlers need
- CustomAutoResult: dataclass for bare '/model custom' results
- switch_model(): core pipeline — parse → detect → resolve → validate
- switch_to_custom_provider(): resolve endpoint + auto-detect model

The shared functions are pure (no I/O side effects). Each caller
handles its own platform-specific concerns:
- CLI: sets self.model/provider/etc, calls save_config_value(), prints
- Gateway: writes config.yaml directly, sets env vars, returns markdown

Net result: -244 lines from handlers, +234 lines in shared module.
The handlers are now ~80 lines each (down from ~150+) and can't drift
apart on core logic.
@teknium1 teknium1 merged commit 2e52427 into main Mar 24, 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

1 participant