← All proposals

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

proposals/AgentTree/agent-tree-story-shared.tsx383 lines · View on GitHub
import type { AgentTreeAgent, AgentTreeHandoff } from './AgentTree';

// Stand-in model maker logo for the stories. The consumer passes its own.
export const MODEL = (
	<svg aria-hidden='true' fill='currentColor' viewBox='0 0 16 16'>
		<path d='M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 3a4 4 0 1 1 0 8 4 4 0 0 1 0-8Z' />
	</svg>
);

const MINUTE = 60_000;

/**
 * A lead with three sub-agents and two helpers, at the start of a run: the lead
 * is working and everyone else is idle until work is handed to them. The times
 * are relative to when the story loads.
 */
export const makeCrew = (): AgentTreeAgent => {
	const now = Date.now();
	return {
		activity: 'Splitting the checkout fix between three agents',
		children: [
			{
				activity: 'Waiting for work',
				children: [
					{
						activity: 'Waiting for work',
						context: 0.05,
						contextLimit: 200000,
						id: 'lint',
						model: 'Zappy',
						name: 'Lint helper',
						status: 'idle',
						task: 'Lint the files the reviewer is reading.',
					},
					{
						activity: 'Waiting for work',
						context: 0.05,
						contextLimit: 200000,
						id: 'types',
						model: 'Zappy',
						name: 'Type helper',
						status: 'idle',
						task: 'Check the types of the changed functions.',
					},
				],
				context: 0.12,
				contextLimit: 200000,
				iconName: 'sparklesSolid',
				id: 'reviewer',
				model: 'Partner model',
				modelLogo: MODEL,
				name: 'Code reviewer',
				status: 'idle',
				task: 'Review the change that guards empty carts, and report anything that could break checkout.',
				tools: [
					{ iconName: 'codeBranch', name: 'Git' },
					{ iconName: 'magnifyingGlass', name: 'Code search' },
				],
			},
			{
				activity: 'Waiting for work',
				context: 0.08,
				contextLimit: 200000,
				id: 'tests',
				model: 'Zappy',
				name: 'Test runner',
				status: 'idle',
				task: 'Run the unit and end-to-end tests against the fix.',
				tools: [
					{ iconName: 'terminal', name: 'Terminal' },
					{ iconName: 'codeBranch', name: 'Git' },
				],
			},
			{
				activity: 'Waiting for work',
				context: 0.06,
				contextLimit: 200000,
				iconName: 'sparklesSolid',
				id: 'docs',
				model: 'Partner model',
				modelLogo: MODEL,
				name: 'Docs writer',
				status: 'idle',
				task: 'Write the release note and update the changelog.',
				tools: [{ iconName: 'fileLines', name: 'Docs' }],
			},
		],
		context: 0.34,
		contextLimit: 200000,
		cost: 0.42,
		id: 'lead',
		model: 'Zappy',
		name: 'Release lead',
		started: now - 2 * MINUTE,
		status: 'active',
		task: 'Ship the fix for empty carts: review it, test it and write the release note.',
		tools: [
			{ iconName: 'codeBranch', name: 'Git' },
			{ iconName: 'terminal', name: 'Terminal' },
			{ iconName: 'magnifyingGlass', name: 'Code search' },
			{ iconName: 'fileLines', name: 'Docs' },
			{ iconName: 'code', name: 'Models' },
		],
	};
};

type Patch = Partial<AgentTreeAgent>;

/**
 * One step of the scripted run: the handoff, and what changes when it arrives.
 * The receiver starts working with the handed-over text as its last handoff;
 * a sender that is not the lead is finished and stops. `patches` maps an agent
 * id to its change; a function gets the time of arrival.
 */
interface Step {
	handoff: AgentTreeHandoff;
	patches: Record<string, (now: number) => Patch>;
}

