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.

9.3 KB
Raw Blame
import type { SidebarAgentItem, SidebarAgentListResponse, SidebarGroup } from '@lobechat/types';
import isEqual from 'fast-deep-equal';
import { type SWRResponse } from 'swr';

import {
  cacheScope,
  createReplicaSlice,
  linkReplicaEntity,
  type ReplicaEntityAdapter,
  type ReplicaLens,
  type ReplicaSyncResult,
} from '@/libs/replica';
import { useClientDataSWR } from '@/libs/swr';
import { agentConfigKeys } from '@/libs/swr/keys';
import { homeService } from '@/services/home';
import { getAgentStoreState } from '@/store/agent';
import { type HomeStore } from '@/store/home/store';
import { type StoreSetter } from '@/store/types';
import { setNamespace } from '@/utils/storeDebug';

import {
  initialAgentListState,
  mapResponseToState,
  mapStateToResponse,
  type SidebarAgentMetaPatch,
} from './initialState';
import { AGENT_LIST_KEY, agentListResource } from './projection';

const n = setNamespace('agentList');

const LIST_PARAMS = {} as Record<string, never>;

const EMPTY_RESPONSE: SidebarAgentListResponse = mapStateToResponse(initialAgentListState);

/**
 * The sidebar list keeps its long-standing flat fields (`pinnedAgents`,
 * `agentGroups` …) as the replica view, so every selector reads what it did.
 */
const agentListLens: ReplicaLens<HomeStore, SidebarAgentListResponse> = {
  clear: () => ({ ...mapResponseToState(EMPTY_RESPONSE), isAgentListInit: false }),
  get: (state) => (state.isAgentListInit ? mapStateToResponse(state) : undefined),
  keys: (state) => (state.isAgentListInit ? [AGENT_LIST_KEY] : []),
  set: (_state, _key, data) =>
    data
      ? { ...mapResponseToState(data), isAgentListInit: true }
      : { ...mapResponseToState(EMPTY_RESPONSE), isAgentListInit: false },
};

type ItemFn = (item: SidebarAgentItem) => SidebarAgentItem | undefined;

const mapItems = (items: SidebarAgentItem[], id: string, fn: ItemFn) => {
  if (!items.some((item) => item.id === id)) return items;
  return items.flatMap((item) => {
    if (item.id !== id) return [item];
    const next = fn(item);
    return next ? [next] : [];
  });
};

const mapGroups = (groups: SidebarGroup[], id: string, fn: ItemFn) => {
  if (!groups.some((group) => group.items.some((item) => item.id === id))) return groups;
  return groups.map((group) => {
    const items = mapItems(group.items, id, fn);
    return items === group.items ? group : { ...group, items };
  });
};

/** One agent can sit in any bucket, including a folder of either section. */
const sidebarAgentEntity: ReplicaEntityAdapter<SidebarAgentListResponse, SidebarAgentItem> = {
  has: (data, id) =>
    [data.pinned, data.ungrouped, data.privatePinned, data.privateUngrouped].some((items) =>
      items?.some((item) => item.id === id),
    ) ||
    [data.groups, data.privateGroups].some((groups) =>
      groups?.some((group) => group.items.some((item) => item.id === id)),
    ),
  // Private buckets may be absent on rows persisted before they existed.
  map: (data, id, fn) => ({
    ...data,
    groups: mapGroups(data.groups, id, fn),
    pinned: mapItems(data.pinned, id, fn),
    privateGroups: mapGroups(data.privateGroups ?? [], id, fn),
    privatePinned: mapItems(data.privatePinned ?? [], id, fn),
    privateUngrouped: mapItems(data.privateUngrouped ?? [], id, fn),
    ungrouped: mapItems(data.ungrouped, id, fn),
  }),
};

type Setter = StoreSetter<HomeStore>;
export const createAgentListSlice = (set: Setter, get: () => HomeStore, _api?: unknown) =>
  new AgentListActionImpl(set, get, _api);

/**
 * Put the rows named in `ids` first, in that order; rows it does not name keep
 * their relative order after them.
 */
export const reorderItems = (items: SidebarAgentItem[], ids: readonly string[]) => {
  const byId = new Map(items.map((item) => [item.id, item]));
  const listed = ids.flatMap((id) => byId.get(id) ?? []);
  const listedIds = new Set(listed.map((item) => item.id));
  return [...listed, ...items.filter((item) => !listedIds.has(item.id))];
};

/** A pinned-order save dropped because the account or workspace changed meanwhile. */
class ScopeChangedError extends Error {}

export class AgentListActionImpl {
  readonly #set: Setter;
  readonly #get: () => HomeStore;
  readonly #agentList;
  readonly #agents;
  #pinnedOrderQueue: Promise<unknown> = Promise.resolve();
  #recommendedQueue: Promise<unknown> = Promise.resolve();

