import { type Database } from "./database.js"; import { organizationCredentialProviders, organizationWebhookDeliveries, organizationWebhooks, workspaceCredentialProviders, workspaceWebhookDeliveries, workspaceWebhooks, type IntegrationWorkspaceFilter } from "./schema.js"; export type WorkspaceIntegrationScope = { accountId: string; workspaceId: string; }; export declare const WORKSPACE_WEBHOOK_LIMIT = 10; export declare const WORKSPACE_WEBHOOK_MAX_ATTEMPTS = 12; export declare class WorkspaceWebhookLimitError extends Error { constructor(); } /** Someone picked a workspace that isn't one of the organization's shared workspaces. */ export declare class OrganizationIntegrationWorkspaceError extends Error { readonly workspaceIds: readonly string[]; constructor(workspaceIds: readonly string[]); } export type WorkspaceCredentialProviderRow = typeof workspaceCredentialProviders.$inferSelect; export type WorkspaceWebhookRow = typeof workspaceWebhooks.$inferSelect; export type WorkspaceWebhookDeliveryRow = typeof workspaceWebhookDeliveries.$inferSelect; export type OrganizationIntegrationScope = { accountId: string; }; export type OrganizationCredentialProviderRow = typeof organizationCredentialProviders.$inferSelect; export type OrganizationWebhookRow = typeof organizationWebhooks.$inferSelect; export type OrganizationWebhookDeliveryRow = typeof organizationWebhookDeliveries.$inferSelect; export type ResolvedWorkspaceCredentialProvider = WorkspaceCredentialProviderRow | OrganizationCredentialProviderRow; export type InitiatingHuman = { subjectId: string; externalIdentity: { source: string; externalId: string; } | null; }; export declare function integrationInitiatingHuman(subjectId: string | null, externalIdentity: InitiatingHuman["externalIdentity"]): InitiatingHuman | null; export declare function selectWorkspaceCredentialProvider(scope: WorkspaceIntegrationScope, workspace: { accountId: string; id: string; externalSource: string | null; kind: "personal" | "shared"; } | null, provider: W | null, organizationProvider: O | null): W | O | null; /** * Whether an organization's credential provider covers a workspace: its list * and filter pick shared workspaces, its Personal switch picks members' * Personal ones. */ export declare function organizationCredentialProviderReaches(provider: { workspaceFilter: IntegrationWorkspaceFilter | null; allowPersonalWorkspaces: boolean; allowedWorkspaceIds: string[] | null; }, workspace: { id: string; externalSource: string | null; kind: "personal" | "shared"; }): boolean; /** Whether an organization webhook receives a workspace's events: never a Personal one. */ export declare function organizationWebhookReaches(webhook: { workspaceFilter: IntegrationWorkspaceFilter | null; allowedWorkspaceIds: string[] | null; }, workspace: { id: string; externalSource: string | null; kind: "personal" | "shared"; }): boolean; /** Hold through read + create/update so only the winning create returns a secret. */ export declare function withCredentialProviderConfigurationLock(db: Database, scope: { accountId: string; workspaceId?: string; }, fn: (tx: Database) => Promise): Promise; /** Attribution only, never authorization; no session-creator fallback. */ export declare function resolveInitiatingHuman(db: Database, scope: WorkspaceIntegrationScope, subjectId: string | null, turnId?: string): Promise; export declare function integrationWorkspaceFilterMatches(filter: IntegrationWorkspaceFilter | null, workspace: { externalSource: string | null; }): boolean; /** * Only absence inherits organization configuration. A Personal workspace * inherits only from a provider whose Personal switch is on. */ export declare function resolveWorkspaceCredentialProvider(db: Database, scope: WorkspaceIntegrationScope): Promise; export declare function getOrganizationCredentialProvider(db: Database, scope: OrganizationIntegrationScope): Promise; export declare function upsertOrganizationCredentialProvider(db: Database, input: OrganizationIntegrationScope & { url: string; secretEncrypted?: string; enabled: boolean; timeoutMs: number; workspaceFilter: IntegrationWorkspaceFilter | null; /** Replaced on every write, like the rest; omitted means off. */ allowPersonalWorkspaces?: boolean; /** * Shared workspaces it serves; null is every one. Omitted keeps the * stored list, so a client that doesn't know it never widens the reach. */ allowedWorkspaceIds?: readonly string[] | null; createdBySubjectId: string | null; }): Promise; export declare function deleteOrganizationCredentialProvider(db: Database, scope: OrganizationIntegrationScope): Promise; /** Replace the only accepted secret atomically; never keep an overlap slot. */ export declare function rotateOrganizationCredentialProviderSecret(db: Database, input: OrganizationIntegrationScope & { secretEncrypted: string; }): Promise; export declare function rotateOrganizationWebhookSecret(db: Database, input: OrganizationIntegrationScope & { webhookId: string; secretEncrypted: string; }): Promise; export declare const ORGANIZATION_WEBHOOK_LIMIT = 10; export declare class OrganizationWebhookLimitError extends Error { constructor(); } export declare function listOrganizationWebhooks(db: Database, scope: OrganizationIntegrationScope): Promise; export declare function getOrganizationWebhook(db: Database, input: OrganizationIntegrationScope & { webhookId: string; }): Promise; export declare function createOrganizationWebhook(db: Database, input: OrganizationIntegrationScope & { url: string; secretEncrypted: string; eventTypes: string[]; enabled: boolean; description: string | null; workspaceFilter: IntegrationWorkspaceFilter | null; /** Shared workspaces whose events it receives; null or omitted is every one. */ allowedWorkspaceIds?: readonly string[] | null; createdBySubjectId: string | null; }): Promise; export declare function updateOrganizationWebhook(db: Database, input: OrganizationIntegrationScope & { webhookId: string; url?: string; eventTypes?: string[]; enabled?: boolean; description?: string | null; workspaceFilter?: IntegrationWorkspaceFilter | null; allowedWorkspaceIds?: readonly string[] | null; }): Promise; export declare function deleteOrganizationWebhook(db: Database, input: OrganizationIntegrationScope & { webhookId: string; }): Promise; export declare function listOrganizationWebhookDeliveries(db: Database, input: OrganizationIntegrationScope & { webhookId: string; limit?: number; }): Promise; export declare function redeliverOrganizationWebhookDelivery(db: Database, input: OrganizationIntegrationScope & { webhookId: string; deliveryId: string; }): Promise; export declare function getWorkspaceCredentialProvider(db: Database, scope: WorkspaceIntegrationScope): Promise; /** Create or replace the workspace's single provider. Omit the secret to keep it. */ export declare function upsertWorkspaceCredentialProvider(db: Database, input: WorkspaceIntegrationScope & { url: string; secretEncrypted?: string; enabled: boolean; timeoutMs: number; createdBySubjectId: string | null; }): Promise; export declare function deleteWorkspaceCredentialProvider(db: Database, scope: WorkspaceIntegrationScope): Promise; export declare function rotateWorkspaceCredentialProviderSecret(db: Database, input: WorkspaceIntegrationScope & { secretEncrypted: string; }): Promise; export declare function rotateWorkspaceWebhookSecret(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; secretEncrypted: string; }): Promise; export declare function listWorkspaceWebhooks(db: Database, scope: WorkspaceIntegrationScope): Promise; export declare function getWorkspaceWebhook(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; }): Promise; export declare function createWorkspaceWebhook(db: Database, input: WorkspaceIntegrationScope & { url: string; secretEncrypted: string; eventTypes: string[]; enabled: boolean; description: string | null; createdBySubjectId: string | null; }): Promise; export declare function updateWorkspaceWebhook(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; url?: string; eventTypes?: string[]; enabled?: boolean; description?: string | null; }): Promise; export declare function deleteWorkspaceWebhook(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; }): Promise; /** * How many webhooks, of the workspace and of its organization, were queued to * receive one session event. Delivery rows are written by the event's own * insert, so this is exact right after the event commits: 0 means no * integration is subscribed to it. */ export declare function countWebhookDeliveriesForEvent(db: Database, input: WorkspaceIntegrationScope & { eventId: string; }): Promise; export declare function listWorkspaceWebhookDeliveries(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; limit?: number; }): Promise; /** Put a settled delivery back on the queue with a fresh attempt budget. */ export declare function redeliverWorkspaceWebhookDelivery(db: Database, input: WorkspaceIntegrationScope & { webhookId: string; deliveryId: string; }): Promise; export type ClaimedWorkspaceWebhookDelivery = { deliveryId: string; accountId: string; workspaceId: string; webhookId: string; eventId: string; eventType: string; payload: unknown; attempts: number; url: string; secretEncrypted: string; }; /** Cross-workspace claim for the single dispatcher loop. */ export declare function claimWorkspaceWebhookDeliveries(db: Database, input: { claimId: string; limit?: number; claimSeconds?: number; }): Promise; export declare function settleWorkspaceWebhookDelivery(db: Database, input: { deliveryId: string; claimId: string; status: number | null; error: string | null; maxAttempts?: number; }): Promise; export declare function pruneWorkspaceWebhookDeliveries(db: Database, input?: { retentionSeconds?: number; limit?: number; }): Promise; export type ClaimedOrganizationWebhookDelivery = ClaimedWorkspaceWebhookDelivery; export declare function claimOrganizationWebhookDeliveries(db: Database, input: { claimId: string; limit?: number; claimSeconds?: number; }): Promise; export declare function settleOrganizationWebhookDelivery(db: Database, input: { deliveryId: string; claimId: string; status: number | null; error: string | null; maxAttempts?: number; }): Promise; export declare function pruneOrganizationWebhookDeliveries(db: Database, input?: { retentionSeconds?: number; limit?: number; }): Promise;