Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: adding a key shows a spinner and Adding… until it is done; the name field is narrower

yetonece5b23cdb493
diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css
index c01119a02..0065720e3 100644
--- a/internal/gui/assets/app.css
+++ b/internal/gui/assets/app.css
@@ -2508,7 +2508,7 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; }
.acc .plan.mono { font-family: var(--code-font, var(--code)); font-size: 10.5px; }
:where(html[data-fonts]) .acc .plan.mono { font-style: var(--code-style, inherit); font-weight: var(--code-weight, inherit); font-stretch: var(--code-stretch, 100%); }
.acc.adding { display: grid; gap: 8px; padding: 10px 10px 8px 11px; background: var(--card-2); }
-.acc.adding .kf { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: 6px; }
+.acc.adding .kf { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); gap: 6px; }
.acc.adding .kb { display: flex; align-items: center; gap: 4px; }
.acc.adding .kb .grow { flex: 1; }
.acc + .acc.adding { border-top: 1px solid var(--line-2); }
diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js
index e5f785d17..4e3e91610 100644
--- a/internal/gui/assets/app.js
+++ b/internal/gui/assets/app.js
@@ -14429,12 +14429,12 @@ function renderKeyAccounts(p) {
keys.oninput = () => { addingKey.key = keys.value; counted(); };
counted();
const go = async () => {
- if (!splitKeys(keys.value).length) return;
- add.classList.add("busy");
+ if (!splitKeys(keys.value).length || add.classList.contains("busy")) return;
+ saving(add, t("Adding…"));
try {
providers = await api("keys/import", { id: p.id, key: keys.value, protocol: addingKey.protocol || "" });
} catch (e) {
- add.classList.remove("busy");
+ doneSaving(add);
if (!editorError(e.message, "err")) status(e.message, "err");
return;
}
@@ -14478,12 +14478,15 @@ function renderKeyAccounts(p) {
renderProviders();
};
const add = el("button", "text primary", t("Add"));
+ // checking a key with its provider can take seconds: the button says
+ // so with a spinner, not only a fade
const go = async () => {
- add.classList.add("busy");
+ if (add.classList.contains("busy")) return;
+ saving(add, t("Adding…"));
const id = await keyFingerprint(key.value.trim() || p.anonKey || "");
if (await accountAction("keys/add", { id: p.id, name: name.value, key: key.value, protocol: addingKey.protocol || "" }, t("Key added — it takes over when the ones before it run out"))) {
addingKey = null; justAdded = id; renderProviders(); setTimeout(() => { justAdded = ""; }, 2000);
- }
+ } else doneSaving(add);
};
add.onclick = go;
for (const i of [name, key]) i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") go(); else if (e.key === "Escape") { addingKey = null; renderProviders(); } };
diff --git a/internal/gui/tests/key-import.test.cjs b/internal/gui/tests/key-import.test.cjs
index 0bec65e88..e0c207645 100644
--- a/internal/gui/tests/key-import.test.cjs
+++ b/internal/gui/tests/key-import.test.cjs
@@ -4,8 +4,11 @@
// one a line or separated by commas, counts them, and posts keys/import;
// pasting several into the one key's field opens it with them. The note
// says how many were added and how many it had. A key the gateway rests
-// says why and until when on its row. The page doesn't scroll. In English
-// and Chinese.
+// says why and until when on its row. Adding one key, which the gateway
+// checks with the provider and can take seconds, shows a spinner and
+// Adding… on its button until it is done, takes Enter once, and gives the
+// label back when the key is refused; the name field is the narrower one.
+// The page doesn't scroll. In English and Chinese.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
@@ -24,7 +27,7 @@ const relay = () => ({
],
});
-function serve(lang, posts) {
+function serve(lang, posts, adds = []) {
const p = relay();
const providers = { providers: [p], presets: [], excluded: [], gateway: { running: true, window: true } };
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
@@ -43,6 +46,14 @@ function serve(lang, posts) {
p.keyList = [...p.keyList, { id: "cccccccccc", name: "", masked: "sk-…new", on: true }, { id: "dddddddddd", name: "", masked: "sk-…nw2", on: true }];
return json({ ...providers, added: 2, had: 1 });
}
+ // keys/add answers when the test lets it: each call waits on the next
+ // entry of adds, a promise giving true to add the key or false to refuse
+ if (url.pathname === "/api/keys/add" && route.request().method() === "POST") {
+ posts.push(route.request().postDataJSON());
+ if (!await adds.shift()) return route.fulfill({ status: 400, json: { error: "the provider refused this key" } });
+ p.keyList = [...p.keyList, { id: "eeeeeeeeee", name: "", masked: "sk-…add", on: true }];
+ return json(providers);
+ }
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
@@ -51,14 +62,14 @@ function serve(lang, posts) {
}
const words = {
- en: { another: "Add another key", several: "Paste several", add: "Add", three: "3 keys", note: "2 keys added, 1 already there", rest: /Resting · 429 rate limited · back at/ },
- zh: { another: "添加另一个密钥", several: "批量粘贴", add: "添加", three: "3 个密钥", note: "已添加 2 个密钥,1 个已存在", rest: /暂停使用 · 429 限流 · .* 恢复/ },
+ en: { another: "Add another key", several: "Paste several", add: "Add", adding: "Adding…", three: "3 keys", note: "2 keys added, 1 already there", rest: /Resting · 429 rate limited · back at/ },
+ zh: { another: "添加另一个密钥", several: "批量粘贴", add: "添加", adding: "添加中…", three: "3 个密钥", note: "已添加 2 个密钥,1 个已存在", rest: /暂停使用 · 429 限流 · .* 恢复/ },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
- const open = async (t, name) => {
+ const open = async (t, name, adds) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
@@ -68,16 +79,24 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
await browser.close();
});
const page = await (await browser.newContext({ viewport: { width: 900, height: 760 }, reducedMotion: "reduce" })).newPage();
- page.setDefaultTimeout(5000);
+ // room for a busy machine: one click took over 5s at load 190
+ page.setDefaultTimeout(15000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posts = [];
- await page.route("**/*", serve(lang, posts));
+ await page.route("**/*", serve(lang, posts, adds));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Relay" }).first().click();
await page.locator(".editor .accts .acc.add").waitFor();
return { page, errors, posts };
};
+ // got waits until n keys/add posts have arrived: a busy machine can take
+ // seconds to send the first
+ const got = async (posts, n) => {
+ for (let i = 0; i < 300 && posts.length < n; i++) await new Promise((r) => setTimeout(r, 50));
+ assert.equal(posts.length, n);
+ };
+ const later = () => { let done; const p = new Promise((r) => { done = r; }); return { p, done }; };
const scrolled = (page) => page.evaluate(() => [window.scrollY, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop > 0).map((e) => `${e.className}:${e.scrollTop}`)].join(" "));
const posted = async (posts) => {
for (let i = 0; i < 60 && !posts.length; i++) await new Promise((r) => setTimeout(r, 50));
@@ -133,5 +152,42 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.equal(await page.locator(".acc.adding.bulk").count(), 0);
assert.deepEqual(errors, []);
});
+
+ test(`${engine} ${lang}: adding one key shows it is under way`, async (t) => {
+ const refused = later(), taken = later();
+ const { page, errors, posts } = await open(t, "busy", [refused.p, taken.p]);
+ await page.getByRole("button", { name: w.another }).click();
+ const row = page.locator(".acc.adding");
+ const name = row.locator(".kf input").first(), key = row.locator("input[type=password]");
+ await key.waitFor();
+ const [nw, kw] = [await name.evaluate((e) => e.getBoundingClientRect().width), await key.evaluate((e) => e.getBoundingClientRect().width)];
+ assert.ok(nw * 1.8 < kw, `the name field (${nw}px) is not the narrower one beside the key (${kw}px)`);
+ await key.fill("sk-bad");
+ const add = row.locator(".kb button.primary");
+ await add.click();
+ await got(posts, 1);
+ // held: a spinner and Adding…, and a second Enter posts nothing
+ assert.equal((await add.textContent()).trim(), w.adding);
+ assert.equal(await add.getAttribute("aria-busy"), "true");
+ assert.equal(await add.locator("svg").count(), 1);
+ await key.press("Enter");
+ await page.waitForTimeout(100);
+ assert.equal(posts.length, 1, "Enter posted the key again while it was being added");
+ // refused: the label comes back and the row stays to be fixed
+ refused.done(false);
+ await page.waitForFunction(() => document.querySelector(".acc.adding .kb button.primary")?.getAttribute("aria-busy") === null);
+ assert.equal((await add.textContent()).trim(), w.add);
+ assert.equal(await add.locator("svg").count(), 0);
+ assert.equal(await row.count(), 1);
+ await key.fill("sk-good");
+ await add.click();
+ await got(posts, 2);
+ assert.equal((await add.textContent()).trim(), w.adding);
+ taken.done(true);
+ await page.locator('.acc[data-account-id="eeeeeeeeee"]').waitFor();
+ assert.equal(await page.locator(".acc.adding").count(), 0);
+ assert.deepEqual(posts.map((b) => b.key), ["sk-bad", "sk-good"]);
+ assert.deepEqual(errors, []);
+ });
}
}