|
28 | 28 | import com.google.adk.agents.LlmAgent; |
29 | 29 | import com.google.adk.agents.SequentialAgent; |
30 | 30 | import com.google.adk.models.LlmResponse; |
| 31 | +import com.google.adk.plugins.Plugin; |
| 32 | +import com.google.adk.plugins.PluginManager; |
31 | 33 | import com.google.adk.sessions.InMemorySessionService; |
32 | 34 | import com.google.adk.sessions.Session; |
33 | 35 | import com.google.adk.testing.TestLlm; |
|
41 | 43 | import io.reactivex.rxjava3.core.Flowable; |
42 | 44 | import io.reactivex.rxjava3.core.Maybe; |
43 | 45 | import java.util.Map; |
| 46 | +import java.util.concurrent.atomic.AtomicBoolean; |
44 | 47 | import org.junit.Before; |
45 | 48 | import org.junit.Test; |
46 | 49 | import org.junit.runner.RunWith; |
@@ -704,6 +707,169 @@ public void declaration_emptySequentialAgent_fallsBackToRequest() { |
704 | 707 | .build()); |
705 | 708 | } |
706 | 709 |
|
| 710 | + @Test |
| 711 | + public void call_withIncludePluginsTrue_propagatesPlugins() throws Exception { |
| 712 | + AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 713 | + Plugin mockPlugin = |
| 714 | + new Plugin() { |
| 715 | + @Override |
| 716 | + public String getName() { |
| 717 | + return "mock_plugin"; |
| 718 | + } |
| 719 | + |
| 720 | + @Override |
| 721 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 722 | + callbackCalled.set(true); |
| 723 | + return Maybe.empty(); |
| 724 | + } |
| 725 | + }; |
| 726 | + LlmAgent testAgent = |
| 727 | + createTestAgentBuilder(createTestLlm(LlmResponse.builder().build())) |
| 728 | + .name("agent_name") |
| 729 | + .description("agent description") |
| 730 | + .build(); |
| 731 | + AgentTool agentTool = |
| 732 | + AgentTool.create(testAgent, /* skipSummarization= */ false, /* includePlugins= */ true); |
| 733 | + Session session = |
| 734 | + sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet(); |
| 735 | + InvocationContext invocationContext = |
| 736 | + InvocationContext.builder() |
| 737 | + .invocationId(InvocationContext.newInvocationContextId()) |
| 738 | + .agent(testAgent) |
| 739 | + .session(session) |
| 740 | + .sessionService(sessionService) |
| 741 | + .pluginManager(new PluginManager(ImmutableList.of(mockPlugin))) |
| 742 | + .build(); |
| 743 | + ToolContext toolContext = ToolContext.builder(invocationContext).build(); |
| 744 | + |
| 745 | + Map<String, Object> unused = |
| 746 | + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); |
| 747 | + |
| 748 | + assertThat(callbackCalled.get()).isTrue(); |
| 749 | + } |
| 750 | + |
| 751 | + @Test |
| 752 | + public void call_withIncludePluginsFalse_doesNotPropagatePlugins() throws Exception { |
| 753 | + AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 754 | + Plugin mockPlugin = |
| 755 | + new Plugin() { |
| 756 | + @Override |
| 757 | + public String getName() { |
| 758 | + return "mock_plugin"; |
| 759 | + } |
| 760 | + |
| 761 | + @Override |
| 762 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 763 | + callbackCalled.set(true); |
| 764 | + return Maybe.empty(); |
| 765 | + } |
| 766 | + }; |
| 767 | + LlmAgent testAgent = |
| 768 | + createTestAgentBuilder(createTestLlm(LlmResponse.builder().build())) |
| 769 | + .name("agent_name") |
| 770 | + .description("agent description") |
| 771 | + .build(); |
| 772 | + AgentTool agentTool = |
| 773 | + AgentTool.create(testAgent, /* skipSummarization= */ false, /* includePlugins= */ false); |
| 774 | + Session session = |
| 775 | + sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet(); |
| 776 | + InvocationContext invocationContext = |
| 777 | + InvocationContext.builder() |
| 778 | + .invocationId(InvocationContext.newInvocationContextId()) |
| 779 | + .agent(testAgent) |
| 780 | + .session(session) |
| 781 | + .sessionService(sessionService) |
| 782 | + .pluginManager(new PluginManager(ImmutableList.of(mockPlugin))) |
| 783 | + .build(); |
| 784 | + ToolContext toolContext = ToolContext.builder(invocationContext).build(); |
| 785 | + |
| 786 | + Map<String, Object> unused = |
| 787 | + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); |
| 788 | + |
| 789 | + assertThat(callbackCalled.get()).isFalse(); |
| 790 | + } |
| 791 | + |
| 792 | + @Test |
| 793 | + public void call_createWithAgentOnly_defaultsIncludePluginsToFalse() throws Exception { |
| 794 | + AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 795 | + Plugin mockPlugin = |
| 796 | + new Plugin() { |
| 797 | + @Override |
| 798 | + public String getName() { |
| 799 | + return "mock_plugin"; |
| 800 | + } |
| 801 | + |
| 802 | + @Override |
| 803 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 804 | + callbackCalled.set(true); |
| 805 | + return Maybe.empty(); |
| 806 | + } |
| 807 | + }; |
| 808 | + LlmAgent testAgent = |
| 809 | + createTestAgentBuilder(createTestLlm(LlmResponse.builder().build())) |
| 810 | + .name("agent_name") |
| 811 | + .description("agent description") |
| 812 | + .build(); |
| 813 | + AgentTool agentTool = AgentTool.create(testAgent); |
| 814 | + Session session = |
| 815 | + sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet(); |
| 816 | + InvocationContext invocationContext = |
| 817 | + InvocationContext.builder() |
| 818 | + .invocationId(InvocationContext.newInvocationContextId()) |
| 819 | + .agent(testAgent) |
| 820 | + .session(session) |
| 821 | + .sessionService(sessionService) |
| 822 | + .pluginManager(new PluginManager(ImmutableList.of(mockPlugin))) |
| 823 | + .build(); |
| 824 | + ToolContext toolContext = ToolContext.builder(invocationContext).build(); |
| 825 | + |
| 826 | + Map<String, Object> unused = |
| 827 | + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); |
| 828 | + |
| 829 | + assertThat(callbackCalled.get()).isFalse(); |
| 830 | + } |
| 831 | + |
| 832 | + @Test |
| 833 | + public void call_createWithAgentAndSkipSummarization_defaultsIncludePluginsToFalse() |
| 834 | + throws Exception { |
| 835 | + AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 836 | + Plugin mockPlugin = |
| 837 | + new Plugin() { |
| 838 | + @Override |
| 839 | + public String getName() { |
| 840 | + return "mock_plugin"; |
| 841 | + } |
| 842 | + |
| 843 | + @Override |
| 844 | + public Maybe<Content> beforeRunCallback(InvocationContext invocationContext) { |
| 845 | + callbackCalled.set(true); |
| 846 | + return Maybe.empty(); |
| 847 | + } |
| 848 | + }; |
| 849 | + LlmAgent testAgent = |
| 850 | + createTestAgentBuilder(createTestLlm(LlmResponse.builder().build())) |
| 851 | + .name("agent_name") |
| 852 | + .description("agent description") |
| 853 | + .build(); |
| 854 | + AgentTool agentTool = AgentTool.create(testAgent, /* skipSummarization= */ true); |
| 855 | + Session session = |
| 856 | + sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet(); |
| 857 | + InvocationContext invocationContext = |
| 858 | + InvocationContext.builder() |
| 859 | + .invocationId(InvocationContext.newInvocationContextId()) |
| 860 | + .agent(testAgent) |
| 861 | + .session(session) |
| 862 | + .sessionService(sessionService) |
| 863 | + .pluginManager(new PluginManager(ImmutableList.of(mockPlugin))) |
| 864 | + .build(); |
| 865 | + ToolContext toolContext = ToolContext.builder(invocationContext).build(); |
| 866 | + |
| 867 | + Map<String, Object> unused = |
| 868 | + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); |
| 869 | + |
| 870 | + assertThat(callbackCalled.get()).isFalse(); |
| 871 | + } |
| 872 | + |
707 | 873 | private ToolContext createToolContext(BaseAgent agent) { |
708 | 874 | Session session = |
709 | 875 | sessionService.createSession("test-app", "test-user", null, "test-session").blockingGet(); |
|
0 commit comments