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.

2.2 KB
Raw Blame
import { type RetrieveMemoryParams, type RetrieveMemoryResult } from '@lobechat/types';

import { userMemoryKeys } from '@/libs/swr/keys';
import { type LayersEnum } from '@/types/userMemory';

import { userMemoryCacheKey } from '../../utils/cacheKey';
import { defineUserMemoryReplica } from '../../utils/replica';

/**
 * One query of the memory retrieve endpoint (`userMemoryService.retrieveMemory`).
 *
 * The entry key is the long-standing cache key of the query, so the replica view
 * (`memoryMap`) and the SWR key stay keyed exactly as before: any caller that
 * revalidates `userMemory:retrieve` by its root keeps working.
 */
export const userMemoryRetrieveResource = defineUserMemoryReplica<
  RetrieveMemoryParams,
  RetrieveMemoryResult
>({
  key: (params) => userMemoryCacheKey(params),
  name: 'userMemoryRetrieve',
  storage: 'indexedDB',
  // Adopt the existing SWR key so the legacy root-key revalidation still matches.
  syncKey: (params) => userMemoryKeys.retrieve(userMemoryCacheKey(params)),
  version: 1,
});

export interface MemoryDetailParams {
  id: string;
  layer: LayersEnum;
}

/**
 * The flattened detail one right-hand panel reads: the base memory fields, the
 * layer's own row and the resolved source link.
 */
export type MemoryDetailDisplay = Record<string, unknown>;

/** Entry key of one memory detail: one entry per layer + id. */
export const memoryDetailKey = ({ id, layer }: MemoryDetailParams): string => `${layer}:${id}`;

/**
 * One memory detail (`userMemoryService.getMemoryDetail`).
 *
 * A detail is a single transient read behind the right panel. It stays out of
 * storage on purpose: a deleted or edited memory must never paint a stale
 * persisted copy before the network confirms it. The fetcher answers `null`
 * for a memory the server no longer has, which removes the cached detail
 * (`isMissing`).
 */
export const userMemoryDetailResource = defineUserMemoryReplica<
  MemoryDetailParams,
  MemoryDetailDisplay,
  MemoryDetailDisplay | null
>({
  key: memoryDetailKey,
  name: 'userMemoryDetail',
  storage: 'memory',
  // Adopt the existing SWR key so a legacy `mutate(memoryDetail(...))` still matches.
  syncKey: ({ id, layer }) => userMemoryKeys.memoryDetail(layer, id),
  version: 1,
});