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.
This commit is contained in:
parent
33fdd5205e
commit
da25f0a1c3
3 changed files with 545 additions and 65 deletions
94
frontend/packages/swarm-ui/src/pages/LinkApiKeyForm.tsx
Normal file
94
frontend/packages/swarm-ui/src/pages/LinkApiKeyForm.tsx
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
// <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>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
.backend-env-heading {
|
||||
margin: 1.25em 0 0;
|
||||
}
|
||||
|
|
@ -1,24 +1,32 @@
|
|||
// <LinkBackendEnvForm> — writes an agent's backend environment (the text of
|
||||
// a systemd `EnvironmentFile`) into the swarm secret store, or removes it.
|
||||
// PUTs and DELETEs `/api/hives/{hive}/agents/{agent}/backend-env` — 204 on
|
||||
// success, 400/404/500 as `problem+json`, shown via `ApiErrorPanel` like
|
||||
// `LinkGithubAccountForm`.
|
||||
// <LinkBackendEnvForm> — an agent's advanced backend settings: the two
|
||||
// objects the agent loads, configuration first and secrets after it, so a
|
||||
// secret wins over a variable of the same name. Both are the text of a
|
||||
// systemd `EnvironmentFile`.
|
||||
//
|
||||
// A blind set: no route hands the text back, so the box never starts with the
|
||||
// stored value. A save replaces whatever is stored. Whether one is stored
|
||||
// shows on the agent panel (`LinkedAccounts`).
|
||||
import { useState } from "preact/hooks";
|
||||
// The configuration (`…/backend-env`) reads back as stored, so its box starts
|
||||
// with the stored text. The secrets (`…/backend-secrets`) are write-only: their
|
||||
// box starts empty, and the stored secrets change only when it is filled and
|
||||
// saved (or removed). Whether any are stored shows beside the box.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { Panel } from "../ui/panel/Panel.js";
|
||||
import { TextAreaField } from "../ui/text-area-field/TextAreaField.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 { ActionRow } from "../ui/action-row/ActionRow.js";
|
||||
import { SectionHeading } from "../ui/section-heading/SectionHeading.js";
|
||||
import { useConfirm } from "../ui/confirm-dialog/useConfirm.js";
|
||||
import { useSubmit } from "../ui/submit/useSubmit.js";
|
||||
import {
|
||||
backendEnvUrl,
|
||||
backendSecretsUrl,
|
||||
} from "../agent/detail/account-api.js";
|
||||
import "./LinkBackendEnvForm.css";
|
||||
|
||||
// A swarm-controller without the route answers 404 with no problem body.
|
||||
const NO_ROUTE =
|
||||
"this swarm-controller has no backend-env route, or nothing is stored";
|
||||
"this swarm-controller has no such route, or nothing is stored";
|
||||
|
||||
export function LinkBackendEnvForm({
|
||||
hive,
|
||||
|
|
@ -29,32 +37,57 @@ export function LinkBackendEnvForm({
|
|||
agent: string;
|
||||
onClose?: () => void;
|
||||
}) {
|
||||
const [env, setEnv] = useState("");
|
||||
// The success line is what the request did, so it is the done value.
|
||||
const { state, submitting: busy, run } = useSubmit<string>();
|
||||
const [config, setConfig] = useState("");
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [secrets, setSecrets] = useState("");
|
||||
const [secretsStored, setSecretsStored] = useState<boolean | null>(null);
|
||||
const configRun = useSubmit<string>();
|
||||
const secretsRun = useSubmit<string>();
|
||||
const [confirm, confirmDialog] = useConfirm();
|
||||
|
||||
async function removeEnv() {
|
||||
const yes = await confirm({
|
||||
label: "remove the API-key environment",
|
||||
confirmLabel: "remove",
|
||||
danger: true,
|
||||
body: (
|
||||
<p>
|
||||
Remove the stored API-key environment for <strong>{agent}</strong>?
|
||||
The stored text cannot be read back, so it cannot be restored; paste
|
||||
it again to set it.
|
||||
</p>
|
||||
),
|
||||
});
|
||||
if (yes) send("DELETE", "removed the API-key environment for");
|
||||
}
|
||||
// The stored configuration, or nothing when none is stored (a 404).
|
||||
useEffect(() => {
|
||||
let live = true;
|
||||
void (async () => {
|
||||
try {
|
||||
const r = await fetch(backendEnvUrl(hive, agent));
|
||||
if (live && r.ok) setConfig(((await r.json()) as { env: string }).env);
|
||||
} catch {
|
||||
// The box stays empty; a save shows the real problem.
|
||||
}
|
||||
if (live) setLoaded(true);
|
||||
})();
|
||||
return () => {
|
||||
live = false;
|
||||
};
|
||||
}, [hive, agent]);
|
||||
|
||||
function send(method: "PUT" | "DELETE", done: string) {
|
||||
void run(
|
||||
async function loadSecretsStored() {
|
||||
try {
|
||||
const r = await fetch(backendSecretsUrl(hive, agent));
|
||||
if (r.ok)
|
||||
setSecretsStored(((await r.json()) as { stored: boolean }).stored);
|
||||
} catch {
|
||||
setSecretsStored(null);
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
void loadSecretsStored();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- once per agent
|
||||
}, [hive, agent]);
|
||||
|
||||
function send(
|
||||
target: ReturnType<typeof useSubmit<string>>,
|
||||
url: string,
|
||||
method: "PUT" | "DELETE",
|
||||
env: string,
|
||||
done: string,
|
||||
after: () => void,
|
||||
) {
|
||||
void target.run(
|
||||
() =>
|
||||
fetch(
|
||||
`/api/hives/${encodeURIComponent(hive)}/agents/${encodeURIComponent(agent)}/backend-env`,
|
||||
url,
|
||||
method === "PUT"
|
||||
? {
|
||||
method,
|
||||
|
|
@ -67,58 +100,180 @@ export function LinkBackendEnvForm({
|
|||
parse: () => done,
|
||||
refine: (r, problem) =>
|
||||
r.status === 404 && !problem.detail ? { detail: NO_ROUTE } : problem,
|
||||
// The store holds the text; nothing here needs it.
|
||||
onDone: () => setEnv(""),
|
||||
onDone: after,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function save(e: Event) {
|
||||
e.preventDefault();
|
||||
send("PUT", "stored the API-key environment for");
|
||||
async function removeConfig() {
|
||||
const yes = await confirm({
|
||||
label: "remove the backend configuration",
|
||||
confirmLabel: "remove",
|
||||
danger: true,
|
||||
body: (
|
||||
<p>
|
||||
Remove the stored configuration for <strong>{agent}</strong>? An agent
|
||||
whose <code>backendEnvironmentFile</code> has content stores that file
|
||||
again.
|
||||
</p>
|
||||
),
|
||||
});
|
||||
if (yes) {
|
||||
send(
|
||||
configRun,
|
||||
backendEnvUrl(hive, agent),
|
||||
"DELETE",
|
||||
"",
|
||||
"removed the configuration of",
|
||||
() => setConfig(""),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function removeSecrets() {
|
||||
const yes = await confirm({
|
||||
label: "remove the backend secrets",
|
||||
confirmLabel: "remove",
|
||||
danger: true,
|
||||
body: (
|
||||
<p>
|
||||
Remove the stored secrets (the API key) for <strong>{agent}</strong>?
|
||||
The stored text cannot be read back, so it cannot be restored.
|
||||
</p>
|
||||
),
|
||||
});
|
||||
if (yes) {
|
||||
send(
|
||||
secretsRun,
|
||||
backendSecretsUrl(hive, agent),
|
||||
"DELETE",
|
||||
"",
|
||||
"removed the secrets of",
|
||||
() => {
|
||||
setSecrets("");
|
||||
void loadSecretsStored();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Panel
|
||||
title={`set the API-key environment — ${agent}`}
|
||||
title={`backend settings (advanced) — ${agent}`}
|
||||
icon="🔗"
|
||||
onClose={onClose}
|
||||
>
|
||||
<p>
|
||||
Writes <code>NAME=VALUE</code> lines (for example{" "}
|
||||
<code>ANTHROPIC_API_KEY</code> and <code>ANTHROPIC_BASE_URL</code>) to
|
||||
the swarm secret store. Saving replaces whatever is stored, including
|
||||
text the agent wrote itself. The agent fetches it within two minutes and
|
||||
loads it at its next restart; it wins over the agent's{" "}
|
||||
<code>backendEnvironmentFile</code>. Remove deletes the stored text; an
|
||||
agent whose <code>backendEnvironmentFile</code> has content stores that
|
||||
file again.
|
||||
Two env files, loaded at the agent's next restart: the
|
||||
configuration first, then the secrets, so a secret wins over a variable
|
||||
of the same name. For just an API key, use <strong>set API key</strong>.
|
||||
</p>
|
||||
<FormStack onSubmit={save}>
|
||||
<SectionHeading class="backend-env-heading">configuration</SectionHeading>
|
||||
<FormStack
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
send(
|
||||
configRun,
|
||||
backendEnvUrl(hive, agent),
|
||||
"PUT",
|
||||
config,
|
||||
"stored the configuration of",
|
||||
() => {},
|
||||
);
|
||||
}}
|
||||
>
|
||||
<TextAreaField
|
||||
id="backend-env-text"
|
||||
label="environment file text"
|
||||
value={env}
|
||||
id="backend-env-config"
|
||||
label="NAME=VALUE lines, such as ANTHROPIC_BASE_URL"
|
||||
value={config}
|
||||
required
|
||||
rows={6}
|
||||
placeholder="ANTHROPIC_API_KEY=…"
|
||||
onInput={setEnv}
|
||||
rows={5}
|
||||
placeholder={loaded ? "ANTHROPIC_BASE_URL=…" : "loading…"}
|
||||
onInput={setConfig}
|
||||
/>
|
||||
<Button variant="primary" type="submit" disabled={busy}>
|
||||
{busy ? "saving…" : "save"}
|
||||
</Button>
|
||||
<Button type="button" disabled={busy} onClick={() => void removeEnv()}>
|
||||
remove
|
||||
</Button>
|
||||
<ActionRow>
|
||||
<Button
|
||||
variant="primary"
|
||||
type="submit"
|
||||
disabled={configRun.submitting || !loaded}
|
||||
>
|
||||
{configRun.submitting ? "saving…" : "save"}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={configRun.submitting}
|
||||
onClick={() => void removeConfig()}
|
||||
>
|
||||
remove
|
||||
</Button>
|
||||
</ActionRow>
|
||||
</FormStack>
|
||||
<FormResult
|
||||
state={state}
|
||||
state={configRun.state}
|
||||
done={(message) => (
|
||||
<>
|
||||
{message} <strong>{agent}</strong>
|
||||
</>
|
||||
)}
|
||||
failed="failed to update the API-key environment"
|
||||
failed="failed to update the configuration"
|
||||
/>
|
||||
<SectionHeading class="backend-env-heading">secrets</SectionHeading>
|
||||
<FormStack
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
send(
|
||||
secretsRun,
|
||||
backendSecretsUrl(hive, agent),
|
||||
"PUT",
|
||||
secrets,
|
||||
"stored the secrets of",
|
||||
() => {
|
||||
setSecrets("");
|
||||
void loadSecretsStored();
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
<TextAreaField
|
||||
id="backend-env-secrets"
|
||||
label="NAME=VALUE lines, such as ANTHROPIC_API_KEY"
|
||||
value={secrets}
|
||||
rows={3}
|
||||
placeholder="ANTHROPIC_API_KEY=…"
|
||||
onInput={setSecrets}
|
||||
/>
|
||||
<Hint>
|
||||
{secretsStored === null
|
||||
? "Write-only."
|
||||
: secretsStored
|
||||
? "Secrets are stored and never shown. Saving here replaces them; leave the box empty to keep them."
|
||||
: "No secrets are stored."}
|
||||
</Hint>
|
||||
<ActionRow>
|
||||
<Button
|
||||
variant="primary"
|
||||
type="submit"
|
||||
disabled={secretsRun.submitting || secrets.trim() === ""}
|
||||
>
|
||||
{secretsRun.submitting ? "saving…" : "save"}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={secretsRun.submitting || secretsStored === false}
|
||||
onClick={() => void removeSecrets()}
|
||||
>
|
||||
remove
|
||||
</Button>
|
||||
</ActionRow>
|
||||
</FormStack>
|
||||
<FormResult
|
||||
state={secretsRun.state}
|
||||
done={(message) => (
|
||||
<>
|
||||
{message} <strong>{agent}</strong>
|
||||
</>
|
||||
)}
|
||||
failed="failed to update the secrets"
|
||||
/>
|
||||
{confirmDialog}
|
||||
</Panel>
|
||||
|
|
|
|||
Loading…
Reference in a new issue