Skip to content

Commit d4c63a4

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Marking InvocationContext settings as deprecated
- Updating a bunch of calls to InvocationContext that directly modify state to use the Builder instead. - Updating calls to InvocationContext constructors to use InvocationContext.buider() instead. PiperOrigin-RevId: 852935731
1 parent a99c75b commit d4c63a4

14 files changed

Lines changed: 208 additions & 311 deletions

File tree

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ public Optional<List<? extends AfterAgentCallback>> afterAgentCallback() {
187187
* @return new context with updated branch name.
188188
*/
189189
private InvocationContext createInvocationContext(InvocationContext parentContext) {
190-
InvocationContext invocationContext = InvocationContext.copyOf(parentContext);
191-
invocationContext.agent(this);
190+
InvocationContext.Builder builder = parentContext.toBuilder();
191+
builder.agent(this);
192192
// Check for branch to be truthy (not None, not empty string),
193193
if (parentContext.branch().filter(s -> !s.isEmpty()).isPresent()) {
194-
invocationContext.branch(parentContext.branch().get() + "." + name());
194+
builder.branch(parentContext.branch().get() + "." + name());
195195
}
196-
return invocationContext;
196+
return builder.build();
197197
}
198198

199199
/**
@@ -303,17 +303,16 @@ private Single<Optional<Event>> callCallback(
303303
return maybeContent
304304
.map(
305305
content -> {
306-
Event.Builder eventBuilder =
306+
invocationContext.setEndInvocation(true);
307+
return Optional.of(
307308
Event.builder()
308309
.id(Event.generateEventId())
309310
.invocationId(invocationContext.invocationId())
310311
.author(name())
311312
.branch(invocationContext.branch())
312-
.actions(callbackContext.eventActions());
313-
314-
eventBuilder.content(Optional.of(content));
315-
invocationContext.setEndInvocation(true);
316-
return Optional.of(eventBuilder.build());
313+
.actions(callbackContext.eventActions())
314+
.content(content)
315+
.build());
317316
})
318317
.toFlowable();
319318
})

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

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,25 @@ public class InvocationContext {
4444
private final BaseMemoryService memoryService;
4545
private final PluginManager pluginManager;
4646
private final Optional<LiveRequestQueue> liveRequestQueue;
47-
private final Map<String, ActiveStreamingTool> activeStreamingTools = new ConcurrentHashMap<>();
47+
private final Map<String, ActiveStreamingTool> activeStreamingTools;
4848
private final String invocationId;
4949
private final Session session;
5050
private final Optional<Content> userContent;
5151
private final RunConfig runConfig;
5252
private final ResumabilityConfig resumabilityConfig;
53-
private final InvocationCostManager invocationCostManager = new InvocationCostManager();
53+
private final InvocationCostManager invocationCostManager;
5454

5555
private Optional<String> branch;
5656
private BaseAgent agent;
5757
private boolean endInvocation;
5858

59-
private InvocationContext(Builder builder) {
59+
protected InvocationContext(Builder builder) {
6060
this.sessionService = builder.sessionService;
6161
this.artifactService = builder.artifactService;
6262
this.memoryService = builder.memoryService;
6363
this.pluginManager = builder.pluginManager;
6464
this.liveRequestQueue = builder.liveRequestQueue;
65+
this.activeStreamingTools = builder.activeStreamingTools;
6566
this.branch = builder.branch;
6667
this.invocationId = builder.invocationId;
6768
this.agent = builder.agent;
@@ -70,6 +71,7 @@ private InvocationContext(Builder builder) {
7071
this.runConfig = builder.runConfig;
7172
this.endInvocation = builder.endInvocation;
7273
this.resumabilityConfig = builder.resumabilityConfig;
74+
this.invocationCostManager = builder.invocationCostManager;
7375
}
7476

7577
/**
@@ -187,7 +189,7 @@ public static InvocationContext create(
187189
.artifactService(artifactService)
188190
.agent(agent)
189191
.session(session)
190-
.liveRequestQueue(Optional.ofNullable(liveRequestQueue))
192+
.liveRequestQueue(liveRequestQueue)
191193
.runConfig(runConfig)
192194
.build();
193195
}
@@ -197,26 +199,19 @@ public static Builder builder() {
197199
return new Builder();
198200
}
199201

200-
/** Creates a shallow copy of the given {@link InvocationContext}. */
202+
/** Returns a {@link Builder} initialized with the values of this instance. */
203+
public Builder toBuilder() {
204+
return new Builder(this);
205+
}
206+
207+
/**
208+
* Creates a shallow copy of the given {@link InvocationContext}.
209+
*
210+
* @deprecated Use {@code other.toBuilder().build()} instead.
211+
*/
212+
@Deprecated(forRemoval = true)
201213
public static InvocationContext copyOf(InvocationContext other) {
202-
InvocationContext newContext =
203-
builder()
204-
.sessionService(other.sessionService)
205-
.artifactService(other.artifactService)
206-
.memoryService(other.memoryService)
207-
.pluginManager(other.pluginManager)
208-
.liveRequestQueue(other.liveRequestQueue)
209-
.branch(other.branch)
210-
.invocationId(other.invocationId)
211-
.agent(other.agent)
212-
.session(other.session)
213-
.userContent(other.userContent)
214-
.runConfig(other.runConfig)
215-
.endInvocation(other.endInvocation)
216-
.resumabilityConfig(other.resumabilityConfig)
217-
.build();
218-
newContext.activeStreamingTools.putAll(other.activeStreamingTools);
219-
return newContext;
214+
return other.toBuilder().build();
220215
}
221216

222217
/** Returns the session service for managing session state. */
@@ -257,7 +252,10 @@ public String invocationId() {
257252
/**
258253
* Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation
259254
* history.
255+
*
256+
* @deprecated Use {@link #toBuilder()} and {@link Builder#branch(String)} instead.
260257
*/
258+
@Deprecated(forRemoval = true)
261259
public void branch(@Nullable String branch) {
262260
this.branch = Optional.ofNullable(branch);
263261
}
@@ -275,7 +273,12 @@ public BaseAgent agent() {
275273
return agent;
276274
}
277275

278-
/** Sets the [agent] being invoked. This is useful when delegating to a sub-agent. */
276+
/**
277+
* Sets the [agent] being invoked. This is useful when delegating to a sub-agent.
278+
*
279+
* @deprecated Use {@link #toBuilder()} and {@link Builder#agent(BaseAgent)} instead.
280+
*/
281+
@Deprecated(forRemoval = true)
279282
public void agent(BaseAgent agent) {
280283
this.agent = agent;
281284
}
@@ -369,15 +372,53 @@ void incrementAndEnforceLlmCallsLimit(RunConfig runConfig)
369372
"Max number of llm calls limit of " + runConfig.maxLlmCalls() + " exceeded");
370373
}
371374
}
375+
376+
@Override
377+
public boolean equals(Object o) {
378+
if (this == o) {
379+
return true;
380+
}
381+
if (!(o instanceof InvocationCostManager that)) {
382+
return false;
383+
}
384+
return numberOfLlmCalls == that.numberOfLlmCalls;
385+
}
386+
387+
@Override
388+
public int hashCode() {
389+
return Integer.hashCode(numberOfLlmCalls);
390+
}
372391
}
373392

