Open History to see the last commit
1// Fake CLIProxyAPI management API serving the Limits scenarios in scenarios.json.
2// Usage: node hub.mjs <state.json> [port]. state.json is {"scenario": "A", "n0": <epoch ms>};
3// it is re-read on every request, so edit it and press Refresh limits to switch scenarios.
4import * as NodeFS from "node:fs";
5import * as NodeHttp from "node:http";
6
7const [statePath, port = "18517"] = process.argv.slice(2);
8if (!statePath) throw new Error("usage: node hub.mjs <state.json> [port]");
9const scenarios = JSON.parse(
10 NodeFS.readFileSync(new URL("./scenarios.json", import.meta.url), "utf8"),
11);
12const MINUTE = 60_000;
13
14// A window is [usedPercent, minutesFromN0 | null]; null is unknown reset timing.
15const at = (n0, minutes) => (minutes === null ? null : n0 + minutes * MINUTE);
16const usage = (account, n0) => {
17 const [session, weekly, fable] = account.windows;
18 if (account.provider === "codex") {
19 const window = ([used, minutes], seconds) => ({
20 used_percent: used,
21 reset_at: at(n0, minutes) === null ? null : Math.floor(at(n0, minutes) / 1000),
22 limit_window_seconds: seconds,
23 });
24 return {
25 plan_type: "pro",
26 rate_limit: {
27 primary_window: window(session, 18_000),
28 secondary_window: window(weekly, 604_800),
29 },
30 };
31 }
32 const window = ([used, minutes]) => ({
33 utilization: used,
34 resets_at: at(n0, minutes) === null ? null : new Date(at(n0, minutes)).toISOString(),
35 });
36 return {
37 five_hour: window(session),
38 seven_day: window(weekly),
39 limits: fable
40 ? [
41 {
42 kind: "weekly_scoped",
43 percent: fable[0],
44 resets_at: window(fable).resets_at,
45 scope: { model: { display_name: "Fable" } },
46 },
47 ]
48 : [],
49 };
50};
51
52const json = (response, body) => {
53 response.writeHead(200, { "content-type": "application/json" });
54 response.end(JSON.stringify(body));
55};
56
57NodeHttp.createServer((request, response) => {
58 let raw = "";
59 request.on("data", (chunk) => (raw += chunk));
60 request.on("end", () => {
61 const { scenario, n0 } = JSON.parse(NodeFS.readFileSync(statePath, "utf8"));
62 const accounts = scenarios[scenario];
63 if (!accounts) return json(response, { files: [] });
64 if (request.url.endsWith("/v0/management/auth-files")) {
65 return json(response, {
66 files: Object.entries(accounts).map(([id, account]) => ({
67 id,
68 auth_index: id,
69 provider: account.provider,
70 ...(account.email ? { email: account.email } : {}),
71 })),
72 });
73 }
74 if (request.url.endsWith("/v0/management/api-call")) {
75 const call = JSON.parse(raw);
76 const account = accounts[call.auth_index];
77 if (!account) return json(response, { status_code: 404, body: "{}" });
78 if (call.url.endsWith("/rate-limit-reset-credits")) {
79 const credits = Array.from({ length: account.credits ?? 0 }, (_, index) => ({
80 id: `${call.auth_index}-credit-${index}`,
81 status: "available",
82 reset_type: "codex_rate_limits",
83 expires_at: new Date(n0 + 3 * 24 * 60 * MINUTE).toISOString(),
84 }));
85 return json(response, { status_code: 200, body: JSON.stringify({ credits }) });
86 }
87 return json(response, { status_code: 200, body: JSON.stringify(usage(account, n0)) });
88 }
89 response.writeHead(404).end();
90 });
91}).listen(Number(port), "127.0.0.1", () => console.log(`usage limits hub on ${port}`));