event.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from dataclasses import asdict
  2. from datetime import datetime
  3. from openhands.events import Event, EventSource
  4. from openhands.events.observation.observation import Observation
  5. from openhands.events.serialization.action import action_from_dict
  6. from openhands.events.serialization.observation import observation_from_dict
  7. from openhands.events.serialization.utils import remove_fields
  8. # TODO: move `content` into `extras`
  9. TOP_KEYS = ['id', 'timestamp', 'source', 'message', 'cause', 'action', 'observation']
  10. UNDERSCORE_KEYS = ['id', 'timestamp', 'source', 'cause']
  11. DELETE_FROM_TRAJECTORY_EXTRAS = {
  12. 'screenshot',
  13. 'dom_object',
  14. 'axtree_object',
  15. 'active_page_index',
  16. 'last_browser_action',
  17. 'last_browser_action_error',
  18. 'focused_element_bid',
  19. 'extra_element_properties',
  20. }
  21. DELETE_FROM_MEMORY_EXTRAS = DELETE_FROM_TRAJECTORY_EXTRAS | {'open_pages_urls'}
  22. def event_from_dict(data) -> 'Event':
  23. evt: Event
  24. if 'action' in data:
  25. evt = action_from_dict(data)
  26. elif 'observation' in data:
  27. evt = observation_from_dict(data)
  28. else:
  29. raise ValueError('Unknown event type: ' + data)
  30. for key in UNDERSCORE_KEYS:
  31. if key in data:
  32. value = data[key]
  33. if key == 'timestamp' and isinstance(value, datetime):
  34. value = value.isoformat()
  35. if key == 'source':
  36. value = EventSource(value)
  37. setattr(evt, '_' + key, value)
  38. return evt
  39. def event_to_dict(event: 'Event') -> dict:
  40. props = asdict(event)
  41. d = {}
  42. for key in TOP_KEYS:
  43. if hasattr(event, key) and getattr(event, key) is not None:
  44. d[key] = getattr(event, key)
  45. elif hasattr(event, f'_{key}') and getattr(event, f'_{key}') is not None:
  46. d[key] = getattr(event, f'_{key}')
  47. if key == 'id' and d.get('id') == -1:
  48. d.pop('id', None)
  49. if key == 'timestamp' and 'timestamp' in d:
  50. if isinstance(d['timestamp'], datetime):
  51. d['timestamp'] = d['timestamp'].isoformat()
  52. if key == 'source' and 'source' in d:
  53. d['source'] = d['source'].value
  54. props.pop(key, None)
  55. if 'security_risk' in props and props['security_risk'] is None:
  56. props.pop('security_risk')
  57. if 'action' in d:
  58. d['args'] = props
  59. if event.timeout is not None:
  60. d['timeout'] = event.timeout
  61. elif 'observation' in d:
  62. d['content'] = props.pop('content', '')
  63. d['extras'] = props
  64. else:
  65. raise ValueError('Event must be either action or observation')
  66. return d
  67. def event_to_trajectory(event: 'Event') -> dict:
  68. d = event_to_dict(event)
  69. if 'extras' in d:
  70. remove_fields(d['extras'], DELETE_FROM_TRAJECTORY_EXTRAS)
  71. return d
  72. def event_to_memory(event: 'Event', max_message_chars: int) -> dict:
  73. d = event_to_dict(event)
  74. d.pop('id', None)
  75. d.pop('cause', None)
  76. d.pop('timestamp', None)
  77. d.pop('message', None)
  78. d.pop('images_urls', None)
  79. # runnable actions have some extra fields used in the BE/FE, which should not be sent to the LLM
  80. if 'args' in d:
  81. d['args'].pop('blocking', None)
  82. d['args'].pop('keep_prompt', None)
  83. d['args'].pop('is_confirmed', None)
  84. if 'extras' in d:
  85. remove_fields(d['extras'], DELETE_FROM_MEMORY_EXTRAS)
  86. if isinstance(event, Observation) and 'content' in d:
  87. d['content'] = truncate_content(d['content'], max_message_chars)
  88. return d
  89. def truncate_content(content: str, max_chars: int) -> str:
  90. """Truncate the middle of the observation content if it is too long."""
  91. if len(content) <= max_chars or max_chars == -1:
  92. return content
  93. # truncate the middle and include a message to the LLM about it
  94. half = max_chars // 2
  95. return (
  96. content[:half]
  97. + '\n[... Observation truncated due to length ...]\n'
  98. + content[-half:]
  99. )