11from __future__ import annotations
22
33import logging
4- import os
5- from contextlib import AsyncExitStack
64import secrets
75import string
6+ from contextlib import AsyncExitStack
87from typing import Any , Optional
98
109from agent_framework import (
11- AggregateContextProvider ,
1210 ChatAgent ,
13- ChatClientProtocol ,
14- ChatMessage ,
15- ChatMessageStoreProtocol ,
16- ChatOptions ,
17- ContextProvider ,
1811 HostedMCPTool ,
1912 MCPStreamableHTTPTool ,
20- Middleware ,
21- Role ,
22- ToolMode ,
23- ToolProtocol ,
2413)
2514
2615# from agent_framework.azure import AzureAIAgentClient
2716from agent_framework_azure_ai import AzureAIAgentClient
2817from azure .ai .agents .aio import AgentsClient
2918from azure .identity .aio import DefaultAzureCredential
30- from common .models .messages_af import CurrentTeamAgent , TeamConfiguration
3119from common .database .database_base import DatabaseBase
20+ from common .models .messages_af import CurrentTeamAgent , TeamConfiguration
21+ from common .utils .utils_agents import (
22+ generate_assistant_id ,
23+ get_database_team_agent_id ,
24+ )
3225from v4 .common .services .team_service import TeamService
3326from v4 .config .agent_registry import agent_registry
3427from v4 .magentic_agents .models .agent_models import MCPConfig
@@ -157,19 +150,6 @@ def get_chat_client(self, chat_client) -> AzureAIAgentClient:
157150 extra = {"agent_id" : chat_client .agent_id },
158151 )
159152 return chat_client
160- def generate_assistant_id (self , prefix : str = "asst_" , length : int = 24 ) -> str :
161- """
162- Generate a unique ID like 'asst_jRgR5t2U7o8nUPkNGv5HWOgV'.
163-
164- - prefix: leading string (defaults to 'asst_')
165- - length: number of random characters after the prefix
166- """
167- # URL-safe characters similar to what OpenAI-style IDs use
168- alphabet = string .ascii_letters + string .digits # a-zA-Z0-9
169-
170- # cryptographically strong randomness
171- random_part = "" .join (secrets .choice (alphabet ) for _ in range (length ))
172- return f"{ prefix } { random_part } "
173153
174154 def get_agent_id (self , chat_client ) -> str :
175155 """Return the underlying agent ID."""
@@ -181,25 +161,24 @@ def get_agent_id(self, chat_client) -> str:
181161 and self ._agent .chat_client .agent_id is not None
182162 ):
183163 return self ._agent .chat_client .agent_id # type: ignore
184- id = self . generate_assistant_id ()
164+ id = generate_assistant_id ()
185165 self .logger .info ("Generated new agent ID: %s" , id )
186166 return id
187167
188168 async def get_database_team_agent (self ) -> Optional [AzureAIAgentClient ]:
189169 """Retrieve existing team agent from database, if any."""
190170 chat_client = None
191171 try :
192- currentAgent = await self . memory_store . get_team_agent (
193- team_id = self .team_config . team_id , agent_name = self .agent_name
172+ agent_id = await get_database_team_agent_id (
173+ self .memory_store , self . team_config , self .agent_name
194174 )
195- if currentAgent and currentAgent .agent_foundry_id :
196- agent = await self .client .get_agent (
197- agent_id = currentAgent .agent_foundry_id
198- )
199- if agent and agent .agent_id is not None :
175+
176+ if agent_id :
177+ agent = await self .client .get_agent (agent_id = agent_id )
178+ if agent and agent .id is not None :
200179 chat_client = AzureAIAgentClient (
201180 project_endpoint = self .project_endpoint ,
202- agent_id = agent .agent_id ,
181+ agent_id = agent .id ,
203182 model_deployment_name = self .model_deployment_name ,
204183 async_credential = self .creds ,
205184 )
@@ -213,15 +192,15 @@ async def get_database_team_agent(self) -> Optional[AzureAIAgentClient]:
213192 async def save_database_team_agent (self ) -> None :
214193 """Save current team agent to database."""
215194 try :
216- if self ._agent .chat_client . agent_id is None :
195+ if self ._agent .id is None :
217196 self .logger .error ("Cannot save database team agent: agent_id is None" )
218197 return
219198
220199 currentAgent = CurrentTeamAgent (
221200 team_id = self .team_config .team_id ,
222201 team_name = self .team_config .name ,
223202 agent_name = self .agent_name ,
224- agent_foundry_id = self ._agent .chat_client . agent_id ,
203+ agent_foundry_id = self ._agent .id ,
225204 agent_description = self .agent_description ,
226205 agent_instructions = self .agent_instructions ,
227206 )
0 commit comments