|
| 1 | +""" |
| 2 | +AgentManager class to manage Mistral AI agents. |
| 3 | +""" |
| 4 | +import os |
| 5 | +from typing import Dict, List, Optional |
| 6 | + |
| 7 | +from mistralai import Mistral |
| 8 | + |
| 9 | +from opensymbiose.agents.agent import Agent |
| 10 | + |
| 11 | + |
| 12 | +class AgentManager: |
| 13 | + """ |
| 14 | + Manages Mistral AI agents, including creation, retrieval, and handoffs. |
| 15 | + Implements the Singleton pattern to ensure only one instance exists. |
| 16 | + """ |
| 17 | + |
| 18 | + _instance = None |
| 19 | + |
| 20 | + def __new__(cls, api_key: Optional[str] = None): |
| 21 | + """ |
| 22 | + Create a new instance of AgentManager or return the existing one (Singleton pattern). |
| 23 | + |
| 24 | + Args: |
| 25 | + api_key: The Mistral API key. If not provided, it will be read from the environment. |
| 26 | + |
| 27 | + Returns: |
| 28 | + The AgentManager instance |
| 29 | + """ |
| 30 | + if cls._instance is None: |
| 31 | + cls._instance = super(AgentManager, cls).__new__(cls) |
| 32 | + cls._instance._initialized = False |
| 33 | + return cls._instance |
| 34 | + |
| 35 | + def __init__(self, api_key: Optional[str] = None): |
| 36 | + """ |
| 37 | + Initialize the AgentManager with the Mistral API key. |
| 38 | + |
| 39 | + Args: |
| 40 | + api_key: The Mistral API key. If not provided, it will be read from the environment. |
| 41 | + """ |
| 42 | + # Skip initialization if already initialized (part of Singleton pattern) |
| 43 | + if self._initialized: |
| 44 | + return |
| 45 | + |
| 46 | + self.api_key = api_key or os.environ.get("MISTRAL_API_KEY") |
| 47 | + if not self.api_key: |
| 48 | + raise ValueError( |
| 49 | + "Mistral API key is required. Provide it as an argument or set the MISTRAL_API_KEY environment variable.") |
| 50 | + |
| 51 | + self.client = Mistral(self.api_key) |
| 52 | + self.agents: Dict[str, Agent] = {} |
| 53 | + self._initialized = True |
| 54 | + |
| 55 | + def list_agents(self) -> List[Agent]: |
| 56 | + """ |
| 57 | + List all agents from the Mistral API. |
| 58 | + |
| 59 | + Returns: |
| 60 | + A list of Agent objects |
| 61 | + """ |
| 62 | + agent_list = self.client.beta.agents.list() |
| 63 | + return [Agent(agent) for agent in agent_list] |
| 64 | + |
| 65 | + def refresh_agents(self) -> None: |
| 66 | + """ |
| 67 | + Refresh the local cache of agents from the Mistral API. |
| 68 | + """ |
| 69 | + self.agents = {} |
| 70 | + agent_list = self.client.beta.agents.list() |
| 71 | + for agent_data in agent_list: |
| 72 | + agent = Agent(agent_data) |
| 73 | + self.agents[agent.name] = agent |
| 74 | + |
| 75 | + def get_agent(self, agent_name: str) -> Optional[Agent]: |
| 76 | + """ |
| 77 | + Get an agent by name. |
| 78 | + |
| 79 | + Args: |
| 80 | + agent_name: The name of the agent |
| 81 | + |
| 82 | + Returns: |
| 83 | + The Agent object if found, None otherwise |
| 84 | + """ |
| 85 | + # Refresh agents if not already loaded |
| 86 | + if not self.agents: |
| 87 | + self.refresh_agents() |
| 88 | + |
| 89 | + return self.agents.get(agent_name) |
| 90 | + |
| 91 | + def get_or_create_agent( |
| 92 | + self, |
| 93 | + agent_name: str, |
| 94 | + model: str, |
| 95 | + description: str, |
| 96 | + tools: List[Dict[str, str]] |
| 97 | + ) -> Agent: |
| 98 | + """ |
| 99 | + Get an agent by name or create it if it doesn't exist. |
| 100 | + |
| 101 | + Args: |
| 102 | + agent_name: The name of the agent |
| 103 | + model: The model to use for the agent |
| 104 | + description: The description of the agent |
| 105 | + tools: The tools to enable for the agent |
| 106 | + |
| 107 | + Returns: |
| 108 | + The Agent object |
| 109 | + """ |
| 110 | + # Refresh agents if not already loaded |
| 111 | + if not self.agents: |
| 112 | + self.refresh_agents() |
| 113 | + |
| 114 | + # Check if agent exists |
| 115 | + agent = self.agents.get(agent_name) |
| 116 | + |
| 117 | + # Create agent if it doesn't exist |
| 118 | + if not agent: |
| 119 | + agent_data = self.client.beta.agents.create( |
| 120 | + model=model, |
| 121 | + description=description, |
| 122 | + name=agent_name, |
| 123 | + tools=tools |
| 124 | + ) |
| 125 | + agent = Agent(agent_data) |
| 126 | + self.agents[agent_name] = agent |
| 127 | + print(f"Created new agent: {agent}") |
| 128 | + else: |
| 129 | + print(f"Using existing agent: {agent}") |
| 130 | + |
| 131 | + return agent |
| 132 | + |
| 133 | + def create_handoff(self, from_agent_name: str, to_agent_name: str) -> None: |
| 134 | + """ |
| 135 | + Create a handoff from one agent to another. |
| 136 | + |
| 137 | + Args: |
| 138 | + from_agent_name: The name of the agent to handoff from |
| 139 | + to_agent_name: The name of the agent to handoff to |
| 140 | + """ |
| 141 | + from_agent = self.get_agent(from_agent_name) |
| 142 | + to_agent = self.get_agent(to_agent_name) |
| 143 | + |
| 144 | + if not from_agent: |
| 145 | + raise ValueError(f"Agent '{from_agent_name}' not found") |
| 146 | + if not to_agent: |
| 147 | + raise ValueError(f"Agent '{to_agent_name}' not found") |
| 148 | + |
| 149 | + from_agent.add_handoff(to_agent.id, self.client) |
| 150 | + |
| 151 | + # Update the local cache |
| 152 | + self.agents[from_agent_name] = from_agent |
0 commit comments