Skip to content

Commit 36d0cdf

Browse files
committed
fixed test error
Signed-off-by: “claudio-code” <[email protected]>
1 parent da21d97 commit 36d0cdf

File tree

2 files changed

+32
-65
lines changed
  • models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api
  • spring-ai-model/src/main/java/org/springframework/ai/chat/messages

2 files changed

+32
-65
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ public StreamEvent mergeToolUseEvents(StreamEvent previousEvent, StreamEvent eve
9090
ContentBlockToolUse cbToolUse = (ContentBlockToolUse) contentBlockStart.contentBlock();
9191

9292
return eventAggregator.withIndex(contentBlockStart.index())
93-
.withId(cbToolUse.id())
94-
.withName(cbToolUse.name())
95-
.appendPartialJson(""); // CB START always has empty JSON.
93+
.withId(cbToolUse.id())
94+
.withName(cbToolUse.name())
95+
.appendPartialJson(""); // CB START always has empty JSON.
9696
}
9797
}
9898
else if (event.type() == EventType.CONTENT_BLOCK_DELTA) {
9999
ContentBlockDeltaEvent contentBlockDelta = (ContentBlockDeltaEvent) event;
100100
if (ContentBlock.Type.INPUT_JSON_DELTA.getValue().equals(contentBlockDelta.delta().type())) {
101101
return eventAggregator
102-
.appendPartialJson(((ContentBlockDeltaJson) contentBlockDelta.delta()).partialJson());
102+
.appendPartialJson(((ContentBlockDeltaJson) contentBlockDelta.delta()).partialJson());
103103
}
104104
}
105105
else if (event.type() == EventType.CONTENT_BLOCK_STOP) {
@@ -121,7 +121,7 @@ else if (event.type() == EventType.CONTENT_BLOCK_STOP) {
121121
* @return A ChatCompletionResponse representing the processed chunk.
122122
*/
123123
public ChatCompletionResponse eventToChatCompletionResponse(StreamEvent event,
124-
AtomicReference<ChatCompletionResponseBuilder> contentBlockReference) {
124+
AtomicReference<ChatCompletionResponseBuilder> contentBlockReference) {
125125

126126
// https://docs.anthropic.com/claude/reference/messages-streaming
127127

@@ -131,22 +131,22 @@ public ChatCompletionResponse eventToChatCompletionResponse(StreamEvent event,
131131
MessageStartEvent messageStartEvent = (MessageStartEvent) event;
132132

133133
contentBlockReference.get()
134-
.withType(event.type().name())
135-
.withId(messageStartEvent.message().id())
136-
.withRole(messageStartEvent.message().role())
137-
.withModel(messageStartEvent.message().model())
138-
.withUsage(messageStartEvent.message().usage())
139-
.withContent(new ArrayList<>());
134+
.withType(event.type().name())
135+
.withId(messageStartEvent.message().id())
136+
.withRole(messageStartEvent.message().role())
137+
.withModel(messageStartEvent.message().model())
138+
.withUsage(messageStartEvent.message().usage())
139+
.withContent(new ArrayList<>());
140140
}
141141
else if (event.type().equals(EventType.TOOL_USE_AGGREGATE)) {
142142
ToolUseAggregationEvent eventToolUseBuilder = (ToolUseAggregationEvent) event;
143143

144144
if (!CollectionUtils.isEmpty(eventToolUseBuilder.getToolContentBlocks())) {
145145

146146
List<ContentBlock> content = eventToolUseBuilder.getToolContentBlocks()
147-
.stream()
148-
.map(tooToUse -> new ContentBlock(Type.TOOL_USE, tooToUse.id(), tooToUse.name(), tooToUse.input()))
149-
.toList();
147+
.stream()
148+
.map(tooToUse -> new ContentBlock(Type.TOOL_USE, tooToUse.id(), tooToUse.name(), tooToUse.input()))
149+
.toList();
150150
contentBlockReference.get().withContent(content);
151151
}
152152
}
@@ -159,7 +159,7 @@ else if (event.type().equals(EventType.CONTENT_BLOCK_START)) {
159159
}
160160
else if (contentBlockStartEvent.contentBlock() instanceof ContentBlockThinking thinkingBlock) {
161161
ContentBlock cb = new ContentBlock(Type.THINKING, null, null, contentBlockStartEvent.index(), null,
162-
null, null, null, null, null, thinkingBlock.thinking(), null);
162+
null, null, null, null, null, null, thinkingBlock.thinking(), null);
163163
contentBlockReference.get().withType(event.type().name()).withContent(List.of(cb));
164164
}
165165
else {
@@ -176,12 +176,12 @@ else if (event.type().equals(EventType.CONTENT_BLOCK_DELTA)) {
176176
}
177177
else if (contentBlockDeltaEvent.delta() instanceof ContentBlockDeltaThinking thinking) {
178178
ContentBlock cb = new ContentBlock(Type.THINKING_DELTA, null, null, contentBlockDeltaEvent.index(),
179-
null, null, null, null, null, null, thinking.thinking(), null);
179+
null, null, null, null, null, null, null, thinking.thinking(), null);
180180
contentBlockReference.get().withType(event.type().name()).withContent(List.of(cb));
181181
}
182182
else if (contentBlockDeltaEvent.delta() instanceof ContentBlockDeltaSignature sig) {
183183
ContentBlock cb = new ContentBlock(Type.SIGNATURE_DELTA, null, null, contentBlockDeltaEvent.index(),
184-
null, null, null, null, null, sig.signature(), null, null);
184+
null, null, null, null, null, null, sig.signature(), null, null);
185185
contentBlockReference.get().withType(event.type().name()).withContent(List.of(cb));
186186
}
187187
else {
@@ -204,8 +204,10 @@ else if (event.type().equals(EventType.MESSAGE_DELTA)) {
204204
}
205205

206206
if (messageDeltaEvent.usage() != null) {
207-
Usage totalUsage = new Usage(contentBlockReference.get().usage.inputTokens(),
208-
messageDeltaEvent.usage().outputTokens());
207+
var totalUsage = new Usage(contentBlockReference.get().usage.inputTokens(),
208+
messageDeltaEvent.usage().outputTokens(),
209+
contentBlockReference.get().usage.cacheCreationInputTokens(),
210+
contentBlockReference.get().usage.cacheReadInputTokens());
209211
contentBlockReference.get().withUsage(totalUsage);
210212
}
211213
}
@@ -214,10 +216,10 @@ else if (event.type().equals(EventType.MESSAGE_STOP)) {
214216
// with an updated event type and general information like: model, message
215217
// type, id and usage
216218
contentBlockReference.get()
217-
.withType(event.type().name())
218-
.withContent(List.of())
219-
.withStopReason(null)
220-
.withStopSequence(null);
219+
.withType(event.type().name())
220+
.withContent(List.of())
221+
.withStopReason(null)
222+
.withStopSequence(null);
221223
}
222224
else {
223225
// Any other event types that should propagate upwards without content

spring-ai-model/src/main/java/org/springframework/ai/chat/messages/UserMessage.java

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public UserMessage(String textContent, String cache) {
4444
this(MessageType.USER, textContent, new ArrayList<>(), Map.of(), cache);
4545
}
4646

47+
public UserMessage(MessageType messageType, String textContent, Collection<Media> media,
48+
Map<String, Object> metadata, String cache) {
49+
super(messageType, textContent, metadata, cache);
50+
Assert.notNull(media, "media data must not be null");
51+
this.media = new ArrayList<>(media);
52+
}
53+
4754
public UserMessage(String textContent) {
4855
this(textContent, new ArrayList<>(), Map.of());
4956
}
@@ -56,46 +63,9 @@ private UserMessage(String textContent, Collection<Media> media, Map<String, Obj
5663
}
5764

5865
public UserMessage(Resource resource) {
59-
super(MessageType.USER, resource, Map.of());
60-
this.media = new ArrayList<>();
61-
}
62-
63-
public UserMessage(Resource resource, String cache) {
64-
super(MessageType.USER, resource, Map.of(), cache);
65-
this.media = new ArrayList<>();
66-
}
67-
68-
public UserMessage(String textContent, List<Media> media) {
69-
this(MessageType.USER, textContent, media, Map.of());
70-
}
71-
72-
public UserMessage(String textContent, Media... media) {
73-
this(textContent, Arrays.asList(media));
74-
}
75-
76-
public UserMessage(String textContent, Collection<Media> mediaList, Map<String, Object> metadata) {
77-
this(MessageType.USER, textContent, mediaList, metadata);
78-
}
79-
80-
public UserMessage(MessageType messageType, String textContent, Collection<Media> media,
81-
Map<String, Object> metadata) {
82-
super(messageType, textContent, metadata);
83-
Assert.notNull(media, "media data must not be null");
84-
this.media = new ArrayList<>(media);
8566
this(MessageUtils.readResource(resource));
8667
}
8768

88-
public UserMessage(MessageType messageType, String textContent, Collection<Media> media,
89-
Map<String, Object> metadata, String cache) {
90-
super(messageType, textContent, metadata, cache);
91-
Assert.notNull(media, "media data must not be null");
92-
this.media = new ArrayList<>(media);
93-
}
94-
95-
public List<Media> getMedia(String... dummy) {
96-
return this.media;
97-
}
98-
9969
@Override
10070
public String toString() {
10171
return "UserMessage{" + "content='" + getText() + '\'' + ", metadata=" + this.metadata + ", messageType="
@@ -176,9 +146,4 @@ else if (this.resource != null) {
176146

177147
}
178148

179-
@Override
180-
public String getCache() {
181-
return super.getCache();
182-
}
183-
184149
}

0 commit comments

Comments
 (0)