Buckets:

download
raw
10 kB
# backend/config.py
# Standard Library Imports
import os
import shutil
import secrets
import re
from pathlib import Path
import toml
from dotenv import load_dotenv
from multiprocessing import cpu_count
from ascii_colors import ASCIIColors
# --- Helper Function for Type Casting ---
def get_env_var(key, default, cast_type=str):
"""Gets an environment variable and casts it to the desired type."""
value = os.environ.get(key, default)
try:
if cast_type == bool:
if isinstance(value, bool):
return value
return str(value).lower() in ('true', '1', 'yes', 'on')
if cast_type == int and value is not None:
return int(value)
return cast_type(value) if value is not None else default
except (ValueError, TypeError):
return default
# --- Project Root ---
PROJECT_ROOT = Path(__file__).resolve().parent.parent
env_path = PROJECT_ROOT / ".env"
# --- One-Time Migration from config.toml to .env ---
def _migrate_toml_to_env_if_needed():
"""
Checks for a config.toml file and migrates its settings to a new .env file
if .env doesn't already exist.
"""
toml_path = PROJECT_ROOT / "config.toml"
if not env_path.exists() and toml_path.exists():
print("INFO: Found config.toml. Migrating settings to .env file...")
try:
config = toml.load(toml_path)
env_lines = [
"# This file was automatically migrated from config.toml",
"# Please review and update these settings as needed."
]
# Helper to process a section
def process_section(section_name, prefix, defaults={}):
if section_name in config:
env_lines.append(f"\n# {section_name.replace('_', ' ').title()} Settings")
for key, value in config[section_name].items():
env_key = f"{prefix}_{key.upper()}"
env_lines.append(f"{env_key}={value}")
process_section("server", "SERVER")
process_section("app_settings", "APP")
process_section("initial_admin_user", "INITIAL_ADMIN")
process_section("safe_store_defaults", "SAFESTORE")
process_section("redis", "REDIS")
with open(env_path, "w", encoding="utf-8") as f:
f.write("\n".join(env_lines))
# Rename the old config file to prevent re-migration
toml_path.rename(toml_path.with_suffix('.toml.migrated'))
print("INFO: Migration successful. 'config.toml' has been renamed to 'config.toml.migrated'.")
print("INFO: The application will now use the new '.env' file.")
except Exception as e:
print(f"CRITICAL: Failed to migrate config.toml to .env file. Error: {e}")
# --- Security: Auto-rotate weak keys ---
def _ensure_secure_secret_key():
"""
SECURITY FIX:
Checks if the current SECRET_KEY is weak/default or missing.
If vulnerable, generates a new CSPRNG key, updates the runtime, and persists it to .env.
"""
weak_keys = [
"changeme",
"a_very_secret_key_that_should_be_changed_for_production",
"a_very_secret_key_for_flask_sessions_or_jwt",
"your_secret_key_here",
""
]
current_key = os.environ.get("SECRET_KEY", "")
if not current_key or current_key in weak_keys:
ASCIIColors.warning("[Security] SECRET_KEY is missing or weak. Generating a persistent secure key...")
new_key = secrets.token_urlsafe(32)
os.environ["SECRET_KEY"] = new_key
try:
# Force absolute path for .env to ensure we write to the correct location
abs_env_path = env_path.absolute()
content = ""
if abs_env_path.exists():
with open(abs_env_path, "r", encoding="utf-8") as f:
content = f.read()
pattern = re.compile(r'^SECRET_KEY\s*=.*$', re.MULTILINE)
if pattern.search(content):
content = pattern.sub(f'SECRET_KEY="{new_key}"', content)
else:
if content and not content.endswith('\n'):
content += '\n'
content += f'SECRET_KEY="{new_key}"\n'
with open(abs_env_path, "w", encoding="utf-8") as f:
f.write(content)
ASCIIColors.success(f"[Security] New key persisted to: {abs_env_path}")
except Exception as e:
ASCIIColors.error(f"[Security] Persistence failure: {e}. Sessions will reset on next restart.")
# --- Load Environment Variables ---
_migrate_toml_to_env_if_needed()
# Ensure path is clean before loading
load_dotenv(dotenv_path=env_path.absolute(), override=True)
# Ensure secure key exists after loading env
_ensure_secure_secret_key()
# Re-read to ensure the rotated key is the one we use for the rest of the config
SECRET_KEY = os.environ.get("SECRET_KEY", "")
if SECRET_KEY:
import hashlib
fingerprint = hashlib.sha256(SECRET_KEY.encode()).hexdigest()[:8]
ASCIIColors.cyan(f"Auth System: Key Fingerprint [{fingerprint}]")
# --- Application Version ---
APP_VERSION = "2.1.0"
TAG_NAME = "Restart"
LOCALS_DIR = PROJECT_ROOT / "frontend" / "webui" / "public" / "locals"
# --- Configuration Loading from Environment ---
# Server settings
workers_count = get_env_var("SERVER_WORKERS", cpu_count(), int)
# Reverting the Windows worker cap to allow multiple workers, relying on the 'spawn' fix in main.py
if os.name == 'nt' and workers_count > 1:
ASCIIColors.panel("⚠️ [bold orange]WARNING:[/bold orange] Capping [bold]SERVER_WORKERS[/bold] to 1 on Windows to prevent WinError 10022 (Invalid argument).")
workers_count = 1
SERVER_CONFIG = {
"host": get_env_var("SERVER_HOST", "0.0.0.0"),
"port": get_env_var("SERVER_PORT", 9642, int),
"workers": workers_count,
"debug": get_env_var("SERVER_DEBUG", False, bool),
"https_enabled": get_env_var("HTTPS_ENABLED", False, bool),
"ssl_certfile": get_env_var("SSL_CERTFILE", ""),
"ssl_keyfile": get_env_var("SSL_KEYFILE", ""),
"com_hub_port": get_env_var("COM_HUB_PORT", 8042, int),
}
# App settings
APP_DATA_DIR = PROJECT_ROOT / get_env_var("APP_DATA_DIR", "data")
APP_DATA_DIR.mkdir(exist_ok=True, parents=True)
# Hugging Face Cache Setup
# Force disable symlinks on Windows to prevent permission errors when not running as admin.
if os.name == 'nt':
os.environ['HF_HUB_ENABLE_SYMLINKS_EXPERIMENTAL'] = '0'
HUGGINGFACE_CACHE_DIR = APP_DATA_DIR / get_env_var("HUGGINGFACE_CACHE_DIR", "huggingface_cache")
HUGGINGFACE_CACHE_DIR.mkdir(exist_ok=True, parents=True)
os.environ['HUGGING_FACE_HUB_CACHE'] = str(HUGGINGFACE_CACHE_DIR)
os.environ['HF_HOME'] = str(HUGGINGFACE_CACHE_DIR)
DATABASE_URL_CONFIG_KEY = "database_url"
APP_DB_URL = get_env_var("DATABASE_URL", f"sqlite:///{APP_DATA_DIR / 'app_main.db'}")
# Secret Key is now guaranteed to be secure by _ensure_secure_secret_key
SECRET_KEY = get_env_var("SECRET_KEY", "")
if not SECRET_KEY:
ASCIIColors.error("CRITICAL: SECRET_KEY is empty after initialization!")
APP_SETTINGS = {
"data_dir": str(APP_DATA_DIR),
"database_url": APP_DB_URL,
"secret_key": SECRET_KEY,
"access_token_expires_minutes": get_env_var("ACCESS_TOKEN_EXPIRES_MINUTES", 44640, int),
"allow_new_registrations": get_env_var("ALLOW_NEW_REGISTRATIONS", True, bool),
"registration_mode": get_env_var("REGISTRATION_MODE", "admin_approval"),
"public_domain_name": get_env_var("PUBLIC_DOMAIN_NAME", "")
}
# Default initial user, binding, and SafeStore settings
INITIAL_ADMIN_USER_CONFIG = {
"username": get_env_var("INITIAL_ADMIN_USERNAME", "admin"),
"password": get_env_var("INITIAL_ADMIN_PASSWORD", "admin")
}
SAFE_STORE_DEFAULTS = {
"cache_folder": get_env_var("SAFESTORE_CACHE_FOLDER", "data/cache/safestore"),
"chunk_size": get_env_var("SAFESTORE_CHUNK_SIZE", 2048, int),
"chunk_overlap": get_env_var("SAFESTORE_CHUNK_OVERLAP", 150, int),
"global_default_vectorizer": get_env_var("SAFESTORE_GLOBAL_DEFAULT_VECTORIZER", "st:all-MiniLM-L6-v2"),
"encryption_key": get_env_var("SAFESTORE_ENCRYPTION_KEY", "my_super_secret_safe_store_key_CHANGE_ME")
}
# --- Constants for directory names ---
TEMP_UPLOADS_DIR_NAME = "temp_uploads"
DISCUSSION_ASSETS_DIR_NAME = "discussion_assets"
DM_ASSETS_DIR_NAME = "dm_assets"
DATASTORES_DIR_NAME = "safestores"
VOICES_DIR_NAME = "voices"
IMAGES_DIR_NAME = "images"
APPS_DIR_NAME = "apps"
MCPS_DIR_NAME = "mcps"
CUSTOM_APPS_DIR_NAME = "custom_apps"
ZOO_DIR_NAME = "zoo"
MCP_ZOO_DIR_NAME = "mcps_zoo"
APPS_ZOO_DIR_NAME = "apps_zoo"
PROMPTS_ZOO_DIR_NAME = "prompts_zoo"
PERSONALITIES_ZOO_DIR_NAME = "personalities_zoo"
SKILLS_ZOO_DIR_NAME = "skills_zoo"
# --- Full Path Constants ---
APPS_ZOO_ROOT_PATH = APP_DATA_DIR / APPS_ZOO_DIR_NAME
MCPS_ZOO_ROOT_PATH = APP_DATA_DIR / MCP_ZOO_DIR_NAME
PROMPTS_ZOO_ROOT_PATH = APP_DATA_DIR / PROMPTS_ZOO_DIR_NAME
PERSONALITIES_ZOO_ROOT_PATH = APP_DATA_DIR / PERSONALITIES_ZOO_DIR_NAME
SKILLS_ZOO_ROOT_PATH = APP_DATA_DIR / SKILLS_ZOO_DIR_NAME
APPS_ROOT_PATH = APP_DATA_DIR / APPS_DIR_NAME
MCPS_ROOT_PATH = APP_DATA_DIR / MCPS_DIR_NAME
CUSTOM_APPS_ROOT_PATH = APP_DATA_DIR / CUSTOM_APPS_DIR_NAME
APPS_ZOO_ROOT_PATH.mkdir(exist_ok=True, parents=True)
MCPS_ZOO_ROOT_PATH.mkdir(exist_ok=True, parents=True)
APPS_ROOT_PATH.mkdir(exist_ok=True, parents=True)
PROMPTS_ZOO_ROOT_PATH.mkdir(exist_ok=True, parents=True)
PERSONALITIES_ZOO_ROOT_PATH.mkdir(exist_ok=True, parents=True)
SKILLS_ZOO_ROOT_PATH.mkdir(exist_ok=True, parents=True)
# --- Security Constants (Not moved to DB) ---
ALGORITHM = "HS256"
# SECRET_KEY is already defined above
USERS_DIR_NAME = "users"
TEMP_UPLOADS_DIR_NAME = "temp_uploads"
DISCUSSION_ASSETS_DIR_NAME = "discussion_assets"
DM_ASSETS_DIR_NAME = "dm_assets"
SOCIAL_ASSETS_DIR_NAME = "social_assets"
NOTEBOOK_ASSETS_DIR_NAME = "notebook_assets"

Xet Storage Details

Size:
10 kB
·
Xet hash:
ad43f9b1d06bb32def05bd70347ec799b419947273ec6f8a81bb7c740b588988

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.