billing-index-usage.mjs
73 lines 2.8 KB
Raw
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor ⚠ breaking 1 day ago
1 /**
2 * After a successful bridge index, record embedding_input_tokens for hosted billing telemetry.
3 * Enforcement by token cap is documented in HOSTED-CREDITS-DESIGN.md (future: pre-flight or post-hoc policy).
4 */
5 import { billingShadowLogEnabled } from './billing-constants.mjs';
6 import { defaultUserRecord, normalizeBillingUser, effectiveMonthlyIndexingTokensIncluded } from './billing-logic.mjs';
7 import { mutateBillingDb } from './billing-store.mjs';
8
9 /**
10 * @param {string|null} uid
11 * @param {number} statusCode
12 * @param {string} bodyText
13 */
14 export async function recordIndexingTokensAfterBridgeIndex(uid, statusCode, bodyText) {
15 if (!uid || statusCode !== 200 || typeof bodyText !== 'string') return;
16 let j;
17 try {
18 j = JSON.parse(bodyText);
19 } catch {
20 return;
21 }
22 const t = j.embedding_input_tokens;
23 if (typeof t !== 'number' || !Number.isFinite(t) || t < 0) return;
24 const tokens = Math.floor(t);
25 if (tokens === 0) return;
26
27 await mutateBillingDb((db) => {
28 const u = db.users[uid] || defaultUserRecord(uid);
29 normalizeBillingUser(u);
30 if (!db.users[uid]) db.users[uid] = u;
31
32 // Increment both counters atomically in one write to avoid a race with runBillingGate.
33 // A separate write from the billing middleware risks reading a stale Blob snapshot and
34 // overwriting the job counter back to 0 on Netlify's eventually-consistent store.
35 u.monthly_index_jobs_used =
36 Math.max(0, Math.floor(Number(u.monthly_index_jobs_used) || 0)) + 1;
37
38 const prevTokensUsed = Math.max(0, Math.floor(Number(u.monthly_indexing_tokens_used) || 0));
39 const newTokensUsed = prevTokensUsed + tokens;
40 u.monthly_indexing_tokens_used = newTokensUsed;
41
42 // Deduct from pack balance: only the marginal tokens that exceed the monthly included allotment.
43 // This keeps pack_indexing_tokens_balance accurate for display even before BILLING_ENFORCE=true.
44 // When the billing period resets (monthly_indexing_tokens_used → 0), the overflow restarts
45 // from zero so the pack balance is not double-charged.
46 const monthlyIncluded = effectiveMonthlyIndexingTokensIncluded(u);
47 if (monthlyIncluded !== null) {
48 const prevOverflow = Math.max(0, prevTokensUsed - monthlyIncluded);
49 const newOverflow = Math.max(0, newTokensUsed - monthlyIncluded);
50 const packDeduction = newOverflow - prevOverflow;
51 if (packDeduction > 0) {
52 u.pack_indexing_tokens_balance = Math.max(
53 0,
54 Math.floor(Number(u.pack_indexing_tokens_balance) || 0) - packDeduction,
55 );
56 }
57 }
58 });
59
60 if (billingShadowLogEnabled()) {
61 console.log(
62 JSON.stringify({
63 type: 'knowtation_billing_shadow',
64 ts: new Date().toISOString(),
65 user_id: uid,
66 operation: 'index',
67 phase: 'post_index',
68 embedding_input_tokens: tokens,
69 path: '/api/v1/index',
70 }),
71 );
72 }
73 }
File History 2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor 1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6 docs: accept Calendar Events v0 spec with Phase 0 security … Human 2 days ago