4343from utils_kernel import get_agents , initialize_runtime_and_context , rai_success
4444
4545# # Check if the Application Insights Instrumentation Key is set in the environment variables
46- connection_string = os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" )
47- if connection_string :
48- # Configure Application Insights if the Instrumentation Key is found
49- configure_azure_monitor (connection_string = connection_string )
50- logging .info (
51- "Application Insights configured with the provided Instrumentation Key"
52- )
53- else :
54- # Log a warning if the Instrumentation Key is not found
55- logging .warning (
56- "No Application Insights Instrumentation Key found. Skipping configuration"
57- )
46+ # connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
47+ # if connection_string:
48+ # # Configure Application Insights if the Instrumentation Key is found
49+ # configure_azure_monitor(connection_string=connection_string)
50+ # logging.info(
51+ # "Application Insights configured with the provided Instrumentation Key"
52+ # )
53+ # else:
54+ # # Log a warning if the Instrumentation Key is not found
55+ # logging.warning(
56+ # "No Application Insights Instrumentation Key found. Skipping configuration"
57+ # )
5858
5959# Configure logging
6060logging .basicConfig (level = logging .INFO )
@@ -129,10 +129,17 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
129129 kernel , memory_store = await initialize_runtime_and_context (
130130 input_task .session_id , user_id
131131 )
132+ client = None
133+ try :
134+ client = config .get_ai_project_client ()
135+ except Exception as client_exc :
136+ logging .error (f"Error creating AIProjectClient: { client_exc } " )
137+
132138 agents = await AgentFactory .create_all_agents (
133139 session_id = input_task .session_id ,
134140 user_id = user_id ,
135141 memory_store = memory_store ,
142+ client = client ,
136143 )
137144
138145 group_chat_manager = agents [AgentType .GROUP_CHAT_MANAGER .value ]
@@ -166,7 +173,11 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
166173 "description" : input_task .description ,
167174 },
168175 )
169-
176+ if client :
177+ try :
178+ client .close ()
179+ except Exception as e :
180+ logging .error (f"Error sending to AIProjectClient: { e } " )
170181 return {
171182 "status" : f"Plan created with ID: { plan .id } " ,
172183 "session_id" : input_task .session_id ,
@@ -254,12 +265,31 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
254265 kernel , memory_store = await initialize_runtime_and_context (
255266 human_feedback .session_id , user_id
256267 )
257- agents = await AgentFactory .create_all_agents (
258- session_id = human_feedback .session_id , user_id = user_id , memory_store = memory_store
268+
269+ client = None
270+ try :
271+ client = config .get_ai_project_client ()
272+ except Exception as client_exc :
273+ logging .error (f"Error creating AIProjectClient: { client_exc } " )
274+
275+ human_agent = await AgentFactory .create_agent (
276+ agent_type = AgentType .HUMAN ,
277+ session_id = human_feedback .session_id ,
278+ user_id = user_id ,
279+ memory_store = memory_store ,
280+ client = client ,
259281 )
260282
261- # Send the feedback to the human agent
262- human_agent = agents [AgentType .HUMAN .value ]
283+ if human_agent is None :
284+ track_event_if_configured (
285+ "AgentNotFound" ,
286+ {
287+ "status" : "Agent not found" ,
288+ "session_id" : human_feedback .session_id ,
289+ "step_id" : human_feedback .step_id ,
290+ },
291+ )
292+ raise HTTPException (status_code = 404 , detail = "Agent not found" )
263293
264294 # Use the human agent to handle the feedback
265295 await human_agent .handle_human_feedback (human_feedback = human_feedback )
@@ -272,7 +302,11 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
272302 "step_id" : human_feedback .step_id ,
273303 },
274304 )
275-
305+ if client :
306+ try :
307+ client .close ()
308+ except Exception as e :
309+ logging .error (f"Error sending to AIProjectClient: { e } " )
276310 return {
277311 "status" : "Feedback received" ,
278312 "session_id" : human_feedback .session_id ,
@@ -338,14 +372,30 @@ async def human_clarification_endpoint(
338372 kernel , memory_store = await initialize_runtime_and_context (
339373 human_clarification .session_id , user_id
340374 )
341- agents = await AgentFactory .create_all_agents (
375+ client = None
376+ try :
377+ client = config .get_ai_project_client ()
378+ except Exception as client_exc :
379+ logging .error (f"Error creating AIProjectClient: { client_exc } " )
380+
381+ human_agent = await AgentFactory .create_agent (
382+ agent_type = AgentType .HUMAN ,
342383 session_id = human_clarification .session_id ,
343384 user_id = user_id ,
344385 memory_store = memory_store ,
386+ client = client ,
345387 )
346388
347- # Send the feedback to the human agent
348- human_agent = agents [AgentType .HUMAN .value ]
389+ if human_agent is None :
390+ track_event_if_configured (
391+ "AgentNotFound" ,
392+ {
393+ "status" : "Agent not found" ,
394+ "session_id" : human_clarification .session_id ,
395+ "step_id" : human_clarification .step_id ,
396+ },
397+ )
398+ raise HTTPException (status_code = 404 , detail = "Agent not found" )
349399
350400 # Use the human agent to handle the feedback
351401 await human_agent .handle_human_clarification (
@@ -359,7 +409,11 @@ async def human_clarification_endpoint(
359409 "session_id" : human_clarification .session_id ,
360410 },
361411 )
362-
412+ if client :
413+ try :
414+ client .close ()
415+ except Exception as e :
416+ logging .error (f"Error sending to AIProjectClient: { e } " )
363417 return {
364418 "status" : "Clarification received" ,
365419 "session_id" : human_clarification .session_id ,
@@ -432,17 +486,28 @@ async def approve_step_endpoint(
432486 kernel , memory_store = await initialize_runtime_and_context (
433487 human_feedback .session_id , user_id
434488 )
489+ client = None
490+ try :
491+ client = config .get_ai_project_client ()
492+ except Exception as client_exc :
493+ logging .error (f"Error creating AIProjectClient: { client_exc } " )
435494 agents = await AgentFactory .create_all_agents (
436495 session_id = human_feedback .session_id ,
437496 user_id = user_id ,
438497 memory_store = memory_store ,
498+ client = client ,
439499 )
440500
441501 # Send the approval to the group chat manager
442502 group_chat_manager = agents [AgentType .GROUP_CHAT_MANAGER .value ]
443503
444504 await group_chat_manager .handle_human_feedback (human_feedback )
445505
506+ if client :
507+ try :
508+ client .close ()
509+ except Exception as e :
510+ logging .error (f"Error sending to AIProjectClient: { e } " )
446511 # Return a status message
447512 if human_feedback .step_id :
448513 track_event_if_configured (
0 commit comments