@lobehub/chat 1.37.0 → 1.37.2

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 (50) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/changelog/v1.json +18 -0
  3. package/locales/en-US/common.json +2 -2
  4. package/package.json +1 -1
  5. package/src/services/file/_deprecated.test.ts +119 -0
  6. package/src/services/file/{pglite.ts → _deprecated.ts} +28 -32
  7. package/src/services/file/client.test.ts +153 -74
  8. package/src/services/file/client.ts +32 -28
  9. package/src/services/file/index.ts +2 -2
  10. package/src/services/import/_deprecated.ts +74 -0
  11. package/src/services/import/{pglite.test.ts → client.test.ts} +1 -1
  12. package/src/services/import/client.ts +21 -61
  13. package/src/services/import/index.ts +2 -2
  14. package/src/services/message/_deprecated.test.ts +398 -0
  15. package/src/services/message/_deprecated.ts +121 -0
  16. package/src/services/message/client.test.ts +191 -159
  17. package/src/services/message/client.ts +47 -50
  18. package/src/services/message/index.ts +2 -2
  19. package/src/services/plugin/_deprecated.test.ts +162 -0
  20. package/src/services/plugin/_deprecated.ts +42 -0
  21. package/src/services/plugin/client.test.ts +68 -55
  22. package/src/services/plugin/client.ts +20 -11
  23. package/src/services/plugin/index.ts +2 -2
  24. package/src/services/session/_deprecated.test.ts +440 -0
  25. package/src/services/session/_deprecated.ts +183 -0
  26. package/src/services/session/client.test.ts +212 -241
  27. package/src/services/session/client.ts +61 -60
  28. package/src/services/session/index.ts +2 -2
  29. package/src/services/topic/{client.test.ts → _deprecated.test.ts} +1 -1
  30. package/src/services/topic/_deprecated.ts +70 -0
  31. package/src/services/topic/client.ts +40 -25
  32. package/src/services/topic/index.ts +2 -2
  33. package/src/services/topic/pglite.test.ts +1 -1
  34. package/src/services/user/{pglite.test.ts → _deprecated.test.ts} +32 -29
  35. package/src/services/user/_deprecated.ts +57 -0
  36. package/src/services/user/client.test.ts +28 -31
  37. package/src/services/user/client.ts +51 -16
  38. package/src/services/user/index.ts +2 -2
  39. package/src/store/chat/slices/builtinTool/action.test.ts +1 -1
  40. package/src/store/user/slices/common/action.test.ts +1 -1
  41. package/src/services/file/pglite.test.ts +0 -198
  42. package/src/services/import/pglite.ts +0 -34
  43. package/src/services/message/pglite.test.ts +0 -430
  44. package/src/services/message/pglite.ts +0 -118
  45. package/src/services/plugin/pglite.test.ts +0 -175
  46. package/src/services/plugin/pglite.ts +0 -51
  47. package/src/services/session/pglite.test.ts +0 -411
  48. package/src/services/session/pglite.ts +0 -184
  49. package/src/services/topic/pglite.ts +0 -85
  50. package/src/services/user/pglite.ts +0 -92
