import '../../../../core/api_gateway.dart'; import '../../domain/chat_sessions.dart'; /// Titles shared by more than one visible session, so row action labels can /// add the session id the way the web client does. Set duplicateChatTitles(Iterable sessions) { final seen = {}; final duplicates = {}; for (final session in sessions) { final title = chatSessionTitle(session); if (!seen.add(title)) duplicates.add(title); } return duplicates; } String sessionActionsLabel(JsonMap session, Set duplicateTitles) { final title = chatSessionTitle(session); return duplicateTitles.contains(title) ? 'Chat actions for $title (${session['id'] ?? ''})' : 'Chat actions for $title'; } /// The web row's relative recency: `just now`, `N min ago`, `N hr ago`, /// `N days ago`, then a short date (`22 Oct`). A row without a usable /// `updated_at` reports nothing rather than inventing a time. String relativeChatSessionTime(Object? updatedAt, {DateTime? now}) { final millis = chatSessionMillis(updatedAt); if (millis <= 0) return ''; final then = DateTime.fromMillisecondsSinceEpoch(millis); var elapsed = (now ?? DateTime.now()).difference(then); if (elapsed.isNegative) elapsed = Duration.zero; final seconds = elapsed.inSeconds; if (seconds < 45) return 'just now'; final minutes = seconds ~/ 60 < 1 ? 1 : seconds ~/ 60; if (minutes < 60) return '$minutes min ago'; final hours = minutes ~/ 60; if (hours < 24) return '$hours hr ago'; final days = hours ~/ 24; if (days < 7) return days == 1 ? '1 day ago' : '$days days ago'; return '${then.day} ${_shortMonths[then.month - 1]}'; } /// The row's one muted line: the recorded model and the folder (home as `~`). /// The harness is the row's avatar, so it is not repeated as a word. String chatSessionSubtitle(JsonMap session) => [ '${session['model'] ?? ''}'.trim(), homeRelativePath('${session['directory'] ?? ''}'.trim()), ].where((part) => part.isNotEmpty).join(' ยท '); final _homePrefix = RegExp(r'^(/Users/[^/]+|/home/[^/]+|/root)(?=/|$)'); /// `/Users/alan/repos/x` (or `/home/alan/...`) as `~/repos/x`. String homeRelativePath(String path) => path.replaceFirst(_homePrefix, '~'); /// The row's attention/state word: `Closed` takes precedence, otherwise only /// actually unread content is `New`. Loading activity is rendered separately. String? chatSessionRowStatus(JsonMap session, {required bool unseen}) { if (session['closed_at'] != null || session['closed'] == true) { return 'Closed'; } return unseen ? 'New' : null; } const _shortMonths = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', // `Intl.DateTimeFormat('en-GB', { month: 'short' })`, which the web row // uses, abbreviates September as `Sept`, not `Sep`. 'Sept', 'Oct', 'Nov', 'Dec', ];