SlouchyQuill on Discord asked for a Tavily key pool after dsh-tavily-pool: several keys, each key's quota shown, keys rotated. Before this, the comma-separated keys (#1477) always started from the key that last answered, so one key took every search until it was refused, a refused key kept no rest of its own, and nothing showed what a key had left. What changed: - Turns (internal/gateway/search_keys.go). Searches take turns over the keys by default (round robin, a counter per vendor). pick=order (CLI) or In order (Settings) always starts from the first key; the pick is saved in SearchAPI.Pick. - Rests. A refused key rests: 401/403 for 10m ("turned away"), 402 for 1h ("out of credit"), 429 for its Retry-After held to 30s-10m (1m without one). 432/433 count as out of credit from Tavily only; from any other vendor they rest as a rate limit. A resting key is asked after the others, soonest back first, so failover is kept, and it wakes when it answers. Rests are keyed by a hash of the key. - Quota (internal/gateway/search_quota.go). Each Tavily key's quota is read from GET /usage with that key. The fixture is the documented reply from docs.tavily.com. The key's own limit is used when it has one; otherwise its plan's plan_limit/plan_usage, which the account's keys share. A failed read, or a reply missing those fields, says unknown and why, never 0. A read is kept 2m unless asked fresh. A key found used up with no pay-as-you-go credit rests 1h; a later read with credit wakes it. - Keys never appear in full. The vendor's error has the key masked (tvly-…abcd), and errors name "key N (tvly-…abcd)". - GUI. Settings › Models › Web search adds a "Keys of <API>" row under an API with several keys or with Tavily's: In turn / In order, each key masked with what it has used and why it is asked last, and Check again. There is an API for it (POST /api/settings/search-quota), and 13 strings in zh, zh-TW, ja and de. - CLI. `magpie search` lists the pick and each Tavily key's quota, read fresh; `magpie search add … pick=turns|order`. - Docs. gateway-routing.md has a new section "A search API's keys"; both READMEs have a line on web search. What was run: - go test -tags nogui ./internal/gateway ./internal/provider ./internal/cli ./internal/gui: ok. After the last rebase, provider and cli again, and gateway -race -run Search. - New tests: TestReadTavilyUsage, TestSearchKeysTakeTurnsRoundRobin, TestSearchKeyRefusedRests, TestSearchRestFor, TestSearchQuotas and TestSearchKeyNeverInFull. Each guarantee was broken by hand and its tests went red: - turns pinned to the first key: RoundRobin and RefusedRests; - rests ignored: TestSearchAPIKeysTakeTurns, RefusedRests and TestSearchQuotas; - key masking removed: NeverInFull; - a failed read reported as known: TestSearchQuotas. - gofmt -l; go vet -tags nogui for darwin, linux and windows on the four packages. - GUI: search-keys.test.cjs covers en, zh, zh-TW, ja and de × Chromium and WebKit × 1100 and 420px: 20/20. Without the new .search-keys .val wrap rule, de at 420px fails ("Erneut prüfen" went past the window's edge). - search-api, search-first, search-instead, search-mcp, searcher-pick, settings-search, provider-search, gui-de, gui-ja and i18n-keys: 56/56 in each engine. - Full suite in Chromium (concurrency 6, load ~40): 4498/4504. The files that failed (gateway-key-groups, rest-try-now, routing-wb-restricted, text-size, usage-sum-whole) were rerun alone. All pass except usage-sum-whole, which fails now and then on an origin/main worktree too under the same load (#1864, #2023; rest-try-now and routing-wb-restricted are #1905). None of them touch search. - Real Tavily API with two invalid keys: /search returns 401 {"detail":{"error":"Unauthorized: missing or invalid API key."}}, which counts as a refusal. Both keys rest 10m as turned away, the error names them only masked, and both quotas read "unknown · 401". Checked through a test and through the nogui CLI binary in a sandbox HOME. Not tried: a valid key's /usage against the real API. No Tavily key was at hand, so the quota parsing is checked against the documented bytes only.
160 lines
9.9 KiB
JavaScript
160 lines
9.9 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// Settings' Web search section, a search API with several keys
|
|
// (SlouchyQuill on Discord, after dsh-tavily-pool): under it a row of its
|
|
// keys, In turn (the default) or In order, sent as pick "turns" or "order";
|
|
// for Tavily, what each key has used of its limit, read once as the page
|
|
// is drawn and again, fresh, on Check again. Each key is shown masked; a
|
|
// read that failed says Unknown and why, never 0; a key used up says so,
|
|
// and one asked last says until when and why. An API with one key that
|
|
// magpie can't read gets no row. In five languages, Chromium and WebKit,
|
|
// 1100px and 420px wide, with the API faked.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
const { watchSettingsReads, settingsDrawn, reach } = require("./reader.cjs");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const LANGS = ["en", "zh", "zh-TW", "ja", "de"];
|
|
// the words as i18n.js has them
|
|
const I18N = (() => {
|
|
const src = require("node:fs").readFileSync(path.join(assets, "i18n.js"), "utf8");
|
|
return new Function(src.slice(0, src.indexOf("\n};") + 3) + "; return I18N;")();
|
|
})();
|
|
const tr = (lang, s, vars = {}) => {
|
|
const got = lang === "en" ? s : I18N[lang][s];
|
|
assert(got, `"${s}" has no ${lang} translation`);
|
|
return got.replace(/\{(\w+)\}/g, (_, k) => String(vars[k]));
|
|
};
|
|
|
|
const vendors = [
|
|
{ id: "tavily", name: "Tavily", keysURL: "https://app.tavily.com/home", base: "https://api.tavily.com" },
|
|
{ id: "brave", name: "Brave Search", keysURL: "https://api-dashboard.search.brave.com/app/keys", base: "https://api.search.brave.com" },
|
|
];
|
|
const restUntil = new Date(Date.now() + 50 * 60 * 1000).toISOString();
|
|
const quotas = (used) => [
|
|
{ key: "tvly-…aaaa", known: true, used, limit: 1000, of: "key" },
|
|
{ key: "tvly-…bbbb", known: true, used: 1000, limit: 1000, of: "plan", plan: "Researcher", usedUp: true, resting: "used up", restUntil },
|
|
{ key: "tvly-…cccc", known: false, used: 0, limit: 0, err: "401 Unauthorized" },
|
|
];
|
|
|
|
function serve(lang, posted) {
|
|
let pick = "";
|
|
let reads = 0;
|
|
const settings = () => ({
|
|
lang, theme: "light", searchVendors: vendors,
|
|
searchAPIs: [
|
|
{ vendor: "tavily", name: "Tavily", key: "tvly-…aaaa, tvly-…bbbb, tvly-…cccc", keys: 3, quota: true, pick, ready: true },
|
|
{ vendor: "brave", name: "Brave Search", key: "BSA…wxyz", keys: 1, ready: true },
|
|
],
|
|
});
|
|
return async (r) => {
|
|
const url = new URL(r.request().url());
|
|
const json = (data, status = 200) => r.fulfill({ status, json: data });
|
|
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: settings() });
|
|
if (url.pathname === "/api/settings") return json(settings());
|
|
if (url.pathname === "/api/settings/search-api") {
|
|
const b = JSON.parse(r.request().postData());
|
|
posted.push(b);
|
|
if (b.pick) pick = b.pick === "turns" ? "" : b.pick;
|
|
return json(settings());
|
|
}
|
|
if (url.pathname === "/api/settings/search-quota") {
|
|
const b = JSON.parse(r.request().postData());
|
|
posted.push({ quota: b });
|
|
reads++;
|
|
return json({ keys: quotas(reads > 1 ? 813 : 812) });
|
|
}
|
|
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
|
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
|
|
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)];
|
|
await r.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of LANGS) for (const width of [1100, 420]) {
|
|
test(`${engine} ${lang} ${width}px: a search API's keys take turns and say what each has used`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await (await browser.newContext({ viewport: { width, height: 1400 }, reducedMotion: "reduce" })).newPage();
|
|
page.setDefaultTimeout(5000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const posted = [];
|
|
await page.route("**/*", serve(lang, posted));
|
|
await watchSettingsReads(page);
|
|
await page.goto("http://magpie.test/?view=settings&tab=models");
|
|
await settingsDrawn(page);
|
|
const keys = page.locator("#searchList .row.search-keys");
|
|
await keys.first().waitFor();
|
|
// Tavily's only: Brave has one key and no quota magpie can read
|
|
assert.equal(await keys.count(), 1);
|
|
assert.equal(await keys.getAttribute("data-vendor"), "tavily");
|
|
assert.equal(await keys.locator(".name").innerText(), tr(lang, "Keys of {name}", { name: "Tavily" }));
|
|
|
|
const lines = keys.locator(".search-quota .quota-key");
|
|
await page.waitForFunction(() => document.querySelectorAll("#searchList .search-keys .quota-key").length === 3);
|
|
assert.deepEqual(await lines.locator("code").allInnerTexts(), ["tvly-…aaaa", "tvly-…bbbb", "tvly-…cccc"]);
|
|
assert.equal(await lines.nth(0).locator(".used").innerText(), tr(lang, "{used} of {limit} used", { used: "812", limit: await page.evaluate(() => (1000).toLocaleString(intlLang())) }));
|
|
// used up: said, with the plan it is of and why it is asked last
|
|
assert.equal(await lines.nth(1).locator(".warn").last().innerText(), tr(lang, "used up"));
|
|
const note = await lines.nth(1).locator(".faint").innerText();
|
|
assert(note.includes(tr(lang, "{plan} plan, shared by its account's keys", { plan: "Researcher" })), note);
|
|
const until = await page.evaluate((u) => new Date(u).toLocaleTimeString(intlLang(), { hour: "2-digit", minute: "2-digit" }), restUntil);
|
|
assert(note.includes(tr(lang, "asked last until {time}: {why}", { time: until, why: tr(lang, "used up") })), note);
|
|
// a failed read: unknown and why, never 0
|
|
const unknown = await lines.nth(2).locator(".used").innerText();
|
|
assert.equal(unknown, tr(lang, "Unknown: {why}", { why: "401 Unauthorized" }));
|
|
assert(!/(^|\D)0(\D|$)/.test(unknown.replace("401", "")), unknown);
|
|
assert.equal(await lines.nth(2).locator(".used.err").count(), 1);
|
|
assert.deepEqual(posted, [{ quota: { vendor: "tavily", fresh: false } }]);
|
|
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
|
|
// the page clips what is past its edge, so each control is measured
|
|
const past = await keys.evaluate((r) => [...r.querySelectorAll("button, code, .used")]
|
|
.filter((e) => e.getBoundingClientRect().right > innerWidth + 0.5).map((e) => e.textContent));
|
|
assert.deepEqual(past, [], "nothing past the window's edge");
|
|
|
|
const where = () => page.evaluate(() => [scrollX, scrollY, document.scrollingElement.scrollTop, document.querySelector("#view-settings")?.scrollTop]);
|
|
// Settings is drawn again after each change: a box is taken from the
|
|
// row that stays
|
|
const click = async (loc) => {
|
|
let b = null;
|
|
for (let i = 0; i < 20 && !b; i++) b = await loc.boundingBox().catch(() => null) || (await page.waitForTimeout(50), null);
|
|
assert(b, "the button is in the page");
|
|
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
|
|
};
|
|
// the row's foot is past the window's at 420px: the reader wheels down
|
|
// to it before the clicks, which then must not move the page
|
|
await reach(page, keys.locator("button.search-quota-again"));
|
|
const before = await where();
|
|
// Check again: read fresh
|
|
await click(keys.locator("button.search-quota-again", { hasText: tr(lang, "Check again") }));
|
|
await page.waitForFunction(() => /813/.test(document.querySelector("#searchList .search-keys .quota-key .used")?.innerText || ""));
|
|
assert.deepEqual(posted.at(-1), { quota: { vendor: "tavily", fresh: true } });
|
|
// In order, then In turn again
|
|
const segs = keys.locator(".segs .opt");
|
|
assert.deepEqual(await segs.allInnerTexts(), [tr(lang, "In turn"), tr(lang, "In order")]);
|
|
assert.equal(await segs.nth(0).getAttribute("aria-pressed"), "true");
|
|
await click(segs.nth(1));
|
|
await page.waitForFunction(() => document.querySelectorAll("#searchList .search-keys .segs .opt")[1]?.getAttribute("aria-pressed") === "true");
|
|
assert.deepEqual(posted.filter((p) => p.pick), [{ vendor: "tavily", pick: "order" }]);
|
|
await click(keys.locator(".segs .opt").nth(0));
|
|
await page.waitForFunction(() => document.querySelectorAll("#searchList .search-keys .segs .opt")[0]?.getAttribute("aria-pressed") === "true");
|
|
assert.deepEqual(posted.filter((p) => p.pick), [{ vendor: "tavily", pick: "order" }, { vendor: "tavily", pick: "turns" }]);
|
|
// redrawn with the same keys, it isn't read again
|
|
assert.equal(posted.filter((p) => p.quota).length, 2);
|
|
assert.equal(await keys.locator(".quota-key").count(), 3);
|
|
assert.deepEqual(await where(), before, "a click doesn't scroll");
|
|
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|