1616
1717package com .google .adk .agents ;
1818
19+ import static com .google .common .collect .ImmutableList .toImmutableList ;
1920import static java .util .stream .Collectors .joining ;
2021
2122import com .fasterxml .jackson .core .JsonProcessingException ;
4748import com .google .adk .models .BaseLlm ;
4849import com .google .adk .models .Model ;
4950import com .google .adk .tools .BaseTool ;
51+ import com .google .adk .tools .BaseToolset ;
5052import com .google .common .base .Preconditions ;
5153import com .google .common .collect .ImmutableList ;
5254import com .google .errorprone .annotations .CanIgnoreReturnValue ;
5658import io .reactivex .rxjava3 .core .Flowable ;
5759import io .reactivex .rxjava3 .core .Maybe ;
5860import io .reactivex .rxjava3 .core .Single ;
61+ import java .util .ArrayList ;
5962import java .util .List ;
6063import java .util .Map ;
6164import java .util .Optional ;
@@ -81,7 +84,7 @@ public enum IncludeContents {
8184 private final Optional <Model > model ;
8285 private final Instruction instruction ;
8386 private final Instruction globalInstruction ;
84- private final List <BaseTool > tools ;
87+ private final List <Object > toolsUnion ;
8588 private final Optional <GenerateContentConfig > generateContentConfig ;
8689 private final Optional <BaseExampleProvider > exampleProvider ;
8790 private final IncludeContents includeContents ;
@@ -130,7 +133,7 @@ protected LlmAgent(Builder builder) {
130133 this .outputSchema = Optional .ofNullable (builder .outputSchema );
131134 this .executor = Optional .ofNullable (builder .executor );
132135 this .outputKey = Optional .ofNullable (builder .outputKey );
133- this .tools = builder .tools != null ? builder .tools : ImmutableList .of ();
136+ this .toolsUnion = builder .toolsUnion != null ? builder .toolsUnion : ImmutableList .of ();
134137
135138 this .llmFlow = determineLlmFlow ();
136139
@@ -153,7 +156,7 @@ public static class Builder {
153156 private Instruction instruction ;
154157 private Instruction globalInstruction ;
155158 private ImmutableList <BaseAgent > subAgents ;
156- private ImmutableList <BaseTool > tools ;
159+ private ImmutableList <Object > toolsUnion ;
157160 private GenerateContentConfig generateContentConfig ;
158161 private BaseExampleProvider exampleProvider ;
159162 private IncludeContents includeContents ;
@@ -234,14 +237,14 @@ public Builder subAgents(BaseAgent... subAgents) {
234237 }
235238
236239 @ CanIgnoreReturnValue
237- public Builder tools (List <? extends BaseTool > tools ) {
238- this .tools = ImmutableList .copyOf (tools );
240+ public Builder tools (List <?> tools ) {
241+ this .toolsUnion = ImmutableList .copyOf (tools );
239242 return this ;
240243 }
241244
242245 @ CanIgnoreReturnValue
243- public Builder tools (BaseTool ... tools ) {
244- this .tools = ImmutableList .copyOf (tools );
246+ public Builder tools (Object ... tools ) {
247+ this .toolsUnion = ImmutableList .copyOf (tools );
245248 return this ;
246249 }
247250
@@ -580,7 +583,7 @@ protected void validate() {
580583 + ": if outputSchema is set, subAgents must be empty to disable agent"
581584 + " transfer." );
582585 }
583- if (this .tools != null && !this .tools .isEmpty ()) {
586+ if (this .toolsUnion != null && !this .toolsUnion .isEmpty ()) {
584587 throw new IllegalArgumentException (
585588 "Invalid config for agent "
586589 + this .name
@@ -687,6 +690,42 @@ public Single<String> canonicalGlobalInstruction(ReadonlyContext context) {
687690 throw new IllegalStateException ("Unknown Instruction subtype: " + instruction .getClass ());
688691 }
689692
693+ /**
694+ * Constructs the list of tools for this agent based on the {@link #tools} field.
695+ *
696+ * <p>This method is only for use by Agent Development Kit.
697+ *
698+ * @param context The context to retrieve the session state.
699+ * @return The resolved list of tools as a {@link Single} wrapped list of {@link BaseTool}.
700+ */
701+ public Single <List <BaseTool >> canonicalTools (Optional <ReadonlyContext > context ) {
702+ List <Single <List <BaseTool >>> toolSingles = new ArrayList <>();
703+ for (Object toolOrToolset : toolsUnion ) {
704+ if (toolOrToolset instanceof BaseTool baseTool ) {
705+ toolSingles .add (Single .just (ImmutableList .of (baseTool )));
706+ } else if (toolOrToolset instanceof BaseToolset baseToolset ) {
707+ toolSingles .add (baseToolset .getTools (context .orElse (null )));
708+ } else {
709+ throw new IllegalArgumentException (
710+ "Object in tools list is not of a supported type: "
711+ + toolOrToolset .getClass ().getName ());
712+ }
713+ }
714+ return Single .concat (toolSingles )
715+ .toList ()
716+ .map (listOfLists -> listOfLists .stream ().flatMap (List ::stream ).collect (toImmutableList ()));
717+ }
718+
719+ /** Overload of canonicalTools that defaults to an empty context. */
720+ public Single <List <BaseTool >> canonicalTools () {
721+ return canonicalTools (Optional .empty ());
722+ }
723+
724+ /** Convenience overload of canonicalTools that accepts a non-optional ReadonlyContext. */
725+ public Single <List <BaseTool >> canonicalTools (ReadonlyContext context ) {
726+ return canonicalTools (Optional .ofNullable (context ));
727+ }
728+
690729 public Instruction instruction () {
691730 return instruction ;
692731 }
@@ -719,8 +758,8 @@ public IncludeContents includeContents() {
719758 return includeContents ;
720759 }
721760
722- public List <BaseTool > tools () {
723- return tools ;
761+ public List <Object > tools () {
762+ return toolsUnion ;
724763 }
725764
726765 public boolean disallowTransferToParent () {
0 commit comments