@lobehub/chat 0.32.0 → 0.34.0

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 (60) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/package.json +2 -2
  3. package/src/components/EmojiPicker/index.tsx +48 -0
  4. package/src/components/EmojiPicker/style.ts +28 -0
  5. package/src/const/meta.ts +3 -0
  6. package/src/const/settings.ts +50 -0
  7. package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentConfig/index.tsx +13 -11
  8. package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentMeta/AutoGenerateInput.tsx +16 -14
  9. package/src/features/AgentSetting/AgentMeta/BackgroundSwatches.tsx +27 -0
  10. package/src/features/AgentSetting/AgentMeta/index.tsx +128 -0
  11. package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentPlugin/index.tsx +12 -12
  12. package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentPrompt/index.tsx +13 -16
  13. package/src/features/AgentSetting/index.ts +4 -0
  14. package/src/features/AgentSetting/test.json +40 -0
  15. package/src/features/SideBar/{BottomAction.tsx → BottomActions.tsx} +15 -5
  16. package/src/features/SideBar/TopActions.tsx +42 -0
  17. package/src/features/SideBar/index.tsx +5 -30
  18. package/src/hooks/useImportConfig.ts +3 -3
  19. package/src/locales/default/common.ts +4 -0
  20. package/src/locales/default/setting.ts +4 -0
  21. package/src/pages/chat/[id]/Conversation/ChatList/index.tsx +1 -1
  22. package/src/pages/chat/[id]/Conversation/Input/Action.tsx +1 -1
  23. package/src/pages/chat/[id]/Header.tsx +1 -1
  24. package/src/pages/chat/[id]/{Config/SideBar.tsx → Sidebar/Inner.tsx} +2 -2
  25. package/src/pages/chat/[id]/{Config → Sidebar}/Topic/TopicContent.tsx +4 -6
  26. package/src/pages/chat/[id]/{Config → Sidebar}/index.tsx +2 -2
  27. package/src/pages/chat/[id]/index.page.tsx +1 -1
  28. package/src/pages/chat/[id]/{edit → setting}/index.page.tsx +23 -9
  29. package/src/pages/settings/Settings/Agent.tsx +25 -0
  30. package/src/pages/{setting/SettingForm.tsx → settings/Settings/Common.tsx} +7 -113
  31. package/src/pages/settings/Settings/index.tsx +50 -0
  32. package/src/pages/{setting → settings}/index.page.tsx +2 -2
  33. package/src/pages/{setting → settings}/layout.tsx +1 -1
  34. package/src/plugins/searchEngine/Render.tsx +3 -1
  35. package/src/store/session/slices/agentConfig/action.ts +8 -1
  36. package/src/store/session/slices/agentConfig/initialState.ts +2 -13
  37. package/src/store/session/slices/agentConfig/selectors.ts +13 -9
  38. package/src/store/session/slices/chat/actions/message.ts +49 -12
  39. package/src/store/session/slices/chat/actions/topic.ts +4 -2
  40. package/src/store/session/slices/chat/selectors/chat.ts +1 -1
  41. package/src/store/session/slices/session/action.ts +23 -16
  42. package/src/store/session/slices/session/initialState.ts +2 -1
  43. package/src/store/settings/action.ts +82 -11
  44. package/src/store/settings/initialState.ts +4 -27
  45. package/src/store/settings/selectors.ts +12 -7
  46. package/src/store/settings/store.ts +2 -2
  47. package/src/types/settings.ts +16 -8
  48. package/src/utils/storeDebug.ts +11 -0
  49. package/src/pages/chat/[id]/edit/AgentMeta/BackgroundSwatches.tsx +0 -31
  50. package/src/pages/chat/[id]/edit/AgentMeta/EmojiPicker.tsx +0 -78
  51. package/src/pages/chat/[id]/edit/AgentMeta/index.tsx +0 -111
  52. /package/src/pages/chat/[id]/{Config → Sidebar}/Header.tsx +0 -0
  53. /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/DefaultContent.tsx +0 -0
  54. /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/TopicItem.tsx +0 -0
  55. /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/index.tsx +0 -0
  56. /package/src/pages/chat/[id]/{edit → setting}/Header.tsx +0 -0
  57. /package/src/pages/{setting → settings}/Header.tsx +0 -0
  58. /package/src/pages/{setting → settings}/ThemeSwatches/ThemeSwatchesNeutral.tsx +0 -0
  59. /package/src/pages/{setting → settings}/ThemeSwatches/ThemeSwatchesPrimary.tsx +0 -0
  60. /package/src/pages/{setting → settings}/ThemeSwatches/index.ts +0 -0
