/** * Caps meter: atomic reservation of call caps and cost-unit budgets. * Mirrors the Python SDK's `grantex.caps`; both address the same counters. */ export declare const CAP_ERROR_CODE = "E1008"; export type CapWindow = 'per_hour' | 'per_day' | 'per_case'; /** `off` skips caps, `warn` allows over-cap calls and reports them, `enforce` denies them. */ export type CapsMode = 'off' | 'warn' | 'enforce'; export declare const CAPS_MODES: readonly CapsMode[]; /** Rolling window length per window name; `per_case` has no time window. */ export declare const WINDOW_MS: Readonly>; export declare const MAX_COUNT = 2147483647; /** One counter a call is metered against. */ export interface CapLimit { /** Counter identity within a tenant; per-case counters must include the case. */ counter: string; limit: number; window: CapWindow; /** Units this call consumes (default 1). */ units?: number; /** `manifest` or `grant` (default `manifest`). */ scope?: string; /** `calls` or `cost_units` (default `calls`). */ kind?: string; } /** A validated limit with defaults applied. */ export interface ResolvedCapLimit extends Required { /** Stable, fixed-length storage key (SHA-256 hex). */ key: string; } /** Units reserved for one call. Keep it to refund a call proven unsent. */ export interface Reservation { reservationId: string; tenantId: string; limits: readonly ResolvedCapLimit[]; } export interface CounterUsage { limit: ResolvedCapLimit; used: number; remaining: number; } /** A reservation would exceed a cap. `code` is `E1008`. */ export declare class CapExceededError extends Error { readonly code = "E1008"; readonly reason = "cap_exceeded"; readonly limit: number; readonly window: CapWindow; readonly used: number; readonly requested: number; readonly scope: string; readonly kind: string; constructor(args: { limit: number; window: CapWindow; used: number; requested: number; scope: string; kind: string; }); } /** The caps backend could not answer. Callers must treat the call as denied. */ export declare class MeterUnavailableError extends Error { constructor(message: string, options?: { cause?: unknown; }); } /** Limits or arguments are invalid. `subReason` names why. */ export declare class CapsConfigurationError extends Error { readonly subReason: string; constructor(message: string, subReason?: string); } /** Storage for counters. Implementations must be atomic across all limits. */ export interface CapsBackend { /** Reserve every limit or none; throw `CapExceededError` naming the first limit exceeded. */ reserve(tenantId: string, reservationId: string, limits: readonly ResolvedCapLimit[], nowMs: number | undefined): Promise; /** Remove a reservation. Idempotent. */ refund(tenantId: string, reservationId: string, limits: readonly ResolvedCapLimit[], nowMs: number | undefined): Promise; /** Units currently held by the counter within its window. */ usage(tenantId: string, limit: ResolvedCapLimit, nowMs: number | undefined): Promise; } /** Storage key for a counter; identical to the Python SDK's `CapLimit.key()`. */ export declare function counterKey(window: CapWindow, counter: string): Promise; /** Fixed-length tenant component for storage keys. */ export declare function tenantHash(tenantId: string): Promise; /** Validate a reservation's arguments and apply defaults. */ export declare function resolveLimits(tenantId: unknown, limits: readonly CapLimit[]): Promise; /** * Reserve call caps and cost-unit budgets before the call that incurs cost. * * `reserve()` checks every limit and records the call atomically, so * concurrent callers can never exceed a cap. A failed call is **not** refunded * by default: a timeout or error does not prove the provider did no work. Call * `refundUnsent()` only when the provider call is known not to have been sent. * Any backend failure throws `MeterUnavailableError`; callers must deny. */ export declare class CapsMeter { #private; constructor(backend: CapsBackend, options?: { clock?: () => number; }); reserve(tenantId: string, limits: readonly CapLimit[]): Promise; /** Release a reservation whose provider call was proven not sent. */ refundUnsent(reservation: Reservation): Promise; /** Current usage of each counter, for display (consent page, case view). */ usage(tenantId: string, limits: readonly CapLimit[]): Promise; } //# sourceMappingURL=meter.d.ts.map