Skip to content

Commit 07b3ea0

Browse files
committed
Refactor user team selection and management logic
Introduces new methods for getting, setting, and updating a user's current team in the database layer. Refactors the team selection endpoint and service to use these methods, improving clarity and separation of concerns. Updates abstract base and service interfaces to support the new workflow.
1 parent 07fa31f commit 07b3ea0

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

src/backend/common/database/cosmosdb.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CosmosDBClient(DatabaseBase):
4343
"step": Step,
4444
"agent_message": AgentMessage,
4545
"team_config": TeamConfiguration,
46+
"user_current_team": UserCurrentTeam,
4647
}
4748

4849
def __init__(
@@ -432,7 +433,33 @@ async def update_team(self, team: TeamConfiguration) -> None:
432433
team: The TeamConfiguration to update
433434
"""
434435
await self.update_item(team)
435-
436-
async def set_user_default_team(current_team: UserCurrentTeam) -> bool:
436+
437+
async def get_current_team(self, user_id: str, team_id: str) -> UserCurrentTeam:
438+
"""Retrieve the current team for a user."""
439+
await self._ensure_initialized()
440+
if self.container is None:
441+
return None
442+
443+
query = "SELECT * FROM c WHERE c.user_id=@user_id AND c.is_default=true"
444+
parameters = [
445+
{"name": "@user_id", "value": user_id},
446+
{"name": "@team_id", "value": team_id},
447+
]
448+
449+
items = self.container.query_items(query=query, parameters=parameters)
450+
async for item in items:
451+
return UserCurrentTeam(**item)
452+
453+
return None
454+
async def set_current_team(self, current_team: UserCurrentTeam) -> None:
455+
"""Set the current team for a user."""
456+
await self._ensure_initialized()
457+
458+
459+
await self.container.upsert_item(current_team.dict())
460+
async def update_current_team(self, current_team: UserCurrentTeam) -> None:
461+
"""Update the current team for a user."""
462+
await self._ensure_initialized()
437463

438464

465+
await self.container.upsert_item(current_team.dict())

src/backend/common/database/database_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,15 @@ async def get_steps_for_plan(self, plan_id: str) -> List[Step]:
192192
pass
193193

194194
@abstractmethod
195-
async def set_user_default_team(self, UserCurrentTeam: UserCurrentTeam) -> bool:
196-
"""Set the default team for a user."""
195+
async def get_current_team(self, user_id: str, team_id: str) -> UserCurrentTeam:
196+
"""Retrieve the current team for a user."""
197197
pass
198198

199+
@abstractmethod
200+
async def set_current_team(self, current_team: UserCurrentTeam) -> None:
201+
pass
202+
203+
@abstractmethod
204+
async def update_current_team(self, current_team: UserCurrentTeam) -> None:
205+
"""Update the current team for a user."""
206+
pass

src/backend/v3/api/router.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,14 +791,20 @@ async def select_team_endpoint(selection: TeamSelectionRequest, request: Request
791791
status_code=404,
792792
detail=f"Team configuration '{selection.team_id}' not found or access denied"
793793
)
794-
current_team = await team_service.get_user_current_team(user_id=user_id)
795-
if current_team and current_team.team_id != selection.team_id:
796-
current_team.team_id = selection.team_id
797-
await team_service.set_user_current_team(user_id=user_id, current_team=current_team, )
798-
else:
799-
current_team = UserCurrentTeam(
800-
user_id=user_id,
801-
team_id=selection.team_id
794+
set_team = await team_service.handle_team_selection(user_id=user_id, team_id=selection.team_id)
795+
if not set_team:
796+
track_event_if_configured(
797+
"Team selected",
798+
{
799+
"status": "failed",
800+
"team_id": selection.team_id,
801+
"team_name": team_configuration.name,
802+
"user_id": user_id
803+
},
804+
)
805+
raise HTTPException(
806+
status_code=404,
807+
detail=f"Team configuration '{selection.team_id}' failed to set"
802808
)
803809

804810
# save to in-memory config for current user

src/backend/v3/common/services/team_service.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
from datetime import datetime, timezone
66
from typing import Any, Dict, List, Optional, Tuple
77

8-
from azure.core.credentials import AzureKeyCredential
9-
from azure.core.exceptions import (ClientAuthenticationError,
10-
HttpResponseError, ResourceNotFoundError)
11-
from azure.identity import DefaultAzureCredential
8+
from azure.core.exceptions import (
9+
ClientAuthenticationError,
10+
HttpResponseError,
11+
ResourceNotFoundError,
12+
)
13+
1214
from azure.search.documents.indexes import SearchIndexClient
1315
from common.config.app_config import config
1416
from common.database.database_base import DatabaseBase
15-
from common.models.messages_kernel import (StartingTask, TeamAgent,
16-
TeamConfiguration)
17+
from common.models.messages_kernel import (
18+
StartingTask,
19+
TeamAgent,
20+
TeamConfiguration,
21+
UserCurrentTeam,
22+
)
1723
from v3.common.services.foundry_service import FoundryService
1824

1925

@@ -237,7 +243,7 @@ async def delete_user_current_team(self, user_id: str) -> bool:
237243
self.logger.error("Error deleting current team: %s", str(e))
238244
return False
239245

240-
async def set_default_team(self, user_id: str, team_id: str) -> bool:
246+
async def handle_team_selection(self, user_id: str, team_id: str) -> bool:
241247
"""
242248
Set a default team for a user.
243249
@@ -249,9 +255,16 @@ async def set_default_team(self, user_id: str, team_id: str) -> bool:
249255
True if successful, False otherwise
250256
"""
251257
try:
252-
await self.memory_context.set_user_default_team(user_id, team_id)
253-
self.logger.info("Successfully set default team for user %s: %s", user_id, team_id)
254-
return True
258+
current_team = await self.memory_context.get_current_team(user_id, team_id)
259+
260+
if current_team is None:
261+
current_team = UserCurrentTeam(user_id=user_id, team_id=team_id)
262+
await self.memory_context.set_current_team(current_team)
263+
return True
264+
else:
265+
current_team.team_id = team_id
266+
await self.memory_context.update_current_team(current_team)
267+
return True
255268

256269
except Exception as e:
257270
self.logger.error("Error setting default team: %s", str(e))
@@ -384,7 +397,7 @@ async def validate_team_models(
384397
missing_models: List[str] = []
385398
for model in required_models:
386399
# Temporary bypass for known deployed models
387-
if model.lower() in ['gpt-4o', 'o3', 'gpt-4', 'gpt-35-turbo']:
400+
if model.lower() in ["gpt-4o", "o3", "gpt-4", "gpt-35-turbo"]:
388401
continue
389402
if model not in available_models:
390403
missing_models.append(model)

0 commit comments

Comments
 (0)