openhands.memory.condenser.condenser
CONDENSER_METADATA_KEY
Key identifying where metadata is stored in a State
object's extra_data
field.
get_condensation_metadata
def get_condensation_metadata(state: State) -> list[dict[str, Any]]
Utility function to retrieve a list of metadata batches from a State
.
Arguments:
state
- The state to retrieve metadata from.
Returns:
list[dict[str, Any]]: A list of metadata batches, each representing a condensation.
CONDENSER_REGISTRY
Registry of condenser configurations to their corresponding condenser classes.
Condenser Objects
class Condenser(ABC)
Abstract condenser interface.
Condensers take a list of Event
objects and reduce them into a potentially smaller list.
Agents can use condensers to reduce the amount of events they need to consider when deciding which action to take. To use a condenser, agents can call the condensed_history
method on the current State
being considered and use the results instead of the full history.
Example usage::
condenser = Condenser.from_config(condenser_config) events = condenser.condensed_history(state)
add_metadata
def add_metadata(key: str, value: Any) -> None
Add information to the current metadata batch.
Any key/value pairs added to the metadata batch will be recorded in the State
at the end of the current condensation.
Arguments:
-
key
- The key to store the metadata under. -
value
- The metadata to store.
write_metadata
def write_metadata(state: State) -> None
Write the current batch of metadata to the State
.
Resets the current metadata batch: any metadata added after this call will be stored in a new batch and written to the State
at the end of the next condensation.
metadata_batch
@contextmanager
def metadata_batch(state: State)
Context manager to ensure batched metadata is always written to the State
.
condense
@abstractmethod
def condense(events: list[Event]) -> list[Event]
Condense a sequence of events into a potentially smaller list.
New condenser strategies should override this method to implement their own condensation logic. Call self.add_metadata
in the implementation to record any relevant per-condensation diagnostic information.
Arguments:
events
- A list of events representing the entire history of the agent.
Returns:
list[Event]
- An event sequence representing a condensed history of the agent.
condensed_history
def condensed_history(state: State) -> list[Event]
Condense the state's history.
register_config
@classmethod
def register_config(cls, configuration_type: type[CondenserConfig]) -> None
Register a new condenser configuration type.
Instances of registered configuration types can be passed to from_config
to create instances of the corresponding condenser.
Arguments:
configuration_type
- The type of configuration used to create instances of the condenser.
Raises:
ValueError
- If the configuration type is already registered.
from_config
@classmethod
def from_config(cls, config: CondenserConfig) -> Condenser
Create a condenser from a configuration object.
Arguments:
config
- Configuration for the condenser.
Returns:
Condenser
- A condenser instance.
Raises:
ValueError
- If the condenser type is not recognized.
RollingCondenser Objects
class RollingCondenser(Condenser, ABC)
Base class for a specialized condenser strategy that applies condensation to a rolling history.
The rolling history is computed by appending new events to the most recent condensation. For example, the sequence of calls::
assert state.history == [event1, event2, event3] condensation = condenser.condensed_history(state)
...new events are added to the state...
assert state.history == [event1, event2, event3, event4, event5] condenser.condensed_history(state)
will result in second call to condensed_history
passing condensation + [event4, event5]
to the condense
method.