Chat: a reply that streams during a history read is shown whole
The thread controller records numbered (seq) deltas that arrive while the newest page is read and folds back those the page does not include, onto its live parts or parts it lacks; a delta the page already included is not added again when it arrives late. The page's order now leads when a reply streamed during the initial read, so the reply stays below its prompt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/chat_repository.dart';
|
||||
import '../domain/chat_account_usage.dart';
|
||||
import '../domain/chat_live_frames.dart';
|
||||
import '../domain/chat_sessions.dart';
|
||||
import '../domain/chat_shown_items.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
@@ -61,6 +62,7 @@ class ChatThreadController extends ChangeNotifier {
|
||||
StreamSubscription<JsonMap>? _subscription;
|
||||
final _transcriptChanges = ChangeNotifier();
|
||||
final ChatServerQueue _queue = ChatServerQueue();
|
||||
final ChatLiveFrames _live = ChatLiveFrames();
|
||||
|
||||
/// The per-turn change summaries the transcript's change chips show.
|
||||
final ChatTurnDiffs turnDiffs = ChatTurnDiffs();
|
||||
@@ -280,10 +282,20 @@ class ChatThreadController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
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();
|
||||
try {
|
||||
final page = await _repository.history(sessionId, limit: 200);
|
||||
if (!_owned(token)) return;
|
||||
if (!_owned(token)) {
|
||||
_live.abandon(frames);
|
||||
return;
|
||||
}
|
||||
turnDiffs.apply(page['turnDiffs'], sessionId);
|
||||
final inPage = {
|
||||
for (final message in (page['messages'] as List? ?? const []))
|
||||
if (message is Map) message['id'],
|
||||
};
|
||||
if (replaceHeldTurn) {
|
||||
_messages = (page['messages'] as List? ?? const [])
|
||||
.whereType<Map>()
|
||||
@@ -297,15 +309,25 @@ class ChatThreadController extends ChangeNotifier {
|
||||
_applyPage(refreshCachedChatHistory(_messages, page, untouched));
|
||||
} else {
|
||||
// A live frame may have arrived while the page was loading; merge it
|
||||
// over the held transcript instead of replacing it.
|
||||
_applyPage(mergeChatHistory({'messages': _messages}, page));
|
||||
// over the held transcript instead of replacing it. The page's order
|
||||
// leads: a reply that streamed during the read stays below its prompt.
|
||||
_applyPage(
|
||||
mergeChatHistory({'messages': _messages}, page, older: true),
|
||||
);
|
||||
}
|
||||
_messages = _live.settle(
|
||||
frames,
|
||||
page,
|
||||
_messages,
|
||||
replaced: (id) => replaceHeldTurn || inPage.contains(id),
|
||||
);
|
||||
_untouchedCached = null;
|
||||
_authoritative = true;
|
||||
_loaded = true;
|
||||
_loading = false;
|
||||
_error = null;
|
||||
} catch (error) {
|
||||
_live.abandon(frames);
|
||||
if (!_owned(token)) return;
|
||||
// A refused read is not an empty successful history: the reason stays
|
||||
// visible and the held transcript is kept.
|
||||
@@ -416,6 +438,8 @@ class ChatThreadController extends ChangeNotifier {
|
||||
);
|
||||
return;
|
||||
}
|
||||
// A delta a history page already included is not added again.
|
||||
if (!_live.admit(body)) return;
|
||||
final current = _thread;
|
||||
final next = applyChatEvent(current, body);
|
||||
if (identical(next, current)) return;
|
||||
|
||||
Reference in New Issue
Block a user