@@ -1351,7 +1351,6 @@ async def _arun(
1351
1351
session_state = self ._update_session_state (session = agent_session , session_state = session_state )
1352
1352
1353
1353
self .model = cast (Model , self .model )
1354
-
1355
1354
# 6. Determine tools for model
1356
1355
await self ._adetermine_tools_for_model (
1357
1356
model = self .model ,
@@ -1385,97 +1384,102 @@ async def _arun(
1385
1384
if len (run_messages .messages ) == 0 :
1386
1385
log_error ("No messages to be sent to the model." )
1387
1386
1388
- run_messages = run_messages
1389
-
1390
- # Register run for cancellation tracking
1391
- register_run (run_response .run_id ) # type: ignore
1387
+ try :
1388
+ await self ._ahandle_reasoning (run_response = run_response , run_messages = run_messages )
1389
+ raise_if_cancelled (run_response .run_id ) # type: ignore
1392
1390
1393
- # 9. Generate a response from the Model (includes running function calls)
1394
- model_response : ModelResponse = await self .model .aresponse (
1395
- messages = run_messages .messages ,
1396
- tools = self ._tools_for_model ,
1397
- functions = self ._functions_for_model ,
1398
- tool_choice = self .tool_choice ,
1399
- tool_call_limit = self .tool_call_limit ,
1400
- response_format = response_format ,
1401
- )
1391
+ # Register run for cancellation tracking
1392
+ register_run (run_response .run_id ) # type: ignore
1393
+
1394
+ # 9. Generate a response from the Model (includes running function calls)
1395
+ model_response : ModelResponse = await self .model .aresponse (
1396
+ messages = run_messages .messages ,
1397
+ tools = self ._tools_for_model ,
1398
+ functions = self ._functions_for_model ,
1399
+ tool_choice = self .tool_choice ,
1400
+ tool_call_limit = self .tool_call_limit ,
1401
+ response_format = response_format ,
1402
+ )
1402
1403
1403
- # Check for cancellation after model call
1404
- try :
1404
+ # Check for cancellation after model call
1405
1405
raise_if_cancelled (run_response .run_id ) # type: ignore
1406
- except RunCancelledException as e :
1407
- # Handle run cancellation
1408
- log_info (f"Run { run_response .run_id } was cancelled" )
1409
- run_response .content = str (e )
1410
- run_response .status = RunStatus .cancelled
1411
1406
1412
- # Update the Agent Session before exiting
1413
- agent_session .upsert_run (run = run_response )
1414
- await self .asave_session (session = agent_session )
1407
+ # If an output model is provided, generate output using the output model
1408
+ await self ._agenerate_response_with_output_model (model_response = model_response , run_messages = run_messages )
1415
1409
1416
- return run_response
1410
+ # If a parser model is provided, structure the response separately
1411
+ await self ._aparse_response_with_parser_model (model_response = model_response , run_messages = run_messages )
1417
1412
1418
- # If an output model is provided, generate output using the output model
1419
- await self ._agenerate_response_with_output_model (model_response = model_response , run_messages = run_messages )
1413
+ # 10. Update the RunOutput with the model response
1414
+ self ._update_run_response (
1415
+ model_response = model_response , run_response = run_response , run_messages = run_messages
1416
+ )
1420
1417
1421
- # If a parser model is provided, structure the response separately
1422
- await self ._aparse_response_with_parser_model (model_response = model_response , run_messages = run_messages )
1418
+ if self .store_media :
1419
+ self ._store_media (run_response , model_response )
1420
+ else :
1421
+ self ._scrub_media_from_run_output (run_response )
1423
1422
1424
- # 10. Update the RunOutput with the model response
1425
- self ._update_run_response (model_response = model_response , run_response = run_response , run_messages = run_messages )
1423
+ # We should break out of the run function
1424
+ if any (tool_call .is_paused for tool_call in run_response .tools or []):
1425
+ return self ._handle_agent_run_paused (
1426
+ run_response = run_response , run_messages = run_messages , session = agent_session , user_id = user_id
1427
+ )
1426
1428
1427
- if self .store_media :
1428
- self ._store_media (run_response , model_response )
1429
- else :
1430
- self ._scrub_media_from_run_output (run_response )
1429
+ raise_if_cancelled (run_response .run_id ) # type: ignore
1431
1430
1432
- # We should break out of the run function
1433
- if any (tool_call .is_paused for tool_call in run_response .tools or []):
1434
- return self ._handle_agent_run_paused (
1431
+ # 11. Update Agent Memory
1432
+ async for _ in self ._amake_memories_and_summaries (
1435
1433
run_response = run_response , run_messages = run_messages , session = agent_session , user_id = user_id
1436
- )
1434
+ ):
1435
+ pass
1437
1436
1438
- # 11. Update Agent Memory
1439
- async for _ in self ._amake_memories_and_summaries (
1440
- run_response = run_response , run_messages = run_messages , session = agent_session , user_id = user_id
1441
- ):
1442
- pass
1437
+ # 12. Calculate session metrics
1438
+ self ._update_session_metrics (session = agent_session , run_response = run_response )
1443
1439
1444
- # 12. Calculate session metrics
1445
- self ._update_session_metrics (session = agent_session , run_response = run_response )
1440
+ run_response .status = RunStatus .completed
1446
1441
1447
- run_response .status = RunStatus .completed
1442
+ # Convert the response to the structured format if needed
1443
+ self ._convert_response_to_structured_format (run_response )
1448
1444
1449
- # Convert the response to the structured format if needed
1450
- self ._convert_response_to_structured_format (run_response )
1445
+ # Set the run duration
1446
+ if run_response .metrics :
1447
+ run_response .metrics .stop_timer ()
1451
1448
1452
- # Set the run duration
1453
- if run_response .metrics :
1454
- run_response .metrics .stop_timer ()
1449
+ # Optional: Save output to file if save_response_to_file is set
1450
+ self .save_run_response_to_file (
1451
+ run_response = run_response ,
1452
+ input = run_messages .user_message ,
1453
+ session_id = agent_session .session_id ,
1454
+ user_id = user_id ,
1455
+ )
1455
1456
1456
- # Optional: Save output to file if save_response_to_file is set
1457
- self .save_run_response_to_file (
1458
- run_response = run_response ,
1459
- input = run_messages .user_message ,
1460
- session_id = agent_session .session_id ,
1461
- user_id = user_id ,
1462
- )
1457
+ # 13. Add RunOutput to Agent Session
1458
+ agent_session .upsert_run (run = run_response )
1463
1459
1464
- # 13. Add RunOutput to Agent Session
1465
- agent_session . upsert_run ( run = run_response )
1460
+ # 14. Save session to storage
1461
+ await self . asave_session ( session = agent_session )
1466
1462
1467
- # 14. Save session to storage
1468
- await self .asave_session ( session = agent_session )
1463
+ # Log Agent Telemetry
1464
+ await self ._alog_agent_telemetry ( session_id = agent_session . session_id , run_id = run_response . run_id )
1469
1465
1470
- # Log Agent Telemetry
1471
- await self ._alog_agent_telemetry (session_id = agent_session .session_id , run_id = run_response .run_id )
1466
+ log_debug (f"Agent Run End: { run_response .run_id } " , center = True , symbol = "*" )
1472
1467
1473
- log_debug (f"Agent Run End: { run_response .run_id } " , center = True , symbol = "*" )
1468
+ # Always clean up the run tracking
1469
+ cleanup_run (run_response .run_id ) # type: ignore
1474
1470
1475
- # Always clean up the run tracking
1476
- cleanup_run (run_response .run_id ) # type: ignore
1471
+ return run_response
1472
+ except RunCancelledException as e :
1473
+ # Handle run cancellation
1474
+ log_info (f"Run { run_response .run_id } was cancelled" )
1475
+ run_response .content = str (e )
1476
+ run_response .status = RunStatus .cancelled
1477
1477
1478
- return run_response
1478
+ # Update the Agent Session before exiting
1479
+ agent_session .upsert_run (run = run_response )
1480
+ await self .asave_session (session = agent_session )
1481
+
1482
+ return run_response
1479
1483
1480
1484
async def _arun_stream (
1481
1485
self ,
0 commit comments