106 lines · 5.1 KBRaw
| 1 | import { AtIcon, CheckCircleIcon, CircleNotchIcon, ClockIcon, ShieldWarningIcon, WarningCircleIcon } from "@refs/design-system/icons"; |
| 2 | import type { MentionState, OnlineAgent } from "@refs/backend/contract"; |
| 3 | import { AgentAvatar, Ago, Button, Checkbox, DropdownMenu } from "@refs/design-system"; |
| 4 | import { useState } from "react"; |
| 5 | import { useRouteLoaderData } from "react-router"; |
| 6 | import { AgentName, TimelineRow } from "./timeline"; |
| 7 | |
| 8 | export function agentLabel(agent: OnlineAgent): string { |
| 9 | return [agent.runtime, agent.onBehalfOf.name, agent.machine, agent.session.slice(0, 4)].filter(Boolean).join(" · "); |
| 10 | } |
| 11 | |
| 12 | /** The agents working on a branch. */ |
| 13 | export function agentsOn(agents: readonly OnlineAgent[], branch: string): OnlineAgent[] { |
| 14 | return agents.filter((agent) => agent.branch === branch); |
| 15 | } |
| 16 | |
| 17 | export function MentionTools({ agents, body, onBody, canAssign }: { agents: readonly OnlineAgent[]; body: string; onBody: (body: string) => void; canAssign: boolean }) { |
| 18 | const [assign, setAssign] = useState(false); |
| 19 | const hosted = useRouteLoaderData<{ hostedAgents: { handle: string; description: string }[] }>("routes/pull-layout")?.hostedAgents ?? []; |
| 20 | const mentioned = agents.filter((agent) => body.includes(`@agent/${agent.session}`)); |
| 21 | const insert = (token: string) => onBody(`${body}${body && !/\s$/.test(body) ? " " : ""}${token} `); |
| 22 | return ( |
| 23 | <div className="flex flex-wrap items-center gap-3"> |
| 24 | <DropdownMenu> |
| 25 | <DropdownMenu.Trigger |
| 26 | render={ |
| 27 | <Button type="button" variant="secondary" size="sm" icon={<AtIcon size={16} />}> |
| 28 | Mention agent |
| 29 | </Button> |
| 30 | } |
| 31 | /> |
| 32 | <DropdownMenu.Content align="start"> |
| 33 | {hosted.map((agent) => ( |
| 34 | <DropdownMenu.Item key={agent.handle} onClick={() => insert(`@${agent.handle}`)}> |
| 35 | <AgentAvatar name={agent.handle} size="xs" /> |
| 36 | <span>@{agent.handle}</span> |
| 37 | {agent.description && <span className="ml-1 max-w-64 truncate text-xs text-faint">{agent.description}</span>} |
| 38 | </DropdownMenu.Item> |
| 39 | ))} |
| 40 | {agents.map((agent) => ( |
| 41 | <DropdownMenu.Item key={agent.session} onClick={() => insert(`@agent/${agent.session}`)}> |
| 42 | <AgentAvatar name={agent.runtime} size="xs" /> |
| 43 | <AgentName agent={agent} /> |
| 44 | <span className="ml-1 text-xs text-faint"> |
| 45 | {agent.branch ?? "idle"} · active <Ago at={agent.lastSeen} now={Math.floor(Date.now() / 1000)} /> |
| 46 | </span> |
| 47 | </DropdownMenu.Item> |
| 48 | ))} |
| 49 | </DropdownMenu.Content> |
| 50 | </DropdownMenu> |
| 51 | {canAssign && mentioned.length > 0 && ( |
| 52 | <Checkbox label={`Hand this branch to ${mentioned.map(agentLabel).join(", ")}`} checked={assign} onCheckedChange={(checked) => setAssign(checked === true)} /> |
| 53 | )} |
| 54 | <input type="hidden" name="assign" value={canAssign && assign && mentioned.length > 0 ? "1" : ""} /> |
| 55 | </div> |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | const MENTION_TEXT: Record<MentionState["state"], (agent: string) => string> = { |
| 60 | delivered: (agent) => `Delivered to ${agent}`, |
| 61 | waiting: (agent) => `Waiting for ${agent}: it picks this up at its next prompt or tool call`, |
| 62 | offline: (agent) => `${agent} went offline before picking this up`, |
| 63 | working: (agent) => `${agent} is working on this`, |
| 64 | approval: (agent) => `${agent} is waiting for approval`, |
| 65 | answered: (agent) => `${agent} answered this`, |
| 66 | failed: (agent) => `${agent} couldn't finish this`, |
| 67 | }; |
| 68 | |
| 69 | export const RUN_STATES: ReadonlySet<MentionState["state"]> = new Set(["working", "approval", "answered", "failed"]); |
| 70 | |
| 71 | export function RunRows({ mentions }: { mentions: readonly MentionState[] }) { |
| 72 | return mentions.map((mention, index) => ( |
| 73 | <TimelineRow key={index} tone="event" offset="top-[13px]"> |
| 74 | <p className="m-0 flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 py-1 text-compact text-muted [overflow-wrap:anywhere]"> |
| 75 | <MentionIcon state={mention.state} /> |
| 76 | <AgentAvatar name={mention.agent} size="xs" /> |
| 77 | <span className="min-w-0"> |
| 78 | {MENTION_TEXT[mention.state](mention.agent)} |
| 79 | {mention.reason && `: ${mention.reason}`} |
| 80 | </span> |
| 81 | </p> |
| 82 | </TimelineRow> |
| 83 | )); |
| 84 | } |
| 85 | |
| 86 | function MentionIcon({ state }: { state: MentionState["state"] }) { |
| 87 | if (state === "delivered" || state === "answered") return <CheckCircleIcon size={14} className="text-success" />; |
| 88 | if (state === "approval") return <ShieldWarningIcon size={14} />; |
| 89 | if (state === "failed") return <WarningCircleIcon size={14} className="text-danger" />; |
| 90 | if (state === "working") return <CircleNotchIcon size={14} className="animate-spin" />; |
| 91 | return <ClockIcon size={14} />; |
| 92 | } |
| 93 | |
| 94 | export function MentionStates({ mentions }: { mentions: readonly MentionState[] }) { |
| 95 | if (!mentions.length) return null; |
| 96 | return ( |
| 97 | <ul className="m-0 flex list-none flex-col gap-1 px-4 py-2.5 text-compact text-muted shadow-[inset_0_1px_0_var(--hairline)]"> |
| 98 | {mentions.map((mention, index) => ( |
| 99 | <li key={index} className="flex items-center gap-2"> |
| 100 | <MentionIcon state={mention.state} /> |
| 101 | {MENTION_TEXT[mention.state](mention.agent)} |
| 102 | </li> |
| 103 | ))} |
| 104 | </ul> |
| 105 | ); |
| 106 | } |