From 538fe563a08fd8c9808ac0ea874f122d8d9bfa88 Mon Sep 17 00:00:00 2001 From: jie65535 Date: Sat, 20 Jun 2026 23:03:26 +0800 Subject: [PATCH] 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 --- src/main/kotlin/JChatGPT.kt | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/JChatGPT.kt b/src/main/kotlin/JChatGPT.kt index b1fd229..d261b34 100644 --- a/src/main/kotlin/JChatGPT.kt +++ b/src/main/kotlin/JChatGPT.kt @@ -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("[\\s\\S]*?")