( events: list[dict[str, Any]], *, model: str, started: float, finished: float, output_model_schema: type[AgentStructuredOutput] | None, process_error: str | None, )
| 3978 | |
| 3979 | |
| 3980 | def _history_from_events( |
| 3981 | events: list[dict[str, Any]], |
| 3982 | *, |
| 3983 | model: str, |
| 3984 | started: float, |
| 3985 | finished: float, |
| 3986 | output_model_schema: type[AgentStructuredOutput] | None, |
| 3987 | process_error: str | None, |
| 3988 | ) -> AgentHistoryList[AgentStructuredOutput]: |
| 3989 | events = _events_after_terminal_rollbacks(_events_after_terminal_compaction(events)) |
| 3990 | final_result = _structured_result_text(_result_from_events(events), output_model_schema) |
| 3991 | failure = process_error or _failure_from_events(events) |
| 3992 | if final_result is None and failure is not None: |
| 3993 | final_result = _structured_result_text(_last_streamed_assistant_text_from_events(events), output_model_schema) |
| 3994 | if final_result is None and failure is None: |
| 3995 | failure = _recoverable_failure_from_events(events) |
| 3996 | if final_result is None and failure is None: |
| 3997 | failure = 'Rust terminal session did not produce a final result.' |
| 3998 | is_done = final_result is not None and failure is None |
| 3999 | attachments = _attachments_from_events(events) |
| 4000 | history_items = _history_items_from_terminal_turns( |
| 4001 | events, |
| 4002 | started=started, |
| 4003 | finished=finished, |
| 4004 | final_result=final_result, |
| 4005 | attachments=attachments, |
| 4006 | failure=failure, |
| 4007 | is_done=is_done, |
| 4008 | ) |
| 4009 | if history_items is not None: |
| 4010 | history_list: AgentHistoryList[AgentStructuredOutput] = AgentHistoryList( |
| 4011 | history=history_items, |
| 4012 | usage=_usage_from_events(events, model), |
| 4013 | ) |
| 4014 | history_list._output_model_schema = output_model_schema |
| 4015 | return history_list |
| 4016 | tool_calls = _tool_calls_with_final_done( |
| 4017 | _tool_started_calls(events), |
| 4018 | final_result=final_result, |
| 4019 | attachments=attachments, |
| 4020 | is_done=is_done, |
| 4021 | ) |
| 4022 | history = AgentHistory( |
| 4023 | model_output=_model_output_from_tool_calls(tool_calls, events), |
| 4024 | result=_action_results_from_tool_calls( |
| 4025 | tool_calls, |
| 4026 | events, |
| 4027 | final_result=final_result, |
| 4028 | attachments=attachments, |
| 4029 | failure=failure, |
| 4030 | is_done=is_done, |
| 4031 | ), |
| 4032 | state=_browser_state_from_events(events), |
| 4033 | metadata=StepMetadata(step_start_time=started, step_end_time=finished, step_number=max(1, len(events))), |
| 4034 | ) |
| 4035 | history_list: AgentHistoryList[AgentStructuredOutput] = AgentHistoryList( |
| 4036 | history=[history], |
| 4037 | usage=_usage_from_events(events, model), |
searching dependent graphs…