Project Files
dist / toolsProvider.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolsProvider = toolsProvider;
const sdk_1 = require("@lmstudio/sdk");
const child_process_1 = require("child_process");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const zod_1 = require("zod");
const findLMStudioHome_1 = require("./findLMStudioHome");
function getDenoPath() {
const lmstudioHome = (0, findLMStudioHome_1.findLMStudioHome)();
const utilPath = (0, path_1.join)(lmstudioHome, ".internal", "utils");
const denoPath = (0, path_1.join)(utilPath, process.platform === "win32" ? "deno.exe" : "deno");
return denoPath;
}
async function toolsProvider(ctl) {
const tools = [];
const createFileTool = (0, sdk_1.tool)({
name: "run_javascript",
description: (0, sdk_1.text) `
Run a JavaScript code snippet using deno. You cannot import external modules but you have
read/write access to the current working directory.
Pass the code you wish to run as a string in the 'javascript' parameter.
By default, the code will timeout in 5 seconds. You can extend this timeout by setting the
'timeout_seconds' parameter to a higher value in seconds, up to a maximum of 60 seconds.
You will get the stdout and stderr output of the code execution, thus please print the output
you wish to return using 'console.log' or 'console.error'.
`,
parameters: { javascript: zod_1.z.string(), timeout_seconds: zod_1.z.number().optional() },
implementation: async ({ javascript, timeout_seconds }) => {
const workingDirectory = ctl.getWorkingDirectory();
const scriptFileName = `temp_script_${Date.now()}.ts`;
const scriptFilePath = (0, path_1.join)(workingDirectory, scriptFileName);
await (0, promises_1.writeFile)(scriptFilePath, javascript, "utf-8");
const childProcess = (0, child_process_1.spawn)(getDenoPath(), [
"run",
"--allow-read=.",
"--allow-write=.",
"--no-prompt",
"--deny-net",
"--deny-env",
"--deny-sys",
"--deny-run",
"--deny-ffi",
scriptFilePath,
], {
cwd: workingDirectory,
timeout: (timeout_seconds ?? 5) * 1000, // Convert seconds to milliseconds
stdio: "pipe",
env: {
NO_COLOR: "true", // Disable color output in Deno
},
});
let stdout = "";
let stderr = "";
childProcess.stdout.setEncoding("utf-8");
childProcess.stderr.setEncoding("utf-8");
childProcess.stdout.on("data", data => {
stdout += data;
});
childProcess.stderr.on("data", data => {
stderr += data;
});
await new Promise((resolve, reject) => {
childProcess.on("close", code => {
if (code === 0) {
resolve();
}
else {
reject(new Error(`Process exited with code ${code}. Stderr: ${stderr}`));
}
});
childProcess.on("error", err => {
reject(err);
});
});
await (0, promises_1.rm)(scriptFilePath);
return {
stdout: stdout.trim(),
stderr: stderr.trim(),
};
},
});
tools.push(createFileTool);
return tools;
}
//# sourceMappingURL=toolsProvider.js.map