import z from "@monotykamary/schemastery"; import { Context } from "@monotykamary/cordis"; import { IncomingMessage } from "node:http"; //#region src/types.d.ts /** Public contracts shared by multiprovider and concrete provider plugins. */ type AuthKind = 'api-key' | 'oauth' | 'service-account' | 'custom'; type SelectionPolicy = 'round-robin' | 'weighted-round-robin' | 'least-inflight' | 'priority'; type FailureKind = 'rate-limit' | 'quota' | 'auth' | 'transient' | 'fatal'; /** Non-secret identity and routing information for one provider account. */ interface ProviderAccount { id: string; label: string; authKind: AuthKind; /** Opaque provider-owned reference. It is returned only to host consumers, never the browser. */ credentialRef: TCredentialRef; enabled?: boolean; weight?: number; priority?: number; /** Non-secret display metadata. Values are exposed in Settings. */ metadata?: Readonly>; } interface FailureDisposition { kind: FailureKind; retryable: boolean; /** Explicit cooldown. Omit to use the scheduler default for the failure kind. */ cooldownMs?: number; } interface ProviderRegistration { id: string; label: string; /** Return a fresh account inventory. Provider plugins retain credential ownership. */ accounts: () => readonly ProviderAccount[] | Promise[]>; /** Translate transport/provider errors into portable scheduling outcomes. */ classifyFailure?: (error: unknown, account: ProviderAccount) => FailureDisposition; /** Optional hint shown in Settings about where accounts are managed. */ managementHint?: string; } interface AccountPreference { accountId: string; enabled: boolean; weight: number; priority: number; } interface PoolPreference { providerId: string; policy: SelectionPolicy; affinity: boolean; accounts: AccountPreference[]; } interface MultiProviderPreferences { pools: PoolPreference[]; } interface AcquireOptions { providerId: string; /** Stable session/workload identity. Used only when affinity is enabled. */ affinityKey?: string; /** Accounts already attempted by the caller during this logical request. */ excludeAccountIds?: Iterable; } interface LeaseOutcomeSuccess { status: 'success'; } interface LeaseOutcomeFailure { status: 'failure'; error: unknown; } interface LeaseOutcomeCancelled { status: 'cancelled'; } type LeaseOutcome = LeaseOutcomeSuccess | LeaseOutcomeFailure | LeaseOutcomeCancelled; interface AccountLease { readonly id: string; readonly providerId: string; readonly accountId: string; readonly account: ProviderAccount; readonly credentialRef: TCredentialRef; readonly acquiredAt: number; /** Idempotently release capacity and update health from the outcome. Failure settlement returns the provider classification. */ release(outcome?: LeaseOutcome): FailureDisposition | undefined; } type PublicAccountStatus = 'ready' | 'cooldown' | 'disabled'; interface PublicAccountSnapshot { id: string; label: string; authKind: AuthKind; enabled: boolean; weight: number; priority: number; status: PublicAccountStatus; inFlight: number; consecutiveFailures: number; cooldownUntil?: number; lastSelectedAt?: number; lastFailureKind?: FailureKind; metadata: Readonly>; } interface PublicPoolSnapshot { id: string; label: string; policy: SelectionPolicy; affinity: boolean; managementHint?: string; accounts: PublicAccountSnapshot[]; } interface MultiProviderSnapshot { providers: PublicPoolSnapshot[]; } interface SchedulerOptions { defaultPolicy?: SelectionPolicy; affinity?: boolean; rateLimitCooldownMs?: number; quotaCooldownMs?: number; authCooldownMs?: number; transientBaseCooldownMs?: number; maxCooldownMs?: number; now?: () => number; randomId?: () => string; } //#endregion //#region src/preferences.d.ts declare const MULTIPROVIDER_SETTINGS_NAMESPACE: import("@monotykamary/dsh-settings").SettingsNamespace; declare const SELECTION_POLICIES: readonly ["round-robin", "weighted-round-robin", "least-inflight", "priority"]; /** Durable, live preference document used by the scheduler and its Settings UI. */ declare class PreferenceStore { private current; private scope; private readonly watchers; constructor(base?: Partial); attach(ctx: Context): void; snapshot(): MultiProviderPreferences; pool(providerId: string, defaults: { policy: SelectionPolicy; affinity: boolean; }): PoolPreference; watch(listener: () => void): () => void; updatePool(providerId: string, patch: Partial>): Promise; private replace; } //#endregion //#region src/service.d.ts /** Generic multi-account scheduler. Provider plugins retain transport and credential ownership. */ declare class MultiProviderService { readonly preferences: PreferenceStore; private readonly providers; private readonly runtime; private readonly affinity; private readonly roundRobinCursor; private readonly smoothScores; private readonly defaults; private readonly now; private readonly randomId; constructor(options?: SchedulerOptions); attachSettings(ctx: Context): void; registerProvider(registration: ProviderRegistration): () => void; acquire(options: AcquireOptions): Promise>; snapshot(): Promise; updatePool(providerId: string, patch: Partial>): Promise; resetHealth(providerId: string, accountId: string): void; clearAffinity(providerId?: string, affinityKey?: string): void; private registration; private preferenceDefaults; private runtimeFor; private effectiveAccounts; private select; private selectWeighted; private recordSuccess; private recordFailure; private defaultCooldown; } //#endregion //#region src/errors.d.ts declare class UnknownProviderError extends Error { readonly providerId: string; constructor(providerId: string); } declare class NoAccountAvailableError extends Error { readonly providerId: string; readonly nextAvailableAt?: number | undefined; constructor(providerId: string, nextAvailableAt?: number | undefined); } //#endregion //#region src/routes.d.ts declare const MULTIPROVIDER_STATE_PATH = "/plugins/dsh-multiprovider/state"; declare const MULTIPROVIDER_PREFERENCES_PATH = "/plugins/dsh-multiprovider/preferences"; declare const MULTIPROVIDER_RESET_PATH = "/plugins/dsh-multiprovider/reset"; declare function trustedRequest(req: IncomingMessage): boolean; /** Register the same-origin JSON control plane consumed by the Settings page. */ declare function registerMultiProviderRoutes(ctx: Context, service: MultiProviderService): void; //#endregion //#region src/index.d.ts declare module '@monotykamary/cordis' { interface Context { /** Shared scheduler injected by concrete provider extensions. */ multiprovider: MultiProviderService; } } declare const name = "multiprovider"; declare const inject: string[]; interface Config extends Omit { defaultPolicy: SelectionPolicy; affinity: boolean; rateLimitCooldownMs: number; quotaCooldownMs: number; authCooldownMs: number; transientBaseCooldownMs: number; maxCooldownMs: number; } declare const Config: z; declare function apply(ctx: Context, config: Config): void; //#endregion export { AccountLease, AccountPreference, AcquireOptions, AuthKind, Config, FailureDisposition, FailureKind, LeaseOutcome, LeaseOutcomeCancelled, LeaseOutcomeFailure, LeaseOutcomeSuccess, MULTIPROVIDER_PREFERENCES_PATH, MULTIPROVIDER_RESET_PATH, MULTIPROVIDER_SETTINGS_NAMESPACE, MULTIPROVIDER_STATE_PATH, MultiProviderPreferences, MultiProviderService, MultiProviderSnapshot, NoAccountAvailableError, PoolPreference, PreferenceStore, ProviderAccount, ProviderRegistration, PublicAccountSnapshot, PublicAccountStatus, PublicPoolSnapshot, SELECTION_POLICIES, SchedulerOptions, SelectionPolicy, UnknownProviderError, apply, inject, name, registerMultiProviderRoutes, trustedRequest };