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 parts; final bool streaming; List get tools => parts.where(isChatToolPart).toList(); List get reasoning => [ for (final p in parts) if (p['type'] == 'reasoning' && '${p['text'] ?? ''}'.trim().isNotEmpty) '${p['text']}', ]; List 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'); // A question still open is the agent waiting on the operator, not a // stopped turn: say so instead of "Stopped after". final asking = calls.any( (p) => chatToolIsQuestion(p) && !chatToolSettled(p), ); final summary = '${unfinished && !asking ? 'Stopped after' : commands ? 'Ran' : 'Used'} ${calls.length} $noun'; final status = asking ? '$summary · waiting for your answer' : summary; return failed == 0 ? status : '$status · $failed failed'; } bool get failed => tools.any(chatToolFailed); } /// Whether [tool] asks the operator a question (Claude Code's /// AskUserQuestion, or a harness tool named like it). bool chatToolIsQuestion(JsonMap tool) { final name = chatToolLabel(tool).toLowerCase().replaceAll('_', ''); return name == 'askuserquestion' || name == 'question'; } /// Whether [tool] has a final state (completed, failed or answered). bool chatToolSettled(JsonMap tool) { final state = tool['state']; final status = state is Map ? '${state['status']}' : '$state'; return const ['completed', 'error', 'failed', 'done'].contains(status); } /// 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());