@lobehub/lobehub 2.0.0-next.13 → 2.0.0-next.14

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.
Files changed (210) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/changelog/v1.json +9 -0
  3. package/package.json +1 -1
  4. package/packages/const/src/version.ts +3 -3
  5. package/packages/database/src/repositories/dataImporter/deprecated/__tests__/index.test.ts +2 -1
  6. package/packages/database/src/repositories/dataImporter/deprecated/index.ts +7 -1
  7. package/src/app/[variants]/(main)/(mobile)/me/(home)/__tests__/useCategory.test.tsx +9 -0
  8. package/src/app/[variants]/(main)/(mobile)/me/(home)/layout.tsx +0 -2
  9. package/src/app/[variants]/(main)/chat/@session/features/SessionListContent/List/Item/Actions.tsx +3 -28
  10. package/src/app/[variants]/(main)/chat/_layout/Desktop/index.tsx +0 -2
  11. package/src/app/[variants]/(main)/chat/_layout/Mobile.tsx +1 -5
  12. package/src/app/[variants]/(main)/chat/settings/features/HeaderContent.tsx +2 -62
  13. package/src/app/[variants]/(main)/image/page.tsx +0 -2
  14. package/src/app/[variants]/(main)/profile/_layout/Desktop/index.tsx +23 -24
  15. package/src/app/[variants]/(main)/profile/_layout/Mobile/index.tsx +5 -9
  16. package/src/app/[variants]/(main)/settings/_layout/Desktop/index.tsx +0 -2
  17. package/src/app/[variants]/(main)/settings/_layout/Mobile/index.tsx +0 -2
  18. package/src/app/[variants]/(main)/settings/provider/(list)/ProviderGrid/Card.tsx +1 -1
  19. package/src/app/[variants]/loading/index.tsx +1 -10
  20. package/src/components/Link.tsx +12 -0
  21. package/src/envs/app.ts +5 -8
  22. package/src/features/DataImporter/index.tsx +15 -60
  23. package/src/features/DevPanel/PostgresViewer/usePgTable.ts +3 -2
  24. package/src/hooks/useInterceptingRoutes.test.ts +21 -3
  25. package/src/libs/trpc/client/index.ts +0 -1
  26. package/src/libs/trpc/client/lambda.ts +8 -5
  27. package/src/server/routers/desktop/mcp.ts +1 -3
  28. package/src/server/routers/lambda/config/index.test.ts +2 -2
  29. package/src/server/routers/tools/mcp.ts +2 -3
  30. package/src/server/routers/tools/search.test.ts +1 -7
  31. package/src/server/routers/tools/search.ts +1 -4
  32. package/src/services/__tests__/tool.test.ts +0 -3
  33. package/src/services/aiModel/index.test.ts +0 -3
  34. package/src/services/aiModel/index.ts +1 -7
  35. package/src/services/aiProvider/index.test.ts +0 -3
  36. package/src/services/aiProvider/index.ts +1 -7
  37. package/src/services/chatGroup/index.ts +1 -10
  38. package/src/services/config.ts +1 -65
  39. package/src/services/export/index.ts +1 -4
  40. package/src/services/file/index.ts +1 -11
  41. package/src/services/import/index.ts +1 -7
  42. package/src/services/message/index.ts +1 -11
  43. package/src/services/plugin/index.ts +1 -11
  44. package/src/services/session/index.ts +1 -11
  45. package/src/services/tableViewer/client.ts +12 -15
  46. package/src/services/thread/index.ts +1 -7
  47. package/src/services/topic/index.ts +1 -11
  48. package/src/services/user/index.ts +1 -13
  49. package/src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts +0 -241
  50. package/src/store/chat/slices/aiChat/actions/__tests__/generateAIChatV2.test.ts +26 -1
  51. package/src/store/chat/slices/aiChat/actions/__tests__/helpers.ts +3 -1
  52. package/src/store/chat/slices/aiChat/actions/generateAIChat.ts +1 -138
  53. package/src/store/user/slices/common/action.test.ts +1 -4
  54. package/src/app/(backend)/trpc/edge/[trpc]/route.ts +0 -26
  55. package/src/app/[variants]/(main)/(mobile)/me/data/features/Category.tsx +0 -48
  56. package/src/app/[variants]/(main)/(mobile)/me/data/features/Header.tsx +0 -33
  57. package/src/app/[variants]/(main)/(mobile)/me/data/layout.tsx +0 -13
  58. package/src/app/[variants]/(main)/(mobile)/me/data/loading.tsx +0 -5
  59. package/src/app/[variants]/(main)/(mobile)/me/data/page.tsx +0 -29
  60. package/src/app/[variants]/(main)/chat/features/Migration/DBReader.ts +0 -290
  61. package/src/app/[variants]/(main)/chat/features/Migration/ExportConfigButton.tsx +0 -35
  62. package/src/app/[variants]/(main)/chat/features/Migration/Failed.tsx +0 -120
  63. package/src/app/[variants]/(main)/chat/features/Migration/Modal.tsx +0 -81
  64. package/src/app/[variants]/(main)/chat/features/Migration/Start.tsx +0 -108
  65. package/src/app/[variants]/(main)/chat/features/Migration/UpgradeButton.tsx +0 -71
  66. package/src/app/[variants]/(main)/chat/features/Migration/const.ts +0 -15
  67. package/src/app/[variants]/(main)/chat/features/Migration/index.tsx +0 -50
  68. package/src/app/[variants]/loading/Client/Content.tsx +0 -48
  69. package/src/app/[variants]/loading/Client/Error.tsx +0 -27
  70. package/src/app/[variants]/loading/Client/Redirect.tsx +0 -47
  71. package/src/app/[variants]/loading/Client/index.tsx +0 -22
  72. package/src/components/InnerLink.tsx +0 -20
  73. package/src/database/_deprecated/core/__tests__/db-upgrade.test.ts +0 -42
  74. package/src/database/_deprecated/core/__tests__/db.test.ts +0 -79
  75. package/src/database/_deprecated/core/__tests__/model.test.ts +0 -55
  76. package/src/database/_deprecated/core/db.ts +0 -246
  77. package/src/database/_deprecated/core/index.ts +0 -2
  78. package/src/database/_deprecated/core/migrations/migrateSettingsToUser/fixtures/input.json +0 -55
  79. package/src/database/_deprecated/core/migrations/migrateSettingsToUser/fixtures/output.json +0 -60
  80. package/src/database/_deprecated/core/migrations/migrateSettingsToUser/index.test.ts +0 -14
  81. package/src/database/_deprecated/core/migrations/migrateSettingsToUser/index.ts +0 -22
  82. package/src/database/_deprecated/core/migrations/migrateSettingsToUser/type.ts +0 -105
  83. package/src/database/_deprecated/core/model.ts +0 -218
  84. package/src/database/_deprecated/core/schemas.ts +0 -88
  85. package/src/database/_deprecated/core/types/db.ts +0 -15
  86. package/src/database/_deprecated/models/__DEBUG.ts +0 -124
  87. package/src/database/_deprecated/models/__tests__/file.test.ts +0 -83
  88. package/src/database/_deprecated/models/__tests__/message.test.ts +0 -426
  89. package/src/database/_deprecated/models/__tests__/plugin.test.ts +0 -81
  90. package/src/database/_deprecated/models/__tests__/session.test.ts +0 -253
  91. package/src/database/_deprecated/models/__tests__/sessionGroup.test.ts +0 -220
  92. package/src/database/_deprecated/models/__tests__/topic.test.ts +0 -523
  93. package/src/database/_deprecated/models/__tests__/user.test.ts +0 -82
  94. package/src/database/_deprecated/models/file.ts +0 -51
  95. package/src/database/_deprecated/models/message.ts +0 -277
  96. package/src/database/_deprecated/models/plugin.ts +0 -62
  97. package/src/database/_deprecated/models/session.ts +0 -271
  98. package/src/database/_deprecated/models/sessionGroup.ts +0 -93
  99. package/src/database/_deprecated/models/topic.ts +0 -250
  100. package/src/database/_deprecated/models/user.ts +0 -69
  101. package/src/database/_deprecated/schemas/files.ts +0 -39
  102. package/src/database/_deprecated/schemas/message.ts +0 -50
  103. package/src/database/_deprecated/schemas/plugin.ts +0 -12
  104. package/src/database/_deprecated/schemas/session.ts +0 -54
  105. package/src/database/_deprecated/schemas/sessionGroup.ts +0 -8
  106. package/src/database/_deprecated/schemas/topic.ts +0 -12
  107. package/src/database/_deprecated/schemas/user.ts +0 -40
  108. package/src/features/DataImporter/_deprecated.ts +0 -43
  109. package/src/features/InitClientDB/EnableModal.tsx +0 -118
  110. package/src/features/InitClientDB/ErrorResult.tsx +0 -143
  111. package/src/features/InitClientDB/InitIndicator.tsx +0 -124
  112. package/src/features/InitClientDB/PGliteIcon.tsx +0 -28
  113. package/src/features/InitClientDB/features/DatabaseRepair/Backup.tsx +0 -75
  114. package/src/features/InitClientDB/features/DatabaseRepair/Diagnosis.tsx +0 -98
  115. package/src/features/InitClientDB/features/DatabaseRepair/Repair.tsx +0 -218
  116. package/src/features/InitClientDB/features/DatabaseRepair/index.tsx +0 -91
  117. package/src/features/InitClientDB/index.tsx +0 -37
  118. package/src/libs/trpc/client/edge.ts +0 -26
  119. package/src/libs/trpc/edge/context.ts +0 -71
  120. package/src/libs/trpc/edge/index.ts +0 -45
  121. package/src/libs/trpc/edge/init.ts +0 -26
  122. package/src/libs/trpc/edge/middleware/jwtPayload.test.ts +0 -75
  123. package/src/libs/trpc/edge/middleware/jwtPayload.ts +0 -14
  124. package/src/migrations/FromV0ToV1.ts +0 -10
  125. package/src/migrations/FromV1ToV2/fixtures/input-v1-session.json +0 -191
  126. package/src/migrations/FromV1ToV2/fixtures/output-v2.json +0 -202
  127. package/src/migrations/FromV1ToV2/index.ts +0 -82
  128. package/src/migrations/FromV1ToV2/migrations.test.ts +0 -224
  129. package/src/migrations/FromV1ToV2/types/v1.ts +0 -78
  130. package/src/migrations/FromV1ToV2/types/v2.ts +0 -52
  131. package/src/migrations/FromV2ToV3/fixtures/input-v2-session.json +0 -72
  132. package/src/migrations/FromV2ToV3/fixtures/output-v3-from-v1.json +0 -203
  133. package/src/migrations/FromV2ToV3/fixtures/output-v3.json +0 -74
  134. package/src/migrations/FromV2ToV3/index.ts +0 -30
  135. package/src/migrations/FromV2ToV3/migrations.test.ts +0 -42
  136. package/src/migrations/FromV2ToV3/types/v3.ts +0 -27
  137. package/src/migrations/FromV3ToV4/fixtures/azure-input-v3.json +0 -79
  138. package/src/migrations/FromV3ToV4/fixtures/azure-output-v4.json +0 -75
  139. package/src/migrations/FromV3ToV4/fixtures/ollama-input-v3.json +0 -85
  140. package/src/migrations/FromV3ToV4/fixtures/ollama-output-v4.json +0 -86
  141. package/src/migrations/FromV3ToV4/fixtures/openai-input-v3.json +0 -77
  142. package/src/migrations/FromV3ToV4/fixtures/openai-output-v4.json +0 -77
  143. package/src/migrations/FromV3ToV4/fixtures/openrouter-input-v3.json +0 -82
  144. package/src/migrations/FromV3ToV4/fixtures/openrouter-output-v4.json +0 -85
  145. package/src/migrations/FromV3ToV4/fixtures/output-v4-from-v1.json +0 -203
  146. package/src/migrations/FromV3ToV4/index.ts +0 -102
  147. package/src/migrations/FromV3ToV4/migrations.test.ts +0 -195
  148. package/src/migrations/FromV3ToV4/types/v3.ts +0 -52
  149. package/src/migrations/FromV3ToV4/types/v4.ts +0 -37
  150. package/src/migrations/FromV4ToV5/fixtures/from-v1-to-v5-output.json +0 -245
  151. package/src/migrations/FromV4ToV5/fixtures/function-input-v4.json +0 -96
  152. package/src/migrations/FromV4ToV5/fixtures/function-output-v5.json +0 -120
  153. package/src/migrations/FromV4ToV5/index.ts +0 -58
  154. package/src/migrations/FromV4ToV5/migrations.test.ts +0 -49
  155. package/src/migrations/FromV4ToV5/types/v4.ts +0 -21
  156. package/src/migrations/FromV4ToV5/types/v5.ts +0 -27
  157. package/src/migrations/FromV5ToV6/fixtures/from-v1-to-v6-output.json +0 -247
  158. package/src/migrations/FromV5ToV6/fixtures/session-input-v5.json +0 -81
  159. package/src/migrations/FromV5ToV6/fixtures/session-output-v6.json +0 -85
  160. package/src/migrations/FromV5ToV6/index.ts +0 -61
  161. package/src/migrations/FromV5ToV6/migrations.test.ts +0 -50
  162. package/src/migrations/FromV5ToV6/types/v5.ts +0 -48
  163. package/src/migrations/FromV5ToV6/types/v6.ts +0 -63
  164. package/src/migrations/FromV6ToV7/fixtures/output-v7-from-v1.json +0 -203
  165. package/src/migrations/FromV6ToV7/fixtures/provider-input-v6.json +0 -103
  166. package/src/migrations/FromV6ToV7/fixtures/provider-output-v7.json +0 -118
  167. package/src/migrations/FromV6ToV7/index.ts +0 -101
  168. package/src/migrations/FromV6ToV7/migrations.test.ts +0 -64
  169. package/src/migrations/FromV6ToV7/types/v6.ts +0 -61
  170. package/src/migrations/FromV6ToV7/types/v7.ts +0 -69
  171. package/src/migrations/VersionController.test.ts +0 -88
  172. package/src/migrations/VersionController.ts +0 -67
  173. package/src/migrations/index.ts +0 -61
  174. package/src/server/routers/edge/appStatus.ts +0 -3
  175. package/src/server/routers/edge/index.ts +0 -14
  176. package/src/server/routers/edge/upload.ts +0 -16
  177. package/src/services/aiModel/client.ts +0 -70
  178. package/src/services/aiProvider/client.ts +0 -58
  179. package/src/services/baseClientService/index.ts +0 -9
  180. package/src/services/chatGroup/client.ts +0 -63
  181. package/src/services/export/_deprecated.ts +0 -155
  182. package/src/services/export/client.ts +0 -15
  183. package/src/services/file/_deprecated.test.ts +0 -119
  184. package/src/services/file/_deprecated.ts +0 -80
  185. package/src/services/file/client.test.ts +0 -199
  186. package/src/services/file/client.ts +0 -85
  187. package/src/services/import/_deprecated.ts +0 -115
  188. package/src/services/import/client.test.ts +0 -1015
  189. package/src/services/import/client.ts +0 -64
  190. package/src/services/message/_deprecated.test.ts +0 -398
  191. package/src/services/message/_deprecated.ts +0 -168
  192. package/src/services/message/client.test.ts +0 -410
  193. package/src/services/message/client.ts +0 -192
  194. package/src/services/plugin/_deprecated.test.ts +0 -162
  195. package/src/services/plugin/_deprecated.ts +0 -42
  196. package/src/services/plugin/client.test.ts +0 -177
  197. package/src/services/plugin/client.ts +0 -46
  198. package/src/services/session/_deprecated.test.ts +0 -440
  199. package/src/services/session/_deprecated.ts +0 -190
  200. package/src/services/session/client.test.ts +0 -413
  201. package/src/services/session/client.ts +0 -193
  202. package/src/services/thread/client.ts +0 -51
  203. package/src/services/topic/_deprecated.test.ts +0 -245
  204. package/src/services/topic/_deprecated.ts +0 -75
  205. package/src/services/topic/client.ts +0 -89
  206. package/src/services/topic/pglite.test.ts +0 -212
  207. package/src/services/user/_deprecated.test.ts +0 -101
  208. package/src/services/user/_deprecated.ts +0 -70
  209. package/src/services/user/client.test.ts +0 -111
  210. package/src/services/user/client.ts +0 -104
