mirror of
https://github.com/jie65535/JChatGPT.git
synced 2025-10-20 17:13:37 +08:00
Adjust the chat history concatenation format
This commit is contained in:
@@ -200,15 +200,28 @@ object JChatGPT : KotlinPlugin(
|
|||||||
val history = MiraiHibernateRecorder[event.subject, time, nowTimestamp]
|
val history = MiraiHibernateRecorder[event.subject, time, nowTimestamp]
|
||||||
.take(PluginConfig.historyMessageLimit) // 只取最近的部分消息,避免上下文过长
|
.take(PluginConfig.historyMessageLimit) // 只取最近的部分消息,避免上下文过长
|
||||||
.sortedBy { it.time } // 按时间排序
|
.sortedBy { it.time } // 按时间排序
|
||||||
|
.toMutableList()
|
||||||
|
|
||||||
|
// 有一定概率最后一条消息没加入,这里检查然后补充一下
|
||||||
|
val msgIds = event.message.ids.joinToString(",")
|
||||||
|
if (!history.any { it.ids == msgIds }) {
|
||||||
|
history.add(MessageRecord.fromSuccess(event.message.source, event.message))
|
||||||
|
}
|
||||||
|
|
||||||
// 构造历史消息
|
// 构造历史消息
|
||||||
val historyText = StringBuilder()
|
val historyText = StringBuilder()
|
||||||
|
var lastId = 0L
|
||||||
if (event is GroupMessageEvent) {
|
if (event is GroupMessageEvent) {
|
||||||
for (record in history) {
|
for (record in history) {
|
||||||
appendGroupMessageRecord(historyText, record, event)
|
// 同一人发言不要反复出现这人的名字,减少上下文
|
||||||
|
appendGroupMessageRecord(historyText, record, event, lastId != record.fromId)
|
||||||
|
lastId = record.fromId
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (record in history) {
|
for (record in history) {
|
||||||
appendMessageRecord(historyText, record, event)
|
// 同一人发言不要反复出现这人的名字,减少上下文
|
||||||
|
appendMessageRecord(historyText, record, event, lastId != record.fromId)
|
||||||
|
lastId = record.fromId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,23 +237,33 @@ object JChatGPT : KotlinPlugin(
|
|||||||
fun appendGroupMessageRecord(
|
fun appendGroupMessageRecord(
|
||||||
historyText: StringBuilder,
|
historyText: StringBuilder,
|
||||||
record: MessageRecord,
|
record: MessageRecord,
|
||||||
event: GroupMessageEvent
|
event: GroupMessageEvent,
|
||||||
|
showSender: Boolean,
|
||||||
) {
|
) {
|
||||||
if (event.bot.id == record.fromId) {
|
if (showSender) {
|
||||||
historyText.append("**你** " + getNameCard(event.subject.botAsMember))
|
if (event.bot.id == record.fromId) {
|
||||||
} else {
|
historyText.append("**你** " + getNameCard(event.subject.botAsMember))
|
||||||
historyText.append(getNameCard(event.subject, record.fromId))
|
} else {
|
||||||
|
historyText.append(getNameCard(event.subject, record.fromId))
|
||||||
|
}
|
||||||
|
// 发言时间
|
||||||
|
historyText.append(' ')
|
||||||
|
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
|
||||||
}
|
}
|
||||||
// 发言时间
|
|
||||||
historyText.append(' ')
|
|
||||||
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
|
|
||||||
val recordMessage = record.toMessageChain()
|
val recordMessage = record.toMessageChain()
|
||||||
recordMessage[QuoteReply.Key]?.let {
|
recordMessage[QuoteReply.Key]?.let {
|
||||||
historyText.append(" 引用 ${getNameCard(event.subject, it.source.fromId)} 说的\n > ")
|
historyText.append(" 引用 ${getNameCard(event.subject, it.source.fromId)} 说的\n > ")
|
||||||
.appendLine(it.source.originalMessage.content.replace("\n", "\n > "))
|
.appendLine(it.source.originalMessage.content.replace("\n", "\n > "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (showSender) {
|
||||||
// 消息内容
|
// 消息内容
|
||||||
historyText.append(" 说:").appendLine(record.toMessageChain().joinToString("") {
|
historyText.append(" 说:")
|
||||||
|
}
|
||||||
|
|
||||||
|
historyText.appendLine(record.toMessageChain().joinToString("") {
|
||||||
when (it) {
|
when (it) {
|
||||||
is At -> {
|
is At -> {
|
||||||
it.getDisplay(event.subject)
|
it.getDisplay(event.subject)
|
||||||
@@ -269,17 +292,20 @@ object JChatGPT : KotlinPlugin(
|
|||||||
fun appendMessageRecord(
|
fun appendMessageRecord(
|
||||||
historyText: StringBuilder,
|
historyText: StringBuilder,
|
||||||
record: MessageRecord,
|
record: MessageRecord,
|
||||||
event: MessageEvent
|
event: MessageEvent,
|
||||||
|
showSender: Boolean
|
||||||
) {
|
) {
|
||||||
if (event.bot.id == record.fromId) {
|
if (showSender) {
|
||||||
historyText.append("**你** " + event.bot.nameCardOrNick)
|
if (event.bot.id == record.fromId) {
|
||||||
} else {
|
historyText.append("**你** " + event.bot.nameCardOrNick)
|
||||||
historyText.append(event.senderName)
|
} else {
|
||||||
|
historyText.append(event.senderName)
|
||||||
|
}
|
||||||
|
historyText
|
||||||
|
.append(" ")
|
||||||
|
// 发言时间
|
||||||
|
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
|
||||||
}
|
}
|
||||||
historyText
|
|
||||||
.append(" ")
|
|
||||||
// 发言时间
|
|
||||||
.append(timeFormatter.format(Instant.ofEpochSecond(record.time.toLong())))
|
|
||||||
val recordMessage = record.toMessageChain()
|
val recordMessage = record.toMessageChain()
|
||||||
recordMessage[QuoteReply.Key]?.let {
|
recordMessage[QuoteReply.Key]?.let {
|
||||||
historyText.append(" 引用\n > ")
|
historyText.append(" 引用\n > ")
|
||||||
@@ -287,8 +313,11 @@ object JChatGPT : KotlinPlugin(
|
|||||||
.joinToString("", transform = ::singleMessageToText)
|
.joinToString("", transform = ::singleMessageToText)
|
||||||
.replace("\n", "\n > "))
|
.replace("\n", "\n > "))
|
||||||
}
|
}
|
||||||
|
if (showSender) {
|
||||||
|
historyText.append(" 说:")
|
||||||
|
}
|
||||||
// 消息内容
|
// 消息内容
|
||||||
historyText.append(" 说:").appendLine(
|
historyText.appendLine(
|
||||||
record.toMessageChain().joinToString("", transform = ::singleMessageToText))
|
record.toMessageChain().joinToString("", transform = ::singleMessageToText))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user