feat(webui): streamline contextual message controls (#5831)
This commit is contained in:
1 parent
99a7ec256f
commit
920efd00b8
24 files changed
+1776
-955
No files matched your search
@@ -1,9 +1,17 @@
|
||||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { memo, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { MoreHorizontal } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { MessageBubble } from "@/components/MessageBubble";
|
||||
import { AgentActivityCluster } from "@/components/thread/AgentActivityCluster";
|
||||
import { MessageBlockMenuActions, MessageBubble } from "@/components/MessageBubble";
|
||||
import { FallbackResponseSources } from "@/components/ResponseSourceBadge";
|
||||
import {
|
||||
AgentActivityCluster,
|
||||
completedActivityDurationMs,
|
||||
formatActivityDuration,
|
||||
} from "@/components/thread/AgentActivityCluster";
|
||||
import { AssistantSelectionAction } from "@/components/thread/AssistantSelectionAction";
|
||||
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
||||
import { projectActivityTimeline, type TurnUnit } from "@/lib/activity-timeline";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { CliAppInfo, McpPresetInfo, RetryStatus, SlashCommand, UIMessage } from "@/lib/types";
|
||||
|
||||
interface ThreadMessagesProps {
|
||||
@@ -25,6 +33,7 @@ interface ThreadMessagesProps {
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
onForkFromMessage?: (beforeUserIndex: number) => void;
|
||||
onQuoteSelection?: (text: string) => void;
|
||||
onActivityToggle?: () => void;
|
||||
}
|
||||
|
||||
export type DisplayUnit = TurnUnit;
|
||||
@@ -77,6 +86,7 @@ export function ThreadMessages({
|
||||
onOpenFilePreview,
|
||||
onForkFromMessage,
|
||||
onQuoteSelection,
|
||||
onActivityToggle,
|
||||
}: ThreadMessagesProps) {
|
||||
const { t } = useTranslation();
|
||||
const messageListRef = useRef<HTMLDivElement>(null);
|
||||
@@ -109,6 +119,44 @@ export function ThreadMessages({
|
||||
? activeTurnStartIndex(units, activeTurnId)
|
||||
: units.length;
|
||||
const unitKeys = useMemo(() => unitKeysForDisplay(units), [units]);
|
||||
const messageBlocks = useMemo(
|
||||
() => completedMessageBlocks(
|
||||
units,
|
||||
unitKeys,
|
||||
isStreaming,
|
||||
activeTurnId,
|
||||
currentTurnStartIndex,
|
||||
),
|
||||
[activeTurnId, currentTurnStartIndex, isStreaming, unitKeys, units],
|
||||
);
|
||||
const [expandedActivityKeys, setExpandedActivityKeys] = useState<Set<string>>(() => new Set());
|
||||
const [activeContextBlockKey, setActiveContextBlockKey] = useState<string | null>(null);
|
||||
const [openContextBlockKey, setOpenContextBlockKey] = useState<string | null>(null);
|
||||
const pointedContextBlockRef = useRef<string | null>(null);
|
||||
const setActivityExpanded = useCallback((key: string, expanded: boolean) => {
|
||||
onActivityToggle?.();
|
||||
setExpandedActivityKeys((current) => {
|
||||
if (current.has(key) === expanded) return current;
|
||||
const next = new Set(current);
|
||||
if (expanded) next.add(key);
|
||||
else next.delete(key);
|
||||
return next;
|
||||
});
|
||||
}, [onActivityToggle]);
|
||||
const setContextBlockActive = useCallback((key: string | null) => {
|
||||
setActiveContextBlockKey((current) => current === key ? current : key);
|
||||
setOpenContextBlockKey((current) => current !== null && current !== key ? null : current);
|
||||
}, []);
|
||||
const setContextBlockPointed = useCallback((key: string | null) => {
|
||||
pointedContextBlockRef.current = key;
|
||||
setContextBlockActive(key);
|
||||
}, [setContextBlockActive]);
|
||||
const setContextBlockFocused = useCallback((key: string | null) => {
|
||||
setContextBlockActive(key ?? pointedContextBlockRef.current);
|
||||
}, [setContextBlockActive]);
|
||||
const setContextBlockMenuOpen = useCallback((key: string, open: boolean) => {
|
||||
setOpenContextBlockKey((current) => open ? key : current === key ? null : current);
|
||||
}, []);
|
||||
let nextUserIndex = hiddenUserMessageCount;
|
||||
|
||||
return (
|
||||
@@ -118,16 +166,28 @@ export function ThreadMessages({
|
||||
onQuoteSelection={onQuoteSelection}
|
||||
/>
|
||||
{units.map((unit, index) => {
|
||||
const prev = units[index - 1];
|
||||
const marginTop =
|
||||
index > 0
|
||||
? marginAfterPrevUnit(prev)
|
||||
: "";
|
||||
const next = units[index + 1];
|
||||
const hasBodyBelow =
|
||||
unit.type === "activity"
|
||||
&& next?.type === "message"
|
||||
&& next.message.role === "assistant";
|
||||
const contextBlockKey = messageBlocks.blockKeys[index]
|
||||
?? (unit.type === "message" && unit.message.role === "user"
|
||||
? `message-block-${unitKeys[index]}`
|
||||
: undefined);
|
||||
const showBlockContext = messageBlocks.blockIndices.has(index);
|
||||
const blockActivity = messageBlocks.activityByBlock.get(index);
|
||||
const suppressActivity = messageBlocks.suppressedActivityIndices.has(index);
|
||||
const previousVisibleIndex = previousVisibleUnitIndex(
|
||||
index,
|
||||
messageBlocks.suppressedActivityIndices,
|
||||
);
|
||||
const marginTop = suppressActivity || previousVisibleIndex < 0
|
||||
? ""
|
||||
: marginAfterPrevUnit(units[previousVisibleIndex]);
|
||||
const blockActivityExpanded = showBlockContext
|
||||
&& contextBlockKey !== undefined
|
||||
&& expandedActivityKeys.has(contextBlockKey);
|
||||
const deferOffscreenRender =
|
||||
index < units.length - 1
|
||||
&& (
|
||||
@@ -150,7 +210,6 @@ export function ThreadMessages({
|
||||
? unit.message.turnId === activeTurnId
|
||||
: index > currentTurnStartIndex
|
||||
);
|
||||
const isThreadTail = index === units.length - 1;
|
||||
if (
|
||||
unit.type === "message"
|
||||
&& unit.message.role === "user"
|
||||
@@ -165,9 +224,18 @@ export function ThreadMessages({
|
||||
marginTop={marginTop}
|
||||
userPromptId={userPromptId}
|
||||
hasBodyBelow={hasBodyBelow}
|
||||
suppressActivity={suppressActivity}
|
||||
showBlockContext={showBlockContext}
|
||||
blockActivity={blockActivity}
|
||||
blockActivityExpanded={blockActivityExpanded}
|
||||
contextBlockKey={contextBlockKey}
|
||||
contextBlockActive={
|
||||
contextBlockKey !== undefined
|
||||
&& contextBlockKey === (openContextBlockKey ?? activeContextBlockKey)
|
||||
}
|
||||
contextBlockMenuOpen={contextBlockKey === openContextBlockKey}
|
||||
deferOffscreenRender={deferOffscreenRender}
|
||||
isTurnStreaming={unitTurnStreaming}
|
||||
isThreadTail={isThreadTail}
|
||||
retryStatus={
|
||||
unit.type === "activity" && liveActivityClusterIndices.has(index)
|
||||
? retryStatus
|
||||
@@ -184,11 +252,15 @@ export function ThreadMessages({
|
||||
onLoadTraceDetails={onLoadTraceDetails}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
onForkFromMessage={onForkFromMessage}
|
||||
onActivityExpandedChange={setActivityExpanded}
|
||||
onContextBlockActiveChange={setContextBlockPointed}
|
||||
onContextBlockFocusChange={setContextBlockFocused}
|
||||
onContextBlockMenuOpenChange={setContextBlockMenuOpen}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
{pendingActivity ? (
|
||||
<div className={units.length > 0 ? "mt-5" : undefined}>
|
||||
<div className={cn("thread-message-row", units.length > 0 && "mt-5")}>
|
||||
<AgentActivityCluster
|
||||
messages={[]}
|
||||
isTurnStreaming
|
||||
@@ -255,9 +327,15 @@ interface ThreadDisplayUnitProps {
|
||||
marginTop: string;
|
||||
userPromptId?: string;
|
||||
hasBodyBelow: boolean;
|
||||
suppressActivity: boolean;
|
||||
showBlockContext: boolean;
|
||||
blockActivity?: Extract<DisplayUnit, { type: "activity" }>;
|
||||
blockActivityExpanded: boolean;
|
||||
contextBlockKey?: string;
|
||||
contextBlockActive: boolean;
|
||||
contextBlockMenuOpen: boolean;
|
||||
deferOffscreenRender: boolean;
|
||||
isTurnStreaming: boolean;
|
||||
isThreadTail: boolean;
|
||||
retryStatus: RetryStatus | null;
|
||||
forkIndex?: number;
|
||||
showForkBoundary: boolean;
|
||||
@@ -270,6 +348,133 @@ interface ThreadDisplayUnitProps {
|
||||
onLoadTraceDetails?: (refs: string[]) => void | Promise<void>;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
onForkFromMessage?: (beforeUserIndex: number) => void;
|
||||
onActivityExpandedChange: (key: string, expanded: boolean) => void;
|
||||
onContextBlockActiveChange: (key: string | null) => void;
|
||||
onContextBlockFocusChange: (key: string | null) => void;
|
||||
onContextBlockMenuOpenChange: (key: string, open: boolean) => void;
|
||||
}
|
||||
|
||||
interface MessageBlockMenuProps {
|
||||
message: UIMessage;
|
||||
isTurnStreaming: boolean;
|
||||
contextBlockKey: string;
|
||||
contextActive: boolean;
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onContextActiveChange: (key: string | null) => void;
|
||||
onForkFromHere?: () => void;
|
||||
activity?: {
|
||||
label: string;
|
||||
expanded: boolean;
|
||||
controls: string;
|
||||
onToggle: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
function MessageBlockMenu({
|
||||
message,
|
||||
isTurnStreaming,
|
||||
contextBlockKey,
|
||||
contextActive,
|
||||
open,
|
||||
onOpenChange,
|
||||
onContextActiveChange,
|
||||
onForkFromHere,
|
||||
activity,
|
||||
}: MessageBlockMenuProps) {
|
||||
const { t } = useTranslation();
|
||||
const contentRef = useRef<HTMLDivElement>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const [triggerHeight, setTriggerHeight] = useState(28);
|
||||
useLayoutEffect(() => {
|
||||
const trigger = triggerRef.current;
|
||||
if (!open || !trigger) return;
|
||||
const measure = () => setTriggerHeight(trigger.getBoundingClientRect().height || 28);
|
||||
measure();
|
||||
if (typeof ResizeObserver === "undefined") return;
|
||||
const observer = new ResizeObserver(measure);
|
||||
observer.observe(trigger);
|
||||
return () => observer.disconnect();
|
||||
}, [open]);
|
||||
const contextActiveRef = useRef(contextActive);
|
||||
contextActiveRef.current = contextActive;
|
||||
const label = t("message.actions");
|
||||
return (
|
||||
<Popover open={open} onOpenChange={onOpenChange}>
|
||||
<PopoverTrigger asChild>
|
||||
<button
|
||||
ref={triggerRef}
|
||||
type="button"
|
||||
data-message-block-menu-trigger
|
||||
aria-label={label}
|
||||
className={cn(
|
||||
"message-block-menu-trigger group touch-target absolute -start-[var(--message-block-trigger-offset)] top-0 z-20",
|
||||
"inline-flex h-[var(--message-block-control-size)] w-[var(--message-block-control-size)] items-center justify-center text-muted-foreground/70",
|
||||
"transition-[color,opacity] hover:text-foreground focus-visible:outline-none",
|
||||
"motion-reduce:transform-none motion-reduce:transition-none",
|
||||
)}
|
||||
>
|
||||
<span
|
||||
data-message-block-menu-highlight
|
||||
className={cn(
|
||||
"inline-flex h-4 w-7 items-center justify-center rounded-full",
|
||||
"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 motion-reduce:transition-none",
|
||||
)}
|
||||
>
|
||||
<MoreHorizontal className="h-4 w-4" strokeWidth={1.75} aria-hidden />
|
||||
</span>
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
ref={contentRef}
|
||||
data-message-block-menu
|
||||
data-message-context-menu-block={contextBlockKey}
|
||||
side="bottom"
|
||||
align="end"
|
||||
sideOffset={-triggerHeight}
|
||||
collisionPadding={12}
|
||||
tabIndex={-1}
|
||||
aria-label={label}
|
||||
onKeyDownCapture={(event) => {
|
||||
if (event.key !== "Escape"
|
||||
|| !(event.target instanceof Node)
|
||||
|| !event.currentTarget.contains(event.target)) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onOpenChange(false);
|
||||
}}
|
||||
onOpenAutoFocus={(event) => {
|
||||
event.preventDefault();
|
||||
contentRef.current?.focus({ preventScroll: true });
|
||||
}}
|
||||
onCloseAutoFocus={(event) => {
|
||||
if (!contextActiveRef.current || activity?.expanded) event.preventDefault();
|
||||
}}
|
||||
onPointerEnter={(event) => {
|
||||
if (event.pointerType === "touch") return;
|
||||
onContextActiveChange(contextBlockKey);
|
||||
}}
|
||||
onPointerLeave={(event) => {
|
||||
if (event.pointerType === "touch") return;
|
||||
onContextActiveChange(contextBlockKeyForTarget(event.relatedTarget) ?? null);
|
||||
}}
|
||||
className={cn(
|
||||
"w-max max-w-64 overflow-y-auto outline-none",
|
||||
"shadow-[0_2px_10px_rgba(0,0,0,0.08)] dark:shadow-[0_2px_10px_rgba(0,0,0,0.24)]",
|
||||
)}
|
||||
>
|
||||
<MessageBlockMenuActions
|
||||
message={message}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
onForkFromHere={onForkFromHere}
|
||||
activity={activity}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
|
||||
const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
@@ -278,9 +483,15 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
marginTop,
|
||||
userPromptId,
|
||||
hasBodyBelow,
|
||||
suppressActivity,
|
||||
showBlockContext,
|
||||
blockActivity,
|
||||
blockActivityExpanded,
|
||||
contextBlockKey,
|
||||
contextBlockActive,
|
||||
contextBlockMenuOpen,
|
||||
deferOffscreenRender,
|
||||
isTurnStreaming,
|
||||
isThreadTail,
|
||||
retryStatus,
|
||||
forkIndex,
|
||||
showForkBoundary,
|
||||
@@ -293,8 +504,14 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
onLoadTraceDetails,
|
||||
onOpenFilePreview,
|
||||
onForkFromMessage,
|
||||
onActivityExpandedChange,
|
||||
onContextBlockActiveChange,
|
||||
onContextBlockFocusChange,
|
||||
onContextBlockMenuOpenChange,
|
||||
}: ThreadDisplayUnitProps) {
|
||||
const { t } = useTranslation();
|
||||
const elementRef = useRef<HTMLDivElement>(null);
|
||||
const activityDetailsId = useId();
|
||||
const heightRef = useRef(0);
|
||||
const [nearViewport, setNearViewport] = useState(true);
|
||||
const [interacted, setInteracted] = useState(false);
|
||||
@@ -317,43 +534,151 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
const onForkFromHere = useCallback(() => {
|
||||
if (forkIndex !== undefined) onForkFromMessage?.(forkIndex);
|
||||
}, [forkIndex, onForkFromMessage]);
|
||||
const onBlockActivityExpandedChange = useCallback((expanded: boolean) => {
|
||||
if (contextBlockKey !== undefined) {
|
||||
onActivityExpandedChange(contextBlockKey, expanded);
|
||||
window.setTimeout(() => {
|
||||
const focusTarget = elementRef.current?.querySelector<HTMLElement>(
|
||||
expanded
|
||||
? "[data-contextual-activity-collapse]"
|
||||
: "[data-message-block-menu-trigger]",
|
||||
);
|
||||
focusTarget?.focus({ preventScroll: true });
|
||||
}, 0);
|
||||
}
|
||||
}, [contextBlockKey, onActivityExpandedChange]);
|
||||
const activityDurationMs = blockActivity
|
||||
? completedActivityDurationMs(blockActivity.messages, blockActivity.turnLatencyMs)
|
||||
: 0;
|
||||
const activityLabel = blockActivity
|
||||
? activityDurationMs <= 0
|
||||
? t("message.activityWorked")
|
||||
: t("message.activityWorkedFor", {
|
||||
duration: formatActivityDuration(activityDurationMs),
|
||||
})
|
||||
: undefined;
|
||||
const onActivityMenuToggle = useCallback(() => {
|
||||
onBlockActivityExpandedChange(!blockActivityExpanded);
|
||||
if (contextBlockKey !== undefined) {
|
||||
onContextBlockMenuOpenChange(contextBlockKey, false);
|
||||
}
|
||||
}, [
|
||||
blockActivityExpanded,
|
||||
contextBlockKey,
|
||||
onBlockActivityExpandedChange,
|
||||
onContextBlockMenuOpenChange,
|
||||
]);
|
||||
const blockMenu = unit.type === "message" && contextBlockKey !== undefined ? (
|
||||
<MessageBlockMenu
|
||||
message={unit.message}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
contextBlockKey={contextBlockKey}
|
||||
contextActive={contextBlockActive}
|
||||
open={contextBlockMenuOpen}
|
||||
onOpenChange={(open) => onContextBlockMenuOpenChange(contextBlockKey, open)}
|
||||
onContextActiveChange={onContextBlockActiveChange}
|
||||
onForkFromHere={forkIndex !== undefined ? onForkFromHere : undefined}
|
||||
activity={activityLabel ? {
|
||||
label: activityLabel,
|
||||
expanded: blockActivityExpanded,
|
||||
controls: activityDetailsId,
|
||||
onToggle: onActivityMenuToggle,
|
||||
} : undefined}
|
||||
/>
|
||||
) : null;
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
ref={elementRef}
|
||||
className={marginTop}
|
||||
className={cn(
|
||||
"thread-message-row",
|
||||
marginTop,
|
||||
contextBlockKey !== undefined && "message-context-hit-area relative",
|
||||
)}
|
||||
style={retainContent ? undefined : { height: heightRef.current }}
|
||||
onPointerDownCapture={() => setInteracted(true)}
|
||||
onFocusCapture={() => setInteracted(true)}
|
||||
onPointerDownCapture={() => {
|
||||
setInteracted(true);
|
||||
onContextBlockActiveChange(contextBlockKey ?? null);
|
||||
}}
|
||||
data-thread-display-unit={unitKey}
|
||||
data-user-prompt-id={userPromptId}
|
||||
data-message-context-block={contextBlockKey}
|
||||
data-context-block-active={contextBlockActive || undefined}
|
||||
data-message-context-menu-open={contextBlockMenuOpen || undefined}
|
||||
onPointerEnter={(event) => {
|
||||
if (event.pointerType === "touch") return;
|
||||
onContextBlockActiveChange(contextBlockKey ?? null);
|
||||
}}
|
||||
onPointerLeave={(event) => {
|
||||
if (event.pointerType === "touch") return;
|
||||
onContextBlockActiveChange(contextBlockKeyForTarget(event.relatedTarget) ?? null);
|
||||
}}
|
||||
onFocusCapture={() => {
|
||||
setInteracted(true);
|
||||
onContextBlockFocusChange(contextBlockKey ?? null);
|
||||
}}
|
||||
onBlurCapture={(event) => {
|
||||
onContextBlockFocusChange(contextBlockKeyForTarget(event.relatedTarget) ?? null);
|
||||
}}
|
||||
>
|
||||
{retainContent ? unit.type === "activity" ? (
|
||||
<AgentActivityCluster
|
||||
messages={unit.messages}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
retryStatus={retryStatus}
|
||||
hasBodyBelow={hasBodyBelow}
|
||||
turnLatencyMs={unit.turnLatencyMs}
|
||||
startedAtMs={unit.startedAtMs}
|
||||
cliApps={cliApps}
|
||||
mcpPresets={mcpPresets}
|
||||
traceDetailScope={traceDetailScope}
|
||||
onLoadTraceDetails={onLoadTraceDetails}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
/>
|
||||
suppressActivity ? null : (
|
||||
<AgentActivityCluster
|
||||
messages={unit.messages}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
retryStatus={retryStatus}
|
||||
hasBodyBelow={hasBodyBelow}
|
||||
turnLatencyMs={unit.turnLatencyMs}
|
||||
startedAtMs={unit.startedAtMs}
|
||||
cliApps={cliApps}
|
||||
mcpPresets={mcpPresets}
|
||||
traceDetailScope={traceDetailScope}
|
||||
onLoadTraceDetails={onLoadTraceDetails}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
/>
|
||||
)
|
||||
) : (
|
||||
<MessageBubble
|
||||
message={unit.message}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
isThreadTail={isThreadTail}
|
||||
temporary={temporary}
|
||||
cliApps={cliApps}
|
||||
mcpPresets={mcpPresets}
|
||||
slashCommands={slashCommands}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
onForkFromHere={forkIndex !== undefined ? onForkFromHere : undefined}
|
||||
/>
|
||||
<div className={unit.message.role === "assistant" ? "relative" : undefined}>
|
||||
{unit.message.role === "assistant" ? blockMenu : null}
|
||||
{showBlockContext && blockActivity ? (
|
||||
<div className={blockActivityExpanded ? "mb-2" : undefined}>
|
||||
<AgentActivityCluster
|
||||
messages={blockActivity.messages}
|
||||
isTurnStreaming={false}
|
||||
retryStatus={null}
|
||||
hasBodyBelow={false}
|
||||
expanded={blockActivityExpanded}
|
||||
onExpandedChange={onBlockActivityExpandedChange}
|
||||
hideHeader
|
||||
detailsId={activityDetailsId}
|
||||
turnLatencyMs={blockActivity.turnLatencyMs}
|
||||
startedAtMs={blockActivity.startedAtMs}
|
||||
cliApps={cliApps}
|
||||
mcpPresets={mcpPresets}
|
||||
traceDetailScope={traceDetailScope}
|
||||
onLoadTraceDetails={onLoadTraceDetails}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<MessageBubble
|
||||
message={unit.message}
|
||||
temporary={temporary}
|
||||
cliApps={cliApps}
|
||||
mcpPresets={mcpPresets}
|
||||
slashCommands={slashCommands}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
contextMenu={unit.message.role === "user" ? blockMenu : undefined}
|
||||
/>
|
||||
{unit.message.role === "assistant"
|
||||
&& unit.message.kind !== "compaction"
|
||||
&& contextBlockKey === undefined ? (
|
||||
<FallbackResponseSources
|
||||
sources={unit.message.responseSources}
|
||||
className="relative mt-0.5 text-muted-foreground"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{showForkBoundary ? <ForkBoundaryDivider label={forkBoundaryLabel} /> : null}
|
||||
@@ -370,9 +695,15 @@ function threadDisplayUnitPropsEqual(
|
||||
&& previous.marginTop === next.marginTop
|
||||
&& previous.userPromptId === next.userPromptId
|
||||
&& previous.hasBodyBelow === next.hasBodyBelow
|
||||
&& previous.suppressActivity === next.suppressActivity
|
||||
&& previous.showBlockContext === next.showBlockContext
|
||||
&& previous.blockActivity === next.blockActivity
|
||||
&& previous.blockActivityExpanded === next.blockActivityExpanded
|
||||
&& previous.contextBlockKey === next.contextBlockKey
|
||||
&& previous.contextBlockActive === next.contextBlockActive
|
||||
&& previous.contextBlockMenuOpen === next.contextBlockMenuOpen
|
||||
&& previous.deferOffscreenRender === next.deferOffscreenRender
|
||||
&& previous.isTurnStreaming === next.isTurnStreaming
|
||||
&& previous.isThreadTail === next.isThreadTail
|
||||
&& previous.retryStatus === next.retryStatus
|
||||
&& previous.forkIndex === next.forkIndex
|
||||
&& previous.showForkBoundary === next.showForkBoundary
|
||||
@@ -385,9 +716,21 @@ function threadDisplayUnitPropsEqual(
|
||||
&& previous.onLoadTraceDetails === next.onLoadTraceDetails
|
||||
&& previous.onOpenFilePreview === next.onOpenFilePreview
|
||||
&& previous.onForkFromMessage === next.onForkFromMessage
|
||||
&& previous.onActivityExpandedChange === next.onActivityExpandedChange
|
||||
&& previous.onContextBlockActiveChange === next.onContextBlockActiveChange
|
||||
&& previous.onContextBlockFocusChange === next.onContextBlockFocusChange
|
||||
&& previous.onContextBlockMenuOpenChange === next.onContextBlockMenuOpenChange
|
||||
);
|
||||
}
|
||||
|
||||
function contextBlockKeyForTarget(target: EventTarget | null): string | undefined {
|
||||
if (!(target instanceof Element)) return undefined;
|
||||
const owner = target.closest<HTMLElement>(
|
||||
"[data-message-context-block], [data-message-context-menu-block]",
|
||||
);
|
||||
return owner?.dataset.messageContextBlock ?? owner?.dataset.messageContextMenuBlock;
|
||||
}
|
||||
|
||||
function activeTurnStartIndex(units: DisplayUnit[], activeTurnId: string | null): number {
|
||||
if (activeTurnId) {
|
||||
const index = units.findIndex((unit) => (
|
||||
@@ -452,7 +795,7 @@ function unitIndexAfterMessageCount(
|
||||
|
||||
function ForkBoundaryDivider({ label }: { label: string }) {
|
||||
return (
|
||||
<div className="my-5 flex items-center gap-3 text-[11px] text-muted-foreground/80">
|
||||
<div className="thread-message-row my-5 flex items-center gap-3 text-[11px] text-muted-foreground/80">
|
||||
<span aria-hidden className="h-px flex-1 bg-border/70" />
|
||||
<span className="shrink-0">{label}</span>
|
||||
<span aria-hidden className="h-px flex-1 bg-border/70" />
|
||||
@@ -528,7 +871,9 @@ function stableTurnMessageKey(message: UIMessage | undefined, fallbackPhase?: st
|
||||
return `turn-${message.turnId}-${phase}`;
|
||||
}
|
||||
|
||||
function marginAfterPrevUnit(prev: DisplayUnit): string {
|
||||
function marginAfterPrevUnit(
|
||||
prev: DisplayUnit,
|
||||
): string {
|
||||
if (prev.type === "activity") {
|
||||
return "mt-4";
|
||||
}
|
||||
@@ -543,5 +888,119 @@ function marginAfterPrevUnit(prev: DisplayUnit): string {
|
||||
if (denseP) {
|
||||
return "mt-2";
|
||||
}
|
||||
if (p.role === "assistant" && !p.isStreaming && p.content.trim().length > 0) {
|
||||
return "mt-5";
|
||||
}
|
||||
return "mt-5";
|
||||
}
|
||||
|
||||
interface CompletedMessageBlocks {
|
||||
blockKeys: Array<string | undefined>;
|
||||
blockIndices: Set<number>;
|
||||
suppressedActivityIndices: Set<number>;
|
||||
activityByBlock: Map<number, Extract<DisplayUnit, { type: "activity" }>>;
|
||||
}
|
||||
|
||||
function completedMessageBlocks(
|
||||
units: DisplayUnit[],
|
||||
unitKeys: string[],
|
||||
isStreaming: boolean,
|
||||
activeTurnId: string | null,
|
||||
currentTurnStartIndex: number,
|
||||
): CompletedMessageBlocks {
|
||||
const result: CompletedMessageBlocks = {
|
||||
blockKeys: new Array<string | undefined>(units.length),
|
||||
blockIndices: new Set<number>(),
|
||||
suppressedActivityIndices: new Set<number>(),
|
||||
activityByBlock: new Map(),
|
||||
};
|
||||
let groupStart = 0;
|
||||
let groupTurnId: string | undefined;
|
||||
|
||||
const flushGroup = (end: number) => {
|
||||
if (groupStart >= end) return;
|
||||
const indices = Array.from({ length: end - groupStart }, (_, offset) => groupStart + offset);
|
||||
const groupIsStreaming = isStreaming && indices.some((index) => {
|
||||
const unit = units[index];
|
||||
const turnId = displayUnitTurnId(unit);
|
||||
if (activeTurnId && turnId) return turnId === activeTurnId;
|
||||
return index > currentTurnStartIndex;
|
||||
});
|
||||
if (groupIsStreaming) return;
|
||||
|
||||
const blockIndices = indices.filter((index) => {
|
||||
const unit = units[index];
|
||||
return unit.type === "message"
|
||||
&& unit.message.role === "assistant"
|
||||
&& unit.message.kind !== "compaction";
|
||||
});
|
||||
for (const index of blockIndices) {
|
||||
result.blockIndices.add(index);
|
||||
result.blockKeys[index] = `message-block-${unitKeys[index]}`;
|
||||
}
|
||||
|
||||
const activityByBlock = new Map<number, Array<Extract<DisplayUnit, { type: "activity" }>>>();
|
||||
for (const index of indices) {
|
||||
const unit = units[index];
|
||||
if (unit.type !== "activity") continue;
|
||||
const blockIndex = blockIndices.find((candidate) => candidate > index) ?? blockIndices.at(-1);
|
||||
if (blockIndex === undefined) continue;
|
||||
result.suppressedActivityIndices.add(index);
|
||||
const activityUnits = activityByBlock.get(blockIndex) ?? [];
|
||||
activityUnits.push(unit);
|
||||
activityByBlock.set(blockIndex, activityUnits);
|
||||
}
|
||||
for (const [blockIndex, activityUnits] of activityByBlock) {
|
||||
result.activityByBlock.set(blockIndex, mergeActivityUnits(activityUnits));
|
||||
}
|
||||
};
|
||||
|
||||
for (let index = 0; index < units.length; index += 1) {
|
||||
const unit = units[index];
|
||||
if (unit.type === "message" && unit.message.role === "user") {
|
||||
flushGroup(index);
|
||||
groupStart = index + 1;
|
||||
groupTurnId = unit.message.turnId;
|
||||
continue;
|
||||
}
|
||||
const turnId = displayUnitTurnId(unit);
|
||||
if (turnId && groupTurnId && turnId !== groupTurnId) {
|
||||
flushGroup(index);
|
||||
groupStart = index;
|
||||
groupTurnId = turnId;
|
||||
} else if (turnId && !groupTurnId) {
|
||||
groupTurnId = turnId;
|
||||
}
|
||||
}
|
||||
flushGroup(units.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
function mergeActivityUnits(
|
||||
units: Array<Extract<DisplayUnit, { type: "activity" }>>,
|
||||
): Extract<DisplayUnit, { type: "activity" }> {
|
||||
return {
|
||||
type: "activity",
|
||||
messages: units.flatMap((unit) => unit.messages),
|
||||
sourceMessageCount: units.reduce((count, unit) => count + unit.sourceMessageCount, 0),
|
||||
turnLatencyMs: [...units].reverse().find((unit) => unit.turnLatencyMs !== undefined)
|
||||
?.turnLatencyMs,
|
||||
startedAtMs: units.find((unit) => unit.startedAtMs !== undefined)?.startedAtMs,
|
||||
};
|
||||
}
|
||||
|
||||
function displayUnitTurnId(unit: DisplayUnit): string | undefined {
|
||||
return unit.type === "activity"
|
||||
? unit.messages.find((message) => message.turnId)?.turnId
|
||||
: unit.message.turnId;
|
||||
}
|
||||
|
||||
function previousVisibleUnitIndex(
|
||||
index: number,
|
||||
suppressedActivityIndices: ReadonlySet<number>,
|
||||
): number {
|
||||
for (let previous = index - 1; previous >= 0; previous -= 1) {
|
||||
if (!suppressedActivityIndices.has(previous)) return previous;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
Reference in new issue
Block a user