Spaces:
Sleeping
Sleeping
File size: 6,363 Bytes
b972454 | 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 | import { useState } from "react";
interface ProvisionResult {
space_url: string;
space_repo: string;
rotation_secret: string;
username: string;
oauth_token: string;
existing?: boolean;
}
interface Provider {
id: string;
label: string;
envKey: string;
keyUrl: string;
hint: string;
placeholder: string;
}
const PROVIDERS: Provider[] = [
{
id: "groq",
label: "Groq",
envKey: "GROQ_API_KEY",
keyUrl: "https://console.groq.com/keys",
hint: "Fast inference, very generous free tier.",
placeholder: "gsk_…",
},
{
id: "nvidia",
label: "NVIDIA AI",
envKey: "NVIDIA_API_KEY",
keyUrl: "https://build.nvidia.com",
hint: "Free credits for Llama, Mixtral, and more.",
placeholder: "nvapi-…",
},
{
id: "google",
label: "Google AI Studio",
envKey: "GOOGLE_API_KEY",
keyUrl: "https://aistudio.google.com/apikey",
hint: "Gemini Flash & Pro — free tier.",
placeholder: "AIza…",
},
{
id: "openrouter",
label: "OpenRouter",
envKey: "OPENROUTER_API_KEY",
keyUrl: "https://openrouter.ai/keys",
hint: "Access 100+ models; many have free quotas.",
placeholder: "sk-or-…",
},
{
id: "anthropic",
label: "Anthropic (Claude) — paid",
envKey: "ANTHROPIC_API_KEY",
keyUrl: "https://console.anthropic.com/settings/keys",
hint: "Optional. Unlocks the full Claude agent tier (paid key).",
placeholder: "sk-ant-…",
},
];
export function ProviderKeyWizard({
provision,
onDone,
}: {
provision: ProvisionResult;
onDone: () => void;
}) {
const [values, setValues] = useState<Record<string, string>>({});
const [statuses, setStatuses] = useState<Record<string, "idle" | "saving" | "ok" | "err">>({});
const [errors, setErrors] = useState<Record<string, string>>({});
const setKey = (id: string, val: string) =>
setValues((v) => ({ ...v, [id]: val }));
async function saveKey(provider: Provider) {
const val = (values[provider.id] || "").trim();
if (!val) return;
setStatuses((s) => ({ ...s, [provider.id]: "saving" }));
setErrors((e) => ({ ...e, [provider.id]: "" }));
try {
const res = await fetch("/oauth/set-provider-key", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
oauth_token: provision.oauth_token,
repo: provision.space_repo,
key_name: provider.envKey,
key_value: val,
}),
});
if (!res.ok) {
const j = await res.json().catch(() => ({}));
throw new Error((j as { error?: string }).error || `HTTP ${res.status}`);
}
setStatuses((s) => ({ ...s, [provider.id]: "ok" }));
} catch (e) {
setStatuses((s) => ({ ...s, [provider.id]: "err" }));
setErrors((er) => ({ ...er, [provider.id]: (e as Error).message }));
}
}
const anyOk = Object.values(statuses).some((s) => s === "ok");
// Returning users already configured their keys on a previous visit —
// never block them behind a step they've completed before.
const canContinue = anyOk || !!provision.existing;
const field =
"flex-1 bg-surface border border-border rounded-xl px-3 py-2 text-[13px] outline-none focus:border-accent min-w-0";
return (
<div className="p-4 space-y-5 max-w-xl mx-auto overflow-y-auto">
<div>
<h2 className="text-lg font-semibold">
{provision.existing ? `Welcome back, ${provision.username}` : "Add a provider key"}
</h2>
<p className="text-sm text-muted mt-1">
{provision.existing ? (
<>
Your Space at <span className="text-accent">{provision.space_url}</span>{" "}
has been re-linked with a fresh secret (it restarts briefly). Your
existing provider keys are untouched — add more below, or continue.
</>
) : (
<>
Your Space is provisioned at{" "}
<span className="text-accent">{provision.space_url}</span>. Add at
least one free API key so the judges have models to call.
</>
)}
</p>
</div>
{PROVIDERS.map((p) => {
const st = statuses[p.id] ?? "idle";
return (
<div key={p.id} className="space-y-1">
<div className="flex items-center gap-2">
<span className="text-sm font-medium">{p.label}</span>
<span className="text-[11px] text-muted">{p.hint}</span>
{st === "ok" && <span className="ml-auto text-green-400 text-xs">saved ✓</span>}
</div>
<div className="flex gap-2">
<input
className={field}
type="password"
placeholder={p.placeholder}
value={values[p.id] ?? ""}
onChange={(e) => setKey(p.id, e.target.value)}
autoCapitalize="none"
autoCorrect="off"
disabled={st === "ok"}
/>
<a
href={p.keyUrl}
target="_blank"
rel="noopener noreferrer"
className="px-3 py-2 rounded-xl border border-border text-[12px] text-muted whitespace-nowrap"
>
Get key
</a>
<button
onClick={() => saveKey(p)}
disabled={st === "saving" || st === "ok" || !(values[p.id] || "").trim()}
className="px-3 py-2 rounded-xl bg-accent text-white text-[12px] disabled:opacity-40"
>
{st === "saving" ? "…" : "Add"}
</button>
</div>
{st === "err" && errors[p.id] && (
<p className="text-[11px] text-red-400">{errors[p.id]}</p>
)}
</div>
);
})}
<div className="pt-2 border-t border-border">
<button
onClick={onDone}
disabled={!canContinue}
className="w-full py-3 rounded-xl bg-accent text-white font-medium disabled:opacity-40"
>
Open my Space →
</button>
{!canContinue && (
<p className="text-[11px] text-muted text-center mt-1">
Add at least one key above to continue.
</p>
)}
</div>
</div>
);
}
|