diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 95d829ae5..da334c7a3 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -4862,6 +4862,12 @@ function routingPage(part) { const openedOf = (r) => { let s = opened.get(r); if (!s) opened.set(r, s = new Set()); return s; }; // the condition to open once the rules are drawn: {r, k} let focusCond = null; + // a rule with nothing to match by can't be saved (provider.cleanRules + // refuses it too): one Save refused for it says so under it, with the + // way out, until it has a condition or is removed + const bareRule = (r) => r.pause ? !condSet(r, "time") && !r.agents.length && !r.folders?.length && !r.exceptFolders?.length + : !COND_KINDS.some(([k]) => condSet(r, k)); + const refused = new WeakSet(); // what a rule can hold, in the order a rule reads: [kind, its name in // the menu, whether a pause takes it]. A pause holds by hours, days, // agents and folders only: the others are about a request, and a pause @@ -4956,7 +4962,7 @@ function routingPage(part) { // one line of words: a pasted line break is a space if (/[\r\n]/.test(ii.value)) { const at = ii.selectionStart; ii.value = ii.value.replace(/\r?\n|\r/g, " "); ii.setSelectionRange(at, at); } r.intent = ii.value; it.classList.toggle("on", !!r.intent.trim()); - size(); + size(); warn(); // Jev, when there is one, as the effort picked per turn takes it if (r.intent.trim() && !d.classifier && deciders.length) d.classifier = deciders[0].id; drawClassifier(); @@ -4998,7 +5004,7 @@ function routingPage(part) { { value: "", label: t("any reasoning"), note: t("not a condition") }, { value: "on", label: t("reasoning on"), note: t("thinking or any effort level") }, ...EFFORTS.map((v) => ({ value: v, label: t("reasoning ≥ {level}", { level: v }) })), - ], (v) => { r.effort = v; ef.textContent = effortName(v); ef.classList.toggle("on", !!v); }); + ], (v) => { r.effort = v; ef.textContent = effortName(v); ef.classList.toggle("on", !!v); warn(); }); wrap("effort", ef); if (mine === "effort") focus = () => { ef.focus({ preventScroll: true }); ef.onclick({ stopPropagation() {} }); }; } @@ -5021,7 +5027,7 @@ function routingPage(part) { ...clients.map((a) => ({ value: a.id, label: a.name, icon: a.icon, note: r.agents.includes(a.id) ? "✓" : "" })), ], (v) => { r.agents = !v ? [] : r.agents.includes(v) ? r.agents.filter((x) => x !== v) : [...r.agents, v]; - ag.textContent = agentsName(); ag.classList.toggle("on", r.agents.length > 0); + ag.textContent = agentsName(); ag.classList.toggle("on", r.agents.length > 0); warn(); }); wrap("agents", ag); if (mine === "agents") focus = () => { ag.focus({ preventScroll: true }); ag.onclick({ stopPropagation() {} }); }; @@ -5033,7 +5039,7 @@ function routingPage(part) { fd.onclick = (e) => { if (e.target === fd || e.target.tagName === "SPAN") fi.focus(); }; const fi = input((r.folders || []).join(", "), "~/work/acme, ~/oss"); fd.title = t("The agent works in one of these folders or a folder inside one, as it tells the model (Claude Code, Codex, OpenCode, Pi); an agent that doesn't say isn't matched"); - fi.oninput = () => { r.folders = fi.value.split(",").map((x) => x.trim()).filter(Boolean); fd.classList.toggle("on", r.folders.length > 0); fitBox(fi); }; + fi.oninput = () => { r.folders = fi.value.split(",").map((x) => x.trim()).filter(Boolean); fd.classList.toggle("on", r.folders.length > 0); fitBox(fi); warn(); }; fi.onkeydown = (e) => e.stopPropagation(); fitBox(fi); fd.append(el("span", "", t("in folder")), fi); @@ -5048,7 +5054,7 @@ function routingPage(part) { xf.onclick = (e) => { if (e.target === xf || e.target.tagName === "SPAN") xi.focus(); }; const xi = input((r.exceptFolders || []).join(", "), "~/big-task"); xf.title = t("The agent works in none of these folders, nor a folder inside one, as it tells the model; an agent that doesn't say counts as outside them"); - xi.oninput = () => { r.exceptFolders = xi.value.split(",").map((x) => x.trim()).filter(Boolean); xf.classList.toggle("on", r.exceptFolders.length > 0); fitBox(xi); }; + xi.oninput = () => { r.exceptFolders = xi.value.split(",").map((x) => x.trim()).filter(Boolean); xf.classList.toggle("on", r.exceptFolders.length > 0); fitBox(xi); warn(); }; xi.onkeydown = (e) => e.stopPropagation(); fitBox(xi); xf.append(el("span", "", t("not in folder")), xi); @@ -5125,7 +5131,10 @@ function routingPage(part) { ub.onclick = (ev) => pickFrom("member", ub, ev, d.members.map((id) => { const s = subOf(id); return { value: id, label: memberName(id), note: memberNote(id), icon: s ? undefined : modelOf(id)?.icon, icons: s ? groupIcons(s) : undefined }; }), (v) => { r.use = v; drawUse(); warn(); }); const hint = el("span", "rt-rwarn"); + const need = el("div", "rt-rneed"); const warn = () => { + need.hidden = !(refused.has(r) && bareRule(r)); + need.textContent = need.hidden ? "" : t(r.pause ? "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu" : "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu"); const m = infoOf(r.use), bits = []; if (r.pause) bits.push(t("left out of the group then: not tried, not a fallback")); if (!r.pause && r.images && m && m.ready && !m.images) bits.push(t("it doesn't take images")); @@ -5198,7 +5207,7 @@ function routingPage(part) { { name: "Remove rule", icon: CROSS, sep: true, run: () => { d.rules.splice(i, 1); drawRules(); } }, ]); }; - row.append(el("span", "i", String(i + 1)), line, then, more); + row.append(el("span", "i", String(i + 1)), line, then, more, need); rlist.append(row); }); drawClassifier(); @@ -5379,10 +5388,16 @@ function routingPage(part) { // a folder is a whole path (provider.cleanFolder) const badDir = d.rules.findIndex((r) => [...(r.folders || []), ...(r.exceptFolders || [])].some((f) => !/^(~$|~[\\/]|\/|\\\\|[A-Za-z]:([\\/]|$))/.test(f))); if (badDir >= 0) return status(t("Rule {n}: a folder is a whole path, like ~/work or /Users/me/work", { n: badDir + 1 }), "warn"); - const bare = d.rules.findIndex((r) => !r.pause && !r.tokens && !r.images && !r.effort && !r.agents.length && !r.intent && !r.compact && !r.time && !r.folders?.length && !r.exceptFolders?.length); - if (bare >= 0) return status(t("Rule {n} needs a condition", { n: bare + 1 }), "warn"); - const bareP = d.rules.findIndex((r) => r.pause && !r.time && !r.agents.length && !r.folders?.length && !r.exceptFolders?.length); - if (bareP >= 0) return status(t("Rule {n}: a pause needs hours, days, agents or folders", { n: bareP + 1 }), "warn"); + // a rule with nothing to match by: said under each such rule, with + // the way out (its + or its ⋯'s Remove rule), and in the status line + const bares = d.rules.filter(bareRule); + if (bares.length) { + for (const r of bares) refused.add(r); + const n = d.rules.indexOf(bares[0]); + focusCond = { r: bares[0], k: "+" }; + drawRules(); + return status(t(bares[0].pause ? "Rule {n}: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu" : "Rule {n} has no condition. Add one with its +, or remove the rule from its ⋯ menu", { n: n + 1 }), "warn"); + } if (d.rules.some((r) => r.intent) && !d.classifier) return status(t("Choose the model that tells which intent a message is"), "warn"); if (d.effort === "auto" && !d.classifier) return status(t("Choose the model that rates how hard a turn is"), "warn"); if (own && !d.levels.length) return status(t("Pick a level to offer, or leave them to its models"), "warn"); diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js index 12d8c8468..9b61a1e55 100644 --- a/internal/gui/assets/plugins/routing/i18n.js +++ b/internal/gui/assets/plugins/routing/i18n.js @@ -209,7 +209,8 @@ magpie.strings({ "≥ {n} tokens": "≥ {n} tokens", "1 rule": "1 条规则", "{n} rules": "{n} 条规则", - "Rule {n} needs a condition": "第 {n} 条规则还没有条件", + "Rule {n} has no condition. Add one with its +, or remove the rule from its ⋯ menu": "第 {n} 条规则还没有条件:用它的 + 添加一个,或在它的 ⋯ 菜单里删除这条规则", + "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu": "这条规则还没有条件:用它的 + 添加一个,或在它的 ⋯ 菜单里删除这条规则", "goes first; the others stay behind it": "先发给它;其余的在它后面备用", "left out of the group: not tried, not a fallback": "移出分组:不会被调用,也不做备用", "left out of the group then: not tried, not a fallback": "这段时间移出分组:不会被调用,也不做备用", @@ -217,7 +218,8 @@ magpie.strings({ "not in folder": "不在文件夹", "The agent works in none of these folders, nor a folder inside one, as it tells the model; an agent that doesn't say counts as outside them": "Agent 的工作目录不是这些文件夹,也不在它们之中(以它告诉模型的为准);不说明工作目录的 Agent 算在它们之外", "The agent didn't say which folder it works in.": "Agent 没有说明它的工作目录。", - "Rule {n}: a pause needs hours, days, agents or folders": "第 {n} 条规则:暂停需要设时段、星期、Agent 或文件夹", + "Rule {n}: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "第 {n} 条规则:暂停需要设时段、星期、Agent 或文件夹。用它的 + 添加,或在它的 ⋯ 菜单里删除这条规则", + "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "暂停需要设时段、星期、Agent 或文件夹:用它的 + 添加,或在它的 ⋯ 菜单里删除这条规则", "{model} is paused by rule {n} ({when}), so it is left out until that ends.": "{model} 被规则 {n}({when})暂停,在此之前不会被调用。", "{model} is paused by rule {n} of {group} ({when}), so it is left out until that ends.": "{model} 被 {group} 的规则 {n}({when})暂停,在此之前不会被调用。", "asks for “{intent}”": "意图是“{intent}”", @@ -737,7 +739,8 @@ magpie.strings({ "≥ {n} tokens": "≥ {n} tokens", "1 rule": "1 條規則", "{n} rules": "{n} 條規則", - "Rule {n} needs a condition": "第 {n} 條規則還沒有條件", + "Rule {n} has no condition. Add one with its +, or remove the rule from its ⋯ menu": "第 {n} 條規則還沒有條件:用它的 + 新增一個,或在它的 ⋯ 選單裡刪除這條規則", + "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu": "這條規則還沒有條件:用它的 + 新增一個,或在它的 ⋯ 選單裡刪除這條規則", "goes first; the others stay behind it": "先發給它;其餘的在它後面備用", "left out of the group: not tried, not a fallback": "移出群組:不會被呼叫,也不做備用", "left out of the group then: not tried, not a fallback": "這段時間移出群組:不會被呼叫,也不做備用", @@ -745,7 +748,8 @@ magpie.strings({ "not in folder": "不在資料夾", "The agent works in none of these folders, nor a folder inside one, as it tells the model; an agent that doesn't say counts as outside them": "Agent 的工作目錄不是這些資料夾,也不在它們之中(以它告訴模型的為準);不說明工作目錄的 Agent 算在它們之外", "The agent didn't say which folder it works in.": "Agent 沒有說明它的工作目錄。", - "Rule {n}: a pause needs hours, days, agents or folders": "第 {n} 條規則:暫停需要設時段、星期、Agent 或資料夾", + "Rule {n}: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "第 {n} 條規則:暫停需要設時段、星期、Agent 或資料夾。用它的 + 新增,或在它的 ⋯ 選單裡刪除這條規則", + "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "暫停需要設時段、星期、Agent 或資料夾:用它的 + 新增,或在它的 ⋯ 選單裡刪除這條規則", "{model} is paused by rule {n} ({when}), so it is left out until that ends.": "{model} 被規則 {n}({when})暫停,在此之前不會被呼叫。", "{model} is paused by rule {n} of {group} ({when}), so it is left out until that ends.": "{model} 被 {group} 的規則 {n}({when})暫停,在此之前不會被呼叫。", "asks for “{intent}”": "意圖是「{intent}」", @@ -1263,7 +1267,8 @@ magpie.strings({ "≥ {n} tokens": "≥ {n} トークン", "1 rule": "1 件のルール", "{n} rules": "{n} 件のルール", - "Rule {n} needs a condition": "ルール {n} に条件がありません", + "Rule {n} has no condition. Add one with its +, or remove the rule from its ⋯ menu": "ルール {n} に条件がありません。+ で追加するか、⋯ メニューからこのルールを削除してください", + "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu": "このルールには条件がありません。+ で追加するか、⋯ メニューからこのルールを削除してください", "goes first; the others stay behind it": "最初に送信し、ほかはその後ろで控える", "left out of the group: not tried, not a fallback": "グループから外す:使われず、フォールバックにもならない", "left out of the group then: not tried, not a fallback": "この間はグループから外す:使われず、フォールバックにもならない", @@ -1271,7 +1276,8 @@ magpie.strings({ "not in folder": "除外フォルダ", "The agent works in none of these folders, nor a folder inside one, as it tells the model; an agent that doesn't say counts as outside them": "エージェントがモデルに伝える作業フォルダが、これらのフォルダでもその中でもないとき。伝えないエージェントはフォルダの外として扱います", "The agent didn't say which folder it works in.": "エージェントは作業フォルダを伝えていません。", - "Rule {n}: a pause needs hours, days, agents or folders": "ルール {n}:一時停止には時間帯、曜日、エージェント、またはフォルダが必要です", + "Rule {n}: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "ルール {n}:一時停止には時間帯、曜日、エージェント、またはフォルダが必要です。+ で追加するか、⋯ メニューからこのルールを削除してください", + "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "一時停止には時間帯、曜日、エージェント、またはフォルダが必要です。+ で追加するか、⋯ メニューからこのルールを削除してください", "{model} is paused by rule {n} ({when}), so it is left out until that ends.": "{model} はルール {n}({when})で一時停止中のため、終わるまで使われません。", "{model} is paused by rule {n} of {group} ({when}), so it is left out until that ends.": "{model} は {group} のルール {n}({when})で一時停止中のため、終わるまで使われません。", "asks for “{intent}”": "インテントが「{intent}」", @@ -1791,7 +1797,8 @@ magpie.strings({ "≥ {n} tokens": "≥ {n} Token", "1 rule": "1 Regel", "{n} rules": "{n} Regeln", - "Rule {n} needs a condition": "Regel {n} braucht eine Bedingung", + "Rule {n} has no condition. Add one with its +, or remove the rule from its ⋯ menu": "Regel {n} hat keine Bedingung. Füge mit ihrem + eine hinzu oder entferne die Regel über ihr ⋯-Menü", + "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu": "Diese Regel hat keine Bedingung. Füge mit ihrem + eine hinzu oder entferne die Regel über ihr ⋯-Menü", "goes first; the others stay behind it": "kommt zuerst; die anderen bleiben dahinter", "left out of the group: not tried, not a fallback": "aus der Gruppe genommen: nicht versucht, kein Ausweichmodell", "left out of the group then: not tried, not a fallback": "in dieser Zeit aus der Gruppe genommen: nicht versucht, kein Ausweichmodell", @@ -1799,7 +1806,8 @@ magpie.strings({ "not in folder": "nicht im Ordner", "The agent works in none of these folders, nor a folder inside one, as it tells the model; an agent that doesn't say counts as outside them": "Der Agent arbeitet in keinem dieser Ordner und keinem Ordner darin, wie er es dem Modell sagt; ein Agent, der es nicht sagt, zählt als außerhalb", "The agent didn't say which folder it works in.": "Der Agent hat nicht gesagt, in welchem Ordner er arbeitet.", - "Rule {n}: a pause needs hours, days, agents or folders": "Regel {n}: eine Pause braucht Uhrzeiten, Tage, Agenten oder Ordner", + "Rule {n}: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "Regel {n}: eine Pause braucht Uhrzeiten, Tage, Agenten oder Ordner. Füge mit ihrem + etwas hinzu oder entferne die Regel über ihr ⋯-Menü", + "A pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu": "Eine Pause braucht Uhrzeiten, Tage, Agenten oder Ordner. Füge mit ihrem + etwas hinzu oder entferne die Regel über ihr ⋯-Menü", "{model} is paused by rule {n} ({when}), so it is left out until that ends.": "{model} ist durch Regel {n} ({when}) pausiert und wird bis dahin ausgelassen.", "{model} is paused by rule {n} of {group} ({when}), so it is left out until that ends.": "{model} ist durch Regel {n} von {group} ({when}) pausiert und wird bis dahin ausgelassen.", "asks for “{intent}”": "verlangt „{intent}“", diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index 36e8593e0..bb7c164e3 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -634,10 +634,14 @@ button.rt-session { cursor: pointer; } .rt-addc.wide { width: auto; height: 26px; padding: 0 10px 0 8px; border: 1px dashed var(--line); } .rt-addc:hover, .rt-addc.open, .rt-addc:focus-visible { color: var(--fg); background: var(--card-2); } .rt-addc.wide:hover, .rt-addc.wide.open { border-color: var(--muted); } -/* the rule's ⋯: on hover, as the usage rows' */ -.rt-rule > .rt-rmore { align-self: start; opacity: 0; transition: opacity .12s, background .12s, color .12s; } -.rt-rule:hover > .rt-rmore, .rt-rule > .rt-rmore:focus-visible, .rt-rule > .rt-rmore.open { opacity: 1; } -@media (hover: none) { .rt-rule > .rt-rmore { opacity: 1; } } +/* the rule's ⋯, always drawn, in the icons' quiet grey: it is the only + way to move or remove a rule, and drawn on hover alone nobody found it + (ppBruce on Discord: "a rule added has no delete button") */ +.rt-rule > .rt-rmore { align-self: start; } +.rt-rule > .rt-rmore.open { background: var(--pill); color: var(--fg); } +/* why Save refused the rule, under it until it has a condition */ +.rt-rule > .rt-rneed { grid-column: 2 / -1; color: var(--amber, var(--muted)); font-size: 11.5px; } +.rt-rule > .rt-rneed[hidden] { display: none; } /* how a part works, under an ⓘ beside it */ .rt-info { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; color: var(--muted); cursor: help; outline: none; } .rt-info:hover, .rt-info:focus-visible { color: var(--fg); } diff --git a/internal/gui/tests/routing-pause-folder.test.cjs b/internal/gui/tests/routing-pause-folder.test.cjs index 0f3702a58..fa5a1aa6e 100644 --- a/internal/gui/tests/routing-pause-folder.test.cjs +++ b/internal/gui/tests/routing-pause-folder.test.cjs @@ -45,15 +45,15 @@ const TURN = { const words = { en: { cond: "not in ~/big-task", dir: "The agent works in /Users/me/scratch.", none: "The agent didn't say which folder it works in.", label: "not in folder", in: "in folder", aside: "a/opus is paused by rule 1 (not in ~/big-task), so it is left out until that ends. The agent works in /Users/me/scratch.", - bad: "Rule 1: a folder is a whole path, like ~/work or /Users/me/work", bare: "Rule 1: a pause needs hours, days, agents or folders", edit: "Edit", save: "Save", pause: "pause", send: "send to" }, + bad: "Rule 1: a folder is a whole path, like ~/work or /Users/me/work", bare: "Rule 1: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu", edit: "Edit", save: "Save", pause: "pause", send: "send to" }, zh: { cond: "不在 ~/big-task 中", dir: "Agent 的工作目录是 /Users/me/scratch。", none: "Agent 没有说明它的工作目录。", label: "不在文件夹", in: "项目文件夹", - bad: "规则 1:文件夹需是完整路径,如 ~/work 或 /Users/me/work", bare: "第 1 条规则:暂停需要设时段、星期、Agent 或文件夹", edit: "编辑", save: "保存", pause: "暂停", send: "先发给" }, + bad: "规则 1:文件夹需是完整路径,如 ~/work 或 /Users/me/work", bare: "第 1 条规则:暂停需要设时段、星期、Agent 或文件夹。用它的 + 添加,或在它的 ⋯ 菜单里删除这条规则", edit: "编辑", save: "保存", pause: "暂停", send: "先发给" }, "zh-TW": { cond: "不在 ~/big-task 中", dir: "Agent 的工作目錄是 /Users/me/scratch。", none: "Agent 沒有說明它的工作目錄。", label: "不在資料夾", in: "專案資料夾", - bad: "規則 1:資料夾需是完整路徑,如 ~/work 或 /Users/me/work", bare: "第 1 條規則:暫停需要設時段、星期、Agent 或資料夾", edit: "編輯", save: "儲存", pause: "暫停", send: "先發給" }, + bad: "規則 1:資料夾需是完整路徑,如 ~/work 或 /Users/me/work", bare: "第 1 條規則:暫停需要設時段、星期、Agent 或資料夾。用它的 + 新增,或在它的 ⋯ 選單裡刪除這條規則", edit: "編輯", save: "儲存", pause: "暫停", send: "先發給" }, ja: { cond: "~/big-task 以外", dir: "エージェントの作業フォルダは /Users/me/scratch です。", none: "エージェントは作業フォルダを伝えていません。", label: "除外フォルダ", in: "フォルダ", - bad: "ルール 1:フォルダは ~/work や /Users/me/work のような完全なパスで指定してください", bare: "ルール 1:一時停止には時間帯、曜日、エージェント、またはフォルダが必要です", edit: "編集", save: "保存", pause: "一時停止", send: "送信先" }, + bad: "ルール 1:フォルダは ~/work や /Users/me/work のような完全なパスで指定してください", bare: "ルール 1:一時停止には時間帯、曜日、エージェント、またはフォルダが必要です。+ で追加するか、⋯ メニューからこのルールを削除してください", edit: "編集", save: "保存", pause: "一時停止", send: "送信先" }, de: { cond: "nicht in ~/big-task", dir: "Der Agent arbeitet in /Users/me/scratch.", none: "Der Agent hat nicht gesagt, in welchem Ordner er arbeitet.", label: "nicht im Ordner", in: "im Ordner", - bad: "Regel 1: Ein Ordner ist ein ganzer Pfad, etwa ~/work oder /Users/me/work", bare: "Regel 1: eine Pause braucht Uhrzeiten, Tage, Agenten oder Ordner", edit: "Bearbeiten", save: "Speichern", pause: "pausieren", send: "senden an" }, + bad: "Regel 1: Ein Ordner ist ein ganzer Pfad, etwa ~/work oder /Users/me/work", bare: "Regel 1: eine Pause braucht Uhrzeiten, Tage, Agenten oder Ordner. Füge mit ihrem + etwas hinzu oder entferne die Regel über ihr ⋯-Menü", edit: "Bearbeiten", save: "Speichern", pause: "pausieren", send: "senden an" }, }; function serve(lang, posts, route) { diff --git a/internal/gui/tests/routing-pause.test.cjs b/internal/gui/tests/routing-pause.test.cjs index 01f04588a..e309ccb52 100644 --- a/internal/gui/tests/routing-pause.test.cjs +++ b/internal/gui/tests/routing-pause.test.cjs @@ -36,8 +36,8 @@ const route = { }; const words = { - en: { aside: "a/glm is paused by rule 1 (14:00–18:00 Mon–Fri), so it is left out until that ends.", title: "1. 14:00–18:00 Mon–Fri → pause A · glm", pause: "pause", send: "send to", edit: "Edit", add: "Add a rule", save: "Save", bare: "Rule 2: a pause needs hours, days, agents or folders", warn: "left out of the group then: not tried, not a fallback", length: "Conversation length", hours: "Hours and days", toPause: "Pause the model instead" }, - zh: { aside: "a/glm 被规则 1(周一–周五 14:00–18:00)暂停,在此之前不会被调用。", title: "1. 周一–周五 14:00–18:00 → 暂停 A · glm", pause: "暂停", send: "先发给", edit: "编辑", add: "添加规则", save: "保存", bare: "第 2 条规则:暂停需要设时段、星期、Agent 或文件夹", warn: "这段时间移出分组:不会被调用,也不做备用", length: "对话长度", hours: "时段", toPause: "改为暂停该模型" }, + en: { aside: "a/glm is paused by rule 1 (14:00–18:00 Mon–Fri), so it is left out until that ends.", title: "1. 14:00–18:00 Mon–Fri → pause A · glm", pause: "pause", send: "send to", edit: "Edit", add: "Add a rule", save: "Save", bare: "Rule 2: a pause needs hours, days, agents or folders. Add one with its +, or remove the rule from its ⋯ menu", warn: "left out of the group then: not tried, not a fallback", length: "Conversation length", hours: "Hours and days", toPause: "Pause the model instead" }, + zh: { aside: "a/glm 被规则 1(周一–周五 14:00–18:00)暂停,在此之前不会被调用。", title: "1. 周一–周五 14:00–18:00 → 暂停 A · glm", pause: "暂停", send: "先发给", edit: "编辑", add: "添加规则", save: "保存", bare: "第 2 条规则:暂停需要设时段、星期、Agent 或文件夹。用它的 + 添加,或在它的 ⋯ 菜单里删除这条规则", warn: "这段时间移出分组:不会被调用,也不做备用", length: "对话长度", hours: "时段", toPause: "改为暂停该模型" }, "zh-TW": { pause: "暫停", send: "先發給", edit: "編輯", add: "新增規則", save: "儲存" }, ja: { pause: "一時停止", send: "送信先" }, de: { pause: "pausieren", send: "senden an" }, diff --git a/internal/gui/tests/routing-rule-remove.test.cjs b/internal/gui/tests/routing-rule-remove.test.cjs new file mode 100644 index 000000000..6a9177534 --- /dev/null +++ b/internal/gui/tests/routing-rule-remove.test.cjs @@ -0,0 +1,194 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// A routing group's rule can be found and taken out (ppBruce on Discord, +// v0.1.1290: "once a rule is added there is no delete button, and a rule +// with empty conditions can't be saved"). The rule's ⋯, which holds Remove +// rule, is drawn whether or not the pointer is over the rule, and works by +// mouse and by keyboard. A rule left with no condition isn't saved: Save +// says so under that rule and in the status line, with the way out (its +, +// or Remove rule under its ⋯), and both go once the rule has a condition +// again. Chromium and WebKit, every language, at a wide and a phone's width. +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(process.env.ASSETS || path.join(__dirname, "../assets")); +const models = [ + { id: "a/opus", name: "opus", providerName: "A", icon: "generic", context: 1000000, ready: true }, + { id: "b/flash", name: "Deepseek-V4.1-Flash", providerName: "WorkBuddy AI", icon: "generic", context: 1000000, ready: true }, +]; +const member = (id) => ({ id, ready: true, context: 1000000, images: true }); +const rule = (use, more) => ({ use, pause: false, tokens: 0, images: false, effort: "", agents: [], intent: "", compact: false, folders: [], exceptFolders: [], time: null, ...more }); +// the reporter's group: one rule, "has an image" → the flash model +const groups = () => ({ + models, deciders: [], + groups: [{ id: "main", name: "Main", members: ["a/opus", "b/flash"], routing: "order", ready: true, + rules: [rule("b/flash", { images: true })], memberInfo: [member("a/opus"), member("b/flash")] }], + pools: [], +}); + +const words = { + en: { edit: "Edit", save: "Save", image: "has an image", remove: "Remove rule", + status: "Rule 1 has no condition. Add one with its +, or remove the rule from its ⋯ menu", + under: "This rule has no condition. Add one with its +, or remove the rule from its ⋯ menu" }, + zh: { edit: "编辑", save: "保存", image: "带图片", remove: "删除规则", + status: "第 1 条规则还没有条件:用它的 + 添加一个,或在它的 ⋯ 菜单里删除这条规则", + under: "这条规则还没有条件:用它的 + 添加一个,或在它的 ⋯ 菜单里删除这条规则" }, + "zh-TW": { edit: "編輯", save: "儲存", image: "帶圖片", remove: "刪除規則", + status: "第 1 條規則還沒有條件:用它的 + 新增一個,或在它的 ⋯ 選單裡刪除這條規則", + under: "這條規則還沒有條件:用它的 + 新增一個,或在它的 ⋯ 選單裡刪除這條規則" }, + ja: { edit: "編集", save: "保存", image: "画像あり", remove: "ルールを削除", + status: "ルール 1 に条件がありません。+ で追加するか、⋯ メニューからこのルールを削除してください", + under: "このルールには条件がありません。+ で追加するか、⋯ メニューからこのルールを削除してください" }, + de: { edit: "Bearbeiten", save: "Speichern", image: "enthält ein Bild", remove: "Regel entfernen", + status: "Regel 1 hat keine Bedingung. Füge mit ihrem + eine hinzu oder entferne die Regel über ihr ⋯-Menü", + under: "Diese Regel hat keine Bedingung. Füge mit ihrem + eine hinzu oder entferne die Regel über ihr ⋯-Menü" }, +}; + +function serve(lang, posts) { + const state = { agents: [], profiles: [], settings: { lang, theme: "light" } }; + return async (r) => { + const url = new URL(r.request().url()); + const json = (data) => r.fulfill({ 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(state); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json(groups()); + if (url.pathname.startsWith("/api/groups/")) { + posts.push({ path: url.pathname, body: JSON.parse(r.request().postData() || "{}") }); + return json(groups()); + } + 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)]; + try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404 }); } + }; +} + +// wheel to a control, as the reader would (the page keeps still for any +// other scroll), and return where it is +async function reach(page, loc) { + const height = page.viewportSize().height; + await page.mouse.move(Math.min(550, (page.viewportSize().width / 2) | 0), 300); + for (let i = 0; i < 30; i++) { + const y = (await loc.boundingBox()).y; + if (y > 80 && y < height - 160) break; + await page.mouse.wheel(0, y < 80 ? -300 : 300); + await page.waitForTimeout(150); + } + return loc.boundingBox(); +} + +async function press(page, loc) { + const b = await reach(page, loc); + await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2); +} + +async function openEditor(page, w) { + const group = page.locator(".rt-group").first(); + await group.waitFor(); + await press(page, group.locator("button", { hasText: w.edit })); + const ed = page.locator(".rt-gedit"); + await ed.locator(".rt-rule").first().waitFor(); + return ed; +} + +const saves = (posts) => posts.filter((p) => p.path === "/api/groups/save"); + +// the ⋯ as the reader sees it with the pointer elsewhere: drawn, inside +// the editor, and the topmost thing where it is +async function seen(page, more) { + await reach(page, more); + await page.mouse.move(1, 1); + await page.waitForTimeout(200); // past any fade + return more.evaluate((b) => { + const r = b.getBoundingClientRect(), e = b.closest(".rt-gedit").getBoundingClientRect(); + const top = document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2); + return { opacity: getComputedStyle(b).opacity, visibility: getComputedStyle(b).visibility, inside: r.width > 0 && r.left >= e.left && r.right <= e.right + 1, top: !!top && b.contains(top) }; + }); +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + const w = words[lang]; + for (const width of [1100, 390]) { + test(`${engine} ${lang} ${width}: a rule's ⋯ is there to remove it, and a rule with no condition says how`, 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 } })).newPage(); + page.setDefaultTimeout(5000); + const errors = [], posts = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, posts)); + await page.goto("http://magpie.test/?view=routing"); + + // the ⋯ is drawn with the pointer off the rule, and Remove rule + // under it takes the rule out + let ed = await openEditor(page, w); + let row = ed.locator(".rt-rule").first(); + assert.deepEqual(await seen(page, row.locator(".rt-rmore")), { opacity: "1", visibility: "visible", inside: true, top: true }, "the rule's ⋯ is there without hovering"); + await press(page, row.locator(".rt-rmore")); + await page.locator(".row-menu .rm-item", { hasText: w.remove }).click(); + assert.equal(await ed.locator(".rt-rule").count(), 0); + await press(page, ed.locator("button.primary", { hasText: w.save })); + await page.waitForFunction(() => !document.querySelector(".rt-gedit")); + assert.deepEqual(saves(posts).at(-1).body.rules, []); + + // by keyboard: Tab reaches the ⋯ (Option-Tab in WebKit, which as + // Safari tabs to fields alone), Enter opens it, Remove rule is the + // last item + const TAB = engine === "webkit" ? "Alt+Tab" : "Tab"; + ed = await openEditor(page, w); + row = ed.locator(".rt-rule").first(); + await row.locator(".rt-model").focus({ preventScroll: true }); + for (let i = 0; i < 6 && !(await row.locator(".rt-rmore").evaluate((b) => b === document.activeElement)); i++) await page.keyboard.press(TAB); + assert(await row.locator(".rt-rmore").evaluate((b) => b === document.activeElement), "Tab reaches the rule's ⋯"); + await page.keyboard.press("Enter"); + await page.locator(".row-menu").waitFor(); + await page.keyboard.press("ArrowUp"); + assert.equal((await page.evaluate(() => document.activeElement.textContent)).trim(), w.remove); + await page.keyboard.press("Enter"); + assert.equal(await ed.locator(".rt-rule").count(), 0); + await press(page, ed.locator("button.primary", { hasText: w.save })); + await page.waitForFunction(() => !document.querySelector(".rt-gedit")); + + // a rule left with no condition (its "has an image" turned off): + // Save says so under it and in the status line, with the way out, + // and saves nothing + ed = await openEditor(page, w); + row = ed.locator(".rt-rule").first(); + const n = saves(posts).length; + await press(page, row.locator('.rt-c[data-k="images"] .rt-cond')); + assert(await row.locator(".rt-rneed").isHidden(), "nothing said before Save"); + await press(page, ed.locator("button.primary", { hasText: w.save })); + await page.waitForFunction((s) => document.querySelector("#status")?.textContent === s, w.status); + row = ed.locator(".rt-rule").first(); + assert.equal(await row.locator(".rt-rneed").textContent(), w.under); + assert(await row.locator(".rt-rneed").isVisible()); + assert(await row.locator(".rt-addc").evaluate((b) => b === document.activeElement), "the rule's + has the keyboard"); + assert.equal(saves(posts).length, n, "nothing saved"); + // nothing past the editor's side, nor a sideways scroll + const over = await page.evaluate(() => { + const e = document.querySelector(".rt-gedit").getBoundingClientRect(); + return [...document.querySelectorAll(".rt-rule, .rt-rule *")].filter((x) => x.offsetParent && x.getBoundingClientRect().right > e.right + 1).map((x) => x.className || x.tagName); + }); + assert.deepEqual(over, []); + assert(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "no sideways scroll"); + // its condition on again, the line goes and Save saves it + await press(page, row.locator('.rt-c[data-k="images"] .rt-cond')); + assert(await row.locator(".rt-rneed").isHidden(), "said no more once it has a condition"); + await press(page, ed.locator("button.primary", { hasText: w.save })); + await page.waitForFunction(() => !document.querySelector(".rt-gedit")); + assert.deepEqual(saves(posts).at(-1).body.rules, [rule("b/flash", { images: true })]); + assert.deepEqual(errors, []); + }); + } + } +}