Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧭 refactor: Normalize Model-Authored Artifact Types in One Place (#16947)

Marco Beretta7e9ec0f85887
diff --git a/client/src/components/Artifacts/Artifact.tsx b/client/src/components/Artifacts/Artifact.tsx
index d2e98be5f1..e7993e2b35 100644
--- a/client/src/components/Artifacts/Artifact.tsx
+++ b/client/src/components/Artifacts/Artifact.tsx
@@ -7,6 +7,7 @@ import type { Pluggable } from 'unified';
import type { Artifact } from '~/common';
import { useMessageContext, useArtifactContext } from '~/Providers';
import { logger, extractContent, isArtifactRoute } from '~/utils';
+import { normalizeArtifactType } from '~/utils/artifacts';
import { artifactsState } from '~/store/artifacts';
import ArtifactButton from './ArtifactButton';
@@ -81,7 +82,7 @@ export function Artifact({
id: artifactKey,
identifier,
title,
- type,
+ type: normalizeArtifactType(type),
content,
messageId,
index: artifactIndex,
diff --git a/client/src/components/Artifacts/ArtifactCodeEditor.tsx b/client/src/components/Artifacts/ArtifactCodeEditor.tsx
index c758ac4413..85c378688c 100644
--- a/client/src/components/Artifacts/ArtifactCodeEditor.tsx
+++ b/client/src/components/Artifacts/ArtifactCodeEditor.tsx
@@ -18,6 +18,7 @@ import {
useMutationState,
resolveServerContent,
} from '~/Providers/EditorContext';
+import { normalizeArtifactType } from '~/utils/artifacts';
import { getResponseStatus } from '~/utils/errors';
import { useArtifactsContext } from '~/Providers';
import { useEditArtifact } from '~/data-provider';
@@ -57,11 +58,8 @@ const LANG_MAP: Record<string, string> = {
const TYPE_MAP: Record<string, string> = {
'text/html': 'html',
- 'application/vnd.code-html': 'html',
'application/vnd.react': 'typescript',
- 'application/vnd.ant.react': 'typescript',
'text/markdown': 'markdown',
- 'text/md': 'markdown',
'text/plain': 'plaintext',
'application/vnd.mermaid': 'markdown',
};
@@ -208,7 +206,7 @@ function getMonacoLanguage(type?: string, language?: string): string {
if (language && LANG_MAP[language]) {
return LANG_MAP[language];
}
- return TYPE_MAP[type ?? ''] ?? 'plaintext';
+ return TYPE_MAP[normalizeArtifactType(type ?? '')] ?? 'plaintext';
}
function getArtifactEditTarget(artifact: Artifact): ArtifactEditTarget | null {
diff --git a/client/src/components/Artifacts/ArtifactTabs.tsx b/client/src/components/Artifacts/ArtifactTabs.tsx
index 956dcdcf28..8a6854d9b8 100644
--- a/client/src/components/Artifacts/ArtifactTabs.tsx
+++ b/client/src/components/Artifacts/ArtifactTabs.tsx
@@ -7,6 +7,7 @@ import type { ProcessedMermaidSvg } from '~/utils/diagram/export';
import { MermaidRenderer } from '~/components/Messages/Content/Mermaid/Mermaid';
import { MERMAID_ARTIFACT_TYPE, type Artifact } from '~/common/artifacts';
import { useArtifactCode } from '~/Providers/EditorContext';
+import { normalizeArtifactType } from '~/utils/artifacts';
import { ArtifactCodeEditor } from './ArtifactCodeEditor';
import { lazyWithRecovery } from '~/lib/assets/lazy';
import { useLocalize } from '~/hooks';
@@ -85,7 +86,7 @@ function MermaidArtifactTabs({
}
export default function ArtifactTabs(props: ArtifactTabsProps) {
- if (props.artifact.type === MERMAID_ARTIFACT_TYPE) {
+ if (normalizeArtifactType(props.artifact.type ?? '') === MERMAID_ARTIFACT_TYPE) {
return (
<MermaidArtifactTabs
artifact={props.artifact}
diff --git a/client/src/components/Artifacts/Artifacts.tsx b/client/src/components/Artifacts/Artifacts.tsx
index b0a46a0fec..3906c890f4 100644
--- a/client/src/components/Artifacts/Artifacts.tsx
+++ b/client/src/components/Artifacts/Artifacts.tsx
@@ -17,7 +17,12 @@ import {
import type { SandpackPreviewRef } from '@codesandbox/sandpack-react';
import type { CSSProperties } from 'react';
import type { ProcessedMermaidSvg } from '~/utils/diagram/export';
-import { TOOL_ARTIFACT_TYPES, isCodeOnlyArtifact, isPreviewOnlyArtifact } from '~/utils/artifacts';
+import {
+ TOOL_ARTIFACT_TYPES,
+ isCodeOnlyArtifact,
+ isPreviewOnlyArtifact,
+ normalizeArtifactType,
+} from '~/utils/artifacts';
import { copyWithinDocument, openUndockedWindow, prepareUndockedDocument } from './undockedWindow';
import { artifactsOpenedArtifactId, artifactsPaneFocusRequest, undockedArtifacts } from './state';
import { displayFilename } from '~/components/Chat/Messages/Content/Parts/attachmentTypes';
@@ -186,7 +191,8 @@ export default function Artifacts() {
mermaidExportState != null && mermaidExportState.artifactId === currentArtifact?.id
? mermaidExportState.data
: null;
- const isMermaidArtifact = currentArtifact?.type === TOOL_ARTIFACT_TYPES.MERMAID;
+ const isMermaidArtifact =
+ normalizeArtifactType(currentArtifact?.type ?? '') === TOOL_ARTIFACT_TYPES.MERMAID;
const closeArtifacts = useCallback(() => {
if (isMobile) {
diff --git a/client/src/hooks/Artifacts/useArtifactProps.ts b/client/src/hooks/Artifacts/useArtifactProps.ts
index 26271390a0..0ff841f1ff 100644
--- a/client/src/hooks/Artifacts/useArtifactProps.ts
+++ b/client/src/hooks/Artifacts/useArtifactProps.ts
@@ -7,6 +7,7 @@ import {
getProps,
getTemplate,
getArtifactFilename,
+ normalizeArtifactType,
getSvgFiles,
isSvgArtifactType,
languageForFilename,
@@ -22,8 +23,8 @@ export default function useArtifactProps({ artifact }: { artifact: Artifact }) {
const isDarkMode = resolvedMode === 'dark';
const [fileKey, files] = useMemo(() => {
- const key = getKey(artifact.type ?? '', artifact.language);
- const type = artifact.type ?? '';
+ const type = normalizeArtifactType(artifact.type ?? '');
+ const key = getKey(type, artifact.language);
if (key.includes('mermaid')) {
return ['diagram.mmd', getMermaidFiles(artifact.content ?? '', isDarkMode, highContrast)];
@@ -44,7 +45,7 @@ export default function useArtifactProps({ artifact }: { artifact: Artifact }) {
return ['content.md', getMarkdownFiles(wrapped, isDarkMode, highContrast)];
}
- if (type === 'text/markdown' || type === 'text/md' || type === 'text/plain') {
+ if (type === TOOL_ARTIFACT_TYPES.MARKDOWN || type === TOOL_ARTIFACT_TYPES.PLAIN_TEXT) {
return ['content.md', getMarkdownFiles(artifact.content ?? '', isDarkMode, highContrast)];
}
@@ -73,7 +74,7 @@ export default function useArtifactProps({ artifact }: { artifact: Artifact }) {
return [getArtifactFilename(type), getSvgFiles(artifact.content ?? '')];
}
- const fileKey = getArtifactFilename(artifact.type ?? '', artifact.language);
+ const fileKey = getArtifactFilename(type, artifact.language);
const files = removeNullishValues({
[fileKey]: artifact.content,
});
diff --git a/client/src/utils/__tests__/artifacts.test.ts b/client/src/utils/__tests__/artifacts.test.ts
index 11d2fedb6d..c7d7e0c25b 100644
--- a/client/src/utils/__tests__/artifacts.test.ts
+++ b/client/src/utils/__tests__/artifacts.test.ts
@@ -14,6 +14,7 @@ import {
isPreviewOnlyArtifact,
isSvgArtifactType,
languageForFilename,
+ normalizeArtifactType,
TOOL_ARTIFACT_TYPES,
} from '../artifacts';
@@ -1304,3 +1305,61 @@ describe('artifactRowKind', () => {
expect(artifactRowKind({})).toMatchObject({ rendersPreview: true });
});
});
+
+describe('normalizeArtifactType', () => {
+ const SVG = 'image/svg+xml';
+ /** Every spelling a model or an older prompt writes, with the bucket it renders in. */
+ const spellings: Array<[string, string]> = [
+ ['react', TOOL_ARTIFACT_TYPES.REACT],
+ ['application/vnd.ant.react', TOOL_ARTIFACT_TYPES.REACT],
+ ['html', TOOL_ARTIFACT_TYPES.HTML],
+ ['application/vnd.code-html', TOOL_ARTIFACT_TYPES.HTML],
+ ['markdown', TOOL_ARTIFACT_TYPES.MARKDOWN],
+ ['md', TOOL_ARTIFACT_TYPES.MARKDOWN],
+ ['text/md', TOOL_ARTIFACT_TYPES.MARKDOWN],
+ ['mermaid', TOOL_ARTIFACT_TYPES.MERMAID],
+ ['svg', SVG],
+ ['image/svg', SVG],
+ ];
+
+ it.each(spellings)('maps %s to %s', (spelling, canonical) => {
+ expect(normalizeArtifactType(spelling)).toBe(canonical);
+ });
+
+ it('ignores case and surrounding whitespace in an authored spelling', () => {
+ expect(normalizeArtifactType(' React ')).toBe(TOOL_ARTIFACT_TYPES.REACT);
+ expect(normalizeArtifactType('HTML')).toBe(TOOL_ARTIFACT_TYPES.HTML);
+ expect(normalizeArtifactType('APPLICATION/VND.REACT')).toBe(TOOL_ARTIFACT_TYPES.REACT);
+ expect(normalizeArtifactType(' text/html ')).toBe(TOOL_ARTIFACT_TYPES.HTML);
+ expect(getTemplate('Application/Vnd.React')).toBe('react-ts');
+ });
+
+ it('leaves canonical and unknown types as written', () => {
+ Object.values(TOOL_ARTIFACT_TYPES).forEach((type) => {
+ expect(normalizeArtifactType(type)).toBe(type);
+ });
+ expect(normalizeArtifactType('code')).toBe('code');
+ expect(normalizeArtifactType('application/x-unheard-of')).toBe('application/x-unheard-of');
+ expect(normalizeArtifactType('constructor')).toBe('constructor');
+ });
+
+ it.each(spellings)(
+ 'drives every lookup for %s the way its canonical type does',
+ (spelling, canonical) => {
+ expect(getTemplate(spelling)).toBe(getTemplate(canonical));
+ expect(getArtifactFilename(spelling)).toBe(getArtifactFilename(canonical));
+ expect(getDependencies(spelling)).toEqual(getDependencies(canonical));
+ expect(artifactRowKind({ type: spelling })).toEqual(artifactRowKind({ type: canonical }));
+ expect(isSvgArtifactType(spelling)).toBe(isSvgArtifactType(canonical));
+ },
+ );
+
+ it('sends a short react spelling down the React path', () => {
+ expect(getTemplate('react')).toBe('react-ts');
+ expect(getArtifactFilename('react')).toBe('App.tsx');
+ expect(getDependencies('react')).toEqual(getDependencies(TOOL_ARTIFACT_TYPES.REACT));
+ expect(artifactRowKind({ type: 'react' }).label).toEqual({
+ key: 'com_ui_artifact_format_react',
+ });
+ });
+});
diff --git a/client/src/utils/artifacts.ts b/client/src/utils/artifacts.ts
index 8c02ee4b74..5d62aa3054 100644
--- a/client/src/utils/artifacts.ts
+++ b/client/src/utils/artifacts.ts
@@ -21,9 +21,7 @@ import { getCodeBlockFilename } from './downloadFile';
const artifactFilename = {
'application/vnd.react': 'App.tsx',
- 'application/vnd.ant.react': 'App.tsx',
'text/html': 'index.html',
- 'application/vnd.code-html': 'index.html',
/* Office preview buckets — the backend produces a complete sanitized
* `index.html` document (head + body) and ships it via `attachment.text`.
* The Sandpack `static` template loads it as-is. See
@@ -35,7 +33,6 @@ const artifactFilename = {
* template always loads `index.html`, so `getSvgFiles` ships a
* companion HTML shell; the editor/download file stays `index.svg`. */
'image/svg+xml': 'index.svg',
- 'image/svg': 'index.svg',
// mermaid and markdown types are handled separately in useArtifactProps.ts
default: 'index.html',
// 'css': 'css',
@@ -50,15 +47,12 @@ const artifactTemplate: Record<
| 'application/vnd.mermaid'
| 'application/vnd.code'
| 'text/markdown'
- | 'text/md'
| 'text/plain',
SandpackPredefinedTemplate | undefined
> = {
'text/html': 'static',
'application/vnd.react': 'react-ts',
- 'application/vnd.ant.react': 'react-ts',
'application/vnd.mermaid': 'react-ts',
- 'application/vnd.code-html': 'static',
/* CODE bucket reuses the static markdown pipeline — `useArtifactProps`
* pre-wraps the content in a fenced block and hands it to
* `getMarkdownFiles`, so the rendered HTML uses the same `marked`
@@ -66,7 +60,6 @@ const artifactTemplate: Record<
* the panel doesn't pay the sandpack-React boot cost for source files. */
'application/vnd.code': 'static',
'text/markdown': 'static',
- 'text/md': 'static',
'text/plain': 'static',
/* Office preview buckets ride the same static pipeline — the backend
* already sanitized the HTML, so we just hand it to Sandpack. */
@@ -74,7 +67,6 @@ const artifactTemplate: Record<
'application/vnd.librechat.spreadsheet-preview': 'static',
'application/vnd.librechat.presentation-preview': 'static',
'image/svg+xml': 'static',
- 'image/svg': 'static',
default: 'static',
// 'css': 'css',
// 'javascript': 'js',
@@ -88,7 +80,7 @@ export function getKey(type: string, language?: string): string {
}
export function getArtifactFilename(type: string, language?: string): string {
- const key = getKey(type, language);
+ const key = getKey(normalizeArtifactType(type), language);
return artifactFilename[key] ?? artifactFilename.default;
}
@@ -138,8 +130,9 @@ export function getArtifactDownloadFilename(
fileKey: string,
content = artifact.content,
): string {
- const isCode = artifact.type === TOOL_ARTIFACT_TYPES.CODE;
- const isMarkdown = artifact.type === TOOL_ARTIFACT_TYPES.MARKDOWN || artifact.type === 'text/md';
+ const type = normalizeArtifactType(artifact.type ?? '');
+ const isCode = type === TOOL_ARTIFACT_TYPES.CODE;
+ const isMarkdown = type === TOOL_ARTIFACT_TYPES.MARKDOWN;
let fallback = fileKey;
if (isCode) {
fallback = getCodeBlockFilename(
@@ -192,13 +185,12 @@ export function getArtifactDownloadFilename(
}
export function getTemplate(type: string, language?: string): SandpackPredefinedTemplate {
- const key = getKey(type, language);
+ const key = getKey(normalizeArtifactType(type), language);
return artifactTemplate[key] ?? (artifactTemplate.default as SandpackPredefinedTemplate);
}
-/** `image/svg` is the alias some callers emit for `image/svg+xml`. */
export function isSvgArtifactType(type: string): boolean {
- return type === 'image/svg+xml' || type === 'image/svg';
+ return normalizeArtifactType(type) === 'image/svg+xml';
}
/**
@@ -292,21 +284,17 @@ const dependenciesMap: Record<
| 'application/vnd.mermaid'
| 'application/vnd.code'
| 'text/markdown'
- | 'text/md'
| 'text/plain',
Record<string, string>
> = {
'application/vnd.mermaid': mermaidDependencies,
'application/vnd.react': standardDependencies,
- 'application/vnd.ant.react': standardDependencies,
'text/html': standardDependencies,
- 'application/vnd.code-html': standardDependencies,
/* CODE renders in the static markdown template; no React or other
* runtime deps. Empty map skips the sandpack `package.json` install
* step entirely (same as MARKDOWN/PLAIN_TEXT). */
'application/vnd.code': {},
'text/markdown': {},
- 'text/md': {},
'text/plain': {},
/* Office preview HTML is fully self-contained (CSS-only sheet tabs, no
* JS), so no Sandpack-side packages are needed. */
@@ -315,12 +303,11 @@ const dependenciesMap: Record<
'application/vnd.librechat.presentation-preview': {},
/* SVG preview is a static HTML shell + the source file; no npm deps. */
'image/svg+xml': {},
- 'image/svg': {},
default: standardDependencies,
};
export function getDependencies(type: string): Record<string, string> {
- return dependenciesMap[type] ?? standardDependencies;
+ return dependenciesMap[normalizeArtifactType(type)] ?? standardDependencies;
}
export function getProps(type: string): Partial<SandpackProviderProps> {
@@ -493,7 +480,7 @@ export function isPreviewOnlyArtifact(type: string | null | undefined): boolean
if (type == null) {
return false;
}
- return PREVIEW_ONLY_ARTIFACT_TYPES.has(type as ToolArtifactType);
+ return PREVIEW_ONLY_ARTIFACT_TYPES.has(normalizeArtifactType(type) as ToolArtifactType);
}
/**
@@ -501,7 +488,7 @@ export function isPreviewOnlyArtifact(type: string | null | undefined): boolean
* They should stay click-to-open and, once opened, expose only the code view.
*/
export function isCodeOnlyArtifact(type: string | null | undefined): boolean {
- return type === TOOL_ARTIFACT_TYPES.CODE;
+ return type != null && normalizeArtifactType(type) === TOOL_ARTIFACT_TYPES.CODE;
}
/**
@@ -557,17 +544,36 @@ type ArtifactFormatKey = Extract<
>;
/**
- * Legacy and model-authored spellings of the canonical buckets. The
- * markdown `:::artifact` path takes its type straight from the authored
- * attribute, so these arrive alongside the values in
- * `TOOL_ARTIFACT_TYPES`; `artifactTemplate` carries the same aliases.
+ * Every spelling an artifact type arrives in, mapped to its canonical bucket: the
+ * legacy MIME spellings older prompts taught, and the short names a model writes
+ * straight into `:::artifact{type="..."}`. The supported short names are the
+ * rendered buckets a model can author (react, html, markdown, md, mermaid, svg);
+ * anything else passes through unchanged and renders as a generic preview.
*/
const ARTIFACT_TYPE_ALIASES: Record<string, string> = {
'application/vnd.ant.react': TOOL_ARTIFACT_TYPES.REACT,
'application/vnd.code-html': TOOL_ARTIFACT_TYPES.HTML,
'text/md': TOOL_ARTIFACT_TYPES.MARKDOWN,
+ 'image/svg': 'image/svg+xml',
+ react: TOOL_ARTIFACT_TYPES.REACT,
+ html: TOOL_ARTIFACT_TYPES.HTML,
+ markdown: TOOL_ARTIFACT_TYPES.MARKDOWN,
+ md: TOOL_ARTIFACT_TYPES.MARKDOWN,
+ mermaid: TOOL_ARTIFACT_TYPES.MERMAID,
+ svg: 'image/svg+xml',
};
+const CANONICAL_ARTIFACT_TYPES: ReadonlySet<string> = new Set<string>([
+ ...Object.values(TOOL_ARTIFACT_TYPES),
+ 'image/svg+xml',
+]);
+
+/** The canonical type for any spelling an artifact arrives in; every type lookup goes through it. */
+export function normalizeArtifactType(type: string): string {
+ const key = type.trim().toLowerCase();
+ return lookupOwn(ARTIFACT_TYPE_ALIASES, key) ?? (CANONICAL_ARTIFACT_TYPES.has(key) ? key : type);
+}
+
const ARTIFACT_ROW_KINDS: Record<string, ArtifactRowKind> = {
[TOOL_ARTIFACT_TYPES.HTML]: {
lang: 'html',
@@ -637,7 +643,7 @@ export function artifactRowKind({
language?: string | null;
title?: string | null;
}): ArtifactRowKind {
- const resolvedType = (type != null && lookupOwn(ARTIFACT_TYPE_ALIASES, type)) || type;
+ const resolvedType = type != null ? normalizeArtifactType(type) : type;
if (resolvedType != null) {
const known = lookupOwn(ARTIFACT_ROW_KINDS, resolvedType);
if (known != null) {
diff --git a/e2e/specs/mock/scenarios/short-artifact-type.spec.ts b/e2e/specs/mock/scenarios/short-artifact-type.spec.ts
new file mode 100644
index 0000000000..0320bdbd9a
--- /dev/null
+++ b/e2e/specs/mock/scenarios/short-artifact-type.spec.ts
@@ -0,0 +1,74 @@
+import { randomUUID } from 'node:crypto';
+import { expect, test } from '@playwright/test';
+import { getE2EUser } from '../../../setup/user';
+import {
+ deleteConversations,
+ deleteMessagesByConversation,
+ seedConversations,
+ seedMessages,
+} from '../db';
+import type { SeedMessage } from '../db';
+import { messagesView } from '../helpers';
+
+/**
+ * A model may write the short `type="react"` instead of `application/vnd.react`. The type is
+ * normalized once, so the row, the Sandpack template, the dependency set and the panel file all
+ * take the React path. The component computes its text, so a static template, which would show
+ * the source instead of running it, cannot produce the asserted string.
+ */
+test.describe.configure({ timeout: 180_000 });
+
+const ROOT_PARENT = '00000000-0000-0000-0000-000000000000';
+const REACT_ARTIFACT = [
+ ':::artifact{identifier="e2e-short-react" type="react" title="Sum"}',
+ 'export default function App() {',
+ ' const total = [2, 3].reduce((sum, value) => sum + value, 0);',
+ ' return <h1>Sum is {total}</h1>;',
+ '}',
+ ':::',
+].join('\n');
+
+test('a short react type renders as a React artifact @scenario:short-react-type-renders-react-artifact', async ({
+ page,
+}) => {
+ const conversationId = randomUUID();
+ const userEmail = getE2EUser().email;
+ const message: SeedMessage = {
+ messageId: randomUUID(),
+ parentMessageId: ROOT_PARENT,
+ text: REACT_ARTIFACT,
+ isCreatedByUser: false,
+ sender: 'Assistant',
+ model: 'mock-model-a',
+ };
+
+ try {
+ await seedConversations(userEmail, [
+ { conversationId, title: 'Short artifact type', updatedAt: new Date() },
+ ]);
+ await seedMessages(userEmail, conversationId, [message]);
+ await page.goto(`/c/${conversationId}`, { timeout: 15000 });
+
+ const row = messagesView(page).locator('[data-artifact-trigger]').filter({ hasText: 'Sum' });
+ await expect(row).toHaveCount(1);
+ await expect(row.getByText('React', { exact: true })).toBeVisible();
+ await expect(row).toHaveAccessibleName(/Sum.*React.*Opens as a rendered preview/);
+
+ await row.click();
+ const panel = page.locator('#artifact-viewer');
+ await expect(panel).toBeVisible();
+ /** The React template boots Sandpack's hosted runtime, which can time out on a slow network;
+ * Sandpack then offers its own retry, so a timed-out boot is retried rather than failed. */
+ const preview = panel.locator('iframe').contentFrame();
+ await expect(async () => {
+ const retry = preview.getByRole('button', { name: 'Try again' });
+ if (await retry.isVisible()) {
+ await retry.click();
+ }
+ await expect(preview.getByText('Sum is 5', { exact: true })).toBeVisible({ timeout: 30000 });
+ }).toPass({ timeout: 100000 });
+ } finally {
+ await deleteMessagesByConversation([conversationId]);
+ await deleteConversations([conversationId]);
+ }
+});