  constructor(set: Setter, get: () => HomeStore, _api?: unknown) {
    void _api;
    this.#set = set;
    this.#get = get;
    this.#agentList = createReplicaSlice(agentListResource, {
      actionPrefix: n('agentList'),
      entity: sidebarAgentEntity,
      fetcher: () => homeService.getSidebarAgentList(),
      get,
      // An unchanged list must not re-render every sidebar section.
      merge: (incoming, confirmed) => (isEqual(incoming, confirmed) ? undefined : incoming),
      set,
      stateKey: 'agentListReplica',
      view: agentListLens,
    });
    this.#agents = linkReplicaEntity<SidebarAgentItem>([this.#agentList]);
  }

  closeAllAgentsDrawer = (): void => {
    this.#set({ allAgentsDrawerOpen: false }, false, n('closeAllAgentsDrawer'));
  };

  openAllAgentsDrawer = (): void => {
    this.#set({ allAgentsDrawerOpen: true }, false, n('openAllAgentsDrawer'));
  };

  refreshAgentList = async (): Promise<void> => {
    getAgentStoreState().invalidateAvailableAgents();
    await this.#agentList.revalidate(AGENT_LIST_KEY);
  };

  fetchAgentList = async (): Promise<void> => {
    getAgentStoreState().invalidateAvailableAgents();
    await this.#agentList.fetch(LIST_PARAMS, { fresh: true });
  };

  /** Rename / re-avatar an agent: the sidebar row changes now, rolls back on failure. */
  updateAgentMeta = async (id: string, patch: SidebarAgentMetaPatch): Promise<void> => {
    await this.#agents.optimistic(
      id,
      (item) => ({ ...item, ...patch }),
      () => getAgentStoreState().updateAgentMetaById(id, patch, { rethrow: true }),
    );
  };

  /**
   * Reorder the `pinned` rows (team pins in a workspace, the user's own pins
   * in personal mode) by drag and drop: the sidebar moves now and rolls back
   * if the save fails. Saves run one after another, so quick successive drags
   * reach the server in the order they were made. A save still queued when the
   * cache scope changes (another account, or another workspace) is dropped: the
   * request would carry the new session and overwrite its order with rows from
   * the old one.
   */
  reorderPinnedAgents = async (ids: string[]): Promise<void> => {
    const scope = cacheScope.get();
    try {
      await this.#agentList.optimistic(
        AGENT_LIST_KEY,
        (data) => (data ? { ...data, pinned: reorderItems(data.pinned, ids) } : data),
        () => {
          const operation = this.#pinnedOrderQueue
            .catch(() => undefined)
            .then(async () => {
              // Failing the save rolls the unsaved order back out of the list.
              if (cacheScope.get() !== scope) throw new ScopeChangedError();
              await homeService.updatePinnedOrder(ids);
            });
          this.#pinnedOrderQueue = operation;
          return operation;
        },
      );
    } catch (error) {
      // Nothing to report: the user left the account/workspace the drag was made in.
      if (error instanceof ScopeChangedError) return;
      throw error;
    }
  };

  /**
   * Replace the workspace's recommended Agents (owner/admin only): the Agents
   * page updates now and rolls back if the save fails.
   */
  updateRecommendedAgents = async (ids: string[]): Promise<void> => {
    // Only public rows can be recommended: drop ids of rows deleted or made
    // private since, so they never count toward the list's cap.
    const state = this.#get();
    const available = new Set(
      [
        ...state.pinnedAgents,
        ...state.ungroupedAgents,
        ...state.agentGroups.flatMap((group) => group.items),
      ].map((item) => item.id),
    );
    const next = ids.filter((id) => available.has(id));

    // Each save replaces the whole list, so saves run one after another: two
    // quick drags reach the server in the order they were made. A save still
    // queued when the cache scope changes is dropped (see reorderPinnedAgents).
    const scope = cacheScope.get();
    try {
      await this.#agentList.optimistic(
        AGENT_LIST_KEY,
        (data) => (data ? { ...data, recommendedIds: next } : data),
        () => {
          const operation = this.#recommendedQueue
            .catch(() => undefined)
            .then(async () => {
              if (cacheScope.get() !== scope) throw new ScopeChangedError();
              await homeService.updateRecommendedAgents(next);
            });
          this.#recommendedQueue = operation;
          return operation;
        },
      );
    } catch (error) {
      if (error instanceof ScopeChangedError) return;
      throw error;
    }
  };

  /** Fetch orchestration only; read the list through `homeAgentListSelectors`. */
  useFetchAgentList = (isLogin: boolean | undefined): ReplicaSyncResult =>
    this.#agentList.useSync(LIST_PARAMS, { enabled: isLogin === true });

  useSearchAgents = (keyword?: string): SWRResponse<SidebarAgentItem[]> => {
    return useClientDataSWR<SidebarAgentItem[]>(agentConfigKeys.search(keyword), async () => {
      if (!keyword) return [];

      return homeService.searchAgents(keyword);
    });
  };
}

export type AgentListAction = Pick<AgentListActionImpl, keyof AgentListActionImpl>;