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.

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

import {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle,
} from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
// eslint-disable-next-line no-restricted-imports -- the settings barrel imports this feature back
import { useSettingsDialogStore } from "@/features/settings/stores/settings-dialog-store";
import { useLocale, useT } from "@/i18n";
import { apiUrl } from "@/lib/api-base";
import { cn } from "@/lib/utils";
import {
  Alert02Icon,
  Cancel01Icon,
  CancelCircleIcon,
  UnavailableIcon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import {
  type RefObject,
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
import {
  CANVAS_CONSOLE_ENTRIES_TRACKED,
  type CanvasConsoleEntry,
  type CanvasConsoleState,
  appendCanvasEntry,
  buildCanvasFixPrompt,
  canvasErrors,
  canvasStack,
  emptyCanvasConsole,
  parseCanvasReport,
} from "./canvas-console";
import { hashArtifactCode } from "./types";

const RTL_LOCALES: ReadonlySet<string> = new Set(["ar", "he", "fa"]);
const HTML_FRAME_DEFAULT_HEIGHT = 400;
const HTML_FRAME_MAX_HEIGHT = 900;
const BLOCKED_HOSTS_SHOWN = 3;

// The wrapper spans the canvas, so only the notice itself takes clicks.
const NOTICE_WRAP = "pointer-events-none absolute inset-x-0 top-0 flex justify-center p-2";
const NOTICE =
  "pointer-events-auto max-w-[calc(460px*var(--ui-space-scale,1))] gap-y-0.5 rounded-lg border-transparent bg-popover py-3 ps-4.5 text-ui-13 leading-normal text-popover-foreground shadow-[0_2px_8px_-2px_rgba(0,0,0,0.16)] has-data-[slot=alert-action]:pe-12 dark:bg-card dark:shadow-[0_2px_8px_-2px_var(--background)]";
const NOTICE_BODY =
  "text-ui-13 leading-[1.4] text-muted-foreground [&_p:not(:last-child)]:mb-2.5";
const NOTICE_BUTTON = "h-6 rounded-full px-2 text-ui-12 font-medium";
const NOTICE_PRIMARY = `${NOTICE_BUTTON} bg-action text-background hover:bg-action hover:opacity-90`;
const NOTICE_SECONDARY = `${NOTICE_BUTTON} border-transparent bg-[color-mix(in_oklab,var(--foreground)_calc(8%*var(--contrast-wash-gain,1)),transparent)] text-foreground hover:bg-[color-mix(in_oklab,var(--foreground)_calc(12%*var(--contrast-wash-gain,1)),transparent)] dark:bg-[color-mix(in_oklab,var(--foreground)_calc(8%*var(--contrast-wash-gain,1)),transparent)] dark:hover:bg-[color-mix(in_oklab,var(--foreground)_calc(12%*var(--contrast-wash-gain,1)),transparent)]`;
const NOTICE_CLOSE =
  "size-5 rounded-full bg-popover text-popover-foreground hover:bg-muted hover:text-popover-foreground dark:bg-card dark:hover:bg-muted";
// Per URL, not per host; still far above what a real page trips.
const BLOCKED_URIS_TRACKED = 100;
// Bounds both the stored string and the host derived from it.
const BLOCKED_URI_MAX_CHARS = 2048;

// The only directives the network CSP leaves at 'none'. Kept in step by
// test_the_grant_widens_everything_but_the_locked_directives.
const GRANT_CANNOT_FIX = new Set(["object-src", "base-uri", "form-action"]);

// A hostless blockedURI is a bare scheme, and the permissive policy widens every one but this:
// its worker-src is `http: https: blob:` with no data:, so a data: Worker stays blocked
// after the grant. Kept in step by test_the_permissive_policy_widens_every_hostless_scheme_but_one.
const GRANT_CANNOT_FIX_SCHEME: Record<string, string> = { "worker-src": "data" };

type BlockedState = { code: string; uris: string[]; hosts: string[] };

const NOTHING_BLOCKED: BlockedState = { code: "", uris: [], hosts: [] };
const NO_OUTPUT: CanvasConsoleState = emptyCanvasConsole("");

// Reports from before a swap belong to the old canvas. The cap is checked before the duplicate scan.
function appendBlocked(
  current: BlockedState,
  code: string,
  uri: string,
  host: string,
): BlockedState {
  const mine = current.code === code ? current : { code, uris: [], hosts: [] };
  if (mine.uris.length >= BLOCKED_URIS_TRACKED || mine.uris.includes(uri)) {
    return mine === current ? current : mine;
  }
  return {
    code,
    uris: [...mine.uris, uri],
    hosts: mine.hosts.includes(host) ? mine.hosts : [...mine.hosts, host],
  };
}

// Bare-token violations ("eval", "blob") are fixable too; label them with the token.
const BLOCKED_KEYWORD = /^[a-z-]+$/;

function blockedHost(uri: string): string | null {
  if (BLOCKED_KEYWORD.test(uri)) return uri;
  try {
    return new URL(uri).host || null;
  } catch {
    return null;
  }
}

export type ArtifactViewMode = "preview" | "source";
export const ARTIFACT_VIEW_MODES: readonly ArtifactViewMode[] = [
  "preview",
  "source",
];

export function isArtifactViewMode(value: string): value is ArtifactViewMode {
  return (ARTIFACT_VIEW_MODES as readonly string[]).includes(value);
}

export function buildArtifactSrcDoc(code: string): string {
  const resizeScript = `<script>(()=>{const post=()=>parent.postMessage({chatArtifactHeight:document.documentElement.scrollHeight},"*");new ResizeObserver(post).observe(document.documentElement);window.addEventListener("load",post);post();})();</script>`;
  return `${code}\n${resizeScript}`;
}

// No allow-downloads: downloads go through Unsloth's controls outside the sandbox.
export function ArtifactHtmlFrame({
  code,
  title = "HTML preview",
  className,
  fill = false,
  actionFocusTargetRef,
  consoleOpen = false,
  reloadNonce = 0,
  onConsoleOpenChange,
  onOutputCountChange,
  onFixWithModel,
  onFrameReady,
  kind = "html",
}: {
  code: string;
  title?: string;
  className?: string;
  fill?: boolean;
  actionFocusTargetRef?: RefObject<HTMLElement | null>;
  consoleOpen?: boolean;
  reloadNonce?: number;
  onConsoleOpenChange?: (open: boolean) => void;
  onOutputCountChange?: (counts: { errors: number; total: number }) => void;
  // Only surfaces that can route the text to a composer pass this; without it there is no Fix button.
  onFixWithModel?: (prompt: string) => void;
  // A React preview runs compiled code, so its runtime line numbers aren't shown.
  kind?: "html" | "react";
  onFrameReady?: (frame: HTMLIFrameElement) => (() => void) | undefined;
}) {
  const t = useT();
  const locale = useLocale();
  const iframeRef = useRef<HTMLIFrameElement>(null);
  // Global off-by-default switch; the standing half of the gate beside the per-canvas grant.
  const networkAccessEnabled = useChatRuntimeStore(
    (state) => state.allowArtifactNetworkAccess,
  );
  const [height, setHeight] = useState(HTML_FRAME_DEFAULT_HEIGHT);
  // Carries the code it was reported for, so a swapped canvas cannot inherit the old banner.
  const [blocked, setBlocked] = useState<BlockedState>({
    code,
    uris: [],
    hosts: [],
  });
  const blockedForCanvas = blocked.code === code ? blocked : NOTHING_BLOCKED;
  // Granted only by the banner button for the code on screen; nothing the canvas sends may set it.
  // Compared during render so new code never reuses allow_network=1.
  const [grantedCode, setGrantedCode] = useState<string | null>(null);
  const grantedForCanvas = grantedCode === code;
  const networkAllowed = networkAccessEnabled || grantedForCanvas;
  const [dismissedCode, setDismissedCode] = useState<string | null>(null);
  const dismissedForCanvas = dismissedCode === code;
  const [output, setOutput] = useState<CanvasConsoleState>(() =>
    emptyCanvasConsole(code),
  );
  const outputForCanvas = output.code === code ? output : NO_OUTPUT;
  const errors = useMemo(() => canvasErrors(outputForCanvas), [outputForCanvas]);
  const [errorsDismissedCode, setErrorsDismissedCode] = useState<
    string | null
  >(null);
  const pendingEntries = useRef<CanvasConsoleEntry[]>([]);
  const pendingDropped = useRef(false);
  const flushHandle = useRef<number | null>(null);
  const queueEntry = useCallback(
    (entry: CanvasConsoleEntry) => {
      // The canvas can post here directly, past the shell's cap, and a hidden tab never flushes.
      pendingEntries.current.push(entry);
      if (pendingEntries.current.length > CANVAS_CONSOLE_ENTRIES_TRACKED) {
        const oldestLog = pendingEntries.current.findIndex(
          (queued) => queued.kind === "console",
        );
        pendingEntries.current.splice(oldestLog < 0 ? 0 : oldestLog, 1);
        pendingDropped.current = true;
      }
      if (flushHandle.current !== null) return;
      flushHandle.current = window.requestAnimationFrame(() => {
        flushHandle.current = null;
        const batch = pendingEntries.current;
        const dropped = pendingDropped.current;
        pendingEntries.current = [];
        pendingDropped.current = false;
        setOutput((current) => {
          const next = batch.reduce(
            (state, queued) => appendCanvasEntry(state, code, queued),
            current,
          );
          return dropped ? { ...next, capped: true } : next;
        });
      });
    },
    [code],
  );
  useEffect(
    () => () => {
      if (flushHandle.current !== null) {
        window.cancelAnimationFrame(flushHandle.current);
      }
    },
    [],
  );
  useEffect(() => {
    onOutputCountChange?.({
      errors: errors.length,
      total: outputForCanvas.entries.length,
    });
  }, [errors.length, outputForCanvas.entries.length, onOutputCountChange]);
  // Batched reports passed the stamp check under the old load, so a new load drops the queue.
  const dropPendingEntries = useCallback(() => {
    if (flushHandle.current !== null) {
      window.cancelAnimationFrame(flushHandle.current);
      flushHandle.current = null;
    }
    pendingEntries.current = [];
    pendingDropped.current = false;
  }, []);
  const artifactHtml = useMemo(() => buildArtifactSrcDoc(code), [code]);
  // Everything that reruns the document (code, reload counter, network policy) must be in this key.
  const codeVersion = useMemo(() => hashArtifactCode(code), [code]);
  const loadVersion = `${codeVersion}${reloadNonce > 0 ? `.${reloadNonce}` : ""}${
    networkAllowed ? ".net" : ""
  }`;
  const loadedOnce = useRef(false);
  useEffect(() => {
    if (!loadedOnce.current) {
      loadedOnce.current = true;
      return;
    }
    dropPendingEntries();
    setOutput(emptyCanvasConsole(code));
    setErrorsDismissedCode(null);
  }, [loadVersion, code, dropPendingEntries]);
  const src = useMemo(() => {
    const query = new URLSearchParams({ v: loadVersion });
    // Never put the auth token in the URL: in-frame code can read location.href.
    if (networkAllowed) {
      query.set("allow_network", "1");
    }
    return apiUrl(`/api/inference/artifact-preview-frame?${query.toString()}`);
  }, [networkAllowed, loadVersion]);
  useEffect(() => {
    if (iframeRef.current) return onFrameReady?.(iframeRef.current);
  }, [onFrameReady, src]);
  // Feed only parent-initiated loads, so a self-navigated frame can't self-upgrade.
  const pendingPostRef = useRef(false);
  useEffect(() => {
    pendingPostRef.current = true;
  }, [src]);
  const postArtifactHtml = useCallback(() => {
    if (!pendingPostRef.current) return;
    pendingPostRef.current = false;
    // Sandboxed frame has an opaque origin, so a wildcard target is required.
    iframeRef.current?.contentWindow?.postMessage(
      { type: "unsloth:artifact-html", html: artifactHtml },
      "*",
    );
  }, [artifactHtml]);

  useEffect(() => {
    const handler = (event: MessageEvent) => {
      if (event.source !== iframeRef.current?.contentWindow) return;
      if (event.origin !== "null") return;
      if (event.data?.type === "unsloth:artifact-blocked") {
        // event.source survives the swap, so drop reports from the outgoing canvas by version stamp.
        if (event.data.v !== loadVersion) return;
        const uri = event.data.blockedURI;
        // The canvas can post these directly, so cap size too, not just entry count.
        if (typeof uri !== "string" || uri.length > BLOCKED_URI_MAX_CHARS) {
          return;
        }
        // The grant cannot fix these; prompting would widen policy and hide the banner for nothing.
        if (GRANT_CANNOT_FIX.has(event.data.effectiveDirective)) return;
        // The grant widens worker-src to blob: but not data:, so a data: Worker stays blocked.
        if (GRANT_CANNOT_FIX_SCHEME[event.data.effectiveDirective] === uri) {
          return;
        }
        const host = blockedHost(uri);
        if (!host) return;
        setBlocked((current) => appendBlocked(current, code, uri, host));
        return;
      }
      if (
        event.data?.type === "unsloth:artifact-error" ||
        event.data?.type === "unsloth:artifact-console"
      ) {
        if (event.data.v !== loadVersion) return;
        const entry = parseCanvasReport(event.data);
        if (!entry) return;
        queueEntry(entry);
        return;
      }
      if (typeof event.data?.chatArtifactHeight !== "number") return;
      setHeight(
        Math.min(
          Math.max(event.data.chatArtifactHeight, 160),
          HTML_FRAME_MAX_HEIGHT,
        ),
      );
    };
    window.addEventListener("message", handler);
    return () => window.removeEventListener("message", handler);
  }, [postArtifactHtml, code, loadVersion, queueEntry]);

  const showBlockedBanner =
    !networkAllowed && !dismissedForCanvas && blockedForCanvas.uris.length > 0;
  const shownHosts = blockedForCanvas.hosts
    .slice(0, BLOCKED_HOSTS_SHOWN)
    .join(", ");
  const blockedFrom =
    blockedForCanvas.hosts.length > BLOCKED_HOSTS_SHOWN
      ? `${shownHosts}…`
      : shownHosts;
  const focusAfterAction = () => {
    (actionFocusTargetRef?.current ?? iframeRef.current)?.focus({
      preventScroll: true,
    });
  };
  const showErrorBanner = errorsDismissedCode !== code && errors.length > 0;
  const firstError = errors[0];
  const errorTitle =
    errors.length === 1
      ? t("settings.chat.artifacts.errorTitle")
      : t("settings.chat.artifacts.errorTitlePlural", {
          count: errors.length,
        });
  const locationLabel = (entry: CanvasConsoleEntry) => {
    if (entry.line <= 0) return "";
    // A React preview's runtime positions point into compiled code nobody sees, and differ by
    // browser; compile errors carry their source position in the message instead.
    if (kind === "react") return "";
    return entry.column > 0
      ? t("settings.chat.artifacts.errorLocation", {
          line: entry.line,
          column: entry.column,
        })
      : t("settings.chat.artifacts.errorLine", { line: entry.line });
  };

  return (
    <div
      className={cn("relative", fill ? "h-full" : undefined)}
    >
      <iframe
        ref={iframeRef}
        src={src}
        sandbox="allow-scripts"
        referrerPolicy="no-referrer"
        onLoad={postArtifactHtml}
        className={cn(
          "block w-full border-0 bg-background outline-none focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
          className,
        )}
        style={{ height: fill ? "100%" : height }}
        title={title}
      />
      {showBlockedBanner ? (
        <div className={NOTICE_WRAP}>
          <Alert
            role="group"
            dir={RTL_LOCALES.has(locale) ? "rtl" : "ltr"}
            aria-label={t("settings.chat.artifacts.blockedTitle")}
            className={NOTICE}
          >
            <HugeiconsIcon icon={Alert02Icon} strokeWidth={2} className="size-4" />
            <AlertAction className="top-2 end-2">
              <Button
                size="icon-xs"
                variant="ghost"
                className={NOTICE_CLOSE}
                aria-label={t("settings.chat.artifacts.blockedDismiss")}
                onClick={() => {
                  focusAfterAction();
                  setDismissedCode(code);
                }}
              >
                <HugeiconsIcon icon={Cancel01Icon} strokeWidth={2.25} className="size-3" />
              </Button>
            </AlertAction>
            <AlertTitle role="alert">
              {t("settings.chat.artifacts.blockedTitle")}
              <span className="sr-only">
                {" "}
                {t("settings.chat.artifacts.blockedHint", {
                  setting: t("settings.chat.artifacts.allowNetworkAccess"),
                })}
              </span>
            </AlertTitle>
            <AlertDescription className={NOTICE_BODY}>
              <p>
                {t(
                  blockedForCanvas.uris.length === 1
                    ? "settings.chat.artifacts.blockedBanner"
                    : "settings.chat.artifacts.blockedBannerPlural",
                  { count: blockedForCanvas.uris.length, hosts: blockedFrom },
                )}{" "}
                {t("settings.chat.artifacts.blockedHint", {
                  setting: t("settings.chat.artifacts.allowNetworkAccess"),
                })}
              </p>
              <div className="flex flex-wrap gap-2">
                <Button
                  size="xs"
                  className={NOTICE_PRIMARY}
                  onClick={() => {
                    focusAfterAction();
                    setGrantedCode(code);
                  }}
                >
                  {t("settings.chat.artifacts.blockedBannerAction")}
                </Button>
                <Button
                  size="xs"
                  variant="outline"
                  className={NOTICE_SECONDARY}
                  onClick={() => {
                    useSettingsDialogStore.getState().openDialog("browser", {
                      scrollTarget: "browser-html-network",
                      focusFallback:
                        actionFocusTargetRef?.current ?? iframeRef.current,
                    });
                  }}
                >
                  {t("settings.chat.artifacts.blockedSettingsAction")}
                </Button>
              </div>
            </AlertDescription>
          </Alert>
        </div>
      ) : null}
      {showErrorBanner && firstError && !showBlockedBanner ? (
        <div className={NOTICE_WRAP}>
          <Alert
            role="group"
            dir={RTL_LOCALES.has(locale) ? "rtl" : "ltr"}
            aria-label={errorTitle}
            className={NOTICE}
          >
            <HugeiconsIcon icon={CancelCircleIcon} strokeWidth={2} className="size-4" />
            <AlertAction className="top-2 end-2">
              <Button
                size="icon-xs"
                variant="ghost"
                className={NOTICE_CLOSE}
                aria-label={t("settings.chat.artifacts.blockedDismiss")}
                onClick={() => {
                  focusAfterAction();
                  setErrorsDismissedCode(code);
                }}
              >
                <HugeiconsIcon icon={Cancel01Icon} strokeWidth={2.25} className="size-3" />
              </Button>
            </AlertAction>
            <AlertTitle role="alert">{errorTitle}</AlertTitle>
            <AlertDescription className={NOTICE_BODY}>
              <p
                className="line-clamp-2 break-words font-mono text-ui-11p5"
                title={firstError.text}
              >
                {firstError.text}
                {locationLabel(firstError) ? ` (${locationLabel(firstError)})` : ""}
              </p>
              <div className="flex flex-wrap items-center gap-2">
                {onFixWithModel ? (
                  <Button
                    size="xs"
                    className={NOTICE_PRIMARY}
                    onClick={() => onFixWithModel(buildCanvasFixPrompt(title, errors, kind))}
                    title={t("settings.chat.artifacts.errorHint")}
                  >
                    {t("settings.chat.artifacts.errorBannerAction")}
                  </Button>
                ) : null}
                {onConsoleOpenChange ? (
                  <Button
                    size="xs"
                    variant="outline"
                    className={NOTICE_SECONDARY}
                    onClick={() => onConsoleOpenChange(!consoleOpen)}
                  >
                    {t(
                      consoleOpen
                        ? "settings.chat.artifacts.errorConsoleHideAction"
                        : "settings.chat.artifacts.errorConsoleAction",
                    )}
                  </Button>
                ) : null}
              </div>
            </AlertDescription>
          </Alert>
        </div>
      ) : null}
      {consoleOpen ? (
        <section
          aria-label={t("settings.chat.artifacts.consoleTitle")}
          dir={RTL_LOCALES.has(locale) ? "rtl" : "ltr"}
          className="absolute inset-x-0 bottom-0 flex h-2/5 min-h-32 flex-col border-t border-border bg-background/95 text-xs backdrop-blur"
        >
          <div
            className="flex shrink-0 items-center gap-2 border-b border-border/70 px-2.5 py-1.5"
          >
            <span className="text-xs text-muted-foreground">
              {t(
                outputForCanvas.entries.length === 1
                  ? "settings.chat.artifacts.consoleMessageCount"
                  : "settings.chat.artifacts.consoleMessageCountPlural",
                { count: outputForCanvas.entries.length },
              )}
            </span>
            <span className="flex-1" />
            <Tooltip>
              <TooltipTrigger asChild={true}>
                <Button
                  size="icon-sm"
                  variant="ghost"
                  aria-label={t("settings.chat.artifacts.consoleClear")}
                  onClick={() => {
                    dropPendingEntries();
                    setOutput(emptyCanvasConsole(code));
                  }}
                >
                  <HugeiconsIcon icon={UnavailableIcon} strokeWidth={2} />
                </Button>
              </TooltipTrigger>
              <TooltipContent side="bottom" className="tooltip-compact">
                {t("settings.chat.artifacts.consoleClear")}
              </TooltipContent>
            </Tooltip>
            <Tooltip>
              <TooltipTrigger asChild={true}>
                <Button
                  size="icon-sm"
                  variant="ghost"
                  aria-label={t("settings.chat.artifacts.consoleClose")}
                  onClick={() => onConsoleOpenChange?.(false)}
                >
                  <HugeiconsIcon icon={Cancel01Icon} />
                </Button>
              </TooltipTrigger>
              <TooltipContent side="bottom" className="tooltip-compact">
                {t("settings.chat.artifacts.consoleClose")}
              </TooltipContent>
            </Tooltip>
          </div>
          <ol className="min-h-0 flex-1 overflow-auto pb-2 font-mono">
            {outputForCanvas.capped ? (
              <li className="border-b border-border/40 px-2.5 py-2 text-muted-foreground">
                {t("settings.chat.artifacts.consoleCapped", {
                  count: CANVAS_CONSOLE_ENTRIES_TRACKED,
                })}
              </li>
            ) : null}
            {outputForCanvas.entries.length === 0 ? (
              <li className="px-2.5 py-2 text-muted-foreground">
                {t("settings.chat.artifacts.consoleEmpty")}
              </li>
            ) : (
              outputForCanvas.entries.map((entry, index) => (
                <li
                  key={index}
                  className={cn(
                    "border-b border-border/40 px-2.5 py-2 leading-relaxed",
                    entry.level === "error" && "text-destructive",
                    entry.level === "warn" &&
                      "text-amber-600 dark:text-amber-400",
                  )}
                >
                  <p className="whitespace-pre-wrap break-words">
                    {entry.kind === "console" && entry.level !== "log" ? (
                      <span className="mr-1.5 uppercase text-muted-foreground">
                        {entry.level}
                      </span>
                    ) : null}
                    {entry.text}
                    {locationLabel(entry) ? ` (${locationLabel(entry)})` : ""}
                  </p>
                  {canvasStack(entry) ? (
                    <pre className="mt-2 overflow-x-auto border-l border-border pl-2.5 leading-relaxed text-muted-foreground">
                      {canvasStack(entry)}
                    </pre>
                  ) : null}
                </li>
              ))
            )}
          </ol>
        </section>
      ) : null}
    </div>
  );
}