Merge branch 'worktree-agent-a86b9c8a03da895c2'
This commit is contained in:
@@ -59,7 +59,14 @@ class ChatRowKeys {
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<String> _messageIds(JsonMap row) {
|
/// A row's ids, held per row object: rows are values (see
|
||||||
|
/// `transcriptRows`), and an unchanged turn is the same row across builds.
|
||||||
|
static final Expando<List<String>> _idsOf = Expando('chat row ids');
|
||||||
|
|
||||||
|
static List<String> _messageIds(JsonMap row) =>
|
||||||
|
_idsOf[row] ??= _computeMessageIds(row);
|
||||||
|
|
||||||
|
static List<String> _computeMessageIds(JsonMap row) {
|
||||||
// A sent message is the same row before and after the server's copy
|
// A sent message is the same row before and after the server's copy
|
||||||
// replaces the local one, even if the copy carries another id.
|
// replaces the local one, even if the copy carries another id.
|
||||||
final message = row['message'];
|
final message = row['message'];
|
||||||
|
|||||||
@@ -414,10 +414,7 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
// A frame for a cached message is fresh state the next read must not drop.
|
// A frame for a cached message is fresh state the next read must not drop.
|
||||||
final changed = body['message_id'];
|
final changed = body['message_id'];
|
||||||
if (changed != null) _untouchedCached?.remove(changed);
|
if (changed != null) _untouchedCached?.remove(changed);
|
||||||
_messages = (next['messages'] as List? ?? const [])
|
_messages = chatMessageList(next['messages']);
|
||||||
.whereType<Map>()
|
|
||||||
.map((message) => Map<String, dynamic>.from(message))
|
|
||||||
.toList();
|
|
||||||
_running = next['running'] == true;
|
_running = next['running'] == true;
|
||||||
if (!_running) {
|
if (!_running) {
|
||||||
_liveAssistantMessageId = null;
|
_liveAssistantMessageId = null;
|
||||||
|
|||||||
@@ -234,6 +234,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
ChatShellRunsController(widget.gateway, widget.sessionId)
|
ChatShellRunsController(widget.gateway, widget.sessionId)
|
||||||
..addListener(_reportFinishedRuns);
|
..addListener(_reportFinishedRuns);
|
||||||
late ChatRowKeys _rowKeys = ChatRowKeys();
|
late ChatRowKeys _rowKeys = ChatRowKeys();
|
||||||
|
// Keeps the rows of unchanged turns across builds; a streamed delta only
|
||||||
|
// re-derives the live turn.
|
||||||
|
ChatTranscriptRows _transcriptRows = ChatTranscriptRows();
|
||||||
|
// The last [_messageIds] and the message list it was read from.
|
||||||
|
List<JsonMap>? _idsSource;
|
||||||
|
List<String> _ids = const [];
|
||||||
final _sentEntrances = ChatSentEntrances();
|
final _sentEntrances = ChatSentEntrances();
|
||||||
final _rowWidgets = ChatRowWidgets();
|
final _rowWidgets = ChatRowWidgets();
|
||||||
|
|
||||||
@@ -711,6 +717,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
_controller = _attach();
|
_controller = _attach();
|
||||||
_composer = _attachComposer();
|
_composer = _attachComposer();
|
||||||
_rowKeys = ChatRowKeys();
|
_rowKeys = ChatRowKeys();
|
||||||
|
_transcriptRows = ChatTranscriptRows();
|
||||||
_rowWidgets.clear();
|
_rowWidgets.clear();
|
||||||
_chrome.clear();
|
_chrome.clear();
|
||||||
_dismissals = ChatNoticeDismissals();
|
_dismissals = ChatNoticeDismissals();
|
||||||
@@ -797,10 +804,21 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
|
|
||||||
/// The ids of the transcript's messages in view order. Cards are conversation
|
/// The ids of the transcript's messages in view order. Cards are conversation
|
||||||
/// state, not messages, so a pending approval does not count as an arrival.
|
/// state, not messages, so a pending approval does not count as an arrival.
|
||||||
List<String> _messageIds() => [
|
///
|
||||||
for (final message in _visibleMessages())
|
/// The controller replaces its message list on every change, so the ids are
|
||||||
if ('${message['id'] ?? ''}'.trim().isNotEmpty) '${message['id']}'.trim(),
|
/// read once per list rather than on every call within a build.
|
||||||
];
|
List<String> _messageIds() {
|
||||||
|
final messages = _visibleMessages();
|
||||||
|
if (!identical(messages, _idsSource)) {
|
||||||
|
_idsSource = messages;
|
||||||
|
_ids = List.unmodifiable([
|
||||||
|
for (final message in messages)
|
||||||
|
if ('${message['id'] ?? ''}'.trim().isNotEmpty)
|
||||||
|
'${message['id']}'.trim(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return _ids;
|
||||||
|
}
|
||||||
|
|
||||||
String? _newestMessageId() {
|
String? _newestMessageId() {
|
||||||
final ids = _messageIds();
|
final ids = _messageIds();
|
||||||
@@ -1119,7 +1137,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
groupSettledQuestions(
|
groupSettledQuestions(
|
||||||
groupSettledPermissions(
|
groupSettledPermissions(
|
||||||
withChatShownItems(
|
withChatShownItems(
|
||||||
transcriptRows(
|
_transcriptRows.rows(
|
||||||
messages,
|
messages,
|
||||||
trailing: cards,
|
trailing: cards,
|
||||||
running:
|
running:
|
||||||
|
|||||||
@@ -72,7 +72,39 @@ int? _rowTime(JsonMap row) {
|
|||||||
final own = _createdAt(row);
|
final own = _createdAt(row);
|
||||||
if (own != null) return own;
|
if (own != null) return own;
|
||||||
final message = row['message'];
|
final message = row['message'];
|
||||||
return message is Map ? _createdAt(Map<String, dynamic>.from(message)) : null;
|
if (message is! Map) return null;
|
||||||
|
return _createdAt(
|
||||||
|
message is JsonMap ? message : Map<String, dynamic>.from(message),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derived per input object. Rows and items are values that are never changed
|
||||||
|
// in place (see [transcriptRows]), so an unchanged turn keeps one derived row
|
||||||
|
// object across builds instead of an equal copy per build.
|
||||||
|
final Expando<List<JsonMap>> _withoutTools = Expando('chat row without tools');
|
||||||
|
final Expando<JsonMap> _shownRows = Expando('chat shown row');
|
||||||
|
|
||||||
|
/// [row] without its represented Pipelines tool calls: the row itself when it
|
||||||
|
/// has none, nothing when they were all it held.
|
||||||
|
List<JsonMap> _withoutToolCalls(JsonMap row) {
|
||||||
|
final parts = row['parts'];
|
||||||
|
if (row['kind'] != 'assistant' || parts is! List) return [row];
|
||||||
|
final kept = [
|
||||||
|
for (final part in parts.whereType<Map>())
|
||||||
|
if (!chatPipelinesToolRepresented(
|
||||||
|
part is JsonMap ? part : Map<String, dynamic>.from(part),
|
||||||
|
))
|
||||||
|
part,
|
||||||
|
];
|
||||||
|
if (kept.length == parts.length) return [row];
|
||||||
|
if (kept.isNotEmpty ||
|
||||||
|
row['streaming'] == true ||
|
||||||
|
row['activityRecords'] != null) {
|
||||||
|
return [
|
||||||
|
{...row, 'parts': kept},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return const [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [rows] (from [transcriptRows]) with the Pipelines tool calls removed from
|
/// [rows] (from [transcriptRows]) with the Pipelines tool calls removed from
|
||||||
@@ -88,42 +120,26 @@ List<JsonMap> withChatShownItems(
|
|||||||
List<JsonMap> items, {
|
List<JsonMap> items, {
|
||||||
required bool hasMore,
|
required bool hasMore,
|
||||||
}) {
|
}) {
|
||||||
final out = <JsonMap>[];
|
final out = <JsonMap>[
|
||||||
for (final row in rows) {
|
for (final row in rows) ...(_withoutTools[row] ??= _withoutToolCalls(row)),
|
||||||
final parts = row['parts'];
|
];
|
||||||
if (row['kind'] != 'assistant' || parts is! List) {
|
|
||||||
out.add(row);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
final kept = [
|
|
||||||
for (final part in parts.whereType<Map>())
|
|
||||||
if (!chatPipelinesToolRepresented(Map<String, dynamic>.from(part)))
|
|
||||||
part,
|
|
||||||
];
|
|
||||||
if (kept.length == parts.length) {
|
|
||||||
out.add(row);
|
|
||||||
} else if (kept.isNotEmpty ||
|
|
||||||
row['streaming'] == true ||
|
|
||||||
row['activityRecords'] != null) {
|
|
||||||
out.add({...row, 'parts': kept});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (items.isEmpty) return out;
|
if (items.isEmpty) return out;
|
||||||
final timed = [
|
final timed = [
|
||||||
for (final item in items)
|
for (final item in items)
|
||||||
if (_createdAt(item) != null) item,
|
if (_createdAt(item) != null) item,
|
||||||
]..sort((a, b) => _createdAt(a)!.compareTo(_createdAt(b)!));
|
]..sort((a, b) => _createdAt(a)!.compareTo(_createdAt(b)!));
|
||||||
|
final times = [for (final row in out) _rowTime(row)];
|
||||||
int? oldest;
|
int? oldest;
|
||||||
var lastTimed = -1;
|
var lastTimed = -1;
|
||||||
for (var index = 0; index < out.length; index++) {
|
for (var index = 0; index < out.length; index++) {
|
||||||
final time = _rowTime(out[index]);
|
final time = times[index];
|
||||||
if (time == null) continue;
|
if (time == null) continue;
|
||||||
oldest ??= time;
|
oldest ??= time;
|
||||||
lastTimed = index;
|
lastTimed = index;
|
||||||
}
|
}
|
||||||
final result = <JsonMap>[];
|
final result = <JsonMap>[];
|
||||||
var next = 0;
|
var next = 0;
|
||||||
JsonMap rowOf(JsonMap item) => {
|
JsonMap rowOf(JsonMap item) => _shownRows[item] ??= {
|
||||||
'kind': chatShownRowKind,
|
'kind': chatShownRowKind,
|
||||||
'key': 'chat-shown-${item['id']}',
|
'key': 'chat-shown-${item['id']}',
|
||||||
'messageIds': ['shown:${item['id']}'],
|
'messageIds': ['shown:${item['id']}'],
|
||||||
@@ -136,7 +152,7 @@ List<JsonMap> withChatShownItems(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var index = 0; index < out.length; index++) {
|
for (var index = 0; index < out.length; index++) {
|
||||||
final time = _rowTime(out[index]);
|
final time = times[index];
|
||||||
if (time != null) {
|
if (time != null) {
|
||||||
while (next < timed.length && _createdAt(timed[next])! < time) {
|
while (next < timed.length && _createdAt(timed[next])! < time) {
|
||||||
result.add(rowOf(timed[next++]));
|
result.add(rowOf(timed[next++]));
|
||||||
|
|||||||
@@ -59,6 +59,20 @@ ChatTaskNotice? _rowNotice(JsonMap row) {
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The group row last built for a run, by the run's first row. Rows are values
|
||||||
|
// (see `transcriptRows`), so the same run yields the same group object.
|
||||||
|
final Expando<({List<JsonMap> run, JsonMap group})> _groups = Expando(
|
||||||
|
'chat task notice group',
|
||||||
|
);
|
||||||
|
|
||||||
|
bool _identicalRows(List<JsonMap> a, List<JsonMap> b) {
|
||||||
|
if (a.length != b.length) return false;
|
||||||
|
for (var index = 0; index < a.length; index++) {
|
||||||
|
if (!identical(a[index], b[index])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// Folds each run of two or more consecutive background-task notice rows into
|
/// Folds each run of two or more consecutive background-task notice rows into
|
||||||
/// one `task_notices` row, the way a turn's tool calls fold into one chip.
|
/// one `task_notices` row, the way a turn's tool calls fold into one chip.
|
||||||
///
|
///
|
||||||
@@ -73,14 +87,21 @@ List<JsonMap> groupTaskNotices(List<JsonMap> rows) {
|
|||||||
if (run.length == 1) {
|
if (run.length == 1) {
|
||||||
result.add(run.single);
|
result.add(run.single);
|
||||||
} else if (run.isNotEmpty) {
|
} else if (run.isNotEmpty) {
|
||||||
result.add({
|
final held = _groups[run.first];
|
||||||
'kind': 'task_notices',
|
if (held != null && _identicalRows(held.run, run)) {
|
||||||
'key': run.first['key'],
|
result.add(held.group);
|
||||||
// Every grouped message's id, so the row keeps one widget identity as
|
} else {
|
||||||
// notices join it at either end (see ChatRowKeys).
|
final group = <String, dynamic>{
|
||||||
'messageIds': [for (final row in run) '${row['key'] ?? ''}'],
|
'kind': 'task_notices',
|
||||||
'messages': [for (final row in run) row['message']],
|
'key': run.first['key'],
|
||||||
});
|
// Every grouped message's id, so the row keeps one widget identity
|
||||||
|
// as notices join it at either end (see ChatRowKeys).
|
||||||
|
'messageIds': [for (final row in run) '${row['key'] ?? ''}'],
|
||||||
|
'messages': [for (final row in run) row['message']],
|
||||||
|
};
|
||||||
|
_groups[run.first] = (run: List<JsonMap>.of(run), group: group);
|
||||||
|
result.add(group);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
run.clear();
|
run.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import '../../../core/api_gateway.dart';
|
import '../../../core/api_gateway.dart';
|
||||||
import 'chat_task_notices.dart';
|
|
||||||
|
export 'chat_transcript_rows.dart';
|
||||||
|
|
||||||
/// Merge chronological history pages by message id. Incoming authoritative
|
/// Merge chronological history pages by message id. Incoming authoritative
|
||||||
/// copies replace held copies without changing their order; cursor is opaque.
|
/// copies replace held copies without changing their order; cursor is opaque.
|
||||||
@@ -56,158 +57,6 @@ JsonMap refreshCachedChatHistory(
|
|||||||
return mergeChatHistory({'messages': live}, page, older: true);
|
return mergeChatHistory({'messages': live}, page, older: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consecutive assistant messages form one answer turn. All part fields (also
|
|
||||||
/// future/unknown protocol types) are preserved in original order.
|
|
||||||
///
|
|
||||||
/// `key` is the turn's first assistant message id, which is only a display
|
|
||||||
/// anchor: paging an older assistant message into the turn changes it, so widget
|
|
||||||
/// identity uses `messageIds`. That list carries every message the folded turn
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// A message the server marks with a `kind` is content the harness delivered,
|
|
||||||
/// not a turn: an `agent_report` becomes its own `agent_report` row and a
|
|
||||||
/// `notice` its own `system_notice` row, on the agent's side, never folded into
|
|
||||||
/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]).
|
|
||||||
/// Two or more consecutive background-task notices fold into one
|
|
||||||
/// `task_notices` row (see [groupTaskNotices]).
|
|
||||||
///
|
|
||||||
/// [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.
|
|
||||||
List<JsonMap> transcriptRows(
|
|
||||||
List<JsonMap> messages, {
|
|
||||||
List<JsonMap> trailing = const [],
|
|
||||||
bool running = false,
|
|
||||||
bool awaitingResponse = false,
|
|
||||||
String? liveMessageId,
|
|
||||||
}) {
|
|
||||||
final rows = <JsonMap>[];
|
|
||||||
String? key;
|
|
||||||
String model = '';
|
|
||||||
final ids = <String>[];
|
|
||||||
final parts = <JsonMap>[];
|
|
||||||
final activity = <JsonMap>[];
|
|
||||||
String? activityKey;
|
|
||||||
int? createdAt;
|
|
||||||
void flush() {
|
|
||||||
final compactions = activity
|
|
||||||
.where((p) => chatActivityKind(p) == 'compaction')
|
|
||||||
.toList();
|
|
||||||
final passive = activity
|
|
||||||
.where((p) => chatActivityKind(p) != 'compaction')
|
|
||||||
.toList();
|
|
||||||
if (compactions.isNotEmpty) {
|
|
||||||
final latest = compactions.last;
|
|
||||||
rows.add({
|
|
||||||
'kind': 'activity',
|
|
||||||
'key': activityKey == null ? '' : 'chat-activity-$activityKey',
|
|
||||||
'records': compactions,
|
|
||||||
'label': chatActivityLabel(latest),
|
|
||||||
'state': chatActivityState(latest).toLowerCase(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (key != null && (parts.isNotEmpty || passive.isNotEmpty)) {
|
|
||||||
rows.add({
|
|
||||||
'kind': 'assistant',
|
|
||||||
'key': key,
|
|
||||||
'messageIds': List<String>.of(ids),
|
|
||||||
'parts': List<JsonMap>.of(parts),
|
|
||||||
if (passive.isNotEmpty) 'activityRecords': passive,
|
|
||||||
// The model that answered, as the server recorded it on the reply.
|
|
||||||
if (model.isNotEmpty) 'model': model,
|
|
||||||
'created_at': ?createdAt,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
key = null;
|
|
||||||
model = '';
|
|
||||||
ids.clear();
|
|
||||||
parts.clear();
|
|
||||||
activity.clear();
|
|
||||||
activityKey = null;
|
|
||||||
createdAt = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (final message in messages) {
|
|
||||||
final injected = chatInjectedKind(message);
|
|
||||||
if (injected != null) {
|
|
||||||
flush();
|
|
||||||
rows.add({
|
|
||||||
'kind': injected == chatKindAgentReport
|
|
||||||
? 'agent_report'
|
|
||||||
: 'system_notice',
|
|
||||||
'key': message['id'],
|
|
||||||
'message': message,
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (message['role'] == 'assistant') {
|
|
||||||
final id = '${message['id'] ?? ''}'.trim();
|
|
||||||
key ??= id;
|
|
||||||
createdAt ??= chatMessageCreatedAt(message);
|
|
||||||
final recorded = '${message['model'] ?? ''}'.trim();
|
|
||||||
if (recorded.isNotEmpty) model = recorded;
|
|
||||||
if (id.isNotEmpty) ids.add(id);
|
|
||||||
for (final part in (message['parts'] as List? ?? const [])) {
|
|
||||||
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();
|
|
||||||
rows.add({'kind': 'user', 'key': message['id'], 'message': message});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
flush();
|
|
||||||
// Prefer the assistant identity that actually streamed: a queued follow-up
|
|
||||||
// can be later in the transcript while this same reply is still arriving.
|
|
||||||
// Before a first delta, do not reopen an older reply behind a new user turn.
|
|
||||||
final answers = [
|
|
||||||
for (final message in messages)
|
|
||||||
if (chatInjectedKind(message) == null) message,
|
|
||||||
];
|
|
||||||
final streamingId =
|
|
||||||
liveMessageId ??
|
|
||||||
(answers.isNotEmpty && answers.last['role'] == 'assistant'
|
|
||||||
? '${answers.last['id'] ?? ''}'
|
|
||||||
: null);
|
|
||||||
var hasStreamingRow = false;
|
|
||||||
if (running && streamingId != null) {
|
|
||||||
for (final row in rows.reversed) {
|
|
||||||
if (row['kind'] == 'assistant' &&
|
|
||||||
(row['messageIds'] as List).contains(streamingId)) {
|
|
||||||
row['streaming'] = true;
|
|
||||||
hasStreamingRow = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Display-only response shell: never inserted into stored message history.
|
|
||||||
// A queued follow-up does not create a second shell beside the active reply.
|
|
||||||
if (!hasStreamingRow && (running || awaitingResponse)) {
|
|
||||||
rows.add({
|
|
||||||
'kind': 'assistant',
|
|
||||||
'key': 'chat-pending-response',
|
|
||||||
'messageIds': <String>[],
|
|
||||||
'parts': <JsonMap>[],
|
|
||||||
'streaming': true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Consecutive background-task notices are one collapsed group.
|
|
||||||
return [...groupTaskNotices(rows), ...trailing];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The protocol records creation in epoch milliseconds. Invalid values do not
|
/// The protocol records creation in epoch milliseconds. Invalid values do not
|
||||||
/// acquire an invented display time.
|
/// acquire an invented display time.
|
||||||
int? chatMessageCreatedAt(JsonMap message) {
|
int? chatMessageCreatedAt(JsonMap message) {
|
||||||
@@ -312,6 +161,14 @@ List<JsonMap> reconcileChatUser(List<JsonMap> messages, JsonMap remote) {
|
|||||||
return result;
|
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
|
/// 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,
|
/// deltas append by part id, tool snapshots merge without dropping prior fields,
|
||||||
/// and step frames update turn activity. Frames for another session are ignored.
|
/// and step frames update turn activity. Frames for another session are ignored.
|
||||||
@@ -322,10 +179,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) {
|
|||||||
session != frame['session_id']) {
|
session != frame['session_id']) {
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
final messages = (thread['messages'] as List? ?? const [])
|
// Untouched messages keep their identity, so derived rows held for them
|
||||||
.whereType<Map>()
|
// (see ChatTranscriptRows) are reused; a changed message is a new map.
|
||||||
.map((m) => Map<String, dynamic>.from(m))
|
final messages = chatMessageList(thread['messages']);
|
||||||
.toList();
|
|
||||||
final kind = frame['kind'];
|
final kind = frame['kind'];
|
||||||
if (kind == 'user') {
|
if (kind == 'user') {
|
||||||
final part = frame['part'];
|
final part = frame['part'];
|
||||||
|
|||||||
@@ -0,0 +1,257 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import '../../../core/api_gateway.dart';
|
||||||
|
import 'chat_task_notices.dart';
|
||||||
|
import 'chat_transcript.dart';
|
||||||
|
|
||||||
|
/// Consecutive assistant messages form one answer turn. All part fields (also
|
||||||
|
/// future/unknown protocol types) are preserved in original order.
|
||||||
|
///
|
||||||
|
/// `key` is the turn's first assistant message id, which is only a display
|
||||||
|
/// anchor: paging an older assistant message into the turn changes it, so widget
|
||||||
|
/// identity uses `messageIds`. That list carries every message the folded turn
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
/// A message the server marks with a `kind` is content the harness delivered,
|
||||||
|
/// not a turn: an `agent_report` becomes its own `agent_report` row and a
|
||||||
|
/// `notice` its own `system_notice` row, on the agent's side, never folded into
|
||||||
|
/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]).
|
||||||
|
/// Two or more consecutive background-task notices fold into one
|
||||||
|
/// `task_notices` row (see [groupTaskNotices]).
|
||||||
|
///
|
||||||
|
/// [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.
|
||||||
|
///
|
||||||
|
/// Rows are values: nothing downstream changes a row in place, which is what
|
||||||
|
/// lets [ChatTranscriptRows] hand out the same rows again.
|
||||||
|
List<JsonMap> transcriptRows(
|
||||||
|
List<JsonMap> messages, {
|
||||||
|
List<JsonMap> trailing = const [],
|
||||||
|
bool running = false,
|
||||||
|
bool awaitingResponse = false,
|
||||||
|
String? liveMessageId,
|
||||||
|
}) => _finish(
|
||||||
|
[for (final segment in _segments(messages)) ..._segmentRows(segment)],
|
||||||
|
messages,
|
||||||
|
trailing: trailing,
|
||||||
|
running: running,
|
||||||
|
awaitingResponse: awaitingResponse,
|
||||||
|
liveMessageId: liveMessageId,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// [transcriptRows] for a transcript that changes a little at a time.
|
||||||
|
///
|
||||||
|
/// The rows of a run of consecutive answer messages, or of one other message,
|
||||||
|
/// depend on those messages alone. Each run's rows are kept while the run holds
|
||||||
|
/// the identical message objects, so a streamed delta (which replaces only the
|
||||||
|
/// live message) rebuilds only the live turn, and a prepended history page
|
||||||
|
/// only the runs it touches. The result equals [transcriptRows] for the same
|
||||||
|
/// arguments; messages must not be changed in place once passed.
|
||||||
|
class ChatTranscriptRows {
|
||||||
|
/// The previous call's runs, by their first message.
|
||||||
|
Map<JsonMap, _Segment> _held = HashMap.identity();
|
||||||
|
|
||||||
|
List<JsonMap> rows(
|
||||||
|
List<JsonMap> messages, {
|
||||||
|
List<JsonMap> trailing = const [],
|
||||||
|
bool running = false,
|
||||||
|
bool awaitingResponse = false,
|
||||||
|
String? liveMessageId,
|
||||||
|
}) {
|
||||||
|
final next = HashMap<JsonMap, _Segment>.identity();
|
||||||
|
final rows = <JsonMap>[];
|
||||||
|
for (final messages in _segments(messages)) {
|
||||||
|
final held = _held[messages.first];
|
||||||
|
final segment = held != null && _sameMessages(held.messages, messages)
|
||||||
|
? held
|
||||||
|
: _Segment(messages, _segmentRows(messages));
|
||||||
|
next[messages.first] = segment;
|
||||||
|
rows.addAll(segment.rows);
|
||||||
|
}
|
||||||
|
_held = next;
|
||||||
|
return _finish(
|
||||||
|
rows,
|
||||||
|
messages,
|
||||||
|
trailing: trailing,
|
||||||
|
running: running,
|
||||||
|
awaitingResponse: awaitingResponse,
|
||||||
|
liveMessageId: liveMessageId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _sameMessages(List<JsonMap> a, List<JsonMap> b) {
|
||||||
|
if (a.length != b.length) return false;
|
||||||
|
for (var index = 0; index < a.length; index++) {
|
||||||
|
if (!identical(a[index], b[index])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Segment {
|
||||||
|
_Segment(this.messages, this.rows);
|
||||||
|
final List<JsonMap> messages;
|
||||||
|
final List<JsonMap> rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether [message] folds into an answer turn with its assistant neighbours.
|
||||||
|
bool _answerPart(JsonMap message) =>
|
||||||
|
chatInjectedKind(message) == null && message['role'] == 'assistant';
|
||||||
|
|
||||||
|
/// [messages] split into runs whose rows depend on the run alone: each maximal
|
||||||
|
/// run of answer messages, and every other message on its own.
|
||||||
|
List<List<JsonMap>> _segments(List<JsonMap> messages) {
|
||||||
|
final segments = <List<JsonMap>>[];
|
||||||
|
List<JsonMap>? answer;
|
||||||
|
for (final message in messages) {
|
||||||
|
if (_answerPart(message)) {
|
||||||
|
if (answer == null) segments.add(answer = []);
|
||||||
|
answer.add(message);
|
||||||
|
} else {
|
||||||
|
answer = null;
|
||||||
|
segments.add([message]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return segments;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The rows of one run from [_segments].
|
||||||
|
List<JsonMap> _segmentRows(List<JsonMap> segment) {
|
||||||
|
final first = segment.first;
|
||||||
|
if (!_answerPart(first)) {
|
||||||
|
final injected = chatInjectedKind(first);
|
||||||
|
if (injected != null) {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
'kind': injected == chatKindAgentReport
|
||||||
|
? 'agent_report'
|
||||||
|
: 'system_notice',
|
||||||
|
'key': first['id'],
|
||||||
|
'message': first,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
{'kind': 'user', 'key': first['id'], 'message': first},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
final rows = <JsonMap>[];
|
||||||
|
String? key;
|
||||||
|
String model = '';
|
||||||
|
final ids = <String>[];
|
||||||
|
final parts = <JsonMap>[];
|
||||||
|
final activity = <JsonMap>[];
|
||||||
|
String? activityKey;
|
||||||
|
int? createdAt;
|
||||||
|
for (final message in segment) {
|
||||||
|
final id = '${message['id'] ?? ''}'.trim();
|
||||||
|
key ??= id;
|
||||||
|
createdAt ??= chatMessageCreatedAt(message);
|
||||||
|
final recorded = '${message['model'] ?? ''}'.trim();
|
||||||
|
if (recorded.isNotEmpty) model = recorded;
|
||||||
|
if (id.isNotEmpty) ids.add(id);
|
||||||
|
for (final part in (message['parts'] as List? ?? const [])) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final compactions = activity
|
||||||
|
.where((p) => chatActivityKind(p) == 'compaction')
|
||||||
|
.toList();
|
||||||
|
final passive = activity
|
||||||
|
.where((p) => chatActivityKind(p) != 'compaction')
|
||||||
|
.toList();
|
||||||
|
if (compactions.isNotEmpty) {
|
||||||
|
final latest = compactions.last;
|
||||||
|
rows.add({
|
||||||
|
'kind': 'activity',
|
||||||
|
'key': activityKey == null ? '' : 'chat-activity-$activityKey',
|
||||||
|
'records': compactions,
|
||||||
|
'label': chatActivityLabel(latest),
|
||||||
|
'state': chatActivityState(latest).toLowerCase(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (parts.isNotEmpty || passive.isNotEmpty) {
|
||||||
|
rows.add({
|
||||||
|
'kind': 'assistant',
|
||||||
|
'key': key,
|
||||||
|
'messageIds': ids,
|
||||||
|
'parts': parts,
|
||||||
|
if (passive.isNotEmpty) 'activityRecords': passive,
|
||||||
|
// The model that answered, as the server recorded it on the reply.
|
||||||
|
if (model.isNotEmpty) 'model': model,
|
||||||
|
'created_at': ?createdAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The streaming mark, the response shell, task-notice grouping and the
|
||||||
|
/// trailing cards over the transcript's own [rows]. [rows] is not changed: a
|
||||||
|
/// marked row is a copy.
|
||||||
|
List<JsonMap> _finish(
|
||||||
|
List<JsonMap> rows,
|
||||||
|
List<JsonMap> messages, {
|
||||||
|
required List<JsonMap> trailing,
|
||||||
|
required bool running,
|
||||||
|
required bool awaitingResponse,
|
||||||
|
required String? liveMessageId,
|
||||||
|
}) {
|
||||||
|
// Prefer the assistant identity that actually streamed: a queued follow-up
|
||||||
|
// can be later in the transcript while this same reply is still arriving.
|
||||||
|
// Before a first delta, do not reopen an older reply behind a new user turn.
|
||||||
|
String? streamingId = liveMessageId;
|
||||||
|
if (streamingId == null) {
|
||||||
|
for (final message in messages.reversed) {
|
||||||
|
if (chatInjectedKind(message) != null) continue;
|
||||||
|
if (message['role'] == 'assistant') {
|
||||||
|
streamingId = '${message['id'] ?? ''}';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var marked = rows;
|
||||||
|
var hasStreamingRow = false;
|
||||||
|
if (running && streamingId != null) {
|
||||||
|
for (var index = rows.length - 1; index >= 0; index--) {
|
||||||
|
final row = rows[index];
|
||||||
|
if (row['kind'] == 'assistant' &&
|
||||||
|
(row['messageIds'] as List).contains(streamingId)) {
|
||||||
|
marked = List<JsonMap>.of(rows);
|
||||||
|
marked[index] = {...row, 'streaming': true};
|
||||||
|
hasStreamingRow = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Display-only response shell: never inserted into stored message history.
|
||||||
|
// A queued follow-up does not create a second shell beside the active reply.
|
||||||
|
if (!hasStreamingRow && (running || awaitingResponse)) {
|
||||||
|
marked = [
|
||||||
|
...marked,
|
||||||
|
{
|
||||||
|
'kind': 'assistant',
|
||||||
|
'key': 'chat-pending-response',
|
||||||
|
'messageIds': <String>[],
|
||||||
|
'parts': <JsonMap>[],
|
||||||
|
'streaming': true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
// Consecutive background-task notices are one collapsed group.
|
||||||
|
return [...groupTaskNotices(marked), ...trailing];
|
||||||
|
}
|
||||||
@@ -55,6 +55,12 @@ timeline) in the self-time lists are measurement overhead.
|
|||||||
16 ms, `sliver_animated_extent` layout 250 ms inclusive over the stream.
|
16 ms, `sliver_animated_extent` layout 250 ms inclusive over the stream.
|
||||||
Every delta re-derives all rows of the transcript; only the live message
|
Every delta re-derives all rows of the transcript; only the live message
|
||||||
changed. Memoise the settled rows and recompute the tail.
|
changed. Memoise the settled rows and recompute the tail.
|
||||||
|
Change (not yet re-benchmarked): `ChatTranscriptRows`
|
||||||
|
(`chat_transcript_rows.dart`) keeps each message run's rows while it holds
|
||||||
|
the identical message objects, `applyChatEvent` keeps untouched messages'
|
||||||
|
identity, and the shown-item filter, task-notice groups and
|
||||||
|
`ChatRowKeys` ids are held per row object. Equality with a full derivation
|
||||||
|
is covered by `test/features/chat/conversation/chat_transcript_rows_memo_test.dart`.
|
||||||
5. **Spinner during streaming**: `CircularProgressIndicator`'s controller
|
5. **Spinner during streaming**: `CircularProgressIndicator`'s controller
|
||||||
getter looks up an inherited widget every frame (89 ms over the stream) on
|
getter looks up an inherited widget every frame (89 ms over the stream) on
|
||||||
top of the border animation; two 60 fps animations for one running turn.
|
top of the border animation; two 60 fps animations for one running turn.
|
||||||
|
|||||||
@@ -0,0 +1,321 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_permission_card_model.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_permission_group.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_question_group.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_question_card_model.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_row_keys.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_turn_diffs.dart';
|
||||||
|
|
||||||
|
/// The thread view's inputs to its row derivation.
|
||||||
|
class _State {
|
||||||
|
List<JsonMap> messages = [];
|
||||||
|
List<JsonMap> cards = [];
|
||||||
|
List<JsonMap> shown = [];
|
||||||
|
bool hasMore = false;
|
||||||
|
bool running = false;
|
||||||
|
String? live;
|
||||||
|
final diffs = ChatTurnDiffs();
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef _Rows =
|
||||||
|
List<JsonMap> Function(
|
||||||
|
List<JsonMap> messages, {
|
||||||
|
List<JsonMap> trailing,
|
||||||
|
bool running,
|
||||||
|
bool awaitingResponse,
|
||||||
|
String? liveMessageId,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// The chain the thread view runs, with [rows] as its first stage.
|
||||||
|
List<JsonMap> _derive(_Rows rows, _State state) => attachChatTurnDiffs(
|
||||||
|
groupSettledQuestions(
|
||||||
|
groupSettledPermissions(
|
||||||
|
withChatShownItems(
|
||||||
|
rows(
|
||||||
|
state.messages,
|
||||||
|
trailing: state.cards,
|
||||||
|
running: state.running,
|
||||||
|
awaitingResponse: false,
|
||||||
|
liveMessageId: state.live,
|
||||||
|
),
|
||||||
|
state.shown,
|
||||||
|
hasMore: state.hasMore,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
state.diffs,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Comparable form of a row list: permission cards are plain objects.
|
||||||
|
Object? _plain(Object? value) => switch (value) {
|
||||||
|
ChatPermissionCardData card => {
|
||||||
|
'requestId': card.requestId,
|
||||||
|
'action': card.action,
|
||||||
|
'state': '${card.state}',
|
||||||
|
'decision': card.decision,
|
||||||
|
'headline': card.headline,
|
||||||
|
'statusLine': card.statusLine,
|
||||||
|
},
|
||||||
|
Map map => {for (final e in map.entries) e.key: _plain(e.value)},
|
||||||
|
List list => [for (final item in list) _plain(item)],
|
||||||
|
_ => value,
|
||||||
|
};
|
||||||
|
|
||||||
|
const _base = 1700000000000;
|
||||||
|
|
||||||
|
List<JsonMap> _history(int from, int to) => [
|
||||||
|
for (var i = from; i < to; i++) ...[
|
||||||
|
{
|
||||||
|
'id': 'u$i',
|
||||||
|
'role': 'user',
|
||||||
|
'created_at': _base + i * 1000,
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'text': 'question $i'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'a$i',
|
||||||
|
'role': 'assistant',
|
||||||
|
'model': 'm',
|
||||||
|
'created_at': _base + i * 1000 + 100,
|
||||||
|
'parts': [
|
||||||
|
{'type': 'reasoning', 'id': 'r$i', 'text': 'thinking $i'},
|
||||||
|
{
|
||||||
|
'type': 'tool',
|
||||||
|
'id': 'call-$i',
|
||||||
|
'name': i % 5 == 0 ? 'mcp__pipelines__show_file' : 'Bash',
|
||||||
|
'state': {'status': 'completed'},
|
||||||
|
},
|
||||||
|
if (i % 7 == 0)
|
||||||
|
{'type': 'compaction', 'id': 'c$i', 'state': 'completed'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
// A second message in the same turn.
|
||||||
|
if (i.isOdd)
|
||||||
|
{
|
||||||
|
'id': 'b$i',
|
||||||
|
'role': 'assistant',
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'id': 't$i', 'text': 'answer $i'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
if (i % 9 == 0) ...[
|
||||||
|
for (final n in [1, 2])
|
||||||
|
{
|
||||||
|
'id': 'n$i-$n',
|
||||||
|
'role': 'assistant',
|
||||||
|
'kind': 'notice',
|
||||||
|
'created_at': _base + i * 1000 + 500 + n,
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'text': 'Background task finished: job $n'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
if (i % 11 == 0)
|
||||||
|
{
|
||||||
|
'id': 'r$i',
|
||||||
|
'role': 'assistant',
|
||||||
|
'kind': 'agent_report',
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'text': 'report $i'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
JsonMap _thread(_State state) => {
|
||||||
|
'sessionId': 's1',
|
||||||
|
'messages': state.messages,
|
||||||
|
'running': state.running,
|
||||||
|
};
|
||||||
|
|
||||||
|
void _apply(_State state, JsonMap frame) {
|
||||||
|
final next = applyChatEvent(_thread(state), {'session_id': 's1', ...frame});
|
||||||
|
state.messages = chatMessageList(next['messages']);
|
||||||
|
state.running = next['running'] == true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _delta(_State state, String messageId, String text) {
|
||||||
|
_apply(state, {
|
||||||
|
'kind': 'text',
|
||||||
|
'message_id': messageId,
|
||||||
|
'part': {'id': 'live-$messageId'},
|
||||||
|
'delta': text,
|
||||||
|
});
|
||||||
|
state.live = messageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late _State state;
|
||||||
|
late ChatTranscriptRows memo;
|
||||||
|
late ChatRowKeys memoKeys;
|
||||||
|
late ChatRowKeys fullKeys;
|
||||||
|
late List<JsonMap> previous;
|
||||||
|
|
||||||
|
/// Derives through the memo and from scratch, and checks they agree on rows
|
||||||
|
/// and on the keys a long-lived [ChatRowKeys] assigns them.
|
||||||
|
List<JsonMap> check() {
|
||||||
|
final incremental = _derive(memo.rows, state);
|
||||||
|
final full = _derive(transcriptRows, state);
|
||||||
|
expect(_plain(incremental), _plain(full));
|
||||||
|
expect(memoKeys.forRows(incremental), fullKeys.forRows(full));
|
||||||
|
return previous = incremental;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rows of [now] that are the very objects [before] held for the same key.
|
||||||
|
int reused(List<JsonMap> before, List<JsonMap> now) {
|
||||||
|
final held = {for (final row in before) '${row['key']}': row};
|
||||||
|
return now.where((row) => identical(held['${row['key']}'], row)).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
state = _State()..messages = _history(10, 40);
|
||||||
|
state.shown = [
|
||||||
|
{'id': 'f1', 'kind': 'file', 'created_at': _base + 20 * 1000 + 50},
|
||||||
|
{'id': 'f0', 'kind': 'file', 'created_at': _base + 1000},
|
||||||
|
];
|
||||||
|
state.hasMore = true;
|
||||||
|
memo = ChatTranscriptRows();
|
||||||
|
memoKeys = ChatRowKeys();
|
||||||
|
fullKeys = ChatRowKeys();
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('streaming into the last message re-derives only the live turn', () {
|
||||||
|
_apply(state, {
|
||||||
|
'kind': 'user',
|
||||||
|
'message_id': 'u-live',
|
||||||
|
'part': {'type': 'text', 'text': 'go'},
|
||||||
|
});
|
||||||
|
check();
|
||||||
|
for (var i = 0; i < 20; i++) {
|
||||||
|
final before = previous;
|
||||||
|
_delta(state, 'a-live', 'chunk $i ');
|
||||||
|
final now = check();
|
||||||
|
expect(now.last['streaming'], isTrue);
|
||||||
|
// Every row but the live turn is the object the last build held.
|
||||||
|
if (i > 0) expect(reused(before, now), now.length - 1);
|
||||||
|
}
|
||||||
|
// A second message joins the live turn.
|
||||||
|
_delta(state, 'b-live', 'more');
|
||||||
|
check();
|
||||||
|
_apply(state, {
|
||||||
|
'kind': 'step',
|
||||||
|
'step': {'status': 'ended'},
|
||||||
|
});
|
||||||
|
state.live = null;
|
||||||
|
final now = check();
|
||||||
|
expect(now.any((row) => row['streaming'] == true), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a new message appended keeps the settled rows', () {
|
||||||
|
final before = previous;
|
||||||
|
state.messages = [
|
||||||
|
...state.messages,
|
||||||
|
{
|
||||||
|
'id': 'u-new',
|
||||||
|
'role': 'user',
|
||||||
|
'created_at': _base + 99000,
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'text': 'next'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
final now = check();
|
||||||
|
expect(reused(before, now), greaterThan(before.length - 3));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a shown item arriving is placed as a full derivation places it', () {
|
||||||
|
state.shown = mergeChatShownItems(state.shown, [
|
||||||
|
{'id': 'f2', 'kind': 'image', 'created_at': _base + 30 * 1000 + 10},
|
||||||
|
]);
|
||||||
|
final now = check();
|
||||||
|
expect(now.where((row) => row['kind'] == chatShownRowKind), hasLength(2));
|
||||||
|
// An older one waits for its page while history remains.
|
||||||
|
state.shown = mergeChatShownItems(state.shown, [
|
||||||
|
{'id': 'f3', 'kind': 'notify', 'created_at': _base},
|
||||||
|
]);
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('permission cards added, then settled into their turn', () {
|
||||||
|
state.cards = [
|
||||||
|
{
|
||||||
|
'kind': 'permission',
|
||||||
|
'key': 'chat-permission-p1',
|
||||||
|
'frame': {
|
||||||
|
'request_id': 'p1',
|
||||||
|
'action': 'Bash',
|
||||||
|
'source': {'type': 'tool', 'id': 'call-38'},
|
||||||
|
},
|
||||||
|
'answer': '',
|
||||||
|
'error': null,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'kind': 'question',
|
||||||
|
'key': 'chat-question-q1',
|
||||||
|
'form': {'id': 'q1', chatQuestionRaisedIn: 'a37', 'resolved': true},
|
||||||
|
'error': null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
var now = check();
|
||||||
|
expect(now.where((row) => row['kind'] == 'permission'), hasLength(1));
|
||||||
|
state.cards = [
|
||||||
|
{...state.cards.first, 'answer': 'once'},
|
||||||
|
state.cards.last,
|
||||||
|
];
|
||||||
|
now = check();
|
||||||
|
expect(now.where((row) => row['kind'] == 'permission'), isEmpty);
|
||||||
|
expect(now.where((row) => row['permissions'] != null), hasLength(1));
|
||||||
|
expect(now.where((row) => row['questions'] != null), hasLength(1));
|
||||||
|
_delta(state, 'a-live', 'after the card');
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('turn diffs arriving attach as a full derivation attaches them', () {
|
||||||
|
state.diffs.apply({
|
||||||
|
'session_id': 's1',
|
||||||
|
'turn_diffs': [
|
||||||
|
{
|
||||||
|
'turn_id': 't1',
|
||||||
|
'message_ids': ['a31'],
|
||||||
|
'files': 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, 's1');
|
||||||
|
final now = check();
|
||||||
|
expect(now.where((row) => row['turnDiff'] != null), hasLength(1));
|
||||||
|
_delta(state, 'a-live', 'x');
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a prepended history page re-derives only the runs it touches', () {
|
||||||
|
final before = previous;
|
||||||
|
// The older page ends in an assistant message that folds into the
|
||||||
|
// first loaded turn's neighbour, and the last page drops hasMore.
|
||||||
|
state.messages = [..._history(0, 10), ...state.messages];
|
||||||
|
state.hasMore = false;
|
||||||
|
final now = check();
|
||||||
|
expect(reused(before, now), greaterThan(before.length ~/ 2));
|
||||||
|
state.messages = [
|
||||||
|
{
|
||||||
|
'id': 'older',
|
||||||
|
'role': 'assistant',
|
||||||
|
'parts': [
|
||||||
|
{'type': 'text', 'id': 'o', 'text': 'older'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
...state.messages,
|
||||||
|
];
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a cached transcript reused with the same list returns equal rows', () {
|
||||||
|
final before = previous;
|
||||||
|
final now = check();
|
||||||
|
expect(reused(before, now), now.length);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user