diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 319fd801c..8eb14f3f7 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -9729,6 +9729,13 @@ function drawEditor(p, presetID) { // and Advanced opened when one is on, as for Routing: the user who set // a warm-up finds it where it went (ChrisYagao on X: "我的预热去哪里了") if (subOf(a.agent)) advIn.append(...provPrefsField(a.agent, () => !(advanced && advanced.of === of) && provPrefsOn(a.agent) && (adv.open = true))); + // the Codex client, from Agents › Codex too, which gateway mode has not + if (a.agent === "codex") { + const box = el("div", "codex-client"), draw = () => box.replaceChildren(codexMenuBtn(CODEX_CLIENTS, prefs?.codexClient, "Codex client", "codexClient", "client", draw)); + draw(); + if (!prefs) loadSettings().then(draw, () => {}); + advIn.append(...field(t("Codex client"), box, t("Which Codex requests to your ChatGPT accounts say they come from: the CLI or the desktop app"))); + } // a subscription's window too: Codex's backend says 272K for models // that take 872K (#120) const cx = input(draft.contexts || "", t("e.g. 128k · or gpt-6=1m, comma separated")); @@ -21534,7 +21541,8 @@ function renderCodexServiceTier(s) { codexMenuPick(CODEX_TIERS, s.codexServiceTi // a Codex row's button: the choice made, and the app's menu to change it, // posted on its own to settings/ as {: value} -function codexMenuPick(opts, v, label, key, field) { +function codexMenuPick(opts, v, label, key, field) { $("#" + key + "Pick").replaceChildren(codexMenuBtn(opts, v, label, key, field)); } +function codexMenuBtn(opts, v, label, key, field, done) { const cur = opts.find((o) => o.v === (v || "")) || opts[0]; const b = condButton(label); b.append(t(cur.name)); @@ -21542,9 +21550,9 @@ function codexMenuPick(opts, v, label, key, field) { b.onclick = (e) => { e.stopPropagation(); if (b.classList.contains("open")) return closeProtoMenu(); - openProtoMenu(b, opts, cur.v, (x) => x !== cur.v && prefWrite(api("settings/" + key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()), { [field]: x })), label, "sess-menu"); + openProtoMenu(b, opts, cur.v, (x) => x !== cur.v && prefWrite(api("settings/" + key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()), { [field]: x })).then(done), label, "sess-menu"); }; - $("#" + key + "Pick").replaceChildren(b); + return b; } // the Codex magpie presents to ChatGPT (ouhthuo on X) diff --git a/internal/gui/tests/codex-client-editor.test.cjs b/internal/gui/tests/codex-client-editor.test.cjs new file mode 100644 index 000000000..21c16c354 --- /dev/null +++ b/internal/gui/tests/codex-client-editor.test.cjs @@ -0,0 +1,117 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The Codex client (which Codex magpie's ChatGPT requests say they come +// from) is set in Agents › Codex, which gateway mode has not, so the Codex +// provider's editor has it under Advanced too: the same app menu (no +// native select), posted to settings/codex-client on its own, the button +// saying the pick at once. In the app and in gateway mode, every language, +// Chromium and WebKit; no backend, the API is faked here. +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 codex = () => ({ + id: "codex", name: "Codex", icon: "codex-color", chat: "", responses: "", anthropic: "", catalog: "", + models: [{ id: "gpt-6", name: "GPT-6", on: true }], agents: [], fallback: [], headers: {}, keyList: [], + account: { agent: "codex", agentName: "Codex", user: "me@example.com", plan: "PLUS", logins: [{ user: "me@example.com", plan: "PLUS", active: true, on: true }] }, + proxy: "", maxConcurrency: null, +}); +const claude = () => ({ + ...codex(), id: "claude", name: "Claude", icon: "claude-color", models: [{ id: "claude-x", name: "Claude X", on: true }], + account: { agent: "claude", agentName: "Claude Code", user: "me@example.com", plan: "MAX", logins: [{ user: "me@example.com", plan: "MAX", active: true, on: true }] }, +}); + +function serve(lang, gateway, posted) { + let cur = { theme: "light", lang, version: "0.1.400", titleModels: [], codexClient: "", ...(gateway ? { web: true, gatewayOn: true } : {}) }; + const providers = { providers: [codex(), claude()], presets: [], excluded: [], gateway: { running: true, window: true } }; + return async (route) => { + const req = route.request(), url = new URL(req.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:${gateway}${gateway ? ",gateway: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/providers") return json(providers); + if (url.pathname === "/api/settings/codex-client") { + const body = req.postDataJSON(); + posted.push(body); + cur = { ...cur, codexClient: body.client }; + return json(cur); + } + if (url.pathname === "/api/settings") return json(cur); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], models: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + 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)]; + try { + await route.fulfill({ body: await fs.readFile(file), contentType }); + } catch { + await route.fulfill({ status: 404, body: "" }); + } + }; +} + +// the words as i18n.js has them +const W = { + en: { name: "Codex client", cli: "Codex CLI", desktop: "Codex Desktop" }, + zh: { name: "Codex 客户端", cli: "Codex 命令行", desktop: "Codex 桌面版" }, + "zh-TW": { name: "Codex 用戶端", cli: "Codex 命令列", desktop: "Codex 桌面版" }, + ja: { name: "Codex クライアント", cli: "Codex CLI", desktop: "Codex Desktop" }, + de: { name: "Codex-Client", cli: "Codex CLI", desktop: "Codex Desktop" }, +}; +const engines = process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"]; +const langs = process.env.LANGS ? process.env.LANGS.split(",") : Object.keys(W); +const exact = (s) => new RegExp("^\\s*" + s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "\\s*$"); + +for (const engine of engines) { + for (const lang of langs) { + for (const gateway of [false, true]) { + test(`${engine} ${lang}${gateway ? " gateway mode" : ""}: the Codex provider's Advanced picks the Codex client`, async (t) => { + const w = W[lang]; + 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: 720 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(10000); + page.setDefaultNavigationTimeout(60000); + const errors = [], posted = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, gateway, posted)); + const advanced = async (id) => { + await page.goto(`http://magpie.test/?view=providers&edit=${id}`); + const adv = page.locator(".editor details.advanced"); + await adv.waitFor({ timeout: 60000 }); + if (!(await adv.evaluate((d) => d.open))) await adv.locator(":scope > summary").click(); + return adv; + }; + const label = (adv) => adv.locator("label", { hasText: exact(w.name) }); + + let adv = await advanced("codex"); + await label(adv).waitFor(); + const pick = adv.locator(".codex-client button"); + await pick.scrollIntoViewIfNeeded(); + assert.equal((await pick.textContent()).trim(), w.cli, "Codex CLI by default"); + assert.equal(await page.locator("select").count(), 0, "a native select"); + const cancel = page.locator(".editor > .bar .ed-cancel"), clean = await cancel.textContent(); + await pick.click(); + const menu = page.locator(".proto-menu"); + await menu.waitFor(); + assert.deepEqual((await menu.locator(".pm-item .pm-name").allTextContents()).map((s) => s.trim()), [w.cli, w.desktop]); + await menu.locator(".pm-item").filter({ has: page.locator(".pm-name", { hasText: exact(w.desktop) }) }).click(); + await adv.locator(".codex-client button", { hasText: exact(w.desktop) }).waitFor(); + assert.deepEqual(posted, [{ client: "desktop" }]); + // applied at once: nothing for the editor's Save to send + await page.waitForTimeout(100); + assert.equal(await cancel.textContent(), clean, "the pick left the editor with something to save"); + + // only Codex's editor has it + adv = await advanced("claude"); + await adv.locator("label").first().waitFor(); + assert.equal(await label(adv).count(), 0, "the Codex client in Claude's editor"); + assert.deepEqual(errors, []); + }); + } + } +}