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.8 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";

import { ArtifactCard } from "@/features/chat";
import { appSessionStartedAt } from "@/lib/app-session-start";
import {
  type ToolCallMessagePartComponent,
  useAuiState,
  useToolArgsStatus,
} from "@assistant-ui/react";
import { BrowserIcon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { memo } from "react";
import { isToolCallRunning } from "./tool-arg-text";

type RenderHtmlArgs = Record<string, unknown> & {
  code?: string;
  title?: string;
};

const RenderHtmlToolUIImpl: ToolCallMessagePartComponent = ({
  args,
  result,
  status,
  toolCallId,
}) => {
  const { propStatus } = useToolArgsStatus<RenderHtmlArgs>();
  const parsedArgs = (args as RenderHtmlArgs) ?? {};
  const code = typeof parsedArgs.code === "string" ? parsedArgs.code : "";
  const hasCode = code.trim().length > 0;
  const title =
    typeof parsedArgs.title === "string" ? parsedArgs.title : "HTML preview";
  const isRunning = isToolCallRunning(status);
  const codeIsStreaming = propStatus.code === "streaming";

  // Success results start with "Rendered HTML canvas"; errors with "Error:".
  const errorText =
    status?.type === "complete" &&
    typeof result === "string" &&
    result.startsWith("Error:")
      ? result
      : null;
  const isMessageRunning = useAuiState(
    ({ message }) => message.status?.type === "running",
  );
  const messageCreatedAtMs = useAuiState(({ message }) =>
    message.createdAt instanceof Date ? message.createdAt.getTime() : null,
  );
  const isThreadRunning = useAuiState(({ thread }) => thread.isRunning);
  const isLiveGeneratingArtifact =
    isThreadRunning && isMessageRunning && (isRunning || codeIsStreaming);
  const isStaleGeneratingArtifact =
    !(isThreadRunning && isMessageRunning) && (isRunning || codeIsStreaming);
  const messageCreatedThisSession =
    messageCreatedAtMs != null &&
    messageCreatedAtMs >= appSessionStartedAt() - 1000;
  const shouldAutoOpenArtifact =
    (isLiveGeneratingArtifact && (hasCode || isRunning || codeIsStreaming)) ||
    (hasCode && messageCreatedThisSession);

  if (hasCode || (isLiveGeneratingArtifact && !errorText)) {
    return (
      <ArtifactCard
        code={code}
        title={title}
        source="tool"
        sourceToolCallId={toolCallId}
        autoOpen={!errorText && shouldAutoOpenArtifact}
        isStreaming={!errorText && isLiveGeneratingArtifact}
      />
    );
  }

  return (
    <div className="relative my-2 flex min-h-[calc(52px*var(--ui-space-scale,1))] w-full max-w-md items-center overflow-hidden rounded-lg border border-border/70 bg-muted/15 px-3 py-2 text-left dark:bg-muted/10">
      <div className="relative z-10 flex min-w-0 flex-1 items-center gap-2.5">
        <HugeiconsIcon
          icon={BrowserIcon}
          strokeWidth={1.75}
          className="size-5 shrink-0 text-muted-foreground"
        />
        <span className="grid min-w-0 flex-1 gap-1">
          <span className="truncate text-sm font-medium leading-tight text-foreground">
            {errorText
              ? "HTML preview error"
              : isStaleGeneratingArtifact
                ? "HTML preview interrupted"
                : "HTML preview unavailable"}
          </span>
          <span className="truncate text-ui-11 leading-none text-muted-foreground">
            {errorText ??
              (isStaleGeneratingArtifact
                ? "Refresh stopped this preview"
                : "HTML preview")}
          </span>
        </span>
      </div>
    </div>
  );
};

export const RenderHtmlToolUI = memo(
  RenderHtmlToolUIImpl,
) as unknown as ToolCallMessagePartComponent;
RenderHtmlToolUI.displayName = "RenderHtmlToolUI";