Skip to content

Commit

Permalink
DChat: remove IDB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed May 16, 2024
1 parent 9eb0cc0 commit 44ab048
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
33 changes: 2 additions & 31 deletions src/common/state/store-chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { v4 as uuidv4 } from 'uuid';

import { DLLMId, getChatLLMId } from '~/modules/llms/store-llms';

import { IDB_MIGRATION_INITIAL, idbStateStorage } from '../util/idbUtils';
import { idbStateStorage } from '../util/idbUtils';
import { countModelTokens } from '../util/token-counter';
import { defaultSystemPurposeId, SystemPurposeId } from '../../data';

Expand Down Expand Up @@ -407,10 +407,7 @@ export const useChatStore = create<ConversationsStore>()(devtools(
storage: createJSONStorage(() => idbStateStorage),

// Migrations
migrate: (persistedState: unknown, fromVersion: number): ConversationsStore => {
// -1 -> 3: migration loading from localStorage to IndexedDB
if (fromVersion === IDB_MIGRATION_INITIAL)
return _migrateLocalStorageData() as any;
migrate: (persistedState: unknown, _fromVersion: number): ConversationsStore => {

// other: just proceed
return persistedState as any;
Expand Down Expand Up @@ -465,32 +462,6 @@ function getNextBranchTitle(currentTitle: string): string {
return `(1) ${currentTitle}`;
}

/**
* Returns the chats stored in the localStorage, and rename the key for
* backup/data loss prevention purposes
*/
function _migrateLocalStorageData(): ChatState | {} {
const key = 'app-chats';
const value = localStorage.getItem(key);
if (!value) return {};
try {
// parse the localStorage state
const localStorageState = JSON.parse(value)?.state;

// backup and delete the localStorage key
const backupKey = `${key}-v2`;
localStorage.setItem(backupKey, value);
localStorage.removeItem(key);

// match the state from localstorage
return {
conversations: localStorageState?.conversations ?? [],
};
} catch (error) {
console.error('LocalStorage migration error', error);
return {};
}
}

/**
* Convenience function to count the tokens in a DMessage object
Expand Down
15 changes: 0 additions & 15 deletions src/common/util/idbUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { StateStorage } from 'zustand/middleware';
import { del as idbDel, get as idbGet, set as idbSet } from 'idb-keyval';

// used by the state storage middleware to detect data migration from the old state storage (localStorage)
// NOTE: remove past 2024-03-19 (6 months past release of this utility conversion)
export const IDB_MIGRATION_INITIAL = -1;


// set to true to enable debugging
const DEBUG_SCHEDULER = false;
Expand Down Expand Up @@ -130,17 +126,6 @@ export const idbStateStorage: StateStorage = {
if (DEBUG_SCHEDULER)
console.warn(' (read bytes:', value?.length?.toLocaleString(), ')');

/* IMPORTANT!
* We modify the default behavior of `getItem` to return a {version: -1} object if a key is not found.
* This is to trigger the migration across state storage implementations, as Zustand would not call the
* 'migrate' function otherwise.
* See 'https://github.com/enricoros/big-agi/pull/158' for more details
*/
if (value === undefined) {
return JSON.stringify({
version: IDB_MIGRATION_INITIAL,
});
}
return value || null;
},
setItem: (name: string, value: string): void => {
Expand Down

0 comments on commit 44ab048

Please sign in to comment.