Skip to content

Commit ac64b2d

Browse files
committed
Refactor agent initialization and persistence logic
Standardizes agent metadata handling by passing agent_name, agent_description, and agent_instructions through constructors and storing them as instance variables. Refactors save_database_team_agent to use instance variables instead of method arguments. Updates orchestration and API router to support new agent initialization signatures. Improves logging and error handling for agent lifecycle management.
1 parent 4db462a commit ac64b2d

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

src/backend/v4/api/router.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
TeamSelectionRequest,
1515
)
1616
from common.utils.event_utils import track_event_if_configured
17-
from common.utils.utils_af import find_first_available_team, rai_success, rai_validate_team_config
17+
from common.utils.utils_af import (
18+
find_first_available_team,
19+
rai_success,
20+
rai_validate_team_config,
21+
)
1822
from fastapi import (
1923
APIRouter,
2024
BackgroundTasks,
@@ -149,7 +153,10 @@ async def init_team(
149153

150154
# Initialize agent team for this user session
151155
await OrchestrationManager.get_current_or_new_orchestration(
152-
user_id=user_id, team_config=team_configuration, team_switched=team_switched
156+
user_id=user_id,
157+
team_config=team_configuration,
158+
team_switched=team_switched,
159+
team_service=team_service,
153160
)
154161

155162
return {

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
import os
45
from contextlib import AsyncExitStack
56
from typing import Any, Optional
@@ -45,6 +46,9 @@ def __init__(
4546
team_config: TeamConfiguration | None = None,
4647
project_endpoint: str | None = None,
4748
memory_store: DatabaseBase | None = None,
49+
agent_name: str | None = None,
50+
agent_description: str | None = None,
51+
agent_instructions: str | None = None,
4852
) -> None:
4953
self._stack: AsyncExitStack | None = None
5054
self.mcp_cfg: MCPConfig | None = mcp
@@ -56,6 +60,10 @@ def __init__(
5660
self.project_endpoint = project_endpoint
5761
self.creds: Optional[DefaultAzureCredential] = None
5862
self.memory_store: Optional[DatabaseBase] = memory_store
63+
self.agent_name: str | None = agent_name
64+
self.agent_description: str | None = agent_description
65+
self.agent_instructions: str | None = agent_instructions
66+
self.logger = logging.getLogger(__name__)
5967

6068
async def open(self) -> "MCPEnabledBase":
6169
if self._stack is not None:
@@ -142,15 +150,16 @@ async def get_database_team_agent(self) -> Optional[CurrentTeamAgent]:
142150
self.logger.error("Failed to initialize ReasoningAgentTemplate: %s", ex)
143151
return agent
144152

145-
async def save_database_team_agent(self, agent_name, description, instructions) -> None:
153+
async def save_database_team_agent(self) -> None:
146154
"""Save current team agent to database."""
147155
try:
148156
currentAgent = CurrentTeamAgent(
149157
team_id=self.team_config.team_id,
150-
agent_name=agent_name,
158+
team_name=self.team_config.name,
159+
agent_name=self.agent_name,
151160
agent_foundry_id=self._agent.id,
152-
agent_description=description,
153-
agent_instructions=instructions,
161+
agent_description=self.agent_description,
162+
agent_instructions=self.agent_instructions,
154163
)
155164
await self.memory_store.add_team_agent(currentAgent)
156165

@@ -191,13 +200,19 @@ def __init__(
191200
team_service: TeamService | None = None,
192201
team_config: TeamConfiguration | None = None,
193202
memory_store: DatabaseBase | None = None,
203+
agent_name: str | None = None,
204+
agent_description: str | None = None,
205+
agent_instructions: str | None = None,
194206
) -> None:
195207
super().__init__(
196208
mcp=mcp,
197209
team_service=team_service,
198210
team_config=team_config,
199211
project_endpoint=project_endpoint,
200212
memory_store=memory_store,
213+
agent_name=agent_name,
214+
agent_description=agent_description,
215+
agent_instructions=agent_instructions,
201216
)
202217

203218
self._created_ephemeral: bool = (

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ def __init__(
4545
team_service=team_service,
4646
team_config=team_config,
4747
memory_store=memory_store,
48+
agent_name=agent_name,
49+
agent_description=agent_description,
50+
agent_instructions=agent_instructions,
4851
)
49-
self.agent_name = agent_name
50-
self.agent_description = agent_description
51-
self.agent_instructions = agent_instructions
52+
5253
self.enable_code_interpreter = enable_code_interpreter
5354
self.search = search_config
5455
self.logger = logging.getLogger(__name__)
@@ -260,9 +261,7 @@ async def _after_open(self) -> None:
260261

261262
self.logger.info("Initialized ChatAgent '%s'", self.agent_name)
262263

263-
await self.save_database_team_agent(
264-
self.agent_name, self.agent_description, self.agent_instructions
265-
)
264+
await self.save_database_team_agent()
266265

267266
else:
268267
self.logger.info("Using existing ChatAgent '%s'", self.agent_name)

src/backend/v4/magentic_agents/reasoning_agent.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ def __init__(
7070
project_endpoint=project_endpoint,
7171
memory_store=memory_store,
7272
)
73-
self.agent_name = agent_name
74-
self.agent_description = agent_description
75-
self.base_instructions = agent_instructions
7673
self.model_deployment_name = model_deployment_name
7774

7875
self.search_config = search_config
@@ -118,10 +115,8 @@ async def _after_open(self) -> None:
118115
temperature=1.0, # Reasoning models use fixed temperature
119116
model_id=self.model_deployment_name,
120117
)
121-
await self.save_database_team_agent(
122-
self.agent_name, self.agent_description, self.base_instructions
123-
)
124-
118+
await self.save_database_team_agent()
119+
125120
else:
126121
self._agent = agent
127122
self.logger.info("Using existing ReasoningAgent '%s'", self.agent_name)

src/backend/v4/orchestration/orchestration_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async def get_current_or_new_orchestration(
150150
user_id: str,
151151
team_config: TeamConfiguration,
152152
team_switched: bool,
153-
team_service: Optional[TeamService] = None,
153+
team_service:TeamService = None,
154154
):
155155
"""
156156
Return an existing workflow for the user or create a new one if:

0 commit comments

Comments
 (0)