* feat(gui): show and filter usage for each API key * feat(gateway): issue per-user caller keys and attribute usage * feat(gui): manage gateway users and filter caller-key usage * test: verify caller-key failover and document user management * refactor(gateway): use named caller keys without user accounts * feat(gui): manage named caller keys with existing account styles * fix: remove user breakdown from CLI and leftover gateway styling * fix(usage): drop stale user field from request response * feat(gateway): authenticate LAN calls with named caller keys * fix(gateway): preserve LAN key rotation in the named caller store * fix(gui): retain native LAN key copy and rotation controls * fix(gui): inherit the global theme for API key inputs * fix(usage): show client API keys using the existing terminology * fix(gateway): rotate named keys without changing usage identity * fix(gui): unify gateway keys and selectable connection examples Keep sharing controls in Settings and manage credentials only in Gateway. Refresh the named-key list on navigation, ignore stale reads after key actions, and generate all API examples from the selected address and enabled key. * fix(gui): keep caller key names literal in connection menus * fix(access): use Magpie as default key and drop user migration * refactor(gui): name caller key source and tests consistently * refactor(usage): split provider-key accounting out of caller-key PR * fix(gateway): preserve LAN compatibility and add headless key management * fix(gui): clarify gateway keys and confirm destructive actions * test(gateway): guard caller attribution under 120 concurrent requests * fix(gateway): keep open access, tolerate migration errors and revoke legacy keys * fix(backup): preserve named gateway keys and honor no-keys restores * fix(gui): keep local API-key semantics and disambiguate shared credentials * docs(gateway): keep review scope focused and append browser test documentation * fix(backup): retain existing default keys when old backups lack their store * fix(gateway): attribute video creation usage to the caller key * fix(access): explain default key revocation blocked by read-only settings * test(usage): preserve provider and caller identities after rebase * test(gui): reconcile caller key fixtures with merged main * docs(usage): clarify caller coverage and independent provider accounting * fix(gui): initialize fast members for model-shortcut groups Latest main introduced fast-member arrays but omitted the model shortcut's draft. The existing unlisted-models browser regression fails unchanged on main. Initialize fast to an empty list and assert a standard-speed group is saved, without relaxing existing assertions. * fix(access): retain legacy LAN access when the key store cannot be created Allow only the configured legacy credential before either migration marker exists. Refuse revoked mirrors, disabled matches and unreadable stores; keep the fallback's caller identity stable when migration later succeeds. Cover read-only configuration directories, remote credential transports and revocation. * fix(backup): save settings before replacing gateway credentials Leave the key store untouched when settings validation or writes fail. Restore the previous settings association when the key store refuses its write, and report a rollback failure separately. Cover invalid settings and read-only settings/store failures. * fix(gui): reset missing caller filters and focus key controls without scrolling * test(provider): synchronize Zhipu quota fixture observations
188 lines
6.9 KiB
JavaScript
188 lines
6.9 KiB
JavaScript
// Caller keys use the same account list and edit controls as provider keys.
|
|
let gatewayKeys = null, gatewayKeyDraft = null, gatewayKeysBusy = false;
|
|
let gatewayKeysRead = 0;
|
|
let connectURL = "", connectKeyID = "", connectSecret = null;
|
|
|
|
function connectPick(id, label, text, options, value, choose) {
|
|
const b = el("button", "val");
|
|
b.id = id;
|
|
b.type = "button";
|
|
b.setAttribute("aria-label", t(label));
|
|
b.append(el("code", "", text), svg(CHEV, 11, 1.6));
|
|
b.onclick = () => {
|
|
if (b.classList.contains("open")) return closeProtoMenu();
|
|
openProtoMenu(b, options, value, choose, label);
|
|
};
|
|
return b;
|
|
}
|
|
|
|
async function selectConnectKey(id) {
|
|
connectKeyID = id;
|
|
connectSecret = null;
|
|
renderConnect();
|
|
if (!id || !providers?.gateway.lan) return;
|
|
const key = gatewayKeys?.find((k) => k.id === id && !k.off);
|
|
if (!key) return;
|
|
try {
|
|
const out = await api("caller-keys/copy-key", { key: id });
|
|
if (connectKeyID !== id || gatewayKeys?.find((k) => k.id === id) !== key) return;
|
|
connectSecret = { id, secret: out.secret };
|
|
if (view === "gateway") renderConnect();
|
|
} catch (e) { status(t(e.message), "err"); }
|
|
}
|
|
async function loadGatewayKeys() {
|
|
const read = ++gatewayKeysRead;
|
|
try {
|
|
const out = await api("caller-keys");
|
|
if (read !== gatewayKeysRead) return;
|
|
// A fresh list also refreshes a selected credential, which may have rotated.
|
|
connectSecret = null;
|
|
gatewayKeys = out.keys || [];
|
|
renderGatewayKeys();
|
|
if (view === "gateway" && providers) selectConnectKey(connectKeyID);
|
|
} catch (e) {
|
|
if (read !== gatewayKeysRead) return;
|
|
$("#gatewayKeys").replaceChildren(el("div", "none", t(e.message)));
|
|
}
|
|
}
|
|
|
|
async function gatewayKeyAction(action, body) {
|
|
if (gatewayKeysBusy) return null;
|
|
gatewayKeysBusy = true;
|
|
gatewayKeysRead++;
|
|
try {
|
|
const out = await api("caller-keys/" + action, body);
|
|
gatewayKeysRead++;
|
|
connectSecret = null;
|
|
gatewayKeys = out.keys || [];
|
|
gatewayKeyDraft = null;
|
|
renderGatewayKeys();
|
|
if (view === "gateway" && providers) selectConnectKey(connectKeyID);
|
|
return out;
|
|
} catch (e) {
|
|
status(t(e.message), "err");
|
|
return null;
|
|
} finally {
|
|
gatewayKeysBusy = false;
|
|
}
|
|
}
|
|
|
|
function gatewayRename(k) {
|
|
const b = el("button", "n rename", k.name);
|
|
b.title = t("Rename");
|
|
b.onclick = () => {
|
|
const i = input(k.name, t("Name"));
|
|
i.className = "rename-in";
|
|
i.setAttribute("aria-label", t("Name"));
|
|
let finished = false;
|
|
const done = async (save) => {
|
|
if (finished) return;
|
|
finished = true;
|
|
// Keep the other controls in place when blur precedes their click.
|
|
i.replaceWith(b);
|
|
if (save && i.value.trim() !== k.name) await gatewayKeyAction("rename-key", { key: k.id, name: i.value });
|
|
};
|
|
i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") done(true); else if (e.key === "Escape") done(false); };
|
|
i.onblur = () => done(true);
|
|
b.replaceWith(i);
|
|
i.focus({ preventScroll: true });
|
|
};
|
|
return b;
|
|
}
|
|
|
|
function gatewayKeyForm() {
|
|
const box = el("div", "acc adding");
|
|
const name = input(gatewayKeyDraft, t("Key name, e.g. Laptop"));
|
|
name.setAttribute("aria-label", t("Gateway key name"));
|
|
name.oninput = () => { gatewayKeyDraft = name.value; };
|
|
const add = el("button", "text primary", t("Create"));
|
|
const go = async () => {
|
|
add.disabled = true;
|
|
const out = await gatewayKeyAction("add-key", { name: name.value });
|
|
if (!out) { add.disabled = false; return; }
|
|
status(t("Gateway key created. Use Copy on its row to connect a client."), "ok");
|
|
};
|
|
add.onclick = go;
|
|
name.onkeydown = (e) => {
|
|
e.stopPropagation();
|
|
if (e.key === "Enter" && !add.disabled) go();
|
|
else if (e.key === "Escape") { gatewayKeyDraft = null; renderGatewayKeys(); }
|
|
};
|
|
const cancel = el("button", "text", t("Cancel"));
|
|
cancel.onclick = () => { gatewayKeyDraft = null; renderGatewayKeys(); };
|
|
const fields = el("div", "kf");
|
|
fields.append(name);
|
|
const bar = el("div", "kb");
|
|
bar.append(el("span", "grow"), cancel, add);
|
|
box.append(fields, bar);
|
|
queueMicrotask(() => name.focus({ preventScroll: true }));
|
|
return box;
|
|
}
|
|
|
|
function copyCallerKeyBtn(k) {
|
|
const cp = copyBtn("", t("Gateway key"));
|
|
cp.setAttribute("aria-label", t("Copy gateway key"));
|
|
cp.onclick = async () => {
|
|
try {
|
|
const out = await api("caller-keys/copy-key", { key: k.id });
|
|
await copy(out.secret, t("Gateway key"), cp);
|
|
} catch (e) { status(t(e.message), "err"); }
|
|
};
|
|
return cp;
|
|
}
|
|
|
|
function renderGatewayKeys() {
|
|
const box = $("#gatewayKeys");
|
|
$("#gatewayKeysBlock").hidden = !providers?.gateway.lan;
|
|
box.replaceChildren();
|
|
$("#addGatewayKey").onclick = () => { gatewayKeyDraft = ""; renderGatewayKeys(); };
|
|
if (gatewayKeys === null) return;
|
|
const list = el("div", "accts");
|
|
for (const k of gatewayKeys) {
|
|
const row = el("div", "acc" + (k.off ? " off" : " in-use"));
|
|
row.dataset.key = k.id;
|
|
const tick = el("button", "dot tick");
|
|
tick.title = t(k.off ? "Enable key" : "Disable key");
|
|
tick.setAttribute("aria-label", tick.title);
|
|
if (!k.off) tick.append(svg(CHECK, 10, 2.2));
|
|
tick.onclick = () => gatewayKeyAction(k.off ? "on-key" : "off-key", { key: k.id });
|
|
const rm = el("button", "text quiet", t("Remove"));
|
|
rm.onclick = () => askGatewayKey(k, false);
|
|
const rotate = el("button", "text quiet", t("Rotate key"));
|
|
rotate.onclick = () => askGatewayKey(k, true);
|
|
row.append(tick, gatewayRename(k), el("span", "plan mono", k.masked), el("span", "grow"), copyCallerKeyBtn(k), rotate, rm);
|
|
list.append(row);
|
|
}
|
|
if (gatewayKeyDraft !== null) list.append(gatewayKeyForm());
|
|
else if (!gatewayKeys.length) list.append(el("div", "none", t("No gateway keys yet")));
|
|
box.append(list);
|
|
}
|
|
|
|
function askGatewayKey(k, rotate) {
|
|
const ed = el("div", "editor");
|
|
const head = el("div", "ehead");
|
|
head.append(el("b", "", t(rotate ? "Rotate gateway key?" : "Remove gateway key?")));
|
|
ed.append(head, el("p", "lib-confirm", t(rotate
|
|
? "Clients using {name} will need the new key. Its name and usage history stay the same."
|
|
: "Clients using {name} will lose access from other computers. Its usage history is kept.", { name: k.name })));
|
|
const bar = el("div", "bar");
|
|
const cancel = el("button", "text", t("Cancel"));
|
|
cancel.onclick = (e) => { e.stopPropagation(); closeConfirmAsk(); };
|
|
const go = el("button", "text primary", t(rotate ? "Rotate key" : "Remove"));
|
|
go.onclick = async (e) => {
|
|
e.stopPropagation();
|
|
if (go.disabled) return;
|
|
go.disabled = true;
|
|
const out = await gatewayKeyAction(rotate ? "rotate-key" : "remove-key", { key: k.id });
|
|
if (!out) { go.disabled = false; return; }
|
|
closeConfirmAsk();
|
|
if (rotate) status(t("Gateway key rotated. Update clients on other computers."), "ok");
|
|
};
|
|
bar.append(el("span", "grow"), cancel, go);
|
|
ed.append(bar);
|
|
confirmAsk = ed;
|
|
openModal(ed);
|
|
$("#modal").classList.add("lib");
|
|
cancel.focus({ preventScroll: true });
|
|
}
|