1717import javax .annotation .Nonnull ;
1818import lombok .extern .slf4j .Slf4j ;
1919import lombok .val ;
20+ import org .springframework .ai .chat .messages .AssistantMessage .ToolCall ;
2021import org .springframework .ai .chat .messages .Message ;
2122import org .springframework .ai .chat .messages .ToolResponseMessage ;
23+ import org .springframework .ai .chat .messages .ToolResponseMessage .ToolResponse ;
2224import org .springframework .ai .chat .model .AbstractToolCallSupport ;
2325import org .springframework .ai .chat .model .ChatModel ;
2426import org .springframework .ai .chat .model .ChatResponse ;
@@ -59,13 +61,11 @@ public OrchestrationChatModel(OrchestrationClient client) {
5961 @ Nonnull
6062 @ Override
6163 public ChatResponse call (@ Nonnull final Prompt prompt ) {
62- return internalCall (prompt , null );
63- }
64-
65- public ChatResponse internalCall (Prompt prompt , ChatResponse previousChatResponse ) {
66-
6764 if (prompt .getOptions () instanceof OrchestrationChatOptions options ) {
68- runtimeFunctionCallbackConfigurations (FunctionCallingOptions .builder ().functionCallbacks (options .getFunctionCallbacks ()).build ());
65+ runtimeFunctionCallbackConfigurations (
66+ FunctionCallingOptions .builder ()
67+ .functionCallbacks (options .getFunctionCallbacks ())
68+ .build ());
6969 val orchestrationPrompt = toOrchestrationPrompt (prompt );
7070 val response =
7171 new OrchestrationSpringChatResponse (
@@ -76,7 +76,7 @@ && isToolCall(response, Set.of("tool_calls", "stop"))) {
7676 var toolCallConversation = handleToolCalls (prompt , response );
7777 // Recursively call the call method with the tool call message
7878 // conversation that contains the call responses.
79- return internalCall (new Prompt (toolCallConversation , prompt .getOptions ()), response );
79+ return call (new Prompt (toolCallConversation , prompt .getOptions ()));
8080 }
8181 return response ;
8282 }
@@ -128,9 +128,16 @@ private static com.sap.ai.sdk.orchestration.Message[] toOrchestrationMessages(
128128 case USER :
129129 yield new UserMessage (msg .getText ());
130130 case ASSISTANT :
131+ final List <ToolCall > toolCalls =
132+ ((org .springframework .ai .chat .messages .AssistantMessage ) msg ).getToolCalls ();
133+ if (toolCalls != null ) {
134+ yield new AssistantMessage (toolCalls );
135+ }
131136 yield new AssistantMessage (msg .getText ());
132137 case TOOL :
133- yield new ToolMessage ((ToolResponseMessage ) msg );
138+ val responses = ((ToolResponseMessage ) msg ).getResponses ();
139+ ToolResponse response = responses .get (0 );
140+ yield new ToolMessage (response .id (), response .responseData ());
134141 };
135142 return messages .stream ().map (mapper ).toArray (com .sap .ai .sdk .orchestration .Message []::new );
136143 }
0 commit comments