const SCRIPT: Step[] = [
	{
		handoff: {
			from: 'lead',
			id: 'h1',
			label: 'Review the cart guard',
			to: 'reviewer',
		},
		patches: {
			lead: () => ({
				activity: 'Waiting for the review',
				context: 0.38,
			}),
			reviewer: (now) => ({
				activity: 'Reviewing the cart guard in 4 files',
				context: 0.31,
				last: { from: 'Release lead', text: 'Review the cart guard' },
				started: now,
				status: 'active',
			}),
		},
	},
	{
		handoff: {
			from: 'reviewer',
			id: 'h2',
			label: 'Lint the 4 files',
			to: 'lint',
		},
		patches: {
			lint: (now) => ({
				activity: 'Linting 4 files',
				context: 0.14,
				last: { from: 'Code reviewer', text: 'Lint the 4 files' },
				started: now,
				status: 'active',
			}),
			reviewer: () => ({
				activity: 'Waiting for the lint',
				context: 0.44,
			}),
		},
	},
	{
		handoff: {
			from: 'lint',
			id: 'h3',
			label: 'No issues found',
			to: 'reviewer',
		},
		patches: {
			lint: (now) => ({
				activity: 'No issues in 4 files',
				ended: now,
				status: 'done',
			}),
			reviewer: () => ({
				activity: 'Lint is clean, reading the tests',
				context: 0.62,
				last: { from: 'Lint helper', text: 'No issues found' },
			}),
		},
	},
	{
		handoff: {
			from: 'reviewer',
			id: 'h4',
			label: 'Review passed',
			to: 'lead',
		},
		patches: {
			lead: () => ({
				activity: 'Review passed, starting the tests',
				context: 0.45,
				last: { from: 'Code reviewer', text: 'Review passed' },
			}),
			reviewer: (now) => ({
				activity: 'Review passed',
				ended: now,
				status: 'done',
			}),
		},
	},
	{
		handoff: {
			from: 'lead',
			id: 'h5',
			label: 'Run the checks',
			to: 'tests',
		},
		patches: {
			lead: () => ({ activity: 'Waiting for the tests', context: 0.5 }),
			tests: (now) => ({
				activity: 'Running the end-to-end suite',
				context: 0.36,
				last: { from: 'Release lead', text: 'Run the checks' },
				started: now,
				status: 'active',
			}),
		},
	},
	{
		handoff: {
			from: 'tests',
			id: 'h6',
			label: 'All checks passed',
			to: 'lead',
		},
		patches: {
			lead: () => ({
				activity: 'Checks passed, writing the release note',
				context: 0.55,
				last: { from: 'Test runner', text: 'All checks passed' },
			}),
			tests: (now) => ({
				activity: 'All checks passed',
				ended: now,
				status: 'done',
			}),
		},
	},
	{
		handoff: {
			from: 'lead',
			id: 'h7',
			label: 'Write the release note',
			to: 'docs',
		},
		patches: {
			docs: (now) => ({
				activity: 'Writing the release note',
				context: 0.28,
				last: { from: 'Release lead', text: 'Write the release note' },
				started: now,
				status: 'active',
			}),
			lead: () => ({ activity: 'Waiting for the release note', context: 0.58 }),
		},
	},
	{
		handoff: {
			from: 'docs',
			id: 'h8',
			label: 'Release note ready',
			to: 'lead',
		},
		patches: {
			docs: (now) => ({
				activity: 'Release note ready',
				ended: now,
				status: 'done',
			}),
			lead: () => ({
				activity: 'Ready to ship, needs your OK',
				last: { from: 'Docs writer', text: 'Release note ready' },
				status: 'waiting',
			}),
		},
	},
];

/**
 * The scripted run of handoffs, so a story can play them one by one.
 */
export const HANDOFFS: AgentTreeHandoff[] = SCRIPT.map((step) => step.handoff);

/**
 * The tree after handoff `id` has arrived: the receiver works and the sender
 * has stopped (see `SCRIPT`).
 */
export const applyHandoff = (
	root: AgentTreeAgent,
	id: string,
): AgentTreeAgent => {
	const step = SCRIPT.find((s) => s.handoff.id === id);
	if (!step) return root;
	const now = Date.now();
	return Object.entries(step.patches).reduce(
		(tree, [agentId, patch]) => update(tree, agentId, patch(now)),
		root,
	);
};

/**
 * A copy of the tree with the agent `id` changed.
 */
export const update = (
	agent: AgentTreeAgent,
	id: string,
	patch: Partial<AgentTreeAgent>,
): AgentTreeAgent =>
	agent.id === id
		? { ...agent, ...patch }
		: {
				...agent,
				children: agent.children?.map((child) => update(child, id, patch)),
			};

const helper = (
	id: string,
	name: string,
	status: AgentTreeAgent['status'],
	context: number,
): AgentTreeAgent => ({
	activity: 'Working through its share',
	context,
	contextLimit: 200000,
	id,
	model: 'Zappy',
	name,
	status,
});

/**
 * One sub-agent in each state, with context windows that cross 80% and 95%.
 */
export const makeAllStates = (): AgentTreeAgent => ({
	activity: 'Watching its agents',
	children: [
		{
			...helper('s-active', 'Working', 'active', 0.35),
			activity: 'Reading the logs',
		},
		{ ...helper('s-idle', 'Idle', 'idle', 0.1), activity: 'Waiting for work' },
		{
			...helper('s-paused', 'Paused', 'paused', 0.4),
			activity: 'Paused by you',
		},
		{
			...helper('s-waiting', 'Needs you', 'waiting', 0.5),
			activity: 'Needs your OK to continue',
		},
		{
			...helper('s-done', 'Done', 'done', 0.55),
			activity: 'Finished the report',
		},
		{
			...helper('s-failed', 'Failed', 'failed', 0.6),
			activity: 'Stopped with an error',
		},
	],
	context: 0.3,
	contextLimit: 200000,
	id: 'lead',
	model: 'Zappy',
	name: 'Release lead',
	status: 'active',
});

export const makeContextWarning = (): AgentTreeAgent => ({
	activity: 'Context is filling up',
	children: [
		helper('c-ok', 'Under 80%', 'active', 0.62),
		helper('c-warn', 'Over 80%', 'active', 0.86),
		helper('c-full', 'Over 95%', 'active', 0.97),
	],
	context: 0.5,
	contextLimit: 200000,
	id: 'lead',
	model: 'Zappy',
	name: 'Release lead',
	status: 'active',
});