Repository navigation
fix(live): handle goAway and voice activity notifications #1878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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 | ||
|
|
@@ -436,6 +460,8 @@ public final boolean finalResponse() { | |
| } | ||
| return functionCalls().isEmpty() | ||
| && functionResponses().isEmpty() | ||
| && goAway().isEmpty() | ||
| && voiceActivity().isEmpty() | ||
|
Comment on lines
+463
to
+464
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DESIGN (MEDIUM): Please drop this change to |
||
| && !partial().orElse(false) | ||
| && !hasTrailingCodeExecutionResult(); | ||
| } | ||
|
|
@@ -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; | ||
|
|
@@ -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") | ||
|
|
@@ -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); | ||
|
|
@@ -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) | ||
|
|
@@ -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); | ||
|
|
@@ -849,6 +897,8 @@ public int hashCode() { | |
| modelVersion, | ||
| inputTranscription, | ||
| outputTranscription, | ||
| goAway, | ||
| voiceActivity, | ||
| output, | ||
| nodeInfo, | ||
| cacheMetadata, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
Comment on lines
+201
to
+202
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CLEANUP (LOW): A one-line comment is enough here, for example: |
||
| 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()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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(); | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+118
to
+178
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CLEANUP (LOW): Several new tests loop over cases or check multiple behaviors at once. For instance, |
||
| @Test | ||
| public void event_builder_fills_default_actions() { | ||
| Event event = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DESIGN (HIGH): This exposes go-away as a public
Eventfield that callers receive and the session stores. ADK Python consumesgo_awayin the live flow and reconnects (v1, main). ADK Kotlin does the same and has nogoAwayonEvent. Once Java adds reconnection, this field will go unused or need removal.In my live run, the server closed the socket 30 s after
goAway, andrunLiveneither completed nor failed. For now, this event is the caller's only warning.Handling
goAwayinsideBaseLlmFlowis better alternative here