|
| 1 | +package com.google.adk.runner; |
| 2 | + |
| 3 | +import static com.google.adk.testing.TestUtils.createContent; |
| 4 | +import static com.google.adk.testing.TestUtils.createLlmResponse; |
| 5 | +import static com.google.adk.testing.TestUtils.createTestLlm; |
| 6 | +import static com.google.adk.testing.TestUtils.simplifyEvents; |
| 7 | +import static com.google.common.truth.Truth.assertThat; |
| 8 | + |
| 9 | +import com.google.adk.agents.BaseAgent; |
| 10 | +import com.google.adk.agents.InvocationContext; |
| 11 | +import com.google.adk.agents.LlmAgent; |
| 12 | +import com.google.adk.agents.Resumable; |
| 13 | +import com.google.adk.apps.App; |
| 14 | +import com.google.adk.events.Event; |
| 15 | +import com.google.adk.sessions.Session; |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import com.google.genai.types.Content; |
| 18 | +import com.google.genai.types.Part; |
| 19 | +import io.reactivex.rxjava3.core.Flowable; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.junit.runners.JUnit4; |
| 23 | + |
| 24 | +@RunWith(JUnit4.class) |
| 25 | +public final class RunnerResumabilityTest { |
| 26 | + |
| 27 | + private static class TestResumableAgent extends BaseAgent implements Resumable { |
| 28 | + private final boolean resumable; |
| 29 | + |
| 30 | + public TestResumableAgent(String name, boolean resumable) { |
| 31 | + super(name, "", ImmutableList.of(), ImmutableList.of(), ImmutableList.of()); |
| 32 | + this.resumable = resumable; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean isResumable() { |
| 37 | + return resumable; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Flowable<Event> runAsyncImpl(InvocationContext context) { |
| 42 | + return Flowable.just( |
| 43 | + Event.builder() |
| 44 | + .id("event-" + name()) |
| 45 | + .author(name()) |
| 46 | + .content( |
| 47 | + Content.builder() |
| 48 | + .parts( |
| 49 | + ImmutableList.of(Part.builder().text("response from " + name()).build())) |
| 50 | + .build()) |
| 51 | + .build()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected Flowable<Event> runLiveImpl(InvocationContext context) { |
| 56 | + return runAsyncImpl(context); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void runAsync_resumesAtResumableSubAgent() { |
| 62 | + TestResumableAgent subAgent = new TestResumableAgent("sub_agent", true); |
| 63 | + LlmAgent rootAgent = |
| 64 | + LlmAgent.builder() |
| 65 | + .name("root_agent") |
| 66 | + .model(createTestLlm(createLlmResponse(createContent("from root")))) |
| 67 | + .subAgents(ImmutableList.of(subAgent)) |
| 68 | + .build(); |
| 69 | + |
| 70 | + Runner runner = |
| 71 | + Runner.builder().app(App.builder().name("test").rootAgent(rootAgent).build()).build(); |
| 72 | + |
| 73 | + Session session = runner.sessionService().createSession("test", "user").blockingGet(); |
| 74 | + |
| 75 | + Event subAgentEvent = |
| 76 | + Event.builder() |
| 77 | + .id("initial-event") |
| 78 | + .author("sub_agent") |
| 79 | + .content(createContent("subagent greeting")) |
| 80 | + .build(); |
| 81 | + |
| 82 | + var unused = runner.sessionService().appendEvent(session, subAgentEvent).blockingGet(); |
| 83 | + |
| 84 | + var events = |
| 85 | + runner.runAsync("user", session.id(), createContent("continue")).toList().blockingGet(); |
| 86 | + |
| 87 | + assertThat(simplifyEvents(events)).containsExactly("sub_agent: response from sub_agent"); |
| 88 | + } |
| 89 | +} |
0 commit comments