Browse Source

上传播报控制

weicu
liwei1dao 1 year ago
parent
commit
a2cfee2939
  1. 10
      lib/modules/agent/controllers/agent_controller.dart
  2. 25
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  3. 5
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPClient.kt
  4. 12
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt

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

@ -409,6 +409,16 @@ class AgentController extends GetxController {
Map<String, dynamic> metaResult = {};
metaResult = jsonDecode(metaStr) as Map<String, dynamic>;
if (metaResult.isNotEmpty) {
var notreply = false;
if (metaResult.containsKey("notreply")) {
// 如果有导航卡片,直接添加
notreply = metaResult["notreply"] as bool;
}
if (notreply) {
Logger.i(TAG, 'Flutter 结束调用 不回复');
//不回复直接返回
break;
}
// 判断是否为新的回复或响应ID是否改变
if (_isNewAssistantResponse || _currentResponseId != responseId) {
// 添加新消息

25
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -612,7 +612,7 @@ object AgentService : CoroutineScope {
val responseBuilder = StringBuilder()
var aiMetadata:String = ""
var userMetadata = JSONObject()
var broadcast = false //是否播发
var broadcast = true //是否播发
// 如果有图片,添加图片信息到元数据
if (hasImage) {
userMetadata.put("hasImage", true)
@ -739,13 +739,13 @@ object AgentService : CoroutineScope {
override fun onFunctionCallResult(functionCall: JSONObject, functionCallResult: JSONObject) {
audioPlayer?.stopAudio()
val name = functionCall.get("name") as String;
val (metestr, _broadcast)= autoHandleFcunCallResult(name,functionCallResult);
aiMetadata = metestr;
broadcast = _broadcast
val resultmap = autoHandleFcunCallResult(name,functionCallResult);
aiMetadata = resultmap["meta"] as String;
broadcast = resultmap["broadcast"] as Boolean;
sendEvent("function_call_result", mapOf(
"function_call" to functionCall.toString(),
"result" to functionCallResult.toString(),
"meta" to metestr,
"meta" to aiMetadata,
))
}
}
@ -1116,7 +1116,7 @@ object AgentService : CoroutineScope {
/*
* 自动播放音乐
* */
fun autoHandleFcunCallResult(toolname:String,functionCallResult: JSONObject): Pair<String, Boolean>{
fun autoHandleFcunCallResult(toolname:String,functionCallResult: JSONObject): Map<String,Any>{
println("协议工具返回数据 检查是否存在卡片! functionCallResult: ${functionCallResult}")
try {
val contentStr = functionCallResult.optString("context")
@ -1126,13 +1126,18 @@ object AgentService : CoroutineScope {
println("协议工具返回数据 检查是否存在卡片! textStr: ${textStr}")
val metadata: MutableMap<String, Any> = mutableMapOf()
var broadcast = true;
var notreply = false;
if (textStr.isNotEmpty()) {
val meta = JSONObject(textStr)
metadata[toolname] = meta // 现在可以赋值
val metaStr = JSONObject(metadata as Map<*, *>).toString()
if (meta.has("broadcast")){
broadcast = meta["broadcast"] as Boolean
}
if (meta.has("notreply")){
notreply = meta["notreply"] as Boolean
}
metadata[toolname] = meta // 现在可以赋值
metadata["notreply"] = notreply // 现在可以赋值
val metaStr = JSONObject(metadata as Map<*, *>).toString()
// Log.d(TAG, "mcp调用结果: $metaStr")
if (meta.has("card_music")) { //音乐卡片
val cardMusic = meta.getJSONObject("card_music")
@ -1190,13 +1195,13 @@ object AgentService : CoroutineScope {
val end = cardNavigation.optString("end", "")
processNavigation(start, end);
}
return Pair(metaStr, broadcast);
return mapOf("meta" to metaStr, "broadcast" to broadcast,"notreply" to notreply);
}
} catch (e: Exception) {
// 捕获其他类型的异常(可选)
println("为解析到卡片数据!")
}
return Pair("", true);
return mapOf("meta" to "", "broadcast" to true,"notreply" to false);
}
/**

5
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPClient.kt

@ -237,7 +237,10 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
localFunctions[name]?.let { handler ->
return try {
val result = handler.handle(arguments)
mapOf("context" to result)
mapOf("context" to mapOf(
"type" to "text",
"text" to result,
))
} catch (e: Exception) {
mapOf(
"content" to listOf(mapOf(

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

@ -234,7 +234,7 @@ private class ExitNavigationHandler(private val context: Context?) : FunctionHan
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已结束当前导航服务\"}"
return "{\"broadcast\":false,\"result\": \"已结束当前导航服务\"}"
}
}
@ -435,7 +435,7 @@ private class MediaPlayHandler(private val context: Context?) : FunctionHandler
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已开始播放媒体\"}"
return "{\"notreply\":true,\"broadcast\":false,\"result\": \"已开始播放媒体\"}"
}
}
@ -450,7 +450,7 @@ private class MediaPauseHandler(private val context: Context?) : FunctionHandler
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已暂停媒体播放\"}"
return "{\"notreply\":true,\"broadcast\":false,\"result\": \"已暂停媒体播放\"}"
}
}
/**
@ -464,7 +464,7 @@ private class MediaCloseHandler(private val context: Context?) : FunctionHandler
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已关闭媒体播放\"}"
return "{\"notreply\":true,\"broadcast\":false,\"result\": \"已关闭媒体播放\"}"
}
}
/**
@ -478,7 +478,7 @@ private class MediaPreviousHandler(private val context: Context?) : FunctionHand
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已切换到上一首\"}"
return "{\"notreply\":true,\"broadcast\":false,\"result\": \"已切换到上一首\"}"
}
}
@ -493,7 +493,7 @@ private class MediaNextHandler(private val context: Context?) : FunctionHandler
setPackage(context?.packageName) // 设置包名使其成为显式 Intent
}
context?.sendBroadcast(intent)
return "{\"result\": \"已切换到下一首\"}"
return "{\"notreply\":true,\"broadcast\":false,\"result\": \"已切换到下一首\"}"
}
}

Loading…
Cancel
Save