Skip to content

Commit 7f7a995

Browse files
committed
fix: bring back removed reasoning piece
1 parent d267b00 commit 7f7a995

File tree

1 file changed

+73
-69
lines changed

1 file changed

+73
-69
lines changed

libs/agno/agno/agent/agent.py

Lines changed: 73 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ async def _arun(
13511351
session_state = self._update_session_state(session=agent_session, session_state=session_state)
13521352

13531353
self.model = cast(Model, self.model)
1354-
13551354
# 6. Determine tools for model
13561355
await self._adetermine_tools_for_model(
13571356
model=self.model,
@@ -1385,97 +1384,102 @@ async def _arun(
13851384
if len(run_messages.messages) == 0:
13861385
log_error("No messages to be sent to the model.")
13871386

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
13921390

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+
)
14021403

1403-
# Check for cancellation after model call
1404-
try:
1404+
# Check for cancellation after model call
14051405
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
14111406

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)
14151409

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)
14171412

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+
)
14201417

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)
14231422

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+
)
14261428

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
14311430

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(
14351433
run_response=run_response, run_messages=run_messages, session=agent_session, user_id=user_id
1436-
)
1434+
):
1435+
pass
14371436

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)
14431439

1444-
# 12. Calculate session metrics
1445-
self._update_session_metrics(session=agent_session, run_response=run_response)
1440+
run_response.status = RunStatus.completed
14461441

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)
14481444

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()
14511448

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+
)
14551456

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)
14631459

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)
14661462

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)
14691465

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="*")
14721467

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
14741470

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
14771477

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
14791483

14801484
async def _arun_stream(
14811485
self,

0 commit comments

Comments
 (0)