diff --git a/core/src/main/java/com/google/adk/agents/ReadonlyContext.java b/core/src/main/java/com/google/adk/agents/ReadonlyContext.java index 1cdac16de..bad8e7294 100644 --- a/core/src/main/java/com/google/adk/agents/ReadonlyContext.java +++ b/core/src/main/java/com/google/adk/agents/ReadonlyContext.java @@ -17,9 +17,8 @@ package com.google.adk.agents; import com.google.adk.events.Event; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.genai.types.Content; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -28,6 +27,8 @@ public class ReadonlyContext { protected final InvocationContext invocationContext; + private List eventsView; + private Map stateView; public ReadonlyContext(InvocationContext invocationContext) { this.invocationContext = invocationContext; @@ -59,22 +60,26 @@ public String sessionId() { } /** - * Returns a read-only view of the events of the current session. + * Returns an unmodifiable view of the events of the session. * - *

This is a shallow copy and if the underlying values of the list are modified, the read-only - * view will also be modified. + *

Warning: This is a live view, not a snapshot. */ - public ImmutableList events() { - return ImmutableList.copyOf(invocationContext.session().events()); + public List events() { + if (eventsView == null) { + eventsView = Collections.unmodifiableList(invocationContext.session().events()); + } + return eventsView; } /** - * Returns a read-only view of the state of the current session. + * Returns an unmodifiable view of the state of the session. * - *

This is a shallow copy and if the underlying values of the map are modified, the read-only - * view will also be modified. + *

Warning: This is a live view, not a snapshot. */ public Map state() { - return ImmutableMap.copyOf(invocationContext.session().state()); + if (stateView == null) { + stateView = Collections.unmodifiableMap(invocationContext.session().state()); + } + return stateView; } }