Upload core/agent.py with huggingface_hub
Browse files- core/agent.py +111 -0
core/agent.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from abc import ABC, abstractmethod
|
| 3 |
+
from typing import Dict, List, Any, Optional
|
| 4 |
+
from core.models import AgentConfig, Task, AgentMessage
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Set up logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class BaseAgent(ABC):
|
| 13 |
+
"""Abstract base class for all agents in the system"""
|
| 14 |
+
|
| 15 |
+
def __init__(self, config: AgentConfig):
|
| 16 |
+
self.config = config
|
| 17 |
+
self.name = config.name
|
| 18 |
+
self.enabled = config.enabled
|
| 19 |
+
self.max_iterations = config.max_iterations
|
| 20 |
+
self.timeout_seconds = config.timeout_seconds
|
| 21 |
+
self.model_name = config.model_name
|
| 22 |
+
self.message_queue = asyncio.Queue()
|
| 23 |
+
self.tasks = []
|
| 24 |
+
|
| 25 |
+
async def run(self):
|
| 26 |
+
"""Main execution loop for the agent"""
|
| 27 |
+
if not self.enabled:
|
| 28 |
+
logger.info(f"Agent {self.name} is disabled, skipping execution")
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
logger.info(f"Starting agent: {self.name}")
|
| 32 |
+
iteration = 0
|
| 33 |
+
|
| 34 |
+
while iteration < self.max_iterations:
|
| 35 |
+
try:
|
| 36 |
+
# Process any incoming messages
|
| 37 |
+
await self.process_messages()
|
| 38 |
+
|
| 39 |
+
# Execute agent-specific logic
|
| 40 |
+
await self.execute()
|
| 41 |
+
|
| 42 |
+
# Check for new tasks
|
| 43 |
+
await self.check_tasks()
|
| 44 |
+
|
| 45 |
+
iteration += 1
|
| 46 |
+
await asyncio.sleep(1) # Small delay to prevent busy waiting
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logger.error(f"Error in agent {self.name}: {str(e)}")
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
+
logger.info(f"Agent {self.name} finished after {iteration} iterations")
|
| 53 |
+
|
| 54 |
+
async def process_messages(self):
|
| 55 |
+
"""Process messages from other agents"""
|
| 56 |
+
while not self.message_queue.empty():
|
| 57 |
+
message: AgentMessage = await self.message_queue.get()
|
| 58 |
+
await self.handle_message(message)
|
| 59 |
+
|
| 60 |
+
async def handle_message(self, message: AgentMessage):
|
| 61 |
+
"""Handle an incoming message"""
|
| 62 |
+
logger.info(f"Agent {self.name} received message from {message.sender}: {message.content}")
|
| 63 |
+
# Default implementation - override in subclasses as needed
|
| 64 |
+
|
| 65 |
+
async def send_message(self, recipient: str, content: str, message_type: str = "info"):
|
| 66 |
+
"""Send a message to another agent"""
|
| 67 |
+
message = AgentMessage(
|
| 68 |
+
sender=self.name,
|
| 69 |
+
recipient=recipient,
|
| 70 |
+
content=content,
|
| 71 |
+
message_type=message_type
|
| 72 |
+
)
|
| 73 |
+
# In a real implementation, this would send to a message broker
|
| 74 |
+
# For now, we'll just log it
|
| 75 |
+
logger.info(f"Agent {self.name} sending message to {recipient}: {content}")
|
| 76 |
+
|
| 77 |
+
async def add_task(self, task: Task):
|
| 78 |
+
"""Add a task to the agent's queue"""
|
| 79 |
+
self.tasks.append(task)
|
| 80 |
+
logger.info(f"Agent {self.name} added task: {task.description}")
|
| 81 |
+
|
| 82 |
+
async def check_tasks(self):
|
| 83 |
+
"""Check and process any assigned tasks"""
|
| 84 |
+
for task in self.tasks:
|
| 85 |
+
if task.status == "pending" and task.assigned_agent == self.name:
|
| 86 |
+
await self.execute_task(task)
|
| 87 |
+
|
| 88 |
+
async def execute_task(self, task: Task):
|
| 89 |
+
"""Execute a specific task"""
|
| 90 |
+
task.status = "running"
|
| 91 |
+
logger.info(f"Agent {self.name} starting task: {task.description}")
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
result = await self._execute_task_logic(task)
|
| 95 |
+
task.status = "completed"
|
| 96 |
+
task.completed_at = datetime.now()
|
| 97 |
+
task.result = result
|
| 98 |
+
logger.info(f"Agent {self.name} completed task: {task.description}")
|
| 99 |
+
except Exception as e:
|
| 100 |
+
task.status = "failed"
|
| 101 |
+
logger.error(f"Agent {self.name} failed task {task.description}: {str(e)}")
|
| 102 |
+
|
| 103 |
+
@abstractmethod
|
| 104 |
+
async def execute(self):
|
| 105 |
+
"""Execute the agent's primary function - must be implemented by subclasses"""
|
| 106 |
+
pass
|
| 107 |
+
|
| 108 |
+
@abstractmethod
|
| 109 |
+
async def _execute_task_logic(self, task: Task) -> Dict[str, Any]:
|
| 110 |
+
"""Execute the specific logic for a task - must be implemented by subclasses"""
|
| 111 |
+
pass
|