refactor: raise an error instead of exit in util function
This commit is contained in:
parent
84cf5fbfea
commit
87e967a4a2
93
cli/utils.py
93
cli/utils.py
|
|
@ -245,10 +245,7 @@ def _select_custom_provider_model(model_type: str, title: str, default_model: st
|
||||||
).ask()
|
).ask()
|
||||||
|
|
||||||
if choice is None:
|
if choice is None:
|
||||||
from rich.console import Console
|
raise ValueError(f"No {model_type} thinking model selected")
|
||||||
console = Console()
|
|
||||||
console.print(f"\n[red]No {model_type} thinking model selected. Exiting...[/red]")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Handle custom model input
|
# Handle custom model input
|
||||||
if choice == "__CUSTOM_MODEL__":
|
if choice == "__CUSTOM_MODEL__":
|
||||||
|
|
@ -259,10 +256,7 @@ def _select_custom_provider_model(model_type: str, title: str, default_model: st
|
||||||
).ask()
|
).ask()
|
||||||
|
|
||||||
if not custom_model:
|
if not custom_model:
|
||||||
from rich.console import Console
|
raise ValueError(f"No custom {model_type} model name entered")
|
||||||
console = Console()
|
|
||||||
console.print(f"\n[red]No custom {model_type} model name entered. Exiting...[/red]")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
return custom_model
|
return custom_model
|
||||||
|
|
||||||
|
|
@ -274,11 +268,17 @@ def select_shallow_thinking_agent(provider) -> str:
|
||||||
|
|
||||||
# Handle custom provider - use unified model selection
|
# Handle custom provider - use unified model selection
|
||||||
if provider.lower().startswith("custom"):
|
if provider.lower().startswith("custom"):
|
||||||
return _select_custom_provider_model(
|
try:
|
||||||
model_type="shallow",
|
return _select_custom_provider_model(
|
||||||
title="Select Your [Quick-Thinking LLM Engine] (Custom Provider - All Models Available):",
|
model_type="shallow",
|
||||||
default_model="gpt-4o-mini"
|
title="Select Your [Quick-Thinking LLM Engine] (Custom Provider - All Models Available):",
|
||||||
)
|
default_model="gpt-4o-mini"
|
||||||
|
)
|
||||||
|
except ValueError as e:
|
||||||
|
from rich.console import Console
|
||||||
|
console = Console()
|
||||||
|
console.print(f"\n[red]Error: {e}[/red]")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
# Use centralized shallow thinking model definitions
|
# Use centralized shallow thinking model definitions
|
||||||
|
|
||||||
|
|
@ -314,11 +314,17 @@ def select_deep_thinking_agent(provider) -> str:
|
||||||
|
|
||||||
# Handle custom provider - use unified model selection
|
# Handle custom provider - use unified model selection
|
||||||
if provider.lower().startswith("custom"):
|
if provider.lower().startswith("custom"):
|
||||||
return _select_custom_provider_model(
|
try:
|
||||||
model_type="deep",
|
return _select_custom_provider_model(
|
||||||
title="Select Your [Deep-Thinking LLM Engine] (Custom Provider - All Models Available):",
|
model_type="deep",
|
||||||
default_model="o4-mini"
|
title="Select Your [Deep-Thinking LLM Engine] (Custom Provider - All Models Available):",
|
||||||
)
|
default_model="o4-mini"
|
||||||
|
)
|
||||||
|
except ValueError as e:
|
||||||
|
from rich.console import Console
|
||||||
|
console = Console()
|
||||||
|
console.print(f"\n[red]Error: {e}[/red]")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
# Use centralized deep thinking model definitions
|
# Use centralized deep thinking model definitions
|
||||||
|
|
||||||
|
|
@ -347,16 +353,23 @@ def select_deep_thinking_agent(provider) -> str:
|
||||||
return choice
|
return choice
|
||||||
|
|
||||||
def validate_custom_url(url: str) -> str:
|
def validate_custom_url(url: str) -> str:
|
||||||
"""Validate that a custom URL is properly formatted and has a valid hostname."""
|
"""Validate that a custom URL is properly formatted and has a valid hostname.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
url: The URL to validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The validated URL
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If the URL is invalid or malformed
|
||||||
|
"""
|
||||||
import re
|
import re
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from rich.console import Console
|
|
||||||
|
|
||||||
if not url:
|
if not url:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
console = Console()
|
|
||||||
|
|
||||||
# Basic URL format validation
|
# Basic URL format validation
|
||||||
url_pattern = re.compile(
|
url_pattern = re.compile(
|
||||||
r'^https?://' # http:// or https://
|
r'^https?://' # http:// or https://
|
||||||
|
|
@ -367,35 +380,47 @@ def validate_custom_url(url: str) -> str:
|
||||||
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
||||||
|
|
||||||
if not url_pattern.match(url):
|
if not url_pattern.match(url):
|
||||||
console.print(f"[red]Error: Invalid CUSTOM_BASE_URL format: {url}[/red]")
|
raise ValueError(f"Invalid CUSTOM_BASE_URL format: {url}. Please provide a valid URL (e.g., https://api.example.com/v1)")
|
||||||
console.print(f"[red]Please provide a valid URL (e.g., https://api.example.com/v1)[/red]")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Additional validation using urlparse
|
# Additional validation using urlparse
|
||||||
try:
|
try:
|
||||||
parsed = urlparse(url)
|
parsed = urlparse(url)
|
||||||
if not parsed.netloc:
|
if not parsed.netloc:
|
||||||
raise ValueError("No hostname found")
|
raise ValueError(f"Invalid CUSTOM_BASE_URL: {url}. No hostname found")
|
||||||
return url
|
return url
|
||||||
|
except ValueError:
|
||||||
|
# Re-raise ValueError as-is
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
console.print(f"[red]Error: Invalid CUSTOM_BASE_URL: {url}[/red]")
|
raise ValueError(f"Invalid CUSTOM_BASE_URL: {url}. URL parsing error: {e}")
|
||||||
console.print(f"[red]URL parsing error: {e}[/red]")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def get_custom_provider_info() -> tuple[str, str] | None:
|
def get_custom_provider_info() -> tuple[str, str] | None:
|
||||||
"""Get custom provider info if both URL and API key are provided."""
|
"""Get custom provider info if both URL and API key are provided.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple[str, str] | None: (display_name, url) if valid custom provider configured, None otherwise
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
SystemExit: If custom URL is provided but invalid (exits with error message)
|
||||||
|
"""
|
||||||
import os
|
import os
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
from rich.console import Console
|
||||||
|
|
||||||
custom_url = os.getenv("CUSTOM_BASE_URL")
|
custom_url = os.getenv("CUSTOM_BASE_URL")
|
||||||
custom_api_key = os.getenv("CUSTOM_API_KEY")
|
custom_api_key = os.getenv("CUSTOM_API_KEY")
|
||||||
|
|
||||||
if custom_url and custom_api_key:
|
if custom_url and custom_api_key:
|
||||||
validated_url = validate_custom_url(custom_url)
|
try:
|
||||||
parsed = urlparse(validated_url)
|
validated_url = validate_custom_url(custom_url)
|
||||||
hostname = parsed.netloc
|
parsed = urlparse(validated_url)
|
||||||
return f"Custom ({hostname})", validated_url
|
hostname = parsed.netloc
|
||||||
|
return f"Custom ({hostname})", validated_url
|
||||||
|
except ValueError as e:
|
||||||
|
console = Console()
|
||||||
|
console.print(f"[red]Error: {e}[/red]")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue