Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

14.0 KB
Raw Blame
import {
  IconBrandGoogleDrive,
  IconBrandNotion,
  IconChevronRight,
  IconCircleCheck,
  IconPlug,
} from "@tabler/icons-react";
import { useQuery } from "@tanstack/react-query";
import { createFileRoute, Link } from "@tanstack/react-router";
import * as React from "react";
import {
  PageEmpty,
  PageRows,
  PageSection,
  PageShell,
} from "@/components/layout/page-shell";
import { RowMark } from "@/components/layout/row-mark";
import {
  AccessRequestsSection,
  usePendingRequestCounts,
} from "@/components/plugins/access-requests";
import {
  Item,
  ItemActions,
  ItemContent,
  ItemDescription,
  ItemTitle,
} from "@/components/ui/item";
import { Separator } from "@/components/ui/separator";
import {
  moreAppsRow,
  moreAppsSectionDescription,
  providerVocabulary,
} from "@/lib/plugins/provider-vocabulary";
import {
  type CatalogueItem,
  connectionsQueryOptions,
  type PluginServer,
  personalConnections,
  pluginsPageQueryOptions,
  pluginsProvider,
} from "@/lib/plugins/queries";

/**
 * What this deployment can reach, as one list per state.
 *
 * This screen used to be three tabs: a catalogue of what could be added, a second tab for what had
 * been, and skills. Answering one question about one vendor — is Drive available, and what can it
 * do — meant visiting two of them, and the third was a different kind of thing altogether. Two
 * lists say the same thing in one read: what is connected, and what else there is.
 *
 * Every row goes to that vendor's own page, because what a connector needs configured is not the
 * same from one vendor to the next. A token, an OAuth client, an instance hostname, and a grant per
 * tool per Bot do not fit on a row, and the previous screen's attempt to fit them made a page that
 * scrolled sideways.
 */
export const Route = createFileRoute("/_authed/admin/plugins/")({
  component: RouteComponent,
});

/**
 * A vendor's own mark where there is one, and a plug where there is not.
 *
 * The fallback covers a server an administrator added by URL, which has no catalogue entry and so no
 * mark of its own — and it would cover a catalogue vendor Tabler ships no brand for. Only Drive is in
 * the catalogue today, and Tabler has it.
 */
const MARKS: Record<string, React.ComponentType<{ className?: string }>> = {
  "google-drive": IconBrandGoogleDrive,
  notion: IconBrandNotion,
};

const markFor = (key: string) => MARKS[key] ?? IconPlug;

/**
 * What a connected row says on the right.
 *
 * The current answer rather than the field's name, which is what the layout skill asks of a summary:
 * "4 tools · 2 Bots" tells an administrator where this vendor stands, and "Tools" would not.
 *
 * A vendor reached as the person asking is a special case worth its own words. It can be fully
 * configured — client registered, tools listed — and still answer nothing, because the thing that
 * reads anything is a grant belonging to whoever is asking. "Not connected" is about you, not about
 * the deployment.
 */
function summaryFor(
  server: PluginServer,
  /**
   * The vendor's auth kind, from the catalogue rather than the server record.
   *
   * A server row says what this deployment has stored; whose credential reaches it is a fact about
   * the vendor. Undefined for a server added by URL, which has no catalogue entry and is therefore
   * never reached as a person.
   */
  auth: CatalogueItem["auth"] | undefined,
  youConnected: boolean,
): string {
  if (auth === "user-oauth" && !youConnected) return "Not connected";
  if (server.tools.length === 0) return "No tools yet";

  const bots = new Set(server.tools.flatMap((tool) => tool.grantedTo)).size;
  const tools = `${server.tools.length} ${server.tools.length === 1 ? "tool" : "tools"}`;
  if (bots === 0) return `${tools} · no Bots`;
  return `${tools} · ${bots} ${bots === 1 ? "Bot" : "Bots"}`;
}

