refactor(model): extract shared switch_model() from CLI and gateway handlers#2799
Merged
refactor(model): extract shared switch_model() from CLI and gateway handlers#2799
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 4 of the
/modelcommand 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:ModelSwitchResultCustomAutoResult/model customresultsswitch_model()switch_to_custom_provider()The shared functions are pure (no I/O side effects). Each caller handles its own platform-specific concerns:
self.model/provider/etc, callssave_config_value(), prints with ASCII emoticonsImpact
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
/modeltests pass unchanged — the refactor preserves exact behavior.