|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.chat.model; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link MessageAggregator} with Qwen streaming tool call pattern. |
| 31 | + * @author Taewoong Kim |
| 32 | + */ |
| 33 | +class MessageAggregatorQwenTest { |
| 34 | + |
| 35 | + private final MessageAggregator messageAggregator = new MessageAggregator(); |
| 36 | + |
| 37 | + /** |
| 38 | + * Test based on actual Qwen streaming response from OpenRouter. Qwen sends tool name |
| 39 | + * only in the first chunk, subsequent chunks have empty name. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void shouldMergeToolCallsFromQwenStreamingResponse() { |
| 43 | + // Given: Qwen streaming chunks (actual pattern from curl test) |
| 44 | + // Chunk 1: {"tool_calls":[{"id":"...","function":{"name":"getCurrentWeather"}}]} |
| 45 | + ChatResponse chunk1 = new ChatResponse(List.of(new Generation(AssistantMessage.builder() |
| 46 | + .toolCalls(List.of(new AssistantMessage.ToolCall("chatcmpl-tool-123", "function", "getCurrentWeather", ""))) |
| 47 | + .build()))); |
| 48 | + |
| 49 | + // Chunk 2: {"tool_calls":[{"index":0,"function":{"arguments":"{\"location\": |
| 50 | + // \""}}]} |
| 51 | + ChatResponse chunk2 = new ChatResponse(List.of(new Generation(AssistantMessage.builder() |
| 52 | + .toolCalls(List.of(new AssistantMessage.ToolCall("chatcmpl-tool-123", "function", "", "{\"location\": \""))) |
| 53 | + .build()))); |
| 54 | + |
| 55 | + // Chunk 3: {"tool_calls":[{"index":0,"function":{"arguments":"Se"}}]} |
| 56 | + ChatResponse chunk3 = new ChatResponse(List.of(new Generation(AssistantMessage.builder() |
| 57 | + .toolCalls(List.of(new AssistantMessage.ToolCall("chatcmpl-tool-123", "function", "", "Se"))) |
| 58 | + .build()))); |
| 59 | + |
| 60 | + // Chunk 4: {"tool_calls":[{"index":0,"function":{"arguments":"oul"}}]} |
| 61 | + ChatResponse chunk4 = new ChatResponse(List.of(new Generation(AssistantMessage.builder() |
| 62 | + .toolCalls(List.of(new AssistantMessage.ToolCall("chatcmpl-tool-123", "function", "", "oul"))) |
| 63 | + .build()))); |
| 64 | + |
| 65 | + // Chunk 5: {"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]} |
| 66 | + ChatResponse chunk5 = new ChatResponse(List.of(new Generation(AssistantMessage.builder() |
| 67 | + .toolCalls(List.of(new AssistantMessage.ToolCall("chatcmpl-tool-123", "function", "", "\"}"))) |
| 68 | + .build()))); |
| 69 | + |
| 70 | + Flux<ChatResponse> flux = Flux.just(chunk1, chunk2, chunk3, chunk4, chunk5); |
| 71 | + |
| 72 | + // When: Aggregate the streaming responses |
| 73 | + AtomicReference<ChatResponse> finalResponse = new AtomicReference<>(); |
| 74 | + this.messageAggregator.aggregate(flux, finalResponse::set).blockLast(); |
| 75 | + |
| 76 | + // Then: Verify the tool call was properly merged |
| 77 | + assertThat(finalResponse.get()).isNotNull(); |
| 78 | + List<AssistantMessage.ToolCall> toolCalls = finalResponse.get().getResult().getOutput().getToolCalls(); |
| 79 | + |
| 80 | + assertThat(toolCalls).hasSize(1); |
| 81 | + AssistantMessage.ToolCall mergedToolCall = toolCalls.get(0); |
| 82 | + |
| 83 | + // The bug was: toolName would be empty string, causing "toolName cannot be null |
| 84 | + // or empty" |
| 85 | + // After fix: name should be preserved from first chunk |
| 86 | + assertThat(mergedToolCall.name()).isEqualTo("getCurrentWeather"); |
| 87 | + assertThat(mergedToolCall.arguments()).isEqualTo("{\"location\": \"Seoul\"}"); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments