Skip to content

Commit ac6b4dd

Browse files
authored
Merge pull request #748 from fchill/731-duplicated-user-message
[Issue 731] Fix duplicated user message in streaming mode
2 parents 2493f34 + 1bcca91 commit ac6b4dd

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/main/java/com/devoxx/genie/service/prompt/strategy/StreamingPromptStrategy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.devoxx.genie.ui.panel.PromptOutputPanel;
1515
import com.devoxx.genie.ui.util.NotificationUtil;
1616
import com.devoxx.genie.ui.webview.ConversationWebViewController;
17+
import com.devoxx.genie.util.TemplateVariableEscaper;
1718
import com.intellij.openapi.project.Project;
1819
import dev.langchain4j.memory.ChatMemory;
1920
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
@@ -80,9 +81,6 @@ protected void executeStrategySpecific(
8081
// Prepare memory which already adds the user message
8182
prepareMemory(context);
8283

83-
// We need to add this to chat memory when streaming response
84-
chatMemoryManager.addUserMessage(context);
85-
8684
// Create the streaming handler that will process chunks of response
8785
StreamingResponseHandler streamingResponseHandler;
8886

@@ -181,7 +179,10 @@ protected void executeStrategySpecific(
181179
.build();
182180
}
183181

184-
TokenStream chat = assistant.chat(context.getUserPrompt());
182+
String userMessage = context.getUserMessage().singleText();
183+
String cleanText = TemplateVariableEscaper.escape(userMessage);
184+
185+
TokenStream chat = assistant.chat(cleanText);
185186

186187
chat.onPartialResponse(streamingResponseHandler::onPartialResponse)
187188
.onToolExecuted(ToolExecution::request)

src/test/java/com/devoxx/genie/service/prompt/PromptMessageFlowIntegrationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PromptMessageFlowIntegrationTest extends AbstractLightPlatformTestC
3535

3636
@Spy
3737
@InjectMocks
38-
private MessageCreationService messageCreationService;
38+
private MessageCreationService spyMessageCreationService;
3939

4040
@Captor
4141
private ArgumentCaptor<ChatMessageContext> contextCaptor;
@@ -59,15 +59,15 @@ public void setUp() throws Exception {
5959
.thenReturn(mockChatMemoryManager);
6060

6161
messageCreationServiceStaticMock.when(MessageCreationService::getInstance)
62-
.thenReturn(messageCreationService);
62+
.thenReturn(spyMessageCreationService);
6363
}
6464

6565
// Set up a strategy with mocked dependencies using anonymous class for test
6666
strategy = new NonStreamingPromptStrategy(mockProject) {
6767
{
6868
// Manually inject mocked dependencies
6969
this.chatMemoryManager = mockChatMemoryManager;
70-
this.messageCreationService = messageCreationService;
70+
this.messageCreationService = spyMessageCreationService;
7171
// For this test we don't need to mock ThreadPoolManager or PromptExecutionService
7272
// since we're only testing prepareMemory
7373
}
@@ -117,7 +117,7 @@ public void testVerifyCorrectMessageEnrichmentFlow() {
117117
verify(mockChatMemoryManager).prepareMemory(testContext);
118118

119119
// Verify MessageCreationService.addUserMessageToContext was called
120-
verify(messageCreationService).addUserMessageToContext(testContext);
120+
verify(spyMessageCreationService).addUserMessageToContext(testContext);
121121

122122
// Verify addUserMessage was called after context enrichment
123123
verify(mockChatMemoryManager).addUserMessage(testContext);
@@ -142,7 +142,7 @@ public void testVerifyDuplicateCallsToAddUserMessageToContextAreIdempotent() {
142142
testContext.setUserMessage(existingMessage);
143143

144144
// Act - call the context enrichment method directly
145-
messageCreationService.addUserMessageToContext(testContext);
145+
spyMessageCreationService.addUserMessageToContext(testContext);
146146

147147
// Assert - verify the existing message wasn't changed
148148
assertSame(String.valueOf(existingMessage), testContext.getUserMessage(),

0 commit comments

Comments
 (0)