import type { BillingBalance, BillingOrigin } from "@opengeni/contracts"; import { type SQL } from "drizzle-orm"; import type { Database } from "./database.js"; import { type PaidCreditState } from "./paid-credit.js"; /** Read totals and grant remainders from one database snapshot. */ type CreditBalanceSnapshot = BillingBalance & { creditPolicyRevision?: number; }; export declare function getBillingBalance(db: Database, accountId: string, policyRevision?: number, settlementAt?: string, includeHeldForSettlement?: boolean, /** Also report paid credits (`paidCreditsMicros`), for fee decisions. */ includePaidCredits?: boolean): Promise; /** * The balance and the paid-credit state behind it (see paid-credit.ts). One * pass over the account's ledger sums the balance and API spending, exactly * as without own-key fees; the paid-credit figures come from the latest * receipt's running totals plus the rows that can carry a receipt written * after it (normally none), through credit_ledger_receipt_rows_idx. Their * cost does not grow with the number of fees, top-ups or grants. */ export declare function readBillingBalance(db: Database, accountId: string, policyRevision?: number, settlementAt?: string, includeHeldForSettlement?: boolean): Promise<{ balance: CreditBalanceSnapshot; paid: PaidCreditState; }>; /** * How an own-key fee settles: a paid plan's credit first (app use, and only * what the plan's allowance still backs), then paid credits. The rest is fee * debt: parallel calls admitted on the same paid credit can run past it. * plan_period_usage counts exactly `planPaidMicros` against the plan. */ export declare function byokFeeFunding(balance: BillingBalance, paid: PaidCreditState, amountMicros: number, origin: BillingOrigin): { planPaidMicros: number; creditPaidMicros: number; unfundedMicros: number; }; /** * The paid-credit receipt a ledger write records on its row (paid-credit.ts), * read under the account lock just before the write. Paid-for credit, * Stripe refunds and dispute holds, and unpaid credit (promotional grants, * usage refunds, a coupon's discount) get one; plain spending needs none. */ export declare function paidCreditReceipt(tx: Database, input: { accountId: string; settledAt: string; type: string; amountMicros: number; eligibleModelIds?: readonly string[] | null | undefined; metadata?: Record | undefined; sourceType?: string | null | undefined; sourceId?: string | null | undefined; }): Promise<{ metadata: Record; createdAt: SQL; }>; /** * When to stamp a receipt row: the settlement time, but strictly after every * row that can carry a receipt. Running totals are read from the latest * receipt, so a database clock that steps back (a failover to a host slightly * behind) must never stamp a receipt before an earlier one. */ export declare function receiptCreatedAt(settledAt: string, paid: PaidCreditState): SQL; /** * The paid credit an ordinary debit draws, recorded on refundable debits so * the refund can return it: only what unpaid credit could not cover. The * debit charges credits whatever plan credit and covering grants * (`allocations`) leave. */ export declare function paidDrawnMicros(balance: BillingBalance, paid: PaidCreditState, amountMicros: number, allocations: readonly { amountMicros: number; }[], origin: BillingOrigin): number; /** What a bring-your-own-key fee may draw: paid plan credit (app use only), then paid credits. */ export declare function byokFeeSpendableMicros(balance: BillingBalance, origin: BillingOrigin): number; /** Admission read for a bring-your-own-key call: its spendable paid credit. */ export declare function getByokFeeSpendableBalance(db: Database, accountId: string, origin?: BillingOrigin | null): Promise; /** Credits only: what API-origin work may spend (never plan or promotional credit). */ export declare function apiSpendableCreditMicros(balance: BillingBalance): number; /** * The origin a charge is funded under: the caller's explicit origin, else * the one a trusted outer scope fixed (an API request, an agent's turn), * else app, today's behaviour. */ export declare function chargeBillingOrigin(origin?: BillingOrigin | null): BillingOrigin; /** * What a charge pays for. A canonical model ID uses grants scoped to that * model; {@link VOICE_CREDIT_USAGE} (dictation and live voice) uses grants * that cover voice: the verified-signup trial credits and plan credit. * {@link toolCreditUsage} (a paid tool a chat on a model uses) uses plan * credit and grants scoped to that model. Omitted means a non-model resource, * which uses general credits and plan credit. */ export type CreditUsage = string | { readonly kind: "voice"; } | { readonly kind: "tool"; readonly modelId?: string; } | undefined; /** Deployment-funded dictation and live voice. */ export declare const VOICE_CREDIT_USAGE: { readonly kind: "voice"; }; /** * A paid tool (web search, image generation) a chat on `modelId` uses: plan * credit, the Free plan's included, always pays, whatever models its scope * lists; other grants pay only when they list the chat's model. */ export declare function toolCreditUsage(modelId?: string | null): { readonly kind: "tool"; readonly modelId?: string; }; /** General app funding, without model-scoped promotions or scoped Free credit. */ export declare function generalAvailableCreditMicros(balance: BillingBalance): number; export declare function spendableCreditMicros(balance: BillingBalance, usage?: CreditUsage, origin?: BillingOrigin): number; export declare function getSpendableCreditBalance(db: Database, accountId: string, usage?: CreditUsage, policyRevision?: number, settlementAt?: string, /** Funding origin; omitted uses {@link chargeBillingOrigin}. */ origin?: BillingOrigin | null): Promise; /** * Allocations for an already-consumed charge of the full amount: the monthly * plan pays first, covering grants pay beyond it, and other general credit * takes the rest, possibly going negative. */ export declare function planPostUseCreditAllocations(balance: BillingBalance, amountMicros: number, usage: CreditUsage, origin?: BillingOrigin): { grantEntryId: string; amountMicros: number; }[]; export {};