merge(app): land flutter/chat-question-notice
This commit is contained in:
@@ -10,6 +10,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/attention/runtime/attention_controller.dart';
|
||||
import '../features/attention/runtime/attention_store.dart';
|
||||
@@ -91,6 +92,7 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
late final NotificationController _notifications = NotificationController(
|
||||
store: const SharedPreferencesNotificationStore(),
|
||||
);
|
||||
ChatQuestionNotices? _chatQuestionNotices;
|
||||
|
||||
/// One attention surface for the app: the Runs heading's bell draws from it
|
||||
/// and the shell mirrors its count on the Runs tab, so the badge is visible
|
||||
@@ -149,6 +151,17 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
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<AppRoute>(
|
||||
routeInformationParser: const AppRouteParser(),
|
||||
routerDelegate: _delegate,
|
||||
@@ -166,6 +179,7 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
_attention.dispose();
|
||||
_attentionCount.dispose();
|
||||
_notifications.dispose();
|
||||
_chatQuestionNotices?.dispose();
|
||||
_delegate.removeListener(_routeChanged);
|
||||
_delegate.dispose();
|
||||
if (_ownsProvider) {
|
||||
@@ -297,6 +311,16 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
setState(() => _pendingRunSearch = null);
|
||||
}
|
||||
|
||||
Future<String?> _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(
|
||||
|
||||
@@ -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<String?> Function(String sessionId) sessionTitle;
|
||||
final void Function(String sessionId) openChat;
|
||||
final NotificationBridge bridge;
|
||||
late final StreamSubscription<JsonMap> _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<String, dynamic>.from(form)));
|
||||
}
|
||||
|
||||
Future<void> _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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<JsonMap>.broadcast();
|
||||
@override
|
||||
Stream<JsonMap> get frames => controller.stream;
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {};
|
||||
}
|
||||
|
||||
Map<String, dynamic> _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<AppRoute> 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<void>.delayed(Duration.zero);
|
||||
gateway.controller.add(_event(id: 'form-2', session: 'unknown'));
|
||||
await Future<void>.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<void>.delayed(Duration.zero);
|
||||
active = null;
|
||||
allowed = false;
|
||||
gateway.controller.add(_event(id: 'form-2'));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
allowed = true;
|
||||
gateway.controller.add(_event(id: 'form-3', resolved: true));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
gateway.controller.add(_event(id: 'form-4', cancelled: true));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
gateway.controller.add(_event(id: 'form-5', kind: 'approval'));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(bridge.shown, isEmpty);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user