@lobehub/chat 0.105.2 → 0.107.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 +50 -0
- package/README.md +18 -0
- package/README.zh-CN.md +18 -0
- package/docs/Environment-Variable.md +4 -2
- package/docs/Environment-Variable.zh-CN.md +4 -2
- package/locales/en_US/common.json +19 -0
- package/locales/en_US/migration.json +45 -0
- package/locales/en_US/setting.json +7 -7
- package/locales/ja_JP/common.json +19 -0
- package/locales/ja_JP/migration.json +45 -0
- package/locales/ja_JP/setting.json +2 -2
- package/locales/ko_KR/common.json +19 -0
- package/locales/ko_KR/migration.json +45 -0
- package/locales/ko_KR/setting.json +2 -2
- package/locales/ru_RU/common.json +19 -0
- package/locales/ru_RU/migration.json +45 -0
- package/locales/ru_RU/setting.json +2 -2
- package/locales/zh_CN/common.json +19 -0
- package/locales/zh_CN/migration.json +45 -0
- package/locales/zh_CN/setting.json +1 -1
- package/locales/zh_TW/common.json +19 -0
- package/locales/zh_TW/migration.json +45 -0
- package/locales/zh_TW/setting.json +2 -2
- package/package.json +2 -1
- package/src/app/api/openai/chat/createChatCompletion.test.ts +4 -4
- package/src/app/api/openai/chat/createChatCompletion.ts +4 -10
- package/src/app/api/openai/createBizOpenAI/index.ts +1 -1
- package/src/app/api/openai/errorResponse.test.ts +1 -1
- package/src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx +2 -1
- package/src/app/chat/(mobile)/features/ChatInput/SendButton.tsx +2 -2
- package/src/app/chat/features/ChatHeader/ShareButton/Inner.tsx +2 -5
- package/src/app/chat/features/ChatHeader/ShareButton/index.tsx +2 -2
- package/src/app/chat/features/ChatInput/ActionBar/Clear.tsx +2 -2
- package/src/app/chat/features/ChatInput/ActionBar/ModelSwitch.tsx +1 -1
- package/src/app/chat/features/ChatInput/ActionBar/Token/TokenTag.tsx +8 -2
- package/src/app/chat/features/ChatInput/InputAreaInner/index.tsx +2 -2
- package/src/app/chat/features/ChatInput/STT/index.tsx +2 -2
- package/src/app/chat/features/ChatInput/Topic/index.tsx +2 -2
- package/src/app/chat/features/ChatInput/useSend.ts +3 -3
- package/src/app/chat/features/Conversation/ChatList/Actions/index.ts +2 -2
- package/src/app/chat/features/Conversation/ChatList/Error/ApiKeyForm.tsx +2 -2
- package/src/app/chat/features/Conversation/ChatList/Error/InvalidAccess.tsx +2 -2
- package/src/app/chat/features/Conversation/ChatList/Error/Plugin/PluginSettings.tsx +2 -2
- package/src/app/chat/features/Conversation/ChatList/Extras/Assistant.test.tsx +4 -4
- package/src/app/chat/features/Conversation/ChatList/Extras/Assistant.tsx +30 -31
- package/src/app/chat/features/Conversation/ChatList/Extras/TTS.tsx +2 -2
- package/src/app/chat/features/Conversation/ChatList/Extras/Translate.tsx +2 -2
- package/src/app/chat/features/Conversation/ChatList/Extras/User.tsx +8 -5
- package/src/app/chat/features/Conversation/ChatList/Extras/index.ts +3 -3
- package/src/app/chat/features/Conversation/ChatList/Extras/type.ts +5 -0
- package/src/app/chat/features/Conversation/ChatList/Messages/Assistant.tsx +10 -6
- package/src/app/chat/features/Conversation/ChatList/Messages/Default.tsx +7 -3
- package/src/app/chat/features/Conversation/ChatList/Messages/Function.tsx +7 -6
- package/src/app/chat/features/Conversation/ChatList/Messages/index.ts +7 -7
- package/src/app/chat/features/Conversation/ChatList/Plugins/Render/StandaloneType/Iframe.tsx +6 -6
- package/src/app/chat/features/Conversation/ChatList/index.tsx +16 -24
- package/src/app/chat/features/Conversation/ScrollAnchor/index.tsx +2 -2
- package/src/app/chat/features/Conversation/index.tsx +3 -3
- package/src/app/chat/features/Conversation/useInitConversation.ts +43 -0
- package/src/app/chat/features/Migration/ExportConfigButton.tsx +33 -0
- package/src/app/chat/features/Migration/Failed.tsx +115 -0
- package/src/app/chat/features/Migration/Modal.tsx +80 -0
- package/src/app/chat/features/Migration/Start.tsx +106 -0
- package/src/app/chat/features/Migration/UpgradeButton.tsx +74 -0
- package/src/app/chat/features/Migration/const.ts +15 -0
- package/src/app/chat/features/Migration/index.tsx +44 -0
- package/src/app/chat/features/SessionListContent/DefaultMode.tsx +54 -0
- package/src/app/chat/features/SessionListContent/List/Item/Actions.tsx +5 -4
- package/src/app/chat/features/SessionListContent/List/Item/index.tsx +22 -28
- package/src/app/chat/features/SessionListContent/SearchMode.tsx +15 -0
- package/src/app/chat/features/SessionListContent/index.tsx +4 -44
- package/src/app/chat/features/SessionSearchBar/index.tsx +11 -3
- package/src/app/chat/features/TopicListContent/Header.tsx +4 -4
- package/src/app/chat/features/TopicListContent/Topic/TopicContent.tsx +11 -9
- package/src/app/chat/features/TopicListContent/Topic/TopicItem.tsx +15 -13
- package/src/app/chat/features/TopicListContent/Topic/index.tsx +9 -16
- package/src/app/chat/features/TopicListContent/TopicSearchBar/index.tsx +13 -4
- package/src/app/chat/features/TopicListContent/index.tsx +1 -2
- package/src/app/chat/page.tsx +6 -1
- package/src/app/chat/settings/features/HeaderContent.tsx +3 -3
- package/src/app/home/Redirect.tsx +13 -7
- package/src/app/settings/(mobile)/features/ExtraList.tsx +12 -4
- package/src/app/settings/common/Common.tsx +16 -4
- package/src/app/welcome/features/Banner/index.tsx +9 -10
- package/src/chains/summaryTitle.ts +5 -2
- package/src/components/DataStyleModal/index.tsx +64 -0
- package/src/config/client.ts +5 -0
- package/src/const/session.ts +21 -0
- package/src/const/settings.ts +1 -1
- package/src/database/core/__tests__/db.test.ts +8 -5
- package/src/database/core/__tests__/model.test.ts +3 -3
- package/src/database/core/db.ts +38 -10
- package/src/database/core/index.ts +1 -0
- package/src/database/core/model.ts +121 -10
- package/src/database/core/schemas.ts +22 -0
- package/src/database/core/types/db.ts +13 -0
- package/src/database/models/__DEBUG.ts +119 -0
- package/src/database/models/{file.test.ts → __tests__/file.test.ts} +19 -6
- package/src/database/models/__tests__/message.test.ts +314 -0
- package/src/database/models/__tests__/session.test.ts +117 -0
- package/src/database/models/__tests__/topic.test.ts +250 -0
- package/src/database/models/file.ts +9 -5
- package/src/database/models/message.ts +186 -0
- package/src/database/models/session.ts +160 -0
- package/src/database/models/topic.ts +196 -0
- package/src/{types/database → database/schemas}/files.ts +2 -2
- package/src/database/schemas/message.ts +37 -0
- package/src/database/schemas/session.ts +56 -0
- package/src/database/schemas/topic.ts +12 -0
- package/src/features/AgentSetting/AgentConfig/index.tsx +8 -1
- package/src/features/AgentSetting/AgentTTS/SelectWithTTSPreview.tsx +1 -1
- package/src/features/AgentSetting/store/action.ts +1 -1
- package/src/features/AgentSetting/store/initialState.ts +1 -1
- package/src/features/AgentSetting/store/reducers/config.ts +1 -1
- package/src/features/DataImporter/index.tsx +240 -0
- package/src/features/DebugUI/index.tsx +34 -0
- package/src/features/SideBar/BottomActions.tsx +8 -14
- package/src/hooks/useImportConfig.ts +13 -30
- package/src/hooks/useTTS.ts +1 -1
- package/src/layout/GlobalLayout/index.tsx +16 -1
- package/src/locales/default/common.ts +19 -0
- package/src/locales/default/migration.ts +47 -0
- package/src/locales/default/setting.ts +1 -1
- package/src/locales/resources/en_US.ts +2 -0
- package/src/locales/resources/ja_JP.ts +2 -0
- package/src/locales/resources/ko_KR.ts +2 -0
- package/src/locales/resources/ru_RU.ts +2 -0
- package/src/locales/resources/zh_CN.ts +2 -0
- package/src/locales/resources/zh_TW.ts +2 -0
- package/src/migrations/FromV0ToV1.ts +2 -4
- package/src/migrations/FromV1ToV2/fixtures/input-v1-session.json +191 -0
- package/src/migrations/FromV1ToV2/fixtures/output-v2.json +202 -0
- package/src/migrations/FromV1ToV2/index.ts +82 -0
- package/src/migrations/FromV1ToV2/migrations.test.ts +224 -0
- package/src/migrations/FromV1ToV2/types/v1.ts +78 -0
- package/src/migrations/FromV1ToV2/types/v2.ts +52 -0
- package/src/{utils → migrations}/VersionController.test.ts +1 -3
- package/src/{utils → migrations}/VersionController.ts +5 -2
- package/src/migrations/index.ts +17 -3
- package/src/services/__tests__/file.test.ts +3 -3
- package/src/services/chat.ts +66 -5
- package/src/services/config.ts +184 -0
- package/src/services/debug.ts +13 -0
- package/src/services/file.ts +6 -2
- package/src/services/github.ts +25 -0
- package/src/services/message.ts +79 -0
- package/src/services/session.ts +63 -0
- package/src/services/topic.ts +56 -0
- package/src/store/chat/actions/enhance.ts +95 -0
- package/src/store/{session/slices/chat → chat}/actions/message.ts +225 -247
- package/src/store/{session/slices/chat → chat}/actions/plugin.ts +29 -34
- package/src/store/{session/slices/chat → chat}/actions/share.ts +9 -13
- package/src/store/chat/actions/topic.ts +197 -0
- package/src/store/chat/helpers.ts +23 -0
- package/src/store/chat/index.ts +3 -0
- package/src/store/chat/initialState.ts +48 -0
- package/src/store/chat/reducers/message.test.ts +83 -0
- package/src/store/chat/reducers/message.ts +83 -0
- package/src/store/chat/selectors/__tests__/topic.test.ts +43 -0
- package/src/store/chat/selectors/chat.ts +119 -0
- package/src/store/chat/selectors/index.ts +2 -0
- package/src/store/chat/selectors/topic.ts +28 -0
- package/src/store/chat/store.ts +63 -0
- package/src/store/files/slices/images/action.ts +6 -4
- package/src/store/global/selectors/__snapshots__/settings.test.ts.snap +99 -0
- package/src/store/global/selectors/settings.test.ts +51 -0
- package/src/store/global/selectors/settings.ts +36 -5
- package/src/store/global/store.ts +1 -1
- package/src/store/middleware/createHyperStorage/index.ts +40 -24
- package/src/store/middleware/createHyperStorage/keyMapper.ts +8 -2
- package/src/store/middleware/createHyperStorage/type.ts +10 -8
- package/src/store/plugin/slices/plugin/action.ts +1 -1
- package/src/store/plugin/store.ts +2 -4
- package/src/store/session/helpers.ts +1 -1
- package/src/store/session/hooks/useSessionHydrated.ts +6 -10
- package/src/store/session/initialState.ts +26 -7
- package/src/store/session/selectors.ts +1 -2
- package/src/store/session/slices/agent/action.ts +26 -35
- package/src/store/session/slices/agent/selectors.ts +32 -37
- package/src/store/session/slices/session/action.ts +81 -88
- package/src/store/session/slices/session/helpers.ts +19 -0
- package/src/store/session/slices/session/initialState.ts +7 -10
- package/src/store/session/slices/session/selectors/export.ts +12 -9
- package/src/store/session/slices/session/selectors/index.ts +2 -35
- package/src/store/session/slices/session/selectors/list.test.ts +25 -203
- package/src/store/session/slices/session/selectors/list.ts +40 -78
- package/src/store/session/store.ts +11 -17
- package/src/types/agent/index.ts +61 -0
- package/src/types/chatMessage.ts +11 -20
- package/src/types/exportConfig.ts +7 -2
- package/src/types/files.ts +2 -2
- package/src/types/llm.ts +1 -1
- package/src/types/meta.ts +24 -12
- package/src/types/openai/chat.ts +9 -1
- package/src/types/openai/functionCall.ts +4 -0
- package/src/types/session.ts +10 -94
- package/src/types/settings.ts +2 -0
- package/src/types/translate.ts +2 -1
- package/src/utils/config.ts +5 -11
- package/src/utils/fetch.ts +8 -2
- package/src/app/chat/features/Conversation/usePluginsInit.ts +0 -14
- package/src/database/core/schema.ts +0 -9
- package/src/helpers/export.ts +0 -69
- package/src/hooks/useExportConfig.ts +0 -6
- package/src/store/session/slices/chat/actions/index.ts +0 -32
- package/src/store/session/slices/chat/actions/topic.ts +0 -171
- package/src/store/session/slices/chat/actions/translate.ts +0 -113
- package/src/store/session/slices/chat/helpers.ts +0 -9
- package/src/store/session/slices/chat/initialState.ts +0 -13
- package/src/store/session/slices/chat/reducers/message.test.ts +0 -319
- package/src/store/session/slices/chat/reducers/message.ts +0 -156
- package/src/store/session/slices/chat/reducers/topic.test.ts +0 -179
- package/src/store/session/slices/chat/reducers/topic.ts +0 -81
- package/src/store/session/slices/chat/selectors/chat.ts +0 -111
- package/src/store/session/slices/chat/selectors/index.ts +0 -26
- package/src/store/session/slices/chat/selectors/topic.ts +0 -30
- package/src/store/session/slices/chat/selectors/utils.test.ts +0 -385
- package/src/store/session/slices/chat/selectors/utils.ts +0 -91
- package/src/store/session/slices/chat/utils.ts +0 -13
- package/src/store/session/slices/session/reducers/session.test.ts +0 -577
- package/src/store/session/slices/session/reducers/session.ts +0 -182
- package/src/types/database/db.ts +0 -4
- /package/src/store/{session/slices/chat → chat}/reducers/files.test.ts +0 -0
- /package/src/store/{session/slices/chat → chat}/reducers/files.ts +0 -0
package/src/types/llm.ts
CHANGED
package/src/types/meta.ts
CHANGED
|
@@ -1,26 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const LobeMetaDataSchema = z.object({
|
|
2
4
|
/**
|
|
3
5
|
* 角色头像
|
|
4
|
-
* @description 可选参数,如果不传则使用默认头像
|
|
5
6
|
*/
|
|
6
|
-
avatar
|
|
7
|
+
avatar: z.string().optional(),
|
|
7
8
|
/**
|
|
8
9
|
* 背景色
|
|
9
|
-
* @description 可选参数,如果不传则使用默认背景色
|
|
10
10
|
*/
|
|
11
|
-
backgroundColor
|
|
12
|
-
description
|
|
13
|
-
tags
|
|
11
|
+
backgroundColor: z.string().optional(),
|
|
12
|
+
description: z.string().optional(),
|
|
13
|
+
tags: z.array(z.string()).optional(),
|
|
14
14
|
/**
|
|
15
15
|
* 名称
|
|
16
|
-
* @description 可选参数,如果不传则使用默认名称
|
|
17
16
|
*/
|
|
18
|
-
title
|
|
19
|
-
}
|
|
17
|
+
title: z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export type MetaData = z.infer<typeof LobeMetaDataSchema>;
|
|
20
21
|
|
|
21
22
|
export interface BaseDataModel {
|
|
22
|
-
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated
|
|
25
|
+
*/
|
|
26
|
+
createAt?: number;
|
|
27
|
+
|
|
28
|
+
createdAt: number;
|
|
29
|
+
|
|
23
30
|
id: string;
|
|
24
31
|
meta: MetaData;
|
|
25
|
-
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @deprecated
|
|
35
|
+
*/
|
|
36
|
+
updateAt?: number;
|
|
37
|
+
updatedAt: number;
|
|
26
38
|
}
|
package/src/types/openai/chat.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
import { LLMRoleType } from '@/types/llm';
|
|
3
3
|
|
|
4
|
+
import { OpenAIFunctionCall } from './functionCall';
|
|
5
|
+
|
|
4
6
|
interface UserMessageContentPartText {
|
|
5
7
|
text: string;
|
|
6
8
|
type: 'text';
|
|
@@ -31,6 +33,11 @@ export interface OpenAIChatMessage {
|
|
|
31
33
|
role: LLMRoleType;
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
export interface OpenAIChatStringMessage {
|
|
37
|
+
content: string;
|
|
38
|
+
role: LLMRoleType;
|
|
39
|
+
}
|
|
40
|
+
|
|
34
41
|
/**
|
|
35
42
|
* @title OpenAI Stream Payload
|
|
36
43
|
*/
|
|
@@ -40,6 +47,7 @@ export interface OpenAIChatStreamPayload {
|
|
|
40
47
|
* @default 0
|
|
41
48
|
*/
|
|
42
49
|
frequency_penalty?: number;
|
|
50
|
+
functions?: ChatCompletionFunctions[];
|
|
43
51
|
/**
|
|
44
52
|
* @title 生成文本的最大长度
|
|
45
53
|
*/
|
package/src/types/session.ts
CHANGED
|
@@ -1,106 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { LobeAgentConfig } from '@/types/agent';
|
|
2
|
+
|
|
3
3
|
import { BaseDataModel, MetaData } from './meta';
|
|
4
|
-
import { ChatTopicMap } from './topic';
|
|
5
4
|
|
|
6
5
|
export enum LobeSessionType {
|
|
7
|
-
/**
|
|
8
|
-
* 角色
|
|
9
|
-
*/
|
|
10
6
|
Agent = 'agent',
|
|
11
|
-
/**
|
|
12
|
-
* 群聊
|
|
13
|
-
*/
|
|
14
7
|
Group = 'group',
|
|
15
8
|
}
|
|
16
9
|
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 聊天记录
|
|
20
|
-
*/
|
|
21
|
-
chats: ChatMessageMap;
|
|
22
|
-
files?: string[];
|
|
23
|
-
/**
|
|
24
|
-
* 置顶
|
|
25
|
-
*/
|
|
26
|
-
pinned?: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* 主题
|
|
29
|
-
*/
|
|
30
|
-
topics?: ChatTopicMap;
|
|
31
|
-
/**
|
|
32
|
-
* 每个会话的类别
|
|
33
|
-
*/
|
|
34
|
-
type: LobeSessionType;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type TTSServer = 'openai' | 'edge' | 'microsoft';
|
|
10
|
+
export type SessionGroupKey = 'pinned' | 'default' | string;
|
|
38
11
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ttsService: TTSServer;
|
|
43
|
-
voice: {
|
|
44
|
-
edge?: string;
|
|
45
|
-
microsoft?: string;
|
|
46
|
-
openai: string;
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface LobeAgentConfig {
|
|
51
|
-
autoCreateTopicThreshold: number;
|
|
52
|
-
compressThreshold?: number;
|
|
53
|
-
displayMode?: 'chat' | 'docs';
|
|
54
|
-
enableAutoCreateTopic: boolean;
|
|
55
|
-
/**
|
|
56
|
-
* 历史消息长度压缩阈值
|
|
57
|
-
*/
|
|
58
|
-
enableCompressThreshold?: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* 开启历史记录条数
|
|
61
|
-
*/
|
|
62
|
-
enableHistoryCount?: boolean;
|
|
63
|
-
enableMaxTokens?: boolean;
|
|
64
|
-
/**
|
|
65
|
-
* 语言模型示例
|
|
66
|
-
*/
|
|
67
|
-
example?: LLMExample;
|
|
68
|
-
/**
|
|
69
|
-
* 历史消息条数
|
|
70
|
-
*/
|
|
71
|
-
historyCount?: number;
|
|
72
|
-
inputTemplate?: string;
|
|
73
|
-
/**
|
|
74
|
-
* 角色所使用的语言模型
|
|
75
|
-
* @default gpt-3.5-turbo
|
|
76
|
-
*/
|
|
77
|
-
model: LanguageModel | string;
|
|
78
|
-
/**
|
|
79
|
-
* 语言模型参数
|
|
80
|
-
*/
|
|
81
|
-
params: LLMParams;
|
|
82
|
-
/**
|
|
83
|
-
* 启用的插件
|
|
84
|
-
*/
|
|
85
|
-
plugins?: string[];
|
|
86
|
-
/**
|
|
87
|
-
* 系统角色
|
|
88
|
-
*/
|
|
89
|
-
systemRole: string;
|
|
90
|
-
/**
|
|
91
|
-
* 语音服务
|
|
92
|
-
*/
|
|
93
|
-
tts: LobeAgentTTSConfig;
|
|
12
|
+
export enum SessionGroupDefaultKeys {
|
|
13
|
+
Default = 'default',
|
|
14
|
+
Pinned = 'pinned',
|
|
94
15
|
}
|
|
95
16
|
|
|
96
17
|
/**
|
|
97
|
-
* Lobe Agent
|
|
18
|
+
* Lobe Agent
|
|
98
19
|
*/
|
|
99
|
-
export interface LobeAgentSession extends
|
|
100
|
-
/**
|
|
101
|
-
* 语言模型角色设定
|
|
102
|
-
*/
|
|
20
|
+
export interface LobeAgentSession extends BaseDataModel {
|
|
103
21
|
config: LobeAgentConfig;
|
|
22
|
+
group?: SessionGroupKey;
|
|
104
23
|
type: LobeSessionType.Agent;
|
|
105
24
|
}
|
|
106
25
|
|
|
@@ -111,8 +30,5 @@ export interface LobeAgentSettings {
|
|
|
111
30
|
config: LobeAgentConfig;
|
|
112
31
|
meta: MetaData;
|
|
113
32
|
}
|
|
114
|
-
export type LobeSessions = Record<string, LobeAgentSession>;
|
|
115
33
|
|
|
116
|
-
export type
|
|
117
|
-
| keyof LobeAgentConfig
|
|
118
|
-
| ['params', keyof LobeAgentConfig['params']];
|
|
34
|
+
export type LobeSessions = LobeAgentSession[];
|
package/src/types/settings.ts
CHANGED
|
@@ -45,6 +45,8 @@ export interface GlobalBaseSettings {
|
|
|
45
45
|
|
|
46
46
|
export type GlobalDefaultAgent = Pick<LobeAgentSession, 'config' | 'meta'>;
|
|
47
47
|
|
|
48
|
+
export type CustomModels = { displayName: string; name: string }[];
|
|
49
|
+
|
|
48
50
|
export interface OpenAIConfig {
|
|
49
51
|
OPENAI_API_KEY: string;
|
|
50
52
|
azureApiVersion?: string;
|
package/src/types/translate.ts
CHANGED
package/src/utils/config.ts
CHANGED
|
@@ -35,27 +35,21 @@ export const exportConfigFile = (config: object, fileName?: string) => {
|
|
|
35
35
|
a.remove();
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
export const importConfigFile = (
|
|
39
|
-
|
|
40
|
-
//读取完文件之后的回调函数
|
|
41
|
-
reader.onloadend = function (evt) {
|
|
42
|
-
const fileString = evt.target?.result;
|
|
43
|
-
const fileJson = fileString as string;
|
|
44
|
-
|
|
38
|
+
export const importConfigFile = (file: File, onConfigImport: (config: ConfigFile) => void) => {
|
|
39
|
+
file.text().then((text) => {
|
|
45
40
|
try {
|
|
46
|
-
const config = JSON.parse(
|
|
41
|
+
const config = JSON.parse(text);
|
|
47
42
|
const { state } = Migration.migrate(config);
|
|
48
43
|
|
|
49
44
|
onConfigImport({ ...config, state });
|
|
50
45
|
} catch (error) {
|
|
46
|
+
console.error(error);
|
|
51
47
|
notification.error({
|
|
52
48
|
description: `出错原因: ${(error as Error).message}`,
|
|
53
49
|
message: '导入失败',
|
|
54
50
|
});
|
|
55
51
|
}
|
|
56
|
-
};
|
|
57
|
-
//@ts-ignore file 类型不明确
|
|
58
|
-
reader.readAsText(info.file.originFileObj, 'utf8');
|
|
52
|
+
});
|
|
59
53
|
};
|
|
60
54
|
|
|
61
55
|
type CreateConfigFileState<T extends ExportType> = ConfigModelMap[T]['state'];
|
package/src/utils/fetch.ts
CHANGED
|
@@ -27,6 +27,7 @@ export const getMessageError = async (response: Response) => {
|
|
|
27
27
|
|
|
28
28
|
export interface FetchSSEOptions {
|
|
29
29
|
onErrorHandle?: (error: ChatMessageError) => void;
|
|
30
|
+
onFinish?: (text: string) => void;
|
|
30
31
|
onMessageHandle?: (text: string) => void;
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -51,7 +52,7 @@ export const fetchSSE = async (fetchFn: () => Promise<Response>, options: FetchS
|
|
|
51
52
|
const data = response.body;
|
|
52
53
|
|
|
53
54
|
if (!data) return;
|
|
54
|
-
|
|
55
|
+
let output = '';
|
|
55
56
|
const reader = data.getReader();
|
|
56
57
|
const decoder = new TextDecoder();
|
|
57
58
|
|
|
@@ -62,9 +63,12 @@ export const fetchSSE = async (fetchFn: () => Promise<Response>, options: FetchS
|
|
|
62
63
|
done = doneReading;
|
|
63
64
|
const chunkValue = decoder.decode(value, { stream: true });
|
|
64
65
|
|
|
66
|
+
output += chunkValue;
|
|
65
67
|
options.onMessageHandle?.(chunkValue);
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
options?.onFinish?.(output);
|
|
71
|
+
|
|
68
72
|
return returnRes;
|
|
69
73
|
};
|
|
70
74
|
|
|
@@ -74,6 +78,7 @@ interface FetchAITaskResultParams<T> {
|
|
|
74
78
|
* 错误处理函数
|
|
75
79
|
*/
|
|
76
80
|
onError?: (e: Error, rawError?: any) => void;
|
|
81
|
+
onFinish?: (text: string) => void;
|
|
77
82
|
/**
|
|
78
83
|
* 加载状态变化处理函数
|
|
79
84
|
* @param loading - 是否处于加载状态
|
|
@@ -84,7 +89,6 @@ interface FetchAITaskResultParams<T> {
|
|
|
84
89
|
* @param text - 消息内容
|
|
85
90
|
*/
|
|
86
91
|
onMessageHandle?: (text: string) => void;
|
|
87
|
-
|
|
88
92
|
/**
|
|
89
93
|
* 请求对象
|
|
90
94
|
*/
|
|
@@ -96,6 +100,7 @@ export const fetchAIFactory =
|
|
|
96
100
|
async ({
|
|
97
101
|
params,
|
|
98
102
|
onMessageHandle,
|
|
103
|
+
onFinish,
|
|
99
104
|
onError,
|
|
100
105
|
onLoadingChange,
|
|
101
106
|
abortController,
|
|
@@ -114,6 +119,7 @@ export const fetchAIFactory =
|
|
|
114
119
|
onErrorHandle: (error) => {
|
|
115
120
|
errorHandle(new Error(error.message), error);
|
|
116
121
|
},
|
|
122
|
+
onFinish,
|
|
117
123
|
onMessageHandle,
|
|
118
124
|
}).catch(errorHandle);
|
|
119
125
|
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { usePluginStore } from '@/store/plugin';
|
|
2
|
-
import { useSessionStore } from '@/store/session';
|
|
3
|
-
import { agentSelectors } from '@/store/session/selectors';
|
|
4
|
-
|
|
5
|
-
export const usePluginsInit = () => {
|
|
6
|
-
const [plugins] = useSessionStore((s) => [agentSelectors.currentAgentPlugins(s)]);
|
|
7
|
-
|
|
8
|
-
const useFetchPluginStore = usePluginStore((s) => s.useFetchPluginStore);
|
|
9
|
-
useFetchPluginStore();
|
|
10
|
-
|
|
11
|
-
const checkPluginsIsInstalled = usePluginStore((s) => s.useCheckPluginsIsInstalled);
|
|
12
|
-
|
|
13
|
-
checkPluginsIsInstalled(plugins);
|
|
14
|
-
};
|
package/src/helpers/export.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { settingsSelectors, useGlobalStore } from '@/store/global';
|
|
2
|
-
import { useSessionStore } from '@/store/session';
|
|
3
|
-
import { sessionSelectors } from '@/store/session/selectors';
|
|
4
|
-
import { createConfigFile, exportConfigFile } from '@/utils/config';
|
|
5
|
-
|
|
6
|
-
const getSessions = () => sessionSelectors.exportSessions(useSessionStore.getState());
|
|
7
|
-
const getSession = (id: string) => sessionSelectors.getSessionById(id)(useSessionStore.getState());
|
|
8
|
-
|
|
9
|
-
const getSettings = () => settingsSelectors.exportSettings(useGlobalStore.getState());
|
|
10
|
-
|
|
11
|
-
// ============= 导出所有角色 ============= //
|
|
12
|
-
|
|
13
|
-
export const exportAgents = () => {
|
|
14
|
-
const agents = sessionSelectors.exportAgents(useSessionStore.getState());
|
|
15
|
-
|
|
16
|
-
const config = createConfigFile('agents', agents);
|
|
17
|
-
|
|
18
|
-
exportConfigFile(config, 'agents');
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// ============= 导出单个角色 ============= //
|
|
22
|
-
|
|
23
|
-
const getAgent = (id: string) => sessionSelectors.getExportAgent(id)(useSessionStore.getState());
|
|
24
|
-
|
|
25
|
-
export const exportSingleAgent = (id: string) => {
|
|
26
|
-
const agent = getAgent(id);
|
|
27
|
-
if (!agent) return;
|
|
28
|
-
|
|
29
|
-
const config = createConfigFile('agents', { sessions: { [id]: agent } });
|
|
30
|
-
|
|
31
|
-
exportConfigFile(config, agent.meta?.title || 'agent');
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// ============= 导出所有会话 ============= //
|
|
35
|
-
|
|
36
|
-
export const exportSessions = () => {
|
|
37
|
-
const config = createConfigFile('sessions', getSessions());
|
|
38
|
-
|
|
39
|
-
exportConfigFile(config, 'sessions');
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// ============= 导出单个会话 ============= //
|
|
43
|
-
export const exportSingleSession = (id: string) => {
|
|
44
|
-
const session = getSession(id);
|
|
45
|
-
if (!session) return;
|
|
46
|
-
const sessions = { [id]: session };
|
|
47
|
-
|
|
48
|
-
const config = createConfigFile('singleSession', { sessions });
|
|
49
|
-
|
|
50
|
-
exportConfigFile(config, `${session.meta?.title}-session`);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// ============= 导出设置会话 ============= //
|
|
54
|
-
export const exportSettings = () => {
|
|
55
|
-
const settings = getSettings();
|
|
56
|
-
|
|
57
|
-
const config = createConfigFile('settings', { settings });
|
|
58
|
-
|
|
59
|
-
exportConfigFile(config, 'settings');
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const exportAll = () => {
|
|
63
|
-
const sessions = getSessions();
|
|
64
|
-
const settings = getSettings();
|
|
65
|
-
|
|
66
|
-
const config = createConfigFile('all', { ...sessions, settings });
|
|
67
|
-
|
|
68
|
-
exportConfigFile(config, 'config');
|
|
69
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { StateCreator } from 'zustand/vanilla';
|
|
2
|
-
|
|
3
|
-
import { SessionStore } from '@/store/session';
|
|
4
|
-
|
|
5
|
-
import { ChatMessageAction, chatMessage } from './message';
|
|
6
|
-
import { ChatPluginAction, chatPlugin } from './plugin';
|
|
7
|
-
import { ShareAction, chatShare } from './share';
|
|
8
|
-
import { ChatTopicAction, chatTopic } from './topic';
|
|
9
|
-
import { ChatTranslateAction, chatTranslate } from './translate';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 聊天操作
|
|
13
|
-
*/
|
|
14
|
-
export interface ChatAction
|
|
15
|
-
extends ChatTopicAction,
|
|
16
|
-
ChatMessageAction,
|
|
17
|
-
ShareAction,
|
|
18
|
-
ChatTranslateAction,
|
|
19
|
-
ChatPluginAction {}
|
|
20
|
-
|
|
21
|
-
export const createChatSlice: StateCreator<
|
|
22
|
-
SessionStore,
|
|
23
|
-
[['zustand/devtools', never]],
|
|
24
|
-
[],
|
|
25
|
-
ChatAction
|
|
26
|
-
> = (...params) => ({
|
|
27
|
-
...chatTopic(...params),
|
|
28
|
-
...chatMessage(...params),
|
|
29
|
-
...chatShare(...params),
|
|
30
|
-
...chatTranslate(...params),
|
|
31
|
-
...chatPlugin(...params),
|
|
32
|
-
});
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { StateCreator } from 'zustand/vanilla';
|
|
2
|
-
|
|
3
|
-
import { chainSummaryTitle } from '@/chains/summaryTitle';
|
|
4
|
-
import { LOADING_FLAT } from '@/const/message';
|
|
5
|
-
import { chatService } from '@/services/chat';
|
|
6
|
-
import { SessionStore } from '@/store/session';
|
|
7
|
-
import { setNamespace } from '@/utils/storeDebug';
|
|
8
|
-
import { nanoid } from '@/utils/uuid';
|
|
9
|
-
|
|
10
|
-
import { sessionSelectors } from '../../session/selectors';
|
|
11
|
-
import { ChatTopicDispatch, topicReducer } from '../reducers/topic';
|
|
12
|
-
import { chatSelectors, topicSelectors } from '../selectors';
|
|
13
|
-
|
|
14
|
-
const t = setNamespace('chat/topic');
|
|
15
|
-
export interface ChatTopicAction {
|
|
16
|
-
/**
|
|
17
|
-
* 分发主题
|
|
18
|
-
* @param payload - 要分发的主题
|
|
19
|
-
*/
|
|
20
|
-
dispatchTopic: (payload: ChatTopicDispatch) => void;
|
|
21
|
-
openNewTopicOrSaveTopic: () => void;
|
|
22
|
-
/**
|
|
23
|
-
* 移出所有话题
|
|
24
|
-
*/
|
|
25
|
-
removeAllTopic: () => void;
|
|
26
|
-
/**
|
|
27
|
-
* 移除话题
|
|
28
|
-
* @param id
|
|
29
|
-
*/
|
|
30
|
-
removeTopic: (id: string) => void;
|
|
31
|
-
/**
|
|
32
|
-
* 移出所有未标记的话题
|
|
33
|
-
*/
|
|
34
|
-
removeUnstarredTopic: () => void;
|
|
35
|
-
/**
|
|
36
|
-
* 将当前消息保存为主题
|
|
37
|
-
*/
|
|
38
|
-
saveToTopic: () => Promise<string | undefined>;
|
|
39
|
-
/**
|
|
40
|
-
* 切换主题
|
|
41
|
-
* @param id - 要切换的主题的 ID
|
|
42
|
-
*/
|
|
43
|
-
toggleTopic: (id?: string) => void;
|
|
44
|
-
/**
|
|
45
|
-
* 更新主题加载状态
|
|
46
|
-
* @param id - 要更新的主题的 ID
|
|
47
|
-
*/
|
|
48
|
-
updateTopicLoading: (id?: string) => void;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const chatTopic: StateCreator<
|
|
52
|
-
SessionStore,
|
|
53
|
-
[['zustand/devtools', never]],
|
|
54
|
-
[],
|
|
55
|
-
ChatTopicAction
|
|
56
|
-
> = (set, get) => ({
|
|
57
|
-
dispatchTopic: (payload) => {
|
|
58
|
-
const { activeId } = get();
|
|
59
|
-
const session = sessionSelectors.currentSession(get());
|
|
60
|
-
if (!activeId || !session) return;
|
|
61
|
-
|
|
62
|
-
const topics = topicReducer(session.topics || {}, payload);
|
|
63
|
-
|
|
64
|
-
get().dispatchSession({ id: activeId, topics, type: 'updateSessionTopic' });
|
|
65
|
-
},
|
|
66
|
-
openNewTopicOrSaveTopic: () => {
|
|
67
|
-
const { toggleTopic, saveToTopic, activeTopicId } = get();
|
|
68
|
-
const hasTopic = !!activeTopicId;
|
|
69
|
-
|
|
70
|
-
if (hasTopic) toggleTopic();
|
|
71
|
-
else {
|
|
72
|
-
saveToTopic();
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
removeAllTopic: () => {
|
|
76
|
-
const { removeTopic, toggleTopic } = get();
|
|
77
|
-
const topics = topicSelectors.currentTopics(get());
|
|
78
|
-
|
|
79
|
-
for (const { id } of topics) {
|
|
80
|
-
removeTopic(id);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// 切换到默认 topic
|
|
84
|
-
toggleTopic();
|
|
85
|
-
},
|
|
86
|
-
removeTopic: (id) => {
|
|
87
|
-
const { dispatchTopic, dispatchMessage, toggleTopic } = get();
|
|
88
|
-
|
|
89
|
-
// 移除关联的 message
|
|
90
|
-
const messages = topicSelectors.getTopicMessages(id)(get());
|
|
91
|
-
for (const m of messages) {
|
|
92
|
-
dispatchMessage({ id: m.id, type: 'deleteMessage' });
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// 最后移除 topic
|
|
96
|
-
dispatchTopic({ id, type: 'deleteChatTopic' });
|
|
97
|
-
|
|
98
|
-
// 切换到默认 topic
|
|
99
|
-
toggleTopic();
|
|
100
|
-
},
|
|
101
|
-
removeUnstarredTopic: () => {
|
|
102
|
-
const { removeTopic, toggleTopic } = get();
|
|
103
|
-
const topics = topicSelectors.currentTopics(get());
|
|
104
|
-
|
|
105
|
-
for (const { id, favorite } of topics) {
|
|
106
|
-
if (!favorite) removeTopic(id);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// 切换到默认 topic
|
|
110
|
-
toggleTopic();
|
|
111
|
-
},
|
|
112
|
-
saveToTopic: async () => {
|
|
113
|
-
const session = sessionSelectors.currentSession(get());
|
|
114
|
-
if (!session) return;
|
|
115
|
-
|
|
116
|
-
const { dispatchTopic, dispatchMessage, updateTopicLoading } = get();
|
|
117
|
-
// get current messages
|
|
118
|
-
const messages = chatSelectors.currentChats(get());
|
|
119
|
-
|
|
120
|
-
// if there is no message, stop saving
|
|
121
|
-
if (messages.length === 0) return;
|
|
122
|
-
|
|
123
|
-
const topicId = nanoid();
|
|
124
|
-
|
|
125
|
-
const defaultTitle = '默认话题';
|
|
126
|
-
const newTopic = {
|
|
127
|
-
createAt: Date.now(),
|
|
128
|
-
id: topicId,
|
|
129
|
-
title: defaultTitle,
|
|
130
|
-
updateAt: Date.now(),
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
dispatchTopic({
|
|
134
|
-
topic: newTopic,
|
|
135
|
-
type: 'addChatTopic',
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
// 为所有 message 添加 topicId
|
|
139
|
-
for (const m of messages) {
|
|
140
|
-
dispatchMessage({ id: m.id, key: 'topicId', type: 'updateMessage', value: topicId });
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
dispatchTopic({ id: topicId, key: 'title', type: 'updateChatTopic', value: LOADING_FLAT });
|
|
144
|
-
|
|
145
|
-
let output = '';
|
|
146
|
-
|
|
147
|
-
// 自动总结话题标题
|
|
148
|
-
chatService.fetchPresetTaskResult({
|
|
149
|
-
onError: () => {
|
|
150
|
-
dispatchTopic({ id: topicId, key: 'title', type: 'updateChatTopic', value: defaultTitle });
|
|
151
|
-
},
|
|
152
|
-
onLoadingChange: (loading) => {
|
|
153
|
-
updateTopicLoading(loading ? topicId : undefined);
|
|
154
|
-
},
|
|
155
|
-
onMessageHandle: (x) => {
|
|
156
|
-
output += x;
|
|
157
|
-
dispatchTopic({ id: topicId, key: 'title', type: 'updateChatTopic', value: output });
|
|
158
|
-
},
|
|
159
|
-
params: await chainSummaryTitle(messages),
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
return topicId;
|
|
163
|
-
},
|
|
164
|
-
toggleTopic: (id) => {
|
|
165
|
-
set({ activeTopicId: id }, false, t('toggleTopic'));
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
updateTopicLoading: (id) => {
|
|
169
|
-
set({ topicLoadingId: id }, false, t('updateTopicLoading'));
|
|
170
|
-
},
|
|
171
|
-
});
|