| 1 | import { |
| 2 | combineReducers, |
| 3 | configureStore, |
| 4 | ThunkDispatch, |
| 5 | UnknownAction, |
| 6 | } from "@reduxjs/toolkit"; |
| 7 | import { createLogger } from "redux-logger"; |
| 8 | import { |
| 9 | createMigrate, |
| 10 | MigrationManifest, |
| 11 | persistReducer, |
| 12 | persistStore, |
| 13 | } from "redux-persist"; |
| 14 | import { createFilter } from "redux-persist-transform-filter"; |
| 15 | import autoMergeLevel2 from "redux-persist/lib/stateReconciler/autoMergeLevel2"; |
| 16 | import storage from "redux-persist/lib/storage"; |
| 17 | import { IdeMessenger, IIdeMessenger } from "../context/IdeMessenger"; |
| 18 | import configReducer from "./slices/configSlice"; |
| 19 | import editModeStateReducer from "./slices/editState"; |
| 20 | import indexingReducer from "./slices/indexingSlice"; |
| 21 | import { profilesReducer } from "./slices/profilesSlice"; |
| 22 | import sessionReducer from "./slices/sessionSlice"; |
| 23 | import tabsReducer from "./slices/tabsSlice"; |
| 24 | import uiReducer from "./slices/uiSlice"; |
| 25 | |
| 26 | const rootReducer = combineReducers({ |
| 27 | session: sessionReducer, |
| 28 | ui: uiReducer, |
| 29 | editModeState: editModeStateReducer, |
| 30 | config: configReducer, |
| 31 | indexing: indexingReducer, |
| 32 | tabs: tabsReducer, |
| 33 | profiles: profilesReducer, |
| 34 | }); |
| 35 | |
| 36 | const saveSubsetFilters = [ |
| 37 | createFilter("session", [ |
| 38 | "history", |
| 39 | "id", |
| 40 | "lastSessionId", |
| 41 | "title", |
| 42 | |
| 43 | // Persist edit mode in case closes in middle |
| 44 | "mode", |
| 45 | |
| 46 | // higher risk to persist |
| 47 | // codeBlockApplyStates |
| 48 | // symbols |
| 49 | ]), |
| 50 | createFilter("editModeState", [ |
| 51 | "returnToMode", |
| 52 | "lastNonEditSessionWasEmpty", |
| 53 | "codeToEdit", |
| 54 | ]), |
| 55 | createFilter("config", []), |
| 56 | createFilter("ui", ["toolSettings", "toolGroupSettings", "ruleSettings"]), |
| 57 | createFilter("indexing", []), |
| 58 | createFilter("tabs", ["tabs"]), |
| 59 | createFilter("profiles", [ |
| 60 | "preferencesByProfileId", |
| 61 | "selectedProfileId", |
| 62 | "selectedOrganizationId", |
| 63 | "organizations", |
| 64 | ]), |
| 65 | ]; |
| 66 | |
| 67 | const migrations: MigrationManifest = { |
| 68 | "0": (state) => { |
| 69 | const oldState = state as any; |
| 70 | |
| 71 | return { |
| 72 | config: { |
| 73 | defaultModelTitle: oldState?.state?.defaultModelTitle ?? undefined, |
| 74 | }, |
| 75 | session: { |
| 76 | history: oldState?.state?.history ?? [], |
| 77 | id: oldState?.state?.sessionId ?? "", |
| 78 | }, |
| 79 | tabs: { |
| 80 | tabs: [ |
| 81 | { |
| 82 | id: |
| 83 | Date.now().toString(36) + Math.random().toString(36).substring(2), |
| 84 | title: "Chat 1", |
| 85 | isActive: true, |
| 86 | }, |
| 87 | ], |
| 88 | }, |
| 89 | _persist: oldState?._persist, |
| 90 | }; |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | const persistConfig = { |
| 95 | version: 1, |
| 96 | key: "root", |
| 97 | storage, |
| 98 | transforms: [...saveSubsetFilters], |
| 99 | stateReconciler: autoMergeLevel2, |
| 100 | migrate: createMigrate(migrations, { debug: false }), |
| 101 | }; |
| 102 | |
| 103 | const persistedReducer = persistReducer<ReturnType<typeof rootReducer>>( |
| 104 | persistConfig, |
| 105 | rootReducer, |
| 106 | ); |
| 107 | |
| 108 | export function setupStore(options: { ideMessenger?: IIdeMessenger }) { |
| 109 | const ideMessenger = options.ideMessenger ?? new IdeMessenger(); |
| 110 | |
| 111 | const logger = createLogger({ |
| 112 | // Customize logger options if needed |
| 113 | collapsed: true, // Collapse console groups by default |
| 114 | timestamp: false, // Remove timestamps from log |
| 115 | diff: true, // Show diff between states |
| 116 | }); |
| 117 | |
| 118 | return configureStore({ |
| 119 | // persistedReducer causes type errors with async thunks |
| 120 | reducer: persistedReducer as unknown as typeof rootReducer, |
| 121 | // reducer: rootReducer, |
| 122 | middleware: (getDefaultMiddleware) => |
| 123 | getDefaultMiddleware({ |
| 124 | serializableCheck: false, |
| 125 | thunk: { |
| 126 | extraArgument: { |
| 127 | ideMessenger, |
| 128 | }, |
| 129 | }, |
| 130 | }), |
| 131 | // This can be uncommented to get detailed Redux logs |
| 132 | // .concat(logger), |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | export type ThunkExtrasType = { ideMessenger: IIdeMessenger }; |
| 137 | |
| 138 | export type ThunkApiType = { |
| 139 | state: RootState; |
| 140 | extra: ThunkExtrasType; |
| 141 | }; |
| 142 | |
| 143 | export type AppThunkDispatch = ThunkDispatch< |
| 144 | RootState, |
| 145 | ThunkExtrasType, |
| 146 | UnknownAction |
| 147 | >; |
| 148 | |
| 149 | export const store = setupStore({}); |
| 150 | |
| 151 | export type RootState = ReturnType<typeof rootReducer>; |
| 152 | |
| 153 | export type AppDispatch = typeof store.dispatch; |
| 154 | |
| 155 | export const persistor = persistStore(store); |