Files
pipelines/app/lib/features/chat/domain/chat_transcript.dart
T
alan 9bf080597a Place items shown mid-turn inside the running turn
Live chat events name a message by id and carry no time, so messages the
client created from them had no created_at. A running turn then had no
timed rows, and an item the agent showed mid-turn (show_file, show_diff)
was placed after the last timed row: above the prompt and the whole
running reply, usually off screen, until a reload brought the server's
times. Messages created from live events are now stamped with their
arrival; a history read still replaces them with the server's times.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 19:23:21 +00:00

354 lines
13 KiB
Dart

import '../../../core/api_gateway.dart';
export 'chat_transcript_rows.dart';
/// Merge chronological history pages by message id. Incoming authoritative
/// copies replace held copies without changing their order; cursor is opaque.
JsonMap mergeChatHistory(JsonMap previous, JsonMap page, {bool older = false}) {
final held = (previous['messages'] as List? ?? const [])
.whereType<Map>()
.map((m) => Map<String, dynamic>.from(m))
.toList();
final incoming = (page['messages'] as List? ?? const [])
.whereType<Map>()
.map((m) => Map<String, dynamic>.from(m))
.toList();
final latest = <Object, JsonMap>{};
for (final message in incoming) {
if (message['id'] != null) latest[message['id']] = message;
}
final combined = older ? [...incoming, ...held] : [...held, ...incoming];
final seen = <Object>{};
final messages = <JsonMap>[];
for (final message in combined) {
final id = message['id'];
if (id != null && !seen.add(id)) continue;
messages.add(id == null ? message : latest[id] ?? message);
}
return {
'messages': messages,
'nextCursor': '${page['nextCursor'] ?? ''}',
'hasMore': page['hasMore'] == true,
'loaded': true,
'loading': false,
};
}
/// Replaces a transcript that opened from this visit's cached copy with the
/// newest authoritative page.
///
/// [held] is what the view shows: the cached messages plus anything a live
/// frame or a local send added or changed since the conversation reopened.
/// [untouchedCached] names the cached messages nothing has changed since; they
/// are not evidence of the current state, so the page alone decides whether
/// they still exist. The page's messages lead in its order with its copies
/// winning, then live arrivals the page does not hold yet follow in their own
/// order, so a frame received during the read is never dropped.
JsonMap refreshCachedChatHistory(
List<JsonMap> held,
JsonMap page,
Set<Object> untouchedCached,
) {
final live = [
for (final message in held)
if (message['id'] == null || !untouchedCached.contains(message['id']))
message,
];
return mergeChatHistory({'messages': live}, page, older: true);
}
/// The protocol records creation in epoch milliseconds. Invalid values do not
/// acquire an invented display time.
int? chatMessageCreatedAt(JsonMap message) {
final value = message['created_at'];
if (value is! num ||
!value.isFinite ||
value <= 0 ||
value > 8640000000000000 ||
value != value.roundToDouble()) {
return null;
}
return value.toInt();
}
/// `ChatMessage.Kind` of a report another agent delivered into the session.
const String chatKindAgentReport = 'agent_report';
/// `ChatMessage.Kind` of a one-line harness notice (a background task ended).
const String chatKindNotice = 'notice';
/// The injected-content kind the server recorded on [message], or null for an
/// ordinary turn. An unknown future kind keeps the message's own role.
String? chatInjectedKind(JsonMap message) {
final kind = '${message['kind'] ?? ''}';
return kind == chatKindAgentReport || kind == chatKindNotice ? kind : null;
}
/// Whether [part] is a recorded activity record rather than answer content.
///
/// Both spellings occur: `compaction` is the native lifecycle record and
/// `activity` is a generic sequence entry whose [chatActivityKind] carries the
/// concrete kind.
bool chatActivityType(JsonMap part) {
final type = '${part['type'] ?? ''}'.trim().toLowerCase();
return type == 'activity' || type == 'compaction';
}
/// The record's kind, or the part type when the record does not name one.
String chatActivityKind(JsonMap record) {
final kind = '${record['kind'] ?? ''}'.trim();
if (kind.isNotEmpty) return kind;
return '${record['type'] ?? ''}'.trim();
}
/// The record's lifecycle state, exactly as recorded (may be blank).
String chatActivityState(JsonMap record) {
for (final field in const ['state', 'status', 'lifecycle']) {
final value = '${record[field] ?? ''}'.trim();
if (value.isNotEmpty) return value;
}
return '';
}
/// The web's `Compacting context…` phase words. Both this activity line and the
/// local compaction notice use this one constant, so their wording cannot drift.
const String chatCompactionPhaseLabel = 'Compacting context…';
/// The running-phase words shared verbatim with the web's `turnPhaseLabel`.
/// The web clears a compaction on completion/failure, so there is no web copy
/// for a terminal record: those keep the label the server recorded.
const _chatActivityRunningWords = <String, String>{
'compaction': chatCompactionPhaseLabel,
'thinking': 'Thinking…',
'tool': 'Running a tool…',
'waiting': 'Waiting…',
};
/// The one line for a recorded activity record.
///
/// A running kind with a web phase word uses that exact wording. Otherwise the
/// server's recorded [record] label wins, including for an unknown kind; with
/// no label the recorded kind and state are kept verbatim rather than inventing
/// copy. Nothing is inferred from text the model wrote.
String chatActivityLabel(JsonMap record) {
final kind = chatActivityKind(record);
final state = chatActivityState(record);
if (state.toLowerCase() == 'running') {
final known = _chatActivityRunningWords[kind.toLowerCase()];
if (known != null) return known;
}
final recorded = '${record['label'] ?? ''}'.trim();
if (recorded.isNotEmpty) return recorded;
if (kind.isEmpty) return state.isEmpty ? 'Activity' : state;
return state.isEmpty ? kind : '$kind · $state';
}
/// Replace a local optimistic user row when its server echo identifies the
/// same operation/native message; otherwise append once by authoritative id.
List<JsonMap> reconcileChatUser(List<JsonMap> messages, JsonMap remote) {
final operation = remote['operation_id'];
final nativeId = remote['native_message_id'];
final id = remote['id'];
final index = messages.indexWhere(
(message) =>
(operation != null && message['operation_id'] == operation) ||
(nativeId != null && message['native_message_id'] == nativeId) ||
(id != null && message['id'] == id),
);
if (index < 0) return [...messages, remote];
final result = List<JsonMap>.of(messages);
result[index] = remote;
return result;
}
/// The messages of a decoded `messages` value. A message that already is a
/// [JsonMap] is kept as the same object (messages are never changed in place);
/// any other map is converted.
List<JsonMap> chatMessageList(Object? messages) => [
for (final message in (messages as List? ?? const []).whereType<Map>())
message is JsonMap ? message : Map<String, dynamic>.from(message),
];
/// Fold a live chat frame into an immutable thread snapshot. Text and reasoning
/// deltas append by part id, tool snapshots merge without dropping prior fields,
/// and step frames update turn activity. Frames for another session are ignored.
/// A live event names a message by id and carries no time, so a message this
/// creates is stamped with when it arrived ([now], epoch ms). Without one, a
/// running turn has no timed rows and an item the agent shows mid-turn
/// (show_file, show_diff) is placed above the whole turn; a later history
/// read replaces the stamp with the server's own time.
JsonMap applyChatEvent(JsonMap thread, JsonMap frame, {int Function()? now}) {
int arrived() => (now ?? _nowMs)();
final session = thread['sessionId'];
if (session != null &&
frame['session_id'] != null &&
session != frame['session_id']) {
return thread;
}
// Untouched messages keep their identity, so derived rows held for them
// (see ChatTranscriptRows) are reused; a changed message is a new map.
final messages = chatMessageList(thread['messages']);
final kind = frame['kind'];
if (kind == 'user') {
final part = frame['part'];
final text = part is Map ? part['text'] : null;
final id = frame['message_id'];
if (text is! String ||
text.isEmpty ||
id == null ||
messages.any(
(m) =>
m['id'] == id ||
m['native_message_id'] == id ||
(frame['operation_id'] != null &&
m['operation_id'] == frame['operation_id']),
)) {
return thread;
}
messages.add({
'id': id,
'session_id': frame['session_id'],
'role': 'user',
'created_at': arrived(),
'client': frame['client'],
'operation_id': frame['operation_id'],
'parts': [
{'type': 'text', 'text': text},
...(frame['attachments'] is List
? frame['attachments'] as List
: const []),
],
});
return {...thread, 'messages': messages};
}
if (kind == 'text' || kind == 'reasoning' || kind == 'tool') {
final incoming = frame['part'];
if (incoming is! Map || incoming['id'] == null) return thread;
final messageId = frame['message_id'];
// An agent report or notice the harness delivered: drawn on its own, and
// never evidence that the agent's turn is running.
final injected = chatInjectedKind({'kind': frame['message_kind']});
var index = messages.indexWhere(
(m) => m['id'] == messageId && m['role'] == 'assistant',
);
if (index < 0) {
messages.add({
'id': messageId,
'session_id': frame['session_id'],
'role': 'assistant',
'created_at': arrived(),
'kind': ?injected,
'parts': <JsonMap>[],
});
index = messages.length - 1;
}
final message = messages[index];
final parts = (message['parts'] as List? ?? const [])
.whereType<Map>()
.map((p) => Map<String, dynamic>.from(p))
.toList();
final partIndex = parts.indexWhere(
(p) => p['id'] == incoming['id'] && p['type'] == kind,
);
if (kind == 'text' || kind == 'reasoning') {
final delta = frame['delta'];
if (delta is! String || delta.isEmpty) return thread;
if (partIndex < 0) {
parts.add({
'type': kind,
'id': incoming['id'],
'text': delta,
if (incoming['name'] is String) 'name': incoming['name'],
});
} else {
parts[partIndex] = {
...parts[partIndex],
'text': '${parts[partIndex]['text'] ?? ''}$delta',
};
}
} else if (partIndex < 0) {
parts.add(Map<String, dynamic>.from(incoming));
} else {
final merged = {
...parts[partIndex],
...Map<String, dynamic>.from(incoming),
};
if (parts[partIndex]['state'] is Map && incoming['state'] is Map) {
merged['state'] = {
...Map<String, dynamic>.from(parts[partIndex]['state'] as Map),
...Map<String, dynamic>.from(incoming['state'] as Map),
};
}
parts[partIndex] = merged;
}
messages[index] = {...message, 'parts': parts};
if (chatInjectedKind(message) != null) {
return {...thread, 'messages': messages};
}
return {...thread, 'messages': messages, 'running': true};
}
if (kind == 'step' && frame['step'] is Map) {
final step = frame['step'] as Map;
if (step['intermediate'] == true) return thread;
return {
...thread,
'running': step['status'] != 'ended',
'nativeTurnId': step['status'] == 'ended'
? ''
: (step['turn_id'] ?? thread['nativeTurnId'] ?? ''),
};
}
return thread;
}
/// Queue serialized sends per session; operation ids survive retries/replays.
class QueuedChatSend {
QueuedChatSend({
required this.operationId,
required this.text,
this.files = const [],
});
final String operationId;
final String text;
final List<String> files;
/// Retry the same immutable intent with the same idempotency key.
QueuedChatSend retry() => QueuedChatSend(
operationId: operationId,
text: text,
files: List.unmodifiable(files),
);
}
class ChatSendQueue {
final Map<String, List<QueuedChatSend>> _sessions = {};
List<QueuedChatSend> forSession(String sessionId) =>
List.unmodifiable(_sessions[sessionId] ?? const []);
bool enqueue(String sessionId, QueuedChatSend send) {
final queue = _sessions.putIfAbsent(sessionId, () => []);
if (queue.any((item) => item.operationId == send.operationId)) return false;
queue.add(send);
return true;
}
QueuedChatSend? takeNext(String sessionId) {
final queue = _sessions[sessionId];
if (queue == null || queue.isEmpty) return null;
final result = queue.removeAt(0);
if (queue.isEmpty) _sessions.remove(sessionId);
return result;
}
}
/// The model of the latest stored reply, or empty when none recorded one.
String latestReplyModel(List<JsonMap> messages) {
for (final message in messages.reversed) {
if (message['role'] != 'assistant') continue;
final model = '${message['model'] ?? ''}'.trim();
if (model.isNotEmpty) return model;
}
return '';
}
int _nowMs() => DateTime.now().millisecondsSinceEpoch;