feat(webui): streamline contextual message controls (#5831)

This commit is contained in:
chengyongru authored and GitHub committed 2026-09-22 16:37:35 +08:00
1 parent 99a7ec256f
commit 920efd00b8
24 files changed
+1776 -955

No files matched your search

+216 -241
View File
@@ -5,10 +5,10 @@ import {
useMemo,
useRef,
useState,
type ComponentPropsWithoutRef,
type ReactNode,
} from "react";
import {
Activity,
Check,
ChevronRight,
CircleAlert,
@@ -23,7 +23,7 @@ import { DisclosureContent } from "@/components/ui/disclosure";
import { AttachmentTile } from "@/components/AttachmentTile";
import { SessionHandleLabel } from "@/components/SessionHandleLabel";
import { ResponseSourceBadge } from "@/components/ResponseSourceBadge";
import { FallbackResponseSources } from "@/components/ResponseSourceBadge";
import { ImageLightbox } from "@/components/ImageLightbox";
import { MarkdownText } from "@/components/MarkdownText";
import { SlashCommandText } from "@/components/SlashCommandText";
@@ -38,10 +38,7 @@ import {
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { copyTextToClipboard } from "@/lib/clipboard";
import {
fmtDateTime,
formatMessageEndTime,
} from "@/lib/format";
import { formatMessageEndTime } from "@/lib/format";
import { toMediaAttachment } from "@/lib/media";
import { matchingSlashCommand } from "@/lib/slash-command";
import { sessionHandleColor } from "@/lib/session-handle";
@@ -61,19 +58,14 @@ import type {
interface MessageBubbleProps {
message: UIMessage;
/** The containing agent turn has not received turn_end yet. */
isTurnStreaming?: boolean;
/** Give temporary-chat user turns the dashed private-mode treatment. */
temporary?: boolean;
/** When false, hide this message's copy button. Default true. */
showCopyAction?: boolean;
/** Whether this message is the final display unit in the thread. */
isThreadTail?: boolean;
cliApps?: CliAppInfo[];
mcpPresets?: McpPresetInfo[];
slashCommands?: SlashCommand[];
onOpenFilePreview?: (path: string) => void;
onForkFromHere?: () => void;
/** Context-menu trigger positioned against this message's visual block. */
contextMenu?: ReactNode;
}
function ForkArrowIcon({ className }: { className?: string }) {
@@ -96,43 +88,7 @@ function ForkArrowIcon({ className }: { className?: string }) {
);
}
type MessageTimestampProps = Omit<
ComponentPropsWithoutRef<"time">,
"dateTime" | "title"
> & {
timestamp: number;
tooltipLabel: string;
};
function MessageTimestamp({
timestamp,
tooltipLabel,
className,
children,
...props
}: MessageTimestampProps) {
return (
<Tooltip>
<TooltipTrigger asChild>
<time
{...props}
dateTime={new Date(timestamp).toISOString()}
tabIndex={0}
className={cn(
"cursor-help text-[11px] leading-none text-muted-foreground/70 tabular-nums",
"focus-visible:rounded-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
className,
)}
>
{children}
</time>
</TooltipTrigger>
<TooltipContent side="top" align="center">{tooltipLabel}</TooltipContent>
</Tooltip>
);
}
function MessageCopyButton({ content }: { content: string }) {
function useMessageCopy(content: string) {
const { t } = useTranslation();
const [copied, setCopied] = useState(false);
const copyResetRef = useRef<number | null>(null);
@@ -160,28 +116,199 @@ function MessageCopyButton({ content }: { content: string }) {
}, [content]);
const label = copied ? t("message.copiedReply") : t("message.copyReply");
return { copied, label, onCopy };
}
interface MessageBlockMenuActivity {
label: string;
expanded: boolean;
controls: string;
onToggle: () => void;
}
interface MessageBlockMenuActionsProps {
message: UIMessage;
isTurnStreaming?: boolean;
onForkFromHere?: () => void;
activity?: MessageBlockMenuActivity;
}
/** Actions and metadata shown after opening a visual message block's context panel. */
export function MessageBlockMenuActions({
message,
isTurnStreaming = false,
onForkFromHere,
activity,
}: MessageBlockMenuActionsProps) {
const { t } = useTranslation();
const content = message.role === "assistant"
? message.compactReply === "empty"
? t("thread.compaction.empty")
: message.compactReply === "failed"
? t("thread.compaction.failed")
: message.content
: message.content;
const hasText = content.trim().length > 0;
const { copied, label: copyLabel, onCopy } = useMessageCopy(content);
const showFork = message.role === "assistant"
&& !message.isStreaming
&& !isTurnStreaming
&& hasText
&& onForkFromHere !== undefined;
const timestamp = message.role === "assistant"
&& typeof message.completedAt === "number"
&& Number.isFinite(message.completedAt)
? message.completedAt
: message.createdAt;
const timestampLabel = typeof timestamp === "number" && Number.isFinite(timestamp)
? formatMessageEndTime(timestamp)
: "";
const automationSourceLabel = message.role === "assistant"
&& ["cron", "local_trigger", "trigger"].includes(message.source?.kind ?? "")
? message.source?.label?.trim() || t("message.automationSourceFallback")
: "";
return (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={onCopy}
aria-label={label}
className={cn(
"touch-target inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full",
"transition-colors hover:bg-muted/55 hover:text-foreground",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
)}
>
{copied ? (
<Check className="h-4 w-4" aria-hidden />
) : (
<Copy className="h-4 w-4" aria-hidden />
)}
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">{label}</TooltipContent>
</Tooltip>
<TooltipProvider>
<div
data-message-block-menu-actions
className="flex w-max max-w-full flex-col items-start gap-1"
>
{hasText || showFork || activity ? (
<div
data-message-block-toolbar
className="flex w-full flex-wrap items-center gap-x-1 gap-y-1"
>
{hasText || showFork ? (
<div className="flex min-h-[var(--message-block-control-size)] items-center gap-0.5">
{hasText ? (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
data-message-block-copy-action
data-assistant-copy-action={message.role === "assistant" || undefined}
onClick={onCopy}
aria-label={copyLabel}
className={cn(
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-action-width)] items-center justify-center rounded-control",
"text-muted-foreground transition-[color,background-color,scale]",
"hover:bg-muted/70 hover:text-foreground active:scale-[0.96]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
"motion-reduce:transform-none",
)}
>
{copied ? (
<Check
className="h-3.5 w-3.5 -translate-x-px"
strokeWidth={1.75}
aria-hidden
/>
) : (
<Copy
className="h-3.5 w-3.5 -translate-x-px"
strokeWidth={1.75}
aria-hidden
/>
)}
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">{copyLabel}</TooltipContent>
</Tooltip>
) : null}
{showFork ? (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
data-message-block-fork-action
data-assistant-fork-action
onClick={onForkFromHere}
aria-label={t("message.forkFromHere")}
className={cn(
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-action-width)] items-center justify-center rounded-control",
"text-muted-foreground transition-[color,background-color,scale]",
"hover:bg-muted/70 hover:text-foreground active:scale-[0.96]",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
"motion-reduce:transform-none",
)}
>
<ForkArrowIcon className="h-3.5 w-3.5" />
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">
{t("message.forkFromHere")}
</TooltipContent>
</Tooltip>
) : null}
</div>
) : null}
{activity ? (
<button
type="button"
data-message-block-activity-action
onClick={activity.onToggle}
aria-expanded={activity.expanded}
aria-controls={activity.controls}
className={cn(
"group inline-flex min-h-[var(--message-block-control-size)] self-start items-center gap-1.5 whitespace-nowrap",
"text-start text-[11px] leading-4",
"text-muted-foreground transition-colors hover:text-foreground",
"focus-visible:outline-none",
)}
>
<span
data-message-block-activity-icon
className={cn(
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-action-width)] shrink-0 items-center justify-center rounded-control",
"transition-[background-color,box-shadow,scale]",
"group-hover:bg-muted/70 group-active:scale-[0.96]",
"group-focus-visible:ring-2 group-focus-visible:ring-ring",
"motion-reduce:transform-none",
)}
>
<Activity className="h-3.5 w-3.5" strokeWidth={1.5} aria-hidden />
</span>
<span>{activity.label}</span>
</button>
) : null}
</div>
) : null}
<FallbackResponseSources
sources={message.role === "assistant" ? message.responseSources : undefined}
className="min-h-[var(--message-block-control-size)] gap-1"
badgeClassName="h-[var(--message-block-control-size)] min-h-[var(--message-block-control-size)]"
/>
{timestampLabel || automationSourceLabel ? (
<div
data-message-block-metadata
className="mt-0.5 w-full border-t border-border/45 px-1.5 pt-1 text-[10px] leading-4 text-muted-foreground/45"
>
{timestampLabel ? (
<time
data-message-timestamp
data-message-created-at={message.role === "user" || undefined}
data-assistant-completed-at={
message.role === "assistant" && timestamp === message.completedAt || undefined
}
dateTime={new Date(timestamp).toISOString()}
className="flex min-h-[var(--message-block-control-size)] items-center tabular-nums"
>
{timestampLabel}
</time>
) : null}
{automationSourceLabel ? (
<span
data-automation-trigger
className="flex min-h-[var(--message-block-control-size)] items-center break-words"
>
{t("message.automationTriggered")} · {automationSourceLabel}
</span>
) : null}
</div>
) : null}
</div>
</TooltipProvider>
);
}
@@ -272,24 +399,24 @@ function UserDeliveryStatus({
function IncomingSessionMessage({
message,
showCopyAction,
contextMenu,
onOpenFilePreview,
}: {
message: UIMessage;
showCopyAction: boolean;
contextMenu?: ReactNode;
onOpenFilePreview?: (path: string) => void;
}) {
const handle = message.sessionMessage!.session;
const color = sessionHandleColor(handle.id);
const createdAtLabel = formatMessageEndTime(message.createdAt);
const handleName = `@${handle.name}`;
return (
<div
data-session-message
className="group w-full text-[15px]"
className="group relative w-full text-[15px]"
style={{ lineHeight: "var(--cjk-line-height)" }}
>
{contextMenu}
<div
className="min-w-0 rounded-es-[16px] border-s-2 bg-background pb-1 ps-2.5"
style={{ borderInlineStartColor: color }}
@@ -306,23 +433,6 @@ function IncomingSessionMessage({
</MarkdownText>
</div>
</div>
{createdAtLabel || showCopyAction ? (
<TooltipProvider>
<div
className="message-actions mt-1 flex min-h-8 items-center gap-1.5 text-muted-foreground"
>
{showCopyAction ? <MessageCopyButton content={message.content} /> : null}
{createdAtLabel ? (
<MessageTimestamp
timestamp={message.createdAt}
tooltipLabel={fmtDateTime(message.createdAt)}
>
{createdAtLabel}
</MessageTimestamp>
) : null}
</div>
</TooltipProvider>
) : null}
</div>
);
}
@@ -330,15 +440,12 @@ function IncomingSessionMessage({
/** Render user turns as compact bubbles and assistant turns as document-like prose. */
export function MessageBubble({
message,
isTurnStreaming = false,
temporary = false,
showCopyAction = true,
isThreadTail = true,
cliApps = [],
mcpPresets = [],
slashCommands = [],
onOpenFilePreview,
onForkFromHere,
contextMenu,
}: MessageBubbleProps) {
const { t } = useTranslation();
const mentionCliApps = useMemo(
@@ -362,7 +469,7 @@ export function MessageBubble({
return (
<IncomingSessionMessage
message={message}
showCopyAction={showCopyAction}
contextMenu={contextMenu}
onOpenFilePreview={onOpenFilePreview}
/>
);
@@ -378,9 +485,6 @@ export function MessageBubble({
const hasText = userContent.trim().length > 0;
const showDeliveryStatus =
message.deliveryStatus === "sending" || message.deliveryStatus === "failed";
const createdAtLabel = formatMessageEndTime(message.createdAt);
const showCreatedAt = createdAtLabel.length > 0;
const createdAtTitle = showCreatedAt ? fmtDateTime(message.createdAt) : "";
const quotedContext = parsedMessage.quotedContext;
const slashCommand = matchingSlashCommand(userContent, slashCommands);
const messageText = slashCommand ? (
@@ -402,7 +506,11 @@ export function MessageBubble({
/>
);
return (
<div className="group ml-auto flex max-w-[min(85%,36rem)] flex-col items-end gap-1.5">
<div
data-user-text-bubble={hasText && !hasImages && !hasMedia && !quotedContext || undefined}
className="group relative ml-auto flex w-fit max-w-[min(85%,36rem)] flex-col items-end gap-1.5"
>
{contextMenu}
{hasImages ? <UserImages images={images} align="right" /> : null}
{!hasImages && hasMedia ? (
<MessageMedia media={media} align="right" />
@@ -427,23 +535,13 @@ export function MessageBubble({
{messageText}
</p>
) : null}
{showDeliveryStatus || showCreatedAt || (hasText && showCopyAction) ? (
{showDeliveryStatus ? (
<TooltipProvider>
<div className="flex min-h-8 items-center justify-end gap-1.5 text-muted-foreground">
{showCreatedAt ? (
<MessageTimestamp
data-message-created-at
timestamp={message.createdAt}
tooltipLabel={createdAtTitle}
>
{createdAtLabel}
</MessageTimestamp>
) : null}
<UserDeliveryStatus
status={message.deliveryStatus}
errorKind={message.deliveryErrorKind}
/>
{hasText && showCopyAction ? <MessageCopyButton content={message.content} /> : null}
</div>
</TooltipProvider>
) : null}
@@ -461,54 +559,13 @@ export function MessageBubble({
const reasoning = message.role === "assistant" ? message.reasoning ?? "" : "";
const reasoningStreaming = !!(message.role === "assistant" && message.reasoningStreaming);
const hasReasoning = reasoning.length > 0 || reasoningStreaming;
const automationSourceKind = message.source?.kind;
const automationSourceName = message.source?.label?.trim();
const automationSourceLabel = (
automationSourceKind === "cron"
|| automationSourceKind === "local_trigger"
|| automationSourceKind === "trigger"
)
? (automationSourceName || t("message.automationSourceFallback"))
: "";
const automationTriggeredLabel = t("message.automationTriggered");
const showAssistantActions =
message.role === "assistant" && !message.isStreaming && !isTurnStreaming && !empty;
const showCopyButton = showCopyAction && showAssistantActions;
const showForkButton = showAssistantActions && !!onForkFromHere;
const forkLabel = t("message.forkFromHere");
const completedAt = message.completedAt;
const completedAtLabel =
message.role === "assistant" && !message.isStreaming
? formatMessageEndTime(completedAt)
: "";
const assistantTimestamp =
typeof completedAt === "number" && Number.isFinite(completedAt)
? completedAt
: message.createdAt;
const assistantTimestampLabel =
message.role === "assistant" && !message.isStreaming && !isTurnStreaming
? formatMessageEndTime(assistantTimestamp)
: "";
const showCompletedAt =
completedAtLabel.length > 0
&& (!empty || hasReasoning || media.length > 0);
const showAssistantTimestamp =
assistantTimestampLabel.length > 0
&& (!empty || hasReasoning || media.length > 0);
const assistantTimestampTitle = showAssistantTimestamp ? fmtDateTime(assistantTimestamp) : "";
const showAutomationTrigger = showAssistantTimestamp && automationSourceLabel.length > 0;
const fallbackSources = message.responseSources?.filter((source) => source.fallback === true) ?? [];
const showAssistantFooterRow =
showCopyButton || showForkButton || showAssistantTimestamp || fallbackSources.length > 0;
const hasAssistantFooterContent =
message.role === "assistant"
&& (!empty || hasReasoning || media.length > 0);
const showAssistantFooterSlot =
hasAssistantFooterContent
&& (showAssistantFooterRow || ((message.isStreaming || isTurnStreaming) && isThreadTail));
return (
<div className="w-full text-[15px]" style={{ lineHeight: "var(--cjk-line-height)" }}>
<div
data-assistant-message
className="group/assistant relative w-full text-[15px]"
style={{ lineHeight: "var(--cjk-line-height)" }}
>
{contextMenu}
{hasReasoning ? (
<ReasoningBubble
text={reasoning}
@@ -533,68 +590,6 @@ export function MessageBubble({
{media.length > 0 ? <MessageMedia media={media} align="left" /> : null}
</>
)}
{showAssistantFooterSlot ? (
<TooltipProvider>
<div
data-assistant-footer
data-state={showAssistantFooterRow ? "visible" : "reserved"}
aria-hidden={showAssistantFooterRow ? undefined : true}
className={cn(
"message-actions mt-2 flex min-h-8 flex-wrap items-center gap-x-2 gap-y-1 text-muted-foreground",
"transition-opacity duration-300 ease-out motion-reduce:transition-none",
showAssistantFooterRow
? "opacity-100"
: "pointer-events-none opacity-0",
)}
>
{showCopyButton ? (
<MessageCopyButton content={assistantContent} />
) : null}
{showForkButton ? (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={onForkFromHere}
aria-label={forkLabel}
className={cn(
"touch-target inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full",
"transition-colors hover:bg-muted/55 hover:text-foreground",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
)}
>
<ForkArrowIcon className="h-4 w-4" />
</button>
</TooltipTrigger>
<TooltipContent side="top" align="center">{forkLabel}</TooltipContent>
</Tooltip>
) : null}
{showAssistantTimestamp ? (
<MessageTimestamp
{...(showCompletedAt ? { "data-assistant-completed-at": true } : {})}
data-message-timestamp
timestamp={assistantTimestamp}
tooltipLabel={assistantTimestampTitle}
>
{assistantTimestampLabel}
</MessageTimestamp>
) : null}
{fallbackSources.length > 0 ? (
<div className="flex min-w-0 max-w-full flex-wrap items-center gap-x-2 gap-y-1">
{fallbackSources.map((source) => (
<ResponseSourceBadge key={JSON.stringify(source)} source={source} />
))}
</div>
) : null}
{showAutomationTrigger ? (
<AutomationTriggerMeta
label={automationTriggeredLabel}
sourceLabel={automationSourceLabel}
/>
) : null}
</div>
</TooltipProvider>
) : null}
</div>
);
}
@@ -616,26 +611,6 @@ function UserQuotedContext({ text, label }: { text: string; label: string }) {
);
}
function AutomationTriggerMeta({ label, sourceLabel }: { label: string; sourceLabel: string }) {
return (
<Tooltip>
<TooltipTrigger asChild>
<span
data-automation-trigger
tabIndex={0}
className={cn(
"shrink-0 cursor-help text-[11px] leading-none text-muted-foreground/70 tabular-nums",
"focus-visible:rounded-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
)}
>
{label}
</span>
</TooltipTrigger>
<TooltipContent side="top" align="center">{sourceLabel}</TooltipContent>
</Tooltip>
);
}
function mergeMcpMentionPresets(
presets: McpPresetInfo[],
attachments: UIMcpPresetAttachment[] | undefined,