suna/backend/utils/config.py

164 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
2025-04-26 06:13:47 +08:00
from typing import Dict, Any, Optional, get_type_hints, Union
2025-04-24 08:45:58 +08:00
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
2025-04-26 06:13:47 +08:00
ANTHROPIC_API_KEY: str = None
2025-04-24 08:45:58 +08:00
OPENAI_API_KEY: Optional[str] = None
GROQ_API_KEY: Optional[str] = None
OPENROUTER_API_KEY: Optional[str] = None
2025-04-26 06:13:47 +08:00
OPENROUTER_API_BASE: Optional[str] = "https://openrouter.ai/api/v1"
OR_SITE_URL: Optional[str] = None
OR_APP_NAME: Optional[str] = "Suna.so"
2025-04-24 08:45:58 +08:00
# 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-26 06:13:47 +08:00
MODEL_TO_USE: Optional[str] = "anthropic/claude-3-7-sonnet-latest"
2025-04-24 08:45:58 +08:00
# Supabase configuration
2025-04-26 06:13:47 +08:00
SUPABASE_URL: str
SUPABASE_ANON_KEY: str
SUPABASE_SERVICE_ROLE_KEY: str
2025-04-24 08:45:58 +08:00
# Redis configuration
2025-04-26 06:13:47 +08:00
REDIS_HOST: str
2025-04-24 08:45:58 +08:00
REDIS_PORT: int = 6379
2025-04-26 06:13:47 +08:00
REDIS_PASSWORD: str
2025-04-24 08:45:58 +08:00
REDIS_SSL: bool = True
# Daytona sandbox configuration
2025-04-26 06:13:47 +08:00
DAYTONA_API_KEY: str
DAYTONA_SERVER_URL: str
DAYTONA_TARGET: str
2025-04-24 08:45:58 +08:00
# Search and other API keys
2025-04-26 06:13:47 +08:00
TAVILY_API_KEY: str
RAPID_API_KEY: str
2025-04-24 08:45:58 +08:00
CLOUDFLARE_API_TOKEN: Optional[str] = None
2025-04-26 06:13:47 +08:00
FIRECRAWL_API_KEY: str
2025-04-24 08:45:58 +08:00
# Stripe configuration
STRIPE_SECRET_KEY: Optional[str] = None
STRIPE_DEFAULT_PLAN_ID: Optional[str] = None
STRIPE_DEFAULT_TRIAL_DAYS: int = 14
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):
2025-04-26 06:13:47 +08:00
"""Validate configuration based on type hints."""
# Get all configuration fields and their type hints
type_hints = get_type_hints(self.__class__)
2025-04-24 08:45:58 +08:00
2025-04-26 06:13:47 +08:00
# Find missing required fields
missing_fields = []
for field, field_type in type_hints.items():
# Check if the field is Optional
is_optional = hasattr(field_type, "__origin__") and field_type.__origin__ is Union and type(None) in field_type.__args__
2025-04-24 08:45:58 +08:00
2025-04-26 06:13:47 +08:00
# If not optional and value is None, add to missing fields
if not is_optional and getattr(self, field) is None:
missing_fields.append(field)
2025-04-24 08:45:58 +08:00
2025-04-26 06:13:47 +08:00
if missing_fields:
error_msg = f"Missing required configuration fields: {', '.join(missing_fields)}"
logger.error(error_msg)
raise ValueError(error_msg)
2025-04-24 08:45:58 +08:00
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()