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;
|
bool _hasMore = false;
|
||||||
String _cursor = '';
|
String _cursor = '';
|
||||||
String _nativeTurnId = '';
|
String _nativeTurnId = '';
|
||||||
|
String? _liveAssistantMessageId;
|
||||||
JsonMap? _usage;
|
JsonMap? _usage;
|
||||||
String? _error;
|
String? _error;
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
|
|
||||||
List<JsonMap> get messages => _messages;
|
List<JsonMap> get messages => _messages;
|
||||||
bool get running => _running;
|
bool get running => _running;
|
||||||
|
String? get liveAssistantMessageId => _liveAssistantMessageId;
|
||||||
bool get loaded => _loaded;
|
bool get loaded => _loaded;
|
||||||
bool get loading => _loading;
|
bool get loading => _loading;
|
||||||
bool get hasMore => _hasMore;
|
bool get hasMore => _hasMore;
|
||||||
@@ -160,6 +162,7 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
// keep claiming to run until the re-subscribe proves otherwise.
|
// keep claiming to run until the re-subscribe proves otherwise.
|
||||||
_running = false;
|
_running = false;
|
||||||
_nativeTurnId = '';
|
_nativeTurnId = '';
|
||||||
|
_liveAssistantMessageId = null;
|
||||||
_notify();
|
_notify();
|
||||||
await _attach(force: true);
|
await _attach(force: true);
|
||||||
if (!_owned(token)) return;
|
if (!_owned(token)) return;
|
||||||
@@ -253,6 +256,11 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
.map((message) => Map<String, dynamic>.from(message))
|
.map((message) => Map<String, dynamic>.from(message))
|
||||||
.toList();
|
.toList();
|
||||||
_running = next['running'] == true;
|
_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'] ?? ''}';
|
_nativeTurnId = '${next['nativeTurnId'] ?? ''}';
|
||||||
_notify();
|
_notify();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -591,6 +591,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
messages,
|
messages,
|
||||||
trailing: cards,
|
trailing: cards,
|
||||||
running: _controller.running && !_reconnecting && !_staleTranscript,
|
running: _controller.running && !_reconnecting && !_staleTranscript,
|
||||||
|
liveMessageId: _controller.liveAssistantMessageId,
|
||||||
);
|
);
|
||||||
// A transcript held from before a socket gap may still say a compaction is
|
// 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
|
// 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> messages, {
|
||||||
List<JsonMap> trailing = const [],
|
List<JsonMap> trailing = const [],
|
||||||
bool running = false,
|
bool running = false,
|
||||||
|
String? liveMessageId,
|
||||||
}) {
|
}) {
|
||||||
final rows = <JsonMap>[];
|
final rows = <JsonMap>[];
|
||||||
String? key;
|
String? key;
|
||||||
@@ -114,14 +115,18 @@ List<JsonMap> transcriptRows(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
flush();
|
flush();
|
||||||
// Only the newest assistant turn can be live. A running step before the
|
// Prefer the assistant identity that actually streamed: a queued follow-up
|
||||||
// first assistant delta must not reopen reasoning from an earlier reply.
|
// can be later in the transcript while this same reply is still arriving.
|
||||||
if (running && messages.isNotEmpty && messages.last['role'] == 'assistant') {
|
// 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) {
|
for (final row in rows.reversed) {
|
||||||
if (row['kind'] == 'assistant' &&
|
if (row['kind'] == 'assistant' &&
|
||||||
(row['messageIds'] as List).contains(
|
(row['messageIds'] as List).contains(streamingId)) {
|
||||||
'${messages.last['id'] ?? ''}',
|
|
||||||
)) {
|
|
||||||
row['streaming'] = true;
|
row['streaming'] = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,47 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (final cupertino in [false, true]) {
|
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(
|
testWidgets(
|
||||||
'running send waits only for its acknowledgement and keeps newer draft '
|
'running send waits only for its acknowledgement and keeps newer draft '
|
||||||
'(${cupertino ? 'cupertino' : 'material'})',
|
'(${cupertino ? 'cupertino' : 'material'})',
|
||||||
|
|||||||
Reference in New Issue
Block a user