|
|
@ -474,7 +474,7 @@ audioStream.saveAudioDataTo(data: audioData) |
|
|
|
|
|
|
|
|
if !systemPrompt.isEmpty { |
|
|
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) |
|
|
let _systemPrompt = fillTemplate(systemPrompt, with: info) |
|
|
messages.append(chatApiService.createSystemMessage(content: _systemPrompt)) |
|
|
messages.append(chatApiService.createSystemMessage(content: _systemPrompt)) |
|
|
os_log("添加系统提示消息", log: logger, type: .info) |
|
|
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 |
|
|
var result = template |
|
|
for (key, value) in values { |
|
|
let matches = regex.matches(in: template, options: [], range: range).reversed() |
|
|
result = result.replacingOccurrences(of: "{\(key)}", with: value) |
|
|
|
|
|
|
|
|
// 从后往前替换,避免位置偏移问题 |
|
|
|
|
|
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 |
|
|
return result |
|
|
} |
|
|
} |
|
|
func clearChatHistory() -> Bool { |
|
|
func clearChatHistory() -> Bool { |
|
|
|