LibreChat/client/src/utils/localStorage.ts
Danny Avila e16a6190a5
💾 chore: Enhance Local Storage Handling and Update MCP SDK (#6809)
* feat: Update MCP package version and dependencies; refactor ToolContentPart type

* refactor: Change module type to commonjs and update rollup configuration, remove unused dev dependency

* refactor: Change async calls to synchronous for MCP and FlowStateManager retrieval

* chore: Add eslint disable comment for i18next rule in DropdownPopup component

* fix: improve statefulness of mcp servers selected if some were removed since last session

* feat: implement conversation storage cleanup functions and integrate them into mutation success handlers

* feat: enhance storage condition logic in useLocalStorageAlt to prevent unnecessary local storage writes

* refactor: streamline local storage update logic in useLocalStorageAlt
2025-04-09 18:38:48 -04:00

80 lines
2.6 KiB
TypeScript

import { LocalStorageKeys, TConversation, isUUID } from 'librechat-data-provider';
export function getLocalStorageItems() {
const items = {
lastSelectedModel: localStorage.getItem(LocalStorageKeys.LAST_MODEL) ?? '',
lastSelectedTools: localStorage.getItem(LocalStorageKeys.LAST_TOOLS) ?? '',
lastConversationSetup: localStorage.getItem(LocalStorageKeys.LAST_CONVO_SETUP + '_0') ?? '',
};
const lastSelectedModel = items.lastSelectedModel
? (JSON.parse(items.lastSelectedModel) as Record<string, string | undefined> | null)
: {};
const lastSelectedTools = items.lastSelectedTools
? (JSON.parse(items.lastSelectedTools) as string[] | null)
: [];
const lastConversationSetup = items.lastConversationSetup
? (JSON.parse(items.lastConversationSetup) as Partial<TConversation> | null)
: {};
return {
lastSelectedModel,
lastSelectedTools,
lastConversationSetup,
};
}
export function clearLocalStorage(skipFirst?: boolean) {
const keys = Object.keys(localStorage);
keys.forEach((key) => {
if (skipFirst === true && key.endsWith('0')) {
return;
}
if (
key.startsWith(LocalStorageKeys.LAST_MCP_) ||
key.startsWith(LocalStorageKeys.LAST_CODE_TOGGLE_) ||
key.startsWith(LocalStorageKeys.ASST_ID_PREFIX) ||
key.startsWith(LocalStorageKeys.AGENT_ID_PREFIX) ||
key.startsWith(LocalStorageKeys.LAST_CONVO_SETUP) ||
key === LocalStorageKeys.LAST_SPEC ||
key === LocalStorageKeys.LAST_TOOLS ||
key === LocalStorageKeys.LAST_MODEL ||
key === LocalStorageKeys.FILES_TO_DELETE
) {
localStorage.removeItem(key);
}
});
}
export function clearConversationStorage(conversationId?: string | null) {
if (!conversationId) {
return;
}
if (!isUUID.safeParse(conversationId)?.success) {
console.warn(
`Conversation ID ${conversationId} is not a valid UUID. Skipping local storage cleanup.`,
);
return;
}
const keys = Object.keys(localStorage);
keys.forEach((key) => {
if (key.includes(conversationId)) {
localStorage.removeItem(key);
}
});
}
export function clearAllConversationStorage() {
const keys = Object.keys(localStorage);
keys.forEach((key) => {
if (
key.startsWith(LocalStorageKeys.LAST_MCP_) ||
key.startsWith(LocalStorageKeys.LAST_CODE_TOGGLE_) ||
key.startsWith(LocalStorageKeys.TEXT_DRAFT) ||
key.startsWith(LocalStorageKeys.ASST_ID_PREFIX) ||
key.startsWith(LocalStorageKeys.AGENT_ID_PREFIX) ||
key.startsWith(LocalStorageKeys.LAST_CONVO_SETUP)
) {
localStorage.removeItem(key);
}
});
}