mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:57:09 +01:00
* feat(ai-budget): keep a fifth of each day's free pool for people on their first week Pacing keeps the day from being spent before lunch, but not by evening: there is no clawback, so whoever signs up at 21:00 can meet a pool the vendors have already refused — a first answer that never comes, which is the moment a person decides Loki is broken. lib/ai-budget/newcomer-reserve.ts: returning accounts together stop at 80% of the day's capacity; an account under seven days old may draw on the rest, still under its own fair share. getDayUsage now also returns the day's total (same query as the active count). The gate reads the account's age only once the total reaches the held part, and fails open if it cannot; a paid plan with allowance left is not held back. The refusal says what the held part is for, when the day starts over, and the ways past it; the budget screen reads the same rule, so it never shows "12 left" to someone the gate will refuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CG9dupSVqoXCCqKfBCfd9B * fix(billing): payments stay off until switched on, whatever pay URLs are set Live after #1293: every paid plan's button on /pricing read "Pay in Bitcoin" and went to an OrangeCat product page that answers 404. The three ORANGECAT_PAY_URL_* were set on the box before the passes existed; while the tiers had no price that was invisible, and setting the prices turned the dead links on — skipping the reservation flow they were meant to fall back to, and showing "Buy" in Settings → Billing too. orangeCatPayUrl now also requires LOKI_PAYMENTS_OPEN=1. Opening payments is a decision (the company registered, the passes live), so it is a switch, not an inference from a URL. Until then every paid plan's button reserves, as designed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CG9dupSVqoXCCqKfBCfd9B --------- Co-authored-by: Claude <noreply@anthropic.com>
148 lines
5.2 KiB
TypeScript
148 lines
5.2 KiB
TypeScript
/**
|
|
* Paid plans, up to the moment of paying.
|
|
*
|
|
* Prices were set on 2026-10-17 (CHF 15 / 40 / 90) while payments stay off
|
|
* until the company is registered. These pin what that must mean:
|
|
*
|
|
* - past the daily free share, a paid plan's month still admits the turn,
|
|
* and a lapsed or past-due plan's does not;
|
|
* - Free has no allowance, and a failed allowance read never admits or
|
|
* breaks anything — the share's refusal stands;
|
|
* - the refusal offers the plans as well as the person's own key;
|
|
* - only priced paid plans can be reserved, and while no OrangeCat pass
|
|
* exists the plan's button reserves rather than pays.
|
|
*
|
|
* Pure: no database (the gate's reads are injected).
|
|
*/
|
|
import { checkAiBudget } from "@/lib/ai-budget/gate";
|
|
import { allowanceLeft } from "@/lib/ai-budget/plan-allowance";
|
|
import { planAiTokensMonthly, PLAN_LIMITS } from "@/lib/plan";
|
|
import { planCheckout, reservablePlan } from "@/lib/plan-reservation";
|
|
import { PRICING_PLANS } from "@/config/plans";
|
|
|
|
let passed = 0;
|
|
function check(label: string, cond: boolean, detail?: unknown): void {
|
|
if (!cond)
|
|
throw new Error(`✗ ${label}${detail === undefined ? "" : ` — ${JSON.stringify(detail)}`}`);
|
|
passed++;
|
|
console.log(` ✓ ${label}`);
|
|
}
|
|
|
|
const NOW = new Date("2026-10-17T12:00:00Z");
|
|
const spentShare = async () => ({ userSpentTokens: 900_000, activeUsers: 1, userTurns: 100 });
|
|
|
|
async function main() {
|
|
check(
|
|
"prices are the founder's: 15 / 40 / 90",
|
|
PRICING_PLANS.map((p) => p.priceMonthly).join() === "0,15,40,90",
|
|
);
|
|
check("Free has no monthly allowance", planAiTokensMonthly("free") === 0);
|
|
check(
|
|
"each paid plan carries more than the one below it",
|
|
PLAN_LIMITS.aiAnswersMonthly.personal < PLAN_LIMITS.aiAnswersMonthly.pro &&
|
|
PLAN_LIMITS.aiAnswersMonthly.pro < PLAN_LIMITS.aiAnswersMonthly.team,
|
|
);
|
|
|
|
const active = { plan: "personal" as const, planStatus: "active" as const, planExpiresAt: null };
|
|
check(
|
|
"an active plan's allowance is what is left of its month",
|
|
allowanceLeft(active, 1_000_000, NOW) === planAiTokensMonthly("personal") - 1_000_000,
|
|
);
|
|
check("a spent month has nothing left", allowanceLeft(active, 99_000_000, NOW) === 0);
|
|
check(
|
|
"a lapsed pass has no allowance",
|
|
allowanceLeft({ ...active, planExpiresAt: new Date(NOW.getTime() - 1) }, 0, NOW) === 0,
|
|
);
|
|
check(
|
|
"a past-due plan has no allowance",
|
|
allowanceLeft({ ...active, planStatus: "past_due" }, 0, NOW) === 0,
|
|
);
|
|
|
|
const refused = await checkAiBudget("u1", NOW, {
|
|
timeZone: "UTC",
|
|
capacity: () => 100_000,
|
|
readUsage: spentShare,
|
|
planAllowance: async () => 0,
|
|
});
|
|
check("on Free, a spent share refuses", !refused.allowed);
|
|
check(
|
|
"…and the refusal offers the person's own key and the plans",
|
|
!refused.allowed &&
|
|
/own model key/.test(refused.message) &&
|
|
/\(\/pricing\)/.test(refused.message),
|
|
refused,
|
|
);
|
|
|
|
const admitted = await checkAiBudget("u1", NOW, {
|
|
timeZone: "UTC",
|
|
capacity: () => 100_000,
|
|
readUsage: spentShare,
|
|
planAllowance: async () => 5_000_000,
|
|
});
|
|
check("a paid plan with allowance left is admitted past the share", admitted.allowed);
|
|
|
|
const broken = await checkAiBudget("u1", NOW, {
|
|
timeZone: "UTC",
|
|
capacity: () => 100_000,
|
|
readUsage: spentShare,
|
|
planAllowance: async () => {
|
|
throw new Error("connection refused");
|
|
},
|
|
});
|
|
check("an allowance read that fails leaves the share's refusal standing", !broken.allowed);
|
|
|
|
let asked = false;
|
|
await checkAiBudget("u1", NOW, {
|
|
timeZone: "UTC",
|
|
capacity: () => 100_000,
|
|
readUsage: async () => ({ userSpentTokens: 0, activeUsers: 1, userTurns: 0 }),
|
|
planAllowance: async () => {
|
|
asked = true;
|
|
return 0;
|
|
},
|
|
});
|
|
check("a turn inside the share never reads the plan", !asked);
|
|
|
|
check(
|
|
"only priced paid plans can be reserved",
|
|
reservablePlan("personal") === "personal" &&
|
|
reservablePlan("free") === null &&
|
|
reservablePlan("enterprise") === null,
|
|
);
|
|
const personal = PRICING_PLANS.find((p) => p.key === "personal")!;
|
|
const saved = {
|
|
url: process.env.ORANGECAT_PAY_URL_PERSONAL,
|
|
open: process.env.LOKI_PAYMENTS_OPEN,
|
|
};
|
|
delete process.env.ORANGECAT_PAY_URL_PERSONAL;
|
|
delete process.env.LOKI_PAYMENTS_OPEN;
|
|
check("with no OrangeCat pass, the button reserves", planCheckout(personal).kind === "reserve");
|
|
// Live 2026-10-17: the URLs were on the box before the passes existed, and
|
|
// every paid plan's button went to an OrangeCat 404 instead of reserving.
|
|
process.env.ORANGECAT_PAY_URL_PERSONAL = "https://orangecat.ch/pay/personal";
|
|
check(
|
|
"a pay URL alone does not open payments — the button still reserves",
|
|
planCheckout(personal).kind === "reserve",
|
|
);
|
|
process.env.LOKI_PAYMENTS_OPEN = "1";
|
|
const pay = planCheckout(personal);
|
|
check(
|
|
"once payments are switched on, the same button pays",
|
|
pay.kind === "pay" && pay.url.endsWith("/personal"),
|
|
);
|
|
for (const [k, v] of [
|
|
["ORANGECAT_PAY_URL_PERSONAL", saved.url],
|
|
["LOKI_PAYMENTS_OPEN", saved.open],
|
|
] as const) {
|
|
if (v === undefined) delete process.env[k];
|
|
else process.env[k] = v;
|
|
}
|
|
|
|
console.log(`\nplan-allowance: ${passed} checks passed`);
|
|
}
|
|
|
|
void main().catch((e) => {
|
|
console.error(e instanceof Error ? e.message : e);
|
|
process.exit(1);
|
|
});
|