diff --git a/core/src/main/java/com/google/adk/models/LlmRequest.java b/core/src/main/java/com/google/adk/models/LlmRequest.java index e35969147..1a45c3a95 100644 --- a/core/src/main/java/com/google/adk/models/LlmRequest.java +++ b/core/src/main/java/com/google/adk/models/LlmRequest.java @@ -17,7 +17,6 @@ package com.google.adk.models; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableMap.toImmutableMap; @@ -172,29 +171,33 @@ public final Builder appendInstructions(List instructions) { return liveConnectConfig(liveCfg.toBuilder().systemInstruction(newLiveSi).build()); } + // In this particular case we can keep the Optional as a type of a + // parameter, since the function is private and used in only one place while + // the Optional type plays nicely with flatMaps in the code (if we had a + // nullable here, we'd wrap it in the Optional anyway) private Content addInstructions( - Optional currentSystemInstruction, List additionalInstructions) { + @SuppressWarnings("checkstyle:IllegalType") Optional currentSystemInstruction, + List additionalInstructions) { checkArgument( - currentSystemInstruction.isEmpty() - || currentSystemInstruction.get().parts().map(parts -> parts.size()).orElse(0) <= 1, + currentSystemInstruction.flatMap(Content::parts).map(parts -> parts.size()).orElse(0) + <= 1, "At most one instruction is supported."); // Either append to the existing instruction, or create a new one. String instructions = String.join("\n\n", additionalInstructions); - Optional part = - currentSystemInstruction - .flatMap(Content::parts) - .flatMap(parts -> parts.stream().findFirst()); - if (part.isEmpty() || part.get().text().isEmpty()) { - part = Optional.of(Part.fromText(instructions)); - } else { - part = Optional.of(Part.fromText(part.get().text().get() + "\n\n" + instructions)); - } - checkState(part.isPresent(), "Failed to create instruction."); + Part part = + Part.fromText( + currentSystemInstruction + .flatMap(Content::parts) + .flatMap(parts -> parts.stream().findFirst()) + .flatMap(Part::text) + .map(text -> text + "\n\n" + instructions) + .orElse(instructions)); String role = currentSystemInstruction.flatMap(Content::role).orElse("user"); - return Content.builder().parts(part.get()).role(role).build(); + + return Content.builder().parts(part).role(role).build(); } @CanIgnoreReturnValue