Files
pipelines/app/lib/features/chat/ui/sessions/session_row_style.dart
T

110 lines
4.0 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
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<String> duplicateChatTitles(Iterable<JsonMap> sessions) {
final seen = <String>{};
final duplicates = <String>{};
for (final session in sessions) {
final title = chatSessionTitle(session);
if (!seen.add(title)) duplicates.add(title);
}
return duplicates;
}
String sessionActionsLabel(JsonMap session, Set<String> 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 muted second line: which agent runs the chat (an empty backend
/// is a legacy OpenCode row), its model, and the directory with the home
/// folder written as `~`.
String chatSessionMeta(JsonMap session) {
final backend = '${session['backend'] ?? ''}'.trim();
return [
backend.isEmpty ? 'opencode' : backend,
'${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 muted second line as the web words it: recency first, then the
/// directory and model (`1 day ago · /srv/probe · model-a`).
String chatSessionRowMetaLine(JsonMap session, {DateTime? now}) => [
relativeChatSessionTime(session['updated_at'], now: now),
chatSessionMeta(session),
].where((part) => part.isNotEmpty).join(' · ');
/// The word on the row's trailing edge while the session has news or a state:
/// `Closed` from the row, otherwise `New`. A streaming session deliberately
/// says nothing, as on the web, where the row's activity logo carries it.
String? chatSessionRowStatus(JsonMap session, {required bool unseen}) {
if (session['closed_at'] != null || session['closed'] == true) {
return 'Closed';
}
// Streaming has no word, as on the web; the row's activity icon carries it.
if (session['running'] == true) return null;
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',
];
IconData sessionMaterialIcon(JsonMap session) {
if (session['running'] == true) return Icons.sync;
if (session['starred'] == true) return Icons.star;
return Icons.chat_bubble_outline;
}
IconData sessionCupertinoIcon(JsonMap session) {
if (session['running'] == true) return CupertinoIcons.arrow_2_circlepath;
if (session['starred'] == true) return CupertinoIcons.star_fill;
return CupertinoIcons.chat_bubble;
}