@lobehub/chat 1.35.9 → 1.35.11
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 +42 -0
- package/changelog/v1.json +14 -0
- package/package.json +2 -2
- package/src/app/(main)/repos/[id]/@menu/default.tsx +2 -1
- package/src/app/(main)/repos/[id]/page.tsx +2 -1
- package/src/database/schemas/topic.ts +3 -1
- package/src/database/server/core/dbForTest.ts +4 -6
- package/src/database/server/models/__tests__/_test_template.ts +3 -9
- package/src/database/server/models/__tests__/agent.test.ts +2 -8
- package/src/database/server/models/__tests__/asyncTask.test.ts +1 -7
- package/src/database/server/models/__tests__/chunk.test.ts +155 -16
- package/src/database/server/models/__tests__/file.test.ts +123 -15
- package/src/database/server/models/__tests__/knowledgeBase.test.ts +6 -12
- package/src/database/server/models/__tests__/message.test.ts +230 -7
- package/src/database/server/models/__tests__/nextauth.test.ts +1 -7
- package/src/database/server/models/__tests__/plugin.test.ts +1 -7
- package/src/database/server/models/__tests__/session.test.ts +169 -11
- package/src/database/server/models/__tests__/sessionGroup.test.ts +2 -8
- package/src/database/server/models/__tests__/topic.test.ts +1 -7
- package/src/database/server/models/__tests__/user.test.ts +55 -20
- package/src/database/server/models/_template.ts +10 -8
- package/src/database/server/models/agent.ts +17 -13
- package/src/database/server/models/asyncTask.ts +11 -9
- package/src/database/server/models/chunk.ts +19 -14
- package/src/database/server/models/embedding.ts +10 -8
- package/src/database/server/models/file.ts +19 -17
- package/src/database/server/models/knowledgeBase.ts +14 -12
- package/src/database/server/models/message.ts +36 -34
- package/src/database/server/models/plugin.ts +10 -8
- package/src/database/server/models/session.ts +23 -64
- package/src/database/server/models/sessionGroup.ts +11 -9
- package/src/database/server/models/thread.ts +11 -9
- package/src/database/server/models/topic.ts +19 -22
- package/src/database/server/models/user.ts +96 -84
- package/src/database/type.ts +7 -0
- package/src/libs/next-auth/adapter/index.ts +10 -10
- package/src/libs/trpc/async/asyncAuth.ts +2 -1
- package/src/server/routers/async/file.ts +5 -4
- package/src/server/routers/async/ragEval.ts +4 -3
- package/src/server/routers/lambda/_template.ts +2 -1
- package/src/server/routers/lambda/agent.ts +6 -5
- package/src/server/routers/lambda/chunk.ts +5 -5
- package/src/server/routers/lambda/file.ts +4 -3
- package/src/server/routers/lambda/knowledgeBase.ts +2 -1
- package/src/server/routers/lambda/message.ts +4 -2
- package/src/server/routers/lambda/plugin.ts +4 -2
- package/src/server/routers/lambda/ragEval.ts +2 -1
- package/src/server/routers/lambda/session.ts +4 -3
- package/src/server/routers/lambda/sessionGroup.ts +2 -1
- package/src/server/routers/lambda/thread.ts +3 -2
- package/src/server/routers/lambda/topic.ts +4 -2
- package/src/server/routers/lambda/user.ts +10 -9
- package/src/server/services/chunk/index.ts +3 -2
- package/src/server/services/nextAuthUser/index.ts +3 -3
- package/src/server/services/user/index.ts +7 -6
@@ -3,7 +3,7 @@ import { and, desc, like } from 'drizzle-orm/expressions';
|
|
3
3
|
import type { PgTransaction } from 'drizzle-orm/pg-core';
|
4
4
|
|
5
5
|
import { serverDBEnv } from '@/config/db';
|
6
|
-
import {
|
6
|
+
import { LobeChatDatabase } from '@/database/type';
|
7
7
|
import { FilesTabs, QueryFileListParams, SortType } from '@/types/files';
|
8
8
|
|
9
9
|
import {
|
@@ -20,13 +20,15 @@ import {
|
|
20
20
|
|
21
21
|
export class FileModel {
|
22
22
|
private readonly userId: string;
|
23
|
+
private db: LobeChatDatabase;
|
23
24
|
|
24
|
-
constructor(userId: string) {
|
25
|
+
constructor(db: LobeChatDatabase, userId: string) {
|
25
26
|
this.userId = userId;
|
27
|
+
this.db = db;
|
26
28
|
}
|
27
29
|
|
28
30
|
create = async (params: Omit<NewFile, 'id' | 'userId'> & { knowledgeBaseId?: string }) => {
|
29
|
-
const result = await
|
31
|
+
const result = await this.db.transaction(async (trx) => {
|
30
32
|
const result = await trx
|
31
33
|
.insert(files)
|
32
34
|
.values({ ...params, userId: this.userId })
|
@@ -47,11 +49,11 @@ export class FileModel {
|
|
47
49
|
};
|
48
50
|
|
49
51
|
createGlobalFile = async (file: Omit<NewGlobalFile, 'id' | 'userId'>) => {
|
50
|
-
return
|
52
|
+
return this.db.insert(globalFiles).values(file).returning();
|
51
53
|
};
|
52
54
|
|
53
55
|
checkHash = async (hash: string) => {
|
54
|
-
const item = await
|
56
|
+
const item = await this.db.query.globalFiles.findFirst({
|
55
57
|
where: eq(globalFiles.hashId, hash),
|
56
58
|
});
|
57
59
|
if (!item) return { isExist: false };
|
@@ -71,7 +73,7 @@ export class FileModel {
|
|
71
73
|
|
72
74
|
const fileHash = file.fileHash!;
|
73
75
|
|
74
|
-
return await
|
76
|
+
return await this.db.transaction(async (trx) => {
|
75
77
|
// 1. 删除相关的 chunks
|
76
78
|
await this.deleteFileChunks(trx as any, [id]);
|
77
79
|
|
@@ -96,11 +98,11 @@ export class FileModel {
|
|
96
98
|
};
|
97
99
|
|
98
100
|
deleteGlobalFile = async (hashId: string) => {
|
99
|
-
return
|
101
|
+
return this.db.delete(globalFiles).where(eq(globalFiles.hashId, hashId));
|
100
102
|
};
|
101
103
|
|
102
104
|
countUsage = async () => {
|
103
|
-
const result = await
|
105
|
+
const result = await this.db
|
104
106
|
.select({
|
105
107
|
totalSize: sum(files.size),
|
106
108
|
})
|
@@ -114,7 +116,7 @@ export class FileModel {
|
|
114
116
|
const fileList = await this.findByIds(ids);
|
115
117
|
const hashList = fileList.map((file) => file.fileHash!);
|
116
118
|
|
117
|
-
return await
|
119
|
+
return await this.db.transaction(async (trx) => {
|
118
120
|
// 1. 删除相关的 chunks
|
119
121
|
await this.deleteFileChunks(trx as any, ids);
|
120
122
|
|
@@ -159,7 +161,7 @@ export class FileModel {
|
|
159
161
|
};
|
160
162
|
|
161
163
|
clear = async () => {
|
162
|
-
return
|
164
|
+
return this.db.delete(files).where(eq(files.userId, this.userId));
|
163
165
|
};
|
164
166
|
|
165
167
|
query = async ({
|
@@ -198,7 +200,7 @@ export class FileModel {
|
|
198
200
|
}
|
199
201
|
|
200
202
|
// 3. build query
|
201
|
-
let query =
|
203
|
+
let query = this.db
|
202
204
|
.select({
|
203
205
|
chunkTaskId: files.chunkTaskId,
|
204
206
|
createdAt: files.createdAt,
|
@@ -230,7 +232,7 @@ export class FileModel {
|
|
230
232
|
whereClause = and(
|
231
233
|
whereClause,
|
232
234
|
notExists(
|
233
|
-
|
235
|
+
this.db.select().from(knowledgeBaseFiles).where(eq(knowledgeBaseFiles.fileId, files.id)),
|
234
236
|
),
|
235
237
|
);
|
236
238
|
}
|
@@ -240,19 +242,19 @@ export class FileModel {
|
|
240
242
|
};
|
241
243
|
|
242
244
|
findByIds = async (ids: string[]) => {
|
243
|
-
return
|
245
|
+
return this.db.query.files.findMany({
|
244
246
|
where: and(inArray(files.id, ids), eq(files.userId, this.userId)),
|
245
247
|
});
|
246
248
|
};
|
247
249
|
|
248
250
|
findById = async (id: string) => {
|
249
|
-
return
|
251
|
+
return this.db.query.files.findFirst({
|
250
252
|
where: and(eq(files.id, id), eq(files.userId, this.userId)),
|
251
253
|
});
|
252
254
|
};
|
253
255
|
|
254
256
|
countFilesByHash = async (hash: string) => {
|
255
|
-
const result = await
|
257
|
+
const result = await this.db
|
256
258
|
.select({
|
257
259
|
count: count(),
|
258
260
|
})
|
@@ -263,7 +265,7 @@ export class FileModel {
|
|
263
265
|
};
|
264
266
|
|
265
267
|
async update(id: string, value: Partial<FileItem>) {
|
266
|
-
return
|
268
|
+
return this.db
|
267
269
|
.update(files)
|
268
270
|
.set({ ...value, updatedAt: new Date() })
|
269
271
|
.where(and(eq(files.id, id), eq(files.userId, this.userId)));
|
@@ -293,7 +295,7 @@ export class FileModel {
|
|
293
295
|
};
|
294
296
|
|
295
297
|
async findByNames(fileNames: string[]) {
|
296
|
-
return
|
298
|
+
return this.db.query.files.findMany({
|
297
299
|
where: and(
|
298
300
|
or(...fileNames.map((name) => like(files.name, `${name}%`))),
|
299
301
|
eq(files.userId, this.userId),
|
@@ -1,22 +1,24 @@
|
|
1
1
|
import { eq, inArray } from 'drizzle-orm';
|
2
2
|
import { and, desc } from 'drizzle-orm/expressions';
|
3
3
|
|
4
|
-
import {
|
4
|
+
import { LobeChatDatabase } from '@/database/type';
|
5
5
|
import { KnowledgeBaseItem } from '@/types/knowledgeBase';
|
6
6
|
|
7
7
|
import { NewKnowledgeBase, knowledgeBaseFiles, knowledgeBases } from '../../schemas';
|
8
8
|
|
9
9
|
export class KnowledgeBaseModel {
|
10
10
|
private userId: string;
|
11
|
+
private db: LobeChatDatabase;
|
11
12
|
|
12
|
-
constructor(userId: string) {
|
13
|
+
constructor(db: LobeChatDatabase, userId: string) {
|
13
14
|
this.userId = userId;
|
15
|
+
this.db = db;
|
14
16
|
}
|
15
17
|
|
16
18
|
// create
|
17
19
|
|
18
20
|
create = async (params: Omit<NewKnowledgeBase, 'userId'>) => {
|
19
|
-
const [result] = await
|
21
|
+
const [result] = await this.db
|
20
22
|
.insert(knowledgeBases)
|
21
23
|
.values({ ...params, userId: this.userId })
|
22
24
|
.returning();
|
@@ -25,7 +27,7 @@ export class KnowledgeBaseModel {
|
|
25
27
|
};
|
26
28
|
|
27
29
|
addFilesToKnowledgeBase = async (id: string, fileIds: string[]) => {
|
28
|
-
return
|
30
|
+
return this.db
|
29
31
|
.insert(knowledgeBaseFiles)
|
30
32
|
.values(fileIds.map((fileId) => ({ fileId, knowledgeBaseId: id, userId: this.userId })))
|
31
33
|
.returning();
|
@@ -33,17 +35,17 @@ export class KnowledgeBaseModel {
|
|
33
35
|
|
34
36
|
// delete
|
35
37
|
delete = async (id: string) => {
|
36
|
-
return
|
38
|
+
return this.db
|
37
39
|
.delete(knowledgeBases)
|
38
40
|
.where(and(eq(knowledgeBases.id, id), eq(knowledgeBases.userId, this.userId)));
|
39
41
|
};
|
40
42
|
|
41
43
|
deleteAll = async () => {
|
42
|
-
return
|
44
|
+
return this.db.delete(knowledgeBases).where(eq(knowledgeBases.userId, this.userId));
|
43
45
|
};
|
44
46
|
|
45
47
|
removeFilesFromKnowledgeBase = async (knowledgeBaseId: string, ids: string[]) => {
|
46
|
-
return
|
48
|
+
return this.db.delete(knowledgeBaseFiles).where(
|
47
49
|
and(
|
48
50
|
eq(knowledgeBaseFiles.knowledgeBaseId, knowledgeBaseId),
|
49
51
|
inArray(knowledgeBaseFiles.fileId, ids),
|
@@ -53,7 +55,7 @@ export class KnowledgeBaseModel {
|
|
53
55
|
};
|
54
56
|
// query
|
55
57
|
query = async () => {
|
56
|
-
const data = await
|
58
|
+
const data = await this.db
|
57
59
|
.select({
|
58
60
|
avatar: knowledgeBases.avatar,
|
59
61
|
createdAt: knowledgeBases.createdAt,
|
@@ -73,21 +75,21 @@ export class KnowledgeBaseModel {
|
|
73
75
|
};
|
74
76
|
|
75
77
|
findById = async (id: string) => {
|
76
|
-
return
|
78
|
+
return this.db.query.knowledgeBases.findFirst({
|
77
79
|
where: and(eq(knowledgeBases.id, id), eq(knowledgeBases.userId, this.userId)),
|
78
80
|
});
|
79
81
|
};
|
80
82
|
|
81
83
|
// update
|
82
84
|
async update(id: string, value: Partial<KnowledgeBaseItem>) {
|
83
|
-
return
|
85
|
+
return this.db
|
84
86
|
.update(knowledgeBases)
|
85
87
|
.set({ ...value, updatedAt: new Date() })
|
86
88
|
.where(and(eq(knowledgeBases.id, id), eq(knowledgeBases.userId, this.userId)));
|
87
89
|
}
|
88
90
|
|
89
|
-
static async findById(id: string) {
|
90
|
-
return
|
91
|
+
static async findById(db: LobeChatDatabase, id: string) {
|
92
|
+
return db.query.knowledgeBases.findFirst({
|
91
93
|
where: eq(knowledgeBases.id, id),
|
92
94
|
});
|
93
95
|
}
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { count } from 'drizzle-orm';
|
2
2
|
import { and, asc, desc, eq, gte, inArray, isNull, like, lt } from 'drizzle-orm/expressions';
|
3
3
|
|
4
|
-
import { serverDB } from '@/database/server/core/db';
|
5
4
|
import { idGenerator } from '@/database/utils/idGenerator';
|
5
|
+
import { LobeChatDatabase } from '@/database/type';
|
6
6
|
import { getFullFileUrl } from '@/server/utils/files';
|
7
7
|
import {
|
8
8
|
ChatFileItem,
|
@@ -39,9 +39,11 @@ export interface QueryMessageParams {
|
|
39
39
|
|
40
40
|
export class MessageModel {
|
41
41
|
private userId: string;
|
42
|
+
private db: LobeChatDatabase;
|
42
43
|
|
43
|
-
constructor(userId: string) {
|
44
|
+
constructor(db: LobeChatDatabase, userId: string) {
|
44
45
|
this.userId = userId;
|
46
|
+
this.db = db;
|
45
47
|
}
|
46
48
|
|
47
49
|
// **************** Query *************** //
|
@@ -54,7 +56,7 @@ export class MessageModel {
|
|
54
56
|
const offset = current * pageSize;
|
55
57
|
|
56
58
|
// 1. get basic messages
|
57
|
-
const result = await
|
59
|
+
const result = await this.db
|
58
60
|
.select({
|
59
61
|
/* eslint-disable sort-keys-fix/sort-keys-fix*/
|
60
62
|
id: messages.id,
|
@@ -115,7 +117,7 @@ export class MessageModel {
|
|
115
117
|
if (messageIds.length === 0) return [];
|
116
118
|
|
117
119
|
// 2. get relative files
|
118
|
-
const rawRelatedFileList = await
|
120
|
+
const rawRelatedFileList = await this.db
|
119
121
|
.select({
|
120
122
|
fileType: files.fileType,
|
121
123
|
id: messagesFiles.fileId,
|
@@ -139,7 +141,7 @@ export class MessageModel {
|
|
139
141
|
const fileList = relatedFileList.filter((i) => !(i.fileType || '').startsWith('image'));
|
140
142
|
|
141
143
|
// 3. get relative file chunks
|
142
|
-
const chunksList = await
|
144
|
+
const chunksList = await this.db
|
143
145
|
.select({
|
144
146
|
fileId: files.id,
|
145
147
|
fileType: files.fileType,
|
@@ -157,7 +159,7 @@ export class MessageModel {
|
|
157
159
|
.where(inArray(messageQueryChunks.messageId, messageIds));
|
158
160
|
|
159
161
|
// 3. get relative message query
|
160
|
-
const messageQueriesList = await
|
162
|
+
const messageQueriesList = await this.db
|
161
163
|
.select({
|
162
164
|
id: messageQueries.id,
|
163
165
|
messageId: messageQueries.messageId,
|
@@ -216,13 +218,13 @@ export class MessageModel {
|
|
216
218
|
}
|
217
219
|
|
218
220
|
async findById(id: string) {
|
219
|
-
return
|
221
|
+
return this.db.query.messages.findFirst({
|
220
222
|
where: and(eq(messages.id, id), eq(messages.userId, this.userId)),
|
221
223
|
});
|
222
224
|
}
|
223
225
|
|
224
226
|
async findMessageQueriesById(messageId: string) {
|
225
|
-
const result = await
|
227
|
+
const result = await this.db
|
226
228
|
.select({
|
227
229
|
embeddings: embeddings.embeddings,
|
228
230
|
id: messageQueries.id,
|
@@ -240,7 +242,7 @@ export class MessageModel {
|
|
240
242
|
}
|
241
243
|
|
242
244
|
async queryAll(): Promise<MessageItem[]> {
|
243
|
-
return
|
245
|
+
return this.db
|
244
246
|
.select()
|
245
247
|
.from(messages)
|
246
248
|
.orderBy(messages.createdAt)
|
@@ -250,7 +252,7 @@ export class MessageModel {
|
|
250
252
|
}
|
251
253
|
|
252
254
|
async queryBySessionId(sessionId?: string | null): Promise<MessageItem[]> {
|
253
|
-
return
|
255
|
+
return this.db.query.messages.findMany({
|
254
256
|
orderBy: [asc(messages.createdAt)],
|
255
257
|
where: and(eq(messages.userId, this.userId), this.matchSession(sessionId)),
|
256
258
|
});
|
@@ -259,14 +261,14 @@ export class MessageModel {
|
|
259
261
|
async queryByKeyword(keyword: string): Promise<MessageItem[]> {
|
260
262
|
if (!keyword) return [];
|
261
263
|
|
262
|
-
return
|
264
|
+
return this.db.query.messages.findMany({
|
263
265
|
orderBy: [desc(messages.createdAt)],
|
264
266
|
where: and(eq(messages.userId, this.userId), like(messages.content, `%${keyword}%`)),
|
265
267
|
});
|
266
268
|
}
|
267
269
|
|
268
270
|
async count() {
|
269
|
-
const result = await
|
271
|
+
const result = await this.db
|
270
272
|
.select({
|
271
273
|
count: count(),
|
272
274
|
})
|
@@ -283,7 +285,7 @@ export class MessageModel {
|
|
283
285
|
const tomorrow = new Date(today);
|
284
286
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
285
287
|
|
286
|
-
const result = await
|
288
|
+
const result = await this.db
|
287
289
|
.select({
|
288
290
|
count: count(),
|
289
291
|
})
|
@@ -315,7 +317,7 @@ export class MessageModel {
|
|
315
317
|
}: CreateMessageParams,
|
316
318
|
id: string = this.genId(),
|
317
319
|
): Promise<MessageItem> {
|
318
|
-
return
|
320
|
+
return this.db.transaction(async (trx) => {
|
319
321
|
const [item] = (await trx
|
320
322
|
.insert(messages)
|
321
323
|
.values({
|
@@ -366,72 +368,72 @@ export class MessageModel {
|
|
366
368
|
return { ...m, userId: this.userId };
|
367
369
|
});
|
368
370
|
|
369
|
-
return
|
371
|
+
return this.db.insert(messages).values(messagesToInsert);
|
370
372
|
}
|
371
373
|
|
372
374
|
async createMessageQuery(params: NewMessageQuery) {
|
373
|
-
const result = await
|
375
|
+
const result = await this.db.insert(messageQueries).values(params).returning();
|
374
376
|
|
375
377
|
return result[0];
|
376
378
|
}
|
377
379
|
// **************** Update *************** //
|
378
380
|
|
379
381
|
async update(id: string, message: Partial<MessageItem>) {
|
380
|
-
return
|
382
|
+
return this.db
|
381
383
|
.update(messages)
|
382
384
|
.set(message)
|
383
385
|
.where(and(eq(messages.id, id), eq(messages.userId, this.userId)));
|
384
386
|
}
|
385
387
|
|
386
388
|
async updatePluginState(id: string, state: Record<string, any>) {
|
387
|
-
const item = await
|
389
|
+
const item = await this.db.query.messagePlugins.findFirst({
|
388
390
|
where: eq(messagePlugins.id, id),
|
389
391
|
});
|
390
392
|
if (!item) throw new Error('Plugin not found');
|
391
393
|
|
392
|
-
return
|
394
|
+
return this.db
|
393
395
|
.update(messagePlugins)
|
394
396
|
.set({ state: merge(item.state || {}, state) })
|
395
397
|
.where(eq(messagePlugins.id, id));
|
396
398
|
}
|
397
399
|
|
398
400
|
async updateMessagePlugin(id: string, value: Partial<MessagePluginItem>) {
|
399
|
-
const item = await
|
401
|
+
const item = await this.db.query.messagePlugins.findFirst({
|
400
402
|
where: eq(messagePlugins.id, id),
|
401
403
|
});
|
402
404
|
if (!item) throw new Error('Plugin not found');
|
403
405
|
|
404
|
-
return
|
406
|
+
return this.db.update(messagePlugins).set(value).where(eq(messagePlugins.id, id));
|
405
407
|
}
|
406
408
|
|
407
409
|
async updateTranslate(id: string, translate: Partial<MessageItem>) {
|
408
|
-
const result = await
|
410
|
+
const result = await this.db.query.messageTranslates.findFirst({
|
409
411
|
where: and(eq(messageTranslates.id, id)),
|
410
412
|
});
|
411
413
|
|
412
414
|
// If the message does not exist in the translate table, insert it
|
413
415
|
if (!result) {
|
414
|
-
return
|
416
|
+
return this.db.insert(messageTranslates).values({ ...translate, id });
|
415
417
|
}
|
416
418
|
|
417
419
|
// or just update the existing one
|
418
|
-
return
|
420
|
+
return this.db.update(messageTranslates).set(translate).where(eq(messageTranslates.id, id));
|
419
421
|
}
|
420
422
|
|
421
423
|
async updateTTS(id: string, tts: Partial<ChatTTS>) {
|
422
|
-
const result = await
|
424
|
+
const result = await this.db.query.messageTTS.findFirst({
|
423
425
|
where: and(eq(messageTTS.id, id)),
|
424
426
|
});
|
425
427
|
|
426
428
|
// If the message does not exist in the translate table, insert it
|
427
429
|
if (!result) {
|
428
|
-
return
|
430
|
+
return this.db
|
429
431
|
.insert(messageTTS)
|
430
432
|
.values({ contentMd5: tts.contentMd5, fileId: tts.file, id, voice: tts.voice });
|
431
433
|
}
|
432
434
|
|
433
435
|
// or just update the existing one
|
434
|
-
return
|
436
|
+
return this.db
|
435
437
|
.update(messageTTS)
|
436
438
|
.set({ contentMd5: tts.contentMd5, fileId: tts.file, voice: tts.voice })
|
437
439
|
.where(eq(messageTTS.id, id));
|
@@ -440,7 +442,7 @@ export class MessageModel {
|
|
440
442
|
// **************** Delete *************** //
|
441
443
|
|
442
444
|
async deleteMessage(id: string) {
|
443
|
-
return
|
445
|
+
return this.db.transaction(async (tx) => {
|
444
446
|
// 1. 查询要删除的 message 的完整信息
|
445
447
|
const message = await tx
|
446
448
|
.select()
|
@@ -476,25 +478,25 @@ export class MessageModel {
|
|
476
478
|
}
|
477
479
|
|
478
480
|
async deleteMessages(ids: string[]) {
|
479
|
-
return
|
481
|
+
return this.db
|
480
482
|
.delete(messages)
|
481
483
|
.where(and(eq(messages.userId, this.userId), inArray(messages.id, ids)));
|
482
484
|
}
|
483
485
|
|
484
486
|
async deleteMessageTranslate(id: string) {
|
485
|
-
return
|
487
|
+
return this.db.delete(messageTranslates).where(and(eq(messageTranslates.id, id)));
|
486
488
|
}
|
487
489
|
|
488
490
|
async deleteMessageTTS(id: string) {
|
489
|
-
return
|
491
|
+
return this.db.delete(messageTTS).where(and(eq(messageTTS.id, id)));
|
490
492
|
}
|
491
493
|
|
492
494
|
async deleteMessageQuery(id: string) {
|
493
|
-
return
|
495
|
+
return this.db.delete(messageQueries).where(and(eq(messageQueries.id, id)));
|
494
496
|
}
|
495
497
|
|
496
498
|
async deleteMessagesBySession(sessionId?: string | null, topicId?: string | null) {
|
497
|
-
return
|
499
|
+
return this.db
|
498
500
|
.delete(messages)
|
499
501
|
.where(
|
500
502
|
and(
|
@@ -506,7 +508,7 @@ export class MessageModel {
|
|
506
508
|
}
|
507
509
|
|
508
510
|
async deleteAllMessages() {
|
509
|
-
return
|
511
|
+
return this.db.delete(messages).where(eq(messages.userId, this.userId));
|
510
512
|
}
|
511
513
|
|
512
514
|
// **************** Helper *************** //
|
@@ -1,20 +1,22 @@
|
|
1
1
|
import { and, desc, eq } from 'drizzle-orm/expressions';
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { LobeChatDatabase } from '@/database/type';
|
4
4
|
|
5
5
|
import { InstalledPluginItem, NewInstalledPlugin, installedPlugins } from '../../schemas';
|
6
6
|
|
7
7
|
export class PluginModel {
|
8
8
|
private userId: string;
|
9
|
+
private db: LobeChatDatabase;
|
9
10
|
|
10
|
-
constructor(userId: string) {
|
11
|
+
constructor(db: LobeChatDatabase, userId: string) {
|
11
12
|
this.userId = userId;
|
13
|
+
this.db = db;
|
12
14
|
}
|
13
15
|
|
14
16
|
create = async (
|
15
17
|
params: Pick<NewInstalledPlugin, 'type' | 'identifier' | 'manifest' | 'customParams'>,
|
16
18
|
) => {
|
17
|
-
const [result] = await
|
19
|
+
const [result] = await this.db
|
18
20
|
.insert(installedPlugins)
|
19
21
|
.values({ ...params, createdAt: new Date(), updatedAt: new Date(), userId: this.userId })
|
20
22
|
.returning();
|
@@ -23,17 +25,17 @@ export class PluginModel {
|
|
23
25
|
};
|
24
26
|
|
25
27
|
delete = async (id: string) => {
|
26
|
-
return
|
28
|
+
return this.db
|
27
29
|
.delete(installedPlugins)
|
28
30
|
.where(and(eq(installedPlugins.identifier, id), eq(installedPlugins.userId, this.userId)));
|
29
31
|
};
|
30
32
|
|
31
33
|
deleteAll = async () => {
|
32
|
-
return
|
34
|
+
return this.db.delete(installedPlugins).where(eq(installedPlugins.userId, this.userId));
|
33
35
|
};
|
34
36
|
|
35
37
|
query = async () => {
|
36
|
-
return
|
38
|
+
return this.db
|
37
39
|
.select({
|
38
40
|
createdAt: installedPlugins.createdAt,
|
39
41
|
customParams: installedPlugins.customParams,
|
@@ -49,13 +51,13 @@ export class PluginModel {
|
|
49
51
|
};
|
50
52
|
|
51
53
|
findById = async (id: string) => {
|
52
|
-
return
|
54
|
+
return this.db.query.installedPlugins.findFirst({
|
53
55
|
where: and(eq(installedPlugins.identifier, id), eq(installedPlugins.userId, this.userId)),
|
54
56
|
});
|
55
57
|
};
|
56
58
|
|
57
59
|
async update(id: string, value: Partial<InstalledPluginItem>) {
|
58
|
-
return
|
60
|
+
return this.db
|
59
61
|
.update(installedPlugins)
|
60
62
|
.set({ ...value, updatedAt: new Date() })
|
61
63
|
.where(and(eq(installedPlugins.identifier, id), eq(installedPlugins.userId, this.userId)));
|