|
| 1 | +import { useCallback, useEffect, useState } from "react"; |
| 2 | +import { Check, KeyRound, ShieldCheck, Trash2 } from "lucide-react"; |
| 3 | + |
| 4 | +import { |
| 5 | + fetchOperatorCredential, |
| 6 | + writeOperatorCredential, |
| 7 | + type OperatorCredential, |
| 8 | +} from "../../data/chat"; |
| 9 | +import { type WorkspaceTranslate, useWorkspaceI18n } from "./i18n"; |
| 10 | + |
| 11 | +function localizeStatus(status: OperatorCredential["status"], t: WorkspaceTranslate) { |
| 12 | + if (status === "invalid") return t("machine.credentialInvalid"); |
| 13 | + return t(status === "configured" ? "machine.credentialConfigured" : "machine.credentialAbsent"); |
| 14 | +} |
| 15 | + |
| 16 | +function localizeSource(source: string, t: WorkspaceTranslate) { |
| 17 | + if (source === "machine_store") return t("machine.credentialSourceMachine"); |
| 18 | + if (source === "service_environment") return t("machine.credentialSourceEnvironment"); |
| 19 | + return t("machine.credentialSourceUnset"); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * The one place a person stores the operator model credential. |
| 24 | + * |
| 25 | + * The key is write-only end to end: this form submits one, and every readback |
| 26 | + * it renders is the redacted projection, so the browser can configure a |
| 27 | + * credential it is never able to display again. The endpoint is not a secret |
| 28 | + * and reads back as itself. |
| 29 | + */ |
| 30 | +export function OperatorCredentialSettings() { |
| 31 | + const { t } = useWorkspaceI18n(); |
| 32 | + const [credential, setCredential] = useState<OperatorCredential | null>(null); |
| 33 | + const [apiKey, setApiKey] = useState(""); |
| 34 | + const [baseUrl, setBaseUrl] = useState(""); |
| 35 | + const [busy, setBusy] = useState<"" | "load" | "store">(""); |
| 36 | + const [error, setError] = useState<string | null>(null); |
| 37 | + const [notice, setNotice] = useState<string | null>(null); |
| 38 | + |
| 39 | + const reload = useCallback(async () => { |
| 40 | + setBusy("load"); |
| 41 | + try { |
| 42 | + const loaded = await fetchOperatorCredential(); |
| 43 | + setCredential(loaded); |
| 44 | + // Only a non-secret field is prefilled; the key never round-trips. |
| 45 | + setBaseUrl(String(loaded.base_url.value ?? "")); |
| 46 | + } catch (cause) { |
| 47 | + setError(cause instanceof Error ? cause.message : t("machine.credentialError")); |
| 48 | + } finally { |
| 49 | + setBusy(""); |
| 50 | + } |
| 51 | + }, [t]); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + void reload(); |
| 55 | + }, [reload]); |
| 56 | + |
| 57 | + async function submit(update: Parameters<typeof writeOperatorCredential>[0], done: string) { |
| 58 | + setBusy("store"); |
| 59 | + setError(null); |
| 60 | + setNotice(null); |
| 61 | + try { |
| 62 | + const stored = await writeOperatorCredential(update); |
| 63 | + setCredential(stored); |
| 64 | + setBaseUrl(String(stored.base_url.value ?? "")); |
| 65 | + setApiKey(""); |
| 66 | + setNotice(done); |
| 67 | + } catch (cause) { |
| 68 | + setError(cause instanceof Error ? cause.message : t("machine.credentialError")); |
| 69 | + } finally { |
| 70 | + setBusy(""); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (!credential && busy === "load") { |
| 75 | + return <div className="personal-machine-loading" role="status">{t("common.loading")}</div>; |
| 76 | + } |
| 77 | + |
| 78 | + const keyLabel = credential |
| 79 | + ? `${localizeStatus(credential.provider_key.configured ? "configured" : "absent", t)} · ${localizeSource(credential.provider_key.source, t)}` |
| 80 | + : ""; |
| 81 | + const urlLabel = credential |
| 82 | + ? `${credential.base_url.value ?? t("machine.credentialAbsent")} · ${localizeSource(credential.base_url.source, t)}` |
| 83 | + : ""; |
| 84 | + |
| 85 | + return ( |
| 86 | + <section className="personal-operator-credential" data-testid="operator-credential-settings"> |
| 87 | + <header> |
| 88 | + <KeyRound aria-hidden size={17} /> |
| 89 | + <div> |
| 90 | + <strong>{t("machine.credentialTitle")}</strong> |
| 91 | + <p>{t("machine.credentialDescription")}</p> |
| 92 | + </div> |
| 93 | + <span className="personal-operator-credential-status"> |
| 94 | + {credential ? localizeStatus(credential.status, t) : t("common.loading")} |
| 95 | + </span> |
| 96 | + </header> |
| 97 | + |
| 98 | + {credential ? ( |
| 99 | + <dl className="personal-operator-credential-readback"> |
| 100 | + <div> |
| 101 | + <dt>{t("machine.credentialApiKey")}</dt> |
| 102 | + <dd>{keyLabel}</dd> |
| 103 | + </div> |
| 104 | + <div> |
| 105 | + <dt>{t("machine.credentialFingerprint")}</dt> |
| 106 | + <dd><code>{credential.provider_key.fingerprint ?? t("common.none")}</code></dd> |
| 107 | + </div> |
| 108 | + <div> |
| 109 | + <dt>{t("machine.credentialBaseUrl")}</dt> |
| 110 | + <dd>{urlLabel}</dd> |
| 111 | + </div> |
| 112 | + </dl> |
| 113 | + ) : null} |
| 114 | + |
| 115 | + {credential?.status === "invalid" && credential.repair ? ( |
| 116 | + <p className="personal-machine-error" role="alert">{credential.repair}</p> |
| 117 | + ) : null} |
| 118 | + |
| 119 | + <label htmlFor="operator-credential-api-key"> |
| 120 | + <span>{t("machine.credentialApiKey")}</span> |
| 121 | + <input |
| 122 | + autoComplete="off" |
| 123 | + disabled={Boolean(busy)} |
| 124 | + id="operator-credential-api-key" |
| 125 | + onChange={(event) => setApiKey(event.target.value)} |
| 126 | + placeholder={t("machine.credentialApiKeyPlaceholder")} |
| 127 | + type="password" |
| 128 | + value={apiKey} |
| 129 | + /> |
| 130 | + </label> |
| 131 | + |
| 132 | + <label htmlFor="operator-credential-base-url"> |
| 133 | + <span>{t("machine.credentialBaseUrl")}</span> |
| 134 | + <input |
| 135 | + autoComplete="off" |
| 136 | + disabled={Boolean(busy)} |
| 137 | + id="operator-credential-base-url" |
| 138 | + onChange={(event) => setBaseUrl(event.target.value)} |
| 139 | + placeholder={t("machine.credentialBaseUrlPlaceholder")} |
| 140 | + type="text" |
| 141 | + value={baseUrl} |
| 142 | + /> |
| 143 | + </label> |
| 144 | + |
| 145 | + {error ? <p className="personal-machine-error" role="alert">{error}</p> : null} |
| 146 | + {notice ? <p className="personal-machine-notice" role="status" aria-live="polite"><Check aria-hidden size={16} />{notice}</p> : null} |
| 147 | + |
| 148 | + {/* Not `personal-capability-actions`: the browser smoke treats that class |
| 149 | + as the capability editor's own action row, and this panel renders on |
| 150 | + the same page. */} |
| 151 | + <footer className="personal-operator-credential-actions"> |
| 152 | + <button |
| 153 | + className="is-primary" |
| 154 | + disabled={Boolean(busy) || (!apiKey.trim() && !baseUrl.trim())} |
| 155 | + onClick={() => void submit( |
| 156 | + { |
| 157 | + ...(apiKey.trim() ? { provider_key: apiKey } : {}), |
| 158 | + ...(baseUrl.trim() ? { base_url: baseUrl } : {}), |
| 159 | + }, |
| 160 | + t("machine.credentialStored"), |
| 161 | + )} |
| 162 | + type="button" |
| 163 | + > |
| 164 | + {busy === "store" ? t("common.loading") : t("machine.credentialStore")} |
| 165 | + </button> |
| 166 | + <button |
| 167 | + disabled={Boolean(busy) || credential?.provider_key.configured !== true} |
| 168 | + onClick={() => void submit({ clear_provider_key: true }, t("machine.credentialCleared"))} |
| 169 | + type="button" |
| 170 | + > |
| 171 | + <Trash2 aria-hidden size={15} />{t("machine.credentialClearKey")} |
| 172 | + </button> |
| 173 | + <button |
| 174 | + disabled={Boolean(busy) || credential?.base_url.configured !== true} |
| 175 | + onClick={() => void submit({ clear_base_url: true }, t("machine.credentialCleared"))} |
| 176 | + type="button" |
| 177 | + > |
| 178 | + <Trash2 aria-hidden size={15} />{t("machine.credentialClearUrl")} |
| 179 | + </button> |
| 180 | + </footer> |
| 181 | + |
| 182 | + <details className="personal-capability-scope-note"> |
| 183 | + <summary><ShieldCheck aria-hidden size={17} />{t("machine.credentialTitle")}</summary> |
| 184 | + <p>{t("machine.credentialBoundary")}</p> |
| 185 | + </details> |
| 186 | + </section> |
| 187 | + ); |
| 188 | +} |
0 commit comments