import 'dart:convert'; import '../../../core/api_gateway.dart'; /// Tool calls in an agent turn, ported from the web client's `src/chat.js` /// (`toolLabel`, `toolCallsLabel`, `toolDetail`, `toolCallRow`, ...): the turn /// shows one summary chip below its answer, and each call opens to its own /// formatted input and output instead of raw JSON. bool isChatToolPart(JsonMap part) => '${part['type'] ?? ''}' == 'tool'; JsonMap _state(JsonMap part) => part['state'] is Map ? JsonMap.from(part['state'] as Map) : const {}; /// A call's name, or `tool` when unnamed. String chatToolLabel(JsonMap part) { final name = '${part['name'] ?? ''}'.trim(); return name.isEmpty ? 'tool' : name; } /// The turn's chip: `1 tool` / `N tools`. String chatToolCallsLabel(int count) => count == 1 ? '1 tool' : '$count tools'; bool _runningStatus(Object? status) => '$status' == 'streaming' || '$status' == 'running'; /// Still in flight: `streaming` or `running`. bool chatToolRunning(JsonMap part) => isChatToolPart(part) && _runningStatus(_state(part)['status']); /// Ended in an error state. bool chatToolFailed(JsonMap part) => '${_state(part)['status'] ?? ''}' == 'error'; /// Wall-clock duration, or null without a completion time. int? chatToolDurationMillis(JsonMap part) { final time = part['time']; if (time is! Map) return null; num read(Object? v) => v is num ? v : (num.tryParse('$v') ?? 0); final ran = read(time['ran']); final start = ran > 0 ? ran : read(time['created']); final completed = read(time['completed']); if (start <= 0 || completed <= 0 || completed < start) return null; return (completed - start).toInt(); } /// `340ms` / `1.2s` / `2m 3s`. String formatChatToolDuration(int millis) { if (millis < 1000) return '${millis}ms'; if (millis < 60000) return '${(millis / 1000).toStringAsFixed(1)}s'; return '${millis ~/ 60000}m ${(millis % 60000) ~/ 1000}s'; } /// The input keys that carry a human-facing command or description, in order. const chatToolDetailKeys = [ 'description', 'command', 'filePath', 'file_path', 'path', 'pattern', 'query', 'url', ]; const int chatToolDetailMaxLength = 72; /// A one-line command/description from the call's input, elided past 72 /// characters; null when the input names none. String? chatToolDetail(JsonMap part) { final input = _state(part)['input']; if (input is! Map) return null; for (final key in chatToolDetailKeys) { final value = input[key]; if (value is! String) continue; final text = value.trim().replaceAll(RegExp(r'\s+'), ' '); if (text.isEmpty) continue; return text.length <= chatToolDetailMaxLength ? text : '${text.substring(0, chatToolDetailMaxLength - 1).trimRight()}…'; } return null; } /// INPUT: pretty JSON for an object, the raw string while it streams. String chatToolInputText(JsonMap part) { final input = _state(part)['input']; if (input == null) return ''; if (input is String) return input; if (input is Map || input is List) { return const JsonEncoder.withIndent(' ').convert(input); } return '$input'; } /// OUTPUT: the text content; `(no output)` once settled with none. String chatToolOutputText(JsonMap part) { final state = _state(part); final content = state['content']; final text = content is List ? content .whereType() .where((item) => item['type'] == 'text') .map((item) => '${item['text'] ?? ''}') .where((value) => value.trim().isNotEmpty) .join('\n') : ''; if (text.trim().isNotEmpty) return text; if (_runningStatus(state['status'])) return ''; return '(no output)'; } /// ERROR: the server's message, else the output text. String chatToolErrorText(JsonMap part) { final error = _state(part)['error']; final message = error is Map ? '${error['message'] ?? ''}' : ''; return message.trim().isNotEmpty ? message : chatToolOutputText(part); }