Skip to content

Commit 864d606

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Refactor EventsCompactionConfig to require a summarizer
This change makes the BaseEventSummarizer a mandatory part of the EventsCompactionConfig, removing the Optional wrapper. Consequently, the Runner no longer attempts to create a default LlmEventSummarizer if one is not provided in the config. Callers must now explicitly supply a summarizer when enabling event compaction. PiperOrigin-RevId: 855190727
1 parent 54c826c commit 864d606

8 files changed

Lines changed: 117 additions & 67 deletions

File tree

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929
import com.google.adk.events.EventActions;
3030
import com.google.adk.flows.llmflows.ResumabilityConfig;
3131
import com.google.adk.memory.BaseMemoryService;
32-
import com.google.adk.models.Model;
3332
import com.google.adk.plugins.BasePlugin;
3433
import com.google.adk.plugins.PluginManager;
3534
import com.google.adk.sessions.BaseSessionService;
3635
import com.google.adk.sessions.InMemorySessionService;
3736
import com.google.adk.sessions.Session;
3837
import com.google.adk.summarizer.EventsCompactionConfig;
39-
import com.google.adk.summarizer.LlmEventSummarizer;
4038
import com.google.adk.summarizer.SlidingWindowEventCompactor;
4139
import com.google.adk.tools.BaseTool;
4240
import com.google.adk.tools.FunctionTool;
@@ -254,10 +252,7 @@ protected Runner(
254252
this.memoryService = memoryService;
255253
this.pluginManager = new PluginManager(plugins);
256254
this.resumabilityConfig = resumabilityConfig;
257-
this.eventsCompactionConfig =
258-
Optional.ofNullable(eventsCompactionConfig)
259-
.map(c -> createEventsCompactionConfig(agent, c))
260-
.orElse(null);
255+
this.eventsCompactionConfig = eventsCompactionConfig;
261256
}
262257

263258
/**
@@ -793,27 +788,5 @@ private boolean hasLiveRequestQueueParameter(FunctionTool functionTool) {
793788
.anyMatch(parameter -> parameter.getType().equals(LiveRequestQueue.class));
794789
}
795790

796-
/**
797-
* Creates a new {@link EventsCompactionConfig} based on the given configuration. If the {@link
798-
* com.google.adk.summarizer.BaseEventSummarizer} is missing, it will be default to the {@link
799-
* LlmEventSummarizer} using the same model as the LLM base agent.
800-
*/
801-
private static EventsCompactionConfig createEventsCompactionConfig(
802-
BaseAgent agent, EventsCompactionConfig config) {
803-
return new EventsCompactionConfig(
804-
config.compactionInterval(),
805-
config.overlapSize(),
806-
config
807-
.summarizer()
808-
.or(
809-
() ->
810-
Optional.of(agent)
811-
.filter(LlmAgent.class::isInstance)
812-
.map(LlmAgent.class::cast)
813-
.map(LlmAgent::resolvedModel)
814-
.flatMap(Model::model)
815-
.map(LlmEventSummarizer::new)));
816-
}
817-
818791
// TODO: run statelessly
819792
}

core/src/main/java/com/google/adk/summarizer/BaseEventSummarizer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.adk.summarizer;
218

319
import com.google.adk.events.Event;
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
package com.google.adk.summarizer;
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import java.util.Optional;
17+
package com.google.adk.summarizer;
418

519
/**
620
* Configuration for event compaction.
@@ -10,12 +24,7 @@
1024
* @param overlapSize The number of preceding invocations to include from the end of the last
1125
* compacted range. This creates an overlap between consecutive compacted summaries, maintaining
1226
* context.
13-
* @param summarizer An optional event summarizer to use for compaction.
27+
* @param summarizer An event summarizer to use for compaction.
1428
*/
1529
public record EventsCompactionConfig(
16-
int compactionInterval, int overlapSize, Optional<BaseEventSummarizer> summarizer) {
17-
18-
public EventsCompactionConfig(int compactionInterval, int overlapSize) {
19-
this(compactionInterval, overlapSize, Optional.empty());
20-
}
21-
}
30+
int compactionInterval, int overlapSize, BaseEventSummarizer summarizer) {}

