Files
pipelines/app/lib/features/chat/domain/chat_sessions.dart
T
alan d1ffa7c79b feat(app): compact chat session rows and thread header
Follow the web chat layout on both hosts without changing request shapes or
bubble/composer behaviour.

- Session rows (Material and Cupertino): title with trailing relative
  recency, muted `directory · model` line with the `New`/`Closed` word at
  the trailing edge. Shared row content keeps one arrangement; native tiles
  keep their own padding, separators and subtitle size. The state word keeps
  its intrinsic width and the metadata ellipsises first, so it is never
  clipped at 320px/2x.
- Thread header: one row of compact icon-only Back, ellipsised title,
  bounded model control and Chat information. Back keeps the destination as
  its accessible name and both hosts share `Key('chat-back')`.
- Share the epoch-millis parser as `chatSessionMillis`, and match the web's
  relative-time words, including `Sept`.
- Cover the row content, status word at narrow widths, header layout and
  Back accessibility in widget tests; update existing tests for the
  separate trailing status word.
2026-10-22 15:01:21 +01:00

78 lines
2.7 KiB
Dart

import '../../../core/api_gateway.dart';
/// Pure session-list operations. Unknown server fields remain on each row.
String chatSessionTitle(JsonMap session) {
final title = '${session['title'] ?? ''}';
if (title.trim().isNotEmpty) return title;
final id = '${session['id'] ?? ''}';
return id.isEmpty ? 'Untitled chat' : id.substring(0, id.length.clamp(0, 18));
}
List<JsonMap> filterChatSessions(List<JsonMap> sessions, String query) {
final needle = query.trim().toLowerCase();
if (needle.isEmpty) return sessions;
return sessions
.where(
(session) =>
'${chatSessionTitle(session)}\u0000${session['directory'] ?? ''}'
.toLowerCase()
.contains(needle),
)
.toList();
}
/// Directory groups retain server recency order, both within and between groups.
List<({String directory, List<JsonMap> sessions})> groupChatSessions(
List<JsonMap> sessions,
) {
final groups = <String, List<JsonMap>>{};
for (final session in sessions) {
groups.putIfAbsent('${session['directory'] ?? ''}', () => []).add(session);
}
return groups.entries
.map((entry) => (directory: entry.key, sessions: entry.value))
.toList();
}
String chatSessionActivity({required bool running, required bool unseen}) =>
running
? 'streaming'
: unseen
? 'new'
: 'idle';
/// A server timestamp as epoch milliseconds. Accepts an epoch number or an
/// ISO-8601 string and reports `0` for anything unusable, so callers never
/// invent a time for a missing field.
int chatSessionMillis(Object? value) {
if (value is int) return value > 0 ? value : 0;
if (value is num) return value > 0 ? value.toInt() : 0;
final text = '${value ?? ''}'.trim();
if (text.isEmpty) return 0;
final number = int.tryParse(text);
if (number != null) return number > 0 ? number : 0;
return DateTime.tryParse(text)?.millisecondsSinceEpoch ?? 0;
}
/// Native reported values only; missing and invalid values remain unavailable.
JsonMap? chatUsageSnapshot(Object? value) {
if (value is! Map || value is List) return null;
final usage = Map<String, dynamic>.from(value);
final result = <String, dynamic>{};
for (final key in ['total_tokens', 'context_tokens', 'context_window']) {
final n = usage[key];
result[key] = n is int && n >= 0 ? n : null;
}
for (final key in ['cost_usd', 'output_tokens_per_second']) {
final n = usage[key];
result[key] = n is num && n.isFinite && n >= 0 ? n : null;
}
result['scope'] = ['session', 'turn'].contains(usage['scope'])
? usage['scope']
: '';
result['rate_basis'] = ['generation', 'turn'].contains(usage['rate_basis'])
? usage['rate_basis']
: '';
return result.values.any((v) => v != null && v != '') ? result : null;
}