@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,351 @@
|
|
|
1
|
+
import { openExternalUrl, type PluginSurfaceProps } from "@getpaseo/plugin/client";
|
|
2
|
+
import { copyText, Modal, TextInput, useToast } from "@getpaseo/plugin/client/react-native";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
|
|
4
|
+
import { ActivityIndicator, Pressable, Text, View } from "react-native";
|
|
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";
|
|
8
|
+
import { describe, useStore, type ZeroSubStore } from "./store";
|
|
9
|
+
import { Button, useText } from "./ui";
|
|
10
|
+
|
|
11
|
+
type Theme = PluginSurfaceProps["theme"];
|
|
12
|
+
|
|
13
|
+
export function AddAccountModal({
|
|
14
|
+
theme,
|
|
15
|
+
layout,
|
|
16
|
+
host,
|
|
17
|
+
multiHost,
|
|
18
|
+
store,
|
|
19
|
+
family,
|
|
20
|
+
accountId,
|
|
21
|
+
onClose,
|
|
22
|
+
}: {
|
|
23
|
+
theme: Theme;
|
|
24
|
+
layout: PluginSurfaceProps["layout"];
|
|
25
|
+
host: PluginSurfaceProps["host"];
|
|
26
|
+
/** The app knows several hosts, so say which one gets the account. */
|
|
27
|
+
multiHost: boolean;
|
|
28
|
+
store: ZeroSubStore;
|
|
29
|
+
family: Family;
|
|
30
|
+
accountId?: string;
|
|
31
|
+
onClose(): void;
|
|
32
|
+
}) {
|
|
33
|
+
const text = useText(theme);
|
|
34
|
+
const { state } = useStore(store);
|
|
35
|
+
// A browser sign-in finishes by redirecting to `localhost` on the host's computer, so it only
|
|
36
|
+
// works from a browser there, and never on hosts without a desktop (servers, containers).
|
|
37
|
+
// Phones start with a code; the browser stays on offer for someone at the host's computer.
|
|
38
|
+
const mobile = layout.platform === "ios" || layout.platform === "android";
|
|
39
|
+
const hostBrowser = state?.browserSignIn !== false;
|
|
40
|
+
const [method, setMethod] = useState<LoginMethod>(hostBrowser && !mobile ? "browser" : "code");
|
|
41
|
+
const [loginId, setLoginId] = useState<string | null>(null);
|
|
42
|
+
const [startError, setStartError] = useState<string | null>(null);
|
|
43
|
+
// Each start gets a number; only the newest one's answer counts.
|
|
44
|
+
const attempt = useRef(0);
|
|
45
|
+
const closed = useRef(false);
|
|
46
|
+
|
|
47
|
+
const begin = useCallback(
|
|
48
|
+
async (next: LoginMethod) => {
|
|
49
|
+
const mine = ++attempt.current;
|
|
50
|
+
setStartError(null);
|
|
51
|
+
setLoginId(null);
|
|
52
|
+
try {
|
|
53
|
+
const login = await store.rpc(startLogin, { family, accountId, method: next });
|
|
54
|
+
// The modal closed (or a newer start replaced this one) before the daemon answered.
|
|
55
|
+
if (closed.current || mine !== attempt.current) {
|
|
56
|
+
void store.rpc(cancelLogin, { loginId: login.id }).catch(() => undefined);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
setLoginId(login.id);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (!closed.current && mine === attempt.current) setStartError(describe(error));
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
[store, family, accountId],
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (attempt.current === 0) void begin(method);
|
|
69
|
+
}, [begin, method]);
|
|
70
|
+
|
|
71
|
+
useEffect(() => store.watchClosely(), [store]);
|
|
72
|
+
|
|
73
|
+
const login = state?.logins.find((entry) => entry.id === loginId) ?? null;
|
|
74
|
+
const account = login?.accountId ? state?.accounts.find((entry) => entry.id === login.accountId) : undefined;
|
|
75
|
+
const finished = login ? login.step === "done" || login.step === "failed" || login.step === "canceled" : false;
|
|
76
|
+
|
|
77
|
+
const close = useCallback(() => {
|
|
78
|
+
closed.current = true;
|
|
79
|
+
if (loginId && !finished) void store.rpc(cancelLogin, { loginId }).catch(() => undefined);
|
|
80
|
+
onClose();
|
|
81
|
+
}, [loginId, finished, store, onClose]);
|
|
82
|
+
|
|
83
|
+
// Claude's flow serves both links at once; ChatGPT needs a fresh flow to switch.
|
|
84
|
+
const switchMethod = useCallback(
|
|
85
|
+
(next: LoginMethod) => {
|
|
86
|
+
setMethod(next);
|
|
87
|
+
if (family === "codex") void begin(next);
|
|
88
|
+
},
|
|
89
|
+
[family, begin],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const title = accountId ? "Sign in again" : `Add ${FAMILY_LABEL[family]} account`;
|
|
93
|
+
let body: ReactNode;
|
|
94
|
+
if (startError) {
|
|
95
|
+
body = (
|
|
96
|
+
<>
|
|
97
|
+
<Text style={text.danger}>{startError}</Text>
|
|
98
|
+
<Row>
|
|
99
|
+
<Button theme={theme} label="Close" onPress={close} />
|
|
100
|
+
<Button theme={theme} tone="primary" icon="RefreshCw" label="Try again" onPress={() => void begin(method)} />
|
|
101
|
+
</Row>
|
|
102
|
+
</>
|
|
103
|
+
);
|
|
104
|
+
} else if (!login || login.step === "starting") {
|
|
105
|
+
body = <Waiting theme={theme} label="Preparing sign-in…" />;
|
|
106
|
+
} else if (login.step === "waiting") {
|
|
107
|
+
body = (
|
|
108
|
+
<SignInSteps
|
|
109
|
+
theme={theme}
|
|
110
|
+
store={store}
|
|
111
|
+
login={login}
|
|
112
|
+
family={family}
|
|
113
|
+
method={method}
|
|
114
|
+
browserUsable={hostBrowser}
|
|
115
|
+
onSwitch={switchMethod}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
} else if (login.step === "verifying") {
|
|
119
|
+
body = <Waiting theme={theme} label="Checking your account…" />;
|
|
120
|
+
} else if (login.step === "done") {
|
|
121
|
+
const who = account
|
|
122
|
+
? `${account.label}${account.email && account.email !== account.label ? ` (${account.email})` : ""}${
|
|
123
|
+
account.plan ? ` · ${formatPlan(account.plan)}` : ""
|
|
124
|
+
}`
|
|
125
|
+
: "The account";
|
|
126
|
+
body = (
|
|
127
|
+
<>
|
|
128
|
+
<Text style={text.heading}>You're signed in</Text>
|
|
129
|
+
<Text style={text.body}>
|
|
130
|
+
{who} is ready. New {FAMILY_LABEL[family]} agents{multiHost ? ` on ${host.label}` : ""} can use it, and you can
|
|
131
|
+
move an agent to it from the account button in the message box.
|
|
132
|
+
</Text>
|
|
133
|
+
<Row>
|
|
134
|
+
<Button theme={theme} tone="primary" label="Done" onPress={onClose} />
|
|
135
|
+
</Row>
|
|
136
|
+
</>
|
|
137
|
+
);
|
|
138
|
+
} else {
|
|
139
|
+
body = (
|
|
140
|
+
<>
|
|
141
|
+
<Text style={login.step === "failed" ? text.danger : text.muted}>
|
|
142
|
+
{login.message ?? (login.step === "canceled" ? "Sign-in was canceled." : "Sign-in failed.")}
|
|
143
|
+
</Text>
|
|
144
|
+
<Row>
|
|
145
|
+
<Button theme={theme} label="Close" onPress={close} />
|
|
146
|
+
<Button theme={theme} tone="primary" icon="RefreshCw" label="Try again" onPress={() => void begin(method)} />
|
|
147
|
+
</Row>
|
|
148
|
+
</>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<Modal title={title} open onOpenChange={(open) => (open ? undefined : close())}>
|
|
154
|
+
<Modal.Content>
|
|
155
|
+
{multiHost && !accountId && login?.step !== "done" ? (
|
|
156
|
+
<Text style={text.small}>Adds the account on {host.label} only. Other hosts sign in separately.</Text>
|
|
157
|
+
) : null}
|
|
158
|
+
{body}
|
|
159
|
+
</Modal.Content>
|
|
160
|
+
</Modal>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function SignInSteps({
|
|
165
|
+
theme,
|
|
166
|
+
store,
|
|
167
|
+
login,
|
|
168
|
+
family,
|
|
169
|
+
method,
|
|
170
|
+
browserUsable,
|
|
171
|
+
onSwitch,
|
|
172
|
+
}: {
|
|
173
|
+
theme: Theme;
|
|
174
|
+
store: ZeroSubStore;
|
|
175
|
+
login: LoginView;
|
|
176
|
+
family: Family;
|
|
177
|
+
method: LoginMethod;
|
|
178
|
+
/** A browser on the host's computer can finish the sign-in, so offer that route too. */
|
|
179
|
+
browserUsable: boolean;
|
|
180
|
+
onSwitch(method: LoginMethod): void;
|
|
181
|
+
}) {
|
|
182
|
+
const text = useText(theme);
|
|
183
|
+
const toast = useToast();
|
|
184
|
+
const [code, setCode] = useState("");
|
|
185
|
+
const [submitting, setSubmitting] = useState(false);
|
|
186
|
+
const [codeError, setCodeError] = useState<string | null>(null);
|
|
187
|
+
|
|
188
|
+
// Claude: the browser link completes by itself; the code link works from any device.
|
|
189
|
+
const pasteFlow = family === "claude" && (method === "code" || !login.url) && Boolean(login.codeUrl);
|
|
190
|
+
const link = pasteFlow ? login.codeUrl : login.url ?? login.codeUrl;
|
|
191
|
+
|
|
192
|
+
const open = useCallback(() => {
|
|
193
|
+
if (link) void openExternalUrl(link).catch((error: unknown) => toast.error(describe(error)));
|
|
194
|
+
}, [link, toast]);
|
|
195
|
+
const copy = useCallback(
|
|
196
|
+
async (value: string, what: string) => {
|
|
197
|
+
try {
|
|
198
|
+
await copyText(value);
|
|
199
|
+
toast.show(`${what} copied`, { variant: "success" });
|
|
200
|
+
} catch {
|
|
201
|
+
toast.error("Couldn't copy. Select the text and copy it yourself.");
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
[toast],
|
|
205
|
+
);
|
|
206
|
+
const submit = useCallback(async () => {
|
|
207
|
+
const value = code.trim();
|
|
208
|
+
if (!value) return;
|
|
209
|
+
setSubmitting(true);
|
|
210
|
+
setCodeError(null);
|
|
211
|
+
try {
|
|
212
|
+
await store.rpc(submitLoginCode, { loginId: login.id, code: value });
|
|
213
|
+
} catch (error) {
|
|
214
|
+
setCodeError(describe(error));
|
|
215
|
+
} finally {
|
|
216
|
+
setSubmitting(false);
|
|
217
|
+
}
|
|
218
|
+
}, [code, login.id, store]);
|
|
219
|
+
|
|
220
|
+
const inputStyle = useMemo(
|
|
221
|
+
() => ({
|
|
222
|
+
color: theme.colors.foreground,
|
|
223
|
+
backgroundColor: theme.colors.surface2,
|
|
224
|
+
borderColor: theme.colors.border,
|
|
225
|
+
borderWidth: 1,
|
|
226
|
+
borderRadius: 8,
|
|
227
|
+
paddingHorizontal: 12,
|
|
228
|
+
paddingVertical: 10,
|
|
229
|
+
fontSize: 14,
|
|
230
|
+
}),
|
|
231
|
+
[theme],
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const service = family === "claude" ? "Claude" : "ChatGPT";
|
|
235
|
+
const canSwitch = browserUsable && (family === "codex" || (Boolean(login.url) && Boolean(login.codeUrl)));
|
|
236
|
+
return (
|
|
237
|
+
<View style={{ gap: 16 }}>
|
|
238
|
+
<Step theme={theme} number={1} title={`Open the ${service} sign-in page`}>
|
|
239
|
+
<Text style={text.muted}>
|
|
240
|
+
{pasteFlow || login.userCode
|
|
241
|
+
? "Sign in with the account you want to add. This works on any device, including your phone."
|
|
242
|
+
: "Sign in with the account you want to add, using a browser on the host's own computer. It finishes by itself."}
|
|
243
|
+
</Text>
|
|
244
|
+
<Row start>
|
|
245
|
+
<Button theme={theme} tone="primary" icon="ExternalLink" label="Open sign-in page" disabled={!link} onPress={open} />
|
|
246
|
+
{link ? <Button theme={theme} icon="Copy" label="Copy link" onPress={() => void copy(link, "Link")} /> : null}
|
|
247
|
+
</Row>
|
|
248
|
+
</Step>
|
|
249
|
+
|
|
250
|
+
{login.userCode ? (
|
|
251
|
+
<Step theme={theme} number={2} title="Enter this code on that page">
|
|
252
|
+
<Text selectable style={text.mono}>
|
|
253
|
+
{login.userCode}
|
|
254
|
+
</Text>
|
|
255
|
+
<Row start>
|
|
256
|
+
<Button theme={theme} icon="Copy" label="Copy code" onPress={() => void copy(login.userCode ?? "", "Code")} />
|
|
257
|
+
</Row>
|
|
258
|
+
<Text style={text.small}>
|
|
259
|
+
If the page says code sign-in is off, turn on device code sign-in in ChatGPT → Settings → Security (or ask
|
|
260
|
+
your workspace admin), then try again.
|
|
261
|
+
</Text>
|
|
262
|
+
</Step>
|
|
263
|
+
) : null}
|
|
264
|
+
|
|
265
|
+
{pasteFlow ? (
|
|
266
|
+
<Step theme={theme} number={2} title="Paste the code shown after you sign in">
|
|
267
|
+
<TextInput
|
|
268
|
+
value={code}
|
|
269
|
+
onChangeText={setCode}
|
|
270
|
+
placeholder="Paste code"
|
|
271
|
+
placeholderTextColor={theme.colors.foregroundMuted}
|
|
272
|
+
autoCapitalize="none"
|
|
273
|
+
autoCorrect={false}
|
|
274
|
+
onSubmitEditing={() => void submit()}
|
|
275
|
+
style={inputStyle}
|
|
276
|
+
accessibilityLabel="Sign-in code"
|
|
277
|
+
/>
|
|
278
|
+
{codeError ? <Text style={text.danger}>{codeError}</Text> : null}
|
|
279
|
+
<Row start>
|
|
280
|
+
<Button
|
|
281
|
+
theme={theme}
|
|
282
|
+
tone="primary"
|
|
283
|
+
label="Continue"
|
|
284
|
+
busy={submitting}
|
|
285
|
+
disabled={!code.trim()}
|
|
286
|
+
onPress={() => void submit()}
|
|
287
|
+
/>
|
|
288
|
+
</Row>
|
|
289
|
+
</Step>
|
|
290
|
+
) : (
|
|
291
|
+
<Waiting theme={theme} label="Waiting for you to finish signing in…" />
|
|
292
|
+
)}
|
|
293
|
+
|
|
294
|
+
{login.message ? <Text style={text.small}>{login.message}</Text> : null}
|
|
295
|
+
|
|
296
|
+
{canSwitch ? (
|
|
297
|
+
<Pressable
|
|
298
|
+
accessibilityRole="button"
|
|
299
|
+
onPress={() => onSwitch(method === "code" ? "browser" : "code")}
|
|
300
|
+
style={{ alignSelf: "flex-start", paddingVertical: 4 }}
|
|
301
|
+
>
|
|
302
|
+
<Text style={{ color: theme.colors.accent, fontSize: 13, fontWeight: "600" }}>
|
|
303
|
+
{method === "code" ? "At the host's computer? Use its browser instead" : "On a different device? Use a code instead"}
|
|
304
|
+
</Text>
|
|
305
|
+
</Pressable>
|
|
306
|
+
) : null}
|
|
307
|
+
</View>
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function Step({ theme, number, title, children }: { theme: Theme; number: number; title: string; children: ReactNode }) {
|
|
312
|
+
const text = useText(theme);
|
|
313
|
+
return (
|
|
314
|
+
<View style={{ flexDirection: "row", gap: 12 }}>
|
|
315
|
+
<View
|
|
316
|
+
style={{
|
|
317
|
+
width: 24,
|
|
318
|
+
height: 24,
|
|
319
|
+
borderRadius: 12,
|
|
320
|
+
alignItems: "center",
|
|
321
|
+
justifyContent: "center",
|
|
322
|
+
backgroundColor: theme.colors.surface2,
|
|
323
|
+
}}
|
|
324
|
+
>
|
|
325
|
+
<Text style={{ color: theme.colors.foreground, fontSize: 12, fontWeight: "700" }}>{number}</Text>
|
|
326
|
+
</View>
|
|
327
|
+
<View style={{ flex: 1, gap: 8 }}>
|
|
328
|
+
<Text style={text.strong}>{title}</Text>
|
|
329
|
+
{children}
|
|
330
|
+
</View>
|
|
331
|
+
</View>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function Waiting({ theme, label }: { theme: Theme; label: string }) {
|
|
336
|
+
const text = useText(theme);
|
|
337
|
+
return (
|
|
338
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 10, paddingVertical: 6 }}>
|
|
339
|
+
<ActivityIndicator size="small" color={theme.colors.foregroundMuted} />
|
|
340
|
+
<Text style={text.muted}>{label}</Text>
|
|
341
|
+
</View>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function Row({ children, start }: { children: ReactNode; start?: boolean }) {
|
|
346
|
+
return (
|
|
347
|
+
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 8, justifyContent: start ? "flex-start" : "flex-end" }}>
|
|
348
|
+
{children}
|
|
349
|
+
</View>
|
|
350
|
+
);
|
|
351
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import { FAMILY_LABEL, type AccountView, type StateView } from "../shared/model";
|
|
3
|
+
import { setAgentAccount, setDefaultAccount } from "../shared/rpc";
|
|
4
|
+
import type { ZeroSubStore } from "./store";
|
|
5
|
+
|
|
6
|
+
/** Command Center entries and the `/account` slash command. */
|
|
7
|
+
export function contributeCommands(client: PluginClientContext, store: ZeroSubStore): () => void {
|
|
8
|
+
const removers: Array<() => void> = [];
|
|
9
|
+
|
|
10
|
+
removers.push(
|
|
11
|
+
client.addCommandCenterItem({
|
|
12
|
+
id: "open-accounts",
|
|
13
|
+
title: "Manage subscription accounts (ZeroSub)",
|
|
14
|
+
icon: "Users",
|
|
15
|
+
keywords: ["zerosub", "claude", "chatgpt", "codex", "account", "subscription", "login", "switch"],
|
|
16
|
+
context: "global",
|
|
17
|
+
onSelect({ openSurface }) {
|
|
18
|
+
openSurface("accounts");
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
removers.push(
|
|
24
|
+
client.addSlashCommand({
|
|
25
|
+
name: "account",
|
|
26
|
+
description: "Switch the subscription account this agent uses",
|
|
27
|
+
argumentHint: "[name | default]",
|
|
28
|
+
context: "agent",
|
|
29
|
+
async onSubmit({ args, agent, openSurface }) {
|
|
30
|
+
const state = await currentState(store);
|
|
31
|
+
const family = state.providers[agent.provider];
|
|
32
|
+
if (!family) throw new Error(`${agent.provider} agents don't use Claude or ChatGPT subscription accounts.`);
|
|
33
|
+
const query = args.trim();
|
|
34
|
+
if (!query) {
|
|
35
|
+
openSurface("accounts");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const accounts = state.accounts.filter((account) => account.family === family);
|
|
39
|
+
if (/^default$/i.test(query)) {
|
|
40
|
+
await store.rpc(setAgentAccount, { agentId: agent.id, accountId: null });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const match = findAccount(accounts, query);
|
|
44
|
+
if (!match) {
|
|
45
|
+
const names = accounts.map((account) => account.label).join(", ");
|
|
46
|
+
throw new Error(`No ${FAMILY_LABEL[family]} account matches "${query}". Accounts: ${names || "none"}.`);
|
|
47
|
+
}
|
|
48
|
+
if (match.status === "signed_out") throw new Error(`${match.label} is signed out. Sign it in from Accounts first.`);
|
|
49
|
+
await store.rpc(setAgentAccount, { agentId: agent.id, accountId: match.id });
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// One "Make … the default" entry per account, kept in step with the account list.
|
|
55
|
+
let dynamic: Array<() => void> = [];
|
|
56
|
+
let signature = "";
|
|
57
|
+
const syncDynamic = () => {
|
|
58
|
+
const state = store.current.state;
|
|
59
|
+
const next = state ? JSON.stringify(state.accounts.map((a) => [a.id, a.label, a.isDefault, a.status])) : "";
|
|
60
|
+
if (next === signature) return;
|
|
61
|
+
signature = next;
|
|
62
|
+
for (const remove of dynamic) remove();
|
|
63
|
+
dynamic = [];
|
|
64
|
+
if (!state) return;
|
|
65
|
+
state.accounts.forEach((account, index) => {
|
|
66
|
+
if (account.isDefault || account.status === "signed_out") return;
|
|
67
|
+
dynamic.push(
|
|
68
|
+
client.addCommandCenterItem({
|
|
69
|
+
id: `default-${index}`,
|
|
70
|
+
title: `Make ${account.label} the default ${FAMILY_LABEL[account.family]} account`,
|
|
71
|
+
icon: "Star",
|
|
72
|
+
keywords: ["account", "default", "switch", account.email ?? "", account.family],
|
|
73
|
+
context: "global",
|
|
74
|
+
async onSelect() {
|
|
75
|
+
await store.rpc(setDefaultAccount, { accountId: account.id });
|
|
76
|
+
},
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const unsubscribe = store.subscribe(syncDynamic);
|
|
82
|
+
syncDynamic();
|
|
83
|
+
|
|
84
|
+
return () => {
|
|
85
|
+
unsubscribe();
|
|
86
|
+
for (const remove of dynamic) remove();
|
|
87
|
+
for (const remove of removers) remove();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function currentState(store: ZeroSubStore): Promise<StateView> {
|
|
92
|
+
if (!store.current.state) await store.refresh();
|
|
93
|
+
const state = store.current.state;
|
|
94
|
+
if (!state) throw new Error(store.current.error ?? "Accounts are not available yet.");
|
|
95
|
+
return state;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function findAccount(accounts: readonly AccountView[], query: string): AccountView | undefined {
|
|
99
|
+
const needle = query.trim().toLowerCase();
|
|
100
|
+
const exact = accounts.find(
|
|
101
|
+
(account) => account.label.toLowerCase() === needle || account.email?.toLowerCase() === needle,
|
|
102
|
+
);
|
|
103
|
+
if (exact) return exact;
|
|
104
|
+
const prefix = accounts.filter(
|
|
105
|
+
(account) => account.label.toLowerCase().startsWith(needle) || account.email?.toLowerCase().startsWith(needle),
|
|
106
|
+
);
|
|
107
|
+
if (prefix.length === 1) return prefix[0];
|
|
108
|
+
const partial = accounts.filter(
|
|
109
|
+
(account) => account.label.toLowerCase().includes(needle) || account.email?.toLowerCase().includes(needle),
|
|
110
|
+
);
|
|
111
|
+
return partial.length === 1 ? partial[0] : undefined;
|
|
112
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { PluginButtonContentProps } from "@getpaseo/plugin/client";
|
|
2
|
+
import { useToast } from "@getpaseo/plugin/client/react-native";
|
|
3
|
+
import { useState, type ComponentType } from "react";
|
|
4
|
+
import { Text, View } from "react-native";
|
|
5
|
+
import { FAMILY_LABEL, type Family } from "../shared/model";
|
|
6
|
+
import { forkAgent } from "../shared/rpc";
|
|
7
|
+
import { describe, type ZeroSubStore } from "./store";
|
|
8
|
+
import { Button, useText } from "./ui";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Confirmation for carrying a stopped agent's work on with the other provider. It starts a new agent
|
|
12
|
+
* that goes straight to work, so it's worth a second look; refusals (say, no mode as careful as this
|
|
13
|
+
* agent's) show right here.
|
|
14
|
+
*/
|
|
15
|
+
export function forkPopover(store: ZeroSubStore, agentId: string, from: Family, to: Family): ComponentType<PluginButtonContentProps> {
|
|
16
|
+
return function ForkPopover({ theme, close }: PluginButtonContentProps) {
|
|
17
|
+
const toast = useToast();
|
|
18
|
+
const text = useText(theme);
|
|
19
|
+
const [busy, setBusy] = useState(false);
|
|
20
|
+
const [error, setError] = useState<string | null>(null);
|
|
21
|
+
|
|
22
|
+
const start = async () => {
|
|
23
|
+
setBusy(true);
|
|
24
|
+
setError(null);
|
|
25
|
+
try {
|
|
26
|
+
const fork = await store.rpc(forkAgent, { agentId });
|
|
27
|
+
close();
|
|
28
|
+
toast.show(`Continuing on ${FAMILY_LABEL[to]} in “${fork.title}”`, { variant: "success", durationMs: 5_000 });
|
|
29
|
+
} catch (failure) {
|
|
30
|
+
setError(describe(failure));
|
|
31
|
+
setBusy(false);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<View style={{ gap: 12, maxWidth: 360 }}>
|
|
37
|
+
<Text style={text.heading}>Continue on {FAMILY_LABEL[to]}?</Text>
|
|
38
|
+
<Text style={text.body}>
|
|
39
|
+
Every {FAMILY_LABEL[from]} account is at its limit. This starts a new {FAMILY_LABEL[to]} agent in this workspace
|
|
40
|
+
with the conversation so far, allowed no more than this agent is. This agent stays as it is.
|
|
41
|
+
</Text>
|
|
42
|
+
{error ? <Text style={text.danger}>{error}</Text> : null}
|
|
43
|
+
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 8, justifyContent: "flex-end" }}>
|
|
44
|
+
<Button theme={theme} label="Not now" onPress={close} disabled={busy} />
|
|
45
|
+
<Button
|
|
46
|
+
theme={theme}
|
|
47
|
+
tone="primary"
|
|
48
|
+
icon="GitFork"
|
|
49
|
+
label={`Continue on ${FAMILY_LABEL[to]}`}
|
|
50
|
+
busy={busy}
|
|
51
|
+
onPress={() => void start()}
|
|
52
|
+
/>
|
|
53
|
+
</View>
|
|
54
|
+
</View>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
}
|