* feat(attachments): support local files and video attachments Share gateway inspection and send validation for borrowed local paths while preserving binary uploads, original-file ownership, and existing limits. Related to NAN-284. Native provider video input remains outside this change. * test: make cross-platform verification fixtures self-contained * test(webui): align typed fixtures with runtime contracts * fix(webui): preserve uploaded video playback during history sync * test(tui): preserve local attachment drafts across resizes * perf(attachments): preserve upload caches and release video previews * docs(webui): keep attachment guidance user-focused * chore: remove redundant dependency comment * test(providers): make stream idle renewal timing deterministic * fix(attachments): keep upload byte limits out of local references Validate common format/count rules separately from upload byte budgets. Keep local inspection and send-time checks metadata-only, and exclude local references from composer and pending-upload totals. Explain upload size rejections with actual size and maximum in all ten locales. Related to NAN-284. * fix(attachments): retain image references and draft order * fix(attachments): close local reference UX and authorization gaps * fix(attachments): preserve focus during asynchronous path confirmation
107 lines
4.3 KiB
TypeScript
107 lines
4.3 KiB
TypeScript
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);
|
|
}
|
|
}
|