fdc730422d
Phase 2.3 and 2.4 of docs/plans/floating-windows.md. A floating chat is a window of the chat kind over the app's WindowManager, built from the app-lifetime ChatRuntime, so it shows over every destination and at start without the Chat destination being built. Float and Dock move the same keyed view between the tiling and the window host. The pipelines app keeps one runtime and one ChatWindows; saved chatFloat records (v1 and v2) are brought over once. ChatFloatHost, ChatFloatWindow, the float frames and binding, the controller's float list and FloatingFocusRoot are gone; the workspace only asks whether a chat is out in a window. Every MenuAnchor targets the root overlay (guarded by a test), and menus and tooltips name the Alt chords.
218 lines
6.8 KiB
Dart
218 lines
6.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../../../core/error_reason.dart';
|
|
import '../ui/sessions/chat_adaptive.dart';
|
|
import 'chat_bar_control_style.dart';
|
|
import 'chat_effort_sheet.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. Material opens a menu and Cupertino an action sheet 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.',
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _openSheet() => showCupertinoModalPopup<void>(
|
|
context: context,
|
|
builder: (_) => ChatEffortSheet(
|
|
state: _state,
|
|
running: widget.running,
|
|
choose: _choose,
|
|
),
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!_state.supported) return const SizedBox.shrink();
|
|
final cupertino =
|
|
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
|
final label =
|
|
'${chatEffortLabel(_state)}${_nextTurn ? ' · next turn' : ''}';
|
|
final iconOnly =
|
|
!widget.showLabel && MediaQuery.sizeOf(context).width < 600;
|
|
final content = Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(cupertino ? CupertinoIcons.lightbulb : Icons.psychology_outlined),
|
|
if (!iconOnly) ...[
|
|
const SizedBox(width: 4),
|
|
Flexible(child: Text(label, overflow: TextOverflow.ellipsis)),
|
|
],
|
|
],
|
|
);
|
|
const key = Key('chat-effort');
|
|
if (cupertino) {
|
|
return Semantics(
|
|
container: true,
|
|
button: true,
|
|
label: 'Change thinking level, currently ${_state.currentName}',
|
|
onTap: _openSheet,
|
|
child: ExcludeSemantics(
|
|
child: CupertinoButton(
|
|
key: key,
|
|
padding: chatBarButtonPadding,
|
|
onPressed: _openSheet,
|
|
child: chatBarCupertinoContent(context, content),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return MenuAnchor(
|
|
// Above every floating window, and not clipped to one.
|
|
useRootOverlay: true,
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|