@@ -1,184 +0,0 @@
1
- import { DeepPartial } from 'utility-types';
2
-
3
- import { INBOX_SESSION_ID } from '@/const/session';
4
- import { clientDB } from '@/database/client/db';
5
- import { AgentItem } from '@/database/schemas';
6
- import { SessionModel } from '@/database/server/models/session';
7
- import { SessionGroupModel } from '@/database/server/models/sessionGroup';
8
- import { BaseClientService } from '@/services/baseClientService';
9
- import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
10
- import { MetaData } from '@/types/meta';
11
- import {
12
- ChatSessionList,
13
- LobeAgentSession,
14
- LobeSessionType,
15
- LobeSessions,
16
- SessionGroupItem,
17
- SessionGroups,
18
- UpdateSessionParams,
19
- } from '@/types/session';
20
-
21
- import { ISessionService } from './type';
22
-
23
- export class ClientService extends BaseClientService implements ISessionService {
24
- private get sessionModel(): SessionModel {
25
- return new SessionModel(clientDB as any, this.userId);
26
- }
27
-
28
- private get sessionGroupModel(): SessionGroupModel {
29
- return new SessionGroupModel(clientDB as any, this.userId);
30
- }
31
-
32
- async createSession(type: LobeSessionType, data: Partial<LobeAgentSession>): Promise<string> {
33
- const { config, group, meta, ...session } = data;
34
-
35
- const item = await this.sessionModel.create({
36
- config: { ...config, ...meta } as any,
37
- session: { ...session, groupId: group },
38
- type,
39
- });
40
- if (!item) {
41
- throw new Error('session create Error');
42
- }
43
- return item.id;
44
- }
45
-
46
- async batchCreateSessions(importSessions: LobeSessions) {
47
- // @ts-ignore
48
- return this.sessionModel.batchCreate(importSessions);
49
- }
50
-
51
- async cloneSession(id: string, newTitle: string): Promise<string | undefined> {
52
- const res = await this.sessionModel.duplicate(id, newTitle);
53
-
54
- if (res) return res?.id;
55
- }
56
-
57
- async getGroupedSessions(): Promise<ChatSessionList> {
58
- return this.sessionModel.queryWithGroups();
59
- }
60
-
61
- async getSessionConfig(id: string): Promise<LobeAgentConfig> {
62
- const res = await this.sessionModel.findByIdOrSlug(id);
63
-
64
- if (!res) throw new Error('Session not found');
65
-
66
- return res.agent as LobeAgentConfig;
67
- }
68
-
69
- /**
70
- * 这个方法要对应移除的
71
- */
72
- async getSessionsByType(type: 'agent' | 'group' | 'all' = 'all'): Promise<LobeSessions> {
73
- switch (type) {
74
- // TODO: add a filter to get only agents or agents
75
- case 'group': {
76
- // @ts-ignore
77
- return this.sessionModel.query();
78
- }
79
- case 'agent': {
80
- // @ts-ignore
81
- return this.sessionModel.query();
82
- }
83
-
84
- case 'all': {
85
- // @ts-ignore
86
- return this.sessionModel.query();
87
- }
88
- }
89
- }
90
-
91
- async countSessions() {
92
- return this.sessionModel.count();
93
- }
94
-
95
- async searchSessions(keyword: string) {
96
- return this.sessionModel.queryByKeyword(keyword);
97
- }
98
-
99
- async updateSession(id: string, value: Partial<UpdateSessionParams>) {
100
- return this.sessionModel.update(id, {
101
- ...value,
102
- groupId: value.group === 'default' ? null : value.group,
103
- });
104
- }
105
-
106
- async updateSessionConfig(
107
- activeId: string,
108
- config: DeepPartial<LobeAgentConfig>,
109
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
110
- _?: AbortSignal,
111
- ) {
112
- const session = await this.sessionModel.findByIdOrSlug(activeId);
113
- if (!session || !config) return;
114
-
115
- return this.sessionModel.updateConfig(session.agent.id, config as AgentItem);
116
- }
117
-
118
- async updateSessionMeta(
119
- activeId: string,
120
- meta: Partial<MetaData>,
121
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
122
- _?: AbortSignal,
123
- ) {
124
- // inbox 不允许修改 meta
125
- if (activeId === INBOX_SESSION_ID) return;
126
-
127
- return this.sessionModel.update(activeId, meta);
128
- }
129
-
130
- async updateSessionChatConfig(
131
- activeId: string,
132
- config: DeepPartial<LobeAgentChatConfig>,
133
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
134
- _?: AbortSignal,
135
- ) {
136
- return this.updateSessionConfig(activeId, { chatConfig: config });
137
- }
138
-
139
- async removeSession(id: string) {
140
- return this.sessionModel.delete(id);
141
- }
142
-
143
- async removeAllSessions() {
144
- return this.sessionModel.deleteAll();
145
- }
146
-
147
- // ************************************** //
148
- // *********** SessionGroup *********** //
149
- // ************************************** //
150
-
151
- async createSessionGroup(name: string, sort?: number) {
152
- const item = await this.sessionGroupModel.create({ name, sort });
153
- if (!item) {
154
- throw new Error('session group create Error');
155
- }
156
-
157
- return item.id;
158
- }
159
-
160
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
161
- async batchCreateSessionGroups(_groups: SessionGroups) {
162
- return { added: 0, ids: [], skips: [], success: true };
163
- }
164
-
165
- async removeSessionGroup(id: string) {
166
- return await this.sessionGroupModel.delete(id);
167
- }
168
-
169
- async updateSessionGroup(id: string, data: Partial<SessionGroupItem>) {
170
- return this.sessionGroupModel.update(id, data);
171
- }
172
-
173
- async updateSessionGroupOrder(sortMap: { id: string; sort: number }[]) {
174
- return this.sessionGroupModel.updateOrder(sortMap);
175
- }
176
-
177
- async getSessionGroups(): Promise<SessionGroupItem[]> {
178
- return this.sessionGroupModel.query();
179
- }
180
-
181
- async removeSessionGroups() {
182
- return this.sessionGroupModel.deleteAll();
183
- }
184
- }
@@ -1,85 +0,0 @@
1
- import { INBOX_SESSION_ID } from '@/const/session';
2
- import { clientDB } from '@/database/client/db';
3
- import { TopicModel } from '@/database/server/models/topic';
4
- import { BaseClientService } from '@/services/baseClientService';
5
- import { ChatTopic } from '@/types/topic';
6
-
7
- import { CreateTopicParams, ITopicService, QueryTopicParams } from './type';
8
-
9
- export class ClientService extends BaseClientService implements ITopicService {
10
- private get topicModel(): TopicModel {
11
- return new TopicModel(clientDB as any, this.userId);
12
- }
13
-
14
- async createTopic(params: CreateTopicParams): Promise<string> {
15
- const item = await this.topicModel.create({
16
- ...params,
17
- sessionId: this.toDbSessionId(params.sessionId),
18
- } as any);
19
-
20
- if (!item) {
21
- throw new Error('topic create Error');
22
- }
23
-
24
- return item.id;
25
- }
26
-
27
- async batchCreateTopics(importTopics: ChatTopic[]) {
28
- const data = await this.topicModel.batchCreate(importTopics as any);
29
-
30
- return { added: data.length, ids: [], skips: [], success: true };
31
- }
32
-
33
- async cloneTopic(id: string, newTitle?: string) {
34
- const data = await this.topicModel.duplicate(id, newTitle);
35
- return data.topic.id;
36
- }
37
-
38
- async getTopics(params: QueryTopicParams) {
39
- const data = await this.topicModel.query({
40
- ...params,
41
- sessionId: this.toDbSessionId(params.sessionId),
42
- });
43
- return data as unknown as Promise<ChatTopic[]>;
44
- }
45
-
46
- async searchTopics(keyword: string, sessionId?: string) {
47
- const data = await this.topicModel.queryByKeyword(keyword, this.toDbSessionId(sessionId));
48
-
49
- return data as unknown as Promise<ChatTopic[]>;
50
- }
51
-
52
- async getAllTopics() {
53
- const data = await this.topicModel.queryAll();
54
-
55
- return data as unknown as Promise<ChatTopic[]>;
56
- }
57
-
58
- async countTopics() {
59
- return this.topicModel.count();
60
- }
61
-
62
- async updateTopic(id: string, data: Partial<ChatTopic>) {
63
- return this.topicModel.update(id, data as any);
64
- }
65
-
66
- async removeTopic(id: string) {
67
- return this.topicModel.delete(id);
68
- }
69
-
70
- async removeTopics(sessionId: string) {
71
- return this.topicModel.batchDeleteBySessionId(this.toDbSessionId(sessionId));
72
- }
73
-
74
- async batchRemoveTopics(topics: string[]) {
75
- return this.topicModel.batchDelete(topics);
76
- }
77
-
78
- async removeAllTopic() {
79
- return this.topicModel.deleteAll();
80
- }
81
-
82
- private toDbSessionId(sessionId?: string | null) {
83
- return sessionId === INBOX_SESSION_ID ? null : sessionId;
84
- }
85
- }
@@ -1,92 +0,0 @@
1
- import { DeepPartial } from 'utility-types';
2
-
3
- import { clientDB } from '@/database/client/db';
4
- import { users } from '@/database/schemas';
5
- import { MessageModel } from '@/database/server/models/message';
6
- import { SessionModel } from '@/database/server/models/session';
7
- import { UserModel } from '@/database/server/models/user';
8
- import { BaseClientService } from '@/services/baseClientService';
9
- import { UserGuide, UserInitializationState, UserPreference } from '@/types/user';
10
- import { UserSettings } from '@/types/user/settings';
11
- import { AsyncLocalStorage } from '@/utils/localStorage';
12
-
13
- import { IUserService } from './type';
14
-
15
- export class ClientService extends BaseClientService implements IUserService {
16
- private preferenceStorage: AsyncLocalStorage<UserPreference>;
17
-
18
- private get userModel(): UserModel {
19
- return new UserModel(clientDB as any, this.userId);
20
- }
21
- private get messageModel(): MessageModel {
22
- return new MessageModel(clientDB as any, this.userId);
23
- }
24
- private get sessionModel(): SessionModel {
25
- return new SessionModel(clientDB as any, this.userId);
26
- }
27
-
28
- constructor(userId?: string) {
29
- super(userId);
30
- this.preferenceStorage = new AsyncLocalStorage('LOBE_PREFERENCE');
31
- }
32
-
33
- async getUserState(): Promise<UserInitializationState> {
34
- // if user not exist in the db, create one to make sure the user exist
35
- await this.makeSureUserExist();
36
-
37
- const state = await this.userModel.getUserState((encryptKeyVaultsStr) =>
38
- encryptKeyVaultsStr ? JSON.parse(encryptKeyVaultsStr) : {},
39
- );
40
-
41
- const user = await UserModel.findById(clientDB as any, this.userId);
42
- const messageCount = await this.messageModel.count();
43
- const sessionCount = await this.sessionModel.count();
44
-
45
- return {
46
- ...state,
47
- avatar: user?.avatar as string,
48
- canEnablePWAGuide: messageCount >= 4,
49
- canEnableTrace: messageCount >= 4,
50
- hasConversation: messageCount > 0 || sessionCount > 0,
51
- isOnboard: true,
52
- preference: await this.preferenceStorage.getFromLocalStorage(),
53
- };
54
- }
55
-
56
- updateUserSettings = async (value: DeepPartial<UserSettings>) => {
57
- const { keyVaults, ...res } = value;
58
-
59
- return this.userModel.updateSetting({ ...res, keyVaults: JSON.stringify(keyVaults) });
60
- };
61
-
62
- resetUserSettings = async () => {
63
- return this.userModel.deleteSetting();
64
- };
65
-
66
- async updateAvatar(avatar: string) {
67
- await this.userModel.updateUser({ avatar });
68
- }
69
-
70
- async updatePreference(preference: Partial<UserPreference>) {
71
- await this.preferenceStorage.saveToLocalStorage(preference);
72
- }
73
-
74
- // eslint-disable-next-line @typescript-eslint/no-unused-vars,unused-imports/no-unused-vars
75
- async updateGuide(guide: Partial<UserGuide>) {
76
- throw new Error('Method not implemented.');
77
- }
78
-
79
- async makeSureUserExist() {
80
- const existUsers = await clientDB.query.users.findMany();
81
-
82
- let user: { id: string };
83
- if (existUsers.length === 0) {
84
- const result = await clientDB.insert(users).values({ id: this.userId }).returning();
85
- user = result[0];
86
- } else {
87
- user = existUsers[0];
88
- }
89
-
90
- return user;
91
- }
92
- }