fix: complete OPC system implementation
- 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
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
OPC WebSocket Router - Real-time updates for tasks and agent executions
|
||||
"""
|
||||
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Depends
|
||||
from typing import Set
|
||||
from typing import Set, Any, Dict
|
||||
from datetime import datetime
|
||||
import json
|
||||
import asyncio
|
||||
import logging
|
||||
@@ -16,6 +17,18 @@ router = APIRouter()
|
||||
active_connections: Set[WebSocket] = set()
|
||||
|
||||
|
||||
def serialize_datetime(obj: Any) -> Any:
|
||||
"""Recursively serialize datetime objects to ISO format strings"""
|
||||
if isinstance(obj, datetime):
|
||||
return obj.isoformat()
|
||||
elif isinstance(obj, dict):
|
||||
return {key: serialize_datetime(value) for key, value in obj.items()}
|
||||
elif isinstance(obj, list):
|
||||
return [serialize_datetime(item) for item in obj]
|
||||
else:
|
||||
return obj
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
"""Manages WebSocket connections"""
|
||||
|
||||
@@ -74,44 +87,30 @@ async def websocket_endpoint(websocket: WebSocket):
|
||||
|
||||
async def broadcast_task_update(task_id: int, action: str, task_data: dict):
|
||||
"""Broadcast task update to all connected clients"""
|
||||
# Serialize datetime objects to ISO format
|
||||
serialized_data = task_data.copy()
|
||||
for key in ["created_at", "updated_at", "completed_at", "due_date"]:
|
||||
if serialized_data.get(key):
|
||||
serialized_data[key] = serialized_data[key].isoformat() if hasattr(serialized_data[key], 'isoformat') else serialized_data[key]
|
||||
|
||||
timestamp = task_data.get("updated_at")
|
||||
if timestamp and hasattr(timestamp, 'isoformat'):
|
||||
timestamp = timestamp.isoformat()
|
||||
# Serialize all datetime objects recursively
|
||||
serialized_data = serialize_datetime(task_data)
|
||||
|
||||
message = {
|
||||
"type": "task_update",
|
||||
"task_id": task_id,
|
||||
"action": action, # created, updated, moved, deleted
|
||||
"data": serialized_data,
|
||||
"timestamp": timestamp
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
await manager.broadcast(message)
|
||||
|
||||
|
||||
async def broadcast_agent_execution(execution_id: int, status: str, execution_data: dict):
|
||||
"""Broadcast agent execution update"""
|
||||
# Serialize datetime objects to ISO format
|
||||
serialized_data = execution_data.copy()
|
||||
for key in ["started_at", "completed_at"]:
|
||||
if serialized_data.get(key):
|
||||
serialized_data[key] = serialized_data[key].isoformat() if hasattr(serialized_data[key], 'isoformat') else serialized_data[key]
|
||||
|
||||
timestamp = execution_data.get("started_at") or execution_data.get("completed_at")
|
||||
if timestamp and hasattr(timestamp, 'isoformat'):
|
||||
timestamp = timestamp.isoformat()
|
||||
# Serialize all datetime objects recursively
|
||||
serialized_data = serialize_datetime(execution_data)
|
||||
|
||||
message = {
|
||||
"type": "agent_execution",
|
||||
"execution_id": execution_id,
|
||||
"status": status, # pending, running, completed, failed, pending_approval
|
||||
"data": serialized_data,
|
||||
"timestamp": timestamp
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
await manager.broadcast(message)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user