import os import logging import certifi from motor.motor_asyncio import AsyncIOMotorClient # The URI is a secret and must come from the environment only: # - PC/exe: MONGO_URI in the loaded .env # - HF Space: MONGO_URI Space Secret # It is deliberately NOT hardcoded here — an embedded connection string is a # second copy of a live credential that silently drifts out of sync the moment # the real secret is rotated (exactly the duplication the vault is meant to # prevent). MONGO_URI = os.getenv("MONGO_URI", "").strip() class MongoDBClient: _client = None @classmethod def get_client(cls) -> AsyncIOMotorClient: if cls._client is None: if not MONGO_URI: raise RuntimeError( "MONGO_URI is not configured. Set it in the local .env " "(PC/exe) or as an HF Space Secret (cloud) before using " "Mongo-backed memory." ) # We use certifi to prevent SSL handshake errors when running inside Docker cls._client = AsyncIOMotorClient(MONGO_URI, tlsCAFile=certifi.where()) return cls._client @classmethod def get_db(cls): return cls.get_client().jarvis_brain async def log_model_generation_mongo(description: str, image_tier: str, model_path: str, persona: str, inference_target: str): import datetime db = MongoDBClient.get_db() await db.model_generations.insert_one({ "description": description, "image_source_tier": image_tier, "model_path": model_path, "persona": persona, "engine": "stable_fast_3d_local", "inference_target": inference_target, "timestamp": datetime.datetime.utcnow() })