@kapybara/zerosub 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -0
- package/client/accounts-surface.tsx +592 -0
- package/client/add-account.tsx +351 -0
- package/client/commands.ts +112 -0
- package/client/fork-confirm.tsx +57 -0
- package/client/pills.ts +251 -0
- package/client/reset-confirm.tsx +138 -0
- package/client/settings-screen.tsx +120 -0
- package/client/store.ts +139 -0
- package/client/switch-row.tsx +103 -0
- package/client/ui.tsx +231 -0
- package/index.client.tsx +39 -0
- package/index.server.ts +91 -0
- package/package.json +58 -0
- package/paseo-plugin.json +7 -0
- package/server/adapter.ts +126 -0
- package/server/binaries.ts +63 -0
- package/server/claude-resets.ts +192 -0
- package/server/claude.ts +478 -0
- package/server/codex.ts +459 -0
- package/server/families.ts +78 -0
- package/server/handoff.ts +175 -0
- package/server/homes.ts +352 -0
- package/server/json-file.ts +39 -0
- package/server/limits.ts +213 -0
- package/server/machine.ts +17 -0
- package/server/modes.ts +34 -0
- package/server/paths.ts +37 -0
- package/server/process.ts +66 -0
- package/server/reopen.ts +95 -0
- package/server/routing.ts +131 -0
- package/server/service.ts +1692 -0
- package/server/state.ts +177 -0
- package/server/switch-guard.ts +28 -0
- package/server/usage.ts +68 -0
- package/shared/format.ts +99 -0
- package/shared/model.ts +150 -0
- package/shared/preferences.ts +32 -0
- package/shared/rpc.ts +120 -0
- package/shared/timeline.ts +42 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { chmod, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { readJson } from "./json-file";
|
|
4
|
+
import { dataDir, paseoHome } from "./paths";
|
|
5
|
+
|
|
6
|
+
export interface Command {
|
|
7
|
+
command: string;
|
|
8
|
+
/** Arguments that precede ours when the provider is configured with a wrapper command. */
|
|
9
|
+
prefix: string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const CACHE_MS = 60_000;
|
|
13
|
+
let cache: { at: number; value: Record<string, Command> } | null = null;
|
|
14
|
+
|
|
15
|
+
/** The CLI Paseo launches for a provider: `agents.providers.<id>.command` when set, else the name on PATH. */
|
|
16
|
+
export async function providerCommand(provider: "claude" | "codex"): Promise<Command> {
|
|
17
|
+
if (!cache || Date.now() - cache.at > CACHE_MS) {
|
|
18
|
+
const value: Record<string, Command> = {};
|
|
19
|
+
try {
|
|
20
|
+
const config = (await readJson(join(paseoHome(), "config.json"))) as
|
|
21
|
+
| { agents?: { providers?: Record<string, { command?: unknown }> } }
|
|
22
|
+
| undefined;
|
|
23
|
+
for (const id of ["claude", "codex"]) {
|
|
24
|
+
const command = config?.agents?.providers?.[id]?.command;
|
|
25
|
+
if (Array.isArray(command) && typeof command[0] === "string" && command.every((part) => typeof part === "string")) {
|
|
26
|
+
value[id] = { command: command[0], prefix: command.slice(1) as string[] };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// Unreadable config: fall back to PATH lookups.
|
|
31
|
+
}
|
|
32
|
+
cache = { at: Date.now(), value };
|
|
33
|
+
}
|
|
34
|
+
return cache.value[provider] ?? { command: provider, prefix: [] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let browserShim: Promise<string | null> | null = null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A `$BROWSER` stand-in that records the URL instead of opening a window on the daemon machine,
|
|
41
|
+
* so the sign-in link can be opened on whatever device the user is holding.
|
|
42
|
+
*/
|
|
43
|
+
export function browserCaptureScript(): Promise<string | null> {
|
|
44
|
+
if (process.platform === "win32") return Promise.resolve(null);
|
|
45
|
+
browserShim ??= (async () => {
|
|
46
|
+
const directory = join(dataDir(), "bin");
|
|
47
|
+
const path = join(directory, "capture-url");
|
|
48
|
+
try {
|
|
49
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
50
|
+
await writeFile(
|
|
51
|
+
path,
|
|
52
|
+
'#!/bin/sh\n[ -n "$ZEROSUB_BROWSER_OUT" ] && printf \'%s\\n\' "$1" > "$ZEROSUB_BROWSER_OUT"\nexit 0\n',
|
|
53
|
+
{ mode: 0o700 },
|
|
54
|
+
);
|
|
55
|
+
await chmod(path, 0o700);
|
|
56
|
+
return path;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.warn("[ZeroSub] could not create the browser shim", error instanceof Error ? error.message : error);
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
})();
|
|
62
|
+
return browserShim;
|
|
63
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { ResetOffer } from "../shared/model";
|
|
2
|
+
import type { RedeemReply } from "./adapter";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Claude's banked "limit reset" (Claude Code's hidden `/limit-reset`, program `cedar_ember`).
|
|
6
|
+
* Status arrives as a `cedar_ember` block in `/api/oauth/usage?cedar_ember=1`; redeeming posts the
|
|
7
|
+
* next grant to `/api/organizations/{org}/reset_rate_limits`. Field names follow Claude Code 2.1.x.
|
|
8
|
+
*/
|
|
9
|
+
export const CLAUDE_RESET_PROGRAM = "cedar_ember";
|
|
10
|
+
export const GRANT_ID = /^[a-z0-9_-]{1,40}$/;
|
|
11
|
+
|
|
12
|
+
const LIMIT_LABELS: Record<string, string> = {
|
|
13
|
+
five_hour: "5-hour",
|
|
14
|
+
seven_day: "weekly",
|
|
15
|
+
seven_day_opus: "Opus weekly",
|
|
16
|
+
seven_day_sonnet: "Sonnet weekly",
|
|
17
|
+
seven_day_overage_included: "Fable weekly",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const INELIGIBLE: Record<string, string> = {
|
|
21
|
+
config_off: "Limit resets aren't switched on for this account.",
|
|
22
|
+
tier: "This plan doesn't include limit resets.",
|
|
23
|
+
seat: "This seat type doesn't include limit resets.",
|
|
24
|
+
mobile: "Resets bought on mobile can only be used there.",
|
|
25
|
+
surface: "This reset can't be used from Claude Code.",
|
|
26
|
+
cli_version: "Update Claude Code to use this reset.",
|
|
27
|
+
no_grant: "No banked resets on this account.",
|
|
28
|
+
tenure: "This account isn't eligible for resets yet.",
|
|
29
|
+
other_experiment: "This account is in a different limit experiment.",
|
|
30
|
+
unavailable: "Limit resets aren't available right now.",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type Json = Record<string, unknown>;
|
|
34
|
+
|
|
35
|
+
function isObject(value: unknown): value is Json {
|
|
36
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function limitTypes(value: unknown): string[] {
|
|
40
|
+
return Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === "string") : [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function labels(types: readonly string[]): string[] {
|
|
44
|
+
return [...new Set(types.map((type) => LIMIT_LABELS[type] ?? "weekly"))];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function joinLabels(types: readonly string[]): string {
|
|
48
|
+
const names = labels(types);
|
|
49
|
+
if (names.length <= 1) return names[0] ?? "usage";
|
|
50
|
+
return `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isoTime(value: unknown): string | null {
|
|
54
|
+
if (typeof value !== "string") return null;
|
|
55
|
+
const at = Date.parse(value);
|
|
56
|
+
return Number.isFinite(at) ? new Date(at).toISOString() : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Grant {
|
|
60
|
+
id: string;
|
|
61
|
+
label: string;
|
|
62
|
+
resetsLeft: number;
|
|
63
|
+
endsAt: string | null;
|
|
64
|
+
clears: string[];
|
|
65
|
+
paused: boolean;
|
|
66
|
+
usableNow: boolean;
|
|
67
|
+
useRequiresLimit: boolean;
|
|
68
|
+
blocking: string[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseGrant(value: unknown): Grant | null {
|
|
72
|
+
if (!isObject(value) || typeof value.id !== "string" || !GRANT_ID.test(value.id)) return null;
|
|
73
|
+
const left = value.resets_left;
|
|
74
|
+
if (typeof left !== "number" || !Number.isInteger(left) || left < 0) return null;
|
|
75
|
+
return {
|
|
76
|
+
id: value.id,
|
|
77
|
+
label: typeof value.label === "string" ? value.label : "",
|
|
78
|
+
resetsLeft: left,
|
|
79
|
+
endsAt: isoTime(value.ends_at),
|
|
80
|
+
clears: limitTypes(value.clears),
|
|
81
|
+
paused: value.paused === true,
|
|
82
|
+
usableNow: value.usable_now === true,
|
|
83
|
+
useRequiresLimit: value.use_requires_limit !== false,
|
|
84
|
+
blocking: limitTypes(value.blocking),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ClaudeResetStatus {
|
|
89
|
+
offer: ResetOffer | null;
|
|
90
|
+
/** The grant the server will accept next; claims for any other grant are refused. */
|
|
91
|
+
nextGrantId: string | null;
|
|
92
|
+
/** Claude sees the account at a usage limit right now. */
|
|
93
|
+
atLimit: boolean;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Reads the `cedar_ember` block. `offer` is null when nothing is banked. */
|
|
97
|
+
export function parseClaudeResets(block: unknown, now: number = Date.now()): ClaudeResetStatus {
|
|
98
|
+
if (!isObject(block)) return { offer: null, nextGrantId: null, atLimit: false };
|
|
99
|
+
const atLimit = block.at_limit === true;
|
|
100
|
+
const grants = (Array.isArray(block.grants) ? block.grants : []).map(parseGrant).filter((grant): grant is Grant => grant !== null);
|
|
101
|
+
const available = grants.reduce((sum, grant) => sum + grant.resetsLeft, 0);
|
|
102
|
+
if (available === 0) return { offer: null, nextGrantId: null, atLimit };
|
|
103
|
+
|
|
104
|
+
const nextId = typeof block.next_grant_id === "string" && grants.some((grant) => grant.id === block.next_grant_id) ? block.next_grant_id : null;
|
|
105
|
+
const next = grants.find((grant) => grant.id === nextId) ?? grants.find((grant) => grant.resetsLeft > 0) ?? null;
|
|
106
|
+
const eligible = block.eligible === true;
|
|
107
|
+
const cooldown = isoTime(block.cooldown_until);
|
|
108
|
+
|
|
109
|
+
let blockedReason: string | null = null;
|
|
110
|
+
if (!eligible) {
|
|
111
|
+
const reason = typeof block.ineligible_reason === "string" ? block.ineligible_reason : "unavailable";
|
|
112
|
+
blockedReason = INELIGIBLE[reason] ?? INELIGIBLE.unavailable ?? null;
|
|
113
|
+
} else if (cooldown && Date.parse(cooldown) > now) {
|
|
114
|
+
blockedReason = `Resets are cooling down until ${new Date(cooldown).toLocaleString()}.`;
|
|
115
|
+
} else if (!next || !nextId) {
|
|
116
|
+
blockedReason = "Not usable right now.";
|
|
117
|
+
} else if (next.paused) {
|
|
118
|
+
blockedReason = "This reset is paused for now.";
|
|
119
|
+
} else if (next.blocking.length > 0) {
|
|
120
|
+
blockedReason = `This reset doesn't refill your ${joinLabels(next.blocking)} limit, so it can't be used until that resets.`;
|
|
121
|
+
} else if (next.useRequiresLimit && !atLimit) {
|
|
122
|
+
blockedReason = "Can be used once this account hits a limit.";
|
|
123
|
+
} else if (!next.usableNow) {
|
|
124
|
+
blockedReason = "Not usable right now; try again in a minute.";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
nextGrantId: nextId,
|
|
129
|
+
atLimit,
|
|
130
|
+
offer: {
|
|
131
|
+
available,
|
|
132
|
+
usableNow: blockedReason === null,
|
|
133
|
+
blockedReason,
|
|
134
|
+
expiresAt: next?.endsAt ?? null,
|
|
135
|
+
refills: next ? labels(next.clears) : [],
|
|
136
|
+
label: next?.label.trim() || null,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A one-line, secret-free summary of the `cedar_ember` block, for the plugin log. */
|
|
142
|
+
export function describeResetBlock(block: unknown): string {
|
|
143
|
+
if (block === undefined || block === null) return "not offered to this account (no reset program in the usage reply)";
|
|
144
|
+
if (!isObject(block)) return "unreadable reset status";
|
|
145
|
+
const grants = Array.isArray(block.grants) ? block.grants.length : 0;
|
|
146
|
+
const left = (Array.isArray(block.grants) ? block.grants : [])
|
|
147
|
+
.map(parseGrant)
|
|
148
|
+
.reduce((sum, grant) => sum + (grant?.resetsLeft ?? 0), 0);
|
|
149
|
+
const eligibility = block.eligible === true ? "eligible" : `not eligible (${String(block.ineligible_reason ?? "unknown")})`;
|
|
150
|
+
const props = isObject(block.event_props) ? block.event_props : {};
|
|
151
|
+
const seenAs = [props.surface, props.tier].filter((value) => typeof value === "string").join("/");
|
|
152
|
+
return `${eligibility}, ${grants} grant(s), ${left} reset(s) left${block.at_limit === true ? ", at a limit" : ""}${
|
|
153
|
+
seenAs ? ` [seen as ${seenAs}]` : ""
|
|
154
|
+
}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Turns the claim endpoint's reply into an outcome and a sentence for the user. */
|
|
158
|
+
export function readClaimReply(body: unknown): RedeemReply {
|
|
159
|
+
if (!isObject(body) || typeof body.result !== "string") {
|
|
160
|
+
return { outcome: "error", message: "Claude sent an unreadable reply. Check the account's usage before trying again.", left: null };
|
|
161
|
+
}
|
|
162
|
+
const left = typeof body.resets_left === "number" ? body.resets_left : null;
|
|
163
|
+
const leftText = left === null ? "" : ` · ${left} left`;
|
|
164
|
+
switch (body.result) {
|
|
165
|
+
case "reset": {
|
|
166
|
+
const cleared = limitTypes(body.cleared);
|
|
167
|
+
return { outcome: "reset", message: `Limits reset${cleared.length ? ` (${joinLabels(cleared)})` : ""}${leftText}.`, left };
|
|
168
|
+
}
|
|
169
|
+
case "already_used":
|
|
170
|
+
return { outcome: "already_used", message: `That reset was already used${leftText}.`, left };
|
|
171
|
+
case "not_limited":
|
|
172
|
+
return {
|
|
173
|
+
outcome: "not_limited",
|
|
174
|
+
message: "This account isn't at a usage limit right now, so the reset was kept. Use it once the account hits a limit.",
|
|
175
|
+
left,
|
|
176
|
+
};
|
|
177
|
+
case "cooldown": {
|
|
178
|
+
const until = isoTime(body.cooldown_until);
|
|
179
|
+
return {
|
|
180
|
+
outcome: "cooldown",
|
|
181
|
+
message: `Resets are cooling down${until ? ` until ${new Date(until).toLocaleString()}` : ""}. The reset was kept.`,
|
|
182
|
+
left,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
case "ineligible": {
|
|
186
|
+
const reason = typeof body.reason === "string" ? INELIGIBLE[body.reason] : undefined;
|
|
187
|
+
return { outcome: "unavailable", message: reason ?? "This account can't use a reset right now. The reset was kept.", left };
|
|
188
|
+
}
|
|
189
|
+
default:
|
|
190
|
+
return { outcome: "unavailable", message: "Limit resets aren't available right now. The reset was kept.", left };
|
|
191
|
+
}
|
|
192
|
+
}
|