Skip to content

Commit 2d2903c

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Replacing Optional in BaseToolSet with Nullable
PiperOrigin-RevId: 875337870
1 parent 94aacc8 commit 2d2903c

4 files changed

Lines changed: 36 additions & 18 deletions

File tree

core/src/main/java/com/google/adk/tools/BaseToolset.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.reactivex.rxjava3.core.Flowable;
2121
import java.util.List;
2222
import java.util.Optional;
23+
import javax.annotation.Nullable;
2324

2425
/** Base interface for toolsets. */
2526
public interface BaseToolset extends AutoCloseable {
@@ -43,28 +44,35 @@ public interface BaseToolset extends AutoCloseable {
4344
void close() throws Exception;
4445

4546
/**
46-
* Helper method to be used by implementers that returns true if the given tool is in the provided
47-
* list of tools of if testing against the given ToolPredicate returns true (otherwise false).
47+
* Checks if a tool should be selected based on a filter.
4848
*
4949
* @param tool The tool to check.
50-
* @param toolFilter An Optional containing either a ToolPredicate or a List of tool names.
51-
* @param readonlyContext The current context.
52-
* @return true if the tool is selected.
50+
* @param toolFilter A ToolPredicate, a List of tool names, or null.
51+
* @param readonlyContext The context for checking the tool, or null.
5352
*/
5453
default boolean isToolSelected(
55-
BaseTool tool, Optional<Object> toolFilter, Optional<ReadonlyContext> readonlyContext) {
56-
if (toolFilter.isEmpty()) {
54+
BaseTool tool, @Nullable Object toolFilter, @Nullable ReadonlyContext readonlyContext) {
55+
if (toolFilter == null) {
5756
return true;
5857
}
59-
Object filter = toolFilter.get();
60-
if (filter instanceof ToolPredicate toolPredicate) {
58+
59+
if (toolFilter instanceof ToolPredicate toolPredicate) {
6160
return toolPredicate.test(tool, readonlyContext);
6261
}
63-
if (filter instanceof List) {
64-
@SuppressWarnings("unchecked")
65-
List<String> toolNames = (List<String>) filter;
62+
63+
if (toolFilter instanceof List<?> toolNames) {
6664
return toolNames.contains(tool.name());
6765
}
66+
6867
return false;
6968
}
69+
70+
/**
71+
* @deprecated Use {@link #isToolSelected(BaseTool, Object, ReadonlyContext)} instead.
72+
*/
73+
@Deprecated
74+
default boolean isToolSelected(
75+
BaseTool tool, Optional<Object> toolFilter, Optional<ReadonlyContext> readonlyContext) {
76+
return isToolSelected(tool, toolFilter.orElse(null), readonlyContext.orElse(null));
77+
}
7078
}

core/src/main/java/com/google/adk/tools/ToolPredicate.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.adk.agents.ReadonlyContext;
2020
import java.util.Optional;
21+
import javax.annotation.Nullable;
2122

2223
/**
2324
* Functional interface to decide whether a tool should be exposed to the LLM based on the current
@@ -31,6 +32,19 @@ public interface ToolPredicate {
3132
* @param tool The tool to check.
3233
* @param readonlyContext The current context.
3334
* @return true if the tool should be selected, false otherwise.
35+
* @deprecated Use {@link #test(BaseTool, ReadonlyContext)} instead.
3436
*/
37+
@Deprecated
3538
boolean test(BaseTool tool, Optional<ReadonlyContext> readonlyContext);
39+
40+
/**
41+
* Decides if the given tool is selected.
42+
*
43+
* @param tool The tool to check.
44+
* @param readonlyContext The current context.
45+
* @return true if the tool should be selected, false otherwise.
46+
*/
47+
default boolean test(BaseTool tool, @Nullable ReadonlyContext readonlyContext) {
48+
return test(tool, Optional.ofNullable(readonlyContext));
49+
}
3650
}

core/src/main/java/com/google/adk/tools/mcp/McpAsyncToolset.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ public Flowable<BaseTool> getTools(ReadonlyContext readonlyContext) {
170170
.map(
171171
tools ->
172172
tools.stream()
173-
.filter(
174-
tool ->
175-
isToolSelected(tool, toolFilter, Optional.ofNullable(readonlyContext)))
173+
.filter(tool -> isToolSelected(tool, toolFilter.orElse(null), readonlyContext))
176174
.toList())
177175
.onErrorResumeNext(
178176
err -> {

core/src/main/java/com/google/adk/tools/mcp/McpToolset.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ public Flowable<BaseTool> getTools(ReadonlyContext readonlyContext) {
216216
new McpTool(
217217
tool, this.mcpSession, this.mcpSessionManager, this.objectMapper))
218218
.filter(
219-
tool ->
220-
isToolSelected(
221-
tool, toolFilter, Optional.ofNullable(readonlyContext))));
219+
tool -> isToolSelected(tool, toolFilter.orElse(null), readonlyContext)));
222220
})
223221
.retryWhen(
224222
errorObservable ->

0 commit comments

Comments
 (0)