mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Remove unused event_to_memory function from serialization code (#7412)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
parent
4c103761f9
commit
0fec237ead
@ -4,7 +4,6 @@ from openhands.events.serialization.action import (
|
||||
from openhands.events.serialization.event import (
|
||||
event_from_dict,
|
||||
event_to_dict,
|
||||
event_to_memory,
|
||||
event_to_trajectory,
|
||||
)
|
||||
from openhands.events.serialization.observation import (
|
||||
@ -15,7 +14,6 @@ __all__ = [
|
||||
'action_from_dict',
|
||||
'event_from_dict',
|
||||
'event_to_dict',
|
||||
'event_to_memory',
|
||||
'event_to_trajectory',
|
||||
'observation_from_dict',
|
||||
]
|
||||
|
||||
@ -44,8 +44,6 @@ DELETE_FROM_TRAJECTORY_EXTRAS = {
|
||||
'extra_element_properties',
|
||||
}
|
||||
|
||||
DELETE_FROM_MEMORY_EXTRAS = DELETE_FROM_TRAJECTORY_EXTRAS | {'open_pages_urls'}
|
||||
|
||||
|
||||
def event_from_dict(data) -> 'Event':
|
||||
evt: Event
|
||||
@ -142,26 +140,6 @@ def event_to_trajectory(event: 'Event') -> dict:
|
||||
return d
|
||||
|
||||
|
||||
def event_to_memory(event: 'Event', max_message_chars: int) -> dict:
|
||||
d = event_to_dict(event)
|
||||
d.pop('id', None)
|
||||
d.pop('cause', None)
|
||||
d.pop('timestamp', None)
|
||||
d.pop('message', None)
|
||||
d.pop('image_urls', None)
|
||||
|
||||
# runnable actions have some extra fields used in the BE/FE, which should not be sent to the LLM
|
||||
if 'args' in d:
|
||||
d['args'].pop('blocking', None)
|
||||
d['args'].pop('confirmation_state', None)
|
||||
|
||||
if 'extras' in d:
|
||||
remove_fields(d['extras'], DELETE_FROM_MEMORY_EXTRAS)
|
||||
if isinstance(event, Observation) and 'content' in d:
|
||||
d['content'] = truncate_content(d['content'], max_message_chars)
|
||||
return d
|
||||
|
||||
|
||||
def truncate_content(content: str, max_chars: int | None = None) -> str:
|
||||
"""Truncate the middle of the observation content if it is too long."""
|
||||
if max_chars is None or len(content) <= max_chars or max_chars < 0:
|
||||
|
||||
@ -16,7 +16,6 @@ from openhands.events.action.files import FileEditSource, FileReadSource
|
||||
from openhands.events.serialization import (
|
||||
event_from_dict,
|
||||
event_to_dict,
|
||||
event_to_memory,
|
||||
)
|
||||
|
||||
|
||||
@ -40,22 +39,6 @@ def serialization_deserialization(
|
||||
serialized_action_dict == original_action_dict
|
||||
), 'The serialized action should match the original action dict.'
|
||||
|
||||
# memory dict is what is sent to the LLM
|
||||
serialized_action_memory = event_to_memory(action_instance, max_message_chars)
|
||||
original_memory_dict = original_action_dict.copy()
|
||||
|
||||
# we don't send backend properties like id
|
||||
original_memory_dict.pop('id', None)
|
||||
original_memory_dict.pop('timestamp', None)
|
||||
if 'args' in original_memory_dict:
|
||||
original_memory_dict['args'].pop('blocking', None)
|
||||
original_memory_dict['args'].pop('confirmation_state', None)
|
||||
|
||||
# the rest should match
|
||||
assert (
|
||||
serialized_action_memory == original_memory_dict
|
||||
), 'The serialized action in memory should match the original action dict.'
|
||||
|
||||
|
||||
def test_event_props_serialization_deserialization():
|
||||
original_action_dict = {
|
||||
|
||||
@ -12,7 +12,6 @@ from openhands.events.observation.agent import MicroagentKnowledge
|
||||
from openhands.events.serialization import (
|
||||
event_from_dict,
|
||||
event_to_dict,
|
||||
event_to_memory,
|
||||
event_to_trajectory,
|
||||
)
|
||||
from openhands.events.serialization.observation import observation_from_dict
|
||||
@ -30,21 +29,12 @@ def serialization_deserialization(
|
||||
), f'The observation instance should be an instance of {cls}.'
|
||||
serialized_observation_dict = event_to_dict(observation_instance)
|
||||
serialized_observation_trajectory = event_to_trajectory(observation_instance)
|
||||
serialized_observation_memory = event_to_memory(
|
||||
observation_instance, max_message_chars
|
||||
)
|
||||
assert (
|
||||
serialized_observation_dict == original_observation_dict
|
||||
), 'The serialized observation should match the original observation dict.'
|
||||
assert (
|
||||
serialized_observation_trajectory == original_observation_dict
|
||||
), 'The serialized observation trajectory should match the original observation dict.'
|
||||
original_observation_dict.pop('message', None)
|
||||
original_observation_dict.pop('id', None)
|
||||
original_observation_dict.pop('timestamp', None)
|
||||
assert (
|
||||
serialized_observation_memory == original_observation_dict
|
||||
), 'The serialized observation memory should match the original observation dict.'
|
||||
|
||||
|
||||
# Additional tests for various observation subclasses can be included here
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user