From 3eee298a37fd6443809d539f1c63df6ca02627ba Mon Sep 17 00:00:00 2001 From: wolfplus2048 <30993207+wolfplus2048@users.noreply.github.com> Date: Sun, 22 Jun 2025 14:03:52 +0100 Subject: [PATCH] before job --- .../agent/controllers/agent_controller.dart | 22 ++++++-- .../agent_service/AgentService.kt | 53 +++++++++++-------- .../agent_service/AgentServiceImpl.swift | 8 +++ .../agent_service/lib/agent_service.dart | 10 ++++ .../chat_api/SystemFunctionHandler.kt | 22 ++++---- 5 files changed, 76 insertions(+), 39 deletions(-) diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index 2bbaa3d78..cc98107c4 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -253,6 +253,7 @@ class AgentController extends GetxController { case AgentServiceEventType.recognizing: final text = event.data['text'] ?? ''; currentText.value = text; // 保留当前文本,以便其他地方使用 + Logger.i(TAG, '识别中间结果: $text'); if (text.isNotEmpty) { // 查找是否有正在识别中的消息 final index = messages @@ -280,7 +281,7 @@ class AgentController extends GetxController { case AgentServiceEventType.recognitionResult: final text = event.data['text'] ?? ''; - Logger.i(TAG, '识别结果: $text'); + Logger.i(TAG, '识别最终结果: $text'); // 查找是否有正在识别中的消息 final index = messages.lastIndexWhere((msg) => msg.isRecognizing && msg.isUser); @@ -313,16 +314,27 @@ class AgentController extends GetxController { break; case AgentServiceEventType.ttsStarted: - isSpeaking.value = true; - Logger.i(TAG, 'TTS开始播放'); + // isSpeaking.value = true; + // Logger.i(TAG, 'TTS开始播放'); break; case AgentServiceEventType.ttsCompleted: case AgentServiceEventType.ttsStopped: case AgentServiceEventType.ttsCanceled: + // isSpeaking.value = false; + + // Logger.i(TAG, 'TTS停止播放, $event.type'); + break; + + case AgentServiceEventType.playbackStarted: + isSpeaking.value = true; + Logger.i(TAG, 'TTS播放开始'); + break; + + case AgentServiceEventType.playbackCompleted: isSpeaking.value = false; - Logger.i(TAG, 'TTS停止播放, $event.type'); + Logger.i(TAG, 'TTS播放完成'); break; case AgentServiceEventType.imageProcessing: @@ -338,7 +350,7 @@ class AgentController extends GetxController { final token = event.data['token'] ?? ''; final responseId = event.data['responseId'] ?? ''; - + Logger.i(TAG, 'AI回复Token: $token, responseId: $responseId'); if (token.isNotEmpty) { // 如果是新的回复或者响应ID改变,创建新消息 if (_isNewAssistantResponse || 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 d9d2c9700..ea0a1b19a 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 @@ -266,6 +266,14 @@ object AgentService : CoroutineScope { restartIdleCheck() sendEvent("tts_canceled", mapOf("status" to "canceled")) } + TtsEventType.PLAYBACK_STARTED -> { + restartIdleCheck() + sendEvent("playback_started", mapOf("status" to "playback_started")) + } + TtsEventType.PLAYBACK_COMPLETED -> { + restartIdleCheck() + sendEvent("playback_completed", mapOf("status" to "playback_completed")) + } TtsEventType.ERROR -> { isTtsSpeaking = false restartIdleCheck() @@ -400,21 +408,21 @@ object AgentService : CoroutineScope { if (isTtsSpeaking || isAiStreaming) { val currentTime = System.currentTimeMillis() - // 防抖处理:避免过于频繁的打断 - if (currentTime - lastInterruptTime < INTERRUPT_DEBOUNCE_MS) { - return - } + // // 防抖处理:避免过于频繁的打断 + // if (currentTime - lastInterruptTime < INTERRUPT_DEBOUNCE_MS) { + // return + // } - // 简单过滤:太短的内容可能是噪音 - if (recognizing.trim().length < 2) { - return - } + // // 简单过滤:太短的内容可能是噪音 + // if (recognizing.trim().length < 2) { + // return + // } - // 过滤纯语气词 - val trimmedText = recognizing.trim().lowercase() - if (FILLER_WORDS.contains(trimmedText)) { - return - } + // // 过滤纯语气词 + // val trimmedText = recognizing.trim().lowercase() + // if (FILLER_WORDS.contains(trimmedText)) { + // return + // } // 执行打断 lastInterruptTime = currentTime @@ -520,18 +528,17 @@ object AgentService : CoroutineScope { val startTime = System.currentTimeMillis() // 并行执行停止操作,加快响应速度 - launch { stopTts() } - launch { stopAiStream() } + stopTts() + stopAiStream() // 记录打断耗时 - launch { - delay(100) // 短暂延迟后计算耗时 - val duration = System.currentTimeMillis() - startTime - sendEvent("response_interrupted", mapOf( - "status" to "interrupted", - "duration_ms" to duration - )) - } + + val duration = System.currentTimeMillis() - startTime + sendEvent("response_interrupted", mapOf( + "status" to "interrupted", + "duration_ms" to duration + )) + } } 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 e5035bf17..0dc9119f6 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 @@ -909,6 +909,14 @@ extension AgentServiceImpl: TtsEventListener { restartIdleCheck() sendEvent(name: "tts_completed", data: ["status": "completed"]) + case .playbackStarted: + restartIdleCheck() + sendEvent(name: "playback_started", data: ["status": "playback_started"]) + + case .playbackCompleted: + restartIdleCheck() + sendEvent(name: "playback_completed", data: ["status": "playback_completed"]) + case .synthesisCanceled: isSpeaking = false restartIdleCheck() diff --git a/local_plugins/agent_service/lib/agent_service.dart b/local_plugins/agent_service/lib/agent_service.dart index 4ee6907cb..fc1579a05 100644 --- a/local_plugins/agent_service/lib/agent_service.dart +++ b/local_plugins/agent_service/lib/agent_service.dart @@ -38,6 +38,12 @@ enum AgentServiceEventType { /// TTS停止 ttsStopped, + /// 播放开始 + playbackStarted, + + /// 播放完成 + playbackCompleted, + /// 响应中断 responseInterrupted, @@ -147,6 +153,10 @@ class AgentService { return AgentServiceEventType.ttsCanceled; case 'tts_stopped': return AgentServiceEventType.ttsStopped; + case 'playback_started': + return AgentServiceEventType.playbackStarted; + case 'playback_completed': + return AgentServiceEventType.playbackCompleted; case 'response_interrupted': return AgentServiceEventType.responseInterrupted; case 'assistant_token': diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt index 1c5b7de75..24f56af0d 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt @@ -47,17 +47,17 @@ class SystemFunctionHandler(private val context: Context? = null) { handler = ExitInteractionHandler(context) ) - // 注册翻译模式函数 - client.registerLocalFunction( - name = "enter_translation_mode", - description = "用户请求进入实时翻译模式时,启动实时翻译功能", - parameters = mapOf( - "type" to "object", - "properties" to emptyMap(), - "required" to emptyList() - ), - handler = TranslationModeHandler(context) - ) + // // 注册翻译模式函数 + // client.registerLocalFunction( + // name = "enter_translation_mode", + // description = "用户请求进入实时翻译模式时,启动实时翻译功能", + // parameters = mapOf( + // "type" to "object", + // "properties" to emptyMap(), + // "required" to emptyList() + // ), + // handler = TranslationModeHandler(context) + // ) // 注册发送短信函数 client.registerLocalFunction(