From a6a267e8192624cbbb5300b42acce8ebd4fa4a8c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Feb 2026 11:18:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8D=A0=E4=BD=8D?= =?UTF-8?q?=E5=9B=BE=E5=92=8C=E5=9B=BE=E7=89=87=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 移除 onFinish 中的数据库重新加载逻辑(时机不对,会打断图片生成流程) 2. 保留 appendedMessages 和 mergedMessages 合并逻辑 3. 修复 dynamic-provider.ts 的循环依赖(使用动态导入) 4. 不在 onFinish 中调用 completeGeneration(),因为图片可能还在生成 Co-authored-by: Cursor --- components/chat.tsx | 28 +++------------------------- lib/ai/dynamic-provider.ts | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/components/chat.tsx b/components/chat.tsx index 872f3c5..0231ece 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -356,33 +356,11 @@ export function Chat({ } } }, - onFinish: async () => { + onFinish: () => { mutate(unstable_serialize(getChatHistoryPaginationKey)); - // 🆕 生成完成,清除持久化状态 - completeGeneration(); console.log("[Chat] onFinish called"); - - // 🆕 从数据库重新加载消息,确保 data-appendMessage 的消息正确显示 - // 这是根治方案 B:后端已保存消息到数据库,前端从数据库重新加载 - try { - // 延迟一小段时间确保数据库写入完成 - await new Promise(resolve => setTimeout(resolve, 500)); - - // 获取最新消息 - const response = await fetch(`/api/messages?chatId=${id}`); - if (response.ok) { - const data = await response.json(); - if (data.messages && Array.isArray(data.messages)) { - console.log("[Chat] Reloading messages from database:", data.messages.length); - // 更新消息状态 - setMessages(data.messages); - // 清空 appendedMessages,因为已经从数据库加载了完整消息 - setAppendedMessages([]); - } - } - } catch (error) { - console.error("[Chat] Failed to reload messages:", error); - } + // 注意:不在这里清除 completeGeneration(),因为图片生成可能还在进行中 + // completeGeneration() 会在收到图片消息时调用 }, onError: (error) => { if (error instanceof ChatSDKError) { diff --git a/lib/ai/dynamic-provider.ts b/lib/ai/dynamic-provider.ts index b39a0ca..74e04ab 100644 --- a/lib/ai/dynamic-provider.ts +++ b/lib/ai/dynamic-provider.ts @@ -6,11 +6,12 @@ import { wrapLanguageModel, type LanguageModel, } from "ai"; -import { - getEnabledAiModelsWithProvider, - getAiProviderById, - getAiModelById, -} from "@/lib/db/queries"; +// 使用动态导入避免循环依赖问题 +// import { +// getEnabledAiModelsWithProvider, +// getAiProviderById, +// getAiModelById, +// } from "@/lib/db/queries"; import type { AiProvider } from "@/lib/db/schema"; import { createLocalQwen } from "./providers/local-qwen"; import { createGeminiImage } from "./providers/gemini-image"; @@ -143,6 +144,9 @@ export async function getDynamicModel( modelId: string ): Promise { try { + // 使用动态导入避免循环依赖 + const { getAiModelById, getAiProviderById } = await import("@/lib/db/queries"); + const model = await getAiModelById({ id: modelId }); if (!model || !model.isEnabled) { console.warn(`[getDynamicModel] Model ${modelId} not found or disabled`); @@ -238,6 +242,9 @@ export async function getLanguageModelById( // 获取所有可用的模型列表(用于前端展示) export async function getAvailableModels() { try { + // 使用动态导入避免循环依赖 + const { getEnabledAiModelsWithProvider } = await import("@/lib/db/queries"); + const modelsWithProvider = await getEnabledAiModelsWithProvider(); return modelsWithProvider.map(({ model, provider }) => ({ id: model.id, @@ -262,6 +269,9 @@ export async function getSystemModel( purpose: "title" | "artifact" ): Promise { try { + // 使用动态导入避免循环依赖 + const { getEnabledAiModelsWithProvider } = await import("@/lib/db/queries"); + // 首先尝试从数据库获取默认模型 const modelsWithProvider = await getEnabledAiModelsWithProvider();