From e620c8dd14ae44d78d7f3fadff6b302b046bfd7a Mon Sep 17 00:00:00 2001 From: feelshana <151412598@qq.com> Date: Sat, 28 Feb 2026 14:59:26 +0800 Subject: [PATCH] fix(core):Fix OpenAI message merging to include all messages in history for agent.call() --- .../openai/OpenAIConversationMerger.java | 38 ++++--------------- 1 file changed, 8 insertions(+), 30 deletions(-) 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) {