diff --git a/core/src/main/java/com/google/adk/tools/AgentTool.java b/core/src/main/java/com/google/adk/tools/AgentTool.java index 5e8798d0e..ab821a221 100644 --- a/core/src/main/java/com/google/adk/tools/AgentTool.java +++ b/core/src/main/java/com/google/adk/tools/AgentTool.java @@ -25,6 +25,8 @@ import com.google.adk.agents.ConfigAgentUtils; import com.google.adk.agents.ConfigAgentUtils.ConfigurationException; import com.google.adk.agents.LlmAgent; +import com.google.adk.agents.RunConfig; +import com.google.adk.agents.RunConfig.StreamingMode; import com.google.adk.events.Event; import com.google.adk.plugins.Plugin; import com.google.adk.runner.InMemoryRunner; @@ -186,10 +188,17 @@ public Single> runAsync(Map args, ToolContex ? ImmutableList.of(toolContext.invocationContext().pluginManager()) : ImmutableList.of(); Runner runner = new InMemoryRunner(this.agent, toolContext.agentName(), plugins); + // Follow the caller's RunConfig but run unary: only the last event becomes the result. + RunConfig callerRunConfig = toolContext.invocationContext().runConfig(); + RunConfig runConfig = + callerRunConfig.streamingMode() == StreamingMode.NONE + ? callerRunConfig + : callerRunConfig.toBuilder().streamingMode(StreamingMode.NONE).build(); return runner .sessionService() .createSession(toolContext.agentName(), "tmp-user", toolContext.state(), null) - .flatMapPublisher(session -> runner.runAsync(session.userId(), session.id(), content)) + .flatMapPublisher( + session -> runner.runAsync(session.userId(), session.id(), content, runConfig)) .doOnNext( event -> { if (event.actions() != null diff --git a/core/src/test/java/com/google/adk/tools/AgentToolTest.java b/core/src/test/java/com/google/adk/tools/AgentToolTest.java index 755cebc46..6a8b82f8d 100644 --- a/core/src/test/java/com/google/adk/tools/AgentToolTest.java +++ b/core/src/test/java/com/google/adk/tools/AgentToolTest.java @@ -16,6 +16,8 @@ package com.google.adk.tools; +import static com.google.adk.testing.TestUtils.createInvocationContext; +import static com.google.adk.testing.TestUtils.createSubAgent; import static com.google.adk.testing.TestUtils.createTestAgentBuilder; import static com.google.adk.testing.TestUtils.createTestLlm; import static com.google.common.truth.Truth.assertThat; @@ -26,12 +28,16 @@ import com.google.adk.agents.ConfigAgentUtils.ConfigurationException; import com.google.adk.agents.InvocationContext; import com.google.adk.agents.LlmAgent; +import com.google.adk.agents.RunConfig; +import com.google.adk.agents.RunConfig.StreamingMode; +import com.google.adk.agents.RunConfig.ToolExecutionMode; import com.google.adk.agents.SequentialAgent; import com.google.adk.models.LlmResponse; import com.google.adk.plugins.Plugin; import com.google.adk.plugins.PluginManager; import com.google.adk.sessions.InMemorySessionService; import com.google.adk.sessions.Session; +import com.google.adk.testing.TestBaseAgent; import com.google.adk.testing.TestLlm; import com.google.adk.utils.ComponentRegistry; import com.google.common.collect.ImmutableList; @@ -871,6 +877,43 @@ public Maybe beforeRunCallback(InvocationContext invocationContext) { assertThat(callbackCalled.get()).isFalse(); } + @Test + public void call_propagatesCallerRunConfig() throws Exception { + TestBaseAgent testAgent = createSubAgent("agent_name"); + AgentTool agentTool = AgentTool.create(testAgent); + RunConfig runConfig = + RunConfig.builder() + .toolExecutionMode(ToolExecutionMode.SEQUENTIAL) + .maxLlmCalls(7) + .customMetadata(ImmutableMap.of("tier", "x")) + .build(); + ToolContext toolContext = + ToolContext.builder(createInvocationContext(testAgent, runConfig)).build(); + + Map unused = + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); + + assertThat(testAgent.getLastInvocationContext().runConfig()).isEqualTo(runConfig); + } + + @Test + public void call_withStreamingRunConfig_runsAgentWithoutStreaming() throws Exception { + for (StreamingMode streamingMode : ImmutableList.of(StreamingMode.SSE, StreamingMode.BIDI)) { + TestBaseAgent testAgent = createSubAgent("agent_name"); + AgentTool agentTool = AgentTool.create(testAgent); + RunConfig runConfig = RunConfig.builder().streamingMode(streamingMode).maxLlmCalls(7).build(); + ToolContext toolContext = + ToolContext.builder(createInvocationContext(testAgent, runConfig)).build(); + + Map unused = + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); + + // Only the streaming mode changes; the other settings still come from the caller. + assertThat(testAgent.getLastInvocationContext().runConfig()) + .isEqualTo(runConfig.toBuilder().streamingMode(StreamingMode.NONE).build()); + } + } + private ToolContext createToolContext(BaseAgent agent) { Session session = sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet();