diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index ad1f29329..0fb45bb46 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -21880,7 +21880,8 @@ function privacyModelRow(box, why) { const who = el("div", "who"), val = el("div", "val"); who.append(name(), el("div", "sub", pmSub(m))); if (m?.supported || m?.installed) { - if (m.downloading) { + if (m.models?.length > 1) val.append(pmPick(m, (id) => act("pick", id))); + if (pmBusy(m)) { const c = el("button", "text", t("Cancel")); c.onclick = () => act("cancel"); val.append(c); @@ -21897,19 +21898,42 @@ function privacyModelRow(box, why) { clearTimeout(pmTimer); // a download's progress, and where pf-serve runs once it has started const starting = m?.on && m.installed && !m.running && !m.failed && tries++ < 30; - if (m?.downloading || starting) pmTimer = setTimeout(load, 1000); + if (pmBusy(m) || starting) pmTimer = setTimeout(load, 1000); }; const load = () => api("privacy-model").then((m) => { pmState = m; draw(m); }).catch(() => {}); - const act = (doing) => { tries = 0; api("privacy-model", { do: doing }).then((m) => { pmState = m; draw(m); }).catch((e) => { status(t(e.message), "err"); load(); }); }; + const act = (doing, model) => { tries = 0; api("privacy-model", { do: doing, model }).then((m) => { pmState = m; draw(m); }).catch((e) => { status(t(e.message), "err"); load(); }); }; draw(pmState); load(); } +// the local models to pick from (pii.Models), by the app's menu: what +// each is, its size and whether it is downloaded +const PM_MODELS = { "astrlink-guard": ["AstrLink Guard", "Chinese and English, few false alarms in code"], "privacy-filter": ["Privacy Filter", "OpenAI's, masks more ordinary values in code"] }; +// pmBusy is the model downloading (the one picked while another masks, too) +const pmBusy = (m) => (m?.models || []).find((x) => x.downloading) || (m?.downloading ? m : null); +function pmPick(m, pick) { + const b = el("button", "sess-pick pm-model"); + b.type = "button"; + b.setAttribute("aria-haspopup", "menu"); + b.setAttribute("aria-expanded", "false"); + b.title = t("The local model"); + b.append(el("span", "", (PM_MODELS[m.model] || [m.model])[0]), svg(CHEV, 11, 1.6)); + b.onclick = (e) => { + e.stopPropagation(); + if (b.classList.contains("open")) return closeProtoMenu(); + const opts = (m.models || []).map((x) => ({ v: x.model, name: (PM_MODELS[x.model] || [x.model])[0], literalName: true, + note: !x.supported && !x.installed ? t("Not available on this system yet") : t(PM_MODELS[x.model]?.[1] || "") + " · " + fmtBytes(x.size) + (x.installed ? " · " + t("downloaded") : "") })); + openProtoMenu(b, opts, m.model, (v) => v !== m.model && pick(v), "The local model", "sess-menu", "right"); + }; + return b; +} function pmSub(m) { const about = t("Names, addresses and passwords no rule can find, found by a model that runs on this computer"); if (!m) return about; if (!m.supported && !m.installed) return t("Not available on this system yet"); - if (m.downloading) return t("Downloading the model: {done} of {total}", { done: fmtBytes(m.done), total: fmtBytes(m.total) }); - if (m.error) return t("The download stopped: {error}", { error: m.error }); + const d = pmBusy(m); + if (d) return t("Downloading the model: {done} of {total}", { done: fmtBytes(d.done), total: fmtBytes(d.total) }); + const bad = (m.models || []).find((x) => x.error) || (m.error ? m : null); + if (bad) return t("The download stopped: {error}", { error: bad.error }); if (m.on && m.failed) return t("The model didn't answer, so requests are held back: {error}", { error: m.failed }); if (m.on && m.running) { if (m.gpu) return t("On, on the GPU ({device})", { device: m.device }); diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 4f2f98ad0..7904d0185 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1200,6 +1200,10 @@ const I18N = { "Find with a local model": "用本地模型查找", "Delete model": "删除模型", "Delete the model?": "删除模型?", + "The local model": "本地模型", + "Chinese and English, few false alarms in code": "中英文,代码里很少误判", + "OpenAI's, masks more ordinary values in code": "OpenAI 出品,代码里会遮盖更多普通值", + "downloaded": "已下载", "Turning it on again downloads {size} again.": "再次打开时会重新下载 {size}。", "Names, addresses and passwords no rule can find, found by a model that runs on this computer": "规则找不到的姓名、地址和密码,由在这台电脑上运行的模型找出", "Names, addresses and passwords no rule can find, found by a model that runs on this computer. Turning it on downloads {size} once": "规则找不到的姓名、地址和密码,由在这台电脑上运行的模型找出。开启后会下载一次 {size}", @@ -5190,6 +5194,10 @@ const I18N = { "Find with a local model": "用本機模型查找", "Delete model": "刪除模型", "Delete the model?": "刪除模型?", + "The local model": "本機模型", + "Chinese and English, few false alarms in code": "中英文,程式碼裡很少誤判", + "OpenAI's, masks more ordinary values in code": "OpenAI 出品,程式碼裡會遮蓋更多普通值", + "downloaded": "已下載", "Turning it on again downloads {size} again.": "再次開啟時會重新下載 {size}。", "Names, addresses and passwords no rule can find, found by a model that runs on this computer": "規則找不到的姓名、地址和密碼,由在這台電腦上執行的模型找出", "Names, addresses and passwords no rule can find, found by a model that runs on this computer. Turning it on downloads {size} once": "規則找不到的姓名、地址和密碼,由在這台電腦上執行的模型找出。開啟後會下載一次 {size}", @@ -9150,6 +9158,10 @@ const I18N = { "Find with a local model": "ローカルモデルで検出", "Delete model": "モデルを削除", "Delete the model?": "モデルを削除しますか?", + "The local model": "ローカルモデル", + "Chinese and English, few false alarms in code": "中国語と英語、コードでの誤検出が少ない", + "OpenAI's, masks more ordinary values in code": "OpenAI 製、コード中の普通の値も多く隠す", + "downloaded": "ダウンロード済み", "Turning it on again downloads {size} again.": "もう一度オンにすると {size} を再びダウンロードします。", "Names, addresses and passwords no rule can find, found by a model that runs on this computer": "ルールでは見つからない名前・住所・パスワードを、このコンピュータで動くモデルが見つけます", "Names, addresses and passwords no rule can find, found by a model that runs on this computer. Turning it on downloads {size} once": "ルールでは見つからない名前・住所・パスワードを、このコンピュータで動くモデルが見つけます。オンにすると {size} を一度ダウンロードします", @@ -13140,6 +13152,10 @@ const I18N = { "Find with a local model": "Mit lokalem Modell finden", "Delete model": "Modell löschen", "Delete the model?": "Modell löschen?", + "The local model": "Lokales Modell", + "Chinese and English, few false alarms in code": "Chinesisch und Englisch, wenig Fehlalarme in Code", + "OpenAI's, masks more ordinary values in code": "Von OpenAI, maskiert in Code mehr gewöhnliche Werte", + "downloaded": "geladen", "Turning it on again downloads {size} again.": "Wieder eingeschaltet, werden erneut {size} geladen.", "Names, addresses and passwords no rule can find, found by a model that runs on this computer": "Namen, Adressen und Passwörter, die keine Regel findet, gefunden von einem Modell auf diesem Computer", "Names, addresses and passwords no rule can find, found by a model that runs on this computer. Turning it on downloads {size} once": "Namen, Adressen und Passwörter, die keine Regel findet, gefunden von einem Modell auf diesem Computer. Einschalten lädt einmalig {size}", diff --git a/internal/gui/tests/privacy-model.test.cjs b/internal/gui/tests/privacy-model.test.cjs index b9b31c0ee..742506c32 100644 --- a/internal/gui/tests/privacy-model.test.cjs +++ b/internal/gui/tests/privacy-model.test.cjs @@ -36,8 +36,8 @@ function serve(lang, masking, model, posts) { if (url.pathname === "/api/settings") return json(settings()); if (url.pathname === "/api/privacy-model") { if (r.request().method() === "POST") { - const { do: doing } = JSON.parse(r.request().postData()); - posts.push(doing); + const { do: doing, model: id } = JSON.parse(r.request().postData()); + posts.push(id ? doing + " " + id : doing); if (doing === "on") Object.assign(model, { downloading: true, done: 0, total: size, ticks: 0 }); if (doing === "cancel") Object.assign(model, { downloading: false, done: 0 }); if (doing === "remove") Object.assign(model, { installed: false, on: false }); @@ -47,6 +47,9 @@ function serve(lang, masking, model, posts) { model.done = Math.round(size * 0.4); } const { ticks, ...out } = model; + out.model ||= "privacy-filter"; + // every model to pick: this one, and the other as the test has it + out.models = [{ ...out, models: undefined }, ...(model.others || [])]; return json(out); } if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] }); @@ -72,6 +75,15 @@ async function open(engine, lang, width, masking, model, posts) { return { browser, page, errors }; } +const guard = { model: "astrlink-guard", supported: true, installed: false, size: 38958270 }; +const pickWords = { + en: { head: "The local model", note: "Chinese and English, few false alarms in code · 39 MB" }, + zh: { head: "本地模型", note: "中英文,代码里很少误判 · 39 MB" }, + "zh-TW": { head: "本機模型", note: "中英文,程式碼裡很少誤判 · 39 MB" }, + ja: { head: "ローカルモデル", note: "中国語と英語、コードでの誤検出が少ない · 39 MB" }, + de: { head: "Lokales Modell", note: "Chinesisch und Englisch, wenig Fehlalarme in Code · 39 MB" }, +}; + // what the row shows: its words and its New mark, whether they are cut, // that it is inside the list, and that it is the page's first setting const look = (page) => page.evaluate(() => { @@ -176,4 +188,45 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(await two.page.locator("#privacyModelRow .segs").count(), 0); assert.deepEqual([...errors, ...two.errors], []); }); + + // the model is picked in the app's own menu beside the switch (#3578): + // each model by its name, what it is and its size, in every language; + // picking one says so to magpie, and nothing is cut or moved + for (const lang of Object.keys(pickWords)) { + for (const width of [1100, 640]) { + test(`${engine} ${lang} ${width}px: the local model is picked from the app's menu`, async (t) => { + const posts = [], model = { supported: true, installed: true, on: true, running: true, device: "CPU", size, others: [guard] }; + const { browser, page, errors } = await open(engine, lang, width, true, model, posts); + t.after(() => browser.close()); + const row = page.locator("#privacyModelRow"); + await wheelTo(page, row, 300); + const pick = row.locator("button.pm-model"); + await pick.waitFor(); + assert.equal(await page.locator("#privacyModelRow select").count(), 0, "no native select"); + assert.equal((await pick.textContent()).trim(), "Privacy Filter"); + let m = await look(page); + assert(!m.cut && m.inside && !m.wide, `shown in full and inside: ${JSON.stringify(m)}`); + const before = await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings").scrollTop]); + await pick.click(); + const menu = page.locator(".proto-menu"); + await menu.waitFor(); + const items = await menu.evaluate((b) => ({ + head: b.querySelector(".pm-head").textContent, + items: [...b.querySelectorAll(".pm-item")].map((i) => [i.querySelector(".pm-name").textContent, i.querySelector(".pm-note").textContent, i.classList.contains("on")]), + inside: b.getBoundingClientRect().left >= 0 && b.getBoundingClientRect().right <= innerWidth, + })); + assert.equal(items.head, pickWords[lang].head); + assert.deepEqual(items.items.map((i) => i[0]), ["Privacy Filter", "AstrLink Guard"]); + assert.deepEqual(items.items.map((i) => i[2]), [true, false], "the one in use is ticked"); + assert.equal(items.items[1][1], pickWords[lang].note); + assert(items.items[0][1].includes("1.6 GB"), items.items[0][1]); + assert(items.inside, "the menu is inside the window"); + await menu.locator(".pm-item", { hasText: "AstrLink Guard" }).click(); + await page.waitForFunction(() => !document.querySelector(".proto-menu")); + assert.deepEqual(posts, ["pick astrlink-guard"]); + assert.deepEqual(await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings").scrollTop]), before, "the pick moved nothing"); + assert.deepEqual(errors, []); + }); + } + } } diff --git a/internal/pii/astrlink.go b/internal/pii/astrlink.go index edaeb4ba8..da4d1364c 100644 --- a/internal/pii/astrlink.go +++ b/internal/pii/astrlink.go @@ -132,7 +132,9 @@ var gsrv struct { } // spanFinder is the model, loaded (*guard.Model). -type spanFinder interface{ Find(text string) []guard.Span } +type spanFinder interface { + Find(text string) []guard.Span +} // guardLoad reads the model; a var for tests. var guardLoad = func(dir string) (spanFinder, error) {