Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: remove error message when sending a new message (#7177)

Richard Shiuec3b702849ff4
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart
index d8cb97e7a3..71bf7f63bf 100644
--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart
@@ -169,7 +169,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
PredefinedFormat? format,
Map<String, dynamic>? metadata,
) {
- _clearErrorMessages();
+ _clearErrorMessages(emit);
_clearRelatedQuestions();
_startStreamingMessage(message, format, metadata);
lastSentMessage = null;
@@ -266,6 +266,9 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
.send()
.onFailure(Log.error);
},
+ deleteMessage: (mesesage) async {
+ await chatController.remove(mesesage);
+ },
);
},
);
@@ -580,7 +583,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
);
}
- void _clearErrorMessages() {
+ void _clearErrorMessages(Emitter<ChatState> emit) {
final errorMessages = chatController.messages
.where(
(message) =>
@@ -592,6 +595,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
for (final message in errorMessages) {
chatController.remove(message);
}
+ emit(state.copyWith(clearErrorMessages: !state.clearErrorMessages));
}
void _clearRelatedQuestions() {
@@ -654,6 +658,8 @@ class ChatEvent with _$ChatEvent {
const factory ChatEvent.didReceiveRelatedQuestions(
List<String> questions,
) = _DidReceiveRelatedQueston;
+
+ const factory ChatEvent.deleteMessage(Message message) = _DeleteMessage;
}
@freezed
@@ -662,12 +668,14 @@ class ChatState with _$ChatState {
required List<String> selectedSourceIds,
required LoadChatMessageStatus loadingState,
required PromptResponseState promptResponseState,
+ required bool clearErrorMessages,
}) = _ChatState;
factory ChatState.initial() => const ChatState(
selectedSourceIds: [],
loadingState: LoadChatMessageStatus.loading,
promptResponseState: PromptResponseState.ready,
+ clearErrorMessages: false,
);
}
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart
index fc63a7ac82..1d883d59b6 100644
--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart
+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart
@@ -31,6 +31,7 @@ class AnswerStream {
_onEnd?.call();
},
onError: (error) {
+ _error = error.toString();
_onError?.call(error.toString());
},
);
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_text_message.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_text_message.dart
index 42ba3dc125..41e066c78f 100644
--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_text_message.dart
+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_text_message.dart
@@ -1,5 +1,6 @@
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/ai_chat/application/chat_ai_message_bloc.dart';
+import 'package:appflowy/plugins/ai_chat/application/chat_bloc.dart';
import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart';
import 'package:appflowy/plugins/ai_chat/application/chat_message_stream.dart';
import 'package:easy_localization/easy_localization.dart';
@@ -67,58 +68,68 @@ class ChatAIMessageWidget extends StatelessWidget {
final loadingText =
state.progress?.step ?? LocaleKeys.chat_generatingResponse.tr();
- return Padding(
- padding: AIChatUILayout.messageMargin,
- child: state.messageState.when(
- loading: () => ChatAIMessageBubble(
- message: message,
- showActions: false,
- child: ChatAILoading(text: loadingText),
- ),
- ready: () {
- return state.text.isEmpty
- ? ChatAIMessageBubble(
- message: message,
- showActions: false,
- child: ChatAILoading(text: loadingText),
- )
- : ChatAIMessageBubble(
- message: message,
- isLastMessage: isLastMessage,
- showActions: stream == null &&
- state.text.isNotEmpty &&
- !isStreaming,
- onRegenerate: onRegenerate,
- onChangeFormat: onChangeFormat,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- IgnorePointer(
- ignoring: UniversalPlatform.isMobile,
- child: AIMarkdownText(markdown: state.text),
- ),
- if (state.sources.isNotEmpty)
- AIMessageMetadata(
- sources: state.sources,
- onSelectedMetadata: onSelectedMetadata,
+ return BlocListener<ChatBloc, ChatState>(
+ listenWhen: (previous, current) =>
+ previous.clearErrorMessages != current.clearErrorMessages,
+ listener: (context, chatState) {
+ if (state.stream?.error?.isEmpty != false) {
+ return;
+ }
+ context.read<ChatBloc>().add(ChatEvent.deleteMessage(message));
+ },
+ child: Padding(
+ padding: AIChatUILayout.messageMargin,
+ child: state.messageState.when(
+ loading: () => ChatAIMessageBubble(
+ message: message,
+ showActions: false,
+ child: ChatAILoading(text: loadingText),
+ ),
+ ready: () {
+ return state.text.isEmpty
+ ? ChatAIMessageBubble(
+ message: message,
+ showActions: false,
+ child: ChatAILoading(text: loadingText),
+ )
+ : ChatAIMessageBubble(
+ message: message,
+ isLastMessage: isLastMessage,
+ showActions: stream == null &&
+ state.text.isNotEmpty &&
+ !isStreaming,
+ onRegenerate: onRegenerate,
+ onChangeFormat: onChangeFormat,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ IgnorePointer(
+ ignoring: UniversalPlatform.isMobile,
+ child: AIMarkdownText(markdown: state.text),
),
- if (state.sources.isNotEmpty && !isLastMessage)
- const VSpace(8.0),
- ],
- ),
- );
- },
- onError: (error) {
- return ChatErrorMessageWidget(
- errorMessage: LocaleKeys.chat_aiServerUnavailable.tr(),
- );
- },
- onAIResponseLimit: () {
- return ChatErrorMessageWidget(
- errorMessage:
- LocaleKeys.sideBar_askOwnerToUpgradeToAIMax.tr(),
- );
- },
+ if (state.sources.isNotEmpty)
+ AIMessageMetadata(
+ sources: state.sources,
+ onSelectedMetadata: onSelectedMetadata,
+ ),
+ if (state.sources.isNotEmpty && !isLastMessage)
+ const VSpace(8.0),
+ ],
+ ),
+ );
+ },
+ onError: (error) {
+ return ChatErrorMessageWidget(
+ errorMessage: LocaleKeys.chat_aiServerUnavailable.tr(),
+ );
+ },
+ onAIResponseLimit: () {
+ return ChatErrorMessageWidget(
+ errorMessage:
+ LocaleKeys.sideBar_askOwnerToUpgradeToAIMax.tr(),
+ );
+ },
+ ),
),
);
},