2020import com .google .genai .types .Part ;
2121import java .util .ArrayList ;
2222import java .util .List ;
23+ import java .util .concurrent .CopyOnWriteArrayList ;
24+ import java .util .concurrent .locks .ReadWriteLock ;
25+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
2326
2427/**
2528 * Aggregates streaming responses from Spring AI models.
2629 *
2730 * <p>This class helps manage the accumulation of partial responses in streaming mode, ensuring that
2831 * text content is properly concatenated and tool calls are correctly handled.
32+ *
33+ * <p><b>Thread Safety:</b> This class is thread-safe. All public methods are synchronized to ensure
34+ * safe concurrent access. The internal state is protected using a combination of thread-safe data
35+ * structures and synchronization locks.
2936 */
3037public class StreamingResponseAggregator {
3138
32- private final StringBuilder textAccumulator = new StringBuilder ();
33- private final List <Part > toolCallParts = new ArrayList <>();
34- private boolean isFirstResponse = true ;
39+ private final StringBuffer textAccumulator = new StringBuffer ();
40+ private final List <Part > toolCallParts = new CopyOnWriteArrayList <>();
41+ private final ReadWriteLock lock = new ReentrantReadWriteLock ();
42+ private volatile boolean isFirstResponse = true ;
3543
3644 /**
3745 * Processes a streaming LlmResponse and returns the current aggregated state.
@@ -49,38 +57,43 @@ public LlmResponse processStreamingResponse(LlmResponse response) {
4957 return response ;
5058 }
5159
52- // Process each part in the response
53- for (Part part : content .parts ().get ()) {
54- if (part .text ().isPresent ()) {
55- textAccumulator .append (part .text ().get ());
56- } else if (part .functionCall ().isPresent ()) {
57- // Tool calls are typically complete in each response
58- toolCallParts .add (part );
60+ lock .writeLock ().lock ();
61+ try {
62+ // Process each part in the response
63+ for (Part part : content .parts ().get ()) {
64+ if (part .text ().isPresent ()) {
65+ textAccumulator .append (part .text ().get ());
66+ } else if (part .functionCall ().isPresent ()) {
67+ // Tool calls are typically complete in each response
68+ toolCallParts .add (part );
69+ }
5970 }
60- }
6171
62- // Create aggregated content
63- List <Part > aggregatedParts = new ArrayList <>();
64- if (textAccumulator .length () > 0 ) {
65- aggregatedParts .add (Part .fromText (textAccumulator .toString ()));
66- }
67- aggregatedParts .addAll (toolCallParts );
72+ // Create aggregated content
73+ List <Part > aggregatedParts = new ArrayList <>();
74+ if (textAccumulator .length () > 0 ) {
75+ aggregatedParts .add (Part .fromText (textAccumulator .toString ()));
76+ }
77+ aggregatedParts .addAll (toolCallParts );
6878
69- Content aggregatedContent = Content .builder ().role ("model" ).parts (aggregatedParts ).build ();
79+ Content aggregatedContent = Content .builder ().role ("model" ).parts (aggregatedParts ).build ();
7080
71- // Determine if this is still partial
72- boolean isPartial = response .partial ().orElse (false );
73- boolean isTurnComplete = response .turnComplete ().orElse (true );
81+ // Determine if this is still partial
82+ boolean isPartial = response .partial ().orElse (false );
83+ boolean isTurnComplete = response .turnComplete ().orElse (true );
7484
75- LlmResponse aggregatedResponse =
76- LlmResponse .builder ()
77- .content (aggregatedContent )
78- .partial (isPartial )
79- .turnComplete (isTurnComplete )
80- .build ();
85+ LlmResponse aggregatedResponse =
86+ LlmResponse .builder ()
87+ .content (aggregatedContent )
88+ .partial (isPartial )
89+ .turnComplete (isTurnComplete )
90+ .build ();
8191
82- isFirstResponse = false ;
83- return aggregatedResponse ;
92+ isFirstResponse = false ;
93+ return aggregatedResponse ;
94+ } finally {
95+ lock .writeLock ().unlock ();
96+ }
8497 }
8598
8699 /**
@@ -89,36 +102,59 @@ public LlmResponse processStreamingResponse(LlmResponse response) {
89102 * @return The final complete response
90103 */
91104 public LlmResponse getFinalResponse () {
92- List <Part > finalParts = new ArrayList <>();
93- if (textAccumulator .length () > 0 ) {
94- finalParts .add (Part .fromText (textAccumulator .toString ()));
95- }
96- finalParts .addAll (toolCallParts );
105+ lock .writeLock ().lock ();
106+ try {
107+ List <Part > finalParts = new ArrayList <>();
108+ if (textAccumulator .length () > 0 ) {
109+ finalParts .add (Part .fromText (textAccumulator .toString ()));
110+ }
111+ finalParts .addAll (toolCallParts );
112+
113+ Content finalContent = Content .builder ().role ("model" ).parts (finalParts ).build ();
97114
98- Content finalContent = Content .builder ().role ("model" ).parts (finalParts ).build ();
115+ LlmResponse finalResponse =
116+ LlmResponse .builder ().content (finalContent ).partial (false ).turnComplete (true ).build ();
99117
100- LlmResponse finalResponse =
101- LlmResponse .builder ().content (finalContent ).partial (false ).turnComplete (true ).build ();
118+ // Reset internal state without calling reset() to avoid nested locking
119+ textAccumulator .setLength (0 );
120+ toolCallParts .clear ();
121+ isFirstResponse = true ;
102122
103- // Reset for next use
104- reset ();
105- return finalResponse ;
123+ return finalResponse ;
124+ } finally {
125+ lock .writeLock ().unlock ();
126+ }
106127 }
107128
108129 /** Resets the aggregator for reuse. */
109130 public void reset () {
110- textAccumulator .setLength (0 );
111- toolCallParts .clear ();
112- isFirstResponse = true ;
131+ lock .writeLock ().lock ();
132+ try {
133+ textAccumulator .setLength (0 );
134+ toolCallParts .clear ();
135+ isFirstResponse = true ;
136+ } finally {
137+ lock .writeLock ().unlock ();
138+ }
113139 }
114140
115141 /** Returns true if no content has been processed yet. */
116142 public boolean isEmpty () {
117- return textAccumulator .length () == 0 && toolCallParts .isEmpty ();
143+ lock .readLock ().lock ();
144+ try {
145+ return textAccumulator .length () == 0 && toolCallParts .isEmpty ();
146+ } finally {
147+ lock .readLock ().unlock ();
148+ }
118149 }
119150
120151 /** Returns the current accumulated text length. */
121152 public int getAccumulatedTextLength () {
122- return textAccumulator .length ();
153+ lock .readLock ().lock ();
154+ try {
155+ return textAccumulator .length ();
156+ } finally {
157+ lock .readLock ().unlock ();
158+ }
123159 }
124160}
0 commit comments