Skip to content

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

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