import asyncio import os from dotenv import load_dotenv from app.models import init_db, set_rule, add_api_key, add_port import aiosqlite load_dotenv() DB_PATH = "data/context_hub.db" async def scope_exists(scope: str) -> bool: async with aiosqlite.connect(DB_PATH) as db: async with db.execute("SELECT 1 FROM rules WHERE scope=?", (scope,)) as cur: return await cur.fetchone() is not None async def seed_rule_if_absent(scope: str, content: dict): """Insere uniquement si le scope n existe pas — preserve les PATCH agents.""" async with aiosqlite.connect(DB_PATH) as db: await db.execute( "INSERT INTO rules (scope, content) VALUES (?, ?) ON CONFLICT(scope) DO NOTHING", (scope, __import__('json').dumps(content)) ) await db.commit() async def seed_data(): await init_db() print("Database initialized.") infra_rule = { "ssh": {"host": "192.168.100.33", "port": 22222, "user": "Best0f"}, "docker": {"uid": 1026, "gid": 100}, "network": {"from_container": "172.17.0.1", "from_lan": "192.168.100.33"}, "identities": {"bolbol": "Gitea", "Best0f": "SSH", "bestof": "Portainer"}, "git_push_pattern": "http://bolbol:PWD@172.17.0.1:3232/bolbol/REPO.git", "mcp_nas": "heredoc->timeout, heredoc->printf/python, limit=350chars, session-poison-apres-3-erreurs" } llm_rule = { "bifrost_internal": "http://bifrost:8080/v1", "bifrost_lan": "http://192.168.100.33:3085/v1", "bifrost_auth": "header x-bf-vk (JAMAIS Authorization Bearer)", "bifrost_proxy_max_tokens": 16384, "default_model": "opencode/deepseek-v4-flash", "provider": "opencode-go", "budget_usd_month": 10, "vision_model": "openrouter/google/gemini-2.5-flash", "deepseek_limitation": "text-only, 404 sur images", "openrouter": "urgence uniquement" } nyora_rule = { "apps": "family-help:3041, nyora-veille:3055, redaction-pro:3092", "dr_nexum": {"deadline": "2026-11-15", "target_subs": 1000, "target_revenue_eur": 300}, "footer": {"color": "#d4a01a", "line1": "gradient-or", "line2": "NYORA", "line3": "Crafted with precision", "line4": "2026"}, "rules": [ "Ne jamais ecrire Tunisie Telecom ou Zone Sud dans apps Nyora", "Login forms : placeholder=Identifiant uniquement", "Footer Nyora obligatoire sur toutes les apps personnelles" ] } perso_rule = { "family": { "nedya": "nee 1983, coeliaque", "yesmine": "nee 2008, fibromyalgie et nevralgie, priorite accompagnement", "ahmed": "ne 2010", "mondher": "pharmacien, reponses niveau clinique" }, "yasmi": "marque artisanale mode, jasmine, Sfax" } tt_rule = { "baserow_token": "deT2PW3ZFZ0h3euxhKDFnlZhKNrcckYV", "zone": "Gabes, Gafsa, Kebili, Medenine, Sfax, Tataouine, Tozeur", "ci_cpt_zone_sud": 998, "fb_zone_sud": 1006, "rla_contracts": {"table": 856, "count": 55}, "rules": [ "Ne jamais mentionner Baserow/Python/IA dans documents officiels", "Documents = travail Direction Zone Sud Tunisie Telecom" ] } # ON CONFLICT DO NOTHING — les PATCH des agents survivent aux restarts for scope, rule in [("infra", infra_rule), ("llm", llm_rule), ("nyora", nyora_rule), ("perso", perso_rule), ("tt", tt_rule)]: await seed_rule_if_absent(scope, rule) print(f"Rule '{scope}': seeded (or already present)") # Ports : upsert OK (reference statique) ports = [ (3093, "context-hub", "Source de verite unique"), (8787, "nyora-notes", "Ne jamais reutiliser"), (3041, "family-help", "App Nyora"), (3055, "nyora-veille", "App Nyora"), (3092, "redaction-pro", "App Nyora"), ] for port, name, desc in ports: await add_port(port, name, desc) print("Ports seeded.") # API Keys : upsert OK (sync avec .env — volontaire) keys = { "CLAUDE": os.environ.get("API_KEY_CLAUDE"), "HERMES_TT": os.environ.get("API_KEY_HERMES_TT"), "HERMES_NYORA": os.environ.get("API_KEY_HERMES_NYORA"), "HERMES_PERSO": os.environ.get("API_KEY_HERMES_PERSO"), "GEMINI": os.environ.get("API_KEY_GEMINI") } for agent, key in keys.items(): if key: await add_api_key(agent, key) print(f"API key synced: {agent}") else: print(f"Warning: API_KEY_{agent} absent du .env") print("Seed complete.") if __name__ == "__main__": asyncio.run(seed_data())