|
|
|
@ -514,10 +514,9 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
if (tools.length() > 0) { |
|
|
|
put("tools", tools) |
|
|
|
} |
|
|
|
Log.i(TAG, "请求Ai对话: 携带工具数据 ${tools.length()}") |
|
|
|
|
|
|
|
Log.i(TAG, "liwei------------------- 请求Ai对话: context:${messages.getJSONObject(messages.length() - 1).get("content").toString()} 携带工具数据 ${tools.length()}") |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val mediaType = "application/json".toMediaTypeOrNull() |
|
|
|
val request = Request.Builder() |
|
|
|
.url(baseUrl) |
|
|
|
@ -678,6 +677,8 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
if (function.has("arguments")) { |
|
|
|
toolCallInfo.arguments += function.getString("arguments") |
|
|
|
} |
|
|
|
Log.d(TAG, "liwei---------------------- AI发起工具调用: tool=$function") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -706,7 +707,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
put("arguments", firstToolCall.arguments) |
|
|
|
put("id", firstToolCall.id) |
|
|
|
} |
|
|
|
Log.d(TAG, "工具调用: id=${firstToolCall.id}, name=${firstToolCall.name} arguments=${firstToolCall.arguments}") |
|
|
|
Log.d(TAG, "liwei---------------------- 工具调用: id=${firstToolCall.id}, name=${firstToolCall.name} arguments=${firstToolCall.arguments}") |
|
|
|
|
|
|
|
// 通知上层工具调用事件 |
|
|
|
callback.onFunctionCall(functionCall) |
|
|
|
@ -721,7 +722,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
val args = mcpClient?.parseJsonArguments(firstToolCall.arguments) ?: mapOf() |
|
|
|
mcpClient?.callTool(firstToolCall.name, args) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "工具调用错误: ${e.message}") |
|
|
|
Log.e(TAG, "liwei----------------------- 工具调用错误: ${e.message}") |
|
|
|
JSONObject().put("context", "工具调用失败: ${e.message}") |
|
|
|
} |
|
|
|
|
|
|
|
@ -733,7 +734,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
val context = resultObj.optString("context", "工具调用失败").let { |
|
|
|
if (it.isNullOrEmpty()) "工具调用失败" else it |
|
|
|
} |
|
|
|
Log.d(TAG, "mcp工具调用完成: name=${firstToolCall.name}, context=$context") |
|
|
|
Log.d(TAG, "liwei----------------------- mcp工具调用完成: name=${firstToolCall.name}, context=$context") |
|
|
|
|
|
|
|
// 将结果发送回OpenAI继续对话 |
|
|
|
sendFunctionCallResult( |
|
|
|
@ -746,7 +747,7 @@ class OpenAIService(private val context: Context? = null) : CoroutineScope { |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
if (!isCanceled) { |
|
|
|
Log.e(TAG, "处理工具调用异常: ${e.message}") |
|
|
|
Log.e(TAG, "liwei----------------------- 处理工具调用异常: ${e.message}") |
|
|
|
try { |
|
|
|
val errorMessage = "工具调用处理失败: ${e.message}" |
|
|
|
sendFunctionCallResult( |
|
|
|
|