55import com .fasterxml .jackson .databind .JsonNode ;
66import com .fasterxml .jackson .databind .ObjectMapper ;
77import com .fasterxml .jackson .databind .node .ObjectNode ;
8+ import com .google .adk .agents .ReadonlyContext ;
89import com .google .adk .tools .BaseTool ;
10+ import com .google .adk .tools .BaseToolset ;
911import com .google .adk .tools .applicationintegrationtoolset .IntegrationConnectorTool .DefaultHttpExecutor ;
1012import com .google .adk .tools .applicationintegrationtoolset .IntegrationConnectorTool .HttpExecutor ;
13+ import io .reactivex .rxjava3 .core .Flowable ;
1114import java .util .ArrayList ;
1215import java .util .Iterator ;
1316import java .util .List ;
1417import java .util .Map ;
1518import org .jspecify .annotations .Nullable ;
1619
1720/** Application Integration Toolset */
18- public class ApplicationIntegrationToolset {
21+ public class ApplicationIntegrationToolset implements BaseToolset {
1922 String project ;
2023 String location ;
2124 @ Nullable String integration ;
2225 @ Nullable List <String > triggers ;
2326 @ Nullable String connection ;
2427 @ Nullable Map <String , List <String >> entityOperations ;
2528 @ Nullable List <String > actions ;
29+ String serviceAccountJson ;
2630 @ Nullable String toolNamePrefix ;
2731 @ Nullable String toolInstructions ;
2832 public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
@@ -35,16 +39,16 @@ public class ApplicationIntegrationToolset {
3539 *
3640 * <p>integrationTool = new ApplicationIntegrationToolset( project="test-project",
3741 * location="us-central1", integration="test-integration",
38- * triggers=ImmutableList.of("api_trigger/test_trigger",
39- * "api_trigger/test_trigger_2 "),connection=null,enitityOperations=null,actions=null,toolNamePrefix="test-integration-tool",toolInstructions="This
42+ * triggers=ImmutableList.of("api_trigger/test_trigger", "api_trigger/test_trigger_2",
43+ * serviceAccountJson="{....} "),connection=null,enitityOperations=null,actions=null,toolNamePrefix="test-integration-tool",toolInstructions="This
4044 * tool is used to get response from test-integration.");
4145 *
4246 * <p>connectionTool = new ApplicationIntegrationToolset( project="test-project",
4347 * location="us-central1", integration=null, triggers=null, connection="test-connection",
4448 * entityOperations=ImmutableMap.of("Entity1", ImmutableList.of("LIST", "GET", "UPDATE")),
4549 * "Entity2", ImmutableList.of()), actions=ImmutableList.of("ExecuteCustomQuery"),
46- * toolNamePrefix="test-tool", toolInstructions="This tool is used to list, get and update issues
47- * in Jira.");
50+ * serviceAccountJson="{....}", toolNamePrefix="test-tool", toolInstructions="This tool is used to
51+ * list, get and update issues in Jira.");
4852 *
4953 * @param project The GCP project ID.
5054 * @param location The GCP location of integration.
@@ -53,6 +57,9 @@ public class ApplicationIntegrationToolset {
5357 * @param connection(Optional) The connection name.
5458 * @param entityOperations(Optional) The entity operations.
5559 * @param actions(Optional) The actions.
60+ * @param serviceAccountJson(Optional) The service account configuration as a dictionary. Required
61+ * if not using default service credential. Used for fetching the Application Integration or
62+ * Integration Connector resource.
5663 * @param toolNamePrefix(Optional) The tool name prefix.
5764 * @param toolInstructions(Optional) The tool instructions.
5865 */
@@ -64,6 +71,7 @@ public ApplicationIntegrationToolset(
6471 String connection ,
6572 Map <String , List <String >> entityOperations ,
6673 List <String > actions ,
74+ String serviceAccountJson ,
6775 String toolNamePrefix ,
6876 String toolInstructions ) {
6977 this (
@@ -74,9 +82,10 @@ public ApplicationIntegrationToolset(
7482 connection ,
7583 entityOperations ,
7684 actions ,
85+ serviceAccountJson ,
7786 toolNamePrefix ,
7887 toolInstructions ,
79- new DefaultHttpExecutor ());
88+ new DefaultHttpExecutor (). createExecutor ( serviceAccountJson ) );
8089 }
8190
8291 ApplicationIntegrationToolset (
@@ -87,6 +96,7 @@ public ApplicationIntegrationToolset(
8796 String connection ,
8897 Map <String , List <String >> entityOperations ,
8998 List <String > actions ,
99+ String serviceAccountJson ,
90100 String toolNamePrefix ,
91101 String toolInstructions ,
92102 HttpExecutor httpExecutor ) {
@@ -97,6 +107,7 @@ public ApplicationIntegrationToolset(
97107 this .connection = connection ;
98108 this .entityOperations = entityOperations ;
99109 this .actions = actions ;
110+ this .serviceAccountJson = serviceAccountJson ;
100111 this .toolNamePrefix = toolNamePrefix ;
101112 this .toolInstructions = toolInstructions ;
102113 this .httpExecutor = httpExecutor ;
@@ -121,7 +132,7 @@ List<String> getPathUrl(String openApiSchemaString) throws Exception {
121132 return pathUrls ;
122133 }
123134
124- public List <BaseTool > getTools () throws Exception {
135+ private List <BaseTool > getAllTools () throws Exception {
125136 String openApiSchemaString = null ;
126137 List <BaseTool > tools = new ArrayList <>();
127138 if (!isNullOrEmpty (this .integration )) {
@@ -142,7 +153,15 @@ public List<BaseTool> getTools() throws Exception {
142153 if (toolName != null ) {
143154 tools .add (
144155 new IntegrationConnectorTool (
145- openApiSchemaString , pathUrl , toolName , "" , null , null , null , this .httpExecutor ));
156+ openApiSchemaString ,
157+ pathUrl ,
158+ toolName ,
159+ toolInstructions ,
160+ null ,
161+ null ,
162+ null ,
163+ this .serviceAccountJson ,
164+ this .httpExecutor ));
146165 }
147166 }
148167 } else if (!isNullOrEmpty (this .connection )
@@ -182,6 +201,7 @@ public List<BaseTool> getTools() throws Exception {
182201 connectionDetails .name ,
183202 connectionDetails .serviceName ,
184203 connectionDetails .host ,
204+ this .serviceAccountJson ,
185205 this .httpExecutor ));
186206 }
187207 }
@@ -193,4 +213,19 @@ public List<BaseTool> getTools() throws Exception {
193213
194214 return tools ;
195215 }
216+
217+ @ Override
218+ public Flowable <BaseTool > getTools (@ Nullable ReadonlyContext readonlyContext ) {
219+ try {
220+ List <BaseTool > allTools = getAllTools ();
221+ return Flowable .fromIterable (allTools );
222+ } catch (Exception e ) {
223+ return Flowable .error (e );
224+ }
225+ }
226+
227+ @ Override
228+ public void close () throws Exception {
229+ // Nothing to close.
230+ }
196231}
0 commit comments