Skip to content
70 linesCodeBlameRaw
1import { Outlet, data, type ShouldRevalidateFunctionArgs, useMatch, useRouteLoaderData } from "react-router";
2
3import { type WorkspaceAgent, hasCodeAccess } from "@g1t/contracts";
4
5import { AgentsSidebar } from "../../../components/agents-mode";
6
7import type { Route } from "./+types/layout";
8import { workspaceAgents } from "../../../lib/services.server";
9import { requireUser, roleIn } from "../../../lib/session.server";
10
11/**
12 * Agents mode: the workspace's agents, for its sidebar, and how many live
13 * sessions each has (by agent id); null when the agents service does not answer.
14 */
15export type AgentsLayoutData = {
16 slug: string;
17 agents: WorkspaceAgent[] | null;
18 live: Record<string, number>;
19 /** The viewer's id, to tell their own personal agents from members' (which owners see). */
20 viewer_id: string;
21 /** Whether the viewer may create an agent: owners always, members unless owners turned personal agents off. */
22 may_create: boolean;
23};
24
25export async function loader({ params, context, request }: Route.LoaderArgs): Promise<AgentsLayoutData> {
26 // Signed out, sign in first (as Chat and Docs do), rather than a 404.
27 const viewer = requireUser(context, request);
28 if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 });
29 const slug = params.owner.toLowerCase();
30 const owner = roleIn(viewer, slug) === "owner";
31 const [listed, live, policy] = await Promise.all([
32 // Personal agents too: the viewer's own, and every member's for an owner.
33 workspaceAgents.list(slug, viewer!, { personal: "all" }).catch(() => null),
34 // Every live session counts, private ones too: a count says nothing about what it is.
35 workspaceAgents.sessions(slug, viewer!, { status: "live", limit: 200 }).catch(() => null),
36 owner ? Promise.resolve(null) : workspaceAgents.policy(slug, viewer!).catch(() => null),
37 ]);
38 const counts: Record<string, number> = {};
39 for (const session of live?.ok ? live.value : []) counts[session.agent_id] = (counts[session.agent_id] ?? 0) + 1;
40 return {
41 slug,
42 viewer_id: viewer.id,
43 may_create: owner || (policy?.ok ? policy.value.members_create_agents : true),
44 live: counts,
45 agents: listed?.ok ? listed.value.filter((agent) => !agent.archived_at).sort((a, b) => a.display_name.localeCompare(b.display_name)) : null,
46 };
47}
48
49/** The list changes when an agent is made or changed, or work is stopped or started; not when moving between them. */
50export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) {
51 if (formMethod && formMethod !== "GET") return defaultShouldRevalidate;
52 return currentParams.owner !== nextParams.owner;
53}
54
55export default function AgentsLayout({ loaderData }: Route.ComponentProps) {
56 const root = useRouteLoaderData("root") as { user?: { workspaces?: { slug: string; role: string; code_access?: boolean }[] } | null } | undefined;
57 const membership = root?.user?.workspaces?.find((m) => m.slug === loaderData.slug) ?? null;
58 // A phone: the agents first, as a list; the fleet's numbers under it.
59 const index = useMatch("/:owner/-/agents");
60 return (
61 <>
62 {index && (
63 <div className="-mx-4 -mt-6 mb-8 md:hidden">
64 <AgentsSidebar slug={loaderData.slug} shellAgents={null} code={hasCodeAccess(membership)} owner={membership?.role === "owner"} phone />
65 </div>
66 )}
67 <Outlet />
68 </>
69 );
70}