import { inChatCapabilitySetup, type MemberIntegrationSetup, } from "@opengeni/contracts/personal-integration-setup"; import type { AccessContext, CapabilityCatalogItem, Permission } from "@opengeni/sdk"; import { useEffect, useRef, useState } from "react"; import type { AuthNeededItem } from "../timeline/types"; /** What the conversation reads to decide whether this person can connect in the chat. */ export type InChatConnectSource = { getAccessContext: () => Promise; listCapabilities: (workspaceId: string) => Promise<{ items: CapabilityCatalogItem[]; memberIntegrationSetup?: MemberIntegrationSetup | undefined; }>; }; export type InChatConnectSupport = | { status: "idle" | "loading" } /** The grant or the catalog could not be read; `retry` reads them again. */ | { status: "failed"; retry: () => void } | { status: "ready"; permissions: ReadonlySet; permissionMode?: "legacy" | "explicit" | undefined; catalog: ReadonlyMap; /** Who the organization lets turn integrations on (omitted: members). */ memberIntegrationSetup?: MemberIntegrationSetup | undefined; }; export type InChatConnectDecision = /** * The shared connect card: sign in, enable, add to this chat, continue. * `personalOnly`: the person can connect only their own account. */ | { kind: "card"; item: CapabilityCatalogItem; personalOnly: boolean } /** * A deployment's reviewed integration (Outlook Mail, OneDrive, ...): the * person signs in with their own account, which installs its reviewed tools * for that account alone, adds them to this chat and continues. */ | { kind: "integration"; capabilityId: string; name: string } /** An agent's card this person cannot act on here. */ | { kind: "unavailable" } /** Not an in-chat connection: the default recovery row decides. */ | { kind: "default" }; const UNAVAILABLE_REASONS = new Set([ "personal_authority_unavailable", "unsupported_auth", "resource_scope_unavailable", ]); /** Cards that finish through their own flows, never the shared connect card. */ const OWN_FLOW_CAPABILITY_IDS = new Set([ "api:connected-machine", "api:github-app", "api:github-personal", "mcp:codex_apps", ]); /** Whether an auth notice is an agent's request to set up a capability in this chat. */ export function isAgentCapabilityRequest(item: AuthNeededItem): boolean { return ( item.capability !== undefined && item.capability !== null && !item.setupRequest && item.authoritySource !== "host" && !(item.reason && UNAVAILABLE_REASONS.has(item.reason)) && !OWN_FLOW_CAPABILITY_IDS.has(item.capability.id) ); } /** * Decide how a stock conversation shows an agent's capability card. The * shared card handles OAuth MCP capabilities; it is offered only when this * person's own grant can finish every step it takes and the session proxy * lets the browser set chat tools: add it to this chat (`sessions:control`), * and either use the workspace's shared account (`connections:read`) or * connect an account as personal integration setup allows (with only * `connections:personal`, their own account, enabling a reviewed OAuth * integration for personal accounts if it is not on yet). Anything else keeps * the default row. Host-managed connections (`authoritySource: "host"`) * never come here. The rule is `inChatCapabilitySetup`, which the agent's * setup tool also applies, so the agent's reply matches this card. */ export function inChatConnectDecision( item: AuthNeededItem, support: InChatConnectSupport, chatConnections: boolean, ): InChatConnectDecision { if (!chatConnections || !isAgentCapabilityRequest(item)) return { kind: "default" }; if (support.status !== "ready") return { kind: "default" }; const requested = item.capability!; const capability = support.catalog.get(requested.id); const setup = inChatCapabilitySetup( { permissions: [...support.permissions], permissionMode: support.permissionMode }, requested.id, capability, support.memberIntegrationSetup, ); if (setup.kind === "integration") return { kind: "integration", capabilityId: requested.id, name: requested.name }; if (setup.kind === "card" && capability) return { kind: "card", item: capability, personalOnly: setup.personalOnly }; return setup.kind === "unavailable" ? { kind: "unavailable" } : { kind: "default" }; } /** * Read the person's grant and the capability catalog once, and only when the * conversation shows an agent's capability card. */ export function useInChatConnectSupport( client: Partial, workspaceId: string, needed: boolean, ): InChatConnectSupport { const [loaded, setLoaded] = useState<{ client: Partial; workspaceId: string; support: InChatConnectSupport; } | null>(null); const requested = useRef<{ client: Partial; workspaceId: string } | null>( null, ); const [reads, setReads] = useState(0); useEffect(() => { if (!needed) return; if ( typeof client.getAccessContext !== "function" || typeof client.listCapabilities !== "function" ) return; const previous = requested.current; if (previous?.client === client && previous.workspaceId === workspaceId) return; const request = { client, workspaceId }; requested.current = request; let live = true; Promise.all([client.getAccessContext(), client.listCapabilities(workspaceId)]).then( ([access, catalog]) => { if (!live) return; const grant = access.workspaceGrants.find((entry) => entry.workspaceId === workspaceId); setLoaded({ client, workspaceId, support: { status: "ready", permissions: new Set(grant?.permissions ?? []), permissionMode: grant?.permissionMode, catalog: new Map(catalog.items.map((entry) => [entry.id, entry])), ...(catalog.memberIntegrationSetup ? { memberIntegrationSetup: catalog.memberIntegrationSetup } : {}), }, }); }, () => { if (!live) return; const retry = () => { if (requested.current === request) requested.current = null; setLoaded(null); setReads((count) => count + 1); }; setLoaded({ client, workspaceId, support: { status: "failed", retry } }); }, ); return () => { live = false; if (requested.current === request) requested.current = null; }; }, [client, workspaceId, needed, reads]); if (loaded?.client === client && loaded.workspaceId === workspaceId) return loaded.support; return { status: needed ? "loading" : "idle" }; }