""" OPC Agents Router """ from fastapi import APIRouter, Depends, HTTPException, Request from typing import Optional, List from pydantic import BaseModel from db import opc_db from rbac import require_page, User, require_admin router = APIRouter() class AgentUpdate(BaseModel): name: Optional[str] = None description: Optional[str] = None system_prompt: Optional[str] = None enabled: Optional[bool] = None class ExecutionApproval(BaseModel): approved: bool @router.get("/agents") async def list_agents( request: Request, enabled_only: bool = True, ): """List all agents""" user: User = request.state.user agents = await opc_db.get_agents(enabled_only=enabled_only) return {"items": agents} @router.get("/agents/{agent_id}") async def get_agent( agent_id: str, request: Request, ): """Get agent details""" user: User = request.state.user agent = await opc_db.get_agent(agent_id) if not agent: raise HTTPException(status_code=404, detail="Agent not found") return agent @router.put("/agents/{agent_id}", dependencies=[Depends(require_admin())]) async def update_agent( agent_id: str, updates: AgentUpdate, request: Request, ): """Update agent configuration (admin only)""" user: User = request.state.user # This would need an update_agent function in opc_db raise HTTPException(status_code=501, detail="Not implemented yet") @router.post("/agents/{agent_id}/execute") async def trigger_agent( agent_id: str, task_id: int, request: Request, ): """Manually trigger agent execution""" user: User = request.state.user execution = await opc_db.create_agent_execution( task_id=task_id, agent_id=agent_id, actor=user.username ) return execution @router.get("/executions") async def list_executions( request: Request, task_id: Optional[int] = None, agent_id: Optional[str] = None, status: Optional[str] = None, limit: int = 50, ): """List agent executions""" user: User = request.state.user executions = await opc_db.get_agent_executions( task_id=task_id, agent_id=agent_id, status=status, limit=limit ) return {"items": executions} @router.get("/executions/{execution_id}") async def get_execution( execution_id: int, request: Request, ): """Get execution details""" user: User = request.state.user executions = await opc_db.get_agent_executions(limit=1000) execution = next((e for e in executions if e["id"] == execution_id), None) if not execution: raise HTTPException(status_code=404, detail="Execution not found") return execution @router.post("/executions/{execution_id}/approve") async def approve_execution( execution_id: int, approval: ExecutionApproval, request: Request, ): """Approve or reject agent execution (for CxO level actions)""" user: User = request.state.user # This would need an approve_execution function in opc_db # For now, return placeholder return { "execution_id": execution_id, "approved": approval.approved, "approved_by": user.username }