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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import { untrack } from 'svelte';import { SvelteSet } from 'svelte/reactivity';import type { Session } from './api';import { Attention, type AlertKind, type Mail } from './attention';import { boards } from './boards.svelte';import { layout } from './layout.svelte';import { paneTrail } from './tiles/tree';
const KEY = 'albedo.notifications';
class Notifications { enabled = $state(localStorage.getItem(KEY) === 'on'); supported = typeof Notification !== 'undefined'; permission = $state<NotificationPermission>( this.supported ? Notification.permission : 'denied', ); requesting = $state(false); error = $state(''); unread = new SvelteSet<string>(); flashing = new SvelteSet<string>(); #focused = $state(!document.hidden && document.hasFocus()); #attention = new Attention(this.unread, this.flashing); #open = new Map<string, Notification>(); #title = document.title; #count = -1; #icon = document.querySelector<HTMLLinkElement>('link[rel="icon"]'); #originalIcon = this.#icon?.href ?? '';
start() { const focus = () => { this.#focused = !document.hidden && document.hasFocus(); if (this.supported) this.permission = Notification.permission; }; window.addEventListener('focus', focus); window.addEventListener('blur', focus); document.addEventListener('visibilitychange', focus); $effect.root(() => { $effect(() => { const id = this.#reading(); if (id) untrack(() => this.read(id)); }); }); }
#quiet(id: string): boolean { if (document.hidden) return false; if (layout.phone) return layout.session === id; if (layout.focus) return layout.focus === id; return !!(boards.active && boards.sessionIds(boards.active).includes(id)); }
#reading(): string | null { if (!this.#focused) return null; if (layout.phone) return layout.session; if (layout.focus) return layout.focus; const board = boards.board; const id = board?.selected; const window = id ? board?.windows[id] : undefined; if (!board || !id || window?.type !== 'session') return null; const path = paneTrail(board.root, id); if ( !path || path.some( (tile, at) => tile.kind === 'tabs' && tile.active !== path[at + 1]?.id, ) || (board.expanded && !path.some( (tile) => tile.id === board.expanded || (tile.kind === 'leaf' && tile.window === board.expanded), )) ) return null; return window.data.sessionId; }
read(id: string) { this.#attention.read(id); this.#open.get(id)?.close(); this.#open.delete(id); this.#render(); }
async toggle() { this.error = ''; if (this.enabled && this.permission === 'granted') { this.enabled = false; for (const notification of this.#open.values()) notification.close(); this.#open.clear(); } else if (this.supported) { this.requesting = true; try { this.permission = await Notification.requestPermission(); this.enabled = this.permission === 'granted'; } catch { this.error = 'the browser could not enable notifications. try again here.'; } finally { this.requesting = false; } } localStorage.setItem(KEY, this.enabled ? 'on' : 'off'); }
watched(session: Session) { return ( !session.preferences.archived && (session.preferences.pinned || boards.hasSession(session.id) || layout.focus === session.id || (layout.phone && layout.session === session.id)) ); }
changed(previous: Session | undefined, next: Session) { const kind = this.#attention.changed( previous, next, this.watched(next), this.#reading() !== next.id, this.#quiet(next.id), ); if (kind) this.#notify(next, kind); this.#render(); }
mail(mail: Mail, sessions: Session[]) { const session = sessions.find( (item) => item.id === mail.receiver_session_id, ); const kind = this.#attention.mail( mail, !!session && this.watched(session), this.#reading() !== mail.receiver_session_id, this.#quiet(mail.receiver_session_id), ); if (kind && session) this.#notify(session, kind); this.#render(); }
prune(sessions: Session[]) { this.#attention.prune( new Set( sessions .filter((session) => this.watched(session)) .map((session) => session.id), ), ); this.#render(); }
#render() { const count = this.unread.size; if (count === this.#count) return; this.#count = count; document.title = count ? `(${count}) ${this.#title}` : this.#title; if (this.#icon) this.#icon.href = count ? `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="6" fill="#e8e4dc" stroke="#333" stroke-width="1.5"/><circle cx="12" cy="4" r="3" fill="#a05886" stroke="#fff"/></svg>')}` : this.#originalIcon; }
#notify(session: Session, kind: AlertKind) { if ( !document.hidden || !this.enabled || !this.supported || Notification.permission !== 'granted' ) return; try { const body = { finished: 'finished a turn', attention: 'needs your attention', mail: 'received agent mail', }[kind]; const notification = new Notification(session.name || 'albedo', { body, tag: `albedo:${session.id}`, }); this.#open.set(session.id, notification); notification.onclose = () => this.#open.delete(session.id); notification.onclick = () => { window.focus(); boards.revealSession(session); notification.close(); }; } catch { this.error = 'this browser could not show a system notification. tab counts still work.'; } }}
export const notifications = new Notifications();