feat: dock model and storage (herdr-composer.dock)
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
4bb0e85612
commit
6b1f946d25
4 files changed
+401
-1
No files matched your search
@@ -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<string, unknown>} */ (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() === "",
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -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:<agent>`. */
|
||||
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.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user