feat: show tool status notifications in chat during generation
Parse SSE `status` events alongside `chunk` events and display the status text next to the spinner while the assistant is generating. Previously the Android app discarded all non-chunk SSE events. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../data/models/conversation.dart';
|
||||
import '../data/models/message.dart';
|
||||
import '../data/repositories/chat_repository.dart';
|
||||
import 'api_client_provider.dart';
|
||||
|
||||
// Separate NotifierProvider.family so UI re-builds immediately when streaming starts/stops.
|
||||
@@ -18,6 +19,20 @@ class _IsStreamingNotifier extends Notifier<bool> {
|
||||
bool build() => false;
|
||||
}
|
||||
|
||||
// Tracks the current tool status text during generation (empty = no status).
|
||||
final streamingStatusProvider =
|
||||
NotifierProvider.family<_StreamingStatusNotifier, String, int>(
|
||||
(convId) => _StreamingStatusNotifier(convId),
|
||||
);
|
||||
|
||||
class _StreamingStatusNotifier extends Notifier<String> {
|
||||
// ignore: avoid_unused_constructor_parameters
|
||||
_StreamingStatusNotifier(int convId);
|
||||
|
||||
@override
|
||||
String build() => '';
|
||||
}
|
||||
|
||||
final conversationsProvider =
|
||||
AsyncNotifierProvider<ConversationsNotifier, List<Conversation>>(
|
||||
ConversationsNotifier.new);
|
||||
@@ -103,12 +118,19 @@ class MessagesNotifier extends AsyncNotifier<List<Message>> {
|
||||
// ── Step 2: Stream the response (best effort — silent on failure). ──
|
||||
bool streamedContent = false;
|
||||
try {
|
||||
await for (final chunk in repo.streamGeneration(convId)) {
|
||||
streamedContent = true;
|
||||
final msgs = state.requireValue;
|
||||
if (msgs.isEmpty) continue;
|
||||
final updated = msgs.last.copyWith(content: msgs.last.content + chunk);
|
||||
state = AsyncData([...msgs.sublist(0, msgs.length - 1), updated]);
|
||||
await for (final event in repo.streamGeneration(convId)) {
|
||||
if (event is ChatTextChunk) {
|
||||
streamedContent = true;
|
||||
ref.read(streamingStatusProvider(convId).notifier).state = '';
|
||||
final msgs = state.requireValue;
|
||||
if (msgs.isEmpty) continue;
|
||||
final updated =
|
||||
msgs.last.copyWith(content: msgs.last.content + event.text);
|
||||
state = AsyncData([...msgs.sublist(0, msgs.length - 1), updated]);
|
||||
} else if (event is ChatStatusUpdate) {
|
||||
ref.read(streamingStatusProvider(convId).notifier).state =
|
||||
event.status;
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
// SSE failed — fall through to the polling reload below.
|
||||
@@ -157,6 +179,7 @@ class MessagesNotifier extends AsyncNotifier<List<Message>> {
|
||||
}
|
||||
} finally {
|
||||
ref.read(isStreamingProvider(convId).notifier).state = false;
|
||||
ref.read(streamingStatusProvider(convId).notifier).state = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user