From 529cfe20da7e6cd31d1fd48958e11a72ea61faa1 Mon Sep 17 00:00:00 2001 From: AI Clinical Dev Team Date: Fri, 10 Oct 2025 22:07:53 +0800 Subject: [PATCH] fix: prevent project background from being sent on every message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Critical bug fix: Project background was being sent with EVERY message, causing AI to respond to background instead of follow-up questions. Problem: - First message: User asks A, AI answers A 鉁?- Second message: User asks B, but prompt includes background again - AI responds to background content, ignores question B 鉂? Solution: - Only send full prompt template (with project background) on FIRST message - For follow-up messages, send ONLY user input (+ knowledge base if exists) - Maintain conversation history properly Updated: conversationService.assembleContext() - Check if historyMessages.length === 0 (first message) - First message: use renderUserPrompt() with all variables - Follow-up: send userInput directly (optionally with knowledge base) This ensures multi-turn conversations work correctly. --- backend/src/services/conversationService.ts | 29 ++++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/backend/src/services/conversationService.ts b/backend/src/services/conversationService.ts index 0daa5862..0a0940eb 100644 --- a/backend/src/services/conversationService.ts +++ b/backend/src/services/conversationService.ts @@ -157,12 +157,27 @@ export class ConversationService { // 反转顺序(最早的在前) historyMessages.reverse(); - // 渲染用户Prompt模板 - const renderedUserPrompt = agentService.renderUserPrompt(agentId, { - projectBackground, - userInput, - knowledgeBaseContext, - }); + // 判断是否是第一条消息 + const isFirstMessage = historyMessages.length === 0; + + // 渲染用户Prompt + let userPromptContent: string; + + if (isFirstMessage) { + // 第一条消息:使用完整模板(包含项目背景) + userPromptContent = agentService.renderUserPrompt(agentId, { + projectBackground, + userInput, + knowledgeBaseContext, + }); + } else { + // 后续消息:只发送用户输入和知识库上下文(如果有) + if (knowledgeBaseContext) { + userPromptContent = `${userInput}\n\n## 参考文献(来自知识库)\n${knowledgeBaseContext}`; + } else { + userPromptContent = userInput; + } + } // 组装消息数组 const messages: Message[] = [ @@ -183,7 +198,7 @@ export class ConversationService { // 添加当前用户输入 messages.push({ role: 'user', - content: renderedUserPrompt, + content: userPromptContent, }); return messages;