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
@@ -52,6 +52,9 @@ class ChatThreadController extends ChangeNotifier {
final ApiGateway gateway;
final String sessionId;
/// Messages asked for per history page (servers may serve fewer).
static const _pageLimit = 200;
late final ChatRepository _repository = ChatRepository(gateway);
late final ChatThreadCards _cards = ChatThreadCards(
repository: _repository,
@@ -212,9 +215,18 @@ class ChatThreadController extends ChangeNotifier {
_loading = true;
_error = null;
_notify();
await _attach();
if (!_owned(token)) return;
await _readNewest(token, replaceHeldTurn: false);
final frames = _live.beginRead();
final page = await _attach();
if (!_owned(token)) {
_live.abandon(frames);
return;
}
await _readNewest(
token,
replaceHeldTurn: false,
frames: frames,
page: page,
);
}
/// Reads the next older page. Only offered while the server says `has_more`;
@@ -229,7 +241,7 @@ class ChatThreadController extends ChangeNotifier {
final page = await _repository.history(
sessionId,
cursor: _cursor,
limit: 200,
limit: _pageLimit,
);
if (!_owned(token)) return;
turnDiffs.apply(page['turnDiffs'], sessionId);
@@ -276,17 +288,27 @@ class ChatThreadController extends ChangeNotifier {
_awaitingResponseOperation = null;
_authoritative = false;
_notify();
await _attach(force: true);
if (!_owned(token)) return;
await _readNewest(token, replaceHeldTurn: true);
final frames = _live.beginRead();
final page = await _attach(force: true);
if (!_owned(token)) {
_live.abandon(frames);
return;
}
await _readNewest(token, replaceHeldTurn: true, frames: frames, page: page);
}
Future<void> _readNewest(int token, {required bool replaceHeldTurn}) async {
// Deltas of a reply streaming during the read: the page holds the reply
// up to its own point, and those after it are folded back on top.
final frames = _live.beginRead();
/// Applies the newest history page: [page] when the subscribe reply carried
/// it, else one read now. [frames] records the deltas of a reply streaming
/// meanwhile: the page holds the reply up to its own point, and those after
/// it are folded back on top.
Future<void> _readNewest(
int token, {
required bool replaceHeldTurn,
required List<JsonMap> frames,
JsonMap? page,
}) async {
try {
final page = await _repository.history(sessionId, limit: 200);
page ??= await _repository.history(sessionId, limit: _pageLimit);
if (!_owned(token)) {
_live.abandon(frames);
return;
@@ -338,22 +360,29 @@ class ChatThreadController extends ChangeNotifier {
_notify();
}
/// Attaches the session stream once per connection. A refusal leaves history
/// Attaches the session stream once per connection, asking the reply to
/// carry the newest history page, which is returned (null when the server
/// left it out or the stream was already attached). A refusal leaves history
/// usable (the live stream is simply not attached) and is retried on resync.
Future<void> _attach({bool force = false}) async {
if (_disposed || (_subscribed && !force)) return;
Future<JsonMap?> _attach({bool force = false}) async {
if (_disposed || (_subscribed && !force)) return null;
try {
final reply = await _repository.subscribe(sessionId);
if (_disposed) return;
final reply = await _repository.subscribe(
sessionId,
historyLimit: _pageLimit,
);
if (_disposed) return null;
_subscribed = true;
_subscriptionError = null;
_applyQueue(reply['chat_queue']);
_applyShown(reply['chat_shown'], replace: true);
return ChatRepository.historyPageOf(reply);
} catch (error) {
// History stays usable, but the UI must report that live updates failed.
if (_disposed) return;
if (_disposed) return null;
_subscribed = false;
_subscriptionError = '$error';
return null;
}
}
+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,