/// An agent's own notification: the `chat_notify` app notification the server /// records when a chat's model calls the `notify` tool, as the notification /// stream carries it (see docs/ntfy.md). library; /// The stream kind of an agent's notify call. const agentNoticeKind = 'chat_notify'; /// How far back a replayed notice is still worth showing. Older ones were /// missed long enough ago that the chat itself is the better place to read /// them; they are recorded as seen without being shown. const agentNoticeReplayWindow = Duration(hours: 1); /// The most replayed notices shown at once after a reconnect. const agentNoticeReplayLimit = 5; class AgentNotice { const AgentNotice({ required this.id, required this.sessionId, required this.title, required this.message, required this.priority, required this.createdAt, }); /// The server's stable id, `chat::notify:`. final String id; final String sessionId; final String title; final String message; /// The ntfy-style priority: 2 low, 3 default, 4 high. final int priority; final DateTime? createdAt; bool get low => priority < 3; /// The notice in [value], or null when it is not a well-formed agent notice. static AgentNotice? parse(Object? value) { if (value is! Map || value['kind'] != agentNoticeKind) return null; final id = value['id']; if (id is! String || id.isEmpty) return null; final session = _sessionOf(id, value['group']); if (session == null) return null; final priority = value['priority']; final created = value['created_at']; return AgentNotice( id: id, sessionId: session, title: '${value['title'] ?? ''}'.trim(), message: '${value['message'] ?? ''}'.trim(), priority: priority is int ? priority : 3, createdAt: created is String ? DateTime.tryParse(created) : null, ); } /// The chat id: the group `chat:`, else the id's `chat::notify:` head. static String? _sessionOf(String id, Object? group) { if (group is String && group.startsWith('chat:') && group.length > 5) { return group.substring(5); } final match = RegExp(r'^chat:(.+):notify:[^:]+$').firstMatch(id); return match?.group(1); } /// The headline: `: `, with "Chat" for an unknown chat. String headline(String? chatTitle) { final chat = (chatTitle ?? '').trim(); final name = chat.isEmpty ? 'Chat' : chat; return title.isEmpty ? name : '$name: $title'; } } /// Whether the operator is looking at the app right now. enum PagePresence { /// Visible and focused: the operator is in the app. focused, /// Visible but another window has focus. visible, /// Hidden: another tab, minimized, or the screen is off. hidden, } /// How one notice reaches the operator. class AgentNoticeDelivery { const AgentNoticeDelivery({ this.system = false, this.toast = false, this.badge = false, this.chime = false, }); /// A system (browser or OS) notification. final bool system; /// An in-app toast with an Open action. final bool toast; /// A count in the tab title until the page is focused again. final bool badge; /// A short sound, only when nothing the operator would notice is posted. final bool chime; bool get none => !system && !toast && !badge && !chime; @override String toString() => 'AgentNoticeDelivery(system: $system, toast: $toast, badge: $badge, ' 'chime: $chime)'; } /// Decides how [notice] is delivered. /// /// A focused page shows a toast, or nothing when the notice's chat is already /// in view. A page the operator is not looking at gets a system notification /// when [canNotify] (permission granted) and a count in the tab title; a low /// priority notice only raises a system notification when the page is hidden, /// and otherwise waits as a toast. Without system notifications (denied, not /// asked, or an insecure http page) the toast, the title count and, for /// default and high priority, a short chime carry it. A [replay] (missed while /// disconnected) never chimes. AgentNoticeDelivery agentNoticeDelivery({ required AgentNotice notice, required PagePresence presence, required bool chatInView, required bool canNotify, bool replay = false, }) { if (presence == PagePresence.focused) { return AgentNoticeDelivery(toast: !chatInView); } final system = canNotify && (!notice.low || presence == PagePresence.hidden); return AgentNoticeDelivery( system: system, toast: !system, badge: true, chime: !system && !notice.low && !replay, ); }