2026-11-21 17:03:22 +08:00
|
|
|
|
// The model / agent picker, shared by the chat composer and the new-session
|
|
|
|
|
|
// draft: favourites, recents, models grouped by provider with their context
|
|
|
|
|
|
// size, and the thinking level of the chosen model.
|
2026-11-21 14:29:52 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import '../api/models.dart';
|
2026-11-21 17:03:22 +08:00
|
|
|
|
import '../model_prefs.dart';
|
2026-11-21 14:29:52 +08:00
|
|
|
|
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,
|
2026-11-21 17:03:22 +08:00
|
|
|
|
}) async {
|
2026-11-21 14:29:52 +08:00
|
|
|
|
app.refreshModels();
|
2026-11-21 17:03:22 +08:00
|
|
|
|
await ModelPrefs.instance.ensureLoaded();
|
|
|
|
|
|
if (!context.mounted) return;
|
2026-11-21 14:29:52 +08:00
|
|
|
|
return showModalBottomSheet<void>(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
isScrollControlled: true,
|
|
|
|
|
|
builder: (context) => DraggableScrollableSheet(
|
|
|
|
|
|
expand: false,
|
2026-11-21 17:03:22 +08:00
|
|
|
|
initialChildSize: 0.85,
|
|
|
|
|
|
maxChildSize: 0.95,
|
2026-11-21 14:29:52 +08:00
|
|
|
|
builder: (context, controller) => _PickerSheet(
|
|
|
|
|
|
app: app,
|
|
|
|
|
|
kind: kind,
|
|
|
|
|
|
model: model,
|
|
|
|
|
|
agent: agent,
|
|
|
|
|
|
scrollController: controller,
|
|
|
|
|
|
onModel: onModel,
|
|
|
|
|
|
onAgent: onAgent,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-21 17:03:22 +08:00
|
|
|
|
/// The thinking levels of one model, as a small sheet.
|
|
|
|
|
|
Future<void> showEffortSheet(
|
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
|
required List<String> variants,
|
|
|
|
|
|
required String? value,
|
|
|
|
|
|
required String modelName,
|
|
|
|
|
|
required ValueChanged<String?> onPick,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
return showModalBottomSheet<void>(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
builder: (context) => SafeArea(
|
|
|
|
|
|
child: Column(
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Padding(
|
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(16, 14, 16, 2),
|
|
|
|
|
|
child: Text('思考强度',
|
|
|
|
|
|
style:
|
|
|
|
|
|
const TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
|
|
|
|
|
|
),
|
|
|
|
|
|
Padding(
|
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
|
|
|
|
|
child: Text(modelName,
|
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
fontSize: 12, color: Theme.of(context).hintColor)),
|
|
|
|
|
|
),
|
|
|
|
|
|
for (final option in <String?>[null, ...variants])
|
|
|
|
|
|
ListTile(
|
|
|
|
|
|
dense: true,
|
|
|
|
|
|
title: Text(effortLabel(option)),
|
|
|
|
|
|
trailing: (value ?? '') == (option ?? '')
|
|
|
|
|
|
? Icon(Icons.check,
|
|
|
|
|
|
size: 18, color: Theme.of(context).colorScheme.primary)
|
|
|
|
|
|
: null,
|
|
|
|
|
|
onTap: () {
|
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
|
onPick(option);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
|
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 = '';
|
2026-11-21 17:03:22 +08:00
|
|
|
|
final Set<String> _collapsed = {};
|
|
|
|
|
|
|
|
|
|
|
|
ModelPrefs get _prefs => ModelPrefs.instance;
|
2026-11-21 14:29:52 +08:00
|
|
|
|
|
|
|
|
|
|
@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),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-11-21 17:03:22 +08:00
|
|
|
|
Expanded(child: _kind == 'agent' ? _agents() : _models()),
|
2026-11-21 14:29:52 +08:00
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-21 17:03:22 +08:00
|
|
|
|
// -------------------------------------------------------------- agents
|
|
|
|
|
|
|
|
|
|
|
|
Widget _agents() {
|
2026-11-21 14:29:52 +08:00
|
|
|
|
final filter = _filter.toLowerCase();
|
2026-11-21 17:03:22 +08:00
|
|
|
|
final agents = [
|
|
|
|
|
|
for (final a in widget.app.agentList)
|
|
|
|
|
|
if (a.mode == 'primary' && !a.hidden)
|
|
|
|
|
|
if (filter.isEmpty || a.name.toLowerCase().contains(filter)) a,
|
|
|
|
|
|
];
|
|
|
|
|
|
if (agents.isEmpty) return const Center(child: Text('没有结果'));
|
2026-11-21 14:29:52 +08:00
|
|
|
|
return ListView.builder(
|
|
|
|
|
|
controller: widget.scrollController,
|
2026-11-21 17:03:22 +08:00
|
|
|
|
itemCount: agents.length,
|
2026-11-21 14:29:52 +08:00
|
|
|
|
itemBuilder: (context, i) {
|
2026-11-21 17:03:22 +08:00
|
|
|
|
final agent = agents[i];
|
2026-11-21 14:29:52 +08:00
|
|
|
|
return ListTile(
|
2026-11-21 17:03:22 +08:00
|
|
|
|
dense: true,
|
|
|
|
|
|
title: Text(agent.name),
|
|
|
|
|
|
trailing: widget.agent == agent.id
|
|
|
|
|
|
? Icon(Icons.check,
|
|
|
|
|
|
size: 18, color: Theme.of(context).colorScheme.primary)
|
|
|
|
|
|
: null,
|
2026-11-21 14:29:52 +08:00
|
|
|
|
onTap: () {
|
|
|
|
|
|
Navigator.pop(context);
|
2026-11-21 17:03:22 +08:00
|
|
|
|
widget.onAgent(agent.id);
|
2026-11-21 14:29:52 +08:00
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-21 17:03:22 +08:00
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------- models
|
|
|
|
|
|
|
|
|
|
|
|
Widget _models() {
|
|
|
|
|
|
final app = widget.app;
|
|
|
|
|
|
final all = app.modelList;
|
|
|
|
|
|
if (all.isEmpty) {
|
|
|
|
|
|
return const Center(child: Text('正在载入模型…'));
|
|
|
|
|
|
}
|
|
|
|
|
|
final filter = _filter.trim().toLowerCase();
|
|
|
|
|
|
final matching = filter.isEmpty
|
|
|
|
|
|
? all
|
|
|
|
|
|
: [
|
|
|
|
|
|
for (final m in all)
|
|
|
|
|
|
if (m.name.toLowerCase().contains(filter) ||
|
|
|
|
|
|
m.id.toLowerCase().contains(filter))
|
|
|
|
|
|
m,
|
|
|
|
|
|
];
|
|
|
|
|
|
if (matching.isEmpty) return const Center(child: Text('没有结果'));
|
|
|
|
|
|
|
|
|
|
|
|
final byKey = {
|
|
|
|
|
|
for (final m in all) ModelPrefs.keyFor(m.providerId, m.id): m,
|
|
|
|
|
|
};
|
|
|
|
|
|
final favorites = [
|
|
|
|
|
|
for (final key in _prefs.favorites)
|
|
|
|
|
|
if (byKey[key] != null) byKey[key]!,
|
|
|
|
|
|
]..sort((a, b) => a.name.compareTo(b.name));
|
|
|
|
|
|
final recentKeys = [
|
|
|
|
|
|
for (final key in _prefs.recents)
|
|
|
|
|
|
if (byKey[key] != null && !_prefs.favorites.contains(key)) key,
|
|
|
|
|
|
];
|
|
|
|
|
|
final recents = [for (final key in recentKeys) byKey[key]!];
|
|
|
|
|
|
|
|
|
|
|
|
final providers = <String, List<ModelInfo>>{};
|
|
|
|
|
|
for (final model in matching) {
|
|
|
|
|
|
providers.putIfAbsent(model.providerId, () => []).add(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final children = <Widget>[];
|
|
|
|
|
|
if (filter.isEmpty) {
|
|
|
|
|
|
if (favorites.isNotEmpty) {
|
|
|
|
|
|
children.add(_sectionHeader('收藏', 'favorites', count: favorites.length));
|
|
|
|
|
|
if (!_collapsed.contains('favorites')) {
|
|
|
|
|
|
for (final model in favorites) {
|
|
|
|
|
|
children.add(_modelRow(model));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (recents.isNotEmpty) {
|
|
|
|
|
|
children.add(_sectionHeader('最近', 'recents', count: recents.length));
|
|
|
|
|
|
if (!_collapsed.contains('recents')) {
|
|
|
|
|
|
for (final model in recents) {
|
|
|
|
|
|
children.add(_modelRow(model));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for (final entry in providers.entries) {
|
|
|
|
|
|
final label = entry.key.isEmpty ? '其它' : entry.key;
|
|
|
|
|
|
children.add(_sectionHeader(label, 'provider:${entry.key}',
|
|
|
|
|
|
count: entry.value.length));
|
|
|
|
|
|
if (!_collapsed.contains('provider:${entry.key}')) {
|
|
|
|
|
|
for (final model in entry.value) {
|
|
|
|
|
|
children.add(_modelRow(model));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return ListView(
|
|
|
|
|
|
controller: widget.scrollController,
|
|
|
|
|
|
children: children,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget _sectionHeader(String label, String key, {int? count}) {
|
|
|
|
|
|
final collapsed = _collapsed.contains(key);
|
|
|
|
|
|
return InkWell(
|
|
|
|
|
|
onTap: () => setState(() {
|
|
|
|
|
|
if (collapsed) {
|
|
|
|
|
|
_collapsed.remove(key);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
_collapsed.add(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}),
|
|
|
|
|
|
child: Padding(
|
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Icon(collapsed ? Icons.chevron_right : Icons.expand_more,
|
|
|
|
|
|
size: 16, color: Theme.of(context).hintColor),
|
|
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
count == null ? label : '$label($count)',
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
fontWeight: FontWeight.w700,
|
|
|
|
|
|
color: Theme.of(context).hintColor),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget _modelRow(ModelInfo model) {
|
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
|
final key = ModelPrefs.keyFor(model.providerId, model.id);
|
|
|
|
|
|
final selected = widget.model != null &&
|
|
|
|
|
|
widget.model!.id == model.id &&
|
|
|
|
|
|
widget.model!.providerId == model.providerId;
|
|
|
|
|
|
final favorite = _prefs.isFavorite(key);
|
|
|
|
|
|
return ListTile(
|
|
|
|
|
|
dense: true,
|
|
|
|
|
|
contentPadding: const EdgeInsets.only(left: 20, right: 4),
|
|
|
|
|
|
title: Row(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Flexible(
|
|
|
|
|
|
child: Text(model.name,
|
|
|
|
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
|
|
|
|
),
|
|
|
|
|
|
if (model.reasoning)
|
|
|
|
|
|
Padding(
|
|
|
|
|
|
padding: const EdgeInsets.only(left: 4),
|
|
|
|
|
|
child: Icon(Icons.psychology_outlined,
|
|
|
|
|
|
size: 14, color: theme.hintColor),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
subtitle: model.context > 0
|
|
|
|
|
|
? Text('${formatTokenCount(model.context)} 上下文',
|
|
|
|
|
|
style: TextStyle(fontSize: 11, color: theme.hintColor))
|
|
|
|
|
|
: null,
|
|
|
|
|
|
trailing: Row(
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
if (selected)
|
|
|
|
|
|
Padding(
|
|
|
|
|
|
padding: const EdgeInsets.only(right: 4),
|
|
|
|
|
|
child: Icon(Icons.check,
|
|
|
|
|
|
size: 18, color: theme.colorScheme.primary),
|
|
|
|
|
|
),
|
|
|
|
|
|
IconButton(
|
|
|
|
|
|
visualDensity: VisualDensity.compact,
|
|
|
|
|
|
icon: Icon(
|
|
|
|
|
|
favorite ? Icons.star : Icons.star_border,
|
|
|
|
|
|
size: 18,
|
|
|
|
|
|
color: favorite ? theme.colorScheme.primary : theme.hintColor,
|
|
|
|
|
|
),
|
|
|
|
|
|
tooltip: favorite ? '取消收藏' : '收藏',
|
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
|
await _prefs.toggleFavorite(key);
|
|
|
|
|
|
if (mounted) setState(() {});
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
onTap: () => _select(model),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _select(ModelInfo model) async {
|
|
|
|
|
|
final key = ModelPrefs.keyFor(model.providerId, model.id);
|
|
|
|
|
|
final remembered = _prefs.effortFor(key);
|
|
|
|
|
|
final variant = remembered != null && model.variants.contains(remembered)
|
|
|
|
|
|
? remembered
|
|
|
|
|
|
: null;
|
|
|
|
|
|
await _prefs.rememberModel(key);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
|
widget.onModel(ModelRef(model.id, model.providerId, variant));
|
|
|
|
|
|
}
|
2026-11-21 14:29:52 +08:00
|
|
|
|
}
|