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.

4.1 KB
Raw Blame
import type { TopicQuerySortBy } from '@lobechat/types';

import { getActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { MAIN_SIDEBAR_EXCLUDE_TRIGGERS } from '@/const/topic';
import { resolveAgentTopicGroupMode } from '@/features/AgentSidebar/Topic/utils/topicGroupMode';
import { useAgentStore } from '@/store/agent';
import { agentSelectors, builtinAgentSelectors } from '@/store/agent/selectors';
import {
  normalizeTopicListParams,
  type TopicListParams,
} from '@/store/chat/slices/topic/projection';
import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { preferenceSelectors } from '@/store/user/selectors';
import type { TopicGroupMode } from '@/types/topic';

const EXCLUDE_STATUSES_COMPLETED = ['completed'];

/**
 * Filter / sort identity of a sidebar list request β€” the local-first resource's
 * `query`, which decides which persisted row may hydrate the bucket.
 */
export interface SidebarTopicListQuery {
  excludeStatuses?: string[];
  excludeTriggers?: string[];
  onlyMine?: boolean;
  sortBy?: TopicQuerySortBy;
}

/**
 * Pure derivation of that identity from its inputs.
 *
 * Both readers below feed it β€” the reactive hook that renders the sidebar and
 * the imperative one the route loader uses before the first paint β€” so the two
 * paths can never ask for different rows.
 *
 * "Group by status" ordering is resolved server-side so the highest-priority
 * topics stay on the first page even when the list is paginated; only the agent
 * sidebar supports it, group sessions keep the default updatedAt ordering.
 */
export const deriveSidebarTopicListQuery = ({
  includeCompleted,
  isGroupSession,
  onlyMine,
  topicGroupMode,
}: {
  includeCompleted: boolean;
  isGroupSession: boolean;
  onlyMine: boolean;
  topicGroupMode: TopicGroupMode;
}): SidebarTopicListQuery => ({
  excludeStatuses: includeCompleted ? undefined : EXCLUDE_STATUSES_COMPLETED,
  excludeTriggers: MAIN_SIDEBAR_EXCLUDE_TRIGGERS,
  onlyMine: onlyMine || undefined,
  sortBy: !isGroupSession && topicGroupMode === 'byStatus' ? 'status' : undefined,
});

/**
 * Topic-group mode of one agent, resolved the same way the sidebar resolves it
 * for the active agent (agent config β†’ CLI-agent default β†’ user preference).
 */
export const getTopicGroupModeForAgent = (agentId?: string): TopicGroupMode => {
  const agentState = useAgentStore.getState();
  const config = agentId
    ? agentSelectors.getAgentConfigById(agentId)(agentState)
    : agentSelectors.currentAgentConfig(agentState);

  return resolveAgentTopicGroupMode({
    agentTopicGroupMode: config?.chatConfig?.topicGroupMode,
    agentType: config?.agencyConfig?.heterogeneousProvider?.type,
    globalMode: preferenceSelectors.topicGroupMode(useUserStore.getState()),
  });
};

/**
 * The exact params the agent sidebar will ask for, computed without React so a
 * route loader can hydrate the persisted page before the first paint.
 *
 * `session.agentId` names the *route's* agent: until the agent layout commits,
 * the stores still hold the previously active agent.
 */
export const getSidebarTopicListParams = (session: {
  agentId?: string;
  groupId?: string;
}): TopicListParams | null => {
  const { agentId, groupId } = session;
  if (!agentId && !groupId) return null;

  const agentState = useAgentStore.getState();
  const inboxAgentId = builtinAgentSelectors.inboxAgentId(agentState);
  const userState = useUserStore.getState();
  const globalState = useGlobalStore.getState();

  return normalizeTopicListParams({
    ...deriveSidebarTopicListQuery({
      includeCompleted: preferenceSelectors.topicIncludeCompleted(userState),
      isGroupSession: !!groupId,
      onlyMine:
        !!getActiveWorkspaceId() &&
        systemStatusSelectors.topicOnlyMine(groupId ?? agentId)(globalState),
      topicGroupMode: getTopicGroupModeForAgent(agentId),
    }),
    agentId,
    groupId,
    isInbox: !groupId && !!inboxAgentId && inboxAgentId === agentId,
    pageSize: systemStatusSelectors.topicPageSize(globalState),
  });
};