96de9f5bea
Keep the Material-only conversation implementation while preserving public Cupertino parameters for callers. Narrow affected tests to Material hosts so the dead platform paths are no longer exercised.
186 lines
5.9 KiB
Dart
186 lines
5.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../../../core/error_reason.dart';
|
|
import '../../../ui/adaptive/app_menu_anchor.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) return;
|
|
try {
|
|
await _choose(level);
|
|
} 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 AppMenuAnchor(
|
|
menuChildren: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(12, 8, 12, 4),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 280),
|
|
child: Text(
|
|
chatEffortNote(running: widget.running),
|
|
key: const Key('chat-effort-note'),
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
),
|
|
for (final level in _state.levels)
|
|
MenuItemButton(
|
|
key: ValueKey<String>('chat-effort-option-${level.id}'),
|
|
trailingIcon: level.id == _state.current
|
|
? const Icon(Icons.check)
|
|
: null,
|
|
onPressed: () => 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,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
builder: (context, menu, _) => Semantics(
|
|
container: true,
|
|
button: true,
|
|
label: 'Change thinking level, currently ${_state.currentName}',
|
|
onTap: () => menu.isOpen ? menu.close() : menu.open(),
|
|
child: ExcludeSemantics(
|
|
child: Tooltip(
|
|
message: 'Change thinking level (currently ${_state.currentName})',
|
|
child: TextButton(
|
|
key: key,
|
|
style: chatBarTextButtonStyle(context),
|
|
onPressed: () => menu.isOpen ? menu.close() : menu.open(),
|
|
child: content,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|