From 2fc31a706d9e4eabdd29e1f829a45028e20c665f Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 29 May 2023 22:51:09 +0800 Subject: [PATCH] feat: update model use time --- src/api/chat.ts | 2 +- .../api/chat/{ => history}/getHistoryQuote.ts | 0 src/pages/api/chat/saveChat.ts | 55 +++++++++++-------- src/pages/api/model/list.ts | 2 +- src/pages/chat/index.tsx | 47 +++++++++------- 5 files changed, 62 insertions(+), 44 deletions(-) rename src/pages/api/chat/{ => history}/getHistoryQuote.ts (100%) diff --git a/src/api/chat.ts b/src/api/chat.ts index e57a0253a..d47fa17b5 100644 --- a/src/api/chat.ts +++ b/src/api/chat.ts @@ -62,7 +62,7 @@ export const createShareChat = ( ) => POST(`/chat/shareChat/create`, data); /** - * get shareChat + * get shareChat */ export const getShareChatList = (modelId: string) => GET(`/chat/shareChat/list?modelId=${modelId}`); diff --git a/src/pages/api/chat/getHistoryQuote.ts b/src/pages/api/chat/history/getHistoryQuote.ts similarity index 100% rename from src/pages/api/chat/getHistoryQuote.ts rename to src/pages/api/chat/history/getHistoryQuote.ts diff --git a/src/pages/api/chat/saveChat.ts b/src/pages/api/chat/saveChat.ts index 19d1246d5..542a03ab6 100644 --- a/src/pages/api/chat/saveChat.ts +++ b/src/pages/api/chat/saveChat.ts @@ -1,7 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { jsonRes } from '@/service/response'; import { ChatItemType } from '@/types/chat'; -import { connectToDatabase, Chat } from '@/service/mongo'; +import { connectToDatabase, Chat, Model } from '@/service/mongo'; import { authModel } from '@/service/utils/auth'; import { authUser } from '@/service/utils/auth'; import mongoose from 'mongoose'; @@ -65,27 +65,36 @@ export async function saveChat({ })) || [] })); - // 没有 chatId, 创建一个对话 - if (!chatId) { - const { _id } = await Chat.create({ - _id: newChatId ? new mongoose.Types.ObjectId(newChatId) : undefined, - userId, - modelId, - content, - title: content[0].value.slice(0, 20), - latestChat: content[1].value - }); - return _id; - } else { - await Chat.findByIdAndUpdate(chatId, { - $push: { - content: { - $each: content - } - }, - title: content[0].value.slice(0, 20), - latestChat: content[1].value, + const [id] = await Promise.all([ + ...(chatId // update chat + ? [ + Chat.findByIdAndUpdate(chatId, { + $push: { + content: { + $each: content + } + }, + title: content[0].value.slice(0, 20), + latestChat: content[1].value, + updateTime: new Date() + }).then(() => '') + ] + : [ + Chat.create({ + _id: newChatId ? new mongoose.Types.ObjectId(newChatId) : undefined, + userId, + modelId, + content, + title: content[0].value.slice(0, 20), + latestChat: content[1].value + }).then((res) => res._id) + ]), + Model.findByIdAndUpdate(modelId, { updateTime: new Date() - }); - } + }) // update model + ]); + + return { + id + }; } diff --git a/src/pages/api/model/list.ts b/src/pages/api/model/list.ts index f097e60c2..6c5563b7e 100644 --- a/src/pages/api/model/list.ts +++ b/src/pages/api/model/list.ts @@ -20,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< }, '_id avatar name chat.systemPrompt' ).sort({ - _id: -1 + updateTime: -1 }), Collection.find({ userId }) .populate({ diff --git a/src/pages/chat/index.tsx b/src/pages/chat/index.tsx index 3e86d68b1..939435778 100644 --- a/src/pages/chat/index.tsx +++ b/src/pages/chat/index.tsx @@ -105,7 +105,7 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { const { copyData } = useCopyData(); const { isPc } = useGlobalStore(); const { Loading, setIsLoading } = useLoading(); - const { userInfo } = useUserStore(); + const { userInfo, loadMyModels } = useUserStore(); const { isOpen: isOpenSlider, onClose: onCloseSlider, onOpen: onOpenSlider } = useDisclosure(); // close contextMenu @@ -228,13 +228,21 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { }) })); - // refresh history - setTimeout(() => { - loadHistory({ pageNum: 1, init: true }); - generatingMessage(); - }, 100); + // refresh data + loadHistory({ pageNum: 1, init: true }); + loadMyModels(true); + generatingMessage(); }, - [chatId, setForbidLoadChatData, generatingMessage, loadHistory, modelId, router, setChatData] + [ + chatId, + modelId, + setChatData, + loadHistory, + loadMyModels, + generatingMessage, + setForbidLoadChatData, + router + ] ); /** @@ -458,14 +466,14 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { async ({ modelId, chatId, - isLoading = false + loading = false }: { modelId: string; chatId: string; - isLoading?: boolean; + loading?: boolean; }) => { - isLoading && setIsLoading(true); try { + loading && setIsLoading(true); const res = await getInitChatSiteInfo(modelId, chatId); setChatData({ @@ -500,18 +508,18 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { return null; }, [ - router, - loadHistory, - setForbidLoadChatData, - scrollToBottom, - setChatData, setIsLoading, + setChatData, + scrollToBottom, + setForbidLoadChatData, + router, + setLastChatModelId, setLastChatId, - setLastChatModelId + loadHistory ] ); // 初始化聊天框 - const { isLoading } = useQuery(['init', modelId, chatId], () => { + useQuery(['init', modelId, chatId], () => { // pc: redirect to latest model chat if (!modelId && lastChatModelId) { router.replace(`/chat?modelId=${lastChatModelId}&chatId=${lastChatId}`); @@ -529,7 +537,8 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { return loadChatInfo({ modelId, - chatId + chatId, + loading: true }); }); @@ -874,7 +883,7 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { )} - + )}