From 399958cea0f7f750de35f8753c6623801a2dbc37 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Tue, 19 Aug 2025 10:22:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0ai=E8=B6=85=E6=97=B6=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunqiinnovation/chat_api/ChatApiService.kt | 16 ++++++++++++++-- pubspec.yaml | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt index a0841af03..6d49ecbd5 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt @@ -404,12 +404,24 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor throw IllegalStateException("OpenAI客户端未初始化") } Log.e("ChatApiService", "liwei---------,发送AI请求 model:${currentModel} messages:${chatMessages}") - val flow = openAI!!.chatCompletions(chatCompletionRequest) + // 添加超时控制,10秒超时 + val flow = withTimeout(10.seconds) { + openAI!!.chatCompletions(chatCompletionRequest) + } Log.d(TAG, "liwei-------------------------开始AI 对话 6") flow } catch (e: Exception) { Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e) - throw e + if (sessionid == currSessionId) { + try { + getSessionCallback(sessionid)?.onError(sessionid,ChatApiException("流式请求失败: ${e.message}")) + } catch (ex: Exception) { + Log.e(TAG, "onError回调异常: ${ex.message}", ex) + } + clearCurrentSession() + } + return@launch +// throw e } var replyContext = ""; diff --git a/pubspec.yaml b/pubspec.yaml index 04aa9127f..7810473cb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -53,7 +53,7 @@ dependencies: flutter_local_notifications: ^18.0.1 device_info_plus: ^11.5.0 uuid: ^4.3.3 - record: ^5.2.1 + record: ^6.0.0 share_plus: ^11.0.0 image_picker: ^1.1.2 audio_waveforms: ^1.3.0