From 8d566fd967514e734e8ec257f30deca9cca7b135 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 2 Aug 2025 11:38:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0qq=E9=9F=B3=E4=B9=90=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunqiinnovation/chat_api/MCPSubClient.kt | 2 + .../chat_api/QQMusicSystemFunctionHandler.kt | 79 ++++++++++++------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt index cf3a96703..320a208ff 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPSubClient.kt @@ -433,6 +433,7 @@ class MCPSubClient( } } } + /** * 处理连接错误并尝试重连 */ @@ -453,6 +454,7 @@ class MCPSubClient( // 可以在这里添加通知或回调,告知上层连接彻底失败 } } + /** * 关闭连接 */ diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt index ccd706d2a..5ad158337 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/QQMusicSystemFunctionHandler.kt @@ -21,13 +21,18 @@ class QQMusicSystemFunctionHandler(private val context: Context? = null) { // Only register QQ Music search function client.registerLocalFunction( "qqmusic_search_songs", - "在QQ音乐中搜索歌曲并播放,支持歌曲名和歌手名", + "在QQ音乐中搜索内容,支持歌曲、专辑、歌手等搜索类型", mapOf( "type" to "object", "properties" to mapOf( "keyword" to mapOf( "type" to "string", - "description" to "搜索关键词,歌曲名或歌手名" + "description" to "搜索关键词,歌曲名、专辑名或歌手名" + ), + "searchType" to mapOf( + "type" to "integer", + "description" to "搜索类型:0-歌曲,1-专辑,2-歌手", + "default" to 0 ) ), "required" to listOf("keyword") @@ -36,26 +41,26 @@ class QQMusicSystemFunctionHandler(private val context: Context? = null) { ) // QQ音乐播放歌曲 - // client.registerLocalFunction( - // "qqmusic_play_songs", - // "播放QQ音乐歌曲列表", - // mapOf( - // "type" to "object", - // "properties" to mapOf( - // "songMids" to mapOf( - // "type" to "array", - // "description" to "歌曲ID列表", - // "items" to mapOf("type" to "string") - // ), - // "index" to mapOf( - // "type" to "integer", - // "description" to "开始播放的歌曲索引,默认为0" - // ) - // ), - // "required" to listOf("songMids") - // ), - // QQMusicPlaySongsHandler(context) - // ) + client.registerLocalFunction( + "qqmusic_play_songs", + "播放QQ音乐歌曲列表", + mapOf( + "type" to "object", + "properties" to mapOf( + "songMids" to mapOf( + "type" to "array", + "description" to "歌曲ID列表", + "items" to mapOf("type" to "string") + ), + "index" to mapOf( + "type" to "integer", + "description" to "开始播放的歌曲索引,默认为0" + ) + ), + "required" to listOf("songMids") + ), + QQMusicPlaySongsHandler(context) + ) // QQ音乐播放控制 // client.registerLocalFunction( @@ -197,17 +202,29 @@ private class QQMusicSearchHandler(private val context: Context?) : FunctionHand override suspend fun handle(arguments: Map): String { return try { val keyword = arguments["keyword"] as? String ?: return "{\"success\": false, \"message\": \"缺少搜索关键词\"}" + val searchType = arguments["searchType"] as? Int ?: 0 // 默认搜索歌曲 val qqMusicSingleton = QQMusicSingleton.getInstance() var result = "" // 直接调用搜索,内部会自动处理登录检查 - qqMusicSingleton.searchSongs(keyword) { searchResult -> + qqMusicSingleton.searchSongs(keyword, searchType) { searchResult -> result = if (searchResult.isSuccess) { val songs = searchResult.getOrNull() ?: emptyList() - val songsJson = songs.joinToString(",") { song -> + + // 根据搜索类型处理结果 + val processedSongs = when (searchType) { + 0 -> { // 歌曲搜索,只返回第一个结果 + if (songs.isNotEmpty()) listOf(songs.first()) else emptyList() + } + else -> { // 专辑、歌手等其他类型,返回所有结果 + songs + } + } + + val songsJson = processedSongs.joinToString(",") { song -> val songMap = song as Map // 添加调试日志 - Log.d("QQMusicSearchHandler", "原始歌曲数据: $songMap") + Log.d("QQMusicSearchHandler", "原始数据: $songMap") // 修正字段名映射 val mid = songMap["mid"] ?: "unknown" val name = songMap["name"] ?: "unknown" @@ -215,9 +232,17 @@ private class QQMusicSearchHandler(private val context: Context?) : FunctionHand val album = songMap["album"] ?: "" val pic = songMap["pic"] ?: "" val duration = songMap["duration"] ?: 0 - "{\"mid\": \"$mid\", \"name\": \"$name\", \"artist\": \"$artist\", \"album\": \"$album\", \"duration\":$duration,\"pic\": \"$pic\"}" + "{\"mid\": \"$mid\", \"name\": \"$name\", \"artist\": \"$artist\", \"album\": \"$album\", \"duration\":$duration,\"pic\": \"$pic\"}" + } + + val searchTypeText = when (searchType) { + 0 -> "歌曲" + 1 -> "专辑" + 2 -> "歌手" + else -> "内容" } - "{\"success\": true, \"message\": \"搜索成功\", \"data\": [$songsJson]}" + + "{\"success\": true, \"message\": \"${searchTypeText}搜索成功\", \"searchType\": $searchType, \"data\": [$songsJson]}" } else { val error = searchResult.exceptionOrNull() "{\"success\": false, \"message\": \"搜索失败: ${error?.message}\"}"