@@ -1,440 +0,0 @@
1
- import { Mock, beforeEach, describe, expect, it, vi } from 'vitest';
2
-
3
- import { SessionModel } from '@/database/_deprecated/models/session';
4
- import { SessionGroupModel } from '@/database/_deprecated/models/sessionGroup';
5
- import { LobeAgentConfig } from '@/types/agent';
6
- import { LobeAgentSession, LobeSessionType, SessionGroups } from '@/types/session';
7
-
8
- import { ClientService } from './_deprecated';
9
-
10
- const sessionService = new ClientService();
11
-
12
- // Mock the SessionModel
13
- vi.mock('@/database/_deprecated/models/session', () => {
14
- return {
15
- SessionModel: {
16
- create: vi.fn(),
17
- query: vi.fn(),
18
- delete: vi.fn(),
19
- clearTable: vi.fn(),
20
- update: vi.fn(),
21
- count: vi.fn(),
22
- batchCreate: vi.fn(),
23
- findById: vi.fn(),
24
- isEmpty: vi.fn(),
25
- queryByKeyword: vi.fn(),
26
- updateConfig: vi.fn(),
27
- queryByGroupIds: vi.fn(),
28
- updatePinned: vi.fn(),
29
- duplicate: vi.fn(),
30
- queryWithGroups: vi.fn(),
31
- },
32
- };
33
- });
34
-
35
- // Mock the SessionGroupModel
36
- vi.mock('@/database/_deprecated/models/sessionGroup', () => {
37
- return {
38
- SessionGroupModel: {
39
- create: vi.fn(),
40
- query: vi.fn(),
41
- delete: vi.fn(),
42
- clear: vi.fn(),
43
- update: vi.fn(),
44
- batchCreate: vi.fn(),
45
- isEmpty: vi.fn(),
46
- updateOrder: vi.fn(),
47
- queryByKeyword: vi.fn(),
48
- updateConfig: vi.fn(),
49
- queryByGroupIds: vi.fn(),
50
- },
51
- };
52
- });
53
-
54
- describe('SessionService', () => {
55
- const mockSessionId = 'mock-session-id';
56
- const mockSession = {
57
- id: mockSessionId,
58
- type: 'agent',
59
- meta: { title: 'Mock Session' },
60
- } as LobeAgentSession;
61
- const mockSessions = [mockSession];
62
-
63
- beforeEach(() => {
64
- // Reset all mocks before running each test case
65
- vi.resetAllMocks();
66
- });
67
-
68
- describe('createSession', () => {
69
- it('should create a new session and return its id', async () => {
70
- // Setup
71
- const sessionType = LobeSessionType.Agent;
72
- const defaultValue = { meta: { title: 'New Session' } } as Partial<LobeAgentSession>;
73
- (SessionModel.create as Mock).mockResolvedValue(mockSession);
74
-
75
- // Execute
76
- const sessionId = await sessionService.createSession(sessionType, defaultValue);
77
-
78
- // Assert
79
- expect(SessionModel.create).toHaveBeenCalledWith(sessionType, defaultValue);
80
- expect(sessionId).toBe(mockSessionId);
81
- });
82
-
83
- it('should throw an error if session creation fails', async () => {
84
- // Setup
85
- const sessionType = LobeSessionType.Agent;
86
- const defaultValue = { meta: { title: 'New Session' } } as Partial<LobeAgentSession>;
87
- (SessionModel.create as Mock).mockResolvedValue(null);
88
-
89
- // Execute & Assert
90
- await expect(sessionService.createSession(sessionType, defaultValue)).rejects.toThrow(
91
- 'session create Error',
92
- );
93
- });
94
- });
95
-
96
- describe('batchCreateSessions', () => {
97
- it('should batch create sessions', async () => {
98
- // Setup
99
- (SessionModel.batchCreate as Mock).mockResolvedValue(mockSessions);
100
-
101
- // Execute
102
- const result = await sessionService.batchCreateSessions(mockSessions);
103
-
104
- // Assert
105
- expect(SessionModel.batchCreate).toHaveBeenCalledWith(mockSessions);
106
- expect(result).toBe(mockSessions);
107
- });
108
- });
109
-
110
- describe('getSessionsByType', () => {
111
- it('should retrieve sessions with their group ids', async () => {
112
- // Setup
113
- (SessionModel.query as Mock).mockResolvedValue(mockSessions);
114
-
115
- // Execute
116
- const sessions = await sessionService.getSessionsByType();
117
-
118
- // Assert
119
- expect(SessionModel.query).toHaveBeenCalled();
120
- expect(sessions).toBe(mockSessions);
121
- });
122
-
123
- it('should retrieve all agent sessions', async () => {
124
- // Setup
125
- // Assuming that SessionModel.query has been modified to accept filters
126
- const agentSessions = mockSessions.filter((session) => session.type === 'agent');
127
- (SessionModel.query as Mock).mockResolvedValue(agentSessions);
128
-
129
- // Execute
130
- const result = await sessionService.getSessionsByType('agent');
131
-
132
- // Assert
133
- // Assuming that SessionModel.query would be called with a filter for agents
134
- expect(SessionModel.query).toHaveBeenCalled(); // Add filter argument if applicable
135
- expect(result).toBe(agentSessions);
136
- });
137
- });
138
-
139
- describe('removeSession', () => {
140
- it('should remove a session by its id', async () => {
141
- // Setup
142
- (SessionModel.delete as Mock).mockResolvedValue(true);
143
-
144
- // Execute
145
- const result = await sessionService.removeSession(mockSessionId);
146
-
147
- // Assert
148
- expect(SessionModel.delete).toHaveBeenCalledWith(mockSessionId);
149
- expect(result).toBe(true);
150
- });
151
- });
152
-
153
- describe('removeAllSessions', () => {
154
- it('should clear all sessions from the table', async () => {
155
- // Setup
156
- (SessionModel.clearTable as Mock).mockResolvedValue(true);
157
-
158
- // Execute
159
- const result = await sessionService.removeAllSessions();
160
-
161
- // Assert
162
- expect(SessionModel.clearTable).toHaveBeenCalled();
163
- expect(result).toBe(true);
164
- });
165
- });
166
-
167
- describe('updateSession', () => {
168
- it('should update the group of a session', async () => {
169
- // Setup
170
- const groupId = 'new-group';
171
- (SessionModel.update as Mock).mockResolvedValue({ ...mockSession, group: groupId });
172
-
173
- // Execute
174
- const result = await sessionService.updateSession(mockSessionId, { group: groupId });
175
-
176
- // Assert
177
- expect(SessionModel.update).toHaveBeenCalledWith(mockSessionId, { group: groupId });
178
- expect(result).toEqual({ ...mockSession, group: groupId });
179
- });
180
-
181
- it('should update the meta of a session', async () => {
182
- // Setup
183
- const newMeta = { description: 'Updated description' };
184
- (SessionModel.update as Mock).mockResolvedValue({ ...mockSession, meta: newMeta });
185
-
186
- // Execute
187
- const result = await sessionService.updateSession(mockSessionId, { meta: newMeta });
188
-
189
- // Assert
190
- expect(SessionModel.update).toHaveBeenCalledWith(mockSessionId, { meta: newMeta });
191
- expect(result).toEqual({ ...mockSession, meta: newMeta });
192
- });
193
-
194
- it('should update the pinned status of a session', async () => {
195
- // Setup
196
- const pinned = true;
197
-
198
- // Execute
199
- await sessionService.updateSession(mockSessionId, { pinned });
200
-
201
- // Assert
202
- expect(SessionModel.update).toHaveBeenCalledWith(mockSessionId, { pinned: 1 });
203
- });
204
- });
205
-
206
- describe('updateSessionConfig', () => {
207
- it('should update the config of a session', async () => {
208
- // Setup
209
- const newConfig = { model: 'abc' } as LobeAgentConfig;
210
- (SessionModel.updateConfig as Mock).mockResolvedValue({ ...mockSession, config: newConfig });
211
-
212
- // Execute
213
- const result = await sessionService.updateSessionConfig(mockSessionId, newConfig);
214
-
215
- // Assert
216
- expect(SessionModel.updateConfig).toHaveBeenCalledWith(mockSessionId, newConfig);
217
- expect(result).toEqual({ ...mockSession, config: newConfig });
218
- });
219
- });
220
-
221
- describe('countSessions', () => {
222
- it('should return false if no sessions exist', async () => {
223
- // Setup
224
- (SessionModel.count as Mock).mockResolvedValue(0);
225
-
226
- // Execute
227
- const result = await sessionService.countSessions();
228
-
229
- // Assert
230
- expect(SessionModel.count).toHaveBeenCalled();
231
- expect(result).toBe(0);
232
- });
233
-
234
- it('should return true if sessions exist', async () => {
235
- // Setup
236
- (SessionModel.count as Mock).mockResolvedValue(1);
237
-
238
- // Execute
239
- const result = await sessionService.countSessions();
240
-
241
- // Assert
242
- expect(SessionModel.count).toHaveBeenCalled();
243
- expect(result).toBe(1);
244
- });
245
- });
246
-
247
- describe('hasSessions', () => {
248
- it('should return false if no sessions exist', async () => {
249
- // Setup
250
- (SessionModel.count as Mock).mockResolvedValue(0);
251
-
252
- // Execute
253
- const result = await sessionService.hasSessions();
254
-
255
- // Assert
256
- expect(SessionModel.count).toHaveBeenCalled();
257
- expect(result).toBe(false);
258
- });
259
-
260
- it('should return true if sessions exist', async () => {
261
- // Setup
262
- (SessionModel.count as Mock).mockResolvedValue(1);
263
-
264
- // Execute
265
- const result = await sessionService.hasSessions();
266
-
267
- // Assert
268
- expect(SessionModel.count).toHaveBeenCalled();
269
- expect(result).toBe(true);
270
- });
271
- });
272
-
273
- describe('searchSessions', () => {
274
- it('should return sessions that match the keyword', async () => {
275
- // Setup
276
- const keyword = 'search';
277
- (SessionModel.queryByKeyword as Mock).mockResolvedValue(mockSessions);
278
-
279
- // Execute
280
- const result = await sessionService.searchSessions(keyword);
281
-
282
- // Assert
283
- expect(SessionModel.queryByKeyword).toHaveBeenCalledWith(keyword);
284
- expect(result).toBe(mockSessions);
285
- });
286
- });
287
-
288
- describe('cloneSession', () => {
289
- it('should duplicate a session and return its id', async () => {
290
- // Setup
291
- const newTitle = 'Duplicated Session';
292
- (SessionModel.duplicate as Mock).mockResolvedValue({
293
- ...mockSession,
294
- id: 'duplicated-session-id',
295
- });
296
-
297
- // Execute
298
- const duplicatedSessionId = await sessionService.cloneSession(mockSessionId, newTitle);
299
-
300
- // Assert
301
- expect(SessionModel.duplicate).toHaveBeenCalledWith(mockSessionId, newTitle);
302
- expect(duplicatedSessionId).toBe('duplicated-session-id');
303
- });
304
- });
305
-
306
- describe('getGroupedSessions', () => {
307
- it('should retrieve sessions with their group', async () => {
308
- // Setup
309
- (SessionModel.queryWithGroups as Mock).mockResolvedValue(mockSessions);
310
-
311
- // Execute
312
- const sessionsWithGroup = await sessionService.getGroupedSessions();
313
-
314
- // Assert
315
- expect(SessionModel.queryWithGroups).toHaveBeenCalled();
316
- expect(sessionsWithGroup).toBe(mockSessions);
317
- });
318
- });
319
-
320
- // SessionGroup related tests
321
- describe('createSessionGroup', () => {
322
- it('should create a new session group and return its id', async () => {
323
- // Setup
324
- const groupName = 'New Group';
325
- const sort = 1;
326
- (SessionGroupModel.create as Mock).mockResolvedValue({
327
- id: 'new-group-id',
328
- name: groupName,
329
- sort,
330
- });
331
-
332
- // Execute
333
- const groupId = await sessionService.createSessionGroup(groupName, sort);
334
-
335
- // Assert
336
- expect(SessionGroupModel.create).toHaveBeenCalledWith(groupName, sort);
337
- expect(groupId).toBe('new-group-id');
338
- });
339
- });
340
-
341
- describe('batchCreateSessionGroups', () => {
342
- it('should batch create session groups', async () => {
343
- // Setup
344
- const groups = [
345
- { id: 'group-1', name: 'Group 1', sort: 1 },
346
- { id: 'group-2', name: 'Group 2', sort: 2 },
347
- ] as SessionGroups;
348
-
349
- (SessionGroupModel.batchCreate as Mock).mockResolvedValue(groups);
350
-
351
- // Execute
352
- const result = await sessionService.batchCreateSessionGroups(groups);
353
-
354
- // Assert
355
- expect(SessionGroupModel.batchCreate).toHaveBeenCalledWith(groups);
356
- expect(result).toBe(groups);
357
- });
358
- });
359
-
360
- describe('removeSessionGroup', () => {
361
- it('should remove a session group by its id', async () => {
362
- // Setup
363
- const removeChildren = true;
364
- (SessionGroupModel.delete as Mock).mockResolvedValue(true);
365
-
366
- // Execute
367
- const result = await sessionService.removeSessionGroup('group-id', removeChildren);
368
-
369
- // Assert
370
- expect(SessionGroupModel.delete).toHaveBeenCalledWith('group-id', removeChildren);
371
- expect(result).toBe(true);
372
- });
373
- });
374
-
375
- describe('clearSessionGroups', () => {
376
- it('should clear all session groups', async () => {
377
- // Setup
378
- (SessionGroupModel.clear as Mock).mockResolvedValue(true);
379
-
380
- // Execute
381
- const result = await sessionService.removeSessionGroups();
382
-
383
- // Assert
384
- expect(SessionGroupModel.clear).toHaveBeenCalled();
385
- expect(result).toBe(true);
386
- });
387
- });
388
-
389
- describe('getSessionGroups', () => {
390
- it('should retrieve all session groups', async () => {
391
- // Setup
392
- const groups = [
393
- { id: 'group-1', name: 'Group 1', sort: 1 },
394
- { id: 'group-2', name: 'Group 2', sort: 2 },
395
- ];
396
- (SessionGroupModel.query as Mock).mockResolvedValue(groups);
397
-
398
- // Execute
399
- const result = await sessionService.getSessionGroups();
400
-
401
- // Assert
402
- expect(SessionGroupModel.query).toHaveBeenCalled();
403
- expect(result).toBe(groups);
404
- });
405
- });
406
-
407
- describe('updateSessionGroup', () => {
408
- it('should update a session group', async () => {
409
- // Setup
410
- const groupId = 'group-1';
411
- const data = { name: 'Updated Group', sort: 2 };
412
- (SessionGroupModel.update as Mock).mockResolvedValue({ id: groupId, ...data });
413
-
414
- // Execute
415
- const result = await sessionService.updateSessionGroup(groupId, data);
416
-
417
- // Assert
418
- expect(SessionGroupModel.update).toHaveBeenCalledWith(groupId, data);
419
- expect(result).toEqual({ id: groupId, ...data });
420
- });
421
- });
422
-
423
- describe('updateSessionGroupOrder', () => {
424
- it('should update the order of session groups', async () => {
425
- // Setup
426
- const sortMap = [
427
- { id: 'group-1', sort: 2 },
428
- { id: 'group-2', sort: 1 },
429
- ];
430
- (SessionGroupModel.updateOrder as Mock).mockResolvedValue(true);
431
-
432
- // Execute
433
- const result = await sessionService.updateSessionGroupOrder(sortMap);
434
-
435
- // Assert
436
- expect(SessionGroupModel.updateOrder).toHaveBeenCalledWith(sortMap);
437
- expect(result).toBe(true);
438
- });
439
- });
440
- });
@@ -1,190 +0,0 @@
1
- import type { PartialDeep } from 'type-fest';
2
-
3
- import { INBOX_SESSION_ID } from '@/const/session';
4
- import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
5
- import { SessionModel } from '@/database/_deprecated/models/session';
6
- import { SessionGroupModel } from '@/database/_deprecated/models/sessionGroup';
7
- import { UserModel } from '@/database/_deprecated/models/user';
8
- import { useUserStore } from '@/store/user';
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
- } from '@/types/session';
19
- import { merge } from '@/utils/merge';
20
-
21
- import { ISessionService } from './type';
22
-
23
- export class ClientService implements ISessionService {
24
- async createSession(
25
- type: LobeSessionType,
26
- defaultValue: Partial<LobeAgentSession>,
27
- ): Promise<string> {
28
- const item = await SessionModel.create(type, defaultValue);
29
- if (!item) {
30
- throw new Error('session create Error');
31
- }
32
- return item.id;
33
- }
34
-
35
- async batchCreateSessions(importSessions: LobeSessions) {
36
- // @ts-ignore
37
- return SessionModel.batchCreate(importSessions);
38
- }
39
-
40
- async cloneSession(id: string, newTitle: string): Promise<string | undefined> {
41
- const res = await SessionModel.duplicate(id, newTitle);
42
-
43
- if (res) return res?.id;
44
- }
45
-
46
- async getGroupedSessions(): Promise<ChatSessionList> {
47
- return SessionModel.queryWithGroups();
48
- }
49
-
50
- async getSessionConfig(id: string): Promise<LobeAgentConfig> {
51
- if (!id || id === INBOX_SESSION_ID) {
52
- return (await UserModel.getAgentConfig()) || DEFAULT_AGENT_CONFIG;
53
- }
54
-
55
- const res = await SessionModel.findById(id);
56
-
57
- if (!res) throw new Error('Session not found');
58
-
59
- return res.config as LobeAgentConfig;
60
- }
61
-
62
- async getSessionsByType(type: 'agent' | 'group' | 'all' = 'all'): Promise<LobeSessions> {
63
- switch (type) {
64
- // TODO: add a filter to get only agents or agents
65
- case 'group': {
66
- return SessionModel.query();
67
- }
68
- case 'agent': {
69
- return SessionModel.query();
70
- }
71
-
72
- case 'all': {
73
- return SessionModel.query();
74
- }
75
- }
76
- }
77
-
78
- async getAllAgents(): Promise<LobeSessions> {
79
- // TODO: add a filter to get only agents
80
- return SessionModel.query();
81
- }
82
-
83
- async countSessions() {
84
- return SessionModel.count();
85
- }
86
-
87
- // @ts-ignore
88
- async rankSessions() {
89
- throw new Error('Method not implemented.');
90
- }
91
-
92
- async hasSessions() {
93
- return (await this.countSessions()) !== 0;
94
- }
95
-
96
- async searchSessions(keyword: string) {
97
- return SessionModel.queryByKeyword(keyword);
98
- }
99
-
100
- async updateSession(
101
- id: string,
102
- data: Partial<Pick<LobeAgentSession, 'group' | 'meta' | 'pinned'>>,
103
- ) {
104
- const pinned = typeof data.pinned === 'boolean' ? (data.pinned ? 1 : 0) : undefined;
105
- const prev = await SessionModel.findById(id);
106
-
107
- return SessionModel.update(id, merge(prev, { ...data, pinned }));
108
- }
109
-
110
- async updateSessionConfig(
111
- activeId: string,
112
- config: PartialDeep<LobeAgentConfig>,
113
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
114
- _?: AbortSignal,
115
- ) {
116
- // TODO: 需要删除这部分处理逻辑
117
- // 后续直接给用户创建一个 inbox 的 session
118
- if (activeId === INBOX_SESSION_ID) {
119
- return useUserStore.getState().updateDefaultAgent({ config });
120
- }
121
-
122
- return SessionModel.updateConfig(activeId, config);
123
- }
124
-
125
- async updateSessionMeta(
126
- activeId: string,
127
- meta: Partial<MetaData>,
128
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
129
- _?: AbortSignal,
130
- ) {
131
- // inbox 不允许修改 meta
132
- if (activeId === INBOX_SESSION_ID) return;
133
-
134
- return SessionModel.update(activeId, { meta });
135
- }
136
-
137
- async updateSessionChatConfig(
138
- activeId: string,
139
- config: PartialDeep<LobeAgentChatConfig>,
140
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
141
- _?: AbortSignal,
142
- ) {
143
- return this.updateSessionConfig(activeId, { chatConfig: config });
144
- }
145
-
146
- async removeSession(id: string) {
147
- return SessionModel.delete(id);
148
- }
149
-
150
- async removeAllSessions() {
151
- return SessionModel.clearTable();
152
- }
153
-
154
- // ************************************** //
155
- // *********** SessionGroup *********** //
156
- // ************************************** //
157
-
158
- async createSessionGroup(name: string, sort?: number) {
159
- const item = await SessionGroupModel.create(name, sort);
160
- if (!item) {
161
- throw new Error('session group create Error');
162
- }
163
-
164
- return item.id;
165
- }
166
-
167
- async batchCreateSessionGroups(groups: SessionGroups) {
168
- return SessionGroupModel.batchCreate(groups);
169
- }
170
-
171
- async removeSessionGroup(id: string, removeChildren?: boolean) {
172
- return SessionGroupModel.delete(id, removeChildren);
173
- }
174
-
175
- async updateSessionGroup(id: string, data: Partial<SessionGroupItem>) {
176
- return SessionGroupModel.update(id, data as any);
177
- }
178
-
179
- async updateSessionGroupOrder(sortMap: { id: string; sort: number }[]) {
180
- return SessionGroupModel.updateOrder(sortMap);
181
- }
182
-
183
- async getSessionGroups(): Promise<SessionGroupItem[]> {
184
- return SessionGroupModel.query();
185
- }
186
-
187
- async removeSessionGroups() {
188
- return SessionGroupModel.clear();
189
- }
190
- }