Files
t3code/apps/web/src/components/chat/MessagesTimeline.tsx
T

5939 lines
209 KiB
TypeScript

import { ThreadFindTimelineContext } from "./ThreadFindProvider";
import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";
import { MarkdownFindContext, useFindRevealRef } from "./markdownFindContext";
import { ComputerUseAppIcon } from "~/components/Icons";
import { useChatCanvas } from "./ChatCanvasContext";
import { WorkLogBlock, WorkLogButton, WorkLogDetails, WorkLogList, WorkLogRow } from "./WorkLog";
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview";
import { useRightPanelStore } from "~/rightPanelStore";
import {
getQuestionAnswerPreview,
getQuestionAnswerText,
getQuestionTextPreview,
hasQuestionAnswer,
} from "@t3tools/client-runtime/work-log/user-input";
import {
deriveTimelineMinimapItems,
resolveTimelineMinimapPreview,
type TimelineMinimapItem,
} from "./timelineMinimapItems";
import {
COMPOSER_CONTEXT_KINDS,
ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
type AssistantCitation,
type EnvironmentId,
type MessageId,
type OrchestrationV2TurnItem,
type RunAttemptId,
type ScopedThreadRef,
type ServerProvider,
type ServerProviderSkill,
type RunId,
ThreadId,
type ToolActivityIcon,
} from "@t3tools/contracts";
import { parseScopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
import { useAtomValue } from "@effect/atom-react";
import { environmentThreadDetails } from "../../state/threads";
import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
import { Link } from "@tanstack/react-router";
import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows";
import { notificationChildThreadId } from "@t3tools/client-runtime/state/thread-execution";
import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
import {
liveThoughtLine,
resolveWorkEntryToolPresentation,
resolveViewedImageAsset,
workEntryViewedImagePath,
} from "@t3tools/client-runtime/work-log/presentation";
import { resolveWorkGroupScrollAnchor } from "@t3tools/client-runtime/work-log/scroll-anchor";
import {
turnItemHasDetail,
turnItemNeedsDetailFetch,
} from "@t3tools/client-runtime/work-log/item-detail";
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
import {
subagentGroupSummary,
summarizeSubagentStatuses,
} from "@t3tools/client-runtime/state/subagent-display";
import { observeResize } from "~/lib/observeResize";
const NOOP_USE_ARTIFACT_TEMPLATE = () => {};
const NOOP_OPEN_ATTACHMENT = (_attachment: ChatFileAttachment) => {};
import { resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";
import { toolActivityFaviconUrl } from "@t3tools/shared/favicon";
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
import { getProjectFaviconCacheKey } from "@t3tools/shared/projectFavicon";
import { claudeSkillInvocation } from "@t3tools/shared/toolActivity";
import { observeVisibleAnimation } from "../../lib/visibleAnimation";
import {
createContext,
memo,
use,
useCallback,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
type KeyboardEvent,
type MouseEvent,
type ReactNode,
} from "react";
import {
LegendList,
type LegendListRef,
type MaintainScrollAtEndOptions,
} from "@legendapp/list/react";
import { FileDiff } from "@pierre/diffs/react";
import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
import {
type TimelineEntry,
providerErrorPresentation,
createMessageAttachmentPreviewProjector,
selectMessageImageResources,
workEntryDisplayIndicatesToolFailure,
workEntrySignalsSevereFailure,
workLogEntryIsToolLike,
} from "../../session-logic";
import type { CodexArtifactTemplate } from "@t3tools/shared/codexArtifactTemplates";
import {
type ChatMessage,
type ChatFileAttachment,
type ChatImageAttachment,
isFileAttachment,
isImageAttachment,
isVideoAttachment,
type TurnDiffSummary,
} from "../../types";
import {
getRenderablePatch,
resolveDiffThemeName,
resolveFileDiffPath,
} from "../../lib/diffRendering";
import { PREFERRED_HIGHLIGHTER } from "../../lib/syntaxHighlighting";
import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import type { Root, RootContent } from "mdast";
import { T3Wordmark } from "../T3Wordmark";
import { ThreadContextChip } from "../ThreadContextChip";
import {
BotIcon,
BrainIcon,
CheckIcon,
ChevronDownIcon,
ChevronRightIcon,
ChevronUpIcon,
CircleAlertIcon,
DownloadIcon,
EyeIcon,
GitForkIcon,
GlobeIcon,
type LucideIcon,
MessageCircleIcon,
MousePointerClickIcon,
PaintbrushIcon,
MinusIcon,
Redo2Icon,
Minimize2Icon,
SearchIcon,
SmartphoneIcon,
SquarePenIcon,
TerminalIcon,
Undo2Icon,
HammerIcon,
WrenchIcon,
XIcon,
ZapIcon,
RotateCcwIcon,
} from "lucide-react";
import { ChevronDown, ChevronRight, Link as LinkIconNode } from "lucide";
import type {
ComposerContextId,
ComposerContextRecord,
KnownComposerContextRecord,
} from "@t3tools/contracts";
import { Button, InlineButton } from "../ui/button";
import { MorphIcon } from "~/components/MorphIcon";
import { useAssetUrlRefresh, useAssetUrls, useAssetUrlState } from "../../assets/assetUrls";
import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area";
import {
buildAttachmentVideoAsset,
buildAttachmentVideoPreview,
buildExpandedImagePreview,
ExpandedImagePreview,
} from "./ExpandedImagePreview";
import {
SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
SnapShotAttachmentDetails,
} from "./SnapShotAttachmentDetails";
import { ProposedPlanCard } from "./ProposedPlanCard";
import { HtmlRenderFrame } from "./HtmlRenderFrame";
import { McpAppFrame } from "./McpAppFrame";
import { ChangedFilesCard } from "./ChangedFilesTree";
import { useFileContextMenuHandler } from "../../fileContextMenu";
import { useProject, useThreadShell } from "../../state/entities";
import {
CHAT_TIMELINE_ANCHOR_OFFSET,
readTimelinePosition,
rememberTimelinePosition,
timelineContentOverflowsViewport,
} from "./timelineScrollAnchoring";
import { MessageCopyButton } from "./MessageCopyButton";
import { promptLinkFor } from "../promptLinks/copyPromptLink";
import { PierreEntryIcon } from "./PierreEntryIcon";
import { WorkspaceEntryIcon, WorkspaceEntryTooltip } from "./WorkspaceEntryIcon";
import { AssistantSelectionToolbar } from "./AssistantSelectionToolbar";
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
import {
AssistantCitationSource,
type AssistantCitationRequest,
type AssistantCitationTarget,
} from "./AssistantCitationSource";
import { useAssistantCitationTarget, type CitationHistoryPage } from "./useAssistantCitationTarget";
import {
computeStableMessagesTimelineRows,
deriveMessagesTimelineRowsWithState,
type MessagesTimelineRowsProjection,
liveWorkEntryLabel,
resolveAssistantMessageCopyState,
resolveTimelineIsAtEnd,
resolveTimelineMinimapHasPersistentGutter,
resolveTimelineMinimapCurrentIndex,
resolveTimelineMinimapHeightStyle,
resolveTimelineMinimapHitStripWidth,
resolveTimelineMinimapIndexFromPointer,
resolveTimelineMinimapInteractiveWidth,
resolveTimelineMinimapNavigationInteractive,
resolveTimelineMinimapTopPercent,
resolveWorkGroupScrollIndex,
shouldCollapseUserMessage,
shouldFollowWorkGroupAppend,
timelineTurnFoldRunIdsByEntryId,
threadReadLabelPrefix,
threadReadTargetId,
threadReadTargetTitle,
toolGroupAction,
workEntryDisplayLabel,
workEntryReadOutput,
workEntryIsVisibleInGroup,
worktreeSetupAgentStarted,
type StableMessagesTimelineRowsState,
type MessagesTimelineRow,
TIMELINE_MINIMAP_MIN_ITEMS,
type TimelineLatestRun,
type WorkGroupScrollAnchor,
} from "./MessagesTimeline.logic";
import { type ThreadFindMatch } from "./threadFind";
import { useThreadFindNavigation } from "./useThreadFindNavigation";
import { readThreadFindPosition } from "./threadFindPosition";
import type { ThreadFindPositionReader } from "./threadFind";
import { TerminalContextInlineChip } from "./TerminalContextInlineChip";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import { Spinner } from "../ui/spinner";
import { Tooltip, TooltipPopup, TooltipTrigger, TooltipScrollDismissArea } from "../ui/tooltip";
import { WorktreeSetupCard } from "./WorktreeSetupCard";
import {
ContextChipPopover as UserMessageContextPopover,
ContextChipShell,
FileChip,
ImageChipButton,
PULL_REQUEST_CHIP_KINDS,
PullRequestChip,
UnresolvedChip,
} from "../contextChipParts";
import {
asKnownContextRecord,
isPullRequestSummaryContext,
pullRequestContextDisplayState,
pullRequestContextKindLabel,
resolveUserMessageContext,
reviewCommentContextLabel,
selectedMessageContextFragment,
} from "~/lib/composerContextRecords";
import {
collectComposerContextReferences,
formatComposerContextReference,
} from "@t3tools/shared/composerContextReferences";
import {
COMPOSER_CONTEXT_CLIPBOARD_MIME,
encodeComposerContextClipboardHtml,
encodeComposerContextFragment,
} from "@t3tools/shared/composerContextClipboard";
import { chatMarkdownClipboardPayload } from "../../markdown-clipboard";
import { ContextChip, ContextChipLabel, type ContextChipKind } from "../ContextChip";
import { createContextPresentationRegistry } from "../contextPresentationRegistry";
import { useOpenPrLink } from "~/lib/openPullRequestLink";
import { useClientSettings } from "~/hooks/useSettings";
import type { ChatMarkdownContextReference } from "../ChatMarkdown";
import { useMediaQuery } from "~/hooks/useMediaQuery";
import { cn } from "~/lib/utils";
import { useUiStateStore } from "~/uiStateStore";
import { type TimestampFormat } from "@t3tools/contracts/settings";
import {
formatChatTimestampTooltip,
formatDayAwareTimestamp,
formatUpcomingTimestamp,
} from "../../timestampFormat";
import { FetchedToolOutput, V2ItemInspector } from "./V2ItemInspector";
import { useV2ItemSupport } from "../../state/v2ItemSupport";
import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from "../ui/collapsible";
import {
isV2LifecycleItem,
SubagentAvatar,
SubagentElapsed,
SubagentNotificationLink,
V2LifecycleRow,
type HandoffTimelineRun,
} from "./V2LifecycleRow";
import { SecretRequestCard } from "./SecretRequestCard";
import { TimelineSystemDivider } from "./TimelineSystemDivider";
import { SkillChipIcon, SkillInlineText } from "./SkillInlineText";
import * as DateTime from "effect/DateTime";
import { formatWorkspaceRelativePath } from "../../filePathDisplay";
import { forwardWheelToTimeline } from "./timelineMinimapWheel";
import {
buildReviewCommentRenderablePatch,
formatReviewCommentFence,
type ReviewCommentContext,
} from "../../reviewCommentContext";
// ---------------------------------------------------------------------------
// Context — shared state consumed by every row component via Context.
// Propagates through LegendList's memo boundaries for shared callbacks and
// non-row-scoped state. `nowIso` is intentionally excluded — self-ticking
// components (LiveElapsed) handle it.
// ---------------------------------------------------------------------------
interface TimelineRowSharedState {
citationRequest: AssistantCitationTarget | null;
listRef: React.RefObject<LegendListRef | null>;
timestampFormat: TimestampFormat;
routeThreadKey: string;
threadRef: ScopedThreadRef | null;
markdownCwd: string | undefined;
resolvedTheme: "light" | "dark";
workspaceRoot: string | undefined;
skills: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">>;
/** Provider snapshots for resolving handoff endpoints to icons + model names. */
providerStatuses: ReadonlyArray<ServerProvider>;
/** Projection runs, for recovering handoff models on legacy items. */
runs: ReadonlyArray<HandoffTimelineRun>;
activeThreadEnvironmentId: EnvironmentId;
onRevertToTurnCount: (targetTurnCount: number, messageId: MessageId) => void;
onUseArtifactTemplate: (template: CodexArtifactTemplate) => void;
/** Sends text an MCP App asked to post, after the user approved it. */
onSendAppMessage: ((text: string) => Promise<void>) | undefined;
/**
* An MCP App row entering or leaving full screen. The row stays rendered
* and the list stops following new output meanwhile, so the app is not
* virtualized away while the reader is using it.
*/
onAppFullscreenChange: (rowId: string, fullscreen: boolean) => void;
onRunShellCommand: ((command: string) => void) | undefined;
findActive: boolean;
onImageExpand: (preview: ExpandedImagePreview) => void;
displayThreadKey?: string;
onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
onOpenThread: (threadId: OrchestrationV2TurnItem["threadId"]) => void;
onForkFromRun: (input: {
readonly sourceThreadId: ThreadId;
readonly runId: RunId;
}) => Promise<void>;
onRollbackCheckpoint: (input: {
readonly checkpointId: string;
readonly scopeId: string;
}) => void;
onToggleTurnFold: (runId: RunId) => void;
onToggleAttemptFold: (attemptId: RunAttemptId) => void;
onFileOpen: (attachment: ChatFileAttachment) => void;
onFileDownload: (attachment: ChatFileAttachment) => void;
openPullRequest: (event: MouseEvent<HTMLElement>, url: string) => void;
onToggleWorkGroup: (groupId: string, anchorKey: string) => void;
onToggleWorkEntry: (anchorKey: string, collapsed: boolean) => void;
onCancelWorktreeSetup: (() => void) | null;
retryableWorkspacePreparationRunIds: ReadonlySet<RunId>;
onRetryWorkspacePreparation: ((runId: RunId) => void) | null;
onWorktreeSetupWorkLocally: (() => void) | null;
onOpenWorktreeSetupTerminal: ((terminalId: string) => void) | null;
workGroupViewState: WorkGroupViewState;
}
interface TimelineRowActivityState {
isWorking: boolean;
isCompacting: boolean;
isRevertingCheckpoint: boolean;
activeTurnInProgress: boolean;
/** The agent waits on an approval or an answer from the user. */
awaitingUser: boolean;
isPreparingWorktree: boolean;
latestRunId: RunId | null;
/**
* A worktree setup whose script is still running after the agent took
* over. The working header shows it as a chip with a popover; the stage
* list itself has already left the timeline.
*/
backgroundWorktreeSetup: WorktreeSetupSnapshot | null;
}
const TimelineRowCtx = createContext<TimelineRowSharedState>(null!);
const TimelineRowActivityCtx = createContext<TimelineRowActivityState>(null!);
interface WorkGroupViewState {
scrollPositions: Map<string, WorkGroupScrollAnchor>;
expandedEntries: Set<string>;
}
const WorkGroupViewCtx = createContext<{
state: WorkGroupViewState;
onToggleEntry: (collapsed: boolean) => void;
} | null>(null);
const TIMELINE_LIST_HEADER = <div className="h-3 sm:h-4" />;
const TIMELINE_LIST_FADE_HEADER = (
<div className="h-[var(--workspace-titlebar-scroll-fade-height)]" />
);
function TimelineListFooter({
composerInset,
children,
}: {
readonly composerInset: number;
readonly children?: ReactNode;
}) {
return (
<div>
{children}
<div aria-hidden style={{ height: composerInset }} />
<div aria-hidden className="h-3 sm:h-4" />
</div>
);
}
const EMPTY_TIMELINE_SKILLS: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">> = [];
const TIMELINE_MAINTAIN_SCROLL_AT_END = {
animated: false,
on: {
dataChange: true,
// Composer inset changes must not move already-visible messages. New
// rows and row growth still keep live-follow pinned through the other
// triggers below.
footerLayout: false,
itemLayout: true,
layout: true,
},
} as const satisfies MaintainScrollAtEndOptions;
const EMPTY_TIMELINE_RUNS: ReadonlyArray<HandoffTimelineRun> = [];
const EMPTY_RUN_IDS: ReadonlySet<RunId> = new Set();
// Streamed text lands a paragraph at a time. A smooth scroll to the end
// turns each landing into a short glide instead of a jump. Thread switches
// and layout settles keep the instant variant so nothing visibly travels.
const TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH = {
...TIMELINE_MAINTAIN_SCROLL_AT_END,
animated: true,
} as const satisfies MaintainScrollAtEndOptions;
// ---------------------------------------------------------------------------
// Props (public API)
// ---------------------------------------------------------------------------
export interface MessagesTimelineHistoryControls {
readonly hasMoreHistory: boolean;
readonly loading: boolean;
readonly error: string | null;
readonly onLoadEarlier: (throughEntryId?: string) => void;
}
interface MessagesTimelineProps {
citationRequest?: AssistantCitationRequest | null;
citationHistoryLoading?: boolean;
onCiteAssistantText?: (
citation: AssistantCitation,
sourceAnchor: AssistantCitationSourceAnchor,
) => boolean;
isWorking: boolean;
/** The live work belongs to a runless root turn (a provider-native subagent). */
runlessWorkActive?: boolean;
activeTurnInProgress: boolean;
activeTurnStartedAt?: string | null;
worktreeSetup?: WorktreeSetupSnapshot | null;
onCancelWorktreeSetup?: () => void;
/** Runs whose failed workspace preparation can be retried, keyed by run id. */
retryableWorkspacePreparationRunIds?: ReadonlySet<RunId>;
onRetryWorkspacePreparation?: (runId: RunId) => void;
onWorktreeSetupWorkLocally?: () => void;
onOpenWorktreeSetupTerminal?: (terminalId: string) => void;
isPreparingWorktree?: boolean;
isCompacting?: boolean;
/** The agent waits on an approval or an answer from the user. */
awaitingUser?: boolean;
/** Thread state shown after the last message, such as a settled or snoozed line. */
footer?: ReactNode;
listRef: React.RefObject<LegendListRef | null>;
timelineEntries: ReadonlyArray<TimelineEntry>;
latestRun: TimelineLatestRun | null;
runningRunId?: RunId | null;
turnDiffSummaries: ReadonlyArray<TurnDiffSummary>;
routeThreadKey: string;
displayThreadKey?: string;
/** The rows are a held snapshot of the previous thread while the next one loads. */
paintOnly?: boolean;
onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
onOpenThread: (threadId: OrchestrationV2TurnItem["threadId"]) => void;
parentThreadLink?: {
readonly threadId: ThreadId;
readonly title: string;
} | null;
onForkFromRun: (input: {
readonly sourceThreadId: ThreadId;
readonly runId: RunId;
}) => Promise<void>;
onRollbackCheckpoint: (input: {
readonly checkpointId: string;
readonly scopeId: string;
}) => void;
supportsConversationRollback: boolean;
onRevertToTurnCount: (targetTurnCount: number, messageId: MessageId) => void;
onUseArtifactTemplate?: (template: CodexArtifactTemplate) => void;
onSendAppMessage?: (text: string) => Promise<void>;
onRunShellCommand?: (command: string) => void;
isRevertingCheckpoint: boolean;
onImageExpand: (preview: ExpandedImagePreview) => void;
onFileOpen?: (attachment: ChatFileAttachment) => void;
onFileDownload?: (attachment: ChatFileAttachment) => void;
activeThreadEnvironmentId: EnvironmentId;
markdownCwd: string | undefined;
resolvedTheme: "light" | "dark";
timestampFormat: TimestampFormat;
workspaceRoot: string | undefined;
skills?: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">>;
providerStatuses: ReadonlyArray<ServerProvider>;
runs: ReadonlyArray<HandoffTimelineRun>;
anchorMessageId: MessageId | null;
onAnchorReady: (messageId: MessageId, anchorIndex: number) => void;
onAnchorSizeChanged: (messageId: MessageId, size: number) => void;
contentInsetEndAdjustment: number;
onIsAtEndChange: (isAtEnd: boolean) => void;
/**
* Whether the timeline should keep pinning to the live edge as content
* grows. Off while the user is reading history; LegendList's own
* maintainScrollAtEnd would otherwise re-pin regardless of ChatView's
* scroll-mode refs whenever the user drifts near the bottom.
*/
liveFollowEnabled: boolean;
/**
* Whether the real rows extend past the viewport above the composer.
* Reported after scrolls, row size changes, and viewport resizes.
*/
onContentOverflowChange?: (overflows: boolean) => void;
onToolOutputCollapsedAtEnd?: () => void;
onManualNavigation: () => void;
findOpen?: boolean;
cancelPositionRestoreRef?: React.RefObject<(() => void) | null> | undefined;
hideEmptyPlaceholder?: boolean;
topFadeEnabled?: boolean;
historyControls?: MessagesTimelineHistoryControls | undefined;
/** Non-null when older turns exist beyond the loaded window. */
loadEarlier?: CitationHistoryPage | null;
findPositionReaderRef?: React.RefObject<ThreadFindPositionReader | null>;
findExpanded?: boolean;
findQuery?: string;
activeFindMatch?: ThreadFindMatch | null;
findNavigationId?: number;
}
// ---------------------------------------------------------------------------
// MessagesTimeline — list owner
// ---------------------------------------------------------------------------
export function MessagesTimeline(props: MessagesTimelineProps) {
const find = useContext(ThreadFindTimelineContext);
const findOpen = find?.findOpen ?? props.findOpen;
const { onManualNavigation } = props;
useEffect(() => {
if (findOpen) onManualNavigation();
}, [findOpen, onManualNavigation]);
return (
<ConversationTimeline
{...props}
{...find}
liveFollowEnabled={!findOpen && props.liveFollowEnabled}
/>
);
}
const ConversationTimeline = memo(function ConversationTimeline({
citationRequest = null,
citationHistoryLoading = false,
onCiteAssistantText,
isWorking,
runlessWorkActive = false,
activeTurnInProgress,
activeTurnStartedAt = null,
worktreeSetup = null,
footer = null,
onCancelWorktreeSetup,
retryableWorkspacePreparationRunIds = EMPTY_RUN_IDS,
onRetryWorkspacePreparation,
onWorktreeSetupWorkLocally,
onOpenWorktreeSetupTerminal,
isPreparingWorktree = false,
isCompacting = false,
awaitingUser = false,
listRef,
timelineEntries,
findExpanded = false,
findQuery = "",
activeFindMatch = null,
findNavigationId = 0,
findPositionReaderRef,
latestRun,
runningRunId = null,
turnDiffSummaries,
routeThreadKey,
displayThreadKey,
paintOnly = false,
onOpenTurnDiff,
onOpenThread,
parentThreadLink = null,
onForkFromRun,
onRollbackCheckpoint,
supportsConversationRollback,
onRevertToTurnCount,
onUseArtifactTemplate = NOOP_USE_ARTIFACT_TEMPLATE,
onSendAppMessage,
onRunShellCommand,
isRevertingCheckpoint,
onImageExpand,
onFileOpen = NOOP_OPEN_ATTACHMENT,
onFileDownload = NOOP_OPEN_ATTACHMENT,
activeThreadEnvironmentId,
markdownCwd,
resolvedTheme,
timestampFormat,
workspaceRoot,
skills = EMPTY_TIMELINE_SKILLS,
providerStatuses,
runs: runsProp,
anchorMessageId,
onAnchorReady,
onAnchorSizeChanged,
contentInsetEndAdjustment,
onIsAtEndChange,
onContentOverflowChange,
liveFollowEnabled,
onToolOutputCollapsedAtEnd,
onManualNavigation,
cancelPositionRestoreRef,
hideEmptyPlaceholder = false,
topFadeEnabled = false,
historyControls,
loadEarlier = null,
}: MessagesTimelineProps) {
const listIdentityKey = displayThreadKey ?? routeThreadKey;
const rememberedPosition = useMemo(
() => readTimelinePosition(listIdentityKey),
[listIdentityKey],
);
const [expandedRunIds, setExpandedRunIds] = useState<ReadonlySet<RunId>>(
() => rememberedPosition?.disclosures?.runs ?? new Set(),
);
const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState<ReadonlySet<string>>(
() => rememberedPosition?.disclosures?.workGroups ?? new Set(),
);
const [expandedAttemptIds, setExpandedAttemptIds] = useState<ReadonlySet<RunAttemptId>>(
() => rememberedPosition?.disclosures?.attempts ?? new Set(),
);
const [positionedThreadKey, setPositionedThreadKey] = useState<string | null>(() =>
rememberedPosition?.atEnd === false ? null : listIdentityKey,
);
const restoringThreadPosition = positionedThreadKey !== listIdentityKey;
const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
const listIdentityRef = useRef(listIdentityKey);
const previousLatestRunRef = useRef(latestRun);
// The list stays mounted across thread switches. Its first end pins on the
// new thread must snap, not glide, even if that thread is mid-turn.
const [settlingListIdentity, setSettlingListIdentity] = useState<string | null>(listIdentityKey);
let paintedExpandedRunIds = expandedRunIds;
let paintedExpandedWorkGroupIds = expandedWorkGroupIds;
let paintedExpandedAttemptIds = expandedAttemptIds;
if (listIdentityRef.current !== listIdentityKey) {
listIdentityRef.current = listIdentityKey;
setPositionedThreadKey(null);
previousLatestRunRef.current = latestRun;
setSettlingListIdentity(listIdentityKey);
paintedExpandedRunIds = rememberedPosition?.disclosures?.runs ?? new Set();
paintedExpandedWorkGroupIds = rememberedPosition?.disclosures?.workGroups ?? new Set();
paintedExpandedAttemptIds = rememberedPosition?.disclosures?.attempts ?? new Set();
setExpandedRunIds(paintedExpandedRunIds);
setExpandedWorkGroupIds(paintedExpandedWorkGroupIds);
setExpandedAttemptIds(paintedExpandedAttemptIds);
}
const citationThreadRef = useMemo(() => parseScopedThreadKey(routeThreadKey), [routeThreadKey]);
const openPullRequest = useOpenPrLink(citationThreadRef ?? undefined);
const expandCitedRun = useCallback((runId: RunId) => {
setExpandedRunIds((current) => (current.has(runId) ? current : new Set([...current, runId])));
}, []);
// Nested tool state shares the bounded thread-position cache.
const workGroupViewState = useMemo<WorkGroupViewState>(
() =>
rememberedPosition?.disclosures?.workGroupState ?? {
scrollPositions: new Map(),
expandedEntries: new Set(),
},
[listIdentityKey, rememberedPosition],
);
const [minimapStripMap] = useState(() => new Map<string, HTMLSpanElement>());
const [disclosureToggleSettling, setDisclosureToggleSettling] = useState(false);
const disclosureAnchorKeyRef = useRef<string | null>(null);
const disclosureSettleFrameRef = useRef<number | null>(null);
const disclosureSettleSecondFrameRef = useRef<number | null>(null);
const normalizedFindQuery = findQuery.trim();
const findActive = findExpanded;
useEffect(() => {
return () => {
if (disclosureSettleFrameRef.current !== null) {
cancelAnimationFrame(disclosureSettleFrameRef.current);
}
if (disclosureSettleSecondFrameRef.current !== null) {
cancelAnimationFrame(disclosureSettleSecondFrameRef.current);
}
};
}, []);
useEffect(() => {
if (settlingListIdentity === null) return;
// Two frames covers the fresh-data layout pass and the initial end pin.
let second: number | null = null;
const first = requestAnimationFrame(() => {
second = requestAnimationFrame(() => {
setSettlingListIdentity((current) => (current === settlingListIdentity ? null : current));
});
});
return () => {
cancelAnimationFrame(first);
if (second !== null) cancelAnimationFrame(second);
};
}, [settlingListIdentity]);
// A toggle made at the end keeps the end in view while rows re-measure.
// Anywhere else, the toggled row holds its place.
const disclosurePinsEndRef = useRef(false);
const disclosureCollapsedRef = useRef(false);
const pinDisclosureToEnd = useCallback(() => {
if (!disclosurePinsEndRef.current) return;
const element = listRef.current?.getScrollableNode();
if (element) element.scrollTop = element.scrollHeight;
}, [listRef]);
// Settles two frames after the last toggle, or after the last row resize
// while pinned to the end, since tool output can render a few frames late.
const scheduleDisclosureSettle = useCallback(() => {
if (disclosureSettleFrameRef.current !== null) {
cancelAnimationFrame(disclosureSettleFrameRef.current);
}
if (disclosureSettleSecondFrameRef.current !== null) {
cancelAnimationFrame(disclosureSettleSecondFrameRef.current);
}
disclosureSettleFrameRef.current = requestAnimationFrame(() => {
pinDisclosureToEnd();
disclosureSettleSecondFrameRef.current = requestAnimationFrame(() => {
pinDisclosureToEnd();
disclosurePinsEndRef.current = false;
disclosureAnchorKeyRef.current = null;
setDisclosureToggleSettling(false);
disclosureSettleFrameRef.current = null;
disclosureSettleSecondFrameRef.current = null;
// Wait for row measurement and the disclosure click's blur check.
// Closing output can reveal the end without a scroll event.
if (
disclosureCollapsedRef.current &&
resolveTimelineIsAtEnd(listRef.current?.getState()) === true
) {
onToolOutputCollapsedAtEnd?.();
}
});
});
}, [listRef, onToolOutputCollapsedAtEnd, pinDisclosureToEnd]);
const suspendEndScrollMaintenanceForDisclosure = useCallback(
(anchorKey: string, collapsed = false) => {
disclosureAnchorKeyRef.current = anchorKey;
disclosureCollapsedRef.current = collapsed;
disclosurePinsEndRef.current = resolveTimelineIsAtEnd(listRef.current?.getState()) === true;
setDisclosureToggleSettling(true);
scheduleDisclosureSettle();
},
[listRef, scheduleDisclosureSettle],
);
const shouldRestoreVisibleContentPosition = useCallback((row: MessagesTimelineRow) => {
if (disclosurePinsEndRef.current) return false;
const disclosureAnchorKey = disclosureAnchorKeyRef.current;
return disclosureAnchorKey === null || row.id === disclosureAnchorKey;
}, []);
const onToggleTurnFold = useCallback(
(runId: RunId) => {
suspendEndScrollMaintenanceForDisclosure(`turn-fold:${runId}`);
setExpandedRunIds((existing) => {
const next = new Set(existing);
if (next.has(runId)) {
next.delete(runId);
} else {
next.add(runId);
}
return next;
});
},
[suspendEndScrollMaintenanceForDisclosure],
);
const onToggleWorkGroup = useCallback(
(groupId: string, anchorKey: string) => {
suspendEndScrollMaintenanceForDisclosure(anchorKey, expandedWorkGroupIds.has(groupId));
setExpandedWorkGroupIds((existing) => {
const next = new Set(existing);
if (next.has(groupId)) {
next.delete(groupId);
} else {
next.add(groupId);
}
return next;
});
},
[expandedWorkGroupIds, suspendEndScrollMaintenanceForDisclosure],
);
const onToggleAttemptFold = useCallback(
(attemptId: RunAttemptId) => {
suspendEndScrollMaintenanceForDisclosure(`attempt-fold:${attemptId}`);
setExpandedAttemptIds((existing) => {
const next = new Set(existing);
if (next.has(attemptId)) {
next.delete(attemptId);
} else {
next.add(attemptId);
}
return next;
});
},
[suspendEndScrollMaintenanceForDisclosure],
);
// An in-session interrupt leaves its turn expanded so the user keeps their
// place; the next turn (or a reload, since this is local state) folds it.
useEffect(() => {
const previous = previousLatestRunRef.current;
previousLatestRunRef.current = latestRun;
if (!latestRun || previous?.runId === undefined) {
return;
}
if (latestRun.runId === previous.runId) {
if (previous.status === "running" && latestRun.status === "interrupted") {
setExpandedRunIds((existing) => {
const next = new Set(existing);
next.add(latestRun.runId);
return next;
});
}
return;
}
setExpandedRunIds((existing) => {
if (!existing.has(previous.runId)) {
return existing;
}
const next = new Set(existing);
next.delete(previous.runId);
return next;
});
}, [latestRun]);
// Find the fold that hides the match: imported turns have no run id but still
// fold under a synthetic key, so the match's own run id cannot open them.
const findFoldRunIds = useMemo(
() =>
findActive
? timelineTurnFoldRunIdsByEntryId({
timelineEntries,
latestRun,
runningRunId,
isWorking,
runlessWorkActive,
})
: null,
[findActive, timelineEntries, latestRun, runningRunId, isWorking, runlessWorkActive],
);
const activeFindRunId = activeFindMatch
? findFoldRunIds?.get(activeFindMatch.entryId)
: undefined;
useEffect(() => {
if (!activeFindRunId) return;
setExpandedRunIds((previous) =>
previous.has(activeFindRunId) ? previous : new Set(previous).add(activeFindRunId),
);
}, [activeFindRunId]);
const visibleExpandedRunIds = useMemo(() => {
if (!activeFindRunId || paintedExpandedRunIds.has(activeFindRunId)) {
return paintedExpandedRunIds;
}
return new Set(paintedExpandedRunIds).add(activeFindRunId);
}, [activeFindRunId, paintedExpandedRunIds]);
const activeFindAttemptId = activeFindMatch
? timelineEntries.find((entry) => entry.id === activeFindMatch.entryId)?.attempt?.id
: undefined;
useEffect(() => {
if (!activeFindAttemptId) return;
setExpandedAttemptIds((previous) =>
previous.has(activeFindAttemptId) ? previous : new Set(previous).add(activeFindAttemptId),
);
}, [activeFindAttemptId]);
const visibleExpandedAttemptIds = useMemo(
() =>
activeFindAttemptId && !paintedExpandedAttemptIds.has(activeFindAttemptId)
? new Set(paintedExpandedAttemptIds).add(activeFindAttemptId)
: paintedExpandedAttemptIds,
[activeFindAttemptId, paintedExpandedAttemptIds],
);
const rowsProjectionRef = useRef<{
readonly threadKey: string;
readonly workspaceRoot: string | undefined;
readonly projection: MessagesTimelineRowsProjection;
} | null>(null);
const rawRows = useMemo(() => {
const previous = rowsProjectionRef.current;
const projection = deriveMessagesTimelineRowsWithState(
{
timelineEntries,
latestRun,
runningRunId,
expandedRunIds: visibleExpandedRunIds,
expandedAttemptIds: visibleExpandedAttemptIds,
expandedWorkGroupIds,
isWorking,
runlessWorkActive,
activeTurnStartedAt,
turnDiffSummaries,
supportsConversationRollback,
worktreeSetup,
},
previous?.threadKey === listIdentityKey && previous.workspaceRoot === workspaceRoot
? previous.projection
: null,
);
rowsProjectionRef.current = { threadKey: listIdentityKey, workspaceRoot, projection };
return projection.rows;
}, [
rowsProjectionRef,
listIdentityKey,
workspaceRoot,
timelineEntries,
latestRun,
runningRunId,
visibleExpandedRunIds,
visibleExpandedAttemptIds,
expandedWorkGroupIds,
isWorking,
runlessWorkActive,
activeTurnStartedAt,
turnDiffSummaries,
supportsConversationRollback,
worktreeSetup,
]);
const rows = useStableRows(rawRows, listIdentityKey);
// Run status/timestamps churn on every stream event; the shared row context
// must not change with them or every timeline row re-renders per event.
const runs = useStableHandoffRuns(runsProp);
const minimapItems = useMemo(() => deriveTimelineMinimapItems(rows), [rows]);
const restoreRowIndex =
restoringThreadPosition && rememberedPosition?.atEnd === false
? rows.findIndex((row) => row.id === rememberedPosition.rowId)
: -1;
const restoringAlwaysRender = useMemo(
() =>
restoringThreadPosition && restoreRowIndex >= 0 ? { indices: [restoreRowIndex] } : undefined,
[restoreRowIndex, restoringThreadPosition],
);
useLayoutEffect(() => {
if (!restoringThreadPosition || rows.length === 0) return;
const list = listRef.current;
if (!list) return;
if (citationRequest !== null) {
setPositionedThreadKey(listIdentityKey);
return;
}
let cancelled = false;
let settleFrame: number | null = null;
const viewport: HTMLElement | null = list.getScrollableNode();
const cancelRestoration = () => {
if (cancelled) return;
cancelled = true;
if (settleFrame !== null) cancelAnimationFrame(settleFrame);
// Supersede any pending estimated-index scroll before the browser applies the gesture.
if (viewport) void list.scrollToOffset({ offset: viewport.scrollTop, animated: false });
setPositionedThreadKey(listIdentityKey);
};
const cancelForNavigation = () => {
cancelRestoration();
onManualNavigation();
};
const onScrollKey = (event: globalThis.KeyboardEvent) => {
if (
["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(event.key) &&
!(
event.target instanceof Element &&
event.target.closest("input, textarea, [contenteditable=true]")
)
)
cancelForNavigation();
};
viewport?.addEventListener("wheel", cancelForNavigation, { passive: true });
viewport?.addEventListener("touchmove", cancelForNavigation, { passive: true });
viewport?.addEventListener("pointerdown", cancelForNavigation, { passive: true });
viewport?.ownerDocument.addEventListener("keydown", onScrollKey);
const position = rememberedPosition;
const index = position ? rows.findIndex((row) => row.id === position.rowId) : -1;
if (position?.atEnd === false) onManualNavigation();
if (cancelPositionRestoreRef) cancelPositionRestoreRef.current = cancelRestoration;
const scrolling =
position?.atEnd === false
? index >= 0
? list.scrollToIndex({
index,
animated: false,
viewPosition: 0,
viewOffset: -position.offsetWithinRow,
})
: list.scrollToOffset({ offset: position.scrollOffset, animated: false })
: list.scrollToEnd({ animated: false });
void Promise.resolve(scrolling).then(() => {
if (cancelled) return;
if (position?.atEnd !== false || index < 0) {
setPositionedThreadKey(listIdentityKey);
return;
}
// Index scrolling starts from estimates. Keep the saved row mounted
// until its measured position and the DOM agree for two layout frames.
let stableFrames = 0;
const reconcile = () => {
if (cancelled) return;
const state = list.getState();
const rowIndex = state.indexByKey(position.rowId);
const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);
const element = list.getScrollableNode();
if (!row || !element) return;
const offset = Math.max(
0,
Math.min(
element.scrollTop +
row.getBoundingClientRect().top -
element.getBoundingClientRect().top +
position.offsetWithinRow,
element.scrollHeight - element.clientHeight,
),
);
if (Math.abs(element.scrollTop - offset) > 1) {
stableFrames = 0;
void list.scrollToOffset({ offset, animated: false }).then(() => {
if (!cancelled) settleFrame = requestAnimationFrame(reconcile);
});
return;
}
if (++stableFrames >= 2) {
setPositionedThreadKey(listIdentityKey);
} else {
settleFrame = requestAnimationFrame(reconcile);
}
};
settleFrame = requestAnimationFrame(reconcile);
});
return () => {
cancelled = true;
if (cancelPositionRestoreRef?.current === cancelRestoration) {
cancelPositionRestoreRef.current = null;
}
if (settleFrame !== null) cancelAnimationFrame(settleFrame);
viewport?.removeEventListener("wheel", cancelForNavigation);
viewport?.removeEventListener("touchmove", cancelForNavigation);
viewport?.removeEventListener("pointerdown", cancelForNavigation);
viewport?.ownerDocument.removeEventListener("keydown", onScrollKey);
};
}, [
citationRequest,
cancelPositionRestoreRef,
listIdentityKey,
listRef,
onManualNavigation,
rememberedPosition,
restoringThreadPosition,
rows,
]);
const [timelineViewportElement, setTimelineViewportElement] = useState<HTMLDivElement | null>(
null,
);
// Re-measure the minimap gutter when the chat column changes width without a viewport resize.
const chatWidth = useClientSettings((settings) => settings.chatWidth);
const {
target: readyCitationRequest,
positioning: citationPositioning,
onListLoad: onCitationListLoad,
alwaysRender: citationAlwaysRender,
} = useAssistantCitationTarget({
request: citationRequest,
entries: timelineEntries,
rows,
listRef,
viewport: timelineViewportElement,
historyLoading: citationHistoryLoading,
loadEarlier,
onExpandTurn: expandCitedRun,
onManualNavigation,
});
const [findListReady, setFindListReady] = useState(false);
const handleListLoad = useCallback(() => {
onCitationListLoad();
setFindListReady(true);
}, [onCitationListLoad]);
const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false);
const [fullscreenAppRowId, setFullscreenAppRowId] = useState<string | null>(null);
// Only the row that holds the pin can release it.
const onAppFullscreenChange = useCallback((rowId: string, fullscreen: boolean) => {
setFullscreenAppRowId((current) => (fullscreen ? rowId : current === rowId ? null : current));
}, []);
// Every pin holds at once, so navigating to a citation or restoring a
// position never drops a full-screen app's row. The app is pinned by key,
// which stays right as earlier rows load in.
const alwaysRender = useMemo(() => {
const indices = restoringAlwaysRender?.indices ?? [];
const keys = [
...(citationAlwaysRender?.keys ?? []),
...(fullscreenAppRowId === null ? [] : [fullscreenAppRowId]),
];
if (indices.length === 0 && keys.length === 0) return undefined;
return {
...(indices.length === 0 ? {} : { indices }),
...(keys.length === 0 ? {} : { keys }),
};
}, [citationAlwaysRender, restoringAlwaysRender, fullscreenAppRowId]);
const [minimapHitStripWidth, setMinimapHitStripWidth] = useState(0);
const [minimapCurrentIndex, setMinimapCurrentIndex] = useState<number | null>(null);
const handleAnchorReady = useCallback(
(info: { anchorIndex: number | undefined }) => {
if (anchorMessageId !== null && info.anchorIndex !== undefined) {
onAnchorReady(anchorMessageId, info.anchorIndex);
}
},
[anchorMessageId, onAnchorReady],
);
const handleAnchorSizeChanged = useCallback(
(size: number) => {
if (anchorMessageId !== null) {
onAnchorSizeChanged(anchorMessageId, size);
}
},
[anchorMessageId, onAnchorSizeChanged],
);
const anchoredEndSpace = useMemo(() => {
const config = resolveChatListAnchoredEndSpace(
rows,
anchorMessageId,
(row) => (row.kind === "message" && row.message.role === "user" ? row.message.id : null),
{ anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET },
);
return config
? { ...config, onReady: handleAnchorReady, onSizeChanged: handleAnchorSizeChanged }
: undefined;
}, [anchorMessageId, handleAnchorReady, handleAnchorSizeChanged, rows]);
const maintainVisibleContentPosition = useMemo(
() => ({
data: true,
size: true,
shouldRestorePosition: shouldRestoreVisibleContentPosition,
}),
[shouldRestoreVisibleContentPosition],
);
const timelineListFooter = useMemo(
() => (
<TimelineListFooter composerInset={anchoredEndSpace ? 0 : contentInsetEndAdjustment}>
{footer}
</TimelineListFooter>
),
[anchoredEndSpace, contentInsetEndAdjustment, footer],
);
const measureContentOverflow = useCallback(
() =>
timelineContentOverflowsViewport(listRef.current?.getState?.(), {
composerInset: contentInsetEndAdjustment,
anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET,
}),
[contentInsetEndAdjustment, listRef],
);
// LegendList lays rows out from layout effects, so a read on the next frame
// sees the settled positions. One frame is shared across bursts of size
// changes.
const contentOverflowFrameRef = useRef<number | null>(null);
const cancelContentOverflowFrame = useCallback(() => {
if (contentOverflowFrameRef.current !== null) {
cancelAnimationFrame(contentOverflowFrameRef.current);
contentOverflowFrameRef.current = null;
}
}, []);
const reportContentOverflow = useCallback(() => {
if (!onContentOverflowChange || contentOverflowFrameRef.current !== null) return;
contentOverflowFrameRef.current = requestAnimationFrame(() => {
contentOverflowFrameRef.current = null;
onContentOverflowChange(measureContentOverflow());
});
}, [measureContentOverflow, onContentOverflowChange]);
const handleItemSizeChanged = useCallback(() => {
if (disclosurePinsEndRef.current) {
queueMicrotask(pinDisclosureToEnd);
scheduleDisclosureSettle();
}
reportContentOverflow();
}, [pinDisclosureToEnd, reportContentOverflow, scheduleDisclosureSettle]);
useEffect(() => cancelContentOverflowFrame, [cancelContentOverflowFrame]);
// The list's own layout effects have already run here, so estimated row
// positions are in place. Reporting before the first paint lets a thread
// open in its final composer layout instead of correcting it a frame later.
// A frame scheduled with the previous inset would overwrite this read, so
// it is dropped first.
useLayoutEffect(() => {
cancelContentOverflowFrame();
onContentOverflowChange?.(measureContentOverflow());
}, [cancelContentOverflowFrame, measureContentOverflow, onContentOverflowChange, rows.length]);
const handleScroll = useCallback(() => {
const state = listRef.current?.getState?.();
if (restoringThreadPosition || state?.data !== rows) return;
const isAtEnd = resolveTimelineIsAtEnd(state);
const position = state?.data?.length ? resolveWorkGroupScrollAnchor(state) : undefined;
if (position && state && isAtEnd !== undefined && !paintOnly) {
const index = state.indexByKey(position.rowId);
const row = index === undefined ? undefined : state.elementAtIndex(index);
const element = listRef.current?.getScrollableNode();
if (row && element) {
rememberTimelinePosition(
listIdentityKey,
{
...position,
// DOM geometry includes the header and the virtualizer's layout adjustment.
offsetWithinRow: element.getBoundingClientRect().top - row.getBoundingClientRect().top,
scrollOffset: element.scrollTop,
atEnd: isAtEnd,
disclosures: {
runs: paintedExpandedRunIds,
workGroups: paintedExpandedWorkGroupIds,
attempts: paintedExpandedAttemptIds,
workGroupState: workGroupViewState,
},
},
liveFollowEnabled && !anchoredEndSpace,
);
}
}
if (isAtEnd !== undefined && !citationPositioning) {
onIsAtEndChange(isAtEnd);
}
reportContentOverflow();
if (!state || minimapItems.length === 0) {
return;
}
const scrollTop = state.scroll ?? 0;
const scrollBottom = scrollTop + (state.scrollLength ?? 0);
const itemBounds = minimapItems.map((item) => ({
top: resolveTimelineRowTop(state, item.rowIndex),
height: resolveTimelineRowHeight(state, item.rowIndex),
}));
for (const [index, item] of minimapItems.entries()) {
const strip = minimapStripMap.get(item.id);
const bounds = itemBounds[index];
const rowTop = bounds?.top ?? null;
const rowHeight = bounds?.height ?? null;
const inView =
rowTop !== null &&
rowTop < scrollBottom &&
rowTop + Math.max(1, rowHeight ?? 1) > scrollTop;
// Skip no-op attribute writes: this runs for every strip on every scroll
// tick, and rewriting an unchanged attribute still dirties style state.
const next = inView ? "true" : "false";
if (strip && strip.dataset.inView !== next) {
strip.dataset.inView = next;
}
}
const nextCurrentIndex = resolveTimelineMinimapCurrentIndex({
scrollTop,
scrollBottom,
itemBounds,
});
setMinimapCurrentIndex((current) =>
current === nextCurrentIndex ? current : nextCurrentIndex,
);
}, [
citationPositioning,
paintedExpandedRunIds,
paintedExpandedWorkGroupIds,
paintedExpandedAttemptIds,
workGroupViewState,
rows,
listIdentityKey,
liveFollowEnabled,
anchoredEndSpace,
paintOnly,
restoringThreadPosition,
listRef,
minimapItems,
minimapStripMap,
onIsAtEndChange,
reportContentOverflow,
]);
useEffect(() => {
const frame = requestAnimationFrame(handleScroll);
return () => cancelAnimationFrame(frame);
}, [handleScroll, rows.length]);
const savedLiveFollowEnabledRef = useRef(liveFollowEnabled);
useLayoutEffect(() => {
const wasFollowing = savedLiveFollowEnabledRef.current;
savedLiveFollowEnabledRef.current = liveFollowEnabled;
if (wasFollowing && !liveFollowEnabled) handleScroll();
}, [handleScroll, liveFollowEnabled]);
useEffect(() => {
if (!timelineViewportElement) {
return;
}
const measure = () => {
const viewportWidth = timelineViewportElement.getBoundingClientRect().width;
// Without a mounted row, treat the column as full width so the strip stays inert.
const contentWidth =
timelineViewportElement
.querySelector<HTMLElement>("[data-timeline-root]")
?.getBoundingClientRect().width ?? viewportWidth;
const nextHasPersistentGutter = resolveTimelineMinimapHasPersistentGutter(
viewportWidth,
contentWidth,
);
setMinimapHasPersistentGutter((current) =>
current === nextHasPersistentGutter ? current : nextHasPersistentGutter,
);
setMinimapHitStripWidth(resolveTimelineMinimapHitStripWidth(viewportWidth, contentWidth));
reportContentOverflow();
};
const frame = requestAnimationFrame(measure);
const stopObserving = observeResize(timelineViewportElement, measure);
return () => {
cancelAnimationFrame(frame);
stopObserving();
};
}, [timelineViewportElement, rows.length, reportContentOverflow, chatWidth]);
const sharedState = useMemo<TimelineRowSharedState>(
() => ({
citationRequest: readyCitationRequest,
listRef,
timestampFormat,
routeThreadKey,
// Keep Markdown callbacks memoized during unrelated activity updates.
threadRef: citationThreadRef,
markdownCwd,
resolvedTheme,
workspaceRoot,
skills,
providerStatuses,
runs,
activeThreadEnvironmentId,
onRevertToTurnCount,
onRunShellCommand,
findActive,
onImageExpand,
onFileOpen,
onUseArtifactTemplate,
onSendAppMessage,
onAppFullscreenChange,
onFileDownload,
openPullRequest,
onOpenTurnDiff,
onOpenThread,
onForkFromRun,
onRollbackCheckpoint,
onToggleTurnFold,
onToggleAttemptFold,
onToggleWorkGroup,
onToggleWorkEntry: suspendEndScrollMaintenanceForDisclosure,
onCancelWorktreeSetup: onCancelWorktreeSetup ?? null,
retryableWorkspacePreparationRunIds,
onRetryWorkspacePreparation: onRetryWorkspacePreparation ?? null,
onWorktreeSetupWorkLocally: onWorktreeSetupWorkLocally ?? null,
onOpenWorktreeSetupTerminal: onOpenWorktreeSetupTerminal ?? null,
workGroupViewState,
}),
[
readyCitationRequest,
listRef,
timestampFormat,
routeThreadKey,
citationThreadRef,
markdownCwd,
resolvedTheme,
workspaceRoot,
skills,
providerStatuses,
runs,
activeThreadEnvironmentId,
onRevertToTurnCount,
onRunShellCommand,
findActive,
onImageExpand,
onFileOpen,
onUseArtifactTemplate,
onSendAppMessage,
onAppFullscreenChange,
onFileDownload,
openPullRequest,
onOpenTurnDiff,
onOpenThread,
onForkFromRun,
onRollbackCheckpoint,
onToggleTurnFold,
onToggleAttemptFold,
onToggleWorkGroup,
suspendEndScrollMaintenanceForDisclosure,
onCancelWorktreeSetup,
retryableWorkspacePreparationRunIds,
onRetryWorkspacePreparation,
onWorktreeSetupWorkLocally,
onOpenWorktreeSetupTerminal,
workGroupViewState,
],
);
const compactionAwaitingRow =
isCompacting && !rows.some((row) => row.kind === "context-compaction" && row.active);
const backgroundWorktreeSetup =
worktreeSetup !== null &&
worktreeSetup.phase === "running" &&
worktreeSetupAgentStarted(worktreeSetup) &&
latestRun?.startedAt != null
? worktreeSetup
: null;
const activityState = useMemo<TimelineRowActivityState>(
() => ({
isWorking,
isCompacting: compactionAwaitingRow,
isRevertingCheckpoint,
backgroundWorktreeSetup,
activeTurnInProgress,
awaitingUser,
isPreparingWorktree,
latestRunId: latestRun?.runId ?? null,
}),
[
awaitingUser,
compactionAwaitingRow,
backgroundWorktreeSetup,
activeTurnInProgress,
isPreparingWorktree,
isRevertingCheckpoint,
isWorking,
latestRun?.runId,
],
);
const listHeader = useMemo(() => {
const leadingContent =
parentThreadLink === null ? (
topFadeEnabled ? (
TIMELINE_LIST_FADE_HEADER
) : (
TIMELINE_LIST_HEADER
)
) : (
<div className="messages-timeline-row-frame">
<div className="chat-content-lane pt-1 sm:pt-2">
<TimelineSystemDivider
label="Subagent of"
detail={parentThreadLink.title}
icon={BotIcon}
actionLabel="Open parent thread"
onAction={() => onOpenThread(parentThreadLink.threadId)}
/>
</div>
</div>
);
return (
<>
{parentThreadLink === null ? leadingContent : null}
{historyControls ? <TimelineHistoryControl {...historyControls} /> : null}
{parentThreadLink !== null ? leadingContent : null}
</>
);
}, [historyControls, onOpenThread, parentThreadLink, topFadeEnabled]);
const canvas = useChatCanvas();
const registerTimeline = canvas?.registerTimeline;
const setTimelineList = useCallback(
(list: LegendListRef | null) => {
listRef.current = list;
registerTimeline?.(list?.getScrollableNode() ?? null);
},
[listRef, registerTimeline],
);
useLayoutEffect(() => {
if (!findPositionReaderRef || !timelineViewportElement) return;
const read: ThreadFindPositionReader = (query) => {
// Imported turns fold under a synthetic key, so match folds by key, not run id.
const foldRunIds =
findFoldRunIds ??
timelineTurnFoldRunIdsByEntryId({
timelineEntries,
latestRun,
runningRunId,
isWorking,
runlessWorkActive,
});
const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));
return readThreadFindPosition(
timelineViewportElement,
query,
(rowId) => {
const rowIndex = rows.findIndex((row) => row.id === rowId);
for (const row of rows.slice(Math.max(0, rowIndex))) {
// Ordinary rows map to one entry by id; only folds need a scan.
if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {
const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);
if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
return entry.id;
continue;
}
const entry = timelineEntries.find((entry) => {
const runId =
row.kind === "turn-fold"
? foldRunIds.get(entry.id)
: entry.kind === "message"
? entry.message.runId
: entry.kind === "proposed-plan"
? entry.proposedPlan.runId
: null;
return (
runId === row.runId &&
(entry.kind !== "message" || entry.message.role === "assistant") &&
(row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)
);
});
if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))
return entry.id;
}
return undefined;
},
contentInsetEndAdjustment,
);
};
findPositionReaderRef.current = read;
return () => {
if (findPositionReaderRef.current === read) findPositionReaderRef.current = null;
};
}, [
contentInsetEndAdjustment,
findFoldRunIds,
findPositionReaderRef,
isWorking,
latestRun,
rows,
runlessWorkActive,
runningRunId,
timelineEntries,
timelineViewportElement,
]);
useThreadFindNavigation({
listReady: findListReady,
historyControls,
entries: timelineEntries,
container: timelineViewportElement,
query: normalizedFindQuery,
match: activeFindMatch,
navigationId: findNavigationId,
rowIndex: activeFindMatch ? rows.findIndex((row) => row.id === activeFindMatch.entryId) : -1,
listRef,
contentInsetEndAdjustment,
});
// Stable renderItem — no closure deps. Row components read shared state
// from TimelineRowCtx, which propagates through LegendList's memo.
const renderItem = useCallback(
({ item }: { item: MessagesTimelineRow }) => (
<div className="messages-timeline-row-frame">
<div className="chat-content-lane overflow-x-clip" data-timeline-root="true">
<TimelineRowContent row={item} />
</div>
</div>
),
[],
);
const forwardMinimapWheel = useCallback(
(event: WheelEvent) => {
const scrollNode = listRef.current?.getScrollableNode();
if (scrollNode) forwardWheelToTimeline(event, scrollNode);
},
[listRef],
);
if (
rows.length === 0 &&
!isWorking &&
parentThreadLink === null &&
historyControls === undefined &&
// A status line (settled, snoozed) still needs the list, whose footer renders it.
footer === null
) {
if (hideEmptyPlaceholder) {
// Occupy the pane with the theme surface so a thread switch cannot
// punch a hole through to the window chrome (white in light mode).
return <div className="h-full min-h-0 bg-background" data-timeline-loading="true" />;
}
return (
<div
className="flex h-full items-center justify-center ps-(--chat-lane-inset-start) pe-(--chat-lane-inset-end)"
style={{ paddingBottom: contentInsetEndAdjustment }}
>
<p className="text-sm text-muted-foreground/30">
Send a message to start the conversation.
</p>
</div>
);
}
return (
<MarkdownFindContext value={findActive}>
<TimelineRowCtx value={sharedState}>
<TimelineRowActivityCtx value={activityState}>
<TooltipScrollDismissArea
ref={setTimelineViewportElement}
className="relative h-full min-h-0"
data-assistant-citation-viewport="true"
>
{onCiteAssistantText && citationThreadRef ? (
<AssistantSelectionToolbar
viewport={timelineViewportElement}
threadRef={citationThreadRef}
onCite={onCiteAssistantText}
/>
) : null}
<LegendList<MessagesTimelineRow>
ref={setTimelineList}
data={rows}
extraData={`${listIdentityKey}:${rows.length}`}
keyExtractor={keyExtractor}
getItemType={getItemType}
renderItem={renderItem}
estimatedItemSize={90}
initialScrollAtEnd={
!findActive && citationRequest === null && rememberedPosition?.atEnd !== false
}
// Legend needs a data refresh to mount new pins without a scroll event.
dataVersion={readyCitationRequest?.key ?? listIdentityKey}
{...(alwaysRender ? { alwaysRender } : {})}
onLoad={handleListLoad}
{...(anchoredEndSpace ? { anchoredEndSpace } : {})}
contentInsetEndAdjustment={anchoredEndSpace ? contentInsetEndAdjustment : 0}
maintainScrollAtEnd={
citationPositioning ||
(restoringThreadPosition && rememberedPosition?.atEnd === false) ||
anchoredEndSpace ||
!liveFollowEnabled ||
fullscreenAppRowId !== null ||
disclosureToggleSettling
? false
: isWorking && !prefersReducedMotion && settlingListIdentity === null
? TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH
: TIMELINE_MAINTAIN_SCROLL_AT_END
}
maintainVisibleContentPosition={
findActive ||
citationPositioning ||
(restoringThreadPosition && rememberedPosition?.atEnd === false)
? false
: maintainVisibleContentPosition
}
maintainScrollAtEndThreshold={1}
onScroll={handleScroll}
onItemSizeChanged={handleItemSizeChanged}
className={cn(
"messages-timeline-scroll scrollbar-gutter-both h-full min-h-0 overflow-x-hidden overscroll-y-contain [overflow-anchor:none]",
topFadeEnabled && "topbar-scroll-fade",
)}
ListHeaderComponent={listHeader}
ListFooterComponent={timelineListFooter}
/>
<TimelineMinimap
items={minimapItems}
hasPersistentGutter={minimapHasPersistentGutter}
hitStripWidth={minimapHitStripWidth}
currentIndex={minimapCurrentIndex}
stripMap={minimapStripMap}
onWheel={forwardMinimapWheel}
onSelect={(item) => {
onManualNavigation();
void listRef.current?.scrollToIndex({
index: item.rowIndex,
animated: true,
viewOffset: 24,
});
}}
/>
</TooltipScrollDismissArea>
</TimelineRowActivityCtx>
</TimelineRowCtx>
</MarkdownFindContext>
);
});
function TimelineHistoryControl(props: MessagesTimelineHistoryControls) {
if (!props.hasMoreHistory && props.error === null) {
return null;
}
return (
<div className="messages-timeline-row-frame">
<div className="chat-content-lane flex flex-col gap-1.5 pb-2">
{props.hasMoreHistory ? (
<button
type="button"
disabled={props.loading}
aria-label="Load earlier turns"
onClick={() => props.onLoadEarlier()}
className="w-full py-1.5 text-xs text-muted-foreground/60 hover:text-foreground disabled:cursor-default"
>
{props.loading ? "Loading earlier turns…" : "Load earlier turns"}
</button>
) : null}
{props.error !== null ? (
<p role="status" className="text-center text-muted-foreground text-xs">
{props.error}
</p>
) : null}
</div>
</div>
);
}
function keyExtractor(item: MessagesTimelineRow) {
return item.id;
}
function getItemType(item: MessagesTimelineRow) {
return item.kind === "message" ? `message:${item.message.role}` : item.kind;
}
interface TimelinePositionState {
readonly contentLength?: number;
readonly scroll?: number;
readonly scrollLength?: number;
readonly positionAtIndex?: (index: number) => number | undefined;
readonly sizeAtIndex?: (index: number) => number | undefined;
}
function resolveTimelineRowTop(state: TimelinePositionState, rowIndex: number) {
const top = state.positionAtIndex?.(rowIndex);
return typeof top === "number" && Number.isFinite(top) ? top : null;
}
function resolveTimelineRowHeight(state: TimelinePositionState, rowIndex: number) {
const height = state.sizeAtIndex?.(rowIndex);
return typeof height === "number" && Number.isFinite(height) ? height : null;
}
function timelineMinimapEventTargetsPreview(target: EventTarget): boolean {
return target instanceof Element && target.closest("[data-minimap-preview]") !== null;
}
function TimelineMinimap({
hasPersistentGutter,
hitStripWidth,
currentIndex,
items,
stripMap,
onWheel,
onSelect,
}: {
hasPersistentGutter: boolean;
hitStripWidth: number;
currentIndex: number | null;
items: ReadonlyArray<TimelineMinimapItem>;
stripMap: Map<string, HTMLSpanElement>;
onWheel: (event: WheelEvent) => void;
onSelect: (item: TimelineMinimapItem) => void;
}) {
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const wheelTargetRef = useCallback(
(node: HTMLDivElement | null) => {
if (!node) return;
node.addEventListener("wheel", onWheel, { passive: false });
return () => node.removeEventListener("wheel", onWheel);
},
[onWheel],
);
const resolvedActiveIndex =
activeIndex !== null && activeIndex < items.length ? activeIndex : null;
const activeItem = useMemo(
() =>
resolveTimelineMinimapPreview(
resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null),
),
[items, resolvedActiveIndex],
);
const navigationInteractive = resolveTimelineMinimapNavigationInteractive(hitStripWidth);
const activeTopPercent =
resolvedActiveIndex === null
? 0
: resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length);
const activeTooltipTranslate =
resolvedActiveIndex === null
? "-50%"
: resolvedActiveIndex === 0
? "0%"
: resolvedActiveIndex === items.length - 1
? "-100%"
: "-50%";
const resolvedCurrentIndex =
currentIndex !== null && currentIndex >= 0 && currentIndex < items.length ? currentIndex : null;
const previousItem =
resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex - 1] ?? null);
const nextItem = resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex + 1] ?? null);
const resolveActiveIndexFromPointer = useCallback(
(event: MouseEvent<HTMLElement>) => {
const rect = event.currentTarget.getBoundingClientRect();
return resolveTimelineMinimapIndexFromPointer({
itemCount: items.length,
railTop: rect.top,
railHeight: rect.height,
pointerY: event.clientY,
});
},
[items.length],
);
const updateActiveIndexFromPointer = useCallback(
(event: MouseEvent<HTMLElement>) => {
const nextIndex = resolveActiveIndexFromPointer(event);
setActiveIndex(nextIndex);
},
[resolveActiveIndexFromPointer],
);
const moveActiveIndex = useCallback(
(delta: number) => {
setActiveIndex((current) => {
const base = current ?? 0;
return Math.max(0, Math.min(items.length - 1, base + delta));
});
},
[items.length],
);
if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) {
return null;
}
return (
<div
className={cn(
"group/minimap pointer-events-none absolute inset-y-0 left-0 z-40 hidden w-18 [@media(pointer:fine)]:block",
hasPersistentGutter
? "opacity-100"
: "opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",
)}
data-testid="timeline-minimap"
data-persistent-gutter={hasPersistentGutter ? "true" : "false"}
ref={wheelTargetRef}
>
<div className="relative h-full w-full select-none">
<div
className={cn(
"absolute top-1/2 left-3 -translate-y-1/2",
// The strip is width-capped to the side gutter so it never overlays
// the centered content column; with no usable gutter it goes inert.
hitStripWidth > 0 ? "pointer-events-auto" : "pointer-events-none",
)}
style={{
height: resolveTimelineMinimapHeightStyle(items.length),
width: resolveTimelineMinimapInteractiveWidth(hitStripWidth, activeItem !== null),
}}
>
<TimelineMinimapNavigationButton
direction="previous"
disabled={previousItem === null}
interactive={navigationInteractive}
onClick={() => {
if (previousItem) onSelect(previousItem);
}}
/>
<button
aria-label={`Jump to message: ${activeItem?.userText ?? "User message"}`}
className="absolute inset-y-0 left-0 w-full cursor-pointer bg-transparent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
onBlur={() => setActiveIndex(null)}
onClick={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
const nextIndex = resolveActiveIndexFromPointer(event);
const selectedItem = nextIndex === null ? null : (items[nextIndex] ?? null);
if (selectedItem) {
onSelect(selectedItem);
}
event.currentTarget.blur();
}}
onFocus={() => setActiveIndex((current) => current ?? resolvedCurrentIndex ?? 0)}
onKeyDown={(event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
moveActiveIndex(1);
} else if (event.key === "ArrowUp") {
event.preventDefault();
moveActiveIndex(-1);
} else if (event.key === "Home") {
event.preventDefault();
setActiveIndex(0);
} else if (event.key === "End") {
event.preventDefault();
setActiveIndex(items.length - 1);
} else if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
if (activeItem) {
onSelect(activeItem);
}
}
}}
onMouseLeave={() => setActiveIndex(null)}
onMouseMove={updateActiveIndexFromPointer}
onMouseDown={(event) => {
if (timelineMinimapEventTargetsPreview(event.target)) {
return;
}
event.preventDefault();
}}
type="button"
>
<div className="absolute top-0 left-3 h-full w-px bg-border/15" />
{items.map((item, index) => {
const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`;
const activeDistance =
resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex);
return (
// Compositor-friendly on purpose: in-view state and the hover
// fisheye animate constantly (every scroll tick and streaming
// update flips a band of strips), so the strip animates only
// transform and opacity. Width tiers are a scale-x on a fixed
// w-6 box, and the in-view highlight is an opacity-faded bright
// overlay — never background-color or width, which would force
// main-thread style/layout/paint at 60fps for each transition.
<span
aria-hidden="true"
className={cn(
"group/strip pointer-events-none absolute left-0 h-0.5 w-6 origin-left -translate-y-1/2 rounded-full transition-transform duration-150",
activeDistance === 0 ? "bg-muted-foreground/75" : "bg-muted-foreground/35",
activeDistance === 0
? "scale-x-100"
: activeDistance === 1
? "scale-x-[0.667]"
: activeDistance === 2
? "scale-x-[0.417]"
: "scale-x-[0.333]",
)}
data-in-view="false"
data-minimap-strip
key={item.id}
ref={(node) => {
if (node) {
stripMap.set(item.id, node);
} else {
stripMap.delete(item.id);
}
}}
style={{ top }}
>
<span className="absolute inset-0 rounded-full bg-foreground/90 opacity-0 transition-opacity duration-150 group-data-[in-view=true]/strip:opacity-100" />
</span>
);
})}
{activeItem ? (
<span
className="pointer-events-auto absolute left-8 w-80 cursor-text select-text"
data-minimap-preview
onMouseMove={(event) => event.stopPropagation()}
style={{
top: `${activeTopPercent}%`,
transform: `translateY(${activeTooltipTranslate})`,
}}
>
<span className="dropdown-glass block rounded-xl p-3 text-left text-popover-foreground shadow-xl shadow-black/25">
<span className="block max-w-full overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium leading-5">
{activeItem.userText ?? "User message"}
</span>
{activeItem.assistantText ? (
<span
className="mt-1 max-h-[3.75rem] overflow-hidden text-muted-foreground text-sm leading-5"
style={{
display: "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: 3,
}}
>
{activeItem.assistantText}
</span>
) : null}
</span>
</span>
) : null}
</button>
<TimelineMinimapNavigationButton
direction="next"
disabled={nextItem === null}
interactive={navigationInteractive}
onClick={() => {
if (nextItem) onSelect(nextItem);
}}
/>
</div>
</div>
</div>
);
}
function TimelineMinimapNavigationButton({
direction,
disabled,
interactive,
onClick,
}: {
direction: "previous" | "next";
disabled: boolean;
interactive: boolean;
onClick: () => void;
}) {
const previous = direction === "previous";
const label = previous ? "Previous turn" : "Next turn";
const Icon = previous ? ChevronUpIcon : ChevronDownIcon;
return (
<Tooltip>
<TooltipTrigger
render={
<span
className={cn(
"absolute left-1 z-10 inline-flex -translate-x-1/2 opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100",
interactive ? "pointer-events-auto" : "pointer-events-none",
previous ? "bottom-[calc(100%+2px)]" : "top-[calc(100%+2px)]",
)}
/>
}
>
<Button
aria-label={label}
disabled={disabled}
onClick={onClick}
size="icon-micro"
type="button"
variant="ghost-muted"
>
<Icon className="size-4 text-foreground/90" />
</Button>
</TooltipTrigger>
<TooltipPopup side={previous ? "top" : "bottom"}>{label}</TooltipPopup>
</Tooltip>
);
}
// ---------------------------------------------------------------------------
// TimelineRowContent — the actual row component
// ---------------------------------------------------------------------------
type TimelineWorkEntry = Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"][number];
type TimelineRow = MessagesTimelineRow;
const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: TimelineRow }) {
const isExpandedToolGroup = row.kind === "work" && row.isExpandedToolGroup;
const isSubagentGroup = row.kind === "event" && row.projectedItem.item.type === "subagent";
const isWorkLogRow =
row.kind === "work" ||
row.kind === "work-live" ||
row.kind === "work-toggle" ||
row.kind === "thinking";
const isExpandedToolGroupHeader =
(row.kind === "work-toggle" || row.kind === "work-live" || row.kind === "thinking") &&
row.expanded === true;
return (
<div
className={cn(
// Commentary (non-terminal assistant) rows carry no metadata row, so
// they sit closer to the work that follows them.
isWorkLogRow || isSubagentGroup
? undefined
: row.kind === "turn-fold" || row.kind === "working"
? "pb-1.5"
: (row.kind === "message" &&
row.message.role === "assistant" &&
!row.showAssistantMeta) ||
row.kind === "worktree-setup" ||
row.kind === "event" ||
row.kind === "attempt-fold" ||
row.kind === "html-render" ||
row.kind === "mcp-app"
? "pb-2"
: "pb-4",
(row.kind === "message" && row.message.role === "assistant") ||
row.kind === "assistant-meta"
? "group/assistant"
: null,
)}
data-timeline-row-id={row.id}
data-timeline-row-kind={row.kind}
data-message-id={
row.kind === "message" || row.kind === "assistant-meta" ? row.message.id : undefined
}
data-message-role={row.kind === "message" ? row.message.role : undefined}
>
{isWorkLogRow ? (
<WorkLogBlock
continues={row.continuesWorkLog}
layout={
isExpandedToolGroup
? "group-content"
: isExpandedToolGroupHeader
? "group-header"
: "standalone"
}
>
{row.kind === "work" ? (
<WorkGroupSection
anchorKey={row.id}
groupedEntries={row.groupedEntries}
isExpandedToolGroup={row.isExpandedToolGroup}
displayLabel={row.displayLabel}
/>
) : null}
{row.kind === "work-live" ? <LiveWorkEntryTimelineRow row={row} /> : null}
{row.kind === "work-toggle" ? <WorkGroupToggleTimelineRow row={row} /> : null}
{row.kind === "thinking" ? <ThinkingTimelineRow row={row} /> : null}
</WorkLogBlock>
) : null}
{row.kind === "turn-fold" ? <TurnFoldTimelineRow row={row} /> : null}
{row.kind === "attempt-fold" ? <AttemptFoldTimelineRow row={row} /> : null}
{row.kind === "context-compaction" ? <ContextCompactionTimelineRow row={row} /> : null}
{row.kind === "message" && row.message.role === "user" ? <UserTimelineRow row={row} /> : null}
{row.kind === "message" && row.message.role === "assistant" ? (
<AssistantTimelineRow row={row} />
) : null}
{row.kind === "assistant-meta" ? <AssistantMetaTimelineRow row={row} /> : null}
{row.kind === "proposed-plan" ? <ProposedPlanTimelineRow row={row} /> : null}
{row.kind === "html-render" ? <HtmlRenderTimelineRow row={row} /> : null}
{row.kind === "mcp-app" ? <McpAppTimelineRow row={row} /> : null}
{row.kind === "working" ? <WorkingTimelineRow row={row} /> : null}
{row.kind === "worktree-setup" ? <WorktreeSetupTimelineRow row={row} /> : null}
{row.kind === "event" ? <V2EventTimelineRow row={row} /> : null}
</div>
);
});
function WorktreeSetupTimelineRow({
row,
}: {
row: Extract<TimelineRow, { kind: "worktree-setup" }>;
}) {
const ctx = use(TimelineRowCtx);
const terminalId = row.snapshot.setupScript?.terminalId ?? null;
const openTerminal = ctx.onOpenWorktreeSetupTerminal;
const onOpenTerminal = useMemo(
() => (openTerminal && terminalId ? () => openTerminal(terminalId) : null),
[openTerminal, terminalId],
);
return (
<WorktreeSetupCard
snapshot={row.snapshot}
embedded={row.embedded}
onCancel={row.embedded ? null : ctx.onCancelWorktreeSetup}
onWorkLocally={
!row.embedded && row.snapshot.phase === "running" ? ctx.onWorktreeSetupWorkLocally : null
}
onOpenTerminal={onOpenTerminal}
/>
);
}
function ContextCompactionTimelineRow({
row,
}: {
row: Extract<TimelineRow, { kind: "context-compaction" }>;
}) {
return (
<div
role="separator"
aria-label={row.label}
className="mx-auto flex w-full max-w-(--chat-content-max-width) items-center gap-3 py-1 text-muted-foreground text-xs"
>
<span className="h-px flex-1 bg-border/70" />
<span
ref={row.active ? observeVisibleAnimation : undefined}
className="relative shrink-0 overflow-hidden"
>
<span className="flex items-center gap-1.5">
<Minimize2Icon aria-hidden="true" className="size-3" />
{row.label}
</span>
{row.active ? (
<ActivityShimmerOverlay>
<span className="flex items-center gap-1.5">
<Minimize2Icon aria-hidden="true" className="size-3" />
{row.label}
</span>
</ActivityShimmerOverlay>
) : null}
</span>
<span className="h-px flex-1 bg-border/70" />
</div>
);
}
function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
const ctx = use(TimelineRowCtx);
const asset = useMemo(
() =>
file.downloadable === false
? null
: buildAttachmentVideoAsset(ctx.activeThreadEnvironmentId, file),
[ctx.activeThreadEnvironmentId, file.downloadable, file.id, file.mimeType, file.name],
);
const resource = asset?.resource ?? null;
const assetUrl = useAssetUrlState(ctx.activeThreadEnvironmentId, resource);
const refreshAssetUrl = useAssetUrlRefresh(ctx.activeThreadEnvironmentId, resource);
const src = assetUrl._tag === "Success" ? assetUrl.url : (file.previewUrl ?? null);
if (asset === null && src === null) {
return (
<div className="flex aspect-[4/3] w-full items-center justify-center rounded-lg border border-border/80 bg-black px-2 py-3 text-center text-2xs text-white/70">
{file.name}
</div>
);
}
return (
<MediaVideoPlayer
src={src}
sourceFailed={
file.previewUrl === undefined && resource !== null && assetUrl._tag === "Failure"
}
label={file.name}
preload="visible"
onOpen={() => {
const preview = buildAttachmentVideoPreview(ctx.activeThreadEnvironmentId, file);
if (preview) ctx.onImageExpand(preview);
}}
className="block aspect-[4/3] w-full"
videoClassName="aspect-auto size-full rounded-lg border border-border/80"
stateClassName="aspect-auto min-h-full rounded-lg border border-border/80 bg-black text-white"
onRetry={asset ? refreshAssetUrl : undefined}
actionsSource={asset ? { kind: "video", name: file.name, src, asset } : undefined}
/>
);
}
// Screen readers skim a transcript by heading, so every message announces its
// author as one. The thread title in ChatHeader is an <h2>; headings written
// inside a message are exposed below this level. Visually hidden and excluded
// from selection so sighted users and copied text are unaffected.
const MESSAGE_HEADING_LEVEL = 3;
function MessageAuthorHeading({ children }: { children: string }) {
return <h3 className="sr-only select-none">{children}</h3>;
}
function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
const ctx = use(TimelineRowCtx);
const { onImageExpand, onFileOpen } = ctx;
const senderThreadId = row.message.senderThreadId;
const resources = useMemo(
() => selectMessageImageResources(row.message.attachments),
[row.message.attachments],
);
const previewUrls = useAssetUrls(ctx.activeThreadEnvironmentId, resources);
const [projectPreviews] = useState(createMessageAttachmentPreviewProjector);
const messageWithPreviews = useMemo(() => {
const urlsById = new Map(
resources.flatMap((resource, index) => {
const url = previewUrls[index];
return url ? [[resource.attachmentId, url] as const] : [];
}),
);
return projectPreviews(row.message, (attachment) => urlsById.get(attachment.id));
}, [previewUrls, projectPreviews, resources, row.message]);
// The attachment union has an open member, so guards (not literal type
// comparisons) split it. Unknown types render as inert rows below the files.
const userImages = useMemo(
() => (messageWithPreviews.attachments ?? []).filter(isImageAttachment),
[messageWithPreviews.attachments],
);
const userFiles = useMemo(
() => (row.message.attachments ?? []).filter(isFileAttachment),
[row.message.attachments],
);
const userVideos = userFiles.filter(isVideoAttachment);
const otherUserFiles = userFiles.filter((file) => !isVideoAttachment(file));
const unknownAttachments = (row.message.attachments ?? []).filter(
(attachment) => !isImageAttachment(attachment) && !isFileAttachment(attachment),
);
const userMessage = resolveUserMessagePresentation(row.message);
const resolvedContext = useMemo(() => resolveUserMessageContext(row.message), [row.message]);
const previewImages = useMemo(
() => userImages.filter((image) => image.name.startsWith("preview-annotation-")),
[userImages],
);
const revertTurnCount = row.revertTurnCount;
// A file with a chip in the prose needs no standalone row. Media is the exception: the
// thumbnail is the only way to actually see it, so it shows whether or not it has a chip.
const chippedAttachmentIds = new Set(
collectComposerContextReferences(resolvedContext.text).flatMap((occurrence) => {
const record = asKnownContextRecord(resolvedContext.recordsById.get(occurrence.contextId));
return record?.kind === "file" || record?.kind === "image" ? [record.attachmentId] : [];
}),
);
const regularImages = userImages.filter((image) => !image.name.startsWith("preview-annotation-"));
const unchippedFiles = otherUserFiles.filter((file) => !chippedAttachmentIds.has(file.id));
const annotationRecordIds = useMemo(
() =>
resolvedContext.records
.filter((record) => record.kind === "preview-annotation")
.map((record) => record.contextId),
[resolvedContext.records],
);
// Chips read as their labels outside a draft, so a shared link carries the prose alone.
const plainText = useMemo(
() => replaceComposerContextReferences(resolvedContext.text, (reference) => reference.label),
[resolvedContext.text],
);
const promptLink = useMemo(() => promptLinkFor(plainText), [plainText]);
const contextClipboardFragment =
resolvedContext.records.length === 0
? null
: encodeComposerContextFragment({
version: 1,
source: {
environmentId: ctx.activeThreadEnvironmentId,
...(ctx.threadRef ? { threadId: ctx.threadRef.threadId } : {}),
messageId: row.message.id,
},
records: resolvedContext.records,
});
// Chips inside the selection copy as their links (data-markdown-copy); the structured
// fragment rides beside so a paste into a draft brings the payloads along. Only records
// for chips that are actually inside the selection travel, so copying prose next to an
// image never starts importing that image somewhere else.
const onBodyCopyCapture = (event: React.ClipboardEvent<HTMLDivElement>) => {
if (resolvedContext.records.length === 0 || !event.clipboardData) return;
const selection = window.getSelection();
if (!selection || selection.isCollapsed) return;
const copiedMarkdown: string[] = [];
for (let index = 0; index < selection.rangeCount; index += 1) {
const container = document.createElement("div");
container.appendChild(selection.getRangeAt(index).cloneContents());
for (const element of container.querySelectorAll("[data-markdown-copy]")) {
copiedMarkdown.push(element.getAttribute("data-markdown-copy") ?? "");
}
}
const fragment = selectedMessageContextFragment({
markdown: copiedMarkdown.join("\n"),
records: resolvedContext.records,
environmentId: ctx.activeThreadEnvironmentId,
...(ctx.threadRef ? { threadId: ctx.threadRef.threadId } : {}),
messageId: row.message.id,
});
if (!fragment) return;
// Claim the copy: without preventDefault the browser default overwrites the
// custom MIME type. The default content must then be written back explicitly.
const payload = chatMarkdownClipboardPayload(selection);
event.preventDefault();
event.clipboardData.setData("text/plain", payload?.text ?? selection.toString());
if (payload) {
event.clipboardData.setData(
"text/html",
encodeComposerContextClipboardHtml(payload.text, fragment, payload.html),
);
}
event.clipboardData.setData(COMPOSER_CONTEXT_CLIPBOARD_MIME, fragment);
};
const renderContextReference = useCallback(
(reference: ChatMarkdownContextReference) => {
const record = asKnownContextRecord(resolvedContext.recordsById.get(reference.contextId));
// Structured annotations point at the image record, which in turn points at the persisted
// attachment. Filename and order are compatibility fallbacks for legacy messages only.
const annotationImage =
record?.kind === "preview-annotation"
? resolvePreviewAnnotationImage({
record,
recordsById: resolvedContext.recordsById,
userImages,
previewImages,
annotationRecordIds,
})
: null;
const attachment =
record?.kind === "image"
? (userImages.find((image) => image.id === record.attachmentId) ?? null)
: record?.kind === "file"
? (userFiles.find((file) => file.id === record.attachmentId) ?? null)
: null;
return (
<span data-thread-find-ignore>
<UserMessageContextReferenceChip
reference={reference}
record={record}
annotationImage={annotationImage}
attachment={attachment}
onExpandImage={(image) => {
const preview = buildExpandedImagePreview(userImages, image.id);
if (preview) onImageExpand(preview);
}}
onOpenFile={onFileOpen}
onExpandVideo={(file) => {
const preview = buildAttachmentVideoPreview(ctx.activeThreadEnvironmentId, file);
if (preview) onImageExpand(preview);
}}
/>
</span>
);
},
[
resolvedContext.recordsById,
userImages,
userFiles,
previewImages,
annotationRecordIds,
onImageExpand,
onFileOpen,
ctx.activeThreadEnvironmentId,
],
);
return (
<div className="group flex flex-col items-end gap-1">
{userMessage.attribution === "automation" ? (
<p
className="me-1 text-2xs text-muted-foreground/70"
data-user-message-attribution="automation"
>
{userMessage.scheduledTaskId ? (
<Link
to="/settings/scheduled-tasks"
search={{
environmentId: ctx.activeThreadEnvironmentId,
taskId: userMessage.scheduledTaskId,
}}
className="rounded-sm hover:text-muted-foreground hover:underline focus-visible:outline-2 focus-visible:outline-ring"
>
Sent by automation
</Link>
) : (
"Sent by automation"
)}
</p>
) : userMessage.attribution === "t3code" ? (
<p
className="me-1 text-2xs text-muted-foreground/70"
data-user-message-attribution="t3code"
>
Sent by T3 Code
</p>
) : userMessage.attribution === "agent" ? (
<p className="me-1 text-2xs text-muted-foreground/70" data-user-message-attribution="agent">
{senderThreadId ? (
<InlineButton
onClick={() => ctx.onOpenThread(senderThreadId)}
tone="muted"
aria-label="Open sending thread"
>
Sent by another agent
</InlineButton>
) : (
"Sent by another agent"
)}
</p>
) : null}
{row.message.inputIntent && row.message.inputIntent !== "turn_start" ? (
<UserMessageIntentMarker intent={row.message.inputIntent} />
) : null}
<div className="relative max-w-[80%] rounded-2xl bg-message p-3 text-message-foreground">
<MessageAuthorHeading>You</MessageAuthorHeading>
{(regularImages.length > 0 || userVideos.length > 0) && (
<div className="mb-2 grid max-w-[210px] grid-cols-2 gap-2">
{regularImages.map((image) => (
<div
key={image.id}
className={cn(
"bg-background/70",
image.source?.kind === "snap-shot" && image.previewUrl
? cn(SNAP_SHOT_ATTACHMENT_FRAME_CLASS, "col-span-2")
: "aspect-[4/3] overflow-hidden rounded-lg border border-border/80",
)}
>
{image.previewUrl ? (
<button
type="button"
className="block h-full w-full cursor-zoom-in"
aria-label={`Preview ${image.name}`}
onClick={() => {
const preview = buildExpandedImagePreview(regularImages, image.id);
if (!preview) return;
ctx.onImageExpand(preview);
}}
>
<img
src={image.previewUrl}
alt={image.name}
className="block size-full object-cover"
/>
</button>
) : (
<div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-2xs text-muted-foreground/70">
{image.name}
</div>
)}
{image.previewUrl && image.source?.kind === "snap-shot" ? (
<SnapShotAttachmentDetails source={image.source} />
) : null}
</div>
))}
{userVideos.map((file) => (
<UserVideoAttachment key={file.id} file={file} />
))}
</div>
)}
{unchippedFiles.length > 0 || unknownAttachments.length > 0 ? (
<div className="mb-2 flex flex-col gap-1">
{unchippedFiles.map((file) => {
const fileIdentity = (
<>
<PierreEntryIcon pathValue={file.name} kind="file" theme={ctx.resolvedTheme} />
<span className="min-w-0 flex-1 truncate">{file.name}</span>
</>
);
if (file.downloadable !== false) {
return (
<div key={file.id} className="flex min-w-0 items-center gap-1">
<button
type="button"
aria-label={`Preview ${file.name}`}
onClick={() => ctx.onFileOpen(file)}
className="focus-visible:ring-ring/70 flex min-w-0 flex-1 cursor-pointer items-center gap-2 rounded-md py-1 text-left text-sm hover:underline focus-visible:ring-2 focus-visible:ring-inset focus-visible:outline-none"
>
{fileIdentity}
<EyeIcon className="size-4 shrink-0" />
</button>
<Tooltip>
<TooltipTrigger
render={
<Button
size="icon-xs"
variant="ghost-muted"
aria-label={`Download ${file.name}`}
onClick={() => ctx.onFileDownload(file)}
/>
}
>
<DownloadIcon />
</TooltipTrigger>
<TooltipPopup side="top">Download {file.name}</TooltipPopup>
</Tooltip>
</div>
);
}
return (
<div key={file.id} className="flex min-w-0 items-center gap-2 py-1 text-sm">
{fileIdentity}
</div>
);
})}
{unknownAttachments.map((attachment) => (
<div key={attachment.id} className="flex min-w-0 items-center gap-2 py-1 text-sm">
<PierreEntryIcon
pathValue={attachment.name}
kind="file"
theme={ctx.resolvedTheme}
/>
<span className="min-w-0 flex-1 truncate">{attachment.name}</span>
</div>
))}
</div>
) : null}
<div onCopyCapture={onBodyCopyCapture}>
<CollapsibleUserMessageBody
text={resolvedContext.text}
renderContextReference={renderContextReference}
skills={ctx.skills}
markdownCwd={ctx.markdownCwd}
/>
</div>
</div>
{row.projectedItem &&
row.projectedItem.item.status !== "completed" &&
row.projectedItem.item.status !== "pending" &&
row.projectedItem.item.status !== "waiting" ? (
<div className="me-1 flex items-center gap-1.5">
<span className="rounded-full border border-destructive/25 bg-destructive/8 px-1.5 py-0.5 text-3xs font-medium text-destructive">
{row.projectedItem.item.status}
</span>
</div>
) : null}
<div className="flex w-full max-w-[80%] items-center justify-end pe-1 text-xs tabular-nums opacity-0 transition-opacity duration-200 pointer-coarse:opacity-100 focus-within:opacity-100 group-hover:opacity-100">
<div className="flex shrink-0 items-center gap-2">
<Tooltip>
<TooltipTrigger render={<p className="text-muted-foreground text-xs tabular-nums" />}>
{formatDayAwareTimestamp(row.message.createdAt, ctx.timestampFormat)}
</TooltipTrigger>
<TooltipPopup>
{formatChatTimestampTooltip(row.message.createdAt, ctx.timestampFormat)}
</TooltipPopup>
</Tooltip>
<div className="flex items-center gap-0.5">
{typeof revertTurnCount === "number" && (
<RevertUserMessageButton turnCount={revertTurnCount} messageId={row.message.id} />
)}
{resolvedContext.text && (
<MessageCopyButton
// Structured paste needs the canonical links to retain their positions.
text={contextClipboardFragment ? resolvedContext.text : plainText}
{...(contextClipboardFragment
? {
extraFlavors: { [COMPOSER_CONTEXT_CLIPBOARD_MIME]: contextClipboardFragment },
}
: {})}
variant="ghost"
/>
)}
{promptLink !== null && (
<MessageCopyButton
text={promptLink}
label="Copy prompt link"
icon={LinkIconNode}
variant="ghost"
/>
)}
</div>
</div>
</div>
</div>
);
}
function UserMessageIntentMarker({
intent,
}: {
readonly intent: NonNullable<ChatMessage["inputIntent"]>;
}) {
const presentation =
intent === "queued_turn"
? {
label: "Queued",
icon: null,
}
: intent === "promoted_queued_to_steer"
? {
label: "Steer",
icon: Redo2Icon,
}
: {
label: "Steer",
icon: Redo2Icon,
};
const IntentIcon = presentation.icon;
return (
<Tooltip>
<TooltipTrigger
render={
<div
className="me-1 flex items-center justify-end gap-1 text-xs leading-none text-muted-foreground"
data-user-message-intent={intent}
/>
}
>
{IntentIcon ? <IntentIcon aria-hidden="true" className="size-3" /> : null}
{presentation.label}
</TooltipTrigger>
<TooltipPopup side="top">
{intent === "queued_turn"
? "Queued behind the active turn"
: intent === "promoted_queued_to_steer"
? "Originally queued, then promoted to steer the active turn"
: "Steered the active turn"}
</TooltipPopup>
</Tooltip>
);
}
export function resolvePreviewAnnotationImage(input: {
record: Extract<KnownComposerContextRecord, { kind: "preview-annotation" }>;
recordsById: ReadonlyMap<string, ComposerContextRecord>;
userImages: ReadonlyArray<ChatImageAttachment>;
previewImages: ReadonlyArray<ChatImageAttachment>;
annotationRecordIds: ReadonlyArray<string>;
}): ChatImageAttachment | null {
const screenshotRecord = input.record.screenshotContextId
? asKnownContextRecord(input.recordsById.get(input.record.screenshotContextId))
: undefined;
return (
(screenshotRecord?.kind === "image"
? input.userImages.find((image) => image.id === screenshotRecord.attachmentId)
: undefined) ??
input.previewImages.find(
(image) => image.name === `preview-annotation-${input.record.annotationId}.png`,
) ??
input.previewImages[input.annotationRecordIds.indexOf(input.record.contextId)] ??
null
);
}
function RevertUserMessageButton({
turnCount,
messageId,
}: {
turnCount: number;
messageId: MessageId;
}) {
const ctx = use(TimelineRowCtx);
const activity = use(TimelineRowActivityCtx);
return (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="xs"
variant="ghost"
disabled={activity.isRevertingCheckpoint || activity.isWorking}
onClick={() => ctx.onRevertToTurnCount(turnCount, messageId)}
aria-label="Edit from here"
/>
}
>
<Undo2Icon className="size-3" />
</TooltipTrigger>
<TooltipPopup side="top">Edit from here</TooltipPopup>
</Tooltip>
);
}
/**
* Hover-revealed wall-clock time with a full-date tooltip — the same metadata
* presentation as message rows, for work entries and turn folds. The parent
* carries `group/timeline-row`; hover or focus on an existing control reveals
* the time without adding a tab stop. Hidden timestamps stay outside the row
* layout. Visibility changes immediately so leaving flow cannot overlap text
* during a fade-out. Place it before any trailing disclosure control so
* revealing the time does not move the chevron.
*/
function TimelineRowTimestamp({
createdAt,
timestampFormat,
className,
alwaysVisible = false,
}: {
createdAt: string;
timestampFormat: TimestampFormat;
className?: string;
alwaysVisible?: boolean;
}) {
return (
<Tooltip>
<TooltipTrigger
render={
<span
className={cn(
"pointer-events-none absolute me-1 shrink-0 whitespace-nowrap rounded-md text-muted-foreground text-xs tabular-nums opacity-0 group-hover/timeline-row:pointer-events-auto group-hover/timeline-row:static group-hover/timeline-row:opacity-100 group-focus-within/timeline-row:pointer-events-auto group-focus-within/timeline-row:static group-focus-within/timeline-row:opacity-100",
alwaysVisible && "pointer-events-auto static opacity-100",
className,
)}
/>
}
>
{formatDayAwareTimestamp(createdAt, timestampFormat)}
</TooltipTrigger>
<TooltipPopup>{formatChatTimestampTooltip(createdAt, timestampFormat)}</TooltipPopup>
</Tooltip>
);
}
function TurnFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "turn-fold" }> }) {
const ctx = use(TimelineRowCtx);
return (
<div className="group/timeline-row relative flex items-center gap-1 border-b border-border/60 pb-2 pe-0.5 pt-1">
<button
type="button"
aria-expanded={row.expanded}
data-scroll-anchor-ignore
onClick={() => ctx.onToggleTurnFold(row.runId)}
className="flex cursor-pointer select-none items-center gap-1 rounded-md px-1 text-sm leading-relaxed text-muted-foreground tabular-nums transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
>
<span>{row.label}</span>
<MorphIcon className="size-3.5" icon={row.expanded ? ChevronDown : ChevronRight} />
</button>
<TimelineRowTimestamp
createdAt={row.createdAt}
timestampFormat={ctx.timestampFormat}
className="ms-auto"
/>
</div>
);
}
function AttemptFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "attempt-fold" }> }) {
const ctx = use(TimelineRowCtx);
return (
<button
type="button"
aria-expanded={row.expanded}
data-scroll-anchor-ignore
data-superseded-attempt-id={row.attemptId}
onClick={() => ctx.onToggleAttemptFold(row.attemptId)}
className="flex w-full cursor-pointer select-none items-center gap-2 rounded-md border border-border/60 bg-muted/20 px-2.5 py-2 text-left transition-colors hover:bg-muted/35 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
>
<MorphIcon
className="size-3.5 shrink-0 text-muted-foreground"
icon={row.expanded ? ChevronDown : ChevronRight}
/>
<span className="text-xs font-medium text-foreground/80">{row.label}</span>
<span className="text-2xs text-muted-foreground">Cut off by a steer</span>
</button>
);
}
function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
const ctx = use(TimelineRowCtx);
const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
return (
<>
<div className="relative min-w-0 px-1 py-0.5">
<MessageAuthorHeading>T3 Code</MessageAuthorHeading>
<div data-thread-find-text="true">
<AssistantCitationSource
messageId={row.message.id}
{...(ctx.threadRef ? { threadRef: ctx.threadRef } : {})}
itemKey={row.id}
request={ctx.citationRequest}
listRef={ctx.listRef}
>
<ChatMarkdown
text={messageText}
cwd={ctx.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
isStreaming={Boolean(row.message.streaming)}
lineBreaks={shouldPreserveAssistantLineBreaks(messageText)}
skills={ctx.skills}
headingLevelOffset={MESSAGE_HEADING_LEVEL}
onUseArtifactTemplate={ctx.onUseArtifactTemplate}
onRunShellCommand={ctx.onRunShellCommand}
onImageExpand={ctx.onImageExpand}
/>
</AssistantCitationSource>
</div>
<AssistantChangedFilesSection
turnSummary={row.assistantTurnDiffSummary}
routeThreadKey={ctx.routeThreadKey}
resolvedTheme={ctx.resolvedTheme}
onOpenTurnDiff={ctx.onOpenTurnDiff}
/>
{row.showAssistantMeta ? (
<AssistantMessageMeta
className="mt-1.5"
projectedItem={row.projectedItem}
message={row.message}
showCopyButton={row.showAssistantCopyButton}
copyStreaming={row.assistantCopyStreaming}
/>
) : null}
</div>
</>
);
}
function AssistantForkButton({
projectedItem,
}: {
readonly projectedItem: NonNullable<Extract<TimelineRow, { kind: "message" }>["projectedItem"]>;
}) {
const ctx = use(TimelineRowCtx);
const [busy, setBusy] = useState(false);
const support = useV2ItemSupport({
environmentId: ctx.activeThreadEnvironmentId,
sourceThreadId: projectedItem.sourceThreadId,
sourceItemId: projectedItem.sourceItemId,
});
const canFork = canForkProjectedAssistantItem({
projectedItem,
capabilities: support.providerSession?.capabilities,
});
if (!canFork || projectedItem.item.runId === null) return null;
const runId = projectedItem.item.runId;
return (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="xs"
variant="ghost"
disabled={busy}
onClick={() => {
setBusy(true);
void ctx
.onForkFromRun({ sourceThreadId: projectedItem.sourceThreadId, runId })
.finally(() => setBusy(false));
}}
aria-label="Fork from this response"
/>
}
>
<GitForkIcon className={cn("size-3", busy && "animate-pulse")} />
</TooltipTrigger>
<TooltipPopup side="top">Fork from this response</TooltipPopup>
</Tooltip>
);
}
function AssistantMetaTimelineRow({
row,
}: {
row: Extract<TimelineRow, { kind: "assistant-meta" }>;
}) {
return (
<div className="px-1">
<AssistantMessageMeta
className="mt-0.5"
projectedItem={row.projectedItem}
message={row.message}
showCopyButton={row.showAssistantCopyButton}
copyStreaming={row.assistantCopyStreaming}
alwaysVisible
/>
</div>
);
}
function AssistantMessageMeta({
className,
projectedItem,
message,
showCopyButton,
copyStreaming,
alwaysVisible = false,
}: {
className?: string;
projectedItem?: Extract<TimelineRow, { kind: "message" }>["projectedItem"];
message: ChatMessage;
showCopyButton: boolean;
copyStreaming: boolean;
alwaysVisible?: boolean;
}) {
const ctx = use(TimelineRowCtx);
return (
<div
className={cn(
"flex items-center gap-2 text-xs tabular-nums transition-opacity duration-200",
alwaysVisible
? "opacity-100"
: "opacity-0 pointer-coarse:opacity-100 focus-within:opacity-100 group-hover/assistant:opacity-100",
className,
)}
>
{projectedItem && projectedItem.item.status !== "completed" ? (
<span className="rounded-full border border-border/70 px-1.5 py-0.5 font-mono text-3xs text-muted-foreground">
{projectedItem.item.status}
</span>
) : null}
<AssistantCopyButton
message={message}
showCopyButton={showCopyButton}
streaming={copyStreaming}
/>
{projectedItem?.item.type === "assistant_message" ? (
<AssistantForkButton projectedItem={projectedItem} />
) : null}
{!message.streaming && (
<Tooltip>
<TooltipTrigger render={<p className="text-muted-foreground text-xs tabular-nums" />}>
{formatDayAwareTimestamp(message.updatedAt, ctx.timestampFormat)}
</TooltipTrigger>
<TooltipPopup>
{formatChatTimestampTooltip(message.updatedAt, ctx.timestampFormat)}
</TooltipPopup>
</Tooltip>
)}
</div>
);
}
function AssistantCopyButton({
message,
showCopyButton,
streaming,
}: {
message: ChatMessage;
showCopyButton: boolean;
streaming: boolean;
}) {
const assistantCopyState = resolveAssistantMessageCopyState({
text: message.text ?? null,
showCopyButton,
streaming,
});
if (!assistantCopyState.visible) {
return null;
}
return <MessageCopyButton text={assistantCopyState.text ?? ""} variant="ghost" />;
}
function ProposedPlanTimelineRow({
row,
}: {
row: Extract<TimelineRow, { kind: "proposed-plan" }>;
}) {
const ctx = use(TimelineRowCtx);
return (
<div className="min-w-0 px-1 py-0.5">
<ProposedPlanCard
planMarkdown={row.proposedPlan.planMarkdown}
environmentId={ctx.activeThreadEnvironmentId}
threadRef={ctx.threadRef ?? undefined}
cwd={ctx.markdownCwd}
workspaceRoot={ctx.workspaceRoot}
findActive={ctx.findActive}
/>
</div>
);
}
function HtmlRenderTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "html-render" }> }) {
const ctx = use(TimelineRowCtx);
return (
<div className="min-w-0 px-1">
<HtmlRenderFrame
// A recycled row must not keep another page's frozen frame.
key={row.htmlRender.attachmentId}
environmentId={ctx.activeThreadEnvironmentId}
htmlRender={row.htmlRender}
onOpen={ctx.onFileOpen}
/>
</div>
);
}
function McpAppTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "mcp-app" }> }) {
const ctx = use(TimelineRowCtx);
const { awaitingUser } = use(TimelineRowActivityCtx);
return (
<div className="min-w-0 px-1">
<McpAppFrame
// A recycled row must not keep another app's live document.
key={row.mcpApp.attachmentId}
environmentId={ctx.activeThreadEnvironmentId}
threadId={row.sourceThreadId}
conversationThreadId={ctx.threadRef?.threadId ?? row.sourceThreadId}
itemId={row.itemId}
revision={row.revision}
app={row.mcpApp}
onSendMessage={ctx.onSendAppMessage}
awaitingUser={awaitingUser}
onFullscreenChange={(fullscreen) => ctx.onAppFullscreenChange(row.id, fullscreen)}
/>
</div>
);
}
type V2EventTone = "muted" | "warning" | "danger" | "success";
function v2EventPresentation(item: OrchestrationV2TurnItem): {
readonly label: string;
readonly detail: string | null;
readonly tone: V2EventTone;
readonly icon: LucideIcon;
} {
switch (item.type) {
case "error": {
const presentation = providerErrorPresentation(item);
return {
...presentation,
tone:
item.status === "completed"
? "success"
: item.status === "running" || item.failure.class === "usage_limit"
? "warning"
: "danger",
icon: CircleAlertIcon,
};
}
case "run_interrupt_request":
return {
label: "Interrupt requested",
detail: item.message,
tone: "warning",
icon: CircleAlertIcon,
};
case "run_interrupt_result":
return {
label: "Run interrupted",
detail: item.message,
tone: "danger",
icon: XIcon,
};
case "handoff":
return {
label: "Context handoff",
detail:
item.summary ??
`${item.fromProviderInstanceIds.join(", ")} → ${item.toProviderInstanceId}`,
tone: item.status === "failed" ? "danger" : "muted",
icon: ZapIcon,
};
case "fork":
return {
label: "Conversation fork",
detail: `Continues in ${item.targetThreadId}`,
tone: "muted",
icon: GitForkIcon,
};
case "compaction": {
const tokenSummary =
item.beforeTokenCount === undefined && item.afterTokenCount === undefined
? null
: `${item.beforeTokenCount ?? "?"} → ${item.afterTokenCount ?? "?"} tokens`;
return {
label: "Context compacted",
detail: item.summary ?? tokenSummary,
tone: item.status === "failed" ? "danger" : "muted",
icon: MinusIcon,
};
}
case "todo_list": {
const steps = item.steps.map((step) => `${step.status}: ${step.text}`).join("\n");
return {
label: "Plan updated",
detail: [item.explanation, steps].filter(Boolean).join("\n\n") || null,
tone: item.status === "failed" ? "danger" : "success",
icon: CheckIcon,
};
}
default:
return {
label: item.title?.trim() || item.type.replaceAll("_", " "),
detail: null,
tone: item.status === "failed" ? "danger" : "muted",
icon: WrenchIcon,
};
}
}
function V2EventTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "event" }> }) {
const ctx = use(TimelineRowCtx);
const { item, visibility, sourceThreadId } = row.projectedItem;
if (item.type === "subagent" && (row.subagents?.length ?? 1) > 1) {
return <V2SubagentGroup key={row.id} row={row} />;
}
if (item.type === "secret_request") {
return (
<SecretRequestCard
environmentId={ctx.activeThreadEnvironmentId}
item={item}
visibility={visibility}
/>
);
}
if (isV2LifecycleItem(item)) {
return (
<V2LifecycleRow
environmentId={ctx.activeThreadEnvironmentId}
item={item}
resourceSummary={row.resourceSummary}
createdAt={row.createdAt}
timestampFormat={ctx.timestampFormat}
providerStatuses={ctx.providerStatuses}
runs={ctx.runs}
onOpenThread={ctx.onOpenThread}
/>
);
}
const presentation = v2EventPresentation(item);
const Icon = presentation.icon;
if (item.type === "error") {
return (
<details
className={cn(
"group rounded-md border",
presentation.tone === "warning" && "border-warning/25 bg-warning/5",
presentation.tone === "danger" && "border-destructive/25 bg-destructive/5",
presentation.tone === "success" && "border-success/20 bg-success/5",
)}
data-v2-item-type={item.type}
data-v2-item-visibility={visibility}
data-v2-event-disclosure="true"
>
<summary className="flex min-w-0 cursor-pointer list-none items-center gap-2 px-2.5 py-1.5 text-xs [&::-webkit-details-marker]:hidden">
<Icon
className={cn(
"size-3.5 shrink-0",
presentation.tone === "warning" && "text-warning",
presentation.tone === "danger" && "text-destructive",
presentation.tone === "success" && "text-success",
)}
/>
<span className="shrink-0 font-medium text-foreground/90">{presentation.label}</span>
{item.status !== "completed" ? (
<span
className={cn(
"shrink-0 rounded-full border px-1.5 py-0.5 font-mono text-3xs",
item.status === "failed"
? "border-destructive/40 text-destructive"
: "border-border/70 text-muted-foreground",
)}
>
{item.status}
</span>
) : null}
{presentation.detail ? (
<span className="min-w-0 flex-1 truncate text-muted-foreground/65">
{presentation.detail}
</span>
) : null}
{visibility !== "local" ? (
<span className="shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-3xs text-muted-foreground">
{visibility === "inherited" ? "Inherited" : "Synthetic"}
</span>
) : null}
<ChevronDownIcon className="size-3 shrink-0 text-muted-foreground/60 transition-transform group-open:rotate-180" />
</summary>
<div className="border-t border-border/45 px-3 py-2 ps-8">
{presentation.detail ? (
<div className="text-xs leading-relaxed text-muted-foreground">
<ChatMarkdown
text={presentation.detail}
cwd={ctx.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
skills={ctx.skills}
lineBreaks
/>
</div>
) : null}
{visibility === "inherited" ? (
<p className="mt-1 font-mono text-3xs text-muted-foreground/65">
From {sourceThreadId}
</p>
) : null}
<div className={presentation.detail ? "mt-2" : undefined}>
<V2ItemInspector
projectedItem={row.projectedItem}
environmentId={ctx.activeThreadEnvironmentId}
cwd={ctx.markdownCwd}
workspaceRoot={ctx.workspaceRoot}
onOpenThread={ctx.onOpenThread}
onOpenTurnDiff={ctx.onOpenTurnDiff}
onRollbackCheckpoint={ctx.onRollbackCheckpoint}
onImageExpand={ctx.onImageExpand}
/>
</div>
</div>
</details>
);
}
return (
<section
className={cn(
"rounded-lg border px-3 py-2",
presentation.tone === "warning" && "border-warning/25 bg-warning/5",
presentation.tone === "danger" && "border-destructive/25 bg-destructive/5",
presentation.tone === "success" && "border-success/20 bg-success/5",
presentation.tone === "muted" && "border-border/60 bg-card/30",
)}
data-v2-item-type={item.type}
data-v2-item-visibility={visibility}
>
<div className="flex items-start gap-2.5">
<Icon
className={cn(
"mt-0.5 size-3.5 shrink-0",
presentation.tone === "warning" && "text-warning",
presentation.tone === "danger" && "text-destructive",
presentation.tone === "success" && "text-success",
presentation.tone === "muted" && "text-muted-foreground",
)}
/>
<div className="min-w-0 flex-1">
<div className="flex flex-wrap items-center gap-x-2 gap-y-1">
<span className="text-xs font-medium text-foreground/90">{presentation.label}</span>
{item.status !== "completed" ? (
<span
className={cn(
"rounded-full border px-1.5 py-0.5 font-mono text-3xs",
item.status === "failed"
? "border-destructive/40 text-destructive"
: "border-border/70 text-muted-foreground",
)}
>
{item.status}
</span>
) : null}
{visibility !== "local" ? (
<span className="rounded-full bg-muted px-1.5 py-0.5 text-3xs text-muted-foreground">
{visibility === "inherited" ? "Inherited" : "Synthetic"}
</span>
) : null}
</div>
{presentation.detail ? (
<div className="mt-1 text-xs leading-relaxed text-muted-foreground">
<ChatMarkdown
text={presentation.detail}
cwd={ctx.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
skills={ctx.skills}
lineBreaks
/>
</div>
) : null}
{visibility === "inherited" ? (
<p className="mt-1 font-mono text-3xs text-muted-foreground/65">
From {sourceThreadId}
</p>
) : null}
<div className="mt-2">
<V2ItemInspector
projectedItem={row.projectedItem}
environmentId={ctx.activeThreadEnvironmentId}
cwd={ctx.markdownCwd}
workspaceRoot={ctx.workspaceRoot}
onOpenThread={ctx.onOpenThread}
onOpenTurnDiff={ctx.onOpenTurnDiff}
onRollbackCheckpoint={ctx.onRollbackCheckpoint}
onImageExpand={ctx.onImageExpand}
/>
</div>
</div>
</div>
</section>
);
}
/**
* One elapsed span for the whole group: first launch to last settle, ticking
* while any member works. A settled member without a completion time leaves
* the end unknown, so the span is withheld rather than cut short.
*/
function subagentGroupTiming(
agents: ReadonlyArray<{
status: OrchestrationV2TurnItem["status"];
startedAt: DateTime.Utc | null;
completedAt: DateTime.Utc | null;
}>,
) {
let startMs: number | null = null;
let endMs: number | null = null;
let endUnknown = false;
for (const agent of agents) {
if (agent.startedAt) {
const ms = DateTime.toEpochMillis(agent.startedAt);
startMs = startMs === null ? ms : Math.min(startMs, ms);
}
if (agent.completedAt) {
const ms = DateTime.toEpochMillis(agent.completedAt);
endMs = endMs === null ? ms : Math.max(endMs, ms);
} else {
endUnknown = true;
}
}
const live = agents.some(
({ status }) => status === "pending" || status === "running" || status === "waiting",
);
return {
status: live ? ("running" as const) : ("completed" as const),
startedAt: startMs === null ? null : new Date(startMs).toISOString(),
completedAt: live || endUnknown || endMs === null ? null : new Date(endMs).toISOString(),
};
}
const V2SubagentGroup = memo(function V2SubagentGroup({
row,
}: {
row: Extract<TimelineRow, { kind: "event" }>;
}) {
const ctx = use(TimelineRowCtx);
const groupId = `subagent-group:${row.id}`;
const [expanded, setExpanded] = useState(() =>
ctx.workGroupViewState.expandedEntries.has(groupId),
);
const members = (row.subagents ?? [row.projectedItem]).flatMap(({ item }) =>
item.type === "subagent" ? [item] : [],
);
const liveAgents = useAtomValue(
environmentThreadDetails.threadAtom(
scopeThreadRef(ctx.activeThreadEnvironmentId, row.projectedItem.item.threadId),
),
(thread) => thread?.projection.subagents,
);
const agents = members.map((item) => {
const live = liveAgents?.find((agent) => agent.id === item.subagentId);
return {
item,
status: live?.status ?? item.status,
startedAt: live?.startedAt ?? item.startedAt,
completedAt: live?.completedAt ?? item.completedAt,
};
});
const summary = subagentGroupSummary(agents);
const label = `${members.length} ${members.length === 1 ? "subagent" : "subagents"}`;
const statusSummary = summarizeSubagentStatuses(agents.map(({ status }) => status));
const toggleExpanded = (open: boolean) => {
ctx.onToggleWorkEntry(row.id, expanded);
if (open) ctx.workGroupViewState.expandedEntries.add(groupId);
else ctx.workGroupViewState.expandedEntries.delete(groupId);
setExpanded(open);
};
return (
<WorkLogBlock continues={row.continuesWorkLog}>
<Collapsible open={expanded} onOpenChange={toggleExpanded} data-subagent-group>
<CollapsibleTrigger
aria-label={label}
aria-description={statusSummary}
className={cn(
"flex w-full min-w-0 items-center gap-3 py-2 text-left transition-opacity hover:opacity-100",
expanded || summary.active
? "text-foreground opacity-100"
: "text-muted-foreground opacity-55",
)}
>
<span className="flex shrink-0 items-center -space-x-1.5" aria-hidden>
{agents.slice(0, 3).map(({ item, status }) => (
<SubagentAvatar
key={item.id}
driver={item.driver}
provider={ctx.providerStatuses.find(
(provider) => provider.instanceId === item.providerInstanceId,
)}
status={agents.length === 1 ? status : undefined}
/>
))}
{agents.length > 3 ? (
<span className="inline-flex size-6 items-center justify-center rounded-full bg-muted text-3xs font-medium text-muted-foreground ring-2 ring-background">
+{agents.length - 3}
</span>
) : null}
</span>
<span className="min-w-0 flex-1">
<span className="block text-xs font-semibold">{label}</span>
<span
className={cn(
"block truncate text-3xs text-muted-foreground",
summary.active ? "text-info" : summary.failed && "text-destructive",
)}
>
{statusSummary}
</span>
</span>
<span className="shrink-0 text-xs text-muted-foreground tabular-nums">
<SubagentElapsed agent={subagentGroupTiming(agents)} />
</span>
<ChevronDownIcon
aria-hidden
className={cn(
"size-3.5 shrink-0 text-muted-foreground transition-transform",
expanded && "rotate-180",
)}
/>
</CollapsibleTrigger>
{/* Virtualized rows must settle before disclosure scroll anchoring resumes. */}
<CollapsiblePanel animate={false}>
{expanded ? (
<div className="mt-1 mb-1 rounded-lg border border-border/60 bg-card/30 p-1">
{members.map((item) => (
<V2LifecycleRow
environmentId={ctx.activeThreadEnvironmentId}
key={item.id}
item={item}
createdAt={row.createdAt}
timestampFormat={ctx.timestampFormat}
providerStatuses={ctx.providerStatuses}
runs={ctx.runs}
onOpenThread={ctx.onOpenThread}
/>
))}
</div>
) : null}
</CollapsiblePanel>
</Collapsible>
</WorkLogBlock>
);
});
// ---------------------------------------------------------------------------
// Extracted row sections — own their state / store subscriptions so changes
// re-render only the affected row, not the entire list.
// ---------------------------------------------------------------------------
/** Renders standalone activity or one bounded, virtualized expanded tool group. */
const WorkGroupSection = memo(function WorkGroupSection({
anchorKey,
disclosureAnchorKey = anchorKey,
groupedEntries,
isExpandedToolGroup,
displayLabel,
}: {
anchorKey: string;
disclosureAnchorKey?: string;
groupedEntries: Extract<MessagesTimelineRow, { kind: "work" }>["groupedEntries"];
isExpandedToolGroup: boolean;
displayLabel?: string | undefined;
}) {
const { workspaceRoot, routeThreadKey, onToggleWorkEntry } = use(TimelineRowCtx);
const onToggleStandaloneEntry = useCallback(
(collapsed: boolean) => onToggleWorkEntry(disclosureAnchorKey, collapsed),
[disclosureAnchorKey, onToggleWorkEntry],
);
const nonEmptyEntries = useMemo(
() => groupedEntries.filter((entry) => workEntryIsVisibleInGroup(entry, isExpandedToolGroup)),
[groupedEntries, isExpandedToolGroup],
);
if (nonEmptyEntries.length === 0) return null;
if (isExpandedToolGroup && nonEmptyEntries.every((entry) => entry.itemType === "reasoning")) {
return <ReasoningTraceContent entries={nonEmptyEntries} />;
}
if (isExpandedToolGroup) {
return (
<ExpandedWorkGroupEntries
key={`${routeThreadKey}:${anchorKey}`}
anchorKey={anchorKey}
disclosureAnchorKey={disclosureAnchorKey}
entries={nonEmptyEntries}
workspaceRoot={workspaceRoot}
/>
);
}
return (
<section aria-label="Activity">
{nonEmptyEntries.map((workEntry) => (
<SimpleWorkEntryRow
key={workEntry.id}
workEntry={workEntry}
workspaceRoot={workspaceRoot}
displayLabel={displayLabel}
onToggleEntry={onToggleStandaloneEntry}
/>
))}
</section>
);
});
function formatWorkingTimer(startIso: string, endIso: string): string | null {
const startedAtMs = Date.parse(startIso);
const endedAtMs = Date.parse(endIso);
if (!Number.isFinite(startedAtMs) || !Number.isFinite(endedAtMs)) {
return null;
}
const elapsedSeconds = Math.max(0, Math.floor((endedAtMs - startedAtMs) / 1000));
if (elapsedSeconds < 60) {
return `${elapsedSeconds}s`;
}
return formatDuration(elapsedSeconds * 1_000);
}
function formatWorkingTimerNow(startIso: string): string {
return formatWorkingTimer(startIso, new Date().toISOString()) ?? "0s";
}
function WorkingTimer({ createdAt }: { createdAt: string }) {
const textRef = useRef<HTMLSpanElement>(null);
const initialText = formatWorkingTimerNow(createdAt);
useEffect(() => {
const updateText = () => {
if (textRef.current) {
textRef.current.textContent = formatWorkingTimerNow(createdAt);
}
};
updateText();
const id = setInterval(updateText, 1000);
return () => clearInterval(id);
}, [createdAt]);
return (
<span ref={textRef} className="tabular-nums">
{initialText}
</span>
);
}
// Matches the grouped WorkLog row's min-h-6.
const compactWorkEntryHeight = 24;
function ExpandedWorkGroupEntries({
anchorKey,
disclosureAnchorKey,
entries,
workspaceRoot,
}: {
anchorKey: string;
disclosureAnchorKey: string;
entries: TimelineWorkEntry[];
workspaceRoot: string | undefined;
}) {
const { workGroupViewState: viewState, onToggleWorkEntry } = use(TimelineRowCtx);
const [initialScrollIndex] = useState(() =>
resolveWorkGroupScrollIndex(entries, viewState.scrollPositions.get(anchorKey)),
);
const [restoringPosition, setRestoringPosition] = useState(initialScrollIndex !== undefined);
const listRef = useRef<LegendListRef>(null);
const [expandedContentHeight, setExpandedContentHeight] = useState(0);
const [fades, setFades] = useState({ top: false, bottom: false, viewportHeight: 0 });
const [appendState, setAppendState] = useState({ entries, follow: false });
// Capture the pre-change edge once per incoming array, before new layout
// metrics arrive. Edge/viewport changes never turn a status update into a follow.
if (appendState.entries !== entries) {
setAppendState({
entries,
follow:
fades.viewportHeight > 0 &&
shouldFollowWorkGroupAppend(appendState.entries, entries, fades.bottom ? Infinity : 0),
});
}
const groupView = useMemo(
() => ({
state: viewState,
onToggleEntry: (collapsed: boolean) => onToggleWorkEntry(disclosureAnchorKey, collapsed),
}),
[disclosureAnchorKey, onToggleWorkEntry, viewState],
);
const updateScrollFades = useCallback(() => {
const element = listRef.current?.getScrollableNode();
if (!element) return;
const distanceFromEnd = element.scrollHeight - element.clientHeight - element.scrollTop;
const viewportHeight = element.clientHeight;
const top = element.scrollTop > 1;
const bottom = distanceFromEnd > 1;
setFades((previous) =>
previous.top === top &&
previous.bottom === bottom &&
previous.viewportHeight === viewportHeight
? previous
: { top, bottom, viewportHeight },
);
}, []);
const handleScroll = useCallback(() => {
const state = listRef.current?.getState();
const position = state && resolveWorkGroupScrollAnchor(state);
if (position) {
viewState.scrollPositions.set(anchorKey, {
entryId: position.rowId,
offset: position.offsetWithinRow,
});
}
updateScrollFades();
}, [anchorKey, updateScrollFades, viewState]);
const handleLoad = useCallback(() => {
const list = listRef.current;
const element = list?.getScrollableNode();
if (initialScrollIndex && list && element) {
// Bootstrap can report the restored target before the DOM has applied it.
// Reconcile once at load, before releasing the measured anchor row.
const offset = Math.max(
0,
Math.min(list.getState().scroll, element.scrollHeight - element.clientHeight),
);
if (Math.abs(element.scrollTop - offset) > 1) {
void list.scrollToOffset({ offset, animated: false });
}
}
setRestoringPosition(false);
}, [initialScrollIndex]);
useLayoutEffect(() => {
const element = listRef.current?.getScrollableNode();
if (!element) return;
updateScrollFades();
const content = element.firstElementChild;
return observeResize(content ? [element, content] : element, updateScrollFades);
}, [updateScrollFades]);
const renderEntry = useCallback(
({ item }: { item: TimelineWorkEntry }) => (
<SimpleWorkEntryRow key={item.id} workEntry={item} workspaceRoot={workspaceRoot} />
),
[workspaceRoot],
);
const updateExpandedContentHeight = useCallback(() => {
const state = listRef.current?.getState();
let height = 0;
for (const entryId of viewState.expandedEntries) {
if (state?.indexByKey(entryId) === undefined) continue;
// Each open row adds room for its details, including while scrolled out of view.
height += Math.max(
0,
(state.sizes.get(entryId) ?? compactWorkEntryHeight) - compactWorkEntryHeight,
);
}
setExpandedContentHeight(height);
}, [viewState]);
useLayoutEffect(updateExpandedContentHeight, [entries, updateExpandedContentHeight]);
return (
<WorkGroupViewCtx value={groupView}>
<WorkLogList>
<LegendList
ref={listRef}
data={entries}
extraData={workspaceRoot}
keyExtractor={workEntryKey}
renderItem={renderEntry}
estimatedItemSize={compactWorkEntryHeight}
drawDistance={240}
recycleItems
{...(initialScrollIndex ? { initialScrollIndex } : {})}
maintainScrollAtEnd={
appendState.follow ? { animated: false, on: { dataChange: true } } : false
}
maintainScrollAtEndThreshold={1 / Math.max(1, fades.viewportHeight)}
// Measure the restored row even when an intra-row offset puts its
// estimated bounds outside the list's small bootstrap render window.
{...(restoringPosition && initialScrollIndex
? { alwaysRender: { indices: [initialScrollIndex.index] } }
: {})}
maintainVisibleContentPosition
onLoad={handleLoad}
onScroll={handleScroll}
onLayout={updateScrollFades}
onItemSizeChanged={updateExpandedContentHeight}
tabIndex={0}
role="region"
aria-label="Tool calls"
data-tool-group-scroll
style={{ maxHeight: `calc(min(18rem, 50dvh) + ${expandedContentHeight}px)` }}
className={cn(
"scrollbar-gutter-stable scroll-py-6 overflow-x-hidden rounded-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
getVirtualizedScrollFadeClassName(fades),
)}
/>
</WorkLogList>
</WorkGroupViewCtx>
);
}
const workEntryKey = (entry: TimelineWorkEntry) => entry.id;
function ActivityShimmerOverlay({ children }: { children: ReactNode }) {
return (
<span
aria-hidden
className="live-activity-focus pointer-events-none absolute inset-y-0 select-none"
>
<span className="live-activity-focus-counter block">
<span className="live-activity-focus-aligned block text-foreground">{children}</span>
</span>
</span>
);
}
const failedToolIconClassName = "text-tool-error-icon/40";
/** Image icons and the gradient computer-use mark cannot take a currentColor
* tint, so failed rows using them get a trailing x instead. */
function toolIconAcceptsTint(
iconName: WorkEntryIconName,
toolIcon: ToolActivityIcon | undefined,
): boolean {
return toolIcon === undefined && iconName !== "computer";
}
function WorkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "working" }> }) {
const { isCompacting, isPreparingWorktree, backgroundWorktreeSetup } =
use(TimelineRowActivityCtx);
// One span for every label so the setup-to-working handoff swaps text in
// place instead of remounting the row.
const shimmer = isPreparingWorktree || isCompacting;
const label = isPreparingWorktree ? (
"Setting up worktree…"
) : isCompacting ? (
<CompactingLabel />
) : row.createdAt ? (
<>
Working for <WorkingTimer createdAt={row.createdAt} />
</>
) : (
"Working..."
);
return (
<div className="border-b border-border/60 pb-2 pt-1">
<div className="flex h-6 min-w-0 items-baseline gap-2 px-1 text-sm leading-relaxed text-muted-foreground tabular-nums">
<span
ref={shimmer ? observeVisibleAnimation : undefined}
className="relative shrink-0 overflow-hidden whitespace-nowrap"
>
{label}
{shimmer ? <ActivityShimmerOverlay>{label}</ActivityShimmerOverlay> : null}
</span>
{backgroundWorktreeSetup ? (
<BackgroundWorktreeSetupChip snapshot={backgroundWorktreeSetup} />
) : null}
</div>
</div>
);
}
/**
* Trailing chip in the working header while a setup script still runs after
* the agent started. Opens the stage list and live output in a popover; the
* chip leaves with the script, so nothing lingers in the timeline.
*/
function BackgroundWorktreeSetupChip({ snapshot }: { snapshot: WorktreeSetupSnapshot }) {
const ctx = use(TimelineRowCtx);
const terminalId = snapshot.setupScript?.terminalId ?? null;
const openTerminal = ctx.onOpenWorktreeSetupTerminal;
const onOpenTerminal = useMemo(
() => (openTerminal && terminalId ? () => openTerminal(terminalId) : null),
[openTerminal, terminalId],
);
const scriptName = snapshot.setupScript?.name ?? "Setup script";
return (
<Popover>
<PopoverTrigger
render={
<Button
variant="ghost-muted"
size="micro"
className="ml-auto min-w-0 shrink-0"
aria-label={`${scriptName} is still running. Show setup progress.`}
/>
}
>
<Spinner className="size-3 shrink-0" />
<span className="truncate">{scriptName}</span>
</PopoverTrigger>
<PopoverPopup side="bottom" align="end" width="lg" padding="compact">
<WorktreeSetupCard
snapshot={snapshot}
embedded
onCancel={null}
onWorkLocally={null}
onOpenTerminal={onOpenTerminal}
/>
</PopoverPopup>
</Popover>
);
}
function CompactingLabel() {
return (
<span className="inline-flex items-center gap-1.5">
<Minimize2Icon aria-hidden="true" className="size-3" />
Compacting…
</span>
);
}
function ThinkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "thinking" }> }) {
const ctx = use(TimelineRowCtx);
const { isCompacting, isPreparingWorktree } = use(TimelineRowActivityCtx);
// Reserve the activity row during setup so the handoff keeps the same height.
if (isPreparingWorktree || isCompacting) return <WorkLogRow label="" />;
const activity = <LiveActivityRow label="Thinking" iconName="brain" active shimmer />;
const { groupId } = row;
if (groupId === undefined) return activity;
return (
<button
type="button"
className="group/live-work flex min-h-6 w-full max-w-full cursor-pointer items-center rounded-md text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
aria-expanded={row.expanded === true}
onClick={() => ctx.onToggleWorkGroup(groupId, row.id)}
>
{activity}
</button>
);
}
function LiveActivityRow({
label,
iconName,
toolIcon,
failed = false,
active = false,
shimmer = false,
}: {
label: ReactNode;
iconName?: WorkEntryIconName;
toolIcon?: ToolActivityIcon | undefined;
failed?: boolean;
active?: boolean;
shimmer?: boolean;
}) {
const animated = active && !failed;
const showShimmer = animated && shimmer;
return (
<div
ref={animated ? observeVisibleAnimation : undefined}
className="relative min-h-6 w-fit max-w-full min-w-0 overflow-hidden rounded-md text-sm leading-relaxed"
>
<LiveActivityContent
label={label}
iconName={iconName}
toolIcon={toolIcon}
failed={failed}
announceFailure={failed}
active={animated && !shimmer}
/>
{showShimmer ? (
<ActivityShimmerOverlay>
<LiveActivityContent label={label} iconName={iconName} toolIcon={toolIcon} highlighted />
</ActivityShimmerOverlay>
) : null}
</div>
);
}
function LiveActivityContent({
label,
iconName,
toolIcon,
failed = false,
announceFailure = false,
active = false,
highlighted = false,
}: {
label: ReactNode;
iconName: WorkEntryIconName | undefined;
toolIcon?: ToolActivityIcon | undefined;
failed?: boolean;
announceFailure?: boolean;
active?: boolean;
highlighted?: boolean;
}) {
const showTrailingFailureMark =
failed && iconName !== undefined && !toolIconAcceptsTint(iconName, toolIcon);
return (
<WorkLogRow
icon={
iconName ? (
<span
className={cn(
"flex size-4 items-center justify-center",
failed
? failedToolIconClassName
: highlighted
? "text-foreground"
: "text-icon-muted",
)}
role={announceFailure ? "img" : undefined}
aria-label={announceFailure ? "Tool call failed" : undefined}
>
<ToolActivityIconView
icon={toolIcon}
fallbackName={iconName}
className="block size-4 shrink-0 stroke-2"
muted={!highlighted}
/>
</span>
) : null
}
label={
<span
className={cn(
"block truncate",
highlighted && "text-foreground",
active && "live-tool-shine",
)}
>
{label}
</span>
}
trailing={
showTrailingFailureMark ? (
<XIcon aria-hidden className={cn("size-3 shrink-0", failedToolIconClassName)} />
) : null
}
/>
);
}
/** The thread a `t3_thread_read` call targets, titled from live shell state so renames show. */
function useThreadReadTarget(entry: TimelineWorkEntry, environmentId: EnvironmentId) {
const rawThreadId = threadReadTargetId(entry);
const threadId = rawThreadId === null ? null : ThreadId.make(rawThreadId);
const shell = useThreadShell(threadId ? scopeThreadRef(environmentId, threadId) : null);
const title = threadReadTargetTitle(shell);
return threadId && title ? { threadId, title } : null;
}
function threadReadLabel(label: string, target: ReturnType<typeof useThreadReadTarget>) {
const prefix = target && threadReadLabelPrefix(label);
return prefix ? { ...target, prefix, text: `${prefix} “${target.title}”` } : null;
}
/** Only settled rows link the title; the live row is itself a button. */
function ThreadReadLabel({
label,
environmentId,
linked,
}: {
label: NonNullable<ReturnType<typeof threadReadLabel>>;
environmentId: EnvironmentId;
linked: boolean;
}) {
return (
<span className="flex min-w-0">
<span className="shrink-0">{label.prefix} “</span>
{linked ? (
<Link
to="/$environmentId/$threadId"
params={{ environmentId, threadId: label.threadId }}
className="min-w-0 truncate rounded-sm text-foreground underline-offset-2 hover:underline focus-visible:outline-2 focus-visible:outline-ring"
onClick={stopRowToggle}
onKeyDown={stopRowToggle}
>
{label.title}
</Link>
) : (
<span className="min-w-0 truncate">{label.title}</span>
)}
<span className="shrink-0">”</span>
</span>
);
}
function LiveWorkEntryTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "work-live" }> }) {
const ctx = use(TimelineRowCtx);
const threadTarget = useThreadReadTarget(row.entry, ctx.activeThreadEnvironmentId);
const questionHeading = row.entry.questionAnswer
? getQuestionTextPreview(row.entry.questionAnswer)
: "";
const label = questionHeading || liveWorkEntryLabel(row.entry, ctx.workspaceRoot, row.active);
const threadLabel = threadReadLabel(label, threadTarget);
const failed = workEntryDisplayIndicatesToolFailure(row.entry);
// The expanded group already lists the thought, so the preview steps aside.
const thoughtLine = row.thought && !row.expanded ? liveThoughtLine(row.thought.detail ?? "") : "";
// While the thought itself streams, the status line just says Thinking.
const thoughtIsStatus = thoughtLine !== "" && row.entry === row.thought;
return (
<button
type="button"
className={cn(
"group/live-work flex w-full max-w-full cursor-pointer rounded-md text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
thoughtLine ? "flex-col items-start" : "min-h-6 items-center",
)}
aria-label={
failed
? `${thoughtLine ? `${thoughtLine} ` : ""}${threadLabel?.text ?? label}, tool call failed`
: undefined
}
aria-expanded={row.expanded}
onClick={() => ctx.onToggleWorkGroup(row.groupId, row.id)}
>
{thoughtLine ? (
// The latest thought's first sentence sits above the status line as an
// ordinary work-log row, so a finding never hides behind the next tool call.
<WorkLogRow
icon={
<span className="flex size-4 items-center justify-center text-icon-muted">
<ToolActivityIconView
icon={undefined}
fallbackName="brain"
className="block size-4 shrink-0 stroke-2"
muted
/>
</span>
}
label={<span className="line-clamp-4">{thoughtLine}</span>}
wrapLabel
/>
) : null}
<LiveActivityRow
label={
thoughtIsStatus ? (
row.active ? (
"Thinking"
) : (
"Thought"
)
) : row.entry.questionAnswer && hasQuestionAnswer(row.entry.questionAnswer) ? (
<span className="flex min-w-0 gap-1.5">
<span className="min-w-0 truncate">{label}</span>
<span className="min-w-0 truncate text-foreground">
{getQuestionAnswerPreview(row.entry.questionAnswer)}
</span>
</span>
) : row.entry.itemType === "reasoning" ? (
<ReactMarkdown
remarkPlugins={[
remarkGfm,
[remarkThoughtPreview, row.active ? "Thinking" : "Thought"],
]}
>
{row.entry.detail ?? label}
</ReactMarkdown>
) : threadLabel ? (
<ThreadReadLabel
label={threadLabel}
environmentId={ctx.activeThreadEnvironmentId}
linked={false}
/>
) : (
label
)
}
iconName={workEntryIconName(row.entry)}
toolIcon={row.entry.toolIcon ?? row.entry.toolSource?.icon}
failed={failed}
active={row.active}
/>
</button>
);
}
function toolGroupSummaryIconName(
kind: Extract<TimelineRow, { kind: "work-toggle" }>["summaryKind"],
): WorkEntryIconName {
switch (kind) {
case "pull-request":
case "link-pr":
case "unlink-pr":
case "list-prs":
case "watch-pr":
case "unwatch-pr":
return "pull-request";
case "read":
return "eye";
case "edit":
return "square-pen";
case "command":
return "terminal";
case "thread-create":
return "t3-code";
case "browser":
return "browser";
case "device":
return "device";
case "search":
return "globe";
case "code-search":
return "search";
case "other":
return "wrench";
case "dynamic-tool":
return "hammer";
case "reasoning":
return "brain";
case "agent-tool":
return "bot";
case "tone-tool":
return "zap";
case "update":
case "mixed":
return "hammer";
}
}
function WorkGroupToggleTimelineRow({
row,
}: {
row: Extract<TimelineRow, { kind: "work-toggle" }>;
}) {
const ctx = use(TimelineRowCtx);
return (
<WorkGroupHeader
label={row.summary}
iconName={row.summaryToolIcon ?? row.toolSurface ?? toolGroupSummaryIconName(row.summaryKind)}
toolIcon={row.toolIcon}
failed={row.hasFailure}
expanded={row.expanded}
createdAt={row.createdAt}
timestampFormat={ctx.timestampFormat}
onToggle={() => ctx.onToggleWorkGroup(row.groupId, row.id)}
/>
);
}
function WorkGroupHeader(props: {
label: string;
iconName: WorkEntryIconName;
toolIcon?: ToolActivityIcon | undefined;
failed?: boolean | undefined;
active?: boolean | undefined;
expanded: boolean;
createdAt: string;
timestampFormat: TimestampFormat;
onToggle: () => void;
}) {
return (
<WorkLogButton
ref={props.active && !props.failed ? observeVisibleAnimation : undefined}
aria-label={props.failed ? `${props.label}, tool call failed` : props.label}
aria-expanded={props.expanded}
onClick={props.onToggle}
icon={
<ToolActivityIconView
icon={props.toolIcon}
fallbackName={props.iconName}
className="size-4 shrink-0 stroke-2 text-icon-muted"
muted
/>
}
label={
<span className={cn("block truncate", props.active && !props.failed && "live-tool-shine")}>
{props.label}
</span>
}
trailing={
<TimelineRowTimestamp createdAt={props.createdAt} timestampFormat={props.timestampFormat} />
}
/>
);
}
/** Subscribes directly to the UI state store for expand/collapse state,
* so toggling re-renders only this component — not the entire list. */
const AssistantChangedFilesSection = memo(function AssistantChangedFilesSection({
turnSummary,
routeThreadKey,
resolvedTheme,
onOpenTurnDiff,
}: {
turnSummary: TurnDiffSummary | undefined;
routeThreadKey: string;
resolvedTheme: "light" | "dark";
displayThreadKey?: string;
onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
}) {
if (!turnSummary) return null;
const checkpointFiles = turnSummary.files;
if (checkpointFiles.length === 0) return null;
return (
<AssistantChangedFilesSectionInner
turnSummary={turnSummary}
checkpointFiles={checkpointFiles}
routeThreadKey={routeThreadKey}
resolvedTheme={resolvedTheme}
onOpenTurnDiff={onOpenTurnDiff}
/>
);
});
/** Inner component that only mounts when there are actual changed files,
* so the store subscription is unconditional (no hooks after early return). */
function AssistantChangedFilesSectionInner({
turnSummary,
checkpointFiles,
routeThreadKey,
resolvedTheme,
onOpenTurnDiff,
}: {
turnSummary: TurnDiffSummary;
checkpointFiles: TurnDiffSummary["files"];
routeThreadKey: string;
resolvedTheme: "light" | "dark";
displayThreadKey?: string;
onOpenTurnDiff: (runId: RunId, filePath?: string) => void;
}) {
const ctx = use(TimelineRowCtx);
const persistedExpanded = useUiStateStore(
(store) => store.threadChangedFilesExpandedById[routeThreadKey]?.[turnSummary.runId],
);
const setExpanded = useUiStateStore((store) => store.setThreadChangedFilesExpanded);
const allDirectoriesExpanded = persistedExpanded ?? false;
const thread = useThreadShell(ctx.threadRef);
const activeProject = useProject(
thread && thread.projectId
? { environmentId: thread.environmentId, projectId: thread.projectId }
: null,
);
const onFileContextMenu = useFileContextMenuHandler(ctx.activeThreadEnvironmentId);
return (
<ChangedFilesCard
runId={turnSummary.runId}
files={checkpointFiles}
allDirectoriesExpanded={allDirectoriesExpanded}
resolvedTheme={resolvedTheme}
onToggleAllDirectories={() =>
setExpanded(routeThreadKey, turnSummary.runId, !allDirectoriesExpanded)
}
onOpenTurnDiff={onOpenTurnDiff}
onFileContextMenu={(filePath, event) =>
onFileContextMenu(
{
environmentId: ctx.activeThreadEnvironmentId,
filePath,
workspaceRoot: ctx.workspaceRoot,
repositoryRoot:
thread?.worktreePath == null
? activeProject?.repositoryIdentity?.rootPath
: undefined,
},
event,
)
}
/>
);
}
// ---------------------------------------------------------------------------
// Leaf components
// ---------------------------------------------------------------------------
function UserMessageMentionChip(props: {
record: Extract<KnownComposerContextRecord, { kind: "mention" }>;
copyMarkdown: string;
}) {
const ctx = use(TimelineRowCtx);
return (
<Tooltip>
<TooltipTrigger
render={
<ContextChip
kind="mention"
render={<button type="button" />}
aria-label={`Preview ${props.record.path}`}
data-markdown-copy={props.copyMarkdown}
onClick={() => {
if (ctx.threadRef)
useRightPanelStore.getState().openFile(ctx.threadRef, props.record.path);
}}
>
<WorkspaceEntryIcon
path={props.record.path}
environmentId={ctx.activeThreadEnvironmentId}
cwd={ctx.workspaceRoot}
theme={ctx.resolvedTheme}
/>
<ContextChipLabel>{props.record.label}</ContextChipLabel>
</ContextChip>
}
/>
<TooltipPopup>
<WorkspaceEntryTooltip
path={props.record.path}
environmentId={ctx.activeThreadEnvironmentId}
cwd={ctx.workspaceRoot}
/>
</TooltipPopup>
</Tooltip>
);
}
function UserMessageContextChip(props: {
icon: ReactNode;
label: string;
kindLabel?: string;
copyMarkdown: string;
tooltip?: string;
kind: ContextChipKind;
}) {
return (
<ContextChipShell
kind={props.kind}
icon={props.icon}
label={props.label}
aria-label={props.kindLabel ? `${props.kindLabel}, ${props.label}` : undefined}
data-markdown-copy={props.copyMarkdown}
tooltip={props.tooltip}
/>
);
}
function UserMessagePullRequestContextChip(props: {
record: Extract<KnownComposerContextRecord, { kind: "review-comment" }>;
copyMarkdown: string;
kind: ContextChipKind;
}) {
const { activeThreadEnvironmentId, openPullRequest } = use(TimelineRowCtx);
const metadata = props.record.pullRequest;
if (metadata === undefined) return null;
return (
<PullRequestChip
metadata={metadata}
environmentId={activeThreadEnvironmentId}
label={reviewCommentContextLabel(props.record)}
kindLabel={pullRequestContextKindLabel(props.record)}
kind={props.kind}
copyMarkdown={props.copyMarkdown}
onOpen={openPullRequest}
/>
);
}
function UserMessagePreviewAnnotationDetails(props: {
record: Extract<KnownComposerContextRecord, { kind: "preview-annotation" }>;
image: ChatImageAttachment | null;
}) {
const ctx = use(TimelineRowCtx);
const visibleElements = props.record.elements ?? [];
return (
<div className="max-w-full overflow-hidden rounded-lg border border-border/70 bg-background/70">
{props.image?.previewUrl ? (
<button
type="button"
className="block max-h-64 w-full cursor-zoom-in overflow-hidden border-b border-border/70 bg-muted"
aria-label={`Preview ${props.image.name}`}
onClick={() => {
if (!props.image) return;
const preview = buildExpandedImagePreview([props.image], props.image.id);
if (preview) ctx.onImageExpand(preview);
}}
>
<img
src={props.image.previewUrl}
alt="Annotated preview crop"
className="max-h-64 w-full object-contain"
/>
</button>
) : (
<div className="border-b border-border/70 bg-muted/40 px-3 py-2 text-secondary-label text-xs">
Screenshot unavailable
</div>
)}
<div className="min-w-0 px-3 py-2.5">
<div className="text-message-foreground text-xs font-medium">
{props.record.pageTitle?.trim() || props.record.pageUrl || "Preview annotation"}
</div>
{props.record.comment ? (
<div className="mt-1 whitespace-pre-wrap wrap-break-word text-sm">
{props.record.comment}
</div>
) : null}
<div className="mt-1 flex items-center gap-2 text-secondary-label text-3xs">
{props.record.targetSummary ? (
<span className="truncate">{props.record.targetSummary}</span>
) : null}
{(props.record.styleChanges?.length ?? 0) > 0 ? (
<span className="inline-flex shrink-0 items-center gap-1">
<PaintbrushIcon className="size-3" />
{props.record.styleChanges?.length ?? 0}
</span>
) : null}
</div>
{visibleElements.length > 0 ? (
<div className="mt-2 space-y-2 border-t border-border/60 pt-2">
{visibleElements.map((element) => {
const source = element.source;
const sourceLabel = source?.fileName
? `${source.fileName}${source.lineNumber === null ? "" : `:${source.lineNumber}`}`
: null;
return (
<div
key={`${element.selector}\u0000${element.tagName}\u0000${sourceLabel ?? ""}\u0000${element.htmlPreview}`}
className="min-w-0"
>
<div className="flex min-w-0 items-center gap-2 text-xs">
<code className="truncate text-message-foreground">
{element.selector || `<${element.tagName}>`}
</code>
{sourceLabel ? (
<span className="ml-auto shrink-0 text-secondary-label">{sourceLabel}</span>
) : null}
</div>
{element.htmlPreview?.trim() ? (
<pre className="mt-1 max-h-24 overflow-auto whitespace-pre-wrap rounded bg-muted/60 px-2 py-1.5 text-3xs leading-relaxed">
{element.htmlPreview.trim()}
</pre>
) : null}
</div>
);
})}
{(props.record.elements?.length ?? 0) > visibleElements.length ? (
<div className="text-secondary-label text-3xs">
{(props.record.elements?.length ?? 0) - visibleElements.length} more selected
elements
</div>
) : null}
</div>
) : null}
</div>
</div>
);
}
function UserMessageElementDetails({
record,
}: {
record: Extract<KnownComposerContextRecord, { kind: "element" }>;
}) {
const sourceLabel = record.source?.fileName
? `${record.source.fileName}${record.source.lineNumber === null ? "" : `:${record.source.lineNumber}`}`
: null;
return (
<div className="max-w-full overflow-hidden rounded-lg border border-border/70 bg-background/70">
<div className="border-b border-border/70 px-3 py-2.5">
<div className="truncate text-message-foreground text-xs font-medium">
{record.pageTitle?.trim() || record.pageUrl}
</div>
<div className="mt-0.5 truncate text-secondary-label text-3xs">{record.pageUrl}</div>
</div>
<div className="space-y-2 px-3 py-2.5">
<div className="flex min-w-0 items-center gap-2 text-xs">
<code className="truncate text-message-foreground">
{record.selector || `<${record.tagName}>`}
</code>
{sourceLabel ? (
<span className="ml-auto shrink-0 text-secondary-label">{sourceLabel}</span>
) : null}
</div>
{record.htmlPreview?.trim() ? (
<div className="flex h-40 flex-col overflow-hidden rounded border border-border">
<ReadOnlySourcePreview name="element.html" text={record.htmlPreview} />
</div>
) : null}
{record.styles?.trim() ? (
<div className="flex h-32 flex-col overflow-hidden rounded border border-border">
<ReadOnlySourcePreview name="styles.css" text={record.styles} />
</div>
) : null}
</div>
</div>
);
}
interface UserMessageContextRenderContext {
reference: ChatMarkdownContextReference;
annotationImage: ChatImageAttachment | null;
attachment: ChatImageAttachment | ChatFileAttachment | null;
resolvedTheme: "light" | "dark";
copyMarkdown: string;
onExpandImage: (image: ChatImageAttachment) => void;
onExpandVideo: (file: ChatFileAttachment) => void;
onOpenFile: (file: ChatFileAttachment) => void;
}
function UnavailableUserMessageContextChip(props: UserMessageContextRenderContext) {
return (
<UnresolvedChip
label={props.reference.label}
copyMarkdown={props.copyMarkdown}
tooltip="This context is no longer available."
/>
);
}
const userMessageContextPresentationRegistry = createContextPresentationRegistry<
KnownComposerContextRecord,
UserMessageContextRenderContext,
ReactNode
>({
requiredKinds: COMPOSER_CONTEXT_KINDS,
handlers: [
{
kind: "mention",
canRender: (record) => record.kind === "mention",
render: (record, context) =>
record.kind === "mention" ? (
<UserMessageMentionChip record={record} copyMarkdown={context.copyMarkdown} />
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
{
kind: "skill",
canRender: (record) => record.kind === "skill",
render: (record, context) =>
record.kind === "skill" ? (
<UserMessageContextChip
icon={<SkillChipIcon />}
label={record.label || record.name}
kindLabel="Skill"
tooltip={`$${record.name}`}
copyMarkdown={context.copyMarkdown}
kind="skill"
/>
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
{
kind: "thread",
canRender: (record) => record.kind === "thread",
render: (record, context) =>
record.kind === "thread" ? (
<ThreadContextChip record={record} copyMarkdown={context.copyMarkdown} />
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
{
kind: "image",
canRender: (record, context) =>
record.kind === "image" &&
context.attachment !== null &&
isImageAttachment(context.attachment),
render: (record, context) => {
if (
record.kind !== "image" ||
context.attachment === null ||
!isImageAttachment(context.attachment)
) {
return <UnavailableUserMessageContextChip {...context} />;
}
const attachment = context.attachment;
return (
<ImageChipButton
name={record.name}
previewUrl={attachment.previewUrl}
size={formatAttachmentSize(record.sizeBytes)}
data-markdown-copy={context.copyMarkdown}
onClick={() => context.onExpandImage(attachment)}
/>
);
},
},
{
kind: "file",
// A file chip names its attachment by id, so it renders whatever came back under that id.
// `isFileAttachment` excludes pictures, which a legacy `file` attachment may still be.
canRender: (record, context) =>
record.kind === "file" && context.attachment !== null && context.attachment.type === "file",
render: (record, context) => {
if (
record.kind !== "file" ||
context.attachment === null ||
context.attachment.type !== "file"
) {
return <UnavailableUserMessageContextChip {...context} />;
}
const attachment = context.attachment;
const isVideo = isVideoAttachment(attachment);
const disabled =
attachment.downloadable === false && (!isVideo || attachment.previewUrl === undefined);
const size = formatAttachmentSize(record.sizeBytes);
return (
<FileChip
name={record.name}
size={size}
isVideo={isVideo}
theme={context.resolvedTheme}
disabled={disabled}
accessibleLabel={`${isVideo ? "Video" : "File"} attachment, ${record.name}, ${size}`}
copyMarkdown={context.copyMarkdown}
onOpen={() =>
isVideo ? context.onExpandVideo(attachment) : context.onOpenFile(attachment)
}
tooltip={`${record.name}\n${size}`}
/>
);
},
},
{
kind: "terminal",
canRender: (record) => record.kind === "terminal",
render: (record, context, definition) =>
record.kind === "terminal" ? (
<span data-markdown-copy={context.copyMarkdown}>
<TerminalContextInlineChip
label={record.label}
terminalLabel={record.terminalLabel}
lineStart={record.lineStart}
lineEnd={record.lineEnd}
text={record.text}
detailsMode={definition.capabilities.details}
/>
</span>
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
{
kind: "element",
canRender: (record) => record.kind === "element",
render: (record, context) =>
record.kind === "element" ? (
<UserMessageContextPopover
copyMarkdown={context.copyMarkdown}
accessibleLabel={`Browser element, ${record.label}`}
kind="element"
icon={<MousePointerClickIcon />}
label={record.label}
>
<UserMessageElementDetails record={record} />
</UserMessageContextPopover>
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
{
kind: "review-comment",
canRender: (record) => record.kind === "review-comment",
render: (record, context) => {
if (record.kind !== "review-comment") {
return <UnavailableUserMessageContextChip {...context} />;
}
const isPullRequest = isPullRequestSummaryContext(record);
const label = reviewCommentContextLabel(record);
const kindLabel = isPullRequest ? pullRequestContextKindLabel(record) : "Review comment";
const pullRequestState = pullRequestContextDisplayState(record) ?? "unknown";
if (isPullRequest && record.pullRequest !== undefined) {
return (
<UserMessagePullRequestContextChip
record={record}
copyMarkdown={context.copyMarkdown}
kind={PULL_REQUEST_CHIP_KINDS[pullRequestState]}
/>
);
}
return (
<UserMessageContextPopover
copyMarkdown={context.copyMarkdown}
accessibleLabel={`${kindLabel}, ${label}${record.pullRequest ? `, ${record.pullRequest.title}` : ""}`}
kind={isPullRequest ? PULL_REQUEST_CHIP_KINDS[pullRequestState] : "review-comment"}
icon={isPullRequest ? <PullRequestGlyph.pullRequest /> : <MessageCircleIcon />}
label={label}
>
<UserMessageReviewCommentCard
comment={{
id: record.contextId,
sectionId: record.sectionId,
sectionTitle: record.sectionTitle,
filePath: record.filePath,
startIndex: record.startIndex,
endIndex: record.endIndex,
rangeLabel: record.rangeLabel,
text: record.text,
diff: record.diff,
...(record.fenceLanguage !== undefined
? { fenceLanguage: record.fenceLanguage }
: {}),
...(record.pullRequest !== undefined ? { pullRequest: record.pullRequest } : {}),
}}
/>
</UserMessageContextPopover>
);
},
},
{
kind: "preview-annotation",
canRender: (record) => record.kind === "preview-annotation",
render: (record, context) =>
record.kind === "preview-annotation" ? (
<UserMessageContextPopover
copyMarkdown={context.copyMarkdown}
accessibleLabel={`Preview annotation, ${record.label}`}
kind="preview-annotation"
icon={<MousePointerClickIcon />}
label={record.label}
>
<UserMessagePreviewAnnotationDetails record={record} image={context.annotationImage} />
</UserMessageContextPopover>
) : (
<UnavailableUserMessageContextChip {...context} />
),
},
],
fallback: (_kind, _record, context) => <UnavailableUserMessageContextChip {...context} />,
});
/** One inline context chip in a sent message, dispatched by the shared presentation registry. */
function UserMessageContextReferenceChip(props: {
reference: ChatMarkdownContextReference;
record: KnownComposerContextRecord | undefined;
annotationImage: ChatImageAttachment | null;
attachment: ChatImageAttachment | ChatFileAttachment | null;
onExpandImage: (image: ChatImageAttachment) => void;
onExpandVideo: (file: ChatFileAttachment) => void;
onOpenFile: (file: ChatFileAttachment) => void;
}) {
const { resolvedTheme } = use(TimelineRowCtx);
const copyMarkdown = formatComposerContextReference({
kind: props.reference.kind,
contextId: props.reference.contextId as ComposerContextId,
label: props.reference.label,
});
return userMessageContextPresentationRegistry.render(props.reference.kind, props.record, {
reference: props.reference,
annotationImage: props.annotationImage,
attachment: props.attachment,
resolvedTheme,
copyMarkdown,
onExpandImage: props.onExpandImage,
onExpandVideo: props.onExpandVideo,
onOpenFile: props.onOpenFile,
});
}
const COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM = 1.75;
const COLLAPSED_USER_MESSAGE_FADE_MASK = `linear-gradient(to bottom, black calc(100% - ${COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM}rem), transparent)`;
const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(props: {
text: string;
renderContextReference: (reference: ChatMarkdownContextReference) => ReactNode;
skills: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">>;
markdownCwd: string | undefined;
footer?: ReactNode;
}) {
const [expanded, setExpanded] = useState(false);
// Find opens the body only when it selects a match in the clipped part.
const revealForFind = useCallback(() => setExpanded(true), []);
const findRevealRef = useFindRevealRef(revealForFind);
const hasVisibleBody = props.text.trim().length > 0;
const canCollapse = hasVisibleBody && shouldCollapseUserMessage(props.text);
const isCollapsed = canCollapse && !expanded;
const showCollapseControl = canCollapse;
return (
<div>
{hasVisibleBody ? (
<div
ref={findRevealRef}
className={cn("relative", isCollapsed && "max-h-44 overflow-hidden")}
data-user-message-body="true"
data-thread-find-text="true"
data-thread-find-fold={isCollapsed ? "" : undefined}
data-user-message-collapsed={isCollapsed ? "true" : "false"}
data-user-message-collapsible={canCollapse ? "true" : "false"}
data-user-message-fade={isCollapsed ? "true" : "false"}
style={
isCollapsed
? {
WebkitMaskImage: COLLAPSED_USER_MESSAGE_FADE_MASK,
maskImage: COLLAPSED_USER_MESSAGE_FADE_MASK,
}
: undefined
}
>
<UserMessageBody
text={props.text}
renderContextReference={props.renderContextReference}
skills={props.skills}
markdownCwd={props.markdownCwd}
/>
</div>
) : null}
{showCollapseControl || props.footer ? (
<div
className={cn(
"mt-1.5 flex items-center gap-2",
showCollapseControl && props.footer ? "justify-between" : "justify-end",
)}
data-user-message-footer="true"
>
{showCollapseControl ? (
<Button
type="button"
size="xs"
variant="ghost-muted"
aria-expanded={expanded}
data-scroll-anchor-ignore
onClick={() => setExpanded((value) => !value)}
className="-ml-1"
>
{expanded ? "Show less" : "Show full message"}
</Button>
) : null}
{props.footer ? (
<div className="ml-auto flex items-center gap-2">{props.footer}</div>
) : null}
</div>
) : null}
</div>
);
});
const UserMessageBody = memo(function UserMessageBody(props: {
text: string;
renderContextReference?: (reference: ChatMarkdownContextReference) => ReactNode;
skills: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">>;
markdownCwd: string | undefined;
}) {
const ctx = use(TimelineRowCtx);
if (props.text.length === 0) {
return null;
}
return (
<ChatMarkdown
text={props.text}
cwd={props.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
skills={props.skills}
className="text-foreground"
lineBreaks
parseRawHtml={false}
renderContextReference={props.renderContextReference}
headingLevelOffset={MESSAGE_HEADING_LEVEL}
/>
);
});
function UserMessageReviewCommentCard({ comment }: { comment: ReviewCommentContext }) {
const ctx = use(TimelineRowCtx);
const fenceLanguage = comment.fenceLanguage ?? "diff";
const renderablePatch = getRenderablePatch(
buildReviewCommentRenderablePatch(comment),
`review-comment:${comment.id}`,
);
return (
<div className="space-y-2 rounded-lg border border-border/70 bg-background/70 p-3">
<div className="space-y-1">
<div className="text-xs font-medium text-foreground">
{formatWorkspaceRelativePath(comment.filePath, ctx.workspaceRoot)}
</div>
<div className="text-2xs text-muted-foreground">
{comment.sectionTitle} · {comment.rangeLabel}
</div>
</div>
{comment.text.length > 0 && (
<div className="whitespace-pre-wrap wrap-break-word text-sm">
<SkillInlineText text={comment.text} skills={ctx.skills} />
</div>
)}
{fenceLanguage !== "diff" && comment.diff.trim().length > 0 && (
<ChatMarkdown
text={formatReviewCommentFence(fenceLanguage, comment.diff)}
cwd={ctx.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
skills={ctx.skills}
className="text-foreground"
/>
)}
{renderablePatch?.kind === "files" && (
<DiffWorkerPoolProvider>
{renderablePatch.files.map((fileDiff) => (
<FileDiff
key={resolveFileDiffPath(fileDiff)}
fileDiff={fileDiff}
options={{
collapsed: false,
diffStyle: "unified",
theme: resolveDiffThemeName(ctx.resolvedTheme),
preferredHighlighter: PREFERRED_HIGHLIGHTER,
}}
/>
))}
</DiffWorkerPoolProvider>
)}
{renderablePatch?.kind === "raw" && (
<pre className="overflow-x-auto rounded-md bg-muted/40 p-2 text-xs">
{renderablePatch.text}
</pre>
)}
</div>
);
}
// ---------------------------------------------------------------------------
// Structural sharing — reuse old row references when data hasn't changed
// so LegendList (and React) can skip re-rendering unchanged items.
// ---------------------------------------------------------------------------
/** Content-stable projection of the runs the handoff rows read. The incoming
* array is rebuilt on every projection event (status/timestamp churn), but
* the returned reference only changes when a run's identity-relevant fields
* (id, ordinal, instance, model) do — keeping TimelineRowCtx stable. */
function useStableHandoffRuns(
runs: ReadonlyArray<HandoffTimelineRun>,
): ReadonlyArray<HandoffTimelineRun> {
const prev = useRef<{
signature: string;
value: ReadonlyArray<HandoffTimelineRun>;
}>({ signature: "", value: EMPTY_TIMELINE_RUNS });
return useMemo(() => {
const signature = runs
.map(
(run) =>
`${run.id}\0${run.providerInstanceId}\0${run.ordinal}\0${run.modelSelection.instanceId}\0${run.modelSelection.model}`,
)
.join("\n");
if (signature === prev.current.signature) {
return prev.current.value;
}
const value = runs.map((run) => ({
id: run.id,
ordinal: run.ordinal,
providerInstanceId: run.providerInstanceId,
modelSelection: run.modelSelection,
}));
prev.current = { signature, value };
return value;
}, [runs]);
}
/** Returns a structurally-shared copy of `rows`: for each row whose content
* hasn't changed since last call, the previous object reference is reused. */
function useStableRows(rows: MessagesTimelineRow[], identity: string): MessagesTimelineRow[] {
const prevState = useRef<StableMessagesTimelineRowsState>({
byId: new Map<string, MessagesTimelineRow>(),
result: [],
});
const prevIdentity = useRef(identity);
return useMemo(() => {
const previous =
prevIdentity.current === identity
? prevState.current
: { byId: new Map<string, MessagesTimelineRow>(), result: [] };
prevIdentity.current = identity;
const nextState = computeStableMessagesTimelineRows(rows, previous);
prevState.current = nextState;
return nextState.result;
}, [identity, rows]);
}
// ---------------------------------------------------------------------------
// Pure helpers
// ---------------------------------------------------------------------------
type WorkEntryIconName =
| "bot"
| "brain"
| "browser"
| "check"
| "circle-alert"
| "computer"
| "device"
| "eye"
| "globe"
| "hammer"
| "message-circle"
| "search"
| "square-pen"
| "terminal"
| "pull-request"
| "t3-code"
| "wrench"
| "x"
| "zap";
function BrowserAppIcon({ className }: { className: string }) {
return (
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.9"
strokeLinecap="round"
strokeLinejoin="round"
className={className}
aria-hidden
>
<path d="M8.5 19H7.2C4.4 19 3 17.5 3 14.6V7.4C3 4.5 4.5 3 7.4 3h8.2C18.5 3 20 4.5 20 7.4v2.4" />
<circle cx="7.4" cy="7.2" r="0.75" fill="currentColor" stroke="none" />
<path d="M11.2 7.2h4.3" />
<path d="m12.4 11.4 7.5 2.6-3.4 1.6-1.5 3.6z" fill="currentColor" stroke="none" />
</svg>
);
}
function ToolActivityIconView(props: {
icon: ToolActivityIcon | undefined;
fallbackName: WorkEntryIconName;
className: string;
muted: boolean;
}) {
const { resolvedTheme } = use(TimelineRowCtx);
const fallbackClassName = cn(props.className, props.muted && "opacity-70 light:brightness-60");
if (!props.icon) {
return <WorkEntryIcon name={props.fallbackName} className={fallbackClassName} />;
}
if (props.icon._tag === "website") {
const src = toolActivityFaviconUrl(props.icon, resolvedTheme, 32);
return src ? (
<ToolActivityImageIcon
key={src}
cacheKey={src}
src={src}
fallbackName={props.fallbackName}
className={props.className}
muted={props.muted}
/>
) : (
<WorkEntryIcon name={props.fallbackName} className={fallbackClassName} />
);
}
if (props.icon._tag === "themed-logo") {
const src =
resolvedTheme === "dark"
? (props.icon.logoUrlDark ?? props.icon.logoUrl)
: props.icon.logoUrl;
return (
<ToolActivityImageIcon
key={src}
cacheKey={src}
src={src}
fallbackName={props.fallbackName}
className={props.className}
muted={props.muted}
/>
);
}
return (
<NativeAppToolActivityIcon
app={props.icon.app}
fallbackName={props.fallbackName}
className={cn(props.className, "size-5")}
muted={props.muted}
/>
);
}
function NativeAppToolActivityIcon(props: {
app: Extract<ToolActivityIcon, { readonly _tag: "native-app" }>["app"];
fallbackName: WorkEntryIconName;
className: string;
muted: boolean;
}) {
const { activeThreadEnvironmentId } = use(TimelineRowCtx);
const asset = useAssetUrlState(activeThreadEnvironmentId, {
_tag: "native-app-icon",
app: props.app,
});
if (asset._tag !== "Success") {
return (
<WorkEntryIcon
name={props.fallbackName}
className={cn(props.className, props.muted && "opacity-70 light:brightness-60")}
/>
);
}
const cacheKey = getProjectFaviconCacheKey(
activeThreadEnvironmentId,
JSON.stringify(props.app),
asset.url,
);
return (
<ToolActivityImageIcon
key={cacheKey}
cacheKey={cacheKey}
src={asset.url}
fallbackName={props.fallbackName}
className={props.className}
muted={props.muted}
/>
);
}
const loadedToolActivityIconSrcs = new Map<string, string>();
function ToolActivityImageIcon(props: {
cacheKey: string;
src: string;
fallbackName: WorkEntryIconName;
className: string;
muted: boolean;
}) {
const [displayedSrc, setDisplayedSrc] = useState<string | null>(
() => loadedToolActivityIconSrcs.get(props.cacheKey) ?? null,
);
const isLoading = displayedSrc !== props.src;
const handleLoadError = (failedSrc: string) => {
if (loadedToolActivityIconSrcs.get(props.cacheKey) === failedSrc) {
loadedToolActivityIconSrcs.delete(props.cacheKey);
}
setDisplayedSrc((currentSrc) => (currentSrc === failedSrc ? null : currentSrc));
};
return (
<>
{displayedSrc === null ? (
<WorkEntryIcon
name={props.fallbackName}
className={cn(props.className, props.muted && "opacity-70 light:brightness-60")}
/>
) : null}
{displayedSrc ? (
<span
className={cn(
props.className,
"inline-block overflow-hidden rounded-xs bg-background",
props.muted && "opacity-70",
)}
>
<img
src={displayedSrc}
alt=""
aria-hidden
decoding="async"
referrerPolicy="no-referrer"
className={cn("block size-full object-contain", props.muted && "light:brightness-60")}
onError={() => handleLoadError(displayedSrc)}
/>
</span>
) : null}
{isLoading ? (
<img
src={props.src}
alt=""
aria-hidden
decoding="async"
referrerPolicy="no-referrer"
className="hidden"
onLoad={() => {
loadedToolActivityIconSrcs.set(props.cacheKey, props.src);
setDisplayedSrc(props.src);
}}
onError={() => handleLoadError(props.src)}
/>
) : null}
</>
);
}
function WorkEntryIcon({ name, className }: { name: WorkEntryIconName; className: string }) {
switch (name) {
case "pull-request":
return <PullRequestGlyph.pullRequest className={className} aria-hidden />;
case "bot":
return <BotIcon className={className} aria-hidden />;
case "brain":
return <BrainIcon className={className} aria-hidden />;
case "browser":
return <BrowserAppIcon className={className} />;
case "computer":
return <ComputerUseAppIcon className={className} />;
case "device":
return <SmartphoneIcon className={className} aria-hidden />;
case "t3-code":
return <T3Wordmark className={className} aria-hidden />;
case "check":
return <CheckIcon className={className} aria-hidden />;
case "circle-alert":
return <CircleAlertIcon className={className} aria-hidden />;
case "eye":
return <EyeIcon className={className} aria-hidden />;
case "globe":
return <GlobeIcon className={className} aria-hidden />;
case "hammer":
return <HammerIcon className={className} aria-hidden />;
case "search":
return <SearchIcon className={className} aria-hidden />;
case "message-circle":
return <MessageCircleIcon className={className} aria-hidden />;
case "square-pen":
return <SquarePenIcon className={className} aria-hidden />;
case "terminal":
return <TerminalIcon className={className} aria-hidden />;
case "wrench":
return <WrenchIcon className={className} aria-hidden />;
case "x":
return <XIcon className={className} aria-hidden />;
case "zap":
return <ZapIcon className={className} aria-hidden />;
}
}
function workToneIcon(tone: TimelineWorkEntry["tone"]): {
iconName: WorkEntryIconName;
className: string;
} {
if (tone === "error") {
return {
iconName: "circle-alert",
className: "text-foreground/92",
};
}
if (tone === "thinking") {
return {
iconName: "brain",
className: "text-icon-muted",
};
}
if (tone === "info") {
return {
iconName: "check",
className: "text-muted-foreground",
};
}
return {
iconName: "zap",
className: "text-foreground/92",
};
}
function workEntryRawCommand(
workEntry: Pick<TimelineWorkEntry, "command" | "rawCommand">,
): string | null {
const rawCommand = workEntry.rawCommand?.trim();
if (!rawCommand || !workEntry.command) {
return null;
}
return rawCommand === workEntry.command.trim() ? null : rawCommand;
}
function buildToolCallExpandedBody(
workEntry: TimelineWorkEntry,
workspaceRoot: string | undefined,
visibleLabel: string,
viewedImagePath: string | null,
): string | null {
const blocks: string[] = [];
const seen = new Set<string>([visibleLabel.trim()]);
const addBlock = (value: string | null | undefined) => {
const text = value?.trim();
if (!text || seen.has(text)) return;
seen.add(text);
blocks.push(text);
};
if (workEntry.itemType === "dynamic_tool" && workEntry.toolData !== undefined) {
const input =
workEntry.structuredPayload?.type === "dynamic_tool"
? workEntry.structuredPayload.input
: workEntry.toolData !== null &&
typeof workEntry.toolData === "object" &&
"input" in workEntry.toolData
? workEntry.toolData.input
: undefined;
if (input !== undefined) addBlock(`Tool input\n${JSON.stringify(input, null, 2)}`);
}
const command = workEntry.command?.trim();
const raw = workEntryRawCommand(workEntry);
if (command === visibleLabel.trim()) {
seen.add(command);
} else {
addBlock(raw ?? command);
}
const detail = workEntry.detail?.trim();
if (detail !== viewedImagePath?.trim()) {
addBlock(detail);
}
const viewedImagePaths = new Set(
viewedImagePath
? [viewedImagePath.trim(), formatWorkspaceRelativePath(viewedImagePath, workspaceRoot)]
: [],
);
const changedFiles = (workEntry.changedFiles ?? []).flatMap((filePath) => {
const formattedPath = formatWorkspaceRelativePath(filePath, workspaceRoot);
return viewedImagePaths.has(filePath) ||
viewedImagePaths.has(formattedPath) ||
filePath.trim() === detail ||
formattedPath === detail
? []
: [formattedPath];
});
if (changedFiles.length > 0) {
addBlock([...new Set(changedFiles)].join("\n"));
}
return blocks.length > 0 ? blocks.join("\n\n") : null;
}
const toolCallExpandedBodyClassName =
"max-h-64 cursor-text overflow-auto whitespace-pre-wrap break-words font-mono text-secondary-label text-(length:--font-size-code,var(--text-2xs)) leading-relaxed select-text";
function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconName {
if (workEntry.structuredPayload?.type === "notification") {
if (workEntry.structuredPayload.outcome === "failed") return "circle-alert";
const source = workEntry.structuredPayload.source;
switch (source.kind) {
case "subagent":
case "delegated_task":
return "bot";
case "command":
return "terminal";
case "monitor":
return "eye";
case "background_task":
return "zap";
case "system":
return "t3-code";
default:
source satisfies never;
return "zap";
}
}
if (workEntry.itemType === "user_input_request" || workEntry.itemType === "approval_request") {
return "message-circle";
}
if (workEntry.toolSurface) return workEntry.toolSurface;
const toolPresentation = resolveWorkEntryToolPresentation(workEntry);
if (toolPresentation) return toolPresentation.icon;
const action = toolGroupAction(workEntry);
if (action !== "other") return toolGroupSummaryIconName(action);
switch (workEntry.itemType) {
case "dynamic_tool":
return "wrench";
case "subagent":
return "bot";
}
return workToneIcon(workEntry.tone).iconName;
}
const stopRowToggle = (e: { stopPropagation: () => void }) => e.stopPropagation();
function remarkThoughtPreview(fallback: string) {
return (tree: Root) => {
const plainText = (node: Root | RootContent): string => {
if (node.type === "html" || node.type === "definition") return "";
if ("alt" in node) return node.alt ?? "";
if ("value" in node) return node.value;
if ("children" in node) {
const separator = ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(
node.type,
)
? " "
: "";
return node.children.map(plainText).join(separator);
}
return node.type === "break" ? " " : "";
};
tree.children = [
{ type: "text", value: plainText(tree).replace(/\s+/g, " ").trim() || fallback },
];
};
}
function ReasoningTraceContent({ entries }: { entries: ReadonlyArray<TimelineWorkEntry> }) {
const ctx = use(TimelineRowCtx);
const { isWorking, latestRunId } = use(TimelineRowActivityCtx);
return (
<WorkLogDetails>
{entries.map((entry) => (
<ChatMarkdown
key={entry.id}
className="text-foreground"
text={entry.detail ?? ""}
cwd={ctx.markdownCwd}
threadRef={ctx.threadRef ?? undefined}
skills={ctx.skills}
isStreaming={
isWorking && entry.runId === latestRunId && entry.toolLifecycleStatus === "inProgress"
}
headingLevelOffset={MESSAGE_HEADING_LEVEL}
onUseArtifactTemplate={ctx.onUseArtifactTemplate}
onImageExpand={ctx.onImageExpand}
lineBreaks
/>
))}
</WorkLogDetails>
);
}
type WorkEntryRowProps = {
workEntry: TimelineWorkEntry;
workspaceRoot: string | undefined;
displayLabel?: string | undefined;
onToggleEntry?: ((collapsed: boolean) => void) | undefined;
};
const SimpleWorkEntryRow = memo(function SimpleWorkEntryRow(props: WorkEntryRowProps) {
const ctx = use(TimelineRowCtx);
const item = props.workEntry.projectedItem?.item;
const childThreadId =
item?.type === "notification" ? notificationChildThreadId(item.source) : undefined;
if (item?.type !== "notification" || childThreadId === undefined) {
return <WorkEntryLogRow {...props} />;
}
return (
<SubagentNotificationLink
parentRef={scopeThreadRef(ctx.activeThreadEnvironmentId, item.threadId)}
childThreadId={childThreadId}
outcome={item.outcome}
createdAt={props.workEntry.createdAt}
timestampFormat={ctx.timestampFormat}
providerStatuses={ctx.providerStatuses}
onOpenThread={ctx.onOpenThread}
fallback={<WorkEntryLogRow {...props} />}
/>
);
});
function WorkEntryLogRow(props: WorkEntryRowProps) {
const { workEntry, workspaceRoot, displayLabel } = props;
const ctx = use(TimelineRowCtx);
const { threadRef, onImageExpand, timestampFormat } = ctx;
const { retryableWorkspacePreparationRunIds, onRetryWorkspacePreparation } = ctx;
const threadTarget = useThreadReadTarget(workEntry, ctx.activeThreadEnvironmentId);
const createdThread =
workEntry.projectedItem?.item.type === "thread_created"
? workEntry.projectedItem.item
: undefined;
const notifiedSubagentThreadId =
workEntry.projectedItem?.item.type === "notification"
? notificationChildThreadId(workEntry.projectedItem.item.source)
: undefined;
const groupView = use(WorkGroupViewCtx);
const [expanded, setExpanded] = useState(
() => groupView?.state.expandedEntries.has(workEntry.id) ?? false,
);
const toggleExpanded = () => {
const next = !expanded;
if (groupView) {
groupView.onToggleEntry(!next);
if (next) groupView.state.expandedEntries.add(workEntry.id);
else groupView.state.expandedEntries.delete(workEntry.id);
} else {
props.onToggleEntry?.(!next);
}
setExpanded(next);
};
const failureItem = workEntry.projectedItem?.item;
if (failureItem?.type === "error" && failureItem.status === "failed") {
const warning = failureItem.failure.class === "usage_limit";
const resetAt = failureItem.failure.resetAt;
const resetTime = resetAt ? formatUpcomingTimestamp(resetAt, timestampFormat) : null;
const label = warning
? `Usage limit reached.${resetTime ? ` Retry after ${resetTime}.` : ""}`
: workEntry.label;
const retryRunId =
failureItem.runId !== null &&
retryableWorkspacePreparationRunIds.has(failureItem.runId) &&
failureItem.failure.code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE
? failureItem.runId
: null;
return (
<WorkLogRow
data-v2-item-type="error"
data-v2-item-visibility={workEntry.projectedItem?.visibility}
wrapLabel
icon={
<CircleAlertIcon
className={cn("size-4", warning ? "text-warning" : "text-destructive")}
/>
}
label={
<span
className={cn("text-sm font-medium", warning ? "text-warning" : "text-destructive")}
>
{label}
</span>
}
trailing={
<TimelineRowTimestamp
createdAt={workEntry.createdAt}
timestampFormat={timestampFormat}
alwaysVisible
/>
}
>
{!warning ? (
<p className="ms-7 whitespace-pre-wrap break-words py-1 text-sm leading-relaxed text-foreground/80">
{failureItem.failure.message}
</p>
) : null}
{retryRunId !== null && onRetryWorkspacePreparation ? (
<div className="ms-7 pb-1">
<Button
type="button"
size="xs"
variant="outline"
onClick={() => onRetryWorkspacePreparation(retryRunId)}
>
<RotateCcwIcon aria-hidden />
Retry
</Button>
</div>
) : null}
</WorkLogRow>
);
}
const iconConfig = workToneIcon(workEntry.tone);
const showWarningIndicator = workEntry.sourceActivityKind === "runtime.warning";
const showFailedIndicator =
!showWarningIndicator && workEntryDisplayIndicatesToolFailure(workEntry);
const showDestructiveRowStyle =
!showWarningIndicator &&
showFailedIndicator &&
(workEntrySignalsSevereFailure(workEntry) || !workLogEntryIsToolLike(workEntry));
const entryIconName =
showWarningIndicator || showDestructiveRowStyle ? "circle-alert" : workEntryIconName(workEntry);
const entryToolIcon =
showWarningIndicator || showDestructiveRowStyle
? undefined
: (workEntry.toolIcon ?? workEntry.toolSource?.icon);
const isReasoning = workEntry.itemType === "reasoning";
// The question is the row's identity: a generic "User input submitted"
// label buries what was asked, so lead with the question text (even over a
// lone tool row's display label) and keep the answer as the trailing preview.
const questionHeading = workEntry.questionAnswer
? getQuestionTextPreview(workEntry.questionAnswer)
: "";
const previewText =
isReasoning && expanded
? workEntry.toolLifecycleStatus === "inProgress"
? "Thinking"
: "Thought"
: questionHeading || (displayLabel ?? workEntryDisplayLabel(workEntry, workspaceRoot));
const answerPreview =
workEntry.questionAnswer && hasQuestionAnswer(workEntry.questionAnswer)
? getQuestionAnswerPreview(workEntry.questionAnswer)
: null;
const viewedImagePath = workEntryViewedImagePath(workEntry);
const payload = workEntry.structuredPayload;
const skill =
payload?.type === "dynamic_tool"
? claudeSkillInvocation(payload.toolName, payload.input)
: undefined;
// Reads and skills expand to plain text instead of the item inspector. A
// skill's heading already names it, so only its arguments are left to show.
const plainOutput =
toolGroupAction(workEntry) === "read"
? workEntryReadOutput(workEntry, workspaceRoot)
: skill
? (skill.args ?? null)
: undefined;
const viewedImage =
viewedImagePath && threadRef
? resolveViewedImageAsset(viewedImagePath, {
threadId: threadRef.threadId,
workspaceRoot,
})
: null;
const canExpand =
previewText.trim().length > 0 ||
(workEntry.itemType === "dynamic_tool" && workEntry.toolData !== undefined) ||
Boolean(
workEntryRawCommand(workEntry) ||
workEntry.command?.trim() ||
workEntry.detail?.trim() ||
workEntry.changedFiles?.length ||
viewedImage,
);
const expandedBody =
expanded && !isReasoning
? plainOutput !== undefined
? plainOutput
: buildToolCallExpandedBody(
workEntry,
workspaceRoot,
previewText,
viewedImage ? viewedImagePath : null,
)
: null;
// Projected rows expand to the item inspector, so only offer a disclosure
// when it has something to show, even if that output still has to load.
// Reads and skills still fetch the output the timeline withheld.
const plainOutputFetches =
plainOutput !== undefined &&
workEntry.projectedItem !== undefined &&
turnItemNeedsDetailFetch(workEntry.projectedItem.item);
const canExpandProjectedItem =
plainOutput !== undefined
? Boolean(plainOutput || viewedImage || workEntry.questionAnswer || plainOutputFetches)
: workEntry.projectedItem === undefined
? canExpand
: isReasoning
? Boolean(workEntry.detail?.trim())
: Boolean(
viewedImage ||
workEntry.questionAnswer ||
turnItemHasDetail(workEntry.projectedItem.item),
);
// Reserve destructive row styling for severe failures, not routine tool errors.
const iconWrapperClass = cn(
"flex size-4 items-center justify-center",
showWarningIndicator
? "text-warning"
: showDestructiveRowStyle
? "text-destructive"
: showFailedIndicator
? failedToolIconClassName
: workEntry.tone === "tool"
? "text-icon-muted"
: iconConfig.className,
);
const headingClass = showWarningIndicator
? "font-medium text-warning"
: showDestructiveRowStyle
? "font-medium text-destructive"
: workLogEntryIsToolLike(workEntry)
? "text-secondary-label"
: "text-foreground/80";
const threadLabel = isReasoning ? null : threadReadLabel(previewText, threadTarget);
const accessiblePreview = [threadLabel?.text ?? previewText, answerPreview]
.filter(Boolean)
.join(": ");
const accessibleDisplayText = showFailedIndicator
? `${accessiblePreview}, tool call failed`
: accessiblePreview;
const rowToggleProps = canExpandProjectedItem
? {
role: "button" as const,
tabIndex: 0 as const,
"aria-label": accessibleDisplayText,
"aria-expanded": expanded,
onClick: toggleExpanded,
onKeyDown: (e: KeyboardEvent<HTMLDivElement>) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
toggleExpanded();
}
},
}
: {};
return (
<WorkLogRow
data-v2-item-type={workEntry.projectedItem?.item.type}
data-v2-item-visibility={workEntry.projectedItem?.visibility}
{...rowToggleProps}
icon={
<span
className={iconWrapperClass}
role={showFailedIndicator ? "img" : undefined}
aria-label={showFailedIndicator ? "Tool call failed" : undefined}
>
<ToolActivityIconView
icon={entryToolIcon}
fallbackName={entryIconName}
className="block size-4 shrink-0 stroke-2"
muted
/>
</span>
}
label={
<div className="min-w-0 flex-1 overflow-hidden">
<p className="flex min-w-0 w-full items-baseline gap-1.5 text-sm leading-relaxed">
<span
className={cn(answerPreview ? "min-w-0" : "min-w-0 flex-1", "truncate", headingClass)}
>
{isReasoning && !expanded ? (
<ReactMarkdown
remarkPlugins={[
remarkGfm,
[
remarkThoughtPreview,
workEntry.toolLifecycleStatus === "inProgress" ? "Thinking" : "Thought",
],
]}
>
{workEntry.detail ?? previewText}
</ReactMarkdown>
) : threadLabel ? (
<ThreadReadLabel
label={threadLabel}
environmentId={ctx.activeThreadEnvironmentId}
linked
/>
) : (
previewText
)}
</span>
{answerPreview ? (
<span
className={cn(
"min-w-0 truncate",
!expanded &&
workEntry.questionAnswer &&
hasQuestionAnswer(workEntry.questionAnswer)
? "text-foreground"
: "text-muted-foreground",
)}
>
{answerPreview}
</span>
) : null}
</p>
</div>
}
trailing={
<>
{createdThread ? (
<button
type="button"
className="shrink-0 rounded-sm text-sm text-primary hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
aria-label={`Open ${createdThread.title ?? "created thread"}`}
onClick={(event) => {
event.stopPropagation();
ctx.onOpenThread(createdThread.targetThreadId);
}}
onKeyDown={stopRowToggle}
>
Open chat
</button>
) : null}
{notifiedSubagentThreadId ? (
<InlineButton
aria-label="Open subagent thread"
onClick={(event) => {
event.stopPropagation();
ctx.onOpenThread(notifiedSubagentThreadId);
}}
onKeyDown={stopRowToggle}
>
Open subagent
</InlineButton>
) : null}
{showFailedIndicator &&
!showDestructiveRowStyle &&
!toolIconAcceptsTint(entryIconName, entryToolIcon) ? (
<XIcon aria-hidden className={cn("size-3 shrink-0", failedToolIconClassName)} />
) : null}
<TimelineRowTimestamp createdAt={workEntry.createdAt} timestampFormat={timestampFormat} />
<span
className={cn(
"flex size-4 shrink-0 items-center justify-center",
!canExpandProjectedItem && "invisible",
)}
aria-hidden
>
<ChevronRightIcon
className={cn(
"size-3 shrink-0 text-icon-muted opacity-70 transition-transform duration-200",
expanded && "rotate-90",
)}
/>
</span>
</>
}
>
{expanded && viewedImage && threadRef ? (
<WorkLogDetails kind="media">
<ChatMarkdownAssetImage
environmentId={threadRef.environmentId}
resource={viewedImage.resource}
alt={viewedImage.alt}
srcFragment={viewedImage.srcFragment}
workspaceRoot={workspaceRoot}
maxHeightRem={16}
onImageExpand={onImageExpand}
/>
</WorkLogDetails>
) : null}
{expanded && workEntry.questionAnswer ? (
<QuestionAnswerHistory answer={workEntry.questionAnswer} />
) : null}
{expanded && isReasoning ? <ReasoningTraceContent entries={[workEntry]} /> : null}
{expanded &&
!isReasoning &&
!workEntry.questionAnswer &&
canExpandProjectedItem &&
(expandedBody ||
plainOutputFetches ||
(workEntry.projectedItem && plainOutput === undefined)) ? (
<WorkLogDetails kind="panel">
{workEntry.projectedItem && plainOutput === undefined ? (
<V2ItemInspector
projectedItem={workEntry.projectedItem}
environmentId={ctx.activeThreadEnvironmentId}
cwd={ctx.markdownCwd}
workspaceRoot={workspaceRoot}
onOpenThread={ctx.onOpenThread}
onOpenTurnDiff={ctx.onOpenTurnDiff}
onRollbackCheckpoint={ctx.onRollbackCheckpoint}
onImageExpand={ctx.onImageExpand}
/>
) : (
<>
{expandedBody ? (
<pre className={toolCallExpandedBodyClassName}>{expandedBody}</pre>
) : null}
{plainOutputFetches && workEntry.projectedItem ? (
<FetchedToolOutput
projectedItem={workEntry.projectedItem}
environmentId={ctx.activeThreadEnvironmentId}
onImageExpand={onImageExpand}
/>
) : null}
</>
)}
</WorkLogDetails>
) : null}
</WorkLogRow>
);
}
function QuestionAnswerHistory({
answer,
}: {
answer: import("@t3tools/contracts").UserInputAttachmentAnswerPayload;
}) {
const { activeThreadEnvironmentId } = use(TimelineRowCtx);
const attachments = useMemo(() => Object.values(answer.attachmentsByQuestionId).flat(), [answer]);
const resources = useMemo(
() =>
attachments.map((attachment) => ({
_tag: "attachment" as const,
attachmentId: attachment.id,
})),
[attachments],
);
const urls = useAssetUrls(activeThreadEnvironmentId, resources);
return (
<div className="ms-7 mt-2 space-y-2" onClick={stopRowToggle}>
{[
...new Set([
...Object.keys(answer.questionTextById ?? {}),
...Object.keys(answer.answers),
...Object.keys(answer.attachmentsByQuestionId),
]),
].map((questionId) => (
<div key={questionId} className="space-y-1">
{answer.questionTextById?.[questionId] ? (
<p className="whitespace-pre-wrap text-sm text-muted-foreground">
{answer.questionTextById[questionId]}
</p>
) : null}
{getQuestionAnswerText(answer.answers[questionId]) ? (
<p className="ms-3 whitespace-pre-wrap text-sm text-muted-foreground">
{getQuestionAnswerText(answer.answers[questionId])}
</p>
) : null}
<div className="flex flex-wrap gap-2">
{(answer.attachmentsByQuestionId[questionId] ?? []).map((attachment) => {
const url = urls[attachments.indexOf(attachment)];
return (
<a
key={attachment.id}
href={url ?? undefined}
target="_blank"
rel="noreferrer"
className="text-sm underline"
>
{attachment.type === "image" && url ? (
<img
src={url}
alt={attachment.name}
className="h-20 max-w-32 rounded object-contain"
/>
) : (
attachment.name
)}
</a>
);
})}
</div>
</div>
))}
</div>
);
}