🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
'use client';
import { agentDisplayName, getOptedInPluginIds } from '@lobechat/types';
import {
ActionIcon,
createStaticStyles,
Flexbox,
Icon,
Popover,
Skeleton,
Text,
} from '@lobehub/ui';
import { SkillsIcon } from '@lobehub/ui/icons';
import { BookOpen, FileText, Settings, SquareTerminal, UserRound, X } from 'lucide-react';
import { memo, type PropsWithChildren, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useFetchWorkspaceMembers } from '@/business/client/hooks/useFetchWorkspaceMembers';
import { ModelIcon } from '@/components/LobeIcons';
import { ArticleSkeleton } from '@/components/Skeleton';
import UserAvatar from '@/components/UserAvatar';
import { stopCardClick } from '@/features/MemberProfileCard/stopCardClick';
import { useWorkspaceMemberProfile } from '@/features/MemberProfileCard/useWorkspaceMemberProfile';
import ModelSelect from '@/features/ModelSelect';
import { useResourceAccess } from '@/features/ResourcePermission/useResourceAccess';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { usePermission } from '@/hooks/usePermission';
import { useAgentGroupStore } from '@/store/agentGroup';
import { useHomeStore } from '@/store/home';
import { homeAgentListSelectors } from '@/store/home/selectors';
import { densityVars } from '@/styles/densityVars';
import AgentProfileCard from '.';
import { resolveAgentRuntimeLabel } from './runtime';
import {
type AgentProfilePreview as AgentPreview,
useAgentProfileDetail,
} from './useAgentProfileDetail';
import { useAutoClosePopup } from './useAutoClosePopup';
const styles = createStaticStyles(({ css, cssVar }) => ({
closeButton: css`
position: absolute;
z-index: 1;
inset-block-start: 8px;
inset-inline-end: 8px;
color: ${cssVar.colorText};
opacity: 0;
filter: drop-shadow(0 0 2px ${cssVar.colorBgElevated});
transition: opacity ${cssVar.motionDurationMid} ${cssVar.motionEaseOut};
@media (hover: none) {
opacity: 1;
}
`,
content: css`
position: relative;
&:hover [data-profile-card-close],
&:focus-within [data-profile-card-close] {
opacity: 1;
}
`,
// A replaced card (another card opened, its trigger was pressed, the route changed)
// leaves at once instead of fading out over whatever replaced it.
instantClose: css`
&[data-ending-style],
&[data-closed] {
visibility: hidden;
transition: none;
}
`,
footer: css`
padding-block: ${densityVars.contentGap};
padding-inline: ${densityVars.cardPadding};
border-block-start: 1px solid ${cssVar.colorBorderSecondary};
`,
section: css`
padding-block: ${densityVars.contentGap};
padding-inline: ${densityVars.cardPadding};
`,
sectionTitle: css`
margin-block-end: 8px;
font-size: 11px;
font-weight: 600;
color: ${cssVar.colorTextTertiary};
`,
statItem: css`
color: ${cssVar.colorTextSecondary};
`,
trigger: css`
border-radius: ${cssVar.borderRadius};
&[data-popup-open] {
background: ${cssVar.colorFillTertiary};
}
`,
}));
interface AgentProfilePopupProps extends PropsWithChildren {
/** Prefilled data for instant render; fetched data overrides once loaded. */
agent?: Partial<AgentPreview>;
agentId: string;
/** When set, enables group-specific actions (settings nav + model change). */
groupId?: string;
/** Where the card opens; defaults to above a hover trigger and beside a click one. */
placement?: 'top' | 'right' | 'bottom' | 'left';
trigger?: 'click' | 'hover';
}
const AgentProfilePopup = memo<AgentProfilePopupProps>(
({ agent, agentId, groupId, children, placement, trigger = 'click' }) => {
const { t } = useTranslation('chat');
const navigate = useWorkspaceAwareNavigate();
const [open, setOpen] = useState(false);
const { detached, instant, onOpenChange, triggerProps } = useAutoClosePopup({
open,
setOpen,
trigger,
});
const [loading, setLoading] = useState(false);
const { allowed: canEditContent } = usePermission('edit_own_content');
const { canEditResource: canEditAgent, isAccessResolved: isAgentAccessResolved } =
useResourceAccess('agent', open ? agentId : undefined);
const { canEditResource: canEditGroup, isAccessResolved: isGroupAccessResolved } =
useResourceAccess('agentGroup', open ? groupId : undefined);
const canConfigure =
canEditContent &&
isAgentAccessResolved &&
canEditAgent &&
(!groupId || (isGroupAccessResolved && canEditGroup));
const updateMemberAgentConfig = useAgentGroupStore((s) => s.updateMemberAgentConfig);
const listEntry = useHomeStore(homeAgentListSelectors.getAgentById(agentId));
// In a workspace, the member who created the Agent is the one to ask about it.
// Load the member directory only once the card opens, whatever page hosts it.
useFetchWorkspaceMembers({ enabled: open && !!listEntry?.userId });
const creator = useWorkspaceMemberProfile(listEntry?.userId);
const { detail: fetched, isLoading } = useAgentProfileDetail(agentId, open && !!canConfigure);
const runtimeLabel = resolveAgentRuntimeLabel({
fetchedType: fetched?.agencyConfig?.heterogeneousProvider?.type,
listEntry,
});
const isExternal = !!runtimeLabel;
const merged: Partial<AgentPreview> = {
avatar: fetched?.avatar ?? agent?.avatar,
backgroundColor: fetched?.backgroundColor ?? agent?.backgroundColor,
description: fetched?.description ?? agent?.description,
model: fetched?.model ?? agent?.model,
name: fetched?.name ?? agent?.name,
provider: fetched?.provider ?? agent?.provider,
title: fetched?.title ?? agent?.title,
};
const handleModelChange = async (props: { model: string; provider: string }) => {
if (!groupId || !canConfigure) return;
setLoading(true);
try {
await updateMemberAgentConfig(groupId, agentId, {
model: props.model,
provider: props.provider,
});
} finally {
setLoading(false);
}
};
const handleSettings = () => {
if (!groupId || !canConfigure) return;
if (!canConfigure) return;
setOpen(false);
navigate(`/group/${groupId}/profile?tab=${agentId}`);
};
const handleHeaderClick = () => {
setOpen(false);
navigate(`/agent/${agentId}/profile`);
};
const hasDisplay = Boolean(agentDisplayName(merged) || merged.avatar || merged.description);
const showSkeleton = !hasDisplay && isLoading;
const pluginCount = getOptedInPluginIds(fetched?.plugins).length;
const knowledgeCount = fetched?.knowledgeBases?.length ?? 0;
const fileCount = fetched?.files?.length ?? 0;
const hasStats = pluginCount > 0 || knowledgeCount > 0 || fileCount > 0;
const footerLoading = canConfigure && !groupId && isLoading && !fetched;
// An external agent picks its own model inside its CLI, so the LobeHub model
// on its config is not what runs it: name the runtime instead, and never
// offer a model switch that would not take effect.
const modelSection =
canConfigure && groupId && !isExternal ? (
merged.model && (
<Flexbox className={styles.section} gap={4}>
<div className={styles.sectionTitle}>{t('groupSidebar.agentProfile.model')}</div>
<ModelSelect
loading={loading}
value={{ model: merged.model, provider: merged.provider ?? undefined }}
onChange={handleModelChange}
/>
</Flexbox>
)
) : footerLoading && !isExternal ? (
<Flexbox horizontal align={'center'} className={styles.footer} gap={14}>
<Skeleton height={16} width={90} />
<Skeleton height={16} width={60} />
</Flexbox>
) : isExternal || (canConfigure && (merged.model || hasStats)) ? (
<Flexbox horizontal align={'center'} className={styles.footer} gap={14} wrap={'wrap'}>
{isExternal ? (
<Flexbox horizontal align={'center'} className={styles.statItem} gap={6}>
<Icon icon={SquareTerminal} size={14} />
<Text fontSize={12} type={'secondary'}>
{t('agentProfile.runtime', { name: runtimeLabel })}
</Text>
</Flexbox>
) : (
merged.model && (
<Flexbox horizontal align={'center'} className={styles.statItem} gap={6}>
<ModelIcon model={merged.model} size={14} />
<Text fontSize={12} type={'secondary'}>
{merged.model}
</Text>
</Flexbox>
)
)}
{canConfigure && pluginCount > 0 && (
<Flexbox horizontal align={'center'} className={styles.statItem} gap={4}>
<Icon icon={SkillsIcon} size={13} />
<Text fontSize={12} type={'secondary'}>
{t('agentProfile.skills', { count: pluginCount })}
</Text>
</Flexbox>
)}
{canConfigure && knowledgeCount > 0 && (
<Flexbox horizontal align={'center'} className={styles.statItem} gap={4}>
<Icon icon={BookOpen} size={13} />
<Text fontSize={12} type={'secondary'}>
{t('agentProfile.knowledgeBases', { count: knowledgeCount })}
</Text>
</Flexbox>
)}
{canConfigure && fileCount > 0 && (
<Flexbox horizontal align={'center'} className={styles.statItem} gap={4}>
<Icon icon={FileText} size={13} />
<Text fontSize={12} type={'secondary'}>
{t('agentProfile.files', { count: fileCount })}
</Text>
</Flexbox>
)}
</Flexbox>
) : null;
const creatorSection = creator ? (
<Flexbox horizontal align={'center'} className={styles.footer} gap={8}>
<Icon className={styles.statItem} icon={UserRound} size={14} />
<Text fontSize={12} type={'secondary'}>
{t('agentProfile.creator')}
</Text>
<Flexbox horizontal align={'center'} gap={6} style={{ minWidth: 0 }}>
<UserAvatar avatar={creator.avatar} name={creator.name} shape={'circle'} size={18} />
<Text ellipsis fontSize={12}>
{creator.name}
</Text>
</Flexbox>
</Flexbox>
) : null;
const content = showSkeleton ? (
<div style={{ padding: 16, width: 280 }}>
<ArticleSkeleton avatar rows={2} />
</div>
) : (
<AgentProfileCard
agentId={agentId}
avatar={merged.avatar}
backgroundColor={merged.backgroundColor}
description={merged.description}
loading={isLoading && !merged.description}
title={agentDisplayName(merged, t('defaultSession', { ns: 'common' }))}
headerAction={
groupId && canConfigure ? (
<Flexbox horizontal align="center" justify="flex-end" style={{ paddingBlockStart: 0 }}>
<ActionIcon
icon={Settings}
size="small"
title={t('groupSidebar.agentProfile.settings')}
onClick={handleSettings}
/>
</Flexbox>
) : undefined
}
onHeaderClick={canConfigure ? handleHeaderClick : undefined}
>
{creatorSection}
{modelSection}
</AgentProfileCard>
);
return (
<Popover
className={instant ? styles.instantClose : undefined}
classNames={trigger === 'click' ? { trigger: styles.trigger } : undefined}
nativeButton={false}
open={open}
placement={placement ?? (trigger === 'hover' ? 'top' : 'right')}
trigger={trigger}
triggerProps={triggerProps}
content={
// A hidden or unmounted trigger takes the whole popover with it (see useAutoClosePopup).
detached ? null : (
<div className={styles.content} role="presentation" onClick={stopCardClick}>
{content}
<ActionIcon
data-profile-card-close
aria-label={t('close', { ns: 'common' })}
className={styles.closeButton}
icon={X}
size={'small'}
title={t('close', { ns: 'common' })}
onClick={() => setOpen(false)}
/>
</div>
)
}
styles={{
content: { borderRadius: 12, overflow: 'hidden', padding: 0 },
}}
onOpenChange={onOpenChange}
>
{children}
</Popover>
);
},
);
export default AgentProfilePopup;