| import os |
| import sys |
| import re |
| import io |
| import contextlib |
| import traceback |
| import time |
| import ast |
| import requests |
| from datetime import datetime |
| from typing import List, Dict, Optional, Any, Union, TYPE_CHECKING |
|
|
| if TYPE_CHECKING: |
| from agent.tools.definitions import ToolDefinition |
|
|
| |
| class Colors: |
| CYAN = '\033[96m' |
| GREEN = '\033[92m' |
| YELLOW = '\033[93m' |
| RED = '\033[91m' |
| BLUE = '\033[94m' |
| RESET = '\033[0m' |
| BOLD = '\033[1m' |
|
|
| class BaseHandler: |
| def __init__(self, api_key: str, model_name: str, base_url: str = "http://localhost:17830", display_name: str = None): |
| self.api_key = api_key |
| self.model_name = model_name |
| self.display_name = display_name or model_name |
| self.base_url = base_url |
| self.log_url = f"{base_url}/api/agent/log" |
| self.mission_complete = False |
|
|
| |
| self.total_input_tokens = 0 |
| self.total_output_tokens = 0 |
|
|
| |
| self.last_thoughts: str = "" |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| try: |
| from agent.client import CanyonClient |
| self.client_class = CanyonClient |
| except ImportError: |
| try: |
| from .client import CanyonClient |
| self.client_class = CanyonClient |
| except ImportError: |
| print(f"{Colors.RED}[WARN] Could not import CanyonClient. 'client' must be injected manually.{Colors.RESET}") |
| self.client_class = None |
|
|
| self.locals = { |
| "pd": __import__("pandas"), |
| "np": __import__("numpy"), |
| "print": print, |
| "time": time |
| } |
|
|
| |
| |
| if self.client_class: |
| self.locals["client"] = self.client_class(base_url, auto_register=False) |
|
|
| print(f"{Colors.GREEN}[SYSTEM] Agent initialized. Client connected to {base_url}{Colors.RESET}") |
| |
| def log(self, type: str, content: str, metadata: Dict = None): |
| try: |
| payload = { |
| "timestamp": datetime.now().isoformat(), |
| "type": type, |
| "content": content, |
| "metadata": metadata or {} |
| } |
| |
| headers = {"Content-Type": "application/json"} |
| client = self.locals.get("client") |
| if client and hasattr(client, "_session_id") and client._session_id: |
| headers["X-Session-ID"] = client._session_id |
| requests.post(self.log_url, json=payload, headers=headers, timeout=10) |
| except Exception as e: |
| |
| print(f"{Colors.RED}[LOG ERROR] Failed to send log: {e}{Colors.RESET}") |
|
|
| def run_code(self, code: str) -> str: |
| """Executes python code and returns stdout/stderr.""" |
| if not code.strip(): |
| return "No code to execute." |
| |
| |
| self.log("ACTION", code) |
| |
| |
| if "client.deploy_drone" in code: |
| self.log("DEPLOYMENT", "Deploying Drones...", {"code": code}) |
| |
| stdout = io.StringIO() |
| stderr = io.StringIO() |
| |
| print(f"{Colors.YELLOW}[EXECUTING]{Colors.RESET}\n{code}") |
| |
| try: |
| |
| tree = ast.parse(code) |
| last_stmt = tree.body[-1] if tree.body else None |
| |
| |
| if isinstance(last_stmt, ast.Expr): |
| |
| tree.body.pop() |
| |
| exec_code = compile(tree, "<string>", "exec") |
| |
| eval_code = compile(ast.Expression(last_stmt.value), "<string>", "eval") |
| |
| with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): |
| exec(exec_code, self.locals, self.locals) |
| result = eval(eval_code, self.locals, self.locals) |
| if result is not None: |
| print(result) |
| else: |
| |
| with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): |
| exec(code, self.locals, self.locals) |
| |
| output = stdout.getvalue() |
| error = stderr.getvalue() |
| except Exception: |
| output = stdout.getvalue() |
| error = traceback.format_exc() |
| self.log("ERROR", error) |
| |
| result_msg = "" |
| if output: |
| result_msg += f"OUTPUT:\n{output}\n" |
| if error: |
| result_msg += f"ERRORS:\n{error}\n" |
| if not result_msg: |
| result_msg = "Code executed successfully (no output)." |
| |
| print(f"{Colors.CYAN}[RESULT]{Colors.RESET}\n{result_msg.strip()}") |
| return result_msg |
|
|
| def strip_thinking_tags(self, text: str) -> str: |
| """Remove <think>...</think> tags from model output. |
| |
| Some models (e.g., MiniMax M2) output thinking process in these tags, |
| which can interfere with code extraction. |
| """ |
| |
| cleaned = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL) |
| return cleaned.strip() |
|
|
| def extract_code(self, text: str) -> str: |
| """Extracts the last python code block from the response.""" |
| matches = re.findall(r"```python\s*(.*?)```", text, re.DOTALL) |
| if matches: |
| return matches[-1].strip() |
| return None |
|
|
| def send_message(self, message: str) -> str: |
| """Abstract method to send message to LLM.""" |
| raise NotImplementedError("Subclasses must implement send_message") |
|
|
| def send_message_with_tools( |
| self, |
| message: str, |
| tools: List["ToolDefinition"], |
| ) -> Union[str, Dict[str, Any]]: |
| """ |
| Send message with tool definitions and handle tool calls. |
| |
| This method is optional - subclasses that support function/tool calling |
| should override this. The default implementation falls back to send_message. |
| |
| Args: |
| message: The message to send |
| tools: List of ToolDefinition objects available for the LLM to use |
| |
| Returns: |
| Either a plain string response, or a dict containing: |
| - "content": The text response |
| - "tool_calls": List of tool call requests from the LLM |
| """ |
| |
| return self.send_message(message) |
|
|
| def supports_tool_calling(self) -> bool: |
| """ |
| Check if this handler supports native tool/function calling. |
| |
| Subclasses that implement send_message_with_tools should override |
| this to return True. |
| |
| Returns: |
| True if native tool calling is supported, False otherwise |
| """ |
| return False |
|
|
| def get_token_usage(self) -> Dict[str, int]: |
| """Get token usage statistics.""" |
| return { |
| "input_tokens": self.total_input_tokens, |
| "output_tokens": self.total_output_tokens, |
| "total_tokens": self.total_input_tokens + self.total_output_tokens, |
| } |
|
|
| def export_conversation_history(self) -> List[Dict[str, str]]: |
| """Export conversation history in a standard JSON-serializable format. |
| |
| Returns: |
| List of {"role": "user"|"assistant"|"system", "content": "..."} |
| """ |
| |
| if hasattr(self, 'history') and isinstance(self.history, list): |
| return [ |
| {"role": msg.get("role", "user"), "content": msg.get("content", "")} |
| for msg in self.history |
| if isinstance(msg, dict) |
| ] |
| return [] |
|
|
| def import_conversation_history(self, history: List[Dict[str, str]]) -> None: |
| """Import conversation history from saved format. |
| |
| Args: |
| history: List of {"role": "user"|"assistant"|"system", "content": "..."} |
| """ |
| |
| if hasattr(self, 'history'): |
| self.history = [ |
| {"role": msg["role"], "content": msg["content"]} |
| for msg in history |
| ] |
|
|
| def save_conversation_to_backend(self) -> bool: |
| """Save current conversation history to backend for resume support. |
| |
| Returns: |
| True if save successful, False otherwise |
| """ |
| try: |
| client = self.locals.get("client") |
| if client and hasattr(client, "save_conversation_history"): |
| history = self.export_conversation_history() |
| client.save_conversation_history(history) |
| return True |
| except Exception as e: |
| print(f"{Colors.RED}[WARN] Failed to save conversation history: {e}{Colors.RESET}") |
| return False |
|
|
| def load_conversation_from_backend(self) -> bool: |
| """Load conversation history from backend for resume. |
| |
| Returns: |
| True if load successful, False otherwise |
| """ |
| try: |
| client = self.locals.get("client") |
| if client and hasattr(client, "get_conversation_history"): |
| result = client.get_conversation_history() |
| history = result.get("conversation_history", []) |
| if history: |
| self.import_conversation_history(history) |
| print(f"{Colors.GREEN}[SYSTEM] Loaded {len(history)} messages from saved conversation{Colors.RESET}") |
| return True |
| except Exception as e: |
| print(f"{Colors.RED}[WARN] Failed to load conversation history: {e}{Colors.RESET}") |
| return False |
|
|
| def report_token_usage(self) -> bool: |
| """Report current token usage to backend for real-time display. |
| |
| Returns: |
| True if report successful, False otherwise |
| """ |
| try: |
| client = self.locals.get("client") |
| if client and hasattr(client, "update_token_usage"): |
| client.update_token_usage( |
| input_tokens=self.total_input_tokens, |
| output_tokens=self.total_output_tokens |
| ) |
| return True |
| except Exception as e: |
| |
| pass |
| return False |
|
|
| def step(self, user_input: str = None, depth: int = 0, log_type: str = "THOUGHT"): |
| """Single step in the conversation loop with Retry Logic. |
| |
| Args: |
| user_input: Input message to send to the model |
| depth: Recursion depth counter |
| log_type: Type of log entry to create (THOUGHT or REPORT) |
| """ |
| if depth > 50: |
| print(f"{Colors.RED}[WARN] Max recursion depth reached. Stopping conversation chain.{Colors.RESET}") |
| return |
|
|
| if self.mission_complete: |
| print(f"{Colors.GREEN}[SYSTEM] Mission Accomplished. Halting Agent.{Colors.RESET}") |
| return |
|
|
| msg = user_input if user_input else "Proceed." |
|
|
| while True: |
| |
| if not hasattr(self, 'current_delay'): |
| self.current_delay = 5 |
| |
| try: |
| content = self.send_message(msg) |
| |
| self.current_delay = 5 |
|
|
| print(f"\n{Colors.BOLD}[{self.display_name}]{Colors.RESET}\n{content}") |
| |
| |
| from datetime import timedelta |
| utc_plus_4 = datetime.utcnow() + timedelta(hours=4) |
| time_str = utc_plus_4.strftime("%H:%M:%S") |
| |
| |
| |
| log_content = f"{content} [{time_str} UTC+4]" |
| self.log(log_type, log_content, metadata={"model": self.display_name}) |
| |
| cleaned_content = self.strip_thinking_tags(content) |
| code = self.extract_code(cleaned_content) |
| if code: |
| execution_result = self.run_code(code) |
| |
| |
| for val in self.locals.values(): |
| if isinstance(val, dict) and val.get("status") == "EVALUATION_COMPLETE": |
| self.mission_complete = True |
| break |
| |
| |
| if "EVALUATION_COMPLETE" in execution_result: |
| self.mission_complete = True |
|
|
| |
| if not self.mission_complete: |
| self.step(user_input=f"Execution Result:\n{execution_result}", depth=depth+1) |
|
|
| |
| if depth == 0: |
| self.save_conversation_to_backend() |
| self.report_token_usage() |
|
|
| return |
| |
| except Exception as e: |
| is_rate_limit = "429" in str(e) or "ResourceExhausted" in str(e) |
| if is_rate_limit: |
| print(f"{Colors.RED}[WARN] Rate Limit Hit. Waiting {self.current_delay}s...{Colors.RESET}") |
| time.sleep(self.current_delay) |
| self.current_delay = min(self.current_delay * 2, 80) |
| else: |
| print(f"{Colors.RED}[ERROR] API Call failed: {e}{Colors.RESET}") |
| |
| return |
|
|