diff --git a/lib/modules/devices/controllers/devices_controller.dart b/lib/modules/devices/controllers/devices_controller.dart index 1a1fe15a8..ba32f0c98 100644 --- a/lib/modules/devices/controllers/devices_controller.dart +++ b/lib/modules/devices/controllers/devices_controller.dart @@ -28,9 +28,8 @@ class DevicesController extends GetxController { for (var device in User.instance.devices) { if (!devices.containsKey(device.id)) { devices[device.devicetype] = []; - } else { - devices[device.devicetype]!.add(device); } + devices[device.devicetype]!.add(device); } } diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index d5f07c938..ce456f1e3 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -474,7 +474,7 @@ audioStream.saveAudioDataTo(data: audioData) if !systemPrompt.isEmpty { - let info: [String: String] = self.locationInfo ?? ["user_location": "还未获取到位置信息", "user_lat": "未开启定位", "user_lng": "未开启定位"] + let info: [String: String] = UserPortrait.shared.getAll() let _systemPrompt = fillTemplate(systemPrompt, with: info) messages.append(chatApiService.createSystemMessage(content: _systemPrompt)) os_log("添加系统提示消息", log: logger, type: .info) @@ -826,11 +826,34 @@ private func processNavigationCard(_ cardNavigation: [String: Any]) { } } } - func fillTemplate(_ template: String, with values: [String: String]) -> String { + /** + * 填充模板字符串,使用正则表达式匹配 {key} 格式的占位符 + * 对应 Kotlin 版本的 fillTemplate 方法 + */ + func fillTemplate(_ template: String, with data: [String: String]) -> String { + // 使用正则表达式匹配 {key} 格式的占位符 + let regex = try! NSRegularExpression(pattern: "\\{([a-zA-Z0-9_]+)\\}", options: []) + let range = NSRange(location: 0, length: template.utf16.count) + var result = template - for (key, value) in values { - result = result.replacingOccurrences(of: "{\(key)}", with: value) + let matches = regex.matches(in: template, options: [], range: range).reversed() + + // 从后往前替换,避免位置偏移问题 + for match in matches { + if match.numberOfRanges >= 2 { + let keyRange = match.range(at: 1) + if let swiftKeyRange = Range(keyRange, in: template) { + let key = String(template[swiftKeyRange]) + let value = data[key] ?? "未知" + + let fullMatchRange = match.range(at: 0) + if let swiftFullRange = Range(fullMatchRange, in: result) { + result.replaceSubrange(swiftFullRange, with: value) + } + } + } } + return result } func clearChatHistory() -> Bool {