Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

46.9 KB
Raw Blame
import type { AppConfig } from "./config.ts";
import { createHash, randomUUID } from "node:crypto";
import { z } from "zod";
import { ACCOUNT_ID, inventoryAccountSchema, toolkitInventoryPageSchema, toolkitPageSchema, type ToolkitItem } from "./composio-inventory-schema.ts";
import { SPAWNED_PROXIES } from "./proxy-paths.ts";
import { managedConnectorUnavailableReason, managedCustomAuthReason } from "../shared/connector-availability.ts";
import {
  DEFAULT_BACKEND_ORIGIN,
  apiBase,
  canonicalToolkitSlug,
  deleteConnectedAccount,
  inputError,
  keepAuthConfigPick,
  listConnectedAccounts,
  MAX_CONNECTED_ACCOUNT_PAGES,
  noAuthConfigReason,
  projectHeaders,
  refusedAuthConfigReason,
  responseError,
  type ConnectedAccountResponse,
} from "./composio-project.ts";
import { MULTI_ACCOUNT_CONFIG, authConfigRefusal, ensureProjectSession, projectApiKey, raiseAccountCap, sessionForToolkit } from "./composio-session.ts";
import { authConfigUseLine, noManagedAuthToolkit, withNoManagedAuthNote } from "./connector-auth-config-hint.ts";
import { planAccountLink } from "../shared/connector-account-link.ts";
import type { CuratedConnectorSlug } from "../shared/connector-curated.ts";
import { CONNECTOR_TOOL_NAME_PATTERN, type ConnectorToolGrant } from "../shared/wire.ts";
import {
  CONNECTOR_ALLOWED_TOOLS_ENV,
  CONNECTOR_ALLOWED_TOOLS_MAX_BYTES,
  CONNECTOR_SERVICE_SLUGS_ENV,
  CONNECTOR_SERVICE_SLUGS_MAX_BYTES,
  serializeConnectorAllowedTools,
  serializeConnectorServiceSlugs,
} from "./connector-advertisement.ts";
import { connectedAppGrantSlugs, serviceSlugFor, serviceSlugForCandidates } from "./connector-verdict.ts";
import { parseMcpResponse, withToolsListFallback } from "./connector-tools-list.ts";
import { brokerAccess, managedComposioBrokerOff, type BrokerAccess } from "./composio-broker-access.ts";
export { applyManagedBrokerMessage, brokerAccess, setManagedBrokerAccess, type BrokerAccess } from "./composio-broker-access.ts";
export { prepareProjectSession, trustedSessionMcpUrl } from "./composio-session.ts";
import { catalogFor, type CatalogPagination, type ToolkitCard } from "./composio-catalog.ts";
import { forgetInventory, inventoryDiffers, sharedInventory } from "./composio-inventory-cache.ts";
export { onCatalogChange, rememberCatalogIn, toolkitCategories, type CatalogPagination, type CatalogStopReason, type ToolkitCard } from "./composio-catalog.ts";

function toolkitBase() {
  return (process.env.OMB_COMPOSIO_TOOLKITS_API ?? `${DEFAULT_BACKEND_ORIGIN}/api/v3`).replace(/\/$/, "");
}

export interface ConnectedAccountSummary {
  id: string;
  alias?: string;
  status: string;
}

export interface ConnectorServiceState {
  connected: boolean;
  pending: boolean;
  status: string;
  accounts: ConnectedAccountSummary[];
}

interface AccountLinkRequest {
  toolkit: string;
  alias?: string;
}

const connectorServiceSchema = z.object({
  connected: z.boolean(),
  pending: z.boolean().optional(),
  status: z.string().optional(),
  accounts: z.array(z.object({ id: z.string(), alias: z.string().optional(), status: z.string() })).optional(),
});
const connectorServicesResponseSchema = z.object({ services: z.record(z.string(), connectorServiceSchema).optional(), complete: z.boolean().optional() });
const removalResponseSchema = z.object({ removed: z.number() });
const authUrlResponseSchema = z.object({ url: z.string().optional() });
const linkResponseSchema = z.object({ redirect_url: z.string().optional() });

const printableAliasSchema = z.string().min(1).max(64).refine((value) => {
  for (const character of value) {
    const codePoint = character.codePointAt(0);
    if (codePoint === undefined || codePoint < 32 || codePoint === 127) return false;
  }
  return true;
});

type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };

export interface ComposioMcpIntegration {
  command: string;
  args: string[];
  env: Record<string, string>;
}

interface IntegrationContext {
  harnessUrl: string;
  commsToken: string;
  botId: string;
  threadId: string;
  connectorTools?: Record<string, ConnectorToolGrant>;
  /** A Team Cloud member's own installation, instead of this server's. */
  as?: BrokerAccess;
}

/** Separates caches and transports per credential without keeping another plaintext copy of it. */
function backendFingerprint(kind: string, endpoint: string, credential: string): string {
  return createHash("sha256")
    .update(kind)
    .update("\0")
    .update(endpoint)
    .update("\0")
    .update(credential)
    .digest("hex");
}

const MAX_TRACKED_TRANSPORT_SESSIONS = 512;
const transportSessionBackends = new Map<string, string>();

function rememberTransportSession(sessionId: string, identity: string): void {
  transportSessionBackends.delete(sessionId);
  transportSessionBackends.set(sessionId, identity);
  while (transportSessionBackends.size > MAX_TRACKED_TRANSPORT_SESSIONS) {
    const oldest = transportSessionBackends.keys().next().value;
    if (oldest === undefined) break;
    transportSessionBackends.delete(oldest);
  }
}

function canonicalServiceRecord<T>(services: Record<string, T>): Record<string, T> {
  return Object.fromEntries(
    Object.entries(services).map(([slug, state]) => [canonicalToolkitSlug(slug), state]),
  );
}

function requestedServiceRecord<T>(services: Record<string, T>, requested: string[]): Record<string, T> {
  const canonical = canonicalServiceRecord(services);
  return Object.fromEntries(
    requested.flatMap((slug) => {
      const state = canonical[canonicalToolkitSlug(slug)];
      return state === undefined ? [] : [[slug, state]];
    }),
  );
}

export function connectionMode(cfg: AppConfig): "managed" | "self-hosted" | "unavailable" {
  if (projectApiKey(cfg)) return "self-hosted";
  if (brokerAccess()) return "managed";
  return "unavailable";
}

export function configured(cfg: AppConfig): boolean {
  return connectionMode(cfg) !== "unavailable";
}

/** "unreadable": the desktop shell set OMB_CREDENTIAL_STORE=unavailable, so the UI keeps its list instead of wiping it. */
export type ConnectorAvailability = "configured" | "unconfigured" | "unreadable";

export function connectorAvailability(
  cfg: AppConfig,
  storeState: string | undefined = process.env.OMB_CREDENTIAL_STORE,
): ConnectorAvailability {
  if (configured(cfg)) return "configured";
  return storeState === "unavailable" ? "unreadable" : "unconfigured";
}

/** Only a host the managed service comes with (the desktop app, OMB_DESKTOP_PARENT=1, or a Cloud home) calls a missing one an outage. */
export type ConnectorSetup = "ready" | "needs-setup" | "service-unavailable";

export function connectorSetup(
  cfg: AppConfig,
  managedHost: boolean = process.env.OMB_DESKTOP_PARENT === "1",
): ConnectorSetup {
  if (configured(cfg)) return "ready";
  return managedHost && !managedComposioBrokerOff() ? "service-unavailable" : "needs-setup";
}

async function brokerRequest(path: string, init?: RequestInit, broker = brokerAccess()): Promise<Response> {
  if (!broker) throw new Error("The connected-apps service is unavailable");
  const headers = new Headers(init?.headers);
  headers.set("authorization", `Bearer ${broker.token}`);
  if (init?.body) headers.set("content-type", "application/json");
  return fetch(`${broker.url}${path}`, {
    ...init,
    headers,
    signal: init?.signal ?? AbortSignal.timeout(30_000),
  });
}

async function throwBrokerError(res: Response, fallback: string): Promise<never> {
  const status = res.status >= 400 && res.status < 500 ? res.status : 502;
  throw Object.assign(new Error(await responseError(res, fallback)), { status });
}

function trustedAuthUrl(value: string | undefined, slug: string): string {
  if (!value) throw new Error(`Connected-apps service returned no authorization link for ${slug}`);
  const url = new URL(value);
  if (url.protocol !== "https:" || (url.hostname !== "composio.dev" && !url.hostname.endsWith(".composio.dev"))) {
    throw new Error("Connected-apps service returned an untrusted authorization link");
  }
  return url.toString();
}

export function normalizeAccountAlias(value: string | null | undefined): string | undefined {
  if (value === undefined || value === null || value === "") return undefined;
  const parsed = z.string().safeParse(value);
  if (!parsed.success) throw inputError("Account alias must be text");
  const alias = parsed.data.trim();
  if (!printableAliasSchema.safeParse(alias).success) {
    throw inputError("Account alias must be 1-64 printable characters");
  }
  return alias;
}

function validAccountId(value: string | undefined): value is string {
  return Boolean(value && ACCOUNT_ID.test(value));
}

/** Composio's wording when a toolkit needs one of the project's own auth configs. */
const NEEDS_AUTH_CONFIG = /does not manage auth|auth[_ ]?config/i;

export async function mcpIntegration(
  cfg: AppConfig,
  context: IntegrationContext,
): Promise<ComposioMcpIntegration | null> {
  if (!configured(cfg)) return null;
  const allowlist = context.connectorTools === undefined
    ? undefined
    : serializeConnectorAllowedTools(context.connectorTools);
  if (allowlist?.oversized) {
    console.warn(
      `[composio] connector allowlist for bot ${context.botId} exceeds ${CONNECTOR_ALLOWED_TOOLS_MAX_BYTES} bytes;`
      + " tools/list stays unfiltered and grants are enforced per call",
    );
  }
  const serviceSlugs = context.connectorTools === undefined
    ? undefined
    : serializeConnectorServiceSlugs(await connectedServiceSlugs(cfg, context.as, { waitMs: TURN_START_SLUGS_WAIT_MS }));
  if (serviceSlugs?.oversized) {
    console.warn(
      `[composio] connector service slugs for bot ${context.botId} exceed ${CONNECTOR_SERVICE_SLUGS_MAX_BYTES} bytes;`
      + " tools/list falls back to plain-prefix resolution",
    );
  }
  return {
    command: process.execPath,
    args: [SPAWNED_PROXIES.connectors],
    env: {
      ELECTRON_RUN_AS_NODE: "1",
      // The bridge gets only this boot's loopback token; project and broker credentials never leave the harness.
      OMB_CONNECTOR_UPSTREAM_URL: `${context.harnessUrl}/api/internal/connectors/mcp`,
      OMB_CONNECTOR_UPSTREAM_HEADERS: JSON.stringify({ authorization: `Bearer ${context.commsToken}` }),
      OMB_HARNESS_URL: context.harnessUrl,
      // A name apart from the agents proxy token: Codex flattens all mounted MCP env into one environment.
      OMB_CONNECTOR_TOKEN: context.commsToken,
      OMB_BOT_ID: context.botId,
      OMB_THREAD_ID: context.threadId,
      ...(allowlist?.env ? { [CONNECTOR_ALLOWED_TOOLS_ENV]: allowlist.env } : {}),
      ...(serviceSlugs?.env ? { [CONNECTOR_SERVICE_SLUGS_ENV]: serviceSlugs.env } : {}),
    },
  };
}

export async function relayMcp(
  cfg: AppConfig,
  payload: JsonValue,
  transportSessionId?: string,
  beforeSend?: () => void | JsonValue | Promise<void | JsonValue>, signal?: AbortSignal, as?: BrokerAccess,
): Promise<{ status: number; bytes: Uint8Array; contentType: string; transportSessionId?: string }> {
  const apiKey = as ? null : projectApiKey(cfg);
  let url: string;
  let identity: string;
  const headers = new Headers({
    "content-type": "application/json",
    accept: "application/json, text/event-stream",
  });
  if (apiKey) {
    const session = await ensureProjectSession(cfg);
    url = session.mcp.url;
    headers.set("x-api-key", apiKey);
    identity = backendFingerprint("project-mcp", url, apiKey);
  } else {
    const broker = as ?? brokerAccess();
    if (!broker) throw new Error("Connected apps are unavailable");
    url = `${broker.url}/v1/mcp`;
    headers.set("authorization", `Bearer ${broker.token}`);
    identity = backendFingerprint("managed-mcp", url, broker.token);
  }
  const knownIdentity = transportSessionId
    ? transportSessionBackends.get(transportSessionId)
    : undefined;
  const forwardedTransportSessionId = transportSessionId
    && knownIdentity === identity
    ? transportSessionId
    : undefined;
  if (forwardedTransportSessionId) {
    headers.set("mcp-session-id", forwardedTransportSessionId);
  }
  // Discovery may await I/O: check turn authority and permissions after it, right before dispatch.
  const sent = await beforeSend?.(); if (sent !== undefined) payload = sent; // beforeSend may answer the frame to send instead (files it uploaded)
  const response = await withToolsListFallback(identity, payload, () => fetch(url, {
    method: "POST",
    headers,
    body: JSON.stringify(payload),
    signal: signal ?? AbortSignal.timeout(10 * 60_000),
  }));
  const declared = Number(response.headers.get("content-length") ?? "0");
  if (declared > 20 * 1024 * 1024) throw new Error("Connected-app response exceeded 20 MB");
  const raw = new Uint8Array(await response.arrayBuffer());
  if (raw.byteLength > 20 * 1024 * 1024) throw new Error("Connected-app response exceeded 20 MB");
  if (forwardedTransportSessionId) rememberTransportSession(forwardedTransportSessionId, identity);
  const nextTransportSessionId = response.headers.get("mcp-session-id") ?? undefined;
  if (nextTransportSessionId) rememberTransportSession(nextTransportSessionId, identity);
  const contentType = response.headers.get("content-type") ?? "application/json";
  const toolCall = typeof payload === "object" && payload !== null && !Array.isArray(payload) && payload.method === "tools/call";
  const toolkit = toolCall ? noManagedAuthToolkit(new TextDecoder().decode(raw)) : null;
  const bytes = toolkit
    ? await withNoManagedAuthNote(raw, contentType, toolkit, apiKey ? () => sessionForToolkit(cfg, toolkit, { fresh: true }) : null)
    : raw;
  return { status: response.status, bytes, contentType, transportSessionId: nextTransportSessionId };
}

async function listSessionToolkits(
  apiKey: string,
  sessionId: string,
): Promise<{ items: ToolkitItem[]; complete: boolean }> {
  const items: ToolkitItem[] = [];
  const seenCursors = new Set<string>();
  let cursor: string | undefined;
  let complete = true;
  for (let page = 0; page < MAX_CONNECTED_ACCOUNT_PAGES; page += 1) {
    // Unfiltered, this endpoint pages through the whole marketplace 50 at a time.
    const params = new URLSearchParams({ limit: "50", is_connected: "true" });
    if (cursor) params.set("cursor", cursor);
    const response = await fetch(
      `${apiBase()}/tool_router/session/${encodeURIComponent(sessionId)}/toolkits?${params}`,
      { headers: projectHeaders(apiKey), signal: AbortSignal.timeout(15_000) },
    );
    if (!response.ok) throw new Error(await responseError(response, `Composio toolkits: HTTP ${response.status}`));
    const raw: unknown = await response.json();
    const body = toolkitPageSchema.parse(raw);
    items.push(...(body.items ?? []));
    complete &&= toolkitInventoryPageSchema.safeParse(raw).success;
    const next = body.next_cursor || undefined;
    if (!next) return { items, complete };
    if (seenCursors.has(next)) return { items, complete: false };
    seenCursors.add(next);
    cursor = next;
  }
  throw new Error("Composio toolkit inventory exceeded the pagination safety limit");
}

function summarizeAccounts(accounts: ConnectedAccountResponse[], slugs: string[]) {
  const requested = new Set(slugs.map(canonicalToolkitSlug));
  const bySlug = new Map<string, Array<ConnectedAccountSummary & { updatedAt: string }>>();
  for (const account of accounts) {
    const slug = account.toolkit?.slug ? canonicalToolkitSlug(account.toolkit.slug) : "";
    if (!slug || (requested.size && !requested.has(slug)) || !validAccountId(account.id)) continue;
    const alias = account.alias?.trim() ?? "";
    const summary: ConnectedAccountSummary & { updatedAt: string } = {
      id: account.id,
      status: account.status || "UNKNOWN",
      updatedAt: account.updated_at ?? "",
    };
    if (printableAliasSchema.safeParse(alias).success) summary.alias = alias;
    const list = bySlug.get(slug) ?? [];
    list.push(summary);
    bySlug.set(slug, list);
  }
  for (const list of bySlug.values()) list.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
  return bySlug;
}

function publicAccount({ id, alias, status }: ConnectedAccountSummary): ConnectedAccountSummary {
  const account: ConnectedAccountSummary = { id, status };
  if (alias) account.alias = alias;
  return account;
}

function serviceStateFromAccounts(
  accounts: ConnectedAccountSummary[],
): ConnectorServiceState {
  const active = accounts.find((account) => /^active$/i.test(account.status));
  const pending = accounts.find((account) => /^(initiated|initializing|pending)$/i.test(account.status));
  const selected = active ?? pending ?? accounts[0];
  return {
    connected: Boolean(active),
    pending: Boolean(pending),
    status: selected?.status ?? "not_connected",
    accounts: accounts.map(publicAccount),
  };
}

function allServiceStates(
  accountsBySlug: ReadonlyMap<string, ConnectedAccountSummary[]>,
  toolkits: ToolkitItem[],
): Record<string, ConnectorServiceState> {
  const services = new Map(
    [...accountsBySlug].map(([slug, accounts]) => [slug, serviceStateFromAccounts(accounts)]),
  );
  for (const toolkit of toolkits) {
    const slug = toolkit.slug ? canonicalToolkitSlug(toolkit.slug) : "";
    const selected = toolkit.connected_account;
    const selectedId = validAccountId(selected?.id) ? selected.id : undefined;
    if (!slug || (!toolkit.is_no_auth && !selectedId)) continue;
    const existingAccounts = accountsBySlug.get(slug) ?? [];
    const accounts = [...existingAccounts];
    if (selectedId && !accounts.some((account) => account.id === selectedId)) {
      accounts.push({ id: selectedId, status: selected?.status ?? "ACTIVE" });
    }
    const accountState = serviceStateFromAccounts(accounts);
    const status = toolkit.is_no_auth ? "ACTIVE" : selected?.status ?? accountState.status;
    services.set(slug, {
      connected: toolkit.is_no_auth === true || accountState.connected || /^active$/i.test(status),
      pending: accountState.pending || /^(initiated|initializing|pending)$/i.test(status),
      status,
      accounts: accountState.accounts,
    });
  }
  return Object.fromEntries(services);
}

export interface ConnectedServicesInventory {
  services: Record<string, ConnectorServiceState>;
  complete: boolean;
}

export async function connectedServicesInventory(cfg: AppConfig, as?: BrokerAccess): Promise<ConnectedServicesInventory> {
  const apiKey = as ? null : projectApiKey(cfg);
  if (!apiKey) {
    const response = await brokerRequest("/v1/connectors/connected", undefined, as);
    if (!response.ok) await throwBrokerError(response, `Connected apps: HTTP ${response.status}`);
    const body = connectorServicesResponseSchema.required({ services: true }).parse(await response.json());
    const services = canonicalServiceRecord(body.services);
    return {
      services: Object.fromEntries(
        Object.entries(services).map(([slug, state]) => [slug, {
          connected: state.connected,
          pending: state.pending ?? false,
          status: state.status ?? (state.connected ? "ACTIVE" : "not_connected"),
          accounts: state.accounts ?? [],
        }]),
      ),
      // A broker that predates this field sends none, so only an explicit false means its account read came up short.
      complete: body.complete !== false,
    };
  }
  const session = await ensureProjectSession(cfg);
  const userId = session.config?.user_id ?? cfg.composio?.userId;
  if (!userId) throw new Error("Composio Session returned no user ID");
  const [toolkits, accountResult] = await Promise.all([
    listSessionToolkits(apiKey, session.session_id),
    // Scoped project keys can grant Session reads without the raw connected-account list.
    listConnectedAccounts(apiKey, userId, []).then((accounts) => ({ accounts, complete: accounts.every((account) => inventoryAccountSchema.safeParse(account).success) }))
      .catch(() => ({ accounts: [] as ConnectedAccountResponse[], complete: false })),
  ]);
  return {
    services: allServiceStates(summarizeAccounts(accountResult.accounts, []), toolkits.items),
    complete: accountResult.complete && toolkits.complete,
  };
}

/** The inventory as the Apps page shows it: one read for readers together or within 20 s, read anew after a connection is
 * started or removed here, or a status check differs from it. `fresh` always reads. */
export function sharedConnectedInventory(cfg: AppConfig, options: { fresh?: boolean } = {}): Promise<ConnectedServicesInventory> {
  return sharedInventory(connectorToolsIdentity(cfg), () => connectedServicesInventory(cfg), options.fresh);
}

function connectionsChanged(cfg: AppConfig, as?: BrokerAccess): void {
  const identity = connectorToolsIdentity(cfg, as);
  forgetInventory(identity);
  if (connectorServiceSlugsCache?.identity === identity) connectorServiceSlugsCache = { ...connectorServiceSlugsCache, at: -Infinity };
  if (connectorServiceSlugsRequest?.identity === identity) connectorServiceSlugsRequest = null;
}

export async function verifiedConnectedServiceSlugs(cfg: AppConfig, as?: BrokerAccess): Promise<readonly string[]> {
  const { services, complete } = await connectedServicesInventory(cfg, as);
  if (!complete) throw new Error("Connected-app inventory is incomplete; account discovery did not finish.");
  return Object.keys(services);
}

export async function connectedServices(cfg: AppConfig, as?: BrokerAccess): Promise<Record<string, ConnectorServiceState>> {
  return (await connectedServicesInventory(cfg, as)).services;
}

/** Rejects a connectorTools patch naming an unconnected service or a tool without its service prefix. Checks that need the
 * network fail open, since call-time enforcement decides. Returns the message or null. */
export async function validateConnectorGrants(
  cfg: AppConfig,
  grants: Record<string, ConnectorToolGrant>,
): Promise<string | null> {
  const connected = await connectedServices(cfg).catch(() => null);
  const candidates = connected ? Object.keys(connected) : [];
  for (const [slug, grant] of Object.entries(grants)) {
    if (grant.tools === "*") continue;
    const misplaced = grant.tools.filter(
      (tool) => (serviceSlugForCandidates(tool, candidates) ?? serviceSlugFor(tool)) !== slug,
    );
    if (misplaced.length) {
      return `connectorTools.${slug}.tools names tools that belong to another service: ${misplaced.join(", ")}`;
    }
  }
  if (!connected || !connectedAppGrantSlugs(grants).length) return null;
  const unconnected = connectedAppGrantSlugs(grants).filter((slug) => !connected[slug]?.connected);
  if (unconnected.length) {
    return `connectorTools names services that are not connected: ${unconnected.join(", ")}`;
  }
  const catalog = await listToolkits(cfg);
  if (catalog.source !== "api" || !catalog.pagination?.complete || !catalog.fresh) return null;
  const known = new Set(catalog.cards.map((card) => card.slug.toLowerCase()));
  const unknown = connectedAppGrantSlugs(grants).filter((slug) => !known.has(slug));
  if (unknown.length) {
    return `connectorTools names services missing from the connected-apps catalog: ${unknown.join(", ")}`;
  }
  return null;
}

export interface ConnectorToolListing {
  name: string;
  description?: string;
  /** The tool's own listing says it only reads: annotations.readOnlyHint, and not destructiveHint. */
  readOnly?: true;
}

/** The picker offers exactly what a granted bot's bridge would see. */
export async function listConnectorTools(
  cfg: AppConfig,
  options: { force?: boolean } = {},
): Promise<Record<string, ConnectorToolListing[]>> {
  const identity = connectorToolsIdentity(cfg);
  if (!options.force && connectorToolsCache?.identity === identity
    && Date.now() - connectorToolsCache.at < CONNECTOR_TOOLS_CACHE_MS) {
    return connectorToolsCache.services;
  }
  if (connectorToolsRequest?.identity === identity) return connectorToolsRequest.services;
  const walk = collectConnectorTools(cfg).then((services) => {
    connectorToolsCache = { at: Date.now(), identity, services };
    return services;
  });
  connectorToolsRequest = { identity, services: walk };
  try {
    return await walk;
  } finally {
    if (connectorToolsRequest?.services === walk) connectorToolsRequest = null;
  }
}

const CONNECTOR_TOOLS_CACHE_MS = 60_000;
let connectorToolsCache: { at: number; identity: string; services: Record<string, ConnectorToolListing[]> } | null = null;
/** Cache key per backend, so a config switch never serves the previous backend's tools. */
function connectorToolsIdentity(cfg: AppConfig, as?: BrokerAccess): string {
  const apiKey = as ? null : projectApiKey(cfg);
  if (apiKey) return backendFingerprint("project-tools", `${apiBase()}\0${cfg.composio?.userId ?? ""}`, apiKey);
  const broker = as ?? brokerAccess();
  return broker ? backendFingerprint("managed-tools", broker.url, broker.token) : "none";
}

let connectorToolsRequest: { identity: string; services: Promise<Record<string, ConnectorToolListing[]>> } | null = null;

/** Real backend slugs, so an underscored service (bland_ai) is never captured by a plain-prefix grant (bland); a failed read
 * answers the last good one. An answer under a minute old is used as it is. An older one is used while it is read again
 * behind, until it is CONNECTOR_SERVICE_SLUGS_STALE_MS old or a connection was started or removed here: from then the read
 * is waited for, for at most `waitMs` when a caller gives one.
 *
 * Security: this list only decides which service a tool's name belongs to. It never makes a connection usable: the vendor
 * refuses a removed one, and a call that names no tool is checked against verifiedConnectedServiceSlugs, which is never
 * cached. The risk in an old list is a service connected elsewhere since (bland_ai) being read as another's (bland), so the
 * list a call is judged by is never more than CONNECTOR_SERVICE_SLUGS_STALE_MS old. */
export async function connectedServiceSlugs(cfg: AppConfig, as?: BrokerAccess, options: { waitMs?: number } = {}): Promise<readonly string[]> {
  const identity = connectorToolsIdentity(cfg, as);
  const known = connectorServiceSlugsCache?.identity === identity ? connectorServiceSlugsCache : null;
  const age = known ? Date.now() - known.at : Infinity;
  if (known && age < CONNECTOR_SERVICE_SLUGS_CACHE_MS) return known.slugs;
  const read = connectorServiceSlugsRequest?.identity === identity ? connectorServiceSlugsRequest.slugs : readServiceSlugs(cfg, as, identity);
  if (known && age < CONNECTOR_SERVICE_SLUGS_STALE_MS) return known.slugs;
  if (options.waitMs === undefined) return read;
  let timer: ReturnType<typeof setTimeout> | undefined;
  const lastKnown = new Promise<readonly string[]>((resolve) => { timer = setTimeout(() => resolve(known?.slugs ?? []), options.waitMs); });
  return Promise.race([read, lastKnown]).finally(() => clearTimeout(timer));
}

function readServiceSlugs(cfg: AppConfig, as: BrokerAccess | undefined, identity: string): Promise<readonly string[]> {
  const read = connectedServices(cfg, as).then(
    (services) => {
      const slugs = Object.keys(services);
      // A read that a connection change overtook answers its own caller and is not kept.
      if (connectorServiceSlugsRequest?.slugs === read) connectorServiceSlugsCache = { at: Date.now(), identity, slugs };
      return slugs;
    },
    () => connectorServiceSlugsCache?.identity === identity ? connectorServiceSlugsCache.slugs : [] as readonly string[],
  ).finally(() => {
    if (connectorServiceSlugsRequest?.slugs === read) connectorServiceSlugsRequest = null;
  });
  connectorServiceSlugsRequest = { identity, slugs: read };
  serviceSlugReads.add(read);
  void read.finally(() => serviceSlugReads.delete(read));
  return read;
}

/** Resolves once no read of the service slugs is running. */
export async function serviceSlugsReadSettled(): Promise<void> {
  while (serviceSlugReads.size) await Promise.all(serviceSlugReads);
}

const CONNECTOR_SERVICE_SLUGS_CACHE_MS = 60_000;
export const CONNECTOR_SERVICE_SLUGS_STALE_MS = 5 * 60_000;
/** How long a turn's start waits for the slugs when none it may use are held. */
const TURN_START_SLUGS_WAIT_MS = 5_000;
let connectorServiceSlugsCache: { at: number; identity: string; slugs: readonly string[] } | null = null;
let connectorServiceSlugsRequest: { identity: string; slugs: Promise<readonly string[]> } | null = null;
const serviceSlugReads = new Set<Promise<readonly string[]>>();

const TOOL_DESCRIPTION_MAX = 240;
const TOOLS_LIST_PAGES_MAX = 10;

function connectorToolListing(tool: unknown): ConnectorToolListing | null {
  if (!tool || typeof tool !== "object" || Array.isArray(tool)) return null;
  const name = (tool as { name?: unknown }).name;
  if (typeof name !== "string" || !CONNECTOR_TOOL_NAME_PATTERN.test(name)) return null;
  if (name.startsWith("COMPOSIO_")) return null;
  if (name.endsWith("_MANAGE_CONNECTIONS") || name.endsWith("_WAIT_FOR_CONNECTIONS")) return null;
  const description = (tool as { description?: unknown }).description;
  const trimmed = typeof description === "string"
    ? description.replace(/\s+/g, " ").trim().slice(0, TOOL_DESCRIPTION_MAX)
    : "";
  const annotations = (tool as { annotations?: unknown }).annotations;
  const hints = annotations && typeof annotations === "object" && !Array.isArray(annotations) ? annotations as { readOnlyHint?: unknown; destructiveHint?: unknown } : {};
  const readOnly = hints.readOnlyHint === true && hints.destructiveHint !== true ? { readOnly: true as const } : {};
  return { name, ...(trimmed ? { description: trimmed } : {}), ...readOnly };
}

async function collectConnectorTools(cfg: AppConfig): Promise<Record<string, ConnectorToolListing[]>> {
  const candidates = await connectedServiceSlugs(cfg);
  const initialize = await relayMcp(cfg, {
    jsonrpc: "2.0",
    id: "omb-grants-initialize",
    method: "initialize",
    params: {
      protocolVersion: "2025-03-26",
      capabilities: {},
      clientInfo: { name: "openmausbot-grant-editor", version: "1" },
    },
  });
  if (initialize.status !== 200) {
    throw new Error(await responseErrorFromBytes(initialize.status, initialize.bytes));
  }
  const transportSessionId = initialize.transportSessionId;
  // Streamable-HTTP MCP servers expect the initialized notification before the first request.
  await relayMcp(cfg, {
    jsonrpc: "2.0",
    method: "notifications/initialized",
  }, transportSessionId).catch(() => undefined);
  const grouped = new Map<string, ConnectorToolListing[]>();
  let cursor: string | undefined;
  for (let page = 0; page < TOOLS_LIST_PAGES_MAX; page += 1) {
    const list = await relayMcp(cfg, {
      jsonrpc: "2.0",
      id: "omb-grants-tools",
      method: "tools/list",
      params: cursor ? { cursor } : {},
    }, transportSessionId);
    if (list.status !== 200) {
      throw new Error(await responseErrorFromBytes(list.status, list.bytes));
    }
    const frame = parseMcpResponse(new TextDecoder().decode(list.bytes), "omb-grants-tools");
    const error = frame && typeof frame.error === "object" && frame.error !== null
      ? (frame.error as { message?: unknown }).message
      : undefined;
    if (typeof error === "string" && error) throw new Error(error);
    const result = frame && typeof frame.result === "object" && frame.result !== null
      ? (frame.result as { tools?: unknown; nextCursor?: unknown })
      : undefined;
    if (!result || !Array.isArray(result.tools)) {
      throw new Error("Composio returned a tools/list response without a tools array");
    }
    for (const tool of result.tools) {
      const listing = connectorToolListing(tool);
      if (!listing) continue;
      const slug = serviceSlugForCandidates(listing.name, candidates) ?? serviceSlugFor(listing.name);
      if (!slug) continue;
      const bucket = grouped.get(slug) ?? [];
      if (!bucket.some((existing) => existing.name === listing.name)) bucket.push(listing);
      grouped.set(slug, bucket);
    }
    cursor = typeof result.nextCursor === "string" && result.nextCursor ? result.nextCursor : undefined;
    if (!cursor) break;
  }
  for (const bucket of grouped.values()) bucket.sort((a, b) => a.name.localeCompare(b.name));
  return Object.fromEntries([...grouped.entries()].sort(([a], [b]) => a.localeCompare(b)));
}

async function responseErrorFromBytes(status: number, bytes: Uint8Array): Promise<string> {
  try {
    const body: unknown = JSON.parse(new TextDecoder().decode(bytes));
    const message = body && typeof body === "object" && !Array.isArray(body)
      ? (body as { error?: unknown }).error
      : undefined;
    if (message && typeof message === "object" && !Array.isArray(message)) {
      const text = (message as { message?: unknown }).message;
      if (typeof text === "string" && text) return text;
    }
    if (typeof message === "string" && message) return message;
  } catch {
  }
  return `Composio tools/list: HTTP ${status}`;
}

export async function connectionStatus(cfg: AppConfig, slugs: string[]) {
  const status = await readConnectionStatus(cfg, slugs);
  if (inventoryDiffers(connectorToolsIdentity(cfg), status, canonicalToolkitSlug)) connectionsChanged(cfg);
  return status;
}

async function readConnectionStatus(cfg: AppConfig, slugs: string[]) {
  const canonicalSlugs = [...new Set(slugs.map(canonicalToolkitSlug))];
  const apiKey = projectApiKey(cfg);
  if (!apiKey) {
    const response = await brokerRequest(`/v1/connectors?${new URLSearchParams({ services: canonicalSlugs.join(",") })}`);
    if (!response.ok) await throwBrokerError(response, `Connected apps: HTTP ${response.status}`);
    const body = connectorServicesResponseSchema.parse(await response.json());
    return requestedServiceRecord(body.services ?? {}, slugs);
  }
  const session = await ensureProjectSession(cfg);
  const params = new URLSearchParams({ limit: "50" });
  if (canonicalSlugs.length) params.set("toolkits", canonicalSlugs.join(","));
  const userId = session.config?.user_id ?? cfg.composio?.userId;
  const [res, accounts] = await Promise.all([
    fetch(`${apiBase()}/tool_router/session/${encodeURIComponent(session.session_id)}/toolkits?${params}`, {
      headers: projectHeaders(apiKey),
      signal: AbortSignal.timeout(15_000),
    }),
    // Session toolkits list an account only once it is usable, so read the account lifecycle too (scoped keys may be denied it).
    userId
      ? listConnectedAccounts(apiKey, userId, canonicalSlugs).catch(() => [])
      : Promise.resolve([]),
  ]);
  if (!res.ok) throw new Error(await responseError(res, `Composio toolkits: HTTP ${res.status}`));
  const body = toolkitPageSchema.parse(await res.json());
  const bySlug = new Map(
    (body.items ?? []).flatMap((item) => item.slug ? [[canonicalToolkitSlug(item.slug), item] as const] : []),
  );
  const accountsBySlug = summarizeAccounts(accounts, canonicalSlugs);
  return Object.fromEntries(
    slugs.map((slug) => {
      const canonicalSlug = canonicalToolkitSlug(slug);
      const item = bySlug.get(canonicalSlug);
      const serviceAccounts = accountsBySlug.get(canonicalSlug) ?? [];
      const selected = item?.connected_account;
      const selectedId = validAccountId(selected?.id) ? selected.id : undefined;
      const withSelected = selectedId && !serviceAccounts.some((account) => account.id === selectedId)
        ? [...serviceAccounts, { id: selectedId, status: selected?.status ?? "ACTIVE" }]
        : serviceAccounts;
      const accountState = serviceStateFromAccounts(withSelected);
      const state = item?.connected_account?.status
        ?? (item?.is_no_auth ? "ACTIVE" : accountState.status);
      return [slug, {
        connected: item?.is_no_auth === true || accountState.connected || /^active$/i.test(state),
        pending: accountState.pending || /^(initiated|initializing|pending)$/i.test(state),
        status: state,
        accounts: accountState.accounts,
      }];
    }),
  );
}

