Files
pipelines/app/lib/features/chat/ui/sessions/session_attention.dart
T
alan c320dac131 Chat list: tool calls and notices never mark a chat New
Only visible assistant reply text, a pending form or an unresolved
permission request is unread. Tool results, user prompts, harness
notices/agent reports and blank deltas only drive the working state.
2026-11-03 18:34:29 +00:00

75 lines
2.6 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 the agent said something to read or is waiting on the
// operator: visible assistant reply text or a pending form. Tool calls and
// their results, harness notices and agent reports (`message_kind`), the
// operator's own prompts from any device, reasoning, steps, shells,
// compaction and usage are turn activity, never unread.
if (kind == 'text') {
attention =
'${event['delta'] ?? ''}'.trim().isNotEmpty &&
'${event['message_kind'] ?? ''}'.isEmpty;
} else if (kind == 'tool') {
final part = event['part'];
final state = part is Map ? part['state'] : null;
if (state is Map) {
final status = state['status'];
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);
}