src / runShell.ts
import { tool, text } from "@lmstudio/sdk";
import { z } from "zod";
import { spawn } from "child_process";
export function createShellTool(cwd: string) {
return tool({
name: "run_shell",
description: text`Run shell command safely inside workspace`,
parameters: {
command: z.string(),
timeout_seconds: z.number().optional(),
},
implementation: async ({ command, timeout_seconds }) => {
const timeout = (timeout_seconds ?? 10) * 1000;
return new Promise((resolve, reject) => {
const child = spawn(command, {
cwd, // 🔥 필수 ê³ ì •
shell: true,
stdio: "pipe",
env: process.env,
});
let stdout = "";
let stderr = "";
child.stdout?.on("data", d => (stdout += d.toString()));
child.stderr?.on("data", d => (stderr += d.toString()));
const timer = setTimeout(() => {
child.kill("SIGKILL");
reject(new Error("shell timeout"));
}, timeout);
child.on("close", code => {
clearTimeout(timer);
resolve({
command,
code,
stdout,
stderr,
});
});
child.on("error", err => {
clearTimeout(timer);
reject(err);
});
});
},
});
}