@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.
Files changed (56) hide show
  1. package/README.md +220 -26
  2. package/client/accounts-surface.tsx +344 -110
  3. package/client/add-account.tsx +335 -23
  4. package/client/api-account.tsx +250 -0
  5. package/client/azure-account.tsx +311 -0
  6. package/client/commands.ts +44 -8
  7. package/client/composer-item.tsx +203 -0
  8. package/client/fork-confirm.tsx +71 -0
  9. package/client/fork-settings.tsx +148 -0
  10. package/client/pace.tsx +196 -0
  11. package/client/pi-accounts.tsx +571 -0
  12. package/client/pills.ts +63 -25
  13. package/client/reset-confirm.tsx +12 -3
  14. package/client/settings-screen.tsx +48 -9
  15. package/client/store.ts +148 -19
  16. package/client/switch-row.tsx +28 -8
  17. package/client/ui.tsx +11 -0
  18. package/index.client.tsx +4 -1
  19. package/index.server.ts +53 -10
  20. package/package.json +5 -2
  21. package/server/account-identity.ts +11 -0
  22. package/server/adapter.ts +115 -8
  23. package/server/api-usage.ts +144 -0
  24. package/server/azure.ts +346 -0
  25. package/server/binaries.ts +41 -3
  26. package/server/claude.ts +276 -21
  27. package/server/codex-same-conversation.ts +71 -0
  28. package/server/codex.ts +35 -7
  29. package/server/continuations.ts +67 -17
  30. package/server/cursor.ts +222 -0
  31. package/server/families.ts +105 -8
  32. package/server/grok.ts +225 -0
  33. package/server/handoff.ts +211 -29
  34. package/server/homes.ts +1263 -35
  35. package/server/json-file.ts +39 -8
  36. package/server/launchers.ts +319 -0
  37. package/server/limits.ts +80 -3
  38. package/server/main-proxy.ts +238 -0
  39. package/server/modes.ts +20 -4
  40. package/server/paths.ts +81 -1
  41. package/server/pi.ts +528 -0
  42. package/server/placement.ts +133 -0
  43. package/server/process.ts +18 -1
  44. package/server/processes.ts +66 -0
  45. package/server/provider-env.ts +28 -0
  46. package/server/proxy.ts +282 -0
  47. package/server/reopen.ts +50 -8
  48. package/server/routing.ts +337 -30
  49. package/server/service.ts +4320 -805
  50. package/server/state.ts +303 -24
  51. package/server/usage.ts +2 -0
  52. package/shared/format.ts +170 -2
  53. package/shared/model.ts +339 -2
  54. package/shared/preferences.ts +22 -1
  55. package/shared/rpc.ts +130 -8
  56. package/shared/timeline.ts +11 -0
@@ -3,8 +3,11 @@ import { copyText, Modal, TextInput, useToast } from "@getpaseo/plugin/client/re
3
3
  import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
4
4
  import { ActivityIndicator, Pressable, Text, View } from "react-native";
5
5
  import { formatPlan } from "../shared/format";
6
- import { FAMILY_LABEL, type Family, type LoginMethod, type LoginView } from "../shared/model";
7
- import { cancelLogin, startLogin, submitLoginCode } from "../shared/rpc";
6
+ import { FAMILY_LABEL, type AccountView, type Family, type LoginMethod, type LoginView } from "../shared/model";
7
+ import { cancelLogin, importAccount, startLogin, submitLoginCode } from "../shared/rpc";
8
+ import { ApiAccountForm } from "./api-account";
9
+ import { AzureAccountForm } from "./azure-account";
10
+ import { useInputStyle } from "./pi-accounts";
8
11
  import { describe, useStore, type ZeroSubStore } from "./store";
9
12
  import { Button, useText } from "./ui";
10
13
 
@@ -40,8 +43,21 @@ export function AddAccountModal({
40
43
  const [method, setMethod] = useState<LoginMethod>(hostBrowser && !mobile ? "browser" : "code");
41
44
  const [loginId, setLoginId] = useState<string | null>(null);
42
45
  const [startError, setStartError] = useState<string | null>(null);
46
+ // A new Claude account can get a proxy of its own first, so the sign-in itself goes through it.
47
+ const askProxy = family === "claude" && !accountId;
48
+ const [proxy, setProxy] = useState("");
49
+ const [proxyDone, setProxyDone] = useState(!askProxy);
50
+ // A new Claude account is a subscription to sign in, an API key or token, or a folder that's
51
+ // signed in already; a new ChatGPT one, a subscription or an Azure OpenAI key. Asked first.
52
+ const askMode = askProxy || (family === "codex" && !accountId);
53
+ const [mode, setMode] = useState<"ask" | "subscription" | "api" | "import">(askMode ? "ask" : "subscription");
54
+ const [apiAdded, setApiAdded] = useState<string | null>(null);
55
+ const [apiWarning, setApiWarning] = useState<string | null>(null);
56
+ const [imported, setImported] = useState<string | null>(null);
43
57
  // Each start gets a number; only the newest one's answer counts.
44
58
  const attempt = useRef(0);
59
+ /** A sign-in was started for the proxy step as it was last left. */
60
+ const started = useRef(false);
45
61
  const closed = useRef(false);
46
62
 
47
63
  const begin = useCallback(
@@ -50,7 +66,7 @@ export function AddAccountModal({
50
66
  setStartError(null);
51
67
  setLoginId(null);
52
68
  try {
53
- const login = await store.rpc(startLogin, { family, accountId, method: next });
69
+ const login = await store.rpc(startLogin, { family, accountId, method: next, proxy: askProxy && proxy.trim() ? proxy.trim() : undefined });
54
70
  // The modal closed (or a newer start replaced this one) before the daemon answered.
55
71
  if (closed.current || mine !== attempt.current) {
56
72
  void store.rpc(cancelLogin, { loginId: login.id }).catch(() => undefined);
@@ -61,12 +77,14 @@ export function AddAccountModal({
61
77
  if (!closed.current && mine === attempt.current) setStartError(describe(error));
62
78
  }
63
79
  },
64
- [store, family, accountId],
80
+ [store, family, accountId, askProxy, proxy],
65
81
  );
66
82
 
67
83
  useEffect(() => {
68
- if (attempt.current === 0) void begin(method);
69
- }, [begin, method]);
84
+ if (!proxyDone || mode !== "subscription" || started.current) return;
85
+ started.current = true;
86
+ void begin(method);
87
+ }, [proxyDone, mode, begin, method]);
70
88
 
71
89
  useEffect(() => store.watchClosely(), [store]);
72
90
 
@@ -80,23 +98,109 @@ export function AddAccountModal({
80
98
  onClose();
81
99
  }, [loginId, finished, store, onClose]);
82
100
 
83
- // Claude's flow serves both links at once; ChatGPT needs a fresh flow to switch.
101
+ // Claude's flow serves both links at once; ChatGPT and Grok need a fresh flow to switch (Cursor has one link).
84
102
  const switchMethod = useCallback(
85
103
  (next: LoginMethod) => {
86
104
  setMethod(next);
87
- if (family === "codex") void begin(next);
105
+ if (family !== "claude") void begin(next);
88
106
  },
89
107
  [family, begin],
90
108
  );
91
109
 
92
- const title = accountId ? "Sign in again" : `Add ${FAMILY_LABEL[family]} account`;
110
+ // Back to the proxy step: the sign-in started with the old one stops, and starts again after.
111
+ const changeProxy = useCallback(() => {
112
+ // A start still under way is answered for an older attempt, so it's canceled when it comes back.
113
+ attempt.current += 1;
114
+ started.current = false;
115
+ if (loginId && !finished) void store.rpc(cancelLogin, { loginId }).catch(() => undefined);
116
+ setLoginId(null);
117
+ setStartError(null);
118
+ setProxyDone(false);
119
+ }, [loginId, finished, store]);
120
+
121
+ const azure = family === "codex";
122
+ const title = accountId
123
+ ? "Sign in again"
124
+ : mode === "api"
125
+ ? azure
126
+ ? "Add Azure OpenAI account"
127
+ : "Add Claude API account"
128
+ : mode === "import"
129
+ ? "Add a signed-in folder"
130
+ : `Add ${FAMILY_LABEL[family]} account`;
93
131
  let body: ReactNode;
94
- if (startError) {
132
+ if (mode === "ask") {
133
+ body = (
134
+ <ModeStep
135
+ theme={theme}
136
+ family={azure ? "codex" : "claude"}
137
+ autoSwitch={state?.autoSwitch ?? true}
138
+ paidFallback={state?.paidApiFallback ?? false}
139
+ onSubscription={() => setMode("subscription")}
140
+ onApi={() => setMode("api")}
141
+ onImport={() => setMode("import")}
142
+ onCancel={close}
143
+ />
144
+ );
145
+ } else if (mode === "import") {
146
+ const added = imported ? state?.accounts.find((entry) => entry.id === imported) : undefined;
147
+ body = imported ? (
148
+ <>
149
+ <Text style={text.heading}>Account added</Text>
150
+ <Text style={text.body}>
151
+ {added ? describeAccount(added) : "The account"} is ready, signed in as it was. New Claude agents
152
+ {multiHost ? ` on ${host.label}` : ""} can use it, and you can move an agent to it from the account button in the
153
+ message box.
154
+ </Text>
155
+ <Row>
156
+ <Button theme={theme} tone="primary" label="Done" tooltip="Close this window" tooltipAlign="end" onPress={onClose} />
157
+ </Row>
158
+ </>
159
+ ) : (
160
+ <ImportStep theme={theme} store={store} onDone={setImported} onCancel={close} />
161
+ );
162
+ } else if (mode === "api") {
163
+ const added = apiAdded ? state?.accounts.find((entry) => entry.id === apiAdded) : undefined;
164
+ body = apiAdded ? (
165
+ <>
166
+ <Text style={text.heading}>Account added</Text>
167
+ <Text style={text.body}>
168
+ {added?.label ?? "The account"} is ready. Make it the default, or pick it for an agent from the account button in
169
+ the message box;{" "}
170
+ {azure && state?.paidApiFallback
171
+ ? "ZeroSub moves agents onto it by itself only when every ChatGPT subscription account is out."
172
+ : "ZeroSub never moves agents onto it by itself."}
173
+ </Text>
174
+ {apiWarning ? <Text style={text.warning}>{apiWarning}</Text> : null}
175
+ <Row>
176
+ <Button theme={theme} tone="primary" label="Done" tooltip="Close this window" tooltipAlign="end" onPress={onClose} />
177
+ </Row>
178
+ </>
179
+ ) : azure ? (
180
+ <AzureAccountForm
181
+ theme={theme}
182
+ store={store}
183
+ paidFallback={state?.paidApiFallback ?? false}
184
+ onDone={(id, warning) => {
185
+ setApiWarning(warning);
186
+ setApiAdded(id);
187
+ }}
188
+ onCancel={close}
189
+ />
190
+ ) : (
191
+ <ApiAccountForm theme={theme} store={store} onDone={setApiAdded} onCancel={close} />
192
+ );
193
+ } else if (!proxyDone) {
194
+ body = <ProxyStep theme={theme} proxy={proxy} onChange={setProxy} onCancel={close} onContinue={() => setProxyDone(true)} />;
195
+ } else if (startError) {
95
196
  body = (
96
197
  <>
97
198
  <Text style={text.danger}>{startError}</Text>
98
199
  <Row>
99
200
  <Button theme={theme} label="Close" tooltip="Close without adding an account" tooltipAlign="end" onPress={close} />
201
+ {askProxy ? (
202
+ <Button theme={theme} label="Change proxy" tooltip="Go back and change the proxy for this account" tooltipAlign="end" onPress={changeProxy} />
203
+ ) : null}
100
204
  <Button
101
205
  theme={theme}
102
206
  tone="primary"
@@ -126,11 +230,7 @@ export function AddAccountModal({
126
230
  } else if (login.step === "verifying") {
127
231
  body = <Waiting theme={theme} label="Checking your account…" />;
128
232
  } else if (login.step === "done") {
129
- const who = account
130
- ? `${account.label}${account.email && account.email !== account.label ? ` (${account.email})` : ""}${
131
- account.plan ? ` · ${formatPlan(account.plan)}` : ""
132
- }`
133
- : "The account";
233
+ const who = account ? describeAccount(account) : "The account";
134
234
  body = (
135
235
  <>
136
236
  <Text style={text.heading}>You're signed in</Text>
@@ -201,7 +301,9 @@ function SignInSteps({
201
301
  const [submitting, setSubmitting] = useState(false);
202
302
  const [codeError, setCodeError] = useState<string | null>(null);
203
303
 
204
- // Claude: the browser link completes by itself; the code link works from any device.
304
+ // Claude: the browser link completes by itself; the code link works from any device. Cursor's one
305
+ // link works from any device too: cursor-agent asks Cursor whether it was used.
306
+ const anywhere = family === "cursor";
205
307
  const pasteFlow = family === "claude" && (method === "code" || !login.url) && Boolean(login.codeUrl);
206
308
  const link = pasteFlow ? login.codeUrl : login.url ?? login.codeUrl;
207
309
 
@@ -247,13 +349,13 @@ function SignInSteps({
247
349
  [theme],
248
350
  );
249
351
 
250
- const service = family === "claude" ? "Claude" : "ChatGPT";
251
- const canSwitch = browserUsable && (family === "codex" || (Boolean(login.url) && Boolean(login.codeUrl)));
352
+ const service = family === "codex" ? "ChatGPT" : FAMILY_LABEL[family];
353
+ const canSwitch = browserUsable && !anywhere && (family !== "claude" || (Boolean(login.url) && Boolean(login.codeUrl)));
252
354
  return (
253
355
  <View style={{ gap: 16 }}>
254
356
  <Step theme={theme} number={1} title={`Open the ${service} sign-in page`}>
255
357
  <Text style={text.muted}>
256
- {pasteFlow || login.userCode
358
+ {pasteFlow || login.userCode || anywhere
257
359
  ? "Sign in with the account you want to add. This works on any device, including your phone."
258
360
  : "Sign in with the account you want to add, using a browser on the host's own computer. It finishes by itself."}
259
361
  </Text>
@@ -296,10 +398,12 @@ function SignInSteps({
296
398
  onPress={() => void copy(login.userCode ?? "", "Code")}
297
399
  />
298
400
  </Row>
299
- <Text style={text.small}>
300
- If the page says code sign-in is off, turn on device code sign-in in ChatGPT → Settings → Security (or ask
301
- your workspace admin), then try again.
302
- </Text>
401
+ {family === "codex" ? (
402
+ <Text style={text.small}>
403
+ If the page says code sign-in is off, turn on device code sign-in in ChatGPT → Settings → Security (or ask
404
+ your workspace admin), then try again.
405
+ </Text>
406
+ ) : null}
303
407
  </Step>
304
408
  ) : null}
305
409
 
@@ -375,6 +479,214 @@ function Step({ theme, number, title, children }: { theme: Theme; number: number
375
479
  );
376
480
  }
377
481
 
482
+ /** "work (work@acme.com) · Max": an account as the finished steps name it. */
483
+ function describeAccount(account: AccountView): string {
484
+ return `${account.label}${account.email && account.email !== account.label ? ` (${account.email})` : ""}${
485
+ account.plan ? ` · ${formatPlan(account.plan)}` : ""
486
+ }`;
487
+ }
488
+
489
+ /**
490
+ * How a new account connects: a Claude one, a subscription to sign in, an API key or token, or a
491
+ * folder that's signed in already; a ChatGPT one, a subscription to sign in or an Azure OpenAI key.
492
+ */
493
+ function ModeStep({
494
+ theme,
495
+ family,
496
+ autoSwitch,
497
+ paidFallback,
498
+ onSubscription,
499
+ onApi,
500
+ onImport,
501
+ onCancel,
502
+ }: {
503
+ theme: Theme;
504
+ family: "claude" | "codex";
505
+ /** Agents move off an account at its limit by themselves only with automatic switching on. */
506
+ autoSwitch: boolean;
507
+ /** Paid API accounts may take work over by themselves once every subscription is out. */
508
+ paidFallback: boolean;
509
+ onSubscription(): void;
510
+ onApi(): void;
511
+ onImport(): void;
512
+ onCancel(): void;
513
+ }) {
514
+ const text = useText(theme);
515
+ const claude = family === "claude";
516
+ return (
517
+ <View style={{ gap: 12 }}>
518
+ <Text style={text.strong}>How does this account connect?</Text>
519
+ <Choice
520
+ theme={theme}
521
+ title={claude ? "Claude subscription" : "ChatGPT subscription"}
522
+ detail={`Sign in with a ${claude ? "Claude Pro or Max" : "ChatGPT Plus, Pro, Team or Business"} account. Usage limits are tracked${
523
+ autoSwitch
524
+ ? " and agents move between accounts automatically."
525
+ : "; turn on automatic switching to have agents move between accounts by themselves."
526
+ }`}
527
+ onPress={onSubscription}
528
+ />
529
+ {claude ? (
530
+ <>
531
+ <Choice
532
+ theme={theme}
533
+ title="API key or token"
534
+ detail="An Anthropic API key, or a key for another endpoint via its base URL. Billed per use; used only when you choose it."
535
+ onPress={onApi}
536
+ />
537
+ <Choice
538
+ theme={theme}
539
+ title="A folder that's signed in already"
540
+ detail="A Claude Code folder (its CLAUDE_CONFIG_DIR) signed in with a subscription: one you set up yourself, or one kept when you removed its account here. No sign-in; it stays where it is."
541
+ onPress={onImport}
542
+ />
543
+ </>
544
+ ) : (
545
+ <Choice
546
+ theme={theme}
547
+ title="Azure OpenAI (API key)"
548
+ detail={`Codex on your Azure OpenAI resource, with its endpoint and key. Billed per token; ${
549
+ paidFallback ? "used by itself only when every ChatGPT subscription account is out, or when you choose it." : "used only when you choose it."
550
+ }`}
551
+ onPress={onApi}
552
+ />
553
+ )}
554
+ <Row>
555
+ <Button theme={theme} label="Cancel" tooltip="Close without adding an account" tooltipAlign="end" onPress={onCancel} />
556
+ </Row>
557
+ </View>
558
+ );
559
+ }
560
+
561
+ function Choice({ theme, title, detail, onPress }: { theme: Theme; title: string; detail: string; onPress(): void }) {
562
+ const text = useText(theme);
563
+ return (
564
+ <Pressable
565
+ accessibilityRole="button"
566
+ accessibilityLabel={title}
567
+ onPress={onPress}
568
+ style={{ gap: 4, padding: 12, borderRadius: 10, borderWidth: 1, borderColor: theme.colors.border, backgroundColor: theme.colors.surface1 }}
569
+ >
570
+ <Text style={text.strong}>{title}</Text>
571
+ <Text style={text.small}>{detail}</Text>
572
+ </Pressable>
573
+ );
574
+ }
575
+
576
+ /** Adds a folder Claude Code is signed in with already, where it is. */
577
+ function ImportStep({ theme, store, onDone, onCancel }: { theme: Theme; store: ZeroSubStore; onDone(accountId: string): void; onCancel(): void }) {
578
+ const text = useText(theme);
579
+ const inputStyle = useInputStyle(theme);
580
+ const [path, setPath] = useState("");
581
+ const [busy, setBusy] = useState(false);
582
+ const [error, setError] = useState<string | null>(null);
583
+ const add = useCallback(async () => {
584
+ if (!path.trim()) return;
585
+ setBusy(true);
586
+ setError(null);
587
+ try {
588
+ const result = await store.rpc(importAccount, { path: path.trim() });
589
+ await store.refresh();
590
+ onDone(result.accountId);
591
+ } catch (failure) {
592
+ setError(describe(failure));
593
+ } finally {
594
+ setBusy(false);
595
+ }
596
+ }, [path, store, onDone]);
597
+ return (
598
+ <View style={{ gap: 12 }}>
599
+ <Text style={text.strong}>Folder on this host</Text>
600
+ <Text style={text.muted}>
601
+ The folder Claude Code was started with as CLAUDE_CONFIG_DIR, written the same way: its sign-in is saved under that exact path. ZeroSub
602
+ checks who it's signed in as, and adds it unless that account is here already.
603
+ </Text>
604
+ <TextInput
605
+ value={path}
606
+ onChangeText={setPath}
607
+ maxLength={1024}
608
+ placeholder="~/.claude-config/claude-work"
609
+ placeholderTextColor={theme.colors.foregroundMuted}
610
+ autoCapitalize="none"
611
+ autoCorrect={false}
612
+ autoFocus
613
+ onSubmitEditing={() => void add()}
614
+ style={inputStyle}
615
+ accessibilityLabel="Folder to add"
616
+ />
617
+ <Text style={text.small}>
618
+ A full path; ~ is your home folder. Its files are kept as they are; ZeroSub only adds what it shares with your other accounts. Removing the
619
+ account here later only takes it out of ZeroSub again.
620
+ </Text>
621
+ {error ? <Text style={text.danger}>{error}</Text> : null}
622
+ <Row>
623
+ <Button theme={theme} label="Cancel" tooltip="Close without adding an account" tooltipAlign="end" onPress={onCancel} />
624
+ <Button
625
+ theme={theme}
626
+ tone="primary"
627
+ label="Add account"
628
+ tooltip="Check the folder's sign-in and add it"
629
+ tooltipAlign="end"
630
+ busy={busy}
631
+ disabled={!path.trim()}
632
+ onPress={() => void add()}
633
+ />
634
+ </Row>
635
+ </View>
636
+ );
637
+ }
638
+
639
+ /** A new Claude account's optional proxy, asked before its sign-in starts. */
640
+ function ProxyStep({
641
+ theme,
642
+ proxy,
643
+ onChange,
644
+ onCancel,
645
+ onContinue,
646
+ }: {
647
+ theme: Theme;
648
+ proxy: string;
649
+ onChange(proxy: string): void;
650
+ onCancel(): void;
651
+ onContinue(): void;
652
+ }) {
653
+ const text = useText(theme);
654
+ const inputStyle = useInputStyle(theme);
655
+ return (
656
+ <>
657
+ <Text style={text.strong}>Proxy for this account (optional)</Text>
658
+ <TextInput
659
+ value={proxy}
660
+ onChangeText={onChange}
661
+ autoFocus
662
+ maxLength={2048}
663
+ placeholder="http://host:port"
664
+ placeholderTextColor={theme.colors.foregroundMuted}
665
+ autoCapitalize="none"
666
+ autoCorrect={false}
667
+ onSubmitEditing={onContinue}
668
+ style={inputStyle}
669
+ accessibilityLabel="Proxy URL"
670
+ />
671
+ <Text style={text.small}>
672
+ http:// or https://, with user:password@ if the proxy needs it. The CLI signs in through it, and the account's agents use it too.
673
+ Leave it empty to use the network the daemon has; you can set one later from the account's card.
674
+ </Text>
675
+ <Row>
676
+ <Button theme={theme} label="Cancel" tooltip="Close without adding an account" tooltipAlign="end" onPress={onCancel} />
677
+ <Button
678
+ theme={theme}
679
+ tone="primary"
680
+ label={proxy.trim() ? "Continue" : "Continue without a proxy"}
681
+ tooltip="Start signing in"
682
+ tooltipAlign="end"
683
+ onPress={onContinue}
684
+ />
685
+ </Row>
686
+ </>
687
+ );
688
+ }
689
+
378
690
  function Waiting({ theme, label }: { theme: Theme; label: string }) {
379
691
  const text = useText(theme);
380
692
  return (