diff --git a/internal/gui/assets/plugins/sessions/client.js b/internal/gui/assets/plugins/sessions/client.js index b1723a33c..276b7425f 100644 --- a/internal/gui/assets/plugins/sessions/client.js +++ b/internal/gui/assets/plugins/sessions/client.js @@ -618,17 +618,36 @@ magpie.plugin("sessions", (ctx) => { ed.append(h); const names = el("ul", "sm-ask-list sm-provider-list"); const go = el("button", "text primary", t("Move")); + // over a longer list: how many are picked, Select all (Deselect all + // once all are) and Invert selection (Cyrus on Discord) + const pick = el("div", "sm-provider-pick"); + const n = el("span", "note sm-count"); + const all = el("button", "text sm-pick-all"); + const flip = el("button", "text sm-pick-invert", t("Invert selection")); + all.type = flip.type = "button"; + pick.append(n, el("span", "grow"), all, flip); + if (list.length > 1) ed.append(pick); + const boxes = []; + const sync = () => { + for (const [c, id] of boxes) c.checked = chosen.has(id); + go.disabled = !chosen.size; + n.textContent = t("{n} selected", { n: chosen.size }); + all.textContent = t(chosen.size === list.length ? "Deselect all" : "Select all"); + }; + all.onclick = (e) => { e.stopPropagation(); const on = chosen.size < list.length; for (const s of list) on ? chosen.add(s.id) : chosen.delete(s.id); sync(); }; + flip.onclick = (e) => { e.stopPropagation(); for (const s of list) chosen.has(s.id) ? chosen.delete(s.id) : chosen.add(s.id); sync(); }; for (const s of list) { const li = el("li"); const lab = el("label"); const c = el("input", "sm-check"); c.type = "checkbox"; - c.checked = true; - c.onchange = () => { c.checked ? chosen.add(s.id) : chosen.delete(s.id); go.disabled = !chosen.size; }; + c.onchange = () => { c.checked ? chosen.add(s.id) : chosen.delete(s.id); sync(); }; + boxes.push([c, s.id]); lab.append(c, el("span", "name", s.title || s.id), el("span", "sm-prov", s.provider), el("span", "note", ago(s.last))); li.append(lab); names.append(li); } + sync(); ed.append(names); ed.append(el("p", "lib-confirm", t("Only the provider each one names is changed, in its files and in Codex's database: its messages, id, title and archive stay as they are. A copy of its files is kept in magpie's trash folder first, and Undo moves them back. A session written to in the last minute is left alone."))); const bar = el("div", "bar"); diff --git a/internal/gui/assets/plugins/sessions/i18n.js b/internal/gui/assets/plugins/sessions/i18n.js index b38435683..03be94909 100644 --- a/internal/gui/assets/plugins/sessions/i18n.js +++ b/internal/gui/assets/plugins/sessions/i18n.js @@ -65,6 +65,8 @@ magpie.strings({ "Moved 1 session back": "已把 1 个会话迁移回去", "Moved {n} sessions back": "已把 {n} 个会话迁移回去", "Move sessions to {to}?": "把会话迁移到 {to}?", + "Deselect all": "取消全选", + "Invert selection": "反选", "Only the provider each one names is changed, in its files and in Codex's database: its messages, id, title and archive stay as they are. A copy of its files is kept in magpie's trash folder first, and Undo moves them back. A session written to in the last minute is left alone.": "只改每个会话在文件和 Codex 数据库里记录的供应商,消息、会话 ID、标题和归档状态都不变。改之前会先在 magpie 的回收站文件夹里备份一份文件,「撤销」可以迁移回去。最近一分钟内还在写入的会话不会改动。", "Made with {from}: Codex's history lists only {to}'s sessions now": "用 {from} 创建:Codex 现在的历史列表只显示 {to} 的会话", "{n} sessions moved to magpie's trash": "{n} 个会话已移到 magpie 的回收站", @@ -149,6 +151,8 @@ magpie.strings({ "Moved 1 session back": "已把 1 個會話遷移回去", "Moved {n} sessions back": "已把 {n} 個會話遷移回去", "Move sessions to {to}?": "把會話遷移到 {to}?", + "Deselect all": "取消全選", + "Invert selection": "反選", "Only the provider each one names is changed, in its files and in Codex's database: its messages, id, title and archive stay as they are. A copy of its files is kept in magpie's trash folder first, and Undo moves them back. A session written to in the last minute is left alone.": "只改每個會話在檔案和 Codex 資料庫裡記錄的供應商,訊息、會話 ID、標題和歸檔狀態都不變。改之前會先在 magpie 的回收站資料夾裡備份一份檔案,「復原」可以遷移回去。最近一分鐘內還在寫入的會話不會改動。", "Made with {from}: Codex's history lists only {to}'s sessions now": "用 {from} 建立:Codex 現在的歷史列表只顯示 {to} 的會話", "{n} sessions moved to magpie's trash": "{n} 個會話已移到 magpie 的回收站", @@ -233,6 +237,8 @@ magpie.strings({ "Moved 1 session back": "1 件のセッションを元に戻しました", "Moved {n} sessions back": "{n} 件のセッションを元に戻しました", "Move sessions to {to}?": "セッションを {to} に移行しますか?", + "Deselect all": "すべての選択を解除", + "Invert selection": "選択を反転", "Only the provider each one names is changed, in its files and in Codex's database: its messages, id, title and archive stay as they are. A copy of its files is kept in magpie's trash folder first, and Undo moves them back. A session written to in the last minute is left alone.": "変更されるのは、ファイルと Codex のデータベースに記録されたプロバイダーだけです。メッセージ、ID、タイトル、アーカイブ状態はそのままです。変更前にファイルのコピーを magpie のゴミ箱フォルダーに保存し、「取り消す」で元に戻せます。直近 1 分以内に書き込まれたセッションは変更しません。", "Made with {from}: Codex's history lists only {to}'s sessions now": "{from} で作成:Codex の履歴には現在 {to} のセッションだけが表示されます", "{n} sessions moved to magpie's trash": "{n} 件のセッションを magpie のゴミ箱に移動しました", @@ -317,6 +323,8 @@ magpie.strings({ "Moved 1 session back": "1 Sitzung zurückgezogen", "Moved {n} sessions back": "{n} Sitzungen zurückgezogen", "Move sessions to {to}?": "Sitzungen zu {to} umziehen?", + "Deselect all": "Alle abwählen", + "Invert selection": "Auswahl umkehren", "Only the provider each one names is changed, in its files and in Codex's database: its messages, id, title and archive stay as they are. A copy of its files is kept in magpie's trash folder first, and Undo moves them back. A session written to in the last minute is left alone.": "Geändert wird nur der Anbieter, den jede Sitzung in ihren Dateien und in der Datenbank von Codex nennt: Nachrichten, ID, Titel und Archivstatus bleiben, wie sie sind. Vorher wird eine Kopie der Dateien im Papierkorbordner von magpie abgelegt, und Rückgängig zieht sie zurück. Eine Sitzung, in die in der letzten Minute geschrieben wurde, bleibt unverändert.", "Made with {from}: Codex's history lists only {to}'s sessions now": "Mit {from} erstellt: Der Verlauf von Codex zeigt jetzt nur Sitzungen von {to}", "{n} sessions moved to magpie's trash": "{n} Sitzungen in den Papierkorb von magpie verschoben", diff --git a/internal/gui/assets/plugins/sessions/sessions.css b/internal/gui/assets/plugins/sessions/sessions.css index 8bbab369a..62e58ae3f 100644 --- a/internal/gui/assets/plugins/sessions/sessions.css +++ b/internal/gui/assets/plugins/sessions/sessions.css @@ -120,6 +120,9 @@ body.window .sm-group .row.sm-sess { padding-left: 32px; } .sm-provider-list li .name { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .sm-provider-list li .note { flex: none; font-size: 11px; color: var(--faint); } .sm-provider-list { max-height: min(300px, 45vh); overflow-y: auto; } +.sm-provider-pick { grid-column: 1 / -1; min-width: 0; display: flex; align-items: center; gap: 4px 12px; flex-wrap: wrap; } +.sm-provider-pick .grow { flex: 1; } +.sm-provider-pick .text { flex: none; font-size: 12px; } /* Recording gateway conversations (#1355): a setting card above the list, its switch, what it keeps and where, and a labelled delete */ .list.row.sm-record { gap: 6px 12px; min-height: 48px; padding: 9px 11px 9px 13px; height: auto; flex-wrap: wrap; } diff --git a/internal/gui/tests/sessions-codex-provider-pick.test.cjs b/internal/gui/tests/sessions-codex-provider-pick.test.cjs new file mode 100644 index 000000000..1df282c94 --- /dev/null +++ b/internal/gui/tests/sessions-codex-provider-pick.test.cjs @@ -0,0 +1,130 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The Move sessions to {to}? dialog (#887) lists every Codex session made with +// another provider, each ticked. Over a list of more than one, a quiet row +// says how many are picked, with Select all (Deselect all once all are) and +// Invert selection (Cyrus on Discord). Each press re-ticks the boxes, the +// count and Move follow (none picked: Move is off), and Move posts the ones +// picked. At a narrow width the row stays inside the dialog. In every +// language, Chromium and WebKit; no backend, the API is 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 assets = path.resolve(__dirname, "../assets"); +const at = (min) => new Date(Date.now() - min * 60e3).toISOString(); +const sess = (id, title, provider, min) => ({ + agent: "codex", id, title, cwd: "/work/app", start: at(min + 30), last: at(min), models: [], cost: 0, unpriced: 0, + resume: "codex resume " + id, path: "~/.codex/sessions/x/" + id + ".jsonl", size: 2048, messages: 4, files: 1, deletable: true, + provider, uses_provider: "magpie", +}); + +function serve(lang, calls) { + const sessions = [sess("a1", "fix the login form", "custom", 5), sess("b2", "write the post", "custom", 10), sess("c3", "plan the trip", "custom", 15), sess("d4", "new work", "magpie", 1)]; + 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({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/sessions/manage") { + return json({ + agents: [{ agent: "codex", count: sessions.length, deletable: true, name: "Codex", icon: "codex-color" }], + agent: "codex", sessions, terminal: false, trash: [], trashDir: "~/Library/Application Support/magpie/trash/sessions", + }); + } + if (url.pathname === "/api/sessions/codex-provider") { + const body = route.request().postDataJSON(); + calls.push(body); + return json({ moved: body.moves.map((m) => ({ id: m.id, from: "custom", to: m.to, files: [], backup: "" })), refused: [] }); + } + 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: { nav: "Sessions", button: "Move to magpie…", move: "Move", all: "Select all", none: "Deselect all", invert: "Invert selection", n: (n) => `${n} selected` }, + zh: { nav: "会话", button: "迁移到 magpie…", move: "迁移", all: "全选", none: "取消全选", invert: "反选", n: (n) => `已选 ${n} 个` }, + "zh-TW": { nav: "會話", button: "遷移到 magpie…", move: "遷移", all: "全選", none: "取消全選", invert: "反選", n: (n) => `已選 ${n} 個` }, + ja: { nav: "セッション", button: "magpie に移行…", move: "移行", all: "すべて選択", none: "すべての選択を解除", invert: "選択を反転", n: (n) => `${n} 件選択中` }, + de: { nav: "Sitzungen", button: "Zu magpie umziehen…", move: "Umziehen", all: "Alle auswählen", none: "Alle abwählen", invert: "Auswahl umkehren", n: (n) => `${n} ausgewählt` }, +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + const w = words[lang]; + test(`${engine} ${lang}: the move dialog picks all, deselects all and inverts, and Move follows`, 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: 900, height: 640 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + page.on("dialog", (d) => { errors.push("a browser dialog: " + d.message()); d.dismiss(); }); + const calls = []; + await page.route("**/*", serve(lang, calls)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#nav").getByRole("button", { name: w.nav, exact: true }).click(); + const view = page.locator("#view-sessions"); + await view.locator(".sm-provider").waitFor(); + await page.setViewportSize({ width: 420, height: 640 }); + await view.locator(".sm-provider").getByRole("button", { name: w.button, exact: true }).click(); + const ask = page.locator("#modal .sm-provider-ask"); + await ask.waitFor(); + const pick = ask.locator(".sm-provider-pick"); + const move = ask.getByRole("button", { name: w.move, exact: true }); + const state = () => ask.evaluate((e) => ({ + ticked: [...e.querySelectorAll(".sm-provider-list li")].filter((li) => li.querySelector("input").checked).map((li) => li.querySelector(".name").textContent), + count: e.querySelector(".sm-provider-pick .sm-count").textContent, + all: e.querySelector(".sm-provider-pick .sm-pick-all").textContent, + off: e.querySelector(".bar .primary").disabled, + })); + const every = ["fix the login form", "write the post", "plan the trip"]; + + // the row is above the list, inside the dialog at 420px + const box = await pick.evaluate((e) => { + const r = e.getBoundingClientRect(), d = e.closest(".sm-ask").getBoundingClientRect(), l = e.parentElement.querySelector(".sm-provider-list").getBoundingClientRect(); + return { inside: r.left >= d.left - 0.5 && r.right <= d.right + 0.5, above: r.bottom <= l.top + 0.5, fits: e.scrollWidth <= e.clientWidth + 1 }; + }); + assert.deepEqual(box, { inside: true, above: true, fits: true }); + assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true, "no sideways scroll"); + + // every one ticked to start: Deselect all + assert.deepEqual(await state(), { ticked: every, count: w.n(3), all: w.none, off: false }); + await pick.getByRole("button", { name: w.none, exact: true }).click(); + assert.deepEqual(await state(), { ticked: [], count: w.n(0), all: w.all, off: true }); + await pick.getByRole("button", { name: w.all, exact: true }).click(); + assert.deepEqual(await state(), { ticked: every, count: w.n(3), all: w.none, off: false }); + + // one unticked by hand, then inverted: only it is picked + await ask.locator(".sm-provider-list li", { hasText: "write the post" }).locator("input").uncheck(); + assert.deepEqual(await state(), { ticked: ["fix the login form", "plan the trip"], count: w.n(2), all: w.all, off: false }); + await pick.getByRole("button", { name: w.invert, exact: true }).click(); + assert.deepEqual(await state(), { ticked: ["write the post"], count: w.n(1), all: w.all, off: false }); + // inverting all gives none, and Move is off + await pick.getByRole("button", { name: w.all, exact: true }).click(); + await pick.getByRole("button", { name: w.invert, exact: true }).click(); + assert.deepEqual(await state(), { ticked: [], count: w.n(0), all: w.all, off: true }); + await pick.getByRole("button", { name: w.invert, exact: true }).click(); + await ask.locator(".sm-provider-list li", { hasText: "plan the trip" }).locator("input").uncheck(); + await ask.locator(".sm-provider-list li", { hasText: "fix the login form" }).locator("input").uncheck(); + await pick.getByRole("button", { name: w.invert, exact: true }).click(); + assert.deepEqual(await state(), { ticked: ["fix the login form", "plan the trip"], count: w.n(2), all: w.all, off: false }); + + // Move posts what is picked + assert.equal(calls.length, 0, "picking moves nothing"); + await move.click(); + await ask.waitFor({ state: "detached" }); + assert.deepEqual(calls, [{ moves: [{ id: "a1", to: "magpie" }, { id: "c3", to: "magpie" }] }]); + assert.deepEqual(errors, []); + }); + } +}