mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-02 20:58:12 +00:00
v4.6 -1 (#459)
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import { ChatItemType } from '@/types/chat';
|
||||
import { Chat, App, ChatItem } from '@/service/mongo';
|
||||
import { ChatSourceEnum } from '@/constants/chat';
|
||||
import type { ChatItemType } from '@fastgpt/global/core/chat/type.d';
|
||||
import { MongoApp } from '@fastgpt/service/core/app/schema';
|
||||
import { ChatSourceEnum } from '@fastgpt/global/core/chat/constants';
|
||||
import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
|
||||
import { MongoChat } from '@fastgpt/service/core/chat/chatSchema';
|
||||
import { addLog } from '@fastgpt/service/common/mongo/controller';
|
||||
|
||||
type Props = {
|
||||
chatId: string;
|
||||
appId: string;
|
||||
userId: string;
|
||||
teamId: string;
|
||||
tmbId: string;
|
||||
variables?: Record<string, any>;
|
||||
isOwner: boolean;
|
||||
source: `${ChatSourceEnum}`;
|
||||
@@ -16,7 +20,8 @@ type Props = {
|
||||
export async function saveChat({
|
||||
chatId,
|
||||
appId,
|
||||
userId,
|
||||
teamId,
|
||||
tmbId,
|
||||
variables,
|
||||
isOwner,
|
||||
source,
|
||||
@@ -24,20 +29,22 @@ export async function saveChat({
|
||||
content
|
||||
}: Props) {
|
||||
try {
|
||||
const chatHistory = await Chat.findOne(
|
||||
const chatHistory = await MongoChat.findOne(
|
||||
{
|
||||
chatId,
|
||||
userId,
|
||||
teamId,
|
||||
tmbId,
|
||||
appId
|
||||
},
|
||||
'_id'
|
||||
);
|
||||
|
||||
const promise: any[] = [
|
||||
ChatItem.insertMany(
|
||||
MongoChatItem.insertMany(
|
||||
content.map((item) => ({
|
||||
chatId,
|
||||
userId,
|
||||
teamId,
|
||||
tmbId,
|
||||
appId,
|
||||
...item
|
||||
}))
|
||||
@@ -46,8 +53,8 @@ export async function saveChat({
|
||||
|
||||
if (chatHistory) {
|
||||
promise.push(
|
||||
Chat.updateOne(
|
||||
{ chatId, userId, appId },
|
||||
MongoChat.updateOne(
|
||||
{ chatId },
|
||||
{
|
||||
title: content[0].value.slice(0, 20),
|
||||
updateTime: new Date()
|
||||
@@ -56,9 +63,10 @@ export async function saveChat({
|
||||
);
|
||||
} else {
|
||||
promise.push(
|
||||
Chat.create({
|
||||
MongoChat.create({
|
||||
chatId,
|
||||
userId,
|
||||
teamId,
|
||||
tmbId,
|
||||
appId,
|
||||
variables,
|
||||
title: content[0].value.slice(0, 20),
|
||||
@@ -70,7 +78,7 @@ export async function saveChat({
|
||||
|
||||
if (isOwner && source === ChatSourceEnum.online) {
|
||||
promise.push(
|
||||
App.findByIdAndUpdate(appId, {
|
||||
MongoApp.findByIdAndUpdate(appId, {
|
||||
updateTime: new Date()
|
||||
})
|
||||
);
|
||||
@@ -78,16 +86,6 @@ export async function saveChat({
|
||||
|
||||
await Promise.all(promise);
|
||||
} catch (error) {
|
||||
Chat.updateOne(
|
||||
{ chatId, userId },
|
||||
{
|
||||
$push: {
|
||||
content: {
|
||||
$each: [],
|
||||
$slice: -10
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
addLog.error(`update chat history error`, error);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user