src / Python / runPython.ts
import { tool, text } from "@lmstudio/sdk";
import { z } from "zod";
import { runProcess } from "./runProcess";
import { getPython, ensurePython } from "./pythonEnv";
export function createRunPythonTool(ctl: any) {
return tool({
name: "run_python",
description: text`
Run Python code using system Python.
`,
parameters: {
code: z.string(),
timeout_seconds: z.number().optional(),
},
implementation: async ({ code, timeout_seconds }) => {
const cwd = ctl.getWorkingDirectory();
const timeout = (timeout_seconds ?? 5) * 1000;
ensurePython();
const python = getPython();
return runProcess(python, ["-c", code], timeout, cwd);
},
});
}