From 83c6237a5c6dd3ed0bb97fb4d0a67431627b76d1 Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Sun, 6 Apr 2025 12:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ai=E5=9B=9E=E5=A4=8D=E5=8D=A1?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/services/ai_service.dart | 2 +- lib/data/services/deapsound_ai_service.dart | 11 ++++-- .../chat/controllers/chat_controller.dart | 39 ++++++++++++------- lib/modules/chat/models/message_model.dart | 8 +++- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lib/data/services/ai_service.dart b/lib/data/services/ai_service.dart index 37a1f5df0..949dfca67 100644 --- a/lib/data/services/ai_service.dart +++ b/lib/data/services/ai_service.dart @@ -7,7 +7,7 @@ abstract class AiService { }); /// 流式输出方法 - Stream sendMessageStream({ + Stream sendMessageStream({ required List> messages, required String systemPrompt, }); diff --git a/lib/data/services/deapsound_ai_service.dart b/lib/data/services/deapsound_ai_service.dart index 5d1e652b1..dc85acebb 100644 --- a/lib/data/services/deapsound_ai_service.dart +++ b/lib/data/services/deapsound_ai_service.dart @@ -23,7 +23,7 @@ class DeapsoundAIService implements AiService { } @override - Stream sendMessageStream({ + Stream sendMessageStream({ required List> messages, required String systemPrompt, }) async* { @@ -67,10 +67,13 @@ class DeapsoundAIService implements AiService { try { final data = jsonDecode(jsonStr); - if (data['event'] == 'workflow_finished') { - break; + if (data['role'] == 'card') { + yield data['meta']; + if (data['meta']['musicaudio'] != null) { + break; + } } - if (data['content'] != null) { + if (data['role'] == 'ai') { String content = data['content'] ?? ''; if (content.isNotEmpty) { yield content; diff --git a/lib/modules/chat/controllers/chat_controller.dart b/lib/modules/chat/controllers/chat_controller.dart index 4d0457c98..4b62a1a9f 100644 --- a/lib/modules/chat/controllers/chat_controller.dart +++ b/lib/modules/chat/controllers/chat_controller.dart @@ -343,14 +343,18 @@ class ChatController extends GetxController { (chunk) { if (_shouldCancelAiResponse) return; - fullResponse += chunk; - - // 更新加载中的消息内容 - _updateLoadingMessage(fullResponse); - - // 如果TTS启用,使用流式处理发送到TTS - if (isTtsEnabled.value) { - _ttsService.speakStream(chunk); + if (chunk is Map) { + _updateLoadingMessage(data: chunk); + } else { + fullResponse += chunk; + + // 更新加载中的消息内容 + _updateLoadingMessage(content: fullResponse); + + // 如果TTS启用,使用流式处理发送到TTS + if (isTtsEnabled.value) { + _ttsService.speakStream(chunk); + } } }, onDone: () { @@ -402,13 +406,22 @@ class ChatController extends GetxController { } // 更新加载中的消息 - void _updateLoadingMessage(String content) { + void _updateLoadingMessage({Map? data, String? content}) { final index = messages.indexWhere((m) => m.isLoading); if (index != -1) { - messages[index] = messages[index].copyWith( - content: content, - isLoading: true, - ); + if (data != null) { + Map messagesData = messages[index].data ?? {}; + messages[index] = messages[index].copyWith( + data: {...messagesData, ...data}, + isLoading: true, + ); + } + if (content != null) { + messages[index] = messages[index].copyWith( + content: content, + isLoading: true, + ); + } messages.refresh(); _scrollToBottom(); } diff --git a/lib/modules/chat/models/message_model.dart b/lib/modules/chat/models/message_model.dart index 6307989ae..69a7a5b76 100644 --- a/lib/modules/chat/models/message_model.dart +++ b/lib/modules/chat/models/message_model.dart @@ -1,6 +1,7 @@ class Message { final String role; // 'user' or 'assistant' final String content; + final Map? data; final DateTime timestamp; final bool isLoading; @@ -8,6 +9,7 @@ class Message { required this.role, required this.content, required this.timestamp, + this.data, this.isLoading = false, }); @@ -16,6 +18,7 @@ class Message { return Message( role: json['role'] as String, content: json['content'] as String, + data: json['data'], timestamp: DateTime.parse(json['timestamp'] as String), isLoading: json['isLoading'] as bool? ?? false, ); @@ -26,6 +29,7 @@ class Message { return { 'role': role, 'content': content, + 'data': data, 'timestamp': timestamp.toIso8601String(), 'isLoading': isLoading, }; @@ -45,14 +49,16 @@ class Message { Message copyWith({ String? role, String? content, + Map? data, DateTime? timestamp, bool? isLoading, }) { return Message( role: role ?? this.role, content: content ?? this.content, + data: data ?? this.data, timestamp: timestamp ?? this.timestamp, isLoading: isLoading ?? this.isLoading, ); } -} \ No newline at end of file +}