feat: centralized environment handling, ensures ZEN_MCP_FORCE_ENV_OVERRIDE is honored correctly

fix: updated tests to override env variables they need instead of relying on the current values from .env
This commit is contained in:
Fahad
2025-10-04 14:28:56 +04:00
parent 4015e917ed
commit 2c534ac06e
24 changed files with 300 additions and 179 deletions

View File

@@ -7,7 +7,6 @@ It shows which providers are configured and what models can be used.
"""
import logging
import os
from typing import Any, Optional
from mcp.types import TextContent
@@ -15,6 +14,7 @@ from mcp.types import TextContent
from tools.models import ToolModelCategory, ToolOutput
from tools.shared.base_models import ToolRequest
from tools.shared.base_tool import BaseTool
from utils.env import get_env
logger = logging.getLogger(__name__)
@@ -199,7 +199,7 @@ class ListModelsTool(BaseTool):
output_lines.append("")
# Check OpenRouter
openrouter_key = os.getenv("OPENROUTER_API_KEY")
openrouter_key = get_env("OPENROUTER_API_KEY")
is_openrouter_configured = openrouter_key and openrouter_key != "your_openrouter_api_key_here"
output_lines.append(f"## OpenRouter {'' if is_openrouter_configured else ''}")
@@ -307,7 +307,7 @@ class ListModelsTool(BaseTool):
output_lines.append("")
# Check Custom API
custom_url = os.getenv("CUSTOM_API_URL")
custom_url = get_env("CUSTOM_API_URL")
output_lines.append(f"## Custom/Local API {'' if custom_url else ''}")

View File

@@ -27,6 +27,7 @@ from utils.conversation_memory import (
get_conversation_file_list,
get_thread,
)
from utils.env import get_env
from utils.file_utils import read_file_content, read_files
# Import models from tools.models for compatibility
@@ -248,7 +249,7 @@ class BaseTool(ABC):
all_models = ModelProviderRegistry.get_available_model_names()
# Add OpenRouter models if OpenRouter is configured
openrouter_key = os.getenv("OPENROUTER_API_KEY")
openrouter_key = get_env("OPENROUTER_API_KEY")
if openrouter_key and openrouter_key != "your_openrouter_api_key_here":
try:
registry = self._get_openrouter_registry()
@@ -262,7 +263,7 @@ class BaseTool(ABC):
logging.debug(f"Failed to add OpenRouter models to enum: {e}")
# Add custom models if custom API is configured
custom_url = os.getenv("CUSTOM_API_URL")
custom_url = get_env("CUSTOM_API_URL")
if custom_url:
try:
registry = self._get_openrouter_registry()
@@ -432,7 +433,7 @@ class BaseTool(ABC):
notes: list[str] = []
for env_var, label in env_labels.items():
raw = os.getenv(env_var)
raw = get_env(env_var)
if not raw:
continue
@@ -1171,10 +1172,9 @@ When recommending searches, be specific about what information you need and why
no locale set
"""
# Read LOCALE directly from environment to support dynamic changes
# This allows tests to modify os.environ["LOCALE"] and see the changes
import os
# Tests can monkeypatch LOCALE via the environment helper (or .env when override is enforced)
locale = os.getenv("LOCALE", "").strip()
locale = (get_env("LOCALE", "") or "").strip()
if not locale:
return ""
@@ -1277,7 +1277,7 @@ When recommending searches, be specific about what information you need and why
all_models = ModelProviderRegistry.get_available_model_names()
# Add OpenRouter models and their aliases when OpenRouter is configured
openrouter_key = os.getenv("OPENROUTER_API_KEY")
openrouter_key = get_env("OPENROUTER_API_KEY")
if openrouter_key and openrouter_key != "your_openrouter_api_key_here":
try:
registry = self._get_openrouter_registry()
@@ -1296,7 +1296,7 @@ When recommending searches, be specific about what information you need and why
logging.debug(f"Failed to add OpenRouter models to enum: {exc}")
# Add custom models (and their aliases) when a custom endpoint is available
custom_url = os.getenv("CUSTOM_API_URL")
custom_url = get_env("CUSTOM_API_URL")
if custom_url:
try:
registry = self._get_openrouter_registry()