Skip to content

Commit b4e7176

Browse files
committed
close the client to avoid error
1 parent 69860ac commit b4e7176

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/backend/app_kernel.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,17 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
124124
kernel, memory_store = await initialize_runtime_and_context(
125125
input_task.session_id, user_id
126126
)
127+
client = None
128+
try:
129+
client = config.get_ai_project_client()
130+
except Exception as client_exc:
131+
logging.error(f"Error creating AIProjectClient: {client_exc}")
132+
127133
agents = await AgentFactory.create_all_agents(
128134
session_id=input_task.session_id,
129135
user_id=user_id,
130136
memory_store=memory_store,
137+
client=client,
131138
)
132139

133140
group_chat_manager = agents[AgentType.GROUP_CHAT_MANAGER.value]
@@ -161,7 +168,11 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
161168
"description": input_task.description,
162169
},
163170
)
164-
171+
if client:
172+
try:
173+
client.close()
174+
except Exception as e:
175+
logging.error(f"Error sending to AIProjectClient: {e}")
165176
return {
166177
"status": f"Plan created with ID: {plan.id}",
167178
"session_id": input_task.session_id,
@@ -249,8 +260,18 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
249260
kernel, memory_store = await initialize_runtime_and_context(
250261
human_feedback.session_id, user_id
251262
)
263+
264+
client = None
265+
try:
266+
client = config.get_ai_project_client()
267+
except Exception as client_exc:
268+
logging.error(f"Error creating AIProjectClient: {client_exc}")
269+
252270
agents = await AgentFactory.create_all_agents(
253-
session_id=human_feedback.session_id, user_id=user_id, memory_store=memory_store
271+
session_id=human_feedback.session_id,
272+
user_id=user_id,
273+
memory_store=memory_store,
274+
client=client,
254275
)
255276

256277
# Send the feedback to the human agent
@@ -267,7 +288,11 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
267288
"step_id": human_feedback.step_id,
268289
},
269290
)
270-
291+
if client:
292+
try:
293+
client.close()
294+
except Exception as e:
295+
logging.error(f"Error sending to AIProjectClient: {e}")
271296
return {
272297
"status": "Feedback received",
273298
"session_id": human_feedback.session_id,
@@ -333,10 +358,16 @@ async def human_clarification_endpoint(
333358
kernel, memory_store = await initialize_runtime_and_context(
334359
human_clarification.session_id, user_id
335360
)
361+
client = None
362+
try:
363+
client = config.get_ai_project_client()
364+
except Exception as client_exc:
365+
logging.error(f"Error creating AIProjectClient: {client_exc}")
336366
agents = await AgentFactory.create_all_agents(
337367
session_id=human_clarification.session_id,
338368
user_id=user_id,
339369
memory_store=memory_store,
370+
client=client,
340371
)
341372

342373
# Send the feedback to the human agent
@@ -354,7 +385,11 @@ async def human_clarification_endpoint(
354385
"session_id": human_clarification.session_id,
355386
},
356387
)
357-
388+
if client:
389+
try:
390+
client.close()
391+
except Exception as e:
392+
logging.error(f"Error sending to AIProjectClient: {e}")
358393
return {
359394
"status": "Clarification received",
360395
"session_id": human_clarification.session_id,
@@ -427,17 +462,28 @@ async def approve_step_endpoint(
427462
kernel, memory_store = await initialize_runtime_and_context(
428463
human_feedback.session_id, user_id
429464
)
465+
client = None
466+
try:
467+
client = config.get_ai_project_client()
468+
except Exception as client_exc:
469+
logging.error(f"Error creating AIProjectClient: {client_exc}")
430470
agents = await AgentFactory.create_all_agents(
431471
session_id=human_feedback.session_id,
432472
user_id=user_id,
433473
memory_store=memory_store,
474+
client=client,
434475
)
435476

436477
# Send the approval to the group chat manager
437478
group_chat_manager = agents[AgentType.GROUP_CHAT_MANAGER.value]
438479

439480
await group_chat_manager.handle_human_feedback(human_feedback)
440481

482+
if client:
483+
try:
484+
client.close()
485+
except Exception as e:
486+
logging.error(f"Error sending to AIProjectClient: {e}")
441487
# Return a status message
442488
if human_feedback.step_id:
443489
track_event_if_configured(

src/backend/kernel_agents/agent_factory.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ async def create_all_agents(
239239
user_id: str,
240240
temperature: float = 0.0,
241241
memory_store: Optional[CosmosMemoryContext] = None,
242+
client: Optional[Any] = None,
242243
) -> Dict[AgentType, BaseAgent]:
243244
"""Create all agent types for a session in a specific order.
244245
@@ -265,6 +266,13 @@ async def create_all_agents(
265266
planner_agent_type = AgentType.PLANNER
266267
group_chat_manager_type = AgentType.GROUP_CHAT_MANAGER
267268

269+
try:
270+
if client is None:
271+
# Create the AIProjectClient instance using the config
272+
# This is a placeholder; replace with actual client creation logic
273+
client = config.get_ai_project_client()
274+
except Exception as client_exc:
275+
logger.error(f"Error creating AIProjectClient: {client_exc}")
268276
# Initialize cache for this session if it doesn't exist
269277
if session_id not in cls._agent_cache:
270278
cls._agent_cache[session_id] = {}
@@ -280,6 +288,7 @@ async def create_all_agents(
280288
session_id=session_id,
281289
user_id=user_id,
282290
temperature=temperature,
291+
client=client,
283292
memory_store=memory_store,
284293
)
285294

@@ -305,6 +314,7 @@ async def create_all_agents(
305314
user_id=user_id,
306315
temperature=temperature,
307316
agent_instances=agent_instances, # Pass agent instances to the planner
317+
client=client,
308318
response_format=ResponseFormatJsonSchemaType(
309319
json_schema=ResponseFormatJsonSchema(
310320
name=PlannerResponsePlan.__name__,
@@ -324,6 +334,7 @@ async def create_all_agents(
324334
session_id=session_id,
325335
user_id=user_id,
326336
temperature=temperature,
337+
client=client,
327338
agent_instances=agent_instances, # Pass agent instances to the planner
328339
)
329340
agents[group_chat_manager_type] = group_chat_manager

0 commit comments

Comments
 (0)