Skip to content

Commit 39f9357

Browse files
author
wenhaozhao
committed
Merge remote-tracking branch 'origin/main' into feat-mcp_client_timeout
2 parents b76a6db + d204294 commit 39f9357

File tree

8 files changed

+320
-63
lines changed

8 files changed

+320
-63
lines changed

core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.google.adk.tools.ToolContext;
4040
import com.google.common.collect.ImmutableList;
4141
import com.google.common.collect.Iterables;
42-
import com.google.genai.types.FunctionCall;
4342
import com.google.genai.types.FunctionResponse;
4443
import io.opentelemetry.api.trace.Span;
4544
import io.opentelemetry.api.trace.StatusCode;
@@ -621,11 +620,10 @@ private Event buildModelResponseEvent(
621620

622621
Event event = eventBuilder.build();
623622

624-
ImmutableList<FunctionCall> functionCalls = event.functionCalls();
625-
if (!functionCalls.isEmpty()) {
623+
if (!event.functionCalls().isEmpty()) {
626624
Functions.populateClientFunctionCallId(event);
627625
Set<String> longRunningToolIds =
628-
Functions.getLongRunningFunctionCalls(functionCalls, llmRequest.tools());
626+
Functions.getLongRunningFunctionCalls(event.functionCalls(), llmRequest.tools());
629627
if (!longRunningToolIds.isEmpty()) {
630628
event.setLongRunningToolIds(Optional.of(longRunningToolIds));
631629
}

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import io.reactivex.rxjava3.core.Flowable;
3939
import io.reactivex.rxjava3.core.Maybe;
4040
import io.reactivex.rxjava3.core.Single;
41-
import io.reactivex.rxjava3.core.Completable;
4241
import java.util.ArrayList;
4342
import java.util.Collections;
4443
import java.util.List;
@@ -84,7 +83,7 @@ public BaseSessionService sessionService() {
8483
*
8584
* @throws IllegalArgumentException if message has no parts.
8685
*/
87-
private Completable appendNewMessageToSession(
86+
private void appendNewMessageToSession(
8887
Session session,
8988
Content newMessage,
9089
InvocationContext invocationContext,
@@ -124,7 +123,7 @@ private Completable appendNewMessageToSession(
124123
.author("user")
125124
.content(Optional.of(newMessage))
126125
.build();
127-
return this.sessionService.appendEvent(session, event).ignoreElement();
126+
this.sessionService.appendEvent(session, event);
128127
}
129128

130129
/**
@@ -191,20 +190,14 @@ public Flowable<Event> runAsync(Session session, Content newMessage, RunConfig r
191190
newMessage,
192191
runConfig);
193192

194-
Completable newMessageCompletable = Completable.complete();
195193
if (newMessage != null) {
196-
newMessageCompletable =
197-
appendNewMessageToSession(
198-
sess,
199-
newMessage,
200-
invocationContext,
201-
runConfig.saveInputBlobsAsArtifacts());
194+
appendNewMessageToSession(
195+
sess, newMessage, invocationContext, runConfig.saveInputBlobsAsArtifacts());
202196
}
203197

204198
invocationContext.agent(this.findAgentToRun(sess, rootAgent));
205199
Flowable<Event> events = invocationContext.agent().runAsync(invocationContext);
206-
return newMessageCompletable.andThen(
207-
events.concatMapSingle(event -> this.sessionService.appendEvent(sess, event)));
200+
return events.doOnNext(event -> this.sessionService.appendEvent(sess, event));
208201
})
209202
.doOnError(
210203
throwable -> {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ public static LongRunningFunctionTool create(Class<?> cls, String methodName) {
3535
String.format("Method %s not found in class %s.", methodName, cls.getName()));
3636
}
3737

38+
public static LongRunningFunctionTool create(Object instance, String methodName) {
39+
Class<?> cls = instance.getClass();
40+
for (Method method : cls.getMethods()) {
41+
if (method.getName().equals(methodName)) {
42+
return new LongRunningFunctionTool(instance, method);
43+
}
44+
}
45+
throw new IllegalArgumentException(
46+
String.format("Method %s not found in class %s.", methodName, cls.getName()));
47+
}
48+
3849
private LongRunningFunctionTool(Method func) {
3950
super(null, func, /* isLongRunning= */ true);
4051
}
52+
53+
private LongRunningFunctionTool(Object instance, Method func) {
54+
super(instance, func, /* isLongRunning= */ true);
55+
}
4156
}

core/src/main/java/com/google/adk/tools/applicationintegrationtoolset/ApplicationIntegrationToolset.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
import com.fasterxml.jackson.databind.JsonNode;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.node.ObjectNode;
8+
import com.google.adk.agents.ReadonlyContext;
89
import com.google.adk.tools.BaseTool;
10+
import com.google.adk.tools.BaseToolset;
911
import com.google.adk.tools.applicationintegrationtoolset.IntegrationConnectorTool.DefaultHttpExecutor;
1012
import com.google.adk.tools.applicationintegrationtoolset.IntegrationConnectorTool.HttpExecutor;
13+
import io.reactivex.rxjava3.core.Flowable;
1114
import java.util.ArrayList;
1215
import java.util.Iterator;
1316
import java.util.List;
1417
import java.util.Map;
1518
import 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

Comments
 (0)