Skip to content

g1t/apps/web/app/routes/workspace/agents.tsx

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