Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): contain verbose media labels (#16488)

Tobias Wochinger3b1fac4ee031
diff --git a/web/src/components/MediaFileCard/MediaFileCard.clienttest.tsx b/web/src/components/MediaFileCard/MediaFileCard.clienttest.tsx
new file mode 100644
index 0000000000..e09ce2407c
--- /dev/null
+++ b/web/src/components/MediaFileCard/MediaFileCard.clienttest.tsx
@@ -0,0 +1,45 @@
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+import { MediaFileCard } from "./MediaFileCard";
+
+const OFFICE_CONTENT_TYPES = [
+ ["application/msword", "DOC"],
+ [
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ "DOCX",
+ ],
+ ["application/vnd.ms-excel", "XLS"],
+ ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "XLSX"],
+] as const;
+
+describe("MediaFileCard", () => {
+ it.each(OFFICE_CONTENT_TYPES)(
+ "renders %s as %s while keeping the MIME type in the title",
+ (contentType, label) => {
+ render(
+ <MediaFileCard
+ contentType={contentType}
+ fileName="document"
+ onClick={vi.fn()}
+ />,
+ );
+
+ const labelElement = screen.getByText(label);
+ expect(labelElement).toHaveAttribute("title", contentType);
+ expect(labelElement).not.toHaveClass("truncate");
+ },
+ );
+
+ it("renders FILE when the MIME subtype is empty", () => {
+ render(
+ <MediaFileCard
+ contentType="text/"
+ fileName="attachment"
+ onClick={vi.fn()}
+ />,
+ );
+
+ expect(screen.getByText("FILE")).toHaveAttribute("title", "text/");
+ });
+});
diff --git a/web/src/components/MediaFileCard/MediaFileCard.stories.tsx b/web/src/components/MediaFileCard/MediaFileCard.stories.tsx
new file mode 100644
index 0000000000..d153b1e313
--- /dev/null
+++ b/web/src/components/MediaFileCard/MediaFileCard.stories.tsx
@@ -0,0 +1,60 @@
+import { fn } from "storybook/test";
+import preview from "../../../.storybook/preview";
+import { MediaFileCard } from "./MediaFileCard";
+
+const meta = preview.meta({
+ component: MediaFileCard,
+});
+
+export const Default = meta.story({
+ args: {
+ contentType: "application/pdf",
+ fileName: "report.pdf",
+ onClick: fn(),
+ },
+});
+
+export const VerboseMimeType = meta.story({
+ args: {
+ contentType:
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ fileName: "report.docx",
+ onClick: fn(),
+ },
+});
+
+export const TruncatedMimeType = meta.story({
+ args: {
+ contentType:
+ "application/vnd.example.intentionally-verbose-archive-format-for-truncation",
+ fileName: "archive.bin",
+ onClick: fn(),
+ },
+});
+
+export const OfficeFormats = meta.story({
+ render: () => (
+ <div className="flex flex-wrap gap-3">
+ <MediaFileCard
+ contentType="application/msword"
+ fileName="report.doc"
+ onClick={fn()}
+ />
+ <MediaFileCard
+ contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ fileName="report.docx"
+ onClick={fn()}
+ />
+ <MediaFileCard
+ contentType="application/vnd.ms-excel"
+ fileName="report.xls"
+ onClick={fn()}
+ />
+ <MediaFileCard
+ contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
+ fileName="report.xlsx"
+ onClick={fn()}
+ />
+ </div>
+ ),
+});
diff --git a/web/src/components/MediaFileCard/MediaFileCard.tsx b/web/src/components/MediaFileCard/MediaFileCard.tsx
new file mode 100644
index 0000000000..35ba45fca1
--- /dev/null
+++ b/web/src/components/MediaFileCard/MediaFileCard.tsx
@@ -0,0 +1,69 @@
+import {
+ File,
+ Image as ImageIcon,
+ Video,
+ Volume2,
+ type LucideIcon,
+} from "lucide-react";
+import { getMappedMediaLabel } from "@/src/fns/getMappedMediaLabel";
+
+export type MediaFileCardProps = {
+ contentType: string;
+ fileName: string;
+ onClick: () => void;
+};
+
+const MEDIA_TYPE_ICON: Record<string, LucideIcon> = {
+ image: ImageIcon,
+ audio: Volume2,
+ video: Video,
+};
+
+export function MediaFileCard({
+ contentType,
+ fileName,
+ onClick,
+}: MediaFileCardProps) {
+ const mediaType = contentType.split("/")[0] ?? "";
+ const MediaIcon = MEDIA_TYPE_ICON[mediaType];
+ const Icon = MediaIcon ?? File;
+ const isPreviewable = MediaIcon !== undefined;
+ const mappedFileLabel = getMappedMediaLabel(contentType);
+ const mimeSubtypeLabel = contentType.split("/")[1]?.toUpperCase() || "FILE";
+ const fileLabel = mappedFileLabel ?? mimeSubtypeLabel;
+
+ return (
+ <button
+ type="button"
+ onClick={onClick}
+ aria-label={
+ isPreviewable
+ ? `Show ${fileName} inline`
+ : `Open ${fileName} in new tab`
+ }
+ aria-expanded={isPreviewable ? false : undefined}
+ title={fileName}
+ className="from-accent-light-green/30 to-muted hover:from-accent-light-green/40 hover:to-muted/90 dark:from-accent-dark-green/20 dark:to-muted dark:hover:from-accent-dark-green/30 group relative flex h-24 w-24 shrink-0 flex-col items-center justify-center gap-2 rounded-md border bg-linear-to-br px-2 transition-colors"
+ >
+ <Icon className="h-5 w-5 transition-transform group-hover:scale-110" />
+ <div className="flex w-full min-w-0 flex-col items-center gap-1">
+ <span
+ className={
+ mappedFileLabel
+ ? "shrink-0 text-sm font-bold whitespace-nowrap"
+ : "w-full truncate text-sm font-bold"
+ }
+ title={contentType}
+ >
+ {fileLabel}
+ </span>
+ <span
+ className="text-muted-foreground w-full truncate text-xs"
+ title={fileName}
+ >
+ {fileName}
+ </span>
+ </div>
+ </button>
+ );
+}
diff --git a/web/src/components/ui/media/MediaTag.clienttest.tsx b/web/src/components/MediaTag/MediaTag.clienttest.tsx
similarity index 71%
rename from web/src/components/ui/media/MediaTag.clienttest.tsx
rename to web/src/components/MediaTag/MediaTag.clienttest.tsx
index ebc6852922..796c4eb782 100644
--- a/web/src/components/ui/media/MediaTag.clienttest.tsx
+++ b/web/src/components/MediaTag/MediaTag.clienttest.tsx
@@ -3,6 +3,16 @@ import { describe, expect, it, vi } from "vitest";
import { MediaTag } from "./MediaTag";
+const OFFICE_CONTENT_TYPES = [
+ ["application/msword", "DOC"],
+ [
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+ "DOCX",
+ ],
+ ["application/vnd.ms-excel", "XLS"],
+ ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "XLSX"],
+] as const;
+
describe("MediaTag", () => {
it("opens the preview on click", async () => {
const onOpenChange = vi.fn();
@@ -54,11 +64,40 @@ describe("MediaTag", () => {
expect(chip.querySelector("span")?.getAttribute("title")).toBe("");
});
- it("keeps the label title while the peek is closed", () => {
+ it("keeps the MIME type title while the peek is closed", () => {
render(<MediaTag contentType="image/png" open={false} />);
const chip = screen.getByRole("button", { name: "PNG media" });
- expect(chip.querySelector("span")?.getAttribute("title")).toBe("PNG");
+ expect(chip.querySelector("span")?.getAttribute("title")).toBe("image/png");
+ });
+
+ it.each(OFFICE_CONTENT_TYPES)("renders %s as %s", (contentType, label) => {
+ render(<MediaTag contentType={contentType} />);
+
+ expect(
+ screen.getByRole("button", { name: `${label} media` }),
+ ).toBeInTheDocument();
+ });
+
+ it("caps generated MIME labels at ten characters", () => {
+ const contentType =
+ "application/vnd.example.intentionally-verbose-archive-format";
+ render(<MediaTag contentType={contentType} />);
+
+ const label = screen.getByRole("button").querySelector("span");
+ expect(label).toHaveClass("max-w-[10ch]", "truncate");
+ expect(label).toHaveAttribute("title", contentType);
+ });
+
+ it("caps the hover card MIME type at twenty characters", () => {
+ const contentType =
+ "application/vnd.example.intentionally-verbose-archive-format";
+ render(<MediaTag contentType={contentType} open />);
+
+ expect(screen.getByTitle(contentType)).toHaveClass(
+ "max-w-[20ch]",
+ "truncate",
+ );
});
it("shows the fallback when resolved image media fails to render", async () => {
diff --git a/web/src/components/ui/media/MediaTag.stories.tsx b/web/src/components/MediaTag/MediaTag.stories.tsx
similarity index 81%
rename from web/src/components/ui/media/MediaTag.stories.tsx
rename to web/src/components/MediaTag/MediaTag.stories.tsx
index 504a06e1fa..5b7c939b81 100644
--- a/web/src/components/ui/media/MediaTag.stories.tsx
+++ b/web/src/components/MediaTag/MediaTag.stories.tsx
@@ -1,5 +1,5 @@
import { fn } from "storybook/test";
-import preview from "../../../../.storybook/preview";
+import preview from "../../../.storybook/preview";
import { MediaTag } from "./MediaTag";
// Inline SVG so the "ready" image preview renders without any network.
@@ -129,6 +129,33 @@ export const OversizedField = meta.story({
},
});
+export const TruncatedMimeType = meta.story({
+ args: {
+ contentType:
+ "application/vnd.example.intentionally-verbose-archive-format-for-truncation",
+ },
+});
+
+export const TruncatedMimeTypePreview = meta.story({
+ args: {
+ contentType:
+ "application/vnd.example.intentionally-verbose-archive-format-for-truncation",
+ open: true,
+ status: "error",
+ },
+});
+
+export const OfficeFormats = meta.story({
+ render: () => (
+ <div className="flex flex-wrap items-center gap-3">
+ <MediaTag contentType="application/msword" />
+ <MediaTag contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
+ <MediaTag contentType="application/vnd.ms-excel" />
+ <MediaTag contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
+ </div>
+ ),
+});
+
// Design showcase: one chip per media kind, collapsed.
export const AllKinds = meta.story({
render: () => (
diff --git a/web/src/components/ui/media/MediaTag.tsx b/web/src/components/MediaTag/MediaTag.tsx
similarity index 94%
rename from web/src/components/ui/media/MediaTag.tsx
rename to web/src/components/MediaTag/MediaTag.tsx
index d38095489c..a663048f18 100644
--- a/web/src/components/ui/media/MediaTag.tsx
+++ b/web/src/components/MediaTag/MediaTag.tsx
@@ -19,6 +19,8 @@ import {
HoverCardContent,
HoverCardTrigger,
} from "@/src/components/ui/hover-card";
+import { cn } from "@/src/utils/tailwind";
+import { getMappedMediaLabel } from "@/src/fns/getMappedMediaLabel";
/**
* Resolution state of the previewable content. The collapsed chip renders the
@@ -74,7 +76,7 @@ function getMediaKind(contentType: string): MediaKind {
/** "image/svg+xml" -> "SVG", "application/pdf" -> "PDF". */
function getDefaultLabel(contentType: string): string {
const subtype = contentType.split("/")[1]?.split("+")[0];
- return (subtype || "file").toUpperCase();
+ return getMappedMediaLabel(contentType) ?? (subtype || "file").toUpperCase();
}
function formatFileSize(bytes: number): string {
@@ -224,6 +226,7 @@ export const MediaTag = React.forwardRef<HTMLButtonElement, MediaTagProps>(
) => {
const kind = getMediaKind(contentType);
const chipLabel = label ?? getDefaultLabel(contentType);
+ const hasGeneratedLabel = label === undefined;
const canOpen = status === "ready" && Boolean(url);
const [failedPreviewUrl, setFailedPreviewUrl] = React.useState<
string | null
@@ -271,11 +274,14 @@ export const MediaTag = React.forwardRef<HTMLButtonElement, MediaTagProps>(
<KindIcon kind={kind} className="h-2.5 w-2.5 shrink-0" />
)}
<span
- className="relative top-0.25 truncate align-baseline font-mono leading-none"
+ className={cn(
+ "relative top-0.25 truncate align-baseline font-mono leading-none",
+ hasGeneratedLabel && "max-w-[10ch]",
+ )}
// Empty while the peek is open: a native tooltip would render on
// top of the peek. Ancestors with a title still tooltip over the
// peek — containers must suppress theirs too (see IOTableCell).
- title={isOpen ? "" : chipLabel}
+ title={isOpen ? "" : hasGeneratedLabel ? contentType : chipLabel}
>
{chipLabel}
</span>
@@ -289,7 +295,7 @@ export const MediaTag = React.forwardRef<HTMLButtonElement, MediaTagProps>(
<div className="text-muted-foreground flex min-w-0 items-center gap-1.5 text-xs">
<KindIcon kind={kind} className="h-3.5 w-3.5 shrink-0" />
<span
- className="truncate font-mono leading-none"
+ className="max-w-[20ch] truncate font-mono leading-none"
title={contentType}
>
{contentType}
diff --git a/web/src/components/ui/LangfuseMediaView.tsx b/web/src/components/ui/LangfuseMediaView.tsx
index f89fbbc9ca..a2d7e341dc 100644
--- a/web/src/components/ui/LangfuseMediaView.tsx
+++ b/web/src/components/ui/LangfuseMediaView.tsx
@@ -3,14 +3,7 @@ import { cn } from "@/src/utils/tailwind";
import { useState } from "react";
import { Button } from "@/src/components/ui/button";
-import {
- ImageOff,
- ExternalLink,
- File,
- Image as ImageIcon,
- Video,
- Volume2,
-} from "lucide-react";
+import { ImageOff, ExternalLink } from "lucide-react";
import {
MediaReferenceStringSchema,
OBSERVATION_FIELD_SIZE_LIMIT_MEDIA_SOURCE,
@@ -26,6 +19,7 @@ import {
type MediaReturnType,
} from "@/src/features/media/validation";
import { MediaReferenceTag } from "@/src/components/ui/media/MediaReferenceTag";
+import { MediaFileCard } from "@/src/components/MediaFileCard/MediaFileCard";
// Above this, "preview" media falls back to the click-to-open icon instead of
// rendering inline, so a large file isn't fetched/decoded just by opening a view.
@@ -183,8 +177,6 @@ function FileViewer({
if (!src) return null;
const fileName = src.split("/").pop()?.split("?")[0] || "";
- const fileExtension = mimeType.split("/")[1]?.toUpperCase() || "FILE";
-
const openInNewTab = () => {
window.open(src, "_blank", "noopener,noreferrer");
};
@@ -209,27 +201,6 @@ function FileViewer({
image.src = src;
};
- const iconTile = (
- <div className="flex flex-col items-center gap-2">
- {isImage ? (
- <ImageIcon className="h-5 w-5 transition-transform group-hover:scale-110" />
- ) : isAudio ? (
- <Volume2 className="h-5 w-5 transition-transform group-hover:scale-110" />
- ) : isVideo ? (
- <Video className="h-5 w-5 transition-transform group-hover:scale-110" />
- ) : (
- <File className="h-5 w-5 transition-transform group-hover:scale-110" />
- )}
-
- <div className="flex flex-col items-center gap-1">
- <span className="text-sm font-bold">{fileExtension}</span>
- <span className="text-muted-foreground text-xs">
- {fileName.length > 5 ? `${fileName.slice(0, 5)}...` : fileName}
- </span>
- </div>
- </div>
- );
-
const previewContent = isImage ? (
<ResizableImage
src={src}
@@ -274,20 +245,11 @@ function FileViewer({
</Button>
</div>
) : (
- <button
- type="button"
+ <MediaFileCard
+ contentType={contentType}
+ fileName={fileName}
onClick={() => (isPreviewable ? expandPreview() : openInNewTab())}
- aria-label={
- isPreviewable
- ? `Show ${fileName} inline`
- : `Open ${fileName} in new tab`
- }
- aria-expanded={isPreviewable ? isExpanded : undefined}
- title={fileName}
- className="from-accent-light-green/30 to-muted hover:from-accent-light-green/40 hover:to-muted/90 dark:from-accent-dark-green/20 dark:to-muted dark:hover:from-accent-dark-green/30 group relative flex h-24 w-24 flex-col items-center justify-center gap-2 rounded-md border bg-linear-to-br px-2 transition-colors"
- >
- {iconTile}
- </button>
+ />
)}
</div>
);
diff --git a/web/src/components/ui/media/MediaReferenceTag.tsx b/web/src/components/ui/media/MediaReferenceTag.tsx
index c30aedb5c3..e37a97f03e 100644
--- a/web/src/components/ui/media/MediaReferenceTag.tsx
+++ b/web/src/components/ui/media/MediaReferenceTag.tsx
@@ -1,5 +1,5 @@
import { useState } from "react";
-import { MediaTag } from "./MediaTag";
+import { MediaTag } from "../../MediaTag/MediaTag";
import { useResolvedMedia } from "./useResolvedMedia";
import { type MediaDescriptor } from "./mediaUtils";
import { OBSERVATION_FIELD_SIZE_LIMIT_MEDIA_SOURCE } from "@langfuse/shared";
diff --git a/web/src/components/ui/media/useResolvedMedia.ts b/web/src/components/ui/media/useResolvedMedia.ts
index 0694272057..b945eba337 100644
--- a/web/src/components/ui/media/useResolvedMedia.ts
+++ b/web/src/components/ui/media/useResolvedMedia.ts
@@ -1,7 +1,7 @@
import { api } from "@/src/utils/api";
import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";
import { type MediaDescriptor } from "./mediaUtils";
-import { type MediaTagStatus } from "./MediaTag";
+import { type MediaTagStatus } from "../../MediaTag/MediaTag";
type LangfuseRefDescriptor = Extract<MediaDescriptor, { kind: "langfuseRef" }>;
diff --git a/web/src/fns/getMappedMediaLabel.ts b/web/src/fns/getMappedMediaLabel.ts
new file mode 100644
index 0000000000..2725397967
--- /dev/null
+++ b/web/src/fns/getMappedMediaLabel.ts
@@ -0,0 +1,11 @@
+const MAPPED_MEDIA_LABELS: Record<string, string> = {
+ "application/msword": "DOC",
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
+ "DOCX",
+ "application/vnd.ms-excel": "XLS",
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "XLSX",
+};
+
+export function getMappedMediaLabel(contentType: string): string | undefined {
+ return MAPPED_MEDIA_LABELS[contentType.toLowerCase()];
+}