Show session automation counts in chat composer

This commit is contained in:
2026-11-08 16:40:53 +00:00
parent 3fbfb85ace
commit b7f439a94f
13 changed files with 716 additions and 89 deletions
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../../../core/error_reason.dart';
import '../data/chat_repository.dart';
import '../domain/chat_automations.dart';
import '../domain/chat_shells.dart';
/// One session's live background tasks and lazy, bounded output selection.
@@ -12,6 +13,7 @@ class ChatBackgroundTasksController extends ChangeNotifier {
ChatBackgroundTasksController(this.gateway, this.sessionId) {
_listen();
unawaited(refresh());
unawaited(refreshAutomations());
}
ApiGateway gateway;
@@ -27,10 +29,16 @@ class ChatBackgroundTasksController extends ChangeNotifier {
bool _disposed = false;
int _generation = 0;
int _outputGeneration = 0;
Timer? _automationDebounce;
Future<void>? _automationRequest;
bool _automationRefreshPending = false;
ChatAutomationStatus _automations = const ChatAutomationStatus();
int _automationGeneration = 0;
List<JsonMap> get running => runningShells(_shells);
JsonMap? get selectedShell =>
running.where((shell) => shellIdOf(shell) == selectedId).firstOrNull;
ChatAutomationStatus get automationStatus => _automations;
void replace(
ApiGateway nextGateway,
@@ -39,6 +47,8 @@ class ChatBackgroundTasksController extends ChangeNotifier {
}) {
if (_disposed) return;
unawaited(_frames?.cancel());
_automationRequest = null;
_automationRefreshPending = false;
gateway = nextGateway;
sessionId = nextSessionId;
_repository = ChatRepository(gateway);
@@ -53,12 +63,20 @@ class ChatBackgroundTasksController extends ChangeNotifier {
output = null;
error = null;
_outputGeneration++;
_automationGeneration++;
_automationRequest = null;
_automationRefreshPending = false;
_automationDebounce?.cancel();
_automationDebounce = null;
_automations = const ChatAutomationStatus();
unawaited(refresh(notify: notify));
unawaited(refreshAutomations(notify: notify));
}
void _listen() => _frames = gateway.frames.listen((envelope) {
if (_disposed || envelope['type'] != 'chat_event') return;
final frame = envelope['chat_event'];
_scheduleAutomationRefresh(frame);
if (frame is! Map ||
frame['session_id'] != sessionId ||
frame['kind'] != 'shell' ||
@@ -80,6 +98,35 @@ class ChatBackgroundTasksController extends ChangeNotifier {
notifyListeners();
}, onError: (_) {});
void _scheduleAutomationRefresh(Object frame) {
if (frame is! Map) return;
if (frame['session_id'] != sessionId) return;
final kind = '${frame['kind'] ?? ''}'.toLowerCase();
final step = frame['step'];
final part = frame['part'];
final stepEnded =
kind == 'step' &&
step is Map &&
'${step['status'] ?? ''}'.toLowerCase() == 'ended';
final toolCompleted =
kind == 'tool' &&
part is Map &&
(part['state'] is Map) &&
{
'completed',
'error',
}.contains('${(part['state'] as Map)['status'] ?? ''}'.toLowerCase());
if (!stepEnded && !toolCompleted) {
return;
}
if (_automationRequest != null) _automationRefreshPending = true;
_automationDebounce?.cancel();
_automationDebounce = Timer(const Duration(milliseconds: 250), () {
_automationDebounce = null;
unawaited(refreshAutomations());
});
}
Future<void> refresh({bool notify = true}) async {
if (_disposed) return;
final token = ++_generation;
@@ -109,6 +156,65 @@ class ChatBackgroundTasksController extends ChangeNotifier {
notifyListeners();
}
Future<void> refreshAutomations({bool notify = true}) async {
if (_disposed) return;
if (_automationRequest != null) {
_automationRefreshPending = true;
return;
}
final token = ++_automationGeneration;
final request = _readAutomations(token, notify: notify);
_automationRequest = request;
try {
await request;
} finally {
if (identical(_automationRequest, request)) {
_automationRequest = null;
if (_automationRefreshPending && !_disposed) {
_automationRefreshPending = false;
_automationDebounce?.cancel();
_automationDebounce = Timer(const Duration(milliseconds: 250), () {
_automationDebounce = null;
unawaited(refreshAutomations());
});
}
}
}
}
Future<void> _readAutomations(int token, {required bool notify}) async {
final repository = _repository;
final id = sessionId;
// A legacy gateway can throw before returning a Future. Always cross an
// async boundary before notifying listeners, including didUpdateWidget.
await Future<void>.value();
if (_disposed || token != _automationGeneration) return;
final previous = _automations;
try {
final reply = await repository.automations(id);
if (_disposed || token != _automationGeneration) return;
_automations = ChatAutomationStatus.fromReply(
reply,
expectedSessionId: id,
);
} catch (_) {
// Older servers do not know this optional command. Keep shell state
// usable and present capability absence in the inspector instead.
if (_disposed || token != _automationGeneration) return;
_automations = const ChatAutomationStatus();
}
// Unsupported/unknown is intentionally quiet in the composer; supported
// reports need to reveal their positive count.
final changed =
previous.supported != _automations.supported ||
previous.count != _automations.count ||
previous.reason != _automations.reason ||
previous.observedAt != _automations.observedAt;
if (changed || (notify && _automations.supported)) {
notifyListeners();
}
}
void _dropStaleSelection() {
if (selectedId == null || selectedShell != null) return;
selectedId = null;
@@ -167,6 +273,11 @@ class ChatBackgroundTasksController extends ChangeNotifier {
_disposed = true;
_generation++;
_outputGeneration++;
_automationGeneration++;
_automationDebounce?.cancel();
_automationDebounce = null;
_automationRequest = null;
_automationRefreshPending = false;
unawaited(_frames?.cancel());
super.dispose();
}