diff --git a/src/main/kotlin/JChatGPT.kt b/src/main/kotlin/JChatGPT.kt index d2908ca..40d3e34 100644 --- a/src/main/kotlin/JChatGPT.kt +++ b/src/main/kotlin/JChatGPT.kt @@ -717,6 +717,9 @@ object JChatGPT : KotlinPlugin( // 好感度调整 AdjustUserFavorabilityAgent(), + // 请求主人帮助 + RequestOwner(), + // Epic 免费游戏 // EpicFreeGame(), diff --git a/src/main/kotlin/PluginConfig.kt b/src/main/kotlin/PluginConfig.kt index ef25f0f..0acaf59 100644 --- a/src/main/kotlin/PluginConfig.kt +++ b/src/main/kotlin/PluginConfig.kt @@ -5,6 +5,9 @@ import net.mamoe.mirai.console.data.ValueDescription import net.mamoe.mirai.console.data.value object PluginConfig : AutoSavePluginConfig("Config") { + @ValueDescription("主人QQ,AI可以通过工具向主人发起请求,会等待一段时间") + val ownerId: Long by value() + @ValueDescription("OpenAI API base url") val openAiApi: String by value("https://dashscope.aliyuncs.com/compatible-mode/v1/") @@ -107,4 +110,7 @@ object PluginConfig : AutoSavePluginConfig("Config") { @ValueDescription("表情包路径,配置后会加载目录下的文件名,提示词中需要用{meme}来插入上下文") val memeDir: String by value("") + + @ValueDescription("请求主人回复等待时间,单位毫秒,默认300秒") + val requestOwnerWaitTimeout: Long by value(300000L) } \ No newline at end of file diff --git a/src/main/kotlin/tools/RequestOwner.kt b/src/main/kotlin/tools/RequestOwner.kt new file mode 100644 index 0000000..728b1c5 --- /dev/null +++ b/src/main/kotlin/tools/RequestOwner.kt @@ -0,0 +1,57 @@ +package top.jie65535.mirai.tools + +import com.aallam.openai.api.chat.Tool +import com.aallam.openai.api.core.Parameters +import kotlinx.coroutines.withTimeout +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.add +import kotlinx.serialization.json.jsonPrimitive +import kotlinx.serialization.json.put +import kotlinx.serialization.json.putJsonArray +import kotlinx.serialization.json.putJsonObject +import net.mamoe.mirai.event.EventPriority +import net.mamoe.mirai.event.GlobalEventChannel +import net.mamoe.mirai.event.events.FriendMessageEvent +import net.mamoe.mirai.event.events.MessageEvent +import net.mamoe.mirai.event.nextEvent +import net.mamoe.mirai.message.data.content +import top.jie65535.mirai.JChatGPT +import top.jie65535.mirai.PluginConfig +import kotlin.collections.getValue + +class RequestOwner : BaseAgent( + tool = Tool.function( + name = "requestOwnerHelp", + description = "当遇到无法处理的问题时,通过私聊请求系统管理员并阻塞等待其回复,默认超时时间5分钟,请求前建议提前告知群友。", + parameters = Parameters.buildJsonObject { + put("type", "object") + putJsonObject("properties") { + putJsonObject("content") { + put("type", "string") + put("description", "请求内容与上下文") + } + } + putJsonArray("required") { + add("content") + } + } + ) +) { + override val isEnabled: Boolean + get() = PluginConfig.ownerId > 0 && PluginConfig.requestOwnerWaitTimeout > 0 + + override suspend fun execute(args: JsonObject?, event: MessageEvent): String { + requireNotNull(args) + val owner = event.bot.getFriendOrFail(PluginConfig.ownerId) + val content = args.getValue("content").jsonPrimitive.content + JChatGPT.logger.info("请求主人协助中:$content") + owner.sendMessage(JChatGPT.toMessage(owner, content)) + + val nextEvent: FriendMessageEvent = withTimeout(PluginConfig.requestOwnerWaitTimeout) { + GlobalEventChannel.nextEvent(EventPriority.MONITOR) { it.sender.id == PluginConfig.ownerId } + } + val response = nextEvent.message.content + JChatGPT.logger.info("主人回复:$response") + return response + } +} \ No newline at end of file