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.

2.4 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

/*
 * WHICH FENCE MODE IS IN FORCE, decided in one pure function.
 * Kept out of `code-fence-defer.tsx` because that is a `.tsx` and the frontend's tests run under
 * `node --experimental-strip-types`, which cannot load JSX. Every row below is RUN by
 * `tests/code-fence-mode.test.ts` rather than checked by regexes over the source.
 *   "off"        every fence is highlighted at mount. What shipped before `defer` became default.
 *   "defer"      SHIP DEFAULT. An unreached fence is a plain shell and is never tokenized.
 *   "tokenize"   MEASUREMENT ONLY. Same plain shell, but the highlighter is still driven over the
 *                source and the result thrown away.
 *   "window"     "defer", plus a LINE WINDOW inside a fence past `WINDOW_CAP_LINES`: off-screen
 *                lines render their text as one node instead of their token spans. Opt-in only.
 *                It is a superset of "defer": under the cap the two render the same DOM, so an
 *                A/B between them isolates the window and nothing else.
 * `tokenize` is not a shipping mode. `defer` removes the spans AND the tokenizer work that makes
 * them, so an improvement under `defer` alone cannot say which paid for it; `tokenize` holds the
 * DOM at `defer`'s size with only the tokenizer work back, so tokenize-minus-defer is the
 * tokenizer's share and off-minus-tokenize the document's. Reachable ONLY by the exact string
 * "tokenize" -- no boolean, empty value or default lands there, which keeps a measurement arm out
 * of a shipped install.
 */
export type FenceMode = "off" | "defer" | "tokenize" | "window";

// Keep highlighted spans stable: line-window updates can stall Chrome during rapid scrollbar drags.
export const SHIP_DEFAULT: FenceMode = "defer";

/** Unset takes SHIP_DEFAULT; an unrecognised non-empty value degrades to `off`, never the default. */
export const resolveFenceMode = (
  runtime: unknown,
  build: string,
): FenceMode => {
  const raw =
    typeof runtime === "string"
      ? runtime
      : runtime === true
        ? "defer"
        : runtime === false
          ? "off"
          : build;
  return raw === "1" || raw === "defer"
    ? "defer"
    : raw === "tokenize"
      ? "tokenize"
      : raw === "window"
        ? "window"
        : raw === ""
          ? SHIP_DEFAULT
          : "off";
};