Browse Source

Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev

weicu
fdp 1 year ago
parent
commit
cb02ae0860
  1. 78
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/CustomSseClientTransport.kt
  2. 1
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

78
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/CustomSseClientTransport.kt

@ -86,7 +86,7 @@ class CustomSseClientTransport(
// 路径部分 // 路径部分
val path = urlObj.path val path = urlObj.path
Log.e(TAG, "$serviceidString 解析成功: hostUrl:$hostUrl,path:$path,params:$params")
Triple(hostUrl, path, params) Triple(hostUrl, path, params)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "$serviceidString 解析URL失败: $url, ${e.message}") Log.e(TAG, "$serviceidString 解析URL失败: $url, ${e.message}")
@ -101,9 +101,10 @@ class CustomSseClientTransport(
job = scope.launch(CoroutineName("CustomSseMcpClientTransport.collect#${hashCode()}")) { job = scope.launch(CoroutineName("CustomSseMcpClientTransport.collect#${hashCode()}")) {
try { try {
session.incoming.collect { event -> session.incoming.collect { event ->
Log.e(TAG, "$serviceidString event:${event.event}-${event.data}")
when (event.event) { when (event.event) {
"error" -> { "error" -> {
Log.e(TAG, "$serviceidString SSE错误: ${event.data}") // Log.e(TAG, "$serviceidString SSE错误: ${event.data}")
isConnected.set(false) isConnected.set(false)
val exception = Exception("$serviceidString SSE Error: ${event.data}") val exception = Exception("$serviceidString SSE Error: ${event.data}")
_onError(exception) _onError(exception)
@ -113,7 +114,7 @@ class CustomSseClientTransport(
"open" -> { "open" -> {
// SSE连接已打开 // SSE连接已打开
Log.d(TAG, "$serviceidString SSE连接已打开") // Log.d(TAG, "$serviceidString SSE连接已打开")
isConnected.set(true) isConnected.set(true)
} }
"ping" -> { "ping" -> {
@ -122,29 +123,73 @@ class CustomSseClientTransport(
"endpoint" -> { "endpoint" -> {
try { try {
val eventData = event.data ?: "" val eventData = event.data ?: ""
// 构建完整的端点URL // 构建完整的端点URL
val fullEndpoint = if (eventData.contains(hostPart)) { val fullEndpoint = when {
eventData // 如果eventData已经包含完整的URL(包含协议)
} else if (eventData.startsWith("/")) { eventData.startsWith("http://") || eventData.startsWith("https://") -> {
"$hostPart$eventData" eventData
} else { }
eventData // 如果eventData包含hostPart但没有协议
eventData.contains(hostPart.substringAfter("://")) -> {
if (eventData.startsWith("://")) {
"${hostPart.substringBefore("://")}$eventData"
} else {
"https://$eventData"
}
}
// 如果eventData以/开头,说明是路径
eventData.startsWith("/") -> {
"$hostPart$eventData"
}
// 如果eventData不为空但不包含完整信息,尝试构建完整URL
eventData.isNotEmpty() -> {
if (hostPart.isNotEmpty()) {
// 如果eventData看起来像是路径片段,添加/前缀
val path = if (!eventData.startsWith("/")) "/" + eventData else eventData
"$hostPart$path"
} else {
// 如果没有hostPart,尝试添加默认协议
if (!eventData.contains("://")) {
"https://$eventData"
} else {
eventData
}
}
}
// 如果eventData为空,使用hostPart
else -> hostPart
}
// 验证URL格式
val validatedEndpoint = try {
val url = java.net.URL(fullEndpoint)
fullEndpoint
} catch (e: Exception) {
Log.w(TAG, "$serviceidString 端点URL格式可能有问题: $fullEndpoint, 尝试修复")
// 尝试修复URL
when {
!fullEndpoint.startsWith("http://") && !fullEndpoint.startsWith("https://") -> {
"https://$fullEndpoint"
}
else -> fullEndpoint
}
} }
// 添加查询参数 // 添加查询参数
val endpointWithParams = if (queryParams.isNotEmpty()) { val endpointWithParams = if (queryParams.isNotEmpty()) {
if (fullEndpoint.contains("?")) { if (validatedEndpoint.contains("?")) {
val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" } val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" }
"$fullEndpoint&$queryString" "$validatedEndpoint&$queryString"
} else { } else {
val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" } val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" }
"$fullEndpoint?$queryString" "$validatedEndpoint?$queryString"
} }
} else { } else {
fullEndpoint validatedEndpoint
} }
Log.d(TAG, "$serviceidString 处理endpoint事件: $endpointWithParams")
endpoint.complete(endpointWithParams) endpoint.complete(endpointWithParams)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "$serviceidString 处理endpoint事件失败: ${e.message}", e) Log.e(TAG, "$serviceidString 处理endpoint事件失败: ${e.message}", e)
@ -174,7 +219,7 @@ class CustomSseClientTransport(
} }
} }
} catch (e: CancellationException) { } catch (e: CancellationException) {
Log.d(TAG, "$serviceidString SSE事件收集被取消") Log.e(TAG, "$serviceidString SSE事件收集被取消 : ${e.message}", e)
throw e throw e
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "$serviceidString SSE连接异常断开: ${e.message}", e) Log.e(TAG, "$serviceidString SSE连接异常断开: ${e.message}", e)
@ -219,11 +264,12 @@ class CustomSseClientTransport(
* 启动传输层 * 启动传输层
*/ */
override suspend fun start() { override suspend fun start() {
if (!initialized.compareAndSet(false, true)) { if (!initialized.compareAndSet(false, true)) {
Log.e(TAG, "$serviceidString 传输层已经启动,不能重复启动") Log.e(TAG, "$serviceidString 传输层已经启动,不能重复启动")
error("CustomSseClientTransport already started!") error("CustomSseClientTransport already started!")
} }
Log.d(TAG, "CustomSseClientTransport $serviceidString 启动")
// 解析URL // 解析URL
if (urlString != null) { if (urlString != null) {
val urlInfo = parseUrl(urlString) val urlInfo = parseUrl(urlString)

1
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

@ -1352,6 +1352,7 @@ private class UpdateUserPortraitHandler(private val context: Context?) : Functio
context.sendBroadcast(intent) context.sendBroadcast(intent)
// 发送广播通知退出交互 // 发送广播通知退出交互
return JSONObject().apply { return JSONObject().apply {
put("nocard", true)
put("result", "更新成功!") put("result", "更新成功!")
}.toString() }.toString()
} }

Loading…
Cancel
Save