import 'package:device_plugin_interface/device_plugin_interface.dart'; import 'package:get/get.dart'; import '../../devices/device_hub.dart'; import '../../devices/device_vendors.dart'; import '../models/agent_module.dart'; import '../models/device_category.dart'; import 'agent_module_service.dart'; /// 设备品类**首次连接成功**时,把该品类的默认功能铺到 AI 页。 /// /// 挂在 [DeviceHub.sessions] 上,谁上线就按厂商 → 品类映射铺一次; /// 「只铺一次」由 [AgentModuleService.applyDeviceDefaults] 记录。 class AgentDefaults { AgentDefaults._(); static const Map _vendorCategory = { DeviceVendors.bes: DeviceCategories.earphone, DeviceVendors.holder: DeviceCategories.phoneHolder, DeviceVendors.smartcar: DeviceCategories.carAroma, }; /// 厂商 → 品类。设备按键唤醒时用它找该接哪个 agent(见 /// `DeviceAiSessionService._resolveAgentAppId`)。 static String? categoryForVendor(String vendor) => _vendorCategory[vendor]; /// 这个品类默认该由哪个 agent 接管对话 —— 取默认编排里的第一个模块。 /// 找不到返回 null,调用方回退到全局默认(EMAI)。 static String? primaryModuleForVendor(String vendor) { final cat = _vendorCategory[vendor]; if (cat == null) return null; final list = AgentModules.defaultsForCategory(cat); return list.isEmpty ? null : list.first; } static Worker? _worker; static void bind(DeviceHub hub) { _worker?.dispose(); _worker = ever>(hub.sessions, (list) { for (final s in list) { final cat = _vendorCategory[s.vendor]; if (cat != null) applyAgentDefaultsFor(cat); } }); } static void applyAgentDefaultsFor(String categoryId) { try { final service = Get.isRegistered() ? AgentModuleService.to : Get.put(AgentModuleService(), permanent: true); service.applyDeviceDefaults(categoryId); } catch (_) { // 铺默认功能失败不能影响连接本身 } } }