Files
pipelines/app/lib/features/attention/notify/notification_models.dart
T
alan 767643d991 Open the run's artifact from notifications
Phone and browser notifications that carry an artifact open it full
screen in the artifact viewer, with an Open run action and the preview as
expanded text; pipelines://artifact deep links route to the viewer.
2026-11-12 18:56:26 +00:00

254 lines
8.6 KiB
Dart

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 compact row's visible action; [notificationRequestLabel] is its name.
const notificationEnableShortLabel = 'Enable';
/// 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.headline,
required this.helper,
required this.canRequest,
required this.actionLabel,
required this.asked,
});
final NotificationPermissionState state;
/// The browser's full sentence for the state (read out with the row).
final String body;
/// The compact Settings row: a short state line and one short helper.
final String headline;
final String helper;
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,
};
final (headline, helper) = switch (state) {
NotificationPermissionState.unsupported => (
'Not supported in this browser',
'Runs that need you still appear in Needs you.',
),
NotificationPermissionState.needsSecureContext => (
'Needs a secure page',
'Open the app over https or localhost to turn them on.',
),
NotificationPermissionState.granted => (
'Alerts are on',
'Failed and waiting runs raise a notification.',
),
NotificationPermissionState.denied => (
'Blocked by the browser',
'Allow them in the site settings; Needs you still works.',
),
NotificationPermissionState.notRequested =>
asked
? (
'Alerts are still off',
'Permission was not granted. Ask again, or use Needs you.',
)
: (
'Alerts are off',
'Get told when a run fails or needs your decision.',
),
};
return NotificationPermissionView(
state: state,
body: body,
headline: headline,
helper: helper,
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,
this.artifact,
this.preview,
this.runClick,
});
final int? runId;
final String? chatSessionId;
final String title;
final String body;
final String tag;
final NotificationArtifact? artifact;
final String? preview;
final String? runClick;
}
/// The server-provided artifact attached to a notification.
class NotificationArtifact {
const NotificationArtifact({
required this.runId,
required this.sequence,
required this.path,
required this.name,
required this.url,
required this.mediaType,
});
final int runId;
final int sequence;
final String path;
final String name;
final String url;
final String mediaType;
factory NotificationArtifact.fromJson(dynamic value) {
final map = value is Map ? value : const <String, dynamic>{};
String text(String key) => '${map[key] ?? ''}';
int number(String key) => int.tryParse(text(key)) ?? 0;
return NotificationArtifact(
runId: number('run_id'),
sequence: number('sequence'),
path: text('path'),
name: text('name'),
url: text('url'),
mediaType: text('media_type'),
);
}
bool get isValid => runId > 0 && sequence >= 0 && path.isNotEmpty;
}