providers: a model chip's right-click menu copies the model's id (Discord, ARNO on v0.1.815: 希望provider的模型右击菜单除了测试模型外,还能添加拷贝模型id的功能) — the menu a chip opens in a provider's editor had Test this model (and Asked on … where the provider has more than one API) and nothing to take the id with; a chip shows the model's name where it has one, so the id (zai/glm-5.3-flash under "GLM 5.3 Flash") had to be read off its tooltip and typed. The menu now has Copy model ID / 复制模型 ID under Test this model, through the app's own menu (openRowMenu) and copy(): magpie's /api/copy, the page's clipboard and the copy command after it, as every other copy in the app; the footer says "Model ID {model} copied" / 已复制模型 ID {model}. It is on where the model can't be tested (an own-API subscription's, a classifier's) too. The app has English and Chinese strings only (i18n.js has no Japanese table). model-copy-id.test.cjs (a relay's prefixed, plain and hand-picked ids and an own-API account's: the id, not the name, is posted to /api/copy, the footer says so, nothing is tested, picked or scrolled, no left-border accent; en and zh) fails on the old code and passes in WebKit; model-test-why expects the second item beside the disabled test one. model-test-one, model-test-why, provider-typed, provider-detect-models and click-scroll pass in WebKit; Chromium doesn't launch here, so not tried there. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried in the app's own window (its /api/copy path is the one the other copies use).

