Expand forwarded messages into context with nested support

Forwarded messages were collapsed to a placeholder, so the LLM couldn't
read a forwarded conversation a user asked it to look at. Now forwards
are fully expanded (no truncation, relying on the large context window
and cache hits) as Markdown blockquotes, with each nesting level adding
another ">". Nested forwards recurse by depth for clean, unambiguous
indentation; node bodies (including images) render inline.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-20 23:03:26 +08:00
parent 890ccb10d5
commit 538fe563a0

View File

@@ -544,9 +544,8 @@ object JChatGPT : KotlinPlugin(
private fun singleMessageToText(it: SingleMessage): String {
return when (it) {
is ForwardMessage -> {
"[转发消息·${it.nodeList.size}条:${it.title}]"
}
// 完整展开合并转发内容,便于 LLM 阅读分析转发的对话(依赖大上下文+缓存,不做截断)
is ForwardMessage -> formatForward(it, 1)
// 图片格式化
is Image -> {
@@ -565,6 +564,32 @@ object JChatGPT : KotlinPlugin(
}
}
/**
* 递归展开合并转发消息,用 Markdown 引用块表示:每加深一层嵌套多一个 `>`>、>>、>>>…)。
* @param depth 当前嵌套层级,从 1 开始
*/
private fun formatForward(forward: ForwardMessage, depth: Int): String = buildString {
val quote = ">".repeat(depth) + " "
append("[转发消息·").append(forward.nodeList.size).append("")
if (forward.title.isNotEmpty()) append(':').append(forward.title)
append(']')
for (node in forward.nodeList) {
append('\n').append(quote)
.append(node.senderName).append(' ')
.append(shortTimeFormatter.format(Instant.ofEpochSecond(node.time.toLong())))
.append(": ")
node.messageChain.forEach { sub ->
if (sub is ForwardMessage) {
// 嵌套转发:层级加深,自带更深的 `>` 前缀,无需再次缩进
append(formatForward(sub, depth + 1))
} else {
// 其它内容:多行正文对齐到当前引用层级
append(singleMessageToText(sub).replace("\n", "\n$quote"))
}
}
}
}
// endregion - 历史消息相关 -
private val thinkRegex = Regex("<think>[\\s\\S]*?</think>")