@lobehub/chat 1.36.30 → 1.36.32
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 +50 -0
- package/changelog/v1.json +18 -0
- package/docs/self-hosting/environment-variables/model-provider.mdx +7 -0
- package/docs/self-hosting/environment-variables/model-provider.zh-CN.mdx +7 -0
- package/docs/self-hosting/server-database/dokploy.zh-CN.mdx +12 -12
- package/package.json +1 -1
- package/src/app/(main)/chat/(workspace)/@conversation/features/ChatList/Content.tsx +3 -9
- package/src/app/(main)/chat/(workspace)/@conversation/features/ThreadHydration.tsx +2 -4
- package/src/app/(main)/chat/@session/features/SessionListContent/DefaultMode.tsx +2 -5
- package/src/app/(main)/discover/(detail)/plugin/[slug]/features/InstallPlugin.tsx +10 -15
- package/src/database/repositories/dataImporter/__tests__/index.test.ts +11 -18
- package/src/database/repositories/dataImporter/index.ts +31 -46
- package/src/database/server/models/__tests__/_test_template.ts +1 -1
- package/src/database/server/models/__tests__/agent.test.ts +1 -1
- package/src/database/server/models/__tests__/asyncTask.test.ts +1 -1
- package/src/database/server/models/__tests__/chunk.test.ts +1 -1
- package/src/database/server/models/__tests__/file.test.ts +1 -1
- package/src/database/server/models/__tests__/knowledgeBase.test.ts +1 -2
- package/src/database/server/models/__tests__/message.test.ts +35 -72
- package/src/database/server/models/__tests__/nextauth.test.ts +1 -1
- package/src/database/server/models/__tests__/session.test.ts +1 -1
- package/src/database/server/models/__tests__/sessionGroup.test.ts +1 -2
- package/src/database/server/models/__tests__/topic.test.ts +1 -1
- package/src/database/server/models/__tests__/user.test.ts +1 -1
- package/src/database/server/models/_template.ts +2 -2
- package/src/database/server/models/agent.ts +17 -25
- package/src/database/server/models/asyncTask.ts +2 -2
- package/src/database/server/models/chunk.ts +14 -14
- package/src/database/server/models/embedding.ts +1 -1
- package/src/database/server/models/file.ts +8 -10
- package/src/database/server/models/knowledgeBase.ts +4 -6
- package/src/database/server/models/message.ts +54 -65
- package/src/database/server/models/plugin.ts +6 -2
- package/src/database/server/models/ragEval/dataset.ts +2 -2
- package/src/database/server/models/ragEval/datasetRecord.ts +3 -8
- package/src/database/server/models/ragEval/evaluation.ts +3 -2
- package/src/database/server/models/ragEval/evaluationRecord.ts +2 -2
- package/src/database/server/models/session.ts +40 -35
- package/src/database/server/models/sessionGroup.ts +4 -4
- package/src/database/server/models/thread.ts +2 -2
- package/src/database/server/models/topic.ts +48 -53
- package/src/database/server/models/user.ts +12 -12
- package/src/features/AgentSetting/AgentPlugin/index.tsx +1 -1
- package/src/features/ChatInput/ActionBar/Tools/Dropdown.tsx +4 -4
- package/src/features/Portal/Thread/Chat/ChatList.tsx +1 -2
- package/src/hooks/useCheckPluginsIsInstalled.ts +10 -0
- package/src/hooks/useFetchInstalledPlugins.ts +10 -0
- package/src/hooks/useFetchMessages.ts +15 -0
- package/src/hooks/useFetchSessions.ts +13 -0
- package/src/hooks/useFetchThreads.ts +11 -0
- package/src/hooks/useFetchTopics.ts +6 -6
- package/src/layout/GlobalProvider/StoreInitialization.tsx +3 -1
- package/src/libs/agent-runtime/utils/streams/azureOpenai.test.ts +0 -1
- package/src/libs/next-auth/adapter/index.ts +1 -1
- package/src/server/routers/lambda/chunk.ts +2 -2
- package/src/services/user/client.ts +2 -2
- package/src/store/agent/slices/chat/action.test.ts +21 -10
- package/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts +10 -0
- package/src/store/chat/slices/builtinTool/action.ts +0 -1
- package/src/store/chat/slices/message/action.test.ts +3 -1
- package/src/store/chat/slices/message/action.ts +7 -3
- package/src/store/chat/slices/thread/action.ts +3 -3
- package/src/store/chat/slices/topic/action.test.ts +1 -1
- package/src/store/chat/slices/topic/action.ts +3 -3
- package/src/store/global/selectors.ts +6 -0
- package/src/store/session/slices/session/action.ts +6 -3
- package/src/store/session/slices/sessionGroup/action.test.ts +8 -6
- package/src/store/tool/slices/plugin/action.ts +5 -3
- package/src/store/tool/slices/store/action.ts +4 -3
- package/src/store/user/slices/common/action.test.ts +3 -1
- package/vercel.json +1 -1
@@ -46,10 +46,10 @@ export class MessageModel {
|
|
46
46
|
}
|
47
47
|
|
48
48
|
// **************** Query *************** //
|
49
|
-
async
|
49
|
+
query = async (
|
50
50
|
{ current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {},
|
51
51
|
options: { postProcessUrl?: (path: string | null) => Promise<string> } = {},
|
52
|
-
): Promise<MessageItem[]> {
|
52
|
+
): Promise<MessageItem[]> => {
|
53
53
|
const offset = current * pageSize;
|
54
54
|
|
55
55
|
// 1. get basic messages
|
@@ -212,15 +212,15 @@ export class MessageModel {
|
|
212
212
|
};
|
213
213
|
},
|
214
214
|
);
|
215
|
-
}
|
215
|
+
};
|
216
216
|
|
217
|
-
async
|
217
|
+
findById = async (id: string) => {
|
218
218
|
return this.db.query.messages.findFirst({
|
219
219
|
where: and(eq(messages.id, id), eq(messages.userId, this.userId)),
|
220
220
|
});
|
221
|
-
}
|
221
|
+
};
|
222
222
|
|
223
|
-
async
|
223
|
+
findMessageQueriesById = async (messageId: string) => {
|
224
224
|
const result = await this.db
|
225
225
|
.select({
|
226
226
|
embeddings: embeddings.embeddings,
|
@@ -236,47 +236,43 @@ export class MessageModel {
|
|
236
236
|
if (result.length === 0) return undefined;
|
237
237
|
|
238
238
|
return result[0];
|
239
|
-
}
|
239
|
+
};
|
240
240
|
|
241
|
-
async
|
241
|
+
queryAll = async (): Promise<MessageItem[]> => {
|
242
242
|
return this.db
|
243
243
|
.select()
|
244
244
|
.from(messages)
|
245
245
|
.orderBy(messages.createdAt)
|
246
|
-
.where(eq(messages.userId, this.userId))
|
247
|
-
|
248
|
-
.execute();
|
249
|
-
}
|
246
|
+
.where(eq(messages.userId, this.userId));
|
247
|
+
};
|
250
248
|
|
251
|
-
async
|
249
|
+
queryBySessionId = async (sessionId?: string | null): Promise<MessageItem[]> => {
|
252
250
|
return this.db.query.messages.findMany({
|
253
251
|
orderBy: [asc(messages.createdAt)],
|
254
252
|
where: and(eq(messages.userId, this.userId), this.matchSession(sessionId)),
|
255
253
|
});
|
256
|
-
}
|
254
|
+
};
|
257
255
|
|
258
|
-
async
|
256
|
+
queryByKeyword = async (keyword: string): Promise<MessageItem[]> => {
|
259
257
|
if (!keyword) return [];
|
260
|
-
|
261
258
|
return this.db.query.messages.findMany({
|
262
259
|
orderBy: [desc(messages.createdAt)],
|
263
260
|
where: and(eq(messages.userId, this.userId), like(messages.content, `%${keyword}%`)),
|
264
261
|
});
|
265
|
-
}
|
262
|
+
};
|
266
263
|
|
267
|
-
async
|
264
|
+
count = async (): Promise<number> => {
|
268
265
|
const result = await this.db
|
269
266
|
.select({
|
270
|
-
count: count(),
|
267
|
+
count: count(messages.id),
|
271
268
|
})
|
272
269
|
.from(messages)
|
273
|
-
.where(eq(messages.userId, this.userId))
|
274
|
-
.execute();
|
270
|
+
.where(eq(messages.userId, this.userId));
|
275
271
|
|
276
272
|
return result[0].count;
|
277
|
-
}
|
273
|
+
};
|
278
274
|
|
279
|
-
async
|
275
|
+
countToday = async (): Promise<number> => {
|
280
276
|
const today = new Date();
|
281
277
|
today.setHours(0, 0, 0, 0);
|
282
278
|
const tomorrow = new Date(today);
|
@@ -284,7 +280,7 @@ export class MessageModel {
|
|
284
280
|
|
285
281
|
const result = await this.db
|
286
282
|
.select({
|
287
|
-
count: count(),
|
283
|
+
count: count(messages.id),
|
288
284
|
})
|
289
285
|
.from(messages)
|
290
286
|
.where(
|
@@ -293,15 +289,14 @@ export class MessageModel {
|
|
293
289
|
gte(messages.createdAt, today),
|
294
290
|
lt(messages.createdAt, tomorrow),
|
295
291
|
),
|
296
|
-
)
|
297
|
-
.execute();
|
292
|
+
);
|
298
293
|
|
299
294
|
return result[0].count;
|
300
|
-
}
|
295
|
+
};
|
301
296
|
|
302
297
|
// **************** Create *************** //
|
303
298
|
|
304
|
-
async
|
299
|
+
create = async (
|
305
300
|
{
|
306
301
|
fromModel,
|
307
302
|
fromProvider,
|
@@ -313,7 +308,7 @@ export class MessageModel {
|
|
313
308
|
...message
|
314
309
|
}: CreateMessageParams,
|
315
310
|
id: string = this.genId(),
|
316
|
-
): Promise<MessageItem> {
|
311
|
+
): Promise<MessageItem> => {
|
317
312
|
return this.db.transaction(async (trx) => {
|
318
313
|
const [item] = (await trx
|
319
314
|
.insert(messages)
|
@@ -358,31 +353,31 @@ export class MessageModel {
|
|
358
353
|
|
359
354
|
return item;
|
360
355
|
});
|
361
|
-
}
|
356
|
+
};
|
362
357
|
|
363
|
-
async
|
358
|
+
batchCreate = async (newMessages: MessageItem[]) => {
|
364
359
|
const messagesToInsert = newMessages.map((m) => {
|
365
360
|
return { ...m, userId: this.userId };
|
366
361
|
});
|
367
362
|
|
368
363
|
return this.db.insert(messages).values(messagesToInsert);
|
369
|
-
}
|
364
|
+
};
|
370
365
|
|
371
|
-
async
|
366
|
+
createMessageQuery = async (params: NewMessageQuery) => {
|
372
367
|
const result = await this.db.insert(messageQueries).values(params).returning();
|
373
368
|
|
374
369
|
return result[0];
|
375
|
-
}
|
370
|
+
};
|
376
371
|
// **************** Update *************** //
|
377
372
|
|
378
|
-
async
|
373
|
+
update = async (id: string, message: Partial<MessageItem>) => {
|
379
374
|
return this.db
|
380
375
|
.update(messages)
|
381
376
|
.set(message)
|
382
377
|
.where(and(eq(messages.id, id), eq(messages.userId, this.userId)));
|
383
|
-
}
|
378
|
+
};
|
384
379
|
|
385
|
-
async
|
380
|
+
updatePluginState = async (id: string, state: Record<string, any>) => {
|
386
381
|
const item = await this.db.query.messagePlugins.findFirst({
|
387
382
|
where: eq(messagePlugins.id, id),
|
388
383
|
});
|
@@ -392,18 +387,18 @@ export class MessageModel {
|
|
392
387
|
.update(messagePlugins)
|
393
388
|
.set({ state: merge(item.state || {}, state) })
|
394
389
|
.where(eq(messagePlugins.id, id));
|
395
|
-
}
|
390
|
+
};
|
396
391
|
|
397
|
-
async
|
392
|
+
updateMessagePlugin = async (id: string, value: Partial<MessagePluginItem>) => {
|
398
393
|
const item = await this.db.query.messagePlugins.findFirst({
|
399
394
|
where: eq(messagePlugins.id, id),
|
400
395
|
});
|
401
396
|
if (!item) throw new Error('Plugin not found');
|
402
397
|
|
403
398
|
return this.db.update(messagePlugins).set(value).where(eq(messagePlugins.id, id));
|
404
|
-
}
|
399
|
+
};
|
405
400
|
|
406
|
-
async
|
401
|
+
updateTranslate = async (id: string, translate: Partial<MessageItem>) => {
|
407
402
|
const result = await this.db.query.messageTranslates.findFirst({
|
408
403
|
where: and(eq(messageTranslates.id, id)),
|
409
404
|
});
|
@@ -415,9 +410,9 @@ export class MessageModel {
|
|
415
410
|
|
416
411
|
// or just update the existing one
|
417
412
|
return this.db.update(messageTranslates).set(translate).where(eq(messageTranslates.id, id));
|
418
|
-
}
|
413
|
+
};
|
419
414
|
|
420
|
-
async
|
415
|
+
updateTTS = async (id: string, tts: Partial<ChatTTS>) => {
|
421
416
|
const result = await this.db.query.messageTTS.findFirst({
|
422
417
|
where: and(eq(messageTTS.id, id)),
|
423
418
|
});
|
@@ -434,11 +429,11 @@ export class MessageModel {
|
|
434
429
|
.update(messageTTS)
|
435
430
|
.set({ contentMd5: tts.contentMd5, fileId: tts.file, voice: tts.voice })
|
436
431
|
.where(eq(messageTTS.id, id));
|
437
|
-
}
|
432
|
+
};
|
438
433
|
|
439
434
|
// **************** Delete *************** //
|
440
435
|
|
441
|
-
async
|
436
|
+
deleteMessage = async (id: string) => {
|
442
437
|
return this.db.transaction(async (tx) => {
|
443
438
|
// 1. 查询要删除的 message 的完整信息
|
444
439
|
const message = await tx
|
@@ -460,8 +455,7 @@ export class MessageModel {
|
|
460
455
|
const res = await tx
|
461
456
|
.select({ id: messagePlugins.id })
|
462
457
|
.from(messagePlugins)
|
463
|
-
.where(inArray(messagePlugins.toolCallId, toolCallIds))
|
464
|
-
.execute();
|
458
|
+
.where(inArray(messagePlugins.toolCallId, toolCallIds));
|
465
459
|
|
466
460
|
relatedMessageIds = res.map((row) => row.id);
|
467
461
|
}
|
@@ -472,28 +466,24 @@ export class MessageModel {
|
|
472
466
|
// 5. 删除所有相关的 message
|
473
467
|
await tx.delete(messages).where(inArray(messages.id, messageIdsToDelete));
|
474
468
|
});
|
475
|
-
}
|
469
|
+
};
|
476
470
|
|
477
|
-
async
|
478
|
-
|
471
|
+
deleteMessages = async (ids: string[]) =>
|
472
|
+
this.db
|
479
473
|
.delete(messages)
|
480
474
|
.where(and(eq(messages.userId, this.userId), inArray(messages.id, ids)));
|
481
|
-
}
|
482
475
|
|
483
|
-
async
|
484
|
-
|
485
|
-
}
|
476
|
+
deleteMessageTranslate = async (id: string) =>
|
477
|
+
this.db.delete(messageTranslates).where(and(eq(messageTranslates.id, id)));
|
486
478
|
|
487
|
-
async
|
488
|
-
|
489
|
-
}
|
479
|
+
deleteMessageTTS = async (id: string) =>
|
480
|
+
this.db.delete(messageTTS).where(and(eq(messageTTS.id, id)));
|
490
481
|
|
491
|
-
async
|
492
|
-
|
493
|
-
}
|
482
|
+
deleteMessageQuery = async (id: string) =>
|
483
|
+
this.db.delete(messageQueries).where(and(eq(messageQueries.id, id)));
|
494
484
|
|
495
|
-
async
|
496
|
-
|
485
|
+
deleteMessagesBySession = async (sessionId?: string | null, topicId?: string | null) =>
|
486
|
+
this.db
|
497
487
|
.delete(messages)
|
498
488
|
.where(
|
499
489
|
and(
|
@@ -502,11 +492,10 @@ export class MessageModel {
|
|
502
492
|
this.matchTopic(topicId),
|
503
493
|
),
|
504
494
|
);
|
505
|
-
}
|
506
495
|
|
507
|
-
async
|
496
|
+
deleteAllMessages = async () => {
|
508
497
|
return this.db.delete(messages).where(eq(messages.userId, this.userId));
|
509
|
-
}
|
498
|
+
};
|
510
499
|
|
511
500
|
// **************** Helper *************** //
|
512
501
|
|
@@ -19,6 +19,10 @@ export class PluginModel {
|
|
19
19
|
const [result] = await this.db
|
20
20
|
.insert(installedPlugins)
|
21
21
|
.values({ ...params, createdAt: new Date(), updatedAt: new Date(), userId: this.userId })
|
22
|
+
.onConflictDoUpdate({
|
23
|
+
set: { ...params, updatedAt: new Date() },
|
24
|
+
target: [installedPlugins.identifier, installedPlugins.userId],
|
25
|
+
})
|
22
26
|
.returning();
|
23
27
|
|
24
28
|
return result;
|
@@ -56,10 +60,10 @@ export class PluginModel {
|
|
56
60
|
});
|
57
61
|
};
|
58
62
|
|
59
|
-
async
|
63
|
+
update = async (id: string, value: Partial<InstalledPluginItem>) => {
|
60
64
|
return this.db
|
61
65
|
.update(installedPlugins)
|
62
66
|
.set({ ...value, updatedAt: new Date() })
|
63
67
|
.where(and(eq(installedPlugins.identifier, id), eq(installedPlugins.userId, this.userId)));
|
64
|
-
}
|
68
|
+
};
|
65
69
|
}
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { and, desc, eq } from 'drizzle-orm';
|
1
|
+
import { and, desc, eq } from 'drizzle-orm/expressions';
|
2
2
|
|
3
|
-
import { serverDB } from '@/database/server';
|
4
3
|
import { NewEvalDatasetsItem, evalDatasets } from '@/database/schemas';
|
4
|
+
import { serverDB } from '@/database/server';
|
5
5
|
import { RAGEvalDataSetItem } from '@/types/eval';
|
6
6
|
|
7
7
|
export class EvalDatasetModel {
|
@@ -1,11 +1,7 @@
|
|
1
|
-
import { and, eq, inArray } from 'drizzle-orm';
|
1
|
+
import { and, eq, inArray } from 'drizzle-orm/expressions';
|
2
2
|
|
3
|
+
import { NewEvalDatasetRecordsItem, evalDatasetRecords, files } from '@/database/schemas';
|
3
4
|
import { serverDB } from '@/database/server';
|
4
|
-
import {
|
5
|
-
NewEvalDatasetRecordsItem,
|
6
|
-
evalDatasetRecords,
|
7
|
-
files,
|
8
|
-
} from '@/database/schemas';
|
9
5
|
import { EvalDatasetRecordRefFile } from '@/types/eval';
|
10
6
|
|
11
7
|
export class EvalDatasetRecordModel {
|
@@ -50,8 +46,7 @@ export class EvalDatasetRecordModel {
|
|
50
46
|
const fileItems = await serverDB
|
51
47
|
.select({ fileType: files.fileType, id: files.id, name: files.name })
|
52
48
|
.from(files)
|
53
|
-
.where(and(inArray(files.id, fileList), eq(files.userId, this.userId)))
|
54
|
-
.execute();
|
49
|
+
.where(and(inArray(files.id, fileList), eq(files.userId, this.userId)));
|
55
50
|
|
56
51
|
return list.map((item) => {
|
57
52
|
return {
|
@@ -1,12 +1,13 @@
|
|
1
|
-
import { SQL,
|
1
|
+
import { SQL, count } from 'drizzle-orm';
|
2
|
+
import { and, desc, eq, inArray } from 'drizzle-orm/expressions';
|
2
3
|
|
3
|
-
import { serverDB } from '@/database/server';
|
4
4
|
import {
|
5
5
|
NewEvalEvaluationItem,
|
6
6
|
evalDatasets,
|
7
7
|
evalEvaluation,
|
8
8
|
evaluationRecords,
|
9
9
|
} from '@/database/schemas';
|
10
|
+
import { serverDB } from '@/database/server';
|
10
11
|
import { EvalEvaluationStatus, RAGEvalEvaluationItem } from '@/types/eval';
|
11
12
|
|
12
13
|
export class EvalEvaluationModel {
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { and, eq } from 'drizzle-orm';
|
1
|
+
import { and, eq } from 'drizzle-orm/expressions';
|
2
2
|
|
3
|
-
import { serverDB } from '@/database/server';
|
4
3
|
import { NewEvaluationRecordsItem, evaluationRecords } from '@/database/schemas';
|
4
|
+
import { serverDB } from '@/database/server';
|
5
5
|
|
6
6
|
export class EvaluationRecordModel {
|
7
7
|
private userId: string;
|
@@ -31,7 +31,7 @@ export class SessionModel {
|
|
31
31
|
}
|
32
32
|
// **************** Query *************** //
|
33
33
|
|
34
|
-
async
|
34
|
+
query = async ({ current = 0, pageSize = 9999 } = {}) => {
|
35
35
|
const offset = current * pageSize;
|
36
36
|
|
37
37
|
return this.db.query.sessions.findMany({
|
@@ -41,9 +41,9 @@ export class SessionModel {
|
|
41
41
|
where: and(eq(sessions.userId, this.userId), not(eq(sessions.slug, INBOX_SESSION_ID))),
|
42
42
|
with: { agentsToSessions: { columns: {}, with: { agent: true } }, group: true },
|
43
43
|
});
|
44
|
-
}
|
44
|
+
};
|
45
45
|
|
46
|
-
async
|
46
|
+
queryWithGroups = async (): Promise<ChatSessionList> => {
|
47
47
|
// 查询所有会话
|
48
48
|
const result = await this.query();
|
49
49
|
|
@@ -56,9 +56,9 @@ export class SessionModel {
|
|
56
56
|
sessionGroups: groups as unknown as ChatSessionList['sessionGroups'],
|
57
57
|
sessions: result.map((item) => this.mapSessionItem(item as any)),
|
58
58
|
};
|
59
|
-
}
|
59
|
+
};
|
60
60
|
|
61
|
-
async
|
61
|
+
queryByKeyword = async (keyword: string) => {
|
62
62
|
if (!keyword) return [];
|
63
63
|
|
64
64
|
const keywordLowerCase = keyword.toLowerCase();
|
@@ -66,11 +66,11 @@ export class SessionModel {
|
|
66
66
|
const data = await this.findSessionsByKeywords({ keyword: keywordLowerCase });
|
67
67
|
|
68
68
|
return data.map((item) => this.mapSessionItem(item as any));
|
69
|
-
}
|
69
|
+
};
|
70
70
|
|
71
|
-
async
|
71
|
+
findByIdOrSlug = async (
|
72
72
|
idOrSlug: string,
|
73
|
-
): Promise<(SessionItem & { agent: AgentItem }) | undefined> {
|
73
|
+
): Promise<(SessionItem & { agent: AgentItem }) | undefined> => {
|
74
74
|
const result = await this.db.query.sessions.findFirst({
|
75
75
|
where: and(
|
76
76
|
or(eq(sessions.id, idOrSlug), eq(sessions.slug, idOrSlug)),
|
@@ -82,23 +82,22 @@ export class SessionModel {
|
|
82
82
|
if (!result) return;
|
83
83
|
|
84
84
|
return { ...result, agent: (result?.agentsToSessions?.[0] as any)?.agent } as any;
|
85
|
-
}
|
85
|
+
};
|
86
86
|
|
87
|
-
async
|
87
|
+
count = async (): Promise<number> => {
|
88
88
|
const result = await this.db
|
89
89
|
.select({
|
90
|
-
count: count(),
|
90
|
+
count: count(sessions.id),
|
91
91
|
})
|
92
92
|
.from(sessions)
|
93
|
-
.where(eq(sessions.userId, this.userId))
|
94
|
-
.execute();
|
93
|
+
.where(eq(sessions.userId, this.userId));
|
95
94
|
|
96
95
|
return result[0].count;
|
97
|
-
}
|
96
|
+
};
|
98
97
|
|
99
98
|
// **************** Create *************** //
|
100
99
|
|
101
|
-
async
|
100
|
+
create = async ({
|
102
101
|
id = idGenerator('sessions'),
|
103
102
|
type = 'agent',
|
104
103
|
session = {},
|
@@ -110,7 +109,7 @@ export class SessionModel {
|
|
110
109
|
session?: Partial<NewSession>;
|
111
110
|
slug?: string;
|
112
111
|
type: 'agent' | 'group';
|
113
|
-
}): Promise<SessionItem> {
|
112
|
+
}): Promise<SessionItem> => {
|
114
113
|
return this.db.transaction(async (trx) => {
|
115
114
|
const newAgents = await trx
|
116
115
|
.insert(agents)
|
@@ -143,9 +142,9 @@ export class SessionModel {
|
|
143
142
|
|
144
143
|
return result[0];
|
145
144
|
});
|
146
|
-
}
|
145
|
+
};
|
147
146
|
|
148
|
-
async
|
147
|
+
createInbox = async () => {
|
149
148
|
const item = await this.db.query.sessions.findFirst({
|
150
149
|
where: and(eq(sessions.userId, this.userId), eq(sessions.slug, INBOX_SESSION_ID)),
|
151
150
|
});
|
@@ -158,9 +157,9 @@ export class SessionModel {
|
|
158
157
|
slug: INBOX_SESSION_ID,
|
159
158
|
type: 'agent',
|
160
159
|
});
|
161
|
-
}
|
160
|
+
};
|
162
161
|
|
163
|
-
async
|
162
|
+
batchCreate = async (newSessions: NewSession[]) => {
|
164
163
|
const sessionsToInsert = newSessions.map((s) => {
|
165
164
|
return {
|
166
165
|
...s,
|
@@ -170,9 +169,9 @@ export class SessionModel {
|
|
170
169
|
});
|
171
170
|
|
172
171
|
return this.db.insert(sessions).values(sessionsToInsert);
|
173
|
-
}
|
172
|
+
};
|
174
173
|
|
175
|
-
async
|
174
|
+
duplicate = async (id: string, newTitle?: string) => {
|
176
175
|
const result = await this.findByIdOrSlug(id);
|
177
176
|
|
178
177
|
if (!result) return;
|
@@ -193,47 +192,49 @@ export class SessionModel {
|
|
193
192
|
},
|
194
193
|
type: 'agent',
|
195
194
|
});
|
196
|
-
}
|
195
|
+
};
|
197
196
|
|
198
197
|
// **************** Delete *************** //
|
199
198
|
|
200
199
|
/**
|
201
200
|
* Delete a session, also delete all messages and topics associated with it.
|
202
201
|
*/
|
203
|
-
async
|
202
|
+
delete = async (id: string) => {
|
204
203
|
return this.db
|
205
204
|
.delete(sessions)
|
206
205
|
.where(and(eq(sessions.id, id), eq(sessions.userId, this.userId)));
|
207
|
-
}
|
206
|
+
};
|
208
207
|
|
209
208
|
/**
|
210
209
|
* Batch delete sessions, also delete all messages and topics associated with them.
|
211
210
|
*/
|
212
|
-
async
|
211
|
+
batchDelete = async (ids: string[]) => {
|
213
212
|
return this.db
|
214
213
|
.delete(sessions)
|
215
214
|
.where(and(inArray(sessions.id, ids), eq(sessions.userId, this.userId)));
|
216
|
-
}
|
215
|
+
};
|
217
216
|
|
218
|
-
async
|
217
|
+
deleteAll = async () => {
|
219
218
|
return this.db.delete(sessions).where(eq(sessions.userId, this.userId));
|
220
|
-
}
|
219
|
+
};
|
221
220
|
// **************** Update *************** //
|
222
221
|
|
223
|
-
async
|
222
|
+
update = async (id: string, data: Partial<SessionItem>) => {
|
224
223
|
return this.db
|
225
224
|
.update(sessions)
|
226
225
|
.set(data)
|
227
226
|
.where(and(eq(sessions.id, id), eq(sessions.userId, this.userId)))
|
228
227
|
.returning();
|
229
|
-
}
|
228
|
+
};
|
229
|
+
|
230
|
+
updateConfig = async (id: string, data: Partial<AgentItem>) => {
|
231
|
+
if (Object.keys(data).length === 0) return;
|
230
232
|
|
231
|
-
async updateConfig(id: string, data: Partial<AgentItem>) {
|
232
233
|
return this.db
|
233
234
|
.update(agents)
|
234
235
|
.set(data)
|
235
236
|
.where(and(eq(agents.id, id), eq(agents.userId, this.userId)));
|
236
|
-
}
|
237
|
+
};
|
237
238
|
|
238
239
|
// **************** Helper *************** //
|
239
240
|
|
@@ -264,7 +265,11 @@ export class SessionModel {
|
|
264
265
|
} as any;
|
265
266
|
};
|
266
267
|
|
267
|
-
async
|
268
|
+
findSessionsByKeywords = async (params: {
|
269
|
+
current?: number;
|
270
|
+
keyword: string;
|
271
|
+
pageSize?: number;
|
272
|
+
}) => {
|
268
273
|
const { keyword, pageSize = 9999, current = 0 } = params;
|
269
274
|
const offset = current * pageSize;
|
270
275
|
const results = await this.db.query.agents.findMany({
|
@@ -288,5 +293,5 @@ export class SessionModel {
|
|
288
293
|
return results.map((item) => item.agentsToSessions[0].session);
|
289
294
|
} catch {}
|
290
295
|
return [];
|
291
|
-
}
|
296
|
+
};
|
292
297
|
}
|
@@ -46,14 +46,14 @@ export class SessionGroupModel {
|
|
46
46
|
});
|
47
47
|
};
|
48
48
|
|
49
|
-
async
|
49
|
+
update = async (id: string, value: Partial<SessionGroupItem>) => {
|
50
50
|
return this.db
|
51
51
|
.update(sessionGroups)
|
52
52
|
.set({ ...value, updatedAt: new Date() })
|
53
53
|
.where(and(eq(sessionGroups.id, id), eq(sessionGroups.userId, this.userId)));
|
54
|
-
}
|
54
|
+
};
|
55
55
|
|
56
|
-
async
|
56
|
+
updateOrder = async (sortMap: { id: string; sort: number }[]) => {
|
57
57
|
await this.db.transaction(async (tx) => {
|
58
58
|
const updates = sortMap.map(({ id, sort }) => {
|
59
59
|
return tx
|
@@ -64,7 +64,7 @@ export class SessionGroupModel {
|
|
64
64
|
|
65
65
|
await Promise.all(updates);
|
66
66
|
});
|
67
|
-
}
|
67
|
+
};
|
68
68
|
|
69
69
|
private genId = () => idGenerator('sessionGroups');
|
70
70
|
}
|
@@ -71,10 +71,10 @@ export class ThreadModel {
|
|
71
71
|
});
|
72
72
|
};
|
73
73
|
|
74
|
-
async
|
74
|
+
update = async (id: string, value: Partial<ThreadItem>) => {
|
75
75
|
return this.db
|
76
76
|
.update(threads)
|
77
77
|
.set({ ...value, updatedAt: new Date() })
|
78
78
|
.where(and(eq(threads.id, id), eq(threads.userId, this.userId)));
|
79
|
-
}
|
79
|
+
};
|
80
80
|
}
|