@absolutejs/billing 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +23 -2
- package/dist/index.js +15 -5
- package/dist/index.js.map +3 -3
- package/dist/ledger.d.ts +56 -0
- package/dist/ledger.js +54 -0
- package/dist/ledger.js.map +10 -0
- package/dist/manifest.js +12 -4
- package/dist/manifest.js.map +3 -3
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -22,8 +22,15 @@
|
|
|
22
22
|
* old usage snapshot through a new plan without touching any
|
|
23
23
|
* vendor SDK.
|
|
24
24
|
*/
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* An integer amount in a plan's sub-units. 1,000,000 (micros) by default, but
|
|
27
|
+
* a plan may choose a finer denomination — see `Plan.denomination`.
|
|
28
|
+
*/
|
|
26
29
|
export type Micros = number;
|
|
30
|
+
/** Sub-units per currency unit when a plan does not say otherwise. */
|
|
31
|
+
export declare const DEFAULT_DENOMINATION = 1000000;
|
|
32
|
+
/** Nanos — the denomination token-priced APIs need. */
|
|
33
|
+
export declare const NANO_DENOMINATION = 1000000000;
|
|
27
34
|
/**
|
|
28
35
|
* Round a fractional micros value to an integer. The substrate uses
|
|
29
36
|
* **truncation** (banker's-style would surprise callers expecting
|
|
@@ -83,6 +90,17 @@ export type PricedDimension = {
|
|
|
83
90
|
export type Plan = {
|
|
84
91
|
/** Human label for the invoice (`'pro'`, `'enterprise'`, etc.). */
|
|
85
92
|
name: string;
|
|
93
|
+
/**
|
|
94
|
+
* Sub-units per currency unit. Defaults to 1,000,000 (micros).
|
|
95
|
+
*
|
|
96
|
+
* Micros are too coarse for token-priced APIs: at $0.16 per million
|
|
97
|
+
* embedding tokens a five-token call costs $0.0000008, which truncates to
|
|
98
|
+
* ZERO in micros — so a plan priced in micros systematically under-bills
|
|
99
|
+
* its cheapest calls. Set `1_000_000_000` to price in nanos, or any other
|
|
100
|
+
* power of ten the vendor's rate card needs. Every `*Micros` field on the
|
|
101
|
+
* plan and the invoice is denominated in these sub-units.
|
|
102
|
+
*/
|
|
103
|
+
denomination?: number;
|
|
86
104
|
/** Optional flat base fee charged once per invoice period. */
|
|
87
105
|
basePriceMicros?: Micros;
|
|
88
106
|
/**
|
|
@@ -145,6 +163,8 @@ export type Invoice = {
|
|
|
145
163
|
tenant: string;
|
|
146
164
|
plan: string;
|
|
147
165
|
currency: string;
|
|
166
|
+
/** Sub-units per currency unit these amounts are in (default micros). */
|
|
167
|
+
denomination: number;
|
|
148
168
|
period: InvoicePeriod;
|
|
149
169
|
lineItems: LineItem[];
|
|
150
170
|
/** Sum of all `lineItems[].amountMicros`. */
|
|
@@ -169,7 +189,8 @@ export declare const computeInvoice: ({ plan, tenant, period, usage, currency, }
|
|
|
169
189
|
* — no Intl side effects. For locales / advanced formatting, pipe
|
|
170
190
|
* through `Intl.NumberFormat` yourself.
|
|
171
191
|
*/
|
|
172
|
-
export declare const formatMicros: (amount: Micros, currency: string, { minorUnits }?: {
|
|
192
|
+
export declare const formatMicros: (amount: Micros, currency: string, { denomination, minorUnits, }?: {
|
|
193
|
+
denomination?: number;
|
|
173
194
|
minorUnits?: number;
|
|
174
195
|
}) => string;
|
|
175
196
|
export { readProviderBalances, type ProviderBalance, type ProviderBalanceConfig, type ProviderBalanceKind, type ProviderBalanceStatus, type BraveUsageSnapshot, type EmbeddingUsageSnapshot, } from "./balances";
|
package/dist/index.js
CHANGED
|
@@ -380,6 +380,8 @@ var readProviderBalances = async (config) => {
|
|
|
380
380
|
};
|
|
381
381
|
|
|
382
382
|
// src/index.ts
|
|
383
|
+
var DEFAULT_DENOMINATION = 1e6;
|
|
384
|
+
var NANO_DENOMINATION = 1e9;
|
|
383
385
|
var roundMicros = (value, rounding) => {
|
|
384
386
|
if (rounding === "truncate")
|
|
385
387
|
return Math.trunc(value);
|
|
@@ -503,23 +505,29 @@ var computeInvoice = ({
|
|
|
503
505
|
});
|
|
504
506
|
totalMicros = floor;
|
|
505
507
|
}
|
|
508
|
+
const denomination = plan.denomination ?? DEFAULT_DENOMINATION;
|
|
506
509
|
const invoice = {
|
|
507
510
|
currency: currency ?? plan.currency ?? "usd",
|
|
511
|
+
denomination,
|
|
508
512
|
lineItems,
|
|
509
513
|
period,
|
|
510
514
|
plan: plan.name,
|
|
511
515
|
tenant,
|
|
512
516
|
totalMicros,
|
|
513
|
-
totalUnits: totalMicros /
|
|
517
|
+
totalUnits: totalMicros / denomination
|
|
514
518
|
};
|
|
515
519
|
if (plan.metadata !== undefined)
|
|
516
520
|
invoice.metadata = plan.metadata;
|
|
517
521
|
return invoice;
|
|
518
522
|
};
|
|
519
|
-
var formatMicros = (amount, currency, {
|
|
523
|
+
var formatMicros = (amount, currency, {
|
|
524
|
+
denomination = DEFAULT_DENOMINATION,
|
|
525
|
+
minorUnits = 2
|
|
526
|
+
} = {}) => {
|
|
520
527
|
const sign = amount < 0 ? "-" : "";
|
|
521
528
|
const abs = Math.abs(amount);
|
|
522
|
-
const
|
|
529
|
+
const exponent = Math.round(Math.log10(denomination));
|
|
530
|
+
const wholeMicrosPerMinor = 10 ** (exponent - minorUnits);
|
|
523
531
|
const minorTotal = Math.round(abs / wholeMicrosPerMinor);
|
|
524
532
|
const divisor = 10 ** minorUnits;
|
|
525
533
|
const whole = Math.trunc(minorTotal / divisor);
|
|
@@ -534,8 +542,10 @@ export {
|
|
|
534
542
|
readProviderBalances,
|
|
535
543
|
formatMicros,
|
|
536
544
|
createPlan,
|
|
537
|
-
computeInvoice
|
|
545
|
+
computeInvoice,
|
|
546
|
+
NANO_DENOMINATION,
|
|
547
|
+
DEFAULT_DENOMINATION
|
|
538
548
|
};
|
|
539
549
|
|
|
540
|
-
//# debugId=
|
|
550
|
+
//# debugId=D50F9899A29C0CBA64756E2164756E21
|
|
541
551
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
"sources": ["../src/balances.ts", "../src/index.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"/**\n * Provider balances — read each upstream vendor's OWN reported balance / quota /\n * spend from their billing-or-usage API, normalized to one shape. The inverse of\n * `computeInvoice`: that prices YOUR usage into an invoice; this reads what the\n * vendors you pay say you have left (or have spent). Useful for an ops dashboard\n * that reconciles your own metering against vendor truth.\n *\n * These are free reporting endpoints — they run no model and incur no per-call\n * charge (just rate limits). Pure + dependency-free: pass credentials in, get\n * snapshots out. One provider failing never affects the others.\n *\n * Coverage by what each vendor exposes:\n * - balance ($ left): Twilio, Deepgram\n * - quota (units left): ElevenLabs (chars), Apollo (calls/day), Brave (queries)\n * - cost ($ spent): Anthropic, OpenAI — no balance API exists, only spend\n *\n * Brave has no API at all; the host app captures rate-limit headers off its own\n * search calls and passes the latest snapshot in (`config.brave`).\n */\n\nconst FETCH_TIMEOUT_MS = 6000;\n// The LLM providers' org cost-report endpoints are slow (OpenAI's regularly\n// takes ~6s); give them a generous timeout since the result is cached.\nconst COST_FETCH_TIMEOUT_MS = 20_000;\nconst MS_PER_SECOND = 1000;\nconst MS_PER_DAY = 86_400_000;\nconst MILLION = 1_000_000;\nconst THOUSAND = 1000;\nconst COST_WINDOW_DAYS = 30;\n\nexport type ProviderBalanceKind = \"balance\" | \"quota\" | \"cost\" | \"none\";\nexport type ProviderBalanceStatus = \"ok\" | \"unconfigured\" | \"error\";\n\nexport type ProviderBalance = {\n /** Human summary line, e.g. \"$42.10 left\" or \"1.2M / 2M chars\". */\n detail: string;\n checkedAt: string;\n kind: ProviderBalanceKind;\n label: string;\n /** The vendor's spend/limit when known; null when not exposed. */\n limit: number | null;\n /** Caveat for the tile (e.g. \"no balance API — Admin key needed\"). */\n note: string | null;\n provider: string;\n /** Remaining balance/quota for kind balance|quota; null otherwise. */\n remaining: number | null;\n resetDate: string | null;\n status: ProviderBalanceStatus;\n /** Vendor plan/tier where exposed (ElevenLabs \"pro\", Twilio \"Full\"); else null. */\n tier: string | null;\n unit: string;\n used: number | null;\n};\n\n/** Rate-limit snapshot the host app captures off its own Brave search responses\n * (Brave has no usage API). The 30-day-window header is `monthly*`. */\nexport type BraveUsageSnapshot = {\n capturedAt: string;\n monthlyLimit: number | null;\n monthlyRemaining: number | null;\n resetSeconds: number | null;\n};\n\n/**\n * Per-provider credentials. Include a provider's key to get its tile; omit it to\n * skip the provider entirely. A present-but-empty credential yields an\n * \"unconfigured\" tile (so a dashboard can show every provider it cares about and\n * label the ones missing a key).\n */\n/** Embedding consumption the host measured itself, against the plan's cap.\n * Vector vendors meter tokens per month and simply refuse once spent, so\n * this is the number that predicts an outage. */\nexport type EmbeddingUsageSnapshot = {\n capturedAt: string;\n /** True when the provider is currently refusing embeddings. */\n exhausted?: boolean;\n monthlyTokenLimit: number;\n resetDate?: string | null;\n tokensUsed: number;\n};\n\nexport type ProviderBalanceConfig = {\n anthropic?: { adminKey: string };\n apollo?: { apiKey: string };\n brave?: BraveUsageSnapshot | null;\n /** Host-supplied embedding usage. Pinecone exposes no usage API to an\n * ordinary key, so — like Brave — the app reports what it metered. */\n pinecone?: EmbeddingUsageSnapshot | null;\n deepgram?: { apiKey: string };\n elevenlabs?: { apiKey: string };\n openai?: { adminKey: string };\n twilio?: { accountSid: string; authToken: string };\n};\n\nconst nowIso = () => new Date().toISOString();\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null;\n\nconst numberOf = (value: unknown) =>\n typeof value === \"number\" && Number.isFinite(value) ? value : null;\n\nconst usd = (amount: number) => `$${amount.toFixed(2)}`;\n\nconst compact = (value: number) => {\n if (value >= MILLION) return `${(value / MILLION).toFixed(1)}M`;\n if (value >= THOUSAND) return `${(value / THOUSAND).toFixed(0)}K`;\n\n return String(value);\n};\n\nconst base = (provider: string, label: string) => {\n const result: ProviderBalance = {\n checkedAt: nowIso(),\n detail: \"\",\n kind: \"none\",\n label,\n limit: null,\n note: null,\n provider,\n remaining: null,\n resetDate: null,\n status: \"error\",\n tier: null,\n unit: \"\",\n used: null,\n };\n\n return result;\n};\n\nconst unconfigured = (provider: string, label: string, note: string) => {\n const result: ProviderBalance = {\n ...base(provider, label),\n note,\n status: \"unconfigured\",\n };\n\n return result;\n};\n\nconst errored = (provider: string, label: string, message: string) => {\n const result: ProviderBalance = {\n ...base(provider, label),\n detail: \"Couldn't reach provider\",\n note: message,\n };\n\n return result;\n};\n\nconst fetchJson = async (\n url: string,\n headers: Record<string, string>,\n opts: { method?: string; timeoutMs?: number } = {},\n) => {\n const controller = new AbortController();\n const timer = setTimeout(\n () => controller.abort(),\n opts.timeoutMs ?? FETCH_TIMEOUT_MS,\n );\n try {\n const response = await fetch(url, {\n headers,\n method: opts.method ?? \"GET\",\n signal: controller.signal,\n });\n if (!response.ok) {\n // Surface the vendor's own error text (truncated) so the tile is\n // actionable — e.g. Deepgram's \"needs the billing:read scope\".\n const body = await response.text().catch(() => \"\");\n const snippet = body.replace(/\\s+/g, \" \").trim().slice(0, 160);\n throw new Error(\n snippet\n ? `HTTP ${response.status}: ${snippet}`\n : `HTTP ${response.status}`,\n );\n }\n const json: unknown = await response.json();\n\n return json;\n } finally {\n clearTimeout(timer);\n }\n};\n\n// --- Twilio: real account balance + plan type -----------------------------\nconst twilioBalance = async (creds: {\n accountSid: string;\n authToken: string;\n}) => {\n if (!creds.accountSid || !creds.authToken) {\n return unconfigured(\"twilio\", \"Twilio\", \"Twilio credentials unset\");\n }\n try {\n const auth = Buffer.from(`${creds.accountSid}:${creds.authToken}`).toString(\n \"base64\",\n );\n const headers: Record<string, string> = { Authorization: `Basic ${auth}` };\n const [data, account] = await Promise.all([\n fetchJson(\n `https://api.twilio.com/2010-04-01/Accounts/${creds.accountSid}/Balance.json`,\n headers,\n ),\n fetchJson(\n `https://api.twilio.com/2010-04-01/Accounts/${creds.accountSid}.json`,\n headers,\n ),\n ]);\n const balance = isRecord(data) ? numberOf(Number(data.balance)) : null;\n const currency =\n isRecord(data) && typeof data.currency === \"string\"\n ? data.currency\n : \"USD\";\n const accountType =\n isRecord(account) && typeof account.type === \"string\"\n ? account.type\n : null;\n if (balance === null) throw new Error(\"no balance field\");\n const result: ProviderBalance = {\n ...base(\"twilio\", \"Twilio\"),\n detail: `${currency} ${balance.toFixed(2)} left`,\n kind: \"balance\",\n remaining: balance,\n status: \"ok\",\n tier: accountType,\n unit: currency,\n };\n\n return result;\n } catch (error) {\n return errored(\"twilio\", \"Twilio\", String(error));\n }\n};\n\n// --- Deepgram: real $ balance summed across projects ----------------------\nconst extractProjectIds = (projects: unknown) => {\n const list =\n isRecord(projects) && Array.isArray(projects.projects)\n ? projects.projects\n : [];\n\n return list\n .map((proj) =>\n isRecord(proj) && typeof proj.project_id === \"string\"\n ? proj.project_id\n : null,\n )\n .filter((id): id is string => id !== null);\n};\n\nconst sumBalances = (balances: unknown) => {\n const rows =\n isRecord(balances) && Array.isArray(balances.balances)\n ? balances.balances\n : [];\n\n return rows.reduce<number>(\n (total, row) => total + (isRecord(row) ? (numberOf(row.amount) ?? 0) : 0),\n 0,\n );\n};\n\nconst fetchDeepgramTotal = async (headers: Record<string, string>) => {\n const projects = await fetchJson(\n \"https://api.deepgram.com/v1/projects\",\n headers,\n );\n let total = 0;\n for (const projectId of extractProjectIds(projects)) {\n // eslint-disable-next-line no-await-in-loop -- a couple of projects at most\n const balances = await fetchJson(\n `https://api.deepgram.com/v1/projects/${projectId}/balances`,\n headers,\n );\n total += sumBalances(balances);\n }\n\n return total;\n};\n\nconst deepgramBalance = async (creds: { apiKey: string }) => {\n if (!creds.apiKey)\n return unconfigured(\"deepgram\", \"Deepgram\", \"API key unset\");\n try {\n const total = await fetchDeepgramTotal({\n Authorization: `Token ${creds.apiKey}`,\n });\n const result: ProviderBalance = {\n ...base(\"deepgram\", \"Deepgram\"),\n detail: `${usd(total)} left`,\n kind: \"balance\",\n remaining: total,\n status: \"ok\",\n unit: \"USD\",\n };\n\n return result;\n } catch (error) {\n return errored(\"deepgram\", \"Deepgram\", String(error));\n }\n};\n\n// --- ElevenLabs: real character quota used / limit + tier -----------------\nconst elevenLabsBalance = async (creds: { apiKey: string }) => {\n if (!creds.apiKey)\n return unconfigured(\"elevenlabs\", \"ElevenLabs\", \"API key unset\");\n try {\n const data = await fetchJson(\n \"https://api.elevenlabs.io/v1/user/subscription\",\n {\n \"xi-api-key\": creds.apiKey,\n },\n );\n const usedChars = isRecord(data) ? numberOf(data.character_count) : null;\n const limitChars = isRecord(data) ? numberOf(data.character_limit) : null;\n if (usedChars === null || limitChars === null) {\n throw new Error(\"no character fields\");\n }\n const resetUnix = isRecord(data)\n ? numberOf(data.next_character_count_reset_unix)\n : null;\n const tier =\n isRecord(data) && typeof data.tier === \"string\" ? data.tier : null;\n const remaining = Math.max(0, limitChars - usedChars);\n const result: ProviderBalance = {\n ...base(\"elevenlabs\", \"ElevenLabs\"),\n detail: `${compact(remaining)} / ${compact(limitChars)} chars left`,\n kind: \"quota\",\n limit: limitChars,\n remaining,\n resetDate: resetUnix\n ? new Date(resetUnix * MS_PER_SECOND).toISOString()\n : null,\n status: \"ok\",\n tier,\n unit: \"characters\",\n used: usedChars,\n };\n\n return result;\n } catch (error) {\n return errored(\"elevenlabs\", \"ElevenLabs\", String(error));\n }\n};\n\n// --- Apollo: real per-endpoint API quota (master key required) ------------\nconst pickBusiestDayQuota = (data: unknown) => {\n let consumed = 0;\n let limit = 0;\n if (!isRecord(data)) return { consumed, limit };\n for (const value of Object.values(data)) {\n const day = isRecord(value) && isRecord(value.day) ? value.day : null;\n if (!day) continue;\n const dayLimit = numberOf(day.limit) ?? 0;\n if (dayLimit <= limit) continue;\n limit = dayLimit;\n consumed = numberOf(day.consumed) ?? 0;\n }\n\n return { consumed, limit };\n};\n\nconst apolloResult = (consumed: number, limit: number) => {\n if (limit === 0) {\n const noQuota: ProviderBalance = {\n ...base(\"apollo\", \"Apollo\"),\n detail: \"Reached Apollo (no day quota in response)\",\n note: \"If usage stats 403, the key must be an Apollo master key.\",\n status: \"ok\",\n };\n\n return noQuota;\n }\n const result: ProviderBalance = {\n ...base(\"apollo\", \"Apollo\"),\n detail: `${Math.max(0, limit - consumed)} / ${limit} calls left today`,\n kind: \"quota\",\n limit,\n remaining: Math.max(0, limit - consumed),\n status: \"ok\",\n unit: \"calls\",\n used: consumed,\n };\n\n return result;\n};\n\nconst apolloBalance = async (creds: { apiKey: string }) => {\n if (!creds.apiKey) return unconfigured(\"apollo\", \"Apollo\", \"API key unset\");\n try {\n // POST (not GET) per Apollo's API; needs the master key.\n const data = await fetchJson(\n \"https://api.apollo.io/api/v1/usage_stats/api_usage_stats\",\n { \"Content-Type\": \"application/json\", \"X-Api-Key\": creds.apiKey },\n { method: \"POST\" },\n );\n const { consumed, limit } = pickBusiestDayQuota(data);\n\n return apolloResult(consumed, limit);\n } catch (error) {\n return errored(\n \"apollo\",\n \"Apollo\",\n `${error} — usage stats need the Apollo master key`,\n );\n }\n};\n\n// --- Pinecone: no usage API for an ordinary key; same host-snapshot shape --\nconst compactTokens = (tokens: number) => {\n const MILLION = 1_000_000;\n const THOUSAND = 1000;\n if (tokens >= MILLION) return `${(tokens / MILLION).toFixed(1)}M`;\n\n return `${Math.round(tokens / THOUSAND)}k`;\n};\n\nconst pineconeBalance = (snap: EmbeddingUsageSnapshot | null | undefined) => {\n if (!snap) {\n return unconfigured(\n \"pinecone\",\n \"Pinecone embeddings\",\n \"No embedding usage reported yet — the host supplies this from its own metering.\",\n );\n }\n const remaining = Math.max(0, snap.monthlyTokenLimit - snap.tokensUsed);\n const quota: ProviderBalance = {\n ...base(\"pinecone\", \"Pinecone embeddings\"),\n checkedAt: snap.capturedAt,\n detail: snap.exhausted\n ? `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens — quota spent, embeddings refused`\n : `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens this month`,\n kind: \"quota\",\n limit: snap.monthlyTokenLimit,\n note: \"Counted from the host's own metering — Pinecone exposes no usage API.\",\n remaining,\n resetDate: snap.resetDate ?? null,\n status: \"ok\",\n unit: \"tokens\",\n used: snap.tokensUsed,\n };\n\n return quota;\n};\n\n// --- Brave: no API; read the snapshot the host captured off its own calls --\nconst braveBalance = (snap: BraveUsageSnapshot | null | undefined) => {\n if (!snap) {\n return unconfigured(\n \"brave\",\n \"Brave Search\",\n \"No usage captured yet — appears after the next web search.\",\n );\n }\n if (snap.monthlyLimit && snap.monthlyLimit > 0) {\n const remaining = snap.monthlyRemaining ?? 0;\n const quota: ProviderBalance = {\n ...base(\"brave\", \"Brave Search\"),\n checkedAt: snap.capturedAt,\n detail: `${remaining} / ${snap.monthlyLimit} queries left this month`,\n kind: \"quota\",\n limit: snap.monthlyLimit,\n remaining,\n status: \"ok\",\n unit: \"queries\",\n used: snap.monthlyLimit - remaining,\n };\n\n return quota;\n }\n const metered: ProviderBalance = {\n ...base(\"brave\", \"Brave Search\"),\n checkedAt: snap.capturedAt,\n detail: \"Metered · pay-as-you-go\",\n note: \"No prepaid cap — billed per query; the host's spend cap is the ceiling.\",\n status: \"ok\",\n tier: \"metered\",\n };\n\n return metered;\n};\n\nconst costWindowStart = () => Date.now() - COST_WINDOW_DAYS * MS_PER_DAY;\n\n// --- OpenAI: real spend (no balance API). Needs an sk-admin- Admin key. ----\nconst sumOpenAiCosts = (data: unknown) => {\n const buckets = isRecord(data) && Array.isArray(data.data) ? data.data : [];\n\n return buckets.reduce<number>((total, bucket) => {\n const results =\n isRecord(bucket) && Array.isArray(bucket.results) ? bucket.results : [];\n const bucketSum = results.reduce<number>((sub, row) => {\n const amount = isRecord(row) && isRecord(row.amount) ? row.amount : null;\n const value = amount ? Number(amount.value) : NaN;\n\n return sub + (Number.isFinite(value) ? value : 0);\n }, 0);\n\n return total + bucketSum;\n }, 0);\n};\n\nconst openaiBalance = async (creds: { adminKey: string }) => {\n if (!creds.adminKey) {\n return unconfigured(\n \"openai\",\n \"OpenAI\",\n \"No balance API. Provide an sk-admin- Admin key to show real spend.\",\n );\n }\n try {\n const startTime = Math.floor(costWindowStart() / MS_PER_SECOND);\n const data = await fetchJson(\n `https://api.openai.com/v1/organization/costs?start_time=${startTime}&limit=${COST_WINDOW_DAYS + 1}`,\n { Authorization: `Bearer ${creds.adminKey}` },\n { timeoutMs: COST_FETCH_TIMEOUT_MS },\n );\n const result: ProviderBalance = {\n ...base(\"openai\", \"OpenAI\"),\n detail: `${usd(sumOpenAiCosts(data))} spent (30d)`,\n kind: \"cost\",\n note: \"No balance API — real provider-reported spend, not balance.\",\n status: \"ok\",\n };\n\n return result;\n } catch (error) {\n return errored(\"openai\", \"OpenAI\", String(error));\n }\n};\n\n// --- Anthropic: real spend (no balance API). Needs an sk-ant-admin key. ----\nconst sumAnthropicCosts = (data: unknown) => {\n const buckets = isRecord(data) && Array.isArray(data.data) ? data.data : [];\n\n return buckets.reduce<number>((total, bucket) => {\n const results =\n isRecord(bucket) && Array.isArray(bucket.results) ? bucket.results : [];\n const bucketSum = results.reduce<number>((sub, row) => {\n if (!isRecord(row)) return sub;\n const raw = row.amount ?? row.cost ?? row.value;\n const value = Number(isRecord(raw) ? (raw.value ?? raw.amount) : raw);\n\n return sub + (Number.isFinite(value) ? value : 0);\n }, 0);\n\n return total + bucketSum;\n }, 0);\n};\n\nconst anthropicBalance = async (creds: { adminKey: string }) => {\n if (!creds.adminKey) {\n return unconfigured(\n \"anthropic\",\n \"Anthropic\",\n \"No balance API. Provide an sk-ant-admin Admin key to show real spend.\",\n );\n }\n try {\n const startedAt = new Date(costWindowStart()).toISOString();\n const data = await fetchJson(\n `https://api.anthropic.com/v1/organizations/cost_report?starting_at=${startedAt}`,\n { \"anthropic-version\": \"2023-06-01\", \"x-api-key\": creds.adminKey },\n { timeoutMs: COST_FETCH_TIMEOUT_MS },\n );\n const result: ProviderBalance = {\n ...base(\"anthropic\", \"Anthropic\"),\n detail: `${usd(sumAnthropicCosts(data))} spent (30d)`,\n kind: \"cost\",\n note: \"No balance API — real provider-reported spend, not balance.\",\n status: \"ok\",\n };\n\n return result;\n } catch (error) {\n return errored(\n \"anthropic\",\n \"Anthropic\",\n `${error} — needs an sk-ant-admin Admin key (a regular API key won't work).`,\n );\n }\n};\n\n/**\n * Read every configured provider's real balance/quota/spend in parallel. Only\n * providers present in `config` produce a tile. Each runs independently — one\n * failing returns an \"error\" tile, never rejects the whole call. Stateless: the\n * caller owns any caching (these are free reporting calls, ~1/min is plenty).\n */\nexport const readProviderBalances = async (\n config: ProviderBalanceConfig,\n): Promise<ProviderBalance[]> => {\n const jobs: Array<Promise<ProviderBalance>> = [];\n if (config.twilio) jobs.push(twilioBalance(config.twilio));\n if (config.deepgram) jobs.push(deepgramBalance(config.deepgram));\n if (config.elevenlabs) jobs.push(elevenLabsBalance(config.elevenlabs));\n if (config.apollo) jobs.push(apolloBalance(config.apollo));\n if (\"brave\" in config) jobs.push(Promise.resolve(braveBalance(config.brave)));\n if (\"pinecone\" in config) {\n jobs.push(Promise.resolve(pineconeBalance(config.pinecone)));\n }\n if (config.anthropic) jobs.push(anthropicBalance(config.anthropic));\n if (config.openai) jobs.push(openaiBalance(config.openai));\n\n return Promise.all(jobs);\n};\n",
|
|
6
|
-
"/**\n * @absolutejs/billing — cost-model substrate for the AbsoluteJS PaaS.\n *\n * Two pieces:\n *\n * - `createPlan(...)` — declarative pricing config: optional flat\n * base fee + per-dimension unit prices, with optional graduated\n * tiers and free-tier allowances per dimension.\n *\n * - `computeInvoice({ plan, period, tenant, usage, currency? })`\n * — pure function that turns a `@absolutejs/metering`-shaped\n * `Usage` snapshot (or any record of metered numbers) into an\n * `Invoice` of line items + total. All money math is done in\n * integer **micros** (1 micro = 1/1,000,000 of a currency unit\n * — the same denomination Stripe uses internally) so float\n * drift is structurally impossible.\n *\n * Invoice sinks (push to Stripe, post to QuickBooks, mail a PDF)\n * live OUTSIDE this package, in `@absolutejs/billing-adapters/*`.\n * Keeping the substrate pure means the control plane can preview\n * invoices, run dry-run \"would-charge\" projections, and replay an\n * old usage snapshot through a new plan without touching any\n * vendor SDK.\n */\n\n// =============================================================================\n// Money primitives\n// =============================================================================\n\n/** Integer micros — 1,000,000 micros = 1 unit of the currency. */\nexport type Micros = number;\n\n/**\n * Round a fractional micros value to an integer. The substrate uses\n * **truncation** (banker's-style would surprise callers expecting\n * \"$0.0009 → $0.00\" not \"$0.0009 → $0.001\"). Plans override per-plan.\n */\nexport type Rounding = \"truncate\" | \"round-half-up\";\n\nconst roundMicros = (value: number, rounding: Rounding): Micros => {\n if (rounding === \"truncate\") return Math.trunc(value);\n return Math.round(value);\n};\n\n// =============================================================================\n// Pricing config\n// =============================================================================\n\n/**\n * One step in a graduated-tier price table. `upTo` is the inclusive\n * upper bound (in metered units, NOT micros) for this band.\n * `perUnitMicros` is what the customer pays per single metered unit\n * within this band. The last entry must have `upTo: Infinity` to\n * cover any overflow.\n */\nexport type PricingTier = {\n upTo: number;\n perUnitMicros: number;\n};\n\n/**\n * Per-dimension pricing. Three shapes:\n *\n * - Flat per-unit: `{ perUnitMicros: 200, unit: 1024 * 1024 }`\n * charges 200 micros ($0.0002) per MB of usage.\n *\n * - Tiered: `{ tiers: [...], unit: 1 }` charges per the first\n * matching `PricingTier` band.\n *\n * - Custom: `{ price: (quantity) => micros, unit: 1 }` — escape\n * hatch for surge / caps / non-monotonic pricing. The substrate\n * stays pure; you ship whatever function you want.\n *\n * `freeTier` is subtracted from the metered quantity BEFORE pricing\n * — the conventional \"first N units free\" rule.\n *\n * `unit` is the metered-unit denominator: 1 means \"price per single\n * metered unit\", 1024*1024 means \"price per MB when quantity is in\n * bytes.\" Default 1.\n *\n * `label` overrides the line-item display name.\n */\nexport type PricedDimension = {\n label?: string;\n freeTier?: number;\n unit?: number;\n} & (\n | { perUnitMicros: number; tiers?: never; price?: never }\n | { tiers: PricingTier[]; perUnitMicros?: never; price?: never }\n | {\n price: (chargedQuantity: number) => Micros;\n perUnitMicros?: never;\n tiers?: never;\n }\n);\n\nexport type Plan = {\n /** Human label for the invoice (`'pro'`, `'enterprise'`, etc.). */\n name: string;\n /** Optional flat base fee charged once per invoice period. */\n basePriceMicros?: Micros;\n /**\n * Dimensions priced from usage. Keys must match keys on the\n * `usage` record passed to `computeInvoice`. Anything not listed\n * is ignored.\n */\n pricedDimensions: Record<string, PricedDimension>;\n /** Default currency for invoices generated from this plan. */\n currency?: string;\n /** Rounding strategy applied per line item. Default `'truncate'`. */\n rounding?: Rounding;\n /**\n * Minimum charge (in micros) — if the computed total is below\n * this floor, the invoice total is raised to the floor and a\n * single `'minimum-charge-adjustment'` line item captures the\n * difference. Defaults to 0 (no floor).\n */\n minimumChargeMicros?: Micros;\n /** Arbitrary plan-level metadata that flows through to invoices. */\n metadata?: Record<string, string>;\n};\n\nexport const createPlan = (plan: Plan): Plan => {\n for (const [key, dim] of Object.entries(plan.pricedDimensions)) {\n if (dim.tiers !== undefined) {\n if (dim.tiers.length === 0) {\n throw new Error(`billing: dimension '${key}' has no tiers`);\n }\n const last = dim.tiers[dim.tiers.length - 1];\n if (last !== undefined && Number.isFinite(last.upTo)) {\n throw new Error(\n `billing: dimension '${key}' final tier must have upTo: Infinity`,\n );\n }\n let prev = 0;\n for (let i = 0; i < dim.tiers.length; i += 1) {\n const tier = dim.tiers[i]!;\n if (tier.upTo < prev) {\n throw new Error(\n `billing: dimension '${key}' tier #${i} upTo (${tier.upTo}) must be >= previous (${prev})`,\n );\n }\n prev = tier.upTo;\n }\n }\n }\n return plan;\n};\n\n// =============================================================================\n// Invoice shape\n// =============================================================================\n\nexport type LineItem = {\n /**\n * Stable key for the line item. For priced dimensions it's the\n * usage-record key (`'requests'`, `'cpuMs'`, etc.). For the base\n * fee it's `'base'`. For minimum-charge top-up it's\n * `'minimum-charge-adjustment'`.\n */\n key: string;\n /** Human-readable label. */\n label: string;\n /** Metered units BEFORE applying free tier. 0 for the base fee. */\n quantity: number;\n /** Metered units AFTER applying free tier (what's actually charged). */\n chargedQuantity: number;\n /** Free-tier units subtracted from `quantity`. */\n freeTier?: number;\n /** Charge for this line in integer micros. */\n amountMicros: Micros;\n /**\n * Tier-by-tier breakdown when graduated pricing was used. Each\n * entry: `{ tierIndex, unitsInTier, perUnitMicros, amountMicros }`.\n */\n tierBreakdown?: Array<{\n tierIndex: number;\n unitsInTier: number;\n perUnitMicros: number;\n amountMicros: Micros;\n }>;\n};\n\nexport type InvoicePeriod = {\n /** Inclusive period start (`Date.now()` ms). */\n start: number;\n /** Exclusive period end. */\n end: number;\n};\n\nexport type Invoice = {\n tenant: string;\n plan: string;\n currency: string;\n period: InvoicePeriod;\n lineItems: LineItem[];\n /** Sum of all `lineItems[].amountMicros`. */\n totalMicros: Micros;\n /** Convenience: `totalMicros / 1_000_000` as a number. */\n totalUnits: number;\n /** Plan-level metadata copied through unchanged. */\n metadata?: Record<string, string>;\n};\n\n// =============================================================================\n// Pricing math\n// =============================================================================\n\ntype ComputeDimensionInput = {\n quantity: number;\n dim: PricedDimension;\n rounding: Rounding;\n};\n\ntype ComputeDimensionResult = {\n amountMicros: Micros;\n chargedQuantity: number;\n tierBreakdown?: LineItem[\"tierBreakdown\"];\n};\n\nconst computeDimension = ({\n quantity,\n dim,\n rounding,\n}: ComputeDimensionInput): ComputeDimensionResult => {\n const free = dim.freeTier ?? 0;\n const charged = Math.max(0, quantity - free);\n const unit = dim.unit ?? 1;\n const chargedUnits = unit === 1 ? charged : charged / unit;\n\n if (dim.perUnitMicros !== undefined) {\n const amountMicros = roundMicros(\n chargedUnits * dim.perUnitMicros,\n rounding,\n );\n return { amountMicros, chargedQuantity: charged };\n }\n\n if (dim.price !== undefined) {\n const amountMicros = roundMicros(dim.price(charged), rounding);\n return { amountMicros, chargedQuantity: charged };\n }\n\n // Tiered pricing — walk tiers, allocate chargedUnits into bands.\n const tierBreakdown: NonNullable<LineItem[\"tierBreakdown\"]> = [];\n let remaining = chargedUnits;\n let bandFloor = 0;\n let totalMicros = 0;\n for (let i = 0; i < dim.tiers!.length && remaining > 0; i += 1) {\n const tier = dim.tiers![i]!;\n const bandWidth = tier.upTo - bandFloor;\n const unitsInTier = Math.min(remaining, bandWidth);\n if (unitsInTier > 0) {\n const tierMicros = roundMicros(\n unitsInTier * tier.perUnitMicros,\n rounding,\n );\n tierBreakdown.push({\n amountMicros: tierMicros,\n perUnitMicros: tier.perUnitMicros,\n tierIndex: i,\n unitsInTier,\n });\n totalMicros += tierMicros;\n }\n remaining -= unitsInTier;\n bandFloor = tier.upTo;\n }\n return {\n amountMicros: totalMicros,\n chargedQuantity: charged,\n tierBreakdown,\n };\n};\n\n// =============================================================================\n// computeInvoice — pure\n// =============================================================================\n\nexport type ComputeInvoiceInput = {\n plan: Plan;\n tenant: string;\n period: InvoicePeriod;\n /** Metered numbers keyed by the same names as `plan.pricedDimensions`. */\n usage: Record<string, number>;\n /** Override the plan's currency (e.g. for tenant-local invoicing). */\n currency?: string;\n};\n\nexport const computeInvoice = ({\n plan,\n tenant,\n period,\n usage,\n currency,\n}: ComputeInvoiceInput): Invoice => {\n const rounding = plan.rounding ?? \"truncate\";\n const lineItems: LineItem[] = [];\n\n if (plan.basePriceMicros !== undefined && plan.basePriceMicros > 0) {\n lineItems.push({\n amountMicros: plan.basePriceMicros,\n chargedQuantity: 1,\n key: \"base\",\n label: `${plan.name} base fee`,\n quantity: 1,\n });\n }\n\n for (const [key, dim] of Object.entries(plan.pricedDimensions)) {\n const quantity = usage[key] ?? 0;\n if (!Number.isFinite(quantity) || quantity < 0) continue;\n const result = computeDimension({ dim, quantity, rounding });\n if (result.amountMicros === 0 && result.chargedQuantity === 0) continue;\n const item: LineItem = {\n amountMicros: result.amountMicros,\n chargedQuantity: result.chargedQuantity,\n key,\n label: dim.label ?? key,\n quantity,\n };\n if (dim.freeTier !== undefined) item.freeTier = dim.freeTier;\n if (result.tierBreakdown !== undefined && result.tierBreakdown.length > 0) {\n item.tierBreakdown = result.tierBreakdown;\n }\n lineItems.push(item);\n }\n\n let totalMicros = lineItems.reduce((sum, item) => sum + item.amountMicros, 0);\n\n const floor = plan.minimumChargeMicros ?? 0;\n if (floor > 0 && totalMicros < floor) {\n const gap = floor - totalMicros;\n lineItems.push({\n amountMicros: gap,\n chargedQuantity: 1,\n key: \"minimum-charge-adjustment\",\n label: \"Minimum charge adjustment\",\n quantity: 1,\n });\n totalMicros = floor;\n }\n\n const invoice: Invoice = {\n currency: currency ?? plan.currency ?? \"usd\",\n lineItems,\n period,\n plan: plan.name,\n tenant,\n totalMicros,\n totalUnits: totalMicros / 1_000_000,\n };\n if (plan.metadata !== undefined) invoice.metadata = plan.metadata;\n return invoice;\n};\n\n// =============================================================================\n// Display helpers\n// =============================================================================\n\n/**\n * Format an integer micros amount as a human currency string. Pure\n * — no Intl side effects. For locales / advanced formatting, pipe\n * through `Intl.NumberFormat` yourself.\n */\nexport const formatMicros = (\n amount: Micros,\n currency: string,\n { minorUnits = 2 }: { minorUnits?: number } = {},\n): string => {\n const sign = amount < 0 ? \"-\" : \"\";\n const abs = Math.abs(amount);\n const wholeMicrosPerMinor = 10 ** (6 - minorUnits);\n const minorTotal = Math.round(abs / wholeMicrosPerMinor);\n const divisor = 10 ** minorUnits;\n const whole = Math.trunc(minorTotal / divisor);\n const upper = currency.toUpperCase();\n if (minorUnits === 0) return `${sign}${whole} ${upper}`;\n const fraction = minorTotal % divisor;\n const fractionStr = fraction.toString().padStart(minorUnits, \"0\");\n return `${sign}${whole}.${fractionStr} ${upper}`;\n};\n\n// Provider balances — read upstream vendors' real balance/quota/spend (the\n// inverse of computeInvoice). See ./balances.\nexport {\n readProviderBalances,\n type ProviderBalance,\n type ProviderBalanceConfig,\n type ProviderBalanceKind,\n type ProviderBalanceStatus,\n type BraveUsageSnapshot,\n type EmbeddingUsageSnapshot,\n} from \"./balances\";\n"
|
|
6
|
+
"/**\n * @absolutejs/billing — cost-model substrate for the AbsoluteJS PaaS.\n *\n * Two pieces:\n *\n * - `createPlan(...)` — declarative pricing config: optional flat\n * base fee + per-dimension unit prices, with optional graduated\n * tiers and free-tier allowances per dimension.\n *\n * - `computeInvoice({ plan, period, tenant, usage, currency? })`\n * — pure function that turns a `@absolutejs/metering`-shaped\n * `Usage` snapshot (or any record of metered numbers) into an\n * `Invoice` of line items + total. All money math is done in\n * integer **micros** (1 micro = 1/1,000,000 of a currency unit\n * — the same denomination Stripe uses internally) so float\n * drift is structurally impossible.\n *\n * Invoice sinks (push to Stripe, post to QuickBooks, mail a PDF)\n * live OUTSIDE this package, in `@absolutejs/billing-adapters/*`.\n * Keeping the substrate pure means the control plane can preview\n * invoices, run dry-run \"would-charge\" projections, and replay an\n * old usage snapshot through a new plan without touching any\n * vendor SDK.\n */\n\n// =============================================================================\n// Money primitives\n// =============================================================================\n\n/**\n * An integer amount in a plan's sub-units. 1,000,000 (micros) by default, but\n * a plan may choose a finer denomination — see `Plan.denomination`.\n */\nexport type Micros = number;\n\n/** Sub-units per currency unit when a plan does not say otherwise. */\nexport const DEFAULT_DENOMINATION = 1_000_000;\n\n/** Nanos — the denomination token-priced APIs need. */\nexport const NANO_DENOMINATION = 1_000_000_000;\n\n/**\n * Round a fractional micros value to an integer. The substrate uses\n * **truncation** (banker's-style would surprise callers expecting\n * \"$0.0009 → $0.00\" not \"$0.0009 → $0.001\"). Plans override per-plan.\n */\nexport type Rounding = \"truncate\" | \"round-half-up\";\n\nconst roundMicros = (value: number, rounding: Rounding): Micros => {\n if (rounding === \"truncate\") return Math.trunc(value);\n return Math.round(value);\n};\n\n// =============================================================================\n// Pricing config\n// =============================================================================\n\n/**\n * One step in a graduated-tier price table. `upTo` is the inclusive\n * upper bound (in metered units, NOT micros) for this band.\n * `perUnitMicros` is what the customer pays per single metered unit\n * within this band. The last entry must have `upTo: Infinity` to\n * cover any overflow.\n */\nexport type PricingTier = {\n upTo: number;\n perUnitMicros: number;\n};\n\n/**\n * Per-dimension pricing. Three shapes:\n *\n * - Flat per-unit: `{ perUnitMicros: 200, unit: 1024 * 1024 }`\n * charges 200 micros ($0.0002) per MB of usage.\n *\n * - Tiered: `{ tiers: [...], unit: 1 }` charges per the first\n * matching `PricingTier` band.\n *\n * - Custom: `{ price: (quantity) => micros, unit: 1 }` — escape\n * hatch for surge / caps / non-monotonic pricing. The substrate\n * stays pure; you ship whatever function you want.\n *\n * `freeTier` is subtracted from the metered quantity BEFORE pricing\n * — the conventional \"first N units free\" rule.\n *\n * `unit` is the metered-unit denominator: 1 means \"price per single\n * metered unit\", 1024*1024 means \"price per MB when quantity is in\n * bytes.\" Default 1.\n *\n * `label` overrides the line-item display name.\n */\nexport type PricedDimension = {\n label?: string;\n freeTier?: number;\n unit?: number;\n} & (\n | { perUnitMicros: number; tiers?: never; price?: never }\n | { tiers: PricingTier[]; perUnitMicros?: never; price?: never }\n | {\n price: (chargedQuantity: number) => Micros;\n perUnitMicros?: never;\n tiers?: never;\n }\n);\n\nexport type Plan = {\n /** Human label for the invoice (`'pro'`, `'enterprise'`, etc.). */\n name: string;\n /**\n * Sub-units per currency unit. Defaults to 1,000,000 (micros).\n *\n * Micros are too coarse for token-priced APIs: at $0.16 per million\n * embedding tokens a five-token call costs $0.0000008, which truncates to\n * ZERO in micros — so a plan priced in micros systematically under-bills\n * its cheapest calls. Set `1_000_000_000` to price in nanos, or any other\n * power of ten the vendor's rate card needs. Every `*Micros` field on the\n * plan and the invoice is denominated in these sub-units.\n */\n denomination?: number;\n /** Optional flat base fee charged once per invoice period. */\n basePriceMicros?: Micros;\n /**\n * Dimensions priced from usage. Keys must match keys on the\n * `usage` record passed to `computeInvoice`. Anything not listed\n * is ignored.\n */\n pricedDimensions: Record<string, PricedDimension>;\n /** Default currency for invoices generated from this plan. */\n currency?: string;\n /** Rounding strategy applied per line item. Default `'truncate'`. */\n rounding?: Rounding;\n /**\n * Minimum charge (in micros) — if the computed total is below\n * this floor, the invoice total is raised to the floor and a\n * single `'minimum-charge-adjustment'` line item captures the\n * difference. Defaults to 0 (no floor).\n */\n minimumChargeMicros?: Micros;\n /** Arbitrary plan-level metadata that flows through to invoices. */\n metadata?: Record<string, string>;\n};\n\nexport const createPlan = (plan: Plan): Plan => {\n for (const [key, dim] of Object.entries(plan.pricedDimensions)) {\n if (dim.tiers !== undefined) {\n if (dim.tiers.length === 0) {\n throw new Error(`billing: dimension '${key}' has no tiers`);\n }\n const last = dim.tiers[dim.tiers.length - 1];\n if (last !== undefined && Number.isFinite(last.upTo)) {\n throw new Error(\n `billing: dimension '${key}' final tier must have upTo: Infinity`,\n );\n }\n let prev = 0;\n for (let i = 0; i < dim.tiers.length; i += 1) {\n const tier = dim.tiers[i]!;\n if (tier.upTo < prev) {\n throw new Error(\n `billing: dimension '${key}' tier #${i} upTo (${tier.upTo}) must be >= previous (${prev})`,\n );\n }\n prev = tier.upTo;\n }\n }\n }\n return plan;\n};\n\n// =============================================================================\n// Invoice shape\n// =============================================================================\n\nexport type LineItem = {\n /**\n * Stable key for the line item. For priced dimensions it's the\n * usage-record key (`'requests'`, `'cpuMs'`, etc.). For the base\n * fee it's `'base'`. For minimum-charge top-up it's\n * `'minimum-charge-adjustment'`.\n */\n key: string;\n /** Human-readable label. */\n label: string;\n /** Metered units BEFORE applying free tier. 0 for the base fee. */\n quantity: number;\n /** Metered units AFTER applying free tier (what's actually charged). */\n chargedQuantity: number;\n /** Free-tier units subtracted from `quantity`. */\n freeTier?: number;\n /** Charge for this line in integer micros. */\n amountMicros: Micros;\n /**\n * Tier-by-tier breakdown when graduated pricing was used. Each\n * entry: `{ tierIndex, unitsInTier, perUnitMicros, amountMicros }`.\n */\n tierBreakdown?: Array<{\n tierIndex: number;\n unitsInTier: number;\n perUnitMicros: number;\n amountMicros: Micros;\n }>;\n};\n\nexport type InvoicePeriod = {\n /** Inclusive period start (`Date.now()` ms). */\n start: number;\n /** Exclusive period end. */\n end: number;\n};\n\nexport type Invoice = {\n tenant: string;\n plan: string;\n currency: string;\n /** Sub-units per currency unit these amounts are in (default micros). */\n denomination: number;\n period: InvoicePeriod;\n lineItems: LineItem[];\n /** Sum of all `lineItems[].amountMicros`. */\n totalMicros: Micros;\n /** Convenience: `totalMicros / 1_000_000` as a number. */\n totalUnits: number;\n /** Plan-level metadata copied through unchanged. */\n metadata?: Record<string, string>;\n};\n\n// =============================================================================\n// Pricing math\n// =============================================================================\n\ntype ComputeDimensionInput = {\n quantity: number;\n dim: PricedDimension;\n rounding: Rounding;\n};\n\ntype ComputeDimensionResult = {\n amountMicros: Micros;\n chargedQuantity: number;\n tierBreakdown?: LineItem[\"tierBreakdown\"];\n};\n\nconst computeDimension = ({\n quantity,\n dim,\n rounding,\n}: ComputeDimensionInput): ComputeDimensionResult => {\n const free = dim.freeTier ?? 0;\n const charged = Math.max(0, quantity - free);\n const unit = dim.unit ?? 1;\n const chargedUnits = unit === 1 ? charged : charged / unit;\n\n if (dim.perUnitMicros !== undefined) {\n const amountMicros = roundMicros(\n chargedUnits * dim.perUnitMicros,\n rounding,\n );\n return { amountMicros, chargedQuantity: charged };\n }\n\n if (dim.price !== undefined) {\n const amountMicros = roundMicros(dim.price(charged), rounding);\n return { amountMicros, chargedQuantity: charged };\n }\n\n // Tiered pricing — walk tiers, allocate chargedUnits into bands.\n const tierBreakdown: NonNullable<LineItem[\"tierBreakdown\"]> = [];\n let remaining = chargedUnits;\n let bandFloor = 0;\n let totalMicros = 0;\n for (let i = 0; i < dim.tiers!.length && remaining > 0; i += 1) {\n const tier = dim.tiers![i]!;\n const bandWidth = tier.upTo - bandFloor;\n const unitsInTier = Math.min(remaining, bandWidth);\n if (unitsInTier > 0) {\n const tierMicros = roundMicros(\n unitsInTier * tier.perUnitMicros,\n rounding,\n );\n tierBreakdown.push({\n amountMicros: tierMicros,\n perUnitMicros: tier.perUnitMicros,\n tierIndex: i,\n unitsInTier,\n });\n totalMicros += tierMicros;\n }\n remaining -= unitsInTier;\n bandFloor = tier.upTo;\n }\n return {\n amountMicros: totalMicros,\n chargedQuantity: charged,\n tierBreakdown,\n };\n};\n\n// =============================================================================\n// computeInvoice — pure\n// =============================================================================\n\nexport type ComputeInvoiceInput = {\n plan: Plan;\n tenant: string;\n period: InvoicePeriod;\n /** Metered numbers keyed by the same names as `plan.pricedDimensions`. */\n usage: Record<string, number>;\n /** Override the plan's currency (e.g. for tenant-local invoicing). */\n currency?: string;\n};\n\nexport const computeInvoice = ({\n plan,\n tenant,\n period,\n usage,\n currency,\n}: ComputeInvoiceInput): Invoice => {\n const rounding = plan.rounding ?? \"truncate\";\n const lineItems: LineItem[] = [];\n\n if (plan.basePriceMicros !== undefined && plan.basePriceMicros > 0) {\n lineItems.push({\n amountMicros: plan.basePriceMicros,\n chargedQuantity: 1,\n key: \"base\",\n label: `${plan.name} base fee`,\n quantity: 1,\n });\n }\n\n for (const [key, dim] of Object.entries(plan.pricedDimensions)) {\n const quantity = usage[key] ?? 0;\n if (!Number.isFinite(quantity) || quantity < 0) continue;\n const result = computeDimension({ dim, quantity, rounding });\n if (result.amountMicros === 0 && result.chargedQuantity === 0) continue;\n const item: LineItem = {\n amountMicros: result.amountMicros,\n chargedQuantity: result.chargedQuantity,\n key,\n label: dim.label ?? key,\n quantity,\n };\n if (dim.freeTier !== undefined) item.freeTier = dim.freeTier;\n if (result.tierBreakdown !== undefined && result.tierBreakdown.length > 0) {\n item.tierBreakdown = result.tierBreakdown;\n }\n lineItems.push(item);\n }\n\n let totalMicros = lineItems.reduce((sum, item) => sum + item.amountMicros, 0);\n\n const floor = plan.minimumChargeMicros ?? 0;\n if (floor > 0 && totalMicros < floor) {\n const gap = floor - totalMicros;\n lineItems.push({\n amountMicros: gap,\n chargedQuantity: 1,\n key: \"minimum-charge-adjustment\",\n label: \"Minimum charge adjustment\",\n quantity: 1,\n });\n totalMicros = floor;\n }\n\n const denomination = plan.denomination ?? DEFAULT_DENOMINATION;\n const invoice: Invoice = {\n currency: currency ?? plan.currency ?? \"usd\",\n denomination,\n lineItems,\n period,\n plan: plan.name,\n tenant,\n totalMicros,\n totalUnits: totalMicros / denomination,\n };\n if (plan.metadata !== undefined) invoice.metadata = plan.metadata;\n return invoice;\n};\n\n// =============================================================================\n// Display helpers\n// =============================================================================\n\n/**\n * Format an integer micros amount as a human currency string. Pure\n * — no Intl side effects. For locales / advanced formatting, pipe\n * through `Intl.NumberFormat` yourself.\n */\nexport const formatMicros = (\n amount: Micros,\n currency: string,\n {\n denomination = DEFAULT_DENOMINATION,\n minorUnits = 2,\n }: { denomination?: number; minorUnits?: number } = {},\n): string => {\n const sign = amount < 0 ? \"-\" : \"\";\n const abs = Math.abs(amount);\n const exponent = Math.round(Math.log10(denomination));\n const wholeMicrosPerMinor = 10 ** (exponent - minorUnits);\n const minorTotal = Math.round(abs / wholeMicrosPerMinor);\n const divisor = 10 ** minorUnits;\n const whole = Math.trunc(minorTotal / divisor);\n const upper = currency.toUpperCase();\n if (minorUnits === 0) return `${sign}${whole} ${upper}`;\n const fraction = minorTotal % divisor;\n const fractionStr = fraction.toString().padStart(minorUnits, \"0\");\n return `${sign}${whole}.${fractionStr} ${upper}`;\n};\n\n// Provider balances — read upstream vendors' real balance/quota/spend (the\n// inverse of computeInvoice). See ./balances.\nexport {\n readProviderBalances,\n type ProviderBalance,\n type ProviderBalanceConfig,\n type ProviderBalanceKind,\n type ProviderBalanceStatus,\n type BraveUsageSnapshot,\n type EmbeddingUsageSnapshot,\n} from \"./balances\";\n"
|
|
7
7
|
],
|
|
8
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAoBA,IAAM,mBAAmB;AAGzB,IAAM,wBAAwB;AAC9B,IAAM,gBAAgB;AACtB,IAAM,aAAa;AACnB,IAAM,UAAU;AAChB,IAAM,WAAW;AACjB,IAAM,mBAAmB;AAkEzB,IAAM,SAAS,MAAM,IAAI,KAAK,EAAE,YAAY;AAE5C,IAAM,WAAW,CAAC,UAChB,OAAO,UAAU,YAAY,UAAU;AAEzC,IAAM,WAAW,CAAC,UAChB,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,IAAI,QAAQ;AAEhE,IAAM,MAAM,CAAC,WAAmB,IAAI,OAAO,QAAQ,CAAC;AAEpD,IAAM,UAAU,CAAC,UAAkB;AAAA,EACjC,IAAI,SAAS;AAAA,IAAS,OAAO,IAAI,QA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AAI,MAAM;AAAA,EAC3B,MAAM,WAAW,KAAK,MAAM,KAAK,MAAM,YAAY,CAAC;AAAA,EACpD,MAAM,sBAAsB,OAAO,WAAW;AAAA,EAC9C,MAAM,aAAa,KAAK,MAAM,MAAM,mBAAmB;AAAA,EACvD,MAAM,UAAU,MAAM;AAAA,EACtB,MAAM,QAAQ,KAAK,MAAM,aAAa,OAAO;AAAA,EAC7C,MAAM,QAAQ,SAAS,YAAY;AAAA,EACnC,IAAI,eAAe;AAAA,IAAG,OAAO,GAAG,OAAO,SAAS;AAAA,EAChD,MAAM,WAAW,aAAa;AAAA,EAC9B,MAAM,cAAc,SAAS,SAAS,EAAE,SAAS,YAAY,GAAG;AAAA,EAChE,OAAO,GAAG,OAAO,SAAS,eAAe;AAAA;",
|
|
9
|
+
"debugId": "D50F9899A29C0CBA64756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/dist/ledger.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** One priced, metered event, ready to persist. */
|
|
2
|
+
export type LedgerEntry = {
|
|
3
|
+
/** Charge in integer sub-units of the plan's denomination (see
|
|
4
|
+
* `Plan.denomination`) — never a float. */
|
|
5
|
+
amount: number;
|
|
6
|
+
/** What the customer is billed in the product's own unit. */
|
|
7
|
+
credits: number;
|
|
8
|
+
/** Product-level grouping ("chat", "voice"), not the vendor's. */
|
|
9
|
+
feature?: string | null;
|
|
10
|
+
model?: string;
|
|
11
|
+
/** "llm" | "tts" | "embedding" | whatever the product meters. */
|
|
12
|
+
operation: string;
|
|
13
|
+
provider: string;
|
|
14
|
+
/** Idempotency handle for at-least-once callers. */
|
|
15
|
+
requestId?: string;
|
|
16
|
+
/** Null for system/background work with nobody to bill. */
|
|
17
|
+
tenant?: string | null;
|
|
18
|
+
};
|
|
19
|
+
export type LedgerCommit = {
|
|
20
|
+
/** Append the row AND debit `entry.credits` from the tenant's balance in a
|
|
21
|
+
* single atomic unit. Called with tenant null for unattributed work, where
|
|
22
|
+
* there is nothing to debit. */
|
|
23
|
+
commit: (entry: LedgerEntry) => Promise<void>;
|
|
24
|
+
/** Fold the entry into a derived daily aggregate. Best-effort by contract:
|
|
25
|
+
* this module swallows its failures. */
|
|
26
|
+
rollup?: (entry: LedgerEntry) => Promise<void>;
|
|
27
|
+
/** Total sub-units charged since `since`, for the spend cap. */
|
|
28
|
+
spentSince?: (since: Date) => Promise<number>;
|
|
29
|
+
};
|
|
30
|
+
export type UsageLedgerOptions = {
|
|
31
|
+
/** Sub-units per credit. A peg of 1_000 with micros means 1 credit =
|
|
32
|
+
* $0.001. Required for credit conversion; omit to bill in raw amounts. */
|
|
33
|
+
creditPegSubUnits?: number;
|
|
34
|
+
/** Reported when a rollup fails. The charge already succeeded. */
|
|
35
|
+
onRollupError?: (error: unknown, entry: LedgerEntry) => void;
|
|
36
|
+
store: LedgerCommit;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Credits for a charge. Ceiling, not rounding: a product that sells credits
|
|
40
|
+
* must never hand out a fraction it cannot deduct, and rounding down means
|
|
41
|
+
* the smallest calls are free — which is how a "cheap" endpoint becomes an
|
|
42
|
+
* unmetered one.
|
|
43
|
+
*/
|
|
44
|
+
export declare const creditsFor: (amount: number, pegSubUnits: number) => number;
|
|
45
|
+
export type UsageLedger = {
|
|
46
|
+
/** Price-agnostic: hand it an amount already in sub-units. Returns what was
|
|
47
|
+
* written, including the credits it derived. */
|
|
48
|
+
record: (entry: Omit<LedgerEntry, "credits"> & {
|
|
49
|
+
credits?: number;
|
|
50
|
+
}) => Promise<LedgerEntry>;
|
|
51
|
+
/** True once spend since `since` reaches `capSubUnits`. Fails OPEN — a
|
|
52
|
+
* broken cap must not take down paid features, and the per-provider
|
|
53
|
+
* budgets are still in front. */
|
|
54
|
+
overCap: (capSubUnits: number, since: Date) => Promise<boolean>;
|
|
55
|
+
};
|
|
56
|
+
export declare const createUsageLedger: (options: UsageLedgerOptions) => UsageLedger;
|
package/dist/ledger.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/ledger.ts
|
|
18
|
+
var creditsFor = (amount, pegSubUnits) => {
|
|
19
|
+
if (pegSubUnits <= 0)
|
|
20
|
+
return 0;
|
|
21
|
+
return Math.ceil(amount / pegSubUnits);
|
|
22
|
+
};
|
|
23
|
+
var createUsageLedger = (options) => {
|
|
24
|
+
const { creditPegSubUnits, onRollupError, store } = options;
|
|
25
|
+
return {
|
|
26
|
+
overCap: async (capSubUnits, since) => {
|
|
27
|
+
if (!store.spentSince)
|
|
28
|
+
return false;
|
|
29
|
+
try {
|
|
30
|
+
return await store.spentSince(since) >= capSubUnits;
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
record: async (input) => {
|
|
36
|
+
const credits = input.credits ?? (creditPegSubUnits === undefined ? 0 : creditsFor(input.amount, creditPegSubUnits));
|
|
37
|
+
const entry = { ...input, credits };
|
|
38
|
+
await store.commit(entry);
|
|
39
|
+
if (store.rollup) {
|
|
40
|
+
await store.rollup(entry).catch((error) => {
|
|
41
|
+
onRollupError?.(error, entry);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return entry;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
creditsFor,
|
|
50
|
+
createUsageLedger
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//# debugId=0E1FB04E107E92A964756E2164756E21
|
|
54
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/ledger.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"// The layer between a meter and an invoice: every metered event gets priced,\n// converted to the customer's credit unit, written to an append-only ledger,\n// debited against a balance, and folded into a daily rollup that a spend cap\n// can read. Every app billing a metered API rebuilds this, and each one\n// rediscovers the same three traps:\n//\n// - Money in floats. Summing float ledger rows drifts; one real deployment\n// lost 2 cents across 25,740 rows before anyone noticed. Amounts here are\n// integer sub-units (see Plan.denomination) and are only ever added.\n// - The debit and the ledger row diverging. If the row is written but the\n// balance is not debited, a customer gets free usage; the reverse\n// over-charges. They must land together, so the store commits them as one\n// unit — this module never splits them.\n// - The rollup being treated as truth. It is a derived index, rebuildable\n// from the ledger, so a rollup failure must never fail the charge.\n//\n// Storage stays the host's business (Postgres, ClickHouse, anything). This\n// owns the policy and the arithmetic, which is the part that is identical\n// everywhere — and the part that is worth getting wrong only once.\n\n/** One priced, metered event, ready to persist. */\nexport type LedgerEntry = {\n /** Charge in integer sub-units of the plan's denomination (see\n * `Plan.denomination`) — never a float. */\n amount: number;\n /** What the customer is billed in the product's own unit. */\n credits: number;\n /** Product-level grouping (\"chat\", \"voice\"), not the vendor's. */\n feature?: string | null;\n model?: string;\n /** \"llm\" | \"tts\" | \"embedding\" | whatever the product meters. */\n operation: string;\n provider: string;\n /** Idempotency handle for at-least-once callers. */\n requestId?: string;\n /** Null for system/background work with nobody to bill. */\n tenant?: string | null;\n};\n\nexport type LedgerCommit = {\n /** Append the row AND debit `entry.credits` from the tenant's balance in a\n * single atomic unit. Called with tenant null for unattributed work, where\n * there is nothing to debit. */\n commit: (entry: LedgerEntry) => Promise<void>;\n /** Fold the entry into a derived daily aggregate. Best-effort by contract:\n * this module swallows its failures. */\n rollup?: (entry: LedgerEntry) => Promise<void>;\n /** Total sub-units charged since `since`, for the spend cap. */\n spentSince?: (since: Date) => Promise<number>;\n};\n\nexport type UsageLedgerOptions = {\n /** Sub-units per credit. A peg of 1_000 with micros means 1 credit =\n * $0.001. Required for credit conversion; omit to bill in raw amounts. */\n creditPegSubUnits?: number;\n /** Reported when a rollup fails. The charge already succeeded. */\n onRollupError?: (error: unknown, entry: LedgerEntry) => void;\n store: LedgerCommit;\n};\n\n/**\n * Credits for a charge. Ceiling, not rounding: a product that sells credits\n * must never hand out a fraction it cannot deduct, and rounding down means\n * the smallest calls are free — which is how a \"cheap\" endpoint becomes an\n * unmetered one.\n */\nexport const creditsFor = (amount: number, pegSubUnits: number) => {\n if (pegSubUnits <= 0) return 0;\n\n return Math.ceil(amount / pegSubUnits);\n};\n\nexport type UsageLedger = {\n /** Price-agnostic: hand it an amount already in sub-units. Returns what was\n * written, including the credits it derived. */\n record: (\n entry: Omit<LedgerEntry, \"credits\"> & { credits?: number },\n ) => Promise<LedgerEntry>;\n /** True once spend since `since` reaches `capSubUnits`. Fails OPEN — a\n * broken cap must not take down paid features, and the per-provider\n * budgets are still in front. */\n overCap: (capSubUnits: number, since: Date) => Promise<boolean>;\n};\n\nexport const createUsageLedger = (options: UsageLedgerOptions): UsageLedger => {\n const { creditPegSubUnits, onRollupError, store } = options;\n\n return {\n overCap: async (capSubUnits, since) => {\n if (!store.spentSince) return false;\n try {\n return (await store.spentSince(since)) >= capSubUnits;\n } catch {\n return false;\n }\n },\n record: async (input) => {\n const credits =\n input.credits ??\n (creditPegSubUnits === undefined\n ? 0\n : creditsFor(input.amount, creditPegSubUnits));\n const entry: LedgerEntry = { ...input, credits };\n // The charge is the thing that must not be lost; it is awaited and its\n // failure propagates to the caller.\n await store.commit(entry);\n // The rollup is a derived index — rebuildable from the ledger — so its\n // failure is reported, never raised.\n if (store.rollup) {\n await store.rollup(entry).catch((error: unknown) => {\n onRollupError?.(error, entry);\n });\n }\n\n return entry;\n },\n };\n};\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAkEO,IAAM,aAAa,CAAC,QAAgB,gBAAwB;AAAA,EACjE,IAAI,eAAe;AAAA,IAAG,OAAO;AAAA,EAE7B,OAAO,KAAK,KAAK,SAAS,WAAW;AAAA;AAehC,IAAM,oBAAoB,CAAC,YAA6C;AAAA,EAC7E,QAAQ,mBAAmB,eAAe,UAAU;AAAA,EAEpD,OAAO;AAAA,IACL,SAAS,OAAO,aAAa,UAAU;AAAA,MACrC,IAAI,CAAC,MAAM;AAAA,QAAY,OAAO;AAAA,MAC9B,IAAI;AAAA,QACF,OAAQ,MAAM,MAAM,WAAW,KAAK,KAAM;AAAA,QAC1C,MAAM;AAAA,QACN,OAAO;AAAA;AAAA;AAAA,IAGX,QAAQ,OAAO,UAAU;AAAA,MACvB,MAAM,UACJ,MAAM,YACL,sBAAsB,YACnB,IACA,WAAW,MAAM,QAAQ,iBAAiB;AAAA,MAChD,MAAM,QAAqB,KAAK,OAAO,QAAQ;AAAA,MAG/C,MAAM,MAAM,OAAO,KAAK;AAAA,MAGxB,IAAI,MAAM,QAAQ;AAAA,QAChB,MAAM,MAAM,OAAO,KAAK,EAAE,MAAM,CAAC,UAAmB;AAAA,UAClD,gBAAgB,OAAO,KAAK;AAAA,SAC7B;AAAA,MACH;AAAA,MAEA,OAAO;AAAA;AAAA,EAEX;AAAA;",
|
|
8
|
+
"debugId": "0E1FB04E107E92A964756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/dist/manifest.js
CHANGED
|
@@ -380,6 +380,8 @@ var readProviderBalances = async (config) => {
|
|
|
380
380
|
};
|
|
381
381
|
|
|
382
382
|
// src/index.ts
|
|
383
|
+
var DEFAULT_DENOMINATION = 1e6;
|
|
384
|
+
var NANO_DENOMINATION = 1e9;
|
|
383
385
|
var roundMicros = (value, rounding) => {
|
|
384
386
|
if (rounding === "truncate")
|
|
385
387
|
return Math.trunc(value);
|
|
@@ -503,23 +505,29 @@ var computeInvoice = ({
|
|
|
503
505
|
});
|
|
504
506
|
totalMicros = floor;
|
|
505
507
|
}
|
|
508
|
+
const denomination = plan.denomination ?? DEFAULT_DENOMINATION;
|
|
506
509
|
const invoice = {
|
|
507
510
|
currency: currency ?? plan.currency ?? "usd",
|
|
511
|
+
denomination,
|
|
508
512
|
lineItems,
|
|
509
513
|
period,
|
|
510
514
|
plan: plan.name,
|
|
511
515
|
tenant,
|
|
512
516
|
totalMicros,
|
|
513
|
-
totalUnits: totalMicros /
|
|
517
|
+
totalUnits: totalMicros / denomination
|
|
514
518
|
};
|
|
515
519
|
if (plan.metadata !== undefined)
|
|
516
520
|
invoice.metadata = plan.metadata;
|
|
517
521
|
return invoice;
|
|
518
522
|
};
|
|
519
|
-
var formatMicros = (amount, currency, {
|
|
523
|
+
var formatMicros = (amount, currency, {
|
|
524
|
+
denomination = DEFAULT_DENOMINATION,
|
|
525
|
+
minorUnits = 2
|
|
526
|
+
} = {}) => {
|
|
520
527
|
const sign = amount < 0 ? "-" : "";
|
|
521
528
|
const abs = Math.abs(amount);
|
|
522
|
-
const
|
|
529
|
+
const exponent = Math.round(Math.log10(denomination));
|
|
530
|
+
const wholeMicrosPerMinor = 10 ** (exponent - minorUnits);
|
|
523
531
|
const minorTotal = Math.round(abs / wholeMicrosPerMinor);
|
|
524
532
|
const divisor = 10 ** minorUnits;
|
|
525
533
|
const whole = Math.trunc(minorTotal / divisor);
|
|
@@ -3447,5 +3455,5 @@ export {
|
|
|
3447
3455
|
manifest
|
|
3448
3456
|
};
|
|
3449
3457
|
|
|
3450
|
-
//# debugId=
|
|
3458
|
+
//# debugId=FB56701BA9401D2A64756E2164756E21
|
|
3451
3459
|
//# sourceMappingURL=manifest.js.map
|