import { integrationDefinitionIdFromCapabilityId } from "@opengeni/contracts"; import type { OpenGeniBrowserClient as OpenGeniClient } from "@opengeni/sdk/browser"; import { useEffect, useRef, useState } from "react"; import { ConnectSetup } from "../connect-setup"; import { browserConnectWindowHost } from "../connect-window-host"; import { connectionServicePresentation } from "../connection-service-presentation"; import { useOpenGeniText } from "../messages/runtime"; import { useIntegrationCapabilityConnect } from "./integration-capability-connect-flow"; import { SessionCapabilityFrame } from "./session-capability-frame"; export { reconnectCandidate } from "./integration-capability-connect-flow"; /** The client methods the card uses. Session proxies that do not forward * Connect and integration routes cannot render it. */ export function supportsIntegrationCapabilityCard(client: unknown): client is OpenGeniClient { const candidate = client as Partial> | null; return ( !!candidate && typeof candidate.connectTransport === "function" && typeof candidate.listApiIntegrations === "function" && typeof candidate.getSession === "function" && typeof candidate.updateSessionToolPolicy === "function" ); } export type SessionIntegrationCapabilityCardProps = { client: OpenGeniClient; workspaceId: string; sessionId: string; /** `api:integration:` from the agent's recommendation. */ capabilityId: string; name: string; rationale?: string; /** Exact host route the provider returns to after a full-page redirect. */ returnUrl: string; onConfigured?: (() => void | Promise) | undefined; }; /** * One-click Connect card for a reviewed integration recommended in chat. The * person viewing it signs in with their own account in a popup; setup then * installs the integration's reviewed tools for that personal account only, * never as a workspace account. It shows the viewer's own state, so a * teammate in a shared chat sees and connects their own account. When * Microsoft says an administrator must approve the app first, the shared * setup view shows the approval link to pass on, and "Try again" connects * after that. */ export function SessionIntegrationCapabilityCard(props: SessionIntegrationCapabilityCardProps) { return ( ); } function ScopedCard({ client, workspaceId, sessionId, capabilityId, name, rationale = "", returnUrl, onConfigured, }: SessionIntegrationCapabilityCardProps) { const t = useOpenGeniText(); const definitionId = integrationDefinitionIdFromCapabilityId(capabilityId) ?? capabilityId; const presentation = connectionServicePresentation({ id: definitionId, label: name }); const [windows] = useState(() => browserConnectWindowHost(window)); const flow = useIntegrationCapabilityConnect({ client, workspaceId, sessionId, capabilityId, name, returnUrl, windows, onConfigured, }); const { availability, attempt, busy, waiting, error, chatAccessFailed, notRequester } = flow; // The button the person pressed disappears as the card changes step; move // focus to the step's own control so keyboard and screen reader users don't // land back at the top of the page. Only when focus was actually lost. const setupRef = useRef(null); // A message (a blocked pop-up, say) counts as a step: the button pressed // may be gone even though the attempt itself didn't move. const step = waiting ? "waiting" : busy ? "busy" : `${attempt?.state ?? "none"}${error ? ":error" : ""}`; const shownStep = useRef(step); useEffect(() => { if (shownStep.current === step) return; shownStep.current = step; if (step === "busy") return; const region = setupRef.current; const active = region?.ownerDocument.activeElement; if (!region || (active && active !== region.ownerDocument.body)) return; // While waiting, focus the status line: a second Enter on a button there // would open another window or cancel. const target = region.querySelector(".og-session-capability-waiting [role=status]") ?? region.querySelector( ".og-session-capability-primary, .og-connect-setup-primary, button:not([disabled])", ); (target ?? region).focus({ preventScroll: true }); }, [step]); // Connecting replaces the setup controls with "Your account is connected"; // keep keyboard focus on the card by moving it to that line. const shell = useRef(null); useEffect(() => { if (setupRef.current) shell.current = setupRef.current.closest(".og-session-capability-shell"); }); const connected = availability.kind === "connected"; useEffect(() => { const card = shell.current; if (!connected || !card) return; const active = card.ownerDocument.activeElement; if (active && active !== card.ownerDocument.body && active.isConnected) return; card .querySelector(".og-session-capability-copy[role=status]") ?.focus({ preventScroll: true }); }, [connected]); const retrySignIn = flow.signInClosed || Boolean(error); const setup = availability.kind === "ready" && (attempt || busy || error) ? (
{error ?

{error}

: null} {flow.notice && flow.signInClosed ?

{flow.notice}

: null} {/* While the sign-in window is open, the card says so and offers one way out, which ends the attempt; the sign-in controls return if the window closes or the provider moves setup elsewhere. */} {waiting ? null : attempt ? ( ) : busy ? (

{t("sessionIntegrationCapabilityCard.preparing")}

) : null} {/* After Cancel: a sign-in window the browser wouldn't let the card close. */} {flow.notice && !flow.signInClosed ? (

{flow.notice}

) : null} {waiting ? (

{flow.signInWindowGone ? t("sessionIntegrationCapabilityCard.signInWindowGone", { name: presentation.name, }) : t("sessionIntegrationCapabilityCard.finishSigningIn", { name: presentation.name, })}

) : null} {!busy && !waiting && (flow.restartable || (error && !attempt)) ? ( ) : null}
) : undefined; return ( {}} inlineSetup={setup} actionUnavailable={ availability.kind === "unavailable" ? ( availability.reason ) : availability.kind === "error" ? ( <> {t("sessionIntegrationCapabilityCard.couldNotLoad")}{" "} ) : undefined } details={ availability.kind === "connected" && (chatAccessFailed || notRequester) ? ( <> {chatAccessFailed ? (

{t("sessionIntegrationCapabilityCard.chatAccessFailed")}

) : null} {notRequester ? (

{t("sessionIntegrationCapabilityCard.notRequester")}

) : null} ) : undefined } /> ); }