fix: 修复占位图和图片显示问题
1. 移除 onFinish 中的数据库重新加载逻辑(时机不对,会打断图片生成流程) 2. 保留 appendedMessages 和 mergedMessages 合并逻辑 3. 修复 dynamic-provider.ts 的循环依赖(使用动态导入) 4. 不在 onFinish 中调用 completeGeneration(),因为图片可能还在生成 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d6073ec091
commit
a6a267e819
@ -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) {
|
||||
|
||||
@ -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<LanguageModel | null> {
|
||||
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<LanguageModel | null> {
|
||||
try {
|
||||
// 使用动态导入避免循环依赖
|
||||
const { getEnabledAiModelsWithProvider } = await import("@/lib/db/queries");
|
||||
|
||||
// 首先尝试从数据库获取默认模型
|
||||
const modelsWithProvider = await getEnabledAiModelsWithProvider();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user