diff --git a/src/main/kotlin/JChatGPT.kt b/src/main/kotlin/JChatGPT.kt index e9989f1..736fff1 100644 --- a/src/main/kotlin/JChatGPT.kt +++ b/src/main/kotlin/JChatGPT.kt @@ -548,7 +548,10 @@ object JChatGPT : KotlinPlugin( SendCompositeMessage(), // 记忆代理 - MemoryAgent(), + MemoryAppend(), + + // 记忆修改 + MemoryReplace(), // 结束循环 StopLoopAgent(), diff --git a/src/main/kotlin/PluginData.kt b/src/main/kotlin/PluginData.kt index e03e249..66781ec 100644 --- a/src/main/kotlin/PluginData.kt +++ b/src/main/kotlin/PluginData.kt @@ -8,4 +8,28 @@ object PluginData : AutoSavePluginData("data") { * 联系人记忆 */ val contactMemory by value(mutableMapOf()) + + /** + * 添加对话记忆 + */ + fun appendContactMemory(contactId: Long, newMemory: String) { + val memory = contactMemory[contactId] + if (memory.isNullOrEmpty()) { + contactMemory[contactId] = "* $newMemory" + } else { + contactMemory[contactId] = "$memory\n* $newMemory" + } + } + + /** + * 替换对话记忆 + */ + fun replaceContactMemory(contactId: Long, oldMemory: String, newMemory: String) { + val memory = contactMemory[contactId] + if (memory.isNullOrEmpty()) { + contactMemory[contactId] = "* $newMemory" + } else { + contactMemory[contactId] = memory.replace(oldMemory, newMemory) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/tools/MemoryAgent.kt b/src/main/kotlin/tools/MemoryAppend.kt similarity index 72% rename from src/main/kotlin/tools/MemoryAgent.kt rename to src/main/kotlin/tools/MemoryAppend.kt index 868b9ee..533d957 100644 --- a/src/main/kotlin/tools/MemoryAgent.kt +++ b/src/main/kotlin/tools/MemoryAppend.kt @@ -12,16 +12,16 @@ import net.mamoe.mirai.event.events.MessageEvent import top.jie65535.mirai.JChatGPT import top.jie65535.mirai.PluginData -class MemoryAgent : BaseAgent( +class MemoryAppend : BaseAgent( tool = Tool.function( - name = "remember", - description = "用于更新当前记忆块,可以用于记住网友的特征和事实等。新增记忆时请带上原记忆,否则会被覆盖!", + name = "memoryAppend", + description = "新增记忆项", parameters = Parameters.buildJsonObject { put("type", "object") putJsonObject("properties") { putJsonObject("memory") { put("type", "string") - put("description", "新记忆内容,无序列表文本块。") + put("description", "记忆项") } } putJsonArray("required") { @@ -34,9 +34,8 @@ class MemoryAgent : BaseAgent( requireNotNull(args) val contactId = event.subject.id val memoryText = args.getValue("memory").jsonPrimitive.content - JChatGPT.logger.info("Remember ($contactId): $memoryText") - PluginData.contactMemory[contactId] = memoryText - + JChatGPT.logger.info("Remember ($contactId): \"$memoryText\"") + PluginData.appendContactMemory(contactId, memoryText) return "OK" } } \ No newline at end of file diff --git a/src/main/kotlin/tools/MemoryReplace.kt b/src/main/kotlin/tools/MemoryReplace.kt new file mode 100644 index 0000000..f6fe2e7 --- /dev/null +++ b/src/main/kotlin/tools/MemoryReplace.kt @@ -0,0 +1,47 @@ +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 MemoryReplace : BaseAgent( + tool = Tool.Companion.function( + name = "memoryReplace", + description = "替换记忆项", + parameters = Parameters.Companion.buildJsonObject { + put("type", "object") + putJsonObject("properties") { + putJsonObject("oldMemory") { + put("type", "string") + put("description", "原记忆项") + } + putJsonObject("newMemory") { + put("type", "string") + put("description", "新记忆项") + } + } + putJsonArray("required") { + add("oldMemory") + add("newMemory") + } + } + ) +) { + override suspend fun execute(args: JsonObject?, event: MessageEvent): String { + requireNotNull(args) + val contactId = event.subject.id + val oldMemoryText = args.getValue("oldMemory").jsonPrimitive.content + val newMemoryText = args.getValue("newMemory").jsonPrimitive.content + JChatGPT.logger.info("Replace memory ($contactId): \"$oldMemoryText\" -> \"$newMemoryText\"") + PluginData.replaceContactMemory(contactId, oldMemoryText, newMemoryText) + return "OK" + } +} \ No newline at end of file