11"""
22AgentManager class to manage Mistral AI agents.
33"""
4+
45import os
56from typing import Dict , List , Optional
67
@@ -20,10 +21,10 @@ class AgentManager:
2021 def __new__ (cls , api_key : Optional [str ] = None ):
2122 """
2223 Create a new instance of AgentManager or return the existing one (Singleton pattern).
23-
24+
2425 Args:
2526 api_key: The Mistral API key. If not provided, it will be read from the environment.
26-
27+
2728 Returns:
2829 The AgentManager instance
2930 """
@@ -35,7 +36,7 @@ def __new__(cls, api_key: Optional[str] = None):
3536 def __init__ (self , api_key : Optional [str ] = None ):
3637 """
3738 Initialize the AgentManager with the Mistral API key.
38-
39+
3940 Args:
4041 api_key: The Mistral API key. If not provided, it will be read from the environment.
4142 """
@@ -46,81 +47,75 @@ def __init__(self, api_key: Optional[str] = None):
4647 self .api_key = api_key or os .environ .get ("MISTRAL_API_KEY" )
4748 if not self .api_key :
4849 raise ValueError (
49- "Mistral API key is required. Provide it as an argument or set the MISTRAL_API_KEY environment variable." )
50+ "Mistral API key is required. Provide it as an argument or set the MISTRAL_API_KEY environment variable."
51+ )
5052
5153 self .client = Mistral (self .api_key )
5254 self .agents : Dict [str , Agent ] = {}
5355 self ._initialized = True
5456
55- def list_agents (self ) -> List [Agent ]:
57+ async def list_agents (self ) -> List [Agent ]:
5658 """
5759 List all agents from the Mistral API.
58-
60+
5961 Returns:
6062 A list of Agent objects
6163 """
62- agent_list = self .client .beta .agents .list ()
64+ agent_list = await self .client .beta .agents .list_async ()
6365 return [Agent (agent ) for agent in agent_list ]
6466
65- def refresh_agents (self ) -> None :
67+ async def refresh_agents (self ) -> None :
6668 """
6769 Refresh the local cache of agents from the Mistral API.
6870 """
6971 self .agents = {}
70- agent_list = self .client .beta .agents .list ()
72+ agent_list = await self .client .beta .agents .list_async ()
7173 for agent_data in agent_list :
7274 agent = Agent (agent_data )
7375 self .agents [agent .name ] = agent
7476
75- def get_agent (self , agent_name : str ) -> Optional [Agent ]:
77+ async def get_agent (self , agent_name : str ) -> Optional [Agent ]:
7678 """
7779 Get an agent by name.
78-
80+
7981 Args:
8082 agent_name: The name of the agent
81-
83+
8284 Returns:
8385 The Agent object if found, None otherwise
8486 """
8587 # Refresh agents if not already loaded
8688 if not self .agents :
87- self .refresh_agents ()
89+ await self .refresh_agents ()
8890
8991 return self .agents .get (agent_name )
9092
91- def get_or_create_agent (
92- self ,
93- agent_name : str ,
94- model : str ,
95- description : str ,
96- tools : List [Dict [str , str ]]
93+ async def get_or_create_agent (
94+ self , agent_name : str , model : str , description : str , tools : List [Dict [str , str ]]
9795 ) -> Agent :
9896 """
9997 Get an agent by name or create it if it doesn't exist.
100-
98+
10199 Args:
102100 agent_name: The name of the agent
103101 model: The model to use for the agent
104102 description: The description of the agent
105103 tools: The tools to enable for the agent
106-
104+
107105 Returns:
108106 The Agent object
109107 """
110108 # Refresh agents if not already loaded
111109 if not self .agents :
112- self .refresh_agents ()
110+ await self .refresh_agents ()
113111
114112 # Check if agent exists
115113 agent = self .agents .get (agent_name )
116114
117115 # Create agent if it doesn't exist
118116 if not agent :
119- agent_data = self .client .beta .agents .create (
120- model = model ,
121- description = description ,
122- name = agent_name ,
123- tools = tools
117+ agent_data = await self .client .beta .agents .create_async (
118+ model = model , description = description , name = agent_name , tools = tools
124119 )
125120 agent = Agent (agent_data )
126121 self .agents [agent_name ] = agent
@@ -130,23 +125,23 @@ def get_or_create_agent(
130125
131126 return agent
132127
133- def create_handoff (self , from_agent_name : str , to_agent_name : str ) -> None :
128+ async def create_handoff (self , from_agent_name : str , to_agent_name : str ) -> None :
134129 """
135130 Create a handoff from one agent to another.
136-
131+
137132 Args:
138133 from_agent_name: The name of the agent to handoff from
139134 to_agent_name: The name of the agent to handoff to
140135 """
141- from_agent = self .get_agent (from_agent_name )
142- to_agent = self .get_agent (to_agent_name )
136+ from_agent = await self .get_agent (from_agent_name )
137+ to_agent = await self .get_agent (to_agent_name )
143138
144139 if not from_agent :
145140 raise ValueError (f"Agent '{ from_agent_name } ' not found" )
146141 if not to_agent :
147142 raise ValueError (f"Agent '{ to_agent_name } ' not found" )
148143
149- from_agent .add_handoff (to_agent .id , self .client )
144+ await from_agent .add_handoff (to_agent .id , self .client )
150145
151146 # Update the local cache
152147 self .agents [from_agent_name ] = from_agent
0 commit comments