App: open a chat with one round trip

The thread controller asks chat_subscribe to carry the newest history page
(history: true, limit 200) and applies it from the reply; a server that
leaves the page out is asked with chat_messages as before. Deltas arriving
before the reply are recorded from the start of the subscribe, and settle
against the page's live_seq exactly as for a separate read.

Also documents the subscribe history field and benchmarks the indexed page
at limits 20 and 100.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 15:09:02 +00:00
parent 686690f7b4
commit 608b6b8edb
7 changed files with 201 additions and 61 deletions
+29 -19
View File
@@ -154,25 +154,29 @@ class ChatRepository {
final reply = await gateway.call('chat_messages', {
'chat_query': {'session_id': sessionId, 'limit': limit, 'cursor': cursor},
});
return historyPageOf(reply) ??
{
'messages': _maps(reply['chat_messages']),
'nextCursor': '',
'hasMore': false,
'turnDiffs': ?reply['chat_turn_diffs'],
};
}
/// The history page a `chat_messages` or `chat_subscribe` reply carries,
/// or null when it carries none.
static JsonMap? historyPageOf(JsonMap reply) {
final page = reply['chat_history'];
// The session's recorded per-turn changes ride with its history.
final turnDiffs = reply['chat_turn_diffs'];
if (page is Map) {
return {
'messages': _maps(page['messages']),
'nextCursor': '${page['next_cursor'] ?? ''}',
'hasMore': page['has_more'] == true,
'turnDiffs': ?turnDiffs,
// A streaming reply's text so far (see chat_live_frames.dart).
if (page['live_seq'] is int) 'liveSeq': page['live_seq'],
if (page['live_parts'] is List) 'liveParts': page['live_parts'],
};
}
if (page is! Map) return null;
return {
'messages': _maps(reply['chat_messages']),
'nextCursor': '',
'hasMore': false,
'turnDiffs': ?turnDiffs,
'messages': _maps(page['messages']),
'nextCursor': '${page['next_cursor'] ?? ''}',
'hasMore': page['has_more'] == true,
// The session's recorded per-turn changes ride with its history.
'turnDiffs': ?reply['chat_turn_diffs'],
// A streaming reply's text so far (see chat_live_frames.dart).
if (page['live_seq'] is int) 'liveSeq': page['live_seq'],
if (page['live_parts'] is List) 'liveParts': page['live_parts'],
};
}
@@ -189,9 +193,15 @@ class ChatRepository {
return Map<String, dynamic>.from(body);
}
Future<JsonMap> subscribe(String sessionId) =>
/// Attaches the session's live stream. With [historyLimit] the reply also
/// carries the newest history page (see [historyPageOf]) when the server
/// supports it, saving the separate `chat_messages` round trip.
Future<JsonMap> subscribe(String sessionId, {int? historyLimit}) =>
gateway.call('chat_subscribe', {
'chat_subscribe': {'session_id': sessionId},
'chat_subscribe': {
'session_id': sessionId,
if (historyLimit != null) ...{'history': true, 'limit': historyLimit},
},
});
Future<JsonMap> create({
required String title,