@@ -202,69 +202,55 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
202202 if not input_task .session_id :
203203 input_task .session_id = str (uuid .uuid4 ())
204204
205+ # Wrap initialization and agent creation in its own try block for setup errors
205206 try :
206- # Create all agents instead of just the planner agent
207- # This ensures other agents are created first and the planner has access to them
208207 kernel , memory_store = await initialize_runtime_and_context (
209208 input_task .session_id , user_id
210209 )
211- client = None
212- try :
213- client = config .get_ai_project_client ()
214- except Exception as client_exc :
215- logging .error (f"Error creating AIProjectClient: { client_exc } " )
216-
210+ client = config .get_ai_project_client ()
217211 agents = await AgentFactory .create_all_agents (
218212 session_id = input_task .session_id ,
219213 user_id = user_id ,
220214 memory_store = memory_store ,
221215 client = client ,
222216 )
217+ except Exception as setup_exc :
218+ logging .error (f"Failed to initialize agents or context: { setup_exc } " )
219+ track_event_if_configured (
220+ "InputTaskSetupError" ,
221+ {"session_id" : input_task .session_id , "error" : str (setup_exc )},
222+ )
223+ raise HTTPException (
224+ status_code = 500 , detail = "Could not initialize services for your request."
225+ ) from setup_exc
223226
227+ try :
224228 group_chat_manager = agents [AgentType .GROUP_CHAT_MANAGER .value ]
225-
226- # Convert input task to JSON for the kernel function, add user_id here
227-
228- # Use the planner to handle the task
229229 await group_chat_manager .handle_input_task (input_task )
230230
231- # Get plan from memory store
232231 plan = await memory_store .get_plan_by_session (input_task .session_id )
233-
234- if not plan : # If the plan is not found, raise an error
232+ if not plan :
235233 track_event_if_configured (
236234 "PlanNotFound" ,
237- {
238- "status" : "Plan not found" ,
239- "session_id" : input_task .session_id ,
240- "description" : input_task .description ,
241- },
235+ {"status" : "Plan not found" , "session_id" : input_task .session_id },
242236 )
243237 raise HTTPException (status_code = 404 , detail = "Plan not found" )
244- # Log custom event for successful input task processing
238+
245239 track_event_if_configured (
246240 "InputTaskProcessed" ,
247- {
248- "status" : f"Plan created with ID: { plan .id } " ,
249- "session_id" : input_task .session_id ,
250- "plan_id" : plan .id ,
251- "description" : input_task .description ,
252- },
241+ {"status" : f"Plan created with ID: { plan .id } " , "session_id" : input_task .session_id },
253242 )
254- if client :
255- try :
256- client .close ()
257- except Exception as e :
258- logging .error (f"Error sending to AIProjectClient: { e } " )
259243 return {
260244 "status" : f"Plan created with ID: { plan .id } " ,
261245 "session_id" : input_task .session_id ,
262246 "plan_id" : plan .id ,
263247 "description" : input_task .description ,
264248 }
265-
249+ except HTTPException :
250+ # Re-raise HTTPExceptions so they are not caught by the generic block
251+ raise
266252 except Exception as e :
267- # Extract clean error message for rate limit errors
253+ # This now specifically handles errors during task processing
268254 error_msg = str (e )
269255 if "Rate limit is exceeded" in error_msg :
270256 match = re .search (r"Rate limit is exceeded\. Try again in (\d+) seconds?\." , error_msg )
@@ -273,13 +259,16 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
273259
274260 track_event_if_configured (
275261 "InputTaskError" ,
276- {
277- "session_id" : input_task .session_id ,
278- "description" : input_task .description ,
279- "error" : str (e ),
280- },
262+ {"session_id" : input_task .session_id , "error" : str (e )},
281263 )
282- raise HTTPException (status_code = 400 , detail = f"{ error_msg } " ) from e
264+ raise HTTPException (status_code = 400 , detail = f"Error processing plan: { error_msg } " ) from e
265+ finally :
266+ # Ensure the client is closed even if an error occurs
267+ if 'client' in locals () and client :
268+ try :
269+ client .close ()
270+ except Exception as e :
271+ logging .error (f"Error closing AIProjectClient: { e } " )
283272
284273
285274@app .post ("/api/human_feedback" )
0 commit comments