Wire new routes; identity, summarize-url, dashboard, admin

This commit is contained in:
Keysat
2026-06-13 13:36:30 -05:00
parent 04dcf86fa4
commit 318c6c4b81
20 changed files with 12407 additions and 499 deletions
+32 -4
View File
@@ -11,18 +11,34 @@ import { getTierQuotas } from "../config.js";
export async function envelope({
result = null,
installId,
// License is optional but recommended — without it, balance lookups
// route to the install-keyed row even for paid users, which would
// briefly underreport their balance after a commitCredit landed on
// their license-keyed row. Routes pass it through from resolveLicense.
license = null,
// Explicit ledger key override (cloud `user:<id>` path). Takes
// precedence over (installId, license) when present.
creditKey = null,
tier,
creditCharged = 0,
}) {
const quota = await getTierQuotas();
const row = await getOrCreateRow(installId);
const row = await getOrCreateRow({ installId, license, creditKey });
// tier_snapshot on the row was just updated by commitCredit; if no
// credit was committed (free reuse via job_id) it still reflects
// the last-known tier for this install, which is fine.
const balance = computeRemaining(row, quota);
return {
result,
credits_remaining: balance.remaining, // null = unlimited (Max)
// `total` = tier allotment + purchased top-up. Recap renders this
// as the headline number on its credits pill. `remaining` alone
// wouldn't reflect purchased credits at all — so a buyer who
// just bought 5 credits and had 0 tier credits left would still
// see "0 relay credits" until their tier renewed.
credits_remaining: balance.total, // null = unlimited (Max)
// Breakdown for clients that want to display it.
tier_remaining: balance.remaining,
purchased_balance: balance.purchased,
tier,
credit_charged: creditCharged,
};
@@ -35,15 +51,25 @@ export async function envelope({
export async function errorEnvelope({
error,
installId,
license = null,
creditKey = null,
tier = "core",
statusHint = 500,
}) {
let creditsRemaining = null;
let tierRemaining = null;
let purchased = 0;
try {
const quota = await getTierQuotas();
const row = await getOrCreateRow(installId || "unknown");
const row = await getOrCreateRow({
installId: creditKey ? null : installId || "unknown",
license,
creditKey,
});
const balance = computeRemaining(row, quota);
creditsRemaining = balance.remaining;
creditsRemaining = balance.total;
tierRemaining = balance.remaining;
purchased = balance.purchased;
} catch {}
return {
statusHint,
@@ -51,6 +77,8 @@ export async function errorEnvelope({
result: null,
error: typeof error === "string" ? error : error?.message || "unknown_error",
credits_remaining: creditsRemaining,
tier_remaining: tierRemaining,
purchased_balance: purchased,
tier,
credit_charged: 0,
},