|
| 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.runner; |
| 18 | + |
| 19 | +import static com.google.adk.testing.TestUtils.createLlmResponse; |
| 20 | +import static com.google.adk.testing.TestUtils.createTestAgentBuilder; |
| 21 | +import static com.google.adk.testing.TestUtils.createTestLlm; |
| 22 | +import static com.google.common.truth.Truth.assertThat; |
| 23 | + |
| 24 | +import com.google.adk.agents.InvocationContext; |
| 25 | +import com.google.adk.agents.LiveRequestQueue; |
| 26 | +import com.google.adk.agents.LlmAgent; |
| 27 | +import com.google.adk.agents.RunConfig; |
| 28 | +import com.google.adk.sessions.Session; |
| 29 | +import com.google.adk.testing.TestLlm; |
| 30 | +import com.google.common.collect.ImmutableList; |
| 31 | +import com.google.genai.types.AudioTranscriptionConfig; |
| 32 | +import com.google.genai.types.Content; |
| 33 | +import com.google.genai.types.Modality; |
| 34 | +import com.google.genai.types.Part; |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.util.Optional; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.junit.runners.JUnit4; |
| 40 | + |
| 41 | +@RunWith(JUnit4.class) |
| 42 | +public final class InputAudioTranscriptionTest { |
| 43 | + |
| 44 | + private Content createContent(String text) { |
| 45 | + return Content.builder().parts(Part.builder().text(text).build()).build(); |
| 46 | + } |
| 47 | + |
| 48 | + private InvocationContext invokeNewInvocationContextForLive( |
| 49 | + Runner runner, Session session, LiveRequestQueue liveRequestQueue, RunConfig runConfig) |
| 50 | + throws Exception { |
| 51 | + Method method = |
| 52 | + Runner.class.getDeclaredMethod( |
| 53 | + "newInvocationContextForLive", Session.class, Optional.class, RunConfig.class); |
| 54 | + method.setAccessible(true); |
| 55 | + return (InvocationContext) |
| 56 | + method.invoke(runner, session, Optional.of(liveRequestQueue), runConfig); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void newInvocationContextForLive_multiAgent_autoConfiguresInputAudioTranscription() |
| 61 | + throws Exception { |
| 62 | + TestLlm testLlm = createTestLlm(createLlmResponse(createContent("response"))); |
| 63 | + LlmAgent subAgent = createTestAgentBuilder(testLlm).name("sub_agent").build(); |
| 64 | + LlmAgent rootAgent = |
| 65 | + createTestAgentBuilder(testLlm) |
| 66 | + .name("root_agent") |
| 67 | + .subAgents(ImmutableList.of(subAgent)) |
| 68 | + .build(); |
| 69 | + |
| 70 | + Runner runner = new InMemoryRunner(rootAgent, "test", ImmutableList.of()); |
| 71 | + Session session = runner.sessionService().createSession("test", "user").blockingGet(); |
| 72 | + |
| 73 | + RunConfig initialConfig = |
| 74 | + RunConfig.builder() |
| 75 | + .setResponseModalities(ImmutableList.of(new Modality(Modality.Known.AUDIO))) |
| 76 | + .setStreamingMode(RunConfig.StreamingMode.BIDI) |
| 77 | + .build(); |
| 78 | + |
| 79 | + assertThat(initialConfig.inputAudioTranscription()).isNull(); |
| 80 | + |
| 81 | + LiveRequestQueue liveQueue = new LiveRequestQueue(); |
| 82 | + InvocationContext context = |
| 83 | + invokeNewInvocationContextForLive(runner, session, liveQueue, initialConfig); |
| 84 | + |
| 85 | + assertThat(context.runConfig().inputAudioTranscription()).isNotNull(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void newInvocationContextForLive_explicitConfig_preservesUserInputAudioTranscription() |
| 90 | + throws Exception { |
| 91 | + TestLlm testLlm = createTestLlm(createLlmResponse(createContent("response"))); |
| 92 | + LlmAgent subAgent = createTestAgentBuilder(testLlm).name("sub_agent").build(); |
| 93 | + LlmAgent rootAgent = |
| 94 | + createTestAgentBuilder(testLlm) |
| 95 | + .name("root_agent") |
| 96 | + .subAgents(ImmutableList.of(subAgent)) |
| 97 | + .build(); |
| 98 | + |
| 99 | + Runner runner = new InMemoryRunner(rootAgent, "test", ImmutableList.of()); |
| 100 | + Session session = runner.sessionService().createSession("test", "user").blockingGet(); |
| 101 | + |
| 102 | + AudioTranscriptionConfig userConfig = AudioTranscriptionConfig.builder().build(); |
| 103 | + RunConfig configWithUserSetting = |
| 104 | + RunConfig.builder() |
| 105 | + .setResponseModalities(ImmutableList.of(new Modality(Modality.Known.AUDIO))) |
| 106 | + .setStreamingMode(RunConfig.StreamingMode.BIDI) |
| 107 | + .setInputAudioTranscription(userConfig) |
| 108 | + .build(); |
| 109 | + |
| 110 | + LiveRequestQueue liveQueue = new LiveRequestQueue(); |
| 111 | + InvocationContext context = |
| 112 | + invokeNewInvocationContextForLive(runner, session, liveQueue, configWithUserSetting); |
| 113 | + |
| 114 | + assertThat(context.runConfig().inputAudioTranscription()).isSameInstanceAs(userConfig); |
| 115 | + } |
| 116 | +} |
0 commit comments