1515from azure .ai .agents .aio import AgentsClient
1616from azure .identity .aio import DefaultAzureCredential
1717from common .database .database_base import DatabaseBase
18- from common .models .messages_af import CurrentTeamAgent , TeamConfiguration
18+ from common .models .messages_af import TeamConfiguration
1919from common .utils .utils_agents import (
2020 generate_assistant_id ,
21- get_database_team_agent_id ,
2221)
2322from v4 .common .services .team_service import TeamService
2423from v4 .config .agent_registry import agent_registry
@@ -148,13 +147,12 @@ async def _after_open(self) -> None:
148147 """Subclasses must build self._agent here."""
149148 raise NotImplementedError
150149
151- def get_chat_client (self , chat_client ) -> AzureAIClient :
150+ def get_chat_client (self ) -> AzureAIClient :
152151 """Return the underlying ChatClientProtocol (AzureAIClient).
153152
154- Uses agent_name with use_latest_version=True to get the latest agent version
153+ Uses agent_name with use_latest_version=True to get the latest agent version.
154+ Agent reuse is handled automatically by the SDK via agent_name.
155155 """
156- if chat_client :
157- return chat_client
158156 if (
159157 self ._agent
160158 and self ._agent .chat_client
@@ -173,176 +171,16 @@ def get_chat_client(self, chat_client) -> AzureAIClient:
173171 )
174172 return chat_client
175173
176- async def resolve_agent_id (self , agent_id : str ) -> Optional [str ]:
177- """Resolve agent ID via Projects SDK first (for RAI agents), fallback to AgentsClient.
178-
179- Args:
180- agent_id: The agent ID to resolve
181-
182- Returns:
183- The resolved agent ID if found, None otherwise
184- """
185- # Try Projects SDK first (RAI agents were created via project_client)
186- try :
187- if self .project_client :
188- agent = await self .project_client .agents .get_agent (agent_id )
189- if agent and agent .id :
190- self .logger .info (
191- "RAI.AgentReuseSuccess: Resolved agent via Projects SDK (id=%s)" ,
192- agent .id ,
193- )
194- return agent .id
195- except Exception as ex :
196- self .logger .warning (
197- "RAI.AgentReuseMiss: Projects SDK get_agent failed (reason=ProjectsGetFailed, id=%s): %s" ,
198- agent_id ,
199- ex ,
200- )
201-
202- # Fallback via AgentsClient (endpoint)
203- try :
204- if self .client :
205- agent = await self .client .get_agent (agent_id = agent_id )
206- if agent and agent .id :
207- self .logger .info (
208- "RAI.AgentReuseSuccess: Resolved agent via AgentsClient (id=%s)" ,
209- agent .id ,
210- )
211- return agent .id
212- except Exception as ex :
213- self .logger .warning (
214- "RAI.AgentReuseMiss: AgentsClient get_agent failed (reason=EndpointGetFailed, id=%s): %s" ,
215- agent_id ,
216- ex ,
217- )
218-
219- self .logger .error (
220- "RAI.AgentReuseMiss: Agent ID not resolvable via any client (reason=ClientMismatch, id=%s)" ,
221- agent_id ,
222- )
223- return None
224-
225- def get_agent_id (self , chat_client ) -> str :
226- """Return the underlying agent ID or generate a new one.
174+ def get_agent_id (self ) -> str :
175+ """Generate a local agent ID for the ChatAgent wrapper.
227176
228- Note: The new AzureAIClient doesn't expose agent_id directly .
229- We generate a new ID if not available .
177+ The new AzureAIClient identifies agents by name (not ID) on the server side .
178+ This ID is only used locally for the ChatAgent wrapper instance .
230179 """
231- # Generate a new agent ID since AzureAIClient doesn't expose agent_id
232180 id = generate_assistant_id ()
233181 self .logger .info ("Generated new agent ID: %s" , id )
234182 return id
235183
236- async def get_database_team_agent (self ) -> Optional [AzureAIClient ]:
237- """Retrieve existing team agent from database, if any.
238-
239- NOTE: Agent reuse is currently DISABLED to ensure fresh agents are created
240- with the correct Azure AI Search configuration.
241- This prevents issues with stale agents that may not have the search tool configured.
242-
243- To re-enable agent reuse, set ENABLE_AGENT_REUSE=true in environment.
244- """
245- import os
246-
247- # DISABLED: Always create fresh agents to ensure Azure AI Search tool is configured
248- enable_reuse = os .environ .get ("ENABLE_AGENT_REUSE" , "false" ).lower () == "true"
249- if not enable_reuse :
250- self .logger .info (
251- "Agent reuse DISABLED: Creating fresh agent with search tools (agent_name=%s)" ,
252- self .agent_name ,
253- )
254- return None
255-
256- chat_client = None
257- try :
258- agent_id = await get_database_team_agent_id (
259- self .memory_store , self .team_config , self .agent_name
260- )
261-
262- if not agent_id :
263- self .logger .info (
264- "RAI reuse: no stored agent id (agent_name=%s)" , self .agent_name
265- )
266- return None
267-
268- # Use resolve_agent_id to try Projects SDK first, then AgentsClient
269- resolved = await self .resolve_agent_id (agent_id )
270- if not resolved :
271- self .logger .error (
272- "RAI.AgentReuseMiss: stored id %s not resolvable (agent_name=%s)" ,
273- agent_id ,
274- self .agent_name ,
275- )
276- return None
277-
278- # Create client with resolved ID
279- if self .agent_name == "RAIAgent" and self .project_client :
280- chat_client = AzureAIClient (
281- project_endpoint = self .project_endpoint ,
282- agent_id = resolved ,
283- credential = self .creds ,
284- )
285- self .logger .info (
286- "RAI.AgentReuseSuccess: Created AzureAIClient (id=%s)" ,
287- resolved ,
288- )
289- else :
290- chat_client = AzureAIClient (
291- project_endpoint = self .project_endpoint ,
292- agent_id = resolved ,
293- model_deployment_name = self .model_deployment_name ,
294- credential = self .creds ,
295- )
296- self .logger .info (
297- "Created AzureAIClient via endpoint (id=%s)" , resolved
298- )
299-
300- except Exception as ex :
301- self .logger .error (
302- "Failed to initialize Get database team agent (agent_name=%s): %s" ,
303- self .agent_name ,
304- ex ,
305- )
306- return chat_client
307-
308- async def save_database_team_agent (self ) -> None :
309- """Save current team agent to database (only if truly new or changed)."""
310- try :
311- if self ._agent is None or self ._agent .id is None :
312- self .logger .error ("Cannot save database team agent: agent or agent_id is None" )
313- return
314-
315- # Use the agent ID from ChatAgent (set during creation)
316- agent_id = self ._agent .id
317-
318- # Check if stored ID matches current ID
319- stored_id = await get_database_team_agent_id (
320- self .memory_store , self .team_config , self .agent_name
321- )
322- if stored_id == agent_id :
323- self .logger .info (
324- "RAI reuse: id unchanged (id=%s); skip save." , agent_id
325- )
326- return
327-
328- currentAgent = CurrentTeamAgent (
329- team_id = self .team_config .team_id ,
330- team_name = self .team_config .name ,
331- agent_name = self .agent_name ,
332- agent_foundry_id = agent_id ,
333- agent_description = self .agent_description ,
334- agent_instructions = self .agent_instructions ,
335- )
336- await self .memory_store .add_team_agent (currentAgent )
337- self .logger .info (
338- "Saved team agent to database (agent_name=%s, id=%s)" ,
339- self .agent_name ,
340- agent_id ,
341- )
342-
343- except Exception as ex :
344- self .logger .error ("Failed to save database: %s" , ex )
345-
346184 async def _prepare_mcp_tool (self ) -> None :
347185 """Translate MCPConfig to a HostedMCPTool (agent_framework construct)."""
348186 if not self .mcp_cfg :
0 commit comments