From 028471a2b7c008d8190a976f040c018c908972e3 Mon Sep 17 00:00:00 2001 From: Yang Jiahui Date: Mon, 18 Aug 2025 13:34:57 +0800 Subject: [PATCH] [bugfix] chat bubble error and Pronounce ID.UNYX error --- lib/screens/full_screen.dart | 12 ++++---- lib/screens/part_screen.dart | 5 ++-- lib/services/chat_sse_service.dart | 6 +++- lib/services/message_service.dart | 9 ++++++ lib/utils/common_util.dart | 9 ++++-- lib/widgets/chat_bubble.dart | 47 +++++++++++++++--------------- 6 files changed, 52 insertions(+), 36 deletions(-) diff --git a/lib/screens/full_screen.dart b/lib/screens/full_screen.dart index 3fa75a3..ed9d22e 100644 --- a/lib/screens/full_screen.dart +++ b/lib/screens/full_screen.dart @@ -62,23 +62,21 @@ class _FullScreenState extends State { const SizedBox(height: 12), Expanded( child: Consumer( - builder: (context, handler, child) { + builder: (context, messageService, child) { WidgetsBinding.instance.addPostFrameCallback((_) { _scrollToBottom(); }); - final messageService = context.read(); - List fullScreenMessages = List.from(handler.messages); - if (fullScreenMessages.isEmpty) { - Future.delayed(const Duration(milliseconds: 500), () { + if (messageService.messages.isEmpty) { + // Future.delayed(const Duration(milliseconds: 500), () { String text = Intl.getCurrentLocale().startsWith('zh') ? "您好,我是众众,请问有什么可以帮您?" : "Hi, I'm Zhongzhong, may I help you ? "; messageService.addMessage(text, false, MessageStatus.normal); - }); + // }); } return ChatBox( scrollController: _scrollController, - messages: handler.messages, + messages: messageService.messages, ); }, ), diff --git a/lib/screens/part_screen.dart b/lib/screens/part_screen.dart index 7987be0..1c75f8e 100644 --- a/lib/screens/part_screen.dart +++ b/lib/screens/part_screen.dart @@ -110,8 +110,7 @@ class _PartScreenState extends State { _scrollToBottom(); }); return Consumer( - builder: (context, handler, child) { - final messages = handler.messages; + builder: (context, messageService, child) { return AnimatedContainer( duration: const Duration(milliseconds: 180), curve: Curves.easeInOut, @@ -145,7 +144,7 @@ class _PartScreenState extends State { child: ChatBox( scrollController: _scrollController, - messages: messages, + messages: messageService.messages, ), ), ), diff --git a/lib/services/chat_sse_service.dart b/lib/services/chat_sse_service.dart index 88d77cd..313dabd 100644 --- a/lib/services/chat_sse_service.dart +++ b/lib/services/chat_sse_service.dart @@ -78,6 +78,10 @@ class ChatSseService { break; } tempText += textChunk; + if (tempText.contains("ID.")) { + tempText = tempText.replaceAllMapped( + RegExp(r'ID\.', caseSensitive: false), (m) => 'ID '); + } int endIndex = _getCompleteTextEndIndex(tempText); String completeText = CommonUtil.cleanText(tempText.substring(0, endIndex).trim(), true); if (completeText.isNotEmpty) { @@ -136,7 +140,7 @@ class ChatSseService { int _getCompleteTextEndIndex(String buffer) { // 支持句号、问号、感叹号和换行符作为分割依据 - final sentenceEnders = RegExp(r'[,!?:,。!?:\n]'); + final sentenceEnders = RegExp(r'[,.!?:,。!?:\n]'); final matches = sentenceEnders.allMatches(buffer); return matches.isEmpty ? 0 : matches.last.end; } diff --git a/lib/services/message_service.dart b/lib/services/message_service.dart index 2ce8a29..a4454dd 100644 --- a/lib/services/message_service.dart +++ b/lib/services/message_service.dart @@ -36,6 +36,15 @@ class MessageService extends ChangeNotifier { replaceMessage(id: _latestUserMessageId!, text: call.arguments); break; case "onAsrStop": + int index = findMessageIndexById(_latestUserMessageId!); + if (index == -1) { + return; + } + final message = _messages[index]; + if (message.text.isEmpty) { + removeMessageById(_latestUserMessageId!); + return; + } replaceMessage( id: _latestUserMessageId!, status: MessageStatus.normal); if (_asrCompleter != null && !_asrCompleter!.isCompleted) { diff --git a/lib/utils/common_util.dart b/lib/utils/common_util.dart index 56b52a4..201dfbd 100644 --- a/lib/utils/common_util.dart +++ b/lib/utils/common_util.dart @@ -50,8 +50,13 @@ class CommonUtil { .trim(); if (forTts) { - cleanedText = cleanedText.replaceAllMapped( - RegExp(r'ID\.UNYX', caseSensitive: false), (m) => 'I D Unix'); + if (cleanedText.contains("ID.UNYX")) { + cleanedText = cleanedText.replaceAllMapped( + RegExp(r'ID\.UNYX', caseSensitive: false), (m) => 'ID Unix'); + } else { + cleanedText = cleanedText.replaceAllMapped( + RegExp(r'UNYX', caseSensitive: false), (m) => 'Unix'); + } } return cleanedText; diff --git a/lib/widgets/chat_bubble.dart b/lib/widgets/chat_bubble.dart index 21a444a..ed353c5 100644 --- a/lib/widgets/chat_bubble.dart +++ b/lib/widgets/chat_bubble.dart @@ -61,30 +61,31 @@ class _ChatBubbleState extends State { ), child: _buildAssistantContent(isThinking), ) - : IntrinsicWidth( - child: Container( - padding: const EdgeInsets.symmetric( - horizontal: 12, vertical: 12), - decoration: BoxDecoration( - color: message.isUser - ? CommonUtil.commonColor - : Colors.white.withValues(alpha: 0.12), - borderRadius: message.isUser - ? BorderRadius.only( - topLeft: Radius.circular(12), - bottomLeft: Radius.circular(12), - bottomRight: Radius.circular(12), - ) - : BorderRadius.only( - topRight: Radius.circular(12), - bottomLeft: Radius.circular(12), - bottomRight: Radius.circular(12), - ), - ), - child: message.isUser - ? _buildUserContent() - : _buildAssistantContent(isThinking), + : Container( + constraints: const BoxConstraints( + minWidth: 50, ), + padding: const EdgeInsets.symmetric( + horizontal: 12, vertical: 12), + decoration: BoxDecoration( + color: message.isUser + ? CommonUtil.commonColor + : Colors.white.withOpacity(0.12), + borderRadius: message.isUser + ? BorderRadius.only( + topLeft: Radius.circular(12), + bottomLeft: Radius.circular(12), + bottomRight: Radius.circular(12), + ) + : BorderRadius.only( + topRight: Radius.circular(12), + bottomLeft: Radius.circular(12), + bottomRight: Radius.circular(12), + ), + ), + child: message.isUser + ? _buildUserContent() + : _buildAssistantContent(isThinking), ), ), ],