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.
139 lines
4.2 KiB
Dart
139 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'chat_bar_control_style.dart';
|
|
import 'chat_model_chooser.dart';
|
|
import 'chat_models.dart';
|
|
|
|
/// The conversation header's model control.
|
|
///
|
|
/// It shows the opened row's recorded model (with the web's next-turn suffix)
|
|
/// and opens the harness's own catalog in a native chooser. Choosing an entry
|
|
/// sends the web's `chat_model` request. A refusal leaves this control's model
|
|
/// untouched and reports the reason in the chooser; an acceptance adopts the
|
|
/// server's echoed model instead of the row that was tapped and reports it, so
|
|
/// the destination's opened row keeps up with the server.
|
|
class ChatModelControl extends StatefulWidget {
|
|
const ChatModelControl({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.sessionId,
|
|
required this.session,
|
|
this.onChanged,
|
|
this.showLabel = false,
|
|
this.answeredBy = '',
|
|
});
|
|
|
|
/// The model the latest reply recorded, shown when the session itself has
|
|
/// no model on record.
|
|
final String answeredBy;
|
|
|
|
final bool showLabel;
|
|
final ApiGateway gateway;
|
|
final String sessionId;
|
|
|
|
/// The opened row's recorded facts: `backend`, `model`, `model_provider` and
|
|
/// `model_selection`.
|
|
final JsonMap session;
|
|
|
|
/// The server's echo after an accepted switch.
|
|
final ValueChanged<ChatModelSelection>? onChanged;
|
|
|
|
@override
|
|
State<ChatModelControl> createState() => _ChatModelControlState();
|
|
}
|
|
|
|
class _ChatModelControlState extends State<ChatModelControl> {
|
|
late String _provider;
|
|
late String _model;
|
|
late String _selection;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_adopt(widget.session);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatModelControl oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
// The destination replaces the opened row after an accepted switch (and may
|
|
// re-read the list), so a changed row is adopted instead of only read once.
|
|
if (chatModelRecordedFacts(oldWidget.session) !=
|
|
chatModelRecordedFacts(widget.session)) {
|
|
_adopt(widget.session);
|
|
}
|
|
}
|
|
|
|
void _adopt(JsonMap session) {
|
|
final facts = chatModelRecordedFacts(session);
|
|
_provider = facts.provider;
|
|
_model = facts.model;
|
|
_selection = facts.selection;
|
|
}
|
|
|
|
Future<void> _open() async {
|
|
final chooser = ChatModelChooser(
|
|
gateway: widget.gateway,
|
|
sessionId: widget.sessionId,
|
|
backend: '${widget.session['backend'] ?? ''}'.trim(),
|
|
currentProvider: _provider,
|
|
currentModel: _model,
|
|
cupertino: false,
|
|
onAccepted: _accept,
|
|
);
|
|
await showDialog<void>(context: context, builder: (_) => chooser);
|
|
}
|
|
|
|
void _accept(ChatModelSelection applied) {
|
|
// The server has applied the switch, so the destination is told even if
|
|
// this conversation was left while the request was in flight.
|
|
widget.onChanged?.call(applied);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_provider = applied.provider;
|
|
_model = applied.model;
|
|
_selection = applied.selection;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final backend = '${widget.session['backend'] ?? ''}'.trim();
|
|
final label = _model.isNotEmpty
|
|
? chatModelLabel(_model, _selection)
|
|
: widget.answeredBy.isNotEmpty
|
|
? widget.answeredBy
|
|
: '${backend.isEmpty ? 'opencode' : backend} default';
|
|
final compact = !widget.showLabel && MediaQuery.sizeOf(context).width < 600;
|
|
final content = compact
|
|
? const Icon(Icons.memory_outlined)
|
|
: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Flexible(child: Text(label, overflow: TextOverflow.ellipsis)),
|
|
const SizedBox(width: 4),
|
|
const Icon(Icons.expand_more),
|
|
],
|
|
);
|
|
const key = Key('chat-model');
|
|
return Semantics(
|
|
container: true,
|
|
button: true,
|
|
label: 'Change model, currently $label',
|
|
onTap: _open,
|
|
child: ExcludeSemantics(
|
|
child: Tooltip(
|
|
message: 'Change model (currently $label)',
|
|
child: TextButton(
|
|
key: key,
|
|
style: chatBarTextButtonStyle(context, compact: compact),
|
|
onPressed: _open,
|
|
child: content,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|