Skip to content

Commit

Permalink
chore: return related question
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Jun 2, 2024
1 parent e0b1a94 commit 2926fc0
Show file tree
Hide file tree
Showing 38 changed files with 1,417 additions and 357 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'chat_ai_message_bloc.freezed.dart';

class ChatAIMessageBloc extends Bloc<ChatAIMessageEvent, ChatAIMessageState> {
ChatAIMessageBloc({
required Message message,
}) : super(ChatAIMessageState.initial(message)) {
on<ChatAIMessageEvent>(
(event, emit) async {
await event.when(
initial: () async {},
update: (userProfile, deviceId, states) {},
);
},
);
}
}

@freezed
class ChatAIMessageEvent with _$ChatAIMessageEvent {
const factory ChatAIMessageEvent.initial() = Initial;
const factory ChatAIMessageEvent.update(
UserProfilePB userProfile,
String deviceId,
DocumentAwarenessStatesPB states,
) = Update;
}

@freezed
class ChatAIMessageState with _$ChatAIMessageState {
const factory ChatAIMessageState({
required Message message,
}) = _ChatAIMessageState;

factory ChatAIMessageState.initial(Message message) =>
ChatAIMessageState(message: message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
listener.start(
chatMessageCallback: _handleChatMessage,
chatErrorMessageCallback: (err) {
final error = TextMessage(
metadata: {
CustomMessageType.streamError.toString():
CustomMessageType.streamError,
},
text: err.errorMessage,
Log.error("chat error: ${err.errorMessage}");
final error = CustomMessage(
metadata: OnetimeMessageType.serverStreamError.toMap(),
author: const User(id: "system"),
id: 'system',
);
Expand Down Expand Up @@ -109,13 +106,10 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
},
tapMessage: (Message message) {},
streamingChatMessage: (List<Message> messages) {
// filter out loading or error messages
final allMessages = state.messages.where((element) {
return !(element.metadata
?.containsValue(CustomMessageType.loading) ==
true ||
element.metadata
?.containsValue(CustomMessageType.streamError) ==
true);
return !(element.metadata?.containsKey(onetimeMessageType) ==
true);
}).toList();
allMessages.insertAll(0, messages);
emit(state.copyWith(messages: allMessages));
Expand All @@ -140,6 +134,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
),
);
},
retryGenerate: () {},
);
},
);
Expand Down Expand Up @@ -179,21 +174,20 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
Message _loadingMessage(String id) {
return CustomMessage(
author: User(id: id),
metadata: {
CustomMessageType.loading.toString(): CustomMessageType.loading,
},
metadata: OnetimeMessageType.loading.toMap(),
// fake id
id: 'chat_message_loading_id',
);
}

Message _createChatMessage(ChatMessagePB message) {
final id = message.messageId.toString();
final messageId = message.messageId.toString();
return TextMessage(
author: User(id: message.authorId),
id: id,
id: messageId,
text: message.content,
createdAt: message.createdAt.toInt(),
repliedMessage: _getReplyMessage(state.messages, id),
repliedMessage: _getReplyMessage(state.messages, messageId),
);
}

Expand All @@ -218,6 +212,7 @@ class ChatEvent with _$ChatEvent {
List<Message> messages,
bool hasMore,
) = _DidLoadPreviousMessages;
const factory ChatEvent.retryGenerate() = _RetryGenerate;
}

@freezed
Expand All @@ -229,6 +224,7 @@ class ChatState with _$ChatState {
required LoadingState loadingStatus,
required LoadingState loadingPreviousStatus,
required LoadingState answerQuestionStatus,
required List<String> relatedQuestions,
required bool hasMore,
}) = _ChatState;

Expand All @@ -241,6 +237,7 @@ class ChatState with _$ChatState {
loadingPreviousStatus: const LoadingState.finish(),
answerQuestionStatus: const LoadingState.finish(),
hasMore: true,
relatedQuestions: [],
);
}

Expand All @@ -250,4 +247,39 @@ class LoadingState with _$LoadingState {
const factory LoadingState.finish() = _Finish;
}

enum CustomMessageType { loading, streamError }
enum OnetimeMessageType { unknown, loading, serverStreamError }

const onetimeMessageType = "OnetimeMessageType";

extension OnetimeMessageTypeExtension on OnetimeMessageType {
static OnetimeMessageType fromString(String value) {
switch (value) {
case 'OnetimeMessageType.loading':
return OnetimeMessageType.loading;
case 'OnetimeMessageType.serverStreamError':
return OnetimeMessageType.serverStreamError;
default:
Log.error('Unknown OnetimeMessageType: $value');
return OnetimeMessageType.unknown;
}
}

Map<String, String> toMap() {
return {
onetimeMessageType: toString(),
};
}
}

