aeefe67631
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>
85 lines
3.2 KiB
Dart
85 lines
3.2 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import 'chat_transcript.dart';
|
|
|
|
/// Joins a history read with the live reply that streams during it.
|
|
///
|
|
/// A harness that numbers its text and reasoning deltas (`seq`) puts a
|
|
/// streaming block's text so far into the newest history page, under the
|
|
/// block's live part id, and reports the last delta the page includes
|
|
/// (`liveSeq`) and which parts it added (`liveParts`). Without this a client
|
|
/// that read history mid-reply (opening the chat, a reconnect, a resync) only
|
|
/// ever got the deltas after its read, and the reply's first part was missing.
|
|
///
|
|
/// [ChatLiveFrames] records the numbered frames that arrive while a read is in
|
|
/// flight and, once the page is applied, folds back the ones the page does not
|
|
/// include. It also drops a frame already included in a page, which can still
|
|
/// be on its way when the page arrives.
|
|
class ChatLiveFrames {
|
|
final List<List<JsonMap>> _reads = [];
|
|
int _included = 0;
|
|
|
|
/// Starts recording for one read; pass the result to [settle].
|
|
List<JsonMap> beginRead() {
|
|
final frames = <JsonMap>[];
|
|
_reads.add(frames);
|
|
return frames;
|
|
}
|
|
|
|
/// Stops recording for a read that will not be applied.
|
|
void abandon(List<JsonMap> frames) => _reads.remove(frames);
|
|
|
|
/// Whether [frame] should be folded into the held transcript, recording it
|
|
/// for every read in flight. A frame with no number is always folded.
|
|
bool admit(JsonMap frame) {
|
|
final seq = frame['seq'];
|
|
if (seq is! int) return true;
|
|
if (seq <= _included) return false;
|
|
_included = seq;
|
|
for (final read in _reads) {
|
|
read.add(frame);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// [messages] after applying [page]: frames recorded during the read that
|
|
/// the page does not include are folded onto the page's copies. [replaced]
|
|
/// says whether a message's held copy was replaced by the page (frames for a
|
|
/// held copy that was kept are already in it).
|
|
List<JsonMap> settle(
|
|
List<JsonMap> frames,
|
|
JsonMap page,
|
|
List<JsonMap> messages, {
|
|
required bool Function(Object? messageId) replaced,
|
|
}) {
|
|
_reads.remove(frames);
|
|
final liveSeq = page['liveSeq'];
|
|
if (liveSeq is! int) return messages;
|
|
if (liveSeq > _included) _included = liveSeq;
|
|
final live = {...(page['liveParts'] as List? ?? const [])};
|
|
final pageParts = <Object?, Set<Object?>>{};
|
|
for (final message in (page['messages'] as List? ?? const [])) {
|
|
if (message is! Map) continue;
|
|
pageParts[message['id']] = {
|
|
for (final part in (message['parts'] as List? ?? const []))
|
|
if (part is Map) part['id'],
|
|
};
|
|
}
|
|
var thread = <String, dynamic>{'messages': messages};
|
|
for (final frame in frames) {
|
|
final seq = frame['seq'];
|
|
if (seq is! int || seq <= liveSeq) continue;
|
|
final messageId = frame['message_id'];
|
|
if (!replaced(messageId)) continue;
|
|
final part = frame['part'];
|
|
final partId = part is Map ? part['id'] : null;
|
|
final held = pageParts[messageId];
|
|
// A part the page holds whole (not as live text) is already complete.
|
|
if (held != null && held.contains(partId) && !live.contains(partId)) {
|
|
continue;
|
|
}
|
|
thread = applyChatEvent(thread, frame);
|
|
}
|
|
return chatMessageList(thread['messages']);
|
|
}
|
|
}
|