import { type ApiIntegrationAuthPreview, type IntegrationSourceInput, type McpServerConnectionRef } from "@opengeni/contracts"; import { type Database } from "./database.js"; import * as schema from "./schema.js"; export type ApiIntegrationProtocol = "openapi" | "graphql"; export type ApiIntegrationToolSafety = "read" | "write" | "destructive"; export type ApiIntegrationApprovalMode = "never" | "ask"; export type ApiIntegrationFacetDefinition = { readonly facetKey: string; readonly kind: "inbound_trigger" | "delivery_destination" | "identity_link"; readonly configSchema: Readonly>; readonly capabilities: Readonly>; }; export type ApiIntegrationToolDefinition = { readonly id: string; readonly operationKey: string; readonly name: string; readonly description: string; readonly inputSchema: Readonly>; readonly outputSchema?: Readonly>; readonly safety: ApiIntegrationToolSafety; readonly approvalMode: ApiIntegrationApprovalMode; readonly deprecated: boolean; }; type StoredApiIntegrationRevisionBase = { readonly id: string; readonly definitionId: string; readonly contentSha256: string; readonly source: { readonly url?: string; readonly provider?: string; readonly fetchedAt?: string; }; readonly title: string; readonly description?: string; readonly version?: string; readonly tools: readonly ApiIntegrationToolDefinition[]; }; export type StoredOpenApiOperationBinding = { readonly method: "get" | "put" | "post" | "delete" | "patch" | "head" | "options" | "trace"; readonly pathTemplate: string; readonly serverUrl: string; readonly parameters: readonly { readonly name: string; readonly location: "path" | "query" | "header" | "cookie"; readonly required: boolean; readonly schema: Readonly>; readonly description?: string; }[]; readonly requestBody?: { readonly required: boolean; readonly contentTypes: readonly string[]; readonly schemas: Readonly>>>; }; readonly requiredScopeAlternatives?: readonly (readonly string[])[]; /** Reviewed Microsoft Search entity types a read-only search may request. */ readonly searchEntityTypes?: readonly string[]; /** One of the generic find and call tools of a reviewed definition's path scope. */ readonly genericPath?: { readonly role: "find" | "read" | "change"; readonly operations?: readonly { readonly method: "get" | "put" | "post" | "delete" | "patch"; readonly path: string; readonly summary?: string; readonly scopes?: readonly (readonly string[])[]; }[]; }; }; export type StoredGraphqlOperationBinding = { readonly kind: "query" | "mutation"; readonly fieldName: string; readonly operationName: string; readonly variableDefinitions: readonly string[]; readonly variableNames: readonly string[]; readonly defaultSelection?: string; readonly selectionAllowed: boolean; }; /** Protocol-normalized immutable revision persisted in integration_spec_revisions.spec. */ export type StoredApiIntegrationRevision = (StoredApiIntegrationRevisionBase & { readonly protocol: "openapi"; readonly bindings: Readonly>; }) | (StoredApiIntegrationRevisionBase & { readonly protocol: "graphql"; readonly bindings: Readonly>; }); export type InstallApiIntegrationInput = { accountId: string; workspaceId: string; subjectId: string; capabilityId: string; pluginKey: string; serverId: string; name: string; description?: string | null; category?: string; tags?: string[]; definitionId: string; definitionProvenance: "curated" | "workspace"; provider?: string; providerDomain: string; protocol: ApiIntegrationProtocol; baseUrl: string; sourceUrl?: string | null; authScheme?: Record; connectionId?: string | null; instanceKey?: string; displayName?: string; expectedInstanceVersion?: number; requiredScopes?: string[]; ownership?: "workspace" | "subject" | "either"; allowedTools?: string[]; /** * Selected write/destructive tools that run without per-call human approval. * Removing an approval requirement is never a permission reduction, so it * always passes installation acquisition (organization policy) again. */ autoApprovedTools?: string[]; facetDefinitions?: readonly ApiIntegrationFacetDefinition[]; revision: StoredApiIntegrationRevision; owner?: ApiIntegrationOwner; /** * A member's add (no integration management): create only. An instance that * is already set up, or a new revision that would move the other instances, * throws {@link IntegrationChangeRequiresAdminError} inside the transaction. */ memberCreate?: boolean; }; export type InstalledApiIntegration = { capabilityId: string; pluginId: string; pluginVersionId: string; integrationFacetId: string; apiFacetId: string; pluginInstallationId: string; integrationFacetInstallationId: string; apiFacetInstallationId: string; installationVersion: number; instanceId: string; instanceKey: string; displayName: string; instanceVersion: number; revisionId: string; serverId: string; status: "installed"; }; export type ApiIntegrationRuntime = { capabilityId: string; pluginKey: string; pluginInstallationId: string; installationVersion: number; instanceId: string; instanceKey: string; displayName: string; instanceVersion: number; serverId: string; name: string; description: string | null; protocol: ApiIntegrationProtocol; definitionId: string; definitionProvenance: "curated" | "workspace"; baseUrl: string; sourceUrl: string | null; providerDomain: string; authScheme: Record; connectionRef: McpServerConnectionRef | null; /** The bound connection's status (`active`, `needs_reauth`, `revoked`, `error`); null without one. */ connectionStatus: string | null; /** When the bound connection was last used (ISO time); null without one or before first use. */ connectionLastUsedAt: string | null; /** Live credential authority revision used to invalidate stale human approvals. */ connectionAuthorityGeneration: number | null; allowedTools: string[]; requireApproval: true | string[]; revision: StoredApiIntegrationRevision; /** * Set when the organization serves it here (an organization connector of * kind "api"), not an installation of this workspace's own. */ organizationConnector?: { id: string; generation: number; login: "each_person" | "organization_key" | "none"; /** How the API authenticates, as the organization reviewed it. */ auth: ApiIntegrationAuthPreview; }; /** * An organization key: where each credential goes and its encrypted value, * decrypted only by the adapter that sends it. */ organizationCredential?: { placements: readonly OrganizationApiCredentialPlacement[]; }; /** Who added this instance (a member removes only their own); set by the installed list. */ instanceCreatedBySubjectId?: string; }; /** One encrypted credential placement of an organization key for an API integration. */ export type OrganizationApiCredentialPlacement = { carrier: "header" | "query" | "cookie"; name: string; prefix?: string; valueEncrypted: string; }; /** The instance key an organization's API integration runs under in every workspace it reaches. */ export declare const ORGANIZATION_API_INTEGRATION_INSTANCE_KEY = "organization"; /** Bound to an account that is signed in now; an expired or revoked one needs reconnecting. */ export declare function apiIntegrationConnected(integration: Pick): boolean; export type ApiIntegrationOwner = { kind: "direct" | "plugin" | "migration"; id: string; removable: boolean; }; export type ApiIntegrationUninstallPreview = { capabilityId: string; instanceKey: string; displayName: string | null; installed: boolean; installationVersion: number | null; instanceVersion: number | null; directOwner: ApiIntegrationOwner | null; remainingOwners: ApiIntegrationOwner[]; removesRuntimeIntegration: boolean; removesDefinition: boolean; }; export type UninstallApiIntegrationResult = { capabilityId: string; instanceKey: string; status: "not_installed" | "uninstalled" | "retained_by_other_owners"; remainingOwners: ApiIntegrationOwner[]; definitionStatus: "retained" | "disabled"; }; export declare class ApiIntegrationInstallationVersionConflictError extends Error { readonly capabilityId: string; readonly expectedVersion: number; readonly actualVersion: number; readonly name = "ApiIntegrationInstallationVersionConflictError"; constructor(capabilityId: string, expectedVersion: number, actualVersion: number); } /** A known rejection of the selected credential, not an internal install failure. */ export declare class ApiIntegrationConnectionReferenceError extends Error { readonly reason: "not_found" | "inactive" | "provider_mismatch" | "kind_mismatch" | "scope_mismatch"; readonly name = "ApiIntegrationConnectionReferenceError"; constructor(reason: "not_found" | "inactive" | "provider_mismatch" | "kind_mismatch" | "scope_mismatch", message: string); } export declare function installApiIntegration(db: Database, input: InstallApiIntegrationInput): Promise; export declare function listInstalledApiIntegrations(db: Database, workspaceId: string, subjectId?: string, /** Only bindings with these runtime server ids (a live target fence). */ serverIds?: readonly string[]): Promise; /** * Exact runnable API-integration server ids for a scheduled service run. * Only runtime ids and the minimum delegation-matching metadata are selected; * no integration definition, credential, token, endpoint, or tool schema is * materialized before incident admission. */ export declare function listInstalledApiIntegrationServerIdsForDelegations(db: Database, workspaceId: string, delegations: readonly { serverId: string; connectionId: string; providerDomain: string; ownerSubjectId: string; kind?: string | undefined; }[]): Promise; /** * The API integrations that run in this workspace for the reading subject: * the workspace's own installations it can see (shared ones, plus its own * "Only me" ones), then the organization's API integrations that reach the * workspace. A workspace's own setup of the same integration wins: an * organization one is left out when the reader already has an instance of * the same capability here, or when the workspace turned it off. */ export declare function listInstalledApiIntegrationsInRlsContext(scopedDb: Database, workspaceId: string, subjectId?: string, serverIds?: readonly string[]): Promise; /** * The organization's active API integrations that reach this workspace and * that this workspace turned off (its turned-off override installation, the * `organizationOverride` marker of ORGANIZATION_CONNECTOR_OVERRIDE_METADATA_KEY), * so the workspace can list each as off here and turn it back on. */ export declare function listOrganizationApiIntegrationsTurnedOffHere(db: Database, workspaceId: string): Promise; /** * An organization API integration as the runtime a workspace installation of * it would be, under the "organization" instance key. "Each person signs in" * is a per-person selector (no connection id): each turn attaches the * initiating person's own account for that provider, exactly as for an * organization MCP connector. An organization key carries its encrypted * placements; "none" calls the API without credentials. */ export declare function organizationApiIntegrationRuntime(row: typeof schema.organizationConnectors.$inferSelect): ApiIntegrationRuntime[]; /** The encrypted placements of an organization key, as stored on an API organization connector. */ export declare function organizationApiCredentialPlacements(config: Record): OrganizationApiCredentialPlacement[]; export declare function getApiIntegrationUninstallPreview(db: Database, workspaceId: string, subjectId: string, capabilityId: string, instanceKey: string): Promise; /** No discovery or new authority: recover only an exact, currently visible and * directly owned installed source. The installer must still lock and recheck * the snapshot before committing; this read is not installation admission. */ export declare function getApiIntegrationReconciliationSnapshot(db: Database, input: { accountId: string; workspaceId: string; subjectId: string; source: IntegrationSourceInput; connectionId?: string; instanceKey?: string; expectedRevisionId: string; expectedContentSha256: string; allowedTools?: string[]; }): Promise<{ runtime: ApiIntegrationRuntime; baseServerId: string; name: string; provider: string | null; requiredScopes: string[]; } | null>; export declare function uninstallApiIntegration(db: Database, input: { accountId: string; workspaceId: string; subjectId: string; capabilityId: string; instanceKey: string; expectedInstallationVersion: number; expectedInstanceVersion: number; /** * A member's removal: only an instance this subject added. One an admin * or another person added throws {@link IntegrationChangeRequiresAdminError}. */ memberRemoval?: boolean; }): Promise; export {};