suna/backend/utils/config.py

168 lines
5.5 KiB
Python
Raw Normal View History

2025-04-24 08:45:58 +08:00
"""
2025-04-24 11:34:45 +08:00
Configuration management.
2025-04-24 08:45:58 +08:00
This module provides a centralized way to access configuration settings and
environment variables across the application. It supports different environment
modes (development, staging, production) and provides validation for required
values.
Usage:
from utils.config import config
# Access configuration values
api_key = config.OPENAI_API_KEY
env_mode = config.ENV_MODE
"""
import os
from enum import Enum
from typing import Dict, Any, Optional, get_type_hints
from dotenv import load_dotenv
import logging
logger = logging.getLogger(__name__)
class EnvMode(Enum):
"""Environment mode enumeration."""
LOCAL = "local"
STAGING = "staging"
PRODUCTION = "production"
class Configuration:
"""
Centralized configuration for AgentPress backend.
This class loads environment variables and provides type checking and validation.
Default values can be specified for optional configuration items.
"""
# Environment mode
ENV_MODE: EnvMode = EnvMode.LOCAL
# LLM API keys
OPENAI_API_KEY: Optional[str] = None
ANTHROPIC_API_KEY: Optional[str] = None
GROQ_API_KEY: Optional[str] = None
OPENROUTER_API_KEY: Optional[str] = None
OPENROUTER_API_BASE: str = "https://openrouter.ai/api/v1"
# AWS Bedrock credentials
AWS_ACCESS_KEY_ID: Optional[str] = None
AWS_SECRET_ACCESS_KEY: Optional[str] = None
AWS_REGION_NAME: Optional[str] = None
# Model configuration
2025-04-24 11:34:45 +08:00
MODEL_TO_USE: str = "anthropic/claude-3-7-sonnet-latest"
2025-04-24 08:45:58 +08:00
# Supabase configuration
SUPABASE_URL: Optional[str] = None
SUPABASE_ANON_KEY: Optional[str] = None
SUPABASE_SERVICE_ROLE_KEY: Optional[str] = None
# Redis configuration
REDIS_HOST: Optional[str] = None
REDIS_PORT: int = 6379
REDIS_PASSWORD: Optional[str] = None
REDIS_SSL: bool = True
# Daytona sandbox configuration
DAYTONA_API_KEY: Optional[str] = None
DAYTONA_SERVER_URL: Optional[str] = None
DAYTONA_TARGET: Optional[str] = None
# Search and other API keys
TAVILY_API_KEY: Optional[str] = None
RAPID_API_KEY: Optional[str] = None
CLOUDFLARE_API_TOKEN: Optional[str] = None
# Stripe configuration
STRIPE_SECRET_KEY: Optional[str] = None
STRIPE_DEFAULT_PLAN_ID: Optional[str] = None
STRIPE_DEFAULT_TRIAL_DAYS: int = 14
# Open Router configuration
OR_SITE_URL: Optional[str] = None
2025-04-24 11:34:45 +08:00
OR_APP_NAME: Optional[str] = "Suna.so"
2025-04-24 08:45:58 +08:00
def __init__(self):
"""Initialize configuration by loading from environment variables."""
# Load environment variables from .env file if it exists
load_dotenv()
# Set environment mode first
env_mode_str = os.getenv("ENV_MODE", EnvMode.LOCAL.value)
try:
self.ENV_MODE = EnvMode(env_mode_str.lower())
except ValueError:
logger.warning(f"Invalid ENV_MODE: {env_mode_str}, defaulting to LOCAL")
self.ENV_MODE = EnvMode.LOCAL
logger.info(f"Environment mode: {self.ENV_MODE.value}")
# Load configuration from environment variables
self._load_from_env()
# Perform validation
self._validate()
def _load_from_env(self):
"""Load configuration values from environment variables."""
for key, expected_type in get_type_hints(self.__class__).items():
env_val = os.getenv(key)
if env_val is not None:
# Convert environment variable to the expected type
if expected_type == bool:
# Handle boolean conversion
setattr(self, key, env_val.lower() in ('true', 't', 'yes', 'y', '1'))
elif expected_type == int:
# Handle integer conversion
try:
setattr(self, key, int(env_val))
except ValueError:
logger.warning(f"Invalid value for {key}: {env_val}, using default")
elif expected_type == EnvMode:
# Already handled for ENV_MODE
pass
else:
# String or other type
setattr(self, key, env_val)
def _validate(self):
"""Validate configuration based on environment mode."""
# Keys required in all environments
required_keys = []
# Add keys required in non-local environments
if self.ENV_MODE != EnvMode.LOCAL:
required_keys.extend([
"SUPABASE_URL",
"SUPABASE_SERVICE_ROLE_KEY"
])
# Additional keys required in production
if self.ENV_MODE == EnvMode.PRODUCTION:
required_keys.extend([
"REDIS_HOST",
"REDIS_PASSWORD"
])
# Validate required keys
for key in required_keys:
if not getattr(self, key):
logger.warning(f"Required configuration {key} is missing for {self.ENV_MODE.value} environment")
def get(self, key: str, default: Any = None) -> Any:
"""Get a configuration value with an optional default."""
return getattr(self, key, default)
def as_dict(self) -> Dict[str, Any]:
"""Return configuration as a dictionary."""
return {
key: getattr(self, key)
for key in get_type_hints(self.__class__).keys()
if not key.startswith('_')
}
# Create a singleton instance
config = Configuration()