feat: make context compaction visible in channels (#5656)

* feat: surface context compaction lifecycle

* fix: complete and simplify context compaction notifications
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-05 20:21:54 +08:00
1 parent 455533169d
commit 5229989f28
52 files changed
+1849 -140

No files matched your search

+45 -2
View File
@@ -14,6 +14,7 @@ import {
} from "@opentui/core"
import type {
ContextCompaction,
FileEditEvent,
HistoryMessage,
MediaAttachment,
@@ -126,6 +127,10 @@ export class Transcript {
}> = []
private readonly markdown = new Set<MarkdownRenderable>()
private readonly activities = new Set<Activity>()
private readonly compactions = new Map<string, {
phase: ContextCompaction["phase"]
text: TextRenderable
}>()
private readonly frames = new Set<BoxRenderable>()
private readonly userRows = new Set<BoxRenderable>()
private readonly userMessages = new Set<{
@@ -181,6 +186,9 @@ export class Transcript {
this.theme = theme
this.codeRailColor = RGBA.fromHex(theme.border)
for (const { renderable, tone } of this.styledText) renderable.fg = theme[tone]
for (const { text, phase } of this.compactions.values()) {
text.fg = phase === "failed" ? theme.error : theme.muted
}
for (const message of this.userMessages) {
message.renderable.content = this.userMessageContent(
message.content,
@@ -243,6 +251,7 @@ export class Transcript {
this.styledText.length = 0
this.markdown.clear()
this.activities.clear()
this.compactions.clear()
this.frames.clear()
this.userRows.clear()
this.userMessages.clear()
@@ -257,7 +266,8 @@ export class Transcript {
history(messages: HistoryMessage[]): void {
for (const message of messages) {
if (message.role === "user") {
if (message.compaction) this.compaction(message.compaction)
else if (message.role === "user") {
this.user(message.content, message.turnId, message.media)
}
else if (message.role === "assistant") this.assistant(message.content)
@@ -273,7 +283,9 @@ export class Transcript {
const previousHeight = this.root.scrollHeight
let index = 1 // Keep the launch header first.
for (const message of messages) {
if (message.role === "user") {
if (message.compaction) {
if (this.compaction(message.compaction, index)) index += 1
} else if (message.role === "user") {
if (message.turnId && this.userTurnIds.has(message.turnId)) continue
this.writeUser(message.content, message.media, index++)
if (message.turnId) this.userTurnIds.add(message.turnId)
@@ -328,6 +340,36 @@ export class Transcript {
this.writeRole(error ? "×" : "·", content, error ? "error" : "muted")
}
compaction(compaction: ContextCompaction, index?: number): boolean {
let entry = this.compactions.get(compaction.id)
// Hydration can replay a terminal state before an already queued start event.
if (entry && (entry.phase !== "started" || entry.phase === compaction.phase)) return false
const added = !entry
if (index === undefined) {
this.noteOutput()
if (added) this.finishActivity()
}
if (!entry) {
const row = this.createRow("compaction")
const text = this.createText("", "muted", false, "compaction-status")
row.add(text)
this.root.add(row, index)
this.wrote = true
entry = { phase: compaction.phase, text }
this.compactions.set(compaction.id, entry)
}
entry.phase = compaction.phase
entry.text.content = compaction.phase === "started"
? " ≋ Compacting conversation…"
: compaction.phase === "succeeded"
? " ✓ Conversation compacted"
: compaction.phase === "cancelled"
? " · Conversation compaction cancelled"
: " × Could not compact conversation"
entry.text.fg = compaction.phase === "failed" ? this.theme.error : this.theme.muted
return added
}
stream(delta: string): void {
if (!delta) return
this.noteOutput()
@@ -416,6 +458,7 @@ export class Transcript {
this.pendingStream = ""
this.live = null
this.activity = null
this.compactions.clear()
this.frames.clear()
this.userRows.clear()
this.userMessages.clear()