Skip to content

Commit 2f5769d

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: GeminiLlmConnection: Unwrap List from Optional
PiperOrigin-RevId: 860227853
1 parent 0dd5278 commit 2f5769d

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

core/src/main/java/com/google/adk/models/GeminiLlmConnection.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void handleReceiveError(Throwable throwable) {
184184
if (closed.compareAndSet(false, true)) {
185185
logger.error("Error during WebSocket receive operation", throwable);
186186
responseProcessor.onError(throwable);
187-
sessionFuture.thenAccept(this::closeSessionIgnoringErrors).exceptionally(err -> null);
187+
sessionFuture.thenAccept(this::closeSessionIgnoringErrors).exceptionally(unusedError -> null);
188188
}
189189
}
190190

@@ -198,26 +198,22 @@ public Completable sendHistory(List<Content> history) {
198198
public Completable sendContent(Content content) {
199199
Objects.requireNonNull(content, "content cannot be null");
200200

201-
Optional<List<FunctionResponse>> functionResponses = extractFunctionResponses(content);
202-
203-
if (functionResponses.isPresent()) {
204-
return sendToolResponseInternal(
205-
LiveSendToolResponseParameters.builder()
206-
.functionResponses(functionResponses.get())
207-
.build());
208-
} else {
201+
List<FunctionResponse> functionResponses = extractFunctionResponses(content);
202+
if (functionResponses.isEmpty()) {
209203
return sendClientContentInternal(
210204
LiveSendClientContentParameters.builder()
211205
.turns(ImmutableList.of(content))
212206
.turnComplete(true)
213207
.build());
214208
}
209+
return sendToolResponseInternal(
210+
LiveSendToolResponseParameters.builder().functionResponses(functionResponses).build());
215211
}
216212

217213
/** Extracts FunctionResponse parts from a Content object if all parts are FunctionResponses. */
218-
private Optional<List<FunctionResponse>> extractFunctionResponses(Content content) {
214+
private List<FunctionResponse> extractFunctionResponses(Content content) {
219215
if (content.parts().isEmpty() || content.parts().get().isEmpty()) {
220-
return Optional.empty();
216+
return ImmutableList.of();
221217
}
222218

223219
ImmutableList<FunctionResponse> responses =
@@ -226,12 +222,8 @@ private Optional<List<FunctionResponse>> extractFunctionResponses(Content conten
226222
.flatMap(Optional::stream)
227223
.collect(toImmutableList());
228224

229-
// Ensure *all* parts were function responses
230-
if (responses.size() == content.parts().get().size()) {
231-
return Optional.of(responses);
232-
} else {
233-
return Optional.empty();
234-
}
225+
// Ensure *all* parts were function responses.
226+
return (responses.size() == content.parts().get().size()) ? responses : ImmutableList.of();
235227
}
236228

237229
@Override
@@ -283,7 +275,9 @@ private void closeInternal(Throwable throwable) {
283275
}
284276

285277
if (sessionFuture.isDone()) {
286-
sessionFuture.thenAccept(this::closeSessionIgnoringErrors).exceptionally(err -> null);
278+
sessionFuture
279+
.thenAccept(this::closeSessionIgnoringErrors)
280+
.exceptionally(unusedError -> null);
287281
} else {
288282
sessionFuture.cancel(false);
289283
}

0 commit comments

Comments
 (0)