@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,277 +0,0 @@
1
- import { UIChatMessage } from '@lobechat/types';
2
- import type { PartialDeep } from 'type-fest';
3
-
4
- import { BaseModel } from '@/database/_deprecated/core';
5
- import { DBModel } from '@/database/_deprecated/core/types/db';
6
- import { DB_Message, DB_MessageSchema } from '@/database/_deprecated/schemas/message';
7
- import { nanoid } from '@/utils/uuid';
8
-
9
- /**
10
- * use `@/types/message` instead
11
- * @deprecated
12
- */
13
- export interface CreateMessageParams
14
- extends Partial<Omit<UIChatMessage, 'content' | 'role'>>,
15
- Pick<UIChatMessage, 'content' | 'role'> {
16
- files?: string[];
17
- fromModel?: string;
18
- fromProvider?: string;
19
- sessionId: string;
20
- traceId?: string;
21
- }
22
-
23
- export interface QueryMessageParams {
24
- current?: number;
25
- pageSize?: number;
26
- sessionId: string;
27
- topicId?: string;
28
- }
29
-
30
- class _MessageModel extends BaseModel {
31
- constructor() {
32
- super('messages', DB_MessageSchema);
33
- }
34
-
35
- // **************** Query *************** //
36
-
37
- async query({
38
- sessionId,
39
- topicId,
40
- pageSize = 9999,
41
- current = 0,
42
- }: QueryMessageParams): Promise<UIChatMessage[]> {
43
- const offset = current * pageSize;
44
-
45
- const query = !!topicId
46
- ? // TODO: The query {"sessionId":"xxx","topicId":"xxx"} on messages would benefit of a compound index [sessionId+topicId]
47
- this.table.where({ sessionId, topicId }) // Use a compound index
48
- : this.table
49
- .where('sessionId')
50
- .equals(sessionId)
51
- .and((message) => !message.topicId);
52
-
53
- const dbMessages: DBModel<DB_Message>[] = await query
54
- .sortBy('createdAt')
55
- // handle page size
56
- .then((sortedArray) => sortedArray.slice(offset, offset + pageSize));
57
-
58
- const messages = dbMessages.map((msg) => this.mapToChatMessage(msg));
59
-
60
- const finalList: UIChatMessage[] = [];
61
-
62
- const addItem = (item: UIChatMessage) => {
63
- const isExist = finalList.some((i) => item.id === i.id);
64
- if (!isExist) {
65
- finalList.push(item);
66
- }
67
- };
68
- const messageMap = new Map<string, UIChatMessage>();
69
- for (const item of messages) messageMap.set(item.id, item);
70
-
71
- for (const item of messages) {
72
- if (!item.parentId || !messageMap.has(item.parentId)) {
73
- // 如果消息没有父消息或者父消息不在列表中,直接添加
74
- addItem(item);
75
- } else {
76
- // 如果消息有父消息,确保先添加父消息
77
- addItem(messageMap.get(item.parentId)!);
78
- addItem(item);
79
- }
80
- }
81
- return finalList;
82
- }
83
-
84
- async findById(id: string): Promise<DBModel<DB_Message>> {
85
- return this.table.get(id);
86
- }
87
-
88
- async queryAll() {
89
- const data: DBModel<DB_Message>[] = await this.table.orderBy('updatedAt').toArray();
90
-
91
- return data.map((element) => this.mapToChatMessage(element));
92
- }
93
-
94
- async queryBySessionId(sessionId: string) {
95
- return this.table.where('sessionId').equals(sessionId).toArray();
96
- }
97
-
98
- queryByTopicId = async (topicId: string) => {
99
- const dbMessages = await this.table.where('topicId').equals(topicId).toArray();
100
-
101
- return dbMessages.map((message) => this.mapToChatMessage(message));
102
- };
103
-
104
- async count() {
105
- return this.table.count();
106
- }
107
-
108
- // **************** Create *************** //
109
-
110
- async create(data: CreateMessageParams) {
111
- const id = nanoid();
112
-
113
- const messageData: DB_Message = this.mapChatMessageToDBMessage(data as UIChatMessage);
114
-
115
- return this._addWithSync(messageData, id);
116
- }
117
-
118
- async batchCreate(messages: UIChatMessage[]) {
119
- const data: DB_Message[] = messages.map((m) => this.mapChatMessageToDBMessage(m));
120
-
121
- return this._batchAdd(data);
122
- }
123
-
124
- async duplicateMessages(messages: UIChatMessage[]): Promise<UIChatMessage[]> {
125
- const duplicatedMessages = await this.createDuplicateMessages(messages);
126
- // 批量添加复制后的消息到数据库
127
- await this.batchCreate(duplicatedMessages);
128
- return duplicatedMessages;
129
- }
130
-
131
- // **************** Delete *************** //
132
-
133
- async delete(id: string) {
134
- return super._deleteWithSync(id);
135
- }
136
-
137
- async bulkDelete(ids: string[]) {
138
- return super._bulkDeleteWithSync(ids);
139
- }
140
-
141
- async clearTable() {
142
- return this._clearWithSync();
143
- }
144
-
145
- /**
146
- * Deletes multiple messages based on the assistantId and optionally the topicId.
147
- * If topicId is not provided, it deletes messages where topicId is undefined or null.
148
- * If topicId is provided, it deletes messages with that specific topicId.
149
- *
150
- * @param {string} sessionId - The identifier of the assistant associated with the messages.
151
- * @param {string | undefined} topicId - The identifier of the topic associated with the messages (optional).
152
- * @returns {Promise<void>}
153
- */
154
- async batchDelete(sessionId: string, topicId: string | undefined): Promise<void> {
155
- // If topicId is specified, use both assistantId and topicId as the filter criteria in the query.
156
- // Otherwise, filter by assistantId and require that topicId is undefined.
157
- const query = !!topicId
158
- ? this.table.where({ sessionId, topicId }) // Use a compound index
159
- : this.table
160
- .where('sessionId')
161
- .equals(sessionId)
162
- .and((message) => !message.topicId);
163
-
164
- // Retrieve a collection of message IDs that satisfy the criteria
165
- const messageIds = await query.primaryKeys();
166
-
167
- // Use the bulkDelete method to delete all selected messages in bulk
168
- return this._bulkDeleteWithSync(messageIds);
169
- }
170
-
171
- async batchDeleteBySessionId(sessionId: string): Promise<void> {
172
- // If topicId is specified, use both assistantId and topicId as the filter criteria in the query.
173
- // Otherwise, filter by assistantId and require that topicId is undefined.
174
- const messageIds = await this.table.where('sessionId').equals(sessionId).primaryKeys();
175
-
176
- // Use the bulkDelete method to delete all selected messages in bulk
177
- return this._bulkDeleteWithSync(messageIds);
178
- }
179
-
180
- /**
181
- * Delete all messages associated with the topicId
182
- * @param topicId
183
- */
184
- async batchDeleteByTopicId(topicId: string): Promise<void> {
185
- const messageIds = await this.table.where('topicId').equals(topicId).primaryKeys();
186
-
187
- return this._bulkDeleteWithSync(messageIds);
188
- }
189
-
190
- // **************** Update *************** //
191
-
192
- async update(id: string, data: PartialDeep<DB_Message>) {
193
- return super._updateWithSync(id, data);
194
- }
195
-
196
- async updatePluginState(id: string, value: any) {
197
- const item = await this.findById(id);
198
-
199
- return this.update(id, { pluginState: { ...item.pluginState, ...value } });
200
- }
201
-
202
- async updatePlugin(id: string, value: any) {
203
- const item = await this.findById(id);
204
-
205
- return this.update(id, { plugin: { ...item.plugin, ...value } });
206
- }
207
-
208
- /**
209
- * Batch updates multiple fields of the specified messages.
210
- *
211
- * @param {string[]} messageIds - The identifiers of the messages to be updated.
212
- * @param {Partial<DB_Message>} updateFields - An object containing the fields to update and their new values.
213
- * @returns {Promise<number>} - The number of updated messages.
214
- */
215
- async batchUpdate(messageIds: string[], updateFields: Partial<DB_Message>): Promise<number> {
216
- // Retrieve the messages by their IDs
217
- const messagesToUpdate = await this.table.where('id').anyOf(messageIds).toArray();
218
-
219
- // Update the specified fields of each message
220
- const updatedMessages = messagesToUpdate.map((message) => ({
221
- ...message,
222
- ...updateFields,
223
- }));
224
-
225
- // Use the bulkPut method to update the messages in bulk
226
- await this._bulkPutWithSync(updatedMessages);
227
-
228
- return updatedMessages.length;
229
- }
230
-
231
- // **************** Helper *************** //
232
-
233
- private async createDuplicateMessages(messages: UIChatMessage[]): Promise<UIChatMessage[]> {
234
- // 创建一个映射来存储原始消息ID和复制消息ID之间的关系
235
- const idMapping = new Map<string, string>();
236
-
237
- // 首先复制所有消息,并为每个复制的消息生成新的ID
238
- const duplicatedMessages = messages.map((originalMessage) => {
239
- const newId = nanoid();
240
- idMapping.set(originalMessage.id, newId);
241
-
242
- return { ...originalMessage, id: newId };
243
- });
244
-
245
- // 更新 parentId 为复制后的新ID
246
- for (const duplicatedMessage of duplicatedMessages) {
247
- if (duplicatedMessage.parentId && idMapping.has(duplicatedMessage.parentId)) {
248
- duplicatedMessage.parentId = idMapping.get(duplicatedMessage.parentId);
249
- }
250
- }
251
-
252
- return duplicatedMessages;
253
- }
254
-
255
- private mapChatMessageToDBMessage(message: UIChatMessage): DB_Message {
256
- const { extra, ...messageData } = message;
257
-
258
- return { ...messageData, ...extra } as DB_Message;
259
- }
260
-
261
- private mapToChatMessage = ({
262
- fromModel,
263
- fromProvider,
264
- translate,
265
- tts,
266
- ...item
267
- }: DBModel<DB_Message>) => {
268
- return {
269
- ...item,
270
- extra: { fromModel, fromProvider, translate, tts },
271
- meta: {},
272
- topicId: item.topicId ?? undefined,
273
- } as UIChatMessage;
274
- };
275
- }
276
-
277
- export const MessageModel = new _MessageModel();
@@ -1,62 +0,0 @@
1
- import { LobeTool } from '@lobechat/types';
2
- import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk';
3
-
4
- import { BaseModel } from '@/database/_deprecated/core';
5
- import { merge } from '@/utils/merge';
6
-
7
- import { DB_Plugin, DB_PluginSchema } from '../schemas/plugin';
8
-
9
- export interface InstallPluginParams {
10
- identifier: string;
11
- manifest?: LobeChatPluginManifest;
12
- type: 'plugin' | 'customPlugin';
13
- }
14
-
15
- class _PluginModel extends BaseModel {
16
- constructor() {
17
- super('plugins', DB_PluginSchema);
18
- }
19
- // **************** Query *************** //
20
-
21
- getList = async (): Promise<DB_Plugin[]> => {
22
- return this.table.toArray();
23
- };
24
- // **************** Create *************** //
25
-
26
- create = async (plugin: InstallPluginParams) => {
27
- const old = await this.table.get(plugin.identifier);
28
- const dbPlugin = this.mapToDBPlugin(plugin);
29
-
30
- return this._putWithSync(merge(old, dbPlugin), plugin.identifier);
31
- };
32
-
33
- batchCreate = async (plugins: LobeTool[]) => {
34
- const dbPlugins = plugins.map((item) => this.mapToDBPlugin(item));
35
-
36
- return this._batchAdd(dbPlugins);
37
- };
38
- // **************** Delete *************** //
39
-
40
- delete(id: string) {
41
- return this._deleteWithSync(id);
42
- }
43
- clear() {
44
- return this._clearWithSync();
45
- }
46
-
47
- // **************** Update *************** //
48
-
49
- update: (id: string, value: Partial<DB_Plugin>) => Promise<number> = async (id, value) => {
50
- const { success } = await this._updateWithSync(id, value);
51
-
52
- return success;
53
- };
54
-
55
- // **************** Helper *************** //
56
-
57
- mapToDBPlugin(plugin: LobeTool) {
58
- return { ...plugin, id: plugin.identifier } as DB_Plugin;
59
- }
60
- }
61
-
62
- export const PluginModel = new _PluginModel();
@@ -1,271 +0,0 @@
1
- import type { PartialDeep } from 'type-fest';
2
-
3
- import { DEFAULT_AGENT_LOBE_SESSION } from '@/const/session';
4
- import { BaseModel } from '@/database/_deprecated/core';
5
- import { DBModel } from '@/database/_deprecated/core/types/db';
6
- import { DB_Session, DB_SessionSchema } from '@/database/_deprecated/schemas/session';
7
- import { LobeAgentConfig } from '@/types/agent';
8
- import {
9
- ChatSessionList,
10
- LobeAgentSession,
11
- LobeSessions,
12
- SessionDefaultGroup,
13
- SessionGroupId,
14
- } from '@/types/session';
15
- import { merge } from '@/utils/merge';
16
- import { uuid } from '@/utils/uuid';
17
-
18
- import { MessageModel } from './message';
19
- import { SessionGroupModel } from './sessionGroup';
20
- import { TopicModel } from './topic';
21
-
22
- class _SessionModel extends BaseModel {
23
- constructor() {
24
- super('sessions', DB_SessionSchema);
25
- }
26
-
27
- // **************** Query *************** //
28
-
29
- async query({
30
- pageSize = 9999,
31
- current = 0,
32
- }: { current?: number; pageSize?: number } = {}): Promise<LobeSessions> {
33
- const offset = current * pageSize;
34
-
35
- const items: DBModel<DB_Session>[] = await this.table
36
- .orderBy('updatedAt')
37
- .reverse()
38
- .offset(offset)
39
- .limit(pageSize)
40
- .toArray();
41
-
42
- return this.mapToAgentSessions(items);
43
- }
44
-
45
- async queryWithGroups(): Promise<ChatSessionList> {
46
- const sessionGroups = await SessionGroupModel.query();
47
-
48
- const sessions = await this.query();
49
-
50
- return { sessionGroups, sessions };
51
- }
52
-
53
- /**
54
- * get sessions by group
55
- * @param group
56
- */
57
- async querySessionsByGroupId(group: SessionGroupId): Promise<LobeSessions> {
58
- const items: DBModel<DB_Session>[] = await this.table
59
- .where('group')
60
- .equals(group)
61
- .and((session) => !session.pinned)
62
- .reverse()
63
- .sortBy('updatedAt');
64
-
65
- return this.mapToAgentSessions(items);
66
- }
67
-
68
- async queryByGroupIds(groups: string[]) {
69
- const pools = groups.map(async (id) => {
70
- return [id, await this.querySessionsByGroupId(id)] as const;
71
- });
72
- const groupItems = await Promise.all(pools);
73
-
74
- return Object.fromEntries(groupItems);
75
- }
76
-
77
- /**
78
- * Query sessions by keyword in title, description, content, or translated content
79
- * @param keyword The keyword to search for
80
- */
81
- async queryByKeyword(keyword: string): Promise<LobeSessions> {
82
- if (!keyword) return [];
83
-
84
- const startTime = Date.now();
85
- const keywordLowerCase = keyword.toLowerCase();
86
-
87
- // First, filter sessions by title and description
88
- const matchingSessionsPromise = this.table
89
- .filter((session) => {
90
- return (
91
- session.meta.title?.toLowerCase().includes(keywordLowerCase) ||
92
- session.meta.description?.toLowerCase().includes(keywordLowerCase)
93
- );
94
- })
95
- .toArray();
96
-
97
- // Next, find message IDs that contain the keyword in content or translated content
98
- const matchingMessagesPromise = this.db.messages
99
- .filter((message) => {
100
- // check content
101
- if (message.content.toLowerCase().includes(keywordLowerCase)) return true;
102
-
103
- // check translate content
104
- if (message.translate && message.translate.content) {
105
- return message.translate.content.toLowerCase().includes(keywordLowerCase);
106
- }
107
-
108
- return false;
109
- })
110
- .toArray();
111
-
112
- // match topics
113
- const matchingTopicsPromise = this.db.topics
114
- .filter((topic) => {
115
- return topic.title?.toLowerCase().includes(keywordLowerCase);
116
- })
117
- .toArray();
118
-
119
- // Resolve both promises
120
- const [matchingSessions, matchingMessages, matchingTopics] = await Promise.all([
121
- matchingSessionsPromise,
122
- matchingMessagesPromise,
123
- matchingTopicsPromise,
124
- ]);
125
-
126
- const sessionIdsFromMessages = matchingMessages.map((message) => message.sessionId);
127
- const sessionIdsFromTopics = matchingTopics.map((topic) => topic.sessionId);
128
-
129
- // Combine session IDs from both sources
130
- const combinedSessionIds = new Set([
131
- ...sessionIdsFromMessages,
132
- ...sessionIdsFromTopics,
133
- ...matchingSessions.map((session) => session.id),
134
- ]);
135
-
136
- // Retrieve unique sessions by IDs
137
- const items: DBModel<DB_Session>[] = await this.table
138
- .where('id')
139
- .anyOf([...combinedSessionIds])
140
- .toArray();
141
-
142
- console.log(`检索到 ${items.length} 项,耗时 ${Date.now() - startTime}ms`);
143
- return this.mapToAgentSessions(items);
144
- }
145
-
146
- async getPinnedSessions(): Promise<LobeSessions> {
147
- const items: DBModel<DB_Session>[] = await this.table
148
- .where('pinned')
149
- .equals(1)
150
- .reverse()
151
- .sortBy('updatedAt');
152
-
153
- return this.mapToAgentSessions(items);
154
- }
155
-
156
- async findById(id: string): Promise<DBModel<DB_Session>> {
157
- return this.table.get(id);
158
- }
159
-
160
- async isEmpty() {
161
- return (await this.table.count()) === 0;
162
- }
163
-
164
- async count() {
165
- return this.table.count();
166
- }
167
-
168
- // **************** Create *************** //
169
-
170
- async create(type: 'agent' | 'group', defaultValue: Partial<LobeAgentSession>, id = uuid()) {
171
- const data = merge(DEFAULT_AGENT_LOBE_SESSION, { type, ...defaultValue });
172
- const dataDB = this.mapToDB_Session(data);
173
- return this._addWithSync(dataDB, id);
174
- }
175
-
176
- async batchCreate(sessions: LobeAgentSession[]) {
177
- const DB_Sessions = await Promise.all(
178
- sessions.map(async (s) => {
179
- if (s.group && s.group !== SessionDefaultGroup.Default) {
180
- // Check if the group exists in the SessionGroup table
181
- const groupExists = await SessionGroupModel.findById(s.group);
182
- // If the group does not exist, set it to default
183
- if (!groupExists) {
184
- s.group = SessionDefaultGroup.Default;
185
- }
186
- }
187
- return this.mapToDB_Session(s);
188
- }),
189
- );
190
-
191
- return this._batchAdd<DB_Session>(DB_Sessions, { idGenerator: uuid });
192
- }
193
-
194
- async duplicate(id: string, newTitle?: string) {
195
- const session = await this.findById(id);
196
- if (!session) return;
197
-
198
- const newSession = merge(session, { meta: { title: newTitle } });
199
-
200
- return this._addWithSync(newSession, uuid());
201
- }
202
-
203
- // **************** Delete *************** //
204
-
205
- /**
206
- * Delete a session , also delete all messages and topic associated with it.
207
- */
208
- async delete(id: string) {
209
- return this.db.transaction('rw', [this.table, this.db.topics, this.db.messages], async () => {
210
- // Delete all topics associated with the session
211
- await TopicModel.batchDeleteBySessionId(id);
212
-
213
- // Delete all messages associated with the session
214
- await MessageModel.batchDeleteBySessionId(id);
215
-
216
- // Finally, delete the session itself
217
- await this._deleteWithSync(id);
218
- });
219
- }
220
-
221
- async batchDelete(ids: string[]) {
222
- return this._bulkDeleteWithSync(ids);
223
- }
224
-
225
- async clearTable() {
226
- return this._clearWithSync();
227
- }
228
-
229
- // **************** Update *************** //
230
-
231
- async update(id: string, data: Partial<DB_Session>) {
232
- return super._updateWithSync(id, data);
233
- }
234
-
235
- async updateConfig(id: string, data: PartialDeep<LobeAgentConfig>) {
236
- const session = await this.findById(id);
237
- if (!session) return;
238
-
239
- const config = merge(session.config, data);
240
-
241
- return this.update(id, { config });
242
- }
243
-
244
- // **************** Helper *************** //
245
-
246
- private mapToDB_Session(session: LobeAgentSession): DBModel<DB_Session> {
247
- return {
248
- ...session,
249
- createdAt: session.createdAt?.valueOf(),
250
- group: session.group || SessionDefaultGroup.Default,
251
- pinned: session.pinned ? 1 : 0,
252
- updatedAt: session.updatedAt?.valueOf(),
253
- };
254
- }
255
-
256
- private DB_SessionToAgentSession(session: DBModel<DB_Session>) {
257
- return {
258
- ...session,
259
- createdAt: new Date(session.createdAt),
260
- model: session.config.model,
261
- pinned: !!session.pinned,
262
- updatedAt: new Date(session.updatedAt),
263
- } as LobeAgentSession;
264
- }
265
-
266
- private mapToAgentSessions(session: DBModel<DB_Session>[]) {
267
- return session.map((item) => this.DB_SessionToAgentSession(item));
268
- }
269
- }
270
-
271
- export const SessionModel = new _SessionModel();
@@ -1,93 +0,0 @@
1
- import { BaseModel } from '@/database/_deprecated/core';
2
- import {
3
- DB_SessionGroup,
4
- DB_SessionGroupSchema,
5
- } from '@/database/_deprecated/schemas/sessionGroup';
6
- import { SessionGroups } from '@/types/session';
7
- import { nanoid } from '@/utils/uuid';
8
-
9
- class _SessionGroupModel extends BaseModel {
10
- constructor() {
11
- super('sessionGroups', DB_SessionGroupSchema);
12
- }
13
-
14
- // **************** Query *************** //
15
-
16
- async query(): Promise<SessionGroups> {
17
- const allGroups = await this.table.toArray();
18
-
19
- // 自定义排序,先按 sort 存在与否分组,然后分别排序
20
- return allGroups.sort((a, b) => {
21
- // 如果两个项都有 sort,则按 sort 排序
22
- if (a.sort !== undefined && b.sort !== undefined) {
23
- // 如果sort 一样,按时间倒序排序
24
- if (a.sort === b.sort) return b.createdAt - a.createdAt;
25
-
26
- return a.sort - b.sort;
27
- }
28
- // 如果 a 有 sort 而 b 没有,则 a 排在前面
29
- if (a.sort !== undefined) {
30
- return -1;
31
- }
32
- // 如果 b 有 sort 而 a 没有,则 b 排在前面
33
- if (b.sort !== undefined) {
34
- return 1;
35
- }
36
- // 如果两个项都没有 sort,则按 createdAt 倒序排序
37
- return b.createdAt - a.createdAt;
38
- });
39
- }
40
-
41
- async findById(id: string): Promise<DB_SessionGroup> {
42
- return this.table.get(id);
43
- }
44
-
45
- // **************** Create *************** //
46
-
47
- async create(name: string, sort?: number, id = nanoid()) {
48
- return this._addWithSync({ name, sort }, id);
49
- }
50
-
51
- async batchCreate(groups: SessionGroups) {
52
- return this._batchAdd(groups, { idGenerator: nanoid });
53
- }
54
-
55
- // **************** Delete *************** //
56
- async delete(id: string, removeGroupItem: boolean = false) {
57
- const { SessionModel } = await import('./session');
58
- this.db.sessions.toCollection().modify(async (session) => {
59
- // update all session associated with the sessionGroup to default
60
- if (session.group === id) {
61
- await SessionModel.update(session.id, { group: 'default' });
62
- }
63
- });
64
-
65
- if (!removeGroupItem) {
66
- return this._deleteWithSync(id);
67
- } else {
68
- const sessionIds = await this.db.sessions.where('group').equals(id).primaryKeys();
69
-
70
- return await SessionModel.batchDelete(sessionIds);
71
- }
72
- }
73
-
74
- async clear() {
75
- await this._clearWithSync();
76
- }
77
-
78
- // **************** Update *************** //
79
-
80
- async update(id: string, data: Partial<DB_SessionGroup>) {
81
- return super._updateWithSync(id, data);
82
- }
83
-
84
- async updateOrder(sortMap: { id: string; sort: number }[]) {
85
- return this.db.transaction('rw', this.table, async () => {
86
- for (const { id, sort } of sortMap) {
87
- await this.update(id, { sort });
88
- }
89
- });
90
- }
91
- }
92
-
93
- export const SessionGroupModel = new _SessionGroupModel();