import type { BillingOrigin } from "@opengeni/contracts"; import { type Database } from "./database.js"; import { type RecordEventDeliveryInput, type RecordEventDeliveryResult } from "./event-triggers.js"; export type McpEventSubscriptionMode = "poll" | "webhook"; /** * Why a subscription polls: the type (or this deployment) offered no webhook * delivery when last checked, or a webhook subscription was tried and failed. */ export type McpEventSubscriptionPollReason = "not_offered" | "callback_failed"; export type McpEventSubscriptionRecord = { id: string; accountId: string; workspaceId: string; sourceId: string; streamKey: string; eventName: string; arguments: Record; mode: McpEventSubscriptionMode | null; /** Null when it does not poll, or when an older release moved it to polling. */ pollReason: McpEventSubscriptionPollReason | null; pollReasonAt: Date | null; cursor: string | null; gapPending: boolean; routeId: string | null; secretEncrypted: string | null; serverSubscriptionId: string | null; refreshBefore: Date | null; nextActionAt: Date; failures: number; failingSince: Date | null; lastError: string | null; lastSuccessAt: Date | null; createdAt: Date; updatedAt: Date; }; /** Identity-only inventory of subscriptions whose next step is due; every read after it is workspace-scoped. */ export declare function listDueMcpEventSubscriptions(db: Database, limit?: number): Promise>; export type ClaimedMcpEventSubscription = { subscription: McpEventSubscriptionRecord; /** False when no runnable automation wants it any more: end it. */ wanted: boolean; }; /** * Lease a due subscription for one pump step. Another pump holding the lease, * or a step that is no longer due, makes this a no-op (null). */ export declare function claimMcpEventSubscription(db: Database, workspaceId: string, subscriptionId: string, leaseMs: number): Promise; export type McpEventSubscriptionStepResult = { /** Undefined keeps the stored value. */ mode?: McpEventSubscriptionMode; /** Why it polls and since when; null clears both. Undefined keeps the stored value. */ pollReason?: { reason: McpEventSubscriptionPollReason; since: Date; } | null; cursor?: string | null; gapPending?: boolean; serverSubscriptionId?: string | null; refreshBefore?: Date | null; nextActionAt: Date; outcome: { kind: "success"; } | { kind: "failure"; error: string; }; }; /** Store a step's result and release the lease. */ export declare function finishMcpEventSubscriptionStep(db: Database, workspaceId: string, subscriptionId: string, result: McpEventSubscriptionStepResult): Promise; /** End a subscription: its row and its webhook route. Later deliveries to the route get 410. */ export declare function deleteMcpEventSubscription(db: Database, workspaceId: string, subscriptionId: string): Promise; /** * Give a subscription its own webhook route and signing secret, once. The * route exists before `events/subscribe` is called, so the server's first * `verification` POST already finds the secret by URL. */ export declare function ensureMcpEventSubscriptionWebhook(db: Database, workspaceId: string, subscriptionId: string, secretEncrypted: string): Promise<{ routeId: string; secretEncrypted: string; } | null>; /** * Leave webhook mode: drop the route and secret so the subscription polls * instead, and record why. Returns the subscription as it now is, with the * cursor any webhook deliveries meanwhile advanced. */ export declare function dropMcpEventSubscriptionWebhook(db: Database, workspaceId: string, subscriptionId: string, pollReason: { reason: McpEventSubscriptionPollReason; since: Date; }): Promise; /** The subscription a webhook route belongs to, including its encrypted secret. */ export declare function getMcpEventSubscriptionByRoute(db: Database, workspaceId: string, routeId: string): Promise; export declare function getMcpEventSubscription(db: Database, workspaceId: string, subscriptionId: string): Promise; /** Store a subscription's server id once its webhook subscribe call returned. */ export declare function setMcpEventSubscriptionServerId(db: Database, workspaceId: string, subscriptionId: string, serverSubscriptionId: string): Promise; export type RecordMcpEventDeliveryInput = { workspaceId: string; subscriptionId: string; delivery: Omit; /** The position after this delivery; undefined keeps the stored cursor. */ cursor?: string | null; /** The server skipped events before this delivery. */ truncated?: boolean; /** * What the automations this delivery fires bill as when an API caller set * the subscription's source up (`event_sources.secret_billing_origin` is * api): the caller passes `fundedBillingOrigin(accountId, "api")`, which is * app for a legacy account. The mark is read in the delivery's transaction. */ apiSourceBillingOrigin?: BillingOrigin; }; export type RecordMcpEventDeliveryResult = RecordEventDeliveryResult & { subscription: McpEventSubscriptionRecord; }; /** * Record a delivery of a subscription's events and advance its cursor in one * transaction, so a crash never stores events without their position or the * reverse. A pending or reported gap marks the first newly stored event. */ export declare function recordMcpEventDelivery(db: Database, input: RecordMcpEventDeliveryInput): Promise; /** Mark that the server skipped events, with the fresh cursor it gave. */ export declare function recordMcpEventGap(db: Database, workspaceId: string, subscriptionId: string, cursor: string | null): Promise; /** Wake a subscription now: the server ended it, or nothing may want it any more. */ export declare function wakeMcpEventSubscription(db: Database, workspaceId: string, subscriptionId: string, options?: { forgetServerSubscription?: boolean; onlyIfUnwanted?: boolean; }): Promise; /** The streams of a source that automations listen on for one event name. */ export declare function listMcpEventStreamKeys(db: Database, workspaceId: string, sourceId: string, eventName: string): Promise; /** The subscriptions of one source, for its owner's view of what it listens to. */ export declare function listMcpEventSubscriptionsForSource(db: Database, workspaceId: string, sourceId: string): Promise;