Skip to content
Closed
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
17 changes: 7 additions & 10 deletions core/src/main/java/com/google/adk/agents/LlmAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.adk.agents;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.stream.Collectors.joining;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -698,31 +697,29 @@ public Single<String> canonicalGlobalInstruction(ReadonlyContext context) {
* @param context The context to retrieve the session state.
* @return The resolved list of tools as a {@link Single} wrapped list of {@link BaseTool}.
*/
public Single<List<BaseTool>> canonicalTools(Optional<ReadonlyContext> context) {
List<Single<List<BaseTool>>> toolSingles = new ArrayList<>();
public Flowable<List<BaseTool>> canonicalTools(Optional<ReadonlyContext> context) {
List<Flowable<List<BaseTool>>> toolFlowables = new ArrayList<>();
for (Object toolOrToolset : toolsUnion) {
if (toolOrToolset instanceof BaseTool baseTool) {
toolSingles.add(Single.just(ImmutableList.of(baseTool)));
toolFlowables.add(Flowable.just(ImmutableList.of(baseTool)));
} else if (toolOrToolset instanceof BaseToolset baseToolset) {
toolSingles.add(baseToolset.getTools(context.orElse(null)));
toolFlowables.add(baseToolset.getTools(context.orElse(null)));
} else {
throw new IllegalArgumentException(
"Object in tools list is not of a supported type: "
+ toolOrToolset.getClass().getName());
}
}
return Single.concat(toolSingles)
.toList()
.map(listOfLists -> listOfLists.stream().flatMap(List::stream).collect(toImmutableList()));
return Flowable.concat(toolFlowables).flatMap(Flowable::fromIterable).toList().toFlowable();
}

/** Overload of canonicalTools that defaults to an empty context. */
public Single<List<BaseTool>> canonicalTools() {
public Flowable<List<BaseTool>> canonicalTools() {
return canonicalTools(Optional.empty());
}

/** Convenience overload of canonicalTools that accepts a non-optional ReadonlyContext. */
public Single<List<BaseTool>> canonicalTools(ReadonlyContext context) {
public Flowable<List<BaseTool>> canonicalTools(ReadonlyContext context) {
return canonicalTools(Optional.ofNullable(context));
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/google/adk/tools/BaseToolset.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.google.adk.tools;

import com.google.adk.agents.ReadonlyContext;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.Flowable;
import java.util.List;
import java.util.Optional;

Expand All @@ -12,9 +12,9 @@ public interface BaseToolset extends AutoCloseable {
* Return all tools in the toolset based on the provided context.
*
* @param readonlyContext Context used to filter tools available to the agent.
* @return A Single emitting a list of tools available under the specified context.
* @return A Flowable emitting a list of tools available under the specified context.
*/
Single<List<BaseTool>> getTools(ReadonlyContext readonlyContext);
Flowable<List<BaseTool>> getTools(ReadonlyContext readonlyContext);

/**
* Performs cleanup and releases resources held by the toolset.
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/google/adk/tools/mcp/McpToolset.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.spec.McpSchema.ListToolsResult;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.Flowable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -151,8 +151,8 @@ public McpToolset(ServerParameters connectionParams) {
}

@Override
public Single<List<BaseTool>> getTools(ReadonlyContext readonlyContext) {
return Single.fromCallable(
public Flowable<List<BaseTool>> getTools(ReadonlyContext readonlyContext) {
return Flowable.fromCallable(
() -> {
for (int i = 0; i < MAX_RETRIES; i++) {
try {
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/com/google/adk/tools/BaseToolsetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.google.adk.agents.ReadonlyContext;
import com.google.common.collect.ImmutableList;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.Flowable;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -23,15 +23,15 @@ public void testGetTools() {
BaseToolset toolset =
new BaseToolset() {
@Override
public Single<List<BaseTool>> getTools(ReadonlyContext readonlyContext) {
return Single.just(ImmutableList.of(mockTool1, mockTool2));
public Flowable<List<BaseTool>> getTools(ReadonlyContext readonlyContext) {
return Flowable.just(ImmutableList.of(mockTool1, mockTool2));
}

@Override
public void close() throws Exception {}
};

List<BaseTool> tools = toolset.getTools(mockContext).blockingGet();
List<BaseTool> tools = toolset.getTools(mockContext).blockingFirst();
assertThat(tools).containsExactly(mockTool1, mockTool2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void buildGraphRecursive(

if (agent instanceof LlmAgent) {
LlmAgent llmAgent = (LlmAgent) agent;
List<BaseTool> tools = llmAgent.canonicalTools().blockingGet();
List<BaseTool> tools = llmAgent.canonicalTools().blockingFirst();
if (tools != null) {
for (BaseTool tool : tools) {
if (tool != null) {
Expand Down