/** * The bearer resolver every provider's chat turn uses on the shared core: * load the leased connection's credential under the exact accepted turn, * refresh it proactively when stale, and share one in-flight refresh per * connection and refresh generation within the process (the database's * per-connection advisory lock serializes other replicas). * * The caller supplies the provider's view of a loaded credential, how a * snapshot is built from it, and the errors it raises; this module decides * when to refresh and which outcomes may be shared between turns. */ import { type SubscriptionCoreAdapter } from "@opengeni/subscriptions"; /** * When a credential is refreshed: this long before expiry, or `fallbackMs` * after the last refresh when the expiry is unknown. `null` for a * credential that never renews (no refresher, or a credential format whose * `autoRenews` is false): it is refreshed only once its known expiry passed * (or on a forced refresh after the provider refused it), and that refresh * marks it needs-relogin. */ export type SubscriptionCoreRefreshPolicy = { windowMs: number; fallbackMs: number; } | null; /** The refresh policy the provider's adapter declares for one decoded credential. */ export declare function subscriptionCoreRefreshPolicy(adapter: Pick, "capabilitiesFor" | "credential" | "refresh">, credential: Credential): SubscriptionCoreRefreshPolicy; /** What every loaded credential view exposes to the resolver. */ export type SubscriptionCoreLoadedCredentialBase = { refreshGeneration: number; planType: string | null; expiresAt: Date | null; lastRefreshAt: Date | null; }; export type SubscriptionCoreRefreshedBase = { kind: "refreshed"; refreshGeneration: number; /** The plan the rotated credential reported; absent when the caller does not track it. */ planType?: string | null; }; /** A refresh outcome whose `refreshed` member carries a provider-specific payload. */ export type SubscriptionCoreRefreshOutcomeOf = Refreshed /** Another refresh or credential writer already advanced the generation. */ | { kind: "superseded"; } | { kind: "relogin"; message: string; marked: boolean; } | { kind: "lease_lost"; } | { kind: "not_visible"; } | { kind: "refused"; } | { kind: "error"; error: unknown; }; export type SubscriptionCoreCredentialLoadOf = { kind: "loaded"; credential: Loaded; } | { kind: "lease_lost"; } | { kind: "not_visible"; } | { kind: "needs_relogin"; } | { kind: "unavailable"; }; export type SubscriptionCoreResolverInput = { /** * Separates flights of different callers (chat turns, connection-level * operations): only flights in the same namespace share an outcome. */ flightNamespace: string; connectionId: string; /** The exact turn lease (turn, holder, generation) this resolver serves. */ holderKey: string; /** * The refresh policy of a loaded credential * (`subscriptionCoreRefreshPolicy(adapter, credential)`): it depends on the * credential's format. */ policy(loaded: Loaded): SubscriptionCoreRefreshPolicy; load(): Promise>; /** The expiry embedded in the credential, used when the store records none. */ embeddedExpiry(loaded: Loaded): Date | null; refresh(loaded: Loaded): Promise>; snapshot(loaded: Loaded): Snapshot; refreshedSnapshot(outcome: Refreshed, loaded: Loaded): Snapshot; /** A persisted refresh reported a plan different from the recorded one. */ onPlanChanged?: (() => void) | undefined; errors: { /** The sign-in was revoked; only a new sign-in recovers. */ relogin(message: string | null): Error; /** The turn no longer holds its lease; dispatch must stop. */ leaseLost(): Error; /** The connection, or the turn's authority over it, is no longer usable. */ accessLost(): Error; }; }; export declare function buildSubscriptionCoreCredentialResolver(input: SubscriptionCoreResolverInput): { getToken: () => Promise; /** Scoped read only: null when normal use would require credential renewal. */ getFreshToken: () => Promise; refresh: () => Promise; };