From b7dfc99c6d7fd3c12668ddfec7534e5c827f0124 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 13 May 2025 15:42:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8D=A1=E7=89=87=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=20=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/controllers/agent_controller.dart | 59 ++++++++--------- lib/modules/agent/views/message_bubble.dart | 4 -- .../controllers/pairing_controller.dart | 63 ++++++++++--------- 3 files changed, 64 insertions(+), 62 deletions(-) diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index 12fdc30e7..2aef7f7ac 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -347,35 +347,38 @@ class AgentController extends GetxController { break; case AgentServiceEventType.functionCallResult: + final responseId = event.data['responseId'] ?? ''; final result = event.data['result'] ?? ''; - final parsedResult = result.isNotEmpty ? jsonDecode(result) : {}; - final meta = parsedResult["meta"] ?? {}; - // final functionCall = event.data['function_call'] ?? ''; - logger.d('mcp 执行结果meta: $meta'); - - // if (meta.isNotEmpty) { - // // logger.i('Flutter 结束调用 mcp metaResult: $metaResult'); - // // 判断是否为新的回复或响应ID是否改变 - // if (_isNewAssistantResponse) { - // // 添加新消息 - // messages.add(Message(isUser: false, text: "", meta: meta)); - // _isNewAssistantResponse = false; - // } else { - // // 更新最后一条消息 - // final lastIndex = messages.length - 1; - // if (lastIndex >= 0 && !messages[lastIndex].isUser) { - // messages[lastIndex] = Message( - // isUser: false, - // text: messages[lastIndex].text, - // meta: meta, - // timestamp: messages[lastIndex].timestamp, - // ); - // } - // } - - // _scrollToBottom(); - // } - logger.i('mcp 结果处理完成'); + final functionCall = event.data['function_call'] ?? ''; + logger.i('Flutter 结束调用 mcp: $result, $functionCall'); + + if (result.isNotEmpty) { + final parsedResult = jsonDecode(result); + final meta = parsedResult["meta"] ?? ""; + final metaResult = jsonDecode(meta); + + logger.i('Flutter 结束调用 mcp metaResult: $metaResult'); + // 判断是否为新的回复或响应ID是否改变 + if (_isNewAssistantResponse || _currentResponseId != responseId) { + // 添加新消息 + messages.add(Message(isUser: false, text: "", meta: metaResult)); + _isNewAssistantResponse = false; + _currentResponseId = responseId; + } else { + // 更新最后一条消息 + final lastIndex = messages.length - 1; + if (lastIndex >= 0 && !messages[lastIndex].isUser) { + messages[lastIndex] = Message( + isUser: false, + text: messages[lastIndex].text, + meta: meta, + timestamp: messages[lastIndex].timestamp, + ); + } + } + // 滚动到底部 + _scrollToBottom(); + } break; default: break; diff --git a/lib/modules/agent/views/message_bubble.dart b/lib/modules/agent/views/message_bubble.dart index 78f1acda4..952551e18 100644 --- a/lib/modules/agent/views/message_bubble.dart +++ b/lib/modules/agent/views/message_bubble.dart @@ -209,14 +209,10 @@ class MessageBubble extends GetView { final rawList = mate!['card_musiclist']['musics'] as List; final playlist = rawList.map((e) => Map.from(e)).toList(); - // final playlist = - // mate!['card_musiclist']['musics'] as List>; - //音乐播放列表 return _buildMusicContent(context, playlist); } else if (mate!.keys.contains('card_weather')) { final city = mate!['card_weather']['city'] as String; - final rawList = mate!['card_weather']['dayweather'] as List; final daylist = rawList.map((e) => Map.from(e)).toList(); diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index 88a349161..7ecf4e076 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -14,6 +14,8 @@ import 'package:ble_service/ble_service.dart'; class PairingController extends GetxController { static PairingController get to => Get.find(); + // 需要设备 + final bool needdeivice = false; // 状态标志 final isLoading = false.obs; @@ -28,7 +30,7 @@ class PairingController extends GetxController { // 状态描述(用于UI显示) final statusDescription = 'initializing'.tr.obs; - + // 按钮文本 final actionButtonText = '去系统设置'.obs; @@ -117,7 +119,7 @@ class PairingController extends GetxController { // 刷新连接设备并检查配对状态 statusDescription.value = 'searchingDevice'.tr; await _bluetoothService.refreshDeviceConnection(); - + // 设备连接时检查配对状态 if (isDeviceConnected) { await _checkDevicePairingStatus(); @@ -146,7 +148,7 @@ class PairingController extends GetxController { } Logger.info('发现已关联的Companion设备: $pairedMac'); - + // 检查当前连接设备与已保存的设备是否一致 if (currentDevice!.address != pairedMac) { // MAC地址不匹配 @@ -154,13 +156,13 @@ class PairingController extends GetxController { statusDescription.value = 'pleaseConnectPairedDevice'.tr; return; } - + // 判断设备名称是否符合要求 if (!_isValidDeviceName(currentDevice!.name)) { statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; return; } - + // 设备匹配且合规,标记为已配对 Logger.info('当前设备与已配对设备匹配'); _companionAssociated.value = true; @@ -223,7 +225,7 @@ class PairingController extends GetxController { _bluetoothState.value = state; _updateStatusDescription(); }); - + // 监听设备连接状态变化 _deviceWorker = ever(_bluetoothService.currentDeviceRx, (BluetoothDevice? device) { @@ -238,12 +240,13 @@ class PairingController extends GetxController { /// 处理设备连接成功事件 Future _handleDeviceConnected() async { if (!isDeviceConnected || _isNavigatingToHome) return; - + // 检查配对状态,决定下一步操作 await _checkDevicePairingStatus(); - + // 如果还未配对且设备合规,启动配对流程 - if (!_companionAssociated.value && _isValidDeviceName(currentDevice!.name)) { + if (!_companionAssociated.value && + _isValidDeviceName(currentDevice!.name)) { _startCompanionAssociation(); } } @@ -256,7 +259,7 @@ class PairingController extends GetxController { actionButtonText.value = '关联DeapSound耳机'; return; } - + if (!hasPermissions) { statusDescription.value = 'permissionDenied'.tr; actionButtonText.value = '去系统设置'; @@ -271,9 +274,11 @@ class PairingController extends GetxController { if (isDeviceConnected) { if (Platform.isAndroid && !isCompanionAssociated && _isAssociating) { - statusDescription.value = '${'associatingDevice'.tr} ${currentDevice!.name}'; + statusDescription.value = + '${'associatingDevice'.tr} ${currentDevice!.name}'; } else { - statusDescription.value = '${'connectedDevice'.tr} ${currentDevice!.name}'; + statusDescription.value = + '${'connectedDevice'.tr} ${currentDevice!.name}'; } return; } @@ -286,21 +291,20 @@ class PairingController extends GetxController { statusDescription.value = 'notFound'.tr; } - /// 启动Companion设备关联流程 Future _startCompanionAssociation() async { // 如果不是Android平台,已跳转,已完成关联或正在进行关联,直接返回 - if (!Platform.isAndroid || - _isNavigatingToHome || - _companionAssociated.value || - _isAssociating || + if (!Platform.isAndroid || + _isNavigatingToHome || + _companionAssociated.value || + _isAssociating || !isDeviceConnected) { return; } - + // 重置拒绝状态 _userRejectedAssociation.value = false; - + // 设置关联状态标志 _isAssociating = true; _updateStatusDescription(); @@ -314,14 +318,14 @@ class PairingController extends GetxController { _autoNavigateToHome(); return; } - + // 检查设备名称是否符合要求 if (!_isValidDeviceName(currentDevice!.name)) { statusDescription.value = 'pleaseConnectDeepSoundDevice'.tr; _isAssociating = false; return; } - + Logger.info('开始Companion设备关联流程, 设备MAC: ${currentDevice!.address}'); var success = await _bleService.startDeviceAssociation( @@ -329,7 +333,7 @@ class PairingController extends GetxController { macAddress: currentDevice!.address, ); _isAssociating = false; - + if (success) { Logger.info('Companion设备关联成功'); _companionAssociated.value = true; @@ -338,8 +342,7 @@ class PairingController extends GetxController { // 注册后台扫描,确保应用被杀死后仍能被唤醒 await _bleService.registerBackgroundScan( macAddress: currentDevice!.address); - - + // 关联成功后跳转 _autoNavigateToHome(); } else { @@ -359,9 +362,9 @@ class PairingController extends GetxController { /// 自动跳转到主页 Future _autoNavigateToHome() async { - if (_isNavigatingToHome || !isDeviceConnected) return; + if (needdeivice && (_isNavigatingToHome || !isDeviceConnected)) return; - // 启动BLE后台服务 + // 启动BLE后台服务 await _startAgentService(); _isNavigatingToHome = true; @@ -375,7 +378,7 @@ class PairingController extends GetxController { /// 启动BLE后台服务 Future _startAgentService() async { try { - if (!isDeviceConnected) return; + if (needdeivice && !isDeviceConnected) return; final result = await BleManager.to.startAgentService(); Logger.info(result ? 'BLE后台服务启动成功' : 'BLE后台服务启动失败'); @@ -426,7 +429,7 @@ class PairingController extends GetxController { Future openAppSettings() async { await AppSettings.openAppSettings(); } - + /// 重新开始关联 void restartAssociation() { if (isDeviceConnected && !isCompanionAssociated) { @@ -435,8 +438,8 @@ class PairingController extends GetxController { } /// 跳过连接 - void skipConnecting() async{ - // 启动BLE后台服务 + void skipConnecting() async { + // 启动BLE后台服务 await _startAgentService(); _isNavigatingToHome = true; Get.offAllNamed(Routes.home);