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 String systemPrompt,
});

11
lib/data/services/deapsound_ai_service.dart

@ -23,7 +23,7 @@ class DeapsoundAIService implements AiService {
}
@override
Stream<String> sendMessageStream({
Stream sendMessageStream({
required List<Map<String, String>> 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;

39
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();
}

8
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,
);
}
}
}

Loading…
Cancel
Save