@devries-cameron20 / t3code-1 / commits / 7ffc3bb7e8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

refactor(web): chat markdown images and videos live in their own module (#22694)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 21, 2026 at 01:32 UTC 7ffc3bb7e80d88327a06d667e4be1bdef85329d3
4 files changed +482 -456
apps/web/src/components/ChatMarkdown.tsx
+12 -454
@@ -38,7 +38,6 @@ import {
38 import {
39 AssetWorkspaceAssetNotFoundError,
40 AuthPreviewOperateScope,
41 - type AssetResource,
41 type EditorId,
42 type EnvironmentId,
43 type ServerConfig,
@@ -60,8 +59,7 @@ import {
59 classifyMarkdownImageSource,
60 markdownImageSourceFragment,
61 } from "@t3tools/client-runtime/markdown-images";
63 -import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
64 -import { MISSING_FILE_ACCESS_MESSAGE } from "@t3tools/client-runtime/state/assets";
62 +import { mediaUrlReference } from "@t3tools/client-runtime/media-reference";
63 import { mediaKindFromPath, mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview";
64 import * as Cause from "effect/Cause";
65 import * as Option from "effect/Option";
@@ -69,14 +67,10 @@ import * as Schema from "effect/Schema";
67 import { AsyncResult, Atom } from "effect/reactivity";
68 import React, {
69 Suspense,
72 - type CSSProperties,
70 type ComponentProps,
71 type ClipboardEvent as ReactClipboardEvent,
75 - type KeyboardEvent as ReactKeyboardEvent,
76 - type MouseEvent as ReactMouseEvent,
72 use,
73 memo,
79 - useEffect,
74 useLayoutEffect,
75 useRef,
76 useState,
@@ -108,9 +102,8 @@ import {
102 type ExpandedImagePreview,
103 } from "./chat/ExpandedImagePreview";
104 import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
111 -import { markdownImageGallery, markdownImageItems } from "./chat/markdownImageGallery";
112 -import { MediaVideoPlayer } from "./media/MediaVideoPlayer";
113 -import { MediaActions, type MediaActionSource } from "./media/MediaActions";
105 +import { markdownImageGallery } from "./chat/markdownImageGallery";
106 +import { type MediaActionSource } from "./media/MediaActions";
107 import { resolveProtocolRelativeMediaUrl } from "./media/mediaContent";
108 import {
109 revealInFileExplorerLabelForKind,
@@ -149,7 +142,6 @@ import {
142 import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
143 import { isElectron } from "../env";
144 import { readLocalApi } from "../localApi";
152 -import { useAssetUrlRefresh, useAssetUrlState } from "../assets/assetUrls";
145 import { cn } from "../lib/utils";
146 import { resolveRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen";
147 import { useRightPanelStore } from "../rightPanelStore";
@@ -161,7 +153,6 @@ import {
153 readEnvironmentScope,
154 readPreparedConnection,
155 useEnvironmentScope,
164 - usePreparedConnection,
156 } from "../state/session";
157 import { previewEnvironment } from "../state/preview";
158 import { useAtomCommand } from "../state/use-atom-command";
@@ -198,6 +189,15 @@ import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBl
189 import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock";
190 import { MarkdownTable } from "./chat/MarkdownTable";
191 import { MarkdownDetails } from "./chat/MarkdownDetails";
192 +import {
193 + authoredImageSizeStyle,
194 + ChatMarkdownAssetImage,
195 + ChatMarkdownImage,
196 + ChatMarkdownImageFallback,
197 + ChatMarkdownVideo,
198 + MarkdownLinkContext,
199 + markdownImageCopy,
200 +} from "./chat/ChatMarkdownMedia";
201 import { MarkdownExternalLinkContent } from "./chat/MarkdownExternalLink";
202 import { MarkdownFileLink } from "./chat/MarkdownFileLink";
203 import { canUseMarkdownFileShellActions } from "./chat/MarkdownFileLink.logic";
@@ -380,448 +380,6 @@ function normalizeMarkdownLinkHrefKey(href: string): string {
380 : rewrittenHref;
381 }
382
383 -const CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME = "max-w-[min(100%,30rem)]";
384 -const CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME = cn(
385 - "max-h-[30rem]",
386 - CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
387 -);
388 -const CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME = "inline-block!";
389 -const CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME = "rounded-lg border border-border/40";
390 -const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(
391 - "h-auto w-auto object-contain",
392 - CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
393 -);
394 -
395 -function markdownImageCopy(alt: string, src: string, title: string | undefined): string {
396 - const escapedAlt = alt.replaceAll("\\", "\\\\").replaceAll("[", "\\[").replaceAll("]", "\\]");
397 - const titleSuffix =
398 - title === undefined ? "" : ` "${title.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
399 - return `![${escapedAlt}](${src}${titleSuffix})`;
400 -}
401 -
402 -/**
403 - * `maxHeightRem` folds a height cap into the width bound: `max-height` alone
404 - * would not feed back through `aspect-ratio` once `width` is definite, so a
405 - * tall image would keep a box wider than the picture it draws.
406 - */
407 -function authoredImageSizeStyle(
408 - width: string | number | undefined,
409 - height: string | number | undefined,
410 - maxHeightRem = 30,
411 -): CSSProperties | undefined {
412 - const parsedWidth = Number(width);
413 - const parsedHeight = Number(height);
414 - const hasWidth = Number.isFinite(parsedWidth) && parsedWidth > 0;
415 - const hasHeight = Number.isFinite(parsedHeight) && parsedHeight > 0;
416 - if (hasWidth && hasHeight) {
417 - return {
418 - width: parsedWidth,
419 - height: "auto",
420 - aspectRatio: `${parsedWidth} / ${parsedHeight}`,
421 - maxWidth: `min(100%, 30rem, ${(maxHeightRem * parsedWidth) / parsedHeight}rem)`,
422 - };
423 - }
424 - if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
425 - if (hasHeight) return { maxHeight: `min(30rem, ${parsedHeight}px)` };
426 - return undefined;
427 -}
428 -
429 -const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(
430 - CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
431 - CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
432 -);
433 -const MarkdownLinkContext = React.createContext(false);
434 -
435 -function expandableMarkdownImageProps(
436 - onImageExpand: ((preview: ExpandedImagePreview) => void) | undefined,
437 - alt: string,
438 -) {
439 - if (!onImageExpand) return {};
440 - const previewName = alt.trim() || "image";
441 - const expand = (event: ReactMouseEvent | ReactKeyboardEvent) => {
442 - if (event.currentTarget.closest("a")) return;
443 - event.preventDefault();
444 - event.stopPropagation();
445 - const item = markdownImageItems.get(event.currentTarget);
446 - if (item) onImageExpand(markdownImageGallery(event.currentTarget, item));
447 - };
448 - return {
449 - role: "button" as const,
450 - tabIndex: 0,
451 - "aria-label": `Preview ${previewName}`,
452 - onClick: expand,
453 - onKeyDown: (event: ReactKeyboardEvent) => {
454 - if (event.key === "Enter" || event.key === " ") expand(event);
455 - },
456 - };
457 -}
458 -
459 -function ChatMarkdownMediaUnavailableLabel(props: {
460 - readonly alt: string;
461 - readonly kind?: "image" | "video" | undefined;
462 - readonly detail?: string | undefined;
463 -}) {
464 - const label = props.kind === "video" ? "Video unavailable" : "Image unavailable";
465 - return (
466 - <span data-thread-find-ignore className="inline-flex items-center gap-1.5">
467 - <TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />
468 - {[label, props.alt, props.detail].filter(Boolean).join(" · ")}
469 - </span>
470 - );
471 -}
472 -
473 -function ChatMarkdownImageFallback(props: {
474 - readonly alt: string;
475 - readonly copyMarkdown?: string | undefined;
476 - readonly kind?: "image" | "video";
477 - readonly actionsSource?: MediaActionSource | undefined;
478 - readonly detail?: string | undefined;
479 -}) {
480 - const content = (
481 - <span
482 - data-markdown-copy={props.copyMarkdown}
483 - className={cn(
484 - CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
485 - "rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
486 - )}
487 - >
488 - <ChatMarkdownMediaUnavailableLabel alt={props.alt} kind={props.kind} detail={props.detail} />
489 - </span>
490 - );
491 - return props.actionsSource ? (
492 - <MediaActions source={props.actionsSource}>{content}</MediaActions>
493 - ) : (
494 - content
495 - );
496 -}
497 -
498 -const CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME = cn(
499 - "aspect-video w-full overflow-hidden bg-muted/60",
500 - CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
501 - CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
502 -);
503 -
504 -/**
505 - * Callers key this on the file's identity, not its URL: a re-signed URL for
506 - * the same file keeps the decoded image on screen while the new bytes arrive,
507 - * and a different file starts from the slot again.
508 - */
509 -function ChatMarkdownImage(props: {
510 - readonly src: string | null;
511 - readonly sourceFailed?: boolean | undefined;
512 - readonly unavailableDetail?: string | undefined;
513 - readonly alt: string;
514 - readonly copyMarkdown: string | undefined;
515 - readonly standalone: boolean;
516 - readonly className?: string | undefined;
517 - readonly style?: CSSProperties | undefined;
518 - readonly imageProps?:
519 - | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
520 - | undefined;
521 - readonly actionsSource: MediaActionSource;
522 - readonly originalUrl?: string | undefined;
523 - readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
524 -}) {
525 - const [loadedSrc, setLoadedSrc] = useState<string | null>(null);
526 - const [failedSrc, setFailedSrc] = useState<string | null>(null);
527 - const src = props.src ?? loadedSrc;
528 - const failed = props.sourceFailed === true || (src !== null && failedSrc === src);
529 - const settled = src !== null && !failed && (!props.standalone || loadedSrc !== null);
530 - // Cached images are complete before `onLoad` can fire.
531 - const markLoadedIfComplete = (image: HTMLImageElement | null) => {
532 - if (!image) return;
533 - if (image.complete && image.naturalWidth > 0) setLoadedSrc(image.currentSrc || image.src);
534 - markdownImageItems.set(image, {
535 - src,
536 - name: props.alt.trim() || "image",
537 - actionsSource: props.actionsSource,
538 - ...(props.originalUrl ? { originalUrl: props.originalUrl } : {}),
539 - });
540 - };
541 - const imageEvents = (loadingSrc: string) => ({
542 - onLoad: () => {
543 - setLoadedSrc(loadingSrc);
544 - setFailedSrc(null);
545 - },
546 - onError: () => {
547 - setFailedSrc(loadingSrc);
548 - setLoadedSrc(null);
549 - },
550 - });
551 -
552 - if (settled) {
553 - return (
554 - <MediaActions source={props.actionsSource}>
555 - <img
556 - {...props.imageProps}
557 - ref={markLoadedIfComplete}
558 - src={src}
559 - alt={props.alt}
560 - data-markdown-copy={props.copyMarkdown}
561 - decoding="async"
562 - draggable={false}
563 - className={cn(
564 - CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,
565 - props.className,
566 - props.onImageExpand && "cursor-zoom-in",
567 - )}
568 - style={props.style}
569 - {...expandableMarkdownImageProps(props.onImageExpand, props.alt)}
570 - {...imageEvents(src)}
571 - />
572 - </MediaActions>
573 - );
574 - }
575 - if (!props.standalone) {
576 - return failed ? (
577 - <ChatMarkdownImageFallback
578 - alt={props.alt}
579 - copyMarkdown={props.copyMarkdown}
580 - actionsSource={props.actionsSource}
581 - detail={props.unavailableDetail}
582 - />
583 - ) : (
584 - <span
585 - id={props.imageProps?.id}
586 - data-markdown-copy={props.copyMarkdown}
587 - role="status"
588 - aria-label="Loading image"
589 - className={CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME}
590 - />
591 - );
592 - }
593 - return (
594 - <MediaActions source={props.actionsSource}>
595 - <span
596 - id={props.imageProps?.id}
597 - data-markdown-copy={props.copyMarkdown}
598 - className={cn(
599 - CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
600 - CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME,
601 - "relative",
602 - )}
603 - style={props.style}
604 - {...(failed
605 - ? { role: "alert" as const }
606 - : { role: "status" as const, "aria-label": "Loading image" })}
607 - >
608 - {failed ? (
609 - <span className="flex size-full items-center justify-center p-2 text-center text-xs text-muted-foreground">
610 - <ChatMarkdownMediaUnavailableLabel alt={props.alt} detail={props.unavailableDetail} />
611 - </span>
612 - ) : src !== null ? (
613 - <img
614 - ref={markLoadedIfComplete}
615 - src={src}
616 - alt={props.alt}
617 - decoding="async"
618 - draggable={false}
619 - className="invisible absolute inset-0 size-full"
620 - {...imageEvents(src)}
621 - />
622 - ) : null}
623 - </span>
624 - </MediaActions>
625 - );
626 -}
627 -
628 -function ChatMarkdownVideo(props: {
629 - readonly src: string | null;
630 - readonly alt: string;
631 - readonly copyMarkdown: string | undefined;
632 - readonly originalUrl?: string | undefined;
633 - readonly sourceFailed?: boolean | undefined;
634 - readonly style?: CSSProperties | undefined;
635 - readonly mediaIdentity?: string | undefined;
636 - readonly actionsSource?: MediaActionSource | undefined;
637 - readonly onRetry?: (() => Promise<unknown>) | undefined;
638 -}) {
639 - return (
640 - <MediaVideoPlayer
641 - key={props.mediaIdentity ?? props.copyMarkdown ?? props.src}
642 - src={props.src}
643 - sourceFailed={props.sourceFailed}
644 - label={props.alt}
645 - originalUrl={props.originalUrl}
646 - style={props.style}
647 - copyMarkdown={props.copyMarkdown}
648 - className={cn(
649 - CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
650 - CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
651 - "w-full",
652 - )}
653 - videoClassName={cn(
654 - CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
655 - CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
656 - )}
657 - onRetry={props.onRetry}
658 - actionsSource={props.actionsSource}
659 - />
660 - );
661 -}
662 -
663 -export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
664 - readonly environmentId: EnvironmentId;
665 - readonly resource: Extract<
666 - AssetResource,
667 - {
668 - readonly _tag:
669 - | "attachment"
670 - | "workspace-file"
671 - | "media-file"
672 - | "github-media"
673 - | "tool-output-image";
674 - }
675 - >;
676 - readonly kind?: "image" | "video";
677 - readonly alt: string;
678 - readonly copyMarkdown?: string;
679 - readonly srcFragment?: string;
680 - readonly standalone?: boolean | undefined;
681 - readonly maxHeightRem?: number | undefined;
682 - readonly style?: CSSProperties | undefined;
683 - readonly className?: string | undefined;
684 - readonly imageProps?:
685 - | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
686 - | undefined;
687 - readonly originalUrl?: string | undefined;
688 - readonly framed?: boolean | undefined;
689 - readonly fallbackSrc?: string | undefined;
690 - readonly workspaceRoot?: string | undefined;
691 - readonly workspaceMutationId?: string | null | undefined;
692 - readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
693 -}) {
694 - const assetUrl = useAssetUrlState(props.environmentId, props.resource);
695 - const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource);
696 - const connectionReady = usePreparedConnection(props.environmentId)._tag === "Some";
697 - const resource = props.resource;
698 - const refreshToken = JSON.stringify([props.environmentId, resource, props.workspaceMutationId]);
699 - const handledRefreshToken = useRef<string | null | undefined>(undefined);
700 - const assetLoading = assetUrl._tag === "Loading" || assetUrl.waiting === true;
701 - useEffect(() => {
702 - if (!connectionReady) {
703 - handledRefreshToken.current = null;
704 - return;
705 - }
706 - if (
707 - props.workspaceMutationId === undefined ||
708 - props.kind === "video" ||
709 - (resource._tag !== "media-file" && resource._tag !== "workspace-file") ||
710 - handledRefreshToken.current === refreshToken
711 - ) {
712 - return;
713 - }
714 - if (assetLoading) {
715 - // Only the initial mount shares a pending request; edits and reconnects need a new one.
716 - if (handledRefreshToken.current === undefined) {
717 - handledRefreshToken.current = refreshToken;
718 - }
719 - return;
720 - }
721 - handledRefreshToken.current = refreshToken;
722 - void refreshAssetUrl().catch(() => undefined);
723 - }, [
724 - assetLoading,
725 - connectionReady,
726 - props.kind,
727 - props.workspaceMutationId,
728 - refreshAssetUrl,
729 - refreshToken,
730 - resource._tag,
731 - ]);
732 - const path =
733 - resource._tag === "media-file"
734 - ? resource.path
735 - : resource._tag === "workspace-file" && props.workspaceRoot
736 - ? `${props.workspaceRoot.replace(/[\\/]+$/, "")}/${resource.path}`
737 - : undefined;
738 - const reference = path
739 - ? mediaFileReference(path, props.workspaceRoot)
740 - : props.originalUrl
741 - ? mediaUrlReference(props.originalUrl)
742 - : undefined;
743 - const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
744 - const fallbackSrc = assetUrl._tag === "Failure" ? props.fallbackSrc : undefined;
745 - const src =
746 - assetUrl._tag === "Success"
747 - ? assetUrl.url + (props.srcFragment ?? "")
748 - : fallbackSrc === undefined
749 - ? null
750 - : fallbackSrc + (props.srcFragment ?? "");
751 - const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
752 - const maxHeightRem = props.maxHeightRem ?? 30;
753 - const style =
754 - props.style ??
755 - (knownSize
756 - ? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
757 - : maxHeightRem !== 30
758 - ? { maxHeight: `${maxHeightRem}rem` }
759 - : undefined);
760 - const actionsSource: MediaActionSource = {
761 - kind: props.kind ?? "image",
762 - name: props.alt || (props.kind ?? "image"),
763 - src,
764 - ...(fallbackSrc === undefined
765 - ? { asset: { environmentId: props.environmentId, resource } }
766 - : {}),
767 - ...(reference ? { reference } : {}),
768 - ...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file")
769 - ? {
770 - onOpenFile: () =>
771 - useRightPanelStore
772 - .getState()
773 - .openFile(
774 - { environmentId: props.environmentId, threadId: resource.threadId },
775 - relativePath,
776 - ),
777 - }
778 - : {}),
779 - };
780 -
781 - if (props.kind === "video") {
782 - return (
783 - <ChatMarkdownVideo
784 - src={src}
785 - sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
786 - alt={props.alt}
787 - copyMarkdown={props.copyMarkdown}
788 - originalUrl={props.originalUrl}
789 - style={props.style}
790 - mediaIdentity={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
791 - onRetry={refreshAssetUrl}
792 - actionsSource={actionsSource}
793 - />
794 - );
795 - }
796 -
797 - return (
798 - <ChatMarkdownImage
799 - key={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
800 - src={src}
801 - sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
802 - unavailableDetail={
803 - assetUrl._tag === "Failure" &&
804 - assetUrl.reason === "missing-file-access" &&
805 - fallbackSrc === undefined
806 - ? MISSING_FILE_ACCESS_MESSAGE
807 - : undefined
808 - }
809 - alt={props.alt}
810 - copyMarkdown={props.copyMarkdown}
811 - standalone={props.standalone ?? true}
812 - className={cn(
813 - props.framed === false ? undefined : CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME,
814 - props.className,
815 - )}
816 - style={style}
817 - imageProps={props.imageProps}
818 - actionsSource={actionsSource}
819 - originalUrl={props.originalUrl}
820 - onImageExpand={props.onImageExpand}
821 - />
822 - );
823 -});
824 -
383 // Heading ids are added after sanitizing, which would prefix them a second time.
384 const CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS = [
385 ...CHAT_MARKDOWN_REHYPE_PLUGINS,
apps/web/src/components/chat/ChatMarkdownMedia.tsx new
+466
@@ -0,0 +1,466 @@
1 +import { MISSING_FILE_ACCESS_MESSAGE } from "@t3tools/client-runtime/state/assets";
2 +import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
3 +import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
4 +import { TriangleAlertIcon } from "lucide-react";
5 +import {
6 + createContext,
7 + memo,
8 + useEffect,
9 + useRef,
10 + useState,
11 + type ComponentProps,
12 + type CSSProperties,
13 + type KeyboardEvent as ReactKeyboardEvent,
14 + type MouseEvent as ReactMouseEvent,
15 +} from "react";
16 +
17 +import { useAssetUrlRefresh, useAssetUrlState } from "../../assets/assetUrls";
18 +import { cn } from "../../lib/utils";
19 +import { useRightPanelStore } from "../../rightPanelStore";
20 +import { usePreparedConnection } from "../../state/session";
21 +import { MediaActions, type MediaActionSource } from "../media/MediaActions";
22 +import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
23 +import type { ExpandedImagePreview } from "./ExpandedImagePreview";
24 +import { markdownImageGallery, markdownImageItems } from "./markdownImageGallery";
25 +
26 +const CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME = "max-w-[min(100%,30rem)]";
27 +const CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME = cn(
28 + "max-h-[30rem]",
29 + CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
30 +);
31 +const CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME = "inline-block!";
32 +const CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME = "rounded-lg border border-border/40";
33 +const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(
34 + "h-auto w-auto object-contain",
35 + CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
36 +);
37 +
38 +export function markdownImageCopy(alt: string, src: string, title: string | undefined): string {
39 + const escapedAlt = alt.replaceAll("\\", "\\\\").replaceAll("[", "\\[").replaceAll("]", "\\]");
40 + const titleSuffix =
41 + title === undefined ? "" : ` "${title.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
42 + return `![${escapedAlt}](${src}${titleSuffix})`;
43 +}
44 +
45 +/**
46 + * `maxHeightRem` folds a height cap into the width bound: `max-height` alone
47 + * would not feed back through `aspect-ratio` once `width` is definite, so a
48 + * tall image would keep a box wider than the picture it draws.
49 + */
50 +export function authoredImageSizeStyle(
51 + width: string | number | undefined,
52 + height: string | number | undefined,
53 + maxHeightRem = 30,
54 +): CSSProperties | undefined {
55 + const parsedWidth = Number(width);
56 + const parsedHeight = Number(height);
57 + const hasWidth = Number.isFinite(parsedWidth) && parsedWidth > 0;
58 + const hasHeight = Number.isFinite(parsedHeight) && parsedHeight > 0;
59 + if (hasWidth && hasHeight) {
60 + return {
61 + width: parsedWidth,
62 + height: "auto",
63 + aspectRatio: `${parsedWidth} / ${parsedHeight}`,
64 + maxWidth: `min(100%, 30rem, ${(maxHeightRem * parsedWidth) / parsedHeight}rem)`,
65 + };
66 + }
67 + if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
68 + if (hasHeight) return { maxHeight: `min(30rem, ${parsedHeight}px)` };
69 + return undefined;
70 +}
71 +
72 +const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(
73 + CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
74 + CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
75 +);
76 +export const MarkdownLinkContext = createContext(false);
77 +
78 +function expandableMarkdownImageProps(
79 + onImageExpand: ((preview: ExpandedImagePreview) => void) | undefined,
80 + alt: string,
81 +) {
82 + if (!onImageExpand) return {};
83 + const previewName = alt.trim() || "image";
84 + const expand = (event: ReactMouseEvent | ReactKeyboardEvent) => {
85 + if (event.currentTarget.closest("a")) return;
86 + event.preventDefault();
87 + event.stopPropagation();
88 + const item = markdownImageItems.get(event.currentTarget);
89 + if (item) onImageExpand(markdownImageGallery(event.currentTarget, item));
90 + };
91 + return {
92 + role: "button" as const,
93 + tabIndex: 0,
94 + "aria-label": `Preview ${previewName}`,
95 + onClick: expand,
96 + onKeyDown: (event: ReactKeyboardEvent) => {
97 + if (event.key === "Enter" || event.key === " ") expand(event);
98 + },
99 + };
100 +}
101 +
102 +function ChatMarkdownMediaUnavailableLabel(props: {
103 + readonly alt: string;
104 + readonly kind?: "image" | "video" | undefined;
105 + readonly detail?: string | undefined;
106 +}) {
107 + const label = props.kind === "video" ? "Video unavailable" : "Image unavailable";
108 + return (
109 + <span data-thread-find-ignore className="inline-flex items-center gap-1.5">
110 + <TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />
111 + {[label, props.alt, props.detail].filter(Boolean).join(" · ")}
112 + </span>
113 + );
114 +}
115 +
116 +export function ChatMarkdownImageFallback(props: {
117 + readonly alt: string;
118 + readonly copyMarkdown?: string | undefined;
119 + readonly kind?: "image" | "video";
120 + readonly actionsSource?: MediaActionSource | undefined;
121 + readonly detail?: string | undefined;
122 +}) {
123 + const content = (
124 + <span
125 + data-markdown-copy={props.copyMarkdown}
126 + className={cn(
127 + CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
128 + "rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
129 + )}
130 + >
131 + <ChatMarkdownMediaUnavailableLabel alt={props.alt} kind={props.kind} detail={props.detail} />
132 + </span>
133 + );
134 + return props.actionsSource ? (
135 + <MediaActions source={props.actionsSource}>{content}</MediaActions>
136 + ) : (
137 + content
138 + );
139 +}
140 +
141 +const CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME = cn(
142 + "aspect-video w-full overflow-hidden bg-muted/60",
143 + CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
144 + CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
145 +);
146 +
147 +/**
148 + * Callers key this on the file's identity, not its URL: a re-signed URL for
149 + * the same file keeps the decoded image on screen while the new bytes arrive,
150 + * and a different file starts from the slot again.
151 + */
152 +export function ChatMarkdownImage(props: {
153 + readonly src: string | null;
154 + readonly sourceFailed?: boolean | undefined;
155 + readonly unavailableDetail?: string | undefined;
156 + readonly alt: string;
157 + readonly copyMarkdown: string | undefined;
158 + readonly standalone: boolean;
159 + readonly className?: string | undefined;
160 + readonly style?: CSSProperties | undefined;
161 + readonly imageProps?:
162 + | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
163 + | undefined;
164 + readonly actionsSource: MediaActionSource;
165 + readonly originalUrl?: string | undefined;
166 + readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
167 +}) {
168 + const [loadedSrc, setLoadedSrc] = useState<string | null>(null);
169 + const [failedSrc, setFailedSrc] = useState<string | null>(null);
170 + const src = props.src ?? loadedSrc;
171 + const failed = props.sourceFailed === true || (src !== null && failedSrc === src);
172 + const settled = src !== null && !failed && (!props.standalone || loadedSrc !== null);
173 + // Cached images are complete before `onLoad` can fire.
174 + const markLoadedIfComplete = (image: HTMLImageElement | null) => {
175 + if (!image) return;
176 + if (image.complete && image.naturalWidth > 0) setLoadedSrc(image.currentSrc || image.src);
177 + markdownImageItems.set(image, {
178 + src,
179 + name: props.alt.trim() || "image",
180 + actionsSource: props.actionsSource,
181 + ...(props.originalUrl ? { originalUrl: props.originalUrl } : {}),
182 + });
183 + };
184 + const imageEvents = (loadingSrc: string) => ({
185 + onLoad: () => {
186 + setLoadedSrc(loadingSrc);
187 + setFailedSrc(null);
188 + },
189 + onError: () => {
190 + setFailedSrc(loadingSrc);
191 + setLoadedSrc(null);
192 + },
193 + });
194 +
195 + if (settled) {
196 + return (
197 + <MediaActions source={props.actionsSource}>
198 + <img
199 + {...props.imageProps}
200 + ref={markLoadedIfComplete}
201 + src={src}
202 + alt={props.alt}
203 + data-markdown-copy={props.copyMarkdown}
204 + decoding="async"
205 + draggable={false}
206 + className={cn(
207 + CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,
208 + props.className,
209 + props.onImageExpand && "cursor-zoom-in",
210 + )}
211 + style={props.style}
212 + {...expandableMarkdownImageProps(props.onImageExpand, props.alt)}
213 + {...imageEvents(src)}
214 + />
215 + </MediaActions>
216 + );
217 + }
218 + if (!props.standalone) {
219 + return failed ? (
220 + <ChatMarkdownImageFallback
221 + alt={props.alt}
222 + copyMarkdown={props.copyMarkdown}
223 + actionsSource={props.actionsSource}
224 + detail={props.unavailableDetail}
225 + />
226 + ) : (
227 + <span
228 + id={props.imageProps?.id}
229 + data-markdown-copy={props.copyMarkdown}
230 + role="status"
231 + aria-label="Loading image"
232 + className={CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME}
233 + />
234 + );
235 + }
236 + return (
237 + <MediaActions source={props.actionsSource}>
238 + <span
239 + id={props.imageProps?.id}
240 + data-markdown-copy={props.copyMarkdown}
241 + className={cn(
242 + CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
243 + CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME,
244 + "relative",
245 + )}
246 + style={props.style}
247 + {...(failed
248 + ? { role: "alert" as const }
249 + : { role: "status" as const, "aria-label": "Loading image" })}
250 + >
251 + {failed ? (
252 + <span className="flex size-full items-center justify-center p-2 text-center text-xs text-muted-foreground">
253 + <ChatMarkdownMediaUnavailableLabel alt={props.alt} detail={props.unavailableDetail} />
254 + </span>
255 + ) : src !== null ? (
256 + <img
257 + ref={markLoadedIfComplete}
258 + src={src}
259 + alt={props.alt}
260 + decoding="async"
261 + draggable={false}
262 + className="invisible absolute inset-0 size-full"
263 + {...imageEvents(src)}
264 + />
265 + ) : null}
266 + </span>
267 + </MediaActions>
268 + );
269 +}
270 +
271 +export function ChatMarkdownVideo(props: {
272 + readonly src: string | null;
273 + readonly alt: string;
274 + readonly copyMarkdown: string | undefined;
275 + readonly originalUrl?: string | undefined;
276 + readonly sourceFailed?: boolean | undefined;
277 + readonly style?: CSSProperties | undefined;
278 + readonly mediaIdentity?: string | undefined;
279 + readonly actionsSource?: MediaActionSource | undefined;
280 + readonly onRetry?: (() => Promise<unknown>) | undefined;
281 +}) {
282 + return (
283 + <MediaVideoPlayer
284 + key={props.mediaIdentity ?? props.copyMarkdown ?? props.src}
285 + src={props.src}
286 + sourceFailed={props.sourceFailed}
287 + label={props.alt}
288 + originalUrl={props.originalUrl}
289 + style={props.style}
290 + copyMarkdown={props.copyMarkdown}
291 + className={cn(
292 + CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
293 + CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
294 + "w-full",
295 + )}
296 + videoClassName={cn(
297 + CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
298 + CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
299 + )}
300 + onRetry={props.onRetry}
301 + actionsSource={props.actionsSource}
302 + />
303 + );
304 +}
305 +
306 +export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
307 + readonly environmentId: EnvironmentId;
308 + readonly resource: Extract<
309 + AssetResource,
310 + {
311 + readonly _tag:
312 + | "attachment"
313 + | "workspace-file"
314 + | "media-file"
315 + | "github-media"
316 + | "tool-output-image";
317 + }
318 + >;
319 + readonly kind?: "image" | "video";
320 + readonly alt: string;
321 + readonly copyMarkdown?: string;
322 + readonly srcFragment?: string;
323 + readonly standalone?: boolean | undefined;
324 + readonly maxHeightRem?: number | undefined;
325 + readonly style?: CSSProperties | undefined;
326 + readonly className?: string | undefined;
327 + readonly imageProps?:
328 + | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
329 + | undefined;
330 + readonly originalUrl?: string | undefined;
331 + readonly framed?: boolean | undefined;
332 + readonly fallbackSrc?: string | undefined;
333 + readonly workspaceRoot?: string | undefined;
334 + readonly workspaceMutationId?: string | null | undefined;
335 + readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
336 +}) {
337 + const assetUrl = useAssetUrlState(props.environmentId, props.resource);
338 + const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource);
339 + const connectionReady = usePreparedConnection(props.environmentId)._tag === "Some";
340 + const resource = props.resource;
341 + const refreshToken = JSON.stringify([props.environmentId, resource, props.workspaceMutationId]);
342 + const handledRefreshToken = useRef<string | null | undefined>(undefined);
343 + const assetLoading = assetUrl._tag === "Loading" || assetUrl.waiting === true;
344 + useEffect(() => {
345 + if (!connectionReady) {
346 + handledRefreshToken.current = null;
347 + return;
348 + }
349 + if (
350 + props.workspaceMutationId === undefined ||
351 + props.kind === "video" ||
352 + (resource._tag !== "media-file" && resource._tag !== "workspace-file") ||
353 + handledRefreshToken.current === refreshToken
354 + ) {
355 + return;
356 + }
357 + if (assetLoading) {
358 + // Only the initial mount shares a pending request; edits and reconnects need a new one.
359 + if (handledRefreshToken.current === undefined) {
360 + handledRefreshToken.current = refreshToken;
361 + }
362 + return;
363 + }
364 + handledRefreshToken.current = refreshToken;
365 + void refreshAssetUrl().catch(() => undefined);
366 + }, [
367 + assetLoading,
368 + connectionReady,
369 + props.kind,
370 + props.workspaceMutationId,
371 + refreshAssetUrl,
372 + refreshToken,
373 + resource._tag,
374 + ]);
375 + const path =
376 + resource._tag === "media-file"
377 + ? resource.path
378 + : resource._tag === "workspace-file" && props.workspaceRoot
379 + ? `${props.workspaceRoot.replace(/[\\/]+$/, "")}/${resource.path}`
380 + : undefined;
381 + const reference = path
382 + ? mediaFileReference(path, props.workspaceRoot)
383 + : props.originalUrl
384 + ? mediaUrlReference(props.originalUrl)
385 + : undefined;
386 + const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
387 + const fallbackSrc = assetUrl._tag === "Failure" ? props.fallbackSrc : undefined;
388 + const src =
389 + assetUrl._tag === "Success"
390 + ? assetUrl.url + (props.srcFragment ?? "")
391 + : fallbackSrc === undefined
392 + ? null
393 + : fallbackSrc + (props.srcFragment ?? "");
394 + const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
395 + const maxHeightRem = props.maxHeightRem ?? 30;
396 + const style =
397 + props.style ??
398 + (knownSize
399 + ? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
400 + : maxHeightRem !== 30
401 + ? { maxHeight: `${maxHeightRem}rem` }
402 + : undefined);
403 + const actionsSource: MediaActionSource = {
404 + kind: props.kind ?? "image",
405 + name: props.alt || (props.kind ?? "image"),
406 + src,
407 + ...(fallbackSrc === undefined
408 + ? { asset: { environmentId: props.environmentId, resource } }
409 + : {}),
410 + ...(reference ? { reference } : {}),
411 + ...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file")
412 + ? {
413 + onOpenFile: () =>
414 + useRightPanelStore
415 + .getState()
416 + .openFile(
417 + { environmentId: props.environmentId, threadId: resource.threadId },
418 + relativePath,
419 + ),
420 + }
421 + : {}),
422 + };
423 +
424 + if (props.kind === "video") {
425 + return (
426 + <ChatMarkdownVideo
427 + src={src}
428 + sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
429 + alt={props.alt}
430 + copyMarkdown={props.copyMarkdown}
431 + originalUrl={props.originalUrl}
432 + style={props.style}
433 + mediaIdentity={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
434 + onRetry={refreshAssetUrl}
435 + actionsSource={actionsSource}
436 + />
437 + );
438 + }
439 +
440 + return (
441 + <ChatMarkdownImage
442 + key={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
443 + src={src}
444 + sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
445 + unavailableDetail={
446 + assetUrl._tag === "Failure" &&
447 + assetUrl.reason === "missing-file-access" &&
448 + fallbackSrc === undefined
449 + ? MISSING_FILE_ACCESS_MESSAGE
450 + : undefined
451 + }
452 + alt={props.alt}
453 + copyMarkdown={props.copyMarkdown}
454 + standalone={props.standalone ?? true}
455 + className={cn(
456 + props.framed === false ? undefined : CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME,
457 + props.className,
458 + )}
459 + style={style}
460 + imageProps={props.imageProps}
461 + actionsSource={actionsSource}
462 + originalUrl={props.originalUrl}
463 + onImageExpand={props.onImageExpand}
464 + />
465 + );
466 +});
apps/web/src/components/chat/TimelineWorkEntryRow.tsx
+2 -1
@@ -28,7 +28,8 @@ import {
28 workEntrySignalsSevereFailure,
29 workLogEntryIsToolLike,
30 } from "../../session-logic";
31 -import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";
31 +import ChatMarkdown from "../ChatMarkdown";
32 +import { ChatMarkdownAssetImage } from "./ChatMarkdownMedia";
33 import { ChevronRightIcon, CircleAlertIcon, XIcon, RotateCcwIcon } from "lucide-react";
34 import { Button, InlineButton } from "../ui/button";
35 import { useAssetUrls } from "../../assets/assetUrls";
apps/web/src/components/chat/V2ItemInspector.tsx
+2 -1
@@ -24,7 +24,8 @@ import { useTurnItemDetail } from "../../state/queries";
24 import { useV2ItemSupport } from "../../state/v2ItemSupport";
25 import { formatWorkspaceRelativePath } from "../../filePathDisplay";
26 import { Button } from "../ui/button";
27 -import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";
27 +import ChatMarkdown from "../ChatMarkdown";
28 +import { ChatMarkdownAssetImage } from "./ChatMarkdownMedia";
29 import { RenderErrorBoundary } from "../RenderErrorBoundary";
30 import { CommandOutputPanel, useCommandOutputStreaming } from "./CommandOutputPanel";
31 import { resolveExternalWebLinkHref } from "./externalLinkContextMenu";