Update message history display

This commit is contained in:
2025-02-28 23:48:10 +08:00
parent de17af77f2
commit e44ddd0552

View File

@ -201,20 +201,24 @@ object JChatGPT : KotlinPlugin(
} }
replace("{history}") { replace("{history}") {
if (includeHistory) { if (!includeHistory) {
// 一段时间内的消息 return@replace "暂无内容"
val beforeTimestamp = now.minusMinutes(PluginConfig.historyWindowMin.toLong()).toEpochSecond().toInt() }
val nowTimestamp = now.toEpochSecond().toInt()
// 最近这段时间的历史对话 // 一段时间内的消息
val history = MiraiHibernateRecorder[event.subject, beforeTimestamp, nowTimestamp] val beforeTimestamp = now.minusMinutes(PluginConfig.historyWindowMin.toLong()).toEpochSecond().toInt()
.take(PluginConfig.historyMessageLimit) // 只取最近的部分消息,避免上下文过长 val nowTimestamp = now.toEpochSecond().toInt()
.sortedBy { it.time } // 按时间排序 // 最近这段时间的历史对话
// 构造历史消息 val history = MiraiHibernateRecorder[event.subject, beforeTimestamp, nowTimestamp]
val historyText = StringBuilder() .take(PluginConfig.historyMessageLimit) // 只取最近的部分消息,避免上下文过长
.sortedBy { it.time } // 按时间排序
// 构造历史消息
val historyText = StringBuilder()
if (event is GroupMessageEvent) {
for (record in history) { for (record in history) {
if (event.bot.id == record.fromId) { if (event.bot.id == record.fromId) {
historyText.append("") historyText.append("")
} else if (event is GroupMessageEvent) { } else {
val recordSender = event.subject[record.fromId] val recordSender = event.subject[record.fromId]
if (recordSender != null) { if (recordSender != null) {
// 群活跃等级 // 群活跃等级
@ -226,37 +230,57 @@ object JChatGPT : KotlinPlugin(
if (recordSender.specialTitle.isNotEmpty()) { if (recordSender.specialTitle.isNotEmpty()) {
historyText.append(recordSender.specialTitle) historyText.append(recordSender.specialTitle)
} else { } else {
historyText.append(when (recordSender.permission) { historyText.append(
OWNER -> "群主" when (recordSender.permission) {
ADMINISTRATOR -> "管理员" OWNER -> "群主"
MEMBER -> recordSender.temperatureTitle ADMINISTRATOR -> "管理员"
}) MEMBER -> recordSender.temperatureTitle
}
)
} }
// 群名片 // 群名片
historyText historyText
.append("") .append("")
.append(recordSender.nameCardOrNick) .append(recordSender.nameCardOrNick)
// .append(" (").append(recordSender.id).append(")") // .append(" (").append(recordSender.id).append(")")
} else { } else {
// 未知群员 // 未知群员
historyText.append("未知群员(").append(record.fromId).append(")") historyText.append("未知群员(").append(record.fromId).append(")")
} }
} else {
historyText.append(event.senderName)
} }
historyText historyText
.append(" ") .append(" ")
// 发言时间 // 发言时间
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong()))) .append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
// 消息内容 // 消息内容
.append(" 说:").appendLine(record.toMessageChain().contentToString()) .append(" 说:").appendLine(record.toMessageChain().joinToString("") {
} when (it) {
is At -> {
it.getDisplay(event.subject)
}
historyText.toString() is ForwardMessage -> {
it.title + "\n" + it.preview
}
is QuoteReply -> {
">" + it.source.originalMessage.contentToString().replace("\n", "\n> ") + "\n"
}
else -> {
it.contentToString()
}
}
})
}
} else { } else {
"暂无内容" // TODO 私聊
} }
historyText.toString()
} }
return prompt.toString() return prompt.toString()
} }
@ -501,7 +525,7 @@ object JChatGPT : KotlinPlugin(
// 过会撤回加载消息 // 过会撤回加载消息
if (receipt != null) { if (receipt != null) {
launch { launch {
delay(3.seconds); delay(3.seconds)
try { try {
receipt.recall() receipt.recall()
} catch (e: Throwable) { } catch (e: Throwable) {