Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.0 KB
Raw Blame
import { shallow } from 'zustand/shallow';
import { createWithEqualityFn } from 'zustand/traditional';
import { type StateCreator } from 'zustand/vanilla';

import { createDevtools } from '../middleware/createDevtools';
import { expose } from '../middleware/expose';
import { flattenActions } from '../utils/flattenActions';
import { type ResetableStore, ResetableStoreAction } from '../utils/resetableStore';
import { type UserMemoryStoreState } from './initialState';
import { initialState } from './initialState';
import { type ActivityAction } from './slices/activity';
import { createActivitySlice } from './slices/activity';
import { type AgentMemoryAction } from './slices/agent';
import { createAgentMemorySlice } from './slices/agent';
import { type BaseAction } from './slices/base';
import { createBaseSlice } from './slices/base';
import { type ContextAction } from './slices/context';
import { createContextSlice } from './slices/context';
import { type ContextStoryAction } from './slices/contextStory';
import { createContextStorySlice } from './slices/contextStory';
import { type ExperienceAction } from './slices/experience';
import { createExperienceSlice } from './slices/experience';
import { type HomeAction } from './slices/home';
import { createHomeSlice } from './slices/home';
import { type IdentityAction } from './slices/identity';
import { createIdentitySlice } from './slices/identity';
import { type PreferenceAction } from './slices/preference';
import { createPreferenceSlice } from './slices/preference';

export type UserMemoryStore = UserMemoryStoreState &
  ActivityAction &
  AgentMemoryAction &
  BaseAction &
  ContextAction &
  ContextStoryAction &
  ExperienceAction &
  HomeAction &
  IdentityAction &
  PreferenceAction &
  ResetableStore;

type UserMemoryStoreAction = ActivityAction &
  AgentMemoryAction &
  BaseAction &
  ContextAction &
  ContextStoryAction &
  ExperienceAction &
  HomeAction &
  IdentityAction &
  PreferenceAction &
  ResetableStore;

class UserMemoryStoreResetAction extends ResetableStoreAction<UserMemoryStore> {
  protected readonly resetActionName = 'resetUserMemoryStore';
}

const createStore: StateCreator<UserMemoryStore, [['zustand/devtools', never]]> = (
  set: any,
  get: any,
  store: any,
) => ({
  ...initialState,
  ...flattenActions<UserMemoryStoreAction>([
    createActivitySlice(set, get, store),
    createAgentMemorySlice(set, get, store),
    createBaseSlice(set, get, store),
    createContextSlice(set, get, store),
    createContextStorySlice(set, get, store),
    createExperienceSlice(set, get, store),
    createHomeSlice(set, get, store),
    createIdentitySlice(set, get, store),
    createPreferenceSlice(set, get, store),
    new UserMemoryStoreResetAction(set, get, store),
  ]),
});

const devtools = createDevtools('userMemory');

export const useUserMemoryStore = createWithEqualityFn<UserMemoryStore>()(
  devtools(createStore),
  shallow,
);

expose('userMemory', useUserMemoryStore);

export const getUserMemoryStoreState = () => useUserMemoryStore.getState();