Browse Source

上传bug相关优化

newdev_shunjiawei
liwei1dao 12 months ago
parent
commit
fce5010450
  1. 26
      lib/modules/agent/controllers/agent_controller.dart
  2. 54
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/CustomSseClientTransport.kt
  3. 65
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt
  4. 39
      local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift

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

@ -613,10 +613,20 @@ class AgentController extends GetxController with WidgetsBindingObserver {
final sessionid = event.data['sessionid'] ?? ''; final sessionid = event.data['sessionid'] ?? '';
final text = event.data['text'] ?? ''; final text = event.data['text'] ?? '';
Logger.i(TAG, '[$sessionid]识别最终结果: $text'); Logger.i(TAG, '[$sessionid]识别最终结果: $text');
if (currsessionid.value != "" && currsessionid.value != sessionid) {
final index = messages.lastIndexWhere(
(msg) => msg.sessionid == currsessionid.value && msg.isUser);
//出现了为识别结束的消息
if (index >= 0) {
//异除去当前会话的识别中消息
messages.removeAt(index);
}
}
currsessionid.value = sessionid;
// 查找是否有正在识别中的消息 // 查找是否有正在识别中的消息
final index = messages.lastIndexWhere( final index = messages.lastIndexWhere(
(msg) => msg.sessionid == sessionid && msg.isUser); (msg) => msg.sessionid == sessionid && msg.isUser);
if (text.isNotEmpty) { if (text.isNotEmpty) {
if (index >= 0) { if (index >= 0) {
// 将识别中的消息转换为最终消息 // 将识别中的消息转换为最终消息
@ -655,7 +665,7 @@ class AgentController extends GetxController with WidgetsBindingObserver {
// 查找是否有正在识别中的消息 // 查找是否有正在识别中的消息
final index = messages.lastIndexWhere( final index = messages.lastIndexWhere(
(msg) => msg.sessionid == sessionid && msg.isUser); (msg) => msg.sessionid == sessionid && msg.isUser);
currsessionid.value = '';
if (currentText.value.isNotEmpty) { if (currentText.value.isNotEmpty) {
if (index >= 0) { if (index >= 0) {
// 将识别中的消息转换为最终消息 // 将识别中的消息转换为最终消息
@ -668,14 +678,14 @@ class AgentController extends GetxController with WidgetsBindingObserver {
functionCalls: messages[index].functionCalls, functionCalls: messages[index].functionCalls,
isToolCallsExpanded: messages[index].isToolCallsExpanded, isToolCallsExpanded: messages[index].isToolCallsExpanded,
); );
// messages.refresh();
} else { } else {
// 如果没有找到识别中的消息,添加新消息 // 如果没有找到识别中的消息,添加新消息
messages.add(Message( messages.add(Message(
sessionid: sessionid, sessionid: sessionid,
isUser: true, isUser: true,
text: currentText.value, text: currentText.value,
isRecognizing: false)); isRecognizing: false,
));
} }
isProcessing.value = true; isProcessing.value = true;
@ -685,8 +695,8 @@ class AgentController extends GetxController with WidgetsBindingObserver {
} else if (index >= 0) { } else if (index >= 0) {
// 如果识别结果为空,移除临时的识别消息 // 如果识别结果为空,移除临时的识别消息
messages.removeAt(index); messages.removeAt(index);
// messages.refresh();
} }
messages.refresh();
break; break;
case AgentServiceEventType.ttsStarted: case AgentServiceEventType.ttsStarted:
isSpeaking.value = true; isSpeaking.value = true;

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

@ -177,14 +177,39 @@ class CustomSseClientTransport(
} }
} }
// 添加查询参数 // 添加查询参数,避免重复参数
val endpointWithParams = if (queryParams.isNotEmpty()) { val endpointWithParams = if (queryParams.isNotEmpty()) {
// 解析现有的查询参数
val existingParams = mutableMapOf<String, String>()
if (validatedEndpoint.contains("?")) { if (validatedEndpoint.contains("?")) {
val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" } val urlParts = validatedEndpoint.split("?", limit = 2)
"$validatedEndpoint&$queryString" if (urlParts.size == 2) {
urlParts[1].split("&").forEach { param ->
val parts = param.split("=", limit = 2)
if (parts.size == 2) {
existingParams[parts[0]] = parts[1]
}
}
}
}
// 合并参数,避免重复
val allParams = mutableMapOf<String, String>()
allParams.putAll(existingParams)
allParams.putAll(queryParams)
// 构建最终URL
val baseUrl = if (validatedEndpoint.contains("?")) {
validatedEndpoint.split("?")[0]
} else { } else {
val queryString = queryParams.entries.joinToString("&") { "${it.key}=${it.value}" } validatedEndpoint
"$validatedEndpoint?$queryString" }
if (allParams.isNotEmpty()) {
val queryString = allParams.entries.joinToString("&") { "${it.key}=${it.value}" }
"$baseUrl?$queryString"
} else {
baseUrl
} }
} else { } else {
validatedEndpoint validatedEndpoint
@ -280,7 +305,7 @@ class CustomSseClientTransport(
} }
sessionScope = CoroutineScope(transportScope.coroutineContext + SupervisorJob()) sessionScope = CoroutineScope(transportScope.coroutineContext + SupervisorJob())
Log.e(TAG, "$serviceidString 解析成功: start 会话")
// 创建SSE会话 - 直接使用原始URL // 创建SSE会话 - 直接使用原始URL
session = urlString?.let { session = urlString?.let {
val sseConnectUrl = if (queryParams.isNotEmpty()) { val sseConnectUrl = if (queryParams.isNotEmpty()) {
@ -303,14 +328,14 @@ class CustomSseClientTransport(
reconnectionTime = reconnectionTime, reconnectionTime = reconnectionTime,
block = requestBuilder, block = requestBuilder,
) )
Log.e(TAG, "$serviceidString: start 会话 collectEvents")
// 收集SSE事件 // 收集SSE事件
collectEvents() collectEvents()
Log.e(TAG, "$serviceidString: start 会话 endpoint")
// 等待endpoint就绪 // 等待endpoint就绪
endpoint.await() endpoint.await()
Log.d(TAG, "CustomSseClientTransport启动完成") Log.d(TAG, "$serviceidString: CustomSseClientTransport启动完成")
} }
/** /**
@ -322,15 +347,18 @@ class CustomSseClientTransport(
Log.e(TAG, "发送失败: 未连接") Log.e(TAG, "发送失败: 未连接")
error("Not connected") error("Not connected")
} }
try { try {
val messageEndpoint = endpoint.getCompleted() val messageEndpoint = endpoint.getCompleted()
Log.d(TAG, "$serviceidString 准备发送消息到: $messageEndpoint")
val jsonString = json.encodeToString(message) val jsonString = json.encodeToString(message)
val response = client.post(messageEndpoint) { val response = withTimeout(15000) { // 减少超时时间到15秒
headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString()) client.post(messageEndpoint) {
setBody(jsonString) headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(jsonString)
}
} }
// 优化处理百度API的202状态码 // 优化处理百度API的202状态码

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

@ -60,6 +60,18 @@ class SystemFunctionHandler(private val context: Context? = null) {
// handler = TranslationModeHandler(context) // handler = TranslationModeHandler(context)
// ) // )
// 注册获取联系人列表函数
client.registerLocalFunction(
name = "get_contacts_list",
description = "获取用户通信录联系人列表",
parameters = mapOf(
"type" to "object",
"properties" to emptyMap<String, Any>(),
"required" to emptyList<String>()
),
handler = GetContactsListHandler(context)
)
// 注册发送短信函数 // 注册发送短信函数
client.registerLocalFunction( client.registerLocalFunction(
name = "send_text_message", name = "send_text_message",
@ -67,7 +79,7 @@ class SystemFunctionHandler(private val context: Context? = null) {
parameters = mapOf( parameters = mapOf(
"type" to "object", "type" to "object",
"properties" to mapOf( "properties" to mapOf(
"contact" to mapOf( "phone_number" to mapOf(
"type" to "string", "type" to "string",
"description" to "电话号码" "description" to "电话号码"
), ),
@ -76,23 +88,11 @@ class SystemFunctionHandler(private val context: Context? = null) {
"description" to "短信内容" "description" to "短信内容"
) )
), ),
"required" to listOf("contact", "message") "required" to listOf("phone_number", "message")
), ),
handler = SendTextMessageHandler(context) handler = SendTextMessageHandler(context)
) )
// 注册获取联系人列表函数
client.registerLocalFunction(
name = "get_contacts_list",
description = "获取用户通信录联系人列表",
parameters = mapOf(
"type" to "object",
"properties" to emptyMap<String, Any>(),
"required" to emptyList<String>()
),
handler = GetContactsListHandler(context)
)
// 注册拨打电话函数 // 注册拨打电话函数
client.registerLocalFunction( client.registerLocalFunction(
name = "make_phone_call", name = "make_phone_call",
@ -100,12 +100,12 @@ class SystemFunctionHandler(private val context: Context? = null) {
parameters = mapOf( parameters = mapOf(
"type" to "object", "type" to "object",
"properties" to mapOf( "properties" to mapOf(
"contact" to mapOf( "phone_number" to mapOf(
"type" to "string", "type" to "string",
"description" to "联系人姓名或电话号码" "description" to "联系人电话号码"
) )
), ),
"required" to listOf("contact") "required" to listOf("phone_number")
), ),
handler = MakePhoneCallHandler(context) handler = MakePhoneCallHandler(context)
) )
@ -495,10 +495,10 @@ private class TranslationModeHandler(private val context: Context?) : FunctionHa
*/ */
private class SendTextMessageHandler(private val context: Context?) : FunctionHandler { private class SendTextMessageHandler(private val context: Context?) : FunctionHandler {
override suspend fun handle(arguments: Map<String, Any>): String { override suspend fun handle(arguments: Map<String, Any>): String {
val contact = arguments["contact"] as? String val phone_number = arguments["phone_number"] as? String
val message = arguments["message"] as? String val message = arguments["message"] as? String
if (contact == null || message == null) { if (phone_number == null || message == null) {
return JSONObject().apply { return JSONObject().apply {
put("error", JSONObject().apply { put("error", JSONObject().apply {
put("code", "invalid_arguments") put("code", "invalid_arguments")
@ -529,12 +529,12 @@ private class SendTextMessageHandler(private val context: Context?) : FunctionHa
}.toString() }.toString()
} }
val phoneNumber = handler.getPhoneNumberForContact(contact) val phoneNumber = handler.getPhoneNumberForContact(phone_number)
if (phoneNumber.isEmpty()) { if (phoneNumber.isEmpty()) {
return JSONObject().apply { return JSONObject().apply {
put("error", JSONObject().apply { put("error", JSONObject().apply {
put("code", "contact_not_found") put("code", "contact_not_found")
put("message", "未找到联系人信息: $contact") put("message", "未找到联系人信息: $phone_number")
}) })
}.toString() }.toString()
} }
@ -556,7 +556,7 @@ private class SendTextMessageHandler(private val context: Context?) : FunctionHa
} }
JSONObject().apply { JSONObject().apply {
put("result", "已成功向${contact}发送短信") put("result", "已成功向${phone_number}发送短信")
put("phone", phoneNumber) put("phone", phoneNumber)
put("message", message) put("message", message)
}.toString() }.toString()
@ -637,13 +637,13 @@ private class MakePhoneCallHandler(private val context: Context?) : FunctionHand
} }
override suspend fun handle(arguments: Map<String, Any>): String { override suspend fun handle(arguments: Map<String, Any>): String {
val contact = arguments["contact"] as? String val phone_number = arguments["phone_number"] as? String
if (contact == null) { if (phone_number == null) {
return JSONObject().apply { return JSONObject().apply {
put("error", JSONObject().apply { put("error", JSONObject().apply {
put("code", "invalid_arguments") put("code", "invalid_arguments")
put("message", "缺少必要参数:contact") put("message", "缺少必要参数:phone_number")
}) })
}.toString() }.toString()
} }
@ -670,12 +670,12 @@ private class MakePhoneCallHandler(private val context: Context?) : FunctionHand
}.toString() }.toString()
} }
val phoneNumber = handler.getPhoneNumberForContact(contact) val phoneNumber = handler.getPhoneNumberForContact(phone_number)
if (phoneNumber.isEmpty()) { if (phoneNumber.isEmpty()) {
return JSONObject().apply { return JSONObject().apply {
put("error", JSONObject().apply { put("error", JSONObject().apply {
put("code", "contact_not_found") put("code", "contact_not_found")
put("message", "未找到联系人信息: $contact") put("message", "未找到联系人信息: $phone_number")
}) })
}.toString() }.toString()
} }
@ -688,7 +688,7 @@ private class MakePhoneCallHandler(private val context: Context?) : FunctionHand
context.startActivity(intent) context.startActivity(intent)
JSONObject().apply { JSONObject().apply {
put("result", "正在拨打${contact}的电话") put("result", "正在拨打${phone_number}的电话")
put("phone", phoneNumber) put("phone", phoneNumber)
}.toString() }.toString()
} catch (e: SecurityException) { } catch (e: SecurityException) {
@ -1106,10 +1106,11 @@ private class GetCurrentLocationHandler(private val context: Context?) : Functio
val result = JSONObject().apply { val result = JSONObject().apply {
put("result", JSONObject().apply { put("result", JSONObject().apply {
put("longitude", longitude) put("经度", longitude) // 经度
put("latitude", latitude) put("纬度", latitude) // 纬度
put("timestamp", System.currentTimeMillis()) put("经纬度", "$latitude,$longitude")
put("纬经度", "$longitude,$latitude")
put("时间戳", System.currentTimeMillis())
}) })
}.toString() }.toString()

39
local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift

@ -71,12 +71,13 @@ public class SystemFunctionHandler: NSObject {
// 注册翻译模式函数 // 注册翻译模式函数
// registerTranslationModeFunction(client: client) // registerTranslationModeFunction(client: client)
// 注册发送短信函数
registerSendTextMessageFunction(client: client)
// 注册查询通讯录函数 // 注册查询通讯录函数
registerGetContactsFunction(client: client) registerGetContactsFunction(client: client)
// 注册发送短信函数
registerSendTextMessageFunction(client: client)
// 注册拨打电话函数 // 注册拨打电话函数
registerMakePhoneCallFunction(client: client) registerMakePhoneCallFunction(client: client)
@ -163,16 +164,16 @@ public class SystemFunctionHandler: NSObject {
let parameters: [String: Any] = [ let parameters: [String: Any] = [
"type": "object", "type": "object",
"properties": [ "properties": [
"contact": [ "phone_number": [
"type": "string", "type": "string",
"description": "联系人姓名或电话号码" "description": "联系人电话号码"
], ],
"message": [ "message": [
"type": "string", "type": "string",
"description": "要发送的短信内容" "description": "要发送的短信内容"
] ]
], ],
"required": ["contact", "message"] "required": ["phone_number", "message"]
] ]
let handler = SendTextMessageHandler() let handler = SendTextMessageHandler()
@ -194,8 +195,8 @@ public class SystemFunctionHandler: NSObject {
let handler = GetContactsHandler() let handler = GetContactsHandler()
_ = client.registerLocalFunction( _ = client.registerLocalFunction(
name: "get_contacts", name: "get_contacts_list",
description: "获取联系人列表,读取通讯录信息,联系人名称和电话", description: "获取用户通信录联系人列表",
parameters: parameters, parameters: parameters,
handler: handler handler: handler
) )
@ -206,12 +207,12 @@ public class SystemFunctionHandler: NSObject {
let parameters: [String: Any] = [ let parameters: [String: Any] = [
"type": "object", "type": "object",
"properties": [ "properties": [
"contact": [ "phone_number": [
"type": "string", "type": "string",
"description": "联系人姓名或电话号码" "description": "联系人电话号码"
] ]
], ],
"required": ["contact"] "required": ["phone_number"]
] ]
let handler = MakePhoneCallHandler() let handler = MakePhoneCallHandler()
@ -628,22 +629,22 @@ private class SendTextMessageHandler: NSObject, FunctionHandler, MFMessageCompos
private var continuation: CheckedContinuation<Result, Never>? private var continuation: CheckedContinuation<Result, Never>?
func handle(arguments: [String: Any]) async throws -> String { func handle(arguments: [String: Any]) async throws -> String {
guard let contact = arguments["contact"] as? String, guard let phoneNumber = arguments["phone_number"] as? String,
let message = arguments["message"] as? String else { let message = arguments["message"] as? String else {
throw ChatApiException("缺少必要参数") throw ChatApiException("缺少必要参数")
} }
let result = await sendTextMessage(contact: contact, message: message) let result = await sendTextMessage(phoneNumber: phoneNumber, message: message)
return "{\"result\": \"\(result.message)\"}" return "{\"result\": \"\(result.message)\"}"
} }
private func sendTextMessage(contact: String, message: String) async -> Result { private func sendTextMessage(phoneNumber: String, message: String) async -> Result {
return await withCheckedContinuation { continuation in return await withCheckedContinuation { continuation in
self.continuation = continuation self.continuation = continuation
DispatchQueue.main.async { DispatchQueue.main.async {
if MFMessageComposeViewController.canSendText() { if MFMessageComposeViewController.canSendText() {
let messageVC = MFMessageComposeViewController() let messageVC = MFMessageComposeViewController()
messageVC.recipients = [contact] messageVC.recipients = [phoneNumber]
messageVC.body = message messageVC.body = message
messageVC.messageComposeDelegate = self // 设置 delegate messageVC.messageComposeDelegate = self // 设置 delegate
@ -693,16 +694,16 @@ private class SendTextMessageHandler: NSObject, FunctionHandler, MFMessageCompos
/// 拨打电话处理器 /// 拨打电话处理器
private class MakePhoneCallHandler: FunctionHandler { private class MakePhoneCallHandler: FunctionHandler {
func handle(arguments: [String: Any]) async throws -> String { func handle(arguments: [String: Any]) async throws -> String {
guard let contact = arguments["contact"] as? String else { guard let phoneNumber = arguments["phone_number"] as? String else {
throw ChatApiException("缺少联系人参数") throw ChatApiException("缺少联系人参数")
} }
let result = await makePhoneCall(contact: contact) let result = await makePhoneCall(phoneNumber: phoneNumber)
return "{\"result\": \"\(result.message)\"}" return "{\"result\": \"\(result.message)\"}"
} }
private func makePhoneCall(contact: String) async -> Result { private func makePhoneCall(phoneNumber: String) async -> Result {
guard let url = URL(string: "tel:\(contact)") else { guard let url = URL(string: "tel:\(phoneNumber)") else {
return Result(success: false, message: "无法创建电话URL") return Result(success: false, message: "无法创建电话URL")
} }
return await withCheckedContinuation { continuation in return await withCheckedContinuation { continuation in

Loading…
Cancel
Save