@@ -6,9 +6,12 @@ import { SessionStore, sessionSelectors } from '@/store/session';
6
6
  import { MetaData } from '@/types/meta';
7
7
  import { LobeAgentConfig } from '@/types/session';
8
8
  import { fetchPresetTaskResult } from '@/utils/fetch';
9
+ import { setNamespace } from '@/utils/storeDebug';
9
10
 
10
11
  import { SessionLoadingState } from './initialState';
11
12
 
13
+ const t = setNamespace('agentConfig');
14
+
12
15
  /**
13
16
  * 代理行为接口
14
17
  */
@@ -234,6 +237,10 @@ export const createAgentSlice: StateCreator<
234
237
  }
235
238
  },
236
239
  updateLoadingState: (key, value) => {
237
- set({ autocompleteLoading: { ...get().autocompleteLoading, [key]: value } });
240
+ set(
241
+ { autocompleteLoading: { ...get().autocompleteLoading, [key]: value } },
242
+ false,
243
+ t('updateLoadingState', { key, value }),
244
+ );
238
245
  },
239
246
  });
@@ -1,4 +1,4 @@
1
- import { LanguageModel } from '@/types/llm';
1
+ import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
2
2
  import { MetaData } from '@/types/meta';
3
3
  import { LobeAgentConfig } from '@/types/session';
4
4
 
@@ -8,18 +8,7 @@ export interface AgentConfigState {
8
8
  autocompleteLoading: SessionLoadingState;
9
9
  }
10
10
 
11
- export const initialLobeAgentConfig: LobeAgentConfig = {
12
- displayMode: 'chat',
13
- model: LanguageModel.GPT3_5,
14
- params: {
15
- frequency_penalty: 0,
16
- presence_penalty: 0,
17
- temperature: 0.6,
18
- top_p: 1,
19
- },
20
- plugins: [],
21
- systemRole: '',
22
- };
11
+ export const initialLobeAgentConfig: LobeAgentConfig = DEFAULT_AGENT_CONFIG;
23
12
 
