Skip to content

Your lightweight open-source personal agent.

4.3 KB
Raw Blame
import type { SessionMention } from "./types";
import { isLocalAttachment, type LocalAttachment } from "../../../packages/client-events/attachments";

/** Ordered attachments retain browser Files in memory and persist local references. */
export interface ComposerDraft {
  text: string;
  attachments: Array<File | LocalAttachment>;
  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 } : {}),
    }];
  });
  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 };
}

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
      && previous.attachments.length === draft.attachments.length
      && previous.attachments.every((attachment, index) => attachment === draft.attachments[index])
      && 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;
    if (!draft.text && !draft.quotedContext && !draft.attachments.some(isLocalAttachment)) {
      removeStoredDraft(key);
      return this;
    }
    try {
      const raw = JSON.stringify({
        text: draft.text,
        quotedContext: draft.quotedContext,
        sessionMentions: draft.sessionMentions,
        attachments: draft.attachments.filter(isLocalAttachment),
      });
      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);
  }
}