@lobehub/chat 1.32.1 → 1.32.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 1.32.2](https://github.com/lobehub/lobe-chat/compare/v1.32.1...v1.32.2)
6
+
7
+ <sup>Released on **2024-11-19**</sup>
8
+
9
+ <br/>
10
+
11
+ <details>
12
+ <summary><kbd>Improvements and Fixes</kbd></summary>
13
+
14
+ </details>
15
+
16
+ <div align="right">
17
+
18
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
19
+
20
+ </div>
21
+
5
22
  ### [Version 1.32.1](https://github.com/lobehub/lobe-chat/compare/v1.32.0...v1.32.1)
6
23
 
7
24
  <sup>Released on **2024-11-19**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "1.32.1",
3
+ "version": "1.32.2",
4
4
  "description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
@@ -1,5 +1,6 @@
1
1
  import { asc, count, eq, ilike, inArray, notExists, or, sum } from 'drizzle-orm';
2
2
  import { and, desc, like } from 'drizzle-orm/expressions';
3
+ import type { PgTransaction } from 'drizzle-orm/pg-core';
3
4
 
4
5
  import { serverDBEnv } from '@/config/db';
5
6
  import { serverDB } from '@/database/server/core/db';
@@ -9,6 +10,9 @@ import {
9
10
  FileItem,
10
11
  NewFile,
11
12
  NewGlobalFile,
13
+ chunks,
14
+ embeddings,
15
+ fileChunks,
12
16
  files,
13
17
  globalFiles,
14
18
  knowledgeBaseFiles,
@@ -68,6 +72,10 @@ export class FileModel {
68
72
  const fileHash = file.fileHash!;
69
73
 
70
74
  return await serverDB.transaction(async (trx) => {
75
+ // 1. 删除相关的 chunks
76
+ await this.deleteFileChunks(trx as any, [id]);
77
+
78
+ // 2. 删除文件记录
71
79
  await trx.delete(files).where(and(eq(files.id, id), eq(files.userId, this.userId)));
72
80
 
73
81
  const result = await trx
@@ -107,6 +115,9 @@ export class FileModel {
107
115
  const hashList = fileList.map((file) => file.fileHash!);
108
116
 
109
117
  return await serverDB.transaction(async (trx) => {
118
+ // 1. 删除相关的 chunks
119
+ await this.deleteFileChunks(trx as any, ids);
120
+
110
121
  // delete the files
111
122
  await trx.delete(files).where(and(inArray(files.id, ids), eq(files.userId, this.userId)));
112
123
 
@@ -289,4 +300,30 @@ export class FileModel {
289
300
  ),
290
301
  });
291
302
  }
303
+
304
+ // 抽象出通用的删除 chunks 方法
305
+ private async deleteFileChunks(trx: PgTransaction<any>, fileIds: string[]) {
306
+ const BATCH_SIZE = 1000; // 每批处理的数量
307
+
308
+ // 1. 获取所有关联的 chunk IDs
309
+ const relatedChunks = await trx
310
+ .select({ chunkId: fileChunks.chunkId })
311
+ .from(fileChunks)
312
+ .where(inArray(fileChunks.fileId, fileIds));
313
+
314
+ const chunkIds = relatedChunks.map((c) => c.chunkId).filter(Boolean) as string[];
315
+
316
+ if (chunkIds.length === 0) return;
317
+
318
+ // 2. 分批处理删除
319
+ for (let i = 0; i < chunkIds.length; i += BATCH_SIZE) {
320
+ const batchChunkIds = chunkIds.slice(i, i + BATCH_SIZE);
321
+
322
+ await trx.delete(embeddings).where(inArray(embeddings.chunkId, batchChunkIds));
323
+
324
+ await trx.delete(chunks).where(inArray(chunks.id, batchChunkIds));
325
+ }
326
+
327
+ return chunkIds;
328
+ }
292
329
  }
@@ -154,8 +154,6 @@ export const fileRouter = router({
154
154
  removeFile: fileProcedure.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {
155
155
  const file = await ctx.fileModel.delete(input.id);
156
156
 
157
- // delete the orphan chunks
158
- await ctx.chunkModel.deleteOrphanChunks();
159
157
  if (!file) return;
160
158
 
161
159
  // delele the file from remove from S3 if it is not used by other files
@@ -187,9 +185,6 @@ export const fileRouter = router({
187
185
  .mutation(async ({ input, ctx }) => {
188
186
  const needToRemoveFileList = await ctx.fileModel.deleteMany(input.ids);
189
187
 
190
- // delete the orphan chunks
191
- await ctx.chunkModel.deleteOrphanChunks();
192
-
193
188
  if (!needToRemoveFileList || needToRemoveFileList.length === 0) return;
194
189
 
195
190
  // remove from S3