@absolutejs/billing 0.3.1 → 0.4.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.
@@ -53,6 +53,17 @@ export type BraveUsageSnapshot = {
53
53
  * "unconfigured" tile (so a dashboard can show every provider it cares about and
54
54
  * label the ones missing a key).
55
55
  */
56
+ /** Embedding consumption the host measured itself, against the plan's cap.
57
+ * Vector vendors meter tokens per month and simply refuse once spent, so
58
+ * this is the number that predicts an outage. */
59
+ export type EmbeddingUsageSnapshot = {
60
+ capturedAt: string;
61
+ /** True when the provider is currently refusing embeddings. */
62
+ exhausted?: boolean;
63
+ monthlyTokenLimit: number;
64
+ resetDate?: string | null;
65
+ tokensUsed: number;
66
+ };
56
67
  export type ProviderBalanceConfig = {
57
68
  anthropic?: {
58
69
  adminKey: string;
@@ -61,6 +72,9 @@ export type ProviderBalanceConfig = {
61
72
  apiKey: string;
62
73
  };
63
74
  brave?: BraveUsageSnapshot | null;
75
+ /** Host-supplied embedding usage. Pinecone exposes no usage API to an
76
+ * ordinary key, so — like Brave — the app reports what it metered. */
77
+ pinecone?: EmbeddingUsageSnapshot | null;
64
78
  deepgram?: {
65
79
  apiKey: string;
66
80
  };
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
- /** Integer micros — 1,000,000 micros = 1 unit of the currency. */
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
- export { readProviderBalances, type ProviderBalance, type ProviderBalanceConfig, type ProviderBalanceKind, type ProviderBalanceStatus, type BraveUsageSnapshot, } from "./balances";
196
+ export { readProviderBalances, type ProviderBalance, type ProviderBalanceConfig, type ProviderBalanceKind, type ProviderBalanceStatus, type BraveUsageSnapshot, type EmbeddingUsageSnapshot, } from "./balances";
package/dist/index.js CHANGED
@@ -236,6 +236,33 @@ var apolloBalance = async (creds) => {
236
236
  return errored("apollo", "Apollo", `${error} \u2014 usage stats need the Apollo master key`);
237
237
  }
238
238
  };
239
+ var compactTokens = (tokens) => {
240
+ const MILLION2 = 1e6;
241
+ const THOUSAND2 = 1000;
242
+ if (tokens >= MILLION2)
243
+ return `${(tokens / MILLION2).toFixed(1)}M`;
244
+ return `${Math.round(tokens / THOUSAND2)}k`;
245
+ };
246
+ var pineconeBalance = (snap) => {
247
+ if (!snap) {
248
+ return unconfigured("pinecone", "Pinecone embeddings", "No embedding usage reported yet \u2014 the host supplies this from its own metering.");
249
+ }
250
+ const remaining = Math.max(0, snap.monthlyTokenLimit - snap.tokensUsed);
251
+ const quota = {
252
+ ...base("pinecone", "Pinecone embeddings"),
253
+ checkedAt: snap.capturedAt,
254
+ detail: snap.exhausted ? `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens \u2014 quota spent, embeddings refused` : `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens this month`,
255
+ kind: "quota",
256
+ limit: snap.monthlyTokenLimit,
257
+ note: "Counted from the host's own metering \u2014 Pinecone exposes no usage API.",
258
+ remaining,
259
+ resetDate: snap.resetDate ?? null,
260
+ status: "ok",
261
+ unit: "tokens",
262
+ used: snap.tokensUsed
263
+ };
264
+ return quota;
265
+ };
239
266
  var braveBalance = (snap) => {
240
267
  if (!snap) {
241
268
  return unconfigured("brave", "Brave Search", "No usage captured yet \u2014 appears after the next web search.");
@@ -342,6 +369,9 @@ var readProviderBalances = async (config) => {
342
369
  jobs.push(apolloBalance(config.apollo));
343
370
  if ("brave" in config)
344
371
  jobs.push(Promise.resolve(braveBalance(config.brave)));
372
+ if ("pinecone" in config) {
373
+ jobs.push(Promise.resolve(pineconeBalance(config.pinecone)));
374
+ }
345
375
  if (config.anthropic)
346
376
  jobs.push(anthropicBalance(config.anthropic));
347
377
  if (config.openai)
@@ -350,6 +380,8 @@ var readProviderBalances = async (config) => {
350
380
  };
351
381
 
352
382
  // src/index.ts
383
+ var DEFAULT_DENOMINATION = 1e6;
384
+ var NANO_DENOMINATION = 1e9;
353
385
  var roundMicros = (value, rounding) => {
354
386
  if (rounding === "truncate")
355
387
  return Math.trunc(value);
@@ -473,23 +505,29 @@ var computeInvoice = ({
473
505
  });
474
506
  totalMicros = floor;
475
507
  }
508
+ const denomination = plan.denomination ?? DEFAULT_DENOMINATION;
476
509
  const invoice = {
477
510
  currency: currency ?? plan.currency ?? "usd",
511
+ denomination,
478
512
  lineItems,
479
513
  period,
480
514
  plan: plan.name,
481
515
  tenant,
482
516
  totalMicros,
483
- totalUnits: totalMicros / 1e6
517
+ totalUnits: totalMicros / denomination
484
518
  };
485
519
  if (plan.metadata !== undefined)
486
520
  invoice.metadata = plan.metadata;
487
521
  return invoice;
488
522
  };
489
- var formatMicros = (amount, currency, { minorUnits = 2 } = {}) => {
523
+ var formatMicros = (amount, currency, {
524
+ denomination = DEFAULT_DENOMINATION,
525
+ minorUnits = 2
526
+ } = {}) => {
490
527
  const sign = amount < 0 ? "-" : "";
491
528
  const abs = Math.abs(amount);
492
- const wholeMicrosPerMinor = 10 ** (6 - minorUnits);
529
+ const exponent = Math.round(Math.log10(denomination));
530
+ const wholeMicrosPerMinor = 10 ** (exponent - minorUnits);
493
531
  const minorTotal = Math.round(abs / wholeMicrosPerMinor);
494
532
  const divisor = 10 ** minorUnits;
495
533
  const whole = Math.trunc(minorTotal / divisor);
@@ -504,8 +542,10 @@ export {
504
542
  readProviderBalances,
505
543
  formatMicros,
506
544
  createPlan,
507
- computeInvoice
545
+ computeInvoice,
546
+ NANO_DENOMINATION,
547
+ DEFAULT_DENOMINATION
508
548
  };
509
549
 
510
- //# debugId=C0D9028B844C873764756E2164756E21
550
+ //# debugId=D50F9899A29C0CBA64756E2164756E21
511
551
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -2,10 +2,10 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/balances.ts", "../src/index.ts"],
4
4
  "sourcesContent": [
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 */\nexport type ProviderBalanceConfig = {\n anthropic?: { adminKey: string };\n apollo?: { apiKey: string };\n brave?: BraveUsageSnapshot | 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// --- 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 (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} from \"./balances\";\n"
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/**\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,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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/manifest.js CHANGED
@@ -236,6 +236,33 @@ var apolloBalance = async (creds) => {
236
236
  return errored("apollo", "Apollo", `${error} \u2014 usage stats need the Apollo master key`);
237
237
  }
238
238
  };
239
+ var compactTokens = (tokens) => {
240
+ const MILLION2 = 1e6;
241
+ const THOUSAND2 = 1000;
242
+ if (tokens >= MILLION2)
243
+ return `${(tokens / MILLION2).toFixed(1)}M`;
244
+ return `${Math.round(tokens / THOUSAND2)}k`;
245
+ };
246
+ var pineconeBalance = (snap) => {
247
+ if (!snap) {
248
+ return unconfigured("pinecone", "Pinecone embeddings", "No embedding usage reported yet \u2014 the host supplies this from its own metering.");
249
+ }
250
+ const remaining = Math.max(0, snap.monthlyTokenLimit - snap.tokensUsed);
251
+ const quota = {
252
+ ...base("pinecone", "Pinecone embeddings"),
253
+ checkedAt: snap.capturedAt,
254
+ detail: snap.exhausted ? `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens \u2014 quota spent, embeddings refused` : `${compactTokens(snap.tokensUsed)} / ${compactTokens(snap.monthlyTokenLimit)} tokens this month`,
255
+ kind: "quota",
256
+ limit: snap.monthlyTokenLimit,
257
+ note: "Counted from the host's own metering \u2014 Pinecone exposes no usage API.",
258
+ remaining,
259
+ resetDate: snap.resetDate ?? null,
260
+ status: "ok",
261
+ unit: "tokens",
262
+ used: snap.tokensUsed
263
+ };
264
+ return quota;
265
+ };
239
266
  var braveBalance = (snap) => {
240
267
  if (!snap) {
241
268
  return unconfigured("brave", "Brave Search", "No usage captured yet \u2014 appears after the next web search.");
@@ -342,6 +369,9 @@ var readProviderBalances = async (config) => {
342
369
  jobs.push(apolloBalance(config.apollo));
343
370
  if ("brave" in config)
344
371
  jobs.push(Promise.resolve(braveBalance(config.brave)));
372
+ if ("pinecone" in config) {
373
+ jobs.push(Promise.resolve(pineconeBalance(config.pinecone)));
374
+ }
345
375
  if (config.anthropic)
346
376
  jobs.push(anthropicBalance(config.anthropic));
347
377
  if (config.openai)
@@ -350,6 +380,8 @@ var readProviderBalances = async (config) => {
350
380
  };
351
381
 
352
382
  // src/index.ts
383
+ var DEFAULT_DENOMINATION = 1e6;
384
+ var NANO_DENOMINATION = 1e9;
353
385
  var roundMicros = (value, rounding) => {
354
386
  if (rounding === "truncate")
355
387
  return Math.trunc(value);
@@ -473,23 +505,29 @@ var computeInvoice = ({
473
505
  });
474
506
  totalMicros = floor;
475
507
  }
508
+ const denomination = plan.denomination ?? DEFAULT_DENOMINATION;
476
509
  const invoice = {
477
510
  currency: currency ?? plan.currency ?? "usd",
511
+ denomination,
478
512
  lineItems,
479
513
  period,
480
514
  plan: plan.name,
481
515
  tenant,
482
516
  totalMicros,
483
- totalUnits: totalMicros / 1e6
517
+ totalUnits: totalMicros / denomination
484
518
  };
485
519
  if (plan.metadata !== undefined)
486
520
  invoice.metadata = plan.metadata;
487
521
  return invoice;
488
522
  };
489
- var formatMicros = (amount, currency, { minorUnits = 2 } = {}) => {
523
+ var formatMicros = (amount, currency, {
524
+ denomination = DEFAULT_DENOMINATION,
525
+ minorUnits = 2
526
+ } = {}) => {
490
527
  const sign = amount < 0 ? "-" : "";
491
528
  const abs = Math.abs(amount);
492
- const wholeMicrosPerMinor = 10 ** (6 - minorUnits);
529
+ const exponent = Math.round(Math.log10(denomination));
530
+ const wholeMicrosPerMinor = 10 ** (exponent - minorUnits);
493
531
  const minorTotal = Math.round(abs / wholeMicrosPerMinor);
494
532
  const divisor = 10 ** minorUnits;
495
533
  const whole = Math.trunc(minorTotal / divisor);
@@ -3417,5 +3455,5 @@ export {
3417
3455
  manifest
3418
3456
  };
3419
3457
 
3420
- //# debugId=02E8D489549DA13F64756E2164756E21
3458
+ //# debugId=FB56701BA9401D2A64756E2164756E21
3421
3459
  //# sourceMappingURL=manifest.js.map