Skip to content

Commit eadb06e

Browse files
google-genai-botcopybara-github
authored andcommitted
test: create TestAgent009
Additionally add support for instantiating LongRunningFunctionTool from config, the `func` parameter is interpreted as a name of a FuncTool registered elsewhere, that the LongRunningFunction tool is wrapping PiperOrigin-RevId: 845264027
1 parent 441c9a6 commit eadb06e

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ public Method func() {
185185
return func;
186186
}
187187

188+
/** Returns the underlying function's {@link Object} instance if present. */
189+
@Nullable
190+
Object instance() {
191+
return instance;
192+
}
193+
194+
/** Returns whether the function requires confirmation */
195+
boolean requireConfirmation() {
196+
return requireConfirmation;
197+
}
198+
188199
/** Returns true if the wrapped function returns a Flowable and can be used for streaming. */
189200
public boolean isStreaming() {
190201
Type returnType = func.getGenericReturnType();

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.adk.tools;
1818

19+
import com.fasterxml.jackson.core.type.TypeReference;
20+
import com.google.adk.utils.ComponentRegistry;
1921
import java.lang.reflect.Method;
2022
import javax.annotation.Nullable;
2123

@@ -70,6 +72,14 @@ public static LongRunningFunctionTool create(
7072
return new LongRunningFunctionTool(instance, method, requireConfirmation);
7173
}
7274

75+
/** Creates a LongRunningFunctionTool from a FunctionTool. */
76+
public static LongRunningFunctionTool create(FunctionTool tool) {
77+
@Nullable Object instance = tool.instance();
78+
Method m = tool.func();
79+
boolean requireConfirmation = tool.requireConfirmation();
80+
return create(instance, m, requireConfirmation);
81+
}
82+
7383
private LongRunningFunctionTool(Method func, boolean requireConfirmation) {
7484
super(null, func, /* isLongRunning= */ true, requireConfirmation);
7585
}
@@ -78,4 +88,22 @@ private LongRunningFunctionTool(
7888
@Nullable Object instance, Method func, boolean requireConfirmation) {
7989
super(instance, func, /* isLongRunning= */ true, requireConfirmation);
8090
}
91+
92+
public static LongRunningFunctionTool fromConfig(ToolArgsConfig config, String configAbsPath) {
93+
String funcName =
94+
config
95+
.getOrEmpty("func", new TypeReference<String>() {})
96+
.orElseThrow(() -> new IllegalArgumentException("func argument is empty"));
97+
98+
FunctionTool funcTool =
99+
ComponentRegistry.getInstance()
100+
.get(funcName, FunctionTool.class)
101+
.orElseThrow(
102+
() ->
103+
new IllegalArgumentException(
104+
String.format(
105+
"failed to find FunctionTool %s in the ComponentRegistry", funcName)));
106+
107+
return create(funcTool);
108+
}
81109
}

core/src/main/java/com/google/adk/utils/ComponentRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.adk.tools.ExitLoopTool;
3333
import com.google.adk.tools.GoogleSearchTool;
3434
import com.google.adk.tools.LoadArtifactsTool;
35+
import com.google.adk.tools.LongRunningFunctionTool;
3536
import com.google.adk.tools.UrlContextTool;
3637
import com.google.adk.tools.mcp.McpToolset;
3738
import java.util.Map;
@@ -110,6 +111,7 @@ private void initializePreWiredEntries() {
110111
registerAdkToolInstance("url_context", UrlContextTool.INSTANCE);
111112

112113
registerAdkToolClass(AgentTool.class);
114+
registerAdkToolClass(LongRunningFunctionTool.class);
113115

114116
registerAdkToolsetClass(McpToolset.class);
115117
// TODO: add all python tools that also exist in Java.

0 commit comments

Comments
 (0)