Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213import { SvelteMap } from 'svelte/reactivity';import { attachmentProblem, attachmentSelection, remapMarkers, withoutMarkers, type Attached, type AttachedFile,} from './attachments';
// What was typed in a chat's composer and not yet sent. The composer is// destroyed with its window when another board is shown or the page goes, so// a draft lives here, by session, and in localStorage. Only the text is kept// there: images and files are too big for it, so a reload drops them and the// image markers. Text a person typed, `[File #n: name]` included, stays.
const KEY = 'albedo.draft.v1:';/** A draft untouched this long is not worth keeping; the session may be gone. */const STALE_MS = 30 * 24 * 60 * 60 * 1000;const SAVE_AFTER_MS = 300;
function read(session: string): string { try { const saved = JSON.parse(localStorage.getItem(KEY + session) ?? ''); return typeof saved?.text === 'string' ? withoutMarkers(saved.text) : ''; } catch { return ''; }}
/** Drops the saved drafts nobody came back to. */function prune() { try { for (const key of Object.keys(localStorage).filter((name) => name.startsWith(KEY), )) { const saved = JSON.parse(localStorage.getItem(key) ?? 'null'); if (!(Date.now() - saved?.at < STALE_MS)) localStorage.removeItem(key); } } catch {}}
const unsaved = new Set<Draft>();let timer: ReturnType<typeof setTimeout> | undefined;
function flush() { clearTimeout(timer); timer = undefined; for (const draft of unsaved) draft.save(); unsaved.clear();}
export class Draft { readonly images = new SvelteMap<number, Attached>(); readonly files = new SvelteMap<number, AttachedFile>(); /** The last draft attachment identity handed out. */ next = 0; #text: string; #session: string; #notice = ''; #reading = 0; #additions = Promise.resolve(); #listeners = new Set<() => void>();
constructor(session: string) { this.#session = session; this.#text = read(session); }
get text(): string { return this.#text; }
/** Setting the text saves it a moment later, once typing pauses. */ set text(value: string) { if (value === this.#text) return; this.#text = value; unsaved.add(this); timer ??= setTimeout(flush, SAVE_AFTER_MS); this.#notify(); }
get notice(): string { return this.#notice; } set notice(value: string) { if (value === this.#notice) return; this.#notice = value; this.#notify(); }
get reading(): number { return this.#reading; }
/** A replacement composer sees reads and edits completed by its predecessor. */ subscribe(listener: () => void): () => void { this.#listeners.add(listener); listener(); return () => { this.#listeners.delete(listener); }; }
/** What the draft holds now, to bring back with `load`. */ snapshot(): DraftSnapshot { return { text: this.#text, images: new Map(this.images), files: new Map(this.files), next: this.next, }; }
/** Replaces the draft with a snapshot, its attachment numbers included. */ load(snapshot: DraftSnapshot) { this.images.clear(); this.files.clear(); for (const [n, image] of snapshot.images) this.images.set(n, image); for (const [n, file] of snapshot.files) this.files.set(n, file); this.next = Math.max(this.next, snapshot.next); this.text = snapshot.text; this.#notify(); this.show?.(snapshot.text); }
/** Reads belong to the draft, so leaving a board cannot enable Send early. */ readAttachments(read: () => Promise<void>): Promise<void> { this.#reading++; this.#notify(); this.#additions = this.#additions.then(read, read).finally(() => { this.#reading--; this.#notify(); }); return this.#additions; }
#notify() { for (const listener of this.#listeners) listener(); }
/** Why the attachments cannot go as one message, or null; restored overflow keeps its bytes. */ get problem(): string | null { return attachmentProblem( attachmentSelection(this.#text, this.images, this.files), ); }
/** Shows the draft in an upstream composer; subscribers also survive replacements. */ show: ((text: string) => void) | null = null;
/** Focuses the editor while its composer is mounted. */ focus: (() => void) | null = null; focusRequested = false;
/** * Appends cancelled messages after what is typed, each a paragraph, their * images renumbered after the draft's and their files added as cards. Every * byte is kept even past one message's limits; `problem` then holds Send. */ restore( sends: { text: string; images: Attached[]; files?: AttachedFile[] }[], ) { const parts = this.#text.trim() ? [this.#text.trimEnd()] : []; for (const send of sends) { const numbers = new Map<number, number>(); send.images.forEach((image, index) => { numbers.set(index + 1, ++this.next); this.images.set(this.next, image); }); for (const file of send.files ?? []) this.files.set(++this.next, file); const text = remapMarkers(send.text, numbers); if (text.trim()) parts.push(text); } this.text = parts.join('\n\n'); this.#notify(); this.show?.(this.#text); }
save() { try { const text = withoutMarkers(this.#text); if (text.trim()) localStorage.setItem( KEY + this.#session, JSON.stringify({ text, at: Date.now() }), ); else localStorage.removeItem(KEY + this.#session); } catch {} }}
export interface DraftSnapshot { text: string; images: Map<number, Attached>; files: Map<number, AttachedFile>; next: number;}
const drafts = new Map<string, Draft>();
export function draftOf(session: string): Draft { let draft = drafts.get(session); if (!draft) drafts.set(session, (draft = new Draft(session))); return draft;}
if (typeof window !== 'undefined') { prune(); // The last words typed before a reload or a closed tab are still waiting on the timer. window.addEventListener('pagehide', flush);}