Compare commits

..

2 Commits

Author SHA1 Message Date
6705859604 Add requestOwnerHelp tool 2025-12-06 00:24:24 +08:00
621a045a62 Update Jina to allow trial usage without a key 2025-12-02 23:52:34 +08:00
4 changed files with 70 additions and 2 deletions

View File

@@ -717,6 +717,9 @@ object JChatGPT : KotlinPlugin(
// 好感度调整 // 好感度调整
AdjustUserFavorabilityAgent(), AdjustUserFavorabilityAgent(),
// 请求主人帮助
RequestOwner(),
// Epic 免费游戏 // Epic 免费游戏
// EpicFreeGame(), // EpicFreeGame(),

View File

@@ -5,6 +5,9 @@ import net.mamoe.mirai.console.data.ValueDescription
import net.mamoe.mirai.console.data.value import net.mamoe.mirai.console.data.value
object PluginConfig : AutoSavePluginConfig("Config") { object PluginConfig : AutoSavePluginConfig("Config") {
@ValueDescription("主人QQAI可以通过工具向主人发起请求会等待一段时间")
val ownerId: Long by value()
@ValueDescription("OpenAI API base url") @ValueDescription("OpenAI API base url")
val openAiApi: String by value("https://dashscope.aliyuncs.com/compatible-mode/v1/") val openAiApi: String by value("https://dashscope.aliyuncs.com/compatible-mode/v1/")
@@ -107,4 +110,7 @@ object PluginConfig : AutoSavePluginConfig("Config") {
@ValueDescription("表情包路径,配置后会加载目录下的文件名,提示词中需要用{meme}来插入上下文") @ValueDescription("表情包路径,配置后会加载目录下的文件名,提示词中需要用{meme}来插入上下文")
val memeDir: String by value("") val memeDir: String by value("")
@ValueDescription("请求主人回复等待时间单位毫秒默认300秒")
val requestOwnerWaitTimeout: Long by value(300000L)
} }

View File

@@ -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
}
}

View File

@@ -41,7 +41,7 @@ class VisitWeb : BaseAgent(
} }
override val isEnabled: Boolean override val isEnabled: Boolean
get() = PluginConfig.jinaApiKey.isNotEmpty() get() = true // PluginConfig.jinaApiKey.isNotEmpty()
override val loadingMessage: String override val loadingMessage: String
get() = "上网中..." get() = "上网中..."
@@ -62,7 +62,9 @@ class VisitWeb : BaseAgent(
private suspend fun jinaReadPage(url: String): String { private suspend fun jinaReadPage(url: String): String {
return try { return try {
httpClient.get(JINA_READER_URL_PREFIX + url) { httpClient.get(JINA_READER_URL_PREFIX + url) {
header("Authorization", "Bearer ${PluginConfig.jinaApiKey}") if (PluginConfig.jinaApiKey.isNotEmpty()) {
header("Authorization", "Bearer ${PluginConfig.jinaApiKey}")
}
}.bodyAsText() }.bodyAsText()
} catch (e: Throwable) { } catch (e: Throwable) {
"Error fetching \"$url\": ${e.message}" "Error fetching \"$url\": ${e.message}"