@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.
- package/CHANGELOG.md +52 -0
- package/package.json +2 -2
- package/src/components/EmojiPicker/index.tsx +48 -0
- package/src/components/EmojiPicker/style.ts +28 -0
- package/src/const/meta.ts +3 -0
- package/src/const/settings.ts +50 -0
- package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentConfig/index.tsx +13 -11
- package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentMeta/AutoGenerateInput.tsx +16 -14
- package/src/features/AgentSetting/AgentMeta/BackgroundSwatches.tsx +27 -0
- package/src/features/AgentSetting/AgentMeta/index.tsx +128 -0
- package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentPlugin/index.tsx +12 -12
- package/src/{pages/chat/[id]/edit → features/AgentSetting}/AgentPrompt/index.tsx +13 -16
- package/src/features/AgentSetting/index.ts +4 -0
- package/src/features/AgentSetting/test.json +40 -0
- package/src/features/SideBar/{BottomAction.tsx → BottomActions.tsx} +15 -5
- package/src/features/SideBar/TopActions.tsx +42 -0
- package/src/features/SideBar/index.tsx +5 -30
- package/src/hooks/useImportConfig.ts +3 -3
- package/src/locales/default/common.ts +4 -0
- package/src/locales/default/setting.ts +4 -0
- package/src/pages/chat/[id]/Conversation/ChatList/index.tsx +1 -1
- package/src/pages/chat/[id]/Conversation/Input/Action.tsx +1 -1
- package/src/pages/chat/[id]/Header.tsx +1 -1
- package/src/pages/chat/[id]/{Config/SideBar.tsx → Sidebar/Inner.tsx} +2 -2
- package/src/pages/chat/[id]/{Config → Sidebar}/Topic/TopicContent.tsx +4 -6
- package/src/pages/chat/[id]/{Config → Sidebar}/index.tsx +2 -2
- package/src/pages/chat/[id]/index.page.tsx +1 -1
- package/src/pages/chat/[id]/{edit → setting}/index.page.tsx +23 -9
- package/src/pages/settings/Settings/Agent.tsx +25 -0
- package/src/pages/{setting/SettingForm.tsx → settings/Settings/Common.tsx} +7 -113
- package/src/pages/settings/Settings/index.tsx +50 -0
- package/src/pages/{setting → settings}/index.page.tsx +2 -2
- package/src/pages/{setting → settings}/layout.tsx +1 -1
- package/src/plugins/searchEngine/Render.tsx +3 -1
- package/src/store/session/slices/agentConfig/action.ts +8 -1
- package/src/store/session/slices/agentConfig/initialState.ts +2 -13
- package/src/store/session/slices/agentConfig/selectors.ts +13 -9
- package/src/store/session/slices/chat/actions/message.ts +49 -12
- package/src/store/session/slices/chat/actions/topic.ts +4 -2
- package/src/store/session/slices/chat/selectors/chat.ts +1 -1
- package/src/store/session/slices/session/action.ts +23 -16
- package/src/store/session/slices/session/initialState.ts +2 -1
- package/src/store/settings/action.ts +82 -11
- package/src/store/settings/initialState.ts +4 -27
- package/src/store/settings/selectors.ts +12 -7
- package/src/store/settings/store.ts +2 -2
- package/src/types/settings.ts +16 -8
- package/src/utils/storeDebug.ts +11 -0
- package/src/pages/chat/[id]/edit/AgentMeta/BackgroundSwatches.tsx +0 -31
- package/src/pages/chat/[id]/edit/AgentMeta/EmojiPicker.tsx +0 -78
- package/src/pages/chat/[id]/edit/AgentMeta/index.tsx +0 -111
- /package/src/pages/chat/[id]/{Config → Sidebar}/Header.tsx +0 -0
- /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/DefaultContent.tsx +0 -0
- /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/TopicItem.tsx +0 -0
- /package/src/pages/chat/[id]/{Config → Sidebar}/Topic/index.tsx +0 -0
- /package/src/pages/chat/[id]/{edit → setting}/Header.tsx +0 -0
- /package/src/pages/{setting → settings}/Header.tsx +0 -0
- /package/src/pages/{setting → settings}/ThemeSwatches/ThemeSwatchesNeutral.tsx +0 -0
- /package/src/pages/{setting → settings}/ThemeSwatches/ThemeSwatchesPrimary.tsx +0 -0
- /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(
|
|
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 {
|
|
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
|
|
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 =
|
|
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(
|
|
114
|
-
|
|
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({
|
|
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
|
-
|
|
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
|
-
//
|
|
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.
|
|
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(
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 } =
|
|
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
|
-
|
|
63
|
-
|
|
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:
|
|
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 {
|
|
1
|
+
import { DEFAULT_SETTINGS } from '@/const/settings';
|
|
2
2
|
import type { GlobalSettings } from '@/types/settings';
|
|
3
3
|
|
|
4
|
-
export type SidebarTabKey = 'chat' | 'market' | '
|
|
4
|
+
export type SidebarTabKey = 'chat' | 'market' | 'settings';
|
|
5
5
|
|
|
6
|
-
export
|
|
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:
|
|
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 {
|
|
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
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
4
|
+
import { type AppSettingsState, initialState } from './initialState';
|
|
5
5
|
|
|
6
|
-
export type SettingsStore = SettingsAction &
|
|
6
|
+
export type SettingsStore = SettingsAction & AppSettingsState;
|
|
7
7
|
|
|
8
8
|
export const createStore: StateCreator<SettingsStore, [['zustand/devtools', never]]> = (
|
|
9
9
|
...parameters
|
package/src/types/settings.ts
CHANGED
|
@@ -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;
|
|
@@ -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;
|