feat: 智能分析器支持历史对话上下文
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

1. analyzeIntent 接收历史对话消息
2. 传递最近10条对话给 LLM 分析
3. 帮助 AI 理解用户连续对话的意图(如再调亮一点)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-02-01 16:13:44 +08:00
parent ddf49cb329
commit 544a1cd46a
2 changed files with 62 additions and 9 deletions

View File

@ -422,13 +422,32 @@ export async function POST(request: Request) {
console.log("[Smart Auto Mode] Input:", { textContent: textContent.substring(0, 100), hasImage, imageCount: imageUrls.length, fromHistory: smartAutoHistoryImages.length > 0 });
// 2. 调用智能分析器
// 🆕 从历史消息中提取对话文本(用于上下文理解)
const historyMessages: { role: "user" | "assistant"; content: string }[] = [];
for (const msg of messagesFromDb.slice(-10)) { // 最多取最近10条
const parts = msg.parts as any[];
if (parts && parts.length > 0) {
// 提取文本内容
const textParts = parts.filter((p: any) => p.type === 'text');
const content = textParts.map((p: any) => p.text || '').join(' ').trim();
if (content) {
historyMessages.push({
role: msg.role as "user" | "assistant",
content: content.substring(0, 300), // 限制长度
});
}
}
}
console.log("[Smart Auto Mode] History context:", historyMessages.length, "messages");
// 2. 调用智能分析器(传入历史对话上下文)
const { analyzeIntent } = await import("@/lib/ai/smart-analyzer");
const analysisResult = await analyzeIntent({
text: textContent,
hasImage,
imageUrls,
imageFromHistory: smartAutoHistoryImages.length > 0, // 🆕 标识图片是否来自历史
historyMessages, // 🆕 传入历史对话用于上下文理解
});
console.log("[Smart Auto Mode] Analysis result:", {

View File

@ -130,6 +130,12 @@ const ANALYZER_SYSTEM_PROMPT = `你是一个友好的AI图片编辑助手。用
"needsClarification": false
}`;
// 历史消息类型
interface HistoryMessage {
role: "user" | "assistant";
content: string;
}
/**
* 分析用户意图
*/
@ -138,10 +144,17 @@ export async function analyzeIntent(params: {
hasImage: boolean;
imageUrls?: string[];
imageFromHistory?: boolean; // 🆕 图片是否来自历史记录
historyMessages?: HistoryMessage[]; // 🆕 历史对话消息(用于上下文理解)
}): Promise<AnalysisResult> {
const { text, hasImage, imageUrls, imageFromHistory } = params;
const { text, hasImage, imageUrls, imageFromHistory, historyMessages } = params;
console.log("[Smart Analyzer] Analyzing intent:", { text, hasImage, imageCount: imageUrls?.length, imageFromHistory });
console.log("[Smart Analyzer] Analyzing intent:", {
text,
hasImage,
imageCount: imageUrls?.length,
imageFromHistory,
historyCount: historyMessages?.length || 0
});
// 先尝试关键词快速匹配(高置信度场景直接返回,节省LLM调用)
const quickResult = quickKeywordMatch(text, hasImage);
@ -150,9 +163,9 @@ export async function analyzeIntent(params: {
return quickResult;
}
// 尝试使用 Qwen3 VL 进行深度分析
// 尝试使用 Qwen3 VL 进行深度分析(传入历史对话)
try {
const llmResult = await analyzWithLLM(text, hasImage);
const llmResult = await analyzWithLLM(text, hasImage, historyMessages);
if (llmResult) {
console.log("[Smart Analyzer] LLM analysis result:", llmResult);
return llmResult;
@ -183,7 +196,11 @@ function quickKeywordMatch(text: string, hasImage: boolean): AnalysisResult {
/**
* 使用 LLM 进行深度分析
*/
async function analyzWithLLM(text: string, hasImage: boolean): Promise<AnalysisResult | null> {
async function analyzWithLLM(
text: string,
hasImage: boolean,
historyMessages?: HistoryMessage[]
): Promise<AnalysisResult | null> {
// 使用动态导入避免循环依赖问题
const { getEnabledAiModelsWithProvider } = await import("@/lib/db/queries");
const { getLanguageModelById } = await import("./dynamic-provider");
@ -205,12 +222,29 @@ async function analyzWithLLM(text: string, hasImage: boolean): Promise<AnalysisR
return null;
}
// 🆕 构建包含历史上下文的消息
let contextMessage = "";
// 添加历史对话上下文(最多取最近5轮对话)
if (historyMessages && historyMessages.length > 0) {
const recentHistory = historyMessages.slice(-10); // 最多10条消息(约5轮对话)
const historyText = recentHistory.map(msg =>
`${msg.role === 'user' ? '用户' : 'AI'}: ${msg.content.substring(0, 200)}`
).join('\n');
contextMessage = `## 对话历史(最近的对话上下文):
${historyText}
## 当前请求:
`;
}
// 构建用户消息
const userMessage = hasImage
? `用户上传了图片,请求内容:"${text}"`
: `用户请求(无图片):"${text}"`;
? `${contextMessage}用户上传了图片,请求内容:"${text}"`
: `${contextMessage}用户请求(无图片):"${text}"`;
console.log("[Smart Analyzer] Calling Qwen3 VL for analysis");
console.log("[Smart Analyzer] Calling Qwen3 VL for analysis with", historyMessages?.length || 0, "history messages");
// 调用模型分析
const { text: responseText } = await generateText({