11/*
2- * Copyright 2023-2024 the original author or authors.
2+ * Copyright 2023-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2828import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlock .Type ;
2929import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent ;
3030import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent .ContentBlockDeltaJson ;
31+ import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent .ContentBlockDeltaSignature ;
3132import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent .ContentBlockDeltaText ;
3233import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent .ContentBlockDeltaThinking ;
33- import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockDeltaEvent .ContentBlockDeltaSignature ;
3434import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockStartEvent ;
3535import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockStartEvent .ContentBlockText ;
36- import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockStartEvent .ContentBlockToolUse ;
3736import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockStartEvent .ContentBlockThinking ;
37+ import org .springframework .ai .anthropic .api .AnthropicApi .ContentBlockStartEvent .ContentBlockToolUse ;
3838import org .springframework .ai .anthropic .api .AnthropicApi .EventType ;
3939import org .springframework .ai .anthropic .api .AnthropicApi .MessageDeltaEvent ;
4040import org .springframework .ai .anthropic .api .AnthropicApi .MessageStartEvent ;
5555 * @author Christian Tzolov
5656 * @author Jihoon Kim
5757 * @author Alexandros Pappas
58+ * @author Claudio Silva Junior
59+ * @author Soby Chacko
60+ * @author Sun Yuhan
5861 * @since 1.0.0
5962 */
6063public class StreamHelper {
@@ -81,7 +84,9 @@ public boolean isToolUseFinish(StreamEvent event) {
8184 */
8285 public StreamEvent mergeToolUseEvents (StreamEvent previousEvent , StreamEvent event ) {
8386
84- ToolUseAggregationEvent eventAggregator = (ToolUseAggregationEvent ) previousEvent ;
87+ if (!(previousEvent instanceof ToolUseAggregationEvent eventAggregator )) {
88+ return event ;
89+ }
8590
8691 if (event .type () == EventType .CONTENT_BLOCK_START ) {
8792 ContentBlockStartEvent contentBlockStart = (ContentBlockStartEvent ) event ;
@@ -159,7 +164,7 @@ else if (event.type().equals(EventType.CONTENT_BLOCK_START)) {
159164 }
160165 else if (contentBlockStartEvent .contentBlock () instanceof ContentBlockThinking thinkingBlock ) {
161166 ContentBlock cb = new ContentBlock (Type .THINKING , null , null , contentBlockStartEvent .index (), null ,
162- null , null , null , null , null , thinkingBlock .thinking (), null );
167+ null , null , null , null , thinkingBlock . signature () , thinkingBlock .thinking (), null );
163168 contentBlockReference .get ().withType (event .type ().name ()).withContent (List .of (cb ));
164169 }
165170 else {
0 commit comments