Files
pipelines/app/lib/features/chat/ui/transcript/chat_reasoning_disclosure.dart
T

102 lines
3.1 KiB
Dart

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),
),
],
),
);
}
}