From e7380f1c9c0665ca7b65327f7db362f137c0f6c1 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Thu, 8 Jan 2026 00:24:00 -0800 Subject: [PATCH] refactor: Introduce a ToolMaker interface Instead of using Object as a parent for Toolset and BaseTool, use the marker instead PiperOrigin-RevId: 853597287 --- .../java/com/google/adk/agents/LlmAgent.java | 15 ++++++++------- .../com/google/adk/agents/ToolResolver.java | 5 +++-- .../java/com/google/adk/tools/BaseTool.java | 2 +- .../com/google/adk/tools/BaseToolset.java | 2 +- .../java/com/google/adk/tools/ToolMarker.java | 19 +++++++++++++++++++ 5 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 core/src/main/java/com/google/adk/tools/ToolMarker.java diff --git a/core/src/main/java/com/google/adk/agents/LlmAgent.java b/core/src/main/java/com/google/adk/agents/LlmAgent.java index ab3ee7edb..c89eb99a6 100644 --- a/core/src/main/java/com/google/adk/agents/LlmAgent.java +++ b/core/src/main/java/com/google/adk/agents/LlmAgent.java @@ -48,6 +48,7 @@ import com.google.adk.models.Model; import com.google.adk.tools.BaseTool; import com.google.adk.tools.BaseToolset; +import com.google.adk.tools.ToolMarker; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -84,7 +85,7 @@ public enum IncludeContents { private final Optional model; private final Instruction instruction; private final Instruction globalInstruction; - private final List toolsUnion; + private final List toolsUnion; private final ImmutableList toolsets; private final Optional generateContentConfig; // TODO: Remove exampleProvider field - examples should only be provided via ExampleTool @@ -152,7 +153,7 @@ public static Builder builder() { } /** Extracts BaseToolset instances from the toolsUnion list. */ - private static ImmutableList extractToolsets(List toolsUnion) { + private static ImmutableList extractToolsets(List toolsUnion) { return toolsUnion.stream() .filter(obj -> obj instanceof BaseToolset) .map(obj -> (BaseToolset) obj) @@ -165,7 +166,7 @@ public static class Builder extends BaseAgent.Builder { private Instruction instruction; private Instruction globalInstruction; - private ImmutableList toolsUnion; + private ImmutableList toolsUnion; private GenerateContentConfig generateContentConfig; private BaseExampleProvider exampleProvider; private IncludeContents includeContents; @@ -221,13 +222,13 @@ public Builder globalInstruction(String globalInstruction) { } @CanIgnoreReturnValue - public Builder tools(List tools) { + public Builder tools(List tools) { this.toolsUnion = ImmutableList.copyOf(tools); return this; } @CanIgnoreReturnValue - public Builder tools(Object... tools) { + public Builder tools(ToolMarker... tools) { this.toolsUnion = ImmutableList.copyOf(tools); return this; } @@ -679,7 +680,7 @@ public Single> canonicalGlobalInstruction(ReadonlyCon */ public Flowable canonicalTools(Optional context) { List> toolFlowables = new ArrayList<>(); - for (Object toolOrToolset : toolsUnion) { + for (ToolMarker toolOrToolset : toolsUnion) { if (toolOrToolset instanceof BaseTool baseTool) { toolFlowables.add(Flowable.just(baseTool)); } else if (toolOrToolset instanceof BaseToolset baseToolset) { @@ -740,7 +741,7 @@ public Single> tools() { return canonicalTools().toList(); } - public List toolsUnion() { + public List toolsUnion() { return toolsUnion; } diff --git a/core/src/main/java/com/google/adk/agents/ToolResolver.java b/core/src/main/java/com/google/adk/agents/ToolResolver.java index 4d3670fe4..d11ebf457 100644 --- a/core/src/main/java/com/google/adk/agents/ToolResolver.java +++ b/core/src/main/java/com/google/adk/agents/ToolResolver.java @@ -23,6 +23,7 @@ import com.google.adk.tools.BaseTool.ToolArgsConfig; import com.google.adk.tools.BaseTool.ToolConfig; import com.google.adk.tools.BaseToolset; +import com.google.adk.tools.ToolMarker; import com.google.adk.utils.ComponentRegistry; import com.google.common.collect.ImmutableList; import java.lang.reflect.Constructor; @@ -56,14 +57,14 @@ private ToolResolver() {} * @throws ConfigurationException if any tool configuration is invalid (e.g., missing name), if a * tool cannot be found by its name or class, or if tool instantiation fails. */ - static ImmutableList resolveToolsAndToolsets( + static ImmutableList resolveToolsAndToolsets( List toolConfigs, String configAbsPath) throws ConfigurationException { if (toolConfigs == null || toolConfigs.isEmpty()) { return ImmutableList.of(); } - ImmutableList.Builder resolvedItems = ImmutableList.builder(); + ImmutableList.Builder resolvedItems = ImmutableList.builder(); for (ToolConfig toolConfig : toolConfigs) { try { diff --git a/core/src/main/java/com/google/adk/tools/BaseTool.java b/core/src/main/java/com/google/adk/tools/BaseTool.java index eda6fac3e..3659fd3fe 100644 --- a/core/src/main/java/com/google/adk/tools/BaseTool.java +++ b/core/src/main/java/com/google/adk/tools/BaseTool.java @@ -44,7 +44,7 @@ import org.slf4j.LoggerFactory; /** The base class for all ADK tools. */ -public abstract class BaseTool { +public abstract class BaseTool implements ToolMarker { private final String name; private final String description; private final boolean isLongRunning; diff --git a/core/src/main/java/com/google/adk/tools/BaseToolset.java b/core/src/main/java/com/google/adk/tools/BaseToolset.java index c7620f913..03176fb81 100644 --- a/core/src/main/java/com/google/adk/tools/BaseToolset.java +++ b/core/src/main/java/com/google/adk/tools/BaseToolset.java @@ -6,7 +6,7 @@ import java.util.Optional; /** Base interface for toolsets. */ -public interface BaseToolset extends AutoCloseable { +public interface BaseToolset extends AutoCloseable, ToolMarker { /** * Return all tools in the toolset based on the provided context. diff --git a/core/src/main/java/com/google/adk/tools/ToolMarker.java b/core/src/main/java/com/google/adk/tools/ToolMarker.java new file mode 100644 index 000000000..9cb2c18a7 --- /dev/null +++ b/core/src/main/java/com/google/adk/tools/ToolMarker.java @@ -0,0 +1,19 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.adk.tools; + +/** Marker interface for BaseTool and BaseToolset. */ +public interface ToolMarker {}