feat(freebuff): enhance Freebucks handling and pricing logic across services

This commit is contained in:
MUH. IQRAM BAHRING
2026-09-07 22:26:28 +08:00
parent eedfdf982b
commit 9c17c5f96c
9 changed files with 352 additions and 19 deletions
+87 -1
View File
@@ -15,6 +15,22 @@
* includes the active session's own 1.0-unit reservation. `limit` can be
* raised by referral/streak rewards (entitlementBreakdown.base + referral +
* streak).
*
* Freebucks-metered accounts (2026-09-02+) get NO pool rows: the server sends
* a `freebucks` block instead — { balance, daily:{limit,spent,remaining,resetAt},
* wallet:{balance,...}, monthly?:{limitUsd,spentUsd,remainingUsd,resetAt},
* planId, prices:{model→Freebucks/hr}, priceNotices?, priceChanges? }. The
* daily pool replaces rateLimitsByModel, so the tracker folds it in by
* replicating the pool snapshot under every priced model id, exactly as the
* official picker does.
*
* Pricing is SERVER-AUTHORITATIVE and nothing here hardcodes a number: each
* row carries the live `prices[model]` (plus its `priceNotices` tagline), and
* `applyFreebucksPriceChanges` folds the server's announced `priceChanges`
* schedule into the map — a promo (e.g. Solar Pro 4's Labor Day 0-Freebucks
* offer) expires and reverts on the server's timeline with no client release,
* mirroring upstream common/src/util/freebuff-price-changes.ts. A compact
* account summary (`freebucks`) rides the response for the card header.
*/
import REGISTRY from "../../providers/registry/index.js";
@@ -26,6 +42,28 @@ const MODEL_LABELS = Object.fromEntries(
(freebuffRegistry.models || []).map((m) => [m.id, m.name]),
);
// Fold the server's announced price schedule into the live price map —
// mirrors upstream applyFreebucksPriceChanges: due changes override the price
// AND its picker tagline, and drop off the pending list.
function applyFreebucksPriceChanges(info) {
const now = Date.now();
const due = (info?.priceChanges || []).filter((c) => Date.parse(c.at) <= now);
if (due.length === 0) return info;
const prices = { ...(info.prices || {}) };
const priceNotices = { ...(info.priceNotices || {}) };
for (const change of due.sort((a, b) => Date.parse(a.at) - Date.parse(b.at))) {
if (prices[change.modelId] === undefined) continue;
prices[change.modelId] = change.price;
priceNotices[change.modelId] = change.tagline;
}
return {
...info,
prices,
priceNotices,
priceChanges: (info.priceChanges || []).filter((c) => Date.parse(c.at) > now),
};
}
function sessionUrl() {
return U("freebuff").url;
}
@@ -102,11 +140,59 @@ export async function getFreebuffUsage(accessToken, providerSpecificData, proxyO
};
}
// Freebucks meter: the daily Freebucks pool is the quota for metered
// accounts, replicated under each priced model like the picker's rings.
// Prices come off the wire (prices + priceChanges folded in); each row
// carries its own price/tagline, and a compact summary rides the response
// for the account header in the UI.
let freebucksSummary = null;
const rawFreebucks = data.freebucks;
if (rawFreebucks && rawFreebucks.daily && typeof rawFreebucks.daily === "object") {
const freebucks = applyFreebucksPriceChanges(rawFreebucks);
const spent = Number(freebucks.daily.spent);
const limit = Number(freebucks.daily.limit);
for (const [model, price] of Object.entries(freebucks.prices || {})) {
quotas[model] = {
used: Number.isFinite(spent) ? spent : 0,
total: Number.isFinite(limit) ? limit : 0,
resetAt: freebucks.daily.resetAt || null,
unlimited: false,
recurring: true,
price: Number.isFinite(Number(price)) ? Number(price) : undefined,
...(freebucks.priceNotices?.[model] ? { priceNote: freebucks.priceNotices[model] } : {}),
...(MODEL_LABELS[model] ? { displayName: MODEL_LABELS[model] } : {}),
};
}
freebucksSummary = {
balance: Number.isFinite(Number(freebucks.balance)) ? Number(freebucks.balance) : null,
daily: {
limit: Number.isFinite(limit) ? limit : 0,
spent: Number.isFinite(spent) ? spent : 0,
remaining: Number.isFinite(Number(freebucks.daily.remaining)) ? Number(freebucks.daily.remaining) : 0,
resetAt: freebucks.daily.resetAt || null,
},
wallet: {
balance: Number.isFinite(Number(freebucks.wallet?.balance)) ? Number(freebucks.wallet.balance) : 0,
},
...(freebucks.monthly && Number.isFinite(Number(freebucks.monthly.remainingUsd))
? {
monthly: {
remainingUsd: Number(freebucks.monthly.remainingUsd),
limitUsd: Number.isFinite(Number(freebucks.monthly.limitUsd)) ? Number(freebucks.monthly.limitUsd) : null,
resetAt: freebucks.monthly.resetAt || null,
},
}
: {}),
};
}
const plan = data.accessTier === "limited" ? "Freebuff (Limited)" : "Freebuff";
if (Object.keys(quotas).length === 0) {
return { plan, message: "Freebuff connected. No session quota to report right now." };
}
return { plan, quotas };
const out = { plan, quotas };
if (freebucksSummary) out.freebucks = freebucksSummary;
return out;
} catch (error) {
return { message: `Freebuff usage error: ${error.message}` };
}