|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import threading |
| 4 | +import time |
| 5 | +from abc import ABC, abstractmethod |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from smolagents import LiteLLMModel, ToolCallingAgent |
| 9 | + |
| 10 | + |
| 11 | +class Agent(ABC): |
| 12 | + """Base class for AI agents.""" |
| 13 | + |
| 14 | + def __init__(self, model: LiteLLMModel, api_key: str = None, anthropic_api_key: str = None): |
| 15 | + self.model = model |
| 16 | + self.api_key = api_key |
| 17 | + self.anthropic_api_key = anthropic_api_key |
| 18 | + self.agents = {} |
| 19 | + self.setup_agents() |
| 20 | + |
| 21 | + # Rate limiting configuration |
| 22 | + self.min_request_interval = 1.0 # Minimum seconds between requests |
| 23 | + self.last_request_time = None |
| 24 | + self.request_lock = threading.Lock() |
| 25 | + |
| 26 | + def wait_for_rate_limit(self): |
| 27 | + """Ensure minimum time between requests.""" |
| 28 | + with self.request_lock: |
| 29 | + if self.last_request_time: |
| 30 | + elapsed = time.time() - self.last_request_time |
| 31 | + if elapsed < self.min_request_interval: |
| 32 | + sleep_time = self.min_request_interval - elapsed |
| 33 | + print(f"Rate limiting: waiting {sleep_time:.2f} seconds...") |
| 34 | + time.sleep(sleep_time) |
| 35 | + self.last_request_time = time.time() |
| 36 | + |
| 37 | + def get_prompt(self, prompt_name: str) -> str: |
| 38 | + """Load prompt template from file.""" |
| 39 | + prompt_path = Path(__file__).parent / "prompts" / prompt_name |
| 40 | + if prompt_path.exists(): |
| 41 | + with open(prompt_path, 'r') as f: |
| 42 | + return f.read() |
| 43 | + return "" |
| 44 | + |
| 45 | + @abstractmethod |
| 46 | + def setup_agents(self): |
| 47 | + """Setup agents specific to this agent.""" |
| 48 | + pass |
| 49 | + |
| 50 | + def get_manager_agent(self) -> ToolCallingAgent: |
| 51 | + """Get the main manager agent for this system.""" |
| 52 | + # Try to find father_of_george first |
| 53 | + if 'father_of_george' in self.agents: |
| 54 | + return self.agents['father_of_george'] |
| 55 | + |
| 56 | + # Look for agents with 'Manager' in their description or name |
| 57 | + for key, agent in self.agents.items(): |
| 58 | + if hasattr(agent, 'description') and 'Manager' in agent.description: |
| 59 | + return agent |
| 60 | + if hasattr(agent, 'name') and 'Manager' in agent.name: |
| 61 | + return agent |
| 62 | + |
| 63 | + # Fallback: return the first agent if no manager found |
| 64 | + if self.agents: |
| 65 | + return list(self.agents.values())[0] |
| 66 | + |
| 67 | + return None |
| 68 | + |
| 69 | + def get_all_agents(self) -> list: |
| 70 | + """Get all agents in this system.""" |
| 71 | + return list(self.agents.values()) |
| 72 | + |
| 73 | + def run_task(self, task_description: str, context: dict = None) -> dict: |
| 74 | + """Run a task using this system's agents.""" |
| 75 | + results = { |
| 76 | + "task_description": task_description, |
| 77 | + "completed": False, |
| 78 | + "output": None, |
| 79 | + "error": None, |
| 80 | + } |
| 81 | + |
| 82 | + try: |
| 83 | + self.wait_for_rate_limit() |
| 84 | + |
| 85 | + # Get the manager agent |
| 86 | + manager_agent = self.get_manager_agent() |
| 87 | + if not manager_agent: |
| 88 | + results["error"] = "No manager agent found for this system" |
| 89 | + return results |
| 90 | + |
| 91 | + # Create task prompt |
| 92 | + prompt = self._create_task_prompt(task_description, context) |
| 93 | + |
| 94 | + print(f"Using {manager_agent.name} for task: {task_description}") |
| 95 | + |
| 96 | + # Run the agent |
| 97 | + agent_output = manager_agent.run(prompt) |
| 98 | + results["output"] = str(agent_output) |
| 99 | + results["completed"] = True |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + results["error"] = str(e) |
| 103 | + print(f"Error running task: {e}") |
| 104 | + |
| 105 | + return results |
| 106 | + |
| 107 | + def _create_task_prompt(self, task_description: str, context: dict = None) -> str: |
| 108 | + """Create a prompt for the given task.""" |
| 109 | + prompt = f"Task: {task_description}\n\n" |
| 110 | + |
| 111 | + if context: |
| 112 | + prompt += "Context:\n" |
| 113 | + for key, value in context.items(): |
| 114 | + prompt += f"- {key}: {value}\n" |
| 115 | + prompt += "\n" |
| 116 | + |
| 117 | + return prompt |
| 118 | + |
| 119 | + |
| 120 | +# Tool definitions for different agent types |
| 121 | +def get_manager_tools(): |
| 122 | + """Tools available to manager agents.""" |
| 123 | + return [ |
| 124 | + # TODO: Add manager-specific tools |
| 125 | + # - orchestrate_subagents |
| 126 | + # - collect_results |
| 127 | + # - make_decisions |
| 128 | + # - validate_outputs |
| 129 | + ] |
| 130 | + |
| 131 | +def get_code_analysis_tools(): |
| 132 | + """Tools available to code analysis agents.""" |
| 133 | + return [ |
| 134 | + # TODO: Add code analysis tools |
| 135 | + # - analyze_syntax |
| 136 | + # - analyze_semantics |
| 137 | + # - extract_patterns |
| 138 | + # - identify_vulnerabilities |
| 139 | + ] |
| 140 | + |
| 141 | +def get_retrieval_tools(): |
| 142 | + """Tools available to retrieval agents.""" |
| 143 | + return [ |
| 144 | + # TODO: Add retrieval tools |
| 145 | + # - query_rag_db |
| 146 | + # - search_vector_db |
| 147 | + # - retrieve_context |
| 148 | + # - validate_information |
| 149 | + ] |
| 150 | + |
| 151 | +def get_v8_search_tools(): |
| 152 | + """Tools available to V8 search agents.""" |
| 153 | + return [ |
| 154 | + # TODO: Add V8 search tools |
| 155 | + # - fuzzy_find |
| 156 | + # - regex_search |
| 157 | + # - compile_with_clang |
| 158 | + # - test_with_python |
| 159 | + # - view_call_graph |
| 160 | + # - web_search |
| 161 | + ] |
| 162 | + |
| 163 | +def get_program_builder_tools(): |
| 164 | + """Tools available to program builder agents.""" |
| 165 | + return [ |
| 166 | + # TODO: Add program builder tools |
| 167 | + # - query_postgres_db |
| 168 | + # - generate_seed_program |
| 169 | + # - combine_contexts |
| 170 | + # - validate_syntax |
| 171 | + ] |
| 172 | + |
| 173 | +def get_corpus_generation_tools(): |
| 174 | + """Tools available to corpus generation agents.""" |
| 175 | + return [ |
| 176 | + # TODO: Add corpus generation tools |
| 177 | + # - validate_syntax |
| 178 | + # - validate_semantics |
| 179 | + # - test_program |
| 180 | + # - evaluate_interestingness |
| 181 | + ] |
| 182 | + |
| 183 | +def get_runtime_analysis_tools(): |
| 184 | + """Tools available to runtime analysis agents.""" |
| 185 | + return [ |
| 186 | + # TODO: Add runtime analysis tools |
| 187 | + # - analyze_execution_state |
| 188 | + # - check_coverage |
| 189 | + # - evaluate_flags |
| 190 | + # - determine_seed_quality |
| 191 | + ] |
| 192 | + |
| 193 | +def get_validation_tools(): |
| 194 | + """Tools available to validation agents.""" |
| 195 | + return [ |
| 196 | + # TODO: Add validation tools |
| 197 | + # - validate_corpus |
| 198 | + # - check_db_integrity |
| 199 | + # - verify_results |
| 200 | + # - quality_assurance |
| 201 | + ] |
0 commit comments