# Class SlidingWindowEventCompactor

java.lang.Object
com.google.adk.summarizer.SlidingWindowEventCompactor

All Implemented Interfaces:
  `EventCompactor`

public final class SlidingWindowEventCompactor extends Object implements EventCompactor
This class performs events compaction in a sliding window fashion based on the `EventsCompactionConfig`.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SlidingWindowEventCompactor(EventsCompactionConfig config)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `io.reactivex.rxjava3.core.Completable`
  `compact(Session session, BaseSessionService sessionService)`
  Runs compaction for SlidingWindowCompactor.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### SlidingWindowEventCompactor

    public SlidingWindowEventCompactor(EventsCompactionConfig config)

- ## Method Details

  - ### compact

    public io.reactivex.rxjava3.core.Completable compact(Session session, BaseSessionService sessionService)
    Runs compaction for SlidingWindowCompactor.

    This method implements the sliding window compaction logic. It determines if enough new invocations have occurred since the last compaction based on `EventsCompactionConfig.compactionInterval()`. If so, it selects a range of events to compact based on `EventsCompactionConfig.overlapSize()`, and calls `BaseEventSummarizer.summarizeEvents(List)`.

    The compaction process is controlled by two parameters:

    1. `EventsCompactionConfig.compactionInterval()`: The number of *new* user-initiated invocations that, once fully represented in the session's events, will trigger a compaction. 2. `overlap_size`: The number of preceding invocations to include from the end of the last compacted range. This creates an overlap between consecutive compacted summaries, maintaining context.

    The compactor is called after an agent has finished processing a turn and all its events have been added to the session. It checks if a new compaction is needed.

    When a compaction is triggered: - The compactor identifies the range of `invocation_id`s to be summarized. - This range starts `overlap_size` invocations before the beginning of the new block of `compaction_invocation_threshold` invocations and ends with the last invocation in the current block. - A `CompactedEvent` is created, summarizing all events within this determined `invocation_id` range. This `CompactedEvent` is then appended to the session.

    Here is an example with `compaction_invocation_threshold = 2` and `overlap_size = 1`: Let's assume events are added for `invocation_id`s 1, 2, 3, and 4 in order.

    1. **After `invocation_id` 2 events are added:** - The session now contains events for invocations 1 and 2. This fulfills the `compaction_invocation_threshold = 2` criteria. - Since this is the first compaction, the range starts from the beginning. - A `CompactedEvent` is generated, summarizing events within `invocation_id` range [1, 2]. - The session now contains: `[ E(inv=1, role=user), E(inv=1, role=model), E(inv=2, role=user), E(inv=2, role=model), CompactedEvent(inv=[1, 2])]`.

    2. **After `invocation_id` 3 events are added:** - No compaction happens yet, because only 1 new invocation (`inv=3`) has been completed since the last compaction, and `compaction_invocation_threshold` is 2.

    3. **After `invocation_id` 4 events are added:** - The session now contains new events for invocations 3 and 4, again fulfilling `compaction_invocation_threshold = 2`. - The last `CompactedEvent` covered up to `invocation_id` 2. With `overlap_size = 1`, the new compaction range will start one invocation before the new block (inv 3), which is `invocation_id` 2. - The new compaction range is from `invocation_id` 2 to 4. - A new `CompactedEvent` is generated, summarizing events within `invocation_id` range [2, 4]. - The session now contains: `[ E(inv=1, role=user), E(inv=1, role=model), E(inv=2, role=user), E(inv=2, role=model), CompactedEvent(inv=[1, 2]), E(inv=3, role=user), E(inv=3, role=model), E(inv=4, role=user), E(inv=4, role=model), CompactedEvent(inv=[2, 4])]`.

    Specified by:
      `compact` in interface `EventCompactor`
    Parameters:
      `session` - the session containing the events to be compacted.
      `sessionService` - the session service for appending the new compaction event.
    Returns:
      the `Event` containing the events summary.