Split memory agent

This commit is contained in:
2025-07-20 11:11:47 +08:00
parent c479282fe4
commit 3a5caf86a1
4 changed files with 81 additions and 8 deletions

View File

@ -548,7 +548,10 @@ object JChatGPT : KotlinPlugin(
SendCompositeMessage(),
// 记忆代理
MemoryAgent(),
MemoryAppend(),
// 记忆修改
MemoryReplace(),
// 结束循环
StopLoopAgent(),

View File

@ -8,4 +8,28 @@ object PluginData : AutoSavePluginData("data") {
* 联系人记忆
*/
val contactMemory by value(mutableMapOf<Long, String>())
/**
* 添加对话记忆
*/
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)
}
}
}

View File

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

View File

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