Skip to content
refs
179 lines · 8.6 KBRaw
1import { AtIcon, CheckCircleIcon, CircleNotchIcon, ClockIcon, ShieldWarningIcon, WarningCircleIcon } from "@refs/design-system/icons";
2import type { MentionState, OnlineAgent } from "@refs/backend/contract";
3import { AgentAvatar, Ago, Badge, Button, Checkbox, DropdownMenu, HoverCard } from "@refs/design-system";
4import { useState } from "react";
5import { useParams, useRouteLoaderData } from "react-router";
6import { AppLink } from "./link";
7import { AgentName, TimelineRow } from "./timeline";
8
9export function agentLabel(agent: OnlineAgent): string {
10 return [agent.runtime, agent.onBehalfOf.name, agent.machine, agent.session.slice(0, 4)].filter(Boolean).join(" · ");
11}
12
13/** The agents working on a branch. */
14export function agentsOn(agents: readonly OnlineAgent[], branch: string): OnlineAgent[] {
15 return agents.filter((agent) => agent.branch === branch);
16}
17
18export function MentionTools({ agents, body, onBody, canAssign }: { agents: readonly OnlineAgent[]; body: string; onBody: (body: string) => void; canAssign: boolean }) {
19 const [assign, setAssign] = useState(false);
20 const hosted = useHostedAgents();
21 const mentioned = agents.filter((agent) => body.includes(`@agent/${agent.session}`));
22 const insert = (token: string) => onBody(`${body}${body && !/\s$/.test(body) ? " " : ""}${token} `);
23 return (
24 <div className="flex flex-wrap items-center gap-3">
25 <DropdownMenu>
26 <DropdownMenu.Trigger
27 render={
28 <Button type="button" variant="secondary" size="sm" icon={<AtIcon size={16} />}>
29 Mention agent
30 </Button>
31 }
32 />
33 <DropdownMenu.Content align="start">
34 {hosted.map((agent) => (
35 <DropdownMenu.Item key={agent.handle} onClick={() => insert(`@${agent.handle}`)}>
36 <AgentAvatar name={agent.handle} size="xs" />
37 <span>@{agent.handle}</span>
38 {agent.description && <span className="ml-1 max-w-64 truncate text-xs text-faint">{agent.description}</span>}
39 </DropdownMenu.Item>
40 ))}
41 {agents.map((agent) => (
42 <DropdownMenu.Item key={agent.session} onClick={() => insert(`@agent/${agent.session}`)}>
43 <AgentAvatar name={agent.runtime} size="xs" />
44 <AgentName agent={agent} />
45 <span className="ml-1 text-xs text-faint">
46 {agent.branch ?? "idle"} · active <Ago at={agent.lastSeen} now={Math.floor(Date.now() / 1000)} />
47 </span>
48 </DropdownMenu.Item>
49 ))}
50 </DropdownMenu.Content>
51 </DropdownMenu>
52 {canAssign && mentioned.length > 0 && (
53 <Checkbox label={`Hand this branch to ${mentioned.map(agentLabel).join(", ")}`} checked={assign} onCheckedChange={(checked) => setAssign(checked === true)} />
54 )}
55 <input type="hidden" name="assign" value={canAssign && assign && mentioned.length > 0 ? "1" : ""} />
56 </div>
57 );
58}
59
60const MENTION_TEXT: Record<MentionState["state"], (agent: string) => string> = {
61 delivered: (agent) => `Delivered to ${agent}`,
62 waiting: (agent) => `Waiting for ${agent}: it picks this up at its next prompt or tool call`,
63 offline: (agent) => `${agent} went offline before picking this up`,
64 working: (agent) => `${agent} is working on this`,
65 approval: (agent) => `${agent} is waiting for approval`,
66 answered: (agent) => `${agent} answered this`,
67 failed: (agent) => `${agent} couldn't finish this`,
68};
69
70export const RUN_STATES: ReadonlySet<MentionState["state"]> = new Set(["working", "approval", "answered", "failed"]);
71
72type HostedProfile = { handle: string; description: string; scope: "builtin" | "namespace" | "repo"; model: string | null };
73
74function useHostedAgents(): HostedProfile[] {
75 return useRouteLoaderData<{ hostedAgents: HostedProfile[] }>("routes/pull-layout")?.hostedAgents ?? [];
76}
77
78const SCOPE_LABEL: Record<HostedProfile["scope"], string> = { builtin: "Built in", namespace: "Shared", repo: "This repository" };
79
80const HOSTED_TEXT: Partial<Record<MentionState["state"], string>> = { working: "is working on this", answered: "answered this", failed: "couldn't finish this" };
81
82export function RunRows({ mentions }: { mentions: readonly MentionState[] }) {
83 return mentions.map((mention, index) => (
84 <TimelineRow key={index} tone="event" offset="top-[13px]">
85 <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]">
86 <MentionIcon state={mention.state} />
87 {mention.hosted ? (
88 <>
89 <HostedAgentChip mention={mention} hosted={mention.hosted} />
90 <span className="min-w-0">
91 {HOSTED_TEXT[mention.state] ?? MENTION_TEXT[mention.state]("").trim()}
92 {mention.reason && `: ${mention.reason}`}
93 </span>
94 </>
95 ) : (
96 <>
97 <AgentAvatar name={mention.agent} size="xs" />
98 <span className="min-w-0">
99 {MENTION_TEXT[mention.state](mention.agent)}
100 {mention.reason && `: ${mention.reason}`}
101 </span>
102 </>
103 )}
104 </p>
105 </TimelineRow>
106 ));
107}
108
109function HostedAgentChip({ mention, hosted }: { mention: MentionState; hosted: NonNullable<MentionState["hosted"]> }) {
110 const params = useParams();
111 const profile = useHostedAgents().find((agent) => agent.handle === hosted.handle);
112 const href = `/${params.ns}/${params.repo}/agents?run=${hosted.session}`;
113 const now = Math.floor(Date.now() / 1000);
114 return (
115 <HoverCard
116 render={
117 <AppLink href={href} className="-mx-1 inline-flex items-center gap-1.5 rounded-chip px-1 font-medium text-ink transition-colors hover:bg-hover focus-visible:bg-hover motion-reduce:transition-none">
118 <AgentAvatar name={hosted.handle} size="xs" />@{hosted.handle}
119 </AppLink>
120 }
121 >
122 <div className="flex flex-col gap-3 text-compact">
123 <div className="flex items-center gap-2.5">
124 <AgentAvatar name={hosted.handle} size="base" />
125 <div className="flex min-w-0 flex-col">
126 <span className="font-semibold text-ink">@{hosted.handle}</span>
127 <span className="text-xs text-muted">{profile?.model ?? "GLM-5.3 on Workers AI"}</span>
128 </div>
129 {profile && (
130 <Badge variant="outline" className="ml-auto">
131 {SCOPE_LABEL[profile.scope]}
132 </Badge>
133 )}
134 </div>
135 {profile?.description && <p className="m-0 text-muted">{profile.description}</p>}
136 <div className="flex flex-col gap-1 rounded-control bg-sunken p-2.5">
137 <span className="flex items-center gap-1.5 font-medium text-ink">
138 <MentionIcon state={mention.state} />
139 {RUN_STATE_LABEL[mention.state] ?? "Waiting"}
140 <span className="ml-auto text-xs font-normal text-faint">
141 <Ago at={hosted.startedAt} now={now} />
142 </span>
143 </span>
144 {mention.state === "working" && (
145 <span className="text-xs text-muted [overflow-wrap:anywhere]">
146 {hosted.lastStep ? `Step ${hosted.lastStep.step}: ${hosted.lastStep.tools.length ? hosted.lastStep.tools.join(" · ") : "writing the answer"}` : "Reading the pull request"}
147 </span>
148 )}
149 {mention.state === "failed" && mention.reason && <span className="text-xs text-muted">{mention.reason}</span>}
150 </div>
151 <span className="text-xs text-accent-text">{mention.state === "working" ? "Click to watch its steps" : "Click to see its steps"}</span>
152 </div>
153 </HoverCard>
154 );
155}
156
157const RUN_STATE_LABEL: Partial<Record<MentionState["state"], string>> = { working: "Working", approval: "Waiting for approval", answered: "Answered", failed: "Didn't finish" };
158
159function MentionIcon({ state }: { state: MentionState["state"] }) {
160 if (state === "delivered" || state === "answered") return <CheckCircleIcon size={14} className="text-success" />;
161 if (state === "approval") return <ShieldWarningIcon size={14} />;
162 if (state === "failed") return <WarningCircleIcon size={14} className="text-danger" />;
163 if (state === "working") return <CircleNotchIcon size={14} className="animate-spin motion-reduce:animate-none" />;
164 return <ClockIcon size={14} />;
165}
166
167export function MentionStates({ mentions }: { mentions: readonly MentionState[] }) {
168 if (!mentions.length) return null;
169 return (
170 <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)]">
171 {mentions.map((mention, index) => (
172 <li key={index} className="flex items-center gap-2">
173 <MentionIcon state={mention.state} />
174 {MENTION_TEXT[mention.state](mention.agent)}
175 </li>
176 ))}
177 </ul>
178 );
179}