Make chat reasoning a footer chip that opens a live sheet like tools
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../sessions/chat_adaptive.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 cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final label = chatReasoningLabel(widget.steps.length);
|
||||
return ChatTurnChip(
|
||||
chipKey: const Key('chat-reasoning-chip'),
|
||||
leading: widget.streaming
|
||||
? _spinner(cupertino)
|
||||
: Icon(
|
||||
cupertino ? CupertinoIcons.lightbulb : 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(bool cupertino) => cupertino
|
||||
? const CupertinoActivityIndicator(radius: 7)
|
||||
: 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 cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final style = cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyMedium;
|
||||
final secondary = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
return DefaultTextStyle.merge(
|
||||
style: style,
|
||||
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(cupertino),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
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),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user