|
49 | 49 | import static org.mockito.Mockito.verify; |
50 | 50 | import static org.mockito.Mockito.when; |
51 | 51 |
|
| 52 | +import com.google.adk.agents.BaseAgent; |
| 53 | +import com.google.adk.agents.CallbackContext; |
52 | 54 | import com.google.adk.agents.InvocationContext; |
53 | 55 | import com.google.adk.agents.LlmAgent; |
54 | 56 | import com.google.adk.agents.LoopAgent; |
|
77 | 79 | import com.google.genai.types.Part; |
78 | 80 | import io.opentelemetry.api.trace.Tracer; |
79 | 81 | import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; |
| 82 | +import io.reactivex.rxjava3.core.Completable; |
80 | 83 | import io.reactivex.rxjava3.core.Flowable; |
81 | 84 | import io.reactivex.rxjava3.core.Maybe; |
82 | 85 | import java.time.Instant; |
| 86 | +import java.util.ArrayList; |
83 | 87 | import java.util.List; |
| 88 | +import java.util.Map; |
84 | 89 | import java.util.Objects; |
85 | 90 | import java.util.Optional; |
86 | 91 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -486,6 +491,219 @@ public void runAsync_resume_runsFullPluginBracket() { |
486 | 491 | verify(resumePlugin, atLeastOnce()).onEventCallback(any(), any()); |
487 | 492 | } |
488 | 493 |
|
| 494 | + // A resume hands beforeRunCallback and the agent the callback data stored in |
| 495 | + // onUserMessageCallback, as a new invocation does. |
| 496 | + @Test |
| 497 | + public void runAsync_resume_keepsCallbackContextDataFromOnUserMessage() { |
| 498 | + CallbackDataRelayPlugin plugin = new CallbackDataRelayPlugin(); |
| 499 | + TestLlm testLlm = |
| 500 | + createTestLlm( |
| 501 | + createFunctionCallLlmResponse( |
| 502 | + "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")), |
| 503 | + createTextLlmResponse("resumed and summarized")); |
| 504 | + LlmAgent agent = |
| 505 | + createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build(); |
| 506 | + Runner runner = resumableRunner(agent, plugin); |
| 507 | + Session session = newSession(runner); |
| 508 | + |
| 509 | + runTurn(runner, session, "start"); |
| 510 | + answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")); |
| 511 | + |
| 512 | + // First the new invocation, then the resume, whose callbacks used to see an empty map. |
| 513 | + assertThat(plugin.seenBeforeRun).containsExactly("message-1", "message-2").inOrder(); |
| 514 | + assertThat(plugin.seenBeforeAgent) |
| 515 | + .containsExactly("agent:message-1", "agent:message-2") |
| 516 | + .inOrder(); |
| 517 | + } |
| 518 | + |
| 519 | + // A resume routed past the root to the sub-agent that paused hands it the same callback data. |
| 520 | + @Test |
| 521 | + public void runAsync_resumeRoutedToSubAgent_keepsCallbackContextDataFromOnUserMessage() { |
| 522 | + CallbackDataRelayPlugin plugin = new CallbackDataRelayPlugin(); |
| 523 | + Content transferCall = |
| 524 | + Content.fromParts( |
| 525 | + Part.fromFunctionCall( |
| 526 | + "transfer_to_agent", ImmutableMap.of("agent_name", "sub_agent_1"))); |
| 527 | + TestLlm testLlm = |
| 528 | + createTestLlm( |
| 529 | + createLlmResponse(transferCall), |
| 530 | + createTextLlmResponse("sub-agent answer"), |
| 531 | + createFunctionCallLlmResponse( |
| 532 | + "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")), |
| 533 | + createTextLlmResponse("resumed answer")); |
| 534 | + LlmAgent subAgent1 = |
| 535 | + createTestAgentBuilder(testLlm).name("sub_agent_1").tools(pendingFunctionTool()).build(); |
| 536 | + LlmAgent rootAgent = |
| 537 | + createTestAgentBuilder(testLlm) |
| 538 | + .name("root_agent") |
| 539 | + .subAgents(ImmutableList.of(subAgent1)) |
| 540 | + .build(); |
| 541 | + Runner runner = resumableRunner(rootAgent, plugin); |
| 542 | + Session session = newSession(runner); |
| 543 | + |
| 544 | + // The root transfers and the sub-agent answers; the next message goes to the sub-agent alone, |
| 545 | + // which pauses, so the resume routes to it rather than re-entering the root. |
| 546 | + runTurn(runner, session, "hi"); |
| 547 | + runTurn(runner, session, "start the job"); |
| 548 | + answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")); |
| 549 | + |
| 550 | + assertThat(plugin.seenBeforeAgent) |
| 551 | + .containsExactly( |
| 552 | + "root_agent:message-1", |
| 553 | + "sub_agent_1:message-1", |
| 554 | + "sub_agent_1:message-2", |
| 555 | + "sub_agent_1:message-3") |
| 556 | + .inOrder(); |
| 557 | + } |
| 558 | + |
| 559 | + // A plugin that ends the invocation in onUserMessageCallback ends a resume too, as a new |
| 560 | + // invocation. |
| 561 | + @Test |
| 562 | + public void runAsync_resume_endInvocationFromOnUserMessage_skipsTheModel() { |
| 563 | + TestLlm testLlm = |
| 564 | + createTestLlm( |
| 565 | + createFunctionCallLlmResponse( |
| 566 | + "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")), |
| 567 | + createTextLlmResponse("should not be reached")); |
| 568 | + LlmAgent agent = |
| 569 | + createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build(); |
| 570 | + Runner runner = resumableRunner(agent, new EndOnAnswerPlugin()); |
| 571 | + Session session = newSession(runner); |
| 572 | + |
| 573 | + runTurn(runner, session, "start"); |
| 574 | + assertThat(testLlm.getRequests()).hasSize(1); |
| 575 | + |
| 576 | + answerCall(runner, session, "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello")); |
| 577 | + |
| 578 | + // The plugin ended the resumed invocation, so the model is not asked again. |
| 579 | + assertThat(testLlm.getRequests()).hasSize(1); |
| 580 | + } |
| 581 | + |
| 582 | + // A resume without a new message hands onRunErrorCallback the callback data stored in |
| 583 | + // beforeRunCallback, as a new invocation does. |
| 584 | + @Test |
| 585 | + public void runAsync_resumeWithoutMessage_onRunErrorSeesBeforeRunData() { |
| 586 | + assertThat(onRunErrorReadsOnFailedResume(/* stateDelta= */ null)).containsExactly("run-2"); |
| 587 | + } |
| 588 | + |
| 589 | + // The same with only a state delta, which the resume stores as a content-less event first. |
| 590 | + @Test |
| 591 | + public void runAsync_resumeWithStateDeltaOnly_onRunErrorSeesBeforeRunData() { |
| 592 | + assertThat(onRunErrorReadsOnFailedResume(ImmutableMap.<String, Object>of("key", "value"))) |
| 593 | + .containsExactly("run-2"); |
| 594 | + } |
| 595 | + |
| 596 | + /** What onRunErrorCallback reads when a paused invocation resumes without a message and fails. */ |
| 597 | + private static ImmutableList<String> onRunErrorReadsOnFailedResume( |
| 598 | + @Nullable Map<String, Object> stateDelta) { |
| 599 | + FailAfterFirstRunPlugin plugin = new FailAfterFirstRunPlugin(); |
| 600 | + TestLlm testLlm = |
| 601 | + createTestLlm( |
| 602 | + createFunctionCallLlmResponse( |
| 603 | + "lro_call_id", "pendingTool", ImmutableMap.of("message", "hello"))); |
| 604 | + LlmAgent agent = |
| 605 | + createTestAgentBuilder(testLlm).name("agent").tools(pendingFunctionTool()).build(); |
| 606 | + Runner runner = resumableRunner(agent, plugin); |
| 607 | + Session session = newSession(runner); |
| 608 | + String invocationId = runTurn(runner, session, "start").get(0).invocationId(); |
| 609 | + |
| 610 | + resumeFlowable(runner, session, invocationId, /* newMessage= */ null, stateDelta) |
| 611 | + .test() |
| 612 | + .assertError(IllegalStateException.class); |
| 613 | + |
| 614 | + return ImmutableList.copyOf(plugin.seenOnRunError); |
| 615 | + } |
| 616 | + |
| 617 | + /** |
| 618 | + * Stores a numbered value before each run, fails every run after the first, and records what |
| 619 | + * onRunErrorCallback reads back. |
| 620 | + */ |
| 621 | + private static final class FailAfterFirstRunPlugin extends BasePlugin { |
| 622 | + private static final String KEY = "run"; |
| 623 | + private static final String VALUE = "run-%d"; |
| 624 | + |
| 625 | + private final AtomicInteger runs = new AtomicInteger(); |
| 626 | + private final List<String> seenOnRunError = new ArrayList<>(); |
| 627 | + |
| 628 | + FailAfterFirstRunPlugin() { |
| 629 | + super("fail-after-first-run"); |
| 630 | + } |
| 631 | + |
| 632 | + @Override |
| 633 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 634 | + int run = runs.incrementAndGet(); |
| 635 | + invocationContext.callbackContextData().put(KEY, VALUE.formatted(run)); |
| 636 | + return run == 1 ? Maybe.empty() : Maybe.error(new IllegalStateException("run failed")); |
| 637 | + } |
| 638 | + |
| 639 | + @Override |
| 640 | + public Completable onRunErrorCallback(InvocationContext invocationContext, Throwable error) { |
| 641 | + seenOnRunError.add(String.valueOf(invocationContext.callbackContextData().get(KEY))); |
| 642 | + return Completable.complete(); |
| 643 | + } |
| 644 | + } |
| 645 | + |
| 646 | + /** Ends the invocation when the message answers a call, so the pausing turn runs normally. */ |
| 647 | + private static final class EndOnAnswerPlugin extends BasePlugin { |
| 648 | + |
| 649 | + EndOnAnswerPlugin() { |
| 650 | + super("end-on-answer"); |
| 651 | + } |
| 652 | + |
| 653 | + @Override |
| 654 | + public Maybe<Content> onUserMessageCallback( |
| 655 | + InvocationContext invocationContext, Content userMessage) { |
| 656 | + if (answersACall(userMessage)) { |
| 657 | + invocationContext.setEndInvocation(true); |
| 658 | + } |
| 659 | + return Maybe.empty(); |
| 660 | + } |
| 661 | + |
| 662 | + private static boolean answersACall(Content message) { |
| 663 | + return message.parts().stream() |
| 664 | + .flatMap(List::stream) |
| 665 | + .anyMatch(part -> part.functionResponse().isPresent()); |
| 666 | + } |
| 667 | + } |
| 668 | + |
| 669 | + /** Stores a numbered value for each user message and records what later callbacks read back. */ |
| 670 | + private static final class CallbackDataRelayPlugin extends BasePlugin { |
| 671 | + private static final String KEY = "relay"; |
| 672 | + private static final String VALUE = "message-%d"; |
| 673 | + private static final String SEEN = "%s:%s"; |
| 674 | + |
| 675 | + private final AtomicInteger userMessages = new AtomicInteger(); |
| 676 | + private final List<String> seenBeforeRun = new ArrayList<>(); |
| 677 | + private final List<String> seenBeforeAgent = new ArrayList<>(); |
| 678 | + |
| 679 | + CallbackDataRelayPlugin() { |
| 680 | + super("callback-data-relay"); |
| 681 | + } |
| 682 | + |
| 683 | + @Override |
| 684 | + public Maybe<Content> onUserMessageCallback( |
| 685 | + InvocationContext invocationContext, Content userMessage) { |
| 686 | + invocationContext |
| 687 | + .callbackContextData() |
| 688 | + .put(KEY, VALUE.formatted(userMessages.incrementAndGet())); |
| 689 | + return Maybe.empty(); |
| 690 | + } |
| 691 | + |
| 692 | + @Override |
| 693 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 694 | + seenBeforeRun.add(String.valueOf(invocationContext.callbackContextData().get(KEY))); |
| 695 | + return Maybe.empty(); |
| 696 | + } |
| 697 | + |
| 698 | + @Override |
| 699 | + public Maybe<Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) { |
| 700 | + seenBeforeAgent.add( |
| 701 | + SEEN.formatted( |
| 702 | + agent.name(), callbackContext.invocationContext().callbackContextData().get(KEY))); |
| 703 | + return Maybe.empty(); |
| 704 | + } |
| 705 | + } |
| 706 | + |
489 | 707 | // An unanswered call keeps the invocation paused: plain text and no message are both inert. |
490 | 708 | @Test |
491 | 709 | public void resume_pausedCallUnanswered_staysPausedForPlainTextAndNoMessage() { |
|
0 commit comments