Skip to content

Commit a5bb26e

Browse files
committed
Refactor team selection and plan retrieval endpoints
Simplifies the /api/plans endpoint by removing session_id and team_id query parameters and related logic. Refactors get_current_team to only require user_id, updating its usage across database and service layers. Enhances team initialization and configuration upload endpoints to better handle current team selection and configuration updates.
1 parent dbbd5e1 commit a5bb26e

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

src/backend/app_kernel.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,7 @@ async def approve_step_endpoint(
598598
@app.get("/api/plans")
599599
async def get_plans(
600600
request: Request,
601-
session_id: Optional[str] = Query(None),
602601
plan_id: Optional[str] = Query(None),
603-
team_id: Optional[str] = Query(None),
604602
):
605603
"""
606604
Retrieve plans for the current user.
@@ -673,20 +671,7 @@ async def get_plans(
673671

674672
# # Initialize memory context
675673
memory_store = await DatabaseFactory.get_database(user_id=user_id)
676-
if session_id:
677-
plan = await memory_store.get_plan_by_session(session_id=session_id)
678-
if not plan:
679-
track_event_if_configured(
680-
"GetPlanBySessionNotFound",
681-
{"status_code": 400, "detail": "Plan not found"},
682-
)
683-
raise HTTPException(status_code=404, detail="Plan not found")
684674

685-
# Use get_steps_by_plan to match the original implementation
686-
steps = await memory_store.get_steps_by_plan(plan_id=plan.id)
687-
plan_with_steps = PlanWithSteps(**plan.model_dump(), steps=steps)
688-
plan_with_steps.update_step_counts()
689-
return [plan_with_steps]
690675
if plan_id:
691676
plan = await memory_store.get_plan_by_plan_id(plan_id=plan_id)
692677
if not plan:

src/backend/common/database/cosmosdb.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,23 +434,22 @@ async def update_team(self, team: TeamConfiguration) -> None:
434434
"""
435435
await self.update_item(team)
436436

437-
async def get_current_team(self, user_id: str, team_id: str) -> UserCurrentTeam:
437+
async def get_current_team(self, user_id: str) -> Optional[UserCurrentTeam]:
438438
"""Retrieve the current team for a user."""
439439
await self._ensure_initialized()
440440
if self.container is None:
441441
return None
442442

443-
query = "SELECT * FROM c WHERE c.user_id=@user_id AND c.is_default=true"
443+
query = "SELECT * FROM c WHERE c.data_type=@data_type AND c.user_id=@user_id"
444444
parameters = [
445+
{"name": "@data_type", "value": "user_current_team"},
445446
{"name": "@user_id", "value": user_id},
446-
{"name": "@team_id", "value": team_id},
447447
]
448448

449-
items = self.container.query_items(query=query, parameters=parameters)
450-
async for item in items:
451-
return UserCurrentTeam(**item)
449+
# Get the appropriate model class
450+
teams = await self.query_items(query, parameters, UserCurrentTeam)
451+
return teams[0] if teams else None
452452

453-
return None
454453
async def set_current_team(self, current_team: UserCurrentTeam) -> None:
455454
"""Set the current team for a user."""
456455
await self._ensure_initialized()

src/backend/common/database/database_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async def get_steps_for_plan(self, plan_id: str) -> List[Step]:
192192
pass
193193

194194
@abstractmethod
195-
async def get_current_team(self, user_id: str, team_id: str) -> UserCurrentTeam:
195+
async def get_current_team(self, user_id: str) -> Optional[UserCurrentTeam]:
196196
"""Retrieve the current team for a user."""
197197
pass
198198

src/backend/v3/api/router.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
TeamSelectionRequest, UserCurrentTeam)
1414
from common.utils.event_utils import track_event_if_configured
1515
from common.utils.utils_kernel import rai_success, rai_validate_team_config
16-
from fastapi import (APIRouter, BackgroundTasks, Depends, FastAPI, File,
16+
from fastapi import (APIRouter, BackgroundTasks, Depends, FastAPI, File, Query,
1717
HTTPException, Request, UploadFile, WebSocket,
1818
WebSocketDisconnect)
1919
from kernel_agents.agent_factory import AgentFactory
@@ -103,7 +103,11 @@ async def init_team(
103103
# Initialize memory store and service
104104
memory_store = await DatabaseFactory.get_database(user_id=user_id)
105105
team_service = TeamService(memory_store)
106-
106+
user_current_team = await memory_store.get_current_team(user_id=user_id)
107+
if not user_current_team:
108+
await team_service.handle_team_selection(user_id=user_id, team_id=init_team_id)
109+
else:
110+
init_team_id = user_current_team.team_id
107111
# Verify the team exists and user has access to it
108112
team_configuration = await team_service.get_team_configuration(init_team_id, user_id)
109113
if team_configuration is None:
@@ -120,7 +124,8 @@ async def init_team(
120124

121125
return {
122126
"status": "Request started successfully",
123-
"team_id": init_team_id
127+
"team_id": init_team_id,
128+
"team": team_configuration
124129
}
125130

126131
except Exception as e:
@@ -351,7 +356,7 @@ async def user_clarification(human_feedback: messages.UserClarificationResponse,
351356

352357

353358
@app_v3.post("/upload_team_config")
354-
async def upload_team_config_endpoint(request: Request, file: UploadFile = File(...)):
359+
async def upload_team_config_endpoint(request: Request, file: UploadFile = File(...), team_id: Optional[str] = Query(None),):
355360
"""
356361
Upload and save a team configuration JSON file.
357362
@@ -487,6 +492,10 @@ async def upload_team_config_endpoint(request: Request, file: UploadFile = File(
487492

488493
# Save the configuration
489494
try:
495+
print("Saving team configuration...", team_id)
496+
if team_id:
497+
team_config.team_id = team_id
498+
team_config.id = team_id # Ensure id is also set for updates
490499
team_id = await team_service.save_team_configuration(team_config)
491500
except ValueError as e:
492501
raise HTTPException(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ async def handle_team_selection(self, user_id: str, team_id: str) -> bool:
255255
True if successful, False otherwise
256256
"""
257257
try:
258-
current_team = await self.memory_context.get_current_team(user_id, team_id)
258+
current_team = await self.memory_context.get_current_team(user_id)
259259

260260
if current_team is None:
261261
current_team = UserCurrentTeam(user_id=user_id, team_id=team_id)

0 commit comments

Comments
 (0)