Skip to content

Commit 6d235a0

Browse files
Set ChatClientResponse on AdvisorObservationContext
1 parent 1aa1ed0 commit 6d235a0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/DefaultAroundAdvisorChain.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
2626
import reactor.core.publisher.Flux;
2727

28+
import org.springframework.ai.chat.client.ChatClientMessageAggregator;
2829
import org.springframework.ai.chat.client.ChatClientRequest;
2930
import org.springframework.ai.chat.client.ChatClientResponse;
3031
import org.springframework.ai.chat.client.advisor.api.Advisor;
@@ -55,6 +56,8 @@ public class DefaultAroundAdvisorChain implements BaseAdvisorChain {
5556

5657
public static final AdvisorObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultAdvisorObservationConvention();
5758

59+
private static final ChatClientMessageAggregator CHAT_CLIENT_MESSAGE_AGGREGATOR = new ChatClientMessageAggregator();
60+
5861
private final List<CallAdvisor> originalCallAdvisors;
5962

6063
private final List<StreamAdvisor> originalStreamAdvisors;
@@ -136,11 +139,13 @@ public Flux<ChatClientResponse> nextStream(ChatClientRequest chatClientRequest)
136139
observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start();
137140

138141
// @formatter:off
139-
return Flux.defer(() -> advisor.adviseStream(chatClientRequest, this)
142+
Flux<ChatClientResponse> chatClientResponse = Flux.defer(() -> advisor.adviseStream(chatClientRequest, this)
140143
.doOnError(observation::error)
141144
.doFinally(s -> observation.stop())
142145
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)));
143146
// @formatter:on
147+
return CHAT_CLIENT_MESSAGE_AGGREGATOR.aggregateChatClientResponse(chatClientResponse,
148+
observationContext::setChatClientResponse);
144149
});
145150
}
146151

0 commit comments

Comments
 (0)