374393
/** Builder for {@link InvocationContext}. */
375394
public static class Builder {
395+
396+
private Builder() {}
397+
398+
private Builder(InvocationContext context) {
399+
this.sessionService = context.sessionService;
400+
this.artifactService = context.artifactService;
401+
this.memoryService = context.memoryService;
402+
this.pluginManager = context.pluginManager;
403+
this.liveRequestQueue = context.liveRequestQueue;
404+
this.activeStreamingTools = new ConcurrentHashMap<>(context.activeStreamingTools);
405+
this.branch = context.branch;
406+
this.invocationId = context.invocationId;
407+
this.agent = context.agent;
408+
this.session = context.session;
409+
this.userContent = context.userContent;
410+
this.runConfig = context.runConfig;
411+
this.endInvocation = context.endInvocation;
412+
this.resumabilityConfig = context.resumabilityConfig;
413+
this.invocationCostManager = context.invocationCostManager;
414+
}
415+
376416
private BaseSessionService sessionService;
377417
private BaseArtifactService artifactService;
378418
private BaseMemoryService memoryService;
379419
private PluginManager pluginManager = new PluginManager();
380420
private Optional<LiveRequestQueue> liveRequestQueue = Optional.empty();
421+
private Map<String, ActiveStreamingTool> activeStreamingTools = new ConcurrentHashMap<>();
381422
private Optional<String> branch = Optional.empty();
382423
private String invocationId = newInvocationContextId();
383424
private BaseAgent agent;
@@ -386,6 +427,7 @@ public static class Builder {
386427
private RunConfig runConfig = RunConfig.builder().build();
387428
private boolean endInvocation = false;
388429
private ResumabilityConfig resumabilityConfig = new ResumabilityConfig();
430+
private InvocationCostManager invocationCostManager = new InvocationCostManager();
389431

390432
/**
391433
* Sets the session service for managing session state.
@@ -457,8 +499,8 @@ public Builder liveRequestQueue(Optional<LiveRequestQueue> liveRequestQueue) {
457499
* @return this builder instance for chaining.
458500
*/
459501
@CanIgnoreReturnValue
460-
public Builder liveRequestQueue(LiveRequestQueue liveRequestQueue) {
461-
this.liveRequestQueue = Optional.of(liveRequestQueue);
502+
public Builder liveRequestQueue(@Nullable LiveRequestQueue liveRequestQueue) {
503+
this.liveRequestQueue = Optional.ofNullable(liveRequestQueue);
462504
return this;
463505
}
464506

@@ -617,7 +659,8 @@ public boolean equals(Object o) {
617659
&& Objects.equals(session, that.session)
618660
&& Objects.equals(userContent, that.userContent)
619661
&& Objects.equals(runConfig, that.runConfig)
620-
&& Objects.equals(resumabilityConfig, that.resumabilityConfig);
662+
&& Objects.equals(resumabilityConfig, that.resumabilityConfig)
663+
&& Objects.equals(invocationCostManager, that.invocationCostManager);
621664
}
622665

623666
@Override
@@ -636,6 +679,7 @@ public int hashCode() {
636679
userContent,
637680
runConfig,
638681
endInvocation,
639-
resumabilityConfig);
682+
resumabilityConfig,
683+
invocationCostManager);
640684
}
641685
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package com.google.adk.agents;
1717

1818
import static com.google.common.base.Strings.isNullOrEmpty;
19+
import static com.google.common.collect.ImmutableList.toImmutableList;
1920

2021
import com.google.adk.agents.ConfigAgentUtils.ConfigurationException;
2122
import com.google.adk.events.Event;
2223
import io.reactivex.rxjava3.core.Flowable;
23-
import java.util.ArrayList;
2424
import java.util.List;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
@@ -101,14 +101,15 @@ public static ParallelAgent fromConfig(ParallelAgentConfig config, String config
101101
*
102102
* @param currentAgent Current agent.
103103
* @param invocationContext Invocation context to update.
104+
* @return A new invocation context with branch set.
104105
*/
105-
private static void setBranchForCurrentAgent(
106+
private static InvocationContext setBranchForCurrentAgent(
106107
BaseAgent currentAgent, InvocationContext invocationContext) {
107108
String branch = invocationContext.branch().orElse(null);
108109
if (isNullOrEmpty(branch)) {
109-
invocationContext.branch(currentAgent.name());
110+
return invocationContext.toBuilder().branch(currentAgent.name()).build();
110111
} else {
111-
invocationContext.branch(branch + "." + currentAgent.name());
112+
return invocationContext.toBuilder().branch(branch + "." + currentAgent.name()).build();
112113
}
113114
}
114115

@@ -122,18 +123,16 @@ private static void setBranchForCurrentAgent(
122123
*/
123124
@Override
124125
protected Flowable<Event> runAsyncImpl(InvocationContext invocationContext) {
125-
setBranchForCurrentAgent(this, invocationContext);
126-
127126
List<? extends BaseAgent> currentSubAgents = subAgents();
128127
if (currentSubAgents == null || currentSubAgents.isEmpty()) {
129128
return Flowable.empty();
130129
}
131130

132-
List<Flowable<Event>> agentFlowables = new ArrayList<>();
133-
for (BaseAgent subAgent : currentSubAgents) {
134-
agentFlowables.add(subAgent.runAsync(invocationContext));
135-
}
136-
return Flowable.merge(agentFlowables);
131+
var updatedInvocationContext = setBranchForCurrentAgent(this, invocationContext);
132+
return Flowable.merge(
133+
currentSubAgents.stream()
134+
.map(subAgent -> subAgent.runAsync(updatedInvocationContext))
135+
.collect(toImmutableList()));
137136
}
138137

139138
/**

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.adk.examples.ExampleUtils;
2222
import com.google.adk.models.LlmRequest;
2323
import com.google.common.collect.ImmutableList;
24+
import com.google.genai.types.Content;
2425
import io.reactivex.rxjava3.core.Single;
2526

2627
/** {@link RequestProcessor} that populates examples in LLM request. */
@@ -38,9 +39,12 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
3839
LlmRequest.Builder builder = request.toBuilder();
3940

4041
String query =
41-
context.userContent().isPresent()
42-
? context.userContent().get().parts().get().get(0).text().orElse("")
43-
: "";
42+
context
43+
.userContent()
44+
.flatMap(Content::parts)
45+
.filter(parts -> !parts.isEmpty())
46+
.map(parts -> parts.get(0).text().orElse(""))
47+
.orElse("");
4448
agent
4549
.exampleProvider()
4650
.ifPresent(

0 commit comments

Comments
 (0)