Skip to content

Commit 16beadc

Browse files
chat item soft delete (#6216)
* chat item soft delete * temp * fix * remove code * perf: delete chat item --------- Co-authored-by: archer <545436317@qq.com>
1 parent 166a176 commit 16beadc

File tree

12 files changed

+322
-121
lines changed

12 files changed

+322
-121
lines changed

packages/global/core/chat/type.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ export type ChatItemValueItemType =
130130
| AIChatItemValueItemType;
131131
export type ChatItemMergeType = UserChatItemType | SystemChatItemType | AIChatItemType;
132132

133-
export type ChatItemSchema = ChatItemMergeType & {
133+
export type ChatItemSchemaType = ChatItemMergeType & {
134134
dataId: string;
135135
chatId: string;
136136
userId: string;
137137
teamId: string;
138138
tmbId: string;
139139
appId: string;
140140
time: Date;
141+
deleteTime?: Date | null;
141142
};
142143

143144
export type AdminFbkType = {
@@ -171,6 +172,17 @@ export type ChatSiteItemType = ChatItemMergeType & {
171172
time?: Date;
172173
durationSeconds?: number;
173174
errorMsg?: string;
175+
deleteTime?: Date | null;
176+
collapseTop?: {
177+
count: number;
178+
dataIds: string[];
179+
isExpanded: boolean;
180+
};
181+
collapseBottom?: {
182+
count: number;
183+
dataIds: string[];
184+
isExpanded: boolean;
185+
};
174186
} & ChatBoxInputType &
175187
ResponseTagItemType;
176188

packages/service/core/chat/chatItemSchema.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { connectionMongo, getMongoModel } from '../../common/mongo';
22
const { Schema } = connectionMongo;
3-
import { type ChatItemSchema as ChatItemType } from '@fastgpt/global/core/chat/type';
3+
import { type ChatItemSchemaType } from '@fastgpt/global/core/chat/type';
44
import { ChatRoleMap } from '@fastgpt/global/core/chat/constants';
55
import { getNanoid } from '@fastgpt/global/common/string/tools';
66
import {
@@ -81,22 +81,29 @@ const ChatItemSchema = new Schema({
8181
}
8282
},
8383
isFeedbackRead: Boolean,
84+
deleteTime: {
85+
type: Date,
86+
default: null
87+
},
8488

8589
// @deprecated
8690
[DispatchNodeResponseKeyEnum.nodeResponse]: Array
8791
});
8892

89-
/*
90-
delete by app;
93+
/*
94+
delete by app;
9195
delete by chat id;
92-
get chat list;
93-
get chat logs;
94-
close custom feedback;
96+
close custom feedback;
9597
*/
9698
ChatItemSchema.index({ appId: 1, chatId: 1, dataId: 1 });
97-
// Anchor filter
99+
// Get histories
100+
ChatItemSchema.index({ appId: 1, chatId: 1, deleteTime: 1 });
101+
// get chatitem list,Anchor filter
98102
ChatItemSchema.index({ appId: 1, chatId: 1, _id: -1 });
99-
// timer, clear history
100-
ChatItemSchema.index({ teamId: 1, time: -1 });
103+
// Query by role (AI/Human), get latest chat item, permission check
104+
ChatItemSchema.index({ appId: 1, chatId: 1, obj: 1, _id: -1 });
101105

102-
export const MongoChatItem = getMongoModel<ChatItemType>(ChatItemCollectionName, ChatItemSchema);
106+
export const MongoChatItem = getMongoModel<ChatItemSchemaType>(
107+
ChatItemCollectionName,
108+
ChatItemSchema
109+
);

packages/service/core/chat/controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { mongoSessionRun } from '../../common/mongo/sessionRun';
1111
import { UserError } from '@fastgpt/global/common/error/utils';
1212

1313
export async function getChatItems({
14+
includeDeleted = false,
1415
appId,
1516
chatId,
1617
field,
@@ -21,6 +22,7 @@ export async function getChatItems({
2122
prevId,
2223
nextId
2324
}: {
25+
includeDeleted?: boolean;
2426
appId: string;
2527
chatId?: string;
2628
field: string;
@@ -42,8 +44,7 @@ export async function getChatItems({
4244

4345
// Extend dataId
4446
field = `dataId ${field}`;
45-
46-
const baseCondition = { appId, chatId };
47+
const baseCondition = includeDeleted ? { appId, chatId } : { appId, chatId, deleteTime: null };
4748

4849
const { histories, total, hasMorePrev, hasMoreNext } = await (async () => {
4950
// Mode 1: offset pagination (original logic)

packages/web/i18n/en/app.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"auto_execute_tip": "After turning it on, the workflow will be automatically triggered when the user enters the conversation interface. \nExecution order: 1. Dialogue starter; 2. Global variables; 3. Automatic execution.",
5959
"auto_save": "Auto save",
6060
"change_app_type": "Change App Type",
61+
"chat.collapse_deleted_items": "Collapse deleted records",
62+
"chat.expand_deleted_items": "Expand deleted records",
6163
"chat_debug": "Chat Preview",
6264
"chat_logs": "Logs",
6365
"chat_logs_tips": "Logs will record the online, shared, and API (requires chatId) conversation records of this app.",

packages/web/i18n/zh-CN/app.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
"auto_execute_tip": "开启后,用户进入对话界面将自动触发工作流。执行顺序:1、对话开场白;2、全局变量;3、自动执行。",
6161
"auto_save": "自动保存",
6262
"change_app_type": "更改应用类型",
63+
"chat.collapse_deleted_items": "收起已删除记录",
64+
"chat.expand_deleted_items": "展开已删除记录",
6365
"chat_debug": "调试预览",
6466
"chat_logs": "对话日志",
6567
"chat_logs_tips": "日志会记录该应用的在线、分享和 API(需填写 chatId)对话记录",

packages/web/i18n/zh-Hant/app.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"auto_execute_tip": "開啟後,使用者進入對話式介面將自動觸發工作流程。\n執行順序:1、對話開場白;2、全域變數;3、自動執行。",
5959
"auto_save": "自動儲存",
6060
"change_app_type": "更改應用程式類型",
61+
"chat.collapse_deleted_items": "收起已刪除記錄",
62+
"chat.expand_deleted_items": "展開已刪除記錄",
6163
"chat_debug": "聊天預覽",
6264
"chat_logs": "對話紀錄",
6365
"chat_logs_tips": "紀錄會記錄此應用程式的線上、分享和 API(需填寫 chatId)對話紀錄",

0 commit comments

Comments
 (0)