import '../domain/attention_models.dart'; /// The permission state the notification surface reports. /// /// The Dart names are this client's; [wireName] is the browser model's own word /// (`unsupported` | `insecure` | `default` | `granted` | `denied`) so the two /// clients' vocabularies stay traceable. [notRequested] is the Notification /// API's `default`: supported and secure, but the operator has not decided yet. enum NotificationPermissionState { unsupported('unsupported'), needsSecureContext('insecure'), notRequested('default'), granted('granted'), denied('denied'); const NotificationPermissionState(this.wireName); /// The browser model's own word for this state (`web/src/notify.js`). final String wireName; } /// The platform's own notification facts, as the bridge answers them. /// /// [permission] is the Notification API's raw word; anything unrecognised is /// read as `default` rather than guessed, exactly as the browser does. class NotificationEnvironment { const NotificationEnvironment({ required this.supported, required this.secureContext, required this.permission, }); final bool supported; final bool secureContext; final String permission; } /// The permission state for one environment. /// /// Only the platform's own answers decide it: an unsupported API and an /// insecure page are reported before the permission word, and `granted` is never /// assumed — a page that has not been granted stays [notRequested]. NotificationPermissionState notificationStateOf( NotificationEnvironment environment, ) { if (!environment.supported) return NotificationPermissionState.unsupported; if (!environment.secureContext) { return NotificationPermissionState.needsSecureContext; } switch (environment.permission) { case 'granted': return NotificationPermissionState.granted; case 'denied': return NotificationPermissionState.denied; default: return NotificationPermissionState.notRequested; } } /// Android's NOTIFICATION_RATIONALE, verbatim (`RunAlerts.kt`, `notify.js`). const notificationRationale = 'Pipelines can notify you when a run is waiting for your decision or has ' 'failed. Without it, those runs still appear in the in-app Needs you ' 'surface.'; /// The Settings section heading and the explicit-gesture action label. const notificationSectionTitle = 'Notifications'; const notificationRequestLabel = 'Enable notifications'; /// The honest wording for every state, verbatim from `web/src/notify.js`; each /// says plainly that the in-app `Needs you` surface is the only channel when the /// platform cannot notify. const notificationUnsupportedCopy = 'This browser does not support notifications. The in-app Needs you surface ' 'is the only channel.'; const notificationInsecureCopy = 'Notifications need a secure context (https or localhost). This page is not ' 'secure, so the in-app Needs you surface is the only channel.'; const notificationGrantedCopy = 'Notifications are on. Failed and waiting runs will raise a browser ' 'notification.'; const notificationDeniedCopy = 'Notifications are blocked for this site. Allow them in the browser\'s site ' 'settings to use them; until then the in-app Needs you surface is the only ' 'channel.'; const notificationDefaultCopy = 'Notifications are off. Enable them to be told when a run fails or needs ' 'your decision.'; const notificationDefaultAskedCopy = 'Notifications are still off. Your browser did not grant permission when ' 'asked; you can ask again, or the in-app Needs you surface remains the only ' 'channel.'; /// The total permission view for the Settings tile. /// /// Only a supported, secure page whose permission is still `default` offers the /// request control, and [asked] only changes the honest wording — nothing here /// asks for permission on its own. class NotificationPermissionView { const NotificationPermissionView({ required this.state, required this.body, required this.canRequest, required this.actionLabel, required this.asked, }); final NotificationPermissionState state; final String body; final bool canRequest; final String? actionLabel; final bool asked; } /// The view for one state, with the browser's own copy for it. NotificationPermissionView notificationPermissionView( NotificationPermissionState state, { bool asked = false, }) { final canRequest = state == NotificationPermissionState.notRequested; final body = switch (state) { NotificationPermissionState.unsupported => notificationUnsupportedCopy, NotificationPermissionState.needsSecureContext => notificationInsecureCopy, NotificationPermissionState.granted => notificationGrantedCopy, NotificationPermissionState.denied => notificationDeniedCopy, NotificationPermissionState.notRequested => asked ? notificationDefaultAskedCopy : notificationDefaultCopy, }; return NotificationPermissionView( state: state, body: body, canRequest: canRequest, actionLabel: canRequest ? notificationRequestLabel : null, asked: asked, ); } /// The short headline one kind shows; the browser's `alertTitle` verbatim. String notificationAlertTitle(AttentionKind kind) => switch (kind) { AttentionKind.decision => 'A run needs your decision', AttentionKind.failed => 'A run failed', }; /// The line naming the run: its label, then the pipeline it belongs to. /// /// The words are the entry's own — a blank or equal pipeline is never repeated, /// so there is no trailing separator and nothing is invented. String notificationAlertBody(AttentionEntry entry) { final pipeline = entry.pipeline; final label = entry.label; if (pipeline.isEmpty || pipeline == label) return label; return '$label \u00b7 $pipeline'; } /// One notification the platform is asked to show: the entry's own words, plus /// the stable identity used as the browser's coalescing tag. class NotificationMessage { const NotificationMessage({ this.runId, this.chatSessionId, required this.title, required this.body, required this.tag, }); final int? runId; final String? chatSessionId; final String title; final String body; final String tag; }