Skip to content

Commit 1c4d6cf

Browse files
authored
Refactor: Centralize event type checks into utility method
1 parent c529c83 commit 1c4d6cf

File tree

1 file changed

+9
-8
lines changed
  • models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api

1 file changed

+9
-8
lines changed

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/StreamHelper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,19 @@
5454
public class StreamHelper {
5555

5656
public boolean isToolUseStart(StreamEvent event) {
57-
if (event == null || event.type() == null || event.type() != EventType.CONTENT_BLOCK_START) {
58-
return false;
59-
}
60-
return ContentBlock.Type.TOOL_USE.getValue().equals(((ContentBlockStartEvent) event).contentBlock().type());
57+
if (isInvalidEvent(event, EventType.CONTENT_BLOCK_START)) {
58+
return false;
59+
}
60+
ContentBlockStartEvent contentBlockStartEvent = (ContentBlockStartEvent) event;
61+
return ContentBlock.Type.TOOL_USE.getValue().equals(contentBlockStartEvent.contentBlock().type());
6162
}
6263

6364
public boolean isToolUseFinish(StreamEvent event) {
65+
return !isInvalidEvent(event, EventType.CONTENT_BLOCK_STOP);
66+
}
6467

65-
if (event == null || event.type() == null || event.type() != EventType.CONTENT_BLOCK_STOP) {
66-
return false;
67-
}
68-
return true;
68+
private boolean isInvalidEvent(StreamEvent event, EventType expectedType) {
69+
return event == null || event.type() == null || event.type() != expectedType;
6970
}
7071

7172
public StreamEvent mergeToolUseEvents(StreamEvent previousEvent, StreamEvent event) {

0 commit comments

Comments
 (0)