This commit is contained in:
Archer
2023-11-09 09:46:57 +08:00
committed by GitHub
parent 661ee79943
commit 8bb5588305
402 changed files with 9899 additions and 5967 deletions

View File

@@ -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);
}
}