agent_tracker/database.py
nickviljoen 54ecd31bdd Add registration form redesign with completion flow for LibreChat-synced agents
Rebuilds the agent registration form into 7 governance sections (Identity,
Classification, Autonomy, IP, Tech Stack, Data Safety, Performance, Declarations)
and introduces a completion flow for agents that come in via the LibreChat
collector without the new required fields.

- New form fields: business_entity, client_scope, agent_classification,
  autonomy_level, ip_ownership, foundation_model, validated_by/date, evals_method,
  plus nested safety / pii / declarations objects and a registration_complete flag.
- registration_complete defaults to true for form-submitted agents and false for
  collector-created ones; existing agents are grandfathered via a startup migration.
- Owner-by-email lookup so LibreChat-synced agents surface in the user's "My Agents"
  view with an Incomplete badge and Complete CTA. Submitting the completion form
  reassigns created_by from the collector marker to the user.
- Daily APScheduler job sends a digest reminder email per owner with a 7-day
  cooldown and 4-nudge cap (configurable). Manual trigger via
  POST /api/admin/completion-reminders/send.
- Admin banner + modal for collector agents whose contact email doesn't match an
  active user, with one-click reassignment.
- Gemini audit extended to also return agent_classification and an autonomy hint;
  applied to agents on next batch run alongside discipline/department.
- New filter dimensions on agent management + admin dashboard: Business Entity,
  Agent Type, Autonomy, plus a Compliance Risks quick toggle.
- CSV export/import gains 21 columns covering all governance fields.
- Discipline 'Optimization' renamed to 'Optimisation' with idempotent startup
  migration; Gemini system prompt and template dropdowns updated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 18:26:55 +02:00

47 lines
2.1 KiB
Python

from motor.motor_asyncio import AsyncIOMotorClient
from dotenv import load_dotenv
import os
load_dotenv()
MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017")
MONGODB_DBNAME = os.getenv("MONGODB_DBNAME", "agenthub_db")
client = AsyncIOMotorClient(MONGODB_URI)
db = client[MONGODB_DBNAME]
users_collection = db.get_collection("users")
agents_collection = db.get_collection("agents")
agent_usage_collection = db.get_collection("agent_usage")
notifications_collection = db.get_collection("token_notifications")
agent_ratings_collection = db.get_collection("agent_ratings")
audit_history_collection = db.get_collection("audit_history")
completion_reminders_collection = db.get_collection("completion_reminders")
async def ensure_indexes():
"""Create database indexes for performance"""
try:
await agent_ratings_collection.create_index(
[("agent_id", 1), ("user_id", 1)],
unique=True
)
await agents_collection.create_index([("verification_status", 1)])
await agents_collection.create_index([("audit_status", 1)])
await agents_collection.create_index([("business_entity", 1)])
await agents_collection.create_index([("agent_classification", 1)])
await agents_collection.create_index([("autonomy_level", 1)])
await agents_collection.create_index([("ip_ownership", 1)])
await agents_collection.create_index([("registration_complete", 1)])
await audit_history_collection.create_index([("agent_id", 1), ("audit_date", -1)])
await completion_reminders_collection.create_index([("user_email", 1)], unique=True)
print("Database indexes ensured successfully")
except Exception as e:
print(f"Warning: Failed to create indexes: {e}")
async def check_database_health():
"""Check MongoDB connection health"""
try:
# Attempt to ping the database
await client.admin.command('ping')
return {"status": "connected", "healthy": True}
except Exception as e:
return {"status": "disconnected", "healthy": False, "error": str(e)}