Files
pipelines/app/lib/features/chat/domain/chat_turn_activity.dart
T
alan 22866ec0ce Chat: fold activity-only rows between messages into one summary line
Tool-only turns, thinking-only turns and background-task notices that sit
between two content rows become one collapsible activity_group row, e.g.
"Ran 7 commands · used 9 tools · 8 background tasks finished · 1 failed",
the failure count in the error colour and never ellipsized. Expanding it
shows the folded rows as they render on their own. A run's task notices
fold into one chip at the newest notice; a notice whose id recurs later is
counted only at its latest position.

Grouping is a pure domain step (groupChatActivity) applied last in the
thread view's row pipeline; group rows list every member id so ChatRowKeys
keeps their identity, unchanged runs return the identical group object, and
the transcript anchor accepts a group's agent-turn ids. The scroll fixture
gains ?bgwork=1 for the shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 10:58:25 +00:00

127 lines
3.9 KiB
Dart

import '../../../core/api_gateway.dart';
import 'chat_tool_calls.dart';
import 'chat_transcript.dart';
/// Presentation facts only: never infer a command or successful outcome from
/// answer prose, and never treat a still-running record as a completed command.
class ChatTurnActivityData {
ChatTurnActivityData(this.parts, {required this.streaming});
final List<JsonMap> parts;
final bool streaming;
List<JsonMap> get tools => parts.where(isChatToolPart).toList();
List<String> get reasoning => [
for (final p in parts)
if (p['type'] == 'reasoning' && '${p['text'] ?? ''}'.trim().isNotEmpty)
'${p['text']}',
];
List<JsonMap> get records => parts.where(chatActivityType).toList();
bool get hasDetails =>
tools.isNotEmpty || reasoning.isNotEmpty || records.isNotEmpty;
JsonMap? get active {
for (final tool in tools.reversed) {
if (chatToolRunning(tool)) return tool;
}
if (records.isNotEmpty) {
// Sequence updates can be distinct records; a completed newest record
// must not resurrect an older running phase.
final record = records.last;
if ([
'running',
'streaming',
'pending',
].contains(chatActivityState(record))) {
return record;
}
}
return null;
}
String get identity {
final p = active;
return p == null
? 'thinking'
: '${p['type']}:${p['id'] ?? p['name'] ?? p['kind'] ?? ''}';
}
DateTime? get started {
final p = active;
if (p == null) return null;
final state = p['state'];
final time = p['time'] ?? (state is Map ? state['time'] : null);
if (time is! Map) return null;
final value = time['ran'] ?? time['started'] ?? time['created'];
if (value is num && value > 0) {
return DateTime.fromMillisecondsSinceEpoch(value.toInt());
}
return value is String ? DateTime.tryParse(value) : null;
}
String get label {
if (streaming) {
final p = active;
if (p == null) {
for (final part in parts.reversed) {
if (part['type'] == 'text' &&
'${part['text'] ?? ''}'.trim().isNotEmpty) {
return 'Responding';
}
if (part['type'] == 'reasoning') return 'Thinking';
}
return 'Thinking';
}
if (!isChatToolPart(p)) {
return chatActivityLabel(p).replaceAll('…', '').trim();
}
final state = p['state'];
final title = state is Map ? '${state['title'] ?? ''}'.trim() : '';
return title.isNotEmpty
? title
: chatToolDetail(p) ??
(chatToolIsCommand(p)
? 'Running a command'
: 'Running ${chatToolLabel(p)}');
}
final calls = tools;
if (calls.isEmpty) {
return records.isNotEmpty ? 'Activity details' : 'Reasoning';
}
final commands = calls.every(chatToolIsCommand);
final failed = calls.where(chatToolFailed).length;
final unfinished = calls.any((p) {
final state = p['state'];
return chatToolRunning(p) ||
(state is Map &&
[
'stopped',
'interrupted',
'canceled',
'cancelled',
'pending',
].contains(state['status']));
});
final noun = commands
? (calls.length == 1 ? 'command' : 'commands')
: (calls.length == 1 ? 'tool' : 'tools');
final summary =
'${unfinished
? 'Stopped after'
: commands
? 'Ran'
: 'Used'} ${calls.length} $noun';
return failed == 0 ? summary : '$summary · $failed failed';
}
bool get failed => tools.any(chatToolFailed);
}
/// Whether [tool] is a shell command (`Ran N commands`), not another tool.
bool chatToolIsCommand(JsonMap tool) => const [
'bash',
'shell',
'command',
'exec',
'execute',
'run_command',
].contains(chatToolLabel(tool).toLowerCase());