Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

4.4 KB
Raw Blame
import {
  AlertCircleIcon,
  Cancel01Icon,
  Clock01Icon,
  Globe02Icon,
  Tick02Icon,
} from "@hugeicons/core-free-icons";
import { lazy, Suspense, useState } from "react";

import { Button } from "@/components/ui/button";
import { Icon, type IconSvgElement } from "@/components/ui/icon";
import { translate } from "@/i18n/language";
import type { Message } from "@/i18n/messages";
import { useT } from "@/i18n/use-translation";

import { decisionCardHeading } from "./card-prompts";
import { CardActions, CardError, CardRow } from "./approval-card-parts";
import { CardBox, CardDone, type ReceiptTone } from "./card-shell";
import { cardsMessages } from "./cards.messages";
import type { DecisionMessageCard } from "./decision-card";
import type { RoomCardDecision } from "./room-message-card-contract";

const BrowserConnectionDialog = lazy(() =>
  import("@/features/local-computer/browser-connection/browser-connection-dialog").then(
    (module) => ({ default: module.BrowserConnectionDialog }),
  ),
);

/**
 * Asks the owner of an Agent's Local Computer to connect their everyday
 * Chrome. The token goes from the dialog to that Computer only; TeamHub
 * resolves the card once the Computer confirms the connection, and that wakes
 * the Agent. Closing the dialog leaves the card waiting.
 */
export function BrowserConnectionDecision({
  card,
  currentActorId,
  messageId,
  onDecision,
  tenantId,
}: {
  card: DecisionMessageCard;
  currentActorId?: string;
  messageId: string;
  onDecision: (decision: RoomCardDecision) => Promise<void> | void;
  tenantId: string;
}) {
  const t = useT();
  const [open, setOpen] = useState(false);
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState("");
  const owner = currentActorId === card.decision_owner.id;
  const agentName = card.requester.display_name;

  async function decline() {
    setBusy(true);
    setError("");
    try {
      await onDecision({
        decision: "deny",
        expectedVersion: card.version,
        messageId,
        requestId: card.decision_id,
      });
    } catch (cause) {
      setError(
        cause instanceof Error
          ? cause.message
          : translate(cardsMessages.cardUpdateFailed),
      );
    } finally {
      setBusy(false);
    }
  }

  const result = card.status === "pending" ? null : browserResult(card);
  return (
    <>
      <CardBox
        heading={decisionCardHeading(card, t)}
        label={t(cardsMessages.labelBrowserConnection)}
      >
        <CardRow
          icon={<Icon icon={Globe02Icon} size={16} />}
          title={t(cardsMessages.everydayChrome)}
        />
        {result ? (
          <CardDone
            icon={result.icon}
            text={t(result.text)}
            tone={result.tone}
          />
        ) : owner ? (
          <CardActions>
            <Button
              disabled={busy}
              onClick={() => setOpen(true)}
              size="large"
              tone="primary"
            >
              {t(cardsMessages.connect)}
            </Button>
            <Button
              disabled={busy}
              onClick={() => void decline()}
              size="large"
              tone="subtle"
            >
              {t(cardsMessages.notNow)}
            </Button>
          </CardActions>
        ) : null}
        {error ? <CardError>{error}</CardError> : null}
      </CardBox>
      {open && owner && !result ? (
        <Suspense fallback={null}>
          <BrowserConnectionDialog
            computerId={card.browser_connection?.computer_id ?? ""}
            onClose={() => setOpen(false)}
            reason={t(cardsMessages.continuesOnceConnected, {
              agent: agentName,
            })}
            tenantId={tenantId}
          />
        </Suspense>
      ) : null}
    </>
  );
}

/** How a settled browser connection ended, in a few words. */
function browserResult(card: DecisionMessageCard): {
  icon: IconSvgElement;
  text: Message<string>;
  tone: ReceiptTone;
} {
  if (card.answer?.decision === "connected")
    return { icon: Tick02Icon, text: cardsMessages.connected, tone: "success" };
  switch (card.status) {
    case "denied":
      return {
        icon: Cancel01Icon,
        text: cardsMessages.notConnected,
        tone: "muted",
      };
    case "expired":
      return { icon: Clock01Icon, text: cardsMessages.expired, tone: "muted" };
    default:
      return {
        icon: AlertCircleIcon,
        text: cardsMessages.browserConnectionFailed,
        tone: "danger",
      };
  }
}