fix: full chat history, picked model/agent, usage sheet, image previews

- The chat loaded only one page of history: a desc page's older side is
  cursor.next, not previous, and the server rejects a cursor combined
  with order (InvalidCursorError). The first page stores `next`, later
  pages pass only `cursor`. tool/history_probe.dart walks a 1698-message
  session down to the oldest message with no gaps.
- The message list is now reversed (newest at offset 0), so older pages
  grow above and the viewport stays put; near the top it loads by itself
  and a "load earlier" row remains as a fallback.
- The agent picker filtered only on mode, so Compaction/Title/Summary
  (primary but hidden) showed up. Filter `hidden` too, and mark the
  current model or agent with a check.
- The usage ring's sheet was guarded by a flag nothing ever set; the
  ring now opens the context detail sheet.
- Tool image previews use a self-loading dialog and Image.memory: no
  temp file and no path_provider, whose Android implementation throws
  ClassNotFoundException: Landroid/content/Context; on the emulator.
- "Add server" in the connected server sheet now opens the form and
  returns to the session list once connected.
- Add a Linux desktop target for fast local UI checks, plus the probe
  that pages history.
- Version 0.3.2 (code 302).
This commit is contained in:
blumia committed 2026-11-19 12:34:25 +08:00
1 parent 2f5aad3814
commit 61977d2b14
24 files changed
+834 -130

No files matched your search

+36 -14
View File
@@ -473,14 +473,12 @@ class ChatState extends ChangeNotifier {
bool hasMore = false;
bool loadingOlder = false;
Map<String, List<String>> imagePaths = {};
final Map<String, String> previewCache = {};
bool previewOpen = false;
String previewTitle = '';
String? previewPath;
bool previewLoading = false;
int previewSeq = 0;
ModelRef? selectedModel;
String selectedAgent;
ChatState(this.app, this.session);
ChatState(this.app, this.session)
: selectedModel = session.model,
selectedAgent = session.agent;
/// Public way to signal UI changes.
void touch() => notifyListeners();
@@ -494,8 +492,9 @@ class ChatState extends ChangeNotifier {
try {
final page = await app.client!.listMessages(id, directory: dir);
_setMessages(page.data);
olderCursor = page.previous;
hasMore = page.previous != null;
// A desc page's `next` cursor points at the older side.
olderCursor = page.next;
hasMore = page.next != null;
loading = false;
notifyListeners();
await reloadTail();
@@ -524,9 +523,9 @@ class ChatState extends ChangeNotifier {
final seen = msgs.map((m) => m.id).toSet();
final older = page.data.where((m) => !seen.contains(m.id)).toList();
msgs = [...older, ...msgs];
_reindex();
olderCursor = page.previous;
hasMore = page.previous != null;
_sortMsgs();
olderCursor = page.next;
hasMore = page.next != null;
} catch (_) {}
loadingOlder = false;
notifyListeners();
@@ -534,6 +533,15 @@ class ChatState extends ChangeNotifier {
void _setMessages(List<Message> list) {
msgs = list;
_sortMsgs();
}
/// Oldest first: a page arrives newest-first (order: desc).
void _sortMsgs() {
msgs.sort((a, b) {
if (a.created == b.created) return a.id.compareTo(b.id);
return a.created.compareTo(b.created);
});
_reindex();
}
@@ -553,7 +561,7 @@ class ChatState extends ChangeNotifier {
created: DateTime.now().millisecondsSinceEpoch,
files: files?.map((f) => FileAttachment(f['name'] ?? '附件', '')).toList() ?? const [],
));
_reindex();
_sortMsgs();
notifyListeners();
try {
await app.client!.prompt(this.id, text: t, messageId: id, directory: dir, files: files);
@@ -617,6 +625,8 @@ class ChatState extends ChangeNotifier {
Future<void> switchModel(ModelRef model) async {
try {
await app.client!.switchModel(id, model, directory: dir);
selectedModel = model;
notifyListeners();
} catch (e) {
error = e.toString();
notifyListeners();
@@ -626,6 +636,8 @@ class ChatState extends ChangeNotifier {
Future<void> switchAgent(String agent) async {
try {
await app.client!.switchAgent(id, agent, directory: dir);
selectedAgent = agent;
notifyListeners();
} catch (e) {
error = e.toString();
notifyListeners();
@@ -719,6 +731,16 @@ class ChatState extends ChangeNotifier {
case 'session.tool.progress':
if (mid != null) _applyTool(mid, type, data);
break;
case 'session.model.selected':
final model = data['model'];
if (model is Map<String, dynamic>) {
selectedModel = ModelRef.fromJson(model);
}
break;
case 'session.agent.selected':
final agent = data['agent'];
if (agent is String && agent.isNotEmpty) selectedAgent = agent;
break;
}
notifyListeners();
}
@@ -726,7 +748,7 @@ class ChatState extends ChangeNotifier {
void _ensureAssistant(String id, int created) {
if (byId.containsKey(id)) return;
msgs.add(Message(id: id, type: 'assistant', created: created));
_reindex();
_sortMsgs();
}
void _applyDelta(String mid, int ordinal, String kind, String type, Map<String, dynamic> data) {