1717package com .google .adk .agents ;
1818
1919import static com .google .common .collect .ImmutableList .toImmutableList ;
20- import static java .util .Objects .requireNonNullElse ;
2120import static java .util .stream .Collectors .joining ;
2221
2322import com .fasterxml .jackson .core .JsonProcessingException ;
@@ -104,12 +103,12 @@ public enum IncludeContents {
104103 private final Optional <Integer > maxSteps ;
105104 private final boolean disallowTransferToParent ;
106105 private final boolean disallowTransferToPeers ;
107- private final ImmutableList < ? extends BeforeModelCallback > beforeModelCallback ;
108- private final ImmutableList < ? extends AfterModelCallback > afterModelCallback ;
109- private final ImmutableList < ? extends OnModelErrorCallback > onModelErrorCallback ;
110- private final ImmutableList < ? extends BeforeToolCallback > beforeToolCallback ;
111- private final ImmutableList < ? extends AfterToolCallback > afterToolCallback ;
112- private final ImmutableList < ? extends OnToolErrorCallback > onToolErrorCallback ;
106+ private final Optional < List < ? extends BeforeModelCallback > > beforeModelCallback ;
107+ private final Optional < List < ? extends AfterModelCallback > > afterModelCallback ;
108+ private final Optional < List < ? extends OnModelErrorCallback > > onModelErrorCallback ;
109+ private final Optional < List < ? extends BeforeToolCallback > > beforeToolCallback ;
110+ private final Optional < List < ? extends AfterToolCallback > > afterToolCallback ;
111+ private final Optional < List < ? extends OnToolErrorCallback > > onToolErrorCallback ;
113112 private final Optional <Schema > inputSchema ;
114113 private final Optional <Schema > outputSchema ;
115114 private final Optional <Executor > executor ;
@@ -127,28 +126,29 @@ protected LlmAgent(Builder builder) {
127126 builder .beforeAgentCallback ,
128127 builder .afterAgentCallback );
129128 this .model = Optional .ofNullable (builder .model );
130- this .instruction = requireNonNullElse (builder .instruction , new Instruction .Static ("" ));
129+ this .instruction =
130+ builder .instruction == null ? new Instruction .Static ("" ) : builder .instruction ;
131131 this .globalInstruction =
132- requireNonNullElse ( builder .globalInstruction , new Instruction .Static ("" )) ;
132+ builder .globalInstruction == null ? new Instruction .Static ("" ) : builder . globalInstruction ;
133133 this .generateContentConfig = Optional .ofNullable (builder .generateContentConfig );
134134 this .exampleProvider = Optional .ofNullable (builder .exampleProvider );
135- this .includeContents = requireNonNullElse (builder .includeContents , IncludeContents .DEFAULT );
135+ this .includeContents =
136+ builder .includeContents != null ? builder .includeContents : IncludeContents .DEFAULT ;
136137 this .planning = builder .planning != null && builder .planning ;
137138 this .maxSteps = Optional .ofNullable (builder .maxSteps );
138139 this .disallowTransferToParent = builder .disallowTransferToParent ;
139140 this .disallowTransferToPeers = builder .disallowTransferToPeers ;
140- this .beforeModelCallback = requireNonNullElse (builder .beforeModelCallback , ImmutableList .of ());
141- this .afterModelCallback = requireNonNullElse (builder .afterModelCallback , ImmutableList .of ());
142- this .onModelErrorCallback =
143- requireNonNullElse (builder .onModelErrorCallback , ImmutableList .of ());
144- this .beforeToolCallback = requireNonNullElse (builder .beforeToolCallback , ImmutableList .of ());
145- this .afterToolCallback = requireNonNullElse (builder .afterToolCallback , ImmutableList .of ());
146- this .onToolErrorCallback = requireNonNullElse (builder .onToolErrorCallback , ImmutableList .of ());
141+ this .beforeModelCallback = Optional .ofNullable (builder .beforeModelCallback );
142+ this .afterModelCallback = Optional .ofNullable (builder .afterModelCallback );
143+ this .onModelErrorCallback = Optional .ofNullable (builder .onModelErrorCallback );
144+ this .beforeToolCallback = Optional .ofNullable (builder .beforeToolCallback );
145+ this .afterToolCallback = Optional .ofNullable (builder .afterToolCallback );
146+ this .onToolErrorCallback = Optional .ofNullable (builder .onToolErrorCallback );
147147 this .inputSchema = Optional .ofNullable (builder .inputSchema );
148148 this .outputSchema = Optional .ofNullable (builder .outputSchema );
149149 this .executor = Optional .ofNullable (builder .executor );
150150 this .outputKey = Optional .ofNullable (builder .outputKey );
151- this .toolsUnion = requireNonNullElse ( builder .toolsUnion , ImmutableList .of () );
151+ this .toolsUnion = builder .toolsUnion != null ? builder . toolsUnion : ImmutableList .of ();
152152 this .toolsets = extractToolsets (this .toolsUnion );
153153 this .codeExecutor = Optional .ofNullable (builder .codeExecutor );
154154
@@ -704,16 +704,7 @@ private static boolean isThought(Part part) {
704704
705705 @ Override
706706 protected Flowable <Event > runAsyncImpl (InvocationContext invocationContext ) {
707- return llmFlow
708- .run (invocationContext )
709- .concatMap (
710- event -> {
711- this .maybeSaveOutputToState (event );
712- if (invocationContext .shouldPauseInvocation (event )) {
713- return Flowable .just (event ).concatWith (Flowable .empty ());
714- }
715- return Flowable .just (event );
716- });
707+ return llmFlow .run (invocationContext ).doOnNext (this ::maybeSaveOutputToState );
717708 }
718709
719710 @ Override
@@ -850,27 +841,27 @@ public boolean disallowTransferToPeers() {
850841 return disallowTransferToPeers ;
851842 }
852843
853- public List <? extends BeforeModelCallback > beforeModelCallback () {
844+ public Optional < List <? extends BeforeModelCallback > > beforeModelCallback () {
854845 return beforeModelCallback ;
855846 }
856847
857- public List <? extends AfterModelCallback > afterModelCallback () {
848+ public Optional < List <? extends AfterModelCallback > > afterModelCallback () {
858849 return afterModelCallback ;
859850 }
860851
861- public List <? extends BeforeToolCallback > beforeToolCallback () {
852+ public Optional < List <? extends BeforeToolCallback > > beforeToolCallback () {
862853 return beforeToolCallback ;
863854 }
864855
865- public List <? extends AfterToolCallback > afterToolCallback () {
856+ public Optional < List <? extends AfterToolCallback > > afterToolCallback () {
866857 return afterToolCallback ;
867858 }
868859
869- public List <? extends OnModelErrorCallback > onModelErrorCallback () {
860+ public Optional < List <? extends OnModelErrorCallback > > onModelErrorCallback () {
870861 return onModelErrorCallback ;
871862 }
872863
873- public List <? extends OnToolErrorCallback > onToolErrorCallback () {
864+ public Optional < List <? extends OnToolErrorCallback > > onToolErrorCallback () {
874865 return onToolErrorCallback ;
875866 }
876867
@@ -880,7 +871,7 @@ public List<? extends OnToolErrorCallback> onToolErrorCallback() {
880871 * <p>This method is only for use by Agent Development Kit.
881872 */
882873 public List <? extends BeforeModelCallback > canonicalBeforeModelCallbacks () {
883- return beforeModelCallback ;
874+ return beforeModelCallback . orElse ( ImmutableList . of ()) ;
884875 }
885876
886877 /**
@@ -889,7 +880,7 @@ public List<? extends BeforeModelCallback> canonicalBeforeModelCallbacks() {
889880 * <p>This method is only for use by Agent Development Kit.
890881 */
891882 public List <? extends AfterModelCallback > canonicalAfterModelCallbacks () {
892- return afterModelCallback ;
883+ return afterModelCallback . orElse ( ImmutableList . of ()) ;
893884 }
894885
895886 /**
@@ -898,7 +889,7 @@ public List<? extends AfterModelCallback> canonicalAfterModelCallbacks() {
898889 * <p>This method is only for use by Agent Development Kit.
899890 */
900891 public List <? extends OnModelErrorCallback > canonicalOnModelErrorCallbacks () {
901- return onModelErrorCallback ;
892+ return onModelErrorCallback . orElse ( ImmutableList . of ()) ;
902893 }
903894
904895 /**
@@ -907,7 +898,7 @@ public List<? extends OnModelErrorCallback> canonicalOnModelErrorCallbacks() {
907898 * <p>This method is only for use by Agent Development Kit.
908899 */
909900 public List <? extends BeforeToolCallback > canonicalBeforeToolCallbacks () {
910- return beforeToolCallback ;
901+ return beforeToolCallback . orElse ( ImmutableList . of ()) ;
911902 }
912903
913904 /**
@@ -916,7 +907,7 @@ public List<? extends BeforeToolCallback> canonicalBeforeToolCallbacks() {
916907 * <p>This method is only for use by Agent Development Kit.
917908 */
918909 public List <? extends AfterToolCallback > canonicalAfterToolCallbacks () {
919- return afterToolCallback ;
910+ return afterToolCallback . orElse ( ImmutableList . of ()) ;
920911 }
921912
922913 /**
@@ -925,7 +916,7 @@ public List<? extends AfterToolCallback> canonicalAfterToolCallbacks() {
925916 * <p>This method is only for use by Agent Development Kit.
926917 */
927918 public List <? extends OnToolErrorCallback > canonicalOnToolErrorCallbacks () {
928- return onToolErrorCallback ;
919+ return onToolErrorCallback . orElse ( ImmutableList . of ()) ;
929920 }
930921
931922 public Optional <Schema > inputSchema () {
0 commit comments