157 lines
4.5 KiB
Dart
157 lines
4.5 KiB
Dart
// The model / agent picker, shared by the chat's app-bar chip and the
|
|||
|
|
// new-session draft (where it chooses what the session will start on).
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
|
||
|
|
import '../api/models.dart';
|
||
|
|
import '../state.dart';
|
||
|
|
|
||
|
|
Future<void> showModelPickerSheet(
|
||
|
|
BuildContext context, {
|
||
|
|
required AppState app,
|
||
|
|
required String kind,
|
||
|
|
required ModelRef? model,
|
||
|
|
required String agent,
|
||
|
|
required ValueChanged<ModelRef> onModel,
|
||
|
|
required ValueChanged<String> onAgent,
|
||
|
|
}) {
|
||
|
|
app.refreshModels();
|
||
|
|
return showModalBottomSheet<void>(
|
||
|
|
context: context,
|
||
|
|
isScrollControlled: true,
|
||
|
|
builder: (context) => DraggableScrollableSheet(
|
||
|
|
expand: false,
|
||
|
|
initialChildSize: 0.8,
|
||
|
|
maxChildSize: 0.92,
|
||
|
|
builder: (context, controller) => _PickerSheet(
|
||
|
|
app: app,
|
||
|
|
kind: kind,
|
||
|
|
model: model,
|
||
|
|
agent: agent,
|
||
|
|
scrollController: controller,
|
||
|
|
onModel: onModel,
|
||
|
|
onAgent: onAgent,
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
class _PickerSheet extends StatefulWidget {
|
||
|
|
final AppState app;
|
||
|
|
final String kind;
|
||
|
|
final ModelRef? model;
|
||
|
|
final String agent;
|
||
|
|
final ScrollController scrollController;
|
||
|
|
final ValueChanged<ModelRef> onModel;
|
||
|
|
final ValueChanged<String> onAgent;
|
||
|
|
|
||
|
|
const _PickerSheet({
|
||
|
|
required this.app,
|
||
|
|
required this.kind,
|
||
|
|
required this.model,
|
||
|
|
required this.agent,
|
||
|
|
required this.scrollController,
|
||
|
|
required this.onModel,
|
||
|
|
required this.onAgent,
|
||
|
|
});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<_PickerSheet> createState() => _PickerSheetState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _PickerSheetState extends State<_PickerSheet> {
|
||
|
|
late String _kind = widget.kind;
|
||
|
|
String _filter = '';
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
return ListenableBuilder(
|
||
|
|
listenable: widget.app,
|
||
|
|
builder: (context, _) => Column(
|
||
|
|
children: [
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
||
|
|
child: Text(_kind == 'agent' ? '选择智能体' : '选择模型',
|
||
|
|
style: const TextStyle(
|
||
|
|
fontSize: 18, fontWeight: FontWeight.bold)),
|
||
|
|
),
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||
|
|
child: SegmentedButton<String>(
|
||
|
|
segments: const [
|
||
|
|
ButtonSegment(value: 'model', label: Text('模型')),
|
||
|
|
ButtonSegment(value: 'agent', label: Text('智能体')),
|
||
|
|
],
|
||
|
|
selected: {_kind},
|
||
|
|
showSelectedIcon: false,
|
||
|
|
onSelectionChanged: (s) {
|
||
|
|
setState(() => _kind = s.first);
|
||
|
|
widget.app.refreshModels();
|
||
|
|
},
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.all(12),
|
||
|
|
child: TextField(
|
||
|
|
onChanged: (v) => setState(() => _filter = v),
|
||
|
|
decoration: const InputDecoration(
|
||
|
|
hintText: '搜索',
|
||
|
|
border: OutlineInputBorder(),
|
||
|
|
isDense: true),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Expanded(child: _items()),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Widget _items() {
|
||
|
|
final filter = _filter.toLowerCase();
|
||
|
|
// (key, title, modelOrAgent, selected)
|
||
|
|
final items = <(String, String, Object?, bool)>[];
|
||
|
|
if (_kind == 'model') {
|
||
|
|
for (final m in widget.app.modelList) {
|
||
|
|
final selected = widget.model != null &&
|
||
|
|
widget.model!.providerId == m.providerId &&
|
||
|
|
widget.model!.id == m.id;
|
||
|
|
items.add((
|
||
|
|
'${m.providerId}/${m.id}',
|
||
|
|
m.name,
|
||
|
|
ModelRef(m.id, m.providerId),
|
||
|
|
selected
|
||
|
|
));
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
for (final a in widget.app.agentList) {
|
||
|
|
// Compaction, Title and Summary are primary but hidden.
|
||
|
|
if (a.mode != 'primary' || a.hidden) continue;
|
||
|
|
items.add(('agent/${a.id}', a.name, a.id, widget.agent == a.id));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
final visible = items
|
||
|
|
.where((e) => filter.isEmpty || e.$2.toLowerCase().contains(filter))
|
||
|
|
.toList();
|
||
|
|
if (visible.isEmpty) {
|
||
|
|
return const Center(child: Text('没有结果'));
|
||
|
|
}
|
||
|
|
return ListView.builder(
|
||
|
|
controller: widget.scrollController,
|
||
|
|
itemCount: visible.length,
|
||
|
|
itemBuilder: (context, i) {
|
||
|
|
final it = visible[i];
|
||
|
|
return ListTile(
|
||
|
|
title: Text(it.$2),
|
||
|
|
trailing: it.$4 ? const Icon(Icons.check) : null,
|
||
|
|
onTap: () {
|
||
|
|
Navigator.pop(context);
|
||
|
|
if (it.$3 is ModelRef) {
|
||
|
|
widget.onModel(it.$3 as ModelRef);
|
||
|
|
} else {
|
||
|
|
widget.onAgent(it.$3 as String);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|