browse.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from dataclasses import dataclass, field
  2. from opendevin.core.schema import ObservationType
  3. from opendevin.events.utils import remove_fields
  4. from .observation import Observation
  5. @dataclass
  6. class BrowserOutputObservation(Observation):
  7. """
  8. This data class represents the output of a browser.
  9. """
  10. url: str
  11. screenshot: str = field(repr=False) # don't show in repr
  12. status_code: int = 200
  13. error: bool = False
  14. observation: str = ObservationType.BROWSE
  15. # do not include in the memory
  16. open_pages_urls: list = field(default_factory=list)
  17. active_page_index: int = -1
  18. dom_object: dict = field(default_factory=dict, repr=False) # don't show in repr
  19. axtree_object: dict = field(default_factory=dict, repr=False) # don't show in repr
  20. last_browser_action: str = ''
  21. focused_element_bid: str = ''
  22. def to_dict(self):
  23. dictionary = super().to_dict()
  24. # add screenshot for frontend showcase only, not for agent consumption
  25. dictionary['screenshot'] = self.screenshot
  26. return dictionary
  27. def to_memory(self) -> dict:
  28. memory_dict = super().to_memory()
  29. # remove some fields from the memory, as currently they are too big for LLMs
  30. remove_fields(
  31. memory_dict['extras'],
  32. {
  33. 'screenshot',
  34. 'dom_object',
  35. 'axtree_object',
  36. 'open_pages_urls',
  37. 'active_page_index',
  38. 'last_browser_action',
  39. 'focused_element_bid',
  40. },
  41. )
  42. return memory_dict
  43. @property
  44. def message(self) -> str:
  45. return 'Visited ' + self.url