import type { Settings } from "@opengeni/config"; import { type SubscriptionCoreConnectionBearer, type SubscriptionCoreConnectionRead, type SubscriptionQuota } from "@opengeni/subscriptions"; import { type Database } from "../database.js"; import { type SubscriptionCoreConnectionCredentialLoad, type SubscriptionCoreConnectionRefreshDeps, type SubscriptionCoreConnectionRefreshOutcome, type SubscriptionCoreFetch, type SubscriptionCoreOperationLeaseRef, type SubscriptionCoreOperationScope, type SubscriptionCoreUsageNotApplied } from "./operations.js"; import { type SubscriptionCoreProvider } from "./provider.js"; import { type SubscriptionOperationKind } from "../subscription-core-repository.js"; export type SubscriptionCoreOperationCandidate = { connectionId: string; planType: string | null; /** The session's explicit choice (session scope only). */ explicit: boolean; /** The connection's model allowlist (null: every model). */ allowedModelIds: string[] | null; }; /** One connection's bearer for an operation, with the generation that minted it. */ export type SubscriptionCoreConnectionToken = SubscriptionCoreConnectionBearer & { planType: string | null; /** The refresh generation of this bearer (fences quota observations). */ credentialVersion: number; }; export type SubscriptionCoreConnectionResolverDeps = SubscriptionCoreConnectionRefreshDeps & { /** Test seams for the credential read and the refresh under the core lock. */ load?: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null) => Promise; refreshCredential?: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, observedRefreshGeneration: number, deps: SubscriptionCoreConnectionRefreshDeps) => Promise; /** The caller's relogin error (default: a core error with the adapter's text). */ relogin?: (message: string | null) => Error; /** The caller's lost-operation error (default: the binding's `operationUnavailable`). */ unavailable?: () => Error; }; /** One adapter call made with a connection's read (`runSubscriptionCoreConnectionRead`). */ export type SubscriptionCoreConnectionReadRun = /** Not readable in this context. */ { kind: "not_visible"; } | { kind: "relogin"; } | { kind: "token_error"; error: unknown; } /** The call threw. */ | { kind: "fetch_error"; error: unknown; } | { kind: "read"; value: T; }; /** What a probe reports; provider and refresh failures never throw. */ export type SubscriptionCoreUsageProbe = /** The adapter has no usage probe. */ { kind: "unsupported"; } /** Not readable in this workspace context (for example a personal connection). */ | { kind: "not_visible"; } | { kind: "relogin"; } | { kind: "token_error"; error: unknown; } | { kind: "fetch_error"; error: unknown; } | { kind: "read"; /** The adapter's decoded-or-raw usage response. */ response: unknown; /** The recorded observation, or null when the response carried none. */ quota: SubscriptionQuota | null; /** * The observation was written to the connection's quota (false when the * response carried none, the refresh-generation fence refused it, or * the write failed). */ applied: boolean; /** Why it was not written (absent when it was). */ notApplied?: SubscriptionCoreUsageNotApplied; /** The error of a failed write (`notApplied: "write_failed"`). */ writeError?: unknown; /** The observation ended a stored exhaustion (the caller wakes waiters). */ recovered: boolean; }; export type SubscriptionCoreLiveModelsProbe = { kind: "unsupported"; } | { kind: "not_visible"; } | { kind: "relogin"; } | { kind: "error"; error: unknown; } | { kind: "read"; modelIds: readonly string[]; }; export type SubscriptionCoreConnectionProbeDeps = { /** The physical transport under request custody (default: global fetch). */ fetchImpl?: SubscriptionCoreFetch; /** A caller-built custody fetch (replaces the core's for this read). */ requestFetch?: SubscriptionCoreFetch; resolver?: SubscriptionCoreConnectionResolverDeps; /** * The product models the connection can serve (the caller's catalog when * the usage is read), passed to the adapter's `decodeQuota` so a limit of * a model group reaches each listed model. Absent: no catalog. */ modelIds?: readonly string[]; }; /** The connection-level operations runtime bound to one provider (memoized per binding). */ export declare const subscriptionCoreOperationConnections: (provider: SubscriptionCoreProvider) => { listSubscriptionCoreOperationCandidates: (db: Database, scope: Exclude) => Promise; readSubscriptionCoreExhaustedConnectionIds: (db: Database, scope: Exclude, connectionIds: readonly string[], now?: number) => Promise>; readSubscriptionCoreWorkspaceConnections: (db: Database, scope: Extract) => Promise<{ connections: Array<{ connectionId: string; label: string | null; providerAccountId: string | null; source: "workspace" | "organization"; status: string; }>; primaryConnectionId: string | null; }>; buildSubscriptionCoreConnectionTokenResolver: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, deps?: SubscriptionCoreConnectionResolverDeps) => { getToken: () => Promise; getFreshToken: () => Promise; refresh: () => Promise; }; runSubscriptionCoreOperation: (db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, input: { candidates: readonly string[]; operationKind: SubscriptionOperationKind; holderId: string; /** Defaults to a fresh operation id; the lease row is keyed by it. */ operationId?: string; attemptId?: string; generation?: number; /** A durable logical request identity for custody (default: random). */ requestId?: string; fetchImpl?: SubscriptionCoreFetch; }, run: (operation: { connectionId: string; ref: SubscriptionCoreOperationLeaseRef; resolver: ReturnType<(db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, deps?: SubscriptionCoreConnectionResolverDeps) => { getToken: () => Promise; getFreshToken: () => Promise; refresh: () => Promise; }>; fetch: SubscriptionCoreFetch; /** True while the exact operation lease is still live (renews it). */ fence: () => Promise; }) => Promise) => Promise<{ kind: "ran"; value: T; } | { kind: "unavailable"; }>; probeSubscriptionCoreConnectionUsage: (db: Database, settings: Settings, scope: Extract, connectionId: string, deps?: SubscriptionCoreConnectionProbeDeps) => Promise; probeSubscriptionCoreConnectionLiveModels: (db: Database, settings: Settings, scope: Extract, connectionId: string, deps?: SubscriptionCoreConnectionProbeDeps) => Promise; runSubscriptionCoreConnectionRead: (db: Database, settings: Settings, scope: Extract, connectionId: string, run: (read: SubscriptionCoreConnectionRead) => Promise, deps?: SubscriptionCoreConnectionProbeDeps) => Promise>; refreshExhaustedSubscriptionCoreQuota: (db: Database, settings: Settings, scope: Extract, deps?: SubscriptionCoreConnectionProbeDeps & { minIntervalMs?: number; now?: number; }) => Promise; };