From ea4e6123b3c042b4fcb3474ec4d03e1f57f3dc39 Mon Sep 17 00:00:00 2001 From: jie65535 Date: Tue, 15 Jul 2025 10:37:28 +0800 Subject: [PATCH] Add Memory Agent --- src/main/kotlin/JChatGPT.kt | 12 ++++++++ src/main/kotlin/PluginData.kt | 6 +++- src/main/kotlin/tools/MemoryAgent.kt | 42 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/tools/MemoryAgent.kt diff --git a/src/main/kotlin/JChatGPT.kt b/src/main/kotlin/JChatGPT.kt index 669f010..f06373a 100644 --- a/src/main/kotlin/JChatGPT.kt +++ b/src/main/kotlin/JChatGPT.kt @@ -61,6 +61,7 @@ object JChatGPT : KotlinPlugin( // 注册聊天权限 PermissionService.INSTANCE.register(chatPermission, "JChatGPT Chat Permission") PluginConfig.reload() + PluginData.reload() // 设置Token LargeLanguageModels.reload() @@ -144,6 +145,14 @@ object JChatGPT : KotlinPlugin( "与 \"${event.senderName}\" 私聊中" } } + + replace("{memory}") { + val memoryText = PluginData.contactMemory[event.subject.id] + if (memoryText.isNullOrEmpty()) { + "暂无相关记忆" + } else memoryText + } + return prompt.toString() } @@ -663,6 +672,9 @@ object JChatGPT : KotlinPlugin( // 发送组合消息 SendCompositeMessage(), + // 记忆代理 + MemoryAgent(), + // 结束循环 StopLoopAgent(), diff --git a/src/main/kotlin/PluginData.kt b/src/main/kotlin/PluginData.kt index 68476b9..e03e249 100644 --- a/src/main/kotlin/PluginData.kt +++ b/src/main/kotlin/PluginData.kt @@ -1,7 +1,11 @@ package top.jie65535.mirai import net.mamoe.mirai.console.data.AutoSavePluginData +import net.mamoe.mirai.console.data.value object PluginData : AutoSavePluginData("data") { - + /** + * 联系人记忆 + */ + val contactMemory by value(mutableMapOf()) } \ No newline at end of file diff --git a/src/main/kotlin/tools/MemoryAgent.kt b/src/main/kotlin/tools/MemoryAgent.kt new file mode 100644 index 0000000..1e23b66 --- /dev/null +++ b/src/main/kotlin/tools/MemoryAgent.kt @@ -0,0 +1,42 @@ +package top.jie65535.mirai.tools + +import com.aallam.openai.api.chat.Tool +import com.aallam.openai.api.core.Parameters +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.events.MessageEvent +import top.jie65535.mirai.JChatGPT +import top.jie65535.mirai.PluginData + +class MemoryAgent : BaseAgent( + tool = Tool.function( + name = "remember", + description = "更新当前记忆块。新增记忆时请带上原记忆,否则会被覆盖!", + parameters = Parameters.buildJsonObject { + put("type", "object") + putJsonObject("properties") { + putJsonObject("memory") { + put("type", "string") + put("description", "新记忆内容,无序列表文本块。") + } + } + putJsonArray("required") { + add("memory") + } + } + ) +) { + override suspend fun execute(args: JsonObject?, event: MessageEvent): String { + requireNotNull(args) + val contactId = event.subject.id + val memoryText = args.getValue("memory").jsonPrimitive.content + JChatGPT.logger.info("Remember ($contactId): $memoryText") + PluginData.contactMemory[contactId] = memoryText + + return "OK" + } +} \ No newline at end of file