From 32633e38b4f0cb2697bc0321493019e93ff2bacc Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 6 Aug 2025 18:36:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AEai=E8=AF=AD=E8=A8=80=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_service/AgentService.kt | 8 ++++--- .../agent_service/AgentServiceImpl.swift | 19 ++++++++++++--- .../Sources/chat_api/ChatApiService.swift | 24 +++++++++---------- .../chat_api/Sources/chat_api/MCPClient.swift | 14 +++++------ .../open_ai_service/MCPClient.kt | 2 +- .../ios/Classes/MCPClient.swift | 2 +- .../Sources/open_ai_service/MCPClient.swift | 2 +- 7 files changed, 43 insertions(+), 28 deletions(-) diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index a1921b405..56c8384a4 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -81,6 +81,7 @@ object AgentService : CoroutineScope { // 服务端连接 private var serverurl = "" private var usertoken = "" + private var language = "" // 历史聊天消息缓存 private val historyMessages = JSONArray() @@ -197,8 +198,8 @@ object AgentService : CoroutineScope { //获取语音识别的语种 val supportedLanguagesConfig = config["supportedLanguages"] as? List val supportedLanguages = supportedLanguagesConfig?.toTypedArray() - ?: arrayOf("zh-CN", "en-US") // 默认支持多语言 - + ?: arrayOf("zh-CN") // 默认支持多语言 + language = supportedLanguages[0] val asrInitSuccess = initialize( subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", region = config["azureSpeechRegion"]?.toString() ?: "", @@ -825,7 +826,8 @@ object AgentService : CoroutineScope { // 先添加系统提示词 if (systemPrompt.isNotEmpty()) { - val mapdata = UserPortrait.getAll() + val mapdata = UserPortrait.getAll().toMutableMap() + mapdata["language"] = language // Log.d(TAG, "liwei--------- 用户画像: ${mapdata}") val _systemPrompt = Utils.fillTemplate(systemPrompt, mapdata) messagesWithSystemPrompt.put(createSystemMessage(_systemPrompt)) 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 b884a0c71..bc90164b2 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 @@ -59,6 +59,7 @@ class AgentServiceImpl: NSObject { internal var isAiStreaming: Bool = false var currsessionId: String = "" + var language: String = "" private var idleTimer: DispatchSourceTimer? private let maxIdleSeconds: TimeInterval = 10 @@ -158,6 +159,9 @@ class AgentServiceImpl: NSObject { if let openaivisionModel = config["openaivisionModel"] as? String { self.visionModel = openaivisionModel } + if let supportedLanguages = config["supportedLanguages"] as? [String] { + self.language = supportedLanguages[0] + } if let customSystemPrompt = config["systemPrompt"] as? String, !customSystemPrompt.isEmpty { self.systemPrompt = customSystemPrompt } else { @@ -208,7 +212,7 @@ class AgentServiceImpl: NSObject { azureTtsHelper?.addListener(self) // 获取支持的语言列表,支持从配置中读取 - let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN", "en-US"] + let supportedLanguages = config["supportedLanguages"] as? [String] ?? ["zh-CN"] guard let asrSuccess = azureAsrHelper?.initialize( subscriptionKey: azureSpeechKey, @@ -476,9 +480,16 @@ audioStream.saveAudioDataTo(data: audioData) var messages: [[String: Any]] = [] if !systemPrompt.isEmpty { - - let info: [String: String] = UserPortrait.shared.getAll() + var info = UserPortrait.shared.getAll() + info["language"] = self.language + + // 添加调试日志 + print("系统提示词模板: \(systemPrompt)") + print("用户画像数据: \(info)") + let _systemPrompt = fillTemplate(systemPrompt, with: info) + print("替换后的系统提示词: \(_systemPrompt)") + messages.append(chatApiService.createSystemMessage(content: _systemPrompt)) os_log("添加系统提示消息", log: logger, type: .info) } @@ -897,6 +908,8 @@ private func jsonToString(_ json: [String: Any]) -> String? { os_log("语言列表为空,使用默认语言", log: logger, type: .info) return false } + + self.language = languages[0] // 如果正在识别,需要先停止 let wasRecognizing = isRecognizing diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift index 689117876..267f4ffff 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift +++ b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/ChatApiService.swift @@ -68,18 +68,18 @@ public class ChatApiService: NSObject { /// 设置流式回调 public func setStreamCallback(_ sessionId: String, _ callback: StreamCallback) { - print("[ChatApiService] 设置回调 for sessionId: \(sessionId)") - print("[ChatApiService] 回调对象: \(callback)") + // print("[ChatApiService] 设置回调 for sessionId: \(sessionId)") + // print("[ChatApiService] 回调对象: \(callback)") self.sessionCallbacks[sessionId] = callback print("[ChatApiService] 回调设置完成,当前回调数量: \(sessionCallbacks.count)") } public func getSessionCallback(_ sessionId: String) -> StreamCallback? { let callback = self.sessionCallbacks[sessionId] - print("[ChatApiService] getSessionCallback for sessionId: \(sessionId)") - print("[ChatApiService] sessionCallbacks count: \(sessionCallbacks.count)") - print("[ChatApiService] sessionCallbacks keys: \(Array(sessionCallbacks.keys))") - print("[ChatApiService] callback exists: \(callback != nil)") + // print("[ChatApiService] getSessionCallback for sessionId: \(sessionId)") + // print("[ChatApiService] sessionCallbacks count: \(sessionCallbacks.count)") + // print("[ChatApiService] sessionCallbacks keys: \(Array(sessionCallbacks.keys))") + // print("[ChatApiService] callback exists: \(callback != nil)") return callback } //移除回调函数 @@ -311,7 +311,7 @@ public func sendMessageStream(_ sessionId: String, messages: [[String: Any]]) { tools: chatTools ) - print("[ChatApiService] 发起AI问答 问题:\(messages[0]) 工具数量: \(chatTools.count)") + // print("[ChatApiService] 发起AI问答 问题:\(messages[0]) 工具数量: \(chatTools.count)") } else { query = ChatQuery( messages: chatMessages, @@ -335,14 +335,14 @@ public func sendMessageStream(_ sessionId: String, messages: [[String: Any]]) { // 处理文本内容 if let content = delta.content { replyContext += content - print("[ChatApiService] 收到内容: \(content)") - print("[ChatApiService] 当前sessionId: \(sessionId)") + // print("[ChatApiService] 收到内容: \(content)") + // print("[ChatApiService] 当前sessionId: \(sessionId)") let callback = self.getSessionCallback(sessionId) if let callback = callback { - print("[ChatApiService] 找到回调,准备调用 onToken") + // print("[ChatApiService] 找到回调,准备调用 onToken") callback.onToken(sessionId, content) - print("[ChatApiService] onToken 调用完成") + // print("[ChatApiService] onToken 调用完成") } else { print("[ChatApiService] 警告:未找到 sessionId \(sessionId) 的回调") } @@ -932,7 +932,7 @@ private func convertMapToJsonObject(_ map: [String: Any]) -> [String: Any] { // 创建工具参数 let toolParam = ChatQuery.ChatCompletionToolParam(function: functionDefinition) - print("解析工具 \(name):\(schema)") + // print("解析工具 \(name):\(schema)") chatTools.append(toolParam) } diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift index b65a7aaa4..7e497bfbb 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift +++ b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/MCPClient.swift @@ -479,20 +479,20 @@ public class MCPClient { // 添加连接成功后的工具列表汇总输出 if connectedCount > 0 { print("[MCPClient] 🎉 MCP 连接完成,共连接 \(connectedCount) 个服务器") - print("[MCPClient] 📋 所有可用工具汇总:") + // print("[MCPClient] 📋 所有可用工具汇总:") let allTools = getToolMaps() for (index, toolMap) in allTools.enumerated() { if let function = toolMap["function"] as? [String: Any], let name = function["name"] as? String, let description = function["description"] as? String { - print("[MCPClient] \(index + 1). \(name) - \(description)") + // print("[MCPClient] \(index + 1). \(name) - \(description)") } } - print("[MCPClient] 📊 工具总数: \(allTools.count)") - print("[MCPClient] 🔧 本地函数: \(localFunctionDefs.count) 个") - print("[MCPClient] 🌐 远程工具: \(allTools.count - localFunctionDefs.count) 个") + // print("[MCPClient] 📊 工具总数: \(allTools.count)") + // print("[MCPClient] 🔧 本地函数: \(localFunctionDefs.count) 个") + // print("[MCPClient] 🌐 远程工具: \(allTools.count - localFunctionDefs.count) 个") } return connectedCount > 0 @@ -508,7 +508,7 @@ public class MCPClient { parameters: Any, handler: FunctionHandler ) -> Bool { - print("[MCPClient] 注册本地函数: \(name)") + // print("[MCPClient] 注册本地函数: \(name)") localFunctions[name] = handler let functionDef: [String: Any] = [ @@ -544,7 +544,7 @@ public class MCPClient { for client in subClients.values { let tools = client.getToolMaps() - print("[MCPClient] 读取工具列表: \(tools)") + // print("[MCPClient] 读取工具列表: \(tools)") allToolMaps.append(contentsOf: tools) } diff --git a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt index b15378c46..370e16a5b 100644 --- a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt +++ b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/MCPClient.kt @@ -768,7 +768,7 @@ class MCPClient(private val context: Context? = null) : AutoCloseable { // 保存函数定义 localFunctionDefs[name] = ObjectMapper().writeValueAsString(functionDef) - Log.d(TAG, "注册本地函数成功: $name") + // Log.d(TAG, "注册本地函数成功: $name") return true } catch (e: Exception) { Log.e(TAG, "注册本地函数失败: ${e.message}", e) diff --git a/local_plugins/open_ai_service/ios/Classes/MCPClient.swift b/local_plugins/open_ai_service/ios/Classes/MCPClient.swift index c865ff079..121653f02 100644 --- a/local_plugins/open_ai_service/ios/Classes/MCPClient.swift +++ b/local_plugins/open_ai_service/ios/Classes/MCPClient.swift @@ -312,7 +312,7 @@ public class MCPClient { let jsonString = String(data: jsonData, encoding: .utf8) ?? "" localFunctionDefs[name] = jsonString - print("\(tag) 成功注册本地函数: \(name)") + // print("\(tag) 成功注册本地函数: \(name)") return true } catch { print("\(tag) 注册本地函数失败: \(error.localizedDescription)") diff --git a/local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift b/local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift index c865ff079..121653f02 100644 --- a/local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift +++ b/local_plugins/open_ai_service/ios/open_ai_service/Sources/open_ai_service/MCPClient.swift @@ -312,7 +312,7 @@ public class MCPClient { let jsonString = String(data: jsonData, encoding: .utf8) ?? "" localFunctionDefs[name] = jsonString - print("\(tag) 成功注册本地函数: \(name)") + // print("\(tag) 成功注册本地函数: \(name)") return true } catch { print("\(tag) 注册本地函数失败: \(error.localizedDescription)")