Files
alan fbe1928f59 Remove Cupertino chat transcript branches
Keep the existing Material transcript path while preserving public Cupertino parameters for callers. Narrow transcript tests to the Material-only behavior and remove Cupertino-only fixtures.
2026-11-11 11:46:13 +00:00

174 lines
5.2 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'chat_turn_chip.dart';
/// `Reasoning` / `Reasoning · N steps`.
String chatReasoningLabel(int steps) =>
steps == 1 ? 'Reasoning' : 'Reasoning · $steps steps';
/// A turn's reasoning as the first chip of its footer, the same pattern as its
/// tools and permissions: it spins while the turn streams and opens a native
/// sheet with every step. Only reasoning the backend supplied is shown.
///
/// The open sheet follows live deltas: this state owns the steps the sheet
/// listens to and updates them on every rebuild of the turn.
class ChatReasoningChip extends StatefulWidget {
const ChatReasoningChip({
super.key,
required this.steps,
required this.streaming,
});
final List<String> steps;
final bool streaming;
@override
State<ChatReasoningChip> createState() => _ChatReasoningChipState();
}
typedef _Reasoning = ({List<String> steps, bool streaming});
class _ChatReasoningChipState extends State<ChatReasoningChip> {
late final _live = _LiveReasoning((
steps: widget.steps,
streaming: widget.streaming,
));
@override
void didUpdateWidget(ChatReasoningChip oldWidget) {
super.didUpdateWidget(oldWidget);
final current = _live.value;
if (widget.streaming == current.streaming &&
listEquals(widget.steps, current.steps)) {
return;
}
// The sheet lives in another route, outside this subtree, so it may only
// be marked dirty once this build is over; the next frame shows the delta.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
_live.value = (steps: widget.steps, streaming: widget.streaming);
});
}
@override
void dispose() {
_live.release();
super.dispose();
}
@override
Widget build(BuildContext context) {
final color = Theme.of(context).colorScheme.onSurfaceVariant;
final label = chatReasoningLabel(widget.steps.length);
return ChatTurnChip(
chipKey: const Key('chat-reasoning-chip'),
leading: widget.streaming
? _spinner()
: Icon(Icons.lightbulb_outline, size: 14, color: color),
label: label,
color: color,
semanticsLabel: '$label, expand for details',
sheet: ValueListenableBuilder<_Reasoning>(
valueListenable: _live,
builder: (context, reasoning, _) => _ReasoningSheet(
steps: reasoning.steps,
streaming: reasoning.streaming,
),
),
);
}
}
Widget _spinner() => const SizedBox.square(
dimension: 12,
child: CircularProgressIndicator(strokeWidth: 2),
);
/// The steps an open sheet listens to. The turn can leave the tree while its
/// sheet is still open (its row scrolled away or replaced), so the notifier is
/// disposed only once its owner released it and no sheet listens any more.
class _LiveReasoning extends ValueNotifier<_Reasoning> {
_LiveReasoning(super.value);
bool _released = false;
bool _disposed = false;
void release() {
_released = true;
_disposeIfUnused();
}
@override
void removeListener(VoidCallback listener) {
super.removeListener(listener);
_disposeIfUnused();
}
void _disposeIfUnused() {
if (!_released || _disposed || hasListeners) return;
_disposed = true;
dispose();
}
}
class _ReasoningSheet extends StatelessWidget {
const _ReasoningSheet({required this.steps, required this.streaming});
final List<String> steps;
final bool streaming;
@override
Widget build(BuildContext context) {
final secondary = Theme.of(context).colorScheme.onSurfaceVariant;
return DefaultTextStyle.merge(
style: Theme.of(context).textTheme.bodyMedium,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.8,
),
child: ListView(
key: const Key('chat-reasoning-sheet'),
shrinkWrap: true,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
children: [
Flexible(
child: Text(
chatReasoningLabel(steps.length),
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
if (streaming) ...[const SizedBox(width: 8), _spinner()],
],
),
),
for (final (index, step) in steps.indexed)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (steps.length > 1)
Text(
'STEP ${index + 1}',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: secondary,
),
),
SelectableText(step),
],
),
),
],
),
),
);
}
}