Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/pages/LinkApiKeyForm.tsx
iris da25f0a1c3 swarm ui: an agent's API key is set from its claude login row, and the advanced backend settings show the configuration and the secrets apart
The claude login row of an opened agent gains an API key line: a badge saying whether a key is stored, set or replace and remove, and advanced. Setting a key sends PUT on the backend-secrets route as ANTHROPIC_API_KEY=<key>; the key is never read back. The old API-key environment dialog becomes the advanced backend settings, with two sections: the configuration, which starts with the stored text and saves or removes it, and the secrets, whose box starts empty and replaces the stored secrets only when filled and saved. The link menu's entry is renamed to match, account-api.ts gets the route helpers and a key check with tests, and the swarm UI doc describes the row.
2026-11-20 21:23:05 +01:00

94 lines
2.7 KiB
TypeScript

// <LinkApiKeyForm> — sets an agent's API key. The key goes to the agent's
// backend secrets in the swarm secret store as `ANTHROPIC_API_KEY=<key>`
// (`PUT …/backend-secrets`); no route hands it back, so the box starts empty
// and a save replaces whatever is stored. The advanced form
// (`LinkBackendEnvForm`) is where the rest of the backend environment lives.
import { useState } from "preact/hooks";
import { Panel } from "../ui/panel/Panel.js";
import { TextField } from "../ui/text-field/TextField.js";
import { Button } from "../ui/button/Button.js";
import { FormStack } from "../ui/form-stack/FormStack.js";
import { FormResult } from "../ui/submit/FormResult.js";
import { Hint } from "../ui/hint/Hint.js";
import { useSubmit } from "../ui/submit/useSubmit.js";
import {
apiKeySecrets,
backendSecretsUrl,
checkApiKey,
} from "../agent/detail/account-api.js";
export function LinkApiKeyForm({
hive,
agent,
onSaved,
onClose,
}: {
hive: string;
agent: string;
/** Called after a save, so the row can show the key as stored. */
onSaved?: () => void;
onClose?: () => void;
}) {
const [key, setKey] = useState("");
const { state, submitting, run, fail } = useSubmit();
function save(e: Event) {
e.preventDefault();
const problem = checkApiKey(key);
if (problem !== null) {
fail({ detail: problem });
return;
}
void run(
() =>
fetch(backendSecretsUrl(hive, agent), {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ env: apiKeySecrets(key) }),
}),
{
// The store holds the key; nothing here needs it.
onDone: () => {
setKey("");
onSaved?.();
},
},
);
}
return (
<Panel title={`set the API key — ${agent}`} icon="🔑" onClose={onClose}>
<p>
Bills <strong>{agent}</strong> through the API instead of a Claude
login.
</p>
<FormStack onSubmit={save}>
<TextField
id="api-key"
label="API key"
type="password"
value={key}
required
placeholder="sk-ant-…"
onInput={setKey}
/>
<Hint>
Stored in the swarm secret store and never shown again; a save
replaces the stored key. The agent loads it at its next restart.
</Hint>
<Button variant="primary" type="submit" disabled={submitting}>
{submitting ? "saving…" : "save"}
</Button>
</FormStack>
<FormResult
state={state}
done={() => (
<>
stored the API key for <strong>{agent}</strong>
</>
)}
failed="failed to store the API key"
/>
</Panel>
);
}