merge commit

This commit is contained in:
Chen Li
2025-08-22 17:35:02 +08:00
parent 926f162287
commit e39f42a3df
6 changed files with 71 additions and 59 deletions

View File

@@ -33,7 +33,10 @@ class MessageService extends ChangeNotifier {
_asrChannel.setMethodCallHandler((call) async {
switch (call.method) {
case "onAsrResult":
replaceMessage(id: _latestUserMessageId!, text: call.arguments);
replaceMessage(
id: _latestUserMessageId!,
text: call.arguments,
status: MessageStatus.normal);
break;
case "onAsrStop":
int index = findMessageIndexById(_latestUserMessageId!);
@@ -45,8 +48,6 @@ class MessageService extends ChangeNotifier {
removeMessageById(_latestUserMessageId!);
return;
}
replaceMessage(
id: _latestUserMessageId!, status: MessageStatus.normal);
if (_asrCompleter != null && !_asrCompleter!.isCompleted) {
_asrCompleter!.complete(messages.last.text);
}
@@ -60,7 +61,7 @@ class MessageService extends ChangeNotifier {
// final AudioRecorderService _audioService = AudioRecorderService();
// final VoiceRecognitionService _recognitionService = VoiceRecognitionService();
final TextClassificationService _classificationService =
TextClassificationService();
TextClassificationService();
final VehicleCommandService _vehicleCommandService = VehicleCommandService();
final List<ChatMessage> _messages = [];
@@ -107,9 +108,8 @@ class MessageService extends ChangeNotifier {
_latestAssistantMessageId = null;
_isReplyAborted = false;
changeState(MessageServiceState.recording);
addMessage("", true, MessageStatus.listening);
_latestUserMessageId = messages.last.id;
_asrChannel.invokeMethod("start");
_latestUserMessageId = addMessage("", true, MessageStatus.listening);
_asrChannel.invokeMethod("startAsr");
} catch (e) {
print('录音开始出错: $e');
}
@@ -120,14 +120,16 @@ class MessageService extends ChangeNotifier {
notifyListeners();
}
void addMessage(String text, bool isUser, MessageStatus status) {
String addMessage(String text, bool isUser, MessageStatus status) {
String uuid = Uuid().v1();
_messages.add(ChatMessage(
id: Uuid().v1(),
id: uuid,
text: text,
isUser: isUser,
timestamp: DateTime.now(),
status: status));
notifyListeners();
return uuid;
}
Future<void> stopAndProcessVoiceInput() async {
@@ -137,7 +139,7 @@ class MessageService extends ChangeNotifier {
}
try {
changeState(MessageServiceState.recognizing);
_asrChannel.invokeMethod("stop");
_asrChannel.invokeMethod("stopAsr");
_asrCompleter = Completer<String>();
final recognizedText = await _asrCompleter!.future;
// final audioData = await _audioService.stopRecording();
@@ -194,8 +196,7 @@ class MessageService extends ChangeNotifier {
}
Future<void> reply(String text) async {
addMessage("", false, MessageStatus.thinking);
_latestAssistantMessageId = messages.last.id;
_latestAssistantMessageId = addMessage("", false, MessageStatus.thinking);
bool isChinese = CommonUtil.containChinese(text);
try {
if (_isReplyAborted) {
@@ -226,7 +227,7 @@ class MessageService extends ChangeNotifier {
return;
}
final vehicleCommandResponse =
await _vehicleCommandService.getCommandFromText(text);
await _vehicleCommandService.getCommandFromText(text);
if (vehicleCommandResponse == null) {
if (_isReplyAborted) {
return;
@@ -249,9 +250,11 @@ class MessageService extends ChangeNotifier {
if (!_isReplyAborted) {
if (await TtsUtil.start(isChinese) == true) {
TtsUtil.send(vehicleCommandResponse.tips!);
Future.delayed(const Duration(milliseconds: 300), () => TtsUtil.complete());
}
}
bool containOpenAC = false;
List<String> successCommandList = [];
for (var command in vehicleCommandResponse.commands) {
if (_isReplyAborted) {
return;
@@ -263,11 +266,15 @@ class MessageService extends ChangeNotifier {
if (command.type == VehicleCommandType.openAC) {
containOpenAC = true;
}
bool isSuccess;
if (containOpenAC && command.type == VehicleCommandType.changeACTemp) {
await Future.delayed(const Duration(milliseconds: 2000),
() => processCommand(command, isChinese));
isSuccess = await Future.delayed(const Duration(milliseconds: 2000),
() => processCommand(command, isChinese));
} else {
await processCommand(command, isChinese);
isSuccess = await processCommand(command, isChinese);
}
if (isSuccess) {
successCommandList.add(command.type.name);
}
}
replaceMessage(
@@ -275,12 +282,21 @@ class MessageService extends ChangeNotifier {
text: vehicleCommandResponse.tips!,
status: MessageStatus.normal);
notifyListeners();
if (_isReplyAborted || successCommandList.isEmpty) {
return;
}
String controlResponse =
await _vehicleCommandService.getControlResponse(successCommandList);
if (_isReplyAborted || controlResponse.isEmpty) {
return;
}
addMessage(controlResponse, false, MessageStatus.normal);
}
}
Future<void> processCommand(VehicleCommand command, bool isChinese) async {
String msg;
MessageStatus status;
Future<bool> processCommand(VehicleCommand command, bool isChinese) async {
String msg = "";
MessageStatus status = MessageStatus.normal;
final (isSuccess, result) = await CommandService.executeCommand(
command.type,
params: command.params);
@@ -299,19 +315,17 @@ class MessageService extends ChangeNotifier {
}
}
} else {
if (isSuccess) {
msg = isChinese
? "为您执行\"${command.type.chinese}\"成功"
: "Execute \"${command.type.english}\" successful";
status = MessageStatus.success;
} else {
if (!isSuccess) {
msg = isChinese
? "很抱歉,\"${command.type.chinese}\"失败"
: "Sorry, execute \"${command.type.english}\" unsuccessfully";
status = MessageStatus.failure;
}
}
addMessage(msg, false, status);
if (msg.isNotEmpty) {
addMessage(msg, false, status);
}
return isSuccess;
}
Future<void> answerWrongQuestion(bool isChinese) async {