import { MouseEvent } from 'react';
import { Badge, Button } from '@pantheon-systems/pds-toolkit-react';
import { IconButton } from '../IconButton/IconButton';
import {
AgentTreeAgent,
AgentTreeLabelStrings,
AgentTreeSteer,
fill,
formatTokens,
percent,
realCost,
tokensUsed,
useElapsed,
} from './agent-tree-utils';
export interface AgentTreeDetailsProps {
/**
* The agent the details belong to.
*/
agent: AgentTreeAgent;
/**
* Formats the cost.
*/
formatCost: (amount: number) => string;
/**
* Formats the time it ran.
*/
formatDuration: (milliseconds: number) => string;
/**
* Translatable strings.
*/
labelStrings: AgentTreeLabelStrings;
/**
* Id of the region, which the card's summary controls.
*/
id: string;
/**
* Called when the details close with Escape.
*/
onClose: () => void;
/**
* Called when the person presses the message button, which opens the floating
* message bar.
*/
onMessage: (event: MouseEvent<HTMLElement>) => void;
/**
* Called with what the person tells the agent. Without it the steering row
* does not show.
*/
onSteer?: (action: AgentTreeSteer) => void;
}
type Action = {
action: AgentTreeSteer;
label: string;
variant: 'primary' | 'secondary';
};
/**
* The details of one agent, as wide as its card: what it was asked, its model,
* how long it ran, what it cost and its tokens, its tools, and what the person
* can tell it. It sits in the top layer under the card, so it never pushes the
* page around or gets clipped.
*/
export const AgentTreeDetails = ({
agent,
formatCost,
formatDuration,
id,
labelStrings,
onClose,
onMessage,
onSteer,
}: AgentTreeDetailsProps) => {
const baseClass = 'pds-agent-tree__details';
// What fits the agent's state: approve or decline when it waits, pause or
// resume, stop, retry, and always a message.
const actions: Action[] =
agent.status === 'waiting'
? [
{
action: 'decline',
label: labelStrings.decline,
variant: 'secondary',
},
{
action: 'approve',
label: labelStrings.approve,
variant: 'primary',
},
]
: agent.status === 'active'
? [
{
action: 'pause',
label: labelStrings.pause,
variant: 'secondary',
},
{ action: 'stop', label: labelStrings.stop, variant: 'secondary' },
]
: agent.status === 'paused'
? [
{
action: 'stop',
label: labelStrings.stop,
variant: 'secondary',
},
{
action: 'resume',
label: labelStrings.resume,
variant: 'primary',
},
]
: agent.status === 'failed'
? [
{
action: 'retry',
label: labelStrings.retry,
variant: 'primary',
},
]
: [];
const { isRunning, time } = useElapsed(agent, formatDuration);
const cost = realCost(agent.cost);
const tools = agent.tools ?? [];
return (
<div
aria-label={fill(labelStrings.details, { name: agent.name })}
className={baseClass}
id={id}
onKeyDown={(event) => {
if (event.key === 'Escape') {
event.stopPropagation();
onClose();
}
}}
role='region'
>
{agent.task && (
<p className={`pds-copy-xs ${baseClass}-task`} title={agent.task}>
{agent.task}
</p>
)}
{agent.model && (
<div className={`${baseClass}-chips`}>
<Badge
color='silver-muted'
label={
<>
{agent.modelLogo && (
<span aria-hidden='true' className={`${baseClass}-logo`}>
{agent.modelLogo}
</span>
)}
{agent.model}
</>
}
size='s'
/>
</div>
)}
{(time || cost !== undefined || agent.context !== undefined) && (
<dl className={`pds-copy-xs ${baseClass}-usage`}>
{time && (
<div>
<dt>{isRunning ? labelStrings.running : labelStrings.time}</dt>
<dd>{time}</dd>
</div>
)}
{cost !== undefined && (
<div>
<dt>{labelStrings.cost}</dt>
<dd>{formatCost(cost)}</dd>
</div>
)}
{agent.context !== undefined && (
<div>
<dt>{labelStrings.tokens}</dt>
<dd>
{agent.contextLimit
? formatTokens(tokensUsed(agent))
: `${percent(agent.context)}%`}
</dd>
</div>
)}
</dl>
)}
{tools.length > 0 && (
<div className={`${baseClass}-chips`}>
{tools.map((tool) => (
<Badge
color='silver-muted'
iconName={tool.iconName}
key={tool.name}
label={tool.name}
size='s'
/>
))}
</div>
)}
{onSteer && (
<div className={`${baseClass}-actions`}>
<IconButton
ariaLabel={fill(labelStrings.messageLabel, { name: agent.name })}
hasBorder
iconName='comment'
onClick={onMessage}
size='xs'
/>
{actions.map((item) => (
<Button
key={item.action}
label={item.label}
onClick={() => onSteer(item.action)}
size='xs'
variant={item.variant}
/>
))}
</div>
)}
</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