merge(app): land chat-activity-overlay micro-slice

This commit is contained in:
2026-10-22 09:50:21 +01:00
3 changed files with 475 additions and 30 deletions
@@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_activity_detail.dart';
import '../domain/chat_transcript.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../ui/transcript/chat_markdown_view.dart';
@@ -313,27 +314,48 @@ class _ChatThreadViewState extends State<ChatThreadView> {
/// One recorded activity line for a turn.
///
/// The line is a summary, not a control: expansion belongs to the later overlay
/// slice. Its wording and state come from the recorded row, and the icon only
/// repeats that state with theme roles, so an unknown kind or state is still
/// readable without colour.
class _ChatActivityLine extends StatelessWidget {
/// The line is a summary of the recorded sequence. Its wording and state come
/// from the recorded row, and the icon only repeats that state with theme roles,
/// so an unknown kind or state is still readable without colour. The trailing
/// control expands the same line into the recorded lifecycle and any recorded
/// input, output, error, duration and edit diff; collapsing restores the single
/// line without touching the turn.
class _ChatActivityLine extends StatefulWidget {
const _ChatActivityLine({required this.row});
final JsonMap row;
@override
State<_ChatActivityLine> createState() => _ChatActivityLineState();
}
class _ChatActivityLineState extends State<_ChatActivityLine> {
bool _expanded = false;
void _toggle() => setState(() => _expanded = !_expanded);
/// The latest recorded update, which is also the line's label/state. With no
/// records (a caller-built row) the row itself carries the same fields.
JsonMap _record() {
final records = widget.row['records'];
if (records is List) {
for (final record in records.reversed) {
if (record is Map) return JsonMap.from(record);
}
}
return widget.row;
}
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final state = '${row['state'] ?? ''}'.trim().toLowerCase();
final state = '${widget.row['state'] ?? ''}'.trim().toLowerCase();
final failed = state == 'failed' || state == 'error';
final running = state == 'running';
final completed =
state == 'completed' || state == 'ended' || state == 'succeeded';
final secondary = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final secondary = _secondary(context, cupertino);
final color = failed
? (cupertino
? CupertinoColors.systemRed.resolveFrom(context)
@@ -355,24 +377,130 @@ class _ChatActivityLine extends StatelessWidget {
? CupertinoIcons.check_mark_circled
: Icons.check_circle_outline)
: (cupertino ? CupertinoIcons.circle : Icons.circle_outlined);
final label = '${row['label'] ?? ''}'.trim();
return Semantics(
container: true,
label: label,
value: state,
child: ExcludeSemantics(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
final label = '${widget.row['label'] ?? ''}'.trim();
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(icon, color: color),
const SizedBox(width: 8),
Expanded(child: Text(label, style: style)),
Expanded(
child: Semantics(
container: true,
label: label,
value: state,
child: ExcludeSemantics(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(icon, color: color),
const SizedBox(width: 8),
Expanded(child: Text(label, style: style)),
],
),
),
),
),
_toggleControl(cupertino),
],
),
if (_expanded) _detail(context, cupertino, secondary),
],
),
);
}
/// The host's own disclosure control, with an action label that states what a
/// tap does. Cupertino's button has no built-in label, so the wrapping node
/// carries it and the button keeps the pointer handling.
Widget _toggleControl(bool cupertino) {
const key = ValueKey<String>('chat-activity-toggle');
final label = _expanded ? 'Collapse activity' : 'Expand activity';
final icon = Icon(
_expanded
? (cupertino ? CupertinoIcons.chevron_up : Icons.expand_less)
: (cupertino ? CupertinoIcons.chevron_down : Icons.expand_more),
);
if (!cupertino) {
return IconButton(
key: key,
tooltip: label,
onPressed: _toggle,
icon: icon,
);
}
return Semantics(
container: true,
button: true,
label: label,
onTap: _toggle,
child: ExcludeSemantics(
child: CupertinoButton(
key: key,
padding: EdgeInsets.zero,
onPressed: _toggle,
child: icon,
),
),
);
}
/// The expanded section: every recorded field, with the exact absence sentence
/// for one the record did not carry. Only [ChatActivityDetail] reads the
/// record, so the line and its details cannot disagree about a field.
Widget _detail(BuildContext context, bool cupertino, Color secondary) {
final detail = chatActivityDetail(_record());
final baseStyle = cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodyMedium;
final labelStyle = TextStyle(color: secondary, fontWeight: FontWeight.w600);
Widget field(String label, String value) {
final absent =
value == chatActivityLifecycleAbsent ||
value == chatActivityInputAbsent ||
value == chatActivityOutputAbsent ||
value == chatActivityErrorAbsent ||
value == chatActivityDurationAbsent ||
value == chatActivityDiffAbsent;
return Padding(
padding: const EdgeInsetsDirectional.only(top: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(label, style: labelStyle),
DefaultTextStyle.merge(
style: absent ? TextStyle(color: secondary) : const TextStyle(),
child: SelectableText(value),
),
],
),
);
}
return DefaultTextStyle.merge(
style: baseStyle,
child: Padding(
key: const ValueKey<String>('chat-activity-details'),
padding: const EdgeInsetsDirectional.only(bottom: 2),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
field('Lifecycle', detail.lifecycle),
field('Input', detail.input),
field('Output', detail.output),
field('Error', detail.error),
field('Duration', detail.duration),
field('Edit diff', detail.diff),
],
),
),
);
}
Color _secondary(BuildContext context, bool cupertino) => cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
}
@@ -0,0 +1,194 @@
import 'dart:convert';
import '../../../core/api_gateway.dart';
import 'chat_transcript.dart';
/// The exact sentence for a recorded activity field the server did not send.
/// Input and Output reuse the run detail's own wording, so the same absent fact
/// never reads differently in this app.
const String chatActivityLifecycleAbsent = 'No lifecycle recorded.';
const String chatActivityInputAbsent = 'No input recorded.';
const String chatActivityOutputAbsent = 'No output recorded.';
const String chatActivityErrorAbsent = 'No error recorded.';
const String chatActivityDurationAbsent = 'No duration recorded.';
const String chatActivityDiffAbsent = 'No edit diff recorded.';
/// The recorded detail behind one activity line.
///
/// Every field is the value the server recorded on the line's latest record, or
/// the exact absence sentence above. Nothing is inferred from answer text and an
/// empty string/map/list is absent rather than a blank block.
class ChatActivityDetail {
const ChatActivityDetail({
required this.lifecycle,
required this.input,
required this.output,
required this.error,
required this.duration,
required this.diff,
});
final String lifecycle;
final String input;
final String output;
final String error;
final String duration;
final String diff;
}
/// The expanded detail of [record], reading only the fields the parsed activity
/// record already carries.
///
/// Tool-style records keep their payload in `state` (input, content, error and
/// the invocation's timing envelope), while an activity record may carry the same
/// names at the top level; the top-level value wins when both are present.
ChatActivityDetail chatActivityDetail(JsonMap record) {
final state = record['state'] is Map
? JsonMap.from(record['state'] as Map)
: const <String, dynamic>{};
return ChatActivityDetail(
lifecycle: _lifecycle(record, state) ?? chatActivityLifecycleAbsent,
input: _input(record, state) ?? chatActivityInputAbsent,
output: _output(record, state) ?? chatActivityOutputAbsent,
error: _error(record, state) ?? chatActivityErrorAbsent,
duration: _duration(record, state) ?? chatActivityDurationAbsent,
diff: _diff(record, state) ?? chatActivityDiffAbsent,
);
}
/// The lifecycle state, including a tool-style `state` map's `status`.
String? _lifecycle(JsonMap record, JsonMap state) {
if (record['state'] is! Map) {
final recorded = chatActivityState(record);
if (recorded.isNotEmpty) return recorded;
}
for (final key in const ['status', 'lifecycle']) {
final text = _scalar(record[key]);
if (text != null) return text;
}
for (final key in const ['status', 'state', 'lifecycle']) {
final text = _scalar(state[key]);
if (text != null) return text;
}
return null;
}
String? _input(JsonMap record, JsonMap state) =>
_display(_field(record, state, 'input'));
/// The textual output, either an `output` field or a tool-style `content` list
/// of text parts. A content entry that is not text is not answer output.
String? _output(JsonMap record, JsonMap state) {
final direct = _display(_field(record, state, 'output'));
if (direct != null) return direct;
final content = _field(record, state, 'content');
if (content is! List) return null;
final text = content
.whereType<Map>()
.where((item) => item['type'] == 'text')
.map((item) => '${item['text'] ?? ''}')
.where((item) => item.trim().isNotEmpty)
.join('\n');
return text.isEmpty ? null : text;
}
/// A structured error's message (or type when it has no message), else the
/// recorded error text.
String? _error(JsonMap record, JsonMap state) {
final value = _field(record, state, 'error');
if (value is Map) {
return _display(value['message']) ??
_display(value['type']) ??
_display(value);
}
return _display(value);
}
/// A recorded duration, else one derived from the record's own timing envelope.
String? _duration(JsonMap record, JsonMap state) {
final direct = record['duration'] ?? state['duration'];
if (direct is String && direct.trim().isNotEmpty) return direct;
if (direct is num) return _formatMillis(direct.round());
final millis = record['duration_ms'] ?? state['duration_ms'];
if (millis is num) return _formatMillis(millis.round());
final time = record['time'] ?? state['time'];
if (time is! Map) return null;
final start = _millis(time['ran']) ?? _millis(time['created']);
final completed = _millis(time['completed']);
if (start == null || completed == null || completed < start) return null;
return _formatMillis(completed - start);
}
/// A recorded edit diff, either its text or its line list. A line list keeps the
/// recorded `+`/`-` gutter; no diff is synthesised from an edit call's strings.
String? _diff(JsonMap record, JsonMap state) {
final value = _field(record, state, 'diff');
if (value is String) return value.trim().isEmpty ? null : value;
if (value is! Map) return _display(value);
final lines = value['lines'];
if (lines is List && lines.isNotEmpty) {
final rendered = <String>[];
final file = _display(value['file']);
if (file != null) rendered.add(file);
final added = _int(value['added']);
final removed = _int(value['removed']);
if (added != null || removed != null) {
rendered.add('+${added ?? 0} -${removed ?? 0}');
}
for (final line in lines) {
if (line is Map) {
final text = '${line['text'] ?? ''}';
switch ('${line['kind'] ?? ''}'.toLowerCase()) {
case 'add':
rendered.add('+ $text');
case 'remove':
rendered.add('- $text');
default:
rendered.add(' $text');
}
} else {
rendered.add('$line');
}
}
return rendered.join('\n');
}
return _display(value['text'] ?? value['patch']) ?? _display(value);
}
Object? _field(JsonMap record, JsonMap state, String key) {
final value = record[key];
if (!_absent(value)) return value;
final nested = state[key];
return _absent(nested) ? null : nested;
}
bool _absent(Object? value) =>
value == null ||
(value is String && value.trim().isEmpty) ||
(value is Map && value.isEmpty) ||
(value is List && value.isEmpty);
/// A scalar's exact text or a map/list as formatted, selectable JSON.
String? _display(Object? value) {
if (_absent(value)) return null;
if (value is String) return value;
if (value is Map || value is List) {
return const JsonEncoder.withIndent(' ').convert(value);
}
return '$value';
}
/// A scalar's text; a structured value is not a lifecycle state.
String? _scalar(Object? value) =>
value is Map || value is List ? null : _display(value);
int? _millis(Object? value) => value is num ? value.toInt() : null;
int? _int(Object? value) => value is num ? value.toInt() : null;
/// The compact duration wording shared with the web's `formatToolDuration`.
String _formatMillis(int millis) {
if (millis < 1000) return '${millis}ms';
if (millis < 60000) return '${(millis / 1000).toStringAsFixed(1)}s';
return '${millis ~/ 60000}m ${(millis % 60000) ~/ 1000}s';
}
@@ -174,6 +174,7 @@ void main() {
Future<FakeChatGateway> pumpThread(
WidgetTester tester, {
required bool cupertino,
List<JsonMap>? activityParts,
}) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
@@ -188,15 +189,17 @@ void main() {
chatMessage(
'a1',
'assistant',
parts: [
{
'type': 'activity',
'id': 'e1',
'kind': 'compaction',
'state': 'running',
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
],
parts:
activityParts ??
[
{
'type': 'activity',
'id': 'e1',
'kind': 'compaction',
'state': 'running',
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
],
),
]);
final view = ChatThreadView(
@@ -236,5 +239,125 @@ void main() {
expect(find.text('Answer'), findsOneWidget);
expect(find.textContaining('Unrecognised part'), findsNothing);
});
group('expanded detail', () {
final recorded = <JsonMap>[
{
'type': 'activity',
'id': 'e1',
'kind': 'tool',
'state': 'completed',
'label': 'Ran a tool',
'input': {'command': 'ls'},
'output': 'two files',
'error': 'exit 1',
'duration': 1200,
'diff': {
'file': 'lib/main.dart',
'added': 1,
'removed': 1,
'lines': [
{'kind': 'remove', 'text': 'old'},
{'kind': 'add', 'text': 'new'},
],
},
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
];
final sparse = <JsonMap>[
{
'type': 'activity',
'id': 'e1',
'kind': 'compaction',
'state': 'running',
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
];
Future<void> toggle(WidgetTester tester) async {
final control = find.byKey(
const ValueKey<String>('chat-activity-toggle'),
);
await tester.ensureVisible(control);
await tester.tap(control);
await tester.pumpAndSettle();
}
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host shows only the recorded detail values', (
tester,
) async {
await pumpThread(
tester,
cupertino: cupertino,
activityParts: recorded,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsOneWidget,
);
expect(find.text('Lifecycle'), findsOneWidget);
expect(find.text('completed'), findsOneWidget);
expect(find.textContaining('"command": "ls"'), findsOneWidget);
expect(find.text('two files'), findsOneWidget);
expect(find.text('exit 1'), findsOneWidget);
expect(find.text('1.2s'), findsOneWidget);
expect(
find.text('lib/main.dart\n+1 -1\n- old\n+ new'),
findsOneWidget,
);
for (final absent in const [
'No lifecycle recorded.',
'No input recorded.',
'No output recorded.',
'No error recorded.',
'No duration recorded.',
'No edit diff recorded.',
]) {
expect(find.text(absent), findsNothing, reason: absent);
}
});
testWidgets('$host words every absent field exactly', (tester) async {
await pumpThread(tester, cupertino: cupertino, activityParts: sparse);
await toggle(tester);
expect(find.text('running'), findsOneWidget);
expect(find.text('No input recorded.'), findsOneWidget);
expect(find.text('No output recorded.'), findsOneWidget);
expect(find.text('No error recorded.'), findsOneWidget);
expect(find.text('No duration recorded.'), findsOneWidget);
expect(find.text('No edit diff recorded.'), findsOneWidget);
expect(find.text('No lifecycle recorded.'), findsNothing);
});
testWidgets('$host collapses back to the single line', (tester) async {
await pumpThread(
tester,
cupertino: cupertino,
activityParts: recorded,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsOneWidget,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsNothing,
);
expect(find.text('Input'), findsNothing);
expect(find.text('Ran a tool'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
});
}
});
});
}