diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 11770c7..d483b01 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -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:", { diff --git a/lib/ai/smart-analyzer.ts b/lib/ai/smart-analyzer.ts index a51365b..0f12cc2 100644 --- a/lib/ai/smart-analyzer.ts +++ b/lib/ai/smart-analyzer.ts @@ -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 { - 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 { +async function analyzWithLLM( + text: string, + hasImage: boolean, + historyMessages?: HistoryMessage[] +): Promise { // 使用动态导入避免循环依赖问题 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 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({