feat: history and favorites model

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:31:37 +01:00
1 parent 86ca415d96
commit c655e46dbf
4 files changed
+353 -25

No files matched your search

+61 -22
View File
@@ -35,22 +35,17 @@
* @typedef {object} HistoryEntry
* @property {string} id
* @property {string} text
* @property {number} at
* @property {number} sentAt
* @property {string} agent
* @property {string} agentName
* @property {string} space
* @property {string} spaceName
* @property {string} tab
* @property {string} pane
* @property {string|null} machine
* @property {string[]} attachments
* @property {"sent"|"queued"|"failed"} outcome
* @property {string} [tabName]
*
* @typedef {object} FavoriteEntry
* @property {string} id
* @property {string} text
* @property {number} at
* @property {string} agent
* @property {"history"|"manual"} source
* @property {number} createdAt
*
* @typedef {object} QueueEntry
* @property {string} id
@@ -101,7 +96,7 @@ export function queueKey(agent) {
* @returns {HistoryEntry[]}
*/
export function loadHistory(store) {
return readList(store, KEYS.history, HISTORY_LIMIT, isTextEntry);
return readList(store, KEYS.history, HISTORY_LIMIT, isHistoryEntry, toHistoryEntry);
}
/**
@@ -110,7 +105,7 @@ export function loadHistory(store) {
* @returns {void}
*/
export function saveHistory(store, items) {
writeList(store, KEYS.history, items, HISTORY_LIMIT, isTextEntry);
writeList(store, KEYS.history, items, HISTORY_LIMIT, isHistoryEntry, toHistoryEntry);
}
/**
@@ -121,7 +116,7 @@ export function saveHistory(store, items) {
* @returns {FavoriteEntry[]}
*/
export function loadFavorites(store) {
return readList(store, KEYS.favorites, FAVORITES_LIMIT, isTextEntry);
return readList(store, KEYS.favorites, FAVORITES_LIMIT, isFavoriteEntry, toFavoriteEntry);
}
/**
@@ -130,7 +125,7 @@ export function loadFavorites(store) {
* @returns {void}
*/
export function saveFavorites(store, items) {
writeList(store, KEYS.favorites, items, FAVORITES_LIMIT, isTextEntry);
writeList(store, KEYS.favorites, items, FAVORITES_LIMIT, isFavoriteEntry, toFavoriteEntry);
}
/**
@@ -302,11 +297,14 @@ export function saveUsage(store, counts) {
* @param {string} key
* @param {number} limit - 0 for a list without a limit
* @param {(entry:Record<string, unknown>) => boolean} isValid
* @param {(entry:any) => any} [shape] - the entry in the shape the composer
* uses today (an old entry gets what it lacks)
* @returns {any[]}
*/
function readList(store, key, limit, isValid) {
const list = toList(read(store, key), isValid);
if (!list) return [];
function readList(store, key, limit, isValid, shape) {
const found = toList(read(store, key), isValid);
if (!found) return [];
const list = shape ? found.map(shape) : found;
return limit > 0 ? list.slice(-limit) : list;
}
@@ -318,14 +316,16 @@ function readList(store, key, limit, isValid) {
* @param {unknown} items
* @param {number} limit - 0 for a list without a limit
* @param {(entry:Record<string, unknown>) => boolean} isValid
* @param {(entry:any) => any} [shape]
* @returns {void}
*/
function writeList(store, key, items, limit, isValid) {
function writeList(store, key, items, limit, isValid, shape) {
const source = listSource(items);
// Rubbish in - nothing is written, so the stored list stays as it was.
if (!source) return;
const list = toList(source, isValid) ?? [];
const valid = toList(source, isValid) ?? [];
const list = shape ? valid.map(shape) : valid;
// A list that lost every entry is rubbish, not an empty list - writing it
// would wipe what is there.
if (source.length > 0 && list.length === 0) return;
@@ -366,14 +366,53 @@ function isEntry(entry, isValid) {
}
/**
* An entry the view can show - a history entry and a favourite have the same
* shape: a text and a moment.
* A history entry the view can show: a text and the moment it was sent.
* Entries from before the panel have the moment in `at`.
*
* @param {Record<string, unknown>} entry
* @returns {boolean}
*/
function isTextEntry(entry) {
return isText(entry.id) && isText(entry.text) && isTime(entry.at);
function isHistoryEntry(entry) {
return isText(entry.id) && isText(entry.text) && (isTime(entry.sentAt) || isTime(entry.at));
}
/**
* A favourite the view can show: a text and the moment it was marked
* (`createdAt`, or `at` in the older entries).
*
* @param {Record<string, unknown>} entry
* @returns {boolean}
*/
function isFavoriteEntry(entry) {
return isText(entry.id) && isText(entry.text) && (isTime(entry.createdAt) || isTime(entry.at));
}
/**
* The history entry as it is kept today. An old entry lacks the names of the
* agent and the space - they become empty texts, and `at` becomes `sentAt`.
*
* @param {any} entry
* @returns {object}
*/
function toHistoryEntry(entry) {
const { at, ...rest } = entry;
return {
...rest,
sentAt: isTime(entry.sentAt) ? entry.sentAt : at,
agent: isText(entry.agent) ? entry.agent : "",
agentName: isText(entry.agentName) ? entry.agentName : "",
space: isText(entry.space) ? entry.space : "",
spaceName: isText(entry.spaceName) ? entry.spaceName : "",
};
}
/**
* @param {any} entry
* @returns {object}
*/
function toFavoriteEntry(entry) {
const { at, ...rest } = entry;
return { ...rest, createdAt: isTime(entry.createdAt) ? entry.createdAt : at };
}
/**