import { type Database } from "../database.js"; import { readSubscriptionEffectiveSettings } from "../subscription-core-repository.js"; import { type SubscriptionCoreProvider } from "./provider.js"; import { type PersonalWorkspaceOwnerSettingsInput } from "../personal-workspace-owner-settings.js"; /** A wake the caller delivers after its mutation committed. */ export type SubscriptionCoreWake = { accountId: string; reason: string; workspaceIds?: readonly string[]; /** Only these sessions' waiters (requires exactly one workspace). */ sessionIds?: readonly string[]; /** Only waiters of sessions owned by this organization membership. */ ownerMembershipId?: string; }; /** The wake reason for an administration event of this provider. */ export declare function subscriptionCoreWakeReason(provider: SubscriptionCoreProvider, event: string): string; /** * One subscription connection as the administration projections read it. * `status` and `last_error` are effective: a quarantine that ended reads * `active` with no error (`subscriptionConnectionEffectiveStatus`). */ export type SubscriptionCoreConnectionRow = { id: string; label: string | null; account_email: string | null; plan_type: string | null; provider_account_id: string | null; status: string; last_error: string | null; extra_credits_enabled: boolean; extra_credits_version: number | string; extra_credits_updated_at: Date | string | null; allocator_enabled: boolean; allocator_version: number | string; allowed_model_ids: string[] | null; connected_by_subject_id: string | null; expires_at: Date | string | null; last_refresh_at: Date | string | null; /** When it was last signed in (1503); null when unknown, and on personal rows. */ signed_in_at?: Date | string | null; managed_by_workspace_id: string | null; provider_state: Record | null; updated_at: Date | string; quota: unknown; quota_revision: number | string | null; quota_observed_refresh_generation: number | string | null; quota_updated_at: Date | string | null; /** * The row version (moves on every connection change except a credential * refresh). Shared rows carry it; the owner reader of personal rows does * not (`readSubscriptionCorePersonalConnectionVersionsInTransaction`). */ version?: number | string; }; /** The provider's effective settings at one workspace. */ export type SubscriptionCoreProviderSettings = { enabled: boolean; rotationMode: "spread" | "primary_first"; rotationSource: "organization" | "workspace"; }; export declare function subscriptionCoreProviderSettings(provider: SubscriptionCoreProvider, effective: Awaited>): SubscriptionCoreProviderSettings; export declare function readSubscriptionCorePrimaryConnectionId(tx: Database, provider: SubscriptionCoreProvider, accountId: string, workspaceId: string | null): Promise; /** * Whether the provider is on in this workspace. `automatic` is on; the older * `workspace` and `organization` values are no longer stored (D-50). */ export type SubscriptionCoreWorkspaceSourceMode = "automatic" | "disabled"; /** * `disabled` when the provider is off here; otherwise `workspace` when the * workspace has its own connection, else `organization`. Every connection * that reaches the workspace serves either way (D-50). */ export type SubscriptionCoreEffectiveSource = "workspace" | "organization" | "disabled"; export type SubscriptionCoreWorkspaceSource = { accountId: string; workspaceId: string; workspaceKind: "personal" | "shared"; mode: SubscriptionCoreWorkspaceSourceMode; effectiveSource: SubscriptionCoreEffectiveSource; /** The workspace has a connection of its own. */ workspaceAvailable: boolean; /** An organization connection reaches the workspace. */ organizationAvailable: boolean; /** Always false: no connection is set aside (D-50). Kept for older clients. */ workspaceSetAside: false; /** * How many organization connections reach this workspace. Connections * limited to chosen people are not counted: they reach people, not * workspaces. */ organizationCount: number; }; /** One shared connection that reaches a workspace. */ export type SubscriptionCorePoolConnection = { row: SubscriptionCoreConnectionRow; /** `workspace` for the workspace's own connection, else `organization`. */ source: "workspace" | "organization"; }; export type SubscriptionCoreWorkspacePool = { connections: SubscriptionCorePoolConnection[]; primaryConnectionId: string | null; rotationMode: "spread" | "primary_first"; source: SubscriptionCoreWorkspaceSource; }; /** * The shared connections of this provider that reach the workspace, the * primary connection and whether the provider is on here. Requires the * workspace RLS context on `tx`. Only shared subscription connections in the * workspace's scope are listed; an administrator's wider visibility is * filtered explicitly. Nothing is listed while the provider is off here. */ export declare function readSubscriptionCoreWorkspacePool(tx: Database, provider: SubscriptionCoreProvider, input: { accountId: string; workspaceId: string; /** Session picker only: include shared accounts assigned to the scoped caller. */ includeOwnPeopleScope?: boolean; /** Limits overview only: list the connections even while the provider is off here. */ includeAllAvailableConnections?: boolean; }): Promise; /** * The viewing person's own personal connections of this provider (no * credential material), through the owner-only reader. Requires the * workspace RLS context on `tx`; sets the subject. Empty for anyone else or * for a workspace the person may not use. */ export declare function listSubscriptionCorePersonalConnectionRowsInTransaction(tx: Database, provider: SubscriptionCoreProvider, input: { accountId: string; workspaceId: string; subjectId: string; }): Promise; /** * The row versions of the viewing person's own personal connections of this * provider, through the owner-only reader (0730), keyed by connection id. * Same context and visibility as * `listSubscriptionCorePersonalConnectionRowsInTransaction`. */ export declare function readSubscriptionCorePersonalConnectionVersionsInTransaction(tx: Database, provider: SubscriptionCoreProvider, input: { accountId: string; workspaceId: string; subjectId: string; }): Promise>; /** * The organization's accounts of this provider (every shared connection the * organization administers, including ones a shared workspace manages, * design 5.4) and its rotation, for an organization administrator. * `null` for anyone else: the connection policy hides every row. */ export declare function readSubscriptionCoreOrganizationPool(db: Database, provider: SubscriptionCoreProvider, input: { organizationId: string; subjectId: string; /** * Read only this connection, any organization account (also one a shared * workspace manages), for a route that just changed it. */ connectionId?: string; }): Promise<{ rows: SubscriptionCoreConnectionRow[]; /** * Per connection, the shared workspaces whose own accounts include it: its * workspace pool lists it there (scope reaches the workspace) classified as * the workspace's own, as `readSubscriptionCoreWorkspacePool` classifies it. */ ownInWorkspaceIds: Map; /** Per connection, who it reaches, so a list can say so without reading each one's access. */ reach: Map; primaryConnectionId: string | null; rotationMode: "spread" | "primary_first"; } | null>; /** * Who an organization-listed connection reaches: its owning workspace (a * workspace's own account, used only there), every workspace including new * ones, chosen shared workspaces, Personal workspaces, or chosen people. */ export type SubscriptionCoreConnectionReach = { /** The workspace whose own account this is, or null for an organization account. */ ownerWorkspaceId: string | null; /** Every shared workspace, including ones created later. */ everyWorkspace: boolean; /** The shared workspaces it reaches, when not every one. */ workspaceIds: string[]; /** Every Personal workspace. */ personalWorkspaces: boolean; /** How many chosen people, or null when it is not limited to people. */ people: number | null; }; /** A workspace's pool as one person sees it: the shared pool plus, in their own Personal workspace, their personal connections. */ export type SubscriptionCoreWorkspaceView = { pool: SubscriptionCoreWorkspacePool; /** The viewer's own personal connections (Personal workspace only), through the owner-only reader. */ personal: SubscriptionCoreConnectionRow[]; /** * The primary connection as listed: when personal rows joined, a primary * that is neither a listed shared nor a listed personal connection is null * (personal rows are visible only through the owner reader). */ primaryConnectionId: string | null; }; /** * The workspace's pool of this provider for one viewer, under the workspace * RLS context. In the viewer's own Personal workspace their personal * connections are listed too; nobody else ever sees them. */ export declare function readSubscriptionCoreWorkspaceView(db: Database, provider: SubscriptionCoreProvider, input: { accountId: string; workspaceId: string; viewerSubjectId?: string | null; /** Limits overview only; availability and canonical row visibility still apply. */ includeAllAvailableConnections?: boolean; }): Promise; /** * A row-level security refusal (42501). The settings lock guard (0724) also * raises 42501; it is not a refusal to see the row, so it is excluded here and * surfaces as `SubscriptionSettingsLockedError`. */ export declare function isSubscriptionCoreRlsRefusal(error: unknown): boolean; export type SubscriptionCoreAdministration = { accountId: string; /** null: the organization route. */ workspaceId: string | null; subjectId: string; } & PersonalWorkspaceOwnerSettingsInput; export type SubscriptionCoreAllocatorResult = { kind: "updated" | "unchanged" | "conflict"; allocatorEnabled: boolean; allocatorVersion: number; allocatorUpdatedAt: Date | null; } | { kind: "not_found"; }; export type SubscriptionCoreExtraCreditsResult = { kind: "updated" | "unchanged" | "conflict"; extraCreditsEnabled: boolean; extraCreditsVersion: number; extraCreditsUpdatedAt: Date | null; } | { kind: "not_found"; }; /** * New-allocation eligibility of one connection, with the legacy optimistic * concurrency: the same state is idempotent even with a stale version; a * conflicting stale version returns the current one. A connection has one * switch (D-50): organization administrators may toggle any shared * connection, a workspace's administrators their workspace's own (the * connection write policy decides). */ export declare function setSubscriptionCoreAllocator(db: Database, provider: SubscriptionCoreProvider, input: SubscriptionCoreAdministration & { connectionId: string; enabled: boolean; expectedVersion: number; }): Promise<{ result: SubscriptionCoreAllocatorResult; wake: SubscriptionCoreWake | null; }>; /** * Consent to paid usage beyond the plan for one connection (providers with * the `extraCredits` capability), with the same optimistic concurrency as * the allocator switch. A provider without the capability finds nothing. */ export declare function setSubscriptionCoreExtraCredits(db: Database, provider: SubscriptionCoreProvider, input: SubscriptionCoreAdministration & { connectionId: string; enabled: boolean; expectedVersion: number; }): Promise<{ result: SubscriptionCoreExtraCreditsResult; wake: SubscriptionCoreWake | null; }>; /** Rename one connection (label only). Returns null when it is not manageable. */ export declare function renameSubscriptionCoreConnection(db: Database, provider: SubscriptionCoreProvider, input: SubscriptionCoreAdministration & { connectionId: string; label: string | null; }): Promise; /** * The account unpinned sessions prefer: the primary connection of this * workspace (or the organization), keeping the rotation mode currently in * effect there. */ export declare function setSubscriptionCorePrimary(db: Database, provider: SubscriptionCoreProvider, input: SubscriptionCoreAdministration & { connectionId: string; }): Promise<{ activated: string | null; wake: SubscriptionCoreWake | null; }>; /** * Rotation switch: on is `spread`, off is `primary_first` (D-13). A * workspace override carries the effective (inherited) primary, so toggling * rotation never drops the account unpinned sessions prefer. Returns null * when the subject may not write the row. */ export declare function setSubscriptionCoreRotation(db: Database, provider: SubscriptionCoreProvider, input: SubscriptionCoreAdministration & { rotationEnabled: boolean; }): Promise<{ primaryConnectionId: string | null; wake: SubscriptionCoreWake; } | null>; /** * Whether the provider is on in a workspace, as the workspace's provider * override: `automatic` removes it, `disabled` sets `enabled = false`. Connection scope and the model allowlist are never * touched (design 5.2). Throws the provider's source-refused error. */ export declare function setSubscriptionCoreWorkspaceSource(db: Database, provider: SubscriptionCoreProvider, input: { accountId: string; workspaceId: string; subjectId: string; mode: SubscriptionCoreWorkspaceSourceMode; } & PersonalWorkspaceOwnerSettingsInput): Promise<{ pool: SubscriptionCoreWorkspacePool; wake: SubscriptionCoreWake; }>;