|
|
@ -29,8 +29,7 @@ import java.util.Collections |
|
|
/** |
|
|
/** |
|
|
* ChatAPI服务异常 |
|
|
* ChatAPI服务异常 |
|
|
*/ |
|
|
*/ |
|
|
class ChatApiException(message: String) : Exception("ChatApiException: $message") |
|
|
class ChatApiException(message: String, val code: String? = null, cause: Throwable? = null) : Exception("ChatApiException: $message", cause) |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 流式回调接口 |
|
|
* 流式回调接口 |
|
|
* 与 iOS 版本 StreamCallback 协议保持完全一致 |
|
|
* 与 iOS 版本 StreamCallback 协议保持完全一致 |
|
|
@ -53,7 +52,7 @@ interface StreamCallback { |
|
|
/** |
|
|
/** |
|
|
* 出现错误 |
|
|
* 出现错误 |
|
|
*/ |
|
|
*/ |
|
|
fun onError(sessionid: String,error: Exception) |
|
|
fun onError(sessionid: String,code:Int,message:String) |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 函数调用 - 兼容JSONObject格式 |
|
|
* 函数调用 - 兼容JSONObject格式 |
|
|
@ -354,13 +353,20 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
if (!isInitialized || apiKey.isEmpty() || openAI == null) { |
|
|
if (!isInitialized || apiKey.isEmpty() || openAI == null) { |
|
|
Log.e("ChatApiService", "ChatAPI服务未初始化,无法发送消息") |
|
|
Log.e("ChatApiService", "ChatAPI服务未初始化,无法发送消息") |
|
|
try { |
|
|
try { |
|
|
getSessionCallback(sessionid)?.onError(sessionid,ChatApiException("ChatAPI服务未初始化")) |
|
|
getSessionCallback(sessionid)?.onError(sessionid,1002,"ChatAPI服务未初始化") |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
Log.e(TAG, "onError回调异常: ${e.message}", e) |
|
|
Log.e(TAG, "onError回调异常: ${e.message}", e) |
|
|
} |
|
|
} |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 在开始流式请求前检查网络状态 |
|
|
|
|
|
if (!isNetworkAvailable()) { |
|
|
|
|
|
Log.w("ChatApiService", "[Session: $sessionid] 网络不可用,直接返回网络错误") |
|
|
|
|
|
getSessionCallback(sessionid)?.onError(sessionid,1000,"网络不可用") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 重置状态 |
|
|
// 重置状态 |
|
|
currentMessages = messages |
|
|
currentMessages = messages |
|
|
toolCalls.clear() |
|
|
toolCalls.clear() |
|
|
@ -418,7 +424,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e) |
|
|
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e) |
|
|
if (sessionid == currSessionId) { |
|
|
if (sessionid == currSessionId) { |
|
|
try { |
|
|
try { |
|
|
getSessionCallback(sessionid)?.onError(sessionid,ChatApiException("流式请求失败: ${e.message}")) |
|
|
getSessionCallback(sessionid)?.onError(sessionid,1000,"流式请求失败: ${e.message}") |
|
|
} catch (ex: Exception) { |
|
|
} catch (ex: Exception) { |
|
|
Log.e(TAG, "onError回调异常: ${ex.message}", ex) |
|
|
Log.e(TAG, "onError回调异常: ${ex.message}", ex) |
|
|
} |
|
|
} |
|
|
@ -477,14 +483,6 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
//else{ |
|
|
|
|
|
// delta.content?.let { content -> |
|
|
|
|
|
// Log.d("ChatApiService", "liwei------------ [Session: $sessionid] 中间过程不输出 $content") |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-4") |
|
|
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-4") |
|
|
// 收集工具调用信息 |
|
|
// 收集工具调用信息 |
|
|
delta.toolCalls?.forEach { toolCall -> |
|
|
delta.toolCalls?.forEach { toolCall -> |
|
|
@ -559,7 +557,8 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
if (sessionid == currSessionId) { |
|
|
if (sessionid == currSessionId) { |
|
|
try { |
|
|
try { |
|
|
getSessionCallback(sessionid)?.onError(sessionid,ChatApiException("流式请求失败: ${e.message}")) |
|
|
Log.d(TAG, "AI聊天异常 Session $sessionid 错误类型:${e::class.simpleName} 错误完整类型:${e::class.qualifiedName}") |
|
|
|
|
|
getSessionCallback(sessionid)?.onError(sessionid,1000,"流式请求失败: ${e.message}") |
|
|
} catch (ex: Exception) { |
|
|
} catch (ex: Exception) { |
|
|
Log.e(TAG, "onError回调异常: ${ex.message}", ex) |
|
|
Log.e(TAG, "onError回调异常: ${ex.message}", ex) |
|
|
} |
|
|
} |
|
|
@ -632,13 +631,13 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
} |
|
|
} |
|
|
// 调用MCP工具 |
|
|
// 调用MCP工具 |
|
|
val toolResult = try { |
|
|
val toolResult = try { |
|
|
withTimeout(60000) { // 60秒超时 |
|
|
withTimeout(10000) { // 100秒超时 |
|
|
// 再次检查会话状态 |
|
|
// 再次检查会话状态 |
|
|
if (sessionid != currSessionId) { |
|
|
if (sessionid != currSessionId) { |
|
|
throw CancellationException("Session cancelled") |
|
|
throw CancellationException("Session cancelled") |
|
|
} |
|
|
} |
|
|
_mcpClient?.callTool(functionName, arguments) |
|
|
_mcpClient?.callTool(functionName, arguments) |
|
|
} |
|
|
} |
|
|
} catch (e: TimeoutCancellationException) { |
|
|
} catch (e: TimeoutCancellationException) { |
|
|
Log.w("ChatApiService", "[Session: $sessionid] MCP工具调用超时: $functionName") |
|
|
Log.w("ChatApiService", "[Session: $sessionid] MCP工具调用超时: $functionName") |
|
|
mapOf( |
|
|
mapOf( |
|
|
@ -708,7 +707,8 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
convertMapToJsonObject(result) |
|
|
convertMapToJsonObject(result) |
|
|
) |
|
|
) |
|
|
}else{ |
|
|
}else{ |
|
|
getSessionCallback(sessionid)?.onError(sessionid, ChatApiException(errorText)) |
|
|
Log.d(TAG, "[Session: $sessionid] 工具调用失败 $errorText") |
|
|
|
|
|
//getSessionCallback(sessionid)?.onError(sessionid, ChatApiException(errorText)) |
|
|
} |
|
|
} |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
Log.e(TAG, "onFunctionCallResult回调异常: ${e.message}", e) |
|
|
Log.e(TAG, "onFunctionCallResult回调异常: ${e.message}", e) |
|
|
@ -798,6 +798,19 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor |
|
|
sendMessageStream(sessionid,fullMessages) |
|
|
sendMessageStream(sessionid,fullMessages) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 添加网络检查方法 |
|
|
|
|
|
private fun isNetworkAvailable(): Boolean { |
|
|
|
|
|
return try { |
|
|
|
|
|
val connectivityManager = context?.getSystemService(android.content.Context.CONNECTIVITY_SERVICE) as? android.net.ConnectivityManager |
|
|
|
|
|
val activeNetwork = connectivityManager?.activeNetworkInfo |
|
|
|
|
|
activeNetwork?.isConnectedOrConnecting == true |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.w("ChatApiService", "检查网络状态失败: ${e.message}") |
|
|
|
|
|
true // 如果检查失败,假设网络可用,让后续的网络请求来处理 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 取消当前流式请求 |
|
|
* 取消当前流式请求 |
|
|
*/ |
|
|
*/ |
|
|
|