Browse Source

补充上层退出界面时关闭AI执行流

weicu
liwei1dao 1 year ago
parent
commit
c3cf6b9a9b
  1. 1
      lib/modules/agent/controllers/agent_controller.dart
  2. 9
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  3. 10
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt
  4. 10
      local_plugins/agent_service/lib/agent_service.dart

1
lib/modules/agent/controllers/agent_controller.dart

@ -152,6 +152,7 @@ class AgentController extends GetxController {
@override
void onClose() {
AgentService.stopAiSteam(); //停止AI流
AgentService.stopConversation(); // 停止当前会话
AgentService.stopTts(); // 停止当前语音
textController.dispose();

9
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -571,7 +571,7 @@ object AgentService : CoroutineScope {
/**
* 停止AI流输出
*/
private fun stopAiStream() {
fun stopAiStream() {
if (isAiStreaming) {
try {
// 先更新状态,避免回调时的状态不一致
@ -580,7 +580,8 @@ object AgentService : CoroutineScope {
// 取消当前AI生成任务
currentAiJob?.cancel()
currentAiJob = null
currsessionId = "" //取消会话
// 通知ChatAPI服务终止当前流式请求
chatApiService.cancelCurrentStream()
@ -756,9 +757,6 @@ object AgentService : CoroutineScope {
responseBuilder.append(token)
if (speakResponse && broadcast) {
ttsService?.speakStream(token)
if (token.length > 0) {
audioPlayer?.stopAudio()
}
}
if (broadcast) {
// 发送流式回复token
@ -774,6 +772,7 @@ object AgentService : CoroutineScope {
// 视情况决定是否朗读回复
if (speakResponse && broadcast && sessionid == currsessionId){
ttsService?.flushStream()
audioPlayer?.stopAudio()
}
val response = responseBuilder.toString()
// 发送完整回复,包含是否有图片的标记

10
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt

@ -177,6 +177,16 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream
result.error("STOP_TTS_ERROR", "停止语音合成失败: ${e.message}", null)
}
}
"stopAiSteam" -> {
try {
AgentService.stopAiStream()
result.success(true)
} catch (e: Exception) {
Log.e(TAG, "停止语音合成失败", e)
result.error("STOP_TTS_ERROR", "停止语音合成失败: ${e.message}", null)
}
}
"clearChatHistory" -> {
try {
AgentService.clearChatHistory { success ->

10
local_plugins/agent_service/lib/agent_service.dart

@ -293,6 +293,16 @@ class AgentService {
}
}
/// 停止语音合成
static Future<bool> stopAiSteam() async {
try {
final bool result = await _channel.invokeMethod('stopAiSteam');
return result;
} on PlatformException catch (e) {
throw AgentServiceException(e.code, e.message ?? '停止语音合成失败', e.details);
}
}
/// 停止语音合成
static Future<bool> stopTts() async {
try {

Loading…
Cancel
Save