Make chat reasoning a footer chip that opens a live sheet like tools
This commit is contained in:
@@ -7,7 +7,7 @@ import '../../../../core/api_gateway.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import '../../domain/chat_tool_calls.dart';
|
||||
import 'chat_tool_calls_view.dart';
|
||||
import 'chat_reasoning_disclosure.dart';
|
||||
import 'chat_reasoning_chip.dart';
|
||||
import 'native_disclosure.dart';
|
||||
|
||||
/// Builds an assistant text part; the default keeps the server's text exactly.
|
||||
@@ -26,8 +26,8 @@ class ChatMessageView extends StatelessWidget {
|
||||
final JsonMap row;
|
||||
final ChatMessageTextBuilder? textBuilder;
|
||||
|
||||
/// More of an agent turn's summary chips, after its tools chip (the turn's
|
||||
/// settled permission requests).
|
||||
/// More of an agent turn's summary chips, after its reasoning and tools chips
|
||||
/// (the turn's settled permission requests).
|
||||
final List<Widget> footer;
|
||||
|
||||
@override
|
||||
@@ -198,7 +198,9 @@ class ChatMessageView extends StatelessWidget {
|
||||
return widgets;
|
||||
}
|
||||
|
||||
/// Reasoning stays in the transcript so an open body follows live deltas.
|
||||
/// The turn's footer: its reasoning, tools and permissions chips, then its
|
||||
/// model. Each chip opens a native sheet; an open reasoning sheet follows
|
||||
/// live deltas.
|
||||
Widget _agentTurn(BuildContext context, List<Widget> content, Widget bubble) {
|
||||
final parts = _partsOf(row);
|
||||
final reasoning = [
|
||||
@@ -213,6 +215,12 @@ class ChatMessageView extends StatelessWidget {
|
||||
];
|
||||
final model = '${row['model'] ?? ''}'.trim();
|
||||
final footer = <Widget>[
|
||||
if (reasoning.isNotEmpty)
|
||||
ChatReasoningChip(
|
||||
key: const Key('chat-turn-reasoning'),
|
||||
steps: reasoning,
|
||||
streaming: row['streaming'] == true && _reasoningIsNewest(parts),
|
||||
),
|
||||
if (tools.isNotEmpty) ChatToolCallsChip(calls: tools),
|
||||
...this.footer,
|
||||
if (model.isNotEmpty)
|
||||
@@ -230,15 +238,9 @@ class ChatMessageView extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (content.isNotEmpty) bubble,
|
||||
if (reasoning.isNotEmpty)
|
||||
ChatReasoningDisclosure(
|
||||
key: const Key('chat-turn-reasoning'),
|
||||
steps: reasoning,
|
||||
streaming: row['streaming'] == true,
|
||||
trailing: footer,
|
||||
),
|
||||
if (reasoning.isEmpty && footer.isNotEmpty)
|
||||
if (footer.isNotEmpty)
|
||||
Wrap(
|
||||
key: const Key('chat-message-footer'),
|
||||
spacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: footer,
|
||||
@@ -337,6 +339,21 @@ class ChatMessageView extends StatelessWidget {
|
||||
SelectableText(const JsonEncoder.withIndent(' ').convert(part));
|
||||
}
|
||||
|
||||
/// Whether the turn's newest drawn part is reasoning: while it streams, that
|
||||
/// is the step still arriving. Once an answer or a tool call starts after it,
|
||||
/// the reasoning chip stops spinning (a running tool spins its own chip).
|
||||
bool _reasoningIsNewest(List<JsonMap> parts) {
|
||||
for (final part in parts.reversed) {
|
||||
final type = part['type'];
|
||||
if (type == 'tool') return false;
|
||||
if ((type == 'text' || type == 'reasoning') &&
|
||||
'${part['text'] ?? ''}'.trim().isNotEmpty) {
|
||||
return type == 'reasoning';
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
List<JsonMap> _partsOf(Map<dynamic, dynamic> source) {
|
||||
final parts = source['parts'];
|
||||
if (parts is! List) return const [];
|
||||
|
||||
@@ -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),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
|
||||
/// Only reasoning actually supplied by the backend. Keeping its body in the
|
||||
/// transcript (rather than a modal snapshot) lets an open disclosure rebuild
|
||||
/// with every delta, even while answer text is also arriving.
|
||||
class ChatReasoningDisclosure extends StatefulWidget {
|
||||
const ChatReasoningDisclosure({
|
||||
super.key,
|
||||
required this.steps,
|
||||
required this.streaming,
|
||||
this.trailing = const [],
|
||||
});
|
||||
|
||||
final List<String> steps;
|
||||
final bool streaming;
|
||||
final List<Widget> trailing;
|
||||
|
||||
@override
|
||||
State<ChatReasoningDisclosure> createState() =>
|
||||
_ChatReasoningDisclosureState();
|
||||
}
|
||||
|
||||
class _ChatReasoningDisclosureState extends State<ChatReasoningDisclosure> {
|
||||
late bool _expanded = widget.streaming;
|
||||
|
||||
void _toggle() => setState(() => _expanded = !_expanded);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final label = widget.steps.length == 1
|
||||
? 'Reasoning'
|
||||
: 'Reasoning · ${widget.steps.length} steps';
|
||||
final control = Semantics(
|
||||
key: const Key('chat-reasoning-toggle'),
|
||||
button: true,
|
||||
expanded: _expanded,
|
||||
label: label,
|
||||
onTap: _toggle,
|
||||
excludeSemantics: true,
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
|
||||
minimumSize: Size.zero,
|
||||
onPressed: _toggle,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(child: Text(label)),
|
||||
Icon(
|
||||
_expanded
|
||||
? CupertinoIcons.chevron_up
|
||||
: CupertinoIcons.chevron_down,
|
||||
size: 14,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: _toggle,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(child: Text(label)),
|
||||
Icon(
|
||||
_expanded ? Icons.expand_less : Icons.expand_more,
|
||||
size: 14,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
return DefaultTextStyle.merge(
|
||||
style: cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyMedium,
|
||||
child: Column(
|
||||
key: const Key('chat-reasoning-disclosure'),
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Wrap(
|
||||
key: const Key('chat-message-footer'),
|
||||
spacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [control, ...widget.trailing],
|
||||
),
|
||||
if (_expanded)
|
||||
for (final (index, step) in widget.steps.indexed)
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: index == 0 ? 0 : 12, bottom: 8),
|
||||
child: SelectableText(step),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user