24
13
  export const initialAgentConfigState: AgentConfigState = {
25
14
  // // loading 中间态
@@ -1,10 +1,11 @@
1
1
  import { t } from 'i18next';
2
+ import { defaults } from 'lodash-es';
2
3
 
3
4
  import { DEFAULT_AVATAR, DEFAULT_BACKGROUND_COLOR } from '@/const/meta';
5
+ import { Autocomplete } from '@/features/AgentSetting/AgentMeta';
4
6
  import { SessionStore } from '@/store/session';
5
7
  import { LanguageModel } from '@/types/llm';
6
8
  import { MetaData } from '@/types/meta';
7
- import { LobeAgentConfig } from '@/types/session';
8
9
 
9
10
  import { sessionSelectors } from '../session';
10
11
  import { initialLobeAgentConfig } from './initialState';
@@ -15,6 +16,13 @@ const currentAgentMeta = (s: SessionStore): MetaData => {
15
16
  return { avatar: DEFAULT_AVATAR, backgroundColor: DEFAULT_BACKGROUND_COLOR, ...session?.meta };
16
17
  };
17
18
 
19
+ const currentAutocomplete = (s: SessionStore): Autocomplete => ({
20
+ autocompleteMeta: s.autocompleteMeta,
21
+ autocompleteSessionAgentMeta: s.autocompleteSessionAgentMeta,
22
+ id: s.activeId,
23
+ loading: s.autocompleteLoading,
24
+ });
25
+
18
26
  const currentAgentTitle = (s: SessionStore) => currentAgentMeta(s)?.title || t('defaultSession');
19
27
 
20
28
  const currentAgentDescription = (s: SessionStore) =>
@@ -34,15 +42,11 @@ const currentAgentAvatar = (s: SessionStore) => {
34
42
 
35
43
  const currentAgentConfig = (s: SessionStore) => {
36
44
  const session = sessionSelectors.currentSession(s);
37
- return session?.config;
38
- };
39
-
40
- const currentAgentConfigSafe = (s: SessionStore): LobeAgentConfig => {
41
- return currentAgentConfig(s) || initialLobeAgentConfig;
45
+ return defaults(session?.config, initialLobeAgentConfig);
42
46
  };
43
47
 
44
48
  const currentAgentSystemRole = (s: SessionStore) => {
45
- return currentAgentConfigSafe(s).systemRole;
49
+ return currentAgentConfig(s).systemRole;
46
50
  };
47
51
 
48
52
  const currentAgentModel = (s: SessionStore): LanguageModel => {
@@ -52,7 +56,7 @@ const currentAgentModel = (s: SessionStore): LanguageModel => {
52
56
  };
53
57
 
54
58
  const hasSystemRole = (s: SessionStore) => {
55
- const config = currentAgentConfigSafe(s);
59
+ const config = currentAgentConfig(s);
56
60
 
57
61
  return !!config.systemRole;
58
62
  };
@@ -64,12 +68,12 @@ export const agentSelectors = {
64
68
  currentAgentAvatar,
65
69
  currentAgentBackgroundColor,
66
70
  currentAgentConfig,
67
- currentAgentConfigSafe,
68
71
  currentAgentDescription,
69
72
  currentAgentMeta,
70
73
  currentAgentModel,
71
74
  currentAgentSystemRole,
72
75
  currentAgentTitle,
76
+ currentAutocomplete,
73
77
  getAvatar,
74
78
  getTitle,
75
79
  hasSystemRole,
@@ -1,3 +1,4 @@
1
+ import { template } from 'lodash-es';
1
2
  import { StateCreator } from 'zustand/vanilla';
2
3
 
3
4
  import { LOADING_FLAT } from '@/const/message';
@@ -7,10 +8,13 @@ import { SessionStore, agentSelectors, chatSelectors, sessionSelectors } from '@
7
8
  import { ChatMessage, OpenAIFunctionCall } from '@/types/chatMessage';
8
9
  import { fetchSSE } from '@/utils/fetch';
9
10
  import { isFunctionMessage } from '@/utils/message';
11
+ import { setNamespace } from '@/utils/storeDebug';
10
12
  import { nanoid } from '@/utils/uuid';
11
13
 
12
14
  import { MessageDispatch, messagesReducer } from '../reducers/message';
13
15
 
16
+ const t = setNamespace('chat/message');
17
+
14
18
  /**
15
19
  * 聊天操作
16
20
  */
@@ -110,11 +114,48 @@ export const chatMessage: StateCreator<
110
114
 
111
115
  generateMessage: async (messages, assistantId) => {
112
116
  const { dispatchMessage } = get();
113
- set({ chatLoadingId: assistantId });
114
- const config = agentSelectors.currentAgentConfigSafe(get());
117
+ set(
118
+ { chatLoadingId: assistantId },
119
+ false,
120
+ t('generateMessage(start)', { assistantId, messages }),
121
+ );
122
+ const config = agentSelectors.currentAgentConfig(get());
123
+
124
+ const compiler = template(config.inputTemplate, { interpolate: /{{([\S\s]+?)}}/g });
125
+
126
+ // 对 message 做统一预处理
127
+
128
+ // 1. 替换 inputMessage 模板
129
+ const postMessages = !config.inputTemplate
130
+ ? messages
131
+ : messages.map((m) => {
132
+ if (m.role === 'user') {
133
+ try {
134
+ return { ...m, content: compiler({ text: m.content }) };
135
+ } catch (error) {
136
+ console.error(error);
137
+
138
+ return m;
139
+ }
140
+ }
141
+ return m;
142
+ });
143
+
144
+ // 2. TODO 按参数设定截断长度
145
+
146
+ // 3. 添加 systemRole
147
+ const { systemRole } = agentSelectors.currentAgentConfig(get());
148
+ if (systemRole) {
149
+ postMessages.unshift({ content: systemRole, role: 'system' } as ChatMessage);
150
+ }
115
151
 
116
152
  const fetcher = () =>
117
- fetchChatModel({ messages, model: config.model, ...config.params, plugins: config.plugins });
153
+ fetchChatModel({
154
+ messages: postMessages,
155
+ model: config.model,
156
+ ...config.params,
157
+ plugins: config.plugins,
158
+ });
118
159
 
119
160
  let output = '';
120
161
  let isFunctionCall = false;
@@ -146,7 +187,7 @@ export const chatMessage: StateCreator<
146
187
  },
147
188
  });
148
189
 
149
- set({ chatLoadingId: undefined });
190
+ set({ chatLoadingId: undefined }, false, t('generateMessage(end)'));
150
191
 
151
192
  return { isFunctionCall };
152
193
  },
@@ -154,13 +195,9 @@ export const chatMessage: StateCreator<
154
195
  realFetchAIResponse: async (messages, userMessageId) => {
155
196
  const { dispatchMessage, generateMessage, triggerFunctionCall, activeTopicId } = get();
156
197
 
157
- // 添加 systemRole
158
- const { systemRole, model } = agentSelectors.currentAgentConfigSafe(get());
159
- if (systemRole) {
160
- messages.unshift({ content: systemRole, role: 'system' } as ChatMessage);
161
- }
198
+ const { model } = agentSelectors.currentAgentConfig(get());
162
199
 
163
- // 再添加一个空的信息用于放置 ai 响应,注意顺序不能反
200
+ // 添加一个空的信息用于放置 ai 响应,注意顺序不能反
164
201
  // 因为如果顺序反了,messages 中将包含新增的 ai message
165
202
  const mid = nanoid();
166
203
 
@@ -283,10 +320,10 @@ export const chatMessage: StateCreator<
283
320
 
284
321
  const mid = nanoid();
285
322
 
286
- dispatchMessage({ id: mid, message: LOADING_FLAT, role: 'assistant', type: 'addMessage' });
287
-
288
323
  const chats = chatSelectors.currentChats(get());
289
324
 
325
+ dispatchMessage({ id: mid, message: LOADING_FLAT, role: 'assistant', type: 'addMessage' });
326
+
290
327
  await generateMessage(chats, mid);
291
328
  },
292
329
  });
