merge(app): land live Chat destination after retained-deadline recovery
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Stable widget-key strings for transcript rows while history grows at both
|
||||
/// ends.
|
||||
///
|
||||
/// A folded assistant turn has no stable message boundary: `Load older` can
|
||||
/// prepend messages into the turn (changing its first id) and a live frame can
|
||||
/// append a message (changing its last id). Deriving the row key from either
|
||||
/// boundary remounts the row and resets every native disclosure in it, which
|
||||
/// moved an open reasoning block to another answer or closed it.
|
||||
///
|
||||
/// So the first sighting of a turn assigns a synthetic key, and later calls
|
||||
/// carry that key forward while the turn still contains any message it held
|
||||
/// before. Rows without a message id fall back to their position: they have no
|
||||
/// identity that survives a prepend.
|
||||
class ChatRowKeys {
|
||||
final Map<String, String> _byMessageId = {};
|
||||
int _serial = 0;
|
||||
|
||||
/// One key per row, in row order. Call once per build.
|
||||
List<String> forRows(List<JsonMap> rows) {
|
||||
final nextByMessageId = <String, String>{};
|
||||
final used = <String>{};
|
||||
final keys = <String>[];
|
||||
for (var index = 0; index < rows.length; index++) {
|
||||
final row = rows[index];
|
||||
final kind = '${row['kind'] ?? ''}';
|
||||
final ids = _messageIds(row);
|
||||
String? key;
|
||||
for (final id in ids) {
|
||||
final held = _byMessageId[id];
|
||||
if (held != null) {
|
||||
key = held;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (key == null) {
|
||||
// A row with no id has no identity that survives a prepend, so it
|
||||
// follows its position, as a plain indexed list would. Every other new
|
||||
// row gets a key no older row can generate.
|
||||
key = ids.isEmpty
|
||||
? 'chat-row-$kind-index-$index'
|
||||
: 'chat-row-$kind-${_serial++}';
|
||||
used.add(key);
|
||||
} else if (!used.add(key)) {
|
||||
// Defensive: a held key is only reused once even if input unexpectedly
|
||||
// splits one turn into two rows; duplicate widget keys would throw.
|
||||
key = 'chat-row-$kind-${_serial++}';
|
||||
used.add(key);
|
||||
}
|
||||
keys.add(key);
|
||||
for (final id in ids) {
|
||||
nextByMessageId[id] = key;
|
||||
}
|
||||
}
|
||||
_byMessageId
|
||||
..clear()
|
||||
..addAll(nextByMessageId);
|
||||
return keys;
|
||||
}
|
||||
|
||||
static List<String> _messageIds(JsonMap row) {
|
||||
final listed = row['messageIds'];
|
||||
if (listed is List) {
|
||||
return listed
|
||||
.map((id) => '$id'.trim())
|
||||
.where((id) => id.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
final key = '${row['key'] ?? ''}'.trim();
|
||||
return key.isEmpty ? const [] : [key];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/chat_transcript.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import '../ui/transcript/chat_message_view.dart';
|
||||
import 'chat_row_keys.dart';
|
||||
import 'chat_thread_controller.dart';
|
||||
|
||||
/// One read-only conversation: the controller's transcript in the order the
|
||||
/// server reports it, rendered by the existing [ChatMessageView].
|
||||
///
|
||||
/// History and live-subscription failures are separate notices: readable stored
|
||||
/// history must never hide a refused live watch. The shell owns the navigation
|
||||
/// bar, so this view adds only its own Back control and content.
|
||||
class ChatThreadView extends StatefulWidget {
|
||||
const ChatThreadView({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.sessionId,
|
||||
required this.onBack,
|
||||
this.title = '',
|
||||
this.syncToken = 0,
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final String sessionId;
|
||||
final VoidCallback onBack;
|
||||
|
||||
/// The list row's title when this visit knows it; a deep link may not.
|
||||
final String title;
|
||||
|
||||
/// Advances when the shared socket is replaced, keeping this selection.
|
||||
final int syncToken;
|
||||
|
||||
@override
|
||||
State<ChatThreadView> createState() => _ChatThreadViewState();
|
||||
}
|
||||
|
||||
class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
late ChatThreadController _controller = _attach();
|
||||
late ChatRowKeys _rowKeys = ChatRowKeys();
|
||||
|
||||
ChatThreadController _attach() =>
|
||||
ChatThreadController(widget.gateway, widget.sessionId)..load();
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ChatThreadView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.gateway != widget.gateway ||
|
||||
oldWidget.sessionId != widget.sessionId) {
|
||||
_controller.dispose();
|
||||
_controller = _attach();
|
||||
_rowKeys = ChatRowKeys();
|
||||
} else if (oldWidget.syncToken != widget.syncToken) {
|
||||
unawaited(_controller.resync());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, _) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final rows = transcriptRows(_controller.messages);
|
||||
// Older pages prepend messages and a live frame can append one, so a
|
||||
// folded assistant turn's first message id is not a stable identity.
|
||||
// ChatRowKeys carries a turn's key forward while it still holds any of
|
||||
// its previous messages, keeping the element (and its open disclosures)
|
||||
// across paging and streaming.
|
||||
final keys = _rowKeys.forRows(rows);
|
||||
final initialLoad = !_controller.loaded && _controller.loading;
|
||||
return ListView(
|
||||
key: ValueKey<String>('chat-thread-${widget.sessionId}'),
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
children: [
|
||||
Padding(
|
||||
key: const Key('chat-thread-header'),
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 16, 4),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
_backButton(cupertino),
|
||||
if (widget.title.trim().isNotEmpty)
|
||||
Text(
|
||||
widget.title.trim(),
|
||||
style: cupertino
|
||||
? CupertinoTheme.of(context).textTheme.navTitleTextStyle
|
||||
: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (initialLoad)
|
||||
Padding(
|
||||
key: const Key('chat-thread-loading'),
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
: const CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
if (_controller.hasMore)
|
||||
Padding(
|
||||
key: const Key('chat-load-older'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: _controller.loading
|
||||
? null
|
||||
: _controller.loadOlder,
|
||||
child: const Text('Load older'),
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: _controller.loading
|
||||
? null
|
||||
: _controller.loadOlder,
|
||||
child: const Text('Load older'),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_controller.error != null)
|
||||
ChatErrorNotice(
|
||||
key: const Key('chat-history-error'),
|
||||
message: 'Chat history unavailable\n${_controller.error}',
|
||||
onRetry: _controller.load,
|
||||
),
|
||||
if (_controller.subscriptionError != null)
|
||||
ChatErrorNotice(
|
||||
key: const Key('chat-live-error'),
|
||||
message:
|
||||
'Live updates unavailable\n${_controller.subscriptionError}',
|
||||
onRetry: _controller.resync,
|
||||
),
|
||||
if (_controller.loaded && rows.isEmpty && _controller.error == null)
|
||||
const Padding(
|
||||
key: Key('chat-thread-empty'),
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text('No messages in this chat yet.'),
|
||||
),
|
||||
for (var index = 0; index < rows.length; index++)
|
||||
Padding(
|
||||
key: ValueKey<String>(keys[index]),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: ChatMessageView(row: rows[index]),
|
||||
),
|
||||
if (_controller.running)
|
||||
Padding(
|
||||
key: const Key('chat-running'),
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Semantics(
|
||||
liveRegion: true,
|
||||
child: const Text('Assistant is responding…'),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Widget _backButton(bool cupertino) => cupertino
|
||||
? CupertinoButton(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 8, 0),
|
||||
onPressed: widget.onBack,
|
||||
child: const Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(CupertinoIcons.back),
|
||||
SizedBox(width: 4),
|
||||
Text('Back'),
|
||||
],
|
||||
),
|
||||
)
|
||||
: TextButton.icon(
|
||||
onPressed: widget.onBack,
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
label: const Text('Back to chats'),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user