feat(billing): make metering formula admin-configurable
Memind CI / Test, build, and release guards (pull_request) Failing after 1m29s

Allow margin/FX/cost-mode knobs to be overridden via h5_billing_admin_config so memind_adm can adjust DeepSeek billing without editing env.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-08-06 15:29:16 +08:00
parent c44017eccd
commit 391ba0b705
11 changed files with 491 additions and 12 deletions
+17 -4
View File
@@ -35,7 +35,7 @@ export function estimateAccumulatedCostUsd(tokenStateRaw, estimateConfig = loadC
export function enrichTokenStateForBilling(
tokenStateRaw,
{ sessionCost = null } = {},
{ sessionCost = null, estimateConfig = null } = {},
env = process.env,
) {
const state = normalizeTokenState(tokenStateRaw);
@@ -49,7 +49,10 @@ export function enrichTokenStateForBilling(
return state;
}
const estimatedUsd = estimateAccumulatedCostUsd(state, loadCostEstimateConfig(env));
const estimatedUsd = estimateAccumulatedCostUsd(
state,
estimateConfig ?? loadCostEstimateConfig(env),
);
if (estimatedUsd != null) {
return { ...state, accumulatedCost: estimatedUsd };
}
@@ -86,7 +89,13 @@ async function resolveSessionCostPayload(sessionId, fetchSession, fetchSessionCo
export async function resolveBillingTokenState(
tokenStateRaw,
{ sessionId = null, fetchSession = null, fetchSessionCostFromPg = null } = {},
{
sessionId = null,
fetchSession = null,
fetchSessionCostFromPg = null,
estimateConfig = null,
loadEstimateConfig = null,
} = {},
env = process.env,
) {
const state = normalizeTokenState(tokenStateRaw);
@@ -96,5 +105,9 @@ export async function resolveBillingTokenState(
fetchSessionCostFromPg,
env,
);
return enrichTokenStateForBilling(state, { sessionCost }, env);
const resolvedEstimateConfig =
estimateConfig
?? (typeof loadEstimateConfig === 'function' ? await loadEstimateConfig() : null)
?? loadCostEstimateConfig(env);
return enrichTokenStateForBilling(state, { sessionCost, estimateConfig: resolvedEstimateConfig }, env);
}