Files
pipelines/app/lib/features/chat/conversation/chat_effort_sheet.dart
T
alan b57405df29 Show a thinking-level control beside the chat model and mode
Material menu or Cupertino action sheet of the levels the chat's model
accepts, sent with chat_effort through the chat's own gateway. Hidden when
the model has none; says when a running turn keeps its level; re-reads the
levels after a model switch.
2026-11-09 20:16:13 +00:00

102 lines
2.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import '../../../core/error_reason.dart';
import 'chat_efforts.dart';
/// The Cupertino action sheet of a chat's thinking levels, the current one
/// checked. It stays open with the server's reason when a choice is refused
/// and closes once one is accepted.
class ChatEffortSheet extends StatefulWidget {
const ChatEffortSheet({
super.key,
required this.state,
required this.running,
required this.choose,
});
final ChatEffortState state;
final bool running;
final Future<void> Function(ChatEffortLevel level) choose;
@override
State<ChatEffortSheet> createState() => _ChatEffortSheetState();
}
class _ChatEffortSheetState extends State<ChatEffortSheet> {
bool _busy = false;
String? _error;
Future<void> _choose(ChatEffortLevel level) async {
if (_busy) return;
if (level.id == widget.state.effort) {
Navigator.of(context).pop();
return;
}
setState(() {
_busy = true;
_error = null;
});
try {
await widget.choose(level);
if (mounted) Navigator.of(context).pop();
} catch (error) {
if (!mounted) return;
setState(() {
_busy = false;
_error = describeErrorReason(
error,
fallback: 'The thinking level was not changed.',
);
});
}
}
@override
Widget build(BuildContext context) {
final secondary = CupertinoColors.secondaryLabel.resolveFrom(context);
final error = _error;
return CupertinoActionSheet(
key: const Key('chat-effort-sheet'),
title: const Text('Thinking'),
message: Text(
error ?? chatEffortNote(running: widget.running),
key: const Key('chat-effort-note'),
style: error == null
? null
: TextStyle(color: CupertinoColors.systemRed.resolveFrom(context)),
),
actions: [
for (final level in widget.state.levels)
CupertinoActionSheetAction(
key: ValueKey<String>('chat-effort-option-${level.id}'),
onPressed: () => _choose(level),
child: Column(
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: Text(level.name)),
if (level.id == widget.state.current) ...[
const SizedBox(width: 6),
const Icon(CupertinoIcons.checkmark_alt),
],
],
),
if (level.description.isNotEmpty)
Text(
level.description,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13, color: secondary),
),
],
),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
);
}
}