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.9 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

import assert from "node:assert/strict";
import test from "node:test";

import { registerBundlerResolver } from "./helpers/kit.ts";

registerBundlerResolver();

const {
  BROWSER_EXECUTED_TOOLS,
  isDurableRunCandidate,
  turnRequiresLegacyStream,
} = await import("../src/features/chat/api/durable-gate.ts");

// A turn stays durable unless a browser-executed tool is enabled; requestPayload.tools is not that.

test("a local agentic turn with server-executed tools stays durable", () => {
  assert.equal(
    turnRequiresLegacyStream({
      enable_tools: true,
      enabled_tools: ["web_search", "python", "terminal", "edit_file"],
    }),
    false,
  );
});

test("a passthrough turn carrying the caller's schema catalog stays durable", () => {
  assert.equal(
    turnRequiresLegacyStream({
      enable_tools: true,
      enabled_tools: ["web_search"],
      tools: [{ type: "function", function: { name: "get_weather" } }],
    }),
    false,
  );
});

test("a plain text turn stays durable", () => {
  assert.equal(turnRequiresLegacyStream({ enable_tools: false }), false);
});

test("only a browser-executed tool forces the legacy stream", () => {
  // The browser tools drive this tab's browser panel, so the turn can't outlive it.
  assert.ok(BROWSER_EXECUTED_TOOLS.has("browser_click"));
  assert.ok(BROWSER_EXECUTED_TOOLS.has("browser_screenshot"));
  assert.ok(![...BROWSER_EXECUTED_TOOLS].some((name) => !name.startsWith("browser_")));
  assert.equal(
    turnRequiresLegacyStream({ enable_tools: true, enabled_tools: ["web_search", "browser_navigate"] }),
    true,
  );
});

const durableTurn = {
  externalProvider: false,
  modelIsAudio: false,
  loadedIsDiffusion: false,
  turnCarriesMedia: false,
  continuation: false,
  threadId: "thread-1",
  incognito: false,
  assistantMessageId: "assistant-1",
  hasUserMessage: true,
};

test("a plain text turn on a persisted thread is a durable-run candidate", () => {
  assert.equal(isDurableRunCandidate(durableTurn), true);
});

const legacyCases: [string, Record<string, unknown>, string][] = [
  [
    "an external-provider turn",
    { externalProvider: true },
    "its own client owns that stream, so there is no server run to reattach to",
  ],
  [
    "an audio model",
    { modelIsAudio: true },
    "audio runs on its own path regardless of the gate",
  ],
  [
    "a loaded diffusion model",
    { loadedIsDiffusion: true },
    "the diffusion path has no durable run to join",
  ],
  [
    "THIS turn carrying an attachment",
    { turnCarriesMedia: true },
    "a media turn stays subscriber-owned; a text follow-up to an earlier screenshot must NOT (see the next case)",
  ],
  [
    "a continuation",
    { continuation: true },
    "the seeded partial is autosaved before the request starts, and admission 409s a placeholder that already has content",
  ],
  [
    "an incognito thread",
    { incognito: true },
    "an incognito run has no stored history to resume from",
  ],
  [
    "no thread to reattach to",
    { threadId: undefined },
    "a durable run is found again by its thread; with none, nothing reattaches",
  ],
  [
    "no assistant message to write into",
    { assistantMessageId: null },
    "a follower needs a row to resume, not invent one",
  ],
  [
    "no user message for the run to answer",
    { hasUserMessage: false },
    "there is no turn yet, so nothing survives the tab closing",
  ],
];

for (const [name, override, why] of legacyCases) {
  test(`${name} stays on the subscriber-owned stream`, () => {
    assert.equal(
      isDurableRunCandidate({ ...durableTurn, ...override }),
      false,
      `${name}: ${why}`,
    );
  });
}

test("the gate reads the turn's own media, never the thread's history", () => {
  assert.equal(isDurableRunCandidate({ ...durableTurn, turnCarriesMedia: false }), true);
});