Skip to content

Commit

Permalink
Merge pull request #59 from mylxsw/feature/1.0.8
Browse files Browse the repository at this point in the history
- 非 Web 端聊天支持显示 Latex 公式,代码高亮
- 聊天界面部分细节、错误信息展示优化
- 解决 Web 端聊天分享页面乱的问题
  • Loading branch information
mylxsw authored Nov 6, 2023
2 parents 4286077 + 024eacd commit 44be3d2
Show file tree
Hide file tree
Showing 18 changed files with 505 additions and 89 deletions.
4 changes: 3 additions & 1 deletion lib/bloc/chat_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class ChatMessageReceivedEvent extends ChatMessageEvent {

class ChatMessageSendEvent extends ChatMessageEvent {
final Message message;
final int? index;
final bool isResent;

ChatMessageSendEvent(this.message);
ChatMessageSendEvent(this.message, {this.index, this.isResent = false});
}

class ChatMessageGetRecentEvent extends ChatMessageEvent {
Expand Down
31 changes: 30 additions & 1 deletion lib/bloc/chat_message_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,22 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
await chatMsgRepo.fixMessageStatus(roomId);

// 记录当前消息
final sentMessageId = await chatMsgRepo.sendMessage(roomId, message);
var sentMessageId = 0;
if (event.isResent &&
event.index == 0 &&
last != null &&
last.type == MessageType.text) {
// 如果当前是消息重发,同时重发的是最后一条消息,则不会重新生成该消息,直接生成答案即可
sentMessageId = last.id!;
if (last.statusIsFailed()) {
// 如果最后一条消息发送失败,则重新发送
await chatMsgRepo.updateMessagePart(roomId, last.id!, [
MessagePart('status', 0),
]);
}
} else {
sentMessageId = await chatMsgRepo.sendMessage(roomId, message);
}

// 更新 Room 最后活跃时间
// 这里没有使用 await,因为不需要等待更新完成,让 room 的更新异步的去处理吧
Expand Down Expand Up @@ -312,6 +327,15 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
if (systemCmds.isNotEmpty) {
for (var element in systemCmds) {
try {
// SYSTEM 命令
// - type: 命令类型
//
// type=summary (默认值)
// - question_id: 问题 ID
// - answer_id: 答案 ID
// - quota_consumed: 消耗的配额
// - token: 消耗的 token
// - info: 提示信息
final cmd = jsonDecode(element.content);

message.serverId = cmd['question_id'];
Expand All @@ -320,6 +344,11 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
final quotaConsumed = cmd['quota_consumed'] ?? 0;
final tokenConsumed = cmd['token'] ?? 0;

final info = cmd['info'] ?? '';
if (info != '') {
waitMessage.setExtra({'info': info});
}

if (quotaConsumed == 0 && tokenConsumed == 0) {
continue;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/bloc/group_chat_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class GroupChatSendEvent extends GroupChatEvent {
final int groupId;
final String message;
final List<int> members;
final int? index;
final bool isResent;

GroupChatSendEvent(this.groupId, this.message, this.members);
GroupChatSendEvent(this.groupId, this.message, this.members,
{this.index, this.isResent = false});
}

class GroupChatUpdateMessageStatusEvent extends GroupChatEvent {
Expand Down
2 changes: 1 addition & 1 deletion lib/helper/constant.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

// 客户端应用版本号
const clientVersion = '1.0.7';
const clientVersion = '1.0.8';
// 本地数据库版本号
const databaseVersion = 25;

Expand Down
10 changes: 8 additions & 2 deletions lib/helper/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import 'package:dart_openai/openai.dart';
Object resolveErrorMessage(dynamic e, {bool isChat = false}) {
// TODO
if (e is RequestFailedException) {
final msg = resolveHTTPStatusCode(e.statusCode, isChat: isChat);
final msg =
resolveHTTPStatusCode(e.statusCode, isChat: isChat, message: e.message);
if (msg != null) {
return msg;
}
Expand All @@ -16,7 +17,8 @@ Object resolveErrorMessage(dynamic e, {bool isChat = false}) {
return e.toString();
}

Object? resolveHTTPStatusCode(int statusCode, {bool isChat = false}) {
Object? resolveHTTPStatusCode(int statusCode,
{bool isChat = false, String? message}) {
switch (statusCode) {
case 400:
return const LanguageText('请求参数错误');
Expand Down Expand Up @@ -46,6 +48,10 @@ Object? resolveHTTPStatusCode(int statusCode, {bool isChat = false}) {
case 402:
return const LanguageText(AppLocale.quotaExceeded, action: 'payment');
case 500:
if (message != null && message.isNotEmpty) {
return message;
}

return const LanguageText(AppLocale.internalServerError);
case 502:
return const LanguageText(AppLocale.badGateway);
Expand Down
1 change: 1 addition & 0 deletions lib/page/chat/component/group_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:askaide/helper/image.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class GroupAvatar extends StatelessWidget {
final double size;
final double padding;
Expand Down
23 changes: 15 additions & 8 deletions lib/page/chat/group/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,15 @@ class _GroupChatPageState extends State<GroupChatPage> {
onDeleteMessage: (id) {
handleDeleteMessage(context, id);
},
onResetContext: () => handleResetContext(context),
onSpeakEvent: (message) {
_audioPlayerController.playAudio(message.text);
},
onResentEvent: (message) {
onResentEvent: (message, index) {
_scrollController.animateTo(0,
duration: const Duration(milliseconds: 500),
curve: Curves.easeOut);
_handleSubmit(
message.text,
);
_handleSubmit(message.text, index: index, isResent: true);
},
helpWidgets: state.hasWaitTasks || loadedMessages.isEmpty
? null
Expand Down Expand Up @@ -505,15 +504,23 @@ class _GroupChatPageState extends State<GroupChatPage> {
}

/// 提交新消息
void _handleSubmit(String text) {
void _handleSubmit(
String text, {
int? index,
bool isResent = false,
}) {
setState(() {
_inputEnabled.value = false;
});

var replyMemberIds = (selectedMembers ?? []).map((e) => e.id!).toList();
context
.read<GroupChatBloc>()
.add(GroupChatSendEvent(widget.groupId, text, replyMemberIds));
context.read<GroupChatBloc>().add(GroupChatSendEvent(
widget.groupId,
text,
replyMemberIds,
index: index,
isResent: isResent,
));
}

/// 处理消息删除事件
Expand Down
15 changes: 12 additions & 3 deletions lib/page/chat/home_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,13 @@ class _HomeChatPageState extends State<HomeChatPage> {
onDeleteMessage: (id) {
handleDeleteMessage(context, id, chatHistoryId: chatId);
},
onResentEvent: (message) {
onResetContext: () => handleResetContext(context),
onResentEvent: (message, index) {
_scrollController.animateTo(0,
duration: const Duration(milliseconds: 500), curve: Curves.easeOut);

_handleSubmit(message.text, messagetType: message.type);
_handleSubmit(message.text,
messagetType: message.type, index: index, isResent: true);
},
onSpeakEvent: (message) {
_audioPlayerController.playAudio(message.text);
Expand All @@ -427,7 +429,12 @@ class _HomeChatPageState extends State<HomeChatPage> {
}

/// 提交新消息
void _handleSubmit(String text, {messagetType = MessageType.text}) {
void _handleSubmit(
String text, {
messagetType = MessageType.text,
int? index,
bool isResent = false,
}) {
setState(() {
_inputEnabled.value = false;
});
Expand All @@ -443,6 +450,8 @@ class _HomeChatPageState extends State<HomeChatPage> {
type: messagetType,
chatHistoryId: chatId,
),
index: index,
isResent: isResent,
),
);

Expand Down
15 changes: 12 additions & 3 deletions lib/page/chat/room_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,16 @@ class _RoomChatPageState extends State<RoomChatPage> {
onDeleteMessage: (id) {
handleDeleteMessage(context, id);
},
onResetContext: () => handleResetContext(context),
onSpeakEvent: (message) {
_audioPlayerController.playAudio(message.text);
},
onResentEvent: (message) {
onResentEvent: (message, index) {
_scrollController.animateTo(0,
duration: const Duration(milliseconds: 500),
curve: Curves.easeOut);
_handleSubmit(message.text, messagetType: message.type);
_handleSubmit(message.text,
messagetType: message.type, index: index, isResent: true);
},
helpWidgets: state.processing || loadedMessages.last.isInitMessage()
? null
Expand Down Expand Up @@ -390,7 +392,12 @@ class _RoomChatPageState extends State<RoomChatPage> {
}

/// 提交新消息
void _handleSubmit(String text, {messagetType = MessageType.text}) {
void _handleSubmit(
String text, {
messagetType = MessageType.text,
int? index,
bool isResent = false,
}) {
setState(() {
_inputEnabled.value = false;
});
Expand All @@ -404,6 +411,8 @@ class _RoomChatPageState extends State<RoomChatPage> {
ts: DateTime.now(),
type: messagetType,
),
index: index,
isResent: isResent,
),
);

Expand Down
Loading

0 comments on commit 44be3d2

Please sign in to comment.