/** * Wire single source for the balance pill: the HTTP paths the Host serves and * the exact snapshot shape the browser reads. * * This module is imported by BOTH halves and is therefore inlined into the * browser bundle — it must stay dependency-free (no node builtins, no DSH * imports). The client validates every response through {@link parseBalanceState} * rather than trusting JSON, so a Host/client version skew degrades to "keep the * previous snapshot" instead of rendering undefined. * * @module dsh-balance/protocol */ /** Absolute path serving the current {@link BalanceState} snapshot. */ export declare const STATE_PATH = "/dsh-balance/state"; /** Absolute path triggering an immediate credential + balance poll. */ export declare const REFRESH_PATH = "/dsh-balance/refresh"; /** Wallet currencies the DeepSeek Platform reports. */ export type Currency = 'CNY' | 'USD'; /** * Machine codes for every balance failure. The UI renders its own localized copy * per code; nothing here is user-facing text. * * - `MISSING_CREDENTIAL`: the credential reference resolved to nothing (no request sent) * - `INVALID_CREDENTIAL`: the stored value is not a usable header value * - `UNAUTHORIZED`: the platform rejected the credential (401/403) * - `NETWORK`: the request produced no response * - `TIMEOUT`: the request aborted on `requestTimeoutMs` * - `HTTP`: any other non-2xx status * - `PROTOCOL`: a 2xx body that is not a valid balance response * - `NO_WALLET`: a valid body carrying no wallet entries */ export type BalanceErrorCode = 'MISSING_CREDENTIAL' | 'INVALID_CREDENTIAL' | 'UNAUTHORIZED' | 'NETWORK' | 'TIMEOUT' | 'HTTP' | 'PROTOCOL' | 'NO_WALLET'; /** * One platform reading. `usageClockAt` is the pivot that makes the projection * correct under settlement lag: the platform reports `total` as of `at`, but that * figure only reflects usage up to `at - settlementLagMs`, so local consumption * is accumulated from `usageClockAt` rather than from `at`. */ export interface BalanceReading { readonly currency: Currency; /** Decimal string exactly as the platform reports it (never re-parsed to a float for display). */ readonly total: string; readonly granted: string; readonly toppedUp: string; /** When this reading was fetched, epoch ms. */ readonly at: number; /** `at - settlementLagMs`: the usage cutoff this reading actually covers. */ readonly usageClockAt: number; } /** Which calendar assumption the probe data disagrees with, if any. */ export type CalendarHintKind = 'makeup-actually-peak' | 'makeup-actually-offpeak' | 'unpublished-year' | 'holiday-mismatch'; /** * An advisory, never applied automatically: a suggestion the user can act on in * configuration. Derived from probe candidates only, so an ambiguous calendar * day can never move the main correction factor. */ export interface CalendarHint { readonly kind: CalendarHintKind; readonly detail: { /** Observed probe-to-main correction ratio. */ readonly ratio?: number; readonly samples: number; readonly dates?: readonly string[]; readonly year?: number; }; } /** * The locally projected balance: `reading.total` minus what this machine has * observed itself spend since the reading's usage cutoff. */ export interface BalanceProjection { /** `correction × consumed(usageClockAt, now)`, in the reading's currency. */ readonly spendSinceAnchor: number; /** `reading.total - spendSinceAnchor`, clamped at zero. */ readonly remaining: number; /** True when the projection ran past the reading and was clamped to zero. */ readonly underflow: boolean; /** Which cost basis produced the projection. */ readonly basis: 'table' | 'weights'; /** Effective correction factor (median of unambiguous candidates), or null while uncalibrated. */ readonly correction: number | null; /** How many candidates the median was taken over. */ readonly correctionSamples: number; /** True once `correction` came from observed balance drops rather than the price table. */ readonly calibrated: boolean; /** Ambiguous-calendar candidates held out of the main correction. */ readonly probes: { readonly count: number; readonly ratio: number | null; }; /** Advisory calendar conclusion, or null. */ readonly calendarHint: CalendarHint | null; /** Provider/model routes priced by the fallback shape because no table entry matched. */ readonly unmatchedRoutes: readonly string[]; /** True when the price table's currency differs from the wallet's, disabling the table path. */ readonly currencyMismatch: boolean; /** Balance drops attributed to non-usage causes (grant expiry, corrections). */ readonly adjustmentsSinceAnchor: number; } /** Spend rate and the schedule-aware time-to-empty. */ export interface BalanceBurn { /** Instantaneous rate in the current pricing period, in the wallet currency. */ readonly perHour: number | null; readonly currency: Currency; /** Which estimator produced `perHour`. */ readonly source: 'token' | 'balance' | 'none'; /** Observation window behind `perHour`. */ readonly windowMs: number; /** Wall-clock time until empty, integrated across upcoming pricing periods. */ readonly etaMs: number | null; /** True when `etaMs` was clamped at `etaCapMs`. */ readonly etaCapped: boolean; /** True when the balance is exhausted or the platform reports it unusable. */ readonly depleted: boolean; } /** Current pricing period plus the calendar that decided it. */ export interface BalanceScheduleView { readonly peak: boolean; /** Price multiplier applied to peak rates right now (`1` peak, `offPeakFactor` off-peak). */ readonly factor: number; /** Next period boundary, epoch ms (ETA integration segment end). */ readonly nextBoundaryAt: number; readonly calendar: { /** Years with a usable holiday calendar. */ readonly years: readonly number[]; /** Which layer supplied them. */ readonly origin: 'seed' | 'remote' | 'config'; readonly fetchedAt: number | null; readonly currentYearKnown: boolean; readonly nextYearKnown: boolean; }; } /** The complete snapshot served at {@link STATE_PATH}. */ export interface BalanceState { readonly reading: BalanceReading | null; readonly isAvailable: boolean | null; /** True when no reading arrived within `staleAfterMs`. */ readonly stale: boolean; readonly error: { readonly code: BalanceErrorCode; readonly at: number; } | null; readonly projection: BalanceProjection; readonly burn: BalanceBurn; readonly spend: { readonly today: number; readonly lastHour: number; }; readonly schedule: BalanceScheduleView; /** Client-affecting Host settings, so the browser half needs no config of its own. */ readonly ui: { readonly pollIntervalMs: number; readonly etaWarnMs: number; readonly etaCriticalMs: number; readonly settlementLagMs: number; }; /** Host clock at serialization time, for the client's clock-skew correction. */ readonly serverTime: number; } /** * Validate one `GET /dsh-balance/state` body. * @param value - the parsed JSON body. * @returns the snapshot, or null when any field is missing or malformed. The * caller keeps its previous snapshot rather than rendering a partial one. */ export declare function parseBalanceState(value: unknown): BalanceState | null;