2d0e33dd67
Use the permission selector's shared popup and choice-row primitives for model and thinking controls while preserving catalog, search, and next-turn behavior. Remove the obsolete Cupertino model chooser parameter and cover current-model dismissal.
195 lines
6.0 KiB
Dart
195 lines
6.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../../../core/error_reason.dart';
|
|
import '../../../ui/adaptive/app_popups.dart';
|
|
import 'chat_bar_control_style.dart';
|
|
import 'chat_efforts.dart';
|
|
import 'chat_models.dart';
|
|
|
|
/// The conversation bar's thinking-level control ("Thinking: High").
|
|
///
|
|
/// It shows the levels the session row names for the chat's model without a
|
|
/// request of its own. It opens a menu of those levels; choosing one sends `chat_effort` through the chat's own
|
|
/// gateway. The level applies from the next turn: while a turn runs, the
|
|
/// choices say so and the label marks the new level "next turn". A model with
|
|
/// no levels (or a harness without them) shows no control. After the model
|
|
/// changes, the levels are re-read from that model's `chat_models` reply.
|
|
class ChatEffortControl extends StatefulWidget {
|
|
const ChatEffortControl({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.sessionId,
|
|
required this.session,
|
|
this.running = false,
|
|
this.onChanged,
|
|
this.showLabel = false,
|
|
});
|
|
|
|
final ApiGateway gateway;
|
|
final String sessionId;
|
|
|
|
/// The opened row: `effort`, `efforts`, `default_effort` and its model.
|
|
final JsonMap session;
|
|
|
|
/// Whether the chat's turn is running now.
|
|
final bool running;
|
|
|
|
/// The server's echo after an accepted level.
|
|
final ValueChanged<ChatEffortState>? onChanged;
|
|
final bool showLabel;
|
|
|
|
@override
|
|
State<ChatEffortControl> createState() => _ChatEffortControlState();
|
|
}
|
|
|
|
class _ChatEffortControlState extends State<ChatEffortControl> {
|
|
late ChatEffortState _state = ChatEffortState.fromJson(widget.session);
|
|
bool _nextTurn = false;
|
|
int _reads = 0;
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatEffortControl oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (!widget.running) _nextTurn = false;
|
|
final row = ChatEffortState.fromJson(widget.session);
|
|
if (row.signature !=
|
|
ChatEffortState.fromJson(oldWidget.session).signature) {
|
|
_state = row;
|
|
} else if (chatModelRecordedFacts(oldWidget.session) !=
|
|
chatModelRecordedFacts(widget.session)) {
|
|
unawaited(_reread());
|
|
}
|
|
}
|
|
|
|
/// A model switch keeps the row's old levels; ask the server for the new
|
|
/// model's. A refusal keeps the shown levels rather than inventing any.
|
|
Future<void> _reread() async {
|
|
final read = ++_reads;
|
|
try {
|
|
final state = await readChatEffort(widget.gateway, widget.sessionId);
|
|
if (!mounted || read != _reads) return;
|
|
setState(() => _state = state);
|
|
widget.onChanged?.call(state);
|
|
} catch (_) {}
|
|
}
|
|
|
|
Future<void> _choose(ChatEffortLevel level) async {
|
|
final running = widget.running;
|
|
final applied = await selectChatEffort(
|
|
widget.gateway,
|
|
widget.sessionId,
|
|
level.id,
|
|
);
|
|
widget.onChanged?.call(applied);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_state = applied;
|
|
_nextTurn = running;
|
|
});
|
|
}
|
|
|
|
Future<void> _chooseFromMenu(ChatEffortLevel level) async {
|
|
if (level.id == _state.effort || level.id == _state.current) {
|
|
Navigator.of(context).pop();
|
|
return;
|
|
}
|
|
try {
|
|
await _choose(level);
|
|
if (mounted) Navigator.of(context).pop();
|
|
} catch (error) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.maybeOf(context)?.showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
describeErrorReason(
|
|
error,
|
|
fallback: 'The thinking level was not changed.',
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!_state.supported) return const SizedBox.shrink();
|
|
final label =
|
|
'${chatEffortLabel(_state)}${_nextTurn ? ' · next turn' : ''}';
|
|
final iconOnly =
|
|
!widget.showLabel && MediaQuery.sizeOf(context).width < 600;
|
|
final content = Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.psychology_outlined),
|
|
if (!iconOnly) ...[
|
|
const SizedBox(width: 4),
|
|
Flexible(child: Text(label, overflow: TextOverflow.ellipsis)),
|
|
],
|
|
],
|
|
);
|
|
const key = Key('chat-effort');
|
|
return Semantics(
|
|
container: true,
|
|
button: true,
|
|
label: 'Change thinking level, currently ${_state.currentName}',
|
|
onTap: _open,
|
|
child: ExcludeSemantics(
|
|
child: Tooltip(
|
|
message: 'Change thinking level (currently ${_state.currentName})',
|
|
child: TextButton(
|
|
key: key,
|
|
style: chatBarTextButtonStyle(context, compact: iconOnly),
|
|
onPressed: _open,
|
|
child: content,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _open() async {
|
|
await showAppChoiceMenu<void>(
|
|
context,
|
|
width: 320,
|
|
semanticLabel: 'Change thinking level',
|
|
builder: (_) => ListView(
|
|
key: const Key('chat-effort-menu'),
|
|
shrinkWrap: true,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
|
child: Text(
|
|
chatEffortNote(running: widget.running),
|
|
key: const Key('chat-effort-note'),
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
for (final level in _state.levels)
|
|
AppChoiceMenuItem(
|
|
key: ValueKey<String>('chat-effort-option-${level.id}'),
|
|
selected: level.id == _state.current,
|
|
onTap: () => unawaited(_chooseFromMenu(level)),
|
|
child: level.description.isEmpty
|
|
? Text(level.name)
|
|
: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(level.name),
|
|
Text(
|
|
level.description,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|