diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 60879a0..e59e631 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -205,6 +205,42 @@ function injectHistoryImagesToLastUserMessage( return messages.map((msg, idx) => (idx === lastUserIndex ? updatedMessage : msg)); } +function compactAutoDemand(text: string, maxLength = 56): string { + const normalized = text.replace(/\s+/g, " ").trim(); + if (!normalized) return "按你刚才的描述执行"; + if (normalized.length <= maxLength) return normalized; + return `${normalized.slice(0, maxLength)}...`; +} + +function buildAutoModeFallbackMessage(params: { + text: string; + hasImage: boolean; + fromHistory: boolean; +}): string { + const demand = compactAutoDemand(params.text); + + if (params.hasImage) { + const sourceLine = params.fromHistory + ? "我会基于你上一轮图片继续处理。" + : "我会基于你当前上传图片处理。"; + + return [ + "收到,我先帮你梳理这次修改目标。", + `- 需求理解:${demand}`, + `- 执行基准:${sourceLine}`, + "- 执行方式:优先只改你指定区域,主体、构图和风格尽量保持一致。", + "- 继续迭代:生成后你可直接说“保留A,改B”,我会按最新要求继续优化。", + ].join("\n"); + } + + return [ + "收到,我来按自动模式帮你生成。", + `- 需求理解:${demand}`, + "- 执行方式:先锁定主体和场景,再细化风格、材质与光影。", + "- 继续迭代:你可以继续补充偏好(如构图、镜头、色调、质感)。", + ].join("\n"); +} + /** * 自定义消息转换函数 * 注意:gemini-image 和 huanke-gemini 都需要 URL 格式(不是 Base64) @@ -492,6 +528,9 @@ export async function POST(request: Request) { // ========== Smart Auto Mode Logic ========== if (finalModelId === "auto") { console.log("[Smart Auto Mode] 🚀 Starting intelligent analysis..."); + let autoTextForFallback = ""; + let autoHasImageForFallback = false; + let autoFromHistoryForFallback = false; try { // 🆕 首先获取历史消息(在 Smart Auto Mode 内部单独获取,避免依赖后面的 messagesFromDb) @@ -504,12 +543,14 @@ export async function POST(request: Request) { .filter((p): p is { type: "text"; text: string } => p.type === "text") .map((p) => p.text) .join(" "); + autoTextForFallback = textContent; const autoContextText = typeof autoContext === "string" ? autoContext.trim() : ""; // @ts-ignore - 检查各种可能的图片类型 let hasImage = lastUserMessage.parts.some( (p: any) => p.type === "file" || p.type === "image" || p.type === "image_url" ); + autoHasImageForFallback = hasImage; // @ts-ignore - 提取图片URL let imageUrls = lastUserMessage.parts @@ -570,6 +611,8 @@ export async function POST(request: Request) { if (smartAutoHistoryImages.length > 0) { hasImage = true; imageUrls = smartAutoHistoryImages; + autoHasImageForFallback = true; + autoFromHistoryForFallback = true; console.log("[Smart Auto Mode] ✅ Auto-using", smartAutoHistoryImages.length, "history image(s) for context continuity"); } else { console.log("[Smart Auto Mode] ℹ️ No image found in history, proceeding as pure text"); @@ -624,6 +667,29 @@ export async function POST(request: Request) { console.log("[Smart Auto Mode] 💬 Conversational reply:", smartAutoFriendlyMessage); } + const autoIntent = autoGraphResult.analysis?.intent; + const isImageTask = + autoIntent === "image_gen" || + autoIntent === "image_edit" || + hasImage; + const needsRicherAutoReply = + isImageTask && + !smartAutoNeedsClarification && + ( + !smartAutoFriendlyMessage || + !smartAutoFriendlyMessage.includes("\n") || + smartAutoFriendlyMessage.replace(/\s+/g, "").length < 20 + ); + + if (needsRicherAutoReply) { + smartAutoFriendlyMessage = buildAutoModeFallbackMessage({ + text: autoTextForFallback, + hasImage, + fromHistory: smartAutoHistoryImages.length > 0, + }); + console.log("[Smart Auto Mode] 🧩 Upgraded to richer conversational reply"); + } + if (autoGraphResult.optimizedPrompt) { smartAutoOptimizedPrompt = autoGraphResult.optimizedPrompt; console.log("[Smart Auto Mode] ✨ Prompt optimized (LLM):", { @@ -643,7 +709,7 @@ export async function POST(request: Request) { const allModels = await getEnabledAiModelsWithProvider(); // @ts-ignore - 检查各种可能的图片类型 - const hasImage = message.parts.some( + const hasImage = autoHasImageForFallback || message.parts.some( (p: any) => p.type === "file" || p.type === "image" || p.type === "image_url" ); @@ -651,8 +717,18 @@ export async function POST(request: Request) { let bestModel = null; if (hasImage) { bestModel = allModels.find(m => m.model.supportsImageGen && !m.model.isSubModel); - // 🆕 回退时也生成一个友好消息 - smartAutoFriendlyMessage = "好的,我来帮你处理这张图片~"; + // 回退时也保持结构化、可感知的自动模式反馈 + smartAutoFriendlyMessage = buildAutoModeFallbackMessage({ + text: autoTextForFallback, + hasImage: true, + fromHistory: autoFromHistoryForFallback || smartAutoHistoryImages.length > 0, + }); + } else { + smartAutoFriendlyMessage = buildAutoModeFallbackMessage({ + text: autoTextForFallback, + hasImage: false, + fromHistory: false, + }); } if (!bestModel) { bestModel = allModels.find(m => m.model.isDefault && !m.model.isSubModel);