Watch
0
0
Fork
You've already forked hyperhive
0

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:
iris 2026-11-20 20:23:08 +01:00 • committed by mara
commit da25f0a1c3
5 changed files with 545 additions and 65 deletions

View file

@ -0,0 +1,162 @@
// <AgentApiKey> — how one agent is billed, in the claude login row: whether an
// API key is stored (`GET …/backend-secrets`, which never returns the key), a
// button that sets or replaces it, one that removes it, and the advanced
// backend settings. With a key stored the agent tries the key instead of
// waiting for a claude login; a change reaches it at its next restart.
//
// Its state belongs to one agent: callers key it by agent name.
import { useCallback, useEffect, useState } from "preact/hooks";
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
import { Badge } from "@hive/shared/badge.js";
import { ConfirmDialog } from "../../ui/confirm-dialog/ConfirmDialog.js";
import { Dialog } from "../../ui/dialog/Dialog.js";
import { showToast } from "../../ui/toast/toasts.js";
import { LinkApiKeyForm } from "../../pages/LinkApiKeyForm.js";
import { LinkBackendEnvForm } from "../../pages/LinkBackendEnvForm.js";
import { backendSecretsUrl } from "./account-api.js";
export function AgentApiKey({ hive, agent }: { hive: string; agent: string }) {
const [stored, setStored] = useState<boolean | null>(null);
const [error, setError] = useState<ProblemDetails | null>(null);
const [setting, setSetting] = useState(false);
const [advanced, setAdvanced] = useState(false);
const [confirming, setConfirming] = useState(false);
const [removing, setRemoving] = useState(false);
const [removeError, setRemoveError] = useState<ProblemDetails | null>(null);
const load = useCallback(async () => {
try {
const r = await fetch(backendSecretsUrl(hive, agent));
if (!r.ok) {
setError(await readApiError(r));
return;
}
setStored(((await r.json()) as { stored: boolean }).stored);
setError(null);
} catch (err) {
setError({ detail: String(err) });
}
}, [hive, agent]);
useEffect(() => {
void load();
}, [load]);
async function confirmRemove() {
setRemoving(true);
setRemoveError(null);
try {
const r = await fetch(backendSecretsUrl(hive, agent), {
method: "DELETE",
});
// Nothing stored is the end state the removal wanted.
if (!r.ok && r.status !== 404) {
setRemoveError(await readApiError(r));
return;
}
setConfirming(false);
showToast(`removed the API key for ${agent}`);
await load();
} catch (err) {
setRemoveError({ detail: String(err) });
} finally {
setRemoving(false);
}
}
return (
<div class="agent-api-key">
{error ? (
<ApiErrorPanel context="reading the API key failed" problem={error} />
) : null}
{stored === null ? null : (
<>
<Badge
label="API key"
value={stored ? "stored" : "none"}
tone={stored ? "positive" : "neutral"}
/>{" "}
</>
)}
<Badge
variant="quiet"
value={stored ? "replace API key" : "set API key"}
title={`bill ${agent} through the API with a key instead of a claude login`}
onClick={() => setSetting(true)}
/>{" "}
{stored ? (
<>
<Badge
variant="quiet"
value="remove API key"
title={`remove the stored API key for ${agent}`}
onClick={() => {
setRemoveError(null);
setConfirming(true);
}}
/>{" "}
</>
) : null}
<Badge
variant="quiet"
value="advanced"
title={`edit ${agent}'s backend configuration and secrets as env files`}
onClick={() => setAdvanced(true)}
/>
<Dialog
open={setting}
onClose={() => setSetting(false)}
label="set the API key"
plain
>
{setting ? (
<LinkApiKeyForm
hive={hive}
agent={agent}
onSaved={() => void load()}
onClose={() => setSetting(false)}
/>
) : null}
</Dialog>
<Dialog
open={advanced}
onClose={() => {
setAdvanced(false);
void load();
}}
label="backend settings (advanced)"
plain
>
{advanced ? (
<LinkBackendEnvForm
hive={hive}
agent={agent}
onClose={() => {
setAdvanced(false);
void load();
}}
/>
) : null}
</Dialog>
<ConfirmDialog
open={confirming}
label={`remove the API key for ${agent}`}
confirmLabel={removing ? "removing…" : "remove API key"}
danger
confirmDisabled={removing}
onCancel={() => setConfirming(false)}
onConfirm={() => void confirmRemove()}
>
<p>
Remove the stored API key for <strong>{agent}</strong>? The key cannot
be read back, so it cannot be restored. The agent stops loading it at
its next restart.
</p>
{removeError ? (
<ApiErrorPanel context="remove failed" problem={removeError} />
) : null}
</ConfirmDialog>
</div>
);
}

