import uuid import os from dotenv import load_dotenv from typing import Optional, Dict, Any, List, Generator, Callable from models import TaskPrompt, MCPToolSpec, MCPExecutionResult from components import ( WebAgent, ScriptGenerator, CodeRunner, Registry, Brainstormer, ) from llama_index.core.llms import LLM from llama_index.core.agent import ReActAgent from llama_index.core.tools import FunctionTool # Load environment variables from .env file load_dotenv() class ManagerAgent: """ The central orchestrator of the Alita agent - Revised for Gradio integration. Workflow: 1. Analyze user prompt to understand the request 2. Check existing tools in registry first 3. If research needed, formulate search queries and use WebAgent 4. If tool needed but not found, brainstorm new tool requirements 5. Search for open source tools/solutions via WebAgent 6. Create implementation plan via Brainstormer 7. Return comprehensive response """ def __init__(self, llm: LLM, max_iterations: int = 10000000, update_callback: Optional[Callable[[str], None]] = None): self.llm = llm self.registry = Registry() self.web_agent = WebAgent(llm=llm, max_research_iterations=10000000) self.code_runner = CodeRunner() self.brainstormer = Brainstormer(model_name="claude-sonnet-4-0") self.script_generator = ScriptGenerator(llm=self.llm) self.max_iterations = max_iterations self.update_callback = update_callback # Define the tools available to the internal LlamaIndex Agent self._agent_tools = self._define_agent_tools() # Initialize the internal LlamaIndex ReAct Agent with improved system prompt self.agent = ReActAgent.from_tools( tools=self._agent_tools, llm=self.llm, verbose=True, system_prompt=self._get_system_prompt(), max_iterations=self.max_iterations # Use the configurable max_iterations parameter ) print("šŸ¤– ManagerAgent initialized with ReActAgent and enhanced workflow.") def send_update(self, message: str) -> None: """ Send an update message to the user about the agent's progress. Args: message: The update message to send Returns: None """ print(f"šŸ“¢ Update: {message}") # If a callback function is provided, use it to send the update to the user if self.update_callback: try: self.update_callback(message) except Exception as e: print(f"Error sending update via callback: {e}") def _get_system_prompt(self) -> str: """Enhanced system prompt for better workflow orchestration""" return """You are ALITA, an advanced generalist agent. You are here to help people with their requests. you can do many tasks like research, tool creation, automation, analysis, and much more. What is unique about you is that you can create tools to help people with their requests, even if they are not in your capabilities. Your primary workflow for ANY user request: 1. **ANALYZE PHASE**: - Understand the user's request deeply - Identify if it's: information request, tool request, task automation, research, or creative work. - here you decide wether to answer the request or to create a tool to answer the request, or to search the web only. - if you decide to answer directly, give your answer right away. - if you decide to search the web, use 'web_search' with specific queries. give a first answer to the user saying you are searching the web, then take the action of 'web_search'. - if you there is a thing that needs something more than a text generation or search, then look for existing tool here in the next steps. - Use 'send_user_update' to inform the user about what you're doing and your progress, if you didnt answer direclty to the prompt. - Do not apologize quickly for not being able to answer the prompt, until you do the next steps: EXISTING TOOLS CHECK, TOOL ANALYSIS PHASE, RESEARCH PHASE, TOOL CREATION PHASE. if not successful then apologize. 2. **EXISTING TOOLS CHECK**: - ALWAYS first use 'get_available_tools' to list all tools in your registry - If suitable tools exist but are not deployed, use 'deploy_tool' to activate them - Once tools are active, use 'run_registered_mcp' to execute them OR use 'use_registry_tool' for direct invocation - Keep the user informed of your progress with 'send_user_update' 3. **TOOL ANALYSIS PHASE**: - If you need to determine whether existing tools are sufficient or new tools are needed, use 'brainstorm_tools' - This will analyze the user request against available tools and recommend which tools to use or what new tools to create - Follow the recommendations from the brainstorming phase - Send an update to the user with 'send_user_update' about your findings 4. **RESEARCH PHASE** (if needed): - For information requests: use 'web_search' with specific queries - For in-depth research topics: use 'perform_web_research' for comprehensive autonomous research - For technical solutions: use 'github_search' for open source tools - Use 'retrieve_url_content' to get detailed information from promising results - Send updates to the user with 'send_user_update' about your research progress 5. **TOOL CREATION PHASE** (if no existing tool works): - Use 'brainstorm_tools' to identify what kind of tool is needed - Use 'web_search' and 'github_search' to find existing open source solutions - Use 'generate_mcp_script' to create implementation based on research - Use 'execute_and_register_mcp' to validate and register the new tool - Keep the user informed of your progress with 'send_user_update' 6. **EXECUTION PHASE**: - Use appropriate registered tools via 'run_registered_mcp' or 'use_registry_tool' - Provide comprehensive results with explanations - Send a final update to the user with 'send_user_update' about the results **Key Principles**: - Be proactive in tool discovery and creation - Always search for existing solutions before creating new ones - Provide detailed explanations of your reasoning process - Focus on practical, actionable results - Leverage open source resources extensively - Keep the user informed of your progress with regular updates **Tool Management Capabilities**: - Use 'get_available_tools' to see all tools in your registry - Use 'brainstorm_tools' to analyze if existing tools are sufficient or new ones are needed - Check tool states to determine if they are active ('activated') or inactive ('deactivated') - Use 'deploy_tool' to activate any inactive tools before running them - Remember that tools must be deployed before they can be executed - Use 'use_registry_tool' for direct tool invocation with automatic deployment **Tool Usage Options**: - 'run_registered_mcp': Traditional method that requires separate deployment and execution steps - 'use_registry_tool': Streamlined method that handles deployment automatically and provides direct invocation **Research Capabilities**: - For simple information needs, use 'web_search' for quick answers - For complex research topics requiring in-depth analysis, use 'perform_web_research' - The 'perform_web_research' tool conducts autonomous research across multiple sources and synthesizes findings **Response Style**: - Structure your responses clearly with headers - Explain what you're doing and why - Provide context and next steps - Be conversational but informative - Use 'send_user_update' to keep the user informed throughout the process """ def _define_agent_tools(self) -> List[FunctionTool]: """Enhanced tool definition with better descriptions""" tools = [] # User update tool tools.append( FunctionTool.from_defaults( self.send_update, name="send_user_update", description="Send an update message to the user about your current progress or actions. Takes 'message' (string) containing the update information. Use this tool frequently to keep the user informed about what you're doing." ) ) # Add research tool tools.append( FunctionTool.from_defaults( self.research, name="perform_web_research", description="Performs comprehensive web research on a given topic. Takes 'query' (string) containing the research question or topic to investigate. Returns a detailed research report with findings and sources." ) ) # Get all available tools tools.append( FunctionTool.from_defaults( self.get_available_tools, name="get_available_tools", description="Get a list of all tools currently available in the registry. Returns a list of tool specifications with names, descriptions, and states." ) ) # Use a registered tool tools.append( FunctionTool.from_defaults( self.use_registry_tool, name="use_registry_tool", description="Use a registered tool directly by invoking its endpoint. Takes 'tool_name' (string) and any additional arguments required by the tool. Automatically deploys the tool if needed. Returns the response from the tool." ) ) # Tool brainstorming tools.append( FunctionTool.from_defaults( self.brainstorm_tools, name="brainstorm_tools", description="Analyze the user request against available tools to determine if existing tools are sufficient or new tools are needed. Takes 'user_task' (string) containing the user's request and optionally 'available_tools' (string) with comma-separated tool names. Returns recommendations on which tools to use or what new tools to create." ) ) # Deploy a specific tool tools.append( FunctionTool.from_defaults( self.deploy_tool, name="deploy_tool", description="Deploy and activate a specific tool from the registry. Takes 'tool_name' (string) containing the name of the tool to deploy. Returns the URL of the deployed tool if successful, or an error message if deployment fails." ) ) # # Enhanced execution and registration tool # tools.append( # FunctionTool.from_defaults( # self._run_and_register_mcp, # name="execute_and_register_mcp", # description="Execute a generated MCP script in an isolated environment and register it if successful. Takes 'spec' (MCPToolSpec as dict), 'python_script' (string), 'env_script' (string), and optional 'input_data' (dict). Returns execution result." # ) # ) # # Enhanced registered tool execution # tools.append( # FunctionTool.from_defaults( # self._run_registered_mcp, # name="run_registered_mcp", # description="Execute a previously registered MCP tool. Takes 'tool_name' (string) and optional 'input_data' (dict). Returns execution result with output data." # ) # ) # Add analysis tool for better decision making tools.append( FunctionTool.from_defaults( self._analyze_user_request, name="analyze_user_request", description="Analyze user request to determine the best approach (research, existing tool, new tool creation). Takes 'user_message' (string). Returns analysis with recommended actions." ) ) return tools def _analyze_user_request(self, user_message: str) -> Dict[str, Any]: """Analyze user request to determine optimal workflow path""" analysis = { "request_type": "unknown", "complexity": "medium", "requires_research": False, "requires_tools": False, "suggested_approach": [], "key_concepts": [] } message_lower = user_message.lower() # Look for comprehensive research indicators research_terms = ["comprehensive", "thorough", "in-depth", "detailed", "extensive", "research", "investigate", "analyze", "report", "study"] # Determine request type if any(word in message_lower for word in research_terms): analysis["request_type"] = "deep_research" analysis["requires_research"] = True analysis["complexity"] = "high" analysis["suggested_approach"].append("research") elif any(word in message_lower for word in ["recherche", "search", "find", "lookup", "information", "what is", "explain"]): analysis["request_type"] = "information_request" analysis["requires_research"] = True analysis["suggested_approach"].append("web_search") elif any(word in message_lower for word in ["outil", "tool", "script", "automatise", "automate", "create", "build"]): analysis["request_type"] = "tool_request" analysis["requires_tools"] = True analysis["suggested_approach"].extend(["find_existing_tools", "brainstorm_if_needed"]) elif any(word in message_lower for word in ["analyse", "analyze", "process", "calculate", "compute"]): analysis["request_type"] = "analysis_task" analysis["requires_tools"] = True analysis["suggested_approach"].extend(["find_existing_tools", "research_methods"]) elif any(word in message_lower for word in ["tendance", "trend", "market", "news", "current"]): analysis["request_type"] = "research_task" analysis["requires_research"] = True analysis["complexity"] = "high" analysis["suggested_approach"].extend(["web_search", "github_search"]) # Extract key concepts for better tool matching concepts = [] tech_keywords = ["python", "javascript", "api", "database", "csv", "json", "web", "scraping", "ml", "ai"] for keyword in tech_keywords: if keyword in message_lower: concepts.append(keyword) analysis["key_concepts"] = concepts return analysis def _run_and_register_mcp(self, spec: Dict[str, Any], python_script: str, env_script: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """Enhanced MCP execution and registration with better error handling""" print(f"šŸ”§ ManagerAgent: Executing and registering MCP: {spec.get('name', 'Unnamed Tool')}") try: mcp_spec_obj = MCPToolSpec.from_dict(spec) env_name_suffix = mcp_spec_obj.name.lower().replace(' ', '-')[:10] env_name = f"alita-{env_name_suffix}-{uuid.uuid4().hex[:8]}" print(f"šŸ”„ Setting up environment: {env_name}") env_success = self.code_runner.setup_environment(env_script, env_name) if not env_success: result = MCPExecutionResult( success=False, error_message=f"Environment setup failed for '{env_name}'. Check dependencies in env_script." ) return result.to_dict() print(f"ā–¶ļø Executing script in environment: {env_name}") execution_result = self.code_runner.execute(python_script, env_name, input_data) if execution_result.success: print(f"āœ… Script execution successful. Registering tool: {mcp_spec_obj.name}") mcp_spec_obj.validated_script = python_script mcp_spec_obj.environment_script = env_script self.registry.register_tool(mcp_spec_obj) print(f"šŸŽÆ Tool '{mcp_spec_obj.name}' successfully registered in registry") # Add success message to result execution_result.output_data = execution_result.output_data or {} execution_result.output_data["registration_status"] = "Successfully registered" else: print(f"āŒ Script execution failed for '{mcp_spec_obj.name}': {execution_result.error_message}") # Always cleanup after validation self.code_runner.cleanup_environment(env_name) return execution_result.to_dict() except Exception as e: error_msg = f"Unexpected error in MCP execution: {str(e)}" print(f"🚨 {error_msg}") # Cleanup on error try: if 'env_name' in locals(): self.code_runner.cleanup_environment(env_name) except: pass return MCPExecutionResult(success=False, error_message=error_msg).to_dict() def _run_registered_mcp(self, tool_name: str, input_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """Enhanced registered tool execution with better logging""" print(f"šŸŽÆ ManagerAgent: Running registered tool: {tool_name}") spec = self.registry.get_tool(tool_name) if not spec: error_msg = f"Tool '{tool_name}' not found in registry. Available tools: {list(self.registry.tools.keys())}" print(f"āŒ {error_msg}") return MCPExecutionResult(success=False, error_message=error_msg).to_dict() if not spec.validated_script or not spec.environment_script: error_msg = f"Tool '{tool_name}' missing validated script or environment configuration" print(f"āŒ {error_msg}") return MCPExecutionResult(success=False, error_message=error_msg).to_dict() # Create fresh environment for execution env_name_suffix = spec.name.lower().replace(' ', '-')[:10] env_name = f"alita-run-{env_name_suffix}-{uuid.uuid4().hex[:8]}" try: print(f"šŸ”„ Setting up execution environment: {env_name}") env_success = self.code_runner.setup_environment(spec.environment_script, env_name) if not env_success: return MCPExecutionResult( success=False, error_message=f"Failed to setup environment for tool '{tool_name}'" ).to_dict() print(f"ā–¶ļø Executing registered tool: {tool_name}") execution_result = self.code_runner.execute(spec.validated_script, env_name, input_data) print(f"{'āœ…' if execution_result.success else 'āŒ'} Tool execution completed. Success: {execution_result.success}") return execution_result.to_dict() except Exception as e: error_msg = f"Error executing registered tool '{tool_name}': {str(e)}" print(f"🚨 {error_msg}") return MCPExecutionResult(success=False, error_message=error_msg).to_dict() finally: # Always cleanup try: self.code_runner.cleanup_environment(env_name) except: pass def run_task(self, prompt: TaskPrompt) -> str: """ Enhanced task execution with detailed logging and structured workflow Optimized for Gradio integration with comprehensive responses """ print(f"\n{'='*60}") print(f"šŸš€ ALITA ManagerAgent: Starting task execution") print(f"šŸ“ User prompt: {prompt.text[:100]}{'...' if len(prompt.text) > 100 else ''}") print(f"{'='*60}") # Send initial update to the user self.send_update(f"Starting to process your request: '{prompt.text[:50]}{'...' if len(prompt.text) > 50 else ''}'") try: # Use the internal ReAct agent to handle the complete workflow print("🧠 Engaging ReAct Agent for intelligent task orchestration...") # The ReAct agent will use its tools to: # 1. Analyze the request # 2. Search existing tools # 3. Perform web research if needed # 4. Brainstorm solutions # 5. Create/execute tools as necessary # 6. Provide comprehensive response response = self.agent.chat(prompt.text) print("āœ… Task execution completed successfully") print(f"{'='*60}\n") # Send final update to the user self.send_update("Task completed successfully! Here's your response.") # Format response for better Gradio presentation formatted_response = self._format_response_for_gradio(response.response) return formatted_response except Exception as e: error_msg = f"🚨 ManagerAgent encountered an error during task execution:\n\n**Error Details:**\n{str(e)}\n\n**Next Steps:**\n- Check your API key and network connection\n- Verify all components are properly initialized\n- Try a simpler request to test basic functionality" print(f"āŒ Task execution failed: {e}") print(f"{'='*60}\n") # Send error update to the user self.send_update(f"An error occurred while processing your request: {str(e)}") return error_msg def _format_response_for_gradio(self, response: str) -> str: """Format the agent response for better presentation in Gradio""" # Add header if not present if not response.startswith("##") and not response.startswith("#"): response = f"## šŸ¤– {response}" # Add footer with capabilities reminder (occasionally) if "capabilities" not in response.lower(): footer = "\n\n---\nšŸ’” **Tip**: I can help you with research, tool creation, automation, analysis, and much more. Just ask!" response += footer return response def get_registry_status(self) -> Dict[str, Any]: """Get current status of the tool registry""" return { "total_tools": len(self.registry.tools), "tool_names": list(self.registry.tools.keys()), "registry_ready": len(self.registry.tools) > 0 } def reset_registry(self): """Reset the tool registry (useful for testing)""" self.registry = Registry() print("šŸ”„ Tool registry has been reset") def __str__(self): return f"ManagerAgent(llm={type(self.llm).__name__}, tools_registered={len(self.registry.tools)})" def research(self, query: str, max_iterations: int = None, verbose: bool = None) -> str: """ Performs autonomous web research on the given query using the WebAgent's research function. Args: query: The research question or topic max_iterations: Optional override for the maximum number of research iterations verbose: Optional override for verbose mode Returns: A comprehensive textual report based on web research """ print(f"\n{'='*60}") print(f"🌐 ALITA ManagerAgent: Starting web research") print(f"šŸ“ Research query: {query[:100]}{'...' if len(query) > 100 else ''}") print(f"{'='*60}") try: # Configure WebAgent for this research session if max_iterations is not None: self.web_agent.max_research_iterations = max_iterations if verbose is not None: self.web_agent.verbose = verbose # Perform the research print("šŸ” Initiating autonomous web research. This may take some time... here is the query: ", query) report = self.web_agent.research(query) print("šŸ” here is the report: ", report) print("āœ… Research completed successfully") print(f"{'='*60}\n") return report except Exception as e: error_msg = f"🚨 Error during web research: {str(e)}" print(f"āŒ Research failed: {e}") print(f"{'='*60}\n") import traceback print(traceback.format_exc()) return error_msg def get_available_tools(self) -> List[Dict[str, Any]]: """ Get a list of all tools currently available in the registry. Returns: List of dictionaries containing tool information (name, description, state) """ print("šŸ“‹ ManagerAgent: Retrieving list of all available tools") tools = self.registry.list_tools() # Format the tools for easier consumption by the agent formatted_tools = [] for tool in tools: formatted_tools.append({ "name": tool.name, "description": tool.description, "state": getattr(tool, "state", "unknown"), "input_schema": tool.input_schema if hasattr(tool, "input_schema") else {}, "output_schema": tool.output_schema if hasattr(tool, "output_schema") else {} }) print(f"šŸ” Found {len(formatted_tools)} tools in registry") return formatted_tools def deploy_tool(self, tool_name: str) -> Dict[str, Any]: """ Deploy and activate a specific tool from the registry. Args: tool_name: Name of the tool to deploy Returns: Dictionary with deployment status and URL (if successful) """ print(f"šŸš€ ManagerAgent: Deploying tool '{tool_name}'") # Check if tool exists in registry if not self.registry.get_tool(tool_name): error_msg = f"Tool '{tool_name}' not found in registry" print(f"āŒ {error_msg}") return {"success": False, "error": error_msg} # Attempt to deploy the tool try: url = self.registry.deploy_tool(tool_name) if url: print(f"āœ… Successfully deployed tool '{tool_name}' at {url}") return { "success": True, "tool_name": tool_name, "url": url, "message": f"Tool '{tool_name}' successfully deployed" } else: error_msg = f"Failed to deploy tool '{tool_name}'" print(f"āŒ {error_msg}") return {"success": False, "error": error_msg} except Exception as e: error_msg = f"Error deploying tool '{tool_name}': {str(e)}" print(f"🚨 {error_msg}") return {"success": False, "error": error_msg} def brainstorm_tools(self, user_task: str, available_tools: str = "") -> Dict[str, Any]: """ Use the Brainstormer to analyze if existing tools are sufficient or new tools are needed. Args: user_task: The user's request or task available_tools: Optional comma-separated list of available tool names Returns: Dictionary with tool recommendations or specifications for new tools """ print(f"🧠 ManagerAgent: Brainstorming tools for task: {user_task[:100]}{'...' if len(user_task) > 100 else ''}") # If available_tools is not provided, get them from the registry if not available_tools: tools = self.get_available_tools() available_tools = ", ".join([tool["name"] for tool in tools]) try: # Call the brainstormer to analyze the task and available tools result = self.brainstormer.generate_mcp_specs_to_fulfill_user_task( task=user_task, tools_list=available_tools ) if isinstance(result, dict) and "error" in result: print(f"āŒ Brainstorming failed: {result['error']}") return { "success": False, "error": result["error"], "recommendations": "Unable to analyze tools for this task." } print(f"āœ… Brainstorming complete. Found {len(result)} tool recommendations.") # Format the result for better consumption by the agent return { "success": True, "recommendations": result, "summary": f"Analysis complete. Found {len(result)} tool recommendations." } except Exception as e: error_msg = f"Error during tool brainstorming: {str(e)}" print(f"🚨 {error_msg}") return { "success": False, "error": error_msg, "recommendations": "Unable to analyze tools due to an error." } def use_registry_tool(self, tool_name: str, *args, **kwargs) -> Dict[str, Any]: """ Use a registered tool directly by invoking its endpoint. This method utilizes the Registry's use_tool method to invoke a registered tool. It handles tool deployment if needed and provides proper error handling and user feedback. Args: tool_name: Name of the tool to use *args: Positional arguments to pass to the tool **kwargs: Keyword arguments to pass to the tool Returns: The response from the tool as a Python object """ try: # Send update to user self.send_update(f"Using tool: {tool_name}") # Check if tool exists in registry if not self.registry.get_tool(tool_name): error_msg = f"Tool '{tool_name}' not found in registry" self.send_update(error_msg) return {"error": error_msg, "success": False} # Use the tool via Registry's use_tool method self.send_update(f"Executing tool: {tool_name}") result = self.registry.use_tool(tool_name, *args, **kwargs) # Send success update self.send_update(f"Tool '{tool_name}' executed successfully") # Return result with success flag if isinstance(result, dict): result["success"] = True return result else: return {"result": result, "success": True} except ValueError as e: # Handle expected errors (tool not found, deployment failed) error_msg = str(e) self.send_update(f"Error: {error_msg}") return {"error": error_msg, "success": False} except Exception as e: # Handle unexpected errors error_msg = f"Unexpected error using tool '{tool_name}': {str(e)}" self.send_update(f"Error: {error_msg}") return {"error": error_msg, "success": False}