Skip to content

Commit 64d3a77

Browse files
tilgalascopybara-github
authored andcommitted
fix: make a mutable copy of function args for the beforeToolCallback invocations
PiperOrigin-RevId: 876191407
1 parent 482fc76 commit 64d3a77

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ private static Function<FunctionCall, Maybe<Event>> getFunctionCallMapper(
253253
functionCall.id().map(toolConfirmations::get).orElse(null))
254254
.build();
255255

256-
Map<String, Object> functionArgs = functionCall.args().orElse(new HashMap<>());
256+
Map<String, Object> functionArgs =
257+
functionCall.args().map(HashMap::new).orElse(new HashMap<>());
257258

258259
Maybe<Map<String, Object>> maybeFunctionResult =
259260
maybeInvokeBeforeToolCall(invocationContext, tool, functionArgs, toolContext)
@@ -482,12 +483,8 @@ private static Maybe<Map<String, Object>> maybeInvokeBeforeToolCall(
482483
if (invocationContext.agent() instanceof LlmAgent) {
483484
LlmAgent agent = (LlmAgent) invocationContext.agent();
484485

485-
HashMap<String, Object> mutableFunctionArgs = new HashMap<>(functionArgs);
486-
487486
Maybe<Map<String, Object>> pluginResult =
488-
invocationContext
489-
.pluginManager()
490-
.beforeToolCallback(tool, mutableFunctionArgs, toolContext);
487+
invocationContext.pluginManager().beforeToolCallback(tool, functionArgs, toolContext);
491488

492489
List<? extends BeforeToolCallback> callbacks = agent.canonicalBeforeToolCallbacks();
493490
if (callbacks.isEmpty()) {
@@ -500,8 +497,7 @@ private static Maybe<Map<String, Object>> maybeInvokeBeforeToolCall(
500497
Flowable.fromIterable(callbacks)
501498
.concatMapMaybe(
502499
callback ->
503-
callback.call(
504-
invocationContext, tool, mutableFunctionArgs, toolContext))
500+
callback.call(invocationContext, tool, functionArgs, toolContext))
505501
.firstElement());
506502

507503
return pluginResult.switchIfEmpty(callbackResult);

core/src/test/java/com/google/adk/agents/CallbacksTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,51 @@ public Maybe<Map<String, Object>> beforeToolCallback(
11721172
event,
11731173
ImmutableMap.of("echo_tool", new TestUtils.FailingEchoTool()))
11741174
.blockingGet();
1175-
11761175
assertThat(getFunctionResponse(functionResponseEvent)).isEqualTo(responseFromAgentCb);
11771176
}
11781177

1178+
@Test
1179+
public void handleFunctionCalls_withBeforeToolCallback_modifiesArgs() {
1180+
ImmutableMap<String, Object> originalArgs = ImmutableMap.of("arg1", "val1");
1181+
ImmutableMap<String, Object> modifiedArgs = ImmutableMap.of("arg1", "val1", "arg2", "val2");
1182+
1183+
Callbacks.BeforeToolCallbackSync cb1 =
1184+
(invocationContext, tool, input, toolContext) -> {
1185+
input.put("arg2", "val2");
1186+
return Optional.empty();
1187+
};
1188+
1189+
TestUtils.EchoTool echoTool = new TestUtils.EchoTool();
1190+
1191+
InvocationContext invocationContext =
1192+
createInvocationContext(
1193+
createTestAgentBuilder(createTestLlm(LlmResponse.builder().build()))
1194+
.beforeToolCallbackSync(cb1)
1195+
.build());
1196+
1197+
Event event =
1198+
createEvent("event").toBuilder()
1199+
.content(
1200+
Content.fromParts(
1201+
Part.fromText("..."),
1202+
Part.builder()
1203+
.functionCall(
1204+
FunctionCall.builder()
1205+
.id("fc_id")
1206+
.name("echo_tool")
1207+
.args(originalArgs)
1208+
.build())
1209+
.build()))
1210+
.build();
1211+
1212+
Event functionResponseEvent =
1213+
Functions.handleFunctionCalls(
1214+
invocationContext, event, ImmutableMap.of("echo_tool", echoTool))
1215+
.blockingGet();
1216+
1217+
assertThat(getFunctionResponse(functionResponseEvent)).containsExactly("result", modifiedArgs);
1218+
}
1219+
11791220
@Test
11801221
public void agentRunAsync_withToolCallbacks_inspectsArgsAndReturnsResponse() {
11811222
TestUtils.EchoTool echoTool = new TestUtils.EchoTool();

0 commit comments

Comments
 (0)