Skip to content

Commit 533f1c9

Browse files
committed
Move agent utility functions to utils_agents.py
Refactored generate_assistant_id and get_database_team_agent_id from utils_af.py to a new utils_agents.py for better separation of concerns. Updated imports in lifecycle.py and orchestration_manager.py to use the new module.
1 parent 873fdf4 commit 533f1c9

4 files changed

Lines changed: 44 additions & 36 deletions

File tree

src/backend/common/utils/utils_af.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -217,37 +217,3 @@ async def rai_validate_team_config(
217217
except Exception as e:
218218
logging.error("Error validating team configuration content: %s", e)
219219
return False, "Unable to validate team configuration content. Please try again."
220-
221-
222-
def generate_assistant_id(prefix: str = "asst_", length: int = 24) -> str:
223-
"""
224-
Generate a unique ID like 'asst_jRgR5t2U7o8nUPkNGv5HWOgV'.
225-
226-
- prefix: leading string (defaults to 'asst_')
227-
- length: number of random characters after the prefix
228-
"""
229-
# URL-safe characters similar to what OpenAI-style IDs use
230-
alphabet = string.ascii_letters + string.digits # a-zA-Z0-9
231-
232-
# cryptographically strong randomness
233-
random_part = "".join(secrets.choice(alphabet) for _ in range(length))
234-
return f"{prefix}{random_part}"
235-
236-
237-
async def get_database_team_agent_id(
238-
memory_store: DatabaseBase, team_config: TeamConfiguration, agent_name: str
239-
) -> Optional[str]:
240-
"""Retrieve existing team agent from database, if any."""
241-
agent_id = None
242-
try:
243-
currentAgent = await memory_store.get_team_agent(
244-
team_id=team_config.team_id, agent_name=agent_name
245-
)
246-
if currentAgent and currentAgent.agent_foundry_id:
247-
agent_id = currentAgent.agent_foundry_id
248-
249-
except (
250-
Exception
251-
) as ex: # Consider narrowing this to specific exceptions if possible
252-
logging.error("Failed to initialize Get database team agent: %s", ex)
253-
return agent_id
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import logging
3+
import secrets
4+
import string
5+
from typing import Optional
6+
7+
from common.database.database_base import DatabaseBase
8+
from common.models.messages_af import TeamConfiguration
9+
10+
11+
def generate_assistant_id(prefix: str = "asst_", length: int = 24) -> str:
12+
"""
13+
Generate a unique ID like 'asst_jRgR5t2U7o8nUPkNGv5HWOgV'.
14+
15+
- prefix: leading string (defaults to 'asst_')
16+
- length: number of random characters after the prefix
17+
"""
18+
# URL-safe characters similar to what OpenAI-style IDs use
19+
alphabet = string.ascii_letters + string.digits # a-zA-Z0-9
20+
21+
# cryptographically strong randomness
22+
random_part = "".join(secrets.choice(alphabet) for _ in range(length))
23+
return f"{prefix}{random_part}"
24+
25+
26+
async def get_database_team_agent_id(
27+
memory_store: DatabaseBase, team_config: TeamConfiguration, agent_name: str
28+
) -> Optional[str]:
29+
"""Retrieve existing team agent from database, if any."""
30+
agent_id = None
31+
try:
32+
currentAgent = await memory_store.get_team_agent(
33+
team_id=team_config.team_id, agent_name=agent_name
34+
)
35+
if currentAgent and currentAgent.agent_foundry_id:
36+
agent_id = currentAgent.agent_foundry_id
37+
38+
except (
39+
Exception
40+
) as ex: # Consider narrowing this to specific exceptions if possible
41+
logging.error("Failed to initialize Get database team agent: %s", ex)
42+
return agent_id

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from azure.identity.aio import DefaultAzureCredential
1919
from common.database.database_base import DatabaseBase
2020
from common.models.messages_af import CurrentTeamAgent, TeamConfiguration
21-
from common.utils.utils_af import (
21+
from common.utils.utils_agents import (
2222
generate_assistant_id,
2323
get_database_team_agent_id,
2424
)

src/backend/v4/orchestration/orchestration_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from common.models.messages_af import TeamConfiguration
2323

2424
from common.database.database_base import DatabaseBase
25-
from common.utils.utils_af import (
25+
from common.utils.utils_agents import (
2626
generate_assistant_id,
2727
get_database_team_agent_id,
2828
)

0 commit comments

Comments
 (0)