The best way to control Claude Code, Codex and more from any computer or phone

main
mjs 124 lines 4.62 KB
Raw
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.
4 import * as NodeFS from "node:fs";
5 import * as NodeHttp from "node:http";
6
7 const [statePath, port = "18517"] = process.argv.slice(2);
8 if (!statePath) throw new Error("usage: node hub.mjs <state.json> [port]");
9 const scenarios = JSON.parse(
10 NodeFS.readFileSync(new URL("./scenarios.json", import.meta.url), "utf8"),
11 );
12 const MINUTE = 60_000;
13
14 // A window is [usedPercent, minutesFromN0 | null]; null is unknown reset timing.
15 const at = (n0, minutes) => (minutes === null ? null : n0 + minutes * MINUTE);
16 const iso = (n0, minutes) =>
17 minutes === null ? undefined : new Date(at(n0, minutes)).toISOString();
18 const usage = (account, n0) => {
19 const [session, weekly, fable] = account.windows;
20 if (account.provider === "xai") {
21 return {
22 config: {
23 creditUsagePercent: session[0],
24 currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", end: iso(n0, session[1]) },
25 },
26 };
27 }
28 if (account.provider === "antigravity") {
29 const bucket = (bucketId, window, [used, minutes]) => ({
30 bucketId,
31 window,
32 remainingFraction: 1 - used / 100,
33 resetTime: iso(n0, minutes),
34 });
35 return {
36 buckets: [bucket("gemini-5h", "5h", session), bucket("gemini-weekly", "weekly", weekly)],
37 };
38 }
39 if (account.provider === "codex") {
40 const window = ([used, minutes], seconds) => ({
41 used_percent: used,
42 reset_at: at(n0, minutes) === null ? null : Math.floor(at(n0, minutes) / 1000),
43 limit_window_seconds: seconds,
44 });
45 return {
46 plan_type: "pro",
47 rate_limit: {
48 primary_window: window(session, 18_000),
49 secondary_window: window(weekly, 604_800),
50 },
51 };
52 }
53 const window = ([used, minutes]) => ({
54 utilization: used,
55 resets_at: at(n0, minutes) === null ? null : new Date(at(n0, minutes)).toISOString(),
56 });
57 return {
58 five_hour: window(session),
59 seven_day: window(weekly),
60 limits: fable
61 ? [
62 {
63 kind: "weekly_scoped",
64 percent: fable[0],
65 resets_at: window(fable).resets_at,
66 scope: { model: { display_name: "Fable" } },
67 },
68 ]
69 : [],
70 };
71 };
72
73 const json = (response, body) => {
74 response.writeHead(200, { "content-type": "application/json" });
75 response.end(JSON.stringify(body));
76 };
77
78 NodeHttp.createServer((request, response) => {
79 let raw = "";
80 request.on("data", (chunk) => (raw += chunk));
81 request.on("end", () => {
82 const { scenario, n0 } = JSON.parse(NodeFS.readFileSync(statePath, "utf8"));
83 const accounts = scenarios[scenario];
84 if (!accounts) return json(response, { files: [] });
85 if (request.url.endsWith("/v0/management/auth-files")) {
86 return json(response, {
87 files: Object.entries(accounts).map(([id, account]) => ({
88 id,
89 auth_index: id,
90 provider: account.provider,
91 ...(account.email ? { email: account.email } : {}),
92 ...(account.project_id ? { project_id: account.project_id } : {}),
93 })),
94 });
95 }
96 if (request.url.endsWith("/v0/management/api-call")) {
97 const call = JSON.parse(raw);
98 const account = accounts[call.auth_index];
99 if (!account) return json(response, { status_code: 404, body: "{}" });
100 // "refused" fails every upstream call; "malformed" answers the usage call with the wrong shape.
101 if (account.state === "refused") {
102 return json(response, { status_code: 403, body: JSON.stringify({ error: "refused" }) });
103 }
104 if (call.url.endsWith("/userinfo")) {
105 return json(response, { status_code: 200, body: JSON.stringify({ id: call.auth_index }) });
106 }
107 if (call.url.endsWith("/rate-limit-reset-credits")) {
108 const credits = Array.from({ length: account.credits ?? 0 }, (_, index) => ({
109 id: `${call.auth_index}-credit-${index}`,
110 status: "available",
111 reset_type: "codex_rate_limits",
112 expires_at: new Date(n0 + 3 * 24 * 60 * MINUTE).toISOString(),
113 }));
114 return json(response, { status_code: 200, body: JSON.stringify({ credits }) });
115 }
116 const body =
117 account.state === "malformed"
118 ? { config: "malformed", buckets: "malformed" }
119 : usage(account, n0);
120 return json(response, { status_code: 200, body: JSON.stringify(body) });
121 }
122 response.writeHead(404).end();
123 });
124 }).listen(Number(port), "127.0.0.1", () => console.log(`usage limits hub on ${port}`));