event.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from dataclasses import dataclass
  2. from datetime import datetime
  3. from enum import Enum
  4. from openhands.events.tool import ToolCallMetadata
  5. from openhands.llm.metrics import Metrics
  6. class EventSource(str, Enum):
  7. AGENT = 'agent'
  8. USER = 'user'
  9. ENVIRONMENT = 'environment'
  10. class FileEditSource(str, Enum):
  11. LLM_BASED_EDIT = 'llm_based_edit'
  12. OH_ACI = 'oh_aci' # openhands-aci
  13. class FileReadSource(str, Enum):
  14. OH_ACI = 'oh_aci' # openhands-aci
  15. DEFAULT = 'default'
  16. @dataclass
  17. class Event:
  18. @property
  19. def message(self) -> str | None:
  20. if hasattr(self, '_message'):
  21. return self._message # type: ignore[attr-defined]
  22. return ''
  23. @property
  24. def id(self) -> int:
  25. if hasattr(self, '_id'):
  26. return self._id # type: ignore[attr-defined]
  27. return -1
  28. @property
  29. def timestamp(self):
  30. if hasattr(self, '_timestamp') and isinstance(self._timestamp, str):
  31. return self._timestamp
  32. @timestamp.setter
  33. def timestamp(self, value: datetime) -> None:
  34. if isinstance(value, datetime):
  35. self._timestamp = value.isoformat()
  36. @property
  37. def source(self) -> EventSource | None:
  38. if hasattr(self, '_source'):
  39. return self._source # type: ignore[attr-defined]
  40. return None
  41. @property
  42. def cause(self) -> int | None:
  43. if hasattr(self, '_cause'):
  44. return self._cause # type: ignore[attr-defined]
  45. return None
  46. @property
  47. def timeout(self) -> int | None:
  48. if hasattr(self, '_timeout'):
  49. return self._timeout # type: ignore[attr-defined]
  50. return None
  51. @timeout.setter
  52. def timeout(self, value: int | None) -> None:
  53. self._timeout = value
  54. # Check if .blocking is an attribute of the event
  55. if hasattr(self, 'blocking'):
  56. # .blocking needs to be set to True if .timeout is set
  57. self.blocking = True
  58. # optional metadata, LLM call cost of the edit
  59. @property
  60. def llm_metrics(self) -> Metrics | None:
  61. if hasattr(self, '_llm_metrics'):
  62. return self._llm_metrics # type: ignore[attr-defined]
  63. return None
  64. @llm_metrics.setter
  65. def llm_metrics(self, value: Metrics) -> None:
  66. self._llm_metrics = value
  67. # optional field
  68. @property
  69. def tool_call_metadata(self) -> ToolCallMetadata | None:
  70. if hasattr(self, '_tool_call_metadata'):
  71. return self._tool_call_metadata # type: ignore[attr-defined]
  72. return None
  73. @tool_call_metadata.setter
  74. def tool_call_metadata(self, value: ToolCallMetadata) -> None:
  75. self._tool_call_metadata = value