Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import { untrack } from 'svelte';import { listSessions, type Session } from './api';import { filterKey, overriding, sameFilter, type SessionFilter,} from './session-filter';import { sessions } from './sessions.svelte';
const PAGE = 50;const SETTLE_MS = 150;
/** * The sessions a query's filter tokens ask for. The shared list already holds * the default filter's sessions, so it serves a query that names no other; * otherwise the daemon is asked, a page at a time. Make one in a component's * setup, with the filter its query holds. */export class SessionSearch { #found = $state.raw<Session[]>([]); #next = $state<string | null>(null); #settling = $state(false); #fetching = $state(false); #typed: () => SessionFilter; #wanted = $derived.by(() => { const filter = overriding(sessions.defaults, this.#typed()); return sameFilter(filter, sessions.defaults) ? null : filter; }); #key = $derived(filterKey(this.#wanted)); #inflight: AbortController | undefined; /** Whether the daemon holds matches beyond those shown. */ more = $derived(this.#next !== null); loading = $derived(this.#settling || this.#fetching); items = $derived(this.#wanted ? this.#found : sessions.list);
constructor(typed: () => SessionFilter) { this.#typed = typed; let shown = ''; $effect(() => { const key = this.#key; const filter = untrack(() => this.#wanted); void sessions.revision; if (key !== shown) { shown = key; this.#found = []; this.#next = null; } if (!filter) { this.#settling = false; this.#fetching = false; return; } this.#settling = true; const timer = setTimeout(() => { this.#settling = false; untrack(() => void this.#read(filter, null)); }, SETTLE_MS); return () => { clearTimeout(timer); this.#inflight?.abort(); this.#settling = false; }; }); }
/** Shows the next page of matches. */ async load() { if (this.#wanted && this.#next && !this.loading) await this.#read(this.#wanted, this.#next); }
async #read(filter: SessionFilter, after: string | null) { const controller = (this.#inflight = new AbortController()); this.#fetching = true; try { const page = await listSessions(controller.signal, { filter, limit: after ? PAGE : Math.min(200, Math.max(PAGE, this.#found.length)), next: after, }); if (controller.signal.aborted) return; this.#found = after ? [...this.#found, ...page.items] : page.items; this.#next = page.next; } catch { if (controller.signal.aborted) return; if (!after) { this.#found = []; this.#next = null; } } finally { if (this.#inflight === controller) this.#fetching = false; } }}