g1t/apps/web/app/routes/home.tsx

678 lines28,046 bytesCodeBlame
1import { env } from "cloudflare:workers";
2import { Suspense, lazy } from "react";
3import { type ShouldRevalidateFunctionArgs, data, redirect } from "react-router";
4
5import {
6 type Confidence,
7 type Lifecycle,
8 type Pull,
9 type Repo,
10 type RepoPath,
11 REPO_ROLE_LABELS,
12 isActiveRun,
13 workOwner,
14} from "@g1t/contracts";
15
16import type { Route } from "./+types/home";
17import { page } from "../lib/meta";
18import { WORKSPACE_COOKIE, chosenWorkspace } from "../lib/workspace-choice";
19import {
20 type ActivityItem,
21 type Need,
22 TIME,
23 agentHours,
24 dailyBuckets,
25 eventItem,
26 greetingFor,
27 groupActivity,
28 hourIn,
29 isAgent,
30 rankNeeds,
31 readCookie,
32 stuckMinutes,
33} from "../lib/mission";
34import {
35 type Fact,
36 type Merged,
37 type NeedRow,
38 type QuickAction,
39 RUN_LABEL,
40 confidenceAsk,
41 dateLine,
42 dayKey,
43 landedToday,
44 needPathKey,
45 pullFacts,
46 reachesBack,
47 reasonFor,
48 summaryLine,
49 usd,
50 waitingRows,
51 weekOf,
52 who,
53 whyFor,
54 withConfidence,
55} from "../lib/mission-control";
56import { type NotStarted, chosenRepo, delegateForm, issuePath, notStarted } from "../lib/delegate";
57import { Landing } from "../components/landing";
58import { Skeleton, SkeletonRows } from "../components/ui/skeleton";
59import {
60 agents,
61 billing,
62 deployments,
63 events as eventLog,
64 identity,
65 projects as projectsApi,
66 repos as reposApi,
67 work,
68} from "../lib/services.server";
69import { assertSameOrigin, getViewer, requireUser } from "../lib/session.server";
70import { runners } from "../lib/runners.server";
71import { readableRepos } from "../lib/access.server";
72
73/** The viewer's time zone, which mission control sets, so the greeting fits their day. */
74const TZ_COOKIE = "g1t_tz";
75/** The most projects whose pull requests and events are read for the page. */
76const MAX_PROJECTS = 10;
77const EVENTS_PER_PROJECT = 80;
78/** How many pull requests work lists at once (`LIST_PAGE` in services/work). */
79const PULL_PAGE = 100;
80
81export function meta(args: Route.MetaArgs) {
82 return page(args, {
83 title: "g1t — where people and agents ship software together",
84 description:
85 "The open-source git platform for people and agents: issues, pull requests and review, agents you assign like teammates, a merge queue that keeps main green, and deployments to the edge. Priced at cost plus 20%, never per seat.",
86 });
87}
88
89export function headers({ loaderHeaders }: Route.HeadersArgs) {
90 const out = new Headers();
91 const timing = loaderHeaders.get("Server-Timing");
92 if (timing) out.set("Server-Timing", timing);
93 const cookie = loaderHeaders.get("Set-Cookie");
94 if (cookie) out.set("Set-Cookie", cookie);
95 return out;
96}
97
98/**
99 * Switching tabs, the sort or the activity list changes only the address:
100 * everything is already loaded. A refresh, a form, or anything else loads
101 * again as usual.
102 */
103export function shouldRevalidate({ currentUrl, nextUrl, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) {
104 if (!formMethod && currentUrl.pathname === nextUrl.pathname && currentUrl.search !== nextUrl.search) return false;
105 return defaultShouldRevalidate;
106}
107
108/** Where a need came from, so its row can say what is known about it. */
109type Extra = Partial<Pick<NeedRow, "repo" | "ref" | "by" | "for" | "facts" | "quick" | "link" | "open">> & {
110 /** How sure g1t is of the agent's change, for a pull request. */
111 confidence?: Confidence | null;
112};
113
114/** What the composer said back, when the issue opened but the agent did not start, or nothing opened. */
115export type DelegateResult = { error: string; notStarted: null } | { error: null; notStarted: NotStarted };
116
117/**
118 * "Put an agent on it": opens an issue in one of the viewer's projects and
119 * puts g1t on it, then lands on the issue. When the agent could not
120 * start, the issue is still open, and the composer says why and where to
121 * fix it.
122 */
123export async function action({ request, context }: Route.ActionArgs) {
124 assertSameOrigin(request);
125 const user = requireUser(context, request);
126 const form = await request.formData();
127 if (form.get("intent") !== "delegate") return data<DelegateResult>({ error: "Nothing to do.", notStarted: null }, { status: 400 });
128 const projects = await reposApi.list(user, { memberOnly: true }).catch(() => []);
129 const repo = chosenRepo(form.get("repo"), projects.map((repo) => ({ namespace: repo.namespace, name: repo.name })));
130 if (!repo) return data<DelegateResult>({ error: "Choose one of your projects.", notStarted: null }, { status: 400 });
131 const result = await env.RUNNER.delegate(user, repo, delegateForm(form));
132 if (!result.ok) return data<DelegateResult>({ error: result.error.message, notStarted: null }, { status: 400 });
133 const { issue, agent } = result.value;
134 const refused = notStarted(agent, repo, issue.number);
135 if (!refused) throw redirect(issuePath(repo, issue.number));
136 return data<DelegateResult>({ error: null, notStarted: refused });
137}
138
139export async function loader({ context, request }: Route.LoaderArgs) {
140 const viewer = getViewer(context);
141 if (!viewer) {
142 // The signed-out home page is about signing up; it lists no repositories.
143 return data({ signedIn: false as const });
144 }
145
146 const now = Date.now();
147 const weekAgo = now - 7 * TIME.DAY;
148 const started = Date.now();
149 const times: Record<string, number> = {};
150 // Each call is timed, and one that fails leaves its section empty rather
151 // than taking the page down.
152 const soft = <T,>(name: string, promise: Promise<T>): Promise<T | null> =>
153 promise.then(
154 (value) => ((times[name] = Date.now() - started), value),
155 (error) => {
156 console.warn(`home: ${name} failed`, error);
157 times[name] = Date.now() - started;
158 return null;
159 },
160 );
161 const okOr = <T,>(result: { ok: true; value: T } | { ok: false } | null): T | null => (result?.ok ? result.value : null);
162
163 const cookies = request.headers.get("cookie");
164 const tz = readCookie(cookies, TZ_COOKIE);
165 const memberships = viewer.workspaces ?? [];
166 // The workspace you chose, as the sidebar shows it (lib/workspace-choice.ts).
167 const slug = chosenWorkspace(memberships, readCookie(cookies, WORKSPACE_COOKIE))?.slug ?? null;
168 const username = viewer.username;
169
170 const reposP = soft("repos", reposApi.list(viewer, { memberOnly: true }));
171 // Workflow jobs stuck waiting for a self-hosted runner that is not there.
172 const stuckJobsP = soft("stuck_jobs", runners.stuck(viewer));
173 // The chosen workspace's projects: their open and recently merged pull
174 // requests, all in one call to work, and their recent events, read as
175 // soon as the projects are known.
176 const perRepoP = reposP.then(async (repos) => {
177 const chosen = (repos ?? [])
178 .filter((repo) => slug != null && repo.namespace.toLowerCase() === slug.toLowerCase())
179 .slice(0, MAX_PROJECTS);
180 const [batch, logs] = await Promise.all([
181 work.pullsForRepos(chosen.map((repo) => repo.id), viewer, PULL_PAGE).catch(() => []),
182 Promise.all(chosen.map((repo) => eventLog.list({ repoId: repo.id, limit: EVENTS_PER_PROJECT }).catch(() => null))),
183 ]);
184 const byId = new Map(batch.map((entry) => [entry.repoId, entry]));
185 return Promise.all(
186 chosen.map(async (repo, index) => {
187 const found = byId.get(repo.id);
188 if (found) return { repo, pulls: found.open.slice(0, 60), closed: found.closed, events: logs[index] };
189 // A fork, which the batch leaves out: asked on its own.
190 const path = { namespace: repo.namespace, name: repo.name };
191 const [pulls, closed] = await Promise.all([
192 work.listPulls(path, viewer, "open").catch(() => null),
193 work.listPulls(path, viewer, "closed").catch(() => null),
194 ]);
195 return {
196 repo,
197 pulls: pulls?.ok ? pulls.value.slice(0, 60) : null,
198 closed: closed?.ok ? closed.value : null,
199 events: logs[index],
200 };
201 }),
202 );
203 });
204
205 const [repos, perRepo, active, models, profile, runs, overview, usage, projectList, memories, invitations, tokens, myTokens] = await Promise.all([
206 reposP,
207 soft("projects", perRepoP),
208 soft("pulls", work.listActivePulls(viewer)),
209 slug ? soft("models", env.RUNNER.modelAccess(slug)) : null,
210 soft("profile", identity.profile(username)),
211 slug ? soft("runs", agents.listRuns(viewer, { workspace: slug, limit: 150 })) : null,
212 slug ? soft("deploys", deployments.overview(slug, viewer)) : null,
213 slug ? soft("usage", billing.usage(slug, viewer, new Date(weekAgo - TIME.DAY).toISOString())) : null,
214 slug ? soft("projectList", projectsApi.list(slug, viewer)) : null,
215 slug ? soft("memories", agents.listMemories(viewer, slug, null)) : null,
216 // Repositories someone has invited the viewer to.
217 soft("invitations", identity.myRepoInvitations(viewer)),
218 // Model tokens over the last six weeks: the workspace's, and yours.
219 slug ? soft("tokens", billing.tokenUsage(slug, viewer)) : null,
220 slug ? soft("my_tokens", billing.tokenUsage(slug, viewer, { person: username })) : null,
221 ]);
222
223 const repoList = repos ?? [];
224 // Each pull request is shown under its repository. Most are in the
225 // viewer's own, already listed; the rest are looked up once each.
226 const known = new Map<string, Repo>(repoList.map((repo) => [repo.id, repo]));
227 const missing = [...new Set((active ?? []).map(({ pull }) => pull.repoId))].filter((id) => !known.has(id));
228 for (const repo of await readableRepos(missing, viewer)) known.set(repo.id, repo);
229 const pathOf = (repo: Repo): RepoPath => ({ namespace: repo.namespace, name: repo.name });
230 const inWorkspace = (repo: RepoPath) => slug != null && repo.namespace.toLowerCase() === slug.toLowerCase();
231
232 const activeList = (active ?? []).flatMap((item) => {
233 const repo = known.get(item.pull.repoId);
234 return repo ? [{ ...item, repo }] : [];
235 });
236 const runList = okOr(runs ?? null) ?? [];
237 const liveRuns = runList.filter((run) => isActiveRun(run.status));
238 const overviewList = okOr(overview ?? null);
239 const projectsOk = okOr(projectList ?? null);
240 const runsOn = (repo: RepoPath, number: number) =>
241 runList.filter(
242 (run) => run.number === number && `${run.repo.namespace}/${run.repo.name}`.toLowerCase() === `${repo.namespace}/${repo.name}`.toLowerCase(),
243 );
244
245 const lower = username.toLowerCase();
246 const openPulls = (perRepo ?? []).flatMap(({ repo, pulls }) => (pulls ?? []).map((pull) => ({ pull, repo })));
247 // Never your own, nor one g1t made for you.
248 const reviewRequested = openPulls.filter(
249 ({ pull }) =>
250 pull.status === "open" &&
251 workOwner(pull).username.toLowerCase() !== lower &&
252 pull.reviewers.some((name) => name.toLowerCase() === lower),
253 );
254
255 // --- Needs you ------------------------------------------------------------
256 const needs: Need[] = [];
257 const extras = new Map<string, Extra>();
258 /** What every pull request's row shares: where it is, who is on it, what is known. */
259 const pullExtra = (pull: Pull, repo: Repo, lifecycle: Lifecycle | null): Extra => {
260 const base = `/${repo.namespace}/${repo.name}`;
261 const agentWork = isAgent(pull.agent);
262 return {
263 repo: pathOf(repo),
264 ref: `#${pull.number}`,
265 by: agentWork ? who(pull.agent) : who(pull.author.username),
266 for: agentWork ? workOwner(pull).username : null,
267 facts: pullFacts({
268 checkStatus: pull.checkStatus,
269 files: pull.files,
270 lifecycle,
271 runs: runsOn(repo, pull.number),
272 confidence: pull.confidence,
273 }),
274 open: pull.issue != null ? `${base}/issues/${pull.issue}` : `${base}/pull/${pull.number}?tab=changes`,
275 confidence: pull.confidence ?? null,
276 };
277 };
278 const pullAction = (repo: Repo, pull: Pull, quick: Omit<QuickAction, "to">): QuickAction => ({
279 ...quick,
280 to: `/${repo.namespace}/${repo.name}/pull/${pull.number}`,
281 });
282 const approve = (repo: Repo, pull: Pull) =>
283 pullAction(repo, pull, {
284 label: isAgent(pull.agent) ? "Approve the agent's change" : "Approve the change",
285 fields: { action: "comment", verdict: "approve", body: "" },
286 done: "Approved",
287 });
288
289 for (const invitation of invitations ?? []) {
290 if (invitation.status !== "pending") continue;
291 const key = `invitation:${invitation.id}`;
292 needs.push({
293 key,
294 kind: "invitation",
295 title: `${invitation.invited_by ?? "Someone"} invited you to ${invitation.repo}`,
296 detail: `With the ${REPO_ROLE_LABELS[invitation.role]} role. The invitation expires ${new Date(invitation.expires_at).toISOString().slice(0, 10)}.`,
297 to: `/${invitation.repo}/invitations`,
298 action: "Respond",
299 at: Date.parse(invitation.created_at),
300 where: invitation.repo,
301 });
302 const [namespace, name] = invitation.repo.split("/");
303 extras.set(key, {
304 repo: namespace && name ? { namespace, name } : null,
305 by: who(invitation.invited_by),
306 facts: [
307 { label: "Role", value: REPO_ROLE_LABELS[invitation.role], tone: null },
308 { label: "Expires", value: new Date(invitation.expires_at).toISOString().slice(0, 10), tone: null },
309 ],
310 });
311 }
312 for (const entry of overviewList ?? []) {
313 const latest = entry.latest;
314 if (latest?.kind === "production" && latest.status === "failed" && slug) {
315 const key = `deploy:${entry.slug}`;
316 const to = `/${slug}/${entry.slug}/deployments/${latest.id}`;
317 needs.push({
318 key,
319 kind: "deploy",
320 title: `Production build of ${entry.slug} failed`,
321 detail: latest.error ?? "The last build of the default branch failed. Production still serves the build before it.",
322 to,
323 action: "See the build",
324 at: Date.parse(latest.finishedAt ?? latest.createdAt),
325 where: `${slug}/${entry.slug}`,
326 });
327 const facts: Fact[] = [
328 { label: "Commit", value: latest.commit.slice(0, 7), tone: null },
329 {
330 label: "Production",
331 value: entry.production ? "Serving the build before" : "Not live yet",
332 tone: entry.production ? "good" : "warn",
333 },
334 ];
335 extras.set(key, {
336 repo: { namespace: slug, name: entry.slug },
337 by: who(latest.createdBy),
338 facts,
339 link: { label: "Deployment settings", to: `/${slug}/${entry.slug}/settings/deployments` },
340 });
341 }
342 }
343 for (const { pull, lifecycle, repo } of activeList) {
344 const where = `${repo.namespace}/${repo.name}#${pull.number}`;
345 const to = `/${repo.namespace}/${repo.name}/pull/${pull.number}`;
346 const key = `pull:${pull.id}`;
347 const extra = pullExtra(pull, repo, lifecycle);
348 // Held for its low confidence: the ask says so in a sentence of its own.
349 const lowConfidence = pull.confidence?.level === "low" ? pull.confidence : null;
350 if (lifecycle?.stage === "needs_you") {
351 const held = lowConfidence != null && /confidence in this change is low/i.test(lifecycle.detail);
352 const conflict = !held && /conflict/i.test(lifecycle.detail);
353 const need: Need = {
354 key,
355 kind: conflict ? "conflict" : "stalled",
356 title: pull.title,
357 detail: held && lowConfidence ? confidenceAsk(lowConfidence) : lifecycle.detail,
358 to,
359 action: conflict ? "Resolve" : "Decide",
360 at: Date.parse(pull.updatedAt),
361 where,
362 };
363 needs.push(need);
364 const reason = held ? "low_confidence" : reasonFor(need);
365 extras.set(key, {
366 ...extra,
367 quick:
368 reason === "needs_review" || reason === "low_confidence"
369 ? approve(repo, pull)
370 : /required checks? .*still fails?\b/i.test(lifecycle.detail)
371 ? pullAction(repo, pull, { label: "Re-run failed jobs", fields: { action: "rerun-failed" }, done: "Re-running" })
372 : null,
373 link: reason === "outside_guardrails" ? { label: "Raise the cap", to: `/${repo.namespace}/${repo.name}/settings/guardrails` } : null,
374 });
375 } else if (lifecycle?.stage === "ready") {
376 needs.push({
377 key,
378 kind: "ready",
379 title: pull.title,
380 detail: lowConfidence ? confidenceAsk(lowConfidence) : "Its required checks passed and it was approved. It lands when you merge it.",
381 to,
382 action: "Merge",
383 at: Date.parse(pull.updatedAt),
384 where,
385 });
386 extras.set(key, {
387 ...extra,
388 quick: pullAction(repo, pull, {
389 label: isAgent(pull.agent) ? "Merge the agent's change" : "Merge it",
390 fields: { action: "merge" },
391 done: "Merging",
392 }),
393 });
394 } else if (!lifecycle && pull.status === "open" && pull.checkStatus === "failed") {
395 // Taken out of the merge queue: its change failed combined with what was ahead.
396 needs.push({ key, kind: "checks", title: pull.title, detail: "It failed in the merge queue. Push a fix, then merge it again.", to, action: "See checks", at: Date.parse(pull.updatedAt), where });
397 extras.set(key, { ...extra, quick: null });
398 }
399 }
400 for (const { pull, repo } of reviewRequested) {
401 const key = `review:${pull.id}`;
402 needs.push({
403 key,
404 kind: "review",
405 title: pull.title,
406 detail: `${workOwner(pull).username} asked for your review.`,
407 to: `/${repo.namespace}/${repo.name}/pull/${pull.number}`,
408 action: "Review",
409 at: Date.parse(pull.updatedAt),
410 where: `${repo.namespace}/${repo.name}#${pull.number}`,
411 });
412 extras.set(key, { ...pullExtra(pull, repo, null), quick: approve(repo, pull) });
413 }
414 for (const job of (await stuckJobsP) ?? []) {
415 const key = `runner:${job.id}`;
416 const minutes = Math.max(10, Math.round((now - Date.parse(job.queuedAt)) / 60_000));
417 const [namespace, name] = job.repo.split("/");
418 needs.push({
419 key,
420 kind: "stuck",
421 title: `${job.name} is waiting for a self-hosted runner`,
422 detail: `No runner with labels ${job.labels} is online. Start one, or change the job's runs-on.`,
423 to: `/${job.repo}/actions/runs/${job.runId}`,
424 action: "Look",
425 at: Date.parse(job.queuedAt),
426 where: job.repo,
427 });
428 extras.set(key, {
429 repo: namespace && name ? { namespace, name } : undefined,
430 facts: [
431 { label: "Labels", value: job.labels, tone: null },
432 { label: "Waiting", value: `${minutes} min`, tone: "warn" },
433 ],
434 link: { label: "Runners", to: `/${namespace}/-/runners` },
435 });
436 }
437 for (const run of liveRuns) {
438 const minutes = stuckMinutes(run, now);
439 if (minutes == null) continue;
440 const key = `run:${run.id}`;
441 needs.push({
442 key,
443 kind: "stuck",
444 title: run.title ?? `${run.agent}'s run`,
445 detail: `${run.agent} has reported nothing for ${minutes} min${run.step ? `. Last: ${run.step}` : ""}.`,
446 to: `/${run.repo.namespace}/${run.repo.name}/agents/runs/${run.id}`,
447 action: "Look",
448 at: Date.parse(run.updatedAt),
449 where: `${run.repo.namespace}/${run.repo.name}${run.number != null ? `#${run.number}` : ""}`,
450 });
451 extras.set(key, {
452 repo: run.repo,
453 ref: run.number != null ? `#${run.number}` : null,
454 by: who(run.agent),
455 facts: [
456 { label: "Run", value: RUN_LABEL[run.kind], tone: null },
457 { label: "Quiet for", value: `${minutes} min`, tone: "warn" },
458 { label: "Steps so far", value: String(run.stepCount), tone: null },
459 ...(run.costUsd != null ? [{ label: "Cost so far", value: usd(run.costUsd), tone: null }] : []),
460 ],
461 open: run.number != null ? `/${run.repo.namespace}/${run.repo.name}/pull/${run.number}` : undefined,
462 });
463 }
464
465 const needRows: NeedRow[] = rankNeeds(needs).map((need) => {
466 const extra = extras.get(need.key) ?? {};
467 const reason = withConfidence(reasonFor(need), extra.confidence);
468 return {
469 key: need.key,
470 reason,
471 repo: extra.repo ?? null,
472 ref: extra.ref ?? null,
473 title: need.title,
474 ask: need.detail,
475 by: extra.by ?? null,
476 for: extra.for ?? null,
477 at: need.at,
478 to: need.to,
479 open: extra.open ?? need.to,
480 facts: extra.facts ?? [],
481 why: whyFor(reason, need, extra.confidence),
482 quick: extra.quick ?? null,
483 link: extra.link ?? null,
484 };
485 });
486
487 // --- Waiting on agents ----------------------------------------------------
488 const needKeys = new Set(
489 needRows.flatMap((row) => (row.repo && row.ref ? [needPathKey(row.repo, Number(row.ref.slice(1)))] : [])),
490 );
491 const waiting = waitingRows({
492 active: activeList.filter(({ repo }) => inWorkspace(repo)).map(({ pull, lifecycle, repo }) => ({ pull, lifecycle, repo: pathOf(repo) })),
493 live: liveRuns,
494 drafts: openPulls.filter(({ pull }) => pull.status === "draft").map(({ pull, repo }) => ({ ...pull, repo: pathOf(repo) })),
495 needKeys,
496 });
497
498 // --- Landed ---------------------------------------------------------------
499 const merged: Merged[] = (perRepo ?? []).flatMap(({ repo, closed }) =>
500 (closed ?? []).flatMap((pull) =>
501 pull.status === "merged" && pull.mergedAt
502 ? [
503 {
504 repo: pathOf(repo),
505 number: pull.number,
506 title: pull.title,
507 agent: pull.agent,
508 // Who wrote it, not who merged it: a person's change that g1t
509 // auto-merged is still theirs.
510 authoredByAgent: pull.author.kind === "agent" || isAgent(pull.author.username),
511 mergedBy: pull.mergedBy,
512 mergedAt: pull.mergedAt,
513 files: pull.files,
514 },
515 ]
516 : [],
517 ),
518 );
519 const twoWeeksAgo = now - 14 * TIME.DAY;
520 const complete = perRepo != null && perRepo.every(({ closed }) => closed != null && reachesBack(closed, twoWeeksAgo, PULL_PAGE));
521 const week = weekOf(merged, now, tz, complete);
522 const landed = landedToday(merged, now, tz);
523
524 // --- Activity -------------------------------------------------------------
525 const items: ActivityItem[] = [];
526 const titles: Record<string, string> = {};
527 const titleKey = (repo: RepoPath, number: number) => `${repo.namespace}/${repo.name}#${number}`.toLowerCase();
528 for (const { repo, pulls, closed, events } of perRepo ?? []) {
529 for (const pull of [...(pulls ?? []), ...(closed ?? [])]) titles[titleKey(repo, pull.number)] = pull.title;
530 for (const event of events ?? []) {
531 if (event.type === "issue.opened") titles[titleKey(repo, event.data.number)] ??= event.data.title;
532 const item = eventItem(event, pathOf(repo));
533 if (item) items.push(item);
534 }
535 }
536 for (const entry of overviewList ?? []) {
537 const latest = entry.latest;
538 const served = latest?.status === "ready" || latest?.status === "replaced";
539 if (!slug || latest?.kind !== "production" || (!served && latest.status !== "failed")) continue;
540 items.push({
541 id: `deploy:${latest.id}`,
542 at: Date.parse(latest.finishedAt ?? latest.createdAt),
543 repo: { namespace: slug, name: entry.slug },
544 actor: latest.createdBy,
545 verb: served ? "deployed" : "deploy_failed",
546 number: null,
547 to: `/${slug}/${entry.slug}/deployments/${latest.id}`,
548 });
549 }
550 for (const memory of okOr(memories ?? null)?.workspace ?? []) {
551 const repo = memory.repo ?? memory.source.repo;
552 if (!repo || !slug) continue;
553 items.push({
554 id: `memory:${memory.id}`,
555 at: Date.parse(memory.createdAt),
556 repo,
557 actor: memory.createdBy,
558 verb: "learned",
559 number: null,
560 text: memory.text.length > 90 ? `${memory.text.slice(0, 88)}…` : memory.text,
561 to: `/${slug}/-/memory`,
562 });
563 }
564 const groups = groupActivity(items).slice(0, 40);
565 // Only the titles the feed names travel to the page.
566 const shownTitles: Record<string, string> = {};
567 for (const group of groups) {
568 for (const part of group.parts) {
569 for (const number of part.numbers) {
570 const key = titleKey(group.repo, number);
571 if (titles[key]) shownTitles[key] = titles[key];
572 }
573 }
574 }
575
576 // --- The strip ------------------------------------------------------------
577 const usageOk = okOr(usage ?? null);
578 const costPoints = usageOk
579 ? usageOk.byDay.map((slice) => ({ at: Date.parse(`${slice.key.split("/")[0]}T12:00:00Z`), value: slice.micros / 1_000_000 }))
580 : runList.filter((run) => run.costUsd != null).map((run) => ({ at: Date.parse(run.createdAt), value: run.costUsd ?? 0 }));
581 const weekCost = dailyBuckets(costPoints, 7, now).reduce((sum, v) => sum + v, 0);
582 const month = dayKey(now, tz).slice(0, 7);
583 const projectCount = projectsOk?.length ?? (perRepo != null ? perRepo.length : null);
584 const projectsThisMonth = projectsOk ? projectsOk.filter((project) => dayKey(Date.parse(project.createdAt), tz).slice(0, 7) === month).length : null;
585
586 const models_ = models ?? null;
587 times.total = Date.now() - started;
588 const serverTiming = Object.entries(times)
589 .map(([name, ms]) => `${name};dur=${ms}`)
590 .join(", ");
591 return data(
592 {
593 signedIn: true as const,
594 viewer,
595 name: profile?.name?.trim() || username,
596 greeting: greetingFor(hourIn(now, tz)),
597 date: dateLine(now, tz),
598 summary: summaryLine({
599 total: week.total,
600 byAgents: week.byAgents,
601 agentChanges: week.agentChanges,
602 people: week.people,
603 live: liveRuns.length,
604 needs: needRows.length,
605 }),
606 workspace: slug,
607 repos: repoList.filter((repo) => inWorkspace(repo)).map(pathOf),
608 canRunAgents: models_ == null || models_.hosted || models_.own != null,
609 trial: models_?.own == null ? (models_?.trial ?? null) : null,
610 handedOff: activeList.length > 0 || runList.length > 0 || merged.length > 0,
611 needs: needRows,
612 waiting,
613 landed,
614 liveTotal: liveRuns.length,
615 runsLoaded: runs != null && runs.ok,
616 perRepoLoaded: perRepo != null,
617 stats: {
618 projects: projectCount,
619 projectsThisMonth,
620 agentHours: agentHours(runList, weekAgo, now),
621 weekCost,
622 },
623 week,
624 tokens: { workspace: okOr(tokens), mine: okOr(myTokens) },
625 groups,
626 titles: shownTitles,
627 // A run that is going makes the page worth refreshing on its own.
628 changing:
629 liveRuns.length > 0 ||
630 activeList.some((item) => item.lifecycle && item.lifecycle.stage !== "needs_you" && item.lifecycle.stage !== "ready"),
631 },
632 {
633 headers: { "Server-Timing": serverTiming },
634 },
635 );
636}
637
638export type Loaded = Extract<Route.ComponentProps["loaderData"], { signedIn: true }>;
639
640/**
641 * Mission control, loaded only for someone signed in (components/mission-control.tsx).
642 * The app shell starts loading it once the browser is idle, so the
643 * skeleton below is rarely seen.
644 */
645const MissionControl = lazy(() => import("../components/mission-control"));
646
647/** Mission control's outline, the same size, while its code arrives. */
648function MissionControlSkeleton() {
649 return (
650 <main aria-busy="true" className="mx-auto max-w-7xl space-y-6 px-4 py-8 sm:px-6 sm:py-10">
651 <header>
652 <Skeleton className="h-8 w-72 sm:h-9" />
653 <Skeleton className="mt-2.5 h-3.5 w-96 max-w-full" />
654 </header>
655 <div className="grid grid-cols-2 gap-px overflow-hidden rounded-xl border border-line bg-line lg:grid-cols-5">
656 {Array.from({ length: 5 }, (_, index) => (
657 <div key={index} className="bg-surface p-4">
658 <Skeleton className="h-3 w-16" />
659 <Skeleton className="mt-3 h-6 w-10" />
660 <Skeleton className="mt-2 h-3 w-24" />
661 </div>
662 ))}
663 </div>
664 <div className="rounded-xl border border-line bg-surface">
665 <SkeletonRows rows={5} rowClassName="h-14" />
666 </div>
667 </main>
668 );
669}
670
671export default function Home({ loaderData, actionData }: Route.ComponentProps) {
672 if (!loaderData.signedIn) return <Landing />;
673 return (
674 <Suspense fallback={<MissionControlSkeleton />}>
675 <MissionControl loaderData={loaderData} delegated={actionData ?? null} />
676 </Suspense>
677 );
678}