import { SessionModelAccountServing, type SessionEvent, type SessionEventType } from "@opengeni/contracts"; import { type Database } from "../database.js"; import { type SubscriptionCoreProvider } from "./provider.js"; import { type SubscriptionCoreAccountUsage } from "./account-usage-presentation.js"; import { type SubscriptionCoreWake } from "./administration.js"; /** The neutral session event a choice change records. */ export declare const SUBSCRIPTION_CORE_ACCOUNT_CHOICE_EVENT = "model_account.selection.changed"; /** Why the picker cannot offer a listed connection for this session. */ export type SubscriptionCoreAccountChoiceRefusal = /** The connection cannot serve (reconnect needed, revoked, disabled). */ "inactive" /** Personal connections are not allowed in this workspace. */ | "personal_not_allowed" /** The caller cannot authorize this personal connection for this session. */ | "personal_not_owner_work" /** * The session has no owner (for example a run of a schedule created with an * API key): the binding guard admits no explicit choice for it. */ | "session_has_no_owner"; export type SubscriptionCoreChoosableConnection = { provider: string; connectionId: string; label: string | null; accountEmail: string | null; planType: string | null; status: string; /** The caller's own personal connection (never another person's). */ personal: boolean; /** For a shared connection, the workspace pool it belongs to. */ source: "workspace" | "organization" | "personal"; allowedModelIds: string[] | null; choosable: boolean; refusal: SubscriptionCoreAccountChoiceRefusal | null; /** Cached observation for this already-visible account; never an eligibility decision. */ usage: SubscriptionCoreAccountUsage | null; }; export type SubscriptionCoreSessionAccountChoice = { serving: SessionModelAccountServing | null; /** * The session's binding when it has exactly one: `explicit` after a * person's choice, else the account automatic placement last used. Null * when it has none, or bindings for several providers (see `bindings`). * The connection is withheld (null) unless the caller may see it in the * list. */ binding: SubscriptionCoreSessionChoiceBinding | null; /** * Every binding of the session, one per provider (D-32), ordered by * provider, with the same connection withholding. */ bindings: SubscriptionCoreSessionChoiceBinding[]; connections: SubscriptionCoreChoosableConnection[]; personal: { /** Effective `personalConnectionsAllowed` for this workspace. */ connectionsAllowed: boolean; /** Effective `personalFallbackAllowed` for this workspace. */ fallbackAllowed: boolean; /** The caller's own opt-in (fallback needs it and the setting). */ fallbackOptIn: boolean; }; }; export type SubscriptionCoreSessionChoiceBinding = { provider: string; connectionId: string | null; choice: "explicit" | "automatic"; }; /** * The provider bindings a choice ranges over: every registered provider, or * the bindings a caller passes (a test registration of its own provider). */ export type SubscriptionCoreChoiceProviders = readonly SubscriptionCoreProvider[]; /** * What the model picker may offer for this session, for the calling * subject. Null when the session is not visible to the caller. */ export declare function readSubscriptionCoreSessionAccountChoice(db: Database, input: { accountId: string; workspaceId: string; sessionId: string; subjectId: string; }, providers?: SubscriptionCoreChoiceProviders): Promise; /** A wake for one provider's waiters of the chosen session, delivered after commit. */ export type SubscriptionCoreProviderWake = SubscriptionCoreWake & { provider: string; }; export type SubscriptionCoreSessionChoiceOutcome = /** The binding changed; `events` holds the recorded event. */ "changed" /** The session already had exactly this choice. */ | "unchanged" /** No such session, or no connection of that id this caller may name. */ | "not_found" /** The database refused the connection for this session (binding guard). */ | "refused" /** * The choice could not be applied for now: the transaction kept losing a * deadlock to a concurrent placement after its bounded replay (the binding * is locked before its version is compared, so a lost compare-and-swap is * defensive only). Nothing changed; the client may retry. */ | "conflict"; export type SubscriptionCoreSessionChoiceResult = { outcome: SubscriptionCoreSessionChoiceOutcome; appliedTo: "waiting_turn" | "next_turn"; provider: string | null; connectionId: string | null; events: SessionEvent[]; wakes: SubscriptionCoreProviderWake[]; }; /** Presentation-only compatibility for an existing provider-qualified endpoint. * It shares the same choice, authorization, locking and event transaction. * The mapper cannot change the chosen account or accepted turn authority. */ export type SubscriptionCoreChoiceReceipt = { recordUnchanged: boolean; project: (choice: { provider: string | null; providers: string[]; connectionId: string | null; choice: "explicit" | "automatic"; appliedTo: "waiting_turn" | "next_turn"; turnId: string | null; subjectId: string; }) => { type: SessionEventType; payload: Record; }; }; /** * Make the session's account choice explicit on `connectionId` (any * registered provider's connection the caller may name: a shared connection * of the workspace's pool or the caller's own personal connection), or * return it to automatic placement (`null`). The binding guard enforces * eligibility; a running attempt keeps its account and a waiting turn * re-places at its next check, which the returned wakes trigger. A choice * of an account that cannot serve the session's model is recorded and waits * as `pinned_account_ineligible` (D-24). */ export declare function chooseSubscriptionCoreSessionAccount(db: Database, input: { accountId: string; workspaceId: string; sessionId: string; connectionId: string | null; subjectId: string; /** * With `connectionId: null`, return only this provider's choice to * automatic; without it, every provider's. With a connection, it must * be that connection's provider. */ provider?: string | null; }, providers?: SubscriptionCoreChoiceProviders, receipt?: SubscriptionCoreChoiceReceipt): Promise; export type SubscriptionPersonPreferences = { personalFallbackOptIn: boolean; /** 0 before the person ever saved a preference. */ version: number; }; /** * The caller's own preferences in this organization. Null for anyone who is * not an active human member (agents and service actors have none). */ export declare function readSubscriptionPersonPreferences(db: Database, input: { accountId: string; subjectId: string; }): Promise; /** * Save the caller's own personal-fallback opt-in. `expectedVersion` * (optional) is the version last read: 0 for no saved preference. A stale * version is a `conflict` and changes nothing. * * @deprecated Kept for API compatibility. Placement no longer reads it: a * person turns their own subscription on per workspace instead (D-53). */ export declare function writeSubscriptionPersonPreferences(db: Database, input: { accountId: string; subjectId: string; personalFallbackOptIn: boolean; expectedVersion?: number; }, _providers?: SubscriptionCoreChoiceProviders): Promise<{ outcome: "written"; preferences: SubscriptionPersonPreferences; /** Always empty: the opt-in no longer changes placement (D-53). */ wakes: SubscriptionCoreProviderWake[]; } | { outcome: "conflict"; preferences: SubscriptionPersonPreferences; } | { outcome: "not_member"; }>;