Files
pipelines/app/lib/features/attention/notify/agent_notice.dart
T
alan 355a5767fb Agent notifications are system notifications, never in-app toasts
An agent's notify call now always posts an OS notification when permitted,
whatever app is in front (only skipped while that chat is open in the
focused page); no toast or chime. Without permission the tab title counts
it. The browser's permission prompt is shown on the operator's first click.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 16:32:29 +00:00

135 lines
4.3 KiB
Dart

/// 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:<session>:notify:<id>`.
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:<id>`, else the id's `chat:<id>: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: `<chat title>: <title>`, 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);
}