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:
@@ -157,12 +157,27 @@ export class ConversationService {
|
|||||||
// 反转顺序(最早的在前)
|
// 反转顺序(最早的在前)
|
||||||
historyMessages.reverse();
|
historyMessages.reverse();
|
||||||
|
|
||||||
// 渲染用户Prompt模板
|
// 判断是否是第一条消息
|
||||||
const renderedUserPrompt = agentService.renderUserPrompt(agentId, {
|
const isFirstMessage = historyMessages.length === 0;
|
||||||
projectBackground,
|
|
||||||
userInput,
|
// 渲染用户Prompt
|
||||||
knowledgeBaseContext,
|
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[] = [
|
const messages: Message[] = [
|
||||||
@@ -183,7 +198,7 @@ export class ConversationService {
|
|||||||
// 添加当前用户输入
|
// 添加当前用户输入
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: renderedUserPrompt,
|
content: userPromptContent,
|
||||||
});
|
});
|
||||||
|
|
||||||
return messages;
|
return messages;
|
||||||
|
|||||||
Reference in New Issue
Block a user