Files
pipelines/app/lib/features/chat/ui/sessions/session_attention.dart
T

78 lines
2.7 KiB
Dart

import '../../../../core/api_gateway.dart';
typedef ChatSessionFrameEffect = ({
String sessionId,
bool attention,
bool? running,
});
/// Interprets only authoritative content/action and activity fields from a
/// pushed frame. Recency and unrelated metadata are deliberately not signals.
ChatSessionFrameEffect? chatSessionFrameEffect(JsonMap frame) {
final type = frame['type'];
String? id;
var attention = false;
bool? running;
if (type == 'chat_event') {
final event = frame['chat_event'];
if (event is! Map) return null;
id = event['session_id'] is String ? event['session_id'] as String : null;
final kind = event['kind'];
// A badge means content to read, not transport/activity churn: assistant
// text, another client's user message, a returned tool result, or a pending
// form. Reasoning, starts, steps, shells, compaction and usage are not unread.
if (kind == 'text') {
attention = '${event['delta'] ?? ''}'.isNotEmpty;
} else if (kind == 'user') {
attention = true;
} else if (kind == 'tool') {
final part = event['part'];
final state = part is Map ? part['state'] : null;
if (state is Map) {
final status = state['status'];
attention =
(status == 'completed' || status == 'error') &&
((state['content'] is List &&
(state['content'] as List).isNotEmpty) ||
state['error'] != null);
if (status == 'running' || status == 'streaming') running = true;
}
} else if (kind == 'form') {
final form = event['form'];
attention =
form is Map &&
form['resolved'] != true &&
form['cancelled'] != true &&
'${form['status'] ?? ''}' != 'resolved' &&
'${form['status'] ?? ''}' != 'cancelled';
} else if (kind == 'step') {
final step = event['step'];
if (step is Map) {
if ([
'started',
'streamed',
'running',
'streaming',
].contains(step['status'])) {
running = true;
} else if (step['status'] == 'ended' && step['intermediate'] != true) {
running = false;
}
}
}
} else if (type == 'chat_permission') {
final permission = frame['chat_permission'];
if (permission is! Map) return null;
id = permission['session_id'] is String
? permission['session_id'] as String
: null;
attention =
permission['resolved'] != true &&
'${permission['decision'] ?? ''}'.trim().isEmpty;
} else {
return null;
}
if (id == null || id.isEmpty) return null;
return (sessionId: id, attention: attention, running: running);
}