fix: switching a session's model, and put the model row in the composer
- POST /api/session/:id/model takes the reference under `model`; sending it at the top level was rejected with "Missing key at [model]". - The chat now shows the model/agent row above the input (revealed while typing or running, as dsh-Go does) instead of in the app bar; the draft keeps the same row, with its project chip first. - Thinking levels: /api/model's variants become a 思考 chip and sheet; the choice travels as ModelRef.variant on the switch or the create, and the last level per model is remembered on the device. - The model picker groups providers, adds 收藏 and 最近 (device-local), shows each model's context size and reasoning mark, and collapses sections. - The `/` palette is shared by the chat and the draft (new slash_palette widget); the draft lists commands and skills but not the client-local /compact, which needs an existing session. - 0.3.14 (314).
This commit is contained in:
1 parent
fff6562cbc
commit
7f2f454539
12 files changed
+962
-344
No files matched your search
@@ -0,0 +1,106 @@
|
||||
// Model picker preferences: favorites, recently used models, and the effort
|
||||
// last chosen for each. Device-local; the server has no say in them.
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ModelPrefs {
|
||||
ModelPrefs._();
|
||||
static final ModelPrefs instance = ModelPrefs._();
|
||||
|
||||
static const _favoritesKey = 'model.favorites.v1';
|
||||
static const _recentsKey = 'model.recents.v1';
|
||||
static const _effortsKey = 'model.efforts.v1';
|
||||
static const _maxRecents = 6;
|
||||
|
||||
final Set<String> favorites = {};
|
||||
final List<String> recents = [];
|
||||
final Map<String, String> efforts = {};
|
||||
bool _loaded = false;
|
||||
|
||||
static String keyFor(String providerId, String id) => '$providerId/$id';
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_loaded) return;
|
||||
_loaded = true;
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
favorites
|
||||
..clear()
|
||||
..addAll(prefs.getStringList(_favoritesKey) ?? const []);
|
||||
recents
|
||||
..clear()
|
||||
..addAll(prefs.getStringList(_recentsKey) ?? const []);
|
||||
final raw = prefs.getString(_effortsKey);
|
||||
if (raw != null && raw.isNotEmpty) {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is Map) {
|
||||
efforts
|
||||
..clear()
|
||||
..addAll({
|
||||
for (final entry in decoded.entries)
|
||||
if (entry.value is String)
|
||||
'${entry.key}': entry.value as String,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
bool isFavorite(String key) => favorites.contains(key);
|
||||
|
||||
Future<void> toggleFavorite(String key) async {
|
||||
if (!favorites.remove(key)) favorites.add(key);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_favoritesKey, favorites.toList());
|
||||
}
|
||||
|
||||
Future<void> rememberModel(String key) async {
|
||||
recents.remove(key);
|
||||
recents.insert(0, key);
|
||||
if (recents.length > _maxRecents) recents.removeRange(_maxRecents, recents.length);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setStringList(_recentsKey, recents);
|
||||
}
|
||||
|
||||
String? effortFor(String key) => efforts[key];
|
||||
|
||||
Future<void> rememberEffort(String key, String? variant) async {
|
||||
if (variant == null || variant.isEmpty) {
|
||||
efforts.remove(key);
|
||||
} else {
|
||||
efforts[key] = variant;
|
||||
}
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_effortsKey, jsonEncode(efforts));
|
||||
}
|
||||
}
|
||||
|
||||
/// `200000` reads as `200K`, `1000000` as `1M`.
|
||||
String formatTokenCount(int tokens) {
|
||||
if (tokens <= 0) return '';
|
||||
String trim(String value) =>
|
||||
value.endsWith('.0') ? value.substring(0, value.length - 2) : value;
|
||||
if (tokens >= 1000000) {
|
||||
return '${trim((tokens / 1000000).toStringAsFixed(1))}M';
|
||||
}
|
||||
if (tokens >= 1000) {
|
||||
return '${(tokens / 1000).round()}K';
|
||||
}
|
||||
return '$tokens';
|
||||
}
|
||||
|
||||
/// The Chinese label for an effort variant; unknown ids keep their own name.
|
||||
String effortLabel(String? variant) {
|
||||
if (variant == null || variant.isEmpty) return '默认';
|
||||
return switch (variant.toLowerCase()) {
|
||||
'default' => '默认',
|
||||
'none' => '无',
|
||||
'minimal' => '极低',
|
||||
'low' => '低',
|
||||
'medium' || 'mid' => '中',
|
||||
'high' => '高',
|
||||
'xhigh' || 'max' => '极高',
|
||||
_ => variant,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user