Browse Source

修改ai回复卡片

weicu
tanlongsheng 2 years ago
parent
commit
e55aa913d1
  1. 2
      lib/data/services/ai_service.dart
  2. 11
      lib/data/services/deapsound_ai_service.dart
  3. 39
      lib/modules/chat/controllers/chat_controller.dart
  4. 8
      lib/modules/chat/models/message_model.dart

2
lib/data/services/ai_service.dart

@ -7,7 +7,7 @@ abstract class AiService {
}); });
/// 流式输出方法 /// 流式输出方法
Stream<String> sendMessageStream({ Stream sendMessageStream({
required List<Map<String, String>> messages, required List<Map<String, String>> messages,
required String systemPrompt, required String systemPrompt,
}); });

11
lib/data/services/deapsound_ai_service.dart

@ -23,7 +23,7 @@ class DeapsoundAIService implements AiService {
} }
@override @override
Stream<String> sendMessageStream({ Stream sendMessageStream({
required List<Map<String, String>> messages, required List<Map<String, String>> messages,
required String systemPrompt, required String systemPrompt,
}) async* { }) async* {
@ -67,10 +67,13 @@ class DeapsoundAIService implements AiService {
try { try {
final data = jsonDecode(jsonStr); final data = jsonDecode(jsonStr);
if (data['event'] == 'workflow_finished') { if (data['role'] == 'card') {
break; yield data['meta'];
if (data['meta']['musicaudio'] != null) {
break;
}
} }
if (data['content'] != null) { if (data['role'] == 'ai') {
String content = data['content'] ?? ''; String content = data['content'] ?? '';
if (content.isNotEmpty) { if (content.isNotEmpty) {
yield content; yield content;

39
lib/modules/chat/controllers/chat_controller.dart

@ -343,14 +343,18 @@ class ChatController extends GetxController {
(chunk) { (chunk) {
if (_shouldCancelAiResponse) return; if (_shouldCancelAiResponse) return;
fullResponse += chunk; if (chunk is Map) {
_updateLoadingMessage(data: chunk);
// 更新加载中的消息内容 } else {
_updateLoadingMessage(fullResponse); fullResponse += chunk;
// 如果TTS启用,使用流式处理发送到TTS // 更新加载中的消息内容
if (isTtsEnabled.value) { _updateLoadingMessage(content: fullResponse);
_ttsService.speakStream(chunk);
// 如果TTS启用,使用流式处理发送到TTS
if (isTtsEnabled.value) {
_ttsService.speakStream(chunk);
}
} }
}, },
onDone: () { 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); final index = messages.indexWhere((m) => m.isLoading);
if (index != -1) { if (index != -1) {
messages[index] = messages[index].copyWith( if (data != null) {
content: content, Map messagesData = messages[index].data ?? {};
isLoading: true, messages[index] = messages[index].copyWith(
); data: {...messagesData, ...data},
isLoading: true,
);
}
if (content != null) {
messages[index] = messages[index].copyWith(
content: content,
isLoading: true,
);
}
messages.refresh(); messages.refresh();
_scrollToBottom(); _scrollToBottom();
} }

8
lib/modules/chat/models/message_model.dart

@ -1,6 +1,7 @@
class Message { class Message {
final String role; // 'user' or 'assistant' final String role; // 'user' or 'assistant'
final String content; final String content;
final Map? data;
final DateTime timestamp; final DateTime timestamp;
final bool isLoading; final bool isLoading;
@ -8,6 +9,7 @@ class Message {
required this.role, required this.role,
required this.content, required this.content,
required this.timestamp, required this.timestamp,
this.data,
this.isLoading = false, this.isLoading = false,
}); });
@ -16,6 +18,7 @@ class Message {
return Message( return Message(
role: json['role'] as String, role: json['role'] as String,
content: json['content'] as String, content: json['content'] as String,
data: json['data'],
timestamp: DateTime.parse(json['timestamp'] as String), timestamp: DateTime.parse(json['timestamp'] as String),
isLoading: json['isLoading'] as bool? ?? false, isLoading: json['isLoading'] as bool? ?? false,
); );
@ -26,6 +29,7 @@ class Message {
return { return {
'role': role, 'role': role,
'content': content, 'content': content,
'data': data,
'timestamp': timestamp.toIso8601String(), 'timestamp': timestamp.toIso8601String(),
'isLoading': isLoading, 'isLoading': isLoading,
}; };
@ -45,14 +49,16 @@ class Message {
Message copyWith({ Message copyWith({
String? role, String? role,
String? content, String? content,
Map? data,
DateTime? timestamp, DateTime? timestamp,
bool? isLoading, bool? isLoading,
}) { }) {
return Message( return Message(
role: role ?? this.role, role: role ?? this.role,
content: content ?? this.content, content: content ?? this.content,
data: data ?? this.data,
timestamp: timestamp ?? this.timestamp, timestamp: timestamp ?? this.timestamp,
isLoading: isLoading ?? this.isLoading, isLoading: isLoading ?? this.isLoading,
); );
} }
} }

Loading…
Cancel
Save