Open long chats bottom-anchored so only the rows that show are laid out
This commit is contained in:
@@ -74,27 +74,35 @@ String chatJumpToLatestLabel(int newMessages) {
|
|||||||
/// Where a conversation's transcript is placed when it first shows.
|
/// Where a conversation's transcript is placed when it first shows.
|
||||||
///
|
///
|
||||||
/// No saved position means the operator has none of their own: open at the
|
/// No saved position means the operator has none of their own: open at the
|
||||||
/// newest content. A saved offset is kept, clamped to the current end, so a
|
/// newest content. A saved offset is kept, clamped to the current extent, so a
|
||||||
/// conversation whose content grew or shrank while it was closed still lands on
|
/// conversation whose content grew or shrank while it was closed still lands on
|
||||||
/// a real position.
|
/// a real position. A bottom-anchored transcript measures older rows above its
|
||||||
|
/// centre, so its offsets (and [minScrollExtent]) can be negative.
|
||||||
double chatRestoreOffset({
|
double chatRestoreOffset({
|
||||||
required double? savedOffset,
|
required double? savedOffset,
|
||||||
required double maxScrollExtent,
|
required double maxScrollExtent,
|
||||||
|
double minScrollExtent = 0,
|
||||||
}) {
|
}) {
|
||||||
if (savedOffset == null) return maxScrollExtent;
|
if (savedOffset == null) return maxScrollExtent;
|
||||||
return savedOffset.clamp(0.0, maxScrollExtent).toDouble();
|
return savedOffset.clamp(minScrollExtent, maxScrollExtent).toDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A saved transcript position: the scroll offset and the message id of the
|
||||||
|
/// row the transcript was split at (see `chatTranscriptAnchor`), or null for a
|
||||||
|
/// top-anchored transcript whose offset counts from its first row. An offset
|
||||||
|
/// only means something measured from the same anchor.
|
||||||
|
typedef ChatScrollPosition = ({double offset, String? anchor});
|
||||||
|
|
||||||
/// Per-conversation transcript positions, kept while the chat list stays
|
/// Per-conversation transcript positions, kept while the chat list stays
|
||||||
/// mounted. A conversation opened again returns to the position the operator
|
/// mounted. A conversation opened again returns to the position the operator
|
||||||
/// left; one they never moved in opens at its newest message.
|
/// left; one they never moved in opens at its newest message.
|
||||||
abstract interface class ChatScrollStore {
|
abstract interface class ChatScrollStore {
|
||||||
/// The offset the operator left this conversation at, or null when they never
|
/// The position the operator left this conversation at, or null when they
|
||||||
/// scrolled away from its end.
|
/// never scrolled away from its end.
|
||||||
double? read(String sessionId);
|
ChatScrollPosition? read(String sessionId);
|
||||||
|
|
||||||
/// Records a deliberate position away from the end.
|
/// Records a deliberate position away from the end.
|
||||||
void write(String sessionId, double offset);
|
void write(String sessionId, ChatScrollPosition position);
|
||||||
|
|
||||||
/// Forgets the position once the operator is following the end again.
|
/// Forgets the position once the operator is following the end again.
|
||||||
void clear(String sessionId);
|
void clear(String sessionId);
|
||||||
@@ -107,19 +115,22 @@ abstract interface class ChatScrollStore {
|
|||||||
/// client keeps no transcript position on disk and does not import the web's
|
/// client keeps no transcript position on disk and does not import the web's
|
||||||
/// storage keys.
|
/// storage keys.
|
||||||
class MemoryChatScrollStore implements ChatScrollStore {
|
class MemoryChatScrollStore implements ChatScrollStore {
|
||||||
final Map<String, double> _offsets = {};
|
final Map<String, ChatScrollPosition> _offsets = {};
|
||||||
|
|
||||||
/// A snapshot of the stored offsets, so a test can tell a cleared
|
/// A snapshot of the stored positions, so a test can tell a cleared
|
||||||
/// conversation from one that was never moved.
|
/// conversation from one that was never moved.
|
||||||
Map<String, double> get entries => Map.unmodifiable(_offsets);
|
Map<String, ChatScrollPosition> get entries => Map.unmodifiable(_offsets);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
double? read(String sessionId) => _offsets[sessionId];
|
ChatScrollPosition? read(String sessionId) => _offsets[sessionId];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void write(String sessionId, double offset) {
|
void write(String sessionId, ChatScrollPosition position) {
|
||||||
if (!offset.isFinite || offset < 0) return;
|
// A top-anchored offset counts from the first row and is never negative;
|
||||||
_offsets[sessionId] = offset;
|
// a bottom-anchored one is negative above its centre.
|
||||||
|
if (!position.offset.isFinite) return;
|
||||||
|
if (position.anchor == null && position.offset < 0) return;
|
||||||
|
_offsets[sessionId] = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -35,7 +35,9 @@ import '../../../ui/adaptive/segmented_bar_overlay.dart';
|
|||||||
import '../../../ui/layout/app_sheet_header.dart';
|
import '../../../ui/layout/app_sheet_header.dart';
|
||||||
import 'chat_reading_width.dart';
|
import 'chat_reading_width.dart';
|
||||||
import 'chat_thread_header.dart';
|
import 'chat_thread_header.dart';
|
||||||
|
import 'chat_transcript_anchor.dart';
|
||||||
import 'chat_transcript_cache.dart';
|
import 'chat_transcript_cache.dart';
|
||||||
|
import 'chat_transcript_scroll_view.dart';
|
||||||
import 'chat_permission_card_model.dart';
|
import 'chat_permission_card_model.dart';
|
||||||
import 'chat_permission_group.dart';
|
import 'chat_permission_group.dart';
|
||||||
import 'chat_transcript_card.dart';
|
import 'chat_transcript_card.dart';
|
||||||
@@ -185,7 +187,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
|
|
||||||
/// The conversation's saved position, read when this view is attached and
|
/// The conversation's saved position, read when this view is attached and
|
||||||
/// consumed by the first placement after the history arrives.
|
/// consumed by the first placement after the history arrives.
|
||||||
double? _savedOffset;
|
ChatScrollPosition? _savedPosition;
|
||||||
|
|
||||||
|
/// The message id the transcript is split at (see [chatTranscriptAnchor]),
|
||||||
|
/// chosen once when the history first arrives; null keeps it top-anchored.
|
||||||
|
String? _anchor;
|
||||||
|
bool _anchorChosen = false;
|
||||||
bool _placed = false;
|
bool _placed = false;
|
||||||
bool _pinScheduled = false;
|
bool _pinScheduled = false;
|
||||||
|
|
||||||
@@ -384,7 +391,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||||
_adoptCachedTranscript();
|
_adoptCachedTranscript();
|
||||||
_seedCommandNotice();
|
_seedCommandNotice();
|
||||||
}
|
}
|
||||||
@@ -416,7 +423,9 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
_syncGeneration++;
|
_syncGeneration++;
|
||||||
_commandNotices.clear();
|
_commandNotices.clear();
|
||||||
_seedCommandNotice();
|
_seedCommandNotice();
|
||||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||||
|
_anchor = null;
|
||||||
|
_anchorChosen = false;
|
||||||
_placed = false;
|
_placed = false;
|
||||||
_following = true;
|
_following = true;
|
||||||
_distanceFromBottom = null;
|
_distanceFromBottom = null;
|
||||||
@@ -489,11 +498,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
store.clear(id);
|
store.clear(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_scroll.hasClients) {
|
final offset = _scroll.hasClients ? _scroll.position.pixels : _lastOffset;
|
||||||
store.write(id, _scroll.position.pixels);
|
store.write(id, (offset: offset, anchor: _anchor));
|
||||||
} else {
|
|
||||||
store.write(id, _lastOffset);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Measured distance from the end for the current content.
|
/// Measured distance from the end for the current content.
|
||||||
@@ -535,11 +541,21 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
/// the operator left one, otherwise the newest message.
|
/// the operator left one, otherwise the newest message.
|
||||||
void _placeFromStore() {
|
void _placeFromStore() {
|
||||||
if (!_scroll.hasClients) return;
|
if (!_scroll.hasClients) return;
|
||||||
|
final saved = _savedPosition;
|
||||||
|
if (_anchor != null &&
|
||||||
|
saved == null &&
|
||||||
|
_scroll.position.minScrollExtent >= 0) {
|
||||||
|
// The rows above the centre do not fill the viewport, so a bottom-anchored
|
||||||
|
// list would leave a gap above them. A transcript this short is cheap to
|
||||||
|
// lay out whole: show it top-anchored, from its first row, as before.
|
||||||
|
setState(() => _anchor = null);
|
||||||
|
_scheduleFollow();
|
||||||
|
return;
|
||||||
|
}
|
||||||
_placed = true;
|
_placed = true;
|
||||||
final saved = _savedOffset;
|
_savedPosition = null;
|
||||||
_savedOffset = null;
|
|
||||||
if (saved != null) {
|
if (saved != null) {
|
||||||
_restoreOffset = saved;
|
_restoreOffset = saved.offset;
|
||||||
_restoring = true;
|
_restoring = true;
|
||||||
_following = false;
|
_following = false;
|
||||||
_newestAtRelease = null;
|
_newestAtRelease = null;
|
||||||
@@ -564,6 +580,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
final target = chatRestoreOffset(
|
final target = chatRestoreOffset(
|
||||||
savedOffset: _restoreOffset,
|
savedOffset: _restoreOffset,
|
||||||
maxScrollExtent: end,
|
maxScrollExtent: end,
|
||||||
|
minScrollExtent: _scroll.position.minScrollExtent,
|
||||||
);
|
);
|
||||||
if (_scroll.position.pixels != target) _scroll.jumpTo(target);
|
if (_scroll.position.pixels != target) _scroll.jumpTo(target);
|
||||||
_lastOffset = target;
|
_lastOffset = target;
|
||||||
@@ -680,6 +697,35 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
_restoreOffset = null;
|
_restoreOffset = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The index of the row the transcript is split at, or -1 to keep it
|
||||||
|
/// top-anchored. The anchor is chosen once, when the history first arrives:
|
||||||
|
/// a saved position keeps the anchor it was measured from.
|
||||||
|
int _splitFor(List<JsonMap> rows) {
|
||||||
|
if (!_controller.loaded) return -1;
|
||||||
|
if (!_anchorChosen) {
|
||||||
|
_anchorChosen = true;
|
||||||
|
final saved = _savedPosition;
|
||||||
|
if (saved != null &&
|
||||||
|
saved.anchor != null &&
|
||||||
|
chatTranscriptAnchorIndex(rows, saved.anchor) < 0) {
|
||||||
|
// The row the position was measured from is gone: the offset no longer
|
||||||
|
// names a place in this transcript, so it opens at the newest message.
|
||||||
|
_savedPosition = null;
|
||||||
|
}
|
||||||
|
_anchor = _savedPosition == null
|
||||||
|
? chatTranscriptAnchor(rows)
|
||||||
|
: _savedPosition!.anchor;
|
||||||
|
}
|
||||||
|
var index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||||
|
if (index < 0 && _anchor != null) {
|
||||||
|
// A repair replaced the transcript without the anchor row: split the new
|
||||||
|
// one afresh (a following view is pinned to its end again after layout).
|
||||||
|
_anchor = chatTranscriptAnchor(rows);
|
||||||
|
index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => AnimatedBuilder(
|
Widget build(BuildContext context) => AnimatedBuilder(
|
||||||
animation: Listenable.merge([
|
animation: Listenable.merge([
|
||||||
@@ -743,6 +789,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
// its previous messages, keeping the element (and its open disclosures)
|
// its previous messages, keeping the element (and its open disclosures)
|
||||||
// across paging and streaming.
|
// across paging and streaming.
|
||||||
final keys = _rowKeys.forRows(rows);
|
final keys = _rowKeys.forRows(rows);
|
||||||
|
final split = _splitFor(rows);
|
||||||
final initialLoad = !_controller.loaded && _controller.loading;
|
final initialLoad = !_controller.loaded && _controller.loading;
|
||||||
final historyNotice = _noticeOf(
|
final historyNotice = _noticeOf(
|
||||||
notices,
|
notices,
|
||||||
@@ -825,11 +872,16 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
children: [
|
children: [
|
||||||
// At rest the first item starts below the floating bar
|
// At rest the first item starts below the floating bar
|
||||||
// and the last one ends above the floating composer.
|
// and the last one ends above the floating composer.
|
||||||
ListView(
|
// Bottom-anchored at the split row, the newest rows are
|
||||||
|
// laid out first and older ones only when scrolled to.
|
||||||
|
ChatTranscriptScrollView(
|
||||||
key: ValueKey<String>(
|
key: ValueKey<String>(
|
||||||
'chat-thread-${widget.sessionId}',
|
'chat-thread-${widget.sessionId}',
|
||||||
),
|
),
|
||||||
controller: _scroll,
|
controller: _scroll,
|
||||||
|
splitAfter: split < 0
|
||||||
|
? null
|
||||||
|
: ValueKey<String>(keys[split]),
|
||||||
padding: EdgeInsets.fromLTRB(
|
padding: EdgeInsets.fromLTRB(
|
||||||
side,
|
side,
|
||||||
media.top + 8,
|
media.top + 8,
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import '../../../core/api_gateway.dart';
|
||||||
|
|
||||||
|
/// Where an opened transcript is split into its older and newer halves.
|
||||||
|
///
|
||||||
|
/// Opening a long conversation at its newest message used to lay out every row
|
||||||
|
/// above it: a top-anchored list can only reach its end by measuring the rows
|
||||||
|
/// in between. The transcript is instead a two-sided scroll view whose centre
|
||||||
|
/// sits between an anchor row and the rows after it. The anchor row and every
|
||||||
|
/// older row grow upwards from the centre; the rows after it (and the queued,
|
||||||
|
/// unsent and command lines) grow downwards. Opening lays out only what shows.
|
||||||
|
///
|
||||||
|
/// The split is fixed for as long as the conversation is open, so growth on
|
||||||
|
/// either side never moves the other: an older page loaded above, or a reply
|
||||||
|
/// streaming below, leaves the operator's reading position where it is. The
|
||||||
|
/// newest message row (and a reply that is still streaming) is never the
|
||||||
|
/// anchor or above it, because a row above the centre that grew would push the
|
||||||
|
/// rows above it up while the operator reads them.
|
||||||
|
///
|
||||||
|
/// Returns null when the transcript has fewer than two message rows: there is
|
||||||
|
/// nothing older to keep unmeasured, and the view stays top-anchored.
|
||||||
|
String? chatTranscriptAnchor(List<JsonMap> rows) {
|
||||||
|
var cut = -1;
|
||||||
|
for (var index = rows.length - 1; index >= 0; index--) {
|
||||||
|
if (_messageIds(rows[index]).isNotEmpty) {
|
||||||
|
cut = index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var index = 0; index < rows.length; index++) {
|
||||||
|
if (rows[index]['streaming'] == true && index < cut) {
|
||||||
|
cut = index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var index = cut - 1; index >= 0; index--) {
|
||||||
|
final ids = _messageIds(rows[index]);
|
||||||
|
if (ids.isNotEmpty) return ids.last;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The index of the row that holds [anchor], or -1 when no row does (a repair
|
||||||
|
/// replaced the transcript) or there is no anchor.
|
||||||
|
int chatTranscriptAnchorIndex(List<JsonMap> rows, String? anchor) {
|
||||||
|
if (anchor == null) return -1;
|
||||||
|
for (var index = rows.length - 1; index >= 0; index--) {
|
||||||
|
if (_messageIds(rows[index]).contains(anchor)) return index;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The stored message ids of one user or assistant row. Cards, activity lines
|
||||||
|
/// and the pending-response shell carry none and can never be an anchor.
|
||||||
|
List<String> _messageIds(JsonMap row) {
|
||||||
|
final kind = row['kind'];
|
||||||
|
if (kind == 'assistant') {
|
||||||
|
final listed = row['messageIds'];
|
||||||
|
if (listed is! List) return const [];
|
||||||
|
return [
|
||||||
|
for (final id in listed)
|
||||||
|
if ('$id'.trim().isNotEmpty) '$id'.trim(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if (kind == 'user') {
|
||||||
|
final id = '${row['key'] ?? ''}'.trim();
|
||||||
|
return id.isEmpty ? const [] : [id];
|
||||||
|
}
|
||||||
|
return const [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
|
/// The transcript's scrollable: older rows above a centre, newer rows below.
|
||||||
|
///
|
||||||
|
/// With [splitAfter], the children up to and including the one with that key
|
||||||
|
/// sit above the centre and the rest below it. The centre is at the viewport's
|
||||||
|
/// bottom edge at offset zero: the older children grow upwards from it and the
|
||||||
|
/// newer ones downwards, so the newest rows are laid out first and an older row
|
||||||
|
/// only when it is scrolled to. Offsets above the centre are negative.
|
||||||
|
///
|
||||||
|
/// Without one (or when no child has that key) this is a plain top-anchored
|
||||||
|
/// list starting at its first child. A transcript that fits its viewport stays
|
||||||
|
/// top-anchored, so a short conversation still reads from the top.
|
||||||
|
///
|
||||||
|
/// [padding] keeps the first child below the floating bar and the last child
|
||||||
|
/// above the floating composer, as a list's padding would.
|
||||||
|
class ChatTranscriptScrollView extends StatelessWidget {
|
||||||
|
const ChatTranscriptScrollView({
|
||||||
|
super.key,
|
||||||
|
required this.controller,
|
||||||
|
required this.padding,
|
||||||
|
required this.children,
|
||||||
|
this.splitAfter,
|
||||||
|
});
|
||||||
|
|
||||||
|
static const _centre = ValueKey<String>('chat-thread-newer');
|
||||||
|
|
||||||
|
final ScrollController controller;
|
||||||
|
final EdgeInsets padding;
|
||||||
|
final List<Widget> children;
|
||||||
|
final Key? splitAfter;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final split = splitAfter == null
|
||||||
|
? -1
|
||||||
|
: children.indexWhere((child) => child.key == splitAfter);
|
||||||
|
final anchored = split >= 0;
|
||||||
|
return CustomScrollView(
|
||||||
|
controller: controller,
|
||||||
|
center: _centre,
|
||||||
|
anchor: anchored ? 1 : 0,
|
||||||
|
slivers: [
|
||||||
|
// Before the centre, a sliver grows upwards: its first child sits at
|
||||||
|
// the centre, so the older children go in newest-first.
|
||||||
|
SliverPadding(
|
||||||
|
padding: anchored ? padding.copyWith(bottom: 0) : EdgeInsets.zero,
|
||||||
|
sliver: SliverList(
|
||||||
|
delegate: SliverChildListDelegate([
|
||||||
|
for (var index = split; index >= 0; index--) children[index],
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SliverPadding(
|
||||||
|
key: _centre,
|
||||||
|
padding: anchored ? padding.copyWith(top: 0) : padding,
|
||||||
|
sliver: SliverList(
|
||||||
|
delegate: SliverChildListDelegate(children.sublist(split + 1)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -103,27 +103,39 @@ void main() {
|
|||||||
expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0);
|
expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0);
|
||||||
expect(chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640), 640);
|
expect(chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640), 640);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a bottom-anchored position above the centre stays negative', () {
|
||||||
|
double restore(double saved) => chatRestoreOffset(
|
||||||
|
savedOffset: saved,
|
||||||
|
minScrollExtent: -2000,
|
||||||
|
maxScrollExtent: 300,
|
||||||
|
);
|
||||||
|
expect(restore(-750), -750);
|
||||||
|
expect(restore(-5000), -2000);
|
||||||
|
expect(restore(900), 300);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('store', () {
|
group('store', () {
|
||||||
test('remembers one conversation without touching another', () {
|
test('remembers one conversation without touching another', () {
|
||||||
final store = MemoryChatScrollStore();
|
final store = MemoryChatScrollStore();
|
||||||
store.write('a', 120);
|
store.write('a', (offset: 120, anchor: null));
|
||||||
store.write('b', 30);
|
store.write('b', (offset: -30, anchor: 'm4'));
|
||||||
expect(store.read('a'), 120);
|
expect(store.read('a'), (offset: 120.0, anchor: null));
|
||||||
expect(store.read('b'), 30);
|
expect(store.read('b'), (offset: -30.0, anchor: 'm4'));
|
||||||
expect(store.read('c'), isNull);
|
expect(store.read('c'), isNull);
|
||||||
|
|
||||||
store.clear('a');
|
store.clear('a');
|
||||||
expect(store.read('a'), isNull);
|
expect(store.read('a'), isNull);
|
||||||
expect(store.entries, {'b': 30.0});
|
expect(store.entries, {'b': (offset: -30.0, anchor: 'm4')});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('ignores an unusable offset instead of inventing a position', () {
|
test('ignores an unusable offset instead of inventing a position', () {
|
||||||
final store = MemoryChatScrollStore();
|
final store = MemoryChatScrollStore();
|
||||||
store.write('a', double.nan);
|
store.write('a', (offset: double.nan, anchor: 'm1'));
|
||||||
store.write('a', double.infinity);
|
store.write('a', (offset: double.infinity, anchor: null));
|
||||||
store.write('a', -5);
|
// Only a bottom-anchored offset counts from a centre and can be negative.
|
||||||
|
store.write('a', (offset: -5, anchor: null));
|
||||||
expect(store.read('a'), isNull);
|
expect(store.read('a'), isNull);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,211 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/widgets.dart' as widgets;
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||||
|
|
||||||
|
import 'chat_thread_fixtures.dart';
|
||||||
|
import 'chat_thread_scroll_test.dart' show host, transcriptScroll;
|
||||||
|
|
||||||
|
// The transcript opens bottom-anchored: only the rows that show are built, and
|
||||||
|
// growth on either side of the split never moves the operator's place. In the
|
||||||
|
// 800x600 test viewport the first row starts below the floating header.
|
||||||
|
const _firstRowTop = 80.0;
|
||||||
|
|
||||||
|
List<JsonMap> messages(int count, {int from = 0, int sentences = 6}) => [
|
||||||
|
for (var index = from; index < from + count; index++)
|
||||||
|
chatMessage(
|
||||||
|
'm$index',
|
||||||
|
index.isEven ? 'user' : 'assistant',
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
'type': 'text',
|
||||||
|
'text':
|
||||||
|
'Message $index. '
|
||||||
|
'${List.filled(sentences, 'A sentence that wraps across the transcript width.').join(' ')}',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
Finder row(String id) => find.byWidgetPredicate(
|
||||||
|
(widget) =>
|
||||||
|
widget is ChatMessageView &&
|
||||||
|
(widget.row['key'] == id ||
|
||||||
|
(widget.row['messageIds'] as List?)?.contains(id) == true),
|
||||||
|
skipOffstage: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
Finder get thread => find.byKey(const Key('chat-thread-s'));
|
||||||
|
|
||||||
|
Future<void> dragUntil(WidgetTester tester, Finder target) async {
|
||||||
|
for (var drag = 0; drag < 80 && !tester.any(target); drag++) {
|
||||||
|
await tester.drag(thread, const Offset(0, 500));
|
||||||
|
await tester.pump();
|
||||||
|
}
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> openThread(WidgetTester tester, FakeChatGateway gateway) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('a long transcript opens at its newest message, building only '
|
||||||
|
'what shows', (tester) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
gateway.history = (_) async => chatPage(messages(200));
|
||||||
|
final built = <String>{};
|
||||||
|
widgets.debugOnRebuildDirtyWidget = (element, _) {
|
||||||
|
final widget = element.widget;
|
||||||
|
if (widget is ChatMessageView) built.add('${widget.row['key']}');
|
||||||
|
};
|
||||||
|
addTearDown(() => widgets.debugOnRebuildDirtyWidget = null);
|
||||||
|
|
||||||
|
await openThread(tester, gateway);
|
||||||
|
widgets.debugOnRebuildDirtyWidget = null;
|
||||||
|
|
||||||
|
final scroll = transcriptScroll(tester);
|
||||||
|
expect(
|
||||||
|
scroll.position.pixels,
|
||||||
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||||
|
);
|
||||||
|
expect(scroll.position.minScrollExtent, lessThan(0));
|
||||||
|
final newest = tester.getRect(row('m199'));
|
||||||
|
expect(newest.bottom, lessThanOrEqualTo(600));
|
||||||
|
expect(newest.bottom, greaterThan(400));
|
||||||
|
expect(built, isNot(contains('m0')));
|
||||||
|
expect(built.length, lessThan(20));
|
||||||
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
||||||
|
|
||||||
|
// A reply streaming below the split is followed while at the end.
|
||||||
|
gateway.emitEvent(chatTextFrame('Streaming answer. ', messageId: 'live'));
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump();
|
||||||
|
expect(
|
||||||
|
scroll.position.pixels,
|
||||||
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('scrolling up reaches the first row with no gap above it', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
gateway.history = (_) async => chatPage(messages(40));
|
||||||
|
await openThread(tester, gateway);
|
||||||
|
|
||||||
|
await dragUntil(tester, row('m0'));
|
||||||
|
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final scroll = transcriptScroll(tester);
|
||||||
|
expect(
|
||||||
|
scroll.position.pixels,
|
||||||
|
closeTo(scroll.position.minScrollExtent, 0.5),
|
||||||
|
);
|
||||||
|
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||||
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (final tall in [false, true]) {
|
||||||
|
testWidgets('a short transcript still reads from the top '
|
||||||
|
'(tall newest row: $tall)', (tester) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
// The rows above the newest one fit the viewport.
|
||||||
|
gateway.history = (_) async => chatPage([
|
||||||
|
...messages(2, sentences: 1),
|
||||||
|
...messages(1, from: 2, sentences: tall ? 60 : 1),
|
||||||
|
]);
|
||||||
|
await openThread(tester, gateway);
|
||||||
|
|
||||||
|
final scroll = transcriptScroll(tester);
|
||||||
|
expect(scroll.position.minScrollExtent, 0);
|
||||||
|
expect(
|
||||||
|
scroll.position.pixels,
|
||||||
|
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||||
|
);
|
||||||
|
if (!tall) {
|
||||||
|
expect(scroll.position.maxScrollExtent, 0);
|
||||||
|
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// At its end the tall newest row shows its end; scrolling back reaches
|
||||||
|
// the first row at the top padding.
|
||||||
|
expect(tester.getRect(row('m2')).bottom, lessThanOrEqualTo(600));
|
||||||
|
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('loading an older page keeps the rows the operator reads in '
|
||||||
|
'place', (tester) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
gateway.history = (_) async =>
|
||||||
|
chatPage(messages(30, from: 30), cursor: 'older', hasMore: true);
|
||||||
|
await openThread(tester, gateway);
|
||||||
|
|
||||||
|
await dragUntil(tester, find.text('Load older'));
|
||||||
|
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final held = tester.getRect(row('m30'));
|
||||||
|
gateway.history = (_) async => chatPage(messages(30));
|
||||||
|
await tester.tap(find.text('Load older'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(find.text('Load older'), findsNothing);
|
||||||
|
expect(tester.getRect(row('m30')).top, closeTo(held.top, 0.5));
|
||||||
|
// The older page sits above, reachable by scrolling up.
|
||||||
|
await dragUntil(tester, row('m0'));
|
||||||
|
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('a reopened conversation keeps its place after messages arrived '
|
||||||
|
'while it was closed', (tester) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
gateway.history = (_) async => chatPage(messages(60));
|
||||||
|
final positions = MemoryChatScrollStore();
|
||||||
|
Widget view() => host(
|
||||||
|
ChatThreadView(
|
||||||
|
gateway: gateway,
|
||||||
|
sessionId: 's',
|
||||||
|
scrollPositions: positions,
|
||||||
|
onBack: () {},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(view());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
for (var drag = 0; drag < 4; drag++) {
|
||||||
|
await tester.drag(thread, const Offset(0, 400));
|
||||||
|
await tester.pump();
|
||||||
|
}
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final shown = find.byWidgetPredicate((widget) => widget is ChatMessageView);
|
||||||
|
final top = shown.evaluate().first.widget as ChatMessageView;
|
||||||
|
final id = '${top.row['key']}';
|
||||||
|
final held = tester.getRect(row(id));
|
||||||
|
expect(positions.read('s')!.anchor, isNotNull);
|
||||||
|
|
||||||
|
await tester.pumpWidget(host(const SizedBox()));
|
||||||
|
gateway.history = (_) async => chatPage(messages(64));
|
||||||
|
await tester.pumpWidget(view());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(tester.getRect(row(id)).top, closeTo(held.top, 1));
|
||||||
|
expect(find.text('Jump to latest · 4 new messages'), findsNothing);
|
||||||
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -35,8 +35,14 @@ JsonMap tallPage() => chatPage([
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
ScrollController transcriptScroll(WidgetTester tester) =>
|
ScrollController transcriptScroll(WidgetTester tester) => tester
|
||||||
tester.widget<ListView>(find.byKey(const Key('chat-thread-s'))).controller!;
|
.widget<CustomScrollView>(
|
||||||
|
find.descendant(
|
||||||
|
of: find.byKey(const Key('chat-thread-s')),
|
||||||
|
matching: find.byType(CustomScrollView),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.controller!;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('a streaming transcript stays pinned to its newest end', (
|
testWidgets('a streaming transcript stays pinned to its newest end', (
|
||||||
@@ -181,14 +187,14 @@ void main() {
|
|||||||
);
|
);
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
final saved = scroll.position.pixels;
|
final saved = scroll.position.pixels;
|
||||||
expect(saved, greaterThan(0));
|
expect(saved, lessThan(scroll.position.maxScrollExtent - 300));
|
||||||
expect(positions.read('s'), closeTo(saved, 0.5));
|
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
|
||||||
|
|
||||||
// Leaving the conversation disposes its view but not the destination's
|
// Leaving the conversation disposes its view but not the destination's
|
||||||
// store.
|
// store.
|
||||||
await tester.pumpWidget(host(const SizedBox()));
|
await tester.pumpWidget(host(const SizedBox()));
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
expect(positions.read('s'), closeTo(saved, 0.5));
|
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
|
||||||
|
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
host(
|
host(
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_transcript_anchor.dart';
|
||||||
|
|
||||||
|
JsonMap user(String id) => {'kind': 'user', 'key': id};
|
||||||
|
|
||||||
|
JsonMap assistant(List<String> ids, {bool streaming = false}) => {
|
||||||
|
'kind': 'assistant',
|
||||||
|
'key': ids.isEmpty ? 'chat-pending-response' : ids.first,
|
||||||
|
'messageIds': ids,
|
||||||
|
if (streaming) 'streaming': true,
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('splits before the newest message row', () {
|
||||||
|
final rows = [
|
||||||
|
user('u1'),
|
||||||
|
assistant(['a2', 'a3']),
|
||||||
|
user('u4'),
|
||||||
|
];
|
||||||
|
expect(chatTranscriptAnchor(rows), 'a3');
|
||||||
|
expect(chatTranscriptAnchorIndex(rows, 'a3'), 1);
|
||||||
|
// Any message the folded turn holds finds it, as paging prepends into it.
|
||||||
|
expect(chatTranscriptAnchorIndex(rows, 'a2'), 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a streaming reply and the rows after it stay below the centre', () {
|
||||||
|
// A queued follow-up can be recorded after the reply still arriving.
|
||||||
|
final rows = [
|
||||||
|
user('u1'),
|
||||||
|
assistant(['a2']),
|
||||||
|
user('u3'),
|
||||||
|
assistant(['a4'], streaming: true),
|
||||||
|
user('u5'),
|
||||||
|
];
|
||||||
|
expect(chatTranscriptAnchor(rows), 'u3');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cards, activity lines and the pending shell are never the anchor', () {
|
||||||
|
final rows = [
|
||||||
|
user('u1'),
|
||||||
|
{'kind': 'activity', 'key': 'chat-activity-a2'},
|
||||||
|
assistant(['a2']),
|
||||||
|
assistant(const [], streaming: true),
|
||||||
|
{'kind': 'permission', 'key': 'chat-permission-p1'},
|
||||||
|
];
|
||||||
|
// The newest message row is a2, so the anchor is the row before it.
|
||||||
|
expect(chatTranscriptAnchor(rows), 'u1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fewer than two message rows stay top-anchored', () {
|
||||||
|
expect(chatTranscriptAnchor(const []), isNull);
|
||||||
|
expect(chatTranscriptAnchor([user('u1')]), isNull);
|
||||||
|
expect(
|
||||||
|
chatTranscriptAnchor([
|
||||||
|
user('u1'),
|
||||||
|
{'kind': 'question', 'key': 'chat-question-q1'},
|
||||||
|
]),
|
||||||
|
isNull,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a missing anchor reports no split', () {
|
||||||
|
final rows = [user('u1'), user('u2')];
|
||||||
|
expect(chatTranscriptAnchorIndex(rows, null), -1);
|
||||||
|
expect(chatTranscriptAnchorIndex(rows, 'gone'), -1);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user