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.

28.6 KB
Raw Blame
'use client';

import { DEFAULT_USER_AVATAR_URL } from '@lobechat/const';
import { agentDisplayName, type SidebarAgentItem } from '@lobechat/types';
import {
  Avatar,
  Button,
  Center,
  createStaticStyles,
  cssVar,
  cx,
  DropdownMenu,
  Empty,
  Flexbox,
  Icon,
  SearchBar,
  Segmented,
  Text,
  toast,
  Tooltip,
} from '@lobehub/ui';
import dayjs from 'dayjs';
import isEqual from 'fast-deep-equal';
import { ChevronDownIcon, ChevronRightIcon, PlusIcon } from 'lucide-react';
import { memo, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router';

import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { useWorkspaceMembers } from '@/business/client/hooks/useWorkspaceMembers';
import {
  AgentViewAllRowsSkeleton,
  AgentViewAllSidebarSectionSkeleton,
} from '@/components/Skeleton/AgentViewAll';
import { useKeepSidebarGroupsListed } from '@/features/HomeSidebar/Body/Agent/List/useAgentList';
import { useWorkspaceSidebarItemIds } from '@/features/HomeSidebar/Body/Agent/List/useWorkspaceAgentSections';
import { AgentModalProvider } from '@/features/HomeSidebar/Body/Agent/ModalProvider';
import { useSidebarItemVisibility } from '@/features/HomeSidebar/Body/Agent/useSidebarItemVisibility';
import { useCreateMenuItems } from '@/features/HomeSidebar/hooks';
import NavHeader from '@/features/NavHeader';
import WideScreenContainer from '@/features/WideScreenContainer';
import { useFetchAgentLabels } from '@/hooks/useFetchAgentLabels';
import { useFetchAgentList } from '@/hooks/useFetchAgentList';
import { usePermission } from '@/hooks/usePermission';
import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { useHomeStore } from '@/store/home';
import { homeAgentListSelectors } from '@/store/home/selectors';
import { useUserStore } from '@/store/user';
import { useInterfaceDensity } from '@/styles/density';
import { densityVars } from '@/styles/densityVars';
import { getWorkspaceMemberNickname } from '@/utils/workspaceMemberName';

import AgentCard, { AgentCardGrid } from './AgentCard';
import AgentRow, { type AgentRowAuthor } from './AgentRow';
import { flattenAgentBuckets } from './flattenBuckets';
import { groupItemsByFolder } from './groupByFolder';
import ListConfig from './ListConfig';
import { type AgentListViewOptions, normalizeAgentListViewOptions } from './listViewOptions';
import RecommendedAgentsSection from './RecommendedAgentsSection';
import SidebarAgentsSection from './SidebarAgentsSection';

type SegmentValue = 'private' | 'workspace';
type ViewMode = 'card' | 'list';

interface GroupHeaderProps {
  /** Author avatar for author-grouping headers (Linear-style). */
  avatar?: string | null;
  collapsed: boolean;
  color?: string | null;
  count: number;
  /** Position in the group list — drives the alternating bar tint. */
  index: number;
  label: string;
  onToggle: () => void;
}

// Linear-style group header: a full-width subtle bar with chevron +
// identity (color dot / avatar) + name + count.
const groupHeaderStyles = createStaticStyles(({ css, cssVar }) => ({
  bar: css`
    cursor: pointer;
    user-select: none;

    width: 100%;
    padding-block: 8px;
    padding-inline: ${densityVars.contentGap};
    border-radius: ${cssVar.borderRadiusLG};

    transition: background 0.15s;
  `,
  // Alternating tints (mirrors Linear's group striping).
  barEven: css`
    background: ${cssVar.colorFillQuaternary};

    &:hover {
      background: ${cssVar.colorFillTertiary};
    }
  `,
  barOdd: css`
    background: ${cssVar.colorFillTertiary};

    &:hover {
      background: ${cssVar.colorFillSecondary};
    }
  `,
}));

const GroupHeader = memo<GroupHeaderProps>(
  ({ avatar, collapsed, color, count, index, label, onToggle }) => {
    const compact = useInterfaceDensity() === 'compact';

    return (
      <Flexbox
        horizontal
        align={'center'}
        gap={8}
        paddingBlock={compact ? 6 : undefined}
        className={cx(
          groupHeaderStyles.bar,
          index % 2 === 0 ? groupHeaderStyles.barEven : groupHeaderStyles.barOdd,
        )}
        onClick={onToggle}
      >
        <Icon
          color={cssVar.colorTextSecondary}
          icon={collapsed ? ChevronRightIcon : ChevronDownIcon}
          size={14}
        />
        {avatar ? (
          <Avatar avatar={avatar} size={20} />
        ) : color ? (
          <span
            style={{
              background: color,
              borderRadius: '50%',
              display: 'inline-block',
              height: 9,
              width: 9,
            }}
          />
        ) : null}
        <Text fontSize={13} weight={500}>
          {label}
        </Text>
        <Text fontSize={12} type={'secondary'}>
          {count}
        </Text>
      </Flexbox>
    );
  },
);

GroupHeader.displayName = 'AgentViewAllGroupHeader';

// Bucket tab label: name plus how many agents the tab would list, in the same
// muted-count style the group headers use.
const SegmentLabel = memo<{ count: number; label: string }>(({ count, label }) => (
  <Flexbox horizontal align={'center'} gap={6}>
    {label}
    <Text fontSize={12} type={'secondary'}>
      {count}
    </Text>
  </Flexbox>
));

SegmentLabel.displayName = 'AgentViewAllSegmentLabel';

const AgentViewAllPage = memo(() => {
  const { t } = useTranslation('common');
  const activeWorkspaceId = useActiveWorkspaceId();
  // `?tab=private` lands the page on the Private tab, so each sidebar
  // section's "View all" arrow opens its own bucket. The URL is the single
  // source of truth — the page stays mounted across same-route navigations
  // (Workspace-arrow while on the Private tab), so local state would go stale.
  const [searchParams, setSearchParams] = useSearchParams();
  const segment: SegmentValue = searchParams.get('tab') === 'private' ? 'private' : 'workspace';
  const handleSegmentChange = useCallback(
    (value: SegmentValue) => {
      setSearchParams(value === 'private' ? { tab: 'private' } : {}, { replace: true });
    },
    [setSearchParams],
  );
  const [keyword, setKeyword] = useState('');
  // While searching, sections open and empty folders drop out so matches show.
  const searching = keyword.trim().length > 0;

  // Card vs list rendering — persisted in systemStatus so the page reopens
  // in the last chosen mode (same mechanism as imageTopicViewMode & friends).
  const viewMode = useGlobalStore(systemStatusSelectors.agentListViewMode);
  const updateSystemStatus = useGlobalStore((s) => s.updateSystemStatus);
  const handleViewModeChange = useCallback(
    (mode: ViewMode) => updateSystemStatus({ agentListViewMode: mode }),
    [updateSystemStatus],
  );

  // Grouping / ordering / hidden-agent visibility — persisted alongside the
  // view mode so the page keeps its display config (same as taskListViewOptions).
  const rawViewOptions = useGlobalStore(systemStatusSelectors.agentListViewOptions);
  const viewOptions = useMemo(
    () => normalizeAgentListViewOptions(rawViewOptions, activeWorkspaceId ? 'folder' : 'none'),
    [activeWorkspaceId, rawViewOptions],
  );
  const setViewOptions = useCallback(
    (updater: (prev: AgentListViewOptions) => AgentListViewOptions) => {
      const next = normalizeAgentListViewOptions(updater(viewOptions));
      // The setting is shared by workspace and personal mode, so keep the
      // grouping unset until the member actually picks one; otherwise the
      // workspace's folder default would leak into personal mode.
      const keepDefaultGroupBy =
        rawViewOptions?.groupBy === undefined && next.groupBy === viewOptions.groupBy;
      updateSystemStatus(
        {
          agentListViewOptions: keepDefaultGroupBy ? { ...next, groupBy: undefined } : next,
        },
        'updateAgentListViewOptions',
      );
    },
    [rawViewOptions?.groupBy, updateSystemStatus, viewOptions],
  );

  // The sidebar usually owns these fetches, but this page must survive a
  // direct deep link — SWR dedupes when both are mounted.
  useFetchAgentList();
  useFetchAgentLabels();
  const useFetchWorkspaceUserPreference = useUserStore((s) => s.useFetchWorkspaceUserPreference);
  useFetchWorkspaceUserPreference();

  const isInit = useHomeStore(homeAgentListSelectors.isAgentListInit);
  const pinnedAgents = useHomeStore(homeAgentListSelectors.pinnedAgents, isEqual);
  const recommendedIds = useHomeStore(homeAgentListSelectors.recommendedAgentIds, isEqual);
  const agentGroups = useHomeStore(homeAgentListSelectors.agentGroups, isEqual);
  const ungroupedAgents = useHomeStore(homeAgentListSelectors.ungroupedAgents, isEqual);
  const privatePinnedAgents = useHomeStore(homeAgentListSelectors.privatePinnedAgents, isEqual);
  const privateAgentGroups = useHomeStore(homeAgentListSelectors.privateAgentGroups, isEqual);
  const privateUngroupedAgents = useHomeStore(
    homeAgentListSelectors.privateUngroupedAgents,
    isEqual,
  );

  const { isSidebarItemVisible, setSidebarItemVisible } = useSidebarItemVisibility();
  const keepGroups = useKeepSidebarGroupsListed();
  // In a workspace the sidebar lists only what the member pinned, created,
  // used recently or added; ask the sidebar's own layout instead of the
  // plain hide list.
  const workspaceSidebarIds = useWorkspaceSidebarItemIds();
  const isListed = useCallback(
    (item: SidebarAgentItem) =>
      workspaceSidebarIds ? workspaceSidebarIds.listed.has(item.id) : isSidebarItemVisible(item),
    [isSidebarItemVisible, workspaceSidebarIds],
  );

  const workspaceItems = useMemo(
    () => flattenAgentBuckets(pinnedAgents, agentGroups, ungroupedAgents),
    [pinnedAgents, agentGroups, ungroupedAgents],
  );
  const privateItems = useMemo(
    () => flattenAgentBuckets(privatePinnedAgents, privateAgentGroups, privateUngroupedAgents),
    [privatePinnedAgents, privateAgentGroups, privateUngroupedAgents],
  );

  const items = activeWorkspaceId && segment === 'private' ? privateItems : workspaceItems;

  // The workspace's recommended Agents, in the owners'/admins' order; ids of
  // rows that no longer exist (or turned private) are skipped.
  const recommendedItems = useMemo(() => {
    if (!activeWorkspaceId) return [];
    const byId = new Map(workspaceItems.map((item) => [item.id, item]));
    return recommendedIds.flatMap((id) => byId.get(id) ?? []);
  }, [activeWorkspaceId, recommendedIds, workspaceItems]);
  const { allowed: canEditRecommended } = usePermission('manage_settings');

  // "In sidebar" overview data: everything visible in the sidebar across BOTH
  // the workspace and private buckets — the block answers "what's in my
  // sidebar right now", so it ignores the tab and the search keyword (it
  // hides entirely while searching to keep results scannable).
  const sidebarItems = useMemo(() => {
    // Rebuilt from folder-filtered buckets rather than reusing the page's
    // flattened lists: hiding a Category removes its whole section from the
    // sidebar, so its agents are not "in sidebar" either. The page's own list
    // deliberately keeps showing them — hiding is a sidebar-only preference.
    const inSidebar = workspaceSidebarIds
      ? [...workspaceItems, ...privateItems]
      : [
          ...flattenAgentBuckets(pinnedAgents, keepGroups(agentGroups), ungroupedAgents),
          ...flattenAgentBuckets(
            privatePinnedAgents,
            keepGroups(privateAgentGroups),
            privateUngroupedAgents,
          ),
        ];
    const seen = new Set<string>();
    return inSidebar.filter((item) => {
      if (seen.has(item.id) || !isListed(item)) return false;
      seen.add(item.id);
      return true;
    });
  }, [
    agentGroups,
    isListed,
    keepGroups,
    pinnedAgents,
    privateAgentGroups,
    privateItems,
    privatePinnedAgents,
    privateUngroupedAgents,
    ungroupedAgents,
    workspaceItems,
    workspaceSidebarIds,
  ]);

  // Author info (column, grouping, sorting) only means something on the
  // workspace tab — every private item is the viewer's own.
  const showAuthor = !!activeWorkspaceId && segment !== 'private';

  // Creator column: resolve each item's userId against the member roster.
  const members = useWorkspaceMembers();
  const authorByUserId = useMemo(() => {
    const map = new Map<string, AgentRowAuthor>();
    for (const member of members) {
      const profile = member.user;
      if (!profile) continue;
      map.set(member.userId, {
        avatar: profile.avatar,
        name:
          getWorkspaceMemberNickname(member) ||
          profile.fullName ||
          profile.username ||
          profile.email ||
          undefined,
      });
    }
    return map;
  }, [members]);

  // A persisted author sort/grouping can outlive the tab that offers it
  // (picked on the workspace tab, then the user opens Private where the
  // author controls are hidden) — coerce back to visible defaults instead of
  // sorting by an invisible key with a select value that has no option.
  const { orderDirection, showSidebarHidden } = viewOptions;

  // Tab counts describe how big each bucket is, so they follow the persisted
  // hidden-agent setting (what the tab would list) but ignore the keyword —
  // searching narrows the list below, it doesn't shrink the buckets.
  const bucketCounts = useMemo(() => {
    const count = (list: SidebarAgentItem[]) =>
      showSidebarHidden ? list.length : list.filter(isListed).length;
    return { private: count(privateItems), workspace: count(workspaceItems) };
  }, [workspaceItems, privateItems, showSidebarHidden, isListed]);

  // Label grouping works on every tab (labels exist in personal mode too);
  // author grouping only makes sense where the author column shows.
  const groupBy =
    showAuthor || viewOptions.groupBy === 'label'
      ? viewOptions.groupBy
      : viewOptions.groupBy === 'author'
        ? 'none'
        : viewOptions.groupBy;
  const orderBy =
    !showAuthor && viewOptions.orderBy === 'author' ? 'updatedAt' : viewOptions.orderBy;
  const effectiveViewOptions = useMemo(
    () => ({ ...viewOptions, groupBy, orderBy }),
    [viewOptions, groupBy, orderBy],
  );

  const filteredItems = useMemo(() => {
    // Case and spacing are ignored, so "lobeai" still finds "Lobe AI".
    const fold = (value?: string | null) => value?.toLowerCase().replaceAll(/\s+/g, '') ?? '';
    const query = fold(keyword);
    let matched = query
      ? items.filter(
          (item) =>
            fold(item.name).includes(query) ||
            fold(item.title).includes(query) ||
            fold(item.description).includes(query),
        )
      : items;

    if (!showSidebarHidden) {
      matched = matched.filter(isListed);
    }

    const authorName = (item: SidebarAgentItem) =>
      (item.userId && authorByUserId.get(item.userId)?.name) || '';

    const direction = orderDirection === 'asc' ? 1 : -1;
    return [...matched].sort((a, b) => {
      // Sort on the label the rows actually render, not the raw title — otherwise
      // a list of personal names comes back ordered by their hidden roles.
      if (orderBy === 'title')
        return direction * agentDisplayName(a, '').localeCompare(agentDisplayName(b, ''));
      if (orderBy === 'author') return direction * authorName(a).localeCompare(authorName(b));
      return direction * (dayjs(a.updatedAt).valueOf() - dayjs(b.updatedAt).valueOf());
    });
  }, [items, keyword, orderBy, orderDirection, showSidebarHidden, isListed, authorByUserId]);

  // Author sections (workspace tab only — every private item is the viewer's
  // own, so author buckets would be a single redundant group): items are
  // already sorted, so buckets keep the in-group order; groups themselves
  // read alphabetically.
  const groupedItems = useMemo(() => {
    if (groupBy === 'author') {
      if (!activeWorkspaceId || segment === 'private') return null;
      const buckets = new Map<string, SidebarAgentItem[]>();
      for (const item of filteredItems) {
        const key = item.userId ?? '';
        const bucket = buckets.get(key);
        if (bucket) bucket.push(item);
        else buckets.set(key, [item]);
      }
      const groups = [...buckets.entries()].map(([userId, groupItems]) => ({
        // Same fallback as the row avatars — a member without a profile
        // avatar still gets the default one instead of a bare name.
        avatar: (userId && authorByUserId.get(userId)?.avatar) || DEFAULT_USER_AVATAR_URL,
        color: undefined as string | null | undefined,
        items: groupItems,
        key: `author:${userId || 'unknown'}`,
        label:
          (userId && authorByUserId.get(userId)?.name) || t('agentViewAll.groupBy.unknownAuthor'),
      }));
      return groups.sort((a, b) => a.label.localeCompare(b.label));
    }

    if (groupBy === 'folder') {
      const folders = segment === 'private' ? privateAgentGroups : agentGroups;
      const sections = groupItemsByFolder(
        filteredItems,
        activeWorkspaceId ? folders : [...agentGroups, ...privateAgentGroups],
        t('agentViewAll.groupBy.noFolder'),
      ).filter((section) => !searching || section.items.length > 0);
      // Without any folder the grouping would be a single bucket: stay flat.
      if (sections.every((section) => !section.folder)) return null;

      return sections.map((section) => ({
        avatar: null as string | null,
        color: undefined as string | null | undefined,
        items: section.items,
        key: section.key,
        label: section.label,
      }));
    }

    if (groupBy === 'label') {
      // Label sections: an agent carrying several labels appears once per
      // label (mirrors Linear); unlabeled items collect in a trailing bucket.
      const buckets = new Map<
        string,
        { color?: string | null; items: SidebarAgentItem[]; label: string }
      >();
      const unlabeled: SidebarAgentItem[] = [];
      for (const item of filteredItems) {
        const itemLabels = item.labels ?? [];
        if (itemLabels.length === 0) {
          unlabeled.push(item);
          continue;
        }
        for (const label of itemLabels) {
          const bucket = buckets.get(label.id);
          if (bucket) bucket.items.push(item);
          else buckets.set(label.id, { color: label.color, items: [item], label: label.name });
        }
      }
      const groups = [...buckets.entries()]
        .map(([labelId, bucket]) => ({
          avatar: null as string | null,
          color: bucket.color,
          items: bucket.items,
          key: `label:${labelId}`,
          label: bucket.label,
        }))
        .sort((a, b) => a.label.localeCompare(b.label));
      if (unlabeled.length > 0)
        groups.push({
          avatar: null,
          color: undefined,
          items: unlabeled,
          key: 'label:none',
          label: t('agentViewAll.groupBy.noLabel'),
        });
      return groups;
    }

    return null;
  }, [
    activeWorkspaceId,
    agentGroups,
    groupBy,
    privateAgentGroups,
    searching,
    segment,
    filteredItems,
    authorByUserId,
    t,
  ]);

  // Group collapse state — groups default to COLLAPSED (mirrors Linear), so
  // the EXPANDED keys are what persists (systemStatus): a reload keeps opened
  // groups open, and newly appearing groups start collapsed.
  const expandedGroupKeys = useGlobalStore(
    systemStatusSelectors.agentListExpandedGroupKeys,
    isEqual,
  );
  const expandedGroupSet = useMemo(() => new Set(expandedGroupKeys), [expandedGroupKeys]);
  const toggleGroupCollapsed = useCallback(
    (key: string) => {
      const next = expandedGroupSet.has(key)
        ? expandedGroupKeys.filter((item) => item !== key)
        : [...expandedGroupKeys, key];
      updateSystemStatus({ agentListExpandedGroupKeys: next }, 'toggleAgentListGroupExpanded');
    },
    [expandedGroupKeys, expandedGroupSet, updateSystemStatus],
  );

  const handleToggleSidebar = useCallback(
    async (item: SidebarAgentItem) => {
      try {
        await setSidebarItemVisible(item.id, !isListed(item));
      } catch (error) {
        // Personal mode writes the preference optimistically and never rolls
        // back, workspace mode rolls back silently — either way the row's
        // state stops matching what was saved, so say so.
        console.error('Failed to toggle Agent sidebar visibility:', error);
        toast.error(t('operationFailed'));
      }
    },
    [isListed, setSidebarItemVisible, t],
  );

  const renderCard = useCallback(
    (item: SidebarAgentItem) => (
      <AgentCard
        item={item}
        key={item.id}
        owner={showAuthor && item.userId ? authorByUserId.get(item.userId) : undefined}
        sidebarHidden={!isListed(item)}
        onToggleSidebar={handleToggleSidebar}
      />
    ),
    [showAuthor, authorByUserId, handleToggleSidebar, isListed],
  );

  const renderRow = useCallback(
    (item: SidebarAgentItem) => (
      <AgentRow
        author={item.userId ? authorByUserId.get(item.userId) : undefined}
        item={item}
        key={item.id}
        showAuthor={showAuthor}
        sidebarHidden={!isListed(item)}
        onToggleSidebar={handleToggleSidebar}
      />
    ),
    [showAuthor, authorByUserId, handleToggleSidebar, isListed],
  );

  const { allowed: canCreate, reason: createBlockedReason } = usePermission('create_content');
  const {
    createAgentMenuItem,
    createConnectAgentMenuItem,
    createGroupChatMenuItem,
    createMarketAgentMenuItem,
  } = useCreateMenuItems();

  // Each tab starts creation in its own bucket, so the new row appears in the
  // list the user is currently looking at. The create dialogs still show the
  // choice and let the user change it.
  const createOptions = useMemo(
    () =>
      activeWorkspaceId
        ? { visibility: segment === 'private' ? ('private' as const) : ('public' as const) }
        : undefined,
    [activeWorkspaceId, segment],
  );

  // Same menu as the sidebar's create button: agent / group chat / external
  // CLI agents / platform agent, all inheriting the segment's visibility. The
  // sidebar's "add from Agent list" entry is omitted — it navigates to this
  // very page.
  const createMenuItems = useMemo(() => {
    const connectItem = createConnectAgentMenuItem(createOptions);
    return [
      createAgentMenuItem(createOptions),
      createGroupChatMenuItem(createOptions),
      ...(connectItem ? [{ type: 'divider' as const }, connectItem] : []),
      { type: 'divider' as const },
      createMarketAgentMenuItem(),
    ];
  }, [
    createAgentMenuItem,
    createConnectAgentMenuItem,
    createGroupChatMenuItem,
    createMarketAgentMenuItem,
    createOptions,
  ]);

  return (
    <Flexbox flex={1} height={'100%'}>
      <NavHeader
        left={
          <Text style={{ paddingInlineStart: 4 }} weight={500}>
            {t('agentViewAll.title')}
          </Text>
        }
        right={
          <ListConfig
            options={effectiveViewOptions}
            setOptions={setViewOptions}
            setViewMode={handleViewModeChange}
            showAuthor={showAuthor}
            viewMode={viewMode}
          />
        }
      />
      <WideScreenContainer
        fullWidth
        gap={16}
        paddingBlock={16}
        paddingInline={16}
        wrapperStyle={{ flex: 1, overflowY: 'auto' }}
      >
        {isInit &&
          !keyword.trim() &&
          (recommendedItems.length > 0 || (!!activeWorkspaceId && canEditRecommended)) && (
            <RecommendedAgentsSection
              canEdit={canEditRecommended}
              isListed={isListed}
              items={recommendedItems}
              onToggleSidebar={handleToggleSidebar}
            />
          )}
        {!isInit && !keyword.trim() && <AgentViewAllSidebarSectionSkeleton />}
        {isInit && !keyword.trim() && sidebarItems.length > 0 && (
          <SidebarAgentsSection items={sidebarItems} onToggleSidebar={handleToggleSidebar} />
        )}
        <Flexbox horizontal align={'center'} gap={12} justify={'space-between'}>
          {/* The workspace/private split only exists inside a workspace;
              personal mode leads with the search box instead. */}
          {activeWorkspaceId ? (
            <Segmented
              value={segment}
              options={[
                {
                  label: (
                    <SegmentLabel
                      count={bucketCounts.workspace}
                      label={t('navPanel.publicAgents')}
                    />
                  ),
                  value: 'workspace',
                },
                {
                  label: (
                    <SegmentLabel
                      count={bucketCounts.private}
                      label={t('navPanel.privateAgents')}
                    />
                  ),
                  value: 'private',
                },
              ]}
              onChange={(value) => handleSegmentChange(value as SegmentValue)}
            />
          ) : (
            <SearchBar
              allowClear
              placeholder={t('navPanel.searchAgent')}
              style={{ maxWidth: 240 }}
              value={keyword}
              onChange={(e) => setKeyword(e.target.value)}
            />
          )}
          <Flexbox horizontal align={'center'} gap={8}>
            {activeWorkspaceId && (
              <SearchBar
                allowClear
                placeholder={t('navPanel.searchAgent')}
                style={{ maxWidth: 240 }}
                value={keyword}
                onChange={(e) => setKeyword(e.target.value)}
              />
            )}
            {canCreate ? (
              <DropdownMenu items={createMenuItems}>
                <Button icon={PlusIcon}>
                  <Icon icon={ChevronDownIcon} size={14} />
                </Button>
              </DropdownMenu>
            ) : (
              <Tooltip title={createBlockedReason}>
                <Button disabled icon={PlusIcon}>
                  <Icon icon={ChevronDownIcon} size={14} />
                </Button>
              </Tooltip>
            )}
          </Flexbox>
        </Flexbox>
        {!isInit ? (
          <AgentViewAllRowsSkeleton />
        ) : filteredItems.length === 0 ? (
          <Center flex={1} padding={40}>
            <Empty
              description={
                keyword.trim() ? t('navPanel.searchResultEmpty') : t('agentViewAll.empty')
              }
            />
          </Center>
        ) : viewMode === 'card' ? (
          groupedItems ? (
            <Flexbox gap={8}>
              {groupedItems.map((group, index) => {
                const collapsed = !searching && !expandedGroupSet.has(group.key);
                return (
                  <Flexbox gap={12} key={group.key}>
                    <GroupHeader
                      avatar={group.avatar}
                      collapsed={collapsed}
                      color={group.color}
                      count={group.items.length}
                      index={index}
                      label={group.label}
                      onToggle={() => toggleGroupCollapsed(group.key)}
                    />
                    {!collapsed && <AgentCardGrid>{group.items.map(renderCard)}</AgentCardGrid>}
                  </Flexbox>
                );
              })}
            </Flexbox>
          ) : (
            <AgentCardGrid>{filteredItems.map(renderCard)}</AgentCardGrid>
          )
        ) : (
          // Grouped list shares the card branch's wrapper rhythm (outer gap 8,
          // bare GroupHeader) so toggling the view mode doesn't shift the bars.
          <Flexbox gap={groupedItems ? 8 : 2}>
            {groupedItems
              ? groupedItems.map((group, index) => {
                  const collapsed = !searching && !expandedGroupSet.has(group.key);
                  return (
                    <Flexbox gap={2} key={group.key}>
                      <GroupHeader
                        avatar={group.avatar}
                        collapsed={collapsed}
                        color={group.color}
                        count={group.items.length}
                        index={index}
                        label={group.label}
                        onToggle={() => toggleGroupCollapsed(group.key)}
                      />
                      {!collapsed && group.items.map(renderRow)}
                    </Flexbox>
                  );
                })
              : filteredItems.map(renderRow)}
          </Flexbox>
        )}
      </WideScreenContainer>
    </Flexbox>
  );
});

AgentViewAllPage.displayName = 'AgentViewAllPage';

// The create menu prefers the wizard modal (`openCreateModal`) over blind
// blank-agent creation, and that modal lives in AgentModalContext — normally
// mounted by the Home layout, which this standalone route is NOT inside. Wrap
// the page so the "+" menu opens the same create wizard as the sidebar.
const AgentViewAllPageWithModals = () => (
  <AgentModalProvider>
    <AgentViewAllPage />
  </AgentModalProvider>
);

export default AgentViewAllPageWithModals;