This commit is contained in:
yetone
2026-10-04 13:36:54 +08:00
parent a0d41044bb
commit 81879b90f3
4 changed files with 155 additions and 2 deletions
+3
View File
@@ -7544,6 +7544,9 @@ function renderModels(p) {
if (again) return;
const acts = [noTest ? { name: "Test this model", icon: "M5.5 3.75v8.5L12.25 8z", off: true, why: noTest, run() {} }
: { name: "Test this model", icon: "M5.5 3.75v8.5L12.25 8z", run: () => testOne(id) }];
// its id as agents and the gateway name it, to paste where a model is
// typed (ARNO, Discord: 右击菜单除了测试模型外,还能添加拷贝模型id的功能)
acts.push({ name: "Copy model ID", icon: COPY_ICON, tip: id, run: () => copy(id, id, null, t("Model ID {model} copied", { model: id })) });
if (apis.length && !decidesModel(p, id)) {
const now = apiNow(id);
acts.push({ name: now ? t("Asked on {api}…", { api: apiLabel(now) }) : t("Asked on: Auto…"), icon: "M2.5 5h11M2.5 11h11M10.5 2.5 13.5 5l-3 2.5M5.5 8.5 2.5 11l3 2.5", tip: t("Pick the API this model is asked on"), run: () => pickAPI(c, id) });
+2
View File
@@ -1168,6 +1168,8 @@ const I18N = {
"Right-click to test just this model": "右键可单独测试这个模型",
"Right-click a model to test just it": "右键点击某个模型可单独测试它",
"Test this model": "测试此模型",
"Copy model ID": "复制模型 ID",
"Model ID {model} copied": "已复制模型 ID {model}",
"A classifier's models aren't sent test requests: Test under Endpoints asks Jev's endpoint for them.": "分类器的模型不能单独发测试请求:用「端点」下的「测试」向 Jev 的端点获取它们。",
"{name} is reached through its own API, which magpie translates each agent request for, so a test request can't be sent to it on its own. Ask the model from an agent to try it.": "{name} 走的是它自己的接口,magpie 会为 Agent 的每个请求做转换,所以无法单独给它发测试请求。请在 Agent 里用这个模型试一下。",
"{model} answered in {took}": "{model} 在 {took}内响应",
+145
View File
@@ -0,0 +1,145 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// A model's id copied from its chip (ARNO on Discord: 希望provider的模型右击菜单
// 除了测试模型外,还能添加拷贝模型id的功能): in a provider's editor a model
// chip's right-click menu has "Copy model ID" under "Test this model", which
// puts that model's id — a prefixed one and one picked by hand alike — on the
// clipboard through magpie (/api/copy), says so in the footer, tests nothing,
// neither picks nor unpicks the chip and leaves the page where it is. It is
// there where the model can't be tested too. In English and Chinese,
// Chromium and WebKit.
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 assets = path.resolve(__dirname, "../assets");
// by-hand/model-x is a pick the vendor's list doesn't have, which magpie
// lists first, as picked
const IDS = ["by-hand/model-x", "gpt-5.1", "zai/glm-5.3-flash", "cline-free/mimo-v2.6-flash"];
const relay = {
id: "relay", name: "Relay", icon: "generic", chat: "https://relay.example.com/v1", responses: "", anthropic: "", catalog: "",
models: IDS.map((id) => ({ id, name: id === "zai/glm-5.3-flash" ? "GLM 5.3 Flash" : id, on: id !== "gpt-5.1" })), agents: [], fallback: [], headers: {},
chosen: ["zai/glm-5.3-flash", "cline-free/mimo-v2.6-flash", "by-hand/model-x"],
key: { set: true, masked: "sk-…one" }, keyList: [], balanceToken: { takes: false, set: false }, proxy: "",
};
// one reached through its own API: its models can't be tested, and can
// still be copied
const account = {
id: "acct", name: "Account", icon: "generic", chat: "", responses: "", anthropic: "", catalog: "", modelTest: "own-api",
models: [{ id: "acct/model-a", name: "Model A", on: true }], agents: [], fallback: [], headers: {},
key: { set: false }, keyList: [], balanceToken: { takes: false, set: false }, proxy: "",
};
function serve(lang, copies, tests) {
const providers = { providers: [relay, account], presets: [], excluded: [], gateway: { running: true, window: true } };
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/copy") {
copies.push(route.request().postDataJSON().text);
return json({});
}
if (url.pathname === "/api/provider/test") {
tests.push(route.request().postDataJSON());
return json({ results: [] });
}
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 route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: { copy: "Copy model ID", test: "Test this model", said: (id) => `Model ID ${id} copied` },
zh: { copy: "复制模型 ID", test: "测试此模型", said: (id) => `已复制模型 ID ${id}` },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: a chip's right-click copies the model's id`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width: 900, height: 760 }, reducedMotion: "reduce" })).newPage();
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-model-copy-id.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const copies = [], tests = [];
await page.route("**/*", serve(lang, copies, tests));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Relay" }).click();
await page.locator(".editor .mchips .mchip").first().waitFor();
const chip = (text) => page.locator(".editor .mchips .mchip", { hasText: text });
const menu = page.locator(".pop.row-menu");
const picked = () => page.locator(".editor .mchips .mchip.on").count();
const wait = async (n) => { for (let i = 0; i < 60 && copies.length < n; i++) await page.waitForTimeout(50); };
const copyOf = async (text) => {
const c = chip(text);
await c.scrollIntoViewIfNeeded();
await page.waitForTimeout(150);
const before = await c.evaluate((e) => e.getBoundingClientRect().top);
const scroll = await page.evaluate(() => [...document.querySelectorAll("*")].map((e) => e.scrollTop).join());
const on = await picked();
await c.click({ button: "right" });
await menu.waitFor();
const names = await menu.getByRole("menuitem").allTextContents();
assert.equal(names[0], w.test, "testing stays first");
assert(names.includes(w.copy), `the menu has ${w.copy}: ${names}`);
const n = copies.length;
await menu.getByRole("menuitem", { name: w.copy }).click();
await wait(n + 1);
await menu.waitFor({ state: "detached" });
assert.equal(await picked(), on, "copying doesn't pick");
assert.equal(await c.evaluate((e) => e.getBoundingClientRect().top), before, `${text} moved`);
assert.equal(await page.evaluate(() => [...document.querySelectorAll("*")].map((e) => e.scrollTop).join()), scroll, "nothing scrolled");
return copies.at(-1);
};
// the id, not the name it is shown by
assert.equal(await copyOf("GLM 5.3 Flash"), "zai/glm-5.3-flash");
assert.equal(await page.locator("#status").textContent(), w.said("zai/glm-5.3-flash"));
assert.equal(await copyOf("cline-free/mimo-v2.6-flash"), "cline-free/mimo-v2.6-flash");
assert.equal(await copyOf("gpt-5.1"), "gpt-5.1");
// one picked by hand, which the vendor's list doesn't have
assert.equal(await copyOf("by-hand/model-x"), "by-hand/model-x");
assert.equal(tests.length, 0, "copying tests nothing");
// no left-border accent on the menu
await chip("gpt-5.1").click({ button: "right" });
await menu.waitFor();
const border = await page.evaluate(() => [...document.querySelectorAll(".pop.row-menu, .pop.row-menu *")].map((e) => getComputedStyle(e).borderLeftWidth).filter((b) => parseFloat(b) > 1));
assert.deepEqual(border, [], "no left-border accent");
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
// a model that can't be tested can still be copied
await page.keyboard.press("Escape");
await page.locator(".row.provider", { hasText: "Account" }).click();
await page.locator(".editor .mchips .mchip", { hasText: "Model A" }).waitFor();
await chip("Model A").click({ button: "right" });
await menu.waitFor();
assert(!(await menu.getByRole("menuitem", { name: w.test }).isEnabled()), "this one can't be tested");
const copyItem = menu.getByRole("menuitem", { name: w.copy });
assert(await copyItem.isEnabled(), "Copy model ID is on");
await copyItem.click();
await wait(5);
assert.equal(copies.at(-1), "acct/model-a");
assert.deepEqual(errors, []);
});
}
}
+5 -2
View File
@@ -121,8 +121,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
await c.click({ button: "right" });
const menu = page.locator(".pop.row-menu");
await menu.waitFor();
const item = menu.locator(".rm-item");
assert.equal(await item.count(), 1);
// Test this model, off, and Copy model ID, which is on
const items = menu.locator(".rm-item");
assert.equal(await items.count(), 2);
assert(await items.nth(1).isEnabled(), "its id can still be copied");
const item = items.first();
assert(await item.isDisabled(), "Test this model is off");
assert.equal(await item.getAttribute("title"), w.own);
assert.equal(await item.locator(".rm-why").textContent(), w.own, "the reason is said in the menu");