Spaces:
Runtime error
Runtime error
| 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(task_prompt="", claude_api_key=os.getenv("CLAUDE_API_KEY", "")) | |
| 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 | |
| temperature=0.2 # Lower temperature for more focused responses | |
| ) | |
| print("π€ ManagerAgent initialized with ReActAgent and enhanced workflow (temperature=0.2).") | |
| def send_update(self, message: str) -> None: | |
| """ | |
| Send an update message to the user about the agent's progress. | |
| """ | |
| if not any(emoji in message[:2] for emoji in ["π’", "π", "β ", "β", "β οΈ", "π¬", "π", "π", "β¨"]): | |
| message = f"π’ {message}" | |
| print(f"π£ AGENT: ManagerAgent.send_update CALLED with message: {message}") # DEBUG | |
| print(f"π£ AGENT: self.update_callback is: {self.update_callback}") # DEBUG | |
| if self.update_callback: | |
| try: | |
| self.update_callback(message) # This should call update_status_callback in app.py | |
| print(f"π£ AGENT: Callback invoked successfully.") # DEBUG | |
| except Exception as e: | |
| print(f"β AGENT: Error sending update via callback: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| else: | |
| print("π£ AGENT: No update_callback configured for ManagerAgent.") # DEBUG | |
| # Return a string confirmation, as ReAct tools often expect a string output | |
| return f"Update sent: {message}" # MODIFICATION: Return a string | |
| 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: an information request, a tool request, task automation, research, or creative work. | |
| * Decide whether to answer the request directly, create a new tool, or perform web research. | |
| * If you decide to answer directly, provide your answer right away. | |
| * If you decide to perform web research, use the `perform_web_research` tool with specific queries. Inform the user you are starting research before taking this action. | |
| * If the task requires more than simple text generation or basic web research, proceed to check for existing tools. | |
| * Use `send_user_update` to inform the user about what you're doing and your progress if you don't answer directly. | |
| * Do not apologize for not being able to answer the prompt until you have attempted all subsequent steps (EXISTING TOOLS CHECK, TOOL ANALYSIS PHASE, RESEARCH PHASE, TOOL CREATION PHASE). If all fail, 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 (check their 'state'), use `deploy_tool` to activate them. | |
| * Once tools are active and deployed, use `use_registry_tool` to execute them with the necessary inputs. | |
| * 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`. | |
| * Provide the `brainstorm_tools` function with the `user_task` and the `available_tools` (a comma-separated string of tool names from `get_available_tools`). | |
| * If there are no tools available, provide "none" as the input for `available_tools` to the `brainstorm_tools` function. | |
| * 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 or tool creation): | |
| * Use the `perform_web_research` tool for all web-based information gathering. | |
| * For general information or in-depth research on a topic, provide a clear query to `perform_web_research`. | |
| * If you are looking for open-source code, libraries, or technical solutions (including from GitHub), instruct `perform_web_research` in your query to focus on finding code examples or repositories. For instance: "perform_web_research: Find Python code snippets for parsing CSV files from GitHub." | |
| * Send updates to the user with `send_user_update` about your research progress. | |
| 5. **TOOL CREATION PHASE** (if no existing tool works or can be adapted): | |
| * First, use `brainstorm_tools` to define the specifications of the new tool needed. | |
| * Next, use `perform_web_research` to find existing open-source solutions, code examples, or libraries that can help build the tool. Be specific in your query to `perform_web_research` about looking for implementation details. | |
| * Then, use `generate_mcp_script` to create the Python code and environment script for the tool, using the specification from `brainstorm_tools` and insights from your research. | |
| * Finally, use `execute_and_register_mcp` to test the new tool in a safe environment and, if successful, register it in your tool registry. | |
| * Keep the user informed of your progress with `send_user_update`. | |
| 6. **EXECUTION PHASE** (after a tool is ready, either existing or newly created): | |
| * Ensure the required tool is deployed using `deploy_tool` if it's not already active. | |
| * Use `use_registry_tool` to run the active tool with the appropriate inputs. | |
| * 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 via `perform_web_research`. | |
| * Keep the user informed of your progress with regular updates using `send_user_update`. | |
| **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 'state' from `get_available_tools` to determine if they are active ('activated' or similar) or inactive. | |
| * Use `deploy_tool` to activate any inactive tools before running them. Tools must be deployed before they can be executed by `use_registry_tool`. | |
| **Response Style**: | |
| * Structure your responses clearly with headers where appropriate. | |
| * 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." | |
| ) | |
| ) | |
| # 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}" | |
| 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} | |