|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.flows.llmflows; |
| 18 | + |
| 19 | +import static com.google.adk.testing.TestUtils.createInvocationContext; |
| 20 | +import static com.google.adk.testing.TestUtils.createLlmResponse; |
| 21 | +import static com.google.adk.testing.TestUtils.createTestAgentBuilder; |
| 22 | +import static com.google.adk.testing.TestUtils.createTestLlm; |
| 23 | +import static com.google.adk.testing.TestUtils.simplifyEvents; |
| 24 | +import static com.google.common.truth.Truth.assertThat; |
| 25 | + |
| 26 | +import com.google.adk.agents.InvocationContext; |
| 27 | +import com.google.adk.agents.LlmAgent; |
| 28 | +import com.google.adk.events.Event; |
| 29 | +import com.google.adk.runner.InMemoryRunner; |
| 30 | +import com.google.adk.runner.Runner; |
| 31 | +import com.google.adk.sessions.Session; |
| 32 | +import com.google.adk.tools.BaseTool; |
| 33 | +import com.google.adk.tools.ToolContext; |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | +import com.google.common.collect.ImmutableMap; |
| 36 | +import com.google.genai.types.Content; |
| 37 | +import com.google.genai.types.FunctionDeclaration; |
| 38 | +import com.google.genai.types.Part; |
| 39 | +import com.google.genai.types.Schema; |
| 40 | +import io.reactivex.rxjava3.core.Flowable; |
| 41 | +import io.reactivex.rxjava3.core.Single; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Map; |
| 45 | +import java.util.Optional; |
| 46 | +import org.junit.Test; |
| 47 | +import org.junit.runner.RunWith; |
| 48 | +import org.junit.runners.JUnit4; |
| 49 | + |
| 50 | +@RunWith(JUnit4.class) |
| 51 | +public final class EndInvocationActionTest { |
| 52 | + |
| 53 | + private static class EndInvocationTool extends BaseTool { |
| 54 | + public EndInvocationTool() { |
| 55 | + super("end_invocation", "Ends the current invocation."); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Optional<FunctionDeclaration> declaration() { |
| 60 | + return Optional.of( |
| 61 | + FunctionDeclaration.builder() |
| 62 | + .name(name()) |
| 63 | + .description(description()) |
| 64 | + .parameters(Schema.builder().type("OBJECT").build()) // No parameters needed |
| 65 | + .build()); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContext toolContext) { |
| 70 | + toolContext.setActions(toolContext.actions().toBuilder().endInvocation(true).build()); |
| 71 | + return Single.just(ImmutableMap.of()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void endInvocationTool_stopsFlow() { |
| 77 | + Content endInvocationCallContent = |
| 78 | + Content.fromParts(Part.fromFunctionCall("end_invocation", ImmutableMap.of())); |
| 79 | + Content response1 = Content.fromParts(Part.fromText("response1")); |
| 80 | + Content response2 = Content.fromParts(Part.fromText("response2")); |
| 81 | + |
| 82 | + var testLlm = |
| 83 | + createTestLlm( |
| 84 | + Flowable.just(createLlmResponse(endInvocationCallContent)), |
| 85 | + Flowable.just(createLlmResponse(response1)), |
| 86 | + Flowable.just(createLlmResponse(response2))); |
| 87 | + |
| 88 | + LlmAgent rootAgent = |
| 89 | + createTestAgentBuilder(testLlm) |
| 90 | + .name("root_agent") |
| 91 | + .tools(ImmutableList.of(new EndInvocationTool())) |
| 92 | + .build(); |
| 93 | + InvocationContext invocationContext = createInvocationContext(rootAgent); |
| 94 | + |
| 95 | + Runner runner = getRunnerAndCreateSession(rootAgent, invocationContext.session()); |
| 96 | + |
| 97 | + List<Event> actualEvents = new ArrayList<>(); |
| 98 | + runRunner(runner, invocationContext, actualEvents); |
| 99 | + |
| 100 | + assertThat(simplifyEvents(actualEvents)) |
| 101 | + .containsExactly( |
| 102 | + "root_agent: FunctionCall(name=end_invocation, args={})", |
| 103 | + "root_agent: FunctionResponse(name=end_invocation, response={})") |
| 104 | + .inOrder(); |
| 105 | + } |
| 106 | + |
| 107 | + private Runner getRunnerAndCreateSession(LlmAgent agent, Session session) { |
| 108 | + Runner runner = new InMemoryRunner(agent, session.appName()); |
| 109 | + |
| 110 | + var unused = |
| 111 | + runner |
| 112 | + .sessionService() |
| 113 | + .createSession(session.appName(), session.userId(), session.state(), session.id()) |
| 114 | + .blockingGet(); |
| 115 | + |
| 116 | + return runner; |
| 117 | + } |
| 118 | + |
| 119 | + private void runRunner( |
| 120 | + Runner runner, InvocationContext invocationContext, List<Event> actualEvents) { |
| 121 | + Session session = invocationContext.session(); |
| 122 | + runner |
| 123 | + .runAsync(session.userId(), session.id(), invocationContext.userContent().orElse(null)) |
| 124 | + .blockingForEach(actualEvents::add); |
| 125 | + } |
| 126 | +} |
0 commit comments