Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.2 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"use client";

// After an assistant message finishes, one browser card per fenced html block is appended, skipping
// messages a render_html tool already rendered and full documents the in-place collapse handles;
// React jsx and tsx blocks get a card after the HTML ones, code staying in place.

import {
  memoOnArray,
  partsHaveRenderableRenderHtmlTool,
} from "@/components/assistant-ui/message-derived";
import { ArtifactCard, useChatRuntimeStore } from "@/features/chat";
import {
  extractHtmlFences,
  extractReactFences,
} from "@/features/chat/artifacts/html-fences";
import { useAuiState } from "@assistant-ui/react";
import { type FC, useMemo } from "react";

// Cannot occur in chat text; keeps a fence from being stitched across non-text parts.
const PART_SEPARATOR = "\u0000";

const visibleTextBlob = memoOnArray(
  (content: ReadonlyArray<{ type: string; text?: unknown }>) =>
    content
      .filter((part) => part.type === "text" && "text" in part)
      .map((part) => (part as { text: string }).text)
      .join(PART_SEPARATOR),
);

export const MessageHtmlArtifacts: FC = () => {
  const isRunning = useAuiState(
    ({ message }) => message.status?.type === "running",
  );
  const hasRenderHtmlTool = useAuiState(({ message }) =>
    partsHaveRenderableRenderHtmlTool(message.parts),
  );
  const textBlob = useAuiState(({ message }) =>
    visibleTextBlob(message.content),
  );
  const collapseHtmlArtifacts = useChatRuntimeStore(
    (state) => state.collapseHtmlArtifacts,
  );
  const loadedIsDiffusion = useChatRuntimeStore(
    (state) => state.loadedIsDiffusion,
  );
  // Full documents are already shown by the in-place collapse, except for diffusion (code stays
  // inline there); this picks the path, not whether fenced HTML renders.
  const collapsesFullDocs = collapseHtmlArtifacts && !loadedIsDiffusion;

  const fences = useMemo(() => {
    if (isRunning || hasRenderHtmlTool) {
      return [];
    }
    return textBlob
      .split(PART_SEPARATOR)
      .flatMap((part) => extractHtmlFences(part))
      .filter(
        (fence) =>
          !(fence.isFullDocument && fence.isPlainFence && collapsesFullDocs),
      );
  }, [isRunning, hasRenderHtmlTool, textBlob, collapsesFullDocs]);

  const reactFences = useMemo(
    () =>
      isRunning
        ? []
        : textBlob.split(PART_SEPARATOR).flatMap((part) => extractReactFences(part)),
    [isRunning, textBlob],
  );

  if (fences.length === 0 && reactFences.length === 0) {
    return null;
  }

  return (
    <div className="mt-2 flex flex-col gap-2">
      {fences.map((fence, i) => (
        <ArtifactCard
          key={i}
          code={fence.source}
          title={i === 0 ? "HTML preview" : `HTML preview ${i + 1}`}
          source="fence"
        />
      ))}
      {reactFences.map((fence, i) => (
        <ArtifactCard
          key={`react-${i}`}
          kind="react"
          lang={fence.lang}
          code={fence.source}
          title={i === 0 ? "React preview" : `React preview ${i + 1}`}
          source="fence"
        />
      ))}
    </div>
  );
};