Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion core/src/main/java/com/google/adk/agents/LiveRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public abstract class LiveRequest extends JsonBaseModel {
@JsonProperty("content")
public abstract Optional<Content> content();

/**
* Returns whether {@link #content()} is a partial update that leaves the current turn open.
*
* <p>Defaults to false, so content sends complete the turn unless marked as partial.
*
* @return An optional boolean indicating whether the content is a partial turn update.
*/
@JsonProperty("partial")
public abstract Optional<Boolean> partial();

/**
* Returns the blob of the request.
*
Expand Down Expand Up @@ -83,6 +93,9 @@ static LiveRequest.Builder jacksonBuilder() {
@JsonProperty("content")
public abstract Builder content(@Nullable Content content);

@JsonProperty("partial")
public abstract Builder partial(@Nullable Boolean partial);

@JsonProperty("blob")
public abstract Builder blob(@Nullable Blob blob);

Expand All @@ -103,7 +116,7 @@ public final LiveRequest build() {
}

public static Builder builder() {
return new AutoValue_LiveRequest.Builder().close(false);
return new AutoValue_LiveRequest.Builder().close(false).partial(false);
}

public abstract Builder toBuilder();
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/com/google/adk/agents/LiveRequestQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ public void close() {
}

public void content(Content content) {
processor.onNext(LiveRequest.builder().content(content).build());
content(content, false);
}

/**
* Sends content to the model, optionally as a partial update that leaves the current turn open.
*
* @param content the content to send
* @param partial whether this content is a partial update that does not complete the current turn
*/
public void content(Content content, boolean partial) {
processor.onNext(LiveRequest.builder().content(content).partial(partial).build());
}

public void realtime(Blob blob) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ public Flowable<Event> runLive(InvocationContext invocationContext) {
.concatMapCompletable(
request -> {
if (request.content().isPresent()) {
return connection.sendContent(request.content().get());
return connection.sendContent(
request.content().get(), request.partial().orElse(false));
} else if (request.blob().isPresent()) {
return connection.sendRealtime(request.blob().get());
}
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/com/google/adk/models/BaseLlmConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public interface BaseLlmConnection {
*/
Completable sendContent(Content content);

/**
* Sends user content to the model, optionally leaving the current turn open for more content.
*
* <p>Implementations that do not support incomplete turns retain the existing behavior. Function
* responses use the existing tool-response path, regardless of {@code partial}.
*
* @param content the content to send
* @param partial whether this content is a partial update that does not complete the current turn
*/
default Completable sendContent(Content content, boolean partial) {
return sendContent(content);
}

/**
* Sends a chunk of audio or a frame of video to the model in realtime.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,19 @@ public Completable sendHistory(List<Content> history) {

@Override
public Completable sendContent(Content content) {
return sendContent(content, false);
}

@Override
public Completable sendContent(Content content, boolean partial) {
Objects.requireNonNull(content, "content cannot be null");

List<FunctionResponse> functionResponses = extractFunctionResponses(content);
if (functionResponses.isEmpty()) {
return sendClientContentInternal(
LiveSendClientContentParameters.builder()
.turns(ImmutableList.of(content))
.turnComplete(true)
.turnComplete(!partial)
.build());
}
return sendToolResponseInternal(
Expand Down
123 changes: 123 additions & 0 deletions core/src/test/java/com/google/adk/agents/LiveRequestQueueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.agents;

import static com.google.common.truth.Truth.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.adk.JsonBaseModel;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.subscribers.TestSubscriber;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class LiveRequestQueueTest {

private static final Content CONTENT = Content.fromParts(Part.fromText("context"));

@Test
public void content_defaultsToCompletingTurn() {
LiveRequestQueue queue = new LiveRequestQueue();
TestSubscriber<LiveRequest> subscriber = queue.get().test();

queue.content(CONTENT);

subscriber.assertValueCount(1);
LiveRequest request = subscriber.values().get(0);
assertThat(request.content()).hasValue(CONTENT);
assertThat(request.partial()).hasValue(false);
}

@Test
public void content_withPartialTrue_preservesValue() {
LiveRequestQueue queue = new LiveRequestQueue();
TestSubscriber<LiveRequest> subscriber = queue.get().test();

queue.content(CONTENT, true);

subscriber.assertValueCount(1);
LiveRequest request = subscriber.values().get(0);
assertThat(request.content()).hasValue(CONTENT);
assertThat(request.partial()).hasValue(true);
}

@Test
public void content_withPartialFalse_preservesValue() {
LiveRequestQueue queue = new LiveRequestQueue();
TestSubscriber<LiveRequest> subscriber = queue.get().test();

queue.content(CONTENT, false);

subscriber.assertValueCount(1);
LiveRequest request = subscriber.values().get(0);
assertThat(request.content()).hasValue(CONTENT);
assertThat(request.partial()).hasValue(false);
}

@Test
public void liveRequest_builderDefaultsToNonPartial() {
LiveRequest request = LiveRequest.builder().content(CONTENT).build();

assertThat(request.partial()).hasValue(false);
}

@Test
public void liveRequest_jsonRoundTrip_preservesPartialTrue() {
LiveRequest request = LiveRequest.builder().content(CONTENT).partial(true).build();

JsonNode json = JsonBaseModel.getMapper().valueToTree(request);
LiveRequest restored = LiveRequest.fromJsonString(request.toJson());

assertThat(json.get("partial").booleanValue()).isTrue();
assertThat(json.has("turnComplete")).isFalse();
assertThat(restored.content()).hasValue(CONTENT);
assertThat(restored.partial()).hasValue(true);
}

@Test
public void liveRequest_jsonRoundTrip_preservesPartialFalse() {
LiveRequest request = LiveRequest.builder().content(CONTENT).partial(false).build();

JsonNode json = JsonBaseModel.getMapper().valueToTree(request);
LiveRequest restored = LiveRequest.fromJsonString(request.toJson());

assertThat(json.get("partial").booleanValue()).isFalse();
assertThat(json.has("turnComplete")).isFalse();
assertThat(restored.content()).hasValue(CONTENT);
assertThat(restored.partial()).hasValue(false);
}

@Test
public void liveRequest_jsonWithoutPartial_defaultsToNonPartial() {
LiveRequest request =
LiveRequest.fromJsonString(
"{\"content\":{\"role\":\"user\",\"parts\":[{\"text\":\"context\"}]}}");

assertThat(request.content()).hasValue(CONTENT);
assertThat(request.partial()).hasValue(false);
}

@Test
public void liveRequest_toBuilder_preservesPartial() {
LiveRequest request = LiveRequest.builder().content(CONTENT).partial(true).build();

assertThat(request.toBuilder().build().partial()).hasValue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.adk.agents.Callbacks;
import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.LiveRequest;
import com.google.adk.agents.LiveRequestQueue;
import com.google.adk.agents.LlmAgent;
import com.google.adk.agents.ReadonlyContext;
import com.google.adk.agents.RunConfig;
import com.google.adk.events.Event;
import com.google.adk.flows.llmflows.RequestProcessor.RequestProcessingResult;
import com.google.adk.flows.llmflows.ResponseProcessor.ResponseProcessingResult;
import com.google.adk.models.BaseLlm;
import com.google.adk.models.BaseLlmConnection;
import com.google.adk.models.CacheMetadata;
import com.google.adk.models.LlmCallsLimitExceededException;
import com.google.adk.models.LlmRequest;
Expand Down Expand Up @@ -76,11 +85,54 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.InOrder;

/** Unit tests for {@link BaseLlmFlow}. */
@RunWith(JUnit4.class)
public final class BaseLlmFlowTest {

@Test
public void runLive_contentForwardsPartialFlagsInOrder() {
Content firstContent = Content.fromParts(Part.fromText("first context"));
Content secondContent = Content.fromParts(Part.fromText("second context"));
Content finalContent = Content.fromParts(Part.fromText("final context"));
Content defaultContent = Content.fromParts(Part.fromText("complete input"));
Content unsetContent = Content.fromParts(Part.fromText("unset partial"));
BaseLlm llm = mock(BaseLlm.class);
BaseLlmConnection connection = mock(BaseLlmConnection.class);
when(llm.model()).thenReturn("test-model");
when(llm.connect(any())).thenReturn(connection);
when(connection.sendContent(any(), anyBoolean())).thenReturn(Completable.complete());
when(connection.receive()).thenReturn(Flowable.never());
LiveRequestQueue queue = new LiveRequestQueue();
InvocationContext context =
createInvocationContext(createTestAgentBuilder(llm).executor(Runnable::run).build())
.toBuilder()
.liveRequestQueue(queue)
.build();
TestSubscriber<Event> subscriber = createBaseLlmFlowWithoutProcessors().runLive(context).test();

try {
subscriber.assertNoErrors();
queue.content(firstContent, true);
queue.content(secondContent, true);
queue.content(finalContent, false);
queue.content(defaultContent);
queue.send(LiveRequest.builder().content(unsetContent).partial(null).build());

InOrder order = inOrder(connection);
order.verify(connection).sendContent(firstContent, true);
order.verify(connection).sendContent(secondContent, true);
order.verify(connection).sendContent(finalContent, false);
order.verify(connection).sendContent(defaultContent, false);
order.verify(connection).sendContent(unsetContent, false);
subscriber.assertNoErrors();
} finally {
queue.close();
subscriber.cancel();
}
}

@Test
public void run_singleTextResponse_returnsSingleEvent() {
Content content = Content.fromParts(Part.fromText("LLM response"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.models;

import static com.google.common.truth.Truth.assertThat;

import com.google.genai.types.Blob;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class BaseLlmConnectionTest {

@Test
public void sendContent_withPartialFlag_delegatesToLegacyImplementation() {
LegacyConnection connection = new LegacyConnection();
Content content = Content.fromParts(Part.fromText("context"));

connection.sendContent(content, true).test().assertComplete().assertNoErrors();
connection.sendContent(content, false).test().assertComplete().assertNoErrors();

assertThat(connection.sentContent).containsExactly(content, content).inOrder();
}

/** Implements only the original connection API, not the new two-argument overload. */
private static final class LegacyConnection implements BaseLlmConnection {
final List<Content> sentContent = new ArrayList<>();

@Override
public Completable sendHistory(List<Content> history) {
return Completable.complete();
}

@Override
public Completable sendContent(Content content) {
return Completable.fromAction(() -> sentContent.add(content));
}

@Override
public Completable sendRealtime(Blob blob) {
return Completable.complete();
}

@Override
public Flowable<LlmResponse> receive() {
return Flowable.empty();
}

@Override
public void close() {}

@Override
public void close(Throwable throwable) {}
}
}
Loading
Loading