diff --git a/herdr/herdr-composer/mods/herdr-composer/dock.js b/herdr/herdr-composer/mods/herdr-composer/dock.js new file mode 100644 index 0000000..8de8b6d --- /dev/null +++ b/herdr/herdr-composer/mods/herdr-composer/dock.js @@ -0,0 +1,229 @@ +// dock.js - the model of the dock: the buttons next to the composer that send +// or insert a text kept by the person. +// +// Pure: no gpui-kit, gpui-base or herdr imports. Every function returns a new +// list and leaves its input alone. storage.js keeps the list under +// `herdr-composer.dock` and runs it through `normalize`. + +/** + * @typedef {object} DockButton + * @property {string} id + * @property {string} label + * @property {string} text + * @property {"send"|"insert"} mode + * + * @typedef {object} MenuItem + * @property {string} command + * @property {string} label + * @property {boolean} [disabled] + * @property {boolean} [danger] + */ + +/** How many buttons the dock holds. */ +export const DOCK_LIMIT = 30; + +/** The longest default label: 15 characters and an ellipsis. */ +export const LABEL_MAX = 16; + +const MODES = new Set(["send", "insert"]); + +/** + * The label a button gets when the person gave none: the text with white + * space folded into single spaces, cut to LABEL_MAX characters. + * + * @param {string} text + * @returns {string} + */ +export function defaultLabel(text) { + const folded = String(text).replace(/\s+/g, " ").trim(); + const chars = Array.from(folded); + return chars.length > LABEL_MAX ? `${chars.slice(0, LABEL_MAX - 1).join("")}…` : folded; +} + +/** + * Keep what is a button: `id` and `text` not empty, a known `mode`. An empty + * label becomes the default one. A text that is already there (compared after + * `trim()`) is dropped - the first wins - and the list is cut to DOCK_LIMIT. + * + * @param {unknown} raw + * @returns {DockButton[]} + */ +export function normalize(raw) { + if (!Array.isArray(raw)) return []; + + /** @type {DockButton[]} */ + const out = []; + const seen = new Set(); + for (const item of raw) { + if (out.length >= DOCK_LIMIT) break; + if (!item || typeof item !== "object" || Array.isArray(item)) continue; + const { id, label, text, mode } = /** @type {Record} */ (item); + if (typeof id !== "string" || id === "") continue; + if (typeof text !== "string" || text.trim() === "") continue; + if (typeof mode !== "string" || !MODES.has(mode)) continue; + const key = text.trim(); + if (seen.has(key)) continue; + seen.add(key); + out.push({ + id, + label: typeof label === "string" && label.trim() !== "" ? label : defaultLabel(text), + text, + mode: /** @type {"send"|"insert"} */ (mode), + }); + } + return out; +} + +/** + * @param {DockButton[]} dock + * @param {string} text + * @returns {boolean} + */ +export function isPinned(dock, text) { + const key = String(text).trim(); + return dock.some((button) => button.text.trim() === key); +} + +/** + * Add a button at the end. The same dock comes back when the text is empty, + * already pinned, or the dock is full. + * + * @param {DockButton[]} dock + * @param {string} text + * @param {string} id + * @returns {DockButton[]} + */ +export function pin(dock, text, id) { + const clean = String(text).trim(); + if (clean === "" || isPinned(dock, clean) || dock.length >= DOCK_LIMIT) return dock; + return [...dock, { id, label: defaultLabel(clean), text: clean, mode: "send" }]; +} + +/** + * @param {DockButton[]} dock + * @param {string} text + * @returns {DockButton[]} + */ +export function unpin(dock, text) { + const key = String(text).trim(); + return dock.filter((button) => button.text.trim() !== key); +} + +/** + * @param {DockButton[]} dock + * @param {string} id + * @returns {DockButton[]} + */ +export function remove(dock, id) { + return dock.filter((button) => button.id !== id); +} + +/** + * Move a button one place; at the edge nothing changes. + * + * @param {DockButton[]} dock + * @param {string} id + * @param {-1|1} step + * @returns {DockButton[]} + */ +export function move(dock, id, step) { + const from = dock.findIndex((button) => button.id === id); + const to = from + step; + if (from < 0 || to < 0 || to >= dock.length) return [...dock]; + const out = [...dock]; + [out[from], out[to]] = [out[to], out[from]]; + return out; +} + +/** + * An empty name (after `trim()`) gives the default label back. + * + * @param {DockButton[]} dock + * @param {string} id + * @param {string} label + * @returns {DockButton[]} + */ +export function rename(dock, id, label) { + const clean = String(label).trim(); + return dock.map((button) => + button.id === id ? { ...button, label: clean === "" ? defaultLabel(button.text) : clean } : button, + ); +} + +/** + * @param {DockButton[]} dock + * @param {string} id + * @returns {DockButton[]} + */ +export function toggleMode(dock, id) { + return dock.map((button) => + button.id === id ? { ...button, mode: button.mode === "send" ? "insert" : "send" } : button, + ); +} + +/** + * What a click on a button does. Sending needs an agent; inserting does not. + * + * @param {{mode: "send"|"insert"}} button + * @param {{hasAgent: boolean}} context + * @returns {"send"|"insert"|"none"} + */ +export function dockAction(button, { hasAgent }) { + if (button.mode === "insert") return "insert"; + return hasAgent ? "send" : "none"; +} + +/** + * The right-click menu of a button. + * + * @param {DockButton[]} dock + * @param {string} id + * @returns {MenuItem[]} + */ +export function dockMenu(dock, id) { + const index = dock.findIndex((button) => button.id === id); + const button = dock[index]; + return [ + { command: "rename", label: "Zmień nazwę" }, + { command: "mode", label: button && button.mode === "insert" ? "Wysyłaj od razu" : "Tylko wstawiaj" }, + { command: "left", label: "Przesuń w lewo", disabled: index <= 0 }, + { command: "right", label: "Przesuń w prawo", disabled: index < 0 || index >= dock.length - 1 }, + { command: "remove", label: "Usuń z docka", danger: true }, + ]; +} + +/** + * The right-click menu of a row of the history or the favourites. + * + * @param {{tab: "history"|"favorites", favorite: boolean, pinned: boolean}} row + * @returns {MenuItem[]} + */ +export function rowMenu({ tab, favorite, pinned }) { + /** @type {MenuItem[]} */ + const items = [{ command: "insert", label: "Wstaw" }]; + if (tab === "favorites") { + items.push({ command: "pin", label: pinned ? "Odepnij z docka" : "Przypnij do docka" }); + } + items.push( + favorite + ? { command: "favorite", label: "Usuń z ulubionych", danger: true } + : { command: "favorite", label: "Dodaj do ulubionych" }, + ); + return items; +} + +/** + * The right-click menu of the star: pin the text that is in the field. + * + * @param {{text: string, pinned: boolean}} state + * @returns {MenuItem[]} + */ +export function starMenu({ text, pinned }) { + return [ + { + command: "pin", + label: pinned ? "Odepnij bieżący tekst z docka" : "Przypnij bieżący tekst do docka", + disabled: String(text).trim() === "", + }, + ]; +} diff --git a/herdr/herdr-composer/mods/herdr-composer/storage.js b/herdr/herdr-composer/mods/herdr-composer/storage.js index 81c6f9f..cde05b7 100644 --- a/herdr/herdr-composer/mods/herdr-composer/storage.js +++ b/herdr/herdr-composer/mods/herdr-composer/storage.js @@ -47,6 +47,8 @@ * @property {string} text * @property {number} createdAt * + * @typedef {import("./dock.js").DockButton} DockButton + * * @typedef {object} QueueEntry * @property {string} id * @property {string} text @@ -56,6 +58,8 @@ * @property {string} [reason] */ +import { normalize as normalizeDock } from "./dock.js"; + /** The states a queue entry may be in (the ones state.js knows). */ const QUEUE_STATES = new Set(["queued", "countdown", "sending", "held", "uncertain"]); @@ -79,6 +83,8 @@ export const KEYS = { queue: "herdr-composer.queue", /** The draft is per agent: `herdr-composer.draft:`. */ draft: "herdr-composer.draft", + /** The buttons of the dock (dock.js). New in herdr-composer: no legacy key. */ + dock: "herdr-composer.dock", }; /** How many entries the history keeps. */ @@ -139,6 +145,26 @@ export function saveFavorites(store, items) { writeList(store, KEYS.favorites, items, FAVORITES_LIMIT, isFavoriteEntry, toFavoriteEntry); } +/** + * The buttons of the dock. Only what is a button comes back (dock.js + * `normalize`). The dock is new, so there is no `ovis-composer.dock` to read. + * + * @param {Store} store + * @returns {DockButton[]} + */ +export function loadDock(store) { + return normalizeDock(read(store, KEYS.dock)); +} + +/** + * @param {Store} store + * @param {DockButton[]} items + * @returns {void} + */ +export function saveDock(store, items) { + write(store, KEYS.dock, normalizeDock(items)); +} + /** * The queue of one agent - the messages that wait for their turn. * diff --git a/tests/dock.test.mjs b/tests/dock.test.mjs new file mode 100644 index 0000000..dcdff1f --- /dev/null +++ b/tests/dock.test.mjs @@ -0,0 +1,121 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { + DOCK_LIMIT, LABEL_MAX, defaultLabel, normalize, isPinned, pin, unpin, remove, move, + rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu, +} from "../herdr/herdr-composer/mods/herdr-composer/dock.js"; + +const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra }); +const three = () => [btn("1", "a"), btn("2", "b"), btn("3", "c")]; + +test("limits", () => { + assert.equal(DOCK_LIMIT, 30); + assert.equal(LABEL_MAX, 16); +}); + +test("defaultLabel", () => { + assert.equal(defaultLabel("/handoff"), "/handoff"); + assert.equal(defaultLabel("npm test i raport po polsku"), "npm test i rapo…"); + assert.equal(defaultLabel(" a\n b "), "a b"); +}); + +test("normalize: rubbish, bad modes, duplicates, label", () => { + assert.deepEqual(normalize("x"), []); + assert.deepEqual(normalize(null), []); + const out = normalize([ + { id: "1", text: "a", mode: "send", label: "" }, + { id: "2", text: " a ", mode: "send", label: "x" }, + { id: "3", text: "b", mode: "zly" }, + ]); + assert.deepEqual(out, [{ id: "1", label: "a", text: "a", mode: "send" }]); +}); + +test("normalize: limit 30", () => { + const raw = Array.from({ length: 35 }, (_, i) => ({ id: `i${i}`, text: `t${i}`, mode: "send", label: "l" })); + assert.equal(normalize(raw).length, 30); +}); + +test("pin", () => { + const d = pin([], " /clear ", "9"); + assert.deepEqual(d, [{ id: "9", label: "/clear", text: "/clear", mode: "send" }]); + assert.equal(pin(d, "/clear", "10"), d); + assert.equal(pin(d, " ", "x"), d); + const full = Array.from({ length: 30 }, (_, i) => btn(`i${i}`, `t${i}`)); + assert.equal(pin(full, "new", "n"), full); + assert.equal(isPinned(d, " /clear "), true); + assert.equal(isPinned(d, "other"), false); +}); + +test("unpin and remove", () => { + const d = three(); + assert.deepEqual(unpin(d, " b ").map((b) => b.id), ["1", "3"]); + assert.deepEqual(remove(d, "1").map((b) => b.id), ["2", "3"]); + assert.equal(d.length, 3); +}); + +test("move: edge no change, swap, no mutation", () => { + const d = three(); + const copy = JSON.parse(JSON.stringify(d)); + assert.deepEqual(move(d, "1", -1).map((b) => b.id), ["1", "2", "3"]); + assert.deepEqual(move(d, "3", 1).map((b) => b.id), ["1", "2", "3"]); + assert.deepEqual(move(d, "1", 1).map((b) => b.id), ["2", "1", "3"]); + assert.deepEqual(d, copy); +}); + +test("rename and toggleMode", () => { + const d = three(); + assert.equal(rename(d, "1", " ")[0].label, defaultLabel("a")); + assert.equal(rename(d, "1", " Moje ")[0].label, "Moje"); + assert.equal(d[0].label, "a"); + const once = toggleMode(d, "1"); + assert.equal(once[0].mode, "insert"); + assert.equal(toggleMode(once, "1")[0].mode, "send"); + assert.equal(d[0].mode, "send"); +}); + +test("dockAction", () => { + assert.equal(dockAction({ mode: "insert" }, { hasAgent: true }), "insert"); + assert.equal(dockAction({ mode: "send" }, { hasAgent: true }), "send"); + assert.equal(dockAction({ mode: "send" }, { hasAgent: false }), "none"); +}); + +test("dockMenu", () => { + const d = three(); + const m = dockMenu(d, "1"); + assert.deepEqual(m.map((i) => i.command), ["rename", "mode", "left", "right", "remove"]); + assert.equal(m[0].label, "Zmień nazwę"); + assert.equal(m[1].label, "Tylko wstawiaj"); + assert.equal(m[2].label, "Przesuń w lewo"); + assert.equal(m[2].disabled, true); + assert.notEqual(m[3].disabled, true); + assert.equal(m[4].label, "Usuń z docka"); + assert.equal(m[4].danger, true); + const last = dockMenu(d, "3"); + assert.equal(last[3].disabled, true); + const ins = dockMenu(toggleMode(d, "2"), "2"); + assert.equal(ins[1].label, "Wysyłaj od razu"); +}); + +test("rowMenu", () => { + const h = rowMenu({ tab: "history", favorite: false, pinned: false }); + assert.deepEqual(h.map((i) => i.command), ["insert", "favorite"]); + assert.equal(h[0].label, "Wstaw"); + assert.equal(h[1].label, "Dodaj do ulubionych"); + const f = rowMenu({ tab: "favorites", favorite: true, pinned: true }); + assert.deepEqual(f.map((i) => i.command), ["insert", "pin", "favorite"]); + assert.equal(f[1].label, "Odepnij z docka"); + assert.equal(f[2].label, "Usuń z ulubionych"); + assert.equal(f[2].danger, true); + const g = rowMenu({ tab: "favorites", favorite: true, pinned: false }); + assert.equal(g[1].label, "Przypnij do docka"); +}); + +test("starMenu", () => { + assert.equal(starMenu({ text: "", pinned: false })[0].disabled, true); + assert.equal(starMenu({ text: " ", pinned: false })[0].disabled, true); + const on = starMenu({ text: "x", pinned: false }); + assert.equal(on[0].command, "pin"); + assert.equal(on[0].label, "Przypnij bieżący tekst do docka"); + assert.notEqual(on[0].disabled, true); + assert.equal(starMenu({ text: "x", pinned: true })[0].label, "Odepnij bieżący tekst z docka"); +}); diff --git a/tests/storage.test.mjs b/tests/storage.test.mjs index 2c61350..3ae0b22 100644 --- a/tests/storage.test.mjs +++ b/tests/storage.test.mjs @@ -2,7 +2,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import { HISTORY_LIMIT, queueKey, loadHistory, saveHistory, loadQueue, saveQueue, loadUsage, saveUsage, - KEYS, draftKey, loadDraft, saveDraft, + KEYS, draftKey, loadDraft, saveDraft, loadDock, saveDock, } from "../herdr/herdr-composer/mods/herdr-composer/storage.js"; const memStore = () => { @@ -236,3 +236,27 @@ test("migration: a store that cannot be written to still reads the old data", () const store = { get: (k) => raw.get(k), set: () => { throw new Error("read-only"); } }; assert.deepEqual(loadHistory(store).map((e) => e.id), ["old"]); }); + +test("dock roundtrip under herdr-composer.dock", () => { + const store = memStore(); + const d = [{ id: "1", label: "a", text: "a", mode: "send" }, { id: "2", label: "b", text: "b", mode: "insert" }]; + saveDock(store, d); + assert.equal(KEYS.dock, "herdr-composer.dock"); + assert.deepEqual(store.get("herdr-composer.dock"), d); + assert.deepEqual(loadDock(store), d); +}); + +test("dock does not read the legacy key", () => { + const store = memStore(); + store.set("ovis-composer.dock", [{ id: "1", label: "a", text: "a", mode: "send" }]); + assert.deepEqual(loadDock(store), []); + assert.equal(store.get("herdr-composer.dock"), undefined); +}); + +test("dock rubbish loads as empty", () => { + const store = memStore(); + store.set("herdr-composer.dock", "smieci"); + assert.deepEqual(loadDock(store), []); + store.set("herdr-composer.dock", { a: 1 }); + assert.deepEqual(loadDock(store), []); +});