@kapybara/zerosub 1.5.1 → 1.18.3
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/README.md +220 -26
- package/client/accounts-surface.tsx +344 -110
- package/client/add-account.tsx +335 -23
- package/client/api-account.tsx +250 -0
- package/client/azure-account.tsx +311 -0
- package/client/commands.ts +44 -8
- package/client/composer-item.tsx +203 -0
- package/client/fork-confirm.tsx +71 -0
- package/client/fork-settings.tsx +148 -0
- package/client/pace.tsx +196 -0
- package/client/pi-accounts.tsx +571 -0
- package/client/pills.ts +63 -25
- package/client/reset-confirm.tsx +12 -3
- package/client/settings-screen.tsx +48 -9
- package/client/store.ts +148 -19
- package/client/switch-row.tsx +28 -8
- package/client/ui.tsx +11 -0
- package/index.client.tsx +4 -1
- package/index.server.ts +53 -10
- package/package.json +5 -2
- package/server/account-identity.ts +11 -0
- package/server/adapter.ts +115 -8
- package/server/api-usage.ts +144 -0
- package/server/azure.ts +346 -0
- package/server/binaries.ts +41 -3
- package/server/claude.ts +276 -21
- package/server/codex-same-conversation.ts +71 -0
- package/server/codex.ts +35 -7
- package/server/continuations.ts +67 -17
- package/server/cursor.ts +222 -0
- package/server/families.ts +105 -8
- package/server/grok.ts +225 -0
- package/server/handoff.ts +211 -29
- package/server/homes.ts +1263 -35
- package/server/json-file.ts +39 -8
- package/server/launchers.ts +319 -0
- package/server/limits.ts +80 -3
- package/server/main-proxy.ts +238 -0
- package/server/modes.ts +20 -4
- package/server/paths.ts +81 -1
- package/server/pi.ts +528 -0
- package/server/placement.ts +133 -0
- package/server/process.ts +18 -1
- package/server/processes.ts +66 -0
- package/server/provider-env.ts +28 -0
- package/server/proxy.ts +282 -0
- package/server/reopen.ts +50 -8
- package/server/routing.ts +337 -30
- package/server/service.ts +4320 -805
- package/server/state.ts +303 -24
- package/server/usage.ts +2 -0
- package/shared/format.ts +170 -2
- package/shared/model.ts +339 -2
- package/shared/preferences.ts +22 -1
- package/shared/rpc.ts +130 -8
- package/shared/timeline.ts +11 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { readdir, readFile, realpath, rm, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { forgetSyncs } from "./homes";
|
|
4
|
+
import { Mutex, readJson, writeJsonAtomic } from "./json-file";
|
|
5
|
+
import { dataDir, mainClaudeDir, shownPath } from "./paths";
|
|
6
|
+
import { providerEnv } from "./provider-env";
|
|
7
|
+
import { NO_PROXY_KEYS, PROXY_KEYS, bypassesProxy, noProxyFromEnv, proxyFromEnv, withClaudeNoProxy, withoutClaudeDomains } from "./proxy";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where the proxy set in ZeroSub for the CLI's own login is kept while `~/.claude/settings.json`
|
|
11
|
+
* sets none. That file is the CLI login's settings, and every account's settings are made from it,
|
|
12
|
+
* so a proxy isn't added to it: the CLI login's sessions, and the CLI processes ZeroSub starts for
|
|
13
|
+
* it, get it as environment variables instead (see `mainProxyEnv`). When the file does set a proxy,
|
|
14
|
+
* Claude Code applies it over those variables, so the CLI login's proxy is changed there instead
|
|
15
|
+
* (see `writeMainProxy`).
|
|
16
|
+
*/
|
|
17
|
+
export function mainProxyPath(): string {
|
|
18
|
+
return join(dataDir(), "cli-login.json");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readSavedProxy(): Promise<string | null> {
|
|
22
|
+
const saved = await readJson(mainProxyPath());
|
|
23
|
+
const proxy = saved && typeof saved === "object" && "proxy" in saved && typeof saved.proxy === "string" ? saved.proxy.trim() : "";
|
|
24
|
+
return proxy || null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mainSettingsPath(): string {
|
|
28
|
+
return join(mainClaudeDir(), "settings.json");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Main's settings file the way it's shown: under `~` when it's in the home folder. */
|
|
32
|
+
function mainSettingsName(): string {
|
|
33
|
+
return shownPath(mainSettingsPath());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
37
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Main's settings files reported as unreadable, so each is logged once rather than on every session. */
|
|
41
|
+
const unreadableSettings = new Set<string>();
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The `env` of `~/.claude/settings.json`, which Claude Code applies over the environment it starts
|
|
45
|
+
* with. A file that doesn't parse (a trailing comma, say) sets nothing here, and is logged once: the
|
|
46
|
+
* CLI login still gets the proxy saved for it, rather than going out without one.
|
|
47
|
+
*/
|
|
48
|
+
export async function mainSettingsEnv(): Promise<Record<string, string>> {
|
|
49
|
+
const path = mainSettingsPath();
|
|
50
|
+
const unreadable = (why: string): Record<string, string> => {
|
|
51
|
+
if (!unreadableSettings.has(path)) {
|
|
52
|
+
unreadableSettings.add(path);
|
|
53
|
+
console.error(`[ZeroSub] ${why}; its env sets no proxy for the CLI login until it's fixed`);
|
|
54
|
+
}
|
|
55
|
+
return {};
|
|
56
|
+
};
|
|
57
|
+
let settings: unknown;
|
|
58
|
+
try {
|
|
59
|
+
settings = await readJson(path);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return unreadable(error instanceof Error ? error.message : String(error));
|
|
62
|
+
}
|
|
63
|
+
if (settings !== undefined && !isObject(settings)) return unreadable(`${path} is not a JSON object`);
|
|
64
|
+
unreadableSettings.delete(path);
|
|
65
|
+
if (settings === undefined) return {};
|
|
66
|
+
const strings: Record<string, string> = {};
|
|
67
|
+
if (isObject(settings.env)) for (const [key, value] of Object.entries(settings.env)) if (typeof value === "string") strings[key] = value;
|
|
68
|
+
return strings;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The no-proxy variables in `settingsEnv` that send Anthropic or Claude traffic past any proxy. The
|
|
73
|
+
* entries ZeroSub adds itself (see `withClaudeNoProxy`) don't count: the list it writes there with a
|
|
74
|
+
* proxy has Claude's updater host on it, and that mustn't stop the proxy being changed.
|
|
75
|
+
*/
|
|
76
|
+
function bypassingClaude(settingsEnv: Readonly<Record<string, string>>): string[] {
|
|
77
|
+
return NO_PROXY_KEYS.filter((key) => {
|
|
78
|
+
const list = settingsEnv[key];
|
|
79
|
+
return list !== undefined && withClaudeNoProxy(withoutClaudeDomains(list)) !== withClaudeNoProxy(list);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Why no proxy can be set for the CLI login: main's settings put Anthropic or Claude on a no-proxy
|
|
85
|
+
* list, which Claude Code applies over the one ZeroSub starts it with. Null when one can.
|
|
86
|
+
*/
|
|
87
|
+
export async function mainProxyBlock(): Promise<string | null> {
|
|
88
|
+
const keys = bypassingClaude(await mainSettingsEnv());
|
|
89
|
+
if (keys.length === 0) return null;
|
|
90
|
+
return `${mainSettingsName()} sets ${keys.join(" and ")} in its env with Anthropic or Claude on it, so Claude Code goes around any proxy for them. Take those entries out there first.`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The CLI login's own proxy: the one main's settings set, which Claude Code applies over anything
|
|
95
|
+
* else, with `file` naming them; else the one set in ZeroSub. Null when it uses whatever it inherits.
|
|
96
|
+
*/
|
|
97
|
+
export async function readMainProxy(): Promise<{ proxy: string | null; file: string | null }> {
|
|
98
|
+
const inSettings = proxyFromEnv(await mainSettingsEnv());
|
|
99
|
+
if (inSettings) return { proxy: inSettings, file: mainSettingsName() };
|
|
100
|
+
return { proxy: await readSavedProxy(), file: null };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** How many copies of main's settings from before ZeroSub changed them are kept. */
|
|
104
|
+
const SETTINGS_BACKUPS = 5;
|
|
105
|
+
const SETTINGS_BACKUP = /^claude-settings-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z\.json$/;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The no-proxy variables main's settings had before ZeroSub put its own list in them with a proxy,
|
|
109
|
+
* and that list, so they get them back when the proxy is cleared (see `writeProxyToMainSettings`).
|
|
110
|
+
*/
|
|
111
|
+
function noProxyRecordPath(): string {
|
|
112
|
+
return join(dataDir(), "cli-login-no-proxy.json");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The no-proxy variables main's settings (`env`) had before ZeroSub's list: the recorded ones where
|
|
117
|
+
* that list still is, else what's there (one changed by hand since, say).
|
|
118
|
+
*/
|
|
119
|
+
async function noProxyBefore(env: Readonly<Record<string, unknown>>): Promise<Record<string, unknown>> {
|
|
120
|
+
const record = await readJson(noProxyRecordPath()).catch(() => undefined);
|
|
121
|
+
const wrote = isObject(record) && typeof record.wrote === "string" ? record.wrote : null;
|
|
122
|
+
const recorded = isObject(record) && isObject(record.before) ? record.before : {};
|
|
123
|
+
const before: Record<string, unknown> = {};
|
|
124
|
+
for (const key of NO_PROXY_KEYS) {
|
|
125
|
+
const had = wrote !== null && env[key] === wrote ? recorded[key] : env[key];
|
|
126
|
+
if (had !== undefined) before[key] = had;
|
|
127
|
+
}
|
|
128
|
+
return before;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Sets the proxy variables in main's settings (all four to `proxy`, or none with `null`), leaving
|
|
133
|
+
* the rest as it is. A proxy comes with ZeroSub's no-proxy list, which goes with it again: the
|
|
134
|
+
* no-proxy variables the file had before are put back. It's written where it really is, so a
|
|
135
|
+
* settings file linked in from elsewhere (dotfiles, say) stays linked, with its permissions; the
|
|
136
|
+
* file as it was is kept in ZeroSub's `backups/` first.
|
|
137
|
+
*/
|
|
138
|
+
async function writeProxyToMainSettings(proxy: string | null): Promise<void> {
|
|
139
|
+
const path = await realpath(mainSettingsPath());
|
|
140
|
+
const text = await readFile(path, "utf8");
|
|
141
|
+
let settings: unknown;
|
|
142
|
+
try {
|
|
143
|
+
settings = JSON.parse(text) as unknown;
|
|
144
|
+
} catch {
|
|
145
|
+
throw new Error(`${mainSettingsName()} isn't valid JSON`);
|
|
146
|
+
}
|
|
147
|
+
if (!isObject(settings)) throw new Error(`${mainSettingsName()} is not a JSON object`);
|
|
148
|
+
const env: Record<string, unknown> = isObject(settings.env) ? { ...settings.env } : {};
|
|
149
|
+
const before = await noProxyBefore(env);
|
|
150
|
+
for (const key of PROXY_KEYS) {
|
|
151
|
+
if (proxy) env[key] = proxy;
|
|
152
|
+
else delete env[key];
|
|
153
|
+
}
|
|
154
|
+
let noProxy: string | null = null;
|
|
155
|
+
if (proxy) {
|
|
156
|
+
const stringEnv = Object.fromEntries(Object.entries(env).filter((entry): entry is [string, string] => typeof entry[1] === "string"));
|
|
157
|
+
// Written here, it outranks the lists Claude Code would otherwise inherit, so they're kept in it.
|
|
158
|
+
noProxy = withClaudeNoProxy(withoutClaudeDomains(noProxyFromEnv({ ...process.env, ...providerEnv("claude"), ...stringEnv })));
|
|
159
|
+
for (const key of NO_PROXY_KEYS) env[key] = noProxy;
|
|
160
|
+
} else {
|
|
161
|
+
for (const key of NO_PROXY_KEYS) {
|
|
162
|
+
if (key in before) env[key] = before[key];
|
|
163
|
+
else delete env[key];
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const backups = join(dataDir(), "backups");
|
|
168
|
+
await writeJsonAtomic(join(backups, `claude-settings-${new Date().toISOString().replace(/[:.]/g, "-")}.json`), settings, { durable: false });
|
|
169
|
+
const kept = (await readdir(backups)).filter((name) => SETTINGS_BACKUP.test(name)).sort();
|
|
170
|
+
for (const name of kept.slice(0, -SETTINGS_BACKUPS)) await rm(join(backups, name), { force: true });
|
|
171
|
+
|
|
172
|
+
// Recorded first, so the file never has ZeroSub's list without a note of what it replaced.
|
|
173
|
+
if (noProxy) await writeJsonAtomic(noProxyRecordPath(), { before, wrote: noProxy });
|
|
174
|
+
await writeJsonAtomic(path, { ...settings, env }, { mode: (await stat(path)).mode & 0o777 });
|
|
175
|
+
if (!noProxy) await rm(noProxyRecordPath(), { force: true });
|
|
176
|
+
// Every added home's settings are made from main's: have the next sync remake them.
|
|
177
|
+
forgetSyncs();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const writes = new Mutex();
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Sets (or clears, with `null`) the CLI login's proxy: in main's settings when they set one, which
|
|
184
|
+
* every account without a proxy of its own (and `claude` in a terminal) then uses too, else in
|
|
185
|
+
* ZeroSub's own file. Refused while main's settings send Anthropic past any proxy (see
|
|
186
|
+
* `mainProxyBlock`).
|
|
187
|
+
*/
|
|
188
|
+
export function writeMainProxy(proxy: string | null): Promise<void> {
|
|
189
|
+
return writes.run(async () => {
|
|
190
|
+
const block = proxy ? await mainProxyBlock() : null;
|
|
191
|
+
if (block) throw new Error(block);
|
|
192
|
+
if (proxyFromEnv(await mainSettingsEnv())) {
|
|
193
|
+
await writeProxyToMainSettings(proxy);
|
|
194
|
+
await rm(mainProxyPath(), { force: true });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (proxy) await writeJsonAtomic(mainProxyPath(), { proxy });
|
|
198
|
+
else await rm(mainProxyPath(), { force: true });
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* What the CLI login is started with for the proxy set in ZeroSub, nothing without one or while
|
|
204
|
+
* main's settings set a proxy (Claude Code would go by theirs): all four proxy variables, so nothing
|
|
205
|
+
* it inherits outranks the proxy, and the no-proxy list it would otherwise have (from the daemon's
|
|
206
|
+
* environment, the Claude provider's `env` in Paseo's config and main's settings) without the
|
|
207
|
+
* entries that would send Anthropic or Claude traffic past it, plus localhost, 127.0.0.1 and
|
|
208
|
+
* `downloads.claude.ai`, as for an account's own proxy (see `accountSettings` in server/homes.ts).
|
|
209
|
+
*/
|
|
210
|
+
export async function mainProxyEnv(): Promise<Record<string, string>> {
|
|
211
|
+
const settings = await mainSettingsEnv();
|
|
212
|
+
const proxy = proxyFromEnv(settings) ? null : await readSavedProxy();
|
|
213
|
+
if (!proxy) return {};
|
|
214
|
+
const noProxy = withClaudeNoProxy(withoutClaudeDomains(noProxyFromEnv({ ...process.env, ...providerEnv("claude"), ...settings })));
|
|
215
|
+
const env: Record<string, string> = {};
|
|
216
|
+
for (const key of PROXY_KEYS) env[key] = proxy;
|
|
217
|
+
for (const key of NO_PROXY_KEYS) env[key] = noProxy;
|
|
218
|
+
return env;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The proxy an account's Claude Code reaches Anthropic through when it has none of its own, and
|
|
223
|
+
* where that's set: main's settings, which every added account's are made from, over the Claude
|
|
224
|
+
* provider's `env` in Paseo's config, over the daemon's environment. For the CLI login (`main`),
|
|
225
|
+
* a proxy in main's settings is its own, so only the other two count. Null when it goes direct.
|
|
226
|
+
*/
|
|
227
|
+
export async function inheritedClaudeProxy(main = false): Promise<{ proxy: string; from: string } | null> {
|
|
228
|
+
const provider = providerEnv("claude");
|
|
229
|
+
const settings = { ...(await mainSettingsEnv()) };
|
|
230
|
+
// As it would be once the CLI login's own is taken out of main's settings.
|
|
231
|
+
if (main && proxyFromEnv(settings)) for (const key of PROXY_KEYS) delete settings[key];
|
|
232
|
+
const env: Record<string, string | undefined> = { ...process.env, ...provider, ...settings };
|
|
233
|
+
// Claude Code's order: the first proxy variable set wins.
|
|
234
|
+
const key = PROXY_KEYS.find((name) => env[name]?.trim());
|
|
235
|
+
if (!key || bypassesProxy(noProxyFromEnv(env), "https://api.anthropic.com")) return null;
|
|
236
|
+
const from = key in settings ? mainSettingsName() : key in provider ? "the Claude provider's env in Paseo's config" : "the daemon's environment";
|
|
237
|
+
return { proxy: env[key]!.trim(), from };
|
|
238
|
+
}
|
package/server/modes.ts
CHANGED
|
@@ -12,10 +12,10 @@ function tierOf(mode: ModeInfo | undefined): number {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* The other provider's mode
|
|
16
|
-
* has one, otherwise the closest more careful one (first listed wins a tie). `null` when it has
|
|
17
|
-
* nothing that careful, or the source mode is unknown
|
|
18
|
-
*
|
|
15
|
+
* The other provider's mode whose Paseo tier is no higher than the source agent's: the same tier if
|
|
16
|
+
* it has one, otherwise the closest more careful one (first listed wins a tie). `null` when it has
|
|
17
|
+
* nothing that careful, or the source mode is unknown. Tiers order modes; they don't make two
|
|
18
|
+
* providers' policies equivalent.
|
|
19
19
|
*/
|
|
20
20
|
export function equivalentMode(
|
|
21
21
|
sourceModes: readonly ModeInfo[],
|
|
@@ -32,3 +32,19 @@ export function equivalentMode(
|
|
|
32
32
|
}
|
|
33
33
|
return best?.id ?? null;
|
|
34
34
|
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether `targetModeId`'s Paseo tier is no higher than the source mode's tier, by the same ordering
|
|
38
|
+
* `equivalentMode` picks from. It compares tiers only, not what each provider's own policy allows.
|
|
39
|
+
* An unknown mode on either side is never within.
|
|
40
|
+
*/
|
|
41
|
+
export function withinMode(
|
|
42
|
+
sourceModes: readonly ModeInfo[],
|
|
43
|
+
sourceModeId: string | null | undefined,
|
|
44
|
+
targetModes: readonly ModeInfo[],
|
|
45
|
+
targetModeId: string | null | undefined,
|
|
46
|
+
): boolean {
|
|
47
|
+
const ceiling = tierOf(sourceModes.find((mode) => mode.id === sourceModeId));
|
|
48
|
+
const tier = tierOf(targetModes.find((mode) => mode.id === targetModeId));
|
|
49
|
+
return ceiling >= 0 && tier >= 0 && tier <= ceiling;
|
|
50
|
+
}
|
package/server/paths.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
1
2
|
import { homedir } from "node:os";
|
|
2
|
-
import { join, resolve } from "node:path";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
4
|
|
|
4
5
|
export function paseoHome(): string {
|
|
5
6
|
const configured = process.env.PASEO_HOME?.trim();
|
|
@@ -35,3 +36,82 @@ export function mainCodexHome(): string {
|
|
|
35
36
|
const configured = process.env.CODEX_HOME?.trim();
|
|
36
37
|
return configured ? resolve(configured) : join(homedir(), ".codex");
|
|
37
38
|
}
|
|
39
|
+
|
|
40
|
+
/** Grok Build's home: `GROK_HOME` in the daemon's environment, else `~/.grok`. */
|
|
41
|
+
export function mainGrokHome(): string {
|
|
42
|
+
const configured = process.env.GROK_HOME?.trim();
|
|
43
|
+
return configured ? resolve(configured) : join(homedir(), ".grok");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Where `cursor-agent` keeps its settings (`cli-config.json` and more), as it works it out:
|
|
48
|
+
* `CURSOR_CONFIG_DIR`, else `$XDG_CONFIG_HOME/cursor`, else `~/.cursor`.
|
|
49
|
+
*/
|
|
50
|
+
export function mainCursorConfigDir(): string {
|
|
51
|
+
const configured = process.env.CURSOR_CONFIG_DIR?.trim();
|
|
52
|
+
if (configured) return resolve(configured);
|
|
53
|
+
const xdg = process.env.XDG_CONFIG_HOME?.trim();
|
|
54
|
+
return xdg ? join(resolve(xdg), "cursor") : join(homedir(), ".cursor");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The daemon's XDG config folder, which an added Cursor account's mirrors (see `prepareCursorHome`):
|
|
59
|
+
* `$XDG_CONFIG_HOME`, else `~/.config`. `cursor-agent` keeps its Linux login in `cursor/auth.json` there.
|
|
60
|
+
*/
|
|
61
|
+
export function mainConfigHome(): string {
|
|
62
|
+
return resolve(process.env.XDG_CONFIG_HOME?.trim() || join(homedir(), ".config"));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Pi's agent directory: `PI_CODING_AGENT_DIR` (a leading `~` is the home folder, as Pi reads it), else `~/.pi/agent`. */
|
|
66
|
+
export function mainPiDir(): string {
|
|
67
|
+
const configured = process.env.PI_CODING_AGENT_DIR?.trim();
|
|
68
|
+
if (!configured) return join(homedir(), ".pi", "agent");
|
|
69
|
+
const expanded = configured === "~" ? homedir() : configured.startsWith("~/") ? join(homedir(), configured.slice(2)) : configured;
|
|
70
|
+
return resolve(expanded);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Where each added Claude account's script goes (`claude-<account>.sh`, see server/launchers.ts):
|
|
75
|
+
* `~/.claude-config`, or `ZEROSUB_LAUNCHER_DIR`.
|
|
76
|
+
*/
|
|
77
|
+
export function launcherDir(): string {
|
|
78
|
+
const configured = process.env.ZEROSUB_LAUNCHER_DIR?.trim();
|
|
79
|
+
return configured ? resolve(configured) : join(homedir(), ".claude-config");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where each added Pi account's script goes (`pi-<account>.sh`), and where added Pi accounts' homes
|
|
84
|
+
* are kept unless the user picks another folder: `~/.pi-config`, or `ZEROSUB_PI_DIR`.
|
|
85
|
+
*/
|
|
86
|
+
export function piLauncherDir(): string {
|
|
87
|
+
const configured = process.env.ZEROSUB_PI_DIR?.trim();
|
|
88
|
+
return configured ? resolve(configured) : join(homedir(), ".pi-config");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A path the way it's shown to the user: under `~` when it's in the home folder. */
|
|
92
|
+
export function shownPath(path: string): string {
|
|
93
|
+
const home = homedir();
|
|
94
|
+
return path === home ? "~" : path.startsWith(`${home}${sep}`) ? `~${path.slice(home.length)}` : path;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Whether `home` is a folder directly in `homesDir()`, where ZeroSub makes homes. */
|
|
98
|
+
export function inHomesDir(home: string): boolean {
|
|
99
|
+
return dirname(resolve(home)) === homesDir();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether ZeroSub may delete `home`: a folder of its own directly in `homesDir()`, not a link to one
|
|
104
|
+
* elsewhere and never a CLI's own home. Every other home (where added Pi accounts are kept, say)
|
|
105
|
+
* belongs to the user: removing its account only unregisters it.
|
|
106
|
+
*/
|
|
107
|
+
export async function isOwnHome(home: string): Promise<boolean> {
|
|
108
|
+
const path = resolve(home);
|
|
109
|
+
if (!inHomesDir(path) || path === mainClaudeDir() || path === mainCodexHome() || path === mainPiDir() || path === mainGrokHome()) return false;
|
|
110
|
+
// What a Cursor account's home mirrors could be pointed inside one: never delete a home holding it.
|
|
111
|
+
for (const main of [mainCursorConfigDir(), mainConfigHome()]) {
|
|
112
|
+
const inside = relative(path, main);
|
|
113
|
+
if (inside === "" || (!inside.startsWith("..") && !isAbsolute(inside))) return false;
|
|
114
|
+
}
|
|
115
|
+
const entry = await lstat(path).catch(() => null);
|
|
116
|
+
return !entry?.isSymbolicLink();
|
|
117
|
+
}
|