/** * The DeepSeek balance endpoint. * * One `GET /user/balance` per poll, with the credential resolved per request (so a * rotated key reaches the very next poll) and every failure mapped to a stable code * the UI can localize. Two rules matter most: * * - **A failure is never a zero balance.** The caller keeps the previous reading and * marks it stale; fabricating `0` would make a transient network error look like * an empty wallet. * - **The credential never leaves this module except as a request header**, and is * never included in a message, log line, or thrown error. * * @module dsh-balance/balance-api */ import type { BalanceErrorCode, Currency } from './protocol.js'; /** One wallet as reported by the platform. */ export interface WalletEntry { readonly currency: Currency; readonly total: string; readonly granted: string; readonly toppedUp: string; } /** A successful, fully validated balance response. */ export interface BalanceResponse { readonly isAvailable: boolean; /** Non-empty: an empty list is reported as {@link BalanceErrorCode} `NO_WALLET`. */ readonly wallets: readonly WalletEntry[]; } /** A balance failure carrying its machine code. */ export declare class BalanceRequestError extends Error { readonly code: BalanceErrorCode; constructor(code: BalanceErrorCode, message: string); } /** Endpoint and limits for one poll. */ export interface BalanceApiConfig { /** Validated origin, e.g. `https://api.deepseek.com`. */ readonly origin: string; /** Endpoint path, e.g. `/user/balance`. */ readonly path: string; readonly timeoutMs: number; /** Response size cap; defaults to 64 KiB. */ readonly maxBytes?: number; } /** Injectable transport and lifetime signal. */ export interface BalanceFetchDeps { readonly fetchImpl: (input: string, init?: RequestInit) => Promise; readonly signal: AbortSignal; } /** * Validate a resolved credential value for use as a header. * * @param raw - the resolved value. * @param ref - the credential reference name, used in the message only. * @returns the trimmed key. * @throws {BalanceRequestError} with `INVALID_CREDENTIAL` for a blank or header-unsafe * value. The message names the reference, never the value. */ export declare function assertUsableApiKey(raw: string, ref: string): string; /** * Validate a balance payload. * @param payload - the parsed body. * @returns the validated wallets and availability. * @throws {BalanceRequestError} with `PROTOCOL` when a required field is missing or * malformed, or `NO_WALLET` when the list is empty. */ export declare function parseBalancePayload(payload: unknown): BalanceResponse; /** * Fetch and validate the current balance. * * @param deps - transport and lifetime signal. * @param config - endpoint and limits. * @param apiKey - the resolved credential, sent only as an authorization header. * @returns the validated response. * @throws {BalanceRequestError} for every failure mode, with a stable code. */ export declare function fetchBalance(deps: BalanceFetchDeps, config: BalanceApiConfig, apiKey: string): Promise; /** * Select the wallet to display under the configured preference. * * `auto` prefers a funded CNY wallet, then a funded USD one, then the first entry, * which keeps the common single-currency case obvious and never silently switches * currency on a zero balance. * * @param wallets - validated wallets, in platform order. * @param preference - configured preference. * @returns the selected wallet. */ export declare function selectWallet(wallets: readonly WalletEntry[], preference: 'auto' | Currency): WalletEntry;