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.6 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 {
  hasOnlyStudioOwnedToolHistory,
  studioToolHistoryRequestFields,
} from "../src/features/chat/utils/studio-tool-history.ts";

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

function assistantToolCall(provenance?: unknown) {
  return {
    role: "assistant",
    content: [
      {
        type: "tool-call",
        toolName: "python",
        toolCallId: "call_1",
        args: { code: "print(1)" },
        result: "1",
        ...(provenance === undefined ? {} : { provenance }),
      },
    ],
  };
}

test("local tool provenance marks studio-owned history", () => {
  assert.equal(
    hasOnlyStudioOwnedToolHistory([
      { role: "user", content: [{ type: "text", text: "run python" }] },
      assistantToolCall({ source: "local" }),
    ]),
    true,
  );
});

test("mixed or unmarked tool history remains a client contract", () => {
  assert.equal(
    hasOnlyStudioOwnedToolHistory([
      assistantToolCall({ source: "local" }),
      assistantToolCall({ source: "external" }),
    ]),
    false,
  );
  assert.equal(hasOnlyStudioOwnedToolHistory([assistantToolCall()]), false);
  assert.equal(
    hasOnlyStudioOwnedToolHistory([
      { role: "assistant", content: [{ type: "text", text: "plain reply" }] },
    ]),
    false,
  );
});

test("only studio-owned tool history emits the request marker", () => {
  assert.deepEqual(
    studioToolHistoryRequestFields([assistantToolCall({ source: "local" })]),
    { studio_tool_history: true },
  );
  assert.deepEqual(
    studioToolHistoryRequestFields([
      assistantToolCall({ source: "local" }),
      assistantToolCall({ source: "external" }),
    ]),
    {},
  );
});

function hostedServerSearchCall() {
  return {
    role: "assistant",
    content: [
      {
        type: "tool-call",
        toolName: "web_search",
        toolCallId: "call_search",
        args: { _server_tool: true, query: "news" },
        result: { text: "hits" },
        provenance: { source: "external" },
      },
    ],
  };
}

test("ownership ignores hosted builtins that OpenAI replay drops", () => {
  const mixed = [
    hostedServerSearchCall(),
    assistantToolCall({ source: "local" }),
  ];
  assert.deepEqual(studioToolHistoryRequestFields(mixed), {});
  assert.deepEqual(
    studioToolHistoryRequestFields(mixed, {
      toolCallSurvives: (part) => {
        const args = part.args;
        return !(
          args &&
          typeof args === "object" &&
          !Array.isArray(args) &&
          (args as { _server_tool?: unknown })._server_tool === true
        );
      },
    }),
    { studio_tool_history: true },
  );
});

test("the live request and token-count paths mark ownership after replay", () => {
  const adapter = readSrc("features/chat/api/chat-adapter.ts");
  const helper = adapter.indexOf(
    "function studioToolHistoryRequestFieldsAfterReplay",
  );
  const surviveFn = adapter.indexOf(
    "function toolCallPartSurvivesOpenAIReplay",
  );
  const serializeGate = adapter.indexOf(
    "serializeAssistantToolCallPart",
    surviveFn,
  );
  const first = adapter.indexOf("...studioToolHistoryRequestFieldsAfterReplay(");
  const second = adapter.indexOf(
    "...studioToolHistoryRequestFieldsAfterReplay(",
    first + 1,
  );
  assert.ok(helper >= 0);
  assert.ok(surviveFn >= 0 && surviveFn < helper);
  assert.ok(serializeGate > surviveFn);
  assert.ok(first >= 0 && second > first);
  assert.equal(
    adapter.indexOf("...studioToolHistoryRequestFields(survivingMessages)"),
    -1,
  );
});