|
16 | 16 | package com.google.adk.models.springai; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 |
|
21 | 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
22 | 23 | import com.google.adk.models.LlmRequest; |
|
33 | 34 | import org.springframework.ai.chat.messages.Message; |
34 | 35 | import org.springframework.ai.chat.messages.SystemMessage; |
35 | 36 | import org.springframework.ai.chat.messages.UserMessage; |
| 37 | +import org.springframework.ai.chat.metadata.ChatResponseMetadata; |
| 38 | +import org.springframework.ai.chat.metadata.DefaultUsage; |
36 | 39 | import org.springframework.ai.chat.model.ChatResponse; |
37 | 40 | import org.springframework.ai.chat.model.Generation; |
38 | 41 | import org.springframework.ai.chat.prompt.Prompt; |
@@ -237,6 +240,70 @@ void testToLlmResponseFromChatResponseWithToolCalls() { |
237 | 240 | assertThat(functionCallPart.functionCall().get().id()).contains("call_123"); |
238 | 241 | } |
239 | 242 |
|
| 243 | + @Test |
| 244 | + void testUsageMetadataShouldBeEmptyWhenSpringAiMetadataIsNull() { |
| 245 | + MessageConverter converter = new MessageConverter(new ObjectMapper()); |
| 246 | + AssistantMessage assistantMessage = new AssistantMessage("intermediate chunk"); |
| 247 | + Generation generation = new Generation(assistantMessage); |
| 248 | + |
| 249 | + ChatResponse chatResponse = new ChatResponse(List.of(generation), null); |
| 250 | + |
| 251 | + LlmResponse llmResponse = converter.toLlmResponse(chatResponse, true); |
| 252 | + |
| 253 | + assertTrue( |
| 254 | + llmResponse.usageMetadata().isEmpty(), |
| 255 | + "Expected usageMetadata to be empty for intermediate stream chunks lacking metadata"); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + void testUsageMetadataShouldBeEmptyWhenSpringAiUsageIsNull() { |
| 260 | + MessageConverter converter = new MessageConverter(new ObjectMapper()); |
| 261 | + AssistantMessage assistantMessage = new AssistantMessage("intermediate chunk"); |
| 262 | + Generation generation = new Generation(assistantMessage); |
| 263 | + |
| 264 | + ChatResponseMetadata metadata = ChatResponseMetadata.builder().id("resp-no-usage").build(); |
| 265 | + |
| 266 | + ChatResponse chatResponse = new ChatResponse(List.of(generation), metadata); |
| 267 | + |
| 268 | + LlmResponse llmResponse = converter.toLlmResponse(chatResponse, true); |
| 269 | + |
| 270 | + assertTrue( |
| 271 | + llmResponse.usageMetadata().isEmpty(), |
| 272 | + "Expected usageMetadata to be empty when metadata exists but usage is null"); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + void testUsageMetadataShouldDefaultToZeroWhenSpringAiTokensAreNull() { |
| 277 | + MessageConverter converter = new MessageConverter(new ObjectMapper()); |
| 278 | + AssistantMessage assistantMessage = new AssistantMessage("final chunk"); |
| 279 | + Generation generation = new Generation(assistantMessage); |
| 280 | + |
| 281 | + // Anonymous implementation to simulate incomplete provider data where some token counts are |
| 282 | + // null |
| 283 | + DefaultUsage incompleteUsage = new DefaultUsage(null, null, 42); |
| 284 | + ChatResponseMetadata metadata = |
| 285 | + ChatResponseMetadata.builder().id("resp-partial-tokens").usage(incompleteUsage).build(); |
| 286 | + |
| 287 | + ChatResponse chatResponse = new ChatResponse(List.of(generation), metadata); |
| 288 | + |
| 289 | + LlmResponse llmResponse = converter.toLlmResponse(chatResponse, false); |
| 290 | + |
| 291 | + assertTrue(llmResponse.usageMetadata().isPresent(), "Expected usageMetadata to be present"); |
| 292 | + |
| 293 | + assertEquals( |
| 294 | + 0, |
| 295 | + llmResponse.usageMetadata().get().promptTokenCount().orElse(-1), |
| 296 | + "Null prompt tokens should default to 0"); |
| 297 | + assertEquals( |
| 298 | + 0, |
| 299 | + llmResponse.usageMetadata().get().candidatesTokenCount().orElse(-1), |
| 300 | + "Null completion tokens should default to 0"); |
| 301 | + assertEquals( |
| 302 | + 42, |
| 303 | + llmResponse.usageMetadata().get().totalTokenCount().orElse(-1), |
| 304 | + "Total tokens should be mapped correctly"); |
| 305 | + } |
| 306 | + |
240 | 307 | @Test |
241 | 308 | void testToolCallIdPreservedInConversion() { |
242 | 309 | // Create AssistantMessage with tool call including ID |
|
0 commit comments