|
|
|
@ -673,7 +673,7 @@ object AgentService : CoroutineScope { |
|
|
|
|
|
|
|
override fun onComplete() { |
|
|
|
// 视情况决定是否朗读回复 |
|
|
|
if (speakResponse) { |
|
|
|
if (speakResponse && !nobroadcast) { |
|
|
|
ttsService?.flushStream() |
|
|
|
} |
|
|
|
val response = responseBuilder.toString() |
|
|
|
@ -731,13 +731,16 @@ 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; |
|
|
|
nobroadcast = broadcast |
|
|
|
sendEvent("function_call_result", mapOf( |
|
|
|
"function_call" to functionCall.toString(), |
|
|
|
"result" to functionCallResult.toString(), |
|
|
|
"meta" to metestr, |
|
|
|
)) |
|
|
|
val (metestr, broadcast)= autoHandleFcunCallResult(functionCallResult); |
|
|
|
aiMetadata = metestr; |
|
|
|
nobroadcast = broadcast |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
@ -1102,55 +1105,86 @@ object AgentService : CoroutineScope { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
|
|
|
|
|
/* |
|
|
|
* 自动播放音乐 |
|
|
|
* */ |
|
|
|
fun autoHandleFcunCallResult(functionCallResult: JSONObject): Pair<String, Boolean>{ |
|
|
|
val metaStr = functionCallResult.optString("meta") |
|
|
|
var broadcast = false; |
|
|
|
if (metaStr.isNotEmpty()) { |
|
|
|
val meta = JSONObject(metaStr) |
|
|
|
// Log.d(TAG, "mcp调用结果: $metaStr") |
|
|
|
if (meta.has("card_music")) { //音乐卡片 |
|
|
|
val cardMusic = meta.getJSONObject("card_music") |
|
|
|
val id = cardMusic.optString("id", "") |
|
|
|
val url = cardMusic.optString("url", "") |
|
|
|
val name = cardMusic.optString("name", "") |
|
|
|
val sgener = cardMusic.optString("sgener", "") |
|
|
|
val image = cardMusic.optString("image", "") |
|
|
|
processMusicPlay(mapOf("id" to id,"url" to url, "title" to name, "artist" to sgener,"coverUrl" to image)) |
|
|
|
broadcast = true; |
|
|
|
}else if (meta.has("card_musiclist")) { //音乐列表 |
|
|
|
val cardMusiclist = meta.getJSONObject("card_musiclist") |
|
|
|
val musics = cardMusiclist.optJSONArray("musics") |
|
|
|
if (musics != null) { |
|
|
|
val playlist = mutableListOf<Map<String, String>>() |
|
|
|
for (i in 0 until musics.length()) { |
|
|
|
val item = musics.optJSONObject(i) ?: continue |
|
|
|
val id = item.optString("id", "") |
|
|
|
val url = item.optString("url", "") |
|
|
|
val name = item.optString("name", "") |
|
|
|
val sgener = item.optString("sgener", "") |
|
|
|
val image = item.optString("image", "") |
|
|
|
playlist.add(mapOf("id" to id,"url" to url, "title" to name, "artist" to sgener,"coverUrl" to image)) |
|
|
|
} |
|
|
|
if (playlist.isNotEmpty()) { |
|
|
|
broadcast = true; |
|
|
|
processMusicPlayList(playlist) |
|
|
|
fun autoHandleFcunCallResult(toolname:String,functionCallResult: JSONObject): Pair<String, Boolean>{ |
|
|
|
println("协议工具返回数据 检查是否存在卡片! functionCallResult: ${functionCallResult}") |
|
|
|
try { |
|
|
|
val contentStr = functionCallResult.optString("context") |
|
|
|
println("协议工具返回数据 检查是否存在卡片! context: ${contentStr}") |
|
|
|
val content = JSONObject(contentStr) |
|
|
|
val textStr = content.optString("text") |
|
|
|
println("协议工具返回数据 检查是否存在卡片! textStr: ${textStr}") |
|
|
|
val metadata: MutableMap<String, Any> = mutableMapOf() |
|
|
|
var broadcast = false; |
|
|
|
println("协议工具返回数据 检查是否存在卡片!") |
|
|
|
if (textStr.isNotEmpty()) { |
|
|
|
val meta = JSONObject(textStr) |
|
|
|
metadata[toolname] = meta // 现在可以赋值 |
|
|
|
val metaStr = JSONObject(metadata as Map<*, *>).toString() |
|
|
|
// Log.d(TAG, "mcp调用结果: $metaStr") |
|
|
|
if (meta.has("card_music")) { //音乐卡片 |
|
|
|
val cardMusic = meta.getJSONObject("card_music") |
|
|
|
val id = cardMusic.optString("id", "") |
|
|
|
val url = cardMusic.optString("url", "") |
|
|
|
val name = cardMusic.optString("name", "") |
|
|
|
val sgener = cardMusic.optString("sgener", "") |
|
|
|
val image = cardMusic.optString("image", "") |
|
|
|
processMusicPlay( |
|
|
|
mapOf( |
|
|
|
"id" to id, |
|
|
|
"url" to url, |
|
|
|
"title" to name, |
|
|
|
"artist" to sgener, |
|
|
|
"coverUrl" to image |
|
|
|
) |
|
|
|
) |
|
|
|
broadcast = true; |
|
|
|
} else if (meta.has("card_musiclist")) { //音乐列表 |
|
|
|
val cardMusiclist = meta.getJSONObject("card_musiclist") |
|
|
|
val musics = cardMusiclist.optJSONArray("musics") |
|
|
|
if (musics != null) { |
|
|
|
val playlist = mutableListOf<Map<String, String>>() |
|
|
|
for (i in 0 until musics.length()) { |
|
|
|
val item = musics.optJSONObject(i) ?: continue |
|
|
|
val id = item.optString("id", "") |
|
|
|
val url = item.optString("url", "") |
|
|
|
val name = item.optString("name", "") |
|
|
|
val sgener = item.optString("sgener", "") |
|
|
|
val image = item.optString("image", "") |
|
|
|
playlist.add( |
|
|
|
mapOf( |
|
|
|
"id" to id, |
|
|
|
"url" to url, |
|
|
|
"title" to name, |
|
|
|
"artist" to sgener, |
|
|
|
"coverUrl" to image |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
if (playlist.isNotEmpty()) { |
|
|
|
broadcast = true; |
|
|
|
processMusicPlayList(playlist) |
|
|
|
} else { |
|
|
|
Log.w(TAG, "card_musiclist 中没有有效的音乐条目") |
|
|
|
} |
|
|
|
} else { |
|
|
|
Log.w(TAG, "card_musiclist 中没有有效的音乐条目") |
|
|
|
Log.w(TAG, "card_musiclist.musics 不是一个数组") |
|
|
|
} |
|
|
|
} else { |
|
|
|
Log.w(TAG, "card_musiclist.musics 不是一个数组") |
|
|
|
} else if (meta.has("card_navigation")) { //导航服务 |
|
|
|
val cardNavigation = meta.getJSONObject("card_navigation") |
|
|
|
val start = cardNavigation.optString("start", "") |
|
|
|
val end = cardNavigation.optString("end", "") |
|
|
|
broadcast = true; |
|
|
|
processNavigation(start, end); |
|
|
|
} |
|
|
|
}else if (meta.has("card_navigation")) { //导航服务 |
|
|
|
val cardNavigation = meta.getJSONObject("card_navigation") |
|
|
|
val start = cardNavigation.optString("start", "") |
|
|
|
val end = cardNavigation.optString("end", "") |
|
|
|
broadcast = true; |
|
|
|
processNavigation(start,end); |
|
|
|
return Pair(metaStr, broadcast); |
|
|
|
} |
|
|
|
return Pair(metaStr, broadcast); |
|
|
|
} catch (e: Exception) { |
|
|
|
// 捕获其他类型的异常(可选) |
|
|
|
println("为解析到卡片数据!") |
|
|
|
} |
|
|
|
return Pair("", false); |
|
|
|
} |
|
|
|
@ -1167,6 +1201,7 @@ object AgentService : CoroutineScope { |
|
|
|
*/ |
|
|
|
fun processMusicPlay(song: Map<String,String>){ |
|
|
|
// 在其他 Service、BroadcastReceiver 或 Application 中调用 |
|
|
|
Log.i(TAG, "播放音乐: ${song}") |
|
|
|
MusicServiceStarter.startServiceWithCommand(context, command = "play", song = song) |
|
|
|
} |
|
|
|
|
|
|
|
@ -1184,7 +1219,7 @@ object AgentService : CoroutineScope { |
|
|
|
if (songs.isNotEmpty()) { |
|
|
|
MusicServiceStarter.startServiceWithPlaylist(context, command = "playlist", songs = songs) |
|
|
|
} else { |
|
|
|
Log.i(TAG, "音乐列表为空,未启动播放服务") |
|
|
|
Log.e(TAG, "音乐列表为空,未启动播放服务") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|