Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /routes /sessions /router.py
| """ | |
| ACGS-2 Session Governance - Router Configuration | |
| Constitutional Hash: 608508a9bd224290 | |
| FastAPI router setup, lifecycle management, and route registration. | |
| """ | |
| from __future__ import annotations | |
| from fastapi import APIRouter, Depends, Header, HTTPException, status | |
| from enhanced_agent_bus._compat.security.auth import get_current_user | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| from . import endpoints | |
| # Import from fallbacks module for consistency | |
| from ._fallbacks import SessionContextManager, get_tenant_id | |
| from .models import ( | |
| CreateSessionRequest, | |
| ErrorResponse, | |
| PolicySelectionRequest, | |
| PolicySelectionResponse, | |
| SessionMetricsResponse, | |
| SessionResponse, | |
| UpdateGovernanceRequest, | |
| ) | |
| logger = get_logger(__name__) | |
| # ============================================================================= | |
| # Router Setup | |
| # ============================================================================= | |
| router = APIRouter( | |
| prefix="/api/v1/sessions", | |
| tags=["Session Governance"], | |
| dependencies=[Depends(get_current_user)], | |
| ) | |
| # Global session context manager (initialized on startup) | |
| _session_manager: SessionContextManager | None = None | |
| def get_session_manager() -> SessionContextManager: | |
| """Get the global session context manager. | |
| Raises: | |
| HTTPException: If manager is not initialized | |
| """ | |
| if _session_manager is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | |
| detail="Session context manager not initialized", | |
| ) | |
| return _session_manager | |
| # ============================================================================= | |
| # Lifecycle Functions | |
| # ============================================================================= | |
| async def init_session_manager(redis_url: str = "redis://localhost:6379") -> bool: | |
| """Initialize the session context manager. | |
| Args: | |
| redis_url: Redis connection URL | |
| Returns: | |
| True if initialized successfully | |
| """ | |
| global _session_manager | |
| _session_manager = SessionContextManager(redis_url=redis_url) | |
| return await _session_manager.connect() | |
| async def shutdown_session_manager() -> None: | |
| """Shutdown the session context manager.""" | |
| global _session_manager | |
| if _session_manager: | |
| await _session_manager.disconnect() | |
| _session_manager = None | |
| # ============================================================================= | |
| # Route Registration | |
| # ============================================================================= | |
| async def create_session_route( | |
| request: CreateSessionRequest, | |
| tenant_id: str = Depends(get_tenant_id), | |
| x_user_id: str | None = Header(None, alias="X-User-ID"), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> SessionResponse: | |
| """Create a new session with governance configuration.""" | |
| return await endpoints.create_session(request, tenant_id, x_user_id, manager) | |
| async def get_session_route( | |
| session_id: str, | |
| tenant_id: str = Depends(get_tenant_id), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> SessionResponse: | |
| """Get session governance configuration by ID.""" | |
| return await endpoints.get_session(session_id, tenant_id, manager) | |
| async def update_session_governance_route( | |
| session_id: str, | |
| request: UpdateGovernanceRequest, | |
| tenant_id: str = Depends(get_tenant_id), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> SessionResponse: | |
| """Update session governance configuration.""" | |
| return await endpoints.update_session_governance(session_id, request, tenant_id, manager) | |
| async def delete_session_route( | |
| session_id: str, | |
| tenant_id: str = Depends(get_tenant_id), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ): | |
| """Delete a session.""" | |
| return await endpoints.delete_session(session_id, tenant_id, manager) | |
| async def extend_session_ttl_route( | |
| session_id: str, | |
| ttl_seconds: int = 3600, | |
| tenant_id: str = Depends(get_tenant_id), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> SessionResponse: | |
| """Extend session TTL.""" | |
| return await endpoints.extend_session_ttl(session_id, ttl_seconds, tenant_id, manager) | |
| async def select_session_policies_route( | |
| session_id: str, | |
| request: PolicySelectionRequest | None = None, | |
| tenant_id: str = Depends(get_tenant_id), | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> PolicySelectionResponse: | |
| """Select applicable policies based on session context.""" | |
| return await endpoints.select_session_policies(session_id, request, tenant_id, manager) | |
| async def get_session_metrics_route( | |
| manager: SessionContextManager = Depends(get_session_manager), | |
| ) -> SessionMetricsResponse: | |
| """Get session manager performance metrics.""" | |
| return await endpoints.get_session_metrics(manager) | |
| # ============================================================================= | |
| # Exports | |
| # ============================================================================= | |
| __all__ = [ | |
| "get_session_manager", | |
| "init_session_manager", | |
| "router", | |
| "shutdown_session_manager", | |
| ] | |
Xet Storage Details
- Size:
- 9.69 kB
- Xet hash:
- ffc2aeb58b8ff9ff4e642c1613c508f56287962aaf8346f8b10b4b36b2f4223f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.