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

+13 -3
View File
@@ -25,6 +25,7 @@ import { ImageLightbox } from "@/components/ImageLightbox";
import { MarkdownText } from "@/components/MarkdownText";
import { SlashCommandText } from "@/components/SlashCommandText";
import { ReasoningRow } from "@/components/thread/activity/ReasoningRow";
import { ContextCompactionNotice } from "@/components/thread/ContextCompactionNotice";
import { UserMessageText } from "@/components/UserMessageText";
import {
Tooltip,
@@ -343,6 +344,10 @@ export function MessageBubble({
[mcpPresets, message.mcpPresets],
);
if (message.kind === "compaction" && message.compaction) {
return <ContextCompactionNotice compaction={message.compaction} />;
}
if (message.kind === "trace") {
return <TraceGroup message={message} />;
}
@@ -440,7 +445,12 @@ export function MessageBubble({
);
}
const empty = message.content.trim().length === 0;
const assistantContent = message.compactReply === "empty"
? t("thread.compaction.empty")
: message.compactReply === "failed"
? t("thread.compaction.failed")
: message.content;
const empty = assistantContent.trim().length === 0;
const media = message.media ?? [];
const reasoning = message.role === "assistant" ? message.reasoning ?? "" : "";
const reasoningStreaming = !!(message.role === "assistant" && message.reasoningStreaming);
@@ -507,7 +517,7 @@ export function MessageBubble({
preserveStreamingLayout
onOpenFilePreview={onOpenFilePreview}
>
{message.content}
{assistantContent}
</MarkdownText>
</div>
{media.length > 0 ? <MessageMedia media={media} align="left" /> : null}
@@ -528,7 +538,7 @@ export function MessageBubble({
)}
>
{showCopyButton ? (
<MessageCopyButton content={message.content} />
<MessageCopyButton content={assistantContent} />
) : null}
{showForkButton ? (
<Tooltip>
@@ -0,0 +1,51 @@
import { Archive, CircleAlert, LoaderCircle } from "lucide-react";
import { useTranslation } from "react-i18next";
import { cn } from "@/lib/utils";
import type { UIContextCompaction } from "@/lib/types";
interface ContextCompactionNoticeProps {
compaction: UIContextCompaction;
}
export function ContextCompactionNotice({ compaction }: ContextCompactionNoticeProps) {
const { t } = useTranslation();
const title = compaction.phase === "started"
? t("thread.compaction.started", { defaultValue: "Compressing context…" })
: compaction.phase === "failed"
? t("thread.compaction.failed", { defaultValue: "Context compaction failed" })
: compaction.phase === "cancelled"
? t("thread.compaction.cancelled", { defaultValue: "Context compaction cancelled" })
: t("thread.compaction.succeeded", { defaultValue: "Context compacted" });
const Icon = compaction.phase === "started"
? LoaderCircle
: compaction.phase === "failed"
? CircleAlert
: Archive;
return (
<div
role={compaction.announce ? "status" : undefined}
aria-live={compaction.announce ? "polite" : undefined}
aria-busy={compaction.phase === "started"}
data-context-compaction={compaction.phase}
className="mx-auto flex w-full max-w-[49.5rem] items-center gap-2.5 py-1 text-xs text-muted-foreground"
>
<span
className={cn(
"flex size-6 shrink-0 items-center justify-center rounded-full bg-muted/60",
compaction.phase === "failed" && "text-destructive",
)}
>
<Icon
className={cn(
"size-3.5",
compaction.phase === "started" && "animate-spin motion-reduce:animate-none",
)}
aria-hidden
/>
</span>
<p className="min-w-0 font-medium leading-5 text-foreground/80">{title}</p>
</div>
);
}
@@ -21,6 +21,7 @@ import {
import { INLINE_TOKEN_HIGHLIGHT_COLOR } from "@/components/InlineTokenHighlight";
import {
Activity,
Archive,
ArrowUp,
BookOpen,
Brain,
@@ -235,6 +236,7 @@ interface ThreadComposerProps {
const COMMAND_ICONS: Record<string, LucideIcon> = {
activity: Activity,
archive: Archive,
"book-open": BookOpen,
brain: Brain,
"circle-help": CircleHelp,
@@ -39,6 +39,16 @@ export function assistantForkFlags(units: DisplayUnit[]): boolean[] {
hasLaterUnitBeforeUser = false;
continue;
}
if (
unit.type === "message"
&& unit.message.role === "assistant"
&& unit.message.kind === "compaction"
) {
// Compaction notices are session lifecycle markers, not assistant answers.
// They must neither expose nor displace the answer-level fork action.
flags[i] = false;
continue;
}
if (unit.type === "message" && unit.message.role === "assistant") {
flags[i] = !hasLaterUnitBeforeUser;
}