core/src/main/java/com/google/adk/summarizer/LlmEventSummarizer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.adk.summarizer;
218

319
import static java.util.function.Predicate.not;

core/src/main/java/com/google/adk/summarizer/SlidingWindowEventCompactor.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.adk.summarizer;
218

319
import com.google.adk.events.Event;
@@ -24,15 +40,9 @@ public final class SlidingWindowEventCompactor implements EventCompactor {
2440
private static final Logger logger = LoggerFactory.getLogger(SlidingWindowEventCompactor.class);
2541

2642
private final EventsCompactionConfig config;
27-
private final BaseEventSummarizer summarizer;
2843

2944
public SlidingWindowEventCompactor(EventsCompactionConfig config) {
3045
this.config = config;
31-
this.summarizer =
32-
config
33-
.summarizer()
34-
.orElseThrow(
35-
() -> new IllegalArgumentException("Summarizer is required for event compaction."));
3646
}
3747

3848
/**
@@ -91,7 +101,7 @@ public Completable compact(Session session, BaseSessionService sessionService) {
91101

92102
return Completable.fromMaybe(
93103
getCompactionEvents(session)
94-
.flatMap(summarizer::summarizeEvents)
104+
.flatMap(config.summarizer()::summarizeEvents)
95105
.flatMapSingle(e -> sessionService.appendEvent(session, e)));
96106
}
97107

core/src/test/java/com/google/adk/runner/RunnerTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.adk.plugins.BasePlugin;
4141
import com.google.adk.sessions.Session;
4242
import com.google.adk.summarizer.EventsCompactionConfig;
43+
import com.google.adk.summarizer.LlmEventSummarizer;
4344
import com.google.adk.testing.TestLlm;
4445
import com.google.adk.testing.TestUtils;
4546
import com.google.adk.testing.TestUtils.EchoTool;
@@ -123,17 +124,18 @@ public void tearDown() {
123124

124125
@Test
125126
public void eventsCompaction_enabled() {
126-
LlmAgent agent =
127-
createTestAgent(
128-
createTestLlm(
129-
createLlmResponse(createContent("llm 1")),
130-
createLlmResponse(createContent("summary 1")),
131-
createLlmResponse(createContent("llm 2")),
132-
createLlmResponse(createContent("summary 2"))));
127+
TestLlm testLlm =
128+
createTestLlm(
129+
createLlmResponse(createContent("llm 1")),
130+
createLlmResponse(createContent("summary 1")),
131+
createLlmResponse(createContent("llm 2")),
132+
createLlmResponse(createContent("summary 2")));
133+
LlmAgent agent = createTestAgent(testLlm);
133134

134135
Runner runner =
135136
Runner.builder()
136-
.eventsCompactionConfig(new EventsCompactionConfig(1, 0))
137+
.eventsCompactionConfig(
138+
new EventsCompactionConfig(1, 0, new LlmEventSummarizer(testLlm)))
137139
.agent(agent)
138140
.sessionService(this.runner.sessionService())
139141
.appName(this.runner.appName())

core/src/test/java/com/google/adk/summarizer/LlmEventSummarizerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.adk.summarizer;
218

319
import static com.google.common.truth.Truth.assertThat;

core/src/test/java/com/google/adk/summarizer/SlidingWindowEventCompactorTest.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.adk.summarizer;
218

319
import static com.google.common.truth.Truth.assertThat;
@@ -20,7 +36,6 @@
2036
import io.reactivex.rxjava3.core.Maybe;
2137
import io.reactivex.rxjava3.core.Single;
2238
import java.util.List;
23-
import java.util.Optional;
2439
import org.junit.Rule;
2540
import org.junit.Test;
2641
import org.junit.runner.RunWith;
@@ -42,8 +57,7 @@ public class SlidingWindowEventCompactorTest {
4257
@Test
4358
public void compaction_noEvents() {
4459
EventCompactor compactor =
45-
new SlidingWindowEventCompactor(
46-
new EventsCompactionConfig(2, 1, Optional.of(mockSummarizer)));
60+
new SlidingWindowEventCompactor(new EventsCompactionConfig(2, 1, mockSummarizer));
4761
Session session = Session.builder("id").build();
4862

4963
compactor.compact(session, mockSessionService).blockingSubscribe();
@@ -53,8 +67,7 @@ public void compaction_noEvents() {
5367
@Test
5468
public void compaction_notEnoughInvocations() {
5569
EventCompactor compactor =
56-
new SlidingWindowEventCompactor(
57-
new EventsCompactionConfig(2, 1, Optional.of(mockSummarizer)));
70+
new SlidingWindowEventCompactor(new EventsCompactionConfig(2, 1, mockSummarizer));
5871
Session session =
5972
Session.builder("id")
6073
.events(ImmutableList.of(Event.builder().invocationId("1").build()))
@@ -67,8 +80,7 @@ public void compaction_notEnoughInvocations() {
6780
@Test
6881
public void compaction_notEnoughInvocationsAfterCompact() {
6982
EventCompactor compactor =
70-
new SlidingWindowEventCompactor(
71-
new EventsCompactionConfig(2, 0, Optional.of(mockSummarizer)));
83+
new SlidingWindowEventCompactor(new EventsCompactionConfig(2, 0, mockSummarizer));
7284
Session session =
7385
Session.builder("id")
7486
.events(
@@ -87,8 +99,7 @@ public void compaction_notEnoughInvocationsAfterCompact() {
8799
@Test
88100
public void compaction_firstCompaction() {
89101
EventCompactor compactor =
90-
new SlidingWindowEventCompactor(
91-
new EventsCompactionConfig(2, 1, Optional.of(mockSummarizer)));
102+
new SlidingWindowEventCompactor(new EventsCompactionConfig(2, 1, mockSummarizer));
92103
// Add 4 events without any compaction event
93104
ImmutableList<Event> events =
94105
ImmutableList.of(
@@ -110,8 +121,7 @@ public void compaction_firstCompaction() {
110121
@Test
111122
public void compaction_withOverlap() {
112123
EventCompactor compactor =
113-
new SlidingWindowEventCompactor(
114-
new EventsCompactionConfig(2, 1, Optional.of(mockSummarizer)));
124+
new SlidingWindowEventCompactor(new EventsCompactionConfig(2, 1, mockSummarizer));
115125
// First 2 events are compacted, plus three uncompacted events
116126
ImmutableList<Event> events =
117127
ImmutableList.of(
@@ -141,8 +151,7 @@ public void compaction_withOverlap() {
141151
@Test
142152
public void compaction_multipleEventsWithSameInvocation() {
143153
EventCompactor compactor =
144-
new SlidingWindowEventCompactor(
145-
new EventsCompactionConfig(1, 1, Optional.of(mockSummarizer)));
154+
new SlidingWindowEventCompactor(new EventsCompactionConfig(1, 1, mockSummarizer));
146155
ImmutableList<Event> events =
147156
ImmutableList.of(
148157
Event.builder().invocationId("1").timestamp(1).build(),
@@ -171,8 +180,7 @@ public void compaction_multipleEventsWithSameInvocation() {
171180
@Test
172181
public void compaction_noCompactionEventFromSummarizer() {
173182
EventCompactor compactor =
174-
new SlidingWindowEventCompactor(
175-
new EventsCompactionConfig(1, 0, Optional.of(mockSummarizer)));
183+
new SlidingWindowEventCompactor(new EventsCompactionConfig(1, 0, mockSummarizer));
176184
ImmutableList<Event> events =
177185
ImmutableList.of(Event.builder().invocationId("1").timestamp(1).build());
178186
Session session = Session.builder("id").events(events).build();

0 commit comments

Comments
 (0)