From bef89265326c5e5bdb3fd7e16df03d1fd1d82f28 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Sun, 26 May 2024 23:33:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20connection=20checke?= =?UTF-8?q?r=20(#2672)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 fix: fix check error * 💄 style: improve bubble loading * ♻️ refactor: add store devtools url control * 🚚 refactor: move settings type folder * ♻️ refactor: rename type * 🐛 fix: fix check error --- src/app/(main)/settings/llm/Bedrock/index.tsx | 2 +- .../settings/llm/components/Checker.tsx | 16 ++-- .../llm/components/ProviderConfig/index.tsx | 2 +- .../ProviderModelList/CustomModelOption.tsx | 2 +- .../ProviderModelList/ModelFetcher.tsx | 2 +- .../components/ProviderModelList/Option.tsx | 2 +- .../components/ProviderModelList/index.tsx | 2 +- src/const/settings/agent.ts | 4 +- src/const/settings/common.ts | 4 +- src/const/settings/index.ts | 4 +- src/const/settings/llm.ts | 4 +- src/const/settings/sync.ts | 4 +- src/const/settings/systemAgent.ts | 4 +- src/const/settings/tts.ts | 4 +- .../migrations/migrateSettingsToUser/type.ts | 2 +- .../client/models/__tests__/user.test.ts | 2 +- src/database/client/models/user.ts | 4 +- .../Error/APIKeyForm/ProviderApiKeyForm.tsx | 2 +- .../Conversation/Error/APIKeyForm/index.tsx | 2 +- .../components/BubblesLoading.tsx | 90 ++++++++++--------- src/services/__tests__/share.test.ts | 4 +- src/services/_auth.test.ts | 4 +- src/services/config.ts | 4 +- src/services/share.ts | 6 +- src/services/user/client.test.ts | 6 +- src/services/user/client.ts | 6 +- src/services/user/type.ts | 4 +- src/store/agent/store.ts | 13 +-- src/store/chat/store.ts | 12 +-- src/store/file/store.ts | 12 +-- src/store/global/store.ts | 13 ++- src/store/market/store.ts | 13 ++- src/store/middleware/createDevtools.ts | 23 +++++ src/store/serverConfig/store.ts | 16 ++-- src/store/session/store.ts | 4 +- src/store/tool/store.ts | 13 +-- src/store/user/slices/common/action.ts | 4 +- .../user/slices/modelList/action.test.ts | 4 +- src/store/user/slices/modelList/action.ts | 4 +- .../slices/modelList/selectors/modelConfig.ts | 2 +- .../modelList/selectors/modelProvider.ts | 2 +- src/store/user/slices/settings/action.test.ts | 8 +- src/store/user/slices/settings/action.ts | 6 +- .../user/slices/settings/initialState.ts | 6 +- .../slices/settings/selectors/settings.ts | 13 +-- src/store/user/store.ts | 13 ++- src/types/exportConfig.ts | 4 +- src/types/serverConfig.ts | 4 +- src/types/settings/index.ts | 33 ------- src/types/settings/modelProvider.ts | 62 ------------- src/types/user/index.ts | 4 +- .../base.ts => user/settings/general.ts} | 5 +- src/types/user/settings/index.ts | 54 +++++++++++ src/types/user/settings/modelProvider.ts | 70 +++++++++++++++ src/types/{ => user}/settings/sync.ts | 2 +- src/types/{ => user}/settings/systemAgent.ts | 2 +- src/types/user/settings/tool.ts | 5 ++ src/types/{ => user}/settings/tts.ts | 2 +- 58 files changed, 331 insertions(+), 289 deletions(-) create mode 100644 src/store/middleware/createDevtools.ts delete mode 100644 src/types/settings/index.ts delete mode 100644 src/types/settings/modelProvider.ts rename src/types/{settings/base.ts => user/settings/general.ts} (82%) create mode 100644 src/types/user/settings/index.ts create mode 100644 src/types/user/settings/modelProvider.ts rename src/types/{ => user}/settings/sync.ts (82%) rename src/types/{ => user}/settings/systemAgent.ts (74%) create mode 100644 src/types/user/settings/tool.ts rename src/types/{ => user}/settings/tts.ts (83%) diff --git a/src/app/(main)/settings/llm/Bedrock/index.tsx b/src/app/(main)/settings/llm/Bedrock/index.tsx index 91295e143a94..0d0bcb5d9476 100644 --- a/src/app/(main)/settings/llm/Bedrock/index.tsx +++ b/src/app/(main)/settings/llm/Bedrock/index.tsx @@ -7,7 +7,7 @@ import { useTranslation } from 'react-i18next'; import { Flexbox } from 'react-layout-kit'; import { ModelProvider } from '@/libs/agent-runtime'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import ProviderConfig from '../components/ProviderConfig'; import { LLMProviderConfigKey } from '../const'; diff --git a/src/app/(main)/settings/llm/components/Checker.tsx b/src/app/(main)/settings/llm/components/Checker.tsx index 599b3a7a9cc7..ad8c205135cb 100644 --- a/src/app/(main)/settings/llm/components/Checker.tsx +++ b/src/app/(main)/settings/llm/components/Checker.tsx @@ -50,10 +50,19 @@ const Checker = memo(({ model, provider }) => { const [error, setError] = useState(); const checkConnection = async () => { - const data = await chatService.fetchPresetTaskResult({ + let isError = false; + + await chatService.fetchPresetTaskResult({ onError: (_, rawError) => { setError(rawError); setPass(false); + isError = true; + }, + onFinish: async () => { + if (!isError) { + setError(undefined); + setPass(true); + } }, onLoadingChange: (loading) => { setLoading(loading); @@ -74,11 +83,6 @@ const Checker = memo(({ model, provider }) => { traceName: TraceNameMap.ConnectivityChecker, }, }); - - if (data) { - setError(undefined); - setPass(true); - } }; const isMobile = useIsMobile(); diff --git a/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx b/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx index 1f3d9f0f7525..42887829073b 100644 --- a/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx +++ b/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx @@ -18,7 +18,7 @@ import { import { FORM_STYLE } from '@/const/layoutTokens'; import { useUserStore } from '@/store/user'; import { modelConfigSelectors } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import Checker from '../Checker'; import ProviderModelListSelect from '../ProviderModelList'; diff --git a/src/app/(main)/settings/llm/components/ProviderModelList/CustomModelOption.tsx b/src/app/(main)/settings/llm/components/ProviderModelList/CustomModelOption.tsx index 25d70fd06975..401f0bca086e 100644 --- a/src/app/(main)/settings/llm/components/ProviderModelList/CustomModelOption.tsx +++ b/src/app/(main)/settings/llm/components/ProviderModelList/CustomModelOption.tsx @@ -10,7 +10,7 @@ import ModelIcon from '@/components/ModelIcon'; import { ModelInfoTags } from '@/components/ModelSelect'; import { useUserStore } from '@/store/user'; import { modelConfigSelectors } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; interface CustomModelOptionProps { id: string; diff --git a/src/app/(main)/settings/llm/components/ProviderModelList/ModelFetcher.tsx b/src/app/(main)/settings/llm/components/ProviderModelList/ModelFetcher.tsx index 4692b69465d6..af0b90e2e6ba 100644 --- a/src/app/(main)/settings/llm/components/ProviderModelList/ModelFetcher.tsx +++ b/src/app/(main)/settings/llm/components/ProviderModelList/ModelFetcher.tsx @@ -13,7 +13,7 @@ import { modelProviderSelectors, settingsSelectors, } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; const useStyles = createStyles(({ css, token }) => ({ hover: css` diff --git a/src/app/(main)/settings/llm/components/ProviderModelList/Option.tsx b/src/app/(main)/settings/llm/components/ProviderModelList/Option.tsx index f47f23404a91..e9fc61ed87ff 100644 --- a/src/app/(main)/settings/llm/components/ProviderModelList/Option.tsx +++ b/src/app/(main)/settings/llm/components/ProviderModelList/Option.tsx @@ -11,7 +11,7 @@ import ModelIcon from '@/components/ModelIcon'; import { ModelInfoTags } from '@/components/ModelSelect'; import { useUserStore } from '@/store/user'; import { modelProviderSelectors } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import CustomModelOption from './CustomModelOption'; diff --git a/src/app/(main)/settings/llm/components/ProviderModelList/index.tsx b/src/app/(main)/settings/llm/components/ProviderModelList/index.tsx index fdebb77f4867..51d401bac203 100644 --- a/src/app/(main)/settings/llm/components/ProviderModelList/index.tsx +++ b/src/app/(main)/settings/llm/components/ProviderModelList/index.tsx @@ -9,7 +9,7 @@ import { Flexbox } from 'react-layout-kit'; import { useUserStore } from '@/store/user'; import { modelProviderSelectors } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import ModelConfigModal from './ModelConfigModal'; import ModelFetcher from './ModelFetcher'; diff --git a/src/const/settings/agent.ts b/src/const/settings/agent.ts index a70c8228ee1f..fdbf1d375642 100644 --- a/src/const/settings/agent.ts +++ b/src/const/settings/agent.ts @@ -1,7 +1,7 @@ import { DEFAULT_AGENT_META } from '@/const/meta'; import { ModelProvider } from '@/libs/agent-runtime'; import { LobeAgentChatConfig, LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent'; -import { GlobalDefaultAgent } from '@/types/settings'; +import { UserDefaultAgent } from '@/types/user/settings'; export const DEFAUTT_AGENT_TTS_CONFIG: LobeAgentTTSConfig = { showAllLocaleVoice: false, @@ -34,7 +34,7 @@ export const DEFAULT_AGENT_CONFIG: LobeAgentConfig = { tts: DEFAUTT_AGENT_TTS_CONFIG, }; -export const DEFAULT_AGENT: GlobalDefaultAgent = { +export const DEFAULT_AGENT: UserDefaultAgent = { config: DEFAULT_AGENT_CONFIG, meta: DEFAULT_AGENT_META, }; diff --git a/src/const/settings/common.ts b/src/const/settings/common.ts index e0f0ae626869..3367cac8959b 100644 --- a/src/const/settings/common.ts +++ b/src/const/settings/common.ts @@ -1,6 +1,6 @@ -import { GlobalBaseSettings } from '@/types/settings'; +import { UserGeneralSettings } from '@/types/user/settings'; -export const DEFAULT_COMMON_SETTINGS: GlobalBaseSettings = { +export const DEFAULT_COMMON_SETTINGS: UserGeneralSettings = { fontSize: 14, language: 'auto', password: '', diff --git a/src/const/settings/index.ts b/src/const/settings/index.ts index aa4e308471cd..d5a2a54e5d24 100644 --- a/src/const/settings/index.ts +++ b/src/const/settings/index.ts @@ -1,4 +1,4 @@ -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; import { DEFAULT_AGENT } from './agent'; import { DEFAULT_COMMON_SETTINGS } from './common'; @@ -16,7 +16,7 @@ export * from './systemAgent'; export * from './tool'; export * from './tts'; -export const DEFAULT_SETTINGS: GlobalSettings = { +export const DEFAULT_SETTINGS: UserSettings = { defaultAgent: DEFAULT_AGENT, languageModel: DEFAULT_LLM_CONFIG, sync: DEFAULT_SYNC_CONFIG, diff --git a/src/const/settings/llm.ts b/src/const/settings/llm.ts index a3bc76bc42d6..389bb0865125 100644 --- a/src/const/settings/llm.ts +++ b/src/const/settings/llm.ts @@ -17,9 +17,9 @@ import { filterEnabledModels, } from '@/config/modelProviders'; import { ModelProvider } from '@/libs/agent-runtime'; -import { GlobalLLMConfig } from '@/types/settings'; +import { UserModelProviderConfig } from '@/types/user/settings'; -export const DEFAULT_LLM_CONFIG: GlobalLLMConfig = { +export const DEFAULT_LLM_CONFIG: UserModelProviderConfig = { anthropic: { apiKey: '', enabled: false, diff --git a/src/const/settings/sync.ts b/src/const/settings/sync.ts index 794ee16bc6da..1de34db28ca3 100644 --- a/src/const/settings/sync.ts +++ b/src/const/settings/sync.ts @@ -1,5 +1,5 @@ -import { GlobalSyncSettings } from '@/types/settings'; +import { UserSyncSettings } from '@/types/user/settings'; -export const DEFAULT_SYNC_CONFIG: GlobalSyncSettings = { +export const DEFAULT_SYNC_CONFIG: UserSyncSettings = { webrtc: { enabled: false }, }; diff --git a/src/const/settings/systemAgent.ts b/src/const/settings/systemAgent.ts index 0743694f450f..9dac22a29613 100644 --- a/src/const/settings/systemAgent.ts +++ b/src/const/settings/systemAgent.ts @@ -1,4 +1,4 @@ -import { GlobalSystemAgentConfig, GlobalTranslationConfig } from '@/types/settings'; +import { UserSystemAgentConfig, GlobalTranslationConfig } from '@/types/user/settings'; import { DEFAULT_MODEL, DEFAULT_PROVIDER } from './llm'; @@ -7,6 +7,6 @@ export const DEFAULT_TRANSLATION_CONFIG: GlobalTranslationConfig = { provider: DEFAULT_PROVIDER, }; -export const DEFAULT_SYSTEM_AGENT_CONFIG: GlobalSystemAgentConfig = { +export const DEFAULT_SYSTEM_AGENT_CONFIG: UserSystemAgentConfig = { translation: DEFAULT_TRANSLATION_CONFIG, }; diff --git a/src/const/settings/tts.ts b/src/const/settings/tts.ts index 1c92912993f9..c9b0e45a7e8e 100644 --- a/src/const/settings/tts.ts +++ b/src/const/settings/tts.ts @@ -1,6 +1,6 @@ -import { GlobalTTSConfig } from '@/types/settings'; +import { UserTTSConfig } from '@/types/user/settings'; -export const DEFAULT_TTS_CONFIG: GlobalTTSConfig = { +export const DEFAULT_TTS_CONFIG: UserTTSConfig = { openAI: { sttModel: 'whisper-1', ttsModel: 'tts-1', diff --git a/src/database/client/core/migrations/migrateSettingsToUser/type.ts b/src/database/client/core/migrations/migrateSettingsToUser/type.ts index 31f5157f1a10..065dfbf58936 100644 --- a/src/database/client/core/migrations/migrateSettingsToUser/type.ts +++ b/src/database/client/core/migrations/migrateSettingsToUser/type.ts @@ -3,7 +3,7 @@ import type { ThemeMode } from 'antd-style'; import { LobeAgentTTSConfig } from '@/types/agent'; import { FewShots, LLMParams } from '@/types/llm'; import { MetaData } from '@/types/meta'; -import { STTServer } from '@/types/settings'; +import { STTServer } from '@/types/user/settings'; interface V4LobeAgentConfig { autoCreateTopicThreshold: number; diff --git a/src/database/client/models/__tests__/user.test.ts b/src/database/client/models/__tests__/user.test.ts index 202c3125b8fa..407c99b5bd58 100644 --- a/src/database/client/models/__tests__/user.test.ts +++ b/src/database/client/models/__tests__/user.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; import { UserModel } from '../user'; diff --git a/src/database/client/models/user.ts b/src/database/client/models/user.ts index 38283f133944..a80a81123c6e 100644 --- a/src/database/client/models/user.ts +++ b/src/database/client/models/user.ts @@ -2,7 +2,7 @@ import { DeepPartial } from 'utility-types'; import { BaseModel } from '@/database/client/core'; import { LobeAgentConfig } from '@/types/agent'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; import { uuid } from '@/utils/uuid'; import { DB_User, DB_UserSchema } from '../schemas/user'; @@ -42,7 +42,7 @@ class _UserModel extends BaseModel { // **************** Update *************** // - async updateSettings(settings: DeepPartial) { + async updateSettings(settings: DeepPartial) { const user = await this.getUser(); return this.update(user.id, { settings: settings as any }); diff --git a/src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx b/src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx index 41b491e8b6e6..9ecfaa791c66 100644 --- a/src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx +++ b/src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx @@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next'; import { useUserStore } from '@/store/user'; import { settingsSelectors } from '@/store/user/selectors'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import { FormAction } from '../style'; diff --git a/src/features/Conversation/Error/APIKeyForm/index.tsx b/src/features/Conversation/Error/APIKeyForm/index.tsx index c6fac5869f7b..d18501b77f1f 100644 --- a/src/features/Conversation/Error/APIKeyForm/index.tsx +++ b/src/features/Conversation/Error/APIKeyForm/index.tsx @@ -5,7 +5,7 @@ import { Center, Flexbox } from 'react-layout-kit'; import { ModelProvider } from '@/libs/agent-runtime'; import { useChatStore } from '@/store/chat'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import BedrockForm from './Bedrock'; import ProviderApiKeyForm from './ProviderApiKeyForm'; diff --git a/src/features/Conversation/components/BubblesLoading.tsx b/src/features/Conversation/components/BubblesLoading.tsx index adbc08b25533..62ff47b8eea2 100644 --- a/src/features/Conversation/components/BubblesLoading.tsx +++ b/src/features/Conversation/components/BubblesLoading.tsx @@ -1,52 +1,60 @@ -import { useTheme } from 'antd-style'; +import { css, cx, useTheme } from 'antd-style'; +import { Center } from 'react-layout-kit'; + +const container = css` + circle { + animation: bubble 1.5s cubic-bezier(0.05, 0.2, 0.35, 1) infinite; + } + + circle:nth-child(2) { + animation-delay: 0.3s; + } + + circle:nth-child(3) { + animation-delay: 0.6s; + } + + @keyframes bubble { + 0% { + opacity: 1; + + /* transform: translateY(0); */ + } + + 25% { + opacity: 0.5; + + /* transform: translateY(-4px); */ + } + + 75% { + opacity: 0.25; + + /* transform: translateY(4px); */ + } + + to { + opacity: 1; + + /* transform: translateY(0); */ + } + } +`; const Svg = () => ( - - - - - - - - - - + + + + ); const BubblesLoading = () => { - const { colorTextTertiary } = useTheme(); + const theme = useTheme(); return ( -
+
-
+ ); }; diff --git a/src/services/__tests__/share.test.ts b/src/services/__tests__/share.test.ts index de2192ab41f6..cc882564d929 100644 --- a/src/services/__tests__/share.test.ts +++ b/src/services/__tests__/share.test.ts @@ -2,8 +2,8 @@ import { DeepPartial } from 'utility-types'; import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { LOBE_URL_IMPORT_NAME } from '@/const/url'; -import { GlobalSettings } from '@/types/settings'; import { ShareGPTConversation } from '@/types/share'; +import { UserSettings } from '@/types/user/settings'; import { parseMarkdown } from '@/utils/parseMarkdown'; import { SHARE_GPT_URL, shareService } from '../share'; @@ -91,7 +91,7 @@ describe('ShareGPTService', () => { describe('ShareViaUrl', () => { describe('createShareSettingsUrl', () => { it('should create a share settings URL with the provided settings', () => { - const settings: DeepPartial = { + const settings: DeepPartial = { languageModel: { openai: { apiKey: 'user-key', diff --git a/src/services/_auth.test.ts b/src/services/_auth.test.ts index 0a188b03bafe..885dc6ebbfb3 100644 --- a/src/services/_auth.test.ts +++ b/src/services/_auth.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it, vi } from 'vitest'; import { ModelProvider } from '@/libs/agent-runtime'; import { useUserStore } from '@/store/user'; -import { GlobalLLMConfig, GlobalLLMProviderKey } from '@/types/settings'; +import { UserModelProviderConfig, GlobalLLMProviderKey } from '@/types/user/settings'; import { getProviderAuthPayload } from './_auth'; @@ -21,7 +21,7 @@ vi.mock('zustand/traditional'); const setModelProviderConfig = ( provider: T, - config: Partial, + config: Partial, ) => { useUserStore.setState({ settings: { languageModel: { [provider]: config } }, diff --git a/src/services/config.ts b/src/services/config.ts index 89789c8f2440..aa6f16c14991 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -8,8 +8,8 @@ import { settingsSelectors } from '@/store/user/selectors'; import { ConfigFile } from '@/types/exportConfig'; import { ChatMessage } from '@/types/message'; import { LobeSessions, SessionGroupItem } from '@/types/session'; -import { GlobalSettings } from '@/types/settings'; import { ChatTopic } from '@/types/topic'; +import { UserSettings } from '@/types/user/settings'; import { createConfigFile, exportConfigFile } from '@/utils/config'; export interface ImportResult { @@ -35,7 +35,7 @@ class ConfigService { importMessages = async (messages: ChatMessage[]) => { return messageService.batchCreateMessages(messages); }; - importSettings = async (settings: GlobalSettings) => { + importSettings = async (settings: UserSettings) => { useUserStore.getState().importAppSettings(settings); }; importTopics = async (topics: ChatTopic[]) => { diff --git a/src/services/share.ts b/src/services/share.ts index 25d8ba213bad..cb7c97e042de 100644 --- a/src/services/share.ts +++ b/src/services/share.ts @@ -1,8 +1,8 @@ import { DeepPartial } from 'utility-types'; import { LOBE_URL_IMPORT_NAME } from '@/const/url'; -import { GlobalSettings } from '@/types/settings'; import { ShareGPTConversation } from '@/types/share'; +import { UserSettings } from '@/types/user/settings'; import { withBasePath } from '@/utils/basePath'; import { parseMarkdown } from '@/utils/parseMarkdown'; @@ -40,7 +40,7 @@ class ShareService { * @param settings - The settings object to be encoded in the URL. * @returns The share settings URL. */ - public createShareSettingsUrl(settings: DeepPartial) { + public createShareSettingsUrl(settings: DeepPartial) { return withBasePath(`/?${LOBE_URL_IMPORT_NAME}=${encodeURI(JSON.stringify(settings))}`); } @@ -51,7 +51,7 @@ class ShareService { */ public decodeShareSettings(settings: string) { try { - return { data: JSON.parse(settings) as DeepPartial }; + return { data: JSON.parse(settings) as DeepPartial }; } catch (e) { return { message: JSON.stringify(e) }; } diff --git a/src/services/user/client.test.ts b/src/services/user/client.test.ts index eb455cbdbfd9..22e3885571ad 100644 --- a/src/services/user/client.test.ts +++ b/src/services/user/client.test.ts @@ -2,8 +2,8 @@ import { DeepPartial } from 'utility-types'; import { Mock, beforeEach, describe, expect, it, vi } from 'vitest'; import { UserModel } from '@/database/client/models/user'; -import { GlobalSettings } from '@/types/settings'; import { UserPreference } from '@/types/user'; +import { UserSettings } from '@/types/user/settings'; import { AsyncLocalStorage } from '@/utils/localStorage'; import { ClientService } from './client'; @@ -19,7 +19,7 @@ vi.mock('@/database/client/models/user', () => ({ const mockUser = { avatar: 'avatar.png', - settings: { themeMode: 'light' } as unknown as GlobalSettings, + settings: { themeMode: 'light' } as unknown as UserSettings, uuid: 'user-id', }; @@ -58,7 +58,7 @@ describe('ClientService', () => { }); it('should update user settings correctly', async () => { - const settingsPatch: DeepPartial = { themeMode: 'dark' }; + const settingsPatch: DeepPartial = { themeMode: 'dark' }; (UserModel.updateSettings as Mock).mockResolvedValue(undefined); await clientService.updateUserSettings(settingsPatch); diff --git a/src/services/user/client.ts b/src/services/user/client.ts index 36e99870d6fa..4fbcc3b02b0e 100644 --- a/src/services/user/client.ts +++ b/src/services/user/client.ts @@ -3,8 +3,8 @@ import { DeepPartial } from 'utility-types'; import { MessageModel } from '@/database/client/models/message'; import { SessionModel } from '@/database/client/models/session'; import { UserModel } from '@/database/client/models/user'; -import { GlobalSettings } from '@/types/settings'; import { UserInitializationState, UserPreference } from '@/types/user'; +import { UserSettings } from '@/types/user/settings'; import { AsyncLocalStorage } from '@/utils/localStorage'; import { IUserService } from './type'; @@ -28,12 +28,12 @@ export class ClientService implements IUserService { hasConversation: messageCount > 0 || sessionCount > 0, isOnboard: true, preference: await this.preferenceStorage.getFromLocalStorage(), - settings: user.settings as GlobalSettings, + settings: user.settings as UserSettings, userId: user.uuid, }; } - updateUserSettings = async (patch: DeepPartial) => { + updateUserSettings = async (patch: DeepPartial) => { return UserModel.updateSettings(patch); }; diff --git a/src/services/user/type.ts b/src/services/user/type.ts index dfad022ea919..3b8bf41b2cb5 100644 --- a/src/services/user/type.ts +++ b/src/services/user/type.ts @@ -1,11 +1,11 @@ import { DeepPartial } from 'utility-types'; -import { GlobalSettings } from '@/types/settings'; import { UserInitializationState, UserPreference } from '@/types/user'; +import { UserSettings } from '@/types/user/settings'; export interface IUserService { getUserState: () => Promise; resetUserSettings: () => Promise; updatePreference: (preference: UserPreference) => Promise; - updateUserSettings: (patch: DeepPartial) => Promise; + updateUserSettings: (patch: DeepPartial) => Promise; } diff --git a/src/store/agent/store.ts b/src/store/agent/store.ts index e416cb267e0c..f6de3aaa6c21 100644 --- a/src/store/agent/store.ts +++ b/src/store/agent/store.ts @@ -1,10 +1,8 @@ -import { devtools } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { SessionStoreState, initialState } from './initialState'; import { AgentChatAction, createChatSlice } from './slices/chat/action'; @@ -19,9 +17,6 @@ const createStore: StateCreator = (.. // =============== implement useStore ============ // -export const useAgentStore = createWithEqualityFn()( - devtools(createStore, { - name: 'LobeChat_Agent' + (isDev ? '_DEV' : ''), - }), - shallow, -); +const devtools = createDevtools('agent'); + +export const useAgentStore = createWithEqualityFn()(devtools(createStore), shallow); diff --git a/src/store/chat/store.ts b/src/store/chat/store.ts index 552be7546b2b..3232304d02e9 100644 --- a/src/store/chat/store.ts +++ b/src/store/chat/store.ts @@ -1,10 +1,9 @@ -import { devtools, subscribeWithSelector } from 'zustand/middleware'; +import { subscribeWithSelector } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { ChatStoreState, initialState } from './initialState'; import { ChatBuiltinToolAction, chatToolSlice } from './slices/builtinTool/action'; import { ChatEnhanceAction, chatEnhance } from './slices/enchance/action'; @@ -37,12 +36,9 @@ const createStore: StateCreator = (... }); // =============== 实装 useStore ============ // +const devtools = createDevtools('chat'); export const useChatStore = createWithEqualityFn()( - subscribeWithSelector( - devtools(createStore, { - name: 'LobeChat_Chat' + (isDev ? '_DEV' : ''), - }), - ), + subscribeWithSelector(devtools(createStore)), shallow, ); diff --git a/src/store/file/store.ts b/src/store/file/store.ts index f08061892fb5..1aaa5ef5d9d0 100644 --- a/src/store/file/store.ts +++ b/src/store/file/store.ts @@ -1,10 +1,8 @@ -import { devtools } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { FilesStoreState, initialState } from './initialState'; import { FileAction, createFileSlice } from './slices/images'; import { TTSFileAction, createTTSFileSlice } from './slices/tts'; @@ -20,10 +18,6 @@ const createStore: StateCreator = (... }); // =============== 实装 useStore ============ // +const devtools = createDevtools('file'); -export const useFileStore = createWithEqualityFn()( - devtools(createStore, { - name: 'LobeChat_File' + (isDev ? '_DEV' : ''), - }), - shallow, -); +export const useFileStore = createWithEqualityFn()(devtools(createStore), shallow); diff --git a/src/store/global/store.ts b/src/store/global/store.ts index 4a616ffda66f..5bf1ac439f19 100644 --- a/src/store/global/store.ts +++ b/src/store/global/store.ts @@ -1,10 +1,9 @@ -import { devtools, subscribeWithSelector } from 'zustand/middleware'; +import { subscribeWithSelector } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { type GlobalStoreAction, globalActionSlice } from './action'; import { type GlobalState, initialState } from './initialState'; @@ -19,11 +18,9 @@ const createStore: StateCreator = (. // =============== 实装 useStore ============ // +const devtools = createDevtools('global'); + export const useGlobalStore = createWithEqualityFn()( - subscribeWithSelector( - devtools(createStore, { - name: 'LobeChat_Global' + (isDev ? '_DEV' : ''), - }), - ), + subscribeWithSelector(devtools(createStore)), shallow, ); diff --git a/src/store/market/store.ts b/src/store/market/store.ts index 5bec19726fff..4e1acf86cd00 100644 --- a/src/store/market/store.ts +++ b/src/store/market/store.ts @@ -1,11 +1,11 @@ -import { PersistOptions, devtools, persist } from 'zustand/middleware'; +import { PersistOptions, persist } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import type { StateCreator } from 'zustand/vanilla'; import { createHyperStorage } from '@/store/middleware/createHyperStorage'; -import { isDev } from '@/utils/env'; +import { createDevtools } from '../middleware/createDevtools'; import { type StoreAction, createMarketAction } from './action'; import { type StoreState, initialState } from './initialState'; @@ -40,12 +40,9 @@ const createStore: StateCreator = (...para ...createMarketAction(...parameters), }); +const devtools = createDevtools('market'); + export const useMarketStore = createWithEqualityFn()( - persist( - devtools(createStore, { - name: LOBE_AGENT_MARKET + (isDev ? '_DEV' : ''), - }), - persistOptions, - ), + persist(devtools(createStore), persistOptions), shallow, ); diff --git a/src/store/middleware/createDevtools.ts b/src/store/middleware/createDevtools.ts new file mode 100644 index 000000000000..f7b431d47a08 --- /dev/null +++ b/src/store/middleware/createDevtools.ts @@ -0,0 +1,23 @@ +import { optionalDevtools } from 'zustand-utils'; +import { devtools as _devtools } from 'zustand/middleware'; + +import { isDev } from '@/utils/env'; + +export const createDevtools = + (name: string): typeof _devtools => + (initializer) => { + let showDevtools = false; + + // check url to show devtools + if (typeof window !== 'undefined') { + const url = new URL(window.location.href); + const debug = url.searchParams.get('debug'); + if (debug?.includes(name)) { + showDevtools = true; + } + } + + return optionalDevtools(showDevtools)(initializer, { + name: `LobeChat_${name}` + (isDev ? '_DEV' : ''), + }); + }; diff --git a/src/store/serverConfig/store.ts b/src/store/serverConfig/store.ts index d974e0b1d218..6749625ae6b6 100644 --- a/src/store/serverConfig/store.ts +++ b/src/store/serverConfig/store.ts @@ -1,13 +1,12 @@ import { StoreApi } from 'zustand'; import { createContext } from 'zustand-utils'; -import { devtools } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; import { DEFAULT_FEATURE_FLAGS, IFeatureFlags } from '@/config/featureFlags'; +import { createDevtools } from '@/store/middleware/createDevtools'; import { GlobalServerConfig } from '@/types/serverConfig'; -import { isDev } from '@/utils/env'; import { merge } from '@/utils/merge'; import { StoreApiWithSelector } from '@/utils/zustand'; @@ -42,21 +41,16 @@ declare global { } } +const devtools = createDevtools('serverConfig'); + export const initServerConfigStore = (initState: Partial) => - createWithEqualityFn()( - devtools(createStore(initState || {}), { - name: 'LobeChat_ServerConfig' + (isDev ? '_DEV' : ''), - }), - shallow, - ); + createWithEqualityFn()(devtools(createStore(initState || {})), shallow); export const createServerConfigStore = (initState?: Partial) => { // make sure there is only one store if (!store) { store = createWithEqualityFn()( - devtools(createStore(initState || {}), { - name: 'LobeChat_ServerConfig' + (isDev ? '_DEV' : ''), - }), + devtools(createStore(initState || {})), shallow, ); diff --git a/src/store/session/store.ts b/src/store/session/store.ts index 316150b87af2..8fc63eec4720 100644 --- a/src/store/session/store.ts +++ b/src/store/session/store.ts @@ -1,10 +1,11 @@ -import { devtools, subscribeWithSelector } from 'zustand/middleware'; +import { subscribeWithSelector } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; import { isDev } from '@/utils/env'; +import { createDevtools } from '../middleware/createDevtools'; import { SessionStoreState, initialState } from './initialState'; import { SessionAction, createSessionSlice } from './slices/session/action'; import { SessionGroupAction, createSessionGroupSlice } from './slices/sessionGroup/action'; @@ -20,6 +21,7 @@ const createStore: StateCreator = ( }); // =============== implement useStore ============ // +const devtools = createDevtools('session'); export const useSessionStore = createWithEqualityFn()( subscribeWithSelector( diff --git a/src/store/tool/store.ts b/src/store/tool/store.ts index d9ca804c4c87..b8703485708e 100644 --- a/src/store/tool/store.ts +++ b/src/store/tool/store.ts @@ -1,10 +1,8 @@ -import { devtools } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { ToolStoreState, initialState } from './initialState'; import { BuiltinToolAction, createBuiltinToolSlice } from './slices/builtin'; import { CustomPluginAction, createCustomPluginSlice } from './slices/customPlugin'; @@ -29,9 +27,6 @@ const createStore: StateCreator = (... // =============== 实装 useStore ============ // -export const useToolStore = createWithEqualityFn()( - devtools(createStore, { - name: 'LobeChat_Tool' + (isDev ? '_DEV' : ''), - }), - shallow, -); +const devtools = createDevtools('tools'); + +export const useToolStore = createWithEqualityFn()(devtools(createStore), shallow); diff --git a/src/store/user/slices/common/action.ts b/src/store/user/slices/common/action.ts index be66ebcec2a0..945a3485c577 100644 --- a/src/store/user/slices/common/action.ts +++ b/src/store/user/slices/common/action.ts @@ -7,8 +7,8 @@ import { userService } from '@/services/user'; import { ClientService } from '@/services/user/client'; import type { UserStore } from '@/store/user'; import type { GlobalServerConfig } from '@/types/serverConfig'; -import type { GlobalSettings } from '@/types/settings'; import { UserInitializationState } from '@/types/user'; +import type { UserSettings } from '@/types/user/settings'; import { switchLang } from '@/utils/client/switchLang'; import { merge } from '@/utils/merge'; import { setNamespace } from '@/utils/storeDebug'; @@ -78,7 +78,7 @@ export const createCommonSlice: StateCreator< if (data) { // merge settings - const serverSettings: DeepPartial = { + const serverSettings: DeepPartial = { defaultAgent: serverConfig.defaultAgent, languageModel: serverConfig.languageModel, }; diff --git a/src/store/user/slices/modelList/action.test.ts b/src/store/user/slices/modelList/action.test.ts index 5ac73c02e7d0..e9805ad0be01 100644 --- a/src/store/user/slices/modelList/action.test.ts +++ b/src/store/user/slices/modelList/action.test.ts @@ -4,7 +4,7 @@ import { describe, expect, it, vi } from 'vitest'; import { modelsService } from '@/services/models'; import { userService } from '@/services/user'; import { useUserStore } from '@/store/user'; -import { GeneralModelProviderConfig } from '@/types/settings'; +import { OpenAICompatibleProviderConfig } from '@/types/user/settings'; import { settingsSelectors } from '../settings/selectors'; import { CustomModelCardDispatch } from './reducers/customModelCard'; @@ -24,7 +24,7 @@ describe('LLMSettingsSliceAction', () => { describe('setModelProviderConfig', () => { it('should set OpenAI configuration', async () => { const { result } = renderHook(() => useUserStore()); - const openAIConfig: Partial = { apiKey: 'test-key' }; + const openAIConfig: Partial = { apiKey: 'test-key' }; // Perform the action await act(async () => { diff --git a/src/store/user/slices/modelList/action.ts b/src/store/user/slices/modelList/action.ts index 0cecf3dc18b7..da7a23a5f370 100644 --- a/src/store/user/slices/modelList/action.ts +++ b/src/store/user/slices/modelList/action.ts @@ -6,7 +6,7 @@ import { DEFAULT_MODEL_PROVIDER_LIST } from '@/config/modelProviders'; import { ModelProvider } from '@/libs/agent-runtime'; import { UserStore } from '@/store/user'; import { ChatModelCard } from '@/types/llm'; -import { GlobalLLMConfig, GlobalLLMProviderKey } from '@/types/settings'; +import { UserModelProviderConfig, GlobalLLMProviderKey } from '@/types/user/settings'; import { settingsSelectors } from '../settings/selectors'; import { CustomModelCardDispatch, customModelCardsReducer } from './reducers/customModelCard'; @@ -28,7 +28,7 @@ export interface ModelListAction { removeEnabledModels: (provider: GlobalLLMProviderKey, model: string) => Promise; setModelProviderConfig: ( provider: T, - config: Partial, + config: Partial, ) => Promise; toggleEditingCustomModelCard: (params?: { id: string; provider: GlobalLLMProviderKey }) => void; diff --git a/src/store/user/slices/modelList/selectors/modelConfig.ts b/src/store/user/slices/modelList/selectors/modelConfig.ts index 47ed0720a8f5..854f5791809c 100644 --- a/src/store/user/slices/modelList/selectors/modelConfig.ts +++ b/src/store/user/slices/modelList/selectors/modelConfig.ts @@ -1,4 +1,4 @@ -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import { UserStore } from '../../../store'; import { currentLLMSettings, getProviderConfigById } from '../../settings/selectors/settings'; diff --git a/src/store/user/slices/modelList/selectors/modelProvider.ts b/src/store/user/slices/modelList/selectors/modelProvider.ts index d75da0cf2880..2e76975c8d94 100644 --- a/src/store/user/slices/modelList/selectors/modelProvider.ts +++ b/src/store/user/slices/modelList/selectors/modelProvider.ts @@ -3,7 +3,7 @@ import { uniqBy } from 'lodash-es'; import { filterEnabledModels } from '@/config/modelProviders'; import { ChatModelCard, ModelProviderCard } from '@/types/llm'; import { ServerModelProviderConfig } from '@/types/serverConfig'; -import { GlobalLLMProviderKey } from '@/types/settings'; +import { GlobalLLMProviderKey } from '@/types/user/settings'; import { UserStore } from '../../../store'; import { currentSettings, getProviderConfigById } from '../../settings/selectors/settings'; diff --git a/src/store/user/slices/settings/action.test.ts b/src/store/user/slices/settings/action.test.ts index 0565abbe1889..852fd0058ff7 100644 --- a/src/store/user/slices/settings/action.test.ts +++ b/src/store/user/slices/settings/action.test.ts @@ -7,7 +7,7 @@ import { DEFAULT_AGENT, DEFAULT_SETTINGS } from '@/const/settings'; import { userService } from '@/services/user'; import { useUserStore } from '@/store/user'; import { LobeAgentSettings } from '@/types/session'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; // Mock userService vi.mock('@/services/user', () => ({ @@ -21,7 +21,7 @@ describe('SettingsAction', () => { describe('importAppSettings', () => { it('should import app settings', async () => { const { result } = renderHook(() => useUserStore()); - const newSettings: GlobalSettings = { + const newSettings: UserSettings = { ...DEFAULT_SETTINGS, themeMode: 'dark', }; @@ -69,7 +69,7 @@ describe('SettingsAction', () => { describe('setSettings', () => { it('should set partial settings', async () => { const { result } = renderHook(() => useUserStore()); - const partialSettings: Partial = { themeMode: 'dark' }; + const partialSettings: Partial = { themeMode: 'dark' }; // Perform the action await act(async () => { @@ -116,7 +116,7 @@ describe('SettingsAction', () => { describe('setTranslationSystemAgent', () => { it('should set partial settings', async () => { const { result } = renderHook(() => useUserStore()); - const systemAgentSettings: Partial = { + const systemAgentSettings: Partial = { systemAgent: { translation: { model: 'testmodel', diff --git a/src/store/user/slices/settings/action.ts b/src/store/user/slices/settings/action.ts index 8e9085f01c1b..0659ed47dd99 100644 --- a/src/store/user/slices/settings/action.ts +++ b/src/store/user/slices/settings/action.ts @@ -7,15 +7,15 @@ import { userService } from '@/services/user'; import type { UserStore } from '@/store/user'; import { LocaleMode } from '@/types/locale'; import { LobeAgentSettings } from '@/types/session'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; import { switchLang } from '@/utils/client/switchLang'; import { difference } from '@/utils/difference'; import { merge } from '@/utils/merge'; export interface UserSettingsAction { - importAppSettings: (settings: GlobalSettings) => Promise; + importAppSettings: (settings: UserSettings) => Promise; resetSettings: () => Promise; - setSettings: (settings: DeepPartial) => Promise; + setSettings: (settings: DeepPartial) => Promise; setTranslationSystemAgent: (provider: string, model: string) => Promise; switchLocale: (locale: LocaleMode) => Promise; switchThemeMode: (themeMode: ThemeMode) => Promise; diff --git a/src/store/user/slices/settings/initialState.ts b/src/store/user/slices/settings/initialState.ts index 85e41c3a3b7e..7f5ab31cbdbb 100644 --- a/src/store/user/slices/settings/initialState.ts +++ b/src/store/user/slices/settings/initialState.ts @@ -1,11 +1,11 @@ import { DeepPartial } from 'utility-types'; import { DEFAULT_SETTINGS } from '@/const/settings'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; export interface UserSettingsState { - defaultSettings: GlobalSettings; - settings: DeepPartial; + defaultSettings: UserSettings; + settings: DeepPartial; } export const initialSettingsState: UserSettingsState = { diff --git a/src/store/user/slices/settings/selectors/settings.ts b/src/store/user/slices/settings/selectors/settings.ts index c05d3e642365..cbc6040d7bc6 100644 --- a/src/store/user/slices/settings/selectors/settings.ts +++ b/src/store/user/slices/settings/selectors/settings.ts @@ -7,19 +7,22 @@ import { DEFAULT_TTS_CONFIG, } from '@/const/settings'; import { Locales } from '@/locales/resources'; -import { GeneralModelProviderConfig, GlobalLLMProviderKey, GlobalSettings } from '@/types/settings'; +import { + OpenAICompatibleProviderConfig, + GlobalLLMProviderKey, + UserSettings, +} from '@/types/user/settings'; import { isOnServerSide } from '@/utils/env'; import { merge } from '@/utils/merge'; import { UserStore } from '../../../store'; -export const currentSettings = (s: UserStore): GlobalSettings => - merge(s.defaultSettings, s.settings); +export const currentSettings = (s: UserStore): UserSettings => merge(s.defaultSettings, s.settings); export const currentLLMSettings = (s: UserStore) => currentSettings(s).languageModel; export const getProviderConfigById = (provider: string) => (s: UserStore) => - currentLLMSettings(s)[provider as GlobalLLMProviderKey] as GeneralModelProviderConfig | undefined; + currentLLMSettings(s)[provider as GlobalLLMProviderKey] as OpenAICompatibleProviderConfig | undefined; const password = (s: UserStore) => currentSettings(s).password; @@ -35,7 +38,7 @@ const exportSettings = (s: UserStore) => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { password: _, ...settings } = currentSettings(s); - return settings as GlobalSettings; + return settings as UserSettings; }; const currentLanguage = (s: UserStore) => { diff --git a/src/store/user/store.ts b/src/store/user/store.ts index d6e7e0fb4136..9451a7f8f315 100644 --- a/src/store/user/store.ts +++ b/src/store/user/store.ts @@ -1,10 +1,9 @@ -import { devtools, subscribeWithSelector } from 'zustand/middleware'; +import { subscribeWithSelector } from 'zustand/middleware'; import { shallow } from 'zustand/shallow'; import { createWithEqualityFn } from 'zustand/traditional'; import { StateCreator } from 'zustand/vanilla'; -import { isDev } from '@/utils/env'; - +import { createDevtools } from '../middleware/createDevtools'; import { type UserState, initialState } from './initialState'; import { type UserAuthAction, createAuthSlice } from './slices/auth/action'; import { type CommonAction, createCommonSlice } from './slices/common/action'; @@ -35,11 +34,9 @@ const createStore: StateCreator = (... // =============== 实装 useStore ============ // +const devtools = createDevtools('user'); + export const useUserStore = createWithEqualityFn()( - subscribeWithSelector( - devtools(createStore, { - name: 'LobeChat_User' + (isDev ? '_DEV' : ''), - }), - ), + subscribeWithSelector(devtools(createStore)), shallow, ); diff --git a/src/types/exportConfig.ts b/src/types/exportConfig.ts index 9f6c5b32e437..b1634fdbf515 100644 --- a/src/types/exportConfig.ts +++ b/src/types/exportConfig.ts @@ -1,7 +1,7 @@ import { ChatMessage } from '@/types/message'; import { LobeSessions, SessionGroupItem } from '@/types/session'; -import { GlobalSettings } from '@/types/settings'; import { ChatTopic } from '@/types/topic'; +import { UserSettings } from '@/types/user/settings'; /** * 导出方式 @@ -69,7 +69,7 @@ export interface ConfigStateAgents { * 配置状态:设置 */ export interface ConfigStateSettings { - settings: GlobalSettings; + settings: UserSettings; } /** diff --git a/src/types/serverConfig.ts b/src/types/serverConfig.ts index 742f657f8ac7..0574271d08c0 100644 --- a/src/types/serverConfig.ts +++ b/src/types/serverConfig.ts @@ -1,7 +1,7 @@ import { DeepPartial } from 'utility-types'; import { ChatModelCard } from '@/types/llm'; -import { GlobalDefaultAgent, GlobalLLMProviderKey } from '@/types/settings'; +import { UserDefaultAgent, GlobalLLMProviderKey } from '@/types/user/settings'; export interface ServerModelProviderConfig { enabled?: boolean; @@ -16,7 +16,7 @@ export interface ServerModelProviderConfig { export type ServerLanguageModel = Partial>; export interface GlobalServerConfig { - defaultAgent?: DeepPartial; + defaultAgent?: DeepPartial; enableUploadFileToServer?: boolean; enabledAccessCode?: boolean; enabledOAuthSSO?: boolean; diff --git a/src/types/settings/index.ts b/src/types/settings/index.ts deleted file mode 100644 index c7c03c73cafd..000000000000 --- a/src/types/settings/index.ts +++ /dev/null @@ -1,33 +0,0 @@ -import type { LobeAgentSession } from '@/types/session'; - -import { GlobalBaseSettings } from './base'; -import { GlobalLLMConfig } from './modelProvider'; -import { GlobalSyncSettings } from './sync'; -import { GlobalSystemAgentConfig } from './systemAgent'; -import { GlobalTTSConfig } from './tts'; - -export type GlobalDefaultAgent = Pick; - -export * from './base'; -export * from './modelProvider'; -export * from './sync'; -export * from './systemAgent'; -export * from './tts'; - -export interface GlobalTool { - dalle: { - autoGenerate: boolean; - }; -} - -/** - * 配置设置 - */ -export interface GlobalSettings extends GlobalBaseSettings { - defaultAgent: GlobalDefaultAgent; - languageModel: GlobalLLMConfig; - sync: GlobalSyncSettings; - systemAgent: GlobalSystemAgentConfig; - tool: GlobalTool; - tts: GlobalTTSConfig; -} diff --git a/src/types/settings/modelProvider.ts b/src/types/settings/modelProvider.ts deleted file mode 100644 index bdcdb6e5d032..000000000000 --- a/src/types/settings/modelProvider.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { ChatModelCard } from '@/types/llm'; - -export interface GeneralModelProviderConfig { - apiKey?: string; - /** - * whether to auto fetch model lists - */ - autoFetchModelLists?: boolean; - /** - * user defined model cards - */ - customModelCards?: ChatModelCard[]; - enabled: boolean; - /** - * enabled models id - */ - enabledModels?: string[] | null; - endpoint?: string; - /** - * whether fetch on client - */ - fetchOnClient?: boolean; - /** - * the latest fetch model list time - */ - latestFetchTime?: number; - /** - * fetched models from provider side - */ - remoteModelCards?: ChatModelCard[]; -} - -export interface AzureOpenAIConfig extends GeneralModelProviderConfig { - apiVersion?: string; -} - -export interface AWSBedrockConfig extends Omit { - accessKeyId?: string; - region?: string; - secretAccessKey?: string; -} - -export interface GlobalLLMConfig { - anthropic: GeneralModelProviderConfig; - azure: AzureOpenAIConfig; - bedrock: AWSBedrockConfig; - deepseek: GeneralModelProviderConfig; - google: GeneralModelProviderConfig; - groq: GeneralModelProviderConfig; - minimax: GeneralModelProviderConfig; - mistral: GeneralModelProviderConfig; - moonshot: GeneralModelProviderConfig; - ollama: GeneralModelProviderConfig; - openai: GeneralModelProviderConfig; - openrouter: GeneralModelProviderConfig; - perplexity: GeneralModelProviderConfig; - togetherai: GeneralModelProviderConfig; - zeroone: GeneralModelProviderConfig; - zhipu: GeneralModelProviderConfig; -} - -export type GlobalLLMProviderKey = keyof GlobalLLMConfig; diff --git a/src/types/user/index.ts b/src/types/user/index.ts index 7eea214dd165..fdec61c76c92 100644 --- a/src/types/user/index.ts +++ b/src/types/user/index.ts @@ -1,6 +1,6 @@ import { DeepPartial } from 'utility-types'; -import { GlobalSettings } from '@/types/settings'; +import { UserSettings } from '@/types/user/settings'; export interface LobeUser { avatar?: string; @@ -39,6 +39,6 @@ export interface UserInitializationState { hasConversation?: boolean; isOnboard?: boolean; preference: UserPreference; - settings: DeepPartial; + settings: DeepPartial; userId?: string; } diff --git a/src/types/settings/base.ts b/src/types/user/settings/general.ts similarity index 82% rename from src/types/settings/base.ts rename to src/types/user/settings/general.ts index ae1c2da21564..89da30615647 100644 --- a/src/types/settings/base.ts +++ b/src/types/user/settings/general.ts @@ -3,10 +3,13 @@ import type { ThemeMode } from 'antd-style'; import { LocaleMode } from '@/types/locale'; -export interface GlobalBaseSettings { +export interface UserGeneralSettings { fontSize: number; language: LocaleMode; neutralColor?: NeutralColors; + /** + * @deprecated + */ password: string; primaryColor?: PrimaryColors; themeMode: ThemeMode; diff --git a/src/types/user/settings/index.ts b/src/types/user/settings/index.ts new file mode 100644 index 000000000000..b97e0c27b75c --- /dev/null +++ b/src/types/user/settings/index.ts @@ -0,0 +1,54 @@ +import type { NeutralColors, PrimaryColors } from '@lobehub/ui'; +import type { ThemeMode } from 'antd-style'; + +import { LocaleMode } from '@/types/locale'; +import type { LobeAgentSession } from '@/types/session'; + +import { UserModelProviderConfig } from './modelProvider'; +import { UserSyncSettings } from './sync'; +import { UserSystemAgentConfig } from './systemAgent'; +import { UserToolConfig } from './tool'; +import { UserTTSConfig } from './tts'; + +export type UserDefaultAgent = Pick; + +export * from './general'; +export * from './modelProvider'; +export * from './sync'; +export * from './systemAgent'; +export * from './tts'; + +/** + * 配置设置 + */ +export interface UserSettings { + defaultAgent: UserDefaultAgent; + /** + * @deprecated + */ + fontSize: number; + /** + * @deprecated + */ + language: LocaleMode; + languageModel: UserModelProviderConfig; + /** + * @deprecated + */ + neutralColor?: NeutralColors; + + password: string; + + /** + * @deprecated + */ + primaryColor?: PrimaryColors; + sync: UserSyncSettings; + systemAgent: UserSystemAgentConfig; + /** + * @deprecated + */ + themeMode: ThemeMode; + tool: UserToolConfig; + tts: UserTTSConfig; +} diff --git a/src/types/user/settings/modelProvider.ts b/src/types/user/settings/modelProvider.ts new file mode 100644 index 000000000000..122fa4e62617 --- /dev/null +++ b/src/types/user/settings/modelProvider.ts @@ -0,0 +1,70 @@ +import { ChatModelCard } from '@/types/llm'; + +export interface OpenAICompatibleProviderConfig { + /** + * @deprecated + */ + apiKey?: string; + /** + * whether to auto fetch model lists + */ + autoFetchModelLists?: boolean; + /** + * user defined model cards + */ + customModelCards?: ChatModelCard[]; + enabled: boolean; + /** + * enabled models id + */ + enabledModels?: string[] | null; + /** + * @deprecated + */ + endpoint?: string; + /** + * whether fetch on client + */ + fetchOnClient?: boolean; + /** + * the latest fetch model list time + */ + latestFetchTime?: number; + /** + * fetched models from provider side + */ + remoteModelCards?: ChatModelCard[]; +} + +export interface AzureOpenAIConfig extends Omit { + apiVersion?: string; + endpoint?: string; +} + +export interface AWSBedrockConfig + extends Omit { + accessKeyId?: string; + region?: string; + secretAccessKey?: string; +} + +export interface UserModelProviderConfig { + anthropic: OpenAICompatibleProviderConfig; + azure: AzureOpenAIConfig; + bedrock: AWSBedrockConfig; + deepseek: OpenAICompatibleProviderConfig; + google: OpenAICompatibleProviderConfig; + groq: OpenAICompatibleProviderConfig; + minimax: OpenAICompatibleProviderConfig; + mistral: OpenAICompatibleProviderConfig; + moonshot: OpenAICompatibleProviderConfig; + ollama: OpenAICompatibleProviderConfig; + openai: OpenAICompatibleProviderConfig; + openrouter: OpenAICompatibleProviderConfig; + perplexity: OpenAICompatibleProviderConfig; + togetherai: OpenAICompatibleProviderConfig; + zeroone: OpenAICompatibleProviderConfig; + zhipu: OpenAICompatibleProviderConfig; +} + +export type GlobalLLMProviderKey = keyof UserModelProviderConfig; diff --git a/src/types/settings/sync.ts b/src/types/user/settings/sync.ts similarity index 82% rename from src/types/settings/sync.ts rename to src/types/user/settings/sync.ts index 11073864ff05..85dcbdadc793 100644 --- a/src/types/settings/sync.ts +++ b/src/types/user/settings/sync.ts @@ -4,7 +4,7 @@ export interface WebRTCSyncConfig { enabled: boolean; signaling?: string; } -export interface GlobalSyncSettings { +export interface UserSyncSettings { deviceName?: string; webrtc: WebRTCSyncConfig; } diff --git a/src/types/settings/systemAgent.ts b/src/types/user/settings/systemAgent.ts similarity index 74% rename from src/types/settings/systemAgent.ts rename to src/types/user/settings/systemAgent.ts index 90b86273b389..1743b74866ac 100644 --- a/src/types/settings/systemAgent.ts +++ b/src/types/user/settings/systemAgent.ts @@ -3,6 +3,6 @@ export interface GlobalTranslationConfig { provider: string; } -export interface GlobalSystemAgentConfig { +export interface UserSystemAgentConfig { translation: GlobalTranslationConfig; } diff --git a/src/types/user/settings/tool.ts b/src/types/user/settings/tool.ts new file mode 100644 index 000000000000..ec6ab9818c84 --- /dev/null +++ b/src/types/user/settings/tool.ts @@ -0,0 +1,5 @@ +export interface UserToolConfig { + dalle: { + autoGenerate: boolean; + }; +} diff --git a/src/types/settings/tts.ts b/src/types/user/settings/tts.ts similarity index 83% rename from src/types/settings/tts.ts rename to src/types/user/settings/tts.ts index 606aa24c9d70..6a14301310c8 100644 --- a/src/types/settings/tts.ts +++ b/src/types/user/settings/tts.ts @@ -1,6 +1,6 @@ export type STTServer = 'openai' | 'browser'; -export interface GlobalTTSConfig { +export interface UserTTSConfig { openAI: { sttModel: 'whisper-1'; ttsModel: 'tts-1' | 'tts-1-hd';