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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
// Kept out of kit.ts so only tests that need it load the TypeScript compiler.
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
import ts from "@typescript/typescript6";
export type StubElement = {
type: unknown;
props: Record<string, unknown>;
};
function resolveSibling(fromPath: string, specifier: string): URL {
const base = new URL(specifier, pathToFileURL(fromPath));
for (const suffix of [".ts", ".tsx", "/index.ts", "/index.tsx", ""]) {
const candidate = new URL(base.href + suffix);
if (existsSync(fileURLToPath(candidate))) return candidate;
}
throw new Error(`cannot resolve import "${specifier}" from ${fromPath}`);
}
/** Run a shipped src module with every import replaced by a stub. */
export function loadWithStubs<T>(
moduleUrl: URL,
stubs: Record<string, unknown>,
options: { relativePassthrough?: boolean } = {},
): T {
const path = fileURLToPath(moduleUrl);
const source = readFileSync(path, "utf8").replaceAll(
"import.meta.url",
JSON.stringify(pathToFileURL(path).href),
);
const { outputText } = ts.transpileModule(source, {
fileName: path,
compilerOptions: {
target: ts.ScriptTarget.ES2022,
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.ReactJSX,
// TypeScript 6 turns this on by default, which would wrap the stubs in __importDefault.
esModuleInterop: false,
},
});
const loaded = { exports: {} as T };
const requireStub = (specifier: string): unknown => {
if (specifier in stubs) return stubs[specifier];
// Opt-in so unrelated new imports do not break tests; off by default to keep the throw.
if (options.relativePassthrough && specifier.startsWith(".")) {
return loadWithStubs(resolveSibling(path, specifier), stubs, options);
}
throw new Error(`no stub for import "${specifier}" in ${path}`);
};
new Function("require", "module", "exports", outputText)(
requireStub,
loaded,
loaded.exports,
);
return loaded.exports;
}
export function stubJsxRuntime(): Record<string, unknown> {
const jsx = (type: unknown, props: Record<string, unknown>): StubElement => ({
type,
props,
});
return { jsx, jsxs: jsx, Fragment: Symbol.for("Fragment") };
}