Skip to content

Commit e9cbbaa

Browse files
authored
Merge pull request microsoft#644 from Fr4nc3/macae-rfp-af-101725
Macae rfp af 101725
2 parents 2c06e48 + 65910ec commit e9cbbaa

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/backend/v4/magentic_agents/common/lifecycle.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging
44
import os
55
from contextlib import AsyncExitStack
6+
import secrets
7+
import string
68
from typing import Any, Optional
79

810
from agent_framework import (
@@ -155,6 +157,33 @@ def get_chat_client(self, chat_client) -> AzureAIAgentClient:
155157
extra={"agent_id": chat_client.agent_id},
156158
)
157159
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}"
173+
174+
def get_agent_id(self, chat_client) -> str:
175+
"""Return the underlying agent ID."""
176+
if chat_client and chat_client.agent_id is not None:
177+
return chat_client.agent_id
178+
if (
179+
self._agent
180+
and self._agent.chat_client
181+
and self._agent.chat_client.agent_id is not None
182+
):
183+
return self._agent.chat_client.agent_id # type: ignore
184+
id = self.generate_assistant_id()
185+
self.logger.info("Generated new agent ID: %s", id)
186+
return id
158187

159188
async def get_database_team_agent(self) -> Optional[AzureAIAgentClient]:
160189
"""Retrieve existing team agent from database, if any."""

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ async def _after_open(self) -> None:
235235

236236
# In Azure Search raw tool path, tools/tool_choice are handled server-side.
237237
self._agent = ChatAgent(
238-
chat_client=self.get_chat_client(chatClient),
238+
id=self.get_agent_id(chat_client),
239+
chat_client=self.get_chat_client(chat_client),
239240
instructions=self.agent_instructions,
240241
name=self.agent_name,
241242
description=self.agent_description,
@@ -248,6 +249,7 @@ async def _after_open(self) -> None:
248249
self.logger.info("Initializing agent in MCP mode.")
249250
tools = await self._collect_tools()
250251
self._agent = ChatAgent(
252+
id=self.get_agent_id(chatClient),
251253
chat_client=self.get_chat_client(chatClient),
252254
instructions=self.agent_instructions,
253255
name=self.agent_name,

0 commit comments

Comments
 (0)