|
17 | 17 | package com.google.adk.agents; |
18 | 18 |
|
19 | 19 | import com.google.adk.events.Event; |
20 | | -import com.google.common.collect.ImmutableList; |
21 | | -import com.google.common.collect.ImmutableMap; |
22 | 20 | import com.google.genai.types.Content; |
| 21 | +import java.util.Collections; |
23 | 22 | import java.util.List; |
24 | 23 | import java.util.Map; |
25 | 24 | import java.util.Optional; |
|
28 | 27 | public class ReadonlyContext { |
29 | 28 |
|
30 | 29 | protected final InvocationContext invocationContext; |
| 30 | + private List<Event> eventsView; |
| 31 | + private Map<String, Object> stateView; |
31 | 32 |
|
32 | 33 | public ReadonlyContext(InvocationContext invocationContext) { |
33 | 34 | this.invocationContext = invocationContext; |
@@ -59,22 +60,26 @@ public String sessionId() { |
59 | 60 | } |
60 | 61 |
|
61 | 62 | /** |
62 | | - * Returns a read-only view of the events of the current session. |
| 63 | + * Returns an unmodifiable view of the events of the session. |
63 | 64 | * |
64 | | - * <p>This is a shallow copy and if the underlying values of the list are modified, the read-only |
65 | | - * view will also be modified. |
| 65 | + * <p><b>Warning:</b> This is a live view, not a snapshot. |
66 | 66 | */ |
67 | | - public ImmutableList<Event> events() { |
68 | | - return ImmutableList.copyOf(invocationContext.session().events()); |
| 67 | + public List<Event> events() { |
| 68 | + if (eventsView == null) { |
| 69 | + eventsView = Collections.unmodifiableList(invocationContext.session().events()); |
| 70 | + } |
| 71 | + return eventsView; |
69 | 72 | } |
70 | 73 |
|
71 | 74 | /** |
72 | | - * Returns a read-only view of the state of the current session. |
| 75 | + * Returns an unmodifiable view of the state of the session. |
73 | 76 | * |
74 | | - * <p>This is a shallow copy and if the underlying values of the map are modified, the read-only |
75 | | - * view will also be modified. |
| 77 | + * <p><b>Warning:</b> This is a live view, not a snapshot. |
76 | 78 | */ |
77 | 79 | public Map<String, Object> state() { |
78 | | - return ImmutableMap.copyOf(invocationContext.session().state()); |
| 80 | + if (stateView == null) { |
| 81 | + stateView = Collections.unmodifiableMap(invocationContext.session().state()); |
| 82 | + } |
| 83 | + return stateView; |
79 | 84 | } |
80 | 85 | } |
0 commit comments