2026-09-25 23:42:01 +08:00
|
|
|
import type { SessionMention } from "./types";
|
2026-10-22 18:32:18 +08:00
|
|
|
import { isLocalAttachment, type LocalAttachment } from "../../../packages/client-events/attachments";
|
2026-09-25 23:42:01 +08:00
|
|
|
|
2026-10-22 18:32:18 +08:00
|
|
|
/** Ordered attachments retain browser Files in memory and persist local references. */
|
2026-09-25 23:42:01 +08:00
|
|
|
export interface ComposerDraft {
|
|
|
|
|
text: string;
|
2026-10-22 18:32:18 +08:00
|
|
|
attachments: Array<File | LocalAttachment>;
|
2026-09-25 23:42:01 +08:00
|
|
|
sessionMentions: SessionMention[];
|
|
|
|
|
quotedContext: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const STORAGE_PREFIX = "nanobot.composer-draft.v1.";
|
|
|
|
|
const MAX_STORED_CHARS = 512 * 1024;
|
|
|
|
|
|
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
|
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readDraft(raw: string): ComposerDraft | undefined {
|
|
|
|
|
if (raw.length > MAX_STORED_CHARS) return undefined;
|
|
|
|
|
const value: unknown = JSON.parse(raw);
|
|
|
|
|
if (!isRecord(value) || typeof value.text !== "string"
|
|
|
|
|
|| (value.quotedContext !== null && typeof value.quotedContext !== "string")
|
|
|
|
|
|| !Array.isArray(value.sessionMentions)) return undefined;
|
|
|
|
|
const sessionMentions = value.sessionMentions.flatMap((item: unknown): SessionMention[] => {
|
|
|
|
|
if (!isRecord(item) || typeof item.name !== "string"
|
|
|
|
|
|| !/^[\p{L}\p{N}_-]{1,80}$/u.test(item.name)
|
|
|
|
|
|| typeof item.session_key !== "string" || !item.session_key.startsWith("websocket:")
|
|
|
|
|
|| item.session_key.length > 512 || typeof item.title !== "string") return [];
|
|
|
|
|
return [{
|
|
|
|
|
name: item.name,
|
|
|
|
|
session_key: item.session_key,
|
|
|
|
|
title: item.title.slice(0, 160),
|
|
|
|
|
...(typeof item.id === "string" && /^handle_[a-f0-9]{32}$/i.test(item.id)
|
|
|
|
|
? { id: item.id } : {}),
|
|
|
|
|
}];
|
2026-10-19 21:06:46 +08:00
|
|
|
});
|
2026-10-22 18:32:18 +08:00
|
|
|
const rawAttachments = Array.isArray(value.attachments) ? value.attachments : value.localAttachments;
|
|
|
|
|
const attachments = Array.isArray(rawAttachments) ? rawAttachments.filter(isLocalAttachment) : [];
|
|
|
|
|
return { text: value.text, quotedContext: value.quotedContext, sessionMentions, attachments };
|
2026-09-25 23:42:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeStoredDraft(key: string): void {
|
|
|
|
|
try { localStorage.removeItem(STORAGE_PREFIX + key); } catch { /* Storage is optional. */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearStoredComposerDrafts(): void {
|
|
|
|
|
try {
|
|
|
|
|
for (let index = localStorage.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const key = localStorage.key(index);
|
|
|
|
|
if (key?.startsWith(STORAGE_PREFIX)) localStorage.removeItem(key);
|
|
|
|
|
}
|
|
|
|
|
} catch { /* Storage is optional. */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ComposerDraftStore extends Map<string, ComposerDraft> {
|
|
|
|
|
override get(key: string, persist = false): ComposerDraft | undefined {
|
|
|
|
|
const cached = super.get(key);
|
|
|
|
|
if (cached || !persist) return cached;
|
|
|
|
|
try {
|
|
|
|
|
const raw = localStorage.getItem(STORAGE_PREFIX + key);
|
|
|
|
|
const draft = raw ? readDraft(raw) : undefined;
|
|
|
|
|
if (draft) super.set(key, draft);
|
|
|
|
|
return draft;
|
|
|
|
|
} catch {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override set(key: string, draft: ComposerDraft, persist = false): this {
|
|
|
|
|
const previous = super.get(key);
|
|
|
|
|
const unchanged = previous
|
|
|
|
|
&& previous.text === draft.text
|
|
|
|
|
&& previous.quotedContext === draft.quotedContext
|
2026-10-22 18:32:18 +08:00
|
|
|
&& previous.attachments.length === draft.attachments.length
|
|
|
|
|
&& previous.attachments.every((attachment, index) => attachment === draft.attachments[index])
|
2026-09-25 23:42:01 +08:00
|
|
|
&& previous.sessionMentions.length === draft.sessionMentions.length
|
|
|
|
|
&& previous.sessionMentions.every((mention, index) => mention === draft.sessionMentions[index]);
|
|
|
|
|
// Restoring a composer is not an edit. Pending sends retain this identity across mounts.
|
|
|
|
|
super.set(key, unchanged ? previous : draft);
|
|
|
|
|
if (!persist) return this;
|
2026-10-22 18:32:18 +08:00
|
|
|
if (!draft.text && !draft.quotedContext && !draft.attachments.some(isLocalAttachment)) {
|
2026-09-25 23:42:01 +08:00
|
|
|
removeStoredDraft(key);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const raw = JSON.stringify({
|
|
|
|
|
text: draft.text,
|
|
|
|
|
quotedContext: draft.quotedContext,
|
|
|
|
|
sessionMentions: draft.sessionMentions,
|
2026-10-22 18:32:18 +08:00
|
|
|
attachments: draft.attachments.filter(isLocalAttachment),
|
2026-09-25 23:42:01 +08:00
|
|
|
});
|
|
|
|
|
if (raw.length <= MAX_STORED_CHARS) localStorage.setItem(STORAGE_PREFIX + key, raw);
|
|
|
|
|
else removeStoredDraft(key);
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep the in-memory draft, but do not restore an older saved version after refresh.
|
|
|
|
|
removeStoredDraft(key);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override delete(key: string): boolean {
|
|
|
|
|
removeStoredDraft(key);
|
|
|
|
|
return super.delete(key);
|
|
|
|
|
}
|
|
|
|
|
}
|