Skip to content
TypeScript

refactor(web): timeline user message rows and context chips live in their own files (#22676)

Julius Marminge3e36c5e18177
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx
index 956b82e3f71..c707e369eb4 100644
--- a/apps/web/src/components/chat/MessagesTimeline.tsx
+++ b/apps/web/src/components/chat/MessagesTimeline.tsx
@@ -1,17 +1,12 @@
-import { useRenderedAppearance } from "../../hooks/useTheme";
import { ThreadFindTimelineContext } from "./ThreadFindProvider";
import { shallow } from "zustand/vanilla/shallow";
import { shouldPreserveAssistantLineBreaks } from "@t3tools/shared/markdownPipeline";
-import { MarkdownFindContext, findRevealRef } from "./markdownFindContext";
+import { MarkdownFindContext } from "./markdownFindContext";
import { useChatCanvas } from "./ChatCanvasContext";
import { WorkLogBlock } from "./WorkLog";
-import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
-import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview";
-import { useRightPanelStore } from "~/rightPanelStore";
import { deriveTimelineMinimapItems } from "./timelineMinimapItems";
import {
- COMPOSER_CONTEXT_KINDS,
type AssistantCitation,
type EnvironmentId,
type MessageId,
@@ -24,12 +19,8 @@ import {
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 { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
import { resolveWorkGroupScrollAnchor } from "@t3tools/client-runtime/work-log/scroll-anchor";
-import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
import {
subagentGroupSummary,
summarizeSubagentStatuses,
@@ -58,74 +49,28 @@ import {
type LegendListRef,
type MaintainScrollAtEndOptions,
} from "@legendapp/list/react";
-import { FileDiff } from "@pierre/diffs/react";
-import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
-import {
- type TimelineEntry,
- providerErrorPresentation,
- createMessageAttachmentPreviewProjector,
- selectMessageImageResources,
-} from "../../session-logic";
+import { type TimelineEntry, providerErrorPresentation } 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 { type ChatMessage, type ChatFileAttachment, type TurnDiffSummary } from "../../types";
import ChatMarkdown from "../ChatMarkdown";
-import { ThreadContextChip } from "../ThreadContextChip";
import {
ArrowRightIcon,
BotIcon,
CheckIcon,
ChevronDownIcon,
CircleAlertIcon,
- DownloadIcon,
- EyeIcon,
GitForkIcon,
type LucideIcon,
- MessageCircleIcon,
- MousePointerClickIcon,
- PaintbrushIcon,
MinusIcon,
- Redo2Icon,
Minimize2Icon,
- Undo2Icon,
WrenchIcon,
XIcon,
ZapIcon,
} 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 { ChevronDown, ChevronRight } from "lucide";
+import { Button } from "../ui/button";
import { MorphIcon } from "~/components/MorphIcon";
-import { useAssetUrlRefresh, useAssetUrls, useAssetUrlState } from "../../assets/assetUrls";
-import { AttachmentImage } from "../media/AttachmentImage";
-import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
-import {
- buildAttachmentVideoAsset,
- buildAttachmentVideoPreview,
- buildExpandedImagePreview,
- ExpandedImagePreview,
-} from "./ExpandedImagePreview";
-import {
- SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
- SnapShotAttachmentDetails,
-} from "./SnapShotAttachmentDetails";
+import { ExpandedImagePreview } from "./ExpandedImagePreview";
import { ProposedPlanCard } from "./ProposedPlanCard";
import { HtmlRenderFrame } from "./HtmlRenderFrame";
import { McpAppFrame } from "./McpAppFrame";
@@ -140,9 +85,6 @@ import {
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 } from "./AssistantCitationSource";
@@ -158,7 +100,6 @@ import {
isTimelineMinimapRowVisible,
resolveTimelineMinimapVisibleRange,
resolveTimelineMinimapHitStripWidth,
- shouldCollapseUserMessage,
timelineTurnFoldRunIdsByEntryId,
worktreeSetupAgentStarted,
type StableMessagesTimelineRowsState,
@@ -169,42 +110,10 @@ import { type ThreadFindMatch } from "./threadFind";
import { useThreadFindNavigation } from "./useThreadFindNavigation";
import { readThreadFindPosition } from "./threadFindPosition";
import type { ThreadFindPositionReader } from "./threadFind";
-import { TerminalContextInlineChip } from "./TerminalContextInlineChip";
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";
@@ -222,17 +131,9 @@ import {
} from "./V2LifecycleRow";
import { SecretRequestCard } from "./SecretRequestCard";
import { TimelineSystemDivider } from "./TimelineSystemDivider";
-import { WorkflowOpeningBlock } from "./WorkflowOpeningBlock";
-import { SkillChipIcon } from "./SkillInlineText";
import * as DateTime from "effect/DateTime";
-import { formatWorkspaceRelativePath } from "../../filePathDisplay";
import { forwardWheelToTimeline } from "./timelineMinimapWheel";
-import {
- buildReviewCommentRenderablePatch,
- formatReviewCommentFence,
- type ReviewCommentContext,
-} from "../../reviewCommentContext";
import { TimelineMinimap } from "./TimelineMinimap";
import {
type TimelineRowSharedState,
@@ -255,6 +156,7 @@ import {
LiveWorkEntryTimelineRow,
WorkGroupToggleTimelineRow,
} from "./TimelineWorkGroupRows";
+import { UserMessageRow } from "./TimelineUserMessageRow";
function timelineListStateFor(identity: string): {
readonly identity: string;
@@ -1650,487 +1552,6 @@ function ContextCompactionTimelineRow({
);
}
-function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
- const ctx = use(TimelineRowCtx);
- // Row updates rebuild attachment objects, so the asset follows only the
- // fields it reads.
- const { downloadable, id, mimeType, name } = file;
- const environmentId = ctx.activeThreadEnvironmentId;
- const asset =
- downloadable === false
- ? null
- : buildAttachmentVideoAsset(environmentId, { id, mimeType, 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}
- />
- );
-}
-
-function UserMessageRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
- const ctx = use(TimelineRowCtx);
- return ctx.workflowOpening?.messageId === row.message.id ? (
- <WorkflowOpeningBlock
- opening={ctx.workflowOpening}
- environmentId={ctx.activeThreadEnvironmentId}
- onOpenThread={(threadId) => ctx.onOpenThread(ThreadId.make(threadId))}
- />
- ) : (
- <UserTimelineRow row={row} />
- );
-}
-
-function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
- const ctx = use(TimelineRowCtx);
- const { onImageExpand, onFileOpen } = ctx;
- const senderThreadId = row.message.senderThreadId;
- const resources = selectMessageImageResources(row.message.attachments);
- const previewUrls = useAssetUrls(ctx.activeThreadEnvironmentId, resources);
- const [projectPreviews] = useState(createMessageAttachmentPreviewProjector);
- const previewUrlsById = new Map(
- resources.flatMap((resource, index) => {
- const url = previewUrls[index];
- return url ? [[resource.attachmentId, url] as const] : [];
- }),
- );
- const messageWithPreviews = projectPreviews(row.message, (attachment) =>
- previewUrlsById.get(attachment.id),
- );
- const userImages = (messageWithPreviews.attachments ?? []).filter(isImageAttachment);
- const userFiles = (row.message.attachments ?? []).filter(isFileAttachment);
- 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 = resolveUserMessageContext(row.message);
- const previewImages = userImages.filter((image) => image.name.startsWith("preview-annotation-"));
- const revertTurnCount = row.revertTurnCount;
- 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 = resolvedContext.records
- .filter((record) => record.kind === "preview-annotation")
- .map((record) => record.contextId);
- const plainText = replaceComposerContextReferences(
- resolvedContext.text,
- (reference) => reference.label,
- );
- const promptLink = promptLinkFor(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,
- });
- 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());
- // Diagram source is copied text, not authority to export context records.
- for (const element of container.querySelectorAll(
- "[data-markdown-copy]:not([data-markdown-mermaid])",
- )) {
- 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 = (reference: ChatMarkdownContextReference) => {
- const record = asKnownContextRecord(resolvedContext.recordsById.get(reference.contextId));
- 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>
- );
- };
-
- 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 === "steer" ||
- row.message.inputIntent === "promoted_queued_to_steer" ? (
- <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);
- }}
- >
- <AttachmentImage
- name={image.name}
- mimeType={image.mimeType}
- src={image.previewUrl}
- alt={image.name}
- decoding="async"
- 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
- 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: "steer" | "promoted_queued_to_steer";
-}) {
- 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}
- />
- }
- >
- <Redo2Icon aria-hidden="true" className="size-3" />
- Steer
- </TooltipTrigger>
- <TooltipPopup side="top">
- {intent === "promoted_queued_to_steer"
- ? "Originally queued, then promoted to steer the active turn"
- : "Steered the active turn"}
- </TooltipPopup>
- </Tooltip>
- );
-}
-
-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>
- );
-}
-
function TurnFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "turn-fold" }> }) {
const ctx = use(TimelineRowCtx);
@@ -2928,644 +2349,6 @@ function AssistantChangedFilesSectionInner({
);
}
-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",
- 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} />,
-});
-
-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);
- const revealForFind = () => setExpanded(true);
- const revealRef = findRevealRef(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={revealRef}
- 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 renderedAppearance = useRenderedAppearance();
- 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="wrap-break-word text-sm">
- <ChatMarkdown
- text={comment.text}
- cwd={ctx.markdownCwd}
- skills={ctx.skills}
- parseRawHtml={false}
- lineBreaks
- />
- </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(renderedAppearance),
- 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>
- );
-}
-
function handoffRunsSignature(runs: ReadonlyArray<HandoffTimelineRun>): string {
return runs
.map(
diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts b/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts
new file mode 100644
index 00000000000..9738e29d7cc
--- /dev/null
+++ b/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts
@@ -0,0 +1,25 @@
+import { type ChatImageAttachment } from "../../types";
+import type { ComposerContextRecord, KnownComposerContextRecord } from "@t3tools/contracts";
+import { asKnownContextRecord } from "~/lib/composerContextRecords";
+
+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
+ );
+}
diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx
new file mode 100644
index 00000000000..388562ef25e
--- /dev/null
+++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx
@@ -0,0 +1,619 @@
+import { findRevealRef } from "./markdownFindContext";
+import { type MessageId, type ServerProviderSkill, ThreadId } from "@t3tools/contracts";
+import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
+import { Link } from "@tanstack/react-router";
+import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
+import { memo, use, useState, type ReactNode } from "react";
+import {
+ createMessageAttachmentPreviewProjector,
+ selectMessageImageResources,
+} from "../../session-logic";
+import {
+ type ChatFileAttachment,
+ isFileAttachment,
+ isImageAttachment,
+ isVideoAttachment,
+} from "../../types";
+import ChatMarkdown from "../ChatMarkdown";
+import { DownloadIcon, EyeIcon, Redo2Icon, Undo2Icon } from "lucide-react";
+import { Link as LinkIconNode } from "lucide";
+import { Button, InlineButton } from "../ui/button";
+import { useAssetUrlRefresh, useAssetUrls, useAssetUrlState } from "../../assets/assetUrls";
+import { AttachmentImage } from "../media/AttachmentImage";
+import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
+import {
+ buildAttachmentVideoAsset,
+ buildAttachmentVideoPreview,
+ buildExpandedImagePreview,
+} from "./ExpandedImagePreview";
+import {
+ SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
+ SnapShotAttachmentDetails,
+} from "./SnapShotAttachmentDetails";
+import { MessageCopyButton } from "./MessageCopyButton";
+import { promptLinkFor } from "../promptLinks/copyPromptLink";
+import { PierreEntryIcon } from "./PierreEntryIcon";
+import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
+import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
+import {
+ asKnownContextRecord,
+ resolveUserMessageContext,
+ selectedMessageContextFragment,
+} from "~/lib/composerContextRecords";
+import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
+import {
+ COMPOSER_CONTEXT_CLIPBOARD_MIME,
+ encodeComposerContextClipboardHtml,
+ encodeComposerContextFragment,
+} from "@t3tools/shared/composerContextClipboard";
+import { chatMarkdownClipboardPayload } from "../../markdown-clipboard";
+import type { ChatMarkdownContextReference } from "../ChatMarkdown";
+import { cn } from "~/lib/utils";
+import { formatChatTimestampTooltip, formatDayAwareTimestamp } from "../../timestampFormat";
+import { WorkflowOpeningBlock } from "./WorkflowOpeningBlock";
+import { TimelineRowCtx, TimelineRowActivityCtx, type TimelineRow } from "./timelineRowContext";
+import { MESSAGE_HEADING_LEVEL, MessageAuthorHeading } from "./TimelineRowParts";
+import { UserMessageContextReferenceChip } from "./UserMessageContextChips";
+import { resolvePreviewAnnotationImage } from "./TimelineUserMessageRow.logic";
+
+function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
+ const ctx = use(TimelineRowCtx);
+ // Row updates rebuild attachment objects, so the asset follows only the
+ // fields it reads.
+ const { downloadable, id, mimeType, name } = file;
+ const environmentId = ctx.activeThreadEnvironmentId;
+ const asset =
+ downloadable === false
+ ? null
+ : buildAttachmentVideoAsset(environmentId, { id, mimeType, 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}
+ />
+ );
+}
+
+export function UserMessageRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
+ const ctx = use(TimelineRowCtx);
+ return ctx.workflowOpening?.messageId === row.message.id ? (
+ <WorkflowOpeningBlock
+ opening={ctx.workflowOpening}
+ environmentId={ctx.activeThreadEnvironmentId}
+ onOpenThread={(threadId) => ctx.onOpenThread(ThreadId.make(threadId))}
+ />
+ ) : (
+ <UserTimelineRow row={row} />
+ );
+}
+
+function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
+ const ctx = use(TimelineRowCtx);
+ const { onImageExpand, onFileOpen } = ctx;
+ const senderThreadId = row.message.senderThreadId;
+ const resources = selectMessageImageResources(row.message.attachments);
+ const previewUrls = useAssetUrls(ctx.activeThreadEnvironmentId, resources);
+ const [projectPreviews] = useState(createMessageAttachmentPreviewProjector);
+ const previewUrlsById = new Map(
+ resources.flatMap((resource, index) => {
+ const url = previewUrls[index];
+ return url ? [[resource.attachmentId, url] as const] : [];
+ }),
+ );
+ const messageWithPreviews = projectPreviews(row.message, (attachment) =>
+ previewUrlsById.get(attachment.id),
+ );
+ const userImages = (messageWithPreviews.attachments ?? []).filter(isImageAttachment);
+ const userFiles = (row.message.attachments ?? []).filter(isFileAttachment);
+ 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 = resolveUserMessageContext(row.message);
+ const previewImages = userImages.filter((image) => image.name.startsWith("preview-annotation-"));
+ const revertTurnCount = row.revertTurnCount;
+ 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 = resolvedContext.records
+ .filter((record) => record.kind === "preview-annotation")
+ .map((record) => record.contextId);
+ const plainText = replaceComposerContextReferences(
+ resolvedContext.text,
+ (reference) => reference.label,
+ );
+ const promptLink = promptLinkFor(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,
+ });
+ 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());
+ // Diagram source is copied text, not authority to export context records.
+ for (const element of container.querySelectorAll(
+ "[data-markdown-copy]:not([data-markdown-mermaid])",
+ )) {
+ 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 = (reference: ChatMarkdownContextReference) => {
+ const record = asKnownContextRecord(resolvedContext.recordsById.get(reference.contextId));
+ 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>
+ );
+ };
+
+ 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 === "steer" ||
+ row.message.inputIntent === "promoted_queued_to_steer" ? (
+ <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);
+ }}
+ >
+ <AttachmentImage
+ name={image.name}
+ mimeType={image.mimeType}
+ src={image.previewUrl}
+ alt={image.name}
+ decoding="async"
+ 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
+ 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: "steer" | "promoted_queued_to_steer";
+}) {
+ 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}
+ />
+ }
+ >
+ <Redo2Icon aria-hidden="true" className="size-3" />
+ Steer
+ </TooltipTrigger>
+ <TooltipPopup side="top">
+ {intent === "promoted_queued_to_steer"
+ ? "Originally queued, then promoted to steer the active turn"
+ : "Steered the active turn"}
+ </TooltipPopup>
+ </Tooltip>
+ );
+}
+
+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>
+ );
+}
+
+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);
+ const revealForFind = () => setExpanded(true);
+ const revealRef = findRevealRef(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={revealRef}
+ 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}
+ />
+ );
+});
diff --git a/apps/web/src/components/chat/UserMessageContextChips.tsx b/apps/web/src/components/chat/UserMessageContextChips.tsx
new file mode 100644
index 00000000000..cc5474c3790
--- /dev/null
+++ b/apps/web/src/components/chat/UserMessageContextChips.tsx
@@ -0,0 +1,592 @@
+import { useRenderedAppearance } from "../../hooks/useTheme";
+import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
+import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview";
+import { useRightPanelStore } from "~/rightPanelStore";
+import { COMPOSER_CONTEXT_KINDS } from "@t3tools/contracts";
+import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
+import { use, type ReactNode } from "react";
+import { FileDiff } from "@pierre/diffs/react";
+import { DiffWorkerPoolProvider } from "../DiffWorkerPoolProvider";
+import {
+ type ChatFileAttachment,
+ type ChatImageAttachment,
+ isImageAttachment,
+ isVideoAttachment,
+} from "../../types";
+import {
+ getRenderablePatch,
+ resolveDiffThemeName,
+ resolveFileDiffPath,
+} from "../../lib/diffRendering";
+import { PREFERRED_HIGHLIGHTER } from "../../lib/syntaxHighlighting";
+import ChatMarkdown from "../ChatMarkdown";
+import { ThreadContextChip } from "../ThreadContextChip";
+import { MessageCircleIcon, MousePointerClickIcon, PaintbrushIcon } from "lucide-react";
+import type { ComposerContextId, KnownComposerContextRecord } from "@t3tools/contracts";
+import { buildExpandedImagePreview } from "./ExpandedImagePreview";
+import { WorkspaceEntryIcon, WorkspaceEntryTooltip } from "./WorkspaceEntryIcon";
+import { TerminalContextInlineChip } from "./TerminalContextInlineChip";
+import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
+import {
+ ContextChipPopover as UserMessageContextPopover,
+ ContextChipShell,
+ FileChip,
+ ImageChipButton,
+ PULL_REQUEST_CHIP_KINDS,
+ PullRequestChip,
+ UnresolvedChip,
+} from "../contextChipParts";
+import {
+ isPullRequestSummaryContext,
+ pullRequestContextDisplayState,
+ pullRequestContextKindLabel,
+ reviewCommentContextLabel,
+} from "~/lib/composerContextRecords";
+import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
+import { ContextChip, ContextChipLabel, type ContextChipKind } from "../ContextChip";
+import { createContextPresentationRegistry } from "../contextPresentationRegistry";
+import type { ChatMarkdownContextReference } from "../ChatMarkdown";
+import { SkillChipIcon } from "./SkillInlineText";
+import { formatWorkspaceRelativePath } from "../../filePathDisplay";
+import {
+ buildReviewCommentRenderablePatch,
+ formatReviewCommentFence,
+ type ReviewCommentContext,
+} from "../../reviewCommentContext";
+import { TimelineRowCtx } from "./timelineRowContext";
+
+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",
+ 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} />,
+});
+
+export 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,
+ });
+}
+
+function UserMessageReviewCommentCard({ comment }: { comment: ReviewCommentContext }) {
+ const renderedAppearance = useRenderedAppearance();
+ 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="wrap-break-word text-sm">
+ <ChatMarkdown
+ text={comment.text}
+ cwd={ctx.markdownCwd}
+ skills={ctx.skills}
+ parseRawHtml={false}
+ lineBreaks
+ />
+ </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(renderedAppearance),
+ 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>
+ );
+}