App: keep a left chat's cached transcript following its live frames
A left conversation stays subscribed on the server for the rest of the connection, but the cached transcript was frozen when it was left. A reply that arrived in the background only appeared after the reopened view's subscribe and newest-page read (~1-2s over the network on large chats). The cache now folds the session's chat events into the kept transcript, so the reply is drawn the moment the chat opens; the page still replaces it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
|
||||
/// The newest transcript one conversation view held when it was left.
|
||||
///
|
||||
@@ -23,6 +25,12 @@ class ChatTranscriptSnapshot {
|
||||
/// Transcripts of recently left conversations, kept in memory by the chat
|
||||
/// destination for the rest of the app session, like its scroll positions.
|
||||
///
|
||||
/// A left conversation stays subscribed on the server for the rest of the
|
||||
/// connection (there is no `chat_unsubscribe`), so its live frames keep
|
||||
/// arriving. A transcript written with its gateway's `frames` folds them in,
|
||||
/// so a reopened conversation already shows the replies that arrived while
|
||||
/// it was in the background instead of only after its re-read.
|
||||
///
|
||||
/// Bounded twice: at most [maxSessions] conversations and [maxMessages] held
|
||||
/// messages in total, evicting the least recently used first. A transcript
|
||||
/// larger than [maxMessages] on its own is not kept. Nothing is written to
|
||||
@@ -36,6 +44,7 @@ class ChatTranscriptCache {
|
||||
/// Least recently used first.
|
||||
final LinkedHashMap<String, ChatTranscriptSnapshot> _entries =
|
||||
LinkedHashMap();
|
||||
final Map<String, StreamSubscription<JsonMap>> _follows = {};
|
||||
int _messageCount = 0;
|
||||
|
||||
/// The cached sessions, least recently used first.
|
||||
@@ -49,23 +58,77 @@ class ChatTranscriptCache {
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
void write(String sessionId, ChatTranscriptSnapshot snapshot) {
|
||||
/// Keeps [snapshot] for [sessionId]. With [frames] (the session's gateway
|
||||
/// frames), the session's live chat events keep folding into it until it
|
||||
/// is replaced, removed or evicted.
|
||||
void write(
|
||||
String sessionId,
|
||||
ChatTranscriptSnapshot snapshot, {
|
||||
Stream<JsonMap>? frames,
|
||||
}) {
|
||||
remove(sessionId);
|
||||
if (snapshot.messages.length > maxMessages) return;
|
||||
_entries[sessionId] = snapshot;
|
||||
_messageCount += snapshot.messages.length;
|
||||
while (_entries.length > maxSessions || _messageCount > maxMessages) {
|
||||
remove(_entries.keys.first);
|
||||
if (!_store(sessionId, snapshot)) return;
|
||||
if (frames != null) {
|
||||
_follows[sessionId] = frames.listen(
|
||||
(envelope) => _fold(sessionId, envelope),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void remove(String sessionId) {
|
||||
unawaited(_follows.remove(sessionId)?.cancel());
|
||||
final removed = _entries.remove(sessionId);
|
||||
if (removed != null) _messageCount -= removed.messages.length;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (final follow in _follows.values) {
|
||||
unawaited(follow.cancel());
|
||||
}
|
||||
_follows.clear();
|
||||
_entries.clear();
|
||||
_messageCount = 0;
|
||||
}
|
||||
|
||||
/// Puts [snapshot] last (most recently used) and enforces the bounds;
|
||||
/// false when it is too large to keep at all.
|
||||
bool _store(String sessionId, ChatTranscriptSnapshot snapshot) {
|
||||
if (snapshot.messages.length > maxMessages) return false;
|
||||
_entries[sessionId] = snapshot;
|
||||
_messageCount += snapshot.messages.length;
|
||||
while (_entries.length > maxSessions || _messageCount > maxMessages) {
|
||||
remove(_entries.keys.first);
|
||||
}
|
||||
return _entries.containsKey(sessionId);
|
||||
}
|
||||
|
||||
/// Folds one live chat event for [sessionId] into its kept transcript, in
|
||||
/// place (its recency is unchanged). A reopened view still treats every
|
||||
/// kept message as a placeholder its newest page replaces.
|
||||
void _fold(String sessionId, JsonMap envelope) {
|
||||
if (envelope['type'] != 'chat_event') return;
|
||||
final body = envelope['chat_event'];
|
||||
if (body is! Map || body['session_id'] != sessionId) return;
|
||||
final held = _entries[sessionId];
|
||||
if (held == null) return;
|
||||
final thread = {'sessionId': sessionId, 'messages': held.messages};
|
||||
final next = applyChatEvent(thread, Map<String, dynamic>.from(body));
|
||||
if (identical(next['messages'], held.messages)) return;
|
||||
final messages = chatMessageList(next['messages']);
|
||||
_messageCount += messages.length - held.messages.length;
|
||||
_entries[sessionId] = ChatTranscriptSnapshot(
|
||||
messages: messages,
|
||||
cursor: held.cursor,
|
||||
hasMore: held.hasMore,
|
||||
);
|
||||
if (messages.length > maxMessages) {
|
||||
remove(sessionId);
|
||||
return;
|
||||
}
|
||||
while (_messageCount > maxMessages) {
|
||||
final oldest = _entries.keys.first;
|
||||
if (oldest == sessionId) break;
|
||||
remove(oldest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user