import { AtIcon, CheckCircleIcon, CircleNotchIcon, ClockIcon, ShieldWarningIcon, WarningCircleIcon } from "@refs/design-system/icons";
import type { MentionState, OnlineAgent } from "@refs/backend/contract";
import { AgentAvatar, Ago, Badge, Button, Checkbox, DropdownMenu, HoverCard } from "@refs/design-system";
import { useState } from "react";
import { useParams, useRouteLoaderData } from "react-router";
import { AppLink } from "./link";
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 = useHostedAgents();
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"]);
type HostedProfile = { handle: string; description: string; scope: "builtin" | "namespace" | "repo"; model: string | null };
function useHostedAgents(): HostedProfile[] {
return useRouteLoaderData<{ hostedAgents: HostedProfile[] }>("routes/pull-layout")?.hostedAgents ?? [];
}
const SCOPE_LABEL: Record = { builtin: "Built in", namespace: "Shared", repo: "This repository" };
const HOSTED_TEXT: Partial> = { working: "is working on this", answered: "answered this", failed: "couldn't finish this" };
export function RunRows({ mentions }: { mentions: readonly MentionState[] }) {
return mentions.map((mention, index) => (
{mention.hosted ? (
<>
{HOSTED_TEXT[mention.state] ?? MENTION_TEXT[mention.state]("").trim()}
{mention.reason && `: ${mention.reason}`}
>
) : (
<>
{MENTION_TEXT[mention.state](mention.agent)}
{mention.reason && `: ${mention.reason}`}
>
)}
));
}
function HostedAgentChip({ mention, hosted }: { mention: MentionState; hosted: NonNullable }) {
const params = useParams();
const profile = useHostedAgents().find((agent) => agent.handle === hosted.handle);
const href = `/${params.ns}/${params.repo}/agents?run=${hosted.session}`;
const now = Math.floor(Date.now() / 1000);
return (
@{hosted.handle}
}
>
@{hosted.handle}
{profile?.model ?? "GLM-5.3 on Workers AI"}
{profile && (
{SCOPE_LABEL[profile.scope]}
)}
{profile?.description &&
{profile.description}
}
{RUN_STATE_LABEL[mention.state] ?? "Waiting"}
{mention.state === "working" && (
{hosted.lastStep ? `Step ${hosted.lastStep.step}: ${hosted.lastStep.tools.length ? hosted.lastStep.tools.join(" · ") : "writing the answer"}` : "Reading the pull request"}
)}
{mention.state === "failed" && mention.reason &&
{mention.reason}}
{mention.state === "working" ? "Click to watch its steps" : "Click to see its steps"}
);
}
const RUN_STATE_LABEL: Partial> = { working: "Working", approval: "Waiting for approval", answered: "Answered", failed: "Didn't finish" };
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)}
))}
);
}