import { AtIcon, CheckCircleIcon, CircleNotchIcon, ClockIcon, ShieldWarningIcon, WarningCircleIcon } from "@refs/design-system/icons";
import type { MentionState, OnlineAgent } from "@refs/backend/contract";
import { AgentAvatar, Ago, Button, Checkbox, DropdownMenu } from "@refs/design-system";
import { useState } from "react";
import { useRouteLoaderData } from "react-router";
import { AgentName, TimelineRow } from "./timeline";
export function agentLabel(agent: OnlineAgent): string {
return [agent.runtime, agent.onBehalfOf.name, agent.machine, agent.session.slice(0, 4)].filter(Boolean).join(" · ");
}
/** The agents working on a branch. */
export function agentsOn(agents: readonly OnlineAgent[], branch: string): OnlineAgent[] {
return agents.filter((agent) => agent.branch === branch);
}
export function MentionTools({ agents, body, onBody, canAssign }: { agents: readonly OnlineAgent[]; body: string; onBody: (body: string) => void; canAssign: boolean }) {
const [assign, setAssign] = useState(false);
const hosted = useRouteLoaderData<{ hostedAgents: { handle: string; description: string }[] }>("routes/pull-layout")?.hostedAgents ?? [];
const mentioned = agents.filter((agent) => body.includes(`@agent/${agent.session}`));
const insert = (token: string) => onBody(`${body}${body && !/\s$/.test(body) ? " " : ""}${token} `);
return (
}>
Mention agent
}
/>
{hosted.map((agent) => (
insert(`@${agent.handle}`)}>
@{agent.handle}
{agent.description && {agent.description} }
))}
{agents.map((agent) => (
insert(`@agent/${agent.session}`)}>
{agent.branch ?? "idle"} · active
))}
{canAssign && mentioned.length > 0 && (
setAssign(checked === true)} />
)}
0 ? "1" : ""} />
);
}
const MENTION_TEXT: Record string> = {
delivered: (agent) => `Delivered to ${agent}`,
waiting: (agent) => `Waiting for ${agent}: it picks this up at its next prompt or tool call`,
offline: (agent) => `${agent} went offline before picking this up`,
working: (agent) => `${agent} is working on this`,
approval: (agent) => `${agent} is waiting for approval`,
answered: (agent) => `${agent} answered this`,
failed: (agent) => `${agent} couldn't finish this`,
};
export const RUN_STATES: ReadonlySet = new Set(["working", "approval", "answered", "failed"]);
export function RunRows({ mentions }: { mentions: readonly MentionState[] }) {
return mentions.map((mention, index) => (
{MENTION_TEXT[mention.state](mention.agent)}
{mention.reason && `: ${mention.reason}`}
));
}
function MentionIcon({ state }: { state: MentionState["state"] }) {
if (state === "delivered" || state === "answered") return ;
if (state === "approval") return ;
if (state === "failed") return ;
if (state === "working") return ;
return ;
}
export function MentionStates({ mentions }: { mentions: readonly MentionState[] }) {
if (!mentions.length) return null;
return (
{mentions.map((mention, index) => (
{MENTION_TEXT[mention.state](mention.agent)}
))}
);
}