fix(cli): use typer.Exit for model selection

This commit is contained in:
CadeYu 2026-03-21 14:03:11 +08:00
parent 2d41e39f89
commit 340731c3d7
2 changed files with 11 additions and 8 deletions

View File

@ -1,6 +1,7 @@
from typing import Callable, List, Optional, Tuple, Dict
import questionary
import typer
from rich.console import Console
@ -95,14 +96,14 @@ def resolve_model_choice(
"""Resolve built-in and custom model selections into a concrete model id."""
if choice is None:
console.print(f"\n[red]No {model_role.lower()} llm engine selected. Exiting...[/red]")
exit(1)
raise typer.Exit(1)
if provider.lower() == "openrouter" and choice == CUSTOM_OPENROUTER_MODEL:
prompt_fn = prompt_fn or prompt_custom_openrouter_model
custom_model = prompt_fn(model_role)
if not custom_model or not custom_model.strip():
console.print("\n[red]No OpenRouter model ID provided. Exiting...[/red]")
exit(1)
raise typer.Exit(1)
return custom_model.strip()
return choice

View File

@ -1,5 +1,7 @@
import unittest
import typer
from cli.utils import CUSTOM_OPENROUTER_MODEL, resolve_model_choice
@ -20,29 +22,29 @@ class OpenRouterModelSelectionTests(unittest.TestCase):
self.assertEqual(chosen, "minimax/minimax-m2.1")
def test_exit_on_no_choice(self):
with self.assertRaises(SystemExit) as cm:
with self.assertRaises(typer.Exit) as cm:
resolve_model_choice("openrouter", None, "Quick-Thinking")
self.assertEqual(cm.exception.code, 1)
self.assertEqual(cm.exception.exit_code, 1)
def test_exit_on_empty_custom_model_input(self):
with self.assertRaises(SystemExit) as cm:
with self.assertRaises(typer.Exit) as cm:
resolve_model_choice(
"openrouter",
CUSTOM_OPENROUTER_MODEL,
"Deep-Thinking",
prompt_fn=lambda _: " ",
)
self.assertEqual(cm.exception.code, 1)
self.assertEqual(cm.exception.exit_code, 1)
def test_exit_on_none_custom_model_input(self):
with self.assertRaises(SystemExit) as cm:
with self.assertRaises(typer.Exit) as cm:
resolve_model_choice(
"openrouter",
CUSTOM_OPENROUTER_MODEL,
"Deep-Thinking",
prompt_fn=lambda _: None,
)
self.assertEqual(cm.exception.code, 1)
self.assertEqual(cm.exception.exit_code, 1)
if __name__ == "__main__":