diff --git a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx index 6478e06fec..fd43faaa8b 100644 --- a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx +++ b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx @@ -33,6 +33,7 @@ import ModelParameterModal from '@/app/components/header/account-setting/model-p import { STORAGE_KEYS } from '@/config/storage-keys' import { generateBasicAppFirstTimeRule, generateRule } from '@/service/debug' import { useGenerateRuleTemplate } from '@/service/use-apps' +import { storage } from '@/utils/storage' import IdeaOutput from './idea-output' import InstructionEditorInBasic from './instruction-editor' import InstructionEditorInWorkflow from './instruction-editor-in-workflow' @@ -84,9 +85,7 @@ const GetAutomaticRes: FC = ({ onFinished, }) => { const { t } = useTranslation() - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) const [model, setModel] = React.useState(localModel || { name: '', provider: '', @@ -181,9 +180,7 @@ const GetAutomaticRes: FC = ({ useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) if (localModel) { setModel(localModel) } @@ -212,7 +209,7 @@ const GetAutomaticRes: FC = ({ mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -221,7 +218,7 @@ const GetAutomaticRes: FC = ({ completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const onGenerate = async () => { diff --git a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx index 333a6c7c0d..0d449946ea 100644 --- a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx +++ b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx @@ -20,6 +20,7 @@ import ModelParameterModal from '@/app/components/header/account-setting/model-p import { STORAGE_KEYS } from '@/config/storage-keys' import { generateRule } from '@/service/debug' import { useGenerateRuleTemplate } from '@/service/use-apps' +import { storage } from '@/utils/storage' import { languageMap } from '../../../../workflow/nodes/_base/components/editor/code-editor/index' import IdeaOutput from '../automatic/idea-output' import InstructionEditor from '../automatic/instruction-editor-in-workflow' @@ -54,9 +55,7 @@ export const GetCodeGeneratorResModal: FC = ( }, ) => { const { t } = useTranslation() - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) const [model, setModel] = React.useState(localModel || { name: '', provider: '', @@ -109,7 +108,7 @@ export const GetCodeGeneratorResModal: FC = ( mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -118,7 +117,7 @@ export const GetCodeGeneratorResModal: FC = ( completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const onGenerate = async () => { @@ -169,9 +168,7 @@ export const GetCodeGeneratorResModal: FC = ( useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) if (localModel) { setModel({ ...localModel, diff --git a/web/app/components/app/create-app-modal/index.tsx b/web/app/components/app/create-app-modal/index.tsx index 5325450c5d..57435c512b 100644 --- a/web/app/components/app/create-app-modal/index.tsx +++ b/web/app/components/app/create-app-modal/index.tsx @@ -30,6 +30,7 @@ import { createApp } from '@/service/apps' import { AppModeEnum } from '@/types/app' import { getRedirection } from '@/utils/app-redirection' import { cn } from '@/utils/classnames' +import { storage } from '@/utils/storage' import { basePath } from '@/utils/var' import AppIconPicker from '../../base/app-icon-picker' import ShortcutsName from '../../workflow/shortcuts-name' @@ -98,7 +99,7 @@ function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }: }) if (runtimeMode === 'sandboxed' && (appMode === AppModeEnum.WORKFLOW || appMode === AppModeEnum.ADVANCED_CHAT)) - localStorage.setItem(`${STORAGE_KEYS.LOCAL.WORKFLOW.SANDBOX_RUNTIME_PREFIX}${app.id}`, '1') + storage.set(`${STORAGE_KEYS.LOCAL.WORKFLOW.SANDBOX_RUNTIME_PREFIX}${app.id}`, true) // Track app creation success trackEvent('create_app', { diff --git a/web/app/components/workflow-app/hooks/use-workflow-init.ts b/web/app/components/workflow-app/hooks/use-workflow-init.ts index 93600151b6..3fed7b1a60 100644 --- a/web/app/components/workflow-app/hooks/use-workflow-init.ts +++ b/web/app/components/workflow-app/hooks/use-workflow-init.ts @@ -21,6 +21,7 @@ import { syncWorkflowDraft, } from '@/service/workflow' import { AppModeEnum } from '@/types/app' +import { storage } from '@/utils/storage' import { useWorkflowTemplate } from './use-workflow-template' const hasConnectedUserInput = (nodes: Node[] = [], edges: Edge[] = []): boolean => { @@ -87,9 +88,9 @@ export const useWorkflowInit = () => { const edgesData = isAdvancedChat ? edgesTemplate : [] const runtimeStorageKey = `${STORAGE_KEYS.LOCAL.WORKFLOW.SANDBOX_RUNTIME_PREFIX}${appDetail.id}` - const enableSandboxRuntime = localStorage.getItem(runtimeStorageKey) === '1' + const enableSandboxRuntime = storage.getBoolean(runtimeStorageKey) === true if (enableSandboxRuntime) - localStorage.removeItem(runtimeStorageKey) + storage.remove(runtimeStorageKey) syncWorkflowDraft({ url: `/apps/${appDetail.id}/workflows/draft`, diff --git a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx index 5ec68d9ce6..f2c96dcd0f 100644 --- a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx +++ b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx @@ -17,6 +17,7 @@ import useTheme from '@/hooks/use-theme' import { useGenerateStructuredOutputRules } from '@/service/use-common' import { ModelModeType, Theme } from '@/types/app' import { cn } from '@/utils/classnames' +import { storage } from '@/utils/storage' import { useMittContext } from '../visual-editor/context' import { useVisualEditorStore } from '../visual-editor/store' import { SchemaGeneratorDark, SchemaGeneratorLight } from './assets' @@ -37,9 +38,7 @@ const JsonSchemaGenerator: FC = ({ onApply, crossAxisOffset, }) => { - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) const [open, setOpen] = useState(false) const [view, setView] = useState(GeneratorView.promptEditor) const [model, setModel] = useState(localModel || { @@ -61,9 +60,7 @@ const JsonSchemaGenerator: FC = ({ useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') - : null + const localModel = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) if (localModel) { setModel(localModel) } @@ -96,7 +93,7 @@ const JsonSchemaGenerator: FC = ({ mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -105,7 +102,7 @@ const JsonSchemaGenerator: FC = ({ completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model, setModel]) const { mutateAsync: generateStructuredOutputRules, isPending: isGenerating } = useGenerateStructuredOutputRules() diff --git a/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts b/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts index 50dcb07846..f15a20681d 100644 --- a/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts +++ b/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts @@ -27,6 +27,7 @@ import { useGetLanguage } from '@/context/i18n' import { languages } from '@/i18n-config/language' import { fetchContextGenerateSuggestedQuestions, generateContext } from '@/service/debug' import { AppModeEnum } from '@/types/app' +import { storage } from '@/utils/storage' import { CONTEXT_GEN_STORAGE_SUFFIX, getContextGenStorageKey } from '../utils/storage' import useContextGenData from './use-context-gen-data' @@ -272,10 +273,9 @@ const useContextGenerate = ({ const [inputValue, setInputValue] = useState('') const [isGenerating, { setTrue: setGeneratingTrue, setFalse: setGeneratingFalse }] = useBoolean(false) const [modelOverride, setModelOverride] = useState(() => { - const stored = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) - if (!stored) + const parsed = storage.get(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + if (!parsed) return null - const parsed = JSON.parse(stored) as Model return { ...parsed, completion_params: (parsed.completion_params ?? {}) as CompletionParams, @@ -328,7 +328,7 @@ const useContextGenerate = ({ mode: newValue.mode as ModelModeType, } setModelOverride(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -337,7 +337,7 @@ const useContextGenerate = ({ completion_params: newParams as CompletionParams, } setModelOverride(newModel) - localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel) }, [model]) const promptMessageCount = promptMessages?.length ?? 0