feat(app): show a recorded chat activity sequence as one line
Lift recorded activity/compaction parts out of an assistant turn into a single activity row before the answer. Running kinds use the web's phase words; terminal records keep the label the server recorded.
This commit is contained in:
@@ -41,6 +41,12 @@ JsonMap mergeChatHistory(JsonMap previous, JsonMap page, {bool older = false}) {
|
||||
/// currently holds so the view can keep the same row element while the turn
|
||||
/// grows at either end.
|
||||
///
|
||||
/// A recorded activity sequence is not an answer bubble. Parts whose `type` is
|
||||
/// `activity` or `compaction` are lifted out of the turn and collapse into one
|
||||
/// `activity` row before it, carrying the recorded records, the web wording for
|
||||
/// the latest record and that record's lifecycle state. Several updates of one
|
||||
/// compaction are therefore one line, never one line per update.
|
||||
///
|
||||
/// [trailing] rows are conversation cards (permission requests and questions),
|
||||
/// not messages. They keep the server's order after the transcript, the way the
|
||||
/// web renders approvals and forms at the end of the conversation.
|
||||
@@ -52,7 +58,19 @@ List<JsonMap> transcriptRows(
|
||||
String? key;
|
||||
final ids = <String>[];
|
||||
final parts = <JsonMap>[];
|
||||
final activity = <JsonMap>[];
|
||||
String? activityKey;
|
||||
void flush() {
|
||||
if (activity.isNotEmpty) {
|
||||
final latest = activity.last;
|
||||
rows.add({
|
||||
'kind': 'activity',
|
||||
'key': activityKey == null ? '' : 'chat-activity-$activityKey',
|
||||
'records': List<JsonMap>.of(activity),
|
||||
'label': chatActivityLabel(latest),
|
||||
'state': chatActivityState(latest).toLowerCase(),
|
||||
});
|
||||
}
|
||||
if (key != null && parts.isNotEmpty) {
|
||||
rows.add({
|
||||
'kind': 'assistant',
|
||||
@@ -64,6 +82,8 @@ List<JsonMap> transcriptRows(
|
||||
key = null;
|
||||
ids.clear();
|
||||
parts.clear();
|
||||
activity.clear();
|
||||
activityKey = null;
|
||||
}
|
||||
|
||||
for (final message in messages) {
|
||||
@@ -72,7 +92,14 @@ List<JsonMap> transcriptRows(
|
||||
key ??= id;
|
||||
if (id.isNotEmpty) ids.add(id);
|
||||
for (final part in (message['parts'] as List? ?? const [])) {
|
||||
if (part is Map) parts.add(Map<String, dynamic>.from(part));
|
||||
if (part is! Map) continue;
|
||||
final record = Map<String, dynamic>.from(part);
|
||||
if (chatActivityType(record)) {
|
||||
activityKey ??= id.isEmpty ? null : id;
|
||||
activity.add(record);
|
||||
} else {
|
||||
parts.add(record);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flush();
|
||||
@@ -84,6 +111,61 @@ List<JsonMap> transcriptRows(
|
||||
return rows;
|
||||
}
|
||||
|
||||
/// 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 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': 'Compacting context…',
|
||||
'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) {
|
||||
|
||||
Reference in New Issue
Block a user