View file

@ -17,6 +17,7 @@ import { ExternalLink } from "../../ui/external-link/ExternalLink.js";
import { Hint } from "../../ui/hint/Hint.js";
import { RelativeTime } from "../../ui/relative-time/RelativeTime.js";
import type { AgentActions } from "../roster/AgentActions.js";
import { AgentApiKey } from "./AgentApiKey.js";
import { AgentClaudeLogin } from "./AgentClaudeLogin.js";
import { AgentInference } from "./AgentInference.js";
import { StatusMessage } from "./StatusMessage.js";
@ -187,7 +188,7 @@ function OverviewRows({
);
}
function SettingsRows({ agent }: { agent: string }) {
function SettingsRows({ agent, hive }: { agent: string; hive: string | null }) {
return (
<>
<dt>inference</dt>
@ -206,6 +207,9 @@ function SettingsRows({ agent }: { agent: string }) {
<dt>claude login</dt>
<dd>
<AgentClaudeLogin key={agent} agent={agent} />
{hive ? (
<AgentApiKey key={`${hive}/${agent}`} hive={hive} agent={agent} />
) : null}
</dd>
</>
);
@ -312,7 +316,7 @@ function SectionBody({
return (
<>
<dl class="ui-agent-detail-fields">
<SettingsRows agent={row.name} />
<SettingsRows agent={row.name} hive={row.hive} />
<AccountsRows row={row} actions={actions} />
</dl>
<LinkAccountAction row={row} actions={actions} />

View file

@ -15,7 +15,7 @@ const OPTIONS: DropdownOption[] = [
{ value: "forge", label: "forge account" },
{ value: "gitlab", label: "gitlab account" },
{ value: "github", label: "github account" },
{ value: "backend", label: "API-key environment" },
{ value: "backend", label: "backend settings (advanced)" },
];
export function LinkAccountMenu({
@ -42,7 +42,7 @@ export function LinkAccountMenu({
disabled={!hasHive}
title={
hasHive
? `link a matrix, forge, gitlab or github account, or set the API-key environment, for ${agent}`
? `link a matrix, forge, gitlab or github account, or edit the advanced backend settings, for ${agent}`
: "no hive on record for this agent — nothing to link against"
}
/>

View file

@ -70,3 +70,28 @@ export function checkLink(
if (token === "") return "token is required";
return null;
}
/** GET, PUT and DELETE target for the agent's backend configuration. */
export function backendEnvUrl(hive: string, agent: string): string {
return `${agentBase(hive, agent)}/backend-env`;
}
/** GET (is any stored), PUT and DELETE target for the agent's backend secrets. */
export function backendSecretsUrl(hive: string, agent: string): string {
return `${agentBase(hive, agent)}/backend-secrets`;
}
/** The secrets text that holds `key` as the agent's Anthropic API key. */
export function apiKeySecrets(key: string): string {
return `ANTHROPIC_API_KEY=${key.trim()}`;
}
/** A message for a key the controller would refuse anyway, or null. */
export function checkApiKey(key: string): string | null {
const k = key.trim();
if (k === "") return "key is required";
if (/[\s"'\\]/.test(k)) {
return "a key has no spaces, quotes or backslashes";
}
return null;
}

View file

@ -277,7 +277,7 @@ export function useAgentActions(
<Dialog
open={backendTarget !== null}
onClose={closeBackend}
label="set the API-key environment"
label="backend settings (advanced)"
plain
>
{backendTarget?.hive ? (