Skip to content

Commit 4640dce

Browse files
committed
Fixes google#482 - Corrected high priority code review items
1 parent 1f47a1e commit 4640dce

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎contrib/spring-ai/src/main/java/com/google/adk/models/springai/MessageConverter.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,13 @@ private Content convertAssistantMessageToContent(AssistantMessage assistantMessa
338338
try {
339339
Map<String, Object> args =
340340
objectMapper.readValue(toolCall.arguments(), MAP_TYPE_REFERENCE);
341-
parts.add(Part.fromFunctionCall(toolCall.name(), args));
341+
342+
// Create FunctionCall with ID, name, and args to preserve tool call ID
343+
FunctionCall functionCall =
344+
FunctionCall.builder().id(toolCall.id()).name(toolCall.name()).args(args).build();
345+
346+
// Create Part with the FunctionCall (preserves ID)
347+
parts.add(Part.builder().functionCall(functionCall).build());
342348
} catch (JsonProcessingException e) {
343349
throw MessageConversionException.jsonParsingFailed("tool call arguments", e);
344350
}

‎contrib/spring-ai/src/test/java/com/google/adk/models/springai/MessageConverterTest.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,46 @@ void testToLlmResponseFromChatResponseWithToolCalls() {
223223
Part functionCallPart = content.parts().get().get(1);
224224
assertThat(functionCallPart.functionCall()).isPresent();
225225
assertThat(functionCallPart.functionCall().get().name()).contains("get_weather");
226+
// Verify ID is preserved
227+
assertThat(functionCallPart.functionCall().get().id()).contains("call_123");
228+
}
229+
230+
@Test
231+
void testToolCallIdPreservedInConversion() {
232+
// Create AssistantMessage with tool call including ID
233+
AssistantMessage.ToolCall toolCall =
234+
new AssistantMessage.ToolCall(
235+
"call_abc123", // ID must be preserved
236+
"function",
237+
"get_weather",
238+
"{\"location\":\"San Francisco\"}");
239+
240+
AssistantMessage assistantMessage =
241+
new AssistantMessage("Let me check the weather.", Map.of(), List.of(toolCall));
242+
243+
Generation generation = new Generation(assistantMessage);
244+
ChatResponse chatResponse = new ChatResponse(List.of(generation));
245+
246+
// Convert to LlmResponse
247+
LlmResponse llmResponse = messageConverter.toLlmResponse(chatResponse);
248+
249+
// Verify the converted content preserves the tool call ID
250+
assertThat(llmResponse.content()).isPresent();
251+
Content content = llmResponse.content().get();
252+
assertThat(content.parts()).isPresent();
253+
254+
List<Part> parts = content.parts().get();
255+
Part functionCallPart =
256+
parts.stream()
257+
.filter(p -> p.functionCall().isPresent())
258+
.findFirst()
259+
.orElseThrow(() -> new AssertionError("Expected function call part"));
260+
261+
FunctionCall convertedCall = functionCallPart.functionCall().get();
262+
assertThat(convertedCall.id()).contains("call_abc123"); // ✅ ID MUST BE PRESERVED
263+
assertThat(convertedCall.name()).contains("get_weather");
264+
assertThat(convertedCall.args()).isPresent();
265+
assertThat(convertedCall.args().get()).containsEntry("location", "San Francisco");
226266
}
227267

228268
@Test

0 commit comments

Comments
 (0)