@lobehub/chat 1.36.46 → 1.37.1
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/README.ja-JP.md +8 -8
- package/README.md +8 -8
- package/README.zh-CN.md +8 -8
- package/changelog/v1.json +18 -0
- package/next.config.mjs +4 -1
- package/package.json +5 -3
- package/scripts/migrateClientDB/compile-migrations.ts +14 -0
- package/src/app/(main)/(mobile)/me/(home)/layout.tsx +2 -0
- package/src/app/(main)/chat/_layout/Desktop/index.tsx +3 -2
- package/src/app/(main)/chat/_layout/Mobile.tsx +5 -3
- package/src/app/(main)/chat/features/Migration/DBReader.ts +290 -0
- package/src/app/(main)/chat/features/Migration/UpgradeButton.tsx +4 -8
- package/src/app/(main)/chat/features/Migration/index.tsx +26 -15
- package/src/app/(main)/settings/_layout/Desktop/index.tsx +2 -0
- package/src/app/loading/Client/Content.tsx +11 -1
- package/src/app/loading/Client/Error.tsx +27 -0
- package/src/app/loading/stage.ts +8 -0
- package/src/components/FullscreenLoading/index.tsx +4 -3
- package/src/const/version.ts +1 -0
- package/src/database/client/db.test.ts +172 -0
- package/src/database/client/db.ts +246 -0
- package/src/database/client/migrations.json +289 -0
- package/src/features/InitClientDB/EnableModal.tsx +111 -0
- package/src/features/InitClientDB/ErrorResult.tsx +125 -0
- package/src/features/InitClientDB/InitIndicator.tsx +124 -0
- package/src/features/InitClientDB/PGliteSVG.tsx +22 -0
- package/src/features/InitClientDB/index.tsx +37 -0
- package/src/hooks/useCheckPluginsIsInstalled.ts +2 -2
- package/src/hooks/useFetchInstalledPlugins.ts +2 -2
- package/src/hooks/useFetchMessages.ts +2 -2
- package/src/hooks/useFetchSessions.ts +2 -2
- package/src/hooks/useFetchThreads.ts +2 -2
- package/src/hooks/useFetchTopics.ts +2 -2
- package/src/layout/GlobalProvider/StoreInitialization.tsx +2 -2
- package/src/services/baseClientService/index.ts +9 -0
- package/src/services/debug.ts +32 -34
- package/src/services/file/{client.test.ts → _deprecated.test.ts} +1 -1
- package/src/services/file/index.ts +6 -2
- package/src/services/file/pglite.test.ts +198 -0
- package/src/services/file/pglite.ts +84 -0
- package/src/services/file/type.ts +4 -3
- package/src/services/github.ts +17 -0
- package/src/services/import/index.ts +6 -2
- package/src/services/import/pglite.test.ts +997 -0
- package/src/services/import/pglite.ts +34 -0
- package/src/services/message/{client.test.ts → _deprecated.test.ts} +1 -1
- package/src/services/message/{client.ts → _deprecated.ts} +2 -0
- package/src/services/message/index.ts +6 -2
- package/src/services/message/pglite.test.ts +430 -0
- package/src/services/message/pglite.ts +118 -0
- package/src/services/message/server.ts +9 -9
- package/src/services/message/type.ts +3 -4
- package/src/services/plugin/{client.test.ts → _deprecated.test.ts} +1 -1
- package/src/services/plugin/index.ts +6 -2
- package/src/services/plugin/pglite.test.ts +175 -0
- package/src/services/plugin/pglite.ts +51 -0
- package/src/services/session/{client.test.ts → _deprecated.test.ts} +1 -1
- package/src/services/session/{client.ts → _deprecated.ts} +1 -1
- package/src/services/session/index.ts +6 -2
- package/src/services/session/pglite.test.ts +411 -0
- package/src/services/session/pglite.ts +184 -0
- package/src/services/session/type.ts +14 -1
- package/src/services/topic/client.test.ts +1 -1
- package/src/services/topic/index.ts +6 -3
- package/src/services/topic/pglite.test.ts +212 -0
- package/src/services/topic/pglite.ts +85 -0
- package/src/services/user/{client.test.ts → _deprecated.test.ts} +1 -2
- package/src/services/user/index.ts +8 -2
- package/src/services/user/pglite.test.ts +98 -0
- package/src/services/user/pglite.ts +92 -0
- package/src/store/chat/slices/builtinTool/action.test.ts +4 -5
- package/src/store/global/actions/clientDb.ts +51 -0
- package/src/store/global/initialState.ts +13 -0
- package/src/store/global/selectors.ts +24 -3
- package/src/store/global/store.ts +3 -1
- package/src/store/session/slices/sessionGroup/reducer.test.ts +6 -6
- package/src/store/user/slices/common/action.test.ts +1 -1
- package/src/store/user/slices/common/action.ts +2 -4
- package/src/types/clientDB.ts +29 -0
- package/src/types/importer.ts +17 -5
- package/src/types/meta.ts +0 -9
- package/src/types/session/sessionGroup.ts +3 -3
- package/src/services/message/index.test.ts +0 -48
- /package/src/services/file/{client.ts → _deprecated.ts} +0 -0
- /package/src/services/import/{client.ts → _deprecated.ts} +0 -0
- /package/src/services/plugin/{client.ts → _deprecated.ts} +0 -0
- /package/src/services/topic/{client.ts → _deprecated.ts} +0 -0
- /package/src/services/user/{client.ts → _deprecated.ts} +0 -0
@@ -0,0 +1,411 @@
|
|
1
|
+
import { eq, not } from 'drizzle-orm/expressions';
|
2
|
+
import { Mock, beforeEach, describe, expect, it, vi } from 'vitest';
|
3
|
+
|
4
|
+
import { INBOX_SESSION_ID } from '@/const/session';
|
5
|
+
import { clientDB, initializeDB } from '@/database/client/db';
|
6
|
+
import {
|
7
|
+
NewSession,
|
8
|
+
SessionItem,
|
9
|
+
agents,
|
10
|
+
agentsToSessions,
|
11
|
+
sessionGroups,
|
12
|
+
sessions,
|
13
|
+
users,
|
14
|
+
} from '@/database/schemas';
|
15
|
+
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
|
16
|
+
import { LobeAgentSession, LobeSessionType, SessionGroups } from '@/types/session';
|
17
|
+
|
18
|
+
import { ClientService } from './pglite';
|
19
|
+
|
20
|
+
const userId = 'message-db';
|
21
|
+
const sessionService = new ClientService(userId);
|
22
|
+
|
23
|
+
const mockSessionId = 'mock-session-id';
|
24
|
+
const mockAgentId = 'agent-id';
|
25
|
+
|
26
|
+
// Mock data
|
27
|
+
beforeEach(async () => {
|
28
|
+
await initializeDB();
|
29
|
+
|
30
|
+
// 在每个测试用例之前,清空表
|
31
|
+
await clientDB.transaction(async (trx) => {
|
32
|
+
await trx.insert(users).values([{ id: userId }, { id: '456' }]);
|
33
|
+
await trx.insert(sessions).values([{ id: mockSessionId, userId }]);
|
34
|
+
await trx.insert(agents).values([{ id: mockAgentId, userId }]);
|
35
|
+
await trx.insert(agentsToSessions).values([{ agentId: mockAgentId, sessionId: mockSessionId }]);
|
36
|
+
await trx.insert(sessionGroups).values([
|
37
|
+
{ id: 'group-1', name: 'group-A', sort: 2, userId },
|
38
|
+
{ id: 'group-2', name: 'group-B', sort: 1, userId },
|
39
|
+
{ id: 'group-4', name: 'group-C', sort: 1, userId: '456' },
|
40
|
+
]);
|
41
|
+
});
|
42
|
+
});
|
43
|
+
|
44
|
+
afterEach(async () => {
|
45
|
+
// 在每个测试用例之后,清空表
|
46
|
+
await clientDB.delete(users);
|
47
|
+
});
|
48
|
+
|
49
|
+
describe('SessionService', () => {
|
50
|
+
const mockSession = {
|
51
|
+
id: mockSessionId,
|
52
|
+
type: 'agent',
|
53
|
+
meta: { title: 'Mock Session' },
|
54
|
+
} as LobeAgentSession;
|
55
|
+
|
56
|
+
describe('createSession', () => {
|
57
|
+
it('should create a new session and return its id', async () => {
|
58
|
+
// Setup
|
59
|
+
const sessionType = LobeSessionType.Agent;
|
60
|
+
const defaultValue = { meta: { title: 'New Session' } } as Partial<LobeAgentSession>;
|
61
|
+
|
62
|
+
// Execute
|
63
|
+
const sessionId = await sessionService.createSession(sessionType, defaultValue);
|
64
|
+
|
65
|
+
// Assert
|
66
|
+
expect(sessionId).toMatch(/^ssn_/);
|
67
|
+
});
|
68
|
+
});
|
69
|
+
|
70
|
+
describe('removeSession', () => {
|
71
|
+
it('should remove a session by its id', async () => {
|
72
|
+
// Execute
|
73
|
+
await sessionService.removeSession(mockSessionId);
|
74
|
+
|
75
|
+
// Assert
|
76
|
+
|
77
|
+
const result = await clientDB.query.sessions.findFirst({
|
78
|
+
where: eq(sessions.id, mockSessionId),
|
79
|
+
});
|
80
|
+
// Assert
|
81
|
+
expect(result).toBeUndefined();
|
82
|
+
});
|
83
|
+
});
|
84
|
+
|
85
|
+
describe('removeAllSessions', () => {
|
86
|
+
it('should clear all sessions from the table', async () => {
|
87
|
+
// Setup
|
88
|
+
await clientDB
|
89
|
+
.insert(sessions)
|
90
|
+
.values([{ userId: userId }, { userId: userId }, { userId: userId }]);
|
91
|
+
|
92
|
+
// Execute
|
93
|
+
await sessionService.removeAllSessions();
|
94
|
+
|
95
|
+
// Assert
|
96
|
+
const result = await clientDB.query.sessions.findMany({
|
97
|
+
where: eq(sessionGroups.userId, userId),
|
98
|
+
});
|
99
|
+
|
100
|
+
expect(result.length).toBe(0);
|
101
|
+
});
|
102
|
+
});
|
103
|
+
|
104
|
+
describe('updateSession', () => {
|
105
|
+
it('should update the group of a session', async () => {
|
106
|
+
// Setup
|
107
|
+
const groupId = 'group-1';
|
108
|
+
|
109
|
+
// Execute
|
110
|
+
await sessionService.updateSession(mockSessionId, { group: groupId });
|
111
|
+
|
112
|
+
// Assert
|
113
|
+
const result = await clientDB.query.sessions.findFirst({
|
114
|
+
where: eq(sessions.id, mockSessionId),
|
115
|
+
});
|
116
|
+
expect(result).toMatchObject({ groupId });
|
117
|
+
});
|
118
|
+
|
119
|
+
it('should update the pinned status of a session', async () => {
|
120
|
+
// Setup
|
121
|
+
const pinned = true;
|
122
|
+
|
123
|
+
// Execute
|
124
|
+
await sessionService.updateSession(mockSessionId, { pinned });
|
125
|
+
|
126
|
+
// Assert
|
127
|
+
const result = await clientDB.query.sessions.findFirst({
|
128
|
+
where: eq(sessions.id, mockSessionId),
|
129
|
+
});
|
130
|
+
|
131
|
+
expect(result!.pinned).toBeTruthy();
|
132
|
+
});
|
133
|
+
});
|
134
|
+
|
135
|
+
describe('updateSessionConfig', () => {
|
136
|
+
it('should update the config of a session', async () => {
|
137
|
+
// Setup
|
138
|
+
const newConfig = { model: 'abc' } as LobeAgentConfig;
|
139
|
+
|
140
|
+
// Execute
|
141
|
+
await sessionService.updateSessionConfig(mockSessionId, newConfig);
|
142
|
+
|
143
|
+
// Assert
|
144
|
+
const result = await sessionService.getSessionConfig(mockSessionId);
|
145
|
+
expect(result).toMatchObject(newConfig);
|
146
|
+
});
|
147
|
+
});
|
148
|
+
|
149
|
+
describe('countSessions', () => {
|
150
|
+
it('should return false if no sessions exist', async () => {
|
151
|
+
await clientDB.delete(sessions);
|
152
|
+
|
153
|
+
// Execute
|
154
|
+
const result = await sessionService.countSessions();
|
155
|
+
|
156
|
+
// Assert
|
157
|
+
expect(result).toBe(0);
|
158
|
+
});
|
159
|
+
|
160
|
+
it('should return true if sessions exist', async () => {
|
161
|
+
// Setup
|
162
|
+
await clientDB.delete(sessions);
|
163
|
+
await clientDB.insert(sessions).values([{ userId }]);
|
164
|
+
|
165
|
+
// Execute
|
166
|
+
const result = await sessionService.countSessions();
|
167
|
+
|
168
|
+
// Assert
|
169
|
+
expect(result).toBe(1);
|
170
|
+
});
|
171
|
+
});
|
172
|
+
|
173
|
+
describe('searchSessions', () => {
|
174
|
+
it('should return sessions that match the keyword', async () => {
|
175
|
+
// Setup
|
176
|
+
await clientDB.insert(agents).values({ userId, id: 'agent-1', title: 'Session Name' });
|
177
|
+
await clientDB
|
178
|
+
.insert(agentsToSessions)
|
179
|
+
.values({ agentId: 'agent-1', sessionId: mockSessionId });
|
180
|
+
|
181
|
+
// Execute
|
182
|
+
const keyword = 'Name';
|
183
|
+
const result = await sessionService.searchSessions(keyword);
|
184
|
+
|
185
|
+
// Assert
|
186
|
+
// TODO: 后续需要把这个搜索的标题和描述都加上,现在这个 client 搜索会有问题
|
187
|
+
expect(result).toMatchObject([{ id: mockSessionId }]);
|
188
|
+
});
|
189
|
+
});
|
190
|
+
|
191
|
+
describe('cloneSession', () => {
|
192
|
+
it('should duplicate a session and return its id', async () => {
|
193
|
+
// Setup
|
194
|
+
const newTitle = 'Duplicated Session';
|
195
|
+
const session: NewSession = {
|
196
|
+
id: 'duplicated-session-id',
|
197
|
+
title: '123',
|
198
|
+
userId,
|
199
|
+
};
|
200
|
+
await clientDB.insert(sessions).values([session]);
|
201
|
+
await clientDB.insert(agents).values({ userId, id: 'agent-1' });
|
202
|
+
await clientDB
|
203
|
+
.insert(agentsToSessions)
|
204
|
+
.values({ agentId: 'agent-1', sessionId: 'duplicated-session-id' });
|
205
|
+
|
206
|
+
// Execute
|
207
|
+
const duplicatedSessionId = await sessionService.cloneSession(mockSessionId, newTitle);
|
208
|
+
|
209
|
+
// Assert
|
210
|
+
|
211
|
+
const result = await clientDB.query.sessions.findFirst({
|
212
|
+
where: eq(sessions.id, duplicatedSessionId!),
|
213
|
+
});
|
214
|
+
expect(result).toMatchObject({ title: 'Duplicated Session' });
|
215
|
+
});
|
216
|
+
});
|
217
|
+
|
218
|
+
describe('getGroupedSessions', () => {
|
219
|
+
it('should retrieve sessions with their group', async () => {
|
220
|
+
// Execute
|
221
|
+
const sessionsWithGroup = await sessionService.getGroupedSessions();
|
222
|
+
|
223
|
+
expect(sessionsWithGroup).toMatchObject({
|
224
|
+
sessionGroups: [
|
225
|
+
{ id: 'group-2', name: 'group-B', sort: 1 },
|
226
|
+
{ id: 'group-1', name: 'group-A', sort: 2 },
|
227
|
+
],
|
228
|
+
sessions: [{ id: 'mock-session-id', type: 'agent' }],
|
229
|
+
});
|
230
|
+
});
|
231
|
+
});
|
232
|
+
|
233
|
+
describe('getSessionsByType', () => {
|
234
|
+
it('should get sessions by type "all"', async () => {
|
235
|
+
const sessions = await sessionService.getSessionsByType('all');
|
236
|
+
expect(sessions).toBeDefined();
|
237
|
+
});
|
238
|
+
|
239
|
+
it('should get sessions by type "agent"', async () => {
|
240
|
+
const sessions = await sessionService.getSessionsByType('agent');
|
241
|
+
expect(sessions).toBeDefined();
|
242
|
+
});
|
243
|
+
|
244
|
+
it('should get sessions by type "group"', async () => {
|
245
|
+
const sessions = await sessionService.getSessionsByType('group');
|
246
|
+
expect(sessions).toBeDefined();
|
247
|
+
});
|
248
|
+
});
|
249
|
+
|
250
|
+
describe('getSessionConfig', () => {
|
251
|
+
it.skip('should get default config for INBOX_SESSION_ID', async () => {
|
252
|
+
const config = await sessionService.getSessionConfig(INBOX_SESSION_ID);
|
253
|
+
expect(config).toBeDefined();
|
254
|
+
});
|
255
|
+
|
256
|
+
it('should throw error for non-existent session', async () => {
|
257
|
+
await expect(sessionService.getSessionConfig('non-existent')).rejects.toThrow(
|
258
|
+
'Session not found',
|
259
|
+
);
|
260
|
+
});
|
261
|
+
});
|
262
|
+
|
263
|
+
describe('updateSessionMeta', () => {
|
264
|
+
it('should not update meta for INBOX_SESSION_ID', async () => {
|
265
|
+
const result = await sessionService.updateSessionMeta(INBOX_SESSION_ID, {
|
266
|
+
title: 'New Title',
|
267
|
+
});
|
268
|
+
expect(result).toBeUndefined();
|
269
|
+
});
|
270
|
+
|
271
|
+
it('should update meta for normal session', async () => {
|
272
|
+
const meta = { title: 'Updated Title' };
|
273
|
+
await sessionService.updateSessionMeta(mockSessionId, meta);
|
274
|
+
|
275
|
+
const session = await clientDB.query.sessions.findFirst({
|
276
|
+
where: eq(sessions.id, mockSessionId),
|
277
|
+
});
|
278
|
+
expect(session).toBeDefined();
|
279
|
+
});
|
280
|
+
});
|
281
|
+
|
282
|
+
describe('updateSessionChatConfig', () => {
|
283
|
+
it('should update chat config', async () => {
|
284
|
+
const chatConfig = { temperature: 0.8 } as Partial<LobeAgentChatConfig>;
|
285
|
+
const result = await sessionService.updateSessionChatConfig(mockSessionId, chatConfig);
|
286
|
+
expect(result).toBeDefined();
|
287
|
+
});
|
288
|
+
});
|
289
|
+
|
290
|
+
describe('model getters', () => {
|
291
|
+
it('should return session model instance', () => {
|
292
|
+
// @ts-ignore - accessing private getter
|
293
|
+
const model = sessionService.sessionModel;
|
294
|
+
expect(model).toBeDefined();
|
295
|
+
});
|
296
|
+
|
297
|
+
it('should return session group model instance', () => {
|
298
|
+
// @ts-ignore - accessing private getter
|
299
|
+
const model = sessionService.sessionGroupModel;
|
300
|
+
expect(model).toBeDefined();
|
301
|
+
});
|
302
|
+
});
|
303
|
+
|
304
|
+
// SessionGroup related tests
|
305
|
+
describe('createSessionGroup', () => {
|
306
|
+
it('should create a new session group and return its id', async () => {
|
307
|
+
// Setup
|
308
|
+
const groupName = 'New Group';
|
309
|
+
const sort = 1;
|
310
|
+
|
311
|
+
// Execute
|
312
|
+
const groupId = await sessionService.createSessionGroup(groupName, sort);
|
313
|
+
|
314
|
+
// Assert
|
315
|
+
expect(groupId).toMatch(/^sg_/);
|
316
|
+
|
317
|
+
const result = await clientDB.query.sessionGroups.findFirst({
|
318
|
+
where: eq(sessionGroups.id, groupId),
|
319
|
+
});
|
320
|
+
|
321
|
+
expect(result).toMatchObject({ id: groupId, name: groupName, sort });
|
322
|
+
});
|
323
|
+
});
|
324
|
+
|
325
|
+
describe('removeSessionGroup', () => {
|
326
|
+
it('should remove a session group by its id', async () => {
|
327
|
+
const groupId = 'group-1';
|
328
|
+
// Execute
|
329
|
+
await sessionService.removeSessionGroup(groupId);
|
330
|
+
|
331
|
+
const result = await clientDB.query.sessionGroups.findFirst({
|
332
|
+
where: eq(sessionGroups.id, groupId),
|
333
|
+
});
|
334
|
+
// Assert
|
335
|
+
expect(result).toBeUndefined();
|
336
|
+
});
|
337
|
+
});
|
338
|
+
|
339
|
+
describe('clearSessionGroups', () => {
|
340
|
+
it('should clear all session groups', async () => {
|
341
|
+
// Execute
|
342
|
+
await sessionService.removeSessionGroups();
|
343
|
+
|
344
|
+
// Assert
|
345
|
+
const result = await clientDB.query.sessionGroups.findMany({
|
346
|
+
where: eq(sessionGroups.userId, userId),
|
347
|
+
});
|
348
|
+
|
349
|
+
expect(result.length).toBe(0);
|
350
|
+
|
351
|
+
const result2 = await clientDB.query.sessionGroups.findMany({
|
352
|
+
where: not(eq(sessionGroups.userId, userId)),
|
353
|
+
});
|
354
|
+
|
355
|
+
expect(result2.length).toBeGreaterThan(0);
|
356
|
+
});
|
357
|
+
});
|
358
|
+
|
359
|
+
describe('getSessionGroups', () => {
|
360
|
+
it('should retrieve all session groups', async () => {
|
361
|
+
// Execute
|
362
|
+
const result = await sessionService.getSessionGroups();
|
363
|
+
|
364
|
+
// Assert
|
365
|
+
const groups = [
|
366
|
+
{ id: 'group-2', name: 'group-B', sort: 1 },
|
367
|
+
{ id: 'group-1', name: 'group-A', sort: 2 },
|
368
|
+
];
|
369
|
+
expect(result).toMatchObject(groups);
|
370
|
+
});
|
371
|
+
});
|
372
|
+
|
373
|
+
describe('updateSessionGroup', () => {
|
374
|
+
it('should update a session group', async () => {
|
375
|
+
// Setup
|
376
|
+
const groupId = 'group-1';
|
377
|
+
const data = { name: 'Updated Group', sort: 2 };
|
378
|
+
|
379
|
+
// Execute
|
380
|
+
await sessionService.updateSessionGroup(groupId, data);
|
381
|
+
|
382
|
+
// Assert
|
383
|
+
const result = await clientDB.query.sessionGroups.findFirst({
|
384
|
+
where: eq(sessionGroups.id, groupId),
|
385
|
+
});
|
386
|
+
expect(result).toMatchObject({ id: groupId, ...data });
|
387
|
+
});
|
388
|
+
});
|
389
|
+
|
390
|
+
describe('updateSessionGroupOrder', () => {
|
391
|
+
it('should update the order of session groups', async () => {
|
392
|
+
// Setup
|
393
|
+
const sortMap = [
|
394
|
+
{ id: 'group-1', sort: 2 },
|
395
|
+
{ id: 'group-2', sort: 1 },
|
396
|
+
];
|
397
|
+
|
398
|
+
// Execute
|
399
|
+
await sessionService.updateSessionGroupOrder(sortMap);
|
400
|
+
|
401
|
+
// Assert
|
402
|
+
const data = await clientDB.query.sessionGroups.findMany({
|
403
|
+
where: eq(sessionGroups.userId, userId),
|
404
|
+
});
|
405
|
+
expect(data).toMatchObject([
|
406
|
+
{ id: 'group-1', sort: 2 },
|
407
|
+
{ id: 'group-2', sort: 1 },
|
408
|
+
]);
|
409
|
+
});
|
410
|
+
});
|
411
|
+
});
|
@@ -0,0 +1,184 @@
|
|
1
|
+
import { DeepPartial } from 'utility-types';
|
2
|
+
|
3
|
+
import { INBOX_SESSION_ID } from '@/const/session';
|
4
|
+
import { clientDB } from '@/database/client/db';
|
5
|
+
import { AgentItem } from '@/database/schemas';
|
6
|
+
import { SessionModel } from '@/database/server/models/session';
|
7
|
+
import { SessionGroupModel } from '@/database/server/models/sessionGroup';
|
8
|
+
import { BaseClientService } from '@/services/baseClientService';
|
9
|
+
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
|
10
|
+
import { MetaData } from '@/types/meta';
|
11
|
+
import {
|
12
|
+
ChatSessionList,
|
13
|
+
LobeAgentSession,
|
14
|
+
LobeSessionType,
|
15
|
+
LobeSessions,
|
16
|
+
SessionGroupItem,
|
17
|
+
SessionGroups,
|
18
|
+
UpdateSessionParams,
|
19
|
+
} from '@/types/session';
|
20
|
+
|
21
|
+
import { ISessionService } from './type';
|
22
|
+
|
23
|
+
export class ClientService extends BaseClientService implements ISessionService {
|
24
|
+
private get sessionModel(): SessionModel {
|
25
|
+
return new SessionModel(clientDB as any, this.userId);
|
26
|
+
}
|
27
|
+
|
28
|
+
private get sessionGroupModel(): SessionGroupModel {
|
29
|
+
return new SessionGroupModel(clientDB as any, this.userId);
|
30
|
+
}
|
31
|
+
|
32
|
+
async createSession(type: LobeSessionType, data: Partial<LobeAgentSession>): Promise<string> {
|
33
|
+
const { config, group, meta, ...session } = data;
|
34
|
+
|
35
|
+
const item = await this.sessionModel.create({
|
36
|
+
config: { ...config, ...meta } as any,
|
37
|
+
session: { ...session, groupId: group },
|
38
|
+
type,
|
39
|
+
});
|
40
|
+
if (!item) {
|
41
|
+
throw new Error('session create Error');
|
42
|
+
}
|
43
|
+
return item.id;
|
44
|
+
}
|
45
|
+
|
46
|
+
async batchCreateSessions(importSessions: LobeSessions) {
|
47
|
+
// @ts-ignore
|
48
|
+
return this.sessionModel.batchCreate(importSessions);
|
49
|
+
}
|
50
|
+
|
51
|
+
async cloneSession(id: string, newTitle: string): Promise<string | undefined> {
|
52
|
+
const res = await this.sessionModel.duplicate(id, newTitle);
|
53
|
+
|
54
|
+
if (res) return res?.id;
|
55
|
+
}
|
56
|
+
|
57
|
+
async getGroupedSessions(): Promise<ChatSessionList> {
|
58
|
+
return this.sessionModel.queryWithGroups();
|
59
|
+
}
|
60
|
+
|
61
|
+
async getSessionConfig(id: string): Promise<LobeAgentConfig> {
|
62
|
+
const res = await this.sessionModel.findByIdOrSlug(id);
|
63
|
+
|
64
|
+
if (!res) throw new Error('Session not found');
|
65
|
+
|
66
|
+
return res.agent as LobeAgentConfig;
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
* 这个方法要对应移除的
|
71
|
+
*/
|
72
|
+
async getSessionsByType(type: 'agent' | 'group' | 'all' = 'all'): Promise<LobeSessions> {
|
73
|
+
switch (type) {
|
74
|
+
// TODO: add a filter to get only agents or agents
|
75
|
+
case 'group': {
|
76
|
+
// @ts-ignore
|
77
|
+
return this.sessionModel.query();
|
78
|
+
}
|
79
|
+
case 'agent': {
|
80
|
+
// @ts-ignore
|
81
|
+
return this.sessionModel.query();
|
82
|
+
}
|
83
|
+
|
84
|
+
case 'all': {
|
85
|
+
// @ts-ignore
|
86
|
+
return this.sessionModel.query();
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
async countSessions() {
|
92
|
+
return this.sessionModel.count();
|
93
|
+
}
|
94
|
+
|
95
|
+
async searchSessions(keyword: string) {
|
96
|
+
return this.sessionModel.queryByKeyword(keyword);
|
97
|
+
}
|
98
|
+
|
99
|
+
async updateSession(id: string, value: Partial<UpdateSessionParams>) {
|
100
|
+
return this.sessionModel.update(id, {
|
101
|
+
...value,
|
102
|
+
groupId: value.group === 'default' ? null : value.group,
|
103
|
+
});
|
104
|
+
}
|
105
|
+
|
106
|
+
async updateSessionConfig(
|
107
|
+
activeId: string,
|
108
|
+
config: DeepPartial<LobeAgentConfig>,
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
110
|
+
_?: AbortSignal,
|
111
|
+
) {
|
112
|
+
const session = await this.sessionModel.findByIdOrSlug(activeId);
|
113
|
+
if (!session || !config) return;
|
114
|
+
|
115
|
+
return this.sessionModel.updateConfig(session.agent.id, config as AgentItem);
|
116
|
+
}
|
117
|
+
|
118
|
+
async updateSessionMeta(
|
119
|
+
activeId: string,
|
120
|
+
meta: Partial<MetaData>,
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
122
|
+
_?: AbortSignal,
|
123
|
+
) {
|
124
|
+
// inbox 不允许修改 meta
|
125
|
+
if (activeId === INBOX_SESSION_ID) return;
|
126
|
+
|
127
|
+
return this.sessionModel.update(activeId, meta);
|
128
|
+
}
|
129
|
+
|
130
|
+
async updateSessionChatConfig(
|
131
|
+
activeId: string,
|
132
|
+
config: DeepPartial<LobeAgentChatConfig>,
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
134
|
+
_?: AbortSignal,
|
135
|
+
) {
|
136
|
+
return this.updateSessionConfig(activeId, { chatConfig: config });
|
137
|
+
}
|
138
|
+
|
139
|
+
async removeSession(id: string) {
|
140
|
+
return this.sessionModel.delete(id);
|
141
|
+
}
|
142
|
+
|
143
|
+
async removeAllSessions() {
|
144
|
+
return this.sessionModel.deleteAll();
|
145
|
+
}
|
146
|
+
|
147
|
+
// ************************************** //
|
148
|
+
// *********** SessionGroup *********** //
|
149
|
+
// ************************************** //
|
150
|
+
|
151
|
+
async createSessionGroup(name: string, sort?: number) {
|
152
|
+
const item = await this.sessionGroupModel.create({ name, sort });
|
153
|
+
if (!item) {
|
154
|
+
throw new Error('session group create Error');
|
155
|
+
}
|
156
|
+
|
157
|
+
return item.id;
|
158
|
+
}
|
159
|
+
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
161
|
+
async batchCreateSessionGroups(_groups: SessionGroups) {
|
162
|
+
return { added: 0, ids: [], skips: [], success: true };
|
163
|
+
}
|
164
|
+
|
165
|
+
async removeSessionGroup(id: string) {
|
166
|
+
return await this.sessionGroupModel.delete(id);
|
167
|
+
}
|
168
|
+
|
169
|
+
async updateSessionGroup(id: string, data: Partial<SessionGroupItem>) {
|
170
|
+
return this.sessionGroupModel.update(id, data);
|
171
|
+
}
|
172
|
+
|
173
|
+
async updateSessionGroupOrder(sortMap: { id: string; sort: number }[]) {
|
174
|
+
return this.sessionGroupModel.updateOrder(sortMap);
|
175
|
+
}
|
176
|
+
|
177
|
+
async getSessionGroups(): Promise<SessionGroupItem[]> {
|
178
|
+
return this.sessionGroupModel.query();
|
179
|
+
}
|
180
|
+
|
181
|
+
async removeSessionGroups() {
|
182
|
+
return this.sessionGroupModel.deleteAll();
|
183
|
+
}
|
184
|
+
}
|
@@ -16,13 +16,21 @@ import {
|
|
16
16
|
|
17
17
|
export interface ISessionService {
|
18
18
|
createSession(type: LobeSessionType, defaultValue: Partial<LobeAgentSession>): Promise<string>;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* 需要废弃
|
22
|
+
* @deprecated
|
23
|
+
*/
|
19
24
|
batchCreateSessions(importSessions: LobeSessions): Promise<any>;
|
20
25
|
cloneSession(id: string, newTitle: string): Promise<string | undefined>;
|
21
26
|
|
22
27
|
getGroupedSessions(): Promise<ChatSessionList>;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* @deprecated
|
31
|
+
*/
|
23
32
|
getSessionsByType(type: 'agent' | 'group' | 'all'): Promise<LobeSessions>;
|
24
33
|
countSessions(): Promise<number>;
|
25
|
-
hasSessions(): Promise<boolean>;
|
26
34
|
searchSessions(keyword: string): Promise<LobeSessions>;
|
27
35
|
|
28
36
|
updateSession(
|
@@ -53,6 +61,11 @@ export interface ISessionService {
|
|
53
61
|
// ************************************** //
|
54
62
|
|
55
63
|
createSessionGroup(name: string, sort?: number): Promise<string>;
|
64
|
+
|
65
|
+
/**
|
66
|
+
* 需要废弃
|
67
|
+
* @deprecated
|
68
|
+
*/
|
56
69
|
batchCreateSessionGroups(groups: SessionGroups): Promise<BatchTaskResult>;
|
57
70
|
|
58
71
|
getSessionGroups(): Promise<SessionGroupItem[]>;
|
@@ -4,7 +4,7 @@ import { SessionModel } from '@/database/_deprecated/models/session';
|
|
4
4
|
import { CreateTopicParams, TopicModel } from '@/database/_deprecated/models/topic';
|
5
5
|
import { ChatTopic } from '@/types/topic';
|
6
6
|
|
7
|
-
import { ClientService } from './
|
7
|
+
import { ClientService } from './_deprecated';
|
8
8
|
|
9
9
|
const topicService = new ClientService();
|
10
10
|
// Mock the TopicModel
|
@@ -1,6 +1,9 @@
|
|
1
|
-
|
2
|
-
import { ClientService } from './
|
1
|
+
import { ClientService as DeprecatedService } from './_deprecated';
|
2
|
+
import { ClientService } from './pglite';
|
3
3
|
import { ServerService } from './server';
|
4
4
|
|
5
|
+
const clientService =
|
6
|
+
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
|
7
|
+
|
5
8
|
export const topicService =
|
6
|
-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() :
|
9
|
+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
|