126 lines
3.8 KiB
Dart
126 lines
3.8 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) ??
|
|
(_command(p)
|
|
? 'Running a command'
|
|
: 'Running ${chatToolLabel(p)}');
|
|
}
|
|
final calls = tools;
|
|
if (calls.isEmpty) {
|
|
return records.isNotEmpty ? 'Activity details' : 'Reasoning';
|
|
}
|
|
final commands = calls.every(_command);
|
|
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);
|
|
}
|
|
|
|
bool _command(JsonMap tool) => const [
|
|
'bash',
|
|
'shell',
|
|
'command',
|
|
'exec',
|
|
'execute',
|
|
'run_command',
|
|
].contains(chatToolLabel(tool).toLowerCase());
|