MCPcopy
hub / github.com/browser-use/browser-use / AgentHistoryList

Class AgentHistoryList

browser_use/agent/views.py:595–946  ·  view source on GitHub ↗

List of AgentHistory messages, i.e. the history of the agent's actions and thoughts.

Source from the content-addressed store, hash-verified

593
594
595class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
596 """List of AgentHistory messages, i.e. the history of the agent's actions and thoughts."""
597
598 history: list[AgentHistory]
599 usage: UsageSummary | None = None
600
601 _output_model_schema: type[AgentStructuredOutput] | None = None
602
603 def total_duration_seconds(self) -> float:
604 """Get total duration of all steps in seconds"""
605 total = 0.0
606 for h in self.history:
607 if h.metadata:
608 total += h.metadata.duration_seconds
609 return total
610
611 def __len__(self) -> int:
612 """Return the number of history items"""
613 return len(self.history)
614
615 def __str__(self) -> str:
616 """Representation of the AgentHistoryList object"""
617 return f'AgentHistoryList(all_results={self.action_results()}, all_model_outputs={self.model_actions()})'
618
619 def add_item(self, history_item: AgentHistory) -> None:
620 """Add a history item to the list"""
621 self.history.append(history_item)
622
623 def __repr__(self) -> str:
624 """Representation of the AgentHistoryList object"""
625 return self.__str__()
626
627 def save_to_file(self, filepath: str | Path, sensitive_data: dict[str, str | dict[str, str]] | None = None) -> None:
628 """Save history to JSON file with proper serialization and optional sensitive data filtering"""
629 try:
630 Path(filepath).parent.mkdir(parents=True, exist_ok=True)
631 data = self.model_dump(sensitive_data=sensitive_data)
632 with open(filepath, 'w', encoding='utf-8') as f:
633 json.dump(data, f, indent=2, ensure_ascii=False)
634 except Exception as e:
635 raise e
636
637 # def save_as_playwright_script(
638 # self,
639 # output_path: str | Path,
640 # sensitive_data_keys: list[str] | None = None,
641 # browser_config: BrowserConfig | None = None,
642 # context_config: BrowserContextConfig | None = None,
643 # ) -> None:
644 # """
645 # Generates a Playwright script based on the agent's history and saves it to a file.
646 # Args:
647 # output_path: The path where the generated Python script will be saved.
648 # sensitive_data_keys: A list of keys used as placeholders for sensitive data
649 # (e.g., ['username_placeholder', 'password_placeholder']).
650 # These will be loaded from environment variables in the
651 # generated script.
652 # browser_config: Configuration of the original Browser instance.

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…