OnetimeMessageType? restoreOnetimeMessageType(Map<String, dynamic>? metadata) {
if (metadata == null) {
return null;
}

for (final entry in metadata.entries) {
if (entry.key == onetimeMessageType) {
return OnetimeMessageTypeExtension.fromString(entry.value as String);
}
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,14 @@ import 'package:appflowy_result/appflowy_result.dart';

import 'chat_notification.dart';

typedef ChatMessageCallback = void Function(
ChatMessagePB message,
);

typedef ChatErrorMessageCallback = void Function(
ChatMessageErrorPB message,
);

typedef LatestMessageCallback = void Function(
ChatMessageListPB list,
);
typedef PrevMessageCallback = void Function(
ChatMessageListPB list,
);
typedef ChatMessageCallback = void Function(ChatMessagePB message);
typedef ChatErrorMessageCallback = void Function(ChatMessageErrorPB message);
typedef LatestMessageCallback = void Function(ChatMessageListPB list);
typedef PrevMessageCallback = void Function(ChatMessageListPB list);

class ChatMessageListener {
ChatMessageListener({
required this.chatId,
}) {
_parser = ChatNotificationParser(
id: chatId,
callback: _callback,
);
ChatMessageListener({required this.chatId}) {
_parser = ChatNotificationParser(id: chatId, callback: _callback);
_subscription = RustStreamReceiver.listen(
(observable) => _parser?.parse(observable),
);
Expand All @@ -41,7 +26,9 @@ class ChatMessageListener {
final String chatId;
StreamSubscription<SubscribeObject>? _subscription;
ChatNotificationParser? _parser;

ChatMessageCallback? chatMessageCallback;
ChatMessageCallback? lastSentMessageCallback;
ChatErrorMessageCallback? chatErrorMessageCallback;
LatestMessageCallback? latestMessageCallback;
PrevMessageCallback? prevMessageCallback;
Expand All @@ -52,58 +39,45 @@ class ChatMessageListener {
ChatErrorMessageCallback? chatErrorMessageCallback,
LatestMessageCallback? latestMessageCallback,
PrevMessageCallback? prevMessageCallback,
ChatMessageCallback? lastSentMessageCallback,
void Function()? finishAnswerQuestionCallback,
}) {
this.chatMessageCallback = chatMessageCallback;
this.chatErrorMessageCallback = chatErrorMessageCallback;
this.finishAnswerQuestionCallback = finishAnswerQuestionCallback;
this.latestMessageCallback = latestMessageCallback;
this.prevMessageCallback = prevMessageCallback;
this.lastSentMessageCallback = lastSentMessageCallback;
this.finishAnswerQuestionCallback = finishAnswerQuestionCallback;
}

void _callback(
ChatNotification ty,
FlowyResult<Uint8List, FlowyError> result,
) {
switch (ty) {
case ChatNotification.DidReceiveChatMessage:
result.map(
(r) {
final value = ChatMessagePB.fromBuffer(r);
chatMessageCallback?.call(value);
},
);
break;
case ChatNotification.ChatMessageError:
result.map(
(r) {
final value = ChatMessageErrorPB.fromBuffer(r);
chatErrorMessageCallback?.call(value);
},
);
break;
case ChatNotification.DidLoadLatestChatMessage:
result.map(
(r) {
final value = ChatMessageListPB.fromBuffer(r);
latestMessageCallback?.call(value);
},
);
break;
case ChatNotification.DidLoadPrevChatMessage:
result.map(
(r) {
final value = ChatMessageListPB.fromBuffer(r);
prevMessageCallback?.call(value);
},
);
break;
case ChatNotification.FinishAnswerQuestion:
finishAnswerQuestionCallback?.call();
break;
default:
break;
}
result.map((r) {
switch (ty) {
case ChatNotification.DidReceiveChatMessage:
chatMessageCallback?.call(ChatMessagePB.fromBuffer(r));
break;
case ChatNotification.LastSentMessage:
lastSentMessageCallback?.call(ChatMessagePB.fromBuffer(r));
break;
case ChatNotification.ChatMessageError:
chatErrorMessageCallback?.call(ChatMessageErrorPB.fromBuffer(r));
break;
case ChatNotification.DidLoadLatestChatMessage:
latestMessageCallback?.call(ChatMessageListPB.fromBuffer(r));
break;
case ChatNotification.DidLoadPrevChatMessage:
prevMessageCallback?.call(ChatMessageListPB.fromBuffer(r));
break;
case ChatNotification.FinishAnswerQuestion:
finishAnswerQuestionCallback?.call();
break;
default:
break;
}
});
}

Future<void> stop() async {
Expand Down
Loading

0 comments on commit 2926fc0

Please sign in to comment.