Show recorded timing and progress in background task lists

This commit is contained in:
2026-11-06 15:08:11 +00:00
parent c9a4cd0bf6
commit 6dfb5d7a4b
9 changed files with 513 additions and 9 deletions
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_shells.dart';
import 'chat_task_metadata_view.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../../../ui/text/monospace.dart';
@@ -30,12 +31,14 @@ class ChatBackgroundTaskDetail extends StatelessWidget {
const ChatBackgroundTaskDetail({
super.key,
required this.shell,
required this.now,
required this.output,
required this.heightBudget,
required this.onRefresh,
});
final JsonMap shell;
final DateTime now;
final ChatShellOutputSnapshot? output;
/// The modal viewport's height budget, used to bound its selectable output.
@@ -67,6 +70,7 @@ class ChatBackgroundTaskDetail extends StatelessWidget {
style: const TextStyle(fontFamily: appMonospaceFamily),
),
Text(shellStatusRunningLabel),
ChatTaskMetadataView(shell: shell, now: now, detail: true),
_outputArea(context, cupertino),
],
),
@@ -1,21 +1,54 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../domain/chat_shells.dart';
import '../domain/chat_task_metadata.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'chat_background_task_detail.dart';
import 'chat_background_tasks_controller.dart';
import 'chat_task_metadata_view.dart';
/// Scrollable modal content, backed by the same live controller as its badge.
class ChatBackgroundTasksPanel extends StatelessWidget {
const ChatBackgroundTasksPanel({super.key, required this.controller});
class ChatBackgroundTasksPanel extends StatefulWidget {
const ChatBackgroundTasksPanel({
super.key,
required this.controller,
this.clock,
});
final ChatBackgroundTasksController controller;
final DateTime Function()? clock;
@override
State<ChatBackgroundTasksPanel> createState() =>
_ChatBackgroundTasksPanelState();
}
class _ChatBackgroundTasksPanelState extends State<ChatBackgroundTasksPanel> {
late final Timer _ticker;
ChatBackgroundTasksController get controller => widget.controller;
DateTime get now => (widget.clock ?? DateTime.now)();
@override
void initState() {
super.initState();
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted && widget.controller.running.isNotEmpty) setState(() {});
});
}
@override
void dispose() {
_ticker.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: controller,
builder: (context, _) {
final at = now;
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final running = controller.running;
@@ -44,11 +77,12 @@ class ChatBackgroundTasksPanel extends StatelessWidget {
if (running.isNotEmpty)
Text(backgroundTasksRunningLabel(running.length)),
for (final task in running) ...[
_taskRow(context, task, cupertino),
_taskRow(context, task, cupertino, at),
if (shell != null && shellIdOf(task) == controller.selectedId)
ChatBackgroundTaskDetail(
key: const Key('chat-bg-task-detail'),
shell: shell,
now: at,
output: controller.output,
heightBudget: MediaQuery.sizeOf(context).height,
onRefresh: controller.readOutput,
@@ -63,11 +97,12 @@ class ChatBackgroundTasksPanel extends StatelessWidget {
BuildContext context,
Map<String, dynamic> shell,
bool cupertino,
DateTime at,
) {
final id = shellIdOf(shell);
final selected = controller.selectedId == id;
final title = Text(
shellCommandSummary(shell['command']),
shellCommandSummary(taskText(shell, 'description') ?? shell['command']),
maxLines: 2,
overflow: TextOverflow.ellipsis,
);
@@ -77,7 +112,7 @@ class ChatBackgroundTasksPanel extends StatelessWidget {
child: CupertinoListTile(
key: ValueKey('chat-bg-task-$id'),
title: title,
subtitle: const Text(shellRowLabel),
subtitle: ChatTaskMetadataView(shell: shell, now: at),
backgroundColor: selected
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
: null,
@@ -88,7 +123,7 @@ class ChatBackgroundTasksPanel extends StatelessWidget {
return ListTile(
key: ValueKey('chat-bg-task-$id'),
title: title,
subtitle: const Text(shellRowLabel),
subtitle: ChatTaskMetadataView(shell: shell, now: at),
selected: selected,
onTap: () => controller.select(id),
);
@@ -0,0 +1,96 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_task_metadata.dart';
/// Recorded task facts in the row, with unabridged progress in selected detail.
class ChatTaskMetadataView extends StatelessWidget {
const ChatTaskMetadataView({
super.key,
required this.shell,
required this.now,
this.detail = false,
});
final JsonMap shell;
final DateTime now;
final bool detail;
String _date(BuildContext context, DateTime time) {
final material = Localizations.of<MaterialLocalizations>(
context,
MaterialLocalizations,
);
final date = material != null
? '${time.day} ${material.formatMonthYear(time)}'
: '${time.day} ${CupertinoLocalizations.of(context).datePickerMonth(time.month)} ${time.year}';
return '$date · ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
}
@override
Widget build(BuildContext context) {
final start = taskInstant(shell, 'started_at');
final elapsed = taskElapsed(shell, now);
final duration = taskNumber(shell, 'duration_ms');
final updated = taskInstant(shell, 'updated_at');
final description = taskText(shell, 'description');
final progress = taskText(shell, 'progress');
final kind = taskText(shell, 'task_type');
final lastTool = taskText(shell, 'last_tool');
final tools = taskNumber(shell, 'tool_count');
final tokens = taskNumber(shell, 'token_count');
final counters = [
if (tools != null) '$tools tool calls',
if (tokens != null) '$tokens tokens',
];
final state = elapsed == null
? 'Running'
: 'Running · ${taskDuration(elapsed)}';
final kindLabel = kind == null
? null
: '${kind[0].toUpperCase()}${kind.substring(1).replaceAll('_', ' ')}';
final summary =
progress ??
(description != null &&
!'${shell['command'] ?? ''}'.endsWith(description)
? description
: null);
final facts = <String>[
if (!detail) kindLabel == null ? state : '$kindLabel · $state',
if (detail && elapsed != null) 'Elapsed: ${taskDuration(elapsed)}',
start == null
? 'Start time unavailable'
: 'Started ${_date(context, start)}',
if (start != null && elapsed == null)
'Start time is ahead of this device’s clock',
if (elapsed == null && duration != null)
'Reported runtime: ${taskDuration(Duration(milliseconds: duration))}',
if (detail) ...[?kindLabel, ?description, ?progress] else ...[?summary],
if (counters.isNotEmpty) counters.join(' · '),
if (detail && lastTool != null) 'Last tool: $lastTool',
if (detail && updated != null) 'Last update ${_date(context, updated)}',
];
// CupertinoListTile supplies a one-line default; explicitly allow metadata
// to wrap and grow with the available width and system text scaling.
return DefaultTextStyle(
style: DefaultTextStyle.of(context).style,
softWrap: true,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final fact in facts)
Padding(
padding: const EdgeInsets.only(top: 2),
child: Text(
fact,
maxLines: detail ? null : 2,
overflow: detail ? TextOverflow.clip : TextOverflow.ellipsis,
),
),
],
),
);
}
}
+12 -2
View File
@@ -71,7 +71,11 @@ List<JsonMap> mergeShellFrame(List<JsonMap> shells, JsonMap? shell) {
final index = next.indexWhere((candidate) => shellIdOf(candidate) == id);
final prior = index >= 0 ? next[index] : null;
final merged = <String, dynamic>{
...shell,
...?prior,
for (final entry in shell.entries)
if (entry.value != null &&
!(entry.value is String && (entry.value as String).trim().isEmpty))
entry.key: entry.value,
'id': id,
'command': blankShellValue(shell['command'])
? (prior?['command'] ?? '')
@@ -100,7 +104,13 @@ List<JsonMap> shellsLoaded(List<JsonMap> shells, List<JsonMap> running) {
}
for (final shell in running) {
if (blankShellValue(shell['id'])) continue;
byId[shellIdOf(shell)] = shell;
final prior = shells
.where((entry) => shellIdOf(entry) == shellIdOf(shell))
.firstOrNull;
byId[shellIdOf(shell)] = mergeShellFrame(
prior == null ? const [] : [prior],
shell,
).single;
}
final values = byId.values.toList();
return values.length <= observedShellLimit
@@ -0,0 +1,44 @@
import '../../../core/api_gateway.dart';
/// Optional numeric facts are present only when finite, integral and nonnegative.
int? taskNumber(JsonMap shell, String field) {
final value = shell[field];
if (value is! num ||
!value.isFinite ||
value < 0 ||
value > 8640000000000000 ||
value != value.roundToDouble()) {
return null;
}
return value.toInt();
}
DateTime? taskInstant(JsonMap shell, String field) {
final millis = taskNumber(shell, field);
return millis == null || millis == 0
? null
: DateTime.fromMillisecondsSinceEpoch(millis, isUtc: true).toLocal();
}
String? taskText(JsonMap shell, String field) {
final value = shell[field];
return value is String && value.trim().isNotEmpty ? value.trim() : null;
}
/// A clock ahead of this device is not a negative runtime.
Duration? taskElapsed(JsonMap shell, DateTime now) {
final start = taskInstant(shell, 'started_at');
if (start == null || start.isAfter(now)) return null;
return now.difference(start);
}
String taskDuration(Duration elapsed) {
if (elapsed.inDays > 0) return '${elapsed.inDays}d ${elapsed.inHours % 24}h';
if (elapsed.inHours > 0) {
return '${elapsed.inHours}h ${elapsed.inMinutes % 60}m';
}
if (elapsed.inMinutes > 0) {
return '${elapsed.inMinutes}m ${elapsed.inSeconds % 60}s';
}
return '${elapsed.inSeconds}s';
}