1616from common .config .app_config import config
1717from common .database .database_base import DatabaseBase
1818from common .models .messages_af import TeamConfiguration
19+ from common .utils .agent_name_sanitizer import AgentNameSanitizer
1920from v4 .common .services .team_service import TeamService
2021from v4 .config .agent_registry import agent_registry
2122from v4 .magentic_agents .common .lifecycle import AzureAgentBase
@@ -46,8 +47,11 @@ def __init__(
4647 team_config : TeamConfiguration | None = None ,
4748 memory_store : DatabaseBase | None = None ,
4849 ) -> None :
50+ # Sanitize agent name to comply with Azure requirements
51+ agent_name = AgentNameSanitizer .sanitize (agent_name , "DefaultAgent" )
52+
4953 # Defer project_client creation until async open() to use async credentials
50- project_client = None # Will be created in parent's open() method
54+ project_client = config . get_ai_project_client () # Will be created in parent's open() method
5155
5256 super ().__init__ (
5357 mcp = mcp_config ,
@@ -206,11 +210,11 @@ async def _create_azure_search_enabled_client(self, chatClient=None) -> Optional
206210 tools = [search_tool ]
207211 )
208212 )
209- async for agent in self . project_client . agents .list ():
210- print ( f" Agent: { agent . name } " )
211-
212- async for version in self .project_client .agents .list_versions (agent_name = azure_agent .name ):
213- print (f" Version: { version } " )
213+ # NOTE: Debug agent listing disabled as agents.list() method doesn't exist in current framework
214+ # async for agent in self.project_client.agents.list():
215+ # print(f" Agent: {agent.name}")
216+ # async for version in self.project_client.agents.list_versions(agent_name=azure_agent.name):
217+ # print(f" Version: {version}")
214218
215219 self .logger .info (
216220 "Created Azure server agent with Azure AI Search tool (agent_name=%s, index=%s, query_type=%s)." ,
@@ -219,13 +223,15 @@ async def _create_azure_search_enabled_client(self, chatClient=None) -> Optional
219223 query_type ,
220224 )
221225
222- # Use AzureAIClient with both project_client and async_credential
223- # The async_credential is needed for inference/streaming operations
226+ # Use AzureAIClient with correct credential parameter following reference pattern
227+ # Each agent gets its own client instance with unique agent_name for proper conversation/thread handling
228+ credential = config .get_azure_credential (client_id = config .AZURE_CLIENT_ID )
224229 chat_client = AzureAIClient (
225230 project_client = self .project_client ,
226- async_credential = self . creds if hasattr ( self , 'creds' ) and self . creds else None ,
231+ credential = credential ,
227232 agent_name = self .agent_name ,
228233 use_latest_version = True ,
234+ model_deployment_name = self .model_deployment_name
229235 )
230236 return chat_client
231237 except Exception as ex :
@@ -275,6 +281,7 @@ async def _after_open(self) -> None:
275281 chat_client = self .get_chat_client (chat_client ),
276282 tool_choice = "required" ,
277283 store = True ,
284+ model_id = self .model_deployment_name , # Add model_id to prevent validation error
278285 )
279286 else :
280287 # use MCP path
@@ -309,24 +316,29 @@ async def _after_open(self) -> None:
309316 len (foundry_tools )
310317 )
311318
312- # Use AzureAIClient with the created agent
319+ # Use AzureAIClient with the created agent following reference pattern
320+ # Each agent gets its own client instance with unique agent_name for proper conversation/thread handling
321+ credential = config .get_azure_credential (client_id = config .AZURE_CLIENT_ID )
313322 chat_client = AzureAIClient (
314323 project_client = self .project_client ,
315- async_credential = self . creds if hasattr ( self , 'creds' ) and self . creds else None ,
324+ credential = credential ,
316325 agent_name = self .agent_name ,
317326 use_latest_version = True ,
327+ model_deployment_name = self .model_deployment_name ,
318328 )
319329
320330 # Create ChatAgent with the Azure-backed client
321331 self ._agent = ChatAgent (
322- chat_client = self . get_chat_client ( chat_client ) ,
332+ chat_client = chat_client ,
323333 tools = self ._tools_for_invoke ,
324334 tool_choice = self ._tool_choice ,
325335 store = True ,
336+ model_id = self .model_deployment_name , # Add model_id to prevent validation error
326337 )
327338
328- async for agent in self .project_client .agents .list ():
329- print (f" Agent: { agent .name } " )
339+ # NOTE: Debug agent listing disabled as agents.list() method doesn't exist in current framework
340+ # async for agent in self.project_client.agents.list():
341+ # print(f" Agent: {agent.name}")
330342
331343 self .logger .info ("Initialized ChatAgent '%s'" , self .agent_name )
332344
0 commit comments