5da2ae01a0
- Add database connection retry logic (3 attempts, 5s delay) - Add missing database functions: get_task_by_id, get_agent_execution, approve_agent_execution, update_agent_config, update_execution_status - Complete agent execution approval workflow - Fix agent executor to handle approved executions - Fix inefficient task queries (use direct ID lookup) - Add error action handling in agent executor - Improve LiteLLM error handling with specific exception types - Reduce LiteLLM timeout from 60s to 30s - Fix execution status update to only set started_at once - Consolidate WebSocket datetime serialization with helper function - Implement update agent endpoint
136 lines
3.4 KiB
Python
136 lines
3.4 KiB
Python
"""
|
|
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
|
|
feedback: Optional[str] = None
|
|
|
|
|
|
@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
|
|
try:
|
|
agent = await opc_db.update_agent_config(
|
|
agent_id=agent_id,
|
|
name=updates.name,
|
|
description=updates.description,
|
|
system_prompt=updates.system_prompt,
|
|
enabled=updates.enabled
|
|
)
|
|
return agent
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
|
|
|
|
@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
|
|
execution = await opc_db.get_agent_execution(execution_id)
|
|
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
|
|
try:
|
|
execution = await opc_db.approve_agent_execution(
|
|
execution_id=execution_id,
|
|
approved=approval.approved,
|
|
approved_by=user.username,
|
|
feedback=approval.feedback
|
|
)
|
|
return execution
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|