fix(chat): keep streamed reasoning live across queued follow-ups
This commit is contained in:
@@ -43,6 +43,7 @@ class ChatThreadController extends ChangeNotifier {
|
||||
bool _hasMore = false;
|
||||
String _cursor = '';
|
||||
String _nativeTurnId = '';
|
||||
String? _liveAssistantMessageId;
|
||||
JsonMap? _usage;
|
||||
String? _error;
|
||||
|
||||
@@ -55,6 +56,7 @@ class ChatThreadController extends ChangeNotifier {
|
||||
|
||||
List<JsonMap> get messages => _messages;
|
||||
bool get running => _running;
|
||||
String? get liveAssistantMessageId => _liveAssistantMessageId;
|
||||
bool get loaded => _loaded;
|
||||
bool get loading => _loading;
|
||||
bool get hasMore => _hasMore;
|
||||
@@ -160,6 +162,7 @@ class ChatThreadController extends ChangeNotifier {
|
||||
// keep claiming to run until the re-subscribe proves otherwise.
|
||||
_running = false;
|
||||
_nativeTurnId = '';
|
||||
_liveAssistantMessageId = null;
|
||||
_notify();
|
||||
await _attach(force: true);
|
||||
if (!_owned(token)) return;
|
||||
@@ -253,6 +256,11 @@ class ChatThreadController extends ChangeNotifier {
|
||||
.map((message) => Map<String, dynamic>.from(message))
|
||||
.toList();
|
||||
_running = next['running'] == true;
|
||||
if (!_running) {
|
||||
_liveAssistantMessageId = null;
|
||||
} else if (const ['text', 'reasoning', 'tool'].contains(body['kind'])) {
|
||||
_liveAssistantMessageId = body['message_id'] as String?;
|
||||
}
|
||||
_nativeTurnId = '${next['nativeTurnId'] ?? ''}';
|
||||
_notify();
|
||||
}
|
||||
|
||||
@@ -591,6 +591,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
messages,
|
||||
trailing: cards,
|
||||
running: _controller.running && !_reconnecting && !_staleTranscript,
|
||||
liveMessageId: _controller.liveAssistantMessageId,
|
||||
);
|
||||
// A transcript held from before a socket gap may still say a compaction is
|
||||
// running even when the repair's read is refused. That running state is
|
||||
|
||||
@@ -54,6 +54,7 @@ List<JsonMap> transcriptRows(
|
||||
List<JsonMap> messages, {
|
||||
List<JsonMap> trailing = const [],
|
||||
bool running = false,
|
||||
String? liveMessageId,
|
||||
}) {
|
||||
final rows = <JsonMap>[];
|
||||
String? key;
|
||||
@@ -114,14 +115,18 @@ List<JsonMap> transcriptRows(
|
||||
}
|
||||
}
|
||||
flush();
|
||||
// Only the newest assistant turn can be live. A running step before the
|
||||
// first assistant delta must not reopen reasoning from an earlier reply.
|
||||
if (running && messages.isNotEmpty && messages.last['role'] == 'assistant') {
|
||||
// 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 streamingId =
|
||||
liveMessageId ??
|
||||
(messages.isNotEmpty && messages.last['role'] == 'assistant'
|
||||
? '${messages.last['id'] ?? ''}'
|
||||
: null);
|
||||
if (running && streamingId != null) {
|
||||
for (final row in rows.reversed) {
|
||||
if (row['kind'] == 'assistant' &&
|
||||
(row['messageIds'] as List).contains(
|
||||
'${messages.last['id'] ?? ''}',
|
||||
)) {
|
||||
(row['messageIds'] as List).contains(streamingId)) {
|
||||
row['streaming'] = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,47 @@ void main() {
|
||||
});
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets('reasoning first arriving after a queued follow-up opens live '
|
||||
'($cupertino)', (tester) async {
|
||||
gateway.prompt = (_, _) async => {'chat_queued': true};
|
||||
await tester.pumpWidget(
|
||||
composerThread(
|
||||
gateway,
|
||||
cupertino: cupertino,
|
||||
newOperationId: nextOperation,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
gateway.emitEvent({
|
||||
'session_id': 's',
|
||||
'message_id': 'live',
|
||||
'kind': 'text',
|
||||
'part': {'id': 'live-text-0', 'type': 'text'},
|
||||
'delta': 'Working answer',
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
'Continue',
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.tap(find.byKey(const Key('chat-composer-primary')));
|
||||
await tester.pumpAndSettle();
|
||||
gateway.emitEvent({
|
||||
'session_id': 's',
|
||||
'message_id': 'live',
|
||||
'kind': 'reasoning',
|
||||
'part': {'id': 'live-reasoning-0', 'type': 'reasoning'},
|
||||
'delta': 'Provided reasoning after the follow-up',
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text('Provided reasoning after the follow-up'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(gateway.interruptBodies, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'running send waits only for its acknowledgement and keeps newer draft '
|
||||
'(${cupertino ? 'cupertino' : 'material'})',
|
||||
|
||||
Reference in New Issue
Block a user