perf(chat): isolate usage updates from transcript rendering

This commit is contained in:
2026-10-24 11:13:22 +01:00
parent da44001ab2
commit 6d623197f9
3 changed files with 74 additions and 4 deletions
@@ -35,6 +35,7 @@ class ChatThreadController extends ChangeNotifier {
isAlive: () => !_disposed,
);
StreamSubscription<JsonMap>? _subscription;
final _transcriptChanges = ChangeNotifier();
List<JsonMap> _messages = const [];
bool _running = false;
@@ -55,6 +56,9 @@ class ChatThreadController extends ChangeNotifier {
bool _resyncAgain = false;
List<JsonMap> get messages => _messages;
/// Usage-only snapshots refresh Info, not the expensive transcript tree.
Listenable get transcriptChanges => _transcriptChanges;
bool get running => _running;
String? get liveAssistantMessageId => _liveAssistantMessageId;
bool get loaded => _loaded;
@@ -239,7 +243,7 @@ class ChatThreadController extends ChangeNotifier {
final usage = chatUsageSnapshot(body['usage']);
if (usage == null || !_reportedUsage(usage)) return;
_usage = usage;
_notify();
_notify(transcript: false);
return;
}
if (body['kind'] == 'form') {
@@ -334,13 +338,16 @@ class ChatThreadController extends ChangeNotifier {
_notify();
}
void _notify() {
if (!_disposed) notifyListeners();
void _notify({bool transcript = true}) {
if (_disposed) return;
notifyListeners();
if (transcript && !_disposed) _transcriptChanges.notifyListeners();
}
@override
void dispose() {
_disposed = true;
_transcriptChanges.dispose();
_generation++;
unawaited(_subscription?.cancel());
super.dispose();
@@ -575,7 +575,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: _controller,
animation: _controller.transcriptChanges,
builder: (context, _) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
@@ -0,0 +1,63 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'chat_thread_fixtures.dart';
void main() {
for (final cupertino in [false, true]) {
testWidgets(
'usage updates leave transcript widgets alone but refresh Info ($cupertino)',
(tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
chatMessage(
'a1',
'assistant',
parts: [
{'type': 'text', 'text': '**Existing answer**'},
],
),
]);
final thread = ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
: MaterialApp(home: Scaffold(body: thread)),
);
await tester.pumpAndSettle();
final answer = tester.widget<ChatMessageView>(
find.byType(ChatMessageView),
);
gateway.emitEvent({
'session_id': 's',
'kind': 'usage',
'usage': {'scope': 'session', 'total_tokens': 123},
});
await tester.pumpAndSettle();
expect(
tester.widget<ChatMessageView>(find.byType(ChatMessageView)),
same(answer),
);
await tester.tap(find.byKey(const Key('chat-info')));
await tester.pumpAndSettle();
expect(find.text('123'), findsOneWidget);
gateway.emitEvent({
'session_id': 's',
'kind': 'usage',
'usage': {'scope': 'session', 'total_tokens': 456},
});
await tester.pumpAndSettle();
expect(find.text('456'), findsOneWidget);
expect(find.text('123'), findsNothing);
},
);
}
}