From 72a9307eb3478a74dde8f5a947bb4b153eadf656 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Wed, 16 Aug 2023 16:14:37 +0800 Subject: [PATCH] fix: chat data outsize --- client/src/pages/account/index.tsx | 1 + client/src/service/response.ts | 4 +- client/src/service/utils/chat/saveChat.ts | 108 ++++++++++++---------- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/client/src/pages/account/index.tsx b/client/src/pages/account/index.tsx index a02d5a17b..f1f2e0ff3 100644 --- a/client/src/pages/account/index.tsx +++ b/client/src/pages/account/index.tsx @@ -11,6 +11,7 @@ import SideTabs from '@/components/SideTabs'; import Tabs from '@/components/Tabs'; import UserInfo from './components/Info'; import { serviceSideProps } from '@/utils/i18n'; +import { feConfigs } from '@/store/static'; const BillTable = dynamic(() => import('./components/BillTable'), { ssr: false diff --git a/client/src/service/response.ts b/client/src/service/response.ts index 576486f9d..59d13f78e 100644 --- a/client/src/service/response.ts +++ b/client/src/service/response.ts @@ -52,7 +52,7 @@ export const jsonRes = ( } else if (openaiError[error?.response?.statusText]) { msg = openaiError[error.response.statusText]; } - console.log(error); + console.log(error?.response); } res.status(code).json({ @@ -92,7 +92,7 @@ export const sseErrRes = (res: NextApiResponse, error: any) => { } else if (openaiError[error?.response?.statusText]) { msg = openaiError[error.response.statusText]; } - console.log('sse error => ', error); + console.log('sse error => ', error?.response); sseResponse({ res, diff --git a/client/src/service/utils/chat/saveChat.ts b/client/src/service/utils/chat/saveChat.ts index b63287b32..e29beabae 100644 --- a/client/src/service/utils/chat/saveChat.ts +++ b/client/src/service/utils/chat/saveChat.ts @@ -23,54 +23,68 @@ export async function saveChat({ shareId, content }: Props) { - const chatHistory = await Chat.findOne( - { - chatId, - userId - }, - '_id' - ); - - const promise = []; - - if (chatHistory) { - promise.push( - Chat.updateOne( - { chatId, userId }, - { - $push: { - content: { - $each: content, - $slice: -50 - } - }, - title: content[0].value.slice(0, 20), - updateTime: new Date() - } - ) - ); - } else { - promise.push( - Chat.create({ + try { + const chatHistory = await Chat.findOne( + { chatId, - userId, - appId, - variables, - title: content[0].value.slice(0, 20), - source, - shareId, - content: content - }) + userId + }, + '_id' + ); + + const promise = []; + + if (chatHistory) { + promise.push( + Chat.updateOne( + { chatId, userId }, + { + $push: { + content: { + $each: content, + $slice: -40 + } + }, + title: content[0].value.slice(0, 20), + updateTime: new Date() + } + ) + ); + } else { + promise.push( + Chat.create({ + chatId, + userId, + appId, + variables, + title: content[0].value.slice(0, 20), + source, + shareId, + content: content + }) + ); + } + + if (isOwner && source === ChatSourceEnum.online) { + promise.push( + App.findByIdAndUpdate(appId, { + updateTime: new Date() + }) + ); + } + + await Promise.all(promise); + } catch (error) { + Chat.updateOne( + { chatId, userId }, + { + $push: { + content: { + $each: [], + $slice: -10 + } + } + } ); } - - if (isOwner && source === ChatSourceEnum.online) { - promise.push( - App.findByIdAndUpdate(appId, { - updateTime: new Date() - }) - ); - } - - await Promise.all(promise); }