Skip to content

Commit d4e1d81

Browse files
fix: clean up unnecessary comments in create_plan_endpoint and fixed RAI validation for plans
1 parent 3bde534 commit d4e1d81

File tree

1 file changed

+43
-99
lines changed

1 file changed

+43
-99
lines changed

src/backend/v3/api/router.py

Lines changed: 43 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
1-
import json
2-
import logging
31
import uuid
2+
import logging
3+
import json
4+
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Request
45

56
from auth.auth_utils import get_authenticated_user_details
6-
from common.config.app_config import config
7-
from common.database.database_factory import DatabaseFactory
87
from common.models.messages_kernel import (
98
GeneratePlanRequest,
109
InputTask,
1110
Plan,
1211
PlanStatus,
1312
)
1413
from common.utils.event_utils import track_event_if_configured
15-
from common.utils.utils_kernel import rai_success, rai_validate_team_config
16-
from fastapi import (
17-
APIRouter,
18-
Depends,
19-
File,
20-
HTTPException,
21-
Request,
22-
UploadFile,
23-
WebSocket,
24-
WebSocketDisconnect,
14+
from common.utils.utils_kernel import (
15+
rai_success,
16+
rai_validate_team_config,
2517
)
26-
from kernel_agents.agent_factory import AgentFactory
27-
from semantic_kernel.agents.runtime import InProcessRuntime
2818
from v3.common.services.team_service import TeamService
29-
from v3.config.settings import orchestration_config
30-
from v3.models.models import MPlan, MStep
19+
from kernel_agents.agent_factory import AgentFactory
20+
from common.database.database_factory import DatabaseFactory
3121
from v3.models.orchestration_models import AgentType
22+
from common.config.app_config import config
3223

3324
app_v3 = APIRouter(
3425
prefix="/api/v3",
3526
responses={404: {"description": "Not found"}},
3627
)
3728

3829

39-
# To do: change endpoint to process request
4030
@app_v3.post("/create_plan")
41-
async def process_request(input_task: InputTask, request: Request):
31+
async def create_plan_endpoint(input_task: InputTask, request: Request):
4232
"""
4333
Create a new plan without full processing.
4434
@@ -87,33 +77,31 @@ async def process_request(input_task: InputTask, request: Request):
8777
type: string
8878
description: Error message
8979
"""
90-
# Perform RAI check on the description
91-
# if not await rai_success(input_task.description, False):
92-
# track_event_if_configured(
93-
# "RAI failed",
94-
# {
95-
# "status": "Plan not created - RAI check failed",
96-
# "description": input_task.description,
97-
# "session_id": input_task.session_id,
98-
# },
99-
# )
100-
# raise HTTPException(
101-
# status_code=400,
102-
# detail={
103-
# "error_type": "RAI_VALIDATION_FAILED",
104-
# "message": "Content Safety Check Failed",
105-
# "description": "Your request contains content that doesn't meet our safety guidelines. Please modify your request to ensure it's appropriate and try again.",
106-
# "suggestions": [
107-
# "Remove any potentially harmful, inappropriate, or unsafe content",
108-
# "Use more professional and constructive language",
109-
# "Focus on legitimate business or educational objectives",
110-
# "Ensure your request complies with content policies",
111-
# ],
112-
# "user_action": "Please revise your request and try again",
113-
# },
114-
# )
115-
116-
# Get authenticated user
80+
if not await rai_success(input_task.description, False):
81+
track_event_if_configured(
82+
"RAI failed",
83+
{
84+
"status": "Plan not created - RAI check failed",
85+
"description": input_task.description,
86+
"session_id": input_task.session_id,
87+
},
88+
)
89+
raise HTTPException(
90+
status_code=400,
91+
detail={
92+
"error_type": "RAI_VALIDATION_FAILED",
93+
"message": "Content Safety Check Failed",
94+
"description": "Your request contains content that doesn't meet our safety guidelines. Please modify your request to ensure it's appropriate and try again.",
95+
"suggestions": [
96+
"Remove any potentially harmful, inappropriate, or unsafe content",
97+
"Use more professional and constructive language",
98+
"Focus on legitimate business or educational objectives",
99+
"Ensure your request complies with content policies",
100+
],
101+
"user_action": "Please revise your request and try again",
102+
},
103+
)
104+
117105
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
118106
user_id = authenticated_user["user_principal_id"]
119107

@@ -129,42 +117,23 @@ async def process_request(input_task: InputTask, request: Request):
129117
)
130118
raise HTTPException(status_code=400, detail="no team id")
131119

