|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Agent Coordinator |
| 3 | +# Manages background system and coordinates task execution |
| 4 | + |
| 5 | +import requests |
| 6 | +import time |
| 7 | +import uuid |
| 8 | +import json |
| 9 | + |
| 10 | +class AgentCoordinator: |
| 11 | + def __init__(self, background_system_url="http://localhost:8082"): |
| 12 | + self.background_url = background_system_url |
| 13 | + self.active_tasks = {} |
| 14 | + |
| 15 | + def generate_task_id(self): |
| 16 | + """Generate unique task ID""" |
| 17 | + return str(uuid.uuid4())[:8] # Short ID for readability |
| 18 | + |
| 19 | + def send_task(self, task_type, parameters=None, priority='normal'): |
| 20 | + """Send task to background system""" |
| 21 | + if parameters is None: |
| 22 | + parameters = {} |
| 23 | + |
| 24 | + task_data = { |
| 25 | + 'task_id': self.generate_task_id(), |
| 26 | + 'task_type': task_type, |
| 27 | + 'parameters': parameters, |
| 28 | + 'priority': priority |
| 29 | + } |
| 30 | + |
| 31 | + try: |
| 32 | + response = requests.post(f"{self.background_url}/send_task", json=task_data, timeout=5) |
| 33 | + result = response.json() |
| 34 | + |
| 35 | + task_id = result['task_id'] |
| 36 | + self.active_tasks[task_id] = { |
| 37 | + 'type': task_type, |
| 38 | + 'parameters': parameters, |
| 39 | + 'priority': priority, |
| 40 | + 'sent_time': time.time() |
| 41 | + } |
| 42 | + |
| 43 | + print(f"📤 Agent: Sent task {task_id} ({task_type}) to background system") |
| 44 | + return task_id |
| 45 | + |
| 46 | + except Exception as e: |
| 47 | + print(f"❌ Agent: Failed to send task: {e}") |
| 48 | + return None |
| 49 | + |
| 50 | + def get_result(self, task_id): |
| 51 | + """Get result from background system""" |
| 52 | + try: |
| 53 | + response = requests.get(f"{self.background_url}/get_result?task_id={task_id}", timeout=5) |
| 54 | + result = response.json() |
| 55 | + |
| 56 | + print(f"📤 Agent: Retrieved result for task {task_id}") |
| 57 | + |
| 58 | + # Remove from active tasks |
| 59 | + if task_id in self.active_tasks: |
| 60 | + del self.active_tasks[task_id] |
| 61 | + |
| 62 | + return result |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + print(f"❌ Agent: Failed to get result for {task_id}: {e}") |
| 66 | + return None |
| 67 | + |
| 68 | + def check_status(self, task_id): |
| 69 | + """Check task status""" |
| 70 | + try: |
| 71 | + response = requests.get(f"{self.background_url}/status?task_id={task_id}", timeout=5) |
| 72 | + result = response.json() |
| 73 | + |
| 74 | + print(f"📊 Agent: Status for task {task_id}: {result['status']}") |
| 75 | + return result |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + print(f"❌ Agent: Failed to check status for {task_id}: {e}") |
| 79 | + return None |
| 80 | + |
| 81 | + def cancel_task(self, task_id): |
| 82 | + """Cancel task in background system""" |
| 83 | + try: |
| 84 | + response = requests.delete(f"{self.background_url}/cancel_task?task_id={task_id}", timeout=5) |
| 85 | + result = response.json() |
| 86 | + |
| 87 | + print(f"📤 Agent: Cancelled task {task_id}: {result['status']}") |
| 88 | + |
| 89 | + # Remove from active tasks |
| 90 | + if task_id in self.active_tasks: |
| 91 | + del self.active_tasks[task_id] |
| 92 | + |
| 93 | + return result |
| 94 | + |
| 95 | + except Exception as e: |
| 96 | + print(f"❌ Agent: Failed to cancel task {task_id}: {e}") |
| 97 | + return None |
| 98 | + |
| 99 | + def list_all_tasks(self): |
| 100 | + """List all tasks""" |
| 101 | + try: |
| 102 | + response = requests.get(f"{self.background_url}/list_tasks", timeout=5) |
| 103 | + result = response.json() |
| 104 | + |
| 105 | + print(f"📊 Agent: Retrieved task list") |
| 106 | + return result |
| 107 | + |
| 108 | + except Exception as e: |
| 109 | + print(f"❌ Agent: Failed to list tasks: {e}") |
| 110 | + return None |
| 111 | + |
| 112 | + def wait_for_completion(self, task_id, timeout=300): |
| 113 | + """Wait for task completion with timeout""" |
| 114 | + start_time = time.time() |
| 115 | + |
| 116 | + print(f"⏳ Agent: Waiting for task {task_id} completion...") |
| 117 | + |
| 118 | + while time.time() - start_time < timeout: |
| 119 | + status = self.check_status(task_id) |
| 120 | + if status and status['status'] == 'completed': |
| 121 | + result = self.get_result(task_id) |
| 122 | + print(f"✅ Agent: Task {task_id} completed successfully") |
| 123 | + return result |
| 124 | + elif status and status['status'] == 'failed': |
| 125 | + print(f"❌ Agent: Task {task_id} failed") |
| 126 | + return None |
| 127 | + else: |
| 128 | + print(f"⏳ Agent: Task {task_id} still running (duration: {status.get('duration', 0):.1f}s)") |
| 129 | + time.sleep(5) |
| 130 | + |
| 131 | + print(f"⏰ Agent: Timeout waiting for task {task_id}") |
| 132 | + return None |
| 133 | + |
| 134 | +# Example usage |
| 135 | +def demonstrate_agent_coordination(): |
| 136 | + """Demonstrate agent coordination with background system""" |
| 137 | + |
| 138 | + print("🎯 AGENT COORDINATOR DEMONSTRATION") |
| 139 | + print("=" * 40) |
| 140 | + |
| 141 | + # Create agent coordinator |
| 142 | + agent = AgentCoordinator() |
| 143 | + |
| 144 | + # Send Phase 2 tasks |
| 145 | + print("\n📋 Sending Phase 2 tasks to background system:") |
| 146 | + task_ids = [] |
| 147 | + |
| 148 | + phase_2_tasks = [ |
| 149 | + ('meeting_link_function', {'priority': 'high'}), |
| 150 | + ('team_assignment', {'priority': 'high'}), |
| 151 | + ('token_generation', {'priority': 'normal'}), |
| 152 | + ('email_integration', {'priority': 'normal'}) |
| 153 | + ] |
| 154 | + |
| 155 | + for task_name, task_params in phase_2_tasks: |
| 156 | + task_id = agent.send_task('phase_2_integration', task_params, task_params['priority']) |
| 157 | + if task_id: |
| 158 | + task_ids.append(task_id) |
| 159 | + |
| 160 | + # Agent can work on other things while tasks run |
| 161 | + print("\n🎯 Agent working on analysis while Phase 2 runs in background...") |
| 162 | + time.sleep(2) |
| 163 | + print("🎯 Agent analysis completed") |
| 164 | + |
| 165 | + # Collect Phase 2 results |
| 166 | + print("\n📊 Collecting Phase 2 results:") |
| 167 | + phase_2_results = {} |
| 168 | + |
| 169 | + for task_id in task_ids: |
| 170 | + result = agent.wait_for_completion(task_id, timeout=30) |
| 171 | + if result: |
| 172 | + phase_2_results[task_id] = result |
| 173 | + print(f"✅ {task_id}: {result['status']}") |
| 174 | + else: |
| 175 | + print(f"❌ {task_id}: Timeout or error") |
| 176 | + |
| 177 | + # Send Phase 3 tasks |
| 178 | + print("\n📋 Sending Phase 3 tasks to background system:") |
| 179 | + task_ids = [] |
| 180 | + |
| 181 | + phase_3_tasks = [ |
| 182 | + ('issue_room_logic', {'priority': 'high'}), |
| 183 | + ('auto_pull_implementation', {'priority': 'high'}), |
| 184 | + ('room_switching', {'priority': 'normal'}), |
| 185 | + ('coordinator_overview', {'priority': 'normal'}) |
| 186 | + ] |
| 187 | + |
| 188 | + for task_name, task_params in phase_3_tasks: |
| 189 | + task_id = agent.send_task('phase_3_management', task_params, task_params['priority']) |
| 190 | + if task_id: |
| 191 | + task_ids.append(task_id) |
| 192 | + |
| 193 | + # Agent can work on other things while tasks run |
| 194 | + print("\n🎯 Agent working on dashboard updates while Phase 3 runs in background...") |
| 195 | + time.sleep(2) |
| 196 | + print("🎯 Dashboard updates completed") |
| 197 | + |
| 198 | + # Collect Phase 3 results |
| 199 | + print("\n📊 Collecting Phase 3 results:") |
| 200 | + phase_3_results = {} |
| 201 | + |
| 202 | + for task_id in task_ids: |
| 203 | + result = agent.wait_for_completion(task_id, timeout=30) |
| 204 | + if result: |
| 205 | + phase_3_results[task_id] = result |
| 206 | + print(f"✅ {task_id}: {result['status']}") |
| 207 | + else: |
| 208 | + print(f"❌ {task_id}: Timeout or error") |
| 209 | + |
| 210 | + # Final summary |
| 211 | + print("\n🎯 AGENT COORDINATION COMPLETE:") |
| 212 | + print(f"✅ Phase 2: {len(phase_2_results)} tasks completed") |
| 213 | + print(f"✅ Phase 3: {len(phase_3_results)} tasks completed") |
| 214 | + print(f"✅ Agent remained productive throughout") |
| 215 | + |
| 216 | + return phase_2_results, phase_3_results |
| 217 | + |
| 218 | +if __name__ == "__main__": |
| 219 | + # Start background system first |
| 220 | + print("🚀 Starting Background System Server...") |
| 221 | + import subprocess |
| 222 | + subprocess.Popen(["python", "background_system_server.py"], cwd=os.path.dirname(__file__)) |
| 223 | + time.sleep(3) # Give server time to start |
| 224 | + |
| 225 | + # Then demonstrate agent coordination |
| 226 | + demonstrate_agent_coordination() |
0 commit comments