diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/openai/OpenAIConversationMerger.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/openai/OpenAIConversationMerger.java index 4768a87b1..9ff7c97fb 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/openai/OpenAIConversationMerger.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/openai/OpenAIConversationMerger.java @@ -83,38 +83,16 @@ public OpenAIMessage mergeToUserMessage( StringBuilder textBuffer = new StringBuilder(); textBuffer.append(conversationHistoryPrompt); textBuffer.append(HISTORY_START_TAG).append("\n"); - - // Process all messages EXCEPT the last one as history - int lastIndex = msgs.size() - 1; - - // Append history messages - if (lastIndex > 0) { - for (int i = 0; i < lastIndex; i++) { - processMessage( - msgs.get(i), - roleFormatter, - toolResultConverter, - textBuffer, - allParts, - true); - } - } - textBuffer.append(HISTORY_END_TAG).append("\n"); - - // Process the last message (current turn) - if (lastIndex >= 0) { - // Include prefix only if there is history (multi-turn context) - // or if it's explicitly needed. For single-turn user queries, - // omitting the prefix makes it look like a standard chat request. - boolean includePrefix = lastIndex > 0; + // Include prefix only if there is history (multi-turn context) + // For single-turn user queries, + // omitting the prefix makes it look like a standard chat request. + boolean includePrefix = msgs.size() > 1; + // Process all messages as history (similar to DashScopeConversationMerger) + for (Msg msg : msgs) { processMessage( - msgs.get(lastIndex), - roleFormatter, - toolResultConverter, - textBuffer, - allParts, - includePrefix); + msg, roleFormatter, toolResultConverter, textBuffer, allParts, includePrefix); } + textBuffer.append(HISTORY_END_TAG).append("\n"); // Flush remaining text if (textBuffer.length() > 0) {