@@ -4,10 +4,12 @@ import { LOADING_FLAT } from '@/const/message';
4
4
  import { promptSummaryTitle } from '@/prompts/chat';
5
5
  import { SessionStore, chatSelectors, sessionSelectors, topicSelectors } from '@/store/session';
6
6
  import { fetchPresetTaskResult } from '@/utils/fetch';
7
+ import { setNamespace } from '@/utils/storeDebug';
7
8
  import { nanoid } from '@/utils/uuid';
8
9
 
9
10
  import { ChatTopicDispatch, topicReducer } from '../reducers/topic';
10
11
 
12
+ const t = setNamespace('chat/topic');
11
13
  export interface ChatTopicAction {
12
14
  /**
13
15
  * 分发主题
@@ -116,9 +118,9 @@ export const chatTopic: StateCreator<
116
118
  });
117
119
  },
118
120
  toggleTopic: (id) => {
119
- set({ activeTopicId: id });
121
+ set({ activeTopicId: id }, false, t('toggleTopic'));
120
122
  },
121
123
  updateTopicLoading: (id) => {
122
- set({ topicLoadingId: id });
124
+ set({ topicLoadingId: id }, false, t('updateTopicLoading'));
123
125
  },
124
126
  });
@@ -23,7 +23,7 @@ export const currentChats = (s: SessionStore): ChatMessage[] => {
23
23
  };
24
24
 
25
25
  export const systemRoleSel = (s: SessionStore): string => {
26
- const config = agentSelectors.currentAgentConfigSafe(s);
26
+ const config = agentSelectors.currentAgentConfig(s);
27
27
 
28
28
  return config.systemRole;
29
29
  };
@@ -5,10 +5,12 @@ import { StateCreator } from 'zustand/vanilla';
5
5
 
6
6
  import { SessionStore, initLobeSession } from '@/store/session';
7
7
  import { LobeAgentSession, LobeSessions } from '@/types/session';
8
+ import { setNamespace } from '@/utils/storeDebug';
8
9
  import { uuid } from '@/utils/uuid';
9
10
 
10
11
  import { SessionDispatch, sessionsReducer } from './reducers/session';
11
12
 
13
+ const t = setNamespace('session');
12
14
  export interface SessionAction {
13
15
  activeSession: (sessionId: string) => void;
14
16
  clearSessions: () => void;
@@ -63,11 +65,11 @@ export const createSessionSlice: StateCreator<
63
65
  SessionAction
64
66
  > = (set, get) => ({
65
67
  activeSession: (sessionId) => {
66
- set({ activeId: sessionId });
68
+ set({ activeId: sessionId }, false, t('activeSession'));
67
69
  },
68
70
 
69
71
  clearSessions: () => {
70
- set({ sessions: {} });
72
+ set({ sessions: {} }, false, t('clearSessions'));
71
73
  },
72
74
 
73
75
  createSession: async () => {
@@ -91,24 +93,29 @@ export const createSessionSlice: StateCreator<
91
93
 
92
94
  dispatchSession: (payload) => {
93
95
  const { type, ...res } = payload;
94
- set({ sessions: sessionsReducer(get().sessions, payload) }, false, {
95
- payload: res,
96
- type: `dispatchChat/${type}`,
97
- });
96
+ set(
97
+ { sessions: sessionsReducer(get().sessions, payload) },
98
+ false,
99
+ t(`dispatchChat/${type}`, res),
100
+ );
98
101
  },
99
102
 
100
103
  importSessions: (importSessions) => {
101
104
  const { sessions } = get();
102
- set({
103
- sessions: produce(sessions, (draft) => {
104
- for (const [id, session] of Object.entries(importSessions)) {
105
- // 如果已经存在,则跳过
106
- if (draft[id]) continue;
107
-
108
- draft[id] = session;
109
- }
110
- }),
111
- });
105
+ set(
106
+ {
107
+ sessions: produce(sessions, (draft) => {
108
+ for (const [id, session] of Object.entries(importSessions)) {
109
+ // 如果已经存在,则跳过
110
+ if (draft[id]) continue;
111
+
112
+ draft[id] = session;
113
+ }
114
+ }),
115
+ },
116
+ false,
117
+ t('importSessions', importSessions),
118
+ );
112
119
  },
113
120
  // genShareUrl: () => {
114
121
  // const session = sessionSelectors.currentSession(get());
@@ -1,3 +1,4 @@
1
+ import { DEFAULT_AGENT_META } from '@/const/meta';
1
2
  import { LobeAgentSession, LobeSessionType } from '@/types/session';
2
3
 
3
4
  import { initialLobeAgentConfig } from '../agentConfig';
@@ -18,7 +19,7 @@ export const initLobeSession: LobeAgentSession = {
18
19
  config: initialLobeAgentConfig,
19
20
  createAt: Date.now(),
20
21
  id: '',
21
- meta: {},
22
+ meta: DEFAULT_AGENT_META,
22
23
  type: LobeSessionType.Agent,
23
24
  updateAt: Date.now(),
24
25
  };
@@ -1,26 +1,41 @@
1
1
  import { ThemeMode } from 'antd-style';
2
+ import { produce } from 'immer';
3
+ import { merge } from 'lodash-es';
2
4
  import type { StateCreator } from 'zustand/vanilla';
3
5
 
6
+ import { DEFAULT_AGENT, DEFAULT_BASE_SETTINGS, DEFAULT_SETTINGS } from '@/const/settings';
7
+ import { MetaData } from '@/types/meta';
8
+ import type { LobeAgentSession } from '@/types/session';
9
+ import { LobeAgentConfig } from '@/types/session';
4
10
  import type { GlobalSettings } from '@/types/settings';
11
+ import { setNamespace } from '@/utils/storeDebug';
5
12
 
6
13
  import type { SidebarTabKey } from './initialState';
7
- import { DEFAULT_SETTINGS, SettingsState } from './initialState';
14
+ import { AppSettingsState } from './initialState';
8
15
  import type { SettingsStore } from './store';
9
16
 
17
+ const t = setNamespace('settings');
18
+
10
19
  /**
11
20
  * 设置操作
12
21
  */
13
22
  export interface SettingsAction {
14
- importSettings: (settings: GlobalSettings) => void;
23
+ importAppSettings: (settings: GlobalSettings) => void;
24
+ resetAgentConfig: () => void;
25
+ resetAgentMeta: () => void;
26
+ resetBaseSettings: () => void;
27
+ resetDefaultAgent: () => void;
15
28
  /**
16
29
  * 重置设置
17
30
  */
18
31
  resetSettings: () => void;
32
+ setAgentConfig: (config: Partial<LobeAgentSession['config']>) => void;
33
+ setAgentMeta: (meta: Partial<LobeAgentSession['meta']>) => void;
19
34
  /**
20
35
  * 设置全局设置
21
36
  * @param settings - 全局设置
22
37
  */
23
- setGlobalSettings: (settings: SettingsState) => void;
38
+ setAppSettings: (settings: AppSettingsState) => void;
24
39
  /**
25
40
  * 设置部分配置设置
26
41
  * @param settings - 部分配置设置
@@ -37,6 +52,7 @@ export interface SettingsAction {
37
52
  */
38
53
  switchSideBar: (key: SidebarTabKey) => void;
39
54
  toggleAgentPanel: (visible?: boolean) => void;
55
+ toggleAgentPlugin: (pluginId: string) => void;
40
56
  }
41
57
 
42
58
  export const createSettings: StateCreator<
@@ -45,10 +61,10 @@ export const createSettings: StateCreator<
45
61
  [],
46
62
  SettingsAction
47
63
  > = (set, get) => ({
48
- importSettings: (importSettings) => {
64
+ importAppSettings: (importAppSettings) => {
49
65
  const { setSettings } = get();
50
66
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
51
- const { OPENAI_API_KEY: _, password: __, ...settings } = importSettings;
67
+ const { OPENAI_API_KEY: _, password: __, ...settings } = importAppSettings;
52
68
 
53
69
  setSettings({
54
70
  ...settings,
@@ -56,25 +72,80 @@ export const createSettings: StateCreator<
56
72
  avatar: !get().settings.avatar ? settings.avatar : get().settings.avatar,
57
73
  });
58
74
  },
75
+ resetAgentConfig: () => {
76
+ const settings = produce(get().settings, (draft: GlobalSettings) => {
77
+ draft.defaultAgent.config = DEFAULT_AGENT.config;
78
+ });
79
+ set({ settings }, false, t('resetAgentConfig'));
80
+ },
81
+ resetAgentMeta: () => {
82
+ const settings = produce(get().settings, (draft: GlobalSettings) => {
83
+ draft.defaultAgent.meta = DEFAULT_AGENT.meta;
84
+ });
85
+ set({ settings }, false, t('resetAgentMeta'));
86
+ },
87
+ resetBaseSettings: () => {
88
+ const settings = get().settings;
89
+
90
+ set({ settings: { ...settings, ...DEFAULT_BASE_SETTINGS } }, false, t('resetBaseSettings'));
91
+ },
92
+ resetDefaultAgent: () => {
93
+ const settings = produce(get().settings, (draft: GlobalSettings) => {
94
+ draft.defaultAgent = DEFAULT_AGENT;
95
+ });
96
+ set({ settings }, false, t('resetDefaultAgent'));
97
+ },
59
98
  resetSettings: () => {
60
- set({ settings: DEFAULT_SETTINGS });
99
+ set({ settings: DEFAULT_SETTINGS }, false, t('resetSettings'));
61
100
  },
62
- setGlobalSettings: (settings) => {
63
- set({ ...settings });
101
+ setAgentConfig: (config) => {
102
+ const settings = produce(get().settings, (draft: GlobalSettings) => {
103
+ const oldConfig = draft.defaultAgent.config as LobeAgentConfig;
104
+ draft.defaultAgent.config = merge(oldConfig, config);
105
+ });
106
+
107
+ set({ settings }, false, t('setAgentConfig', config));
108
+ },
109
+ setAgentMeta: (meta) => {
110
+ const settings = produce(get().settings, (draft) => {
111
+ const oldMeta = draft.defaultAgent.meta as MetaData;
112
+ draft.defaultAgent.meta = merge(oldMeta, meta);
113
+ });
114
+
115
+ set({ settings }, false, t('setAgentMeta', meta));
116
+ },
117
+ setAppSettings: (settings) => {
118
+ set({ ...settings }, false, t('setAppSettings', settings));
64
119
  },
65
120
  setSettings: (settings) => {
66
121
  const oldSetting = get().settings;
67
- set({ settings: { ...oldSetting, ...settings } });
122
+ set({ settings: merge(oldSetting, settings) }, false, t('setSettings', settings));
68
123
  },
69
124
  setThemeMode: (themeMode) => {
70
125
  get().setSettings({ themeMode });
71
126
  },
72
127
  switchSideBar: (key) => {
73
- set({ sidebarKey: key });
128
+ set({ sidebarKey: key }, false, t('switchSideBar', key));
74
129
  },
75
130
  toggleAgentPanel: (newValue) => {
76
131
  const showAgentConfig = typeof newValue === 'boolean' ? newValue : !get().showAgentConfig;
77
132
 
78
- set({ showAgentConfig });
133
+ set({ showAgentConfig }, false, t('toggleAgentPanel', newValue));
134
+ },
135
+ toggleAgentPlugin: (id: string) => {
136
+ const settings = produce(get().settings, (draft: GlobalSettings) => {
137
+ const oldConfig = draft.defaultAgent.config as LobeAgentConfig;
138
+ if (oldConfig.plugins === undefined) {
139
+ oldConfig.plugins = [id];
140
+ } else {
141
+ if (oldConfig.plugins.includes(id)) {
142
+ oldConfig.plugins.splice(oldConfig.plugins.indexOf(id), 1);
143
+ } else {
144
+ oldConfig.plugins.push(id);
145
+ }
146
+ }
147
+ });
148
+
149
+ set({ settings }, false, t('toggleAgentPlugin', id));
79
150
  },
80
151
  });
@@ -1,32 +1,9 @@
1
- import { LanguageModel } from '@/types/llm';
1
+ import { DEFAULT_SETTINGS } from '@/const/settings';
2
2
  import type { GlobalSettings } from '@/types/settings';
3
3
 
4
- export type SidebarTabKey = 'chat' | 'market' | 'setting';
4
+ export type SidebarTabKey = 'chat' | 'market' | 'settings';
5
5
 
6
- export const DEFAULT_SETTINGS: GlobalSettings = {
7
- OPENAI_API_KEY: '',
8
- avatar: '',
9
- compressThreshold: 24,
10
- enableCompressThreshold: false,
11
- enableHistoryCount: false,
12
- enableMaxTokens: true,
13
- endpoint: '',
14
- fontSize: 14,
15
- frequencyPenalty: 0,
16
- historyCount: 24,
17
- language: 'zh-CN',
18
- maxTokens: 2000,
19
- model: LanguageModel.GPT3_5,
20
- neutralColor: '',
21
- password: '',
22
- presencePenalty: 0,
23
- primaryColor: '',
24
- temperature: 0.5,
25
- themeMode: 'auto',
26
- topP: 1,
27
- };
28
-
29
- export interface SettingsState {
6
+ export interface AppSettingsState {
30
7
  inputHeight: number;
31
8
  sessionExpandable?: boolean;
32
9
  sessionsWidth: number;
@@ -35,7 +12,7 @@ export interface SettingsState {
35
12
  sidebarKey: SidebarTabKey;
36
13
  }
37
14
 
38
- export const initialState: SettingsState = {
15
+ export const initialState: AppSettingsState = {
39
16
  inputHeight: 200,
40
17
  sessionExpandable: true,
41
18
  sessionsWidth: 320,
@@ -1,16 +1,20 @@
1
1
  import { defaults } from 'lodash-es';
2
2
 
3
- import { DEFAULT_SETTINGS } from '@/store/settings/initialState';
3
+ import { DEFAULT_AGENT_META } from '@/const/meta';
4
+ import { DEFAULT_AGENT, DEFAULT_AGENT_CONFIG, DEFAULT_SETTINGS } from '@/const/settings';
4
5
  import { GlobalSettings } from '@/types/settings';
5
6
 
6
7
  import { SettingsStore } from './store';
7
8
 
8
9
  const currentSettings = (s: SettingsStore) => defaults(s.settings, DEFAULT_SETTINGS);
9
10
 
10
- const selecThemeMode = (s: SettingsStore) => ({
11
- setThemeMode: s.setThemeMode,
12
- themeMode: s.settings.themeMode,
13
- });
11
+ const currentDefaultAgent = (s: SettingsStore) => defaults(s.settings.defaultAgent, DEFAULT_AGENT);
12
+
13
+ const currentAgentConfig = (s: SettingsStore) =>
14
+ defaults(s.settings.defaultAgent.config, DEFAULT_AGENT_CONFIG);
15
+
16
+ const currentAgentMeta = (s: SettingsStore) =>
17
+ defaults(s.settings.defaultAgent.meta, DEFAULT_AGENT_META);
14
18
 
15
19
  export const exportSettings = (s: SettingsStore) => {
16
20
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -20,8 +24,9 @@ export const exportSettings = (s: SettingsStore) => {
20
24
  };
21
25
 
22
26
  export const settingsSelectors = {
27
+ currentAgentConfig,
28
+ currentAgentMeta,
29
+ currentDefaultAgent,
23
30
  currentSettings,
24
31
  exportSettings,
25
-
26
- selecThemeMode,
27
32
  };
@@ -1,9 +1,9 @@
1
1
  import { StateCreator } from 'zustand/vanilla';
2
2
 
3
3
  import { type SettingsAction, createSettings } from './action';
4
- import { type SettingsState, initialState } from './initialState';
4
+ import { type AppSettingsState, initialState } from './initialState';
5
5
 
6
- export type SettingsStore = SettingsAction & SettingsState;
6
+ export type SettingsStore = SettingsAction & AppSettingsState;
7
7
 
8
8
  export const createStore: StateCreator<SettingsStore, [['zustand/devtools', never]]> = (
9
9
  ...parameters
@@ -1,13 +1,11 @@
1
- import { NeutralColors, PrimaryColors } from '@lobehub/ui';
2
- import { ThemeMode } from 'antd-style';
1
+ import type { NeutralColors, PrimaryColors } from '@lobehub/ui';
2
+ import type { ThemeMode } from 'antd-style';
3
3
 
4
- import { Locales } from '@/locales/resources';
5
- import { LanguageModel } from '@/types/llm';
4
+ import type { Locales } from '@/locales/resources';
5
+ import type { LanguageModel } from '@/types/llm';
6
+ import type { LobeAgentSession } from '@/types/session';
6
7
 
7
- /**
8
- * 配置设置
9
- */
10
- export interface GlobalSettings {
8
+ export interface GlobalBaseSettings {
11
9
  OPENAI_API_KEY: string;
12
10
  avatar: string;
13
11
  compressThreshold: number;
@@ -29,4 +27,14 @@ export interface GlobalSettings {
29
27
  themeMode: ThemeMode;
30
28
  topP: number;
31
29
  }
30
+
31
+ export type GlobalDefaultAgent = Partial<LobeAgentSession>;
32
+
33
+ /**
34
+ * 配置设置
35
+ */
36
+ export interface GlobalSettings extends GlobalBaseSettings {
37
+ defaultAgent: GlobalDefaultAgent;
38
+ }
39
+
32
40
  export type ConfigKeys = keyof GlobalSettings;
@@ -0,0 +1,11 @@
1
+ export const setNamespace = (namespace: string) => {
2
+ return (type: string, payload?: any) => {
3
+ const name = [namespace, type].filter(Boolean).join('/');
4
+ return payload
5
+ ? {
6
+ payload,
7
+ type: name,
8
+ }
9
+ : name;
10
+ };
11
+ };
@@ -1,31 +0,0 @@
1
- import { Swatches, primaryColorsSwatches } from '@lobehub/ui';
2
- import { memo } from 'react';
3
- import { shallow } from 'zustand/shallow';
4
-
5
- import { DEFAULT_BACKGROUND_COLOR } from '@/const/meta';
6
- import { agentSelectors, useSessionStore } from '@/store/session';
7
-
8
- const BackgroundSwatches = memo(() => {
9
- const [backgroundColor, updateAgentMeta] = useSessionStore(
10
- (s) => [agentSelectors.currentAgentBackgroundColor(s), s.updateAgentMeta],
11
- shallow,
12
- );
13
-
14
- const handleSelect = (v: any) => {
15
- if (v) {
16
- updateAgentMeta({ backgroundColor: v });
17
- } else {
18
- updateAgentMeta({ backgroundColor: DEFAULT_BACKGROUND_COLOR });
19
- }
20
- };
21
-
22
- return (
23
- <Swatches
24
- activeColor={backgroundColor}
25
- colors={primaryColorsSwatches}
26
- onSelect={handleSelect}
27
- />
28
- );
29
- });
30
-
31
- export default BackgroundSwatches;