/// 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. /// /// An agent's notification is a system (OS) notification whenever the /// browser or OS allows one, whatever app is in front; the only exception is /// the operator already looking at that chat in a focused page. There is no /// in-app toast or sound. Without permission (not asked yet, denied, or an /// insecure http page) only the tab title counts it until the page is focused. AgentNoticeDelivery agentNoticeDelivery({ required AgentNotice notice, required PagePresence presence, required bool chatInView, required bool canNotify, bool replay = false, }) { final looking = presence == PagePresence.focused && chatInView; if (looking) return const AgentNoticeDelivery(); if (canNotify) return const AgentNoticeDelivery(system: true); return AgentNoticeDelivery(badge: presence != PagePresence.focused); }