132-
# Generate session ID if not provided
133120
if not input_task.session_id:
134121
input_task.session_id = str(uuid.uuid4())
135122

136123
try:
137-
# Initialize memory store
138124
memory_store = await DatabaseFactory.get_database(user_id=user_id)
139125

140-
# Create a new Plan object
141-
plan = MPlan()
142-
# session_id=input_task.session_id,
143-
# team_id=input_task.team_id,
144-
# user_id=user_id,
145-
# initial_goal=input_task.description,
146-
# overall_status=PlanStatus.in_progress,
147-
# source=AgentType.PLANNER.value,
148-
# )
149-
150-
# setup and call the magentic orchestration
151-
magentic_orchestration = await orchestration_config.get_current_orchestration(
152-
user_id
153-
)
154-
155-
runtime = InProcessRuntime()
156-
runtime.start()
157-
158-
# invoke returns immediately, wait on result.get
159-
orchestration_result = await magentic_orchestration.invoke(
160-
task=input_task.description, runtime=runtime
126+
plan = Plan(
127+
session_id=input_task.session_id,
128+
team_id=input_task.team_id,
129+
user_id=user_id,
130+
initial_goal=input_task.description,
131+
overall_status=PlanStatus.in_progress,
132+
source=AgentType.PLANNER.value,
161133
)
162-
team_result = await orchestration_result.get()
163134

164-
# Save the plan to the database
165135
await memory_store.add_plan(plan)
166136

167-
# Log successful plan creation
168137
track_event_if_configured(
169138
"PlanCreated",
170139
{
@@ -353,7 +322,7 @@ async def upload_team_config_endpoint(request: Request, file: UploadFile = File(
353322
"team_id": team_id,
354323
"name": team_config.name,
355324
"message": "Team configuration uploaded and saved successfully",
356-
"team": team_config.model_dump(), # Return the full team configuration
325+
"team": team_config.model_dump() # Return the full team configuration
357326
}
358327

359328
except HTTPException:
@@ -654,28 +623,3 @@ async def get_search_indexes_endpoint(request: Request):
654623
except Exception as e:
655624
logging.error(f"Error retrieving search indexes: {str(e)}")
656625
raise HTTPException(status_code=500, detail="Internal server error occurred")
657-
658-
659-
# WebSocket endpoint for job status updates
660-
plans = {} # job_id -> current plan
661-
approvals = {} # job_id -> True/False/None
662-
sockets = {} # job_id -> WebSocket
663-
664-
665-
@app_v3.websocket("/ws/{job_id}")
666-
async def ws(job_id: str, websocket: WebSocket):
667-
await websocket.accept()
668-
sockets[job_id] = websocket
669-
try:
670-
if job_id in plans:
671-
await websocket.send_json({"type": "plan_ready", "plan": plans[job_id]})
672-
while True:
673-
msg = await websocket.receive_json()
674-
if msg.get("type") == "approve":
675-
approvals[job_id] = True
676-
await websocket.send_json({"type": "ack", "message": "approved"})
677-
elif msg.get("type") == "reject":
678-
approvals[job_id] = False
679-
await websocket.send_json({"type": "ack", "message": "rejected"})
680-
except WebSocketDisconnect:
681-
sockets.pop(job_id, None)

0 commit comments

Comments
 (0)