Skip to content
Merged
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
24 changes: 4 additions & 20 deletions core/src/main/java/com/google/adk/agents/ActiveStreamingTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,24 @@ public ActiveStreamingTool(Disposable task, LiveRequestQueue stream) {

public ActiveStreamingTool() {}

/**
* Returns the active task of this streaming tool.
*
* @return The active task.
*/
/** Returns the active task of this streaming tool. */
@Nullable
public Disposable task() {
return task;
}

/**
* Sets the active task of this streaming tool.
*
* @param task The new active task.
*/
/** Sets the active task of this streaming tool. */
public void task(@Nullable Disposable task) {
this.task = task;
}

/**
* Returns the active stream of this streaming tool.
*
* @return The active stream.
*/
/** Returns the active stream of this streaming tool. */
@Nullable
public LiveRequestQueue stream() {
return stream;
}

/**
* Sets the active stream of this streaming tool.
*
* @param stream The new active stream.
*/
/** Sets the active stream of this streaming tool. */
public void stream(@Nullable LiveRequestQueue stream) {
this.stream = stream;
}
Expand Down
35 changes: 10 additions & 25 deletions core/src/main/java/com/google/adk/agents/ConfigAgentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.adk.utils.ComponentRegistry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -47,6 +48,14 @@ public final class ConfigAgentUtils {

private static final Logger logger = LoggerFactory.getLogger(ConfigAgentUtils.class);

private static final ImmutableMap<Class<? extends BaseAgent>, Class<? extends BaseAgentConfig>>
AGENT_TO_CONFIG_CLASS =
ImmutableMap.of(
LlmAgent.class, LlmAgentConfig.class,
SequentialAgent.class, SequentialAgentConfig.class,
ParallelAgent.class, ParallelAgentConfig.class,
LoopAgent.class, LoopAgentConfig.class);

private ConfigAgentUtils() {}

/**
Expand Down Expand Up @@ -294,31 +303,7 @@ private static <T extends BaseAgentConfig> T loadConfigAsType(
*/
private static Class<? extends BaseAgentConfig> getConfigClassForAgent(
Class<? extends BaseAgent> agentClass) {

if (agentClass == LlmAgent.class) {
return LlmAgentConfig.class;
}

if (agentClass == SequentialAgent.class) {
return SequentialAgentConfig.class;
}

if (agentClass == ParallelAgent.class) {
return ParallelAgentConfig.class;
}

if (agentClass == LoopAgent.class) {
return LoopAgentConfig.class;
}

// TODO: Add more agent class to config class mappings as needed
// Example:
// if (agentClass == CustomAgent.class) {
// return CustomAgentConfig.class;
// }

// Default fallback to BaseAgentConfig
return BaseAgentConfig.class;
return AGENT_TO_CONFIG_CLASS.getOrDefault(agentClass, BaseAgentConfig.class);
}

/** Exception thrown when configuration is invalid. */
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/com/google/adk/agents/LlmAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,11 @@ protected void validate() {

if (this.outputSchema != null) {
if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
System.err.println(
"Warning: Invalid config for agent "
+ this.name
+ ": outputSchema cannot co-exist with agent transfer"
logger.warn(
"Invalid config for agent {}: outputSchema cannot co-exist with agent transfer"
+ " configurations. Setting disallowTransferToParent=true and"
+ " disallowTransferToPeers=true.");
+ " disallowTransferToPeers=true.",
this.name);
this.disallowTransferToParent = true;
this.disallowTransferToPeers = true;
}
Expand Down
24 changes: 13 additions & 11 deletions core/src/main/java/com/google/adk/agents/ToolResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static ImmutableList<Object> resolveToolsAndToolsets(

throw new ConfigurationException("Tool or toolset not found: " + toolName);

} catch (Exception e) {
} catch (RuntimeException e) {
String errorMsg = "Failed to resolve tool or toolset: " + toolConfig.name();
logger.error(errorMsg, e);
throw new ConfigurationException(errorMsg, e);
Expand Down Expand Up @@ -156,7 +156,7 @@ static ImmutableList<BaseTool> resolveTools(List<ToolConfig> toolConfigs, String

throw new ConfigurationException("Tool not found: " + toolName);

} catch (Exception e) {
} catch (RuntimeException e) {
String errorMsg = "Failed to resolve tool: " + toolConfig.name();
logger.error(errorMsg, e);
throw new ConfigurationException(errorMsg, e);
Expand Down Expand Up @@ -198,7 +198,7 @@ static BaseTool resolveToolInstance(String toolName) {
logger.debug("Resolved and registered tool instance via reflection: {}", toolName);
return tool;
}
} catch (Exception e) {
} catch (ReflectiveOperationException | RuntimeException e) {
logger.debug("Failed to resolve instance via reflection: {}", toolName, e);
}
}
Expand Down Expand Up @@ -237,7 +237,7 @@ static BaseToolset resolveToolsetInstance(String toolsetName) {
logger.debug("Resolved and registered toolset instance via reflection: {}", toolsetName);
return toolset;
}
} catch (Exception e) {
} catch (ReflectiveOperationException | RuntimeException e) {
logger.debug("Failed to resolve toolset instance via reflection: {}", toolsetName, e);
}
}
Expand Down Expand Up @@ -332,10 +332,11 @@ static BaseToolset resolveToolsetFromClass(
*
* @param toolsetName The toolset name in format "com.example.MyToolsetClass.INSTANCE".
* @return The resolved toolset instance, or {@code null} if not found or not a BaseToolset.
* @throws Exception if the class cannot be loaded or field access fails.
* @throws ReflectiveOperationException if the class cannot be loaded or field access fails.
*/
@Nullable
static BaseToolset resolveToolsetInstanceViaReflection(String toolsetName) throws Exception {
static BaseToolset resolveToolsetInstanceViaReflection(String toolsetName)
throws ReflectiveOperationException {
int lastDotIndex = toolsetName.lastIndexOf('.');
if (lastDotIndex == -1) {
return null;
Expand Down Expand Up @@ -425,7 +426,7 @@ static BaseTool resolveToolFromClass(String className, ToolArgsConfig args, Stri
} catch (NoSuchMethodException e) {
throw new ConfigurationException(
"Class " + className + " does not have fromConfig method but args were provided.", e);
} catch (Exception e) {
} catch (ReflectiveOperationException | RuntimeException e) {
logger.error("Error calling fromConfig on class {}", className, e);
throw new ConfigurationException("Error creating tool from class " + className, e);
}
Expand All @@ -440,7 +441,7 @@ static BaseTool resolveToolFromClass(String className, ToolArgsConfig args, Stri
throw new ConfigurationException(
"Class " + className + " does not have a default constructor and no args were provided.",
e);
} catch (Exception e) {
} catch (ReflectiveOperationException | RuntimeException e) {
logger.error("Error calling default constructor on class {}", className, e);
throw new ConfigurationException(
"Error creating tool from class " + className + " using default constructor", e);
Expand Down Expand Up @@ -476,11 +477,12 @@ static boolean isJavaQualifiedName(String name) {
* @param toolName The fully qualified name of a static field holding a tool instance.
* @return The {@link BaseTool} instance, or {@code null} if {@code toolName} is not in the
* expected format, or if the field is not found, not static, or not of type {@link BaseTool}.
* @throws Exception if the class specified in {@code toolName} cannot be loaded, or if there is a
* security manager preventing reflection, or if accessing the field causes an exception.
* @throws ReflectiveOperationException if the class specified in {@code toolName} cannot be
* loaded, or if accessing the field causes an exception.
*/
@Nullable
static BaseTool resolveInstanceViaReflection(String toolName) throws Exception {
static BaseTool resolveInstanceViaReflection(String toolName)
throws ReflectiveOperationException {
int lastDotIndex = toolName.lastIndexOf('.');
if (lastDotIndex == -1) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void testResolveTools_toolNotFound() {
ConfigurationException.class,
() -> ToolResolver.resolveTools(toolConfigs, "/test/path"));

assertThat(exception).hasMessageThat().contains("Failed to resolve tool: non.existent.Tool");
assertThat(exception).hasMessageThat().contains("Tool not found: non.existent.Tool");
}

/** A test tool with multiple methods annotated with @Schema. */
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/java/com/google/adk/testing/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.RunConfig;
import com.google.adk.artifacts.InMemoryArtifactService;
import com.google.adk.events.Event;
import com.google.adk.events.EventActions;
import com.google.adk.events.EventCompaction;
Expand Down Expand Up @@ -54,6 +55,7 @@ public static InvocationContext createInvocationContext(BaseAgent agent, RunConf
InMemorySessionService sessionService = new InMemorySessionService();
return InvocationContext.builder()
.sessionService(sessionService)
.artifactService(new InMemoryArtifactService())
.invocationId("invocationId")
.agent(agent)
.session(sessionService.createSession("test-app", "test-user").blockingGet())
Expand Down