Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions core/src/main/java/com/google/adk/flows/llmflows/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.adk.tools.BaseTool;
import com.google.adk.tools.FunctionTool;
import com.google.adk.tools.ToolContext;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.genai.types.Content;
Expand Down Expand Up @@ -141,9 +140,12 @@ public static Maybe<Event> handleFunctionCalls(
Map<String, ToolConfirmation> toolConfirmations) {
ImmutableList<FunctionCall> functionCalls = functionCallEvent.functionCalls();

List<FunctionCall> validFunctionCalls = new ArrayList<>();
for (FunctionCall functionCall : functionCalls) {
if (!tools.containsKey(functionCall.name().get())) {
throw new VerifyException("Tool not found: " + functionCall.name().get());
logger.warn("Tool not found: {}", functionCall.name().get());
} else {
validFunctionCalls.add(functionCall);
}
}

Expand All @@ -154,10 +156,10 @@ public static Maybe<Event> handleFunctionCalls(
Observable<Event> functionResponseEventsObservable;
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
functionResponseEventsObservable =
Observable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
Observable.fromIterable(validFunctionCalls).concatMapMaybe(functionCallMapper);
} else {
functionResponseEventsObservable =
Observable.fromIterable(functionCalls)
Observable.fromIterable(validFunctionCalls)
.concatMapEager(call -> functionCallMapper.apply(call).toObservable());
}
return functionResponseEventsObservable
Expand Down Expand Up @@ -209,9 +211,12 @@ public static Maybe<Event> handleFunctionCallsLive(
Map<String, ToolConfirmation> toolConfirmations) {
ImmutableList<FunctionCall> functionCalls = functionCallEvent.functionCalls();

List<FunctionCall> validFunctionCalls = new ArrayList<>();
for (FunctionCall functionCall : functionCalls) {
if (!tools.containsKey(functionCall.name().get())) {
throw new VerifyException("Tool not found: " + functionCall.name().get());
logger.warn("Tool not found: {}", functionCall.name().get());
} else {
validFunctionCalls.add(functionCall);
}
}

Expand All @@ -222,10 +227,10 @@ public static Maybe<Event> handleFunctionCallsLive(
Observable<Event> responseEventsObservable;
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
responseEventsObservable =
Observable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
Observable.fromIterable(validFunctionCalls).concatMapMaybe(functionCallMapper);
} else {
responseEventsObservable =
Observable.fromIterable(functionCalls)
Observable.fromIterable(validFunctionCalls)
.concatMapEager(call -> functionCallMapper.apply(call).toObservable());
}

Expand All @@ -238,7 +243,7 @@ public static Maybe<Event> handleFunctionCallsLive(
if (events.isEmpty()) {
return Maybe.empty();
}
return Maybe.just(Functions.mergeParallelFunctionResponseEvents(events).orElse(null));
return Maybe.fromOptional(Functions.mergeParallelFunctionResponseEvents(events));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.adk.testing.TestUtils.createInvocationContext;
import static com.google.adk.testing.TestUtils.createRootAgent;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.RunConfig;
Expand Down Expand Up @@ -90,11 +89,11 @@ public void handleFunctionCalls_missingTool() {
Part.fromText("..."), Part.fromFunctionCall("missing_tool", ImmutableMap.of())))
.build();

assertThrows(
RuntimeException.class,
() ->
Functions.handleFunctionCalls(
invocationContext, event, /* tools= */ ImmutableMap.of()));
Event functionResponseEvent =
Functions.handleFunctionCalls(invocationContext, event, /* tools= */ ImmutableMap.of())
.blockingGet();

assertThat(functionResponseEvent).isNull();
}

@Test
Expand Down