g1t/apps/web/app/components/agents.tsx

502 lines20,155 bytesCodeBlame
1/**
2 * Agents at work, wherever they show: the project's Agents page, the
3 * workspace's fleet, a pull request's Agent panel and the pull request
4 * list. Stopping and messaging a run go through the project's
5 * `agents.json` resource route, so every place behaves the same.
6 */
7import { Bot, CircleSlash, Clock, Coins, Gauge, Loader2, MessageSquare, OctagonX, Square, TriangleAlert } from "lucide-react";
8import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
9import { Link, useFetcher } from "react-router";
10import { useRefreshWhile } from "../lib/refresh";
11
12import {
13 type AgentRun,
14 type AgentRunStatus,
15 type Confidence,
16 type RunKind,
17 RUN_KIND_LABEL,
18 type Stage,
19 isActiveRun,
20 isAgentKind,
21 takesMessages,
22} from "@g1t/contracts";
23
24import { RunAudit } from "./audit";
25import { STAGE_LABEL, StageDots } from "./lifecycle";
26import { Avatar, SubmitButton, TimeAgo } from "./ui";
27import {
28 AlertDialog,
29 AlertDialogAction,
30 AlertDialogCancel,
31 AlertDialogContent,
32 AlertDialogDescription,
33 AlertDialogFooter,
34 AlertDialogHeader,
35 AlertDialogTitle,
36 AlertDialogTrigger,
37} from "./ui/alert-dialog";
38import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "./ui/dialog";
39import { shownStep } from "../lib/agent-step";
40
41/** How often a page with something running asks again. */
42export const LIVE_MS = 4000;
43
44/** Revalidates the page every few seconds while `live`, as the merge queue does. */
45export function useLiveRefresh(live: boolean) {
46 useRefreshWhile(live, LIVE_MS);
47}
48
49export function formatCost(usd: number | null | undefined): string | null {
50 if (usd == null) return null;
51 if (usd > 0 && usd < 0.01) return "<$0.01";
52 return `$${usd.toFixed(2)}`;
53}
54
55function span(ms: number): string {
56 const seconds = Math.max(0, Math.floor(ms / 1000));
57 const hours = Math.floor(seconds / 3600);
58 const minutes = Math.floor((seconds % 3600) / 60);
59 if (hours > 0) return `${hours}h ${minutes}m`;
60 if (minutes > 0) return `${minutes}m ${String(seconds % 60).padStart(2, "0")}s`;
61 return `${seconds}s`;
62}
63
64/** How long a run has gone on, ticking while it runs. */
65export function Elapsed({ from, to }: { from: string; to?: string | null }) {
66 const [now, setNow] = useState(() => Date.now());
67 useEffect(() => {
68 if (to) return;
69 const timer = setInterval(() => setNow(Date.now()), 1000);
70 return () => clearInterval(timer);
71 }, [to]);
72 const end = to ? new Date(to).getTime() : now;
73 return (
74 <span suppressHydrationWarning className="tabular-nums">
75 {span(end - new Date(from).getTime())}
76 </span>
77 );
78}
79
80const STATUS: Record<AgentRunStatus, { label: string; tone: string; icon: ReactNode }> = {
81 queued: { label: "Starting", tone: "text-muted border-line", icon: <Clock size={12} /> },
82 running: {
83 label: "Running",
84 tone: "text-merged border-merged/40 bg-merged/10",
85 icon: <Loader2 size={12} className="animate-spin" />,
86 },
87 succeeded: { label: "Done", tone: "text-accent border-accent/40 bg-accent/10", icon: <Bot size={12} /> },
88 failed: { label: "Failed", tone: "text-danger border-danger/40 bg-danger/10", icon: <TriangleAlert size={12} /> },
89 stopped: { label: "Stopped", tone: "text-warn border-warn/40 bg-warn/10", icon: <OctagonX size={12} /> },
90};
91
92export function RunStatusBadge({ status }: { status: AgentRunStatus }) {
93 const { label, tone, icon } = STATUS[status];
94 return (
95 <span className={`inline-flex shrink-0 items-center gap-1 rounded-full border px-2 py-0.5 text-xs ${tone}`}>
96 {icon}
97 {label}
98 </span>
99 );
100}
101
102export function KindLabel({ kind }: { kind: RunKind }) {
103 return (
104 <span className="rounded border border-line px-1.5 py-px font-mono text-[0.6875rem] text-muted">
105 {RUN_KIND_LABEL[kind].toLowerCase()}
106 </span>
107 );
108}
109
110/** Where a project's agent actions are posted. */
111function actionUrl(run: AgentRun): string {
112 return `/${run.repo.namespace}/${run.repo.name}/agents.json`;
113}
114
115type ActionResult = { ok: boolean; notice?: string; error?: string } | undefined;
116
117/** Stop a run, after saying what that does. */
118export function StopRun({ run, compact }: { run: AgentRun; compact?: boolean }) {
119 const fetcher = useFetcher<ActionResult>();
120 const busy = fetcher.state !== "idle";
121 const onPull = run.number != null && isAgentKind(run.kind);
122 return (
123 <>
124 <AlertDialog>
125 <AlertDialogTrigger
126 disabled={busy}
127 className="inline-flex items-center gap-1.5 rounded-md border border-line px-2.5 py-1 text-xs text-muted transition-colors hover:border-danger/50 hover:text-danger disabled:opacity-50"
128 >
129 <Square size={11} />
130 {busy ? "Stopping…" : compact ? "Stop" : "Stop run"}
131 </AlertDialogTrigger>
132 <AlertDialogContent>
133 <AlertDialogHeader>
134 <AlertDialogTitle>Stop this {RUN_KIND_LABEL[run.kind].toLowerCase()} run?</AlertDialogTitle>
135 <AlertDialogDescription>
136 Its sandbox is shut down at once and nothing more is pushed.
137 {onPull &&
138 ` g1t stops seeing #${run.number} through and leaves it for a person; what the agent already pushed stays on it.`}
139 </AlertDialogDescription>
140 </AlertDialogHeader>
141 <AlertDialogFooter>
142 <AlertDialogCancel>Keep it running</AlertDialogCancel>
143 <AlertDialogAction
144 onClick={() => fetcher.submit({ intent: "stop", run: run.id }, { method: "post", action: actionUrl(run) })}
145 >
146 Stop
147 </AlertDialogAction>
148 </AlertDialogFooter>
149 </AlertDialogContent>
150 </AlertDialog>
151 {fetcher.data?.error && <span className="text-xs text-danger">{fetcher.data.error}</span>}
152 </>
153 );
154}
155
156/** Send the agent on a run's pull request a message. */
157export function MessageRun({ run }: { run: AgentRun }) {
158 const fetcher = useFetcher<ActionResult>();
159 const [open, setOpen] = useState(false);
160 const form = useRef<HTMLFormElement>(null);
161 // Sent: the box empties, and the notice below says where it went.
162 useEffect(() => {
163 if (fetcher.state === "idle" && fetcher.data?.ok) form.current?.reset();
164 }, [fetcher.state, fetcher.data]);
165 const live = takesMessages(run.kind) && isActiveRun(run.status);
166 if (run.number == null) return null;
167 return (
168 <Dialog open={open} onOpenChange={setOpen}>
169 <DialogTrigger className="inline-flex items-center gap-1.5 rounded-md border border-line px-2.5 py-1 text-xs text-muted transition-colors hover:border-line-strong hover:text-fg">
170 <MessageSquare size={11} />
171 Message
172 </DialogTrigger>
173 <DialogContent>
174 <DialogHeader>
175 <DialogTitle>Message the agent on #{run.number}</DialogTitle>
176 <DialogDescription>
177 {live
178 ? "It reads this at its next step, between tool calls, without starting over."
179 : "This run does not read messages while it works. Your message waits on the pull request and is given to the agent's next run there."}
180 </DialogDescription>
181 </DialogHeader>
182 <fetcher.Form ref={form} method="post" action={actionUrl(run)} className="space-y-3">
183 <input type="hidden" name="intent" value="message" />
184 <input type="hidden" name="number" value={run.number} />
185 <textarea
186 name="body"
187 required
188 rows={4}
189 autoComplete="off"
190 data-1p-ignore
191 placeholder="Keep the old flag working too…"
192 className="w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none placeholder:text-faint focus:border-accent-dim"
193 />
194 {fetcher.data?.error && <p className="text-sm text-danger">{fetcher.data.error}</p>}
195 {fetcher.data?.ok && fetcher.data.notice && <p className="text-sm text-accent">{fetcher.data.notice}</p>}
196 <div className="flex justify-end">
197 <SubmitButton
198 fetcher={fetcher}
199 pending="Sending…"
200 className="inline-flex items-center gap-2 rounded-md bg-fg px-3.5 py-2 text-sm font-medium text-bg hover:bg-white disabled:opacity-50"
201 >
202 Send
203 </SubmitButton>
204 </div>
205 </fetcher.Form>
206 </DialogContent>
207 </Dialog>
208 );
209}
210
211/** What a run is on, as a link: its pull request, or the plan it writes. */
212function RunTarget({ run, showRepo }: { run: AgentRun; showRepo?: boolean }) {
213 const base = `/${run.repo.namespace}/${run.repo.name}`;
214 const repo = showRepo ? (
215 <Link to={base} className="font-mono text-xs text-faint hover:text-muted">
216 {run.repo.namespace}/{run.repo.name}
217 </Link>
218 ) : null;
219 if (run.number != null) {
220 return (
221 <span className="flex min-w-0 flex-wrap items-baseline gap-x-2">
222 {repo}
223 <Link to={`${base}/pull/${run.number}`} prefetch="intent" className="min-w-0 truncate font-medium hover:text-accent">
224 {run.title ?? "Pull request"} <span className="font-normal text-faint">#{run.number}</span>
225 </Link>
226 </span>
227 );
228 }
229 return (
230 <span className="flex min-w-0 flex-wrap items-baseline gap-x-2">
231 {repo}
232 <Link to={`${base}/plans`} className="min-w-0 truncate font-medium hover:text-accent">
233 {run.title ?? "A plan"}
234 </Link>
235 </span>
236 );
237}
238
239/** One run, live or finished, with what can be done about it. */
240export function RunCard({ run, member, showRepo }: { run: AgentRun; member: boolean; showRepo?: boolean }) {
241 const base = `/${run.repo.namespace}/${run.repo.name}`;
242 const active = isActiveRun(run.status);
243 const cost = formatCost(run.costUsd);
244 return (
245 <li
246 className={`rounded-xl border bg-surface p-4 transition-colors hover:border-line-strong ${
247 active ? "border-merged/30" : "border-line"
248 }`}
249 >
250 <div className="flex flex-wrap items-center gap-x-3 gap-y-2">
251 <Avatar name={run.agent} size={22} />
252 <span className="text-sm font-medium">{run.agent}</span>
253 <KindLabel kind={run.kind} />
254 <span className="min-w-0 grow">
255 <RunTarget run={run} showRepo={showRepo} />
256 </span>
257 <RunStatusBadge status={run.status} />
258 </div>
259 {run.step && (
260 <p className={`mt-2.5 truncate font-mono text-xs ${active ? "text-fg/85" : "text-muted"}`} title={shownStep(run.step)}>
261 {active && <span className="mr-1.5 inline-block size-1.5 animate-pulse rounded-full bg-merged align-middle" />}
262 {shownStep(run.step)}
263 </p>
264 )}
265 {run.status === "failed" && run.error && <p className="mt-1.5 text-xs text-danger">{run.error}</p>}
266 <div className="mt-2.5 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted">
267 <span className="flex items-center gap-1">
268 <Clock size={12} />
269 {run.startedAt || active ? <Elapsed from={run.startedAt ?? run.createdAt} to={run.finishedAt} /> : "—"}
270 </span>
271 {cost && (
272 <span className="flex items-center gap-1" title={active ? "So far, as the harness reports it" : "As the harness reported it"}>
273 <Coins size={12} />
274 {cost}
275 {active && " so far"}
276 </span>
277 )}
278 {run.model && <span className="font-mono">{run.model}</span>}
279 {run.startedBy && <span>started by {run.startedBy}</span>}
280 <span>
281 <TimeAgo at={run.createdAt} />
282 </span>
283 <span className="grow" />
284 <Link to={`${base}/agents/runs/${run.id}`} className="hover:text-fg">
285 {run.stepCount} {run.stepCount === 1 ? "step" : "steps"}
286 </Link>
287 {run.number != null && (
288 <Link to={`${base}/sessions/${run.number}`} className="hover:text-fg">
289 Session
290 </Link>
291 )}
292 {member && active && <MessageRun run={run} />}
293 {member && active && <StopRun run={run} compact />}
294 </div>
295 </li>
296 );
297}
298
299/** The runs that are on now, newest first, then the rest. */
300export function splitRuns(runs: AgentRun[]): { live: AgentRun[]; done: AgentRun[] } {
301 return {
302 live: runs.filter((run) => isActiveRun(run.status)),
303 done: runs.filter((run) => !isActiveRun(run.status)),
304 };
305}
306
307type Live = { runs: AgentRun[]; member: boolean };
308
309/**
310 * The runs of a project, fetched from its `agents.json`, every few seconds
311 * while any is running. For pages whose own loader does not have them.
312 */
313export function useRuns(owner: string, repo: string, query: Record<string, string>): Live | null {
314 const fetcher = useFetcher<Live>();
315 const search = new URLSearchParams(query).toString();
316 const url = `/${owner}/${repo}/agents.json?${search}`;
317 const { load } = fetcher;
318 useEffect(() => {
319 load(url);
320 }, [load, url]);
321 const live = fetcher.data?.runs.some((run) => isActiveRun(run.status)) ?? false;
322 useEffect(() => {
323 if (!live) return;
324 const timer = setInterval(() => {
325 if (document.visibilityState === "visible") load(url);
326 }, LIVE_MS);
327 return () => clearInterval(timer);
328 }, [live, load, url]);
329 return fetcher.data ?? null;
330}
331
332/** "Agent confidence: Low — tests not added, 3 revisions", and what the agent said it was unsure of. */
333export function ConfidenceLine({ confidence }: { confidence: Confidence }) {
334 const tone = confidence.level === "low" ? "text-danger" : confidence.level === "medium" ? "text-warn" : "text-accent";
335 const level = { low: "Low", medium: "Medium", high: "High" }[confidence.level];
336 return (
337 <div className="mt-3 text-xs leading-5">
338 <p className="flex items-start gap-2">
339 <Gauge size={13} className={`mt-1 shrink-0 ${tone}`} />
340 <span className="min-w-0">
341 <span className="text-muted">Agent confidence: </span>
342 <span className={`font-medium ${tone}`}>{level}</span>
343 {confidence.reasons.length > 0 && <span className="text-fg-soft"> — {confidence.reasons.join(", ")}</span>}
344 </span>
345 </p>
346 {confidence.uncertainAbout.length > 0 && (
347 <p className="mt-0.5 pl-[1.3125rem] text-muted">Unsure about: {confidence.uncertainAbout.join("; ")}</p>
348 )}
349 </div>
350 );
351}
352
353/**
354 * The agent on a pull request, near the top of its page: who is working
355 * on it, at what stage, what it is doing this minute, for how long and at
356 * what cost, with its session, and Stop and Message for members.
357 */
358export function AgentPanel({
359 owner,
360 repo,
361 number,
362 stage,
363 confidence,
364}: {
365 owner: string;
366 repo: string;
367 number: number;
368 stage?: Stage | null;
369 /** How sure g1t is of the change, once the agent has finished it. */
370 confidence?: Confidence | null;
371}) {
372 const data = useRuns(owner, repo, { number: String(number), limit: "5" });
373 const runs = data?.runs ?? [];
374 const member = data?.member ?? false;
375 const current = runs.find((run) => isActiveRun(run.status)) ?? runs[0];
376 if (!current) return null;
377 const active = isActiveRun(current.status);
378 const base = `/${owner}/${repo}`;
379 const cost = formatCost(current.costUsd);
380 const spent = formatCost(runs.reduce((sum, run) => sum + (run.costUsd ?? 0), 0) || null);
381 return (
382 <section
383 aria-label="Agent"
384 className={`mt-4 rounded-2xl bg-surface p-4 ring-1 ${active ? "ring-merged/40" : "ring-line"}`}
385 >
386 <div className="flex flex-wrap items-center gap-x-3 gap-y-2">
387 <Avatar name={current.agent} size={24} />
388 <p className="text-sm">
389 <span className="font-medium">{current.agent}</span>{" "}
390 {current.startedBy && current.startedBy !== current.agent && (
391 current.startedBy === "g1t" ? (
392 <span className="text-muted">
393 (started by <span className="text-fg">g1t</span>){" "}
394 </span>
395 ) : (
396 <span className="text-muted">
397 on behalf of <span className="text-fg">{current.startedBy}</span>{" "}
398 </span>
399 )
400 )}
401 <span className="text-muted">
402 {active
403 ? `is ${RUN_KIND_LABEL[current.kind].toLowerCase()}`
404 : `${current.status === "succeeded" ? "finished" : current.status} its ${RUN_KIND_LABEL[current.kind].toLowerCase()} run`}
405 </span>
406 </p>
407 {stage && (
408 <span className="flex items-center gap-2 text-xs text-muted">
409 <StageDots stage={stage} />
410 {STAGE_LABEL[stage]}
411 </span>
412 )}
413 <span className="grow" />
414 <RunStatusBadge status={current.status} />
415 </div>
416 {current.step && (
417 <p className="mt-3 truncate rounded-lg bg-bg px-3 py-2 font-mono text-xs text-fg/85 ring-1 ring-line" title={shownStep(current.step)}>
418 {active && <span className="mr-2 inline-block size-1.5 animate-pulse rounded-full bg-merged align-middle" />}
419 {shownStep(current.step)}
420 </p>
421 )}
422 {confidence && <ConfidenceLine confidence={confidence} />}
423 <div className="mt-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted">
424 <span className="flex items-center gap-1">
425 <Clock size={12} />
426 <Elapsed from={current.startedAt ?? current.createdAt} to={current.finishedAt} />
427 </span>
428 {cost && (
429 <span className="flex items-center gap-1">
430 <Coins size={12} />
431 {cost}
432 {active ? " so far" : ""}
433 {runs.length > 1 && spent && ` · ${spent} over ${runs.length} runs`}
434 </span>
435 )}
436 {current.model && <span className="font-mono">{current.model}</span>}
437 <span className="grow" />
438 <Link to={`${base}/agents/runs/${current.id}`} className="hover:text-fg">
439 Run
440 </Link>
441 <Link to={`${base}/sessions/${number}`} className="hover:text-fg">
442 Session
443 </Link>
444 {member && active && <MessageRun run={current} />}
445 {member && active && <StopRun run={current} />}
446 </div>
447 {member && <RunAudit owner={owner} repo={repo} runIds={runs.map((run) => run.id)} live={active} />}
448 </section>
449 );
450}
451
452/** A small "agent working" mark for a row of the pull request list. */
453export function AgentBadge({ run }: { run: AgentRun | undefined }) {
454 if (!run) return null;
455 return (
456 <span
457 title={run.step ? shownStep(run.step) : undefined}
458 className="inline-flex shrink-0 items-center gap-1 rounded-full border border-merged/40 bg-merged/10 px-2 py-0.5 text-xs text-merged"
459 >
460 <Loader2 size={11} className="animate-spin" />
461 {RUN_KIND_LABEL[run.kind]}
462 </span>
463 );
464}
465
466/** The active run on each pull request of a project, by number. */
467export function useActiveRuns(owner: string, repo: string): Map<number, AgentRun> {
468 const data = useRuns(owner, repo, { active: "1", limit: "100" });
469 return useMemo(() => {
470 const byNumber = new Map<number, AgentRun>();
471 for (const run of data?.runs ?? []) {
472 if (run.number != null && !byNumber.has(run.number)) byNumber.set(run.number, run);
473 }
474 return byNumber;
475 }, [data]);
476}
477
478/** Nothing running, said plainly. */
479export function Idle({ children }: { children: ReactNode }) {
480 return (
481 <p className="flex items-start gap-2 rounded-xl border border-dashed border-line px-4 py-6 text-sm text-muted">
482 <CircleSlash size={15} className="mt-0.5 shrink-0 text-faint" />
483 {/* One run of text: a link inside it stays in the sentence. */}
484 <span className="min-w-0">{children}</span>
485 </p>
486 );
487}
488
489/**
490 * What the agent on a pull request is doing this minute, as one line, for
491 * an issue's sidebar: which agent picked the issue up, and where it is.
492 */
493export function AgentStepLine({ owner, repo, number }: { owner: string; repo: string; number: number }) {
494 const data = useRuns(owner, repo, { number: String(number), active: "1", limit: "1" });
495 const run = data?.runs[0];
496 if (!run?.step) return null;
497 return (
498 <span className="mt-0.5 block truncate font-mono text-[0.6875rem] text-faint" title={shownStep(run.step)}>
499 {RUN_KIND_LABEL[run.kind]}: {shownStep(run.step)}
500 </span>
501 );
502}