Files
pipelines/app/lib/features/chat/conversation/chat_model_control.dart
T

149 lines
4.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../ui/sessions/chat_adaptive.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.answeredBy = '',
});
/// The model the latest reply recorded, shown when the session itself has
/// no model on record.
final String answeredBy;
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 cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final chooser = ChatModelChooser(
gateway: widget.gateway,
sessionId: widget.sessionId,
backend: '${widget.session['backend'] ?? ''}'.trim(),
currentProvider: _provider,
currentModel: _model,
cupertino: cupertino,
onAccepted: _accept,
);
if (cupertino) {
await showCupertinoModalPopup<void>(
context: context,
builder: (_) => chooser,
);
return;
}
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 cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final backend = '${widget.session['backend'] ?? ''}'.trim();
final label = _model.isNotEmpty
? chatModelLabel(_model, _selection)
: widget.answeredBy.isNotEmpty
? widget.answeredBy
: '${backend.isEmpty ? 'opencode' : backend} default';
final content = Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: Text(label, overflow: TextOverflow.ellipsis)),
const SizedBox(width: 4),
Icon(cupertino ? CupertinoIcons.chevron_down : Icons.expand_more),
],
);
const key = Key('chat-model');
if (cupertino) {
return Semantics(
container: true,
button: true,
label: 'Change model, currently $label',
onTap: _open,
child: ExcludeSemantics(
child: CupertinoButton(
key: key,
padding: const EdgeInsets.symmetric(horizontal: 8),
onPressed: _open,
child: content,
),
),
);
}
return Tooltip(
message: 'Change model (currently $label)',
child: TextButton(key: key, onPressed: _open, child: content),
);
}
}