fix: prevent project background from being sent on every message

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.
This commit is contained in:
AI Clinical Dev Team
2025-10-10 22:07:53 +08:00
parent 2d51933aee
commit 529cfe20da

View File

@@ -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;