File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
models/spring-ai-anthropic/src
main/java/org/springframework/ai/anthropic/api
test/java/org/springframework/ai/anthropic/api Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 20
20
import java .util .List ;
21
21
import java .util .concurrent .atomic .AtomicReference ;
22
22
23
+ import org .slf4j .Logger ;
24
+ import org .slf4j .LoggerFactory ;
25
+
23
26
import org .springframework .ai .anthropic .api .AnthropicApi .ChatCompletionResponse ;
24
27
import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlock ;
25
28
import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlock .Type ;
56
59
*/
57
60
public class StreamHelper {
58
61
62
+ private static final Logger logger = LoggerFactory .getLogger (StreamHelper .class );
63
+
59
64
public boolean isToolUseStart (StreamEvent event ) {
60
65
if (event == null || event .type () == null || event .type () != EventType .CONTENT_BLOCK_START ) {
61
66
return false ;
@@ -216,7 +221,11 @@ else if (event.type().equals(EventType.MESSAGE_STOP)) {
216
221
}
217
222
else {
218
223
// Any other event types that should propagate upwards without content
224
+ if (contentBlockReference .get () == null ) {
225
+ contentBlockReference .set (new ChatCompletionResponseBuilder ());
226
+ }
219
227
contentBlockReference .get ().withType (event .type ().name ()).withContent (List .of ());
228
+ logger .warn ("Unhandled event type: {}" , event .type ().name ());
220
229
}
221
230
222
231
return contentBlockReference .get ().build ();
Original file line number Diff line number Diff line change
1
+ package org .springframework .ai .anthropic .api ;
2
+
3
+ import java .util .concurrent .atomic .AtomicReference ;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import org .springframework .ai .anthropic .api .StreamHelper .ChatCompletionResponseBuilder ;
8
+
9
+ import static org .assertj .core .api .Assertions .assertThat ;
10
+
11
+ /**
12
+ * @author Ilayaperumal Gopinathan
13
+ */
14
+ class StreamHelperTest {
15
+
16
+ @ Test
17
+ void testErrorEventTypeWithEmptyContentBlock () {
18
+ AnthropicApi .ErrorEvent errorEvent = new AnthropicApi .ErrorEvent (AnthropicApi .EventType .ERROR ,
19
+ new AnthropicApi .ErrorEvent .Error ("error" , "error message" ));
20
+ AtomicReference <ChatCompletionResponseBuilder > contentBlockReference = new AtomicReference <>();
21
+ StreamHelper streamHelper = new StreamHelper ();
22
+ AnthropicApi .ChatCompletionResponse response = streamHelper .eventToChatCompletionResponse (errorEvent ,
23
+ contentBlockReference );
24
+ assertThat (response ).isNotNull ();
25
+ }
26
+
27
+ }
You can’t perform that action at this time.
0 commit comments