feat(app): show recorded chat usage in a native info panel

The chat thread header gains an Info action that opens a platform sheet (Material bottom sheet, Cupertino popup) with the four harness-reported usage facts in the web order and wording: rate, tokens, context and reported cost. Unknown facts read Unavailable; reported zeros stay zero. A live usage frame replaces an open panel without adding messages or touching turn state.
This commit is contained in:
2026-10-22 10:21:53 +01:00
parent 7aa7007aee
commit f487257081
3 changed files with 468 additions and 0 deletions
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../data/chat_repository.dart';
import '../domain/chat_sessions.dart';
import '../domain/chat_transcript.dart';
import 'chat_thread_cards.dart';
@@ -42,6 +43,7 @@ class ChatThreadController extends ChangeNotifier {
bool _hasMore = false;
String _cursor = '';
String _nativeTurnId = '';
JsonMap? _usage;
String? _error;
bool _subscribed = false;
@@ -59,6 +61,11 @@ class ChatThreadController extends ChangeNotifier {
/// The server's opaque older-page cursor. Never interpreted, only passed back.
String get cursor => _cursor;
/// The latest recorded usage snapshot, or null before the harness reports.
/// Unknown fields stay null, so a viewer can distinguish them from zero.
JsonMap? get usage => _usage;
String? get error => _error;
bool get subscribed => _subscribed;
String? get subscriptionError => _subscriptionError;
@@ -222,6 +229,16 @@ class ChatThreadController extends ChangeNotifier {
unawaited(resync());
return;
}
if (body['kind'] == 'usage') {
// Usage replaces the last snapshot and never adds a message, runs a turn
// or admits a queued prompt. An unusable snapshot keeps the previous
// recorded values instead of replacing them with fabricated zeros.
final usage = chatUsageSnapshot(body['usage']);
if (usage == null || !_reportedUsage(usage)) return;
_usage = usage;
_notify();
return;
}
if (body['kind'] == 'form') {
final form = body['form'];
if (form is! Map) return;
@@ -240,6 +257,23 @@ class ChatThreadController extends ChangeNotifier {
_notify();
}
/// Whether a normalized snapshot names a scope or carries a recorded fact.
/// A window of zero reports no usable limit, and a rate basis alone reports
/// no number at all, so neither counts as a fact on its own.
bool _reportedUsage(JsonMap usage) {
if ('${usage['scope'] ?? ''}'.isNotEmpty) return true;
if (usage['context_window'] is int &&
(usage['context_window'] as int) > 0) {
return true;
}
return const [
'total_tokens',
'context_tokens',
'cost_usd',
'output_tokens_per_second',
].any((key) => usage[key] != null);
}
/// The pushed envelope body, or null for another message type. The server
/// wraps `chat_event` in the typed envelope; domain code never sees the
/// transport fields.