Skip to content

Commit 39d6ed2

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Performance improvements
Removed some logging and removed creation of unnecessary objects. PiperOrigin-RevId: 855488421
1 parent c6c9557 commit 39d6ed2

File tree

8 files changed

+96
-93
lines changed

8 files changed

+96
-93
lines changed

core/src/main/java/com/google/adk/agents/CallbackPlugin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.google.adk.agents;
1717

18-
import static com.google.common.collect.ImmutableList.toImmutableList;
19-
2018
import com.google.adk.agents.Callbacks.AfterAgentCallback;
2119
import com.google.adk.agents.Callbacks.AfterAgentCallbackBase;
2220
import com.google.adk.agents.Callbacks.AfterAgentCallbackSync;
@@ -74,7 +72,7 @@ public String getName() {
7472

7573
@SuppressWarnings("unchecked") // The builder ensures that the type is correct.
7674
private <T> ImmutableList<T> getCallbacks(Class<T> type) {
77-
return callbacks.get(type).stream().map(callback -> (T) callback).collect(toImmutableList());
75+
return (ImmutableList<T>) callbacks.get(type);
7876
}
7977

8078
public ImmutableList<? extends Callbacks.BeforeAgentCallback> getBeforeAgentCallback() {

core/src/main/java/com/google/adk/events/EventActions.java

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,40 @@
3030
@JsonDeserialize(builder = EventActions.Builder.class)
3131
public class EventActions {
3232

33-
private Optional<Boolean> skipSummarization = Optional.empty();
34-
private ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
35-
private ConcurrentMap<String, Part> artifactDelta = new ConcurrentHashMap<>();
36-
private Optional<String> transferToAgent = Optional.empty();
37-
private Optional<Boolean> escalate = Optional.empty();
38-
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs =
39-
new ConcurrentHashMap<>();
40-
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations =
41-
new ConcurrentHashMap<>();
42-
private Optional<Boolean> endInvocation = Optional.empty();
43-
private Optional<EventCompaction> compaction = Optional.empty();
33+
private Optional<Boolean> skipSummarization;
34+
private ConcurrentMap<String, Object> stateDelta;
35+
private ConcurrentMap<String, Part> artifactDelta;
36+
private Optional<String> transferToAgent;
37+
private Optional<Boolean> escalate;
38+
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs;
39+
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations;
40+
private Optional<Boolean> endInvocation;
41+
private Optional<EventCompaction> compaction;
4442

4543
/** Default constructor for Jackson. */
46-
public EventActions() {}
44+
public EventActions() {
45+
this.skipSummarization = Optional.empty();
46+
this.stateDelta = new ConcurrentHashMap<>();
47+
this.artifactDelta = new ConcurrentHashMap<>();
48+
this.transferToAgent = Optional.empty();
49+
this.escalate = Optional.empty();
50+
this.requestedAuthConfigs = new ConcurrentHashMap<>();
51+
this.requestedToolConfirmations = new ConcurrentHashMap<>();
52+
this.endInvocation = Optional.empty();
53+
this.compaction = Optional.empty();
54+
}
55+
56+
private EventActions(Builder builder) {
57+
this.skipSummarization = builder.skipSummarization;
58+
this.stateDelta = builder.stateDelta;
59+
this.artifactDelta = builder.artifactDelta;
60+
this.transferToAgent = builder.transferToAgent;
61+
this.escalate = builder.escalate;
62+
this.requestedAuthConfigs = builder.requestedAuthConfigs;
63+
this.requestedToolConfirmations = builder.requestedToolConfirmations;
64+
this.endInvocation = builder.endInvocation;
65+
this.compaction = builder.compaction;
66+
}
4767

4868
@JsonProperty("skipSummarization")
4969
public Optional<Boolean> skipSummarization() {
@@ -191,19 +211,27 @@ public int hashCode() {
191211

192212
/** Builder for {@link EventActions}. */
193213
public static class Builder {
194-
private Optional<Boolean> skipSummarization = Optional.empty();
195-
private ConcurrentMap<String, Object> stateDelta = new ConcurrentHashMap<>();
196-
private ConcurrentMap<String, Part> artifactDelta = new ConcurrentHashMap<>();
197-
private Optional<String> transferToAgent = Optional.empty();
198-
private Optional<Boolean> escalate = Optional.empty();
199-
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs =
200-
new ConcurrentHashMap<>();
201-
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations =
202-
new ConcurrentHashMap<>();
203-
private Optional<Boolean> endInvocation = Optional.empty();
204-
private Optional<EventCompaction> compaction = Optional.empty();
205-
206-
public Builder() {}
214+
private Optional<Boolean> skipSummarization;
215+
private ConcurrentMap<String, Object> stateDelta;
216+
private ConcurrentMap<String, Part> artifactDelta;
217+
private Optional<String> transferToAgent;
218+
private Optional<Boolean> escalate;
219+
private ConcurrentMap<String, ConcurrentMap<String, Object>> requestedAuthConfigs;
220+
private ConcurrentMap<String, ToolConfirmation> requestedToolConfirmations;
221+
private Optional<Boolean> endInvocation;
222+
private Optional<EventCompaction> compaction;
223+
224+
public Builder() {
225+
this.skipSummarization = Optional.empty();
226+
this.stateDelta = new ConcurrentHashMap<>();
227+
this.artifactDelta = new ConcurrentHashMap<>();
228+
this.transferToAgent = Optional.empty();
229+
this.escalate = Optional.empty();
230+
this.requestedAuthConfigs = new ConcurrentHashMap<>();
231+
this.requestedToolConfirmations = new ConcurrentHashMap<>();
232+
this.endInvocation = Optional.empty();
233+
this.compaction = Optional.empty();
234+
}
207235

208236
private Builder(EventActions eventActions) {
209237
this.skipSummarization = eventActions.skipSummarization();
@@ -284,48 +312,20 @@ public Builder compaction(EventCompaction value) {
284312

285313
@CanIgnoreReturnValue
286314
public Builder merge(EventActions other) {
287-
if (other.skipSummarization().isPresent()) {
288-
this.skipSummarization = other.skipSummarization();
289-
}
290-
if (other.stateDelta() != null) {
291-
this.stateDelta.putAll(other.stateDelta());
292-
}
293-
if (other.artifactDelta() != null) {
294-
this.artifactDelta.putAll(other.artifactDelta());
295-
}
296-
if (other.transferToAgent().isPresent()) {
297-
this.transferToAgent = other.transferToAgent();
298-
}
299-
if (other.escalate().isPresent()) {
300-
this.escalate = other.escalate();
301-
}
302-
if (other.requestedAuthConfigs() != null) {
303-
this.requestedAuthConfigs.putAll(other.requestedAuthConfigs());
304-
}
305-
if (other.requestedToolConfirmations() != null) {
306-
this.requestedToolConfirmations.putAll(other.requestedToolConfirmations());
307-
}
308-
if (other.endInvocation().isPresent()) {
309-
this.endInvocation = other.endInvocation();
310-
}
311-
if (other.compaction().isPresent()) {
312-
this.compaction = other.compaction();
313-
}
315+
other.skipSummarization().ifPresent(this::skipSummarization);
316+
this.stateDelta.putAll(other.stateDelta());
317+
this.artifactDelta.putAll(other.artifactDelta());
318+
other.transferToAgent().ifPresent(this::transferToAgent);
319+
other.escalate().ifPresent(this::escalate);
320+
this.requestedAuthConfigs.putAll(other.requestedAuthConfigs());
321+
this.requestedToolConfirmations.putAll(other.requestedToolConfirmations());
322+
other.endInvocation().ifPresent(this::endInvocation);
323+
other.compaction().ifPresent(this::compaction);
314324
return this;
315325
}
316326

317327
public EventActions build() {
318-
EventActions eventActions = new EventActions();
319-
eventActions.setSkipSummarization(this.skipSummarization);
320-
eventActions.setStateDelta(this.stateDelta);
321-
eventActions.setArtifactDelta(this.artifactDelta);
322-
eventActions.setTransferToAgent(this.transferToAgent);
323-
eventActions.setEscalate(this.escalate);
324-
eventActions.setRequestedAuthConfigs(this.requestedAuthConfigs);
325-
eventActions.setRequestedToolConfirmations(this.requestedToolConfirmations);
326-
eventActions.setEndInvocation(this.endInvocation);
327-
eventActions.setCompaction(this.compaction);
328-
return eventActions;
328+
return new EventActions(this);
329329
}
330330
}
331331
}

core/src/main/java/com/google/adk/flows/llmflows/Functions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public static Set<String> getLongRunningFunctionCalls(
368368
continue;
369369
}
370370
BaseTool tool = tools.get(functionCall.name().get());
371-
if (tool.longRunning()) {
371+
if (tool != null && tool.longRunning()) {
372372
longRunningFunctionCalls.add(functionCall.id().orElse(""));
373373
}
374374
}

core/src/main/java/com/google/adk/flows/llmflows/RequestConfirmationLlmRequestProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
6060
InvocationContext invocationContext, LlmRequest llmRequest) {
6161
ImmutableList<Event> events = ImmutableList.copyOf(invocationContext.session().events());
6262
if (events.isEmpty()) {
63-
logger.info(
63+
logger.trace(
6464
"No events are present in the session. Skipping request confirmation processing.");
6565
return Single.just(RequestProcessingResult.create(llmRequest, ImmutableList.of()));
6666
}
@@ -93,7 +93,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
9393
}
9494
}
9595
if (responses.isEmpty()) {
96-
logger.info("No request confirmation function responses found.");
96+
logger.trace("No request confirmation function responses found.");
9797
return Single.just(RequestProcessingResult.create(llmRequest, ImmutableList.of()));
9898
}
9999

core/src/main/java/com/google/adk/sessions/InMemorySessionService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Maybe<Session> getSession(
127127
Session sessionCopy = copySession(storedSession);
128128

129129
// Apply filtering based on config directly to the mutable list in the copy
130-
GetSessionConfig config = configOpt.orElse(GetSessionConfig.builder().build());
130+
GetSessionConfig config = configOpt.orElseGet(() -> GetSessionConfig.builder().build());
131131
List<Event> eventsInCopy = sessionCopy.events();
132132

133133
config
@@ -257,8 +257,8 @@ public Single<Event> appendEvent(Session session, Event event) {
257257

258258
// --- Update the session stored in this service ---
259259
sessions
260-
.getOrDefault(appName, new ConcurrentHashMap<>())
261-
.getOrDefault(userId, new ConcurrentHashMap<>())
260+
.computeIfAbsent(appName, k -> new ConcurrentHashMap<>())
261+
.computeIfAbsent(userId, k -> new ConcurrentHashMap<>())
262262
.put(sessionId, session);
263263

264264
mergeWithGlobalState(appName, userId, session);

core/src/main/java/com/google/adk/sessions/SessionJsonConverter.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ static String convertEventToJson(Event event) {
9494
actionsJson.put("transferAgent", event.actions().transferToAgent());
9595
actionsJson.put("escalate", event.actions().escalate());
9696
actionsJson.put("requestedAuthConfigs", event.actions().requestedAuthConfigs());
97+
actionsJson.put("requestedToolConfirmations", event.actions().requestedToolConfirmations());
98+
actionsJson.put("endInvocation", event.actions().endInvocation());
99+
actionsJson.put("compaction", event.actions().compaction());
97100
eventJson.put("actions", actionsJson);
98101
}
99102
if (event.content().isPresent()) {
@@ -146,26 +149,25 @@ private static Content convertMapToContent(Object rawContentValue) {
146149
*/
147150
@SuppressWarnings("unchecked")
148151
static Event fromApiEvent(Map<String, Object> apiEvent) {
149-
EventActions eventActions = new EventActions();
152+
EventActions.Builder eventActionsBuilder = EventActions.builder();
150153
if (apiEvent.get("actions") != null) {
151154
Map<String, Object> actionsMap = (Map<String, Object>) apiEvent.get("actions");
152-
eventActions.setSkipSummarization(
153-
Optional.ofNullable(actionsMap.get("skipSummarization")).map(value -> (Boolean) value));
154-
eventActions.setStateDelta(
155+
if (actionsMap.get("skipSummarization") != null) {
156+
eventActionsBuilder.skipSummarization((Boolean) actionsMap.get("skipSummarization"));
157+
}
158+
eventActionsBuilder.stateDelta(
155159
actionsMap.get("stateDelta") != null
156160
? new ConcurrentHashMap<>((Map<String, Object>) actionsMap.get("stateDelta"))
157161
: new ConcurrentHashMap<>());
158-
eventActions.setArtifactDelta(
162+
eventActionsBuilder.artifactDelta(
159163
actionsMap.get("artifactDelta") != null
160164
? convertToArtifactDeltaMap(actionsMap.get("artifactDelta"))
161165
: new ConcurrentHashMap<>());
162-
eventActions.setTransferToAgent(
163-
actionsMap.get("transferAgent") != null
164-
? (String) actionsMap.get("transferAgent")
165-
: null);
166-
eventActions.setEscalate(
167-
Optional.ofNullable(actionsMap.get("escalate")).map(value -> (Boolean) value));
168-
eventActions.setRequestedAuthConfigs(
166+
eventActionsBuilder.transferToAgent((String) actionsMap.get("transferAgent"));
167+
if (actionsMap.get("escalate") != null) {
168+
eventActionsBuilder.escalate((Boolean) actionsMap.get("escalate"));
169+
}
170+
eventActionsBuilder.requestedAuthConfigs(
169171
Optional.ofNullable(actionsMap.get("requestedAuthConfigs"))
170172
.map(SessionJsonConverter::asConcurrentMapOfConcurrentMaps)
171173
.orElse(new ConcurrentHashMap<>()));
@@ -176,7 +178,7 @@ static Event fromApiEvent(Map<String, Object> apiEvent) {
176178
.id((String) Iterables.getLast(Splitter.on('/').split(apiEvent.get("name").toString())))
177179
.invocationId((String) apiEvent.get("invocationId"))
178180
.author((String) apiEvent.get("author"))
179-
.actions(eventActions)
181+
.actions(eventActionsBuilder.build())
180182
.content(
181183
Optional.ofNullable(apiEvent.get("content"))
182184
.map(SessionJsonConverter::convertMapToContent)

core/src/main/java/com/google/adk/tools/AgentTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Optional<FunctionDeclaration> declaration() {
109109
public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContext toolContext) {
110110

111111
if (this.skipSummarization) {
112-
toolContext.actions().setSkipSummarization(true);
112+
toolContext.setActions(toolContext.actions().toBuilder().skipSummarization(true).build());
113113
}
114114

115115
Optional<Schema> agentInputSchema = Optional.empty();

core/src/test/java/com/google/adk/sessions/SessionJsonConverterTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public final class SessionJsonConverterTest {
2828

2929
@Test
3030
public void convertEventToJson_fullEvent_success() throws JsonProcessingException {
31-
EventActions actions = new EventActions();
32-
actions.setSkipSummarization(Optional.of(true));
33-
actions.setStateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key", "value")));
34-
actions.setArtifactDelta(
35-
new ConcurrentHashMap<>(ImmutableMap.of("artifact", Part.fromText("artifact_text"))));
36-
actions.setTransferToAgent("agent");
37-
actions.setEscalate(Optional.of(true));
31+
EventActions actions =
32+
EventActions.builder()
33+
.skipSummarization(true)
34+
.stateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key", "value")))
35+
.artifactDelta(
36+
new ConcurrentHashMap<>(
37+
ImmutableMap.of("artifact", Part.fromText("artifact_text"))))
38+
.transferToAgent("agent")
39+
.escalate(true)
40+
.build();
3841

3942
Event event =
4043
Event.builder()

0 commit comments

Comments
 (0)