function RouteComponent() {
  const plugins = useQuery(pluginsPageQueryOptions());
  const connections = useQuery(connectionsQueryOptions());
  /*
   * Requests waiting per app, so an administrator sees which app somebody is asking about from the
   * list, without opening each one. Every request is also listed in full above the lists.
   */
  const requestCounts = usePendingRequestCounts();
  // A NAME for the "More apps" row, which cannot be read off a row that may not exist yet.
  const provider = pluginsProvider(plugins.data);
  const moreApps = moreAppsRow(
    provider,
    plugins.data?.composioConfigured ?? false,
  );

  /*
   * PERSONAL ONLY: a Shared app's connection is the deployment's standing account, not something
   * the signed-in admin connected themselves. Reading it into this set would mark that row "you
   * connected" on a deployment-wide list where nobody connected anything personally.
   */
  const connected = new Set(
    personalConnections(connections.data?.connections ?? []).map(
      (row) => row.serverId,
    ),
  );
  const added = new Set((plugins.data?.servers ?? []).map((s) => s.id));
  const explore = (plugins.data?.catalogue ?? []).filter(
    (entry) => !added.has(entry.key),
  );
  /** Keyed by catalogue key, which is also the server id, so a row can ask how it is reached. */
  const authByKey = new Map(
    (plugins.data?.catalogue ?? []).map((entry) => [entry.key, entry.auth]),
  );

  return (
    <PageShell
      description="Connect services for your company, then choose which Bots can use each service’s tools."
      title="Plugins"
    >
      {/* Pending, error, empty, rows — pending first, so no sentence asserts anything mid-fetch. */}
      {plugins.isPending ? null : plugins.error ? (
        <p className="mt-12 text-destructive text-sm" role="alert">
          Couldn't load plugins. Reload the page.
        </p>
      ) : (
        <>
          {/*
           * First, because each one is somebody waiting. All of them, not only those about an app
           * that is connected: an app nobody has switched on has no page of its own to list them on.
           */}
          <AccessRequestsSection />

          <PageSection
            description="Added for everybody. Open one to set it up and choose which Bots get its tools."
            title="Connected"
          >
            {plugins.data?.servers.length === 0 ? (
              <PageEmpty icon={IconPlug} title="Nothing connected yet">
                Pick one from the list below.
              </PageEmpty>
            ) : (
              <PageRows>
                {plugins.data?.servers.map((server, index) => {
                  const Mark = markFor(server.id);
                  return (
                    <React.Fragment key={server.id}>
                      {/*
                       * A real link with no children. `useRender` merges props, and children passed
                       * here replace the row's own — the media, content and actions all vanish and
                       * the row draws empty. Its accessible name comes from the title inside it.
                       */}
                      <Item
                        data-testid={`plugin-${server.id}`}
                        render={
                          <Link
                            params={{ key: server.id }}
                            to="/admin/plugins/$key"
                          />
                        }
                        size="sm"
                      >
                        <RowMark>
                          <Mark className="size-4" />
                        </RowMark>
                        <ItemContent>
                          <ItemTitle>{server.title}</ItemTitle>
                          {/*
                           * The vendor's last failure takes the description's place when there is
                           * one. A server with no tools and no explanation reads as a server that
                           * offers nothing, which sends somebody looking in the wrong place.
                           */}
                          <ItemDescription
                            className={
                              server.lastError ? "text-destructive" : undefined
                            }
                          >
                            {server.lastError ?? server.summary}
                          </ItemDescription>
                        </ItemContent>
                        <ItemActions>
                          <PendingRequests
                            count={requestCounts?.byApp[server.id] ?? 0}
                          />
                          <span className="text-muted-foreground text-xs">
                            {summaryFor(
                              server,
                              authByKey.get(server.id),
                              connected.has(server.id),
                            )}
                          </span>
                          <IconChevronRight className="size-4 shrink-0 text-muted-foreground" />
                        </ItemActions>
                      </Item>
                      {index !== (plugins.data?.servers.length ?? 0) - 1 && (
                        <Separator />
                      )}
                    </React.Fragment>
                  );
                })}
              </PageRows>
            )}
          </PageSection>

          <PageSection
            description="Reviewed, first-party servers. Open one to add it."
            title="Explore plugins"
          >
            {explore.length === 0 ? (
              <PageEmpty icon={IconCircleCheck} title="All connected">
                Every plugin in the catalogue is connected.
              </PageEmpty>
            ) : (
              <PageRows>
                {explore.map((entry: CatalogueItem, index) => {
                  const Mark = markFor(entry.key);
                  return (
                    <React.Fragment key={entry.key}>
                      <Item
                        data-testid={`plugin-${entry.key}`}
                        render={
                          <Link
                            params={{ key: entry.key }}
                            to="/admin/plugins/$key"
                          />
                        }
                        size="sm"
                      >
                        <RowMark>
                          <Mark className="size-4" />
                        </RowMark>
                        <ItemContent>
                          <ItemTitle>{entry.title}</ItemTitle>
                          <ItemDescription>{entry.summary}</ItemDescription>
                        </ItemContent>
                        <ItemActions>
                          <PendingRequests
                            count={requestCounts?.byApp[entry.key] ?? 0}
                          />
                          <span className="text-muted-foreground text-xs">
                            Not added
                          </span>
                          <IconChevronRight className="size-4 shrink-0 text-muted-foreground" />
                        </ItemActions>
                      </Item>
                      {index !== explore.length - 1 && <Separator />}
                    </React.Fragment>
                  );
                })}
              </PageRows>
            )}
          </PageSection>

          <PageSection
            description={moreAppsSectionDescription(provider)}
            title="More apps"
          >
            <PageRows>
              {/*
               * The row states the setting rather than hiding the feature. A deployment with no
               * Composio key still says that Composio is a thing this build can do and names the
               * variable that turns it on — a section that simply vanished would leave an
               * administrator with nothing to search for, and nothing to tell them the handful of
               * connectors above is not the whole story.
               */}
              {moreApps.linked ? (
                <Item
                  data-testid="plugin-directory"
                  /* The ACTIVE provider's own directory. Read from the vocabulary rather than
                     written here: a hard-coded path is the lowercase spelling of a vendor name,
                     which is how this row pointed every Arcade deployment at a screen that
                     redirected it straight back. The field is a literal union over the generated
                     route tree, so a path no route answers does not compile. */
                  render={
                    <Link to={providerVocabulary(provider).directoryPath} />
                  }
                  size="sm"
                >
                  <RowMark>
                    <IconPlug className="size-4" />
                  </RowMark>
                  <ItemContent>
                    <ItemTitle>{moreApps.title}</ItemTitle>
                    <ItemDescription>{moreApps.description}</ItemDescription>
                  </ItemContent>
                  <ItemActions>
                    <IconChevronRight className="size-4 shrink-0 text-muted-foreground" />
                  </ItemActions>
                </Item>
              ) : (
                /* No chevron and no link: the row goes nowhere, because there is nowhere to go
                   until the key is set. */
                <Item data-testid="plugin-directory" size="sm">
                  <RowMark>
                    <IconPlug className="size-4" />
                  </RowMark>
                  <ItemContent>
                    <ItemTitle>{moreApps.title}</ItemTitle>
                    <ItemDescription>{moreApps.description}</ItemDescription>
                  </ItemContent>
                </Item>
              )}
            </PageRows>
          </PageSection>
        </>
      )}
    </PageShell>
  );
}

/**
 * "2 requests", beside a row's summary, in the foreground colour so it is the first thing on the
 * row that reads as something to do. Nothing at zero: a count of nothing on every row is noise.
 */
function PendingRequests({ count }: { count: number }) {
  if (count === 0) return null;
  return (
    <span className="font-medium text-foreground text-xs tabular-nums">
      {count} {count === 1 ? "request" : "requests"}
    </span>
  );
}