Skip to content

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

534 lines21,181 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
307export type 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. A page whose loader read them passes them as
312 * `initial`, so they come with the page instead of appearing after it;
313 * without, they are fetched once it is up.
314 */
315export function useRuns(
316 owner: string,
317 repo: string,
318 query: Record<string, string>,
319 initial?: Live | null,
320): Live | null {
321 const fetcher = useFetcher<Live>();
322 const search = new URLSearchParams(query).toString();
323 const url = `/${owner}/${repo}/agents.json?${search}`;
324 const { load } = fetcher;
325 const loaded = initial !== undefined;
326 useEffect(() => {
327 if (!loaded) load(url);
328 }, [load, url, loaded]);
329 // The newer of the page's runs and the last poll: the page revalidating
330 // replaces an older poll, and a poll the runs the page came with.
331 const [newest, setNewest] = useState<Live | null>(initial ?? null);
332 useEffect(() => {
333 if (loaded) setNewest(initial ?? null);
334 }, [loaded, initial]);
335 useEffect(() => {
336 if (fetcher.data) setNewest(fetcher.data);
337 }, [fetcher.data]);
338 const data = loaded ? newest : (fetcher.data ?? null);
339 const live = data?.runs.some((run) => isActiveRun(run.status)) ?? false;
340 useEffect(() => {
341 if (!live) return;
342 const timer = setInterval(() => {
343 if (document.visibilityState === "visible") load(url);
344 }, LIVE_MS);
345 return () => clearInterval(timer);
346 }, [live, load, url]);
347 return data;
348}
349
350/** "Agent confidence: Low — tests not added, 3 revisions", and what the agent said it was unsure of. */
351export function ConfidenceLine({ confidence }: { confidence: Confidence }) {
352 const tone = confidence.level === "low" ? "text-danger" : confidence.level === "medium" ? "text-warn" : "text-accent";
353 const level = { low: "Low", medium: "Medium", high: "High" }[confidence.level];
354 return (
355 <div className="mt-3 text-xs leading-5">
356 <p className="flex items-start gap-2">
357 <Gauge size={13} className={`mt-1 shrink-0 ${tone}`} />
358 <span className="min-w-0">
359 <span className="text-muted">Agent confidence: </span>
360 <span className={`font-medium ${tone}`}>{level}</span>
361 {confidence.reasons.length > 0 && <span className="text-fg-soft"> — {confidence.reasons.join(", ")}</span>}
362 </span>
363 </p>
364 {confidence.uncertainAbout.length > 0 && (
365 <p className="mt-0.5 pl-[1.3125rem] text-muted">Unsure about: {confidence.uncertainAbout.join("; ")}</p>
366 )}
367 </div>
368 );
369}
370
371/**
372 * The agent on a pull request, near the top of its page: who is working
373 * on it, at what stage, what it is doing this minute, for how long and at
374 * what cost, with its session, and Stop and Message for members.
375 */
376export function AgentPanel({
377 owner,
378 repo,
379 number,
380 stage,
381 confidence,
382 runs: initial,
383}: {
384 owner: string;
385 repo: string;
386 number: number;
387 stage?: Stage | null;
388 /** How sure g1t is of the change, once the agent has finished it. */
389 confidence?: Confidence | null;
390 /** Its latest five runs, from the page's loader, so the panel comes with the page. */
391 runs?: Live | null;
392}) {
393 const data = useRuns(owner, repo, { number: String(number), limit: "5" }, initial);
394 const runs = data?.runs ?? [];
395 const member = data?.member ?? false;
396 const current = runs.find((run) => isActiveRun(run.status)) ?? runs[0];
397 if (!current) return null;
398 const active = isActiveRun(current.status);
399 const base = `/${owner}/${repo}`;
400 const cost = formatCost(current.costUsd);
401 const spent = formatCost(runs.reduce((sum, run) => sum + (run.costUsd ?? 0), 0) || null);
402 return (
403 <section
404 aria-label="Agent"
405 className={`mt-4 rounded-2xl bg-surface p-4 ring-1 ${active ? "ring-merged/40" : "ring-line"}`}
406 >
407 <div className="flex flex-wrap items-center gap-x-3 gap-y-2">
408 <Avatar name={current.agent} size={24} />
409 <p className="text-sm">
410 <span className="font-medium">{current.agent}</span>{" "}
411 {current.startedBy && current.startedBy !== current.agent && (
412 current.startedBy === "g1t" ? (
413 <span className="text-muted">
414 (started by <span className="text-fg">g1t</span>){" "}
415 </span>
416 ) : (
417 <span className="text-muted">
418 on behalf of <span className="text-fg">{current.startedBy}</span>{" "}
419 </span>
420 )
421 )}
422 <span className="text-muted">
423 {active
424 ? `is ${RUN_KIND_LABEL[current.kind].toLowerCase()}`
425 : `${current.status === "succeeded" ? "finished" : current.status} its ${RUN_KIND_LABEL[current.kind].toLowerCase()} run`}
426 </span>
427 </p>
428 {stage && (
429 <span className="flex items-center gap-2 text-xs text-muted">
430 <StageDots stage={stage} />
431 {STAGE_LABEL[stage]}
432 </span>
433 )}
434 <span className="grow" />
435 <RunStatusBadge status={current.status} />
436 </div>
437 {current.step && (
438 <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)}>
439 {active && <span className="mr-2 inline-block size-1.5 animate-pulse rounded-full bg-merged align-middle" />}
440 {shownStep(current.step)}
441 </p>
442 )}
443 {confidence && <ConfidenceLine confidence={confidence} />}
444 <div className="mt-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted">
445 <span className="flex items-center gap-1">
446 <Clock size={12} />
447 <Elapsed from={current.startedAt ?? current.createdAt} to={current.finishedAt} />
448 </span>
449 {cost && (
450 <span className="flex items-center gap-1">
451 <Coins size={12} />
452 {cost}
453 {active ? " so far" : ""}
454 {runs.length > 1 && spent && ` · ${spent} over ${runs.length} runs`}
455 </span>
456 )}
457 {current.model && <span className="font-mono">{current.model}</span>}
458 <span className="grow" />
459 <Link to={`${base}/agents/runs/${current.id}`} className="hover:text-fg">
460 Run
461 </Link>
462 <Link to={`${base}/sessions/${number}`} className="hover:text-fg">
463 Session
464 </Link>
465 {member && active && <MessageRun run={current} />}
466 {member && active && <StopRun run={current} />}
467 </div>
468 {member && <RunAudit owner={owner} repo={repo} runIds={runs.map((run) => run.id)} live={active} />}
469 </section>
470 );
471}
472
473/** A small "agent working" mark for a row of the pull request list. */
474export function AgentBadge({ run }: { run: AgentRun | undefined }) {
475 if (!run) return null;
476 return (
477 <span
478 title={run.step ? shownStep(run.step) : undefined}
479 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"
480 >
481 <Loader2 size={11} className="animate-spin" />
482 {RUN_KIND_LABEL[run.kind]}
483 </span>
484 );
485}
486
487/** The active run on each pull request of a project, by number. */
488export function useActiveRuns(owner: string, repo: string, initial?: Live | null): Map<number, AgentRun> {
489 const data = useRuns(owner, repo, { active: "1", limit: "100" }, initial);
490 return useMemo(() => {
491 const byNumber = new Map<number, AgentRun>();
492 for (const run of data?.runs ?? []) {
493 if (run.number != null && !byNumber.has(run.number)) byNumber.set(run.number, run);
494 }
495 return byNumber;
496 }, [data]);
497}
498
499/** Nothing running, said plainly. */
500export function Idle({ children }: { children: ReactNode }) {
501 return (
502 <p className="flex items-start gap-2 rounded-xl border border-dashed border-line px-4 py-6 text-sm text-muted">
503 <CircleSlash size={15} className="mt-0.5 shrink-0 text-faint" />
504 {/* One run of text: a link inside it stays in the sentence. */}
505 <span className="min-w-0">{children}</span>
506 </p>
507 );
508}
509
510/**
511 * What the agent on a pull request is doing this minute, as one line, for
512 * an issue's sidebar: which agent picked the issue up, and where it is.
513 */
514export function AgentStepLine({
515 owner,
516 repo,
517 number,
518 runs: initial,
519}: {
520 owner: string;
521 repo: string;
522 number: number;
523 /** The project's active runs, from the page's loader, so the line comes with the page. */
524 runs?: Live | null;
525}) {
526 const data = useRuns(owner, repo, { number: String(number), active: "1", limit: "1" }, initial);
527 const run = data?.runs.find((run) => run.number === number && isActiveRun(run.status));
528 if (!run?.step) return null;
529 return (
530 <span className="mt-0.5 block truncate font-mono text-[0.6875rem] text-faint" title={shownStep(run.step)}>
531 {RUN_KIND_LABEL[run.kind]}: {shownStep(run.step)}
532 </span>
533 );
534}