Skip to content

Commit f0fe1c9

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: Introduce a ToolMaker interface
Instead of using Object as a parent for Toolset and BaseTool, use the marker instead PiperOrigin-RevId: 853341081
1 parent cd0e22c commit f0fe1c9

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.google.adk.models.Model;
4949
import com.google.adk.tools.BaseTool;
5050
import com.google.adk.tools.BaseToolset;
51+
import com.google.adk.tools.ToolMarker;
5152
import com.google.common.base.Preconditions;
5253
import com.google.common.collect.ImmutableList;
5354
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@@ -84,7 +85,7 @@ public enum IncludeContents {
8485
private final Optional<Model> model;
8586
private final Instruction instruction;
8687
private final Instruction globalInstruction;
87-
private final List<Object> toolsUnion;
88+
private final List<ToolMarker> toolsUnion;
8889
private final ImmutableList<BaseToolset> toolsets;
8990
private final Optional<GenerateContentConfig> generateContentConfig;
9091
// TODO: Remove exampleProvider field - examples should only be provided via ExampleTool
@@ -152,7 +153,7 @@ public static Builder builder() {
152153
}
153154

154155
/** Extracts BaseToolset instances from the toolsUnion list. */
155-
private static ImmutableList<BaseToolset> extractToolsets(List<Object> toolsUnion) {
156+
private static ImmutableList<BaseToolset> extractToolsets(List<ToolMarker> toolsUnion) {
156157
return toolsUnion.stream()
157158
.filter(obj -> obj instanceof BaseToolset)
158159
.map(obj -> (BaseToolset) obj)
@@ -165,7 +166,7 @@ public static class Builder extends BaseAgent.Builder<Builder> {
165166

166167
private Instruction instruction;
167168
private Instruction globalInstruction;
168-
private ImmutableList<Object> toolsUnion;
169+
private ImmutableList<ToolMarker> toolsUnion;
169170
private GenerateContentConfig generateContentConfig;
170171
private BaseExampleProvider exampleProvider;
171172
private IncludeContents includeContents;
@@ -221,13 +222,13 @@ public Builder globalInstruction(String globalInstruction) {
221222
}
222223

223224
@CanIgnoreReturnValue
224-
public Builder tools(List<?> tools) {
225+
public Builder tools(List<? extends ToolMarker> tools) {
225226
this.toolsUnion = ImmutableList.copyOf(tools);
226227
return this;
227228
}
228229

229230
@CanIgnoreReturnValue
230-
public Builder tools(Object... tools) {
231+
public Builder tools(ToolMarker... tools) {
231232
this.toolsUnion = ImmutableList.copyOf(tools);
232233
return this;
233234
}
@@ -679,7 +680,7 @@ public Single<Map.Entry<String, Boolean>> canonicalGlobalInstruction(ReadonlyCon
679680
*/
680681
public Flowable<BaseTool> canonicalTools(Optional<ReadonlyContext> context) {
681682
List<Flowable<BaseTool>> toolFlowables = new ArrayList<>();
682-
for (Object toolOrToolset : toolsUnion) {
683+
for (ToolMarker toolOrToolset : toolsUnion) {
683684
if (toolOrToolset instanceof BaseTool baseTool) {
684685
toolFlowables.add(Flowable.just(baseTool));
685686
} else if (toolOrToolset instanceof BaseToolset baseToolset) {
@@ -740,7 +741,7 @@ public Single<List<BaseTool>> tools() {
740741
return canonicalTools().toList();
741742
}
742743

743-
public List<Object> toolsUnion() {
744+
public List<ToolMarker> toolsUnion() {
744745
return toolsUnion;
745746
}
746747

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.adk.tools.BaseTool.ToolArgsConfig;
2424
import com.google.adk.tools.BaseTool.ToolConfig;
2525
import com.google.adk.tools.BaseToolset;
26+
import com.google.adk.tools.ToolMarker;
2627
import com.google.adk.utils.ComponentRegistry;
2728
import com.google.common.collect.ImmutableList;
2829
import java.lang.reflect.Constructor;
@@ -56,14 +57,14 @@ private ToolResolver() {}
5657
* @throws ConfigurationException if any tool configuration is invalid (e.g., missing name), if a
5758
* tool cannot be found by its name or class, or if tool instantiation fails.
5859
*/
59-
static ImmutableList<Object> resolveToolsAndToolsets(
60+
static ImmutableList<ToolMarker> resolveToolsAndToolsets(
6061
List<ToolConfig> toolConfigs, String configAbsPath) throws ConfigurationException {
6162

6263
if (toolConfigs == null || toolConfigs.isEmpty()) {
6364
return ImmutableList.of();
6465
}
6566

66-
ImmutableList.Builder<Object> resolvedItems = ImmutableList.builder();
67+
ImmutableList.Builder<ToolMarker> resolvedItems = ImmutableList.builder();
6768

6869
for (ToolConfig toolConfig : toolConfigs) {
6970
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import org.slf4j.LoggerFactory;
4545

4646
/** The base class for all ADK tools. */
47-
public abstract class BaseTool {
47+
public abstract class BaseTool implements ToolMarker {
4848
private final String name;
4949
private final String description;
5050
private final boolean isLongRunning;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Optional;
77

88
/** Base interface for toolsets. */
9-
public interface BaseToolset extends AutoCloseable {
9+
public interface BaseToolset extends AutoCloseable, ToolMarker {
1010

1111
/**
1212
* Return all tools in the toolset based on the provided context.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.adk.tools;
17+
18+
/** Marker interface for BaseTool and BaseToolset. */
19+
public interface ToolMarker {}

0 commit comments

Comments
 (0)