import { FocusEvent, KeyboardEvent, ReactNode, Ref } from 'react';
import {
AgentAvatar,
Icon,
Panel,
ProgressBar,
StatusBadge,
} from '@pantheon-systems/pds-toolkit-react';
import { IconButton } from '../IconButton/IconButton';
import {
AgentTreeAgent,
AgentTreeLabelStrings,
AgentTreeStatus,
fill,
formatTokens,
percent,
tokensUsed,
useElapsed,
} from './agent-tree-utils';
export interface AgentTreeCardProps {
/**
* The agent this card shows.
*/
agent: AgentTreeAgent;
/**
* How many handoffs have reached this agent. Each new value runs the AI
* gradient down the card's border, from the top to the bottom, as it fades.
*/
arrival?: number;
/**
* The details, shown inside the card under the summary while it is open.
*/
children?: ReactNode;
/**
* Id of the details region the summary controls.
*/
detailsId: string;
/**
* Depth in the tree: 0 the lead, 1 a sub-agent, 2 and more a helper. Helpers
* show a smaller card.
*/
depth: number;
/**
* Slide the AI gradient around the border of a working agent.
*/
isAnimated?: boolean;
/**
* Formats how long the agent has run.
*/
formatDuration: (milliseconds: number) => string;
/**
* Whether this card is the one tab stop of the tree.
*/
isCurrent: boolean;
/**
* Whether the details of this agent are open.
*/
isOpen: boolean;
/**
* Translatable strings.
*/
labelStrings: AgentTreeLabelStrings;
/**
* Called when the card receives focus.
*/
onFocus: (event: FocusEvent<HTMLDivElement>) => void;
/**
* Called on a key press on the card.
*/
onKeyDown: (event: KeyboardEvent<HTMLDivElement>) => void;
/**
* Called on a click on the card.
*/
onPick: () => void;
/**
* Position among its siblings, from 1.
*/
position: number;
/**
* Ref of the card element, used to measure the connectors.
*/
ref: Ref<HTMLDivElement>;
/**
* How many siblings it has, itself included.
*/
setSize: number;
/**
* Ref of the summary, the tree item the keyboard focuses.
*/
summaryRef: Ref<HTMLDivElement>;
}
const statusTypes: Record<
AgentTreeStatus,
'critical' | 'discovery' | 'frozen' | 'neutral' | 'success' | 'working'
> = {
active: 'working',
done: 'success',
failed: 'critical',
idle: 'neutral',
paused: 'frozen',
waiting: 'discovery',
};
export const statusLabel = (
status: AgentTreeStatus,
labelStrings: AgentTreeLabelStrings,
) => {
const labels: Record<AgentTreeStatus, string> = {
active: labelStrings.statusActive,
done: labelStrings.statusDone,
failed: labelStrings.statusFailed,
idle: labelStrings.statusIdle,
paused: labelStrings.statusPaused,
waiting: labelStrings.statusWaiting,
};
return labels[status];
};
/**
* One agent: avatar, name, status, what it does, its context window, its tools
* and the last thing it was handed. It is a tree item, so the tree walks it
* with the arrow keys.
*/
export const AgentTreeCard = ({
agent,
arrival,
children,
depth,
detailsId,
formatDuration,
isAnimated = false,
isCurrent,
isOpen,
labelStrings,
onFocus,
onKeyDown,
onPick,
position,
ref,
setSize,
summaryRef,
}: AgentTreeCardProps) => {
const baseClass = 'pds-agent-tree';
const isSmall = depth >= 2;
const isActive = agent.status === 'active';
const contextPercent = percent(agent.context);
const status = statusLabel(agent.status, labelStrings);
const tools = agent.tools ?? [];
const shownTools = tools.length > 4 ? tools.slice(0, 3) : tools;
const { time } = useElapsed(agent, formatDuration);
const accessibleName = [
`${agent.name}: ${status}`,
agent.activity,
agent.context !== undefined
? fill(labelStrings.contextValue, { count: contextPercent })
: null,
]
.filter(Boolean)
.join(', ');
return (
<div
className={`${baseClass}__node`}
data-open={isOpen ? '' : undefined}
data-small={isSmall ? '' : undefined}
data-status={agent.status}
ref={ref}
>
<Panel
className={`${baseClass}__card`}
isAnimated={isAnimated}
padding='none'
variant={isActive ? 'agentic-default' : 'default'}
>
<div
aria-controls={isOpen ? detailsId : undefined}
aria-label={accessibleName}
aria-level={depth + 1}
aria-posinset={position}
aria-selected={isOpen}
aria-setsize={setSize}
className={`${baseClass}__item`}
onClick={onPick}
onFocus={onFocus}
onKeyDown={onKeyDown}
ref={summaryRef}
role='treeitem'
tabIndex={isCurrent ? 0 : -1}
>
<span className={`pds-copy-s ${baseClass}__title-row`}>
<AgentAvatar
className={`${baseClass}__avatar`}
hasHalo={isActive}
iconName={agent.iconName ?? 'zappy'}
label={
agent.iconName === 'sparklesSolid'
? labelStrings.agentThirdParty
: labelStrings.agentZappy
}
size='s'
/>
<span className={`${baseClass}__heading`}>
<span className={`${baseClass}__name`} title={agent.name}>
{agent.name}
</span>
{agent.model && !isSmall && (
<span
className={`pds-copy-2xs ${baseClass}__subtitle`}
title={agent.model}
>
{agent.model}
</span>
)}
</span>
<Icon
className={`${baseClass}__chevron`}
iconName='angleDown'
size='xs'
/>
</span>
<p
className={`pds-copy-xs ${baseClass}__activity`}
data-live={isActive ? '' : undefined}
title={agent.activity}
>
{agent.activity}
</p>
{agent.context !== undefined && (
<div className={`pds-copy-2xs ${baseClass}__context`}>
<ProgressBar
className={`${baseClass}__meter`}
colorBreakpoints={[
{ colorType: 'info', percentage: 0 },
{ colorType: 'warning', percentage: 80 },
{ colorType: 'critical', percentage: 95 },
]}
label={labelStrings.context}
percentage={contextPercent}
size='s'
/>
<span className={`${baseClass}__tokens`}>
{agent.contextLimit
? `${formatTokens(tokensUsed(agent))} / ${formatTokens(agent.contextLimit)}`
: `${contextPercent}%`}
</span>
</div>
)}
<span className={`pds-copy-xs ${baseClass}__foot`}>
<span className={`${baseClass}__state`}>
<StatusBadge
hasStatusIndicator
label={status}
statusIndicatorText={labelStrings.statusIndicator}
statusType={statusTypes[agent.status]}
/>
{time && <span className={`${baseClass}__time`}>{time}</span>}
</span>
{!isSmall && tools.length > 0 && (
<span className={`${baseClass}__tools`}>
{shownTools.map((tool) => (
<IconButton
ariaLabel={tool.name}
hasBorder={false}
iconName={tool.iconName}
key={tool.name}
size='2xs'
tabIndex={-1}
/>
))}
{tools.length > 4 && (
<span
aria-hidden='true'
className={`${baseClass}__tools-more`}
>
+{tools.length - 3}
</span>
)}
</span>
)}
</span>
{!isSmall && agent.last && (
<span
className={`pds-copy-2xs ${baseClass}__last`}
title={`${agent.last.from}: ${agent.last.text}`}
>
<Icon iconName='arrowRight' size='xs' />
<b>{agent.last.from}</b>
<span>{agent.last.text}</span>
</span>
)}
</div>
{children}
</Panel>
{arrival ? (
<span
aria-hidden='true'
className={`${baseClass}__arrival`}
key={arrival}
>
<span className={`${baseClass}__arrival-ring`} />
</span>
) : null}
</div>
);
};
Context: AgentTree
A tree of agent cards: the lead on top, its sub-agents side by side below, their helpers under them, joined by thin connectors.
Net-new componentOpen in Storybook