feat: persistent per-agent drafts with identity check
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
187a385f6f
commit
2ce1b45ee4
5 files changed
+367
-10
No files matched your search
@@ -70,6 +70,8 @@ export const KEYS = {
|
||||
usage: "ovis-composer.usage",
|
||||
/** The queue is per agent: `ovis-composer.queue:<agent>`. */
|
||||
queue: "ovis-composer.queue",
|
||||
/** The draft is per agent: `ovis-composer.draft:<agent>`. */
|
||||
draft: "ovis-composer.draft",
|
||||
};
|
||||
|
||||
/** How many entries the history keeps. */
|
||||
@@ -157,6 +159,92 @@ export function saveQueue(store, agent, items) {
|
||||
writeList(store, queueKey(agent), items, 0, isQueueEntry);
|
||||
}
|
||||
|
||||
/**
|
||||
* The key of one agent's draft.
|
||||
*
|
||||
* @param {string} agent
|
||||
* @returns {string}
|
||||
*/
|
||||
export function draftKey(agent) {
|
||||
return `${KEYS.draft}:${agent}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} DraftIdentity
|
||||
* @property {number|null} created_ms - when the terminal was created (`hc get`)
|
||||
* @property {string|null} app
|
||||
*
|
||||
* @typedef {object} Draft
|
||||
* @property {string} text
|
||||
* @property {number} caret
|
||||
* @property {object[]} attachments
|
||||
* @property {DraftIdentity} identity - who the draft was written for: the
|
||||
* address can be reused by another terminal after a restart
|
||||
* @property {number} savedAt
|
||||
*/
|
||||
|
||||
/**
|
||||
* The draft of one agent, or nothing when there is none (or it is rubbish).
|
||||
*
|
||||
* The text is kept whole: the limit of 20 000 characters is a limit of the
|
||||
* send, not of the draft - a long text must survive a restart.
|
||||
*
|
||||
* @param {Store} store
|
||||
* @param {string} agent
|
||||
* @returns {Draft|null}
|
||||
*/
|
||||
export function loadDraft(store, agent) {
|
||||
if (typeof agent !== "string" || agent === "") return null;
|
||||
return toDraft(read(store, draftKey(agent)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the draft of one agent. An empty draft is written too - it replaces
|
||||
* the one that was sent. What is not a draft is not written.
|
||||
*
|
||||
* @param {Store} store
|
||||
* @param {string} agent
|
||||
* @param {Draft} draft
|
||||
* @returns {void}
|
||||
*/
|
||||
export function saveDraft(store, agent, draft) {
|
||||
if (typeof agent !== "string" || agent === "") return;
|
||||
const clean = toDraft(draft);
|
||||
if (!clean) return;
|
||||
write(store, draftKey(agent), clean);
|
||||
}
|
||||
|
||||
/**
|
||||
* A draft in the shape the composer insists on, or null.
|
||||
*
|
||||
* @param {unknown} value
|
||||
* @returns {Draft|null}
|
||||
*/
|
||||
function toDraft(value) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
const raw = /** @type {Record<string, any>} */ (value);
|
||||
if (typeof raw.text !== "string") return null;
|
||||
|
||||
const caret = isTime(raw.caret) ? Math.max(0, Math.min(Math.trunc(raw.caret), raw.text.length)) : raw.text.length;
|
||||
const attachments = Array.isArray(raw.attachments)
|
||||
? raw.attachments.filter(
|
||||
(file) => file && typeof file === "object" && isText(file.id) && isText(file.name) && isText(file.path),
|
||||
)
|
||||
: [];
|
||||
const id = raw.identity && typeof raw.identity === "object" ? raw.identity : {};
|
||||
|
||||
return {
|
||||
text: raw.text,
|
||||
caret,
|
||||
attachments,
|
||||
identity: {
|
||||
created_ms: isTime(id.created_ms) ? id.created_ms : null,
|
||||
app: typeof id.app === "string" ? id.app : null,
|
||||
},
|
||||
savedAt: isTime(raw.savedAt) ? raw.savedAt : 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* How often each command was used - the numbers the ranking of the
|
||||
* suggestions breaks its ties with (`suggest.rank` reads `item.uses`).
|
||||
|
||||
Reference in new issue
Block a user