export async function removeService(cfg: AppConfig, slug: string, as?: BrokerAccess) {
  const toolkit = canonicalToolkitSlug(slug);
  const apiKey = as ? null : projectApiKey(cfg);
  if (!apiKey) {
    const response = await brokerRequest(`/v1/connectors/${encodeURIComponent(toolkit)}`, { method: "DELETE" }, as);
    connectionsChanged(cfg, as);
    if (!response.ok) await throwBrokerError(response, `Connected apps: HTTP ${response.status}`);
    return removalResponseSchema.parse(await response.json());
  }
  const session = await ensureProjectSession(cfg);
  const params = new URLSearchParams({ limit: "50", toolkits: toolkit });
  const list = await fetch(
    `${apiBase()}/tool_router/session/${encodeURIComponent(session.session_id)}/toolkits?${params}`,
    { headers: projectHeaders(apiKey), signal: AbortSignal.timeout(15_000) },
  );
  if (!list.ok) throw new Error(await responseError(list, `Composio toolkits: HTTP ${list.status}`));
  const body = toolkitPageSchema.parse(await list.json());
  const id = body.items?.find((item) => item.slug && canonicalToolkitSlug(item.slug) === toolkit)?.connected_account?.id;
  if (!id) return { removed: 0 };
  connectionsChanged(cfg);
  await deleteConnectedAccount(apiKey, id);
  return { removed: 1 };
}

/** Disconnect exactly one account after proving it belongs to this user/toolkit. */
export async function removeAccount(cfg: AppConfig, slug: string, accountId: string) {
  if (!validAccountId(accountId)) throw inputError("Invalid connected-account ID");
  const toolkit = canonicalToolkitSlug(slug);
  const apiKey = projectApiKey(cfg);
  if (!apiKey) {
    const response = await brokerRequest(
      `/v1/connectors/${encodeURIComponent(toolkit)}/accounts/${encodeURIComponent(accountId)}`,
      { method: "DELETE" },
    );
    connectionsChanged(cfg);
    if (!response.ok) await throwBrokerError(response, `Connected apps: HTTP ${response.status}`);
    return removalResponseSchema.parse(await response.json());
  }
  const session = await ensureProjectSession(cfg);
  const userId = session.config?.user_id ?? cfg.composio?.userId;
  if (!userId) throw new Error("Composio Session has no user ID");
  const accounts = await listConnectedAccounts(apiKey, userId, [toolkit]);
  const owned = accounts.some((account) =>
    account.id === accountId
      && account.toolkit?.slug !== undefined
      && canonicalToolkitSlug(account.toolkit.slug) === toolkit
  );
  if (!owned) return { removed: 0 };
  connectionsChanged(cfg);
  await deleteConnectedAccount(apiKey, accountId);
  return { removed: 1 };
}

/** Readies a bot's connection request before its cards show: an auth config the bot named is checked in the person's own
 * project and kept, and each toolkit's own config is named in the Session. Returns which config each toolkit uses. */
export async function prepareConnectionAuthConfigs(
  cfg: AppConfig,
  items: ReadonlyArray<{ slug: string; authConfigId?: string }>,
): Promise<string | undefined> {
  const apiKey = projectApiKey(cfg);
  for (const { slug, authConfigId } of items) {
    if (!authConfigId) continue;
    const toolkit = canonicalToolkitSlug(slug);
    if (!apiKey) throw inputError(managedCustomAuthReason(toolkit), 409);
    await keepAuthConfigPick(apiKey, toolkit, authConfigId);
  }
  if (!apiKey) return undefined;
  const lines: string[] = [];
  for (const toolkit of new Set(items.map(({ slug }) => canonicalToolkitSlug(slug)))) {
    // A config Composio won't take yet still gets its card: Connect then says why.
    const { choice } = await sessionForToolkit(cfg, toolkit).catch(() => ({ choice: null }));
    if (choice) lines.push(authConfigUseLine(toolkit, choice));
  }
  return lines.length ? lines.join(" ") : undefined;
}

export async function authorizeService(cfg: AppConfig, slug: string, requestedAlias?: string | null, as?: BrokerAccess) {
  const alias = normalizeAccountAlias(requestedAlias);
  const toolkit = canonicalToolkitSlug(slug);
  const unavailable = managedConnectorUnavailableReason(as ? "managed" : connectionMode(cfg), toolkit);
  if (unavailable) throw inputError(unavailable, 409);
  const apiKey = as ? null : projectApiKey(cfg);
  connectionsChanged(cfg, as);
  if (!apiKey) {
    const request: RequestInit = { method: "POST" };
    if (alias) request.body = JSON.stringify({ alias });
    const response = await brokerRequest(`/v1/connectors/${encodeURIComponent(toolkit)}/authorize`, request, as);
    if (!response.ok && noManagedAuthToolkit(await response.clone().text().catch(() => ""))) {
      throw inputError(managedCustomAuthReason(toolkit), 409);
    }
    if (!response.ok) await throwBrokerError(response, `Connected apps: HTTP ${response.status}`);
    const body = authUrlResponseSchema.parse(await response.json());
    return { url: trustedAuthUrl(body.url, toolkit) };
  }
  const { session } = await sessionForToolkit(cfg, toolkit);
  const userId = session.config?.user_id ?? cfg.composio?.userId;
  if (!userId) throw new Error("Composio Session has no user ID");
  const accounts = await listConnectedAccounts(apiKey, userId, [toolkit]).catch(() => []);
  const serviceAccounts = accounts.filter((account) =>
    account.toolkit?.slug !== undefined && canonicalToolkitSlug(account.toolkit.slug) === toolkit
  );
  const plan = planAccountLink(toolkit, serviceAccounts, alias, MULTI_ACCOUNT_CONFIG.max_accounts_per_toolkit, () => `omb-retry-${randomUUID()}`);
  if (!plan.ok) throw inputError(plan.error, plan.status);
  for (const id of plan.replace) await deleteConnectedAccount(apiKey, id);
  await raiseAccountCap(apiKey, session);
  const linkRequest: AccountLinkRequest = plan.alias ? { toolkit, alias: plan.alias } : { toolkit };
  const link = (sessionId: string) =>
    fetch(`${apiBase()}/tool_router/session/${encodeURIComponent(sessionId)}/link`, {
      method: "POST",
      headers: projectHeaders(apiKey, true),
      body: JSON.stringify(linkRequest),
      signal: AbortSignal.timeout(30_000),
    });
  let res = await link(session.session_id);
  if (!res.ok) {
    const message = await responseError(res, `Composio authorization: HTTP ${res.status}`);
    if (!NEEDS_AUTH_CONFIG.test(message)) throw new Error(message);
    const fresh = await sessionForToolkit(cfg, toolkit, { fresh: true });
    if (!fresh.choice) {
      if (fresh.readError) throw new Error(`Composio auth configs: ${fresh.readError}`);
      throw inputError(noAuthConfigReason(toolkit));
    }
    res = await link(fresh.session.session_id);
    if (!res.ok) {
      const again = await responseError(res, `Composio authorization: HTTP ${res.status}`);
      if (!NEEDS_AUTH_CONFIG.test(again)) throw new Error(again);
      throw inputError(refusedAuthConfigReason(toolkit, authConfigRefusal(fresh.choice.id) ?? again));
    }
  }
  const body = linkResponseSchema.parse(await res.json());
  return { url: trustedAuthUrl(body.redirect_url, toolkit) };
}

