From bc2438d9212999ffefde9b497142b8488eb325d1 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Fri, 25 Jul 2025 21:14:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E8=AE=BE=E5=A4=87=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=BB=A3=E7=A0=81=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/devices_controller.dart | 3 +- .../agent_service/AgentServiceImpl.swift | 31 ++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) 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 {