import { type Settings } from "@opengeni/config"; import { type StoredQuotaDocument, type SubscriptionQuota, type SubscriptionQuotaBlock } from "@opengeni/subscriptions"; import { type Database } from "../database.js"; import { type SubscriptionCoreTurnIdentity } from "./turns.js"; import { subscriptionCoreRequests } from "./requests.js"; import { type SubscriptionCoreCredentialRefreshWriter, type SubscriptionCoreCredentialRotationOutcome } from "./credential-rotation.js"; import { type SubscriptionCoreProvider } from "./provider.js"; import { type SubscriptionOperationKind } from "../subscription-core-repository.js"; /** A provider HTTP call (the global `fetch` shape the provider clients accept). */ export type SubscriptionCoreFetch = (input: string | URL, init?: RequestInit) => Promise; type SubscriptionCoreRequests = ReturnType; /** Default lease for one operation; renewed before each provider dispatch. */ export declare const SUBSCRIPTION_CORE_OPERATION_LEASE_TTL_MS: number; export type SubscriptionCoreOperationScope = { kind: "turn"; identity: SubscriptionCoreTurnIdentity; } | { kind: "session"; accountId: string; workspaceId: string; sessionId: string; /** The session's recorded owner; null for an ownerless session. */ sessionOwnerSubjectId: string | null; } | { kind: "workspace"; accountId: string; workspaceId: string; subjectId: string; }; /** The exact operation lease that authorizes credential reads and refreshes. */ export type SubscriptionCoreOperationLeaseRef = { operationId: string; attemptId: string; operationKind: SubscriptionOperationKind; connectionId: string; holderId: string; generation: number; }; export type SubscriptionCoreOperationLeaseResult = { kind: "acquired"; leasedUntil: Date; } /** Another live holder or generation owns this operation id. */ | { kind: "busy"; } /** The operation's scope is no longer visible (for example, the turn is no longer accepted). */ | { kind: "not_visible"; } /** The database refused the connection for this operation's authority. */ | { kind: "refused"; }; export type SubscriptionCoreConnectionCredential = { connectionId: string; refreshGeneration: number; /** The decoded secret (the adapter's credential codec). */ credential: Credential; providerAccountId: string | null; /** Provider-owned connection facts; only the provider's adapter interprets them. */ providerState: Record; planType: string | null; expiresAt: Date | null; lastRefreshAt: Date | null; }; export type SubscriptionCoreConnectionCredentialLoad = { kind: "loaded"; credential: SubscriptionCoreConnectionCredential; } /** No live operation lease, or the connection is out of scope. */ | { kind: "not_visible"; } | { kind: "needs_relogin"; } | { kind: "unavailable"; }; export type SubscriptionCoreConnectionRefreshOutcome = { kind: "refreshed"; credential: unknown; refreshGeneration: number; /** The plan the rotated credential reported, already recorded. */ planType?: string | null; } | Exclude; export type { SubscriptionCoreCredentialRefreshWriter }; export type SubscriptionCoreConnectionRefreshDeps = { now?: () => Date; }; /** A connection's credential columns, as a credential read returns them. */ export type SubscriptionCoreConnectionCredentialRow = { status: string; refresh_generation: number | string; credential_encrypted: string | null; expires_at: Date | string | null; last_refresh_at: Date | string | null; provider_account_id: string | null; plan_type: string | null; provider_state: Record | null; }; /** A stored quota row's columns (`subscription_connection_quota`). */ export type SubscriptionCoreStoredQuotaRow = { quota: unknown; quota_revision: number | string | null; quota_observed_refresh_generation: number | string | null; quota_updated_at: Date | string | null; }; /** How one usage reading may change a stored quota beyond its windows. */ export type SubscriptionCoreUsageMergeOptions = { /** * The adapter's `usageReadConfirmsAvailability` for the response that was * decoded (undefined: the adapter has none). False: the reading never ends * a stored quota exhaustion. */ confirmsAvailability?: boolean | undefined; /** * The adapter's `usageReadEndsClaimedBlock` for the response that was * decoded (undefined: no cooldown ends early, and an adapter that defines * the hook ends no quota exhaustion deadline either). Applied only with a * reading that may end a stored quota exhaustion, to each stored block * with its claim as the store holds it now (under the row lock). */ endsClaimedBlock?: ((block: SubscriptionQuotaBlock) => boolean) | undefined; }; /** The merge options one decoded `fetchUsage` response carries, from the adapter. */ export declare function subscriptionCoreUsageMergeOptions(adapter: SubscriptionCoreProvider["adapter"], response: unknown): SubscriptionCoreUsageMergeOptions; /** * A stored quota a usage re-read can end now: it is exhausted, or (for an * adapter whose reading can end claimed blocks) a model cooldown runs whose * claim still explains it. A cooldown without a claim runs to its own end, * so it never warrants a read. */ export declare function subscriptionCoreQuotaBlocksNow(adapter: SubscriptionCoreProvider["adapter"], quota: SubscriptionQuota | null, now: number): boolean; /** The quota write one usage observation makes. */ export type SubscriptionCoreUsageObservationWrite = { /** The quota document to store. */ stored: StoredQuotaDocument; /** When it was observed (the row's `updated_at`). */ observedAt: number; /** It ends a stored exhaustion (the caller wakes waiters after commit). */ recovered: boolean; }; /** * Why a live usage reading changed nothing on the stored quota: * * - `no_reading`: the response carried no quota; * - `not_visible`: the connection is no longer readable in the scope; * - `credential_changed`: the credential was refreshed or replaced while the * reading was in flight (the refresh-generation fence); * - `superseded`: the stored quota already holds a newer observation, so the * reading succeeded and what is stored is at least as current; * - `contended`: other quota writes kept moving the row under the reading; * - `write_failed`: the write threw. */ export type SubscriptionCoreUsageNotApplied = "no_reading" | "not_visible" | "credential_changed" | "superseded" | "contended" | "write_failed"; /** The outcome of recording one usage reading; `notApplied` says why it was not. */ export type SubscriptionCoreUsageRecord = { applied: boolean; recovered: boolean; notApplied?: SubscriptionCoreUsageNotApplied; }; /** A reading that changed nothing, and why. */ export declare function subscriptionCoreUsageNotRecorded(notApplied: SubscriptionCoreUsageNotApplied): SubscriptionCoreUsageRecord; /** * Why `mergeSubscriptionCoreUsageObservation` returned no write: the reading * was made with another credential generation, or the stored quota already * holds a newer observation. */ export declare function subscriptionCoreUsageMergeSkipped(refreshGeneration: number, observation: Pick): SubscriptionCoreUsageRecord; export declare const SUBSCRIPTION_MODEL_CATALOG_TTL_MS = 60000; /** * The session's recorded owner for a session-bound operation (realtime): * never the viewer or creator. `undefined` when the session is not visible. */ export declare function readSubscriptionCoreSessionOwner(db: Database, input: { accountId: string; workspaceId: string; sessionId: string; }): Promise<{ ownerSubjectId: string | null; } | undefined>; /** The operations runtime bound to one provider (memoized per binding). */ export declare const subscriptionCoreOperations: (provider: SubscriptionCoreProvider) => { withOperationScope: (db: Database, scope: SubscriptionCoreOperationScope, operation: (tx: Database) => Promise) => Promise<{ value: T; } | null>; buildSubscriptionCoreOperationFetch: (db: Database, scope: SubscriptionCoreOperationScope, ref: SubscriptionCoreOperationLeaseRef | null, connectionId: string, fetchImpl?: SubscriptionCoreFetch, deps?: { /** Reuse a durable logical identity when the caller has one (transcription). */ requestId?: string; reserve?: SubscriptionCoreRequests["reserveSubscriptionCoreOperationRequest"]; settle?: SubscriptionCoreRequests["settleSubscriptionCoreOperationRequest"]; }) => SubscriptionCoreFetch; acquireSubscriptionCoreOperationLease: (db: Database, scope: SubscriptionCoreOperationScope, ref: SubscriptionCoreOperationLeaseRef, ttlMs?: number) => Promise; renewSubscriptionCoreOperationLease: (db: Database, scope: SubscriptionCoreOperationScope, ref: SubscriptionCoreOperationLeaseRef, ttlMs?: number) => Promise; releaseSubscriptionCoreOperationLease: (db: Database, scope: SubscriptionCoreOperationScope, ref: SubscriptionCoreOperationLeaseRef) => Promise; loadSubscriptionCoreConnectionCredential: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null) => Promise; refreshSubscriptionCoreConnectionCredential: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, observedRefreshGeneration: number, deps?: SubscriptionCoreConnectionRefreshDeps) => Promise; subscriptionCoreConnectionCredentialLoad: (settings: Settings, connectionId: string, row: SubscriptionCoreConnectionCredentialRow | null) => SubscriptionCoreConnectionCredentialLoad; rotateSubscriptionCoreConnectionCredential: (tx: Database, settings: Settings, writer: SubscriptionCoreCredentialRefreshWriter, observedRefreshGeneration: number, deps?: SubscriptionCoreConnectionRefreshDeps) => Promise; persistSubscriptionCoreConnectionRefresh: (tx: Database, tenant: { accountId: string; workspaceId: string; }, connectionId: string, input: Parameters[1]) => Promise; mergeSubscriptionCoreUsageObservation: (row: SubscriptionCoreStoredQuotaRow | null, refreshGeneration: number, observation: SubscriptionQuota, options?: SubscriptionCoreUsageMergeOptions) => SubscriptionCoreUsageObservationWrite | null; recordSubscriptionCoreModelCatalog: (db: Database, scope: Extract, connectionId: string, observation: { slugs: readonly string[]; refreshGeneration: number; observedAt: number; }) => Promise; recordSubscriptionCoreUsageObservation: (db: Database, scope: SubscriptionCoreOperationScope, connectionId: string, observation: SubscriptionQuota, options?: SubscriptionCoreUsageMergeOptions, ref?: SubscriptionCoreOperationLeaseRef | null) => Promise; readSubscriptionCoreOperationQuota: (db: Database, scope: SubscriptionCoreOperationScope, ref: SubscriptionCoreOperationLeaseRef) => Promise<{ quota: SubscriptionQuota | null; } | null>; resolveSubscriptionCoreConnectionId: (db: Database, input: { accountId: string; workspaceId: string; connectionId: string; }) => Promise; };