const CURATED: Array<ToolkitCard & { slug: CuratedConnectorSlug }> = [
  { slug: "slack", label: "Slack", blurb: "Post updates and read channels", domain: "slack.com", logo: null, categories: ["Communication"] },
  { slug: "github", label: "GitHub", blurb: "Issues, pull requests, and code", domain: "github.com", logo: null, categories: ["Developer tools"] },
  { slug: "gmail", label: "Gmail", blurb: "Read and send email", domain: "gmail.com", logo: null, categories: ["Communication"] },
  { slug: "googlecalendar", label: "Google Calendar", blurb: "Read and create events", domain: "calendar.google.com", logo: null, categories: ["Productivity"] },
  { slug: "googlesheets", label: "Google Sheets", blurb: "Read and update spreadsheets", domain: "sheets.google.com", logo: null, categories: ["Productivity"] },
  { slug: "googledocs", label: "Google Docs", blurb: "Read and write documents", domain: "docs.google.com", logo: null, categories: ["Productivity"] },
  { slug: "googledrive", label: "Google Drive", blurb: "Browse and manage files", domain: "drive.google.com", logo: null, categories: ["Files"] },
  { slug: "notion", label: "Notion", blurb: "Pages and databases", domain: "notion.so", logo: null, categories: ["Productivity"] },
  { slug: "linear", label: "Linear", blurb: "Issues and project tracking", domain: "linear.app", logo: null, categories: ["Developer tools"] },
  { slug: "sentry", label: "Sentry", blurb: "Errors and alerts", domain: "sentry.io", logo: null, categories: ["Developer tools"] },
  { slug: "posthog", label: "PostHog", blurb: "Analytics, feature flags, experiments", domain: "posthog.com", logo: null, categories: ["Analytics"] },
  { slug: "discord", label: "Discord", blurb: "Messages and channels", domain: "discord.com", logo: null, categories: ["Communication"] },
  { slug: "twitter", label: "X (Twitter)", blurb: "Post and read on X", domain: "x.com", logo: null, categories: ["Social"] },
  { slug: "reddit", label: "Reddit", blurb: "Browse and post", domain: "reddit.com", logo: null, categories: ["Social"] },
  { slug: "zapier", label: "Zapier", blurb: "Connect 9,000+ apps", domain: "zapier.com", logo: null, categories: ["Automation"] },
  { slug: "hubspot", label: "HubSpot", blurb: "CRM search & updates", domain: "hubspot.com", logo: null, categories: ["Sales and CRM"] },
  { slug: "salesforce", label: "Salesforce", blurb: "CRM records and reports", domain: "salesforce.com", logo: null, categories: ["Sales and CRM"] },
  { slug: "jira", label: "Jira", blurb: "Issues and sprints", domain: "atlassian.com", logo: null, categories: ["Developer tools"] },
  { slug: "asana", label: "Asana", blurb: "Tasks and projects", domain: "asana.com", logo: null, categories: ["Productivity"] },
  { slug: "trello", label: "Trello", blurb: "Boards and cards", domain: "trello.com", logo: null, categories: ["Productivity"] },
  { slug: "dropbox", label: "Dropbox", blurb: "Files and folders", domain: "dropbox.com", logo: null, categories: ["Files"] },
  { slug: "airtable", label: "Airtable", blurb: "Bases and records", domain: "airtable.com", logo: null, categories: ["Productivity"] },
  { slug: "figma", label: "Figma", blurb: "Files and comments", domain: "figma.com", logo: null, categories: ["Design"] },
  { slug: "stripe", label: "Stripe", blurb: "Payments and customers", domain: "stripe.com", logo: null, categories: ["Finance"] },
];

export async function listToolkits(cfg: AppConfig): Promise<{ cards: ToolkitCard[]; source: "api" | "curated"; pagination?: CatalogPagination; fresh?: boolean }> {
  const backendKey = projectApiKey(cfg);
  const broker = backendKey ? null : brokerAccess();
  const base = toolkitBase();
  const identity = backendKey
    ? backendFingerprint("project-catalog", base, backendKey)
    : broker
      ? backendFingerprint("managed-catalog", broker.url, broker.token)
      : null;
  if (!identity) return { cards: CURATED, source: "curated" };
  const catalog = await catalogFor(identity, (cursor) => {
    if (!backendKey) {
      return brokerRequest(cursor ? `/v1/catalog?cursor=${encodeURIComponent(cursor)}` : "/v1/catalog", { signal: AbortSignal.timeout(15_000) }, broker);
    }
    const params = new URLSearchParams({ limit: "500", sort_by: "usage" });
    if (cursor) params.set("cursor", cursor);
    return fetch(`${base}/toolkits?${params}`, { headers: { "x-api-key": backendKey }, signal: AbortSignal.timeout(15_000) });
  });
  return catalog ? { ...catalog, source: "api" } : { cards: CURATED, source: "curated" };
}

export async function toolkitCard(cfg: AppConfig, slug: string): Promise<ToolkitCard> {
  const normalized = canonicalToolkitSlug(slug);
  const { cards } = await listToolkits(cfg);
  return cards.find((card) => card.slug.toLowerCase() === normalized)
    ?? CURATED.find((card) => card.slug === normalized)
    ?? {
      slug: normalized,
      label: normalized.replace(/[-_]+/g, " ").replace(/\b\w/g, (letter) => letter.toUpperCase()),
      blurb: "Connect this app so your bot can continue",
      logo: null,
      domain: null,
    };
}

export const CURATED_SLUGS = CURATED.map((c) => c.slug);