Skip to content

Commit 154a7d6

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Support configuring tool execution mode in RunConfig
PiperOrigin-RevId: 836716906
1 parent 0b8b35b commit 154a7d6

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ public enum StreamingMode {
3838
BIDI
3939
}
4040

41+
/**
42+
* Tool execution mode for the runner, when they are multiple tools requested (by the models or
43+
* callbacks).
44+
*
45+
* <p>NONE: default to PARALLEL.
46+
*
47+
* <p>SEQUENTIAL: Multiple tools are executed in the order they are requested.
48+
*
49+
* <p>PARALLEL: Multiple tools are executed in parallel.
50+
*/
51+
public enum ToolExecutionMode {
52+
NONE,
53+
SEQUENTIAL,
54+
PARALLEL
55+
}
56+
4157
public abstract @Nullable SpeechConfig speechConfig();
4258

4359
public abstract ImmutableList<Modality> responseModalities();
@@ -46,6 +62,8 @@ public enum StreamingMode {
4662

4763
public abstract StreamingMode streamingMode();
4864

65+
public abstract ToolExecutionMode toolExecutionMode();
66+
4967
public abstract @Nullable AudioTranscriptionConfig outputAudioTranscription();
5068

5169
public abstract @Nullable AudioTranscriptionConfig inputAudioTranscription();
@@ -59,13 +77,15 @@ public static Builder builder() {
5977
.setSaveInputBlobsAsArtifacts(false)
6078
.setResponseModalities(ImmutableList.of())
6179
.setStreamingMode(StreamingMode.NONE)
80+
.setToolExecutionMode(ToolExecutionMode.NONE)
6281
.setMaxLlmCalls(500);
6382
}
6483

6584
public static Builder builder(RunConfig runConfig) {
6685
return new AutoValue_RunConfig.Builder()
6786
.setSaveInputBlobsAsArtifacts(runConfig.saveInputBlobsAsArtifacts())
6887
.setStreamingMode(runConfig.streamingMode())
88+
.setToolExecutionMode(runConfig.toolExecutionMode())
6989
.setMaxLlmCalls(runConfig.maxLlmCalls())
7090
.setResponseModalities(runConfig.responseModalities())
7191
.setSpeechConfig(runConfig.speechConfig())
@@ -89,6 +109,9 @@ public abstract static class Builder {
89109
@CanIgnoreReturnValue
90110
public abstract Builder setStreamingMode(StreamingMode streamingMode);
91111

112+
@CanIgnoreReturnValue
113+
public abstract Builder setToolExecutionMode(ToolExecutionMode toolExecutionMode);
114+
92115
@CanIgnoreReturnValue
93116
public abstract Builder setOutputAudioTranscription(
94117
@Nullable AudioTranscriptionConfig outputAudioTranscription);

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.adk.agents.Callbacks.BeforeToolCallback;
2626
import com.google.adk.agents.InvocationContext;
2727
import com.google.adk.agents.LlmAgent;
28+
import com.google.adk.agents.RunConfig.ToolExecutionMode;
2829
import com.google.adk.events.Event;
2930
import com.google.adk.events.EventActions;
3031
import com.google.adk.tools.BaseTool;
@@ -198,7 +199,13 @@ public static Maybe<Event> handleFunctionCalls(
198199
functionResponseEvents.add(maybeFunctionResponseEvent);
199200
}
200201

201-
return Maybe.merge(functionResponseEvents)
202+
Flowable<Event> functionResponseEventsFlowable;
203+
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
204+
functionResponseEventsFlowable = Maybe.concat(functionResponseEvents);
205+
} else {
206+
functionResponseEventsFlowable = Maybe.merge(functionResponseEvents);
207+
}
208+
return functionResponseEventsFlowable
202209
.toList()
203210
.flatMapMaybe(
204211
events -> {
@@ -296,7 +303,13 @@ public static Maybe<Event> handleFunctionCallsLive(
296303
responseEvents.add(maybeFunctionResponseEvent);
297304
}
298305

299-
return Maybe.merge(responseEvents)
306+
Flowable<Event> responseEventsFlowable;
307+
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
308+
responseEventsFlowable = Maybe.concat(responseEvents);
309+
} else {
310+
responseEventsFlowable = Maybe.merge(responseEvents);
311+
}
312+
return responseEventsFlowable
300313
.toList()
301314
.flatMapMaybe(
302315
events -> {

0 commit comments

Comments
 (0)