Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/main/java/com/google/adk/tools/AgentTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
Event lastEvent = optionalLastEvent.get();
Optional<String> outputText = lastEvent.content().map(Content::text);

// Forward state delta to parent session.
if (lastEvent.actions() != null
&& lastEvent.actions().stateDelta() != null
&& !lastEvent.actions().stateDelta().isEmpty()) {
toolContext.state().putAll(lastEvent.actions().stateDelta());
}

if (outputText.isEmpty()) {
return ImmutableMap.of();
}
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/com/google/adk/tools/AgentToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.adk.agents.Callbacks.AfterAgentCallback;
import com.google.adk.agents.ConfigAgentUtils.ConfigurationException;
import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.LlmAgent;
Expand All @@ -35,6 +36,7 @@
import com.google.genai.types.Part;
import com.google.genai.types.Schema;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
Expand Down Expand Up @@ -421,6 +423,35 @@ public void call_withoutInputSchema_requestIsSentToAgent() throws Exception {
.containsExactly(Content.fromParts(Part.fromText("magic")));
}

@Test
public void call_withStateDeltaInResponse_propagatesStateDelta() throws Exception {
AfterAgentCallback afterAgentCallback =
(callbackContext) -> {
callbackContext.state().put("test_key", "test_value");
return Maybe.empty();
};
TestLlm testLlm =
createTestLlm(
LlmResponse.builder()
.content(Content.fromParts(Part.fromText("test response")))
.build());
LlmAgent testAgent =
createTestAgentBuilder(testLlm)
.name("agent name")
.description("agent description")
.afterAgentCallback(afterAgentCallback)
.build();
AgentTool agentTool = AgentTool.create(testAgent);
ToolContext toolContext = createToolContext(testAgent);

assertThat(toolContext.state()).doesNotContainKey("test_key");

Map<String, Object> unused =
agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet();

assertThat(toolContext.state()).containsEntry("test_key", "test_value");
}

private static ToolContext createToolContext(LlmAgent agent) {
return ToolContext.builder(
new InvocationContext(
Expand Down