88import org .springframework .ai .chat .client .advisor .api .AdvisedRequest ;
99import org .springframework .ai .chat .client .advisor .api .AdvisedResponse ;
1010import org .springframework .ai .chat .client .advisor .api .Advisor ;
11- import org .springframework .ai .chat .client .advisor .api .AroundAdvisorChain ;
11+ import org .springframework .ai .chat .client .advisor .api .CallAroundAdvisorChain ;
1212import org .springframework .ai .chat .client .advisor .api .CallAroundAdvisor ;
1313import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisor ;
14+ import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisorChain ;
1415import org .springframework .ai .chat .client .advisor .observation .AdvisorObservationContext ;
1516import org .springframework .ai .chat .client .advisor .observation .AdvisorObservationConvention ;
1617import org .springframework .ai .chat .client .advisor .observation .AdvisorObservationDocumentation ;
2324import io .micrometer .observation .contextpropagation .ObservationThreadLocalAccessor ;
2425import reactor .core .publisher .Flux ;
2526
26- public class DefaultAroundAdvisorChain implements AroundAdvisorChain {
27+ public class DefaultAroundAdvisorChain implements CallAroundAdvisorChain , StreamAroundAdvisorChain {
2728
2829 public static final AdvisorObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultAdvisorObservationConvention ();
2930
@@ -33,35 +34,25 @@ public class DefaultAroundAdvisorChain implements AroundAdvisorChain {
3334
3435 private final ObservationRegistry observationRegistry ;
3536
36- public DefaultAroundAdvisorChain (ObservationRegistry observationRegistry ) {
37- this (observationRegistry , new ArrayDeque <CallAroundAdvisor >(), new ArrayDeque <StreamAroundAdvisor >());
38- }
39-
40- public DefaultAroundAdvisorChain (ObservationRegistry observationRegistry ,
41- Deque <CallAroundAdvisor > callAroundAdvisors , Deque <StreamAroundAdvisor > streamAroundAdvisors ) {
42- Assert .notNull (callAroundAdvisors , "the callAroundAdvisors must be non-null" );
37+ DefaultAroundAdvisorChain (ObservationRegistry observationRegistry , List <Advisor > advisors ) {
38+ Assert .notNull (advisors , "the callAroundAdvisors must be non-null" );
4339 this .observationRegistry = observationRegistry ;
44- this .callAroundAdvisors = callAroundAdvisors ;
45- this .streamAroundAdvisors = streamAroundAdvisors ;
46- }
47-
48- public DefaultAroundAdvisorChain (ObservationRegistry observationRegistry , List <Advisor > advisors ) {
49- this (observationRegistry );
40+ this .callAroundAdvisors = new ArrayDeque <>();
41+ this .streamAroundAdvisors = new ArrayDeque <>();
5042 Assert .notNull (advisors , "the advisors must be non-null" );
5143 this .pushAll (advisors );
5244 }
5345
54- public void pushAll (List <? extends Advisor > advisors ) {
46+ void pushAll (List <? extends Advisor > advisors ) {
5547 Assert .notNull (advisors , "the advisors must be non-null" );
5648 if (!CollectionUtils .isEmpty (advisors )) {
57-
5849 List <CallAroundAdvisor > callAroundAdvisors = advisors .stream ()
5950 .filter (a -> a instanceof CallAroundAdvisor )
6051 .map (a -> (CallAroundAdvisor ) a )
6152 .toList ();
6253
6354 if (!CollectionUtils .isEmpty (callAroundAdvisors )) {
64- callAroundAdvisors .stream (). forEach (this .callAroundAdvisors ::push );
55+ callAroundAdvisors .forEach (this .callAroundAdvisors ::push );
6556 }
6657
6758 List <StreamAroundAdvisor > streamAroundAdvisors = advisors .stream ()
@@ -70,7 +61,7 @@ public void pushAll(List<? extends Advisor> advisors) {
7061 .toList ();
7162
7263 if (!CollectionUtils .isEmpty (streamAroundAdvisors )) {
73- streamAroundAdvisors .stream (). forEach (this .streamAroundAdvisors ::push );
64+ streamAroundAdvisors .forEach (this .streamAroundAdvisors ::push );
7465 }
7566
7667 this .reOrder ();
@@ -79,16 +70,15 @@ public void pushAll(List<? extends Advisor> advisors) {
7970
8071 public void reOrder () {
8172 // Order the advisors in priority order based on their Ordered attribute.
82-
8373 ArrayList <CallAroundAdvisor > temp = new ArrayList <>(this .callAroundAdvisors );
8474 OrderComparator .sort (temp );
8575 this .callAroundAdvisors .clear ();
86- temp .stream (). forEach (this .callAroundAdvisors ::addLast );
76+ temp .forEach (this .callAroundAdvisors ::addLast );
8777
8878 ArrayList <StreamAroundAdvisor > temp2 = new ArrayList <>(this .streamAroundAdvisors );
8979 OrderComparator .sort (temp2 );
9080 this .streamAroundAdvisors .clear ();
91- temp2 .stream (). forEach (this .streamAroundAdvisors ::addLast );
81+ temp2 .forEach (this .streamAroundAdvisors ::addLast );
9282 }
9383
9484 @ Override
@@ -114,9 +104,7 @@ public AdvisedResponse nextAroundCall(AdvisedRequest advisedRequest) {
114104
115105 @ Override
116106 public Flux <AdvisedResponse > nextAroundStream (AdvisedRequest advisedRequest ) {
117-
118107 return Flux .deferContextual (contextView -> {
119-
120108 if (this .streamAroundAdvisors .isEmpty ()) {
121109 return Flux .error (new IllegalStateException ("No AroundAdvisor available to execute" ));
122110 }
@@ -137,9 +125,10 @@ public Flux<AdvisedResponse> nextAroundStream(AdvisedRequest advisedRequest) {
137125
138126 // @formatter:off
139127 return Flux .defer (() -> advisor .aroundStream (advisedRequest , this ))
140- .doOnError (observation ::error )
141- .doFinally (s -> { observation .stop ();
142- }).contextWrite (ctx -> ctx .put (ObservationThreadLocalAccessor .KEY , observation ));
128+ .doOnError (observation ::error )
129+ .doFinally (s -> {
130+ observation .stop ();
131+ }).contextWrite (ctx -> ctx .put (ObservationThreadLocalAccessor .KEY , observation ));
143132 // @formatter:on
144133 });
145134 }
@@ -152,10 +141,12 @@ public static class Builder {
152141
153142 private final DefaultAroundAdvisorChain aroundAdvisorChain ;
154143
144+ // TODO(dj): this has all advisors actually; the build step filters the around
145+ // advisors
155146 private final List <Advisor > aroundAdvisors = new ArrayList <>();
156147
157148 public Builder (ObservationRegistry observationRegistry ) {
158- this .aroundAdvisorChain = new DefaultAroundAdvisorChain (observationRegistry );
149+ this .aroundAdvisorChain = new DefaultAroundAdvisorChain (observationRegistry , aroundAdvisors );
159150 }
160151
161152 public Builder push (Advisor aroundAdvisor ) {
0 commit comments