The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { copySnapshot as copySnapshotToClipboard } from "./copySnapshot"; |
| 2 | import { InlineAudioPreview } from "../files/InlineAudioPreview"; |
| 3 | import { filePreviewKind } from "@t3tools/shared/filePreview"; |
| 4 | import { |
| 5 | CHAT_USER_MARKDOWN_REMARK_PLUGINS, |
| 6 | remarkEnableOrderedListNumbers, |
| 7 | } from "@t3tools/shared/markdownPipeline"; |
| 8 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 9 | import { findRevealRef } from "./markdownFindContext"; |
| 10 | import { |
| 11 | type EnvironmentId, |
| 12 | type MessageId, |
| 13 | type ScheduledTaskId, |
| 14 | type ServerProviderSkill, |
| 15 | ThreadId, |
| 16 | } from "@t3tools/contracts"; |
| 17 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 18 | import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message"; |
| 19 | import { Link } from "@tanstack/react-router"; |
| 20 | import { useQuietThreadShell } from "../../state/entities"; |
| 21 | import { useEnvironment } from "../../state/environments"; |
| 22 | import { useEnvironmentQuery } from "../../state/query"; |
| 23 | import { serverEnvironment } from "../../state/server"; |
| 24 | import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences"; |
| 25 | import { memo, use, useState, type ReactNode } from "react"; |
| 26 | import { |
| 27 | createMessageAttachmentPreviewProjector, |
| 28 | selectMessageImageResources, |
| 29 | } from "../../session-logic"; |
| 30 | import { |
| 31 | type ChatFileAttachment, |
| 32 | isFileAttachment, |
| 33 | isImageAttachment, |
| 34 | isVideoAttachment, |
| 35 | } from "../../types"; |
| 36 | import ChatMarkdown from "../ChatMarkdown"; |
| 37 | import { |
| 38 | CalendarClockIcon, |
| 39 | ChevronDownIcon, |
| 40 | DownloadIcon, |
| 41 | EyeIcon, |
| 42 | type LucideIcon, |
| 43 | MessagesSquareIcon, |
| 44 | Redo2Icon, |
| 45 | PencilIcon, |
| 46 | LinkIcon, |
| 47 | } from "lucide-react"; |
| 48 | import { Button, InlineButton } from "../ui/button"; |
| 49 | import { FreshSessionRow, ModelSwitchRow } from "./V2LifecycleRow"; |
| 50 | import { useAssetUrlRefresh, useAssetUrls, useAssetUrlState } from "../../assets/assetUrls"; |
| 51 | import { AttachmentImage } from "../media/AttachmentImage"; |
| 52 | import { MediaVideoPlayer } from "../media/MediaVideoPlayer"; |
| 53 | import { |
| 54 | buildAttachmentVideoAsset, |
| 55 | buildAttachmentVideoPreview, |
| 56 | buildExpandedImagePreview, |
| 57 | } from "./ExpandedImagePreview"; |
| 58 | import { |
| 59 | SNAP_SHOT_ATTACHMENT_FRAME_CLASS, |
| 60 | SnapShotAttachmentDetails, |
| 61 | } from "./SnapShotAttachmentDetails"; |
| 62 | import { GuestMessageName, guestBubbleClassName } from "./guestMessageStyle"; |
| 63 | import { MessageCopyButton } from "./MessageCopyButton"; |
| 64 | import { promptLinkFor } from "../promptLinks/copyPromptLink"; |
| 65 | import { PierreEntryIcon } from "./PierreEntryIcon"; |
| 66 | import { formatBytes } from "../../lib/formatBytes"; |
| 67 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 68 | import { |
| 69 | asKnownContextRecord, |
| 70 | resolveUserMessageContext, |
| 71 | selectedMessageContextFragment, |
| 72 | } from "~/lib/composerContextRecords"; |
| 73 | import { |
| 74 | collectComposerContextReferences, |
| 75 | formatComposerContextReference, |
| 76 | } from "@t3tools/shared/composerContextReferences"; |
| 77 | import { |
| 78 | COMPOSER_CONTEXT_CLIPBOARD_MIME, |
| 79 | encodeComposerContextClipboardHtml, |
| 80 | encodeComposerContextFragment, |
| 81 | } from "@t3tools/shared/composerContextClipboard"; |
| 82 | import { chatMarkdownClipboardPayload } from "../../markdown-clipboard"; |
| 83 | import type { ChatMarkdownContextReference } from "../ChatMarkdown"; |
| 84 | import { cn } from "~/lib/utils"; |
| 85 | import { markdownProseDirection } from "~/textDirection"; |
| 86 | import { useMediaQuery } from "~/hooks/useMediaQuery"; |
| 87 | import { formatChatTimestampTooltip, formatDayAwareTimestamp } from "../../timestampFormat"; |
| 88 | import { WorkflowOpeningBlock } from "./WorkflowOpeningBlock"; |
| 89 | import { useTallEnoughToFold } from "./useTallEnoughToFold"; |
| 90 | import { TimelineRowCtx, TimelineRowActivityCtx, type TimelineRow } from "./timelineRowContext"; |
| 91 | import { |
| 92 | MESSAGE_HEADING_LEVEL, |
| 93 | MessageAuthorHeading, |
| 94 | TimelineRowTimestamp, |
| 95 | } from "./TimelineRowParts"; |
| 96 | import { UserMessageContextReferenceChip } from "./UserMessageContextChips"; |
| 97 | import { |
| 98 | resolvePreviewAnnotationImage, |
| 99 | userMessageFileLabel, |
| 100 | } from "./TimelineUserMessageRow.logic"; |
| 101 | |
| 102 | // Drawn inside the media so dark screenshots keep an edge on the dark background. |
| 103 | const MEDIA_EDGE_CLASS_NAME = |
| 104 | "relative after:pointer-events-none after:absolute after:inset-0 after:z-10 after:rounded-[inherit] after:ring-1 after:ring-inset after:ring-black/9 dark:after:ring-white/12"; |
| 105 | |
| 106 | const BUBBLE_CHIP_CLASS_NAME = |
| 107 | "[&_[data-slot=context-chip]]:[--context-chip-message-ink:var(--context-chip-message)]"; |
| 108 | |
| 109 | function UserFileCard({ |
| 110 | file, |
| 111 | copyMarkdown, |
| 112 | }: { |
| 113 | readonly file: ChatFileAttachment; |
| 114 | readonly copyMarkdown: string | undefined; |
| 115 | }) { |
| 116 | const ctx = use(TimelineRowCtx); |
| 117 | const gt = useGT(); |
| 118 | const m = useMessages(); |
| 119 | const pastedText = file.source?._tag === "pasted-text"; |
| 120 | const label = userMessageFileLabel(file, m); |
| 121 | const extension = |
| 122 | /\.([a-z0-9]{1,8})$/i.exec(file.name)?.[1]?.toUpperCase() ?? |
| 123 | gt("File", { $context: "file type badge when the name has no extension" }); |
| 124 | const identity = ( |
| 125 | <> |
| 126 | <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-background/60"> |
| 127 | <PierreEntryIcon pathValue={file.name} kind="file" theme={ctx.resolvedTheme} /> |
| 128 | </span> |
| 129 | <span className="min-w-0 flex-1"> |
| 130 | <span className="block truncate text-sm font-medium group-hover/file:underline"> |
| 131 | {label} |
| 132 | </span> |
| 133 | <span className="block text-xs text-muted-foreground tabular-nums"> |
| 134 | {file.downloadable === false ? ( |
| 135 | <T>Sending…</T> |
| 136 | ) : ( |
| 137 | <> |
| 138 | {!pastedText && `${extension} · `} |
| 139 | {formatBytes(file.sizeBytes)} |
| 140 | </> |
| 141 | )} |
| 142 | </span> |
| 143 | </span> |
| 144 | </> |
| 145 | ); |
| 146 | return ( |
| 147 | <div |
| 148 | data-markdown-copy={copyMarkdown} |
| 149 | className="flex w-64 max-w-full items-center gap-1 rounded-2xl bg-message py-2 ps-2 pe-1 text-message-foreground" |
| 150 | > |
| 151 | {file.downloadable === false ? ( |
| 152 | <> |
| 153 | <div className="flex min-w-0 flex-1 items-center gap-3">{identity}</div> |
| 154 | <Button |
| 155 | size="icon-xs" |
| 156 | variant="ghost-muted" |
| 157 | disabled |
| 158 | aria-label={gt("Sending attachment")} |
| 159 | > |
| 160 | <DownloadIcon /> |
| 161 | </Button> |
| 162 | </> |
| 163 | ) : ( |
| 164 | <> |
| 165 | <button |
| 166 | type="button" |
| 167 | aria-label={gt("Preview {label}", { label })} |
| 168 | onClick={() => ctx.onFileOpen(file)} |
| 169 | className="group/file focus-visible:ring-ring/70 flex min-w-0 flex-1 cursor-pointer items-center gap-3 rounded-xl text-left focus-visible:ring-2 focus-visible:ring-inset focus-visible:outline-none" |
| 170 | > |
| 171 | {identity} |
| 172 | </button> |
| 173 | <Tooltip> |
| 174 | <TooltipTrigger |
| 175 | render={ |
| 176 | <Button |
| 177 | size="icon-xs" |
| 178 | variant="ghost-muted" |
| 179 | aria-label={gt("Download {label}", { label })} |
| 180 | onClick={() => ctx.onFileDownload(file)} |
| 181 | /> |
| 182 | } |
| 183 | > |
| 184 | <DownloadIcon /> |
| 185 | </TooltipTrigger> |
| 186 | <TooltipPopup side="top"> |
| 187 | <T> |
| 188 | Download <Var>{label}</Var> |
| 189 | </T> |
| 190 | </TooltipPopup> |
| 191 | </Tooltip> |
| 192 | </> |
| 193 | )} |
| 194 | </div> |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | function UserVideoAttachment({ |
| 199 | file, |
| 200 | standalone = false, |
| 201 | cornerClassName, |
| 202 | }: { |
| 203 | readonly file: ChatFileAttachment; |
| 204 | readonly standalone?: boolean; |
| 205 | readonly cornerClassName?: string | undefined; |
| 206 | }) { |
| 207 | const ctx = use(TimelineRowCtx); |
| 208 | // Row updates rebuild attachment objects, so the asset follows only the |
| 209 | // fields it reads. |
| 210 | const { downloadable, id, mimeType, name } = file; |
| 211 | const environmentId = ctx.activeThreadEnvironmentId; |
| 212 | const asset = |
| 213 | downloadable === false |
| 214 | ? null |
| 215 | : buildAttachmentVideoAsset(environmentId, { id, mimeType, name }); |
| 216 | const resource = asset?.resource ?? null; |
| 217 | const assetUrl = useAssetUrlState(ctx.activeThreadEnvironmentId, resource); |
| 218 | const refreshAssetUrl = useAssetUrlRefresh(ctx.activeThreadEnvironmentId, resource); |
| 219 | const src = assetUrl._tag === "Success" ? assetUrl.url : (file.previewUrl ?? null); |
| 220 | |
| 221 | if (asset === null && src === null) { |
| 222 | return ( |
| 223 | <div |
| 224 | className={cn( |
| 225 | "flex aspect-[4/3] w-full items-center justify-center bg-black px-2 py-3 text-center text-2xs text-white/70", |
| 226 | standalone |
| 227 | ? cn(MEDIA_EDGE_CLASS_NAME, "rounded-2xl", cornerClassName) |
| 228 | : "rounded-lg border border-border/80", |
| 229 | )} |
| 230 | > |
| 231 | {file.name} |
| 232 | </div> |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | const player = ( |
| 237 | <MediaVideoPlayer |
| 238 | src={src} |
| 239 | sourceFailed={ |
| 240 | file.previewUrl === undefined && resource !== null && assetUrl._tag === "Failure" |
| 241 | } |
| 242 | label={file.name} |
| 243 | preload="visible" |
| 244 | onOpen={() => { |
| 245 | const preview = buildAttachmentVideoPreview(ctx.activeThreadEnvironmentId, file); |
| 246 | if (preview) ctx.onImageExpand(preview); |
| 247 | }} |
| 248 | className="block aspect-[4/3] w-full" |
| 249 | videoClassName={cn( |
| 250 | "aspect-auto size-full", |
| 251 | !standalone && "rounded-lg border border-border/80", |
| 252 | )} |
| 253 | stateClassName={cn( |
| 254 | "aspect-auto min-h-full bg-black text-white", |
| 255 | !standalone && "rounded-lg border border-border/80", |
| 256 | )} |
| 257 | onRetry={asset ? refreshAssetUrl : undefined} |
| 258 | actionsSource={asset ? { kind: "video", name: file.name, src, asset } : undefined} |
| 259 | /> |
| 260 | ); |
| 261 | return standalone ? ( |
| 262 | <div className={cn(MEDIA_EDGE_CLASS_NAME, "overflow-hidden rounded-2xl", cornerClassName)}> |
| 263 | {player} |
| 264 | </div> |
| 265 | ) : ( |
| 266 | player |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | export function UserMessageRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) { |
| 271 | const ctx = use(TimelineRowCtx); |
| 272 | const item = row.projectedItem?.item; |
| 273 | const modelSwitch = item?.type === "user_message" ? item.modelSwitch : undefined; |
| 274 | return ( |
| 275 | <> |
| 276 | {item?.type === "user_message" && item.freshContext ? <FreshSessionRow /> : null} |
| 277 | {modelSwitch ? ( |
| 278 | <ModelSwitchRow environmentId={ctx.activeThreadEnvironmentId} modelSwitch={modelSwitch} /> |
| 279 | ) : null} |
| 280 | <UserMessageContent row={row} /> |
| 281 | </> |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | function UserMessageContent({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) { |
| 286 | const ctx = use(TimelineRowCtx); |
| 287 | return ctx.workflowOpening?.messageId === row.message.id ? ( |
| 288 | <WorkflowOpeningBlock |
| 289 | opening={ctx.workflowOpening} |
| 290 | environmentId={ctx.activeThreadEnvironmentId} |
| 291 | onOpenThread={(threadId, intent) => |
| 292 | ctx.onOpenThread(ThreadId.make(threadId), undefined, intent) |
| 293 | } |
| 294 | /> |
| 295 | ) : resolveUserMessagePresentation(row.message).attribution === "automation" ? ( |
| 296 | <AutomationMessageRow row={row} /> |
| 297 | ) : ( |
| 298 | <UserTimelineRow row={row} /> |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | /** Automation prompts are long, repeated runbooks; a folded bubble keeps them out of the way. */ |
| 303 | function AutomationMessageRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) { |
| 304 | const ctx = use(TimelineRowCtx); |
| 305 | const gt = useGT(); |
| 306 | const [expanded, setExpanded] = useState(false); |
| 307 | const revealRef = findRevealRef(() => setExpanded(true)); |
| 308 | const { text, scheduledTaskId } = resolveUserMessagePresentation(row.message); |
| 309 | const summary = |
| 310 | useScheduledTaskTitle(ctx.activeThreadEnvironmentId, scheduledTaskId) ?? |
| 311 | text.trim().split(/\r?\n/, 1)[0] ?? |
| 312 | ""; |
| 313 | |
| 314 | const revertTurnCount = row.revertTurnCount; |
| 315 | const promptLink = promptLinkFor(text); |
| 316 | // Find reads the DOM, so a folded prompt stays mounted but hidden while find is open. |
| 317 | const bodyMounted = expanded || ctx.findActive; |
| 318 | |
| 319 | return ( |
| 320 | <div className="group flex flex-col items-end gap-1" data-user-message-attribution="automation"> |
| 321 | {row.message.inputIntent === "steer" || |
| 322 | row.message.inputIntent === "promoted_queued_to_steer" ? ( |
| 323 | <UserMessageIntentMarker |
| 324 | intent={row.message.inputIntent} |
| 325 | unread={row.projectedItem?.item.status === "pending"} |
| 326 | /> |
| 327 | ) : null} |
| 328 | <div |
| 329 | className={cn( |
| 330 | "min-w-0 max-w-[80%] rounded-2xl bg-message text-message-foreground", |
| 331 | BUBBLE_CHIP_CLASS_NAME, |
| 332 | expanded && "w-full", |
| 333 | )} |
| 334 | data-user-message-bubble="" |
| 335 | > |
| 336 | <MessageAuthorHeading>{gt("Automation")}</MessageAuthorHeading> |
| 337 | <FoldedMessageHeader |
| 338 | icon={CalendarClockIcon} |
| 339 | label={gt("Automation")} |
| 340 | fitLabel |
| 341 | summary={summary} |
| 342 | expanded={expanded} |
| 343 | onToggle={() => setExpanded((value) => !value)} |
| 344 | /> |
| 345 | {bodyMounted ? ( |
| 346 | // `beforematch` does not bubble, so the reveal listener sits on the hidden fold itself. |
| 347 | <div |
| 348 | ref={revealRef} |
| 349 | hidden={!expanded} |
| 350 | className={cn("px-3 pb-3", !ctx.findActive && "max-h-96 overflow-auto")} |
| 351 | data-thread-find-text="true" |
| 352 | > |
| 353 | <UserMessageBody text={text} skills={ctx.skills} markdownCwd={ctx.markdownCwd} /> |
| 354 | </div> |
| 355 | ) : null} |
| 356 | </div> |
| 357 | <UserMessageProjectedStatus row={row} /> |
| 358 | {expanded ? ( |
| 359 | <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"> |
| 360 | <div className="flex shrink-0 items-center gap-2"> |
| 361 | {scheduledTaskId ? ( |
| 362 | <Link |
| 363 | to="/settings/scheduled-tasks" |
| 364 | search={{ environmentId: ctx.activeThreadEnvironmentId, taskId: scheduledTaskId }} |
| 365 | className="rounded-sm text-xs text-muted-foreground hover:underline focus-visible:outline-2 focus-visible:outline-ring" |
| 366 | > |
| 367 | <T>Open automation</T> |
| 368 | </Link> |
| 369 | ) : null} |
| 370 | <TimelineRowTimestamp |
| 371 | createdAt={row.message.createdAt} |
| 372 | timestampFormat={ctx.timestampFormat} |
| 373 | alwaysVisible |
| 374 | /> |
| 375 | <div className="flex items-center gap-0.5"> |
| 376 | {typeof revertTurnCount === "number" && ( |
| 377 | <RevertUserMessageButton turnCount={revertTurnCount} messageId={row.message.id} /> |
| 378 | )} |
| 379 | <MessageCopyButton text={text} variant="ghost" /> |
| 380 | {promptLink !== null && ( |
| 381 | <MessageCopyButton |
| 382 | text={promptLink} |
| 383 | label={gt("Copy prompt link")} |
| 384 | icon={LinkIcon} |
| 385 | variant="ghost" |
| 386 | /> |
| 387 | )} |
| 388 | </div> |
| 389 | </div> |
| 390 | </div> |
| 391 | ) : null} |
| 392 | </div> |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | export function useScheduledTaskTitle( |
| 397 | environmentId: EnvironmentId, |
| 398 | scheduledTaskId: ScheduledTaskId | undefined, |
| 399 | ): string | undefined { |
| 400 | const tasksQuery = useEnvironmentQuery( |
| 401 | scheduledTaskId ? serverEnvironment.scheduledTasksLive({ environmentId, input: {} }) : null, |
| 402 | ); |
| 403 | return tasksQuery.data?.tasks.find((task) => task.id === scheduledTaskId)?.title; |
| 404 | } |
| 405 | |
| 406 | export function FoldedMessageHeader(props: { |
| 407 | icon: LucideIcon; |
| 408 | label: string; |
| 409 | /** A generic label (not a thread title) never truncates, whatever its length in the UI language. */ |
| 410 | fitLabel?: boolean; |
| 411 | summary: string; |
| 412 | expanded: boolean; |
| 413 | onToggle: () => void; |
| 414 | }) { |
| 415 | const Icon = props.icon; |
| 416 | return ( |
| 417 | <button |
| 418 | type="button" |
| 419 | aria-expanded={props.expanded} |
| 420 | data-scroll-anchor-ignore |
| 421 | onClick={props.onToggle} |
| 422 | className="flex w-full min-w-0 items-center gap-1.5 rounded-2xl px-3 py-2 text-left text-sm focus-visible:outline-2 focus-visible:outline-ring" |
| 423 | > |
| 424 | <Icon className="size-4 shrink-0 stroke-2 text-muted-foreground" /> |
| 425 | <span |
| 426 | className={cn( |
| 427 | "shrink-0 text-muted-foreground", |
| 428 | props.fitLabel !== true && "max-w-1/2 truncate", |
| 429 | )} |
| 430 | > |
| 431 | {props.label} |
| 432 | </span> |
| 433 | <span className="min-w-0 flex-1 truncate">{props.summary}</span> |
| 434 | <ChevronDownIcon |
| 435 | className={cn( |
| 436 | "size-3.5 shrink-0 text-muted-foreground transition-transform", |
| 437 | props.expanded && "rotate-180", |
| 438 | )} |
| 439 | /> |
| 440 | </button> |
| 441 | ); |
| 442 | } |
| 443 | |
| 444 | function UserMessageProjectedStatus({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) { |
| 445 | const status = row.projectedItem?.item.status; |
| 446 | if ( |
| 447 | status === undefined || |
| 448 | status === "completed" || |
| 449 | status === "pending" || |
| 450 | status === "waiting" |
| 451 | ) |
| 452 | return null; |
| 453 | return ( |
| 454 | <div className="me-1 flex items-center gap-1.5"> |
| 455 | <span className="rounded-full border border-destructive/25 bg-destructive/8 px-1.5 py-0.5 text-3xs font-medium text-destructive"> |
| 456 | {status} |
| 457 | </span> |
| 458 | </div> |
| 459 | ); |
| 460 | } |
| 461 | |
| 462 | function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) { |
| 463 | const ctx = use(TimelineRowCtx); |
| 464 | const gt = useGT(); |
| 465 | const m = useMessages(); |
| 466 | const { onImageExpand, onFileOpen } = ctx; |
| 467 | const coarsePointer = useMediaQuery({ pointer: "coarse" }); |
| 468 | // A linked environment's agent sent it from a thread there. |
| 469 | const senderRef = |
| 470 | row.message.linkedSender ?? |
| 471 | (row.message.senderThreadId === undefined |
| 472 | ? null |
| 473 | : scopeThreadRef(ctx.activeThreadEnvironmentId, row.message.senderThreadId)); |
| 474 | const resources = selectMessageImageResources(row.message.attachments); |
| 475 | const previewUrls = useAssetUrls(ctx.activeThreadEnvironmentId, resources); |
| 476 | const [projectPreviews] = useState(createMessageAttachmentPreviewProjector); |
| 477 | const previewUrlsById = new Map( |
| 478 | resources.flatMap((resource, index) => { |
| 479 | const url = previewUrls[index]; |
| 480 | return url ? [[resource.attachmentId, url] as const] : []; |
| 481 | }), |
| 482 | ); |
| 483 | const messageWithPreviews = projectPreviews(row.message, (attachment) => |
| 484 | previewUrlsById.get(attachment.id), |
| 485 | ); |
| 486 | const userImages = (messageWithPreviews.attachments ?? []).filter(isImageAttachment); |
| 487 | const userFiles = (row.message.attachments ?? []).filter(isFileAttachment); |
| 488 | const userVideos = userFiles.filter(isVideoAttachment); |
| 489 | const userAudio = userFiles.filter( |
| 490 | (file) => file.downloadable !== false && filePreviewKind(file) === "audio", |
| 491 | ); |
| 492 | const otherUserFiles = userFiles.filter( |
| 493 | (file) => !isVideoAttachment(file) && !userAudio.includes(file), |
| 494 | ); |
| 495 | const unknownAttachments = (row.message.attachments ?? []).filter( |
| 496 | (attachment) => !isImageAttachment(attachment) && !isFileAttachment(attachment), |
| 497 | ); |
| 498 | const userMessage = resolveUserMessagePresentation(row.message); |
| 499 | // Messages from other agents fold to one line so the user's own messages stay easy to find. |
| 500 | const folded = userMessage.attribution === "agent"; |
| 501 | const [expanded, setExpanded] = useState(false); |
| 502 | const revealFoldRef = findRevealRef(() => setExpanded(true)); |
| 503 | const senderShell = useQuietThreadShell(folded ? senderRef : null); |
| 504 | const senderTitle = senderShell?.title.trim() || row.message.linkedSender?.title.trim(); |
| 505 | const senderLabel = |
| 506 | senderTitle || gt("Agent", { $context: "sender of a message from another agent" }); |
| 507 | // Only an environment this client is connected to can open the sender's thread there. |
| 508 | const linkedSenderEnvironment = useEnvironment(row.message.linkedSender?.environmentId ?? null); |
| 509 | const canOpenSender = |
| 510 | senderRef !== null && |
| 511 | (row.message.linkedSender === undefined || linkedSenderEnvironment !== null); |
| 512 | const resolvedContext = resolveUserMessageContext({ ...row.message, text: userMessage.text }); |
| 513 | const previewImages = userImages.filter((image) => image.name.startsWith("preview-annotation-")); |
| 514 | const revertTurnCount = row.revertTurnCount; |
| 515 | const attachmentReferences = collectComposerContextReferences(resolvedContext.text).flatMap( |
| 516 | (occurrence) => { |
| 517 | const record = asKnownContextRecord(resolvedContext.recordsById.get(occurrence.contextId)); |
| 518 | return record?.kind === "file" || record?.kind === "image" |
| 519 | ? [ |
| 520 | { |
| 521 | attachmentId: record.attachmentId, |
| 522 | contextId: occurrence.contextId, |
| 523 | source: occurrence.source, |
| 524 | }, |
| 525 | ] |
| 526 | : []; |
| 527 | }, |
| 528 | ); |
| 529 | const chippedAttachmentIds = new Set( |
| 530 | attachmentReferences.map((reference) => reference.attachmentId), |
| 531 | ); |
| 532 | const regularImages = userImages.filter((image) => !image.name.startsWith("preview-annotation-")); |
| 533 | const unchippedFiles = otherUserFiles.filter((file) => !chippedAttachmentIds.has(file.id)); |
| 534 | // Images or videos sent without words show as media, not as a bubble around them. |
| 535 | const mediaOnly = |
| 536 | !folded && |
| 537 | userMessage.attribution !== "guest" && |
| 538 | resolvedContext.text.trim() === "" && |
| 539 | userAudio.length === 0 && |
| 540 | unchippedFiles.length === 0 && |
| 541 | unknownAttachments.length === 0 && |
| 542 | regularImages.length + userVideos.length > 0; |
| 543 | // Media sent with words sits above the bubble as its own shape, like a chat app. |
| 544 | const mediaAboveText = |
| 545 | !folded && |
| 546 | userMessage.attribution !== "guest" && |
| 547 | !mediaOnly && |
| 548 | regularImages.length + userVideos.length > 0; |
| 549 | const standaloneMedia = mediaOnly || mediaAboveText; |
| 550 | // A lone image keeps its own shape; a 4:3 crop hides most of a wide screenshot. |
| 551 | const loneImage = standaloneMedia && userVideos.length === 0 && regularImages.length === 1; |
| 552 | // Several images share one rounded shape, split by hairlines. |
| 553 | const collage = |
| 554 | standaloneMedia && |
| 555 | userVideos.length === 0 && |
| 556 | regularImages.length > 1 && |
| 557 | regularImages.every((image) => image.source?.kind !== "snap-shot"); |
| 558 | // Corners that touch the bubble or a neighbouring message tighten, like the bubble's. |
| 559 | const mediaCornerClassName = cn( |
| 560 | row.groupedWithPrevious && "rounded-tr-md", |
| 561 | (mediaAboveText || row.groupedWithNext) && "rounded-br-md", |
| 562 | ); |
| 563 | // Files sent without words show as cards, not as a bubble around a name chip. |
| 564 | const fileCardsOnly = |
| 565 | !folded && |
| 566 | userMessage.attribution !== "guest" && |
| 567 | otherUserFiles.length > 0 && |
| 568 | regularImages.length + userVideos.length + userAudio.length + unknownAttachments.length === 0 && |
| 569 | replaceComposerContextReferences(resolvedContext.text, (reference) => |
| 570 | attachmentReferences.some( |
| 571 | (attached) => |
| 572 | attached.contextId === reference.contextId && |
| 573 | otherUserFiles.some((file) => file.id === attached.attachmentId), |
| 574 | ) |
| 575 | ? "" |
| 576 | : reference.label, |
| 577 | ).trim() === ""; |
| 578 | const audioOnly = |
| 579 | !folded && |
| 580 | userMessage.attribution !== "guest" && |
| 581 | userAudio.length > 0 && |
| 582 | regularImages.length + userVideos.length + otherUserFiles.length + unknownAttachments.length === |
| 583 | 0 && |
| 584 | replaceComposerContextReferences(resolvedContext.text, (reference) => |
| 585 | attachmentReferences.some( |
| 586 | (attached) => |
| 587 | attached.contextId === reference.contextId && |
| 588 | userAudio.some((file) => file.id === attached.attachmentId), |
| 589 | ) |
| 590 | ? "" |
| 591 | : reference.label, |
| 592 | ).trim() === ""; |
| 593 | const annotationRecordIds = resolvedContext.records |
| 594 | .filter((record) => record.kind === "preview-annotation") |
| 595 | .map((record) => record.contextId); |
| 596 | const plainText = replaceComposerContextReferences( |
| 597 | resolvedContext.text, |
| 598 | (reference) => reference.label, |
| 599 | ); |
| 600 | const promptLink = promptLinkFor(plainText); |
| 601 | const foldSummary = |
| 602 | plainText |
| 603 | .split(/\r?\n/) |
| 604 | .find((line) => line.trim().length > 0) |
| 605 | ?.trim() ?? ""; |
| 606 | const mediaCopyRecords = mediaOnly |
| 607 | ? resolvedContext.records.flatMap((record) => { |
| 608 | const known = asKnownContextRecord(record); |
| 609 | return (known?.kind === "image" || known?.kind === "file") && |
| 610 | (regularImages.some((image) => image.id === known.attachmentId) || |
| 611 | userVideos.some((file) => file.id === known.attachmentId)) |
| 612 | ? [known] |
| 613 | : []; |
| 614 | }) |
| 615 | : []; |
| 616 | const copyRecords = mediaOnly ? mediaCopyRecords : resolvedContext.records; |
| 617 | const contextClipboardFragment = |
| 618 | copyRecords.length === 0 |
| 619 | ? null |
| 620 | : encodeComposerContextFragment({ |
| 621 | version: 1, |
| 622 | source: { |
| 623 | environmentId: ctx.activeThreadEnvironmentId, |
| 624 | ...(ctx.threadRef ? { threadId: ctx.threadRef.threadId } : {}), |
| 625 | messageId: row.message.id, |
| 626 | }, |
| 627 | records: copyRecords, |
| 628 | }); |
| 629 | const copyText = |
| 630 | resolvedContext.text || mediaCopyRecords.map(formatComposerContextReference).join("\n"); |
| 631 | const copySnapshot = async (id: string) => { |
| 632 | const image = userImages.find((candidate) => candidate.id === id); |
| 633 | const record = resolvedContext.records |
| 634 | .map(asKnownContextRecord) |
| 635 | .find((candidate) => candidate?.kind === "image" && candidate.attachmentId === id); |
| 636 | if (!image?.previewUrl || record?.kind !== "image") |
| 637 | throw new Error(gt("Snapshot is no longer available.")); |
| 638 | const fragment = encodeComposerContextFragment({ |
| 639 | version: 1, |
| 640 | source: { environmentId: ctx.activeThreadEnvironmentId, messageId: row.message.id }, |
| 641 | records: [{ ...record, ...(image.source ? { source: image.source } : {}) }], |
| 642 | }); |
| 643 | if (!fragment) throw new Error(gt("The snapshot is too large to copy.")); |
| 644 | await copySnapshotToClipboard( |
| 645 | image.previewUrl, |
| 646 | formatComposerContextReference(record), |
| 647 | fragment, |
| 648 | ); |
| 649 | }; |
| 650 | const onBodyCopyCapture = (event: React.ClipboardEvent<HTMLDivElement>) => { |
| 651 | if (resolvedContext.records.length === 0 || !event.clipboardData) return; |
| 652 | const selection = window.getSelection(); |
| 653 | if (!selection || selection.isCollapsed) return; |
| 654 | const copiedMarkdown: string[] = []; |
| 655 | for (let index = 0; index < selection.rangeCount; index += 1) { |
| 656 | const container = document.createElement("div"); |
| 657 | container.appendChild(selection.getRangeAt(index).cloneContents()); |
| 658 | // Diagram source is copied text, not authority to export context records. |
| 659 | for (const element of container.querySelectorAll( |
| 660 | "[data-markdown-copy]:not([data-markdown-mermaid])", |
| 661 | )) { |
| 662 | copiedMarkdown.push(element.getAttribute("data-markdown-copy") ?? ""); |
| 663 | } |
| 664 | } |
| 665 | const fragment = selectedMessageContextFragment({ |
| 666 | markdown: copiedMarkdown.join("\n"), |
| 667 | records: resolvedContext.records, |
| 668 | environmentId: ctx.activeThreadEnvironmentId, |
| 669 | ...(ctx.threadRef ? { threadId: ctx.threadRef.threadId } : {}), |
| 670 | messageId: row.message.id, |
| 671 | }); |
| 672 | if (!fragment) return; |
| 673 | // Claim the copy: without preventDefault the browser default overwrites the |
| 674 | // custom MIME type. The default content must then be written back explicitly. |
| 675 | const payload = chatMarkdownClipboardPayload(selection); |
| 676 | event.preventDefault(); |
| 677 | event.clipboardData.setData("text/plain", payload?.text ?? selection.toString()); |
| 678 | if (payload) { |
| 679 | event.clipboardData.setData( |
| 680 | "text/html", |
| 681 | encodeComposerContextClipboardHtml(payload.text, fragment, payload.html), |
| 682 | ); |
| 683 | } |
| 684 | event.clipboardData.setData(COMPOSER_CONTEXT_CLIPBOARD_MIME, fragment); |
| 685 | }; |
| 686 | const renderContextReference = (reference: ChatMarkdownContextReference) => { |
| 687 | const record = asKnownContextRecord(resolvedContext.recordsById.get(reference.contextId)); |
| 688 | if (record?.kind === "file" && userAudio.some((file) => file.id === record.attachmentId)) |
| 689 | return null; |
| 690 | const annotationImage = |
| 691 | record?.kind === "preview-annotation" |
| 692 | ? resolvePreviewAnnotationImage({ |
| 693 | record, |
| 694 | recordsById: resolvedContext.recordsById, |
| 695 | userImages, |
| 696 | previewImages, |
| 697 | annotationRecordIds, |
| 698 | }) |
| 699 | : null; |
| 700 | const attachment = |
| 701 | record?.kind === "image" |
| 702 | ? (userImages.find((image) => image.id === record.attachmentId) ?? null) |
| 703 | : record?.kind === "file" |
| 704 | ? (userFiles.find((file) => file.id === record.attachmentId) ?? null) |
| 705 | : null; |
| 706 | return ( |
| 707 | <span data-thread-find-ignore> |
| 708 | <UserMessageContextReferenceChip |
| 709 | reference={reference} |
| 710 | record={record} |
| 711 | annotationImage={annotationImage} |
| 712 | attachment={attachment} |
| 713 | onExpandImage={(image) => { |
| 714 | const preview = buildExpandedImagePreview(userImages, image.id, copySnapshot); |
| 715 | if (preview) onImageExpand(preview); |
| 716 | }} |
| 717 | onOpenFile={onFileOpen} |
| 718 | onExpandVideo={(file) => { |
| 719 | const preview = buildAttachmentVideoPreview(ctx.activeThreadEnvironmentId, file); |
| 720 | if (preview) onImageExpand(preview); |
| 721 | }} |
| 722 | /> |
| 723 | </span> |
| 724 | ); |
| 725 | }; |
| 726 | |
| 727 | const mediaBlock = |
| 728 | regularImages.length > 0 || userVideos.length > 0 ? ( |
| 729 | <div |
| 730 | className={cn( |
| 731 | "grid", |
| 732 | !standaloneMedia |
| 733 | ? "mb-2 max-w-[210px] grid-cols-2 gap-2" |
| 734 | : loneImage |
| 735 | ? "max-w-full" |
| 736 | : collage |
| 737 | ? cn( |
| 738 | MEDIA_EDGE_CLASS_NAME, |
| 739 | "w-80 max-w-full gap-px overflow-hidden rounded-2xl bg-black/9 dark:bg-white/12", |
| 740 | mediaCornerClassName, |
| 741 | regularImages.length === 2 |
| 742 | ? "auto-rows-[12rem] grid-cols-2" |
| 743 | : regularImages.length === 3 |
| 744 | ? "auto-rows-[calc(6rem-0.5px)] grid-cols-[3fr_2fr]" |
| 745 | : "auto-rows-[6rem] grid-cols-2", |
| 746 | ) |
| 747 | : regularImages.length + userVideos.length === 1 |
| 748 | ? "w-80 max-w-full" |
| 749 | : "w-80 max-w-full grid-cols-2 gap-2", |
| 750 | )} |
| 751 | > |
| 752 | {regularImages.map((image, index) => ( |
| 753 | <div |
| 754 | key={image.id} |
| 755 | className={cn( |
| 756 | "bg-background/70", |
| 757 | image.source?.kind === "snap-shot" && image.previewUrl |
| 758 | ? cn( |
| 759 | "col-span-2", |
| 760 | standaloneMedia |
| 761 | ? cn( |
| 762 | MEDIA_EDGE_CLASS_NAME, |
| 763 | "min-h-16 justify-self-end overflow-hidden rounded-2xl", |
| 764 | loneImage && mediaCornerClassName, |
| 765 | ) |
| 766 | : SNAP_SHOT_ATTACHMENT_FRAME_CLASS, |
| 767 | ) |
| 768 | : loneImage |
| 769 | ? cn(MEDIA_EDGE_CLASS_NAME, "overflow-hidden rounded-2xl", mediaCornerClassName) |
| 770 | : collage |
| 771 | ? cn( |
| 772 | "overflow-hidden bg-background", |
| 773 | regularImages.length === 3 && index === 0 && "row-span-2", |
| 774 | regularImages.length > 3 && |
| 775 | regularImages.length % 2 === 1 && |
| 776 | index === regularImages.length - 1 && |
| 777 | "col-span-2", |
| 778 | ) |
| 779 | : standaloneMedia |
| 780 | ? cn(MEDIA_EDGE_CLASS_NAME, "aspect-[4/3] overflow-hidden rounded-2xl") |
| 781 | : "aspect-[4/3] overflow-hidden rounded-lg border border-border/80", |
| 782 | )} |
| 783 | > |
| 784 | {image.previewUrl ? ( |
| 785 | <button |
| 786 | type="button" |
| 787 | className="block h-full w-full cursor-zoom-in" |
| 788 | aria-label={gt("Preview {label}", { label: image.name })} |
| 789 | onClick={() => { |
| 790 | const preview = buildExpandedImagePreview(regularImages, image.id, copySnapshot); |
| 791 | if (!preview) return; |
| 792 | ctx.onImageExpand(preview); |
| 793 | }} |
| 794 | > |
| 795 | {/* Isolate the thumbnail raster from nearby activity repaints in Chromium. */} |
| 796 | <AttachmentImage |
| 797 | name={image.name} |
| 798 | mimeType={image.mimeType} |
| 799 | src={image.previewUrl} |
| 800 | alt={image.name} |
| 801 | decoding="async" |
| 802 | loading="lazy" |
| 803 | className={cn( |
| 804 | "block transform-gpu", |
| 805 | standaloneMedia && image.source?.kind === "snap-shot" |
| 806 | ? "h-auto max-h-48 w-auto min-w-24 max-w-full object-contain" |
| 807 | : loneImage |
| 808 | ? "h-48 w-auto min-w-24 max-w-full object-contain" |
| 809 | : "size-full object-cover", |
| 810 | )} |
| 811 | /> |
| 812 | </button> |
| 813 | ) : ( |
| 814 | <div |
| 815 | className={cn( |
| 816 | "flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-2xs text-muted-foreground/70", |
| 817 | loneImage && "h-48 w-64 max-w-full", |
| 818 | collage && "h-full", |
| 819 | )} |
| 820 | > |
| 821 | {image.name} |
| 822 | </div> |
| 823 | )} |
| 824 | {image.previewUrl && image.source?.kind === "snap-shot" ? ( |
| 825 | <SnapShotAttachmentDetails |
| 826 | source={image.source} |
| 827 | onCopySnapshot={() => copySnapshot(image.id)} |
| 828 | /> |
| 829 | ) : null} |
| 830 | </div> |
| 831 | ))} |
| 832 | {userVideos.map((file) => ( |
| 833 | <UserVideoAttachment |
| 834 | key={file.id} |
| 835 | file={file} |
| 836 | standalone={standaloneMedia} |
| 837 | cornerClassName={ |
| 838 | regularImages.length + userVideos.length === 1 ? mediaCornerClassName : undefined |
| 839 | } |
| 840 | /> |
| 841 | ))} |
| 842 | </div> |
| 843 | ) : null; |
| 844 | |
| 845 | // Touch shows actions all the time, and narrow lanes have no room beside the |
| 846 | // bubble, so both keep the actions on their own line. |
| 847 | const floatActions = row.groupedWithNext === true && !coarsePointer; |
| 848 | const messageActions = ( |
| 849 | <div |
| 850 | className={cn( |
| 851 | "flex items-center justify-end gap-x-2 gap-y-0.5", |
| 852 | floatActions ? "flex-wrap" : "shrink-0", |
| 853 | )} |
| 854 | > |
| 855 | {folded && senderRef && canOpenSender ? ( |
| 856 | <InlineButton |
| 857 | onClick={() => ctx.onOpenThread(senderRef.threadId, senderRef.environmentId)} |
| 858 | tone="muted" |
| 859 | > |
| 860 | <T>Open thread</T> |
| 861 | </InlineButton> |
| 862 | ) : null} |
| 863 | <Tooltip> |
| 864 | <TooltipTrigger render={<p className="text-muted-foreground text-xs tabular-nums" />}> |
| 865 | {formatDayAwareTimestamp( |
| 866 | row.message.createdAt, |
| 867 | ctx.timestampFormat, |
| 868 | undefined, |
| 869 | undefined, |
| 870 | m, |
| 871 | )} |
| 872 | </TooltipTrigger> |
| 873 | <TooltipPopup> |
| 874 | {formatChatTimestampTooltip(row.message.createdAt, ctx.timestampFormat, m)} |
| 875 | </TooltipPopup> |
| 876 | </Tooltip> |
| 877 | <div className={cn("flex items-center gap-0.5", floatActions && "flex-wrap justify-end")}> |
| 878 | {typeof revertTurnCount === "number" && ( |
| 879 | <RevertUserMessageButton turnCount={revertTurnCount} messageId={row.message.id} /> |
| 880 | )} |
| 881 | {copyText && ( |
| 882 | <MessageCopyButton |
| 883 | text={contextClipboardFragment ? copyText : plainText} |
| 884 | {...(contextClipboardFragment |
| 885 | ? { |
| 886 | extraFlavors: { |
| 887 | [COMPOSER_CONTEXT_CLIPBOARD_MIME]: contextClipboardFragment, |
| 888 | }, |
| 889 | } |
| 890 | : {})} |
| 891 | variant="ghost" |
| 892 | /> |
| 893 | )} |
| 894 | {promptLink !== null && ( |
| 895 | <MessageCopyButton |
| 896 | text={promptLink} |
| 897 | label={gt("Copy prompt link")} |
| 898 | icon={LinkIcon} |
| 899 | variant="ghost" |
| 900 | /> |
| 901 | )} |
| 902 | </div> |
| 903 | </div> |
| 904 | ); |
| 905 | return ( |
| 906 | <div |
| 907 | className="group @container flex flex-col items-end gap-1" |
| 908 | data-user-message-attribution={folded ? "agent" : undefined} |
| 909 | > |
| 910 | {userMessage.attribution === "t3code" ? ( |
| 911 | <p |
| 912 | className="me-1 text-2xs text-muted-foreground/70" |
| 913 | data-user-message-attribution="t3code" |
| 914 | > |
| 915 | <T>Sent by T3 Code</T> |
| 916 | </p> |
| 917 | ) : null} |
| 918 | {/* The row's heading already names the guest for screen readers. */} |
| 919 | {userMessage.guestName !== undefined ? ( |
| 920 | <div aria-hidden> |
| 921 | <GuestMessageName name={userMessage.guestName} /> |
| 922 | </div> |
| 923 | ) : null} |
| 924 | {(row.message.inputIntent === "steer" || |
| 925 | row.message.inputIntent === "promoted_queued_to_steer") && |
| 926 | !row.continuesSteer ? ( |
| 927 | <UserMessageIntentMarker |
| 928 | intent={row.message.inputIntent} |
| 929 | unread={row.projectedItem?.item.status === "pending"} |
| 930 | /> |
| 931 | ) : null} |
| 932 | {mediaAboveText ? ( |
| 933 | <div className="flex max-w-[80%] justify-end"> |
| 934 | {/* The heading leads the media so screen readers reach it before the images. */} |
| 935 | <MessageAuthorHeading> |
| 936 | {gt("You", { $context: "author of the user's own message" })} |
| 937 | </MessageAuthorHeading> |
| 938 | {mediaBlock} |
| 939 | </div> |
| 940 | ) : null} |
| 941 | <div |
| 942 | className={cn( |
| 943 | "relative min-w-0 max-w-[80%] rounded-2xl text-message-foreground", |
| 944 | (mediaAboveText || row.groupedWithPrevious) && "rounded-tr-md", |
| 945 | row.groupedWithNext && "rounded-br-md", |
| 946 | BUBBLE_CHIP_CLASS_NAME, |
| 947 | userMessage.attribution === "guest" |
| 948 | ? guestBubbleClassName(false) |
| 949 | : !mediaOnly && !fileCardsOnly && !audioOnly && "bg-message", |
| 950 | !folded && !mediaOnly && !fileCardsOnly && !audioOnly && "p-3", |
| 951 | folded && expanded && "w-full", |
| 952 | )} |
| 953 | data-user-message-bubble="" |
| 954 | > |
| 955 | {/* Outside the fold so a collapsed message still has a heading to navigate to. */} |
| 956 | {mediaAboveText ? null : ( |
| 957 | <MessageAuthorHeading> |
| 958 | {folded |
| 959 | ? senderLabel |
| 960 | : userMessage.guestName === undefined |
| 961 | ? gt("You", { $context: "author of the user's own message" }) |
| 962 | : gt("{name} (guest)", { name: userMessage.guestName })} |
| 963 | </MessageAuthorHeading> |
| 964 | )} |
| 965 | {folded ? ( |
| 966 | <FoldedMessageHeader |
| 967 | icon={MessagesSquareIcon} |
| 968 | label={senderLabel} |
| 969 | fitLabel={!senderTitle} |
| 970 | summary={foldSummary} |
| 971 | expanded={expanded} |
| 972 | onToggle={() => setExpanded((value) => !value)} |
| 973 | /> |
| 974 | ) : null} |
| 975 | {/* Find reads the DOM, so a folded message stays mounted but hidden while find is open. */} |
| 976 | {!folded || expanded || ctx.findActive ? ( |
| 977 | <div |
| 978 | ref={folded ? revealFoldRef : undefined} |
| 979 | hidden={folded && !expanded} |
| 980 | dir={markdownProseDirection(resolvedContext.text)} |
| 981 | className={cn( |
| 982 | folded && "px-3 pb-3", |
| 983 | folded && !ctx.findActive && "max-h-96 overflow-auto", |
| 984 | )} |
| 985 | > |
| 986 | {!mediaAboveText && mediaBlock} |
| 987 | {userAudio.length > 0 ? ( |
| 988 | <div className={cn("flex max-w-full flex-col gap-1", !audioOnly && "mb-2")}> |
| 989 | {userAudio.map((file) => ( |
| 990 | <InlineAudioPreview |
| 991 | key={file.id} |
| 992 | name={file.name} |
| 993 | variant={audioOnly ? "bubble" : "chip"} |
| 994 | environmentId={ctx.activeThreadEnvironmentId} |
| 995 | resource={{ |
| 996 | _tag: "attachment", |
| 997 | attachmentId: file.id, |
| 998 | fileName: file.name, |
| 999 | mimeType: file.mimeType, |
| 1000 | disposition: "inline", |
| 1001 | }} |
| 1002 | onOpen={() => ctx.onFileOpen(file)} |
| 1003 | /> |
| 1004 | ))} |
| 1005 | </div> |
| 1006 | ) : null} |
| 1007 | {fileCardsOnly ? ( |
| 1008 | <div className="flex flex-col items-end gap-1" onCopyCapture={onBodyCopyCapture}> |
| 1009 | {otherUserFiles.map((file) => ( |
| 1010 | <UserFileCard |
| 1011 | key={file.id} |
| 1012 | file={file} |
| 1013 | copyMarkdown={ |
| 1014 | attachmentReferences.find((reference) => reference.attachmentId === file.id) |
| 1015 | ?.source |
| 1016 | } |
| 1017 | /> |
| 1018 | ))} |
| 1019 | </div> |
| 1020 | ) : null} |
| 1021 | {!fileCardsOnly && (unchippedFiles.length > 0 || unknownAttachments.length > 0) ? ( |
| 1022 | <div className="mb-2 flex flex-col gap-1"> |
| 1023 | {unchippedFiles.map((file) => { |
| 1024 | const pastedText = file.source?._tag === "pasted-text"; |
| 1025 | const label = userMessageFileLabel(file, m); |
| 1026 | const fileIdentity = ( |
| 1027 | <> |
| 1028 | <PierreEntryIcon |
| 1029 | pathValue={file.name} |
| 1030 | kind="file" |
| 1031 | theme={ctx.resolvedTheme} |
| 1032 | /> |
| 1033 | <span className="min-w-0 flex-1"> |
| 1034 | <span className="block truncate">{label}</span> |
| 1035 | {file.downloadable === false || pastedText ? ( |
| 1036 | <span className="block text-xs text-muted-foreground tabular-nums"> |
| 1037 | {file.downloadable === false ? ( |
| 1038 | <T>Sending…</T> |
| 1039 | ) : ( |
| 1040 | formatBytes(file.sizeBytes) |
| 1041 | )} |
| 1042 | </span> |
| 1043 | ) : null} |
| 1044 | </span> |
| 1045 | </> |
| 1046 | ); |
| 1047 | if (file.downloadable !== false) { |
| 1048 | return ( |
| 1049 | <div key={file.id} className="flex min-w-0 items-center gap-1"> |
| 1050 | <button |
| 1051 | type="button" |
| 1052 | aria-label={gt("Preview {label}", { label })} |
| 1053 | onClick={() => ctx.onFileOpen(file)} |
| 1054 | 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" |
| 1055 | > |
| 1056 | {fileIdentity} |
| 1057 | <EyeIcon className="size-4 shrink-0" /> |
| 1058 | </button> |
| 1059 | <Tooltip> |
| 1060 | <TooltipTrigger |
| 1061 | render={ |
| 1062 | <Button |
| 1063 | size="icon-xs" |
| 1064 | variant="ghost-muted" |
| 1065 | aria-label={gt("Download {label}", { label })} |
| 1066 | onClick={() => ctx.onFileDownload(file)} |
| 1067 | /> |
| 1068 | } |
| 1069 | > |
| 1070 | <DownloadIcon /> |
| 1071 | </TooltipTrigger> |
| 1072 | <TooltipPopup side="top"> |
| 1073 | <T> |
| 1074 | Download <Var>{label}</Var> |
| 1075 | </T> |
| 1076 | </TooltipPopup> |
| 1077 | </Tooltip> |
| 1078 | </div> |
| 1079 | ); |
| 1080 | } |
| 1081 | |
| 1082 | return ( |
| 1083 | <div key={file.id} className="flex min-w-0 items-center gap-2 py-1 text-sm"> |
| 1084 | {fileIdentity} |
| 1085 | </div> |
| 1086 | ); |
| 1087 | })} |
| 1088 | {unknownAttachments.map((attachment) => ( |
| 1089 | <div key={attachment.id} className="flex min-w-0 items-center gap-2 py-1 text-sm"> |
| 1090 | <PierreEntryIcon |
| 1091 | pathValue={attachment.name} |
| 1092 | kind="file" |
| 1093 | theme={ctx.resolvedTheme} |
| 1094 | /> |
| 1095 | <span className="min-w-0 flex-1 truncate">{attachment.name}</span> |
| 1096 | </div> |
| 1097 | ))} |
| 1098 | </div> |
| 1099 | ) : null} |
| 1100 | {fileCardsOnly || audioOnly ? null : ( |
| 1101 | <div onCopyCapture={onBodyCopyCapture}> |
| 1102 | <CollapsibleUserMessageBody |
| 1103 | text={resolvedContext.text} |
| 1104 | renderContextReference={renderContextReference} |
| 1105 | skills={ctx.skills} |
| 1106 | markdownCwd={ctx.markdownCwd} |
| 1107 | collapsible={!folded} |
| 1108 | /> |
| 1109 | </div> |
| 1110 | )} |
| 1111 | </div> |
| 1112 | ) : null} |
| 1113 | {floatActions ? ( |
| 1114 | <div |
| 1115 | className={cn( |
| 1116 | "absolute end-full top-1/2 me-2 hidden w-max max-w-[calc(20cqw-0.5rem)] -translate-y-1/2 @min-[37rem]:flex", |
| 1117 | ACTIONS_REVEAL_CLASS_NAME, |
| 1118 | )} |
| 1119 | > |
| 1120 | {messageActions} |
| 1121 | </div> |
| 1122 | ) : null} |
| 1123 | </div> |
| 1124 | <UserMessageProjectedStatus row={row} /> |
| 1125 | {!folded || expanded ? ( |
| 1126 | <div |
| 1127 | className={cn( |
| 1128 | "flex w-full max-w-[80%] justify-end pe-1", |
| 1129 | floatActions && "@min-[37rem]:hidden", |
| 1130 | ACTIONS_REVEAL_CLASS_NAME, |
| 1131 | )} |
| 1132 | > |
| 1133 | {messageActions} |
| 1134 | </div> |
| 1135 | ) : null} |
| 1136 | </div> |
| 1137 | ); |
| 1138 | } |
| 1139 | |
| 1140 | const ACTIONS_REVEAL_CLASS_NAME = |
| 1141 | "items-center text-xs tabular-nums opacity-0 transition-opacity duration-200 pointer-coarse:opacity-100 focus-within:opacity-100 group-hover:opacity-100"; |
| 1142 | |
| 1143 | function UserMessageIntentMarker({ |
| 1144 | intent, |
| 1145 | unread, |
| 1146 | }: { |
| 1147 | readonly intent: "steer" | "promoted_queued_to_steer"; |
| 1148 | readonly unread: boolean; |
| 1149 | }) { |
| 1150 | const gt = useGT(); |
| 1151 | return ( |
| 1152 | <Tooltip> |
| 1153 | <TooltipTrigger |
| 1154 | render={ |
| 1155 | <div |
| 1156 | className="me-1 flex items-center justify-end gap-1 text-xs leading-none text-muted-foreground" |
| 1157 | data-user-message-intent={intent} |
| 1158 | /> |
| 1159 | } |
| 1160 | > |
| 1161 | <Redo2Icon aria-hidden="true" className="size-3" /> |
| 1162 | {unread |
| 1163 | ? gt("Steer · waiting to be read") |
| 1164 | : gt("Steer", { $context: "noun, a message that steers the running turn" })} |
| 1165 | </TooltipTrigger> |
| 1166 | <TooltipPopup side="top"> |
| 1167 | {unread |
| 1168 | ? gt( |
| 1169 | "Waiting for the agent to read this. It reads steers between steps, after a running command finishes.", |
| 1170 | ) |
| 1171 | : intent === "promoted_queued_to_steer" |
| 1172 | ? gt("Originally queued, then promoted to steer the active turn") |
| 1173 | : gt("Steered the active turn")} |
| 1174 | </TooltipPopup> |
| 1175 | </Tooltip> |
| 1176 | ); |
| 1177 | } |
| 1178 | |
| 1179 | function RevertUserMessageButton({ |
| 1180 | turnCount, |
| 1181 | messageId, |
| 1182 | }: { |
| 1183 | turnCount: number; |
| 1184 | messageId: MessageId; |
| 1185 | }) { |
| 1186 | const ctx = use(TimelineRowCtx); |
| 1187 | const activity = use(TimelineRowActivityCtx); |
| 1188 | const gt = useGT(); |
| 1189 | |
| 1190 | return ( |
| 1191 | <Tooltip> |
| 1192 | <TooltipTrigger |
| 1193 | render={ |
| 1194 | <Button |
| 1195 | type="button" |
| 1196 | size="xs" |
| 1197 | variant="ghost" |
| 1198 | disabled={activity.isRevertingCheckpoint || activity.isWorking} |
| 1199 | onClick={() => ctx.onRevertToTurnCount(turnCount, messageId)} |
| 1200 | aria-label={gt("Edit from here")} |
| 1201 | /> |
| 1202 | } |
| 1203 | > |
| 1204 | <PencilIcon className="size-3" /> |
| 1205 | </TooltipTrigger> |
| 1206 | <TooltipPopup side="top"> |
| 1207 | <T>Edit from here</T> |
| 1208 | </TooltipPopup> |
| 1209 | </Tooltip> |
| 1210 | ); |
| 1211 | } |
| 1212 | |
| 1213 | const COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM = 1.75; |
| 1214 | |
| 1215 | const COLLAPSED_USER_MESSAGE_FADE_MASK = `linear-gradient(to bottom, black calc(100% - ${COLLAPSED_USER_MESSAGE_FADE_HEIGHT_REM}rem), transparent)`; |
| 1216 | |
| 1217 | const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(props: { |
| 1218 | text: string; |
| 1219 | renderContextReference: (reference: ChatMarkdownContextReference) => ReactNode; |
| 1220 | skills: ReadonlyArray<ServerProviderSkill>; |
| 1221 | markdownCwd: string | undefined; |
| 1222 | collapsible?: boolean; |
| 1223 | }) { |
| 1224 | const [expanded, setExpanded] = useState(false); |
| 1225 | const revealForFind = () => setExpanded(true); |
| 1226 | const revealRef = findRevealRef(revealForFind); |
| 1227 | const hasVisibleBody = props.text.trim().length > 0; |
| 1228 | const { ref: contentRef, tallEnough: canCollapse } = useTallEnoughToFold( |
| 1229 | props.collapsible !== false && hasVisibleBody, |
| 1230 | ); |
| 1231 | const isCollapsed = canCollapse && !expanded; |
| 1232 | const showCollapseControl = canCollapse; |
| 1233 | |
| 1234 | return ( |
| 1235 | <div> |
| 1236 | {hasVisibleBody ? ( |
| 1237 | <div |
| 1238 | ref={revealRef} |
| 1239 | className={cn("relative", isCollapsed && "max-h-44 overflow-hidden")} |
| 1240 | data-user-message-body="true" |
| 1241 | data-thread-find-text="true" |
| 1242 | data-thread-find-fold={isCollapsed ? "" : undefined} |
| 1243 | data-user-message-collapsed={isCollapsed ? "true" : "false"} |
| 1244 | data-user-message-collapsible={canCollapse ? "true" : "false"} |
| 1245 | data-user-message-fade={isCollapsed ? "true" : "false"} |
| 1246 | style={ |
| 1247 | isCollapsed |
| 1248 | ? { |
| 1249 | WebkitMaskImage: COLLAPSED_USER_MESSAGE_FADE_MASK, |
| 1250 | maskImage: COLLAPSED_USER_MESSAGE_FADE_MASK, |
| 1251 | } |
| 1252 | : undefined |
| 1253 | } |
| 1254 | > |
| 1255 | <div ref={contentRef}> |
| 1256 | <UserMessageBody |
| 1257 | text={props.text} |
| 1258 | renderContextReference={props.renderContextReference} |
| 1259 | skills={props.skills} |
| 1260 | markdownCwd={props.markdownCwd} |
| 1261 | /> |
| 1262 | </div> |
| 1263 | </div> |
| 1264 | ) : null} |
| 1265 | {showCollapseControl ? ( |
| 1266 | <div className="mt-1.5 flex items-center justify-end gap-2" data-user-message-footer="true"> |
| 1267 | {showCollapseControl ? ( |
| 1268 | <Button |
| 1269 | type="button" |
| 1270 | size="xs" |
| 1271 | variant="ghost-muted" |
| 1272 | aria-expanded={expanded} |
| 1273 | data-scroll-anchor-ignore |
| 1274 | onClick={() => setExpanded((value) => !value)} |
| 1275 | className="-ml-1" |
| 1276 | > |
| 1277 | {expanded ? <T>Show less</T> : <T>Show full message</T>} |
| 1278 | </Button> |
| 1279 | ) : null} |
| 1280 | </div> |
| 1281 | ) : null} |
| 1282 | </div> |
| 1283 | ); |
| 1284 | }); |
| 1285 | |
| 1286 | const USER_MESSAGE_REMARK_PLUGINS = [ |
| 1287 | ...CHAT_USER_MARKDOWN_REMARK_PLUGINS, |
| 1288 | remarkEnableOrderedListNumbers, |
| 1289 | ]; |
| 1290 | |
| 1291 | const UserMessageBody = memo(function UserMessageBody(props: { |
| 1292 | text: string; |
| 1293 | renderContextReference?: (reference: ChatMarkdownContextReference) => ReactNode; |
| 1294 | skills: ReadonlyArray<ServerProviderSkill>; |
| 1295 | markdownCwd: string | undefined; |
| 1296 | }) { |
| 1297 | const ctx = use(TimelineRowCtx); |
| 1298 | if (props.text.length === 0) { |
| 1299 | return null; |
| 1300 | } |
| 1301 | return ( |
| 1302 | <ChatMarkdown |
| 1303 | text={props.text} |
| 1304 | cwd={props.markdownCwd} |
| 1305 | threadRef={ctx.threadRef ?? undefined} |
| 1306 | skills={props.skills} |
| 1307 | className="text-message-foreground user-message-markdown" |
| 1308 | lineBreaks |
| 1309 | parseRawHtml={false} |
| 1310 | extraRemarkPlugins={USER_MESSAGE_REMARK_PLUGINS} |
| 1311 | renderContextReference={props.renderContextReference} |
| 1312 | headingLevelOffset={MESSAGE_HEADING_LEVEL} |
| 1313 | /> |
| 1314 | ); |
| 1315 | }); |