@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,81 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
-
3
- import { DB_Plugin } from '../../schemas/plugin';
4
- import { PluginModel } from '../plugin';
5
-
6
- describe('PluginModel', () => {
7
- let pluginData: DB_Plugin;
8
-
9
- beforeEach(() => {
10
- // 设置正确结构的插件数据
11
- pluginData = {
12
- identifier: 'test-plugin',
13
- id: 'test-plugin',
14
- manifest: {},
15
- type: 'plugin',
16
- };
17
- });
18
-
19
- afterEach(async () => {
20
- // 每次测试后清理数据库
21
- await PluginModel.clear();
22
- });
23
-
24
- describe('getList', () => {
25
- it('should fetch and return the plugin list', async () => {
26
- await PluginModel.create(pluginData);
27
- const plugins = await PluginModel.getList();
28
- expect(plugins).toHaveLength(1);
29
- expect(plugins[0]).toEqual(pluginData);
30
- });
31
- });
32
-
33
- describe('create', () => {
34
- it('should create a plugin record', async () => {
35
- await PluginModel.create(pluginData);
36
- const plugins = await PluginModel.getList();
37
- expect(plugins).toHaveLength(1);
38
- expect(plugins[0]).toEqual(pluginData);
39
- });
40
- });
41
-
42
- describe('batchCreate', () => {
43
- it('should batch create plugin records', async () => {
44
- await PluginModel.batchCreate([pluginData, { ...pluginData, identifier: 'abc' }]);
45
- const plugins = await PluginModel.getList();
46
- expect(plugins).toHaveLength(2);
47
- });
48
- });
49
-
50
- describe('delete', () => {
51
- it('should delete a plugin', async () => {
52
- await PluginModel.create(pluginData);
53
- await PluginModel.delete(pluginData.identifier);
54
- const plugins = await PluginModel.getList();
55
- expect(plugins).toHaveLength(0);
56
- });
57
- });
58
-
59
- describe('update', () => {
60
- it('should update a plugin', async () => {
61
- await PluginModel.create(pluginData);
62
- const updatedPluginData: DB_Plugin = {
63
- ...pluginData,
64
- type: 'customPlugin',
65
- };
66
- await PluginModel.update(pluginData.identifier, updatedPluginData);
67
- const plugins = await PluginModel.getList();
68
- expect(plugins).toHaveLength(1);
69
- expect(plugins[0]).toEqual({ ...updatedPluginData, updatedAt: expect.any(Number) });
70
- });
71
- });
72
-
73
- describe('clear', () => {
74
- it('should clear the table', async () => {
75
- await PluginModel.create(pluginData);
76
- await PluginModel.clear();
77
- const plugins = await PluginModel.getList();
78
- expect(plugins).toHaveLength(0);
79
- });
80
- });
81
- });
@@ -1,253 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
-
3
- import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
4
- import { CreateMessageParams, MessageModel } from '@/database/_deprecated/models/message';
5
- import { SessionGroupModel } from '@/database/_deprecated/models/sessionGroup';
6
- import { TopicModel } from '@/database/_deprecated/models/topic';
7
- import { LobeAgentConfig } from '@/types/agent';
8
- import {
9
- LobeAgentSession,
10
- LobeSessionType,
11
- SessionDefaultGroup,
12
- SessionGroupId,
13
- } from '@/types/session';
14
-
15
- import { SessionModel } from '../session';
16
-
17
- describe('SessionModel', () => {
18
- let sessionData: Partial<LobeAgentSession>;
19
-
20
- beforeEach(() => {
21
- // Set up session data with the correct structure
22
- sessionData = {
23
- type: LobeSessionType.Agent,
24
- group: 'testGroup',
25
- meta: {},
26
- config: DEFAULT_AGENT_CONFIG,
27
- };
28
- });
29
-
30
- afterEach(async () => {
31
- // Clean up the database after each test
32
- await SessionModel.clearTable();
33
- });
34
- describe('create', () => {
35
- it('should create a session record', async () => {
36
- const result = await SessionModel.create('agent', sessionData);
37
-
38
- expect(result).toHaveProperty('id');
39
- // Verify that the session has been added to the database
40
- // Assuming findById is a method that retrieves a session by ID
41
- const sessionInDb = await SessionModel.findById(result.id);
42
-
43
- expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
44
- });
45
- });
46
-
47
- describe('batchCreate', () => {
48
- it('should batch create session records', async () => {
49
- const sessionsToCreate = [sessionData, sessionData];
50
- const results = await SessionModel.batchCreate(sessionsToCreate as LobeAgentSession[]);
51
-
52
- expect(results.ids).toHaveLength(sessionsToCreate.length);
53
- // Verify that the sessions have been added to the database
54
- for (const result of results.ids!) {
55
- const sessionInDb = await SessionModel.findById(result);
56
- expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
57
- }
58
- });
59
-
60
- it('should set group to default if it does not exist in SessionGroup', async () => {
61
- const sessionDataWithInvalidGroup = {
62
- ...sessionData,
63
- group: 'nonExistentGroup',
64
- } as LobeAgentSession;
65
-
66
- const results = await SessionModel.batchCreate([sessionDataWithInvalidGroup]);
67
-
68
- // Verify that the group has been set to default
69
- for (const result of results.ids!) {
70
- const sessionInDb = await SessionModel.findById(result);
71
- expect(sessionInDb.group).toEqual(SessionDefaultGroup.Default);
72
- }
73
- });
74
- });
75
-
76
- describe('query', () => {
77
- it('should query sessions with pagination', async () => {
78
- // Create multiple sessions to test the query method
79
- await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
80
-
81
- const queriedSessions = await SessionModel.query({ pageSize: 1, current: 0 });
82
-
83
- expect(queriedSessions).toHaveLength(1);
84
- });
85
- });
86
-
87
- describe('querySessionsByGroupId', () => {
88
- it('should query sessions by group', async () => {
89
- // Create multiple sessions to test the queryByGroup method
90
- const group: SessionGroupId = 'testGroup';
91
- await SessionGroupModel.create('测试分组', 0, group);
92
-
93
- await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
94
-
95
- const sessionsByGroup = await SessionModel.querySessionsByGroupId(group);
96
-
97
- // Assuming all created sessions belong to the same group
98
- expect(sessionsByGroup).toHaveLength(2);
99
- expect(sessionsByGroup.every((i) => i.group === group)).toBeTruthy();
100
- });
101
- });
102
-
103
- describe('update', () => {
104
- it('should update a session', async () => {
105
- const createdSession = await SessionModel.create('agent', sessionData);
106
- const updateData = { group: 'newGroup' };
107
-
108
- await SessionModel.update(createdSession.id, updateData);
109
- const updatedSession = await SessionModel.findById(createdSession.id);
110
-
111
- expect(updatedSession).toHaveProperty('group', 'newGroup');
112
- });
113
-
114
- it('should update pinned status of a session', async () => {
115
- const createdSession = await SessionModel.create('agent', sessionData);
116
- await SessionModel.update(createdSession.id, { pinned: 1 });
117
- const updatedSession = await SessionModel.findById(createdSession.id);
118
- expect(updatedSession).toHaveProperty('pinned', 1);
119
- });
120
- });
121
-
122
- describe('updateConfig', () => {
123
- it('should update config of a session', async () => {
124
- const { id } = await SessionModel.create('agent', sessionData);
125
- const dbSession = await SessionModel.findById(id);
126
-
127
- const newConfig = { ...dbSession.config, systemRole: 'newValue' } as LobeAgentConfig;
128
- await SessionModel.updateConfig(id, newConfig);
129
- const updatedSession = await SessionModel.findById(id);
130
- expect(updatedSession.config).toEqual(newConfig);
131
- });
132
- });
133
-
134
- describe('clearTable', () => {
135
- it('should clear all sessions', async () => {
136
- await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
137
- await SessionModel.clearTable();
138
- const sessionsInDb = await SessionModel.query();
139
- expect(sessionsInDb).toHaveLength(0);
140
- });
141
- });
142
-
143
- describe('isEmpty', () => {
144
- it('should check if table is empty', async () => {
145
- await SessionModel.clearTable();
146
- const isEmpty = await SessionModel.isEmpty();
147
- expect(isEmpty).toBeTruthy();
148
- await SessionModel.create('agent', sessionData);
149
- const isNotEmpty = await SessionModel.isEmpty();
150
- expect(isNotEmpty).toBeFalsy();
151
- });
152
- });
153
-
154
- describe('queryByKeyword', () => {
155
- it('should query sessions by keyword', async () => {
156
- const keyword = 'testKeyword';
157
- const sessionWithKeyword = { ...sessionData, meta: { title: keyword } };
158
- await SessionModel.create('agent', sessionWithKeyword);
159
- const sessionsByKeyword = await SessionModel.queryByKeyword(keyword);
160
- expect(sessionsByKeyword).toHaveLength(1);
161
- expect(sessionsByKeyword[0].meta.title).toContain(keyword);
162
- });
163
- });
164
-
165
- describe('getPinnedSessions', () => {
166
- it('should get pinned sessions', async () => {
167
- const pinnedSession = { ...sessionData, pinned: true };
168
- const unpinnedSession = { ...sessionData, pinned: false };
169
- await SessionModel.batchCreate([pinnedSession, unpinnedSession] as LobeAgentSession[]);
170
- const pinnedSessions = await SessionModel.getPinnedSessions();
171
- expect(pinnedSessions).toHaveLength(1);
172
- expect(pinnedSessions[0].pinned).toBeTruthy();
173
- });
174
- });
175
-
176
- describe('queryWithGroups', () => {
177
- it('should query sessions with groups', async () => {
178
- await SessionModel.create('agent', sessionData);
179
-
180
- const sessionsWithGroups = await SessionModel.queryWithGroups();
181
- expect(sessionsWithGroups.sessions).toHaveLength(1);
182
- expect(sessionsWithGroups.sessions[0]).toEqual(expect.objectContaining(sessionData));
183
- });
184
- });
185
-
186
- describe('queryByGroupIds', () => {
187
- it('should query sessions by group ids', async () => {
188
- const createdSession = await SessionModel.create('agent', sessionData);
189
- const session = await SessionModel.findById(createdSession.id);
190
- const sessionsByGroupIds = await SessionModel.queryByGroupIds([session.group]);
191
- expect(sessionsByGroupIds[session.group]).toHaveLength(1);
192
- expect(sessionsByGroupIds[session.group][0]).toEqual(expect.objectContaining(sessionData));
193
- });
194
- });
195
-
196
- describe('delete', () => {
197
- it('should delete a session', async () => {
198
- const createdSession = await SessionModel.create('agent', sessionData);
199
- await SessionModel.delete(createdSession.id);
200
- const sessionInDb = await SessionModel.findById(createdSession.id);
201
- expect(sessionInDb).toBeUndefined();
202
- });
203
-
204
- // 删除一个 session 时,也需要同步删除具有 sessionId 的 topic 和 message
205
- it('should delete a session and its associated data', async () => {
206
- // create session , topic and message test data
207
- const { id: sessionId } = await SessionModel.create('agent', sessionData);
208
-
209
- const topicData = {
210
- title: 'Test Topic',
211
- sessionId: sessionId,
212
- favorite: false,
213
- };
214
- const createdTopic = await TopicModel.create(topicData);
215
-
216
- const messageData: CreateMessageParams = {
217
- content: 'Test Message',
218
- sessionId: sessionId,
219
- topicId: createdTopic.id,
220
- role: 'user',
221
- };
222
- await MessageModel.create(messageData);
223
-
224
- await SessionModel.delete(sessionId);
225
-
226
- // Verify the session and its related data (topics, messages) are deleted
227
- const sessionInDb = await SessionModel.findById(sessionId);
228
- expect(sessionInDb).toBeUndefined();
229
-
230
- // You need to verify that topics and messages related to the session are also deleted
231
- const topicsInDb = await TopicModel.findBySessionId(sessionId);
232
- expect(topicsInDb).toHaveLength(0);
233
-
234
- // Verify all associated messages are deleted
235
- const messagesInDb = await MessageModel.query({ sessionId });
236
- expect(messagesInDb).toHaveLength(0);
237
- });
238
- });
239
-
240
- describe('duplicate', () => {
241
- it('should duplicate a session', async () => {
242
- const createdSession = await SessionModel.create('agent', sessionData);
243
- const duplicatedSession = await SessionModel.duplicate(createdSession.id);
244
- const sessionsInDb = await SessionModel.query();
245
- expect(sessionsInDb).toHaveLength(2);
246
-
247
- if (!duplicatedSession) return;
248
- const session = await SessionModel.findById(duplicatedSession.id);
249
-
250
- expect(session).toEqual(expect.objectContaining(sessionData));
251
- });
252
- });
253
- });
@@ -1,220 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
-
3
- import { SessionModel } from '@/database/_deprecated/models/session';
4
- import { SessionGroups } from '@/types/session';
5
-
6
- import { DB_SessionGroup } from '../../schemas/sessionGroup';
7
- import { SessionGroupModel } from '../sessionGroup';
8
-
9
- const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
10
-
11
- describe('SessionGroupModel', () => {
12
- let sessionGroupData: DB_SessionGroup;
13
-
14
- beforeEach(() => {
15
- // 设置正确结构的会话组数据
16
- sessionGroupData = {
17
- name: 'test-group',
18
- sort: 1,
19
- };
20
- });
21
-
22
- afterEach(async () => {
23
- // 每次测试后清理数据库
24
- await SessionGroupModel.clear();
25
- });
26
-
27
- describe('create', () => {
28
- it('should create a session group record', async () => {
29
- const createdGroup = await SessionGroupModel.create(
30
- sessionGroupData.name,
31
- sessionGroupData.sort,
32
- );
33
- const item = await SessionGroupModel.findById(createdGroup.id);
34
- expect(item.name).toEqual(sessionGroupData.name);
35
- expect(item.sort).toEqual(sessionGroupData.sort);
36
- });
37
- });
38
-
39
- describe('batchCreate', () => {
40
- it('should batch create session group records', async () => {
41
- const groups = [
42
- sessionGroupData,
43
- { ...sessionGroupData, name: 'another-group' },
44
- ] as SessionGroups;
45
- await SessionGroupModel.batchCreate(groups);
46
- const fetchedGroups = await SessionGroupModel.query();
47
- expect(fetchedGroups).toHaveLength(2);
48
- });
49
- });
50
-
51
- describe('update', () => {
52
- it('should update a session group', async () => {
53
- const createdGroup = await SessionGroupModel.create(
54
- sessionGroupData.name,
55
- sessionGroupData.sort,
56
- );
57
- const updatedGroupData: DB_SessionGroup = { ...sessionGroupData, name: 'updated-group' };
58
- await SessionGroupModel.update(createdGroup.id, updatedGroupData);
59
- const fetchedGroups = await SessionGroupModel.query();
60
- expect(fetchedGroups[0].name).toEqual(updatedGroupData.name);
61
- });
62
- });
63
-
64
- describe('delete', () => {
65
- it('should delete a session group', async () => {
66
- const createdGroup = await SessionGroupModel.create(
67
- sessionGroupData.name,
68
- sessionGroupData.sort,
69
- );
70
- await SessionGroupModel.delete(createdGroup.id);
71
- const fetchedGroups = await SessionGroupModel.query();
72
- expect(fetchedGroups).toHaveLength(0);
73
- });
74
- it('should delete a session group and update associated sessions', async () => {
75
- // Create a new session group
76
- const createdGroup = await SessionGroupModel.create(
77
- sessionGroupData.name,
78
- sessionGroupData.sort,
79
- );
80
-
81
- // Delete the created session group
82
- await SessionGroupModel.delete(createdGroup.id);
83
-
84
- // Fetch all session groups to confirm deletion
85
- const fetchedGroups = await SessionGroupModel.query();
86
- expect(fetchedGroups).toHaveLength(0);
87
-
88
- // Fetch all sessions to confirm update
89
- const fetchedSessions = await SessionModel.query();
90
- fetchedSessions.forEach((session) => {
91
- expect(session.group).not.toEqual(createdGroup.id);
92
- });
93
- });
94
- it('should update associated sessions to default group when a session group is deleted', async () => {
95
- const createdGroup = await SessionGroupModel.create(
96
- sessionGroupData.name,
97
- sessionGroupData.sort,
98
- );
99
- const sessionId = await SessionModel.create('agent', {}, createdGroup.id);
100
- await SessionGroupModel.delete(createdGroup.id);
101
- const updatedSession = await SessionModel.findById(sessionId.id);
102
- expect(updatedSession.group).toEqual('default');
103
- });
104
- });
105
-
106
- describe('query', () => {
107
- it('should fetch and return the session group list', async () => {
108
- await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
109
- const fetchedGroups = await SessionGroupModel.query();
110
- expect(fetchedGroups).toHaveLength(1);
111
- expect(fetchedGroups[0].name).toEqual(sessionGroupData.name);
112
- });
113
-
114
- it('should return an empty array when there are no session groups', async () => {
115
- const fetchedGroups = await SessionGroupModel.query();
116
- expect(fetchedGroups).toEqual([]);
117
- });
118
-
119
- it('should return session groups sorted by sort field when it exists', async () => {
120
- const group1 = await SessionGroupModel.create('group1', 2);
121
- const group2 = await SessionGroupModel.create('group2', 1);
122
- const fetchedGroups = await SessionGroupModel.query();
123
- expect(fetchedGroups[0].id).toEqual(group2.id);
124
- expect(fetchedGroups[1].id).toEqual(group1.id);
125
- });
126
-
127
- it('should return session groups sorted by createdAt when sort field does not exist', async () => {
128
- const group1 = await SessionGroupModel.create('group1');
129
- await new Promise((resolve) => setTimeout(() => resolve(undefined), 300));
130
- const group2 = await SessionGroupModel.create('group2');
131
-
132
- const fetchedGroups = await SessionGroupModel.query();
133
-
134
- expect(fetchedGroups[0].id).toEqual(group2.id);
135
- expect(fetchedGroups[1].id).toEqual(group1.id);
136
- });
137
-
138
- it('should return session groups sorted by sort field first and then by createdAt', async () => {
139
- const group0 = await SessionGroupModel.create('group0');
140
- await sleep(10);
141
- const group1 = await SessionGroupModel.create('group1', 1);
142
- await sleep(10);
143
- const group2 = await SessionGroupModel.create('group2');
144
- await sleep(10);
145
- const group3 = await SessionGroupModel.create('group3', 2);
146
- await sleep(10);
147
-
148
- const fetchedGroups = await SessionGroupModel.query();
149
- expect(fetchedGroups[0].id).toEqual(group1.id);
150
- expect(fetchedGroups[1].id).toEqual(group3.id);
151
- expect(fetchedGroups[2].id).toEqual(group2.id);
152
- expect(fetchedGroups[3].id).toEqual(group0.id);
153
- });
154
-
155
- it('should return session groups sorted by sort ', async () => {
156
- const group1 = await SessionGroupModel.create('group1', 1);
157
- const group2 = await SessionGroupModel.create('group2');
158
- const group3 = await SessionGroupModel.create('group3', 2);
159
- const fetchedGroups = await SessionGroupModel.query();
160
- expect(fetchedGroups[0].id).toEqual(group1.id);
161
- expect(fetchedGroups[1].id).toEqual(group3.id);
162
- expect(fetchedGroups[2].id).toEqual(group2.id);
163
- });
164
-
165
- it('should return session groups sorted by createdAt when sort fields are equal', async () => {
166
- const group1 = await SessionGroupModel.create('group1', 1);
167
- await new Promise((resolve) => setTimeout(() => resolve(undefined), 300));
168
- const group2 = await SessionGroupModel.create('group2', 1);
169
- const fetchedGroups = await SessionGroupModel.query();
170
- expect(fetchedGroups[0].id).toEqual(group2.id);
171
- expect(fetchedGroups[1].id).toEqual(group1.id);
172
- });
173
-
174
- it('should sort session groups correctly when only b has a sort value', async () => {
175
- const groupA = await SessionGroupModel.create('groupA'); // sort undefined
176
- const groupB = await SessionGroupModel.create('groupB', 1); // sort defined
177
- const fetchedGroups = await SessionGroupModel.query();
178
- expect(fetchedGroups[0].id).toEqual(groupB.id);
179
- expect(fetchedGroups[1].id).toEqual(groupA.id);
180
- });
181
- });
182
-
183
- describe('updateOrder', () => {
184
- it('should update the order of session groups', async () => {
185
- const group1 = await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
186
- const group2 = await SessionGroupModel.create('another-group', 2);
187
- await SessionGroupModel.updateOrder([
188
- { id: group1.id, sort: 2 },
189
- { id: group2.id, sort: 1 },
190
- ]);
191
- const fetchedGroups = await SessionGroupModel.query();
192
- expect(fetchedGroups[0].id).toEqual(group2.id);
193
- expect(fetchedGroups[1].id).toEqual(group1.id);
194
- });
195
- });
196
-
197
- describe('findById', () => {
198
- it('should find a session group by id', async () => {
199
- const createdGroup = await SessionGroupModel.create(
200
- sessionGroupData.name,
201
- sessionGroupData.sort,
202
- );
203
- const fetchedGroup = await SessionGroupModel.findById(createdGroup.id);
204
- expect(fetchedGroup).toMatchObject({ ...createdGroup, ...sessionGroupData });
205
- });
206
- });
207
-
208
- describe('clear', () => {
209
- it('should clear all session groups', async () => {
210
- await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
211
- const first = await SessionGroupModel.query();
212
- expect(first).toHaveLength(1);
213
-
214
- await SessionGroupModel.clear();
215
-
216
- const fetchedGroups = await SessionGroupModel.query();
217
- expect(fetchedGroups).toHaveLength(0);
218
- });
219
- });
220
- });