LibreChat/client/src/utils/localStorage.ts
lia-by-librechat[bot] 1871d31401
🪏 fix: Restore Explicit File Search Uploads (#17226)
* fix: Restore Explicit File Search Uploads

* fix: Preserve Unified Assistants Upload Sources

* style: Format Upload Provider Condition

* fix: Keep Unified Code Uploads Implicit

* fix: Respect File Search Upload Role Permissions

* fix: Activate File Search After Upload Persistence

* fix: Recover File Search Activation for Retained Drafts

* fix: Distinguish File Search Choices From Preference Sync

* fix: Preserve Upload Operation and Draft Ownership

* fix: Bound Draft Recovery and Own Staged Upload Cancellation

* fix: Preserve Composer Draft Ownership Across Navigation

* style: Match Pinned Prettier Union Formatting

* fix: Bind Deferred Uploads to Their Originating Composer

* fix: Preserve Upload Ownership Through Acknowledged Composer Handoffs

* test: Pass the New Conversation Sentinel as a String

* fix: Refresh Composer Ownership Atomically on New Chat Reset

* fix: Preserve Composer Ownership During URL Prompt Cleanup

* fix: Refresh Composer Ownership After Project Route Changes

* fix: Fence Upload Activation With Current or Retained Composer Ownership

* fix: Retire Composer Ownership Across History and Reloads

* fix: Preserve New-Chat Upload Ownership During Bootstrap

* fix: Fence Retained Uploads During Composer Cache Recovery

* fix: Transfer File Search Preferences Only on First Assignment

* fix: Preserve Upload State Across Acknowledgement Races

* test: Use Production Pending Draft Keys in Autosave Regressions

* fix: Preserve Upload Ownership Through Assistants Sync

* test: Supply Required Assistants Submission Metadata

* fix: Remove Retired Upload Chips by File Identity

* test: Fix Sync Fixture Type and Regression Formatting

* fix: Preserve Retained Uploads and Final Tool Preferences

* test: Align Final Frame Fixtures With Upload Retirement

---------

Co-authored-by: Lia <lia@librechat.ai>
2026-10-21 23:53:29 +02:00

98 lines
3.9 KiB
TypeScript

import { LocalStorageKeys } from 'librechat-data-provider';
import type { TConversation } from 'librechat-data-provider';
import { FILE_SEARCH_CHOICE_PREFIX, clearFileSearchChoices } from './choices';
export const FILE_SEARCH_UPLOAD_PREFIX = 'file-search-upload:';
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,
};
}
/** Drops every composer draft. These hold whatever the user typed, and a paste held as a file
* keeps its entire text in the files draft, so they outlive a sign-out and the browser tab keeps
* its identity across an in-app account switch: the ordinary draft restore could otherwise hand
* the next account the previous one's writing. Called on the way out of a session rather than only
* on the way in, because a social sign-in returns through the silent refresh and never passes the
* login mutation at all. */
export function clearComposerDraftStorage() {
clearFileSearchChoices();
Object.keys(localStorage).forEach((key) => {
if (
key.startsWith(LocalStorageKeys.FILES_DRAFT) ||
key.startsWith(LocalStorageKeys.TEXT_DRAFT) ||
key.startsWith(FILE_SEARCH_UPLOAD_PREFIX) ||
key.startsWith(FILE_SEARCH_CHOICE_PREFIX)
) {
localStorage.removeItem(key);
}
});
}
export function clearLocalStorage(skipFirst?: boolean) {
/** Ahead of `skipFirst`: that exception exists to preserve the first pane's settings, and a
* shared browser is no place to make an exception for someone else's writing. */
clearComposerDraftStorage();
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.LAST_MEMORY_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 ||
/** A permissive code approval default belongs to the account that chose it, not to
* whoever signs in next on a shared browser. */
key === LocalStorageKeys.LAST_CODE_APPROVAL_MODE
) {
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.LAST_MEMORY_TOGGLE_) ||
key.startsWith(LocalStorageKeys.TEXT_DRAFT) ||
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_MODEL ||
key === LocalStorageKeys.LAST_TOOLS
) {
localStorage.removeItem(key);
}
});
}