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

+10
View File
@@ -175,6 +175,16 @@ function projectOrderedTurn(
};
for (const message of messages) {
if (message.kind === "compaction") {
flushActivity();
flushAnswers();
units.push({
type: "message",
message,
sourceMessageCount: 1 + claimLeadingNoops(),
});
continue;
}
if (isCompletedDisplayNoop(message)) {
absorbDisplayNoop();
continue;
+18 -1
View File
@@ -62,6 +62,23 @@ function deriveAssistantCompletionTimes(messages: UIMessage[]): UIMessage[] {
});
}
function identifyCompactReplies(messages: UIMessage[]): UIMessage[] {
const turns = new Set(messages.flatMap((message) => (
message.role === "user" && message.content.trim().toLowerCase() === "/compact" && message.turnId
? [message.turnId]
: []
)));
return messages.map((message) => {
if (message.role !== "assistant" || message.kind || message.isStreaming
|| !message.turnId || !turns.has(message.turnId)) return message;
if (message.content === "Nothing to compact.") return { ...message, compactReply: "empty" };
if (message.content === "Unable to compact context. Check the logs and try again.") {
return { ...message, compactReply: "failed" };
}
return message;
});
}
export function projectWebuiThreadMessages(messages: UIMessage[]): UIMessage[] {
const normalized = scrubSubagentUiMessages(normalizeLegacyLongTaskMessages(messages));
const hiddenTurns = new Set(normalized.flatMap((message) => (
@@ -75,5 +92,5 @@ export function projectWebuiThreadMessages(messages: UIMessage[]): UIMessage[] {
&& !(message.role === "user" && isModelCommandText(message.content))
&& !(message.role === "assistant" && isModelCommandResponseText(message.content))
));
return deriveAssistantCompletionTimes(visible);
return deriveAssistantCompletionTimes(identifyCompactReplies(visible));
}
+18 -1
View File
@@ -2,7 +2,14 @@ type Role = "user" | "assistant" | "tool" | "system";
/** "trace" rows are intermediate agent breadcrumbs (tool-call hints,
* progress pings) that should not be rendered as conversational replies. */
type MessageKind = "message" | "trace";
type MessageKind = "message" | "trace" | "compaction";
export interface UIContextCompaction {
id: string;
phase: "started" | "succeeded" | "failed" | "cancelled";
/** Live wire transitions announce; hydrated transcript rows stay silent. */
announce?: boolean;
}
export type UITurnPhase = "user" | "reasoning" | "activity" | "answer" | "complete";
export type MessageDeliveryStatus = "sending" | "accepted" | "failed";
@@ -86,6 +93,10 @@ export interface UIMessage {
/** Internal projection marker for assistant text emitted before a later tool.
* It is not a wire message and is rendered as a compact activity row. */
activityKind?: "model";
/** Context-compaction lifecycle rendered as a standalone channel notice. */
compaction?: UIContextCompaction;
/** Display-only localization marker for fixed /compact command replies. */
compactReply?: "empty" | "failed";
/** User turn: optimistic blob URLs for preview. Replay: placeholder chips. */
images?: UIImage[];
/** Signed or local UI-renderable media attachments. */
@@ -1375,6 +1386,12 @@ export type InboundEvent =
event: "recovery_state";
chat_id: string;
} & RecoveryState)
| ({
event: "context_compaction";
chat_id: string;
compaction_id: string;
phase: UIContextCompaction["phase"];
} & InboundTurnMetadata)
| ({
event: "file_edit";
chat_id: string;