Chat list: spinner and background-task badge from server activity on every row
The list applies chat_activity pushes and each row's activity by seq, so every listed chat shows a running turn, not only the chat open here. An idle chat with running background tasks wears a quiet badge with tooltip and semantics "Background tasks running (N)". Older servers keep the old behaviour. List rows that never changed carry the server's current seq so they order after snapshots kept from an earlier server process.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// One chat's live activity as the server observes it: a turn in flight and
|
||||
/// how many background tasks (shells, background commands, subagents) still
|
||||
/// run. It rides on each `chat_sessions` row as `activity` and is pushed to
|
||||
/// every connection as `chat_activity` on each change. [seq] orders one
|
||||
/// chat's snapshots across the list and the pushes; the larger one is newer.
|
||||
typedef ChatListActivity = ({
|
||||
String sessionId,
|
||||
bool turnRunning,
|
||||
int backgroundTasks,
|
||||
int seq,
|
||||
});
|
||||
|
||||
/// Reads an `activity` row field or a `chat_activity` body. [sessionId] names
|
||||
/// the row when the body omits it. Anything malformed is null, so an older
|
||||
/// server (which sends neither) leaves the list exactly as before.
|
||||
ChatListActivity? chatListActivityOf(Object? value, {String? sessionId}) {
|
||||
if (value is! Map) return null;
|
||||
final named = value['session_id'];
|
||||
final id = sessionId ?? (named is String ? named : '');
|
||||
if (id.isEmpty) return null;
|
||||
final tasks = value['background_tasks'];
|
||||
final seq = value['seq'];
|
||||
return (
|
||||
sessionId: id,
|
||||
turnRunning: value['turn_running'] == true,
|
||||
backgroundTasks: tasks is num && tasks > 0 ? tasks.toInt() : 0,
|
||||
seq: seq is num ? seq.toInt() : 0,
|
||||
);
|
||||
}
|
||||
|
||||
/// The row keys the list controller derives from the activity.
|
||||
JsonMap chatListActivityRowFields(ChatListActivity activity) => {
|
||||
'running': activity.turnRunning,
|
||||
'background_tasks': activity.backgroundTasks,
|
||||
};
|
||||
|
||||
/// Running background tasks of a list row; 0 when unknown.
|
||||
int chatSessionBackgroundTasks(JsonMap session) {
|
||||
final tasks = session['background_tasks'];
|
||||
return tasks is int && tasks > 0 ? tasks : 0;
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import '../../../../core/servers/server_scope.dart';
|
||||
import '../../../../core/transport/connection_state.dart';
|
||||
import '../../../attention/runtime/attention_ack_sync.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../domain/chat_list_activity.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_session_local.dart';
|
||||
import 'session_attention.dart';
|
||||
@@ -65,6 +66,9 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
Future<void> _persistTail = Future<void>.value();
|
||||
int _runningRevision = 0;
|
||||
final Map<String, ({int revision, bool running})> _runningFrames = {};
|
||||
// The newest server activity applied per chat (list row or push), so an
|
||||
// older snapshot that arrives later never replaces it.
|
||||
final Map<String, ChatListActivity> _activity = {};
|
||||
StreamSubscription<JsonMap>? _frameSubscription;
|
||||
String? activeSessionId;
|
||||
|
||||
@@ -75,6 +79,9 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
|
||||
void _onFrame(JsonMap frame) {
|
||||
if (frame['type'] == 'chat_session_changed') return _sessionChanged(frame);
|
||||
if (frame['type'] == 'chat_activity') {
|
||||
return _activityChanged(frame['chat_activity']);
|
||||
}
|
||||
final effect = chatSessionFrameEffect(frame);
|
||||
if (effect == null) return;
|
||||
final id = effect.sessionId;
|
||||
@@ -130,6 +137,58 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if (found) _notify();
|
||||
}
|
||||
|
||||
/// The server pushed a chat's turn state and background-task count. It
|
||||
/// reaches every connection, so every listed chat's spinner and
|
||||
/// background-task badge follow it, not only the chat open here. A push
|
||||
/// older than the snapshot already applied is ignored.
|
||||
void _activityChanged(Object? body) {
|
||||
final activity = chatListActivityOf(body);
|
||||
if (activity == null || _disposed) return;
|
||||
final id = activity.sessionId;
|
||||
if (activity.seq <= (_activity[id]?.seq ?? 0)) return;
|
||||
_activity[id] = activity;
|
||||
_runningFrames[id] = (
|
||||
revision: ++_runningRevision,
|
||||
running: activity.turnRunning,
|
||||
);
|
||||
final fields = chatListActivityRowFields(activity);
|
||||
var changed = false;
|
||||
_sessions = [
|
||||
for (final row in _sessions)
|
||||
if ('${row['id'] ?? ''}' == id &&
|
||||
(row['running'] != fields['running'] ||
|
||||
row['background_tasks'] != fields['background_tasks']))
|
||||
() {
|
||||
changed = true;
|
||||
return {...row, ...fields};
|
||||
}()
|
||||
else
|
||||
row,
|
||||
];
|
||||
if (changed) _notify();
|
||||
}
|
||||
|
||||
/// A listed row with the newest activity known for it: the row's own
|
||||
/// `activity` unless a newer push was already applied, then a turn frame
|
||||
/// this connection saw during the read. An older server sends no
|
||||
/// `activity`, so only the turn frames apply, as before.
|
||||
JsonMap _withActivity(JsonMap row, int runningRevision) {
|
||||
final id = '${row['id'] ?? ''}';
|
||||
var next = row;
|
||||
final listed = chatListActivityOf(row['activity'], sessionId: id);
|
||||
if (listed != null) {
|
||||
final known = _activity[id];
|
||||
final activity = known != null && known.seq > listed.seq ? known : listed;
|
||||
_activity[id] = activity;
|
||||
next = {...row, ...chatListActivityRowFields(activity)};
|
||||
}
|
||||
final frame = _runningFrames[id];
|
||||
if ((frame?.revision ?? 0) > runningRevision) {
|
||||
next = {...next, 'running': frame!.running};
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
void _bindAckSync(AttentionAckSync? sync) {
|
||||
if (identical(sync, _ackSync)) return;
|
||||
_ackSync?.removeListener(_ackChanged);
|
||||
@@ -246,6 +305,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if (identical(next.gateway, repository.gateway)) return;
|
||||
repository = next;
|
||||
_runningFrames.clear();
|
||||
_activity.clear();
|
||||
_listenFrames();
|
||||
_bindAckSync(attentionAckSyncOf(next.gateway) ?? _ackSync);
|
||||
refresh();
|
||||
@@ -265,12 +325,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
final sessions = await repository.sessions();
|
||||
if (generation != _generation) return;
|
||||
_sessions = [
|
||||
for (final row in sessions)
|
||||
if ((_runningFrames['${row['id'] ?? ''}']?.revision ?? 0) >
|
||||
runningRevision)
|
||||
{...row, 'running': _runningFrames['${row['id'] ?? ''}']!.running}
|
||||
else
|
||||
row,
|
||||
for (final row in sessions) _withActivity(row, runningRevision),
|
||||
];
|
||||
_seedLocal();
|
||||
_loading = false;
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// The corner badge a chat's avatar wears for its live activity: the
|
||||
/// platform's activity indicator while a turn runs; otherwise, while
|
||||
/// background tasks still run, a quiet muted glyph (never a second spinner).
|
||||
/// Null when the chat is idle.
|
||||
Widget? chatSessionActivityBadge({
|
||||
required bool cupertino,
|
||||
required bool running,
|
||||
required int backgroundTasks,
|
||||
}) {
|
||||
if (running) {
|
||||
return Semantics(
|
||||
key: const Key('chat-row-working'),
|
||||
label: 'Chat is working',
|
||||
child: _WorkingBadge(cupertino: cupertino),
|
||||
);
|
||||
}
|
||||
if (backgroundTasks <= 0) return null;
|
||||
final label = 'Background tasks running ($backgroundTasks)';
|
||||
// Hover shows the tooltip on a pointer device; a touch long press stays the
|
||||
// row's own gesture.
|
||||
return Tooltip(
|
||||
key: const Key('chat-row-background'),
|
||||
message: label,
|
||||
triggerMode: TooltipTriggerMode.manual,
|
||||
excludeFromSemantics: true,
|
||||
child: Semantics(
|
||||
label: label,
|
||||
child: _BackgroundBadge(cupertino: cupertino),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The badge's own disc in the surface colour, so it reads over any avatar.
|
||||
class _BadgeDisc extends StatelessWidget {
|
||||
const _BadgeDisc({required this.cupertino, required this.child});
|
||||
|
||||
final bool cupertino;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final background = cupertino
|
||||
? CupertinoColors.systemBackground.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surface;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(color: background, shape: BoxShape.circle),
|
||||
child: Padding(padding: const EdgeInsets.all(2), child: child),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WorkingBadge extends StatelessWidget {
|
||||
const _WorkingBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _BadgeDisc(
|
||||
cupertino: cupertino,
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator(radius: 6)
|
||||
: const SizedBox.square(
|
||||
dimension: 12,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BackgroundBadge extends StatelessWidget {
|
||||
const _BackgroundBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
return _BadgeDisc(
|
||||
cupertino: cupertino,
|
||||
child: Icon(
|
||||
cupertino ? CupertinoIcons.ellipsis_circle_fill : Icons.pending,
|
||||
size: 12,
|
||||
color: color,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ import '../../../../ui/layout/app_list_avatar.dart';
|
||||
import '../../../../ui/layout/app_list_row.dart';
|
||||
import '../../../../ui/text/relative_time_label.dart';
|
||||
import '../../domain/chat_colors.dart';
|
||||
import '../../domain/chat_list_activity.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_color_palette.dart';
|
||||
import 'session_actions.dart';
|
||||
import 'session_activity_badge.dart';
|
||||
import 'session_harness_icon.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
@@ -68,6 +70,9 @@ class ChatSessionRow extends StatelessWidget {
|
||||
leading: ChatSessionAvatar(
|
||||
session: session,
|
||||
running: session['running'] == true && !unseen,
|
||||
backgroundTasks: session['running'] == true
|
||||
? 0
|
||||
: chatSessionBackgroundTasks(session),
|
||||
selected: selected,
|
||||
),
|
||||
// The star rides in the title's own text, so a narrow row ellipsises
|
||||
@@ -149,17 +154,20 @@ class _Star extends StatelessWidget {
|
||||
|
||||
/// The chat's harness mark in a filled circle, like a contact avatar. A
|
||||
/// running chat carries a small activity badge on the avatar's corner instead
|
||||
/// of a line of its own; a selected chat's avatar takes the accent fill.
|
||||
/// of a line of its own, and an idle chat whose background tasks still run a
|
||||
/// quieter one; a selected chat's avatar takes the accent fill.
|
||||
class ChatSessionAvatar extends StatelessWidget {
|
||||
const ChatSessionAvatar({
|
||||
super.key,
|
||||
required this.session,
|
||||
this.running = false,
|
||||
this.backgroundTasks = 0,
|
||||
this.selected = false,
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final bool running;
|
||||
final int backgroundTasks;
|
||||
final bool selected;
|
||||
|
||||
@override
|
||||
@@ -192,39 +200,12 @@ class ChatSessionAvatar extends StatelessWidget {
|
||||
return AppListAvatar(
|
||||
foreground: mark,
|
||||
background: fill,
|
||||
badge: running
|
||||
? Semantics(
|
||||
key: const Key('chat-row-working'),
|
||||
label: 'Chat is working',
|
||||
child: _WorkingBadge(cupertino: cupertino),
|
||||
)
|
||||
: null,
|
||||
badge: chatSessionActivityBadge(
|
||||
cupertino: cupertino,
|
||||
running: running,
|
||||
backgroundTasks: backgroundTasks,
|
||||
),
|
||||
child: SessionHarnessIcon(session: session, color: mark),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WorkingBadge extends StatelessWidget {
|
||||
const _WorkingBadge({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final background = cupertino
|
||||
? CupertinoColors.systemBackground.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surface;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(color: background, shape: BoxShape.circle),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(2),
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator(radius: 6)
|
||||
: const SizedBox.square(
|
||||
dimension: 12,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user