Skip to content
Open
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
50 changes: 50 additions & 0 deletions core/src/main/java/com/google/adk/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import com.google.genai.types.FunctionResponse;
import com.google.genai.types.GenerateContentResponseUsageMetadata;
import com.google.genai.types.GroundingMetadata;
import com.google.genai.types.LiveServerGoAway;
import com.google.genai.types.Transcription;
import com.google.genai.types.VoiceActivity;
import java.time.InstantSource;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -70,6 +72,8 @@ public class Event extends JsonBaseModel {
private @Nullable String modelVersion;
private @Nullable Transcription inputTranscription;
private @Nullable Transcription outputTranscription;
private @Nullable LiveServerGoAway goAway;
private @Nullable VoiceActivity voiceActivity;
private @Nullable Object output;
private @Nullable NodeInfo nodeInfo;
private @Nullable CacheMetadata cacheMetadata;
Expand Down Expand Up @@ -329,6 +333,26 @@ public void setOutputTranscription(@Nullable Transcription outputTranscription)
this.outputTranscription = outputTranscription;
}

/** A Live server warning that the connection will close; does not complete the model turn. */
@JsonProperty("goAway")
public Optional<LiveServerGoAway> goAway() {
return Optional.ofNullable(goAway);
}

public void setGoAway(@Nullable LiveServerGoAway goAway) {
this.goAway = goAway;
}

/** A Live server notification that the user has started or stopped speaking. */
@JsonProperty("voiceActivity")
public Optional<VoiceActivity> voiceActivity() {
return Optional.ofNullable(voiceActivity);
}

public void setVoiceActivity(@Nullable VoiceActivity voiceActivity) {
this.voiceActivity = voiceActivity;
}

/**
* For a workflow node, returns the value handed to successors, distinct from {@link #content()},
* or empty when the event carries no node output. Holds any value Jackson can serialize, such as
Expand Down Expand Up @@ -436,6 +460,8 @@ public final boolean finalResponse() {
}
return functionCalls().isEmpty()
&& functionResponses().isEmpty()
&& goAway().isEmpty()
&& voiceActivity().isEmpty()
&& !partial().orElse(false)
&& !hasTrailingCodeExecutionResult();
}
Expand Down Expand Up @@ -503,6 +529,8 @@ public static class Builder {
private @Nullable String modelVersion;
private @Nullable Transcription inputTranscription;
private @Nullable Transcription outputTranscription;
private @Nullable LiveServerGoAway goAway;
private @Nullable VoiceActivity voiceActivity;
private @Nullable Object output;
private @Nullable NodeInfo nodeInfo;
private @Nullable CacheMetadata cacheMetadata;
Expand Down Expand Up @@ -691,6 +719,20 @@ public Builder outputTranscription(@Nullable Transcription value) {
return this;
}

@CanIgnoreReturnValue
@JsonProperty("goAway")
public Builder goAway(@Nullable LiveServerGoAway value) {
this.goAway = value;
return this;
}

@CanIgnoreReturnValue
@JsonProperty("voiceActivity")
public Builder voiceActivity(@Nullable VoiceActivity value) {
this.voiceActivity = value;
return this;
}

@Experimental
@CanIgnoreReturnValue
@JsonProperty("output")
Expand Down Expand Up @@ -739,6 +781,8 @@ public Event build() {
timestamp().orElseGet(() -> InstantSource.system().instant().toEpochMilli()));
event.setInputTranscription(inputTranscription);
event.setOutputTranscription(outputTranscription);
event.setGoAway(goAway);
event.setVoiceActivity(voiceActivity);
event.setOutput(output);
event.setNodeInfo(nodeInfo);
event.setCacheMetadata(cacheMetadata);
Expand Down Expand Up @@ -779,6 +823,8 @@ public Builder toBuilder() {
.modelVersion(this.modelVersion)
.inputTranscription(this.inputTranscription)
.outputTranscription(this.outputTranscription)
.goAway(this.goAway)
.voiceActivity(this.voiceActivity)
.output(this.output)
.nodeInfo(this.nodeInfo)
.cacheMetadata(this.cacheMetadata)
Expand Down Expand Up @@ -815,6 +861,8 @@ public boolean equals(Object obj) {
&& Objects.equals(modelVersion, other.modelVersion)
&& Objects.equals(inputTranscription, other.inputTranscription)
&& Objects.equals(outputTranscription, other.outputTranscription)
&& Objects.equals(goAway, other.goAway)
&& Objects.equals(voiceActivity, other.voiceActivity)
&& Objects.equals(output, other.output)
&& Objects.equals(nodeInfo, other.nodeInfo)
&& Objects.equals(cacheMetadata, other.cacheMetadata);
Expand Down Expand Up @@ -849,6 +897,8 @@ public int hashCode() {
modelVersion,
inputTranscription,
outputTranscription,
goAway,
voiceActivity,
output,
nodeInfo,
cacheMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ private Flowable<Event> buildPostprocessingEvents(
|| updatedResponse.usageMetadata().isEmpty())
&& updatedResponse.inputTranscription().isEmpty()
&& updatedResponse.outputTranscription().isEmpty()
&& updatedResponse.goAway().isEmpty()
&& updatedResponse.voiceActivity().isEmpty()
&& updatedResponse.groundingMetadata().isEmpty()) {
return processorEvents;
}
Expand Down Expand Up @@ -931,6 +933,8 @@ private Event buildModelResponseEvent(
.modelVersion(llmResponse.modelVersion().orElse(null))
.inputTranscription(llmResponse.inputTranscription().orElse(null))
.outputTranscription(llmResponse.outputTranscription().orElse(null))
.goAway(llmResponse.goAway().orElse(null))
.voiceActivity(llmResponse.voiceActivity().orElse(null))
.cacheMetadata(llmResponse.cacheMetadata().orElse(null));

Event event = eventBuilder.build();
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/com/google/adk/models/GeminiLlmConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@ static Observable<LlmResponse> convertToServerResponse(
handled.set(true);
});

message
.goAway()
.ifPresent(
goAway -> {
emitter.onNext(LlmResponse.builder().goAway(goAway).build());
handled.set(true);
});
message
.voiceActivity()
.ifPresent(
voiceActivity -> {
emitter.onNext(LlmResponse.builder().voiceActivity(voiceActivity).build());
handled.set(true);
});
message
.voiceActivityDetectionSignal()
.ifPresent(
signal -> {
// This lower-level VAD signal has no ADK event representation. Recognize it
// without treating it as an error or as the end of the model turn.
logger.debug("Received voice activity detection signal: {}", signal);
handled.set(true);
});

if (!handled.get()) {
logger.warn("Received unknown or empty server message.");
emitter.onNext(createUnknownMessageResponse());
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/com/google/adk/models/LlmResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import com.google.genai.types.GenerateContentResponsePromptFeedback;
import com.google.genai.types.GenerateContentResponseUsageMetadata;
import com.google.genai.types.GroundingMetadata;
import com.google.genai.types.LiveServerGoAway;
import com.google.genai.types.Transcription;
import com.google.genai.types.VoiceActivity;
import java.util.List;
import java.util.Optional;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -130,6 +132,14 @@ public abstract class LlmResponse extends JsonBaseModel {
@JsonProperty("outputTranscription")
public abstract Optional<Transcription> outputTranscription();

/** A Live server warning that the connection will close; does not complete the model turn. */
@JsonProperty("goAway")
public abstract Optional<LiveServerGoAway> goAway();

/** A Live server notification that the user has started or stopped speaking. */
@JsonProperty("voiceActivity")
public abstract Optional<VoiceActivity> voiceActivity();

/** Context cache state for this response; set only when context caching is enabled. */
@JsonProperty("cacheMetadata")
public abstract Optional<CacheMetadata> cacheMetadata();
Expand Down Expand Up @@ -189,6 +199,12 @@ public abstract Builder usageMetadata(
@JsonProperty("outputTranscription")
public abstract Builder outputTranscription(@Nullable Transcription outputTranscription);

@JsonProperty("goAway")
public abstract Builder goAway(@Nullable LiveServerGoAway goAway);

@JsonProperty("voiceActivity")
public abstract Builder voiceActivity(@Nullable VoiceActivity voiceActivity);

@JsonProperty("cacheMetadata")
public abstract Builder cacheMetadata(@Nullable CacheMetadata cacheMetadata);

Expand Down
65 changes: 65 additions & 0 deletions core/src/test/java/com/google/adk/events/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import com.google.genai.types.FinishReason;
import com.google.genai.types.FunctionCall;
import com.google.genai.types.GenerateContentResponseUsageMetadata;
import com.google.genai.types.LiveServerGoAway;
import com.google.genai.types.Part;
import com.google.genai.types.Transcription;
import com.google.genai.types.VoiceActivity;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -111,6 +114,68 @@ public void event_builder_works() {
assertThat(EVENT.modelVersion()).hasValue("model_version");
}

@Test
public void event_liveControlFields_roundTripAndCopy_preservePayloads() {
LiveServerGoAway goAway = LiveServerGoAway.builder().timeLeft(Duration.ofMillis(30500)).build();
VoiceActivity voiceActivity =
VoiceActivity.builder()
.voiceActivityType("ACTIVITY_END")
.audioOffset(Duration.ofMillis(1250))
.build();
Event event = EVENT.toBuilder().goAway(goAway).voiceActivity(voiceActivity).build();

Event restored = Event.fromJson(event.toJson());
assertThat(restored).isEqualTo(event);
assertThat(restored.goAway()).hasValue(goAway);
assertThat(restored.voiceActivity()).hasValue(voiceActivity);
Event copied = event.toBuilder().build();
assertThat(copied).isEqualTo(event);
assertThat(copied.hashCode()).isEqualTo(event.hashCode());
assertThat(copied.toBuilder().goAway(null).voiceActivity(null).build()).isEqualTo(EVENT);
}

@Test
public void event_liveControlFields_defaultAndNull_areAbsent() {
for (String json : ImmutableList.of("{}", "{\"goAway\":null,\"voiceActivity\":null}")) {
Event event = Event.fromJson(json);
assertThat(event.goAway()).isEmpty();
assertThat(event.voiceActivity()).isEmpty();
assertThat(event.toJson()).doesNotContain("\"goAway\"");
assertThat(event.toJson()).doesNotContain("\"voiceActivity\"");

event.setGoAway(LiveServerGoAway.builder().build());
event.setVoiceActivity(VoiceActivity.builder().build());
assertThat(event.goAway()).isPresent();
assertThat(event.voiceActivity()).isPresent();
event.setGoAway(null);
event.setVoiceActivity(null);
assertThat(event.goAway()).isEmpty();
assertThat(event.voiceActivity()).isEmpty();
}
}

@Test
public void event_equals_distinguishesLiveControlFields() {
assertThat(EVENT.toBuilder().goAway(LiveServerGoAway.builder().build()).build())
.isNotEqualTo(EVENT);
assertThat(EVENT.toBuilder().voiceActivity(VoiceActivity.builder().build()).build())
.isNotEqualTo(EVENT);
}

@Test
public void event_finalResponse_liveNotifications_areNotFinalAnswers() {
assertThat(Event.builder().goAway(LiveServerGoAway.builder().build()).build().finalResponse())
.isFalse();
for (String type : ImmutableList.of("ACTIVITY_START", "ACTIVITY_END")) {
assertThat(
Event.builder()
.voiceActivity(VoiceActivity.builder().voiceActivityType(type).build())
.build()
.finalResponse())
.isFalse();
}
}

@Test
public void event_builder_fills_default_actions() {
Event event =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.GenerateContentResponseUsageMetadata;
import com.google.genai.types.GroundingMetadata;
import com.google.genai.types.LiveServerGoAway;
import com.google.genai.types.Part;
import com.google.genai.types.Transcription;
import com.google.genai.types.VoiceActivity;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
Expand Down Expand Up @@ -1187,6 +1189,37 @@ public void postprocess_onlyInputTranscription_returnsEvent() {
assertThat(event.outputTranscription()).isEmpty();
}

@Test
public void postprocess_onlyLiveControlFields_returnsEventsWithoutContent() {
for (LlmResponse response :
ImmutableList.of(
LlmResponse.builder().goAway(LiveServerGoAway.builder().build()).build(),
LlmResponse.builder()
.voiceActivity(VoiceActivity.builder().voiceActivityType("ACTIVITY_START").build())
.build())) {
InvocationContext context = createInvocationContext(createTestAgent(createTestLlm(response)));
Event baseEvent =
Event.builder()
.invocationId(context.invocationId())
.author(context.agent().name())
.build();

List<Event> events =
createBaseLlmFlowWithoutProcessors()
.postprocess(
context, baseEvent, LlmRequest.builder().build(), response, Context.current())
.toList()
.blockingGet();

Event event = getOnlyElement(events);
assertThat(event.goAway()).isEqualTo(response.goAway());
assertThat(event.voiceActivity()).isEqualTo(response.voiceActivity());
assertThat(event.content()).isEmpty();
assertThat(event.errorCode()).isEmpty();
assertThat(event.finalResponse()).isFalse();
}
}

@Test
public void postprocess_onlyOutputTranscription_returnsEvent() {
Transcription outputTranscription =
Expand Down
Loading