From 4280881436553e00d000ac3bd739e1e0daeb53e0 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 22 Oct 2026 18:37:18 +0100 Subject: [PATCH] feat(app): notify on chat questions --- app/lib/application/pipelines_app.dart | 24 +++++ .../notify/chat_question_notices.dart | 73 +++++++++++++ .../attention/notify/notification_models.dart | 6 +- .../notify/chat_question_notices_test.dart | 101 ++++++++++++++++++ 4 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 app/lib/features/attention/notify/chat_question_notices.dart create mode 100644 app/test/features/attention/notify/chat_question_notices_test.dart diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 14ecb4f6..96dbe405 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -9,6 +9,7 @@ import '../core/transport/pipelines_client.dart'; import '../shell/adaptive_shell.dart'; import '../shell/destination.dart'; import '../features/attention/notify/notification_controller.dart'; +import '../features/attention/notify/chat_question_notices.dart'; import '../features/attention/notify/notification_store.dart'; import '../features/runs/ui/run_search_selection.dart'; import '../features/search/domain/search_target.dart'; @@ -88,6 +89,7 @@ class _PipelinesAppState extends State late final NotificationController _notifications = NotificationController( store: const SharedPreferencesNotificationStore(), ); + ChatQuestionNotices? _chatQuestionNotices; @override void initState() { @@ -126,6 +128,17 @@ class _PipelinesAppState extends State builder: _buildShell, ); _delegate.addListener(_routeChanged); + _chatQuestionNotices = ChatQuestionNotices( + gateway: _session.gateway, + canNotify: () => _notifications.canNotify, + activeChatSessionId: () { + final route = _delegate.shellRoute; + return route.kind == AppRouteKind.chat ? route.chatSessionId : null; + }, + sessionTitle: _chatSessionTitle, + openChat: (id) => _delegate.goTo(AppRoute.chatSession(id)), + bridge: _notifications.bridge, + ); _config = RouterConfig( routeInformationParser: const AppRouteParser(), routerDelegate: _delegate, @@ -138,6 +151,7 @@ class _PipelinesAppState extends State void dispose() { WidgetsBinding.instance.removeObserver(this); _notifications.dispose(); + _chatQuestionNotices?.dispose(); _delegate.removeListener(_routeChanged); _delegate.dispose(); if (_ownsProvider) { @@ -242,6 +256,16 @@ class _PipelinesAppState extends State setState(() => _pendingRunSearch = null); } + Future _chatSessionTitle(String id) async { + final reply = await _session.gateway.call('chat_sessions'); + final sessions = reply['chat_sessions']; + if (sessions is! List) return null; + for (final value in sessions) { + if (value is Map && value['id'] == id) return '${value['title'] ?? ''}'; + } + return null; + } + /// The Templates page above Settings. Closing it restores the same Settings /// screen instance, so an edited field or an open connection state survives. Widget _buildSchedules(BuildContext context) => SchedulesDestination( diff --git a/app/lib/features/attention/notify/chat_question_notices.dart b/app/lib/features/attention/notify/chat_question_notices.dart new file mode 100644 index 00000000..fc4b3fdb --- /dev/null +++ b/app/lib/features/attention/notify/chat_question_notices.dart @@ -0,0 +1,73 @@ +import 'dart:async'; + +import '../../../core/api_gateway.dart'; +import 'notification_bridge.dart'; +import 'notification_models.dart'; + +/// Delivers live unanswered chat questions through the shared notification +/// capability, without coupling chat event folding to platform notifications. +class ChatQuestionNotices { + ChatQuestionNotices({ + required ApiGateway gateway, + required this.canNotify, + required this.activeChatSessionId, + required this.sessionTitle, + required this.openChat, + required this.bridge, + }) { + _frames = gateway.frames.listen(_onEnvelope); + } + + final bool Function() canNotify; + final String? Function() activeChatSessionId; + final Future Function(String sessionId) sessionTitle; + final void Function(String sessionId) openChat; + final NotificationBridge bridge; + late final StreamSubscription _frames; + bool _disposed = false; + + void _onEnvelope(JsonMap envelope) { + if (envelope['type'] != 'chat_event') return; + final event = envelope['chat_event']; + if (event is! Map || event['kind'] != 'form') return; + final sessionId = event['session_id']; + final form = event['form']; + if (sessionId is! String || sessionId.isEmpty || + form is! Map || form['kind'] != 'question' || + form['resolved'] == true || form['cancelled'] == true || + form['id'] == null || !canNotify() || + activeChatSessionId() == sessionId) { + return; + } + unawaited(_show(sessionId, Map.from(form))); + } + + Future _show(String sessionId, JsonMap form) async { + String? session; + try { + session = await sessionTitle(sessionId); + } catch (_) { + // An unavailable session list is equivalent to an unknown session title. + } + if (_disposed || !canNotify() || activeChatSessionId() == sessionId) return; + final title = (session ?? '').trim(); + try { + bridge.show( + NotificationMessage( + chatSessionId: sessionId, + title: '${title.isEmpty ? 'Chat' : title} needs your answer', + body: '${form['title'] ?? 'The agent asked a question.'}', + tag: 'chat-question:${form['id']}', + ), + onTap: () => openChat(sessionId), + ); + } catch (_) { + // Notification delivery must not interfere with the in-page question. + } + } + + void dispose() { + _disposed = true; + unawaited(_frames.cancel()); + } +} diff --git a/app/lib/features/attention/notify/notification_models.dart b/app/lib/features/attention/notify/notification_models.dart index f817c92e..a3d5af33 100644 --- a/app/lib/features/attention/notify/notification_models.dart +++ b/app/lib/features/attention/notify/notification_models.dart @@ -156,13 +156,15 @@ String notificationAlertBody(AttentionEntry entry) { /// the stable identity used as the browser's coalescing tag. class NotificationMessage { const NotificationMessage({ - required this.runId, + this.runId, + this.chatSessionId, required this.title, required this.body, required this.tag, }); - final int runId; + final int? runId; + final String? chatSessionId; final String title; final String body; final String tag; diff --git a/app/test/features/attention/notify/chat_question_notices_test.dart b/app/test/features/attention/notify/chat_question_notices_test.dart new file mode 100644 index 00000000..6b0b1b87 --- /dev/null +++ b/app/test/features/attention/notify/chat_question_notices_test.dart @@ -0,0 +1,101 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/application/app_route.dart'; +import 'package:pipelines_app/features/attention/notify/chat_question_notices.dart'; + +import 'notification_test_support.dart'; + +class _Gateway implements ApiGateway { + final controller = StreamController.broadcast(); + @override + Stream get frames => controller.stream; + @override + Future call(String type, [JsonMap fields = const {}]) async => {}; +} + +Map _event({ + String id = 'form-1', + String session = 'session_1', + String kind = 'question', + bool resolved = false, + bool cancelled = false, +}) => { + 'type': 'chat_event', + 'chat_event': { + 'session_id': session, + 'kind': 'form', + 'form': { + 'id': id, + 'kind': kind, + 'title': 'Choose an option', + 'resolved': resolved, + 'cancelled': cancelled, + }, + }, +}; + +void main() { + late _Gateway gateway; + late FakeNotificationBridge bridge; + late bool allowed; + late String? active; + late List opened; + late ChatQuestionNotices notices; + + setUp(() { + gateway = _Gateway(); + bridge = FakeNotificationBridge(permission: 'granted'); + allowed = true; + active = null; + opened = []; + notices = ChatQuestionNotices( + gateway: gateway, + canNotify: () => allowed, + activeChatSessionId: () => active, + sessionTitle: (id) async => id == 'session_1' ? ' Design chat ' : null, + openChat: (id) => opened.add(AppRoute.chatSession(id)), + bridge: bridge, + ); + }); + + tearDown(() async { + notices.dispose(); + await gateway.controller.close(); + }); + + test('notifies titled and unknown sessions; tap opens the chat route', () async { + gateway.controller.add(_event()); + await Future.delayed(Duration.zero); + gateway.controller.add(_event(id: 'form-2', session: 'unknown')); + await Future.delayed(Duration.zero); + + expect(bridge.shown.map((message) => message.title), [ + 'Design chat needs your answer', + 'Chat needs your answer', + ]); + expect(bridge.shown.first.body, 'Choose an option'); + expect(bridge.shown.first.tag, 'chat-question:form-1'); + bridge.taps.first?.call(); + expect(opened, [AppRoute.chatSession('session_1')]); + }); + + test('suppresses active, unavailable, resolved, cancelled and non-question frames', () async { + active = 'session_1'; + gateway.controller.add(_event()); + await Future.delayed(Duration.zero); + active = null; + allowed = false; + gateway.controller.add(_event(id: 'form-2')); + await Future.delayed(Duration.zero); + allowed = true; + gateway.controller.add(_event(id: 'form-3', resolved: true)); + await Future.delayed(Duration.zero); + gateway.controller.add(_event(id: 'form-4', cancelled: true)); + await Future.delayed(Duration.zero); + gateway.controller.add(_event(id: 'form-5', kind: 'approval')); + await Future.delayed(Duration.zero); + expect(bridge.shown, isEmpty); + }); +}