g1t/apps/web/app/routes/workspace/agents.tsx
| 1 | import { Coins } from "lucide-react"; |
| 2 | import { Link, data } from "react-router"; |
| 3 | |
| 4 | import type { AgentRun } from "@g1t/contracts"; |
| 5 | |
| 6 | import type { Route } from "./+types/agents"; |
| 7 | import { page } from "../../lib/meta"; |
| 8 | import { Idle, RunCard, formatCost, splitRuns, useLiveRefresh } from "../../components/agents"; |
| 9 | import { agents } from "../../lib/services.server"; |
| 10 | import { getViewer, roleIn, unwrap } from "../../lib/session.server"; |
| 11 | |
| 12 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 13 | return page(args, { title: `Agent fleet · ${params.owner} · g1t` }); |
| 14 | } |
| 15 | |
| 16 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 17 | const viewer = getViewer(context); |
| 18 | if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 }); |
| 19 | const runs = await agents.listRuns(viewer, { workspace: params.owner, limit: 150 }); |
| 20 | return { runs: unwrap(runs) }; |
| 21 | } |
| 22 | |
| 23 | /** What the runs shown cost, by project. */ |
| 24 | function byProject(runs: AgentRun[]): { repo: string; runs: number; cost: number }[] { |
| 25 | const totals = new Map<string, { repo: string; runs: number; cost: number }>(); |
| 26 | for (const run of runs) { |
| 27 | const repo = `${run.repo.namespace}/${run.repo.name}`; |
| 28 | const total = totals.get(repo) ?? { repo, runs: 0, cost: 0 }; |
| 29 | total.runs += 1; |
| 30 | total.cost += run.costUsd ?? 0; |
| 31 | totals.set(repo, total); |
| 32 | } |
| 33 | return [...totals.values()].sort((a, b) => b.cost - a.cost || b.runs - a.runs); |
| 34 | } |
| 35 | |
| 36 | export default function Fleet({ loaderData, params }: Route.ComponentProps) { |
| 37 | const { runs } = loaderData; |
| 38 | const { live, done } = splitRuns(runs); |
| 39 | useLiveRefresh(live.length > 0); |
| 40 | const projects = byProject(runs); |
| 41 | const failed = done.filter((run) => run.status === "failed").length; |
| 42 | return ( |
| 43 | <div className="space-y-10"> |
| 44 | <div className="grid gap-3 sm:grid-cols-3"> |
| 45 | <div className="rounded-xl border border-line bg-surface p-4"> |
| 46 | <p className="text-xs text-muted">At work now</p> |
| 47 | <p className="mt-1 text-2xl font-semibold tabular-nums">{live.length}</p> |
| 48 | </div> |
| 49 | <div className="rounded-xl border border-line bg-surface p-4"> |
| 50 | <p className="text-xs text-muted">Recent runs</p> |
| 51 | <p className="mt-1 text-2xl font-semibold tabular-nums">{runs.length}</p> |
| 52 | {failed > 0 && <p className="mt-0.5 text-xs text-danger">{failed} failed</p>} |
| 53 | </div> |
| 54 | <div className="rounded-xl border border-line bg-surface p-4"> |
| 55 | <p className="flex items-center gap-1 text-xs text-muted"> |
| 56 | <Coins size={12} /> |
| 57 | Their cost, as reported |
| 58 | </p> |
| 59 | <p className="mt-1 text-2xl font-semibold tabular-nums"> |
| 60 | {formatCost(runs.reduce((sum, run) => sum + (run.costUsd ?? 0), 0)) ?? "$0.00"} |
| 61 | </p> |
| 62 | <Link to={`/${params.owner}/-/usage`} className="mt-0.5 block text-xs text-muted hover:text-fg"> |
| 63 | What was charged |
| 64 | </Link> |
| 65 | </div> |
| 66 | </div> |
| 67 | |
| 68 | <section> |
| 69 | <h2 className="text-sm font-medium">Now</h2> |
| 70 | {live.length === 0 ? ( |
| 71 | <div className="mt-3"> |
| 72 | <Idle>No agent is at work in any of {params.owner}'s projects right now.</Idle> |
| 73 | </div> |
| 74 | ) : ( |
| 75 | <ul className="mt-3 space-y-3"> |
| 76 | {live.map((run) => ( |
| 77 | <RunCard key={run.id} run={run} member showRepo /> |
| 78 | ))} |
| 79 | </ul> |
| 80 | )} |
| 81 | </section> |
| 82 | |
| 83 | {projects.length > 0 && ( |
| 84 | <section> |
| 85 | <h2 className="text-sm font-medium">By project</h2> |
| 86 | <ul className="mt-3 divide-y divide-line rounded-xl border border-line bg-surface"> |
| 87 | {projects.map((project) => ( |
| 88 | <li key={project.repo} className="flex items-center gap-3 px-4 py-2.5 text-sm"> |
| 89 | <Link to={`/${project.repo}/agents`} className="min-w-0 grow truncate font-mono hover:text-accent"> |
| 90 | {project.repo} |
| 91 | </Link> |
| 92 | <span className="text-xs text-muted"> |
| 93 | {project.runs} {project.runs === 1 ? "run" : "runs"} |
| 94 | </span> |
| 95 | <span className="w-16 text-right font-mono text-xs tabular-nums">{formatCost(project.cost) ?? "—"}</span> |
| 96 | </li> |
| 97 | ))} |
| 98 | </ul> |
| 99 | </section> |
| 100 | )} |
| 101 | |
| 102 | {done.length > 0 && ( |
| 103 | <section> |
| 104 | <h2 className="text-sm font-medium">Recently finished</h2> |
| 105 | <ul className="mt-3 space-y-3"> |
| 106 | {done.slice(0, 30).map((run) => ( |
| 107 | <RunCard key={run.id} run={run} member showRepo /> |
| 108 | ))} |
| 109 | </ul> |
| 110 | </section> |
| 111 | )} |
| 112 | </div> |
| 113 | ); |
| 114 | } |