GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lite/src/provider-auth.tsx at main · ultralytics/lite

Expand file tree

/

provider-auth.tsx

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
434 lines (415 loc) · 13.9 KB
/

provider-auth.tsx

Copy path

File metadata and controls

434 lines (415 loc) · 13.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
import { invoke } from "@tauri-apps/api/core";
import { Check, ExternalLink, Eye, EyeOff } from "lucide-react";
import { type ReactNode, useState, useSyncExternalStore } from "react";
import { ProviderIcon } from "@/brand-icons";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
import { Spinner } from "@/components/ui/spinner";
import { type Agent, agentLabel, type ModelProvider, providerLabel } from "@/types";
export const AUTH_PROVIDERS = {
codex: {
id: "codex",
agent: "codex",
provider: "openai",
variable: "OPENAI_API_KEY",
keys: "https://platform.openai.com/api-keys",
placeholder: "e.g., sk-proj-…",
signIn: true,
},
claude: {
id: "claude",
agent: "claude",
provider: undefined,
label: "Anthropic",
variable: "ANTHROPIC_API_KEY",
keys: "https://platform.claude.com/settings/keys",
placeholder: "e.g., sk-ant-api…",
signIn: true,
},
deepseek: {
id: "deepseek",
agent: "codex",
provider: "deepseek",
variable: "DEEPSEEK_API_KEY",
keys: "https://platform.deepseek.com/api_keys",
placeholder: "e.g., sk-…",
signIn: false,
note: "Runs Codex against DeepSeek. Usage bills DeepSeek, not OpenAI.",
},
zai: {
id: "zai",
agent: "codex",
provider: "zai",
variable: "ZAI_API_KEY",
keys: "https://z.ai/manage-apikey/apikey-list",
placeholder: "e.g., 1a2b3c….AbCd…",
signIn: false,
note: "Runs Codex against Z.ai. Usage bills Z.ai, not OpenAI.",
},
mimo: {
id: "mimo",
agent: "codex",
provider: "mimo",
variable: "MIMO_API_KEY",
keys: "https://platform.xiaomimimo.com/#/console/api-keys",
placeholder: "e.g., sk-…",
signIn: false,
note: "Runs Codex against Xiaomi MiMo. Usage bills Xiaomi, not OpenAI.",
},
openrouter: {
id: "openrouter",
agent: "codex",
provider: "openrouter",
variable: "OPENROUTER_API_KEY",
keys: "https://openrouter.ai/settings/keys",
placeholder: "e.g., sk-or-v1-…",
signIn: false,
note: "Runs Codex against OpenRouter. Usage bills OpenRouter, not OpenAI.",
},
gemini: {
id: "gemini",
agent: "gemini",
provider: undefined,
label: "Google Gemini",
variable: "GEMINI_API_KEY",
keys: "https://aistudio.google.com/apikey",
placeholder: "e.g., AQ.…",
signIn: true,
},
kimi: {
id: "kimi",
agent: "kimi",
provider: undefined,
label: "Moonshot AI",
variable: "MOONSHOT_API_KEY",
keys: "https://platform.kimi.ai/console/api-keys",
placeholder: "e.g., sk-…",
signIn: true,
},
qwen: {
id: "qwen",
agent: "qwen",
provider: undefined,
label: "Alibaba ModelStudio",
signIn: true,
},
} as const satisfies Record<
string,
{
id: string;
agent: Agent;
provider?: ModelProvider;
label?: string;
variable?: string;
// Where the vendor hands out API keys, and what one of its keys starts like.
keys?: string;
placeholder?: string;
signIn: boolean;
note?: string;
}
>;
// A vendor Lite can hold an API key for.
export type KeyProvider = Extract<(typeof AUTH_PROVIDERS)[keyof typeof AUTH_PROVIDERS], { variable: string }>;
export const KEY_PROVIDERS = Object.values(AUTH_PROVIDERS).filter(
(option): option is KeyProvider => "variable" in option,
);
export interface ProviderAuth {
name: string;
account: string | null;
keyHint: string | null;
cliAuthMethod: "provider" | "apiKey" | null;
identity: AccountIdentity | null;
}
export interface AccountIdentity {
email: string | null;
organization: string | null;
plan: string | null;
}
export function identityText({ email, organization, plan }: AccountIdentity): string {
const tier = plan?.replace(/_/g, " ").replace(/\b\w/g, (letter) => letter.toUpperCase());
const personal = plan && ["free", "go", "plus", "pro", "prolite", "promax", "max"].includes(plan);
return [organization || (personal ? "Personal" : ""), tier, email].filter(Boolean).join(" · ");
}
export type AccountAgent = "claude" | "codex";
const ACCOUNT_NAME = /^[a-z0-9][a-z0-9_-]{0,31}$/;
const accountsKey = (agent: AccountAgent) => `lite.${agent}Accounts.v1`;
const accountListeners = new Set<() => void>();
function storedAccounts(agent: AccountAgent): string[] {
try {
const stored: unknown = JSON.parse(localStorage.getItem(accountsKey(agent)) ?? "[]");
return Array.isArray(stored) ? stored.filter((name) => typeof name === "string" && ACCOUNT_NAME.test(name)) : [];
} catch {
return [];
}
}
let accounts = { claude: storedAccounts("claude"), codex: storedAccounts("codex") };
export function setAccounts(agent: AccountAgent, names: string[]) {
accounts = { ...accounts, [agent]: names };
localStorage.setItem(accountsKey(agent), JSON.stringify(names));
for (const listener of accountListeners) listener();
}
function subscribeAccounts(listener: () => void) {
accountListeners.add(listener);
return () => accountListeners.delete(listener);
}
export function useAccounts() {
return useSyncExternalStore(subscribeAccounts, () => accounts);
}
export function readProviderAuth() {
return invoke<ProviderAuth[]>("provider_auth", { accounts });
}
// The vendor a provider belongs to. A Codex provider is named by types.ts, which owns every ModelProvider
// label already; only a harness Lite has no ModelProvider for carries a name of its own.
export function providerName(option: { provider?: ModelProvider; label?: string }): string {
return option.provider ? providerLabel(option.provider) : (option.label ?? "");
}
// How a new-session row's provider is connected: a key Lite saved, a key in the CLI's own configuration, or
// the CLI's sign-in, in the order they take priority.
export function ProviderAuthDescription({
provider,
status,
}: {
provider: (typeof AUTH_PROVIDERS)[keyof typeof AUTH_PROVIDERS];
status?: ProviderAuth;
}) {
const hint = status?.keyHint;
if (!status) return "Checking…";
if (provider.agent === "gemini" && !hint) return "Sign-in managed by Antigravity";
if (!hint && !status.cliAuthMethod) return "Not set up";
const text = hint
? "API key saved in Lite"
: status.cliAuthMethod === "apiKey"
? `API key in ${agentLabel(provider.agent)}’s configuration`
: status.identity
? `Signed in · ${identityText(status.identity)}`
: "Signed in";
return (
<span className="flex min-w-0 items-center gap-1.5" title={text}>
<Check className="size-3.5 shrink-0" />
<span className="truncate">{text}</span>
</span>
);
}
// Adding or replacing one vendor's key: what it is for, where to get one, and the key itself. Settings and
// the new-session dialog both open this, so a vendor's key is only ever entered here. The key is saved
// the moment the user confirms; onSaved then does whatever the key was wanted for.
export function ApiKeyDialog({
provider,
replacing,
saveLabel = replacing ? "Replace key" : "Save key",
footer,
onClose,
onSaved,
}: {
provider?: KeyProvider;
replacing: boolean;
saveLabel?: string;
footer?: ReactNode;
onClose: () => void;
onSaved: () => Promise<void> | void;
}) {
const [key, setKey] = useState("");
const [shown, setShown] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
function close() {
setKey("");
setError("");
onClose();
}
async function save() {
if (!provider || !key.trim()) return;
setSaving(true);
setError("");
try {
await invoke("save_api_key", { name: provider.id, key: key.trim() });
setKey("");
onClose();
await onSaved();
} catch (reason) {
setError(String(reason));
} finally {
setSaving(false);
}
}
return (
<Dialog open={Boolean(provider)} onOpenChange={(open) => !open && !saving && close()}>
{provider ? (
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<ProviderIcon agent={provider.agent} provider={provider.provider} className="size-5" />
{replacing ? "Replace" : "Add"} {providerName(provider)} API key
</DialogTitle>
<DialogDescription>
{"note" in provider ? provider.note : `${agentLabel(provider.agent)} uses it instead of its own sign-in.`}{" "}
<button
type="button"
className="inline-flex items-center gap-1 text-foreground underline underline-offset-4"
onClick={() => void invoke("open_url", { url: provider.keys })}
>
Get an API key
<ExternalLink aria-hidden="true" className="size-3" />
</button>
</DialogDescription>
</DialogHeader>
<form
className="space-y-1.5"
onSubmit={(event) => {
event.preventDefault();
void save();
}}
>
<InputGroup>
<InputGroupInput
autoFocus
type={shown ? "text" : "password"}
value={key}
className="font-mono"
placeholder={provider.placeholder}
aria-label={`${providerName(provider)} API key`}
autoComplete="off"
disabled={saving}
onChange={(event) => setKey(event.target.value)}
/>
<InputGroupAddon align="inline-end">
<InputGroupButton
size="icon-xs"
aria-label={shown ? "Hide the key" : "Show the key"}
onClick={() => setShown((current) => !current)}
>
{shown ? <EyeOff /> : <Eye />}
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
<p className="text-xs text-muted-foreground">
Kept on this computer and handed to {agentLabel(provider.agent)} as{" "}
<span className="font-mono">{provider.variable}</span>.
</p>
{error ? <p className="text-xs text-destructive">{error}</p> : null}
</form>
<DialogFooter className="items-center sm:justify-between">
<span>{footer}</span>
<span className="flex gap-2">
<Button type="button" variant="outline" disabled={saving} onClick={close}>
Cancel
</Button>
<Button type="button" disabled={saving || !key.trim()} onClick={() => void save()}>
{saving ? <Spinner /> : null}
{saveLabel}
</Button>
</span>
</DialogFooter>
</DialogContent>
) : null}
</Dialog>
);
}
export const createAccountDialog = () => BaseDialog.createHandle<AccountAgent>();
export function AccountDialog({
handle,
onAdded,
}: {
handle: BaseDialog.Handle<AccountAgent>;
onAdded: (agent: AccountAgent, account: string) => void;
}) {
return (
<BaseDialog.Root handle={handle}>
{({ payload: agent }) =>
agent && (
<DialogContent className="sm:max-w-md">
<AccountForm agent={agent} onClose={() => handle.close()} onAdded={(name) => onAdded(agent, name)} />
</DialogContent>
)
}
</BaseDialog.Root>
);
}
function AccountForm({
agent,
onClose,
onAdded,
}: {
agent: AccountAgent;
onClose: () => void;
onAdded: (account: string) => void;
}) {
const accounts = useAccounts()[agent];
const [name, setName] = useState("");
const account = name.trim();
const problem = !account
? ""
: !ACCOUNT_NAME.test(account)
? "Use up to 32 lowercase letters, digits, - and _, starting with a letter or digit."
: accounts.includes(account)
? "That account is already added."
: "";
function add() {
if (!account || problem) return;
setAccounts(agent, [...accounts, account]);
onClose();
onAdded(account);
}
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<ProviderIcon agent={agent} className="size-5" />
Add {agentLabel(agent)} account
</DialogTitle>
<DialogDescription>
{agent === "claude"
? "This account shares your Claude Code settings, skills, plugins, user MCP servers and history."
: "This account shares your Codex settings, permissions, instructions and skills. It keeps its own sign-in, conversations and generated memories."}
</DialogDescription>
</DialogHeader>
<form
className="space-y-1.5"
onSubmit={(event) => {
event.preventDefault();
add();
}}
>
<Input
autoFocus
value={name}
placeholder="e.g., work"
aria-label="Account name"
autoComplete="off"
spellCheck={false}
onChange={(event) => setName(event.target.value.toLowerCase())}
/>
<p className={`text-xs ${problem ? "text-destructive" : "text-muted-foreground"}`}>
{problem || (
<>
{agentLabel(agent)} keeps its sign-in in{" "}
<span className="font-mono">
~/.{agent}-{account || "name"}
</span>
, on this computer and on SSH hosts.
</>
)}
</p>
</form>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button type="button" disabled={!account || Boolean(problem)} onClick={add}>
Add account
</Button>
</DialogFooter>
</>
);
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.