Upload services/governance-gateway/app/api/routes/gateway.py with huggingface_hub
Browse files
services/governance-gateway/app/api/routes/gateway.py
CHANGED
|
@@ -1,59 +1,60 @@
|
|
| 1 |
-
from datetime import UTC, datetime
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from app.
|
| 7 |
-
from app.
|
| 8 |
-
from app.services.
|
| 9 |
-
from app.services.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
| 1 |
+
from datetime import UTC, datetime
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
from fastapi import APIRouter, HTTPException
|
| 5 |
+
|
| 6 |
+
from app.core.config import get_settings
|
| 7 |
+
from app.schemas.gateway import GatewayAnswerRequest, GatewayAnswerResponse, GatewayMetricsResponse
|
| 8 |
+
from app.services.audit import AuditStore
|
| 9 |
+
from app.services.gateway_answer_store import GatewayAnswerStore
|
| 10 |
+
from app.services.orchestrator import GatewayOrchestrator
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
router = APIRouter(prefix="/gateway", tags=["gateway"])
|
| 14 |
+
_settings = get_settings()
|
| 15 |
+
_shared_audit = AuditStore(_settings.audit_log_path)
|
| 16 |
+
_shared_answer_store = GatewayAnswerStore(
|
| 17 |
+
database_url=_settings.database_url,
|
| 18 |
+
enabled=_settings.gateway_answers_persist,
|
| 19 |
+
)
|
| 20 |
+
_orchestrator = GatewayOrchestrator(
|
| 21 |
+
audit_store=_shared_audit,
|
| 22 |
+
answer_store=_shared_answer_store,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@router.post("/answer", response_model=GatewayAnswerResponse)
|
| 27 |
+
def answer(payload: GatewayAnswerRequest) -> GatewayAnswerResponse:
|
| 28 |
+
return _orchestrator.answer(payload)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@router.get("/audit/{audit_id}")
|
| 32 |
+
def get_audit(audit_id: str) -> dict:
|
| 33 |
+
record = _shared_audit.get(audit_id)
|
| 34 |
+
if not record:
|
| 35 |
+
raise HTTPException(status_code=404, detail=f"Audit record {audit_id} not found")
|
| 36 |
+
return record
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@router.get("/history")
|
| 40 |
+
def get_history(limit: int = 20, user_role: Optional[str] = None) -> list[dict]:
|
| 41 |
+
"""List recent gateway interactions from Postgres."""
|
| 42 |
+
return _shared_answer_store.list_history(limit=limit, user_role=user_role)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@router.get("/answers/{answer_id}")
|
| 46 |
+
def get_answer(answer_id: str) -> dict:
|
| 47 |
+
"""Load a stored gateway response from Postgres ``gateway_answers``."""
|
| 48 |
+
record = _shared_answer_store.get(answer_id)
|
| 49 |
+
if not record:
|
| 50 |
+
raise HTTPException(status_code=404, detail=f"Gateway answer {answer_id} not found")
|
| 51 |
+
return record
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@router.get("/metrics", response_model=GatewayMetricsResponse)
|
| 55 |
+
def metrics() -> GatewayMetricsResponse:
|
| 56 |
+
snapshot = _shared_audit.metrics()
|
| 57 |
+
return GatewayMetricsResponse(
|
| 58 |
+
generated_at=datetime.now(UTC),
|
| 59 |
+
**snapshot,
|
| 60 |
+
)
|