dist / tools / fileTools.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run_python = exports.run_bash = exports.delete_path = exports.make_dir = exports.list_dir = exports.edit_file = exports.write_file = exports.read_file = void 0;
const sdk_1 = require("@lmstudio/sdk");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const child_process_1 = require("child_process");
const util_1 = require("util");
const zod_1 = require("zod");
const utils_js_1 = require("../utils.js");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
// 1. Leer archivo
exports.read_file = (0, sdk_1.tool)({
name: "read_file",
description: "Read the content of a file on the host. Supports relative paths to workspace (/home/arkantu/workspace), home (~/...), or absolute paths.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file (e.g. 'my_file.txt', '~/notes.md', or '/home/user/workspace/project/main.py').")
},
implementation: async ({ filepath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
const content = await (0, promises_1.readFile)(targetPath, "utf-8");
return {
success: true,
filepath: targetPath,
content: (0, utils_js_1.truncateOutput)(content)
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 2. Escribir / Crear archivo
exports.write_file = (0, sdk_1.tool)({
name: "write_file",
description: "Write or overwrite text in a file on the host. Automatically creates parent directories. Relative paths are written to the workspace root.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file (e.g. 'src/index.ts', 'plan.md', or absolute path)."),
content: zod_1.z.string().describe("Text content to write to the file.")
},
implementation: async ({ filepath, content }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
await (0, promises_1.mkdir)((0, path_1.dirname)(targetPath), { recursive: true });
await (0, promises_1.writeFile)(targetPath, content, "utf-8");
return {
success: true,
filepath: targetPath,
message: `OK: File saved at ${targetPath} (${content.length} chars)`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 3. Editar archivo (reemplazo puntual de texto)
exports.edit_file = (0, sdk_1.tool)({
name: "edit_file",
description: "Replace exact text inside an existing file. Relative paths resolve against the workspace root.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file to edit."),
old_text: zod_1.z.string().describe("Exact text substring to replace."),
new_text: zod_1.z.string().describe("New replacement text.")
},
implementation: async ({ filepath, old_text, new_text }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
const content = await (0, promises_1.readFile)(targetPath, "utf-8");
if (!content.includes(old_text)) {
return {
success: false,
filepath: targetPath,
error: "old_text not found in file."
};
}
const updated = content.replace(old_text, new_text);
await (0, promises_1.writeFile)(targetPath, updated, "utf-8");
return {
success: true,
filepath: targetPath,
message: `OK: File text replaced successfully in ${targetPath}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 4. Listar directorio
exports.list_dir = (0, sdk_1.tool)({
name: "list_dir",
description: "List files and subdirectories in a directory. Defaults to host workspace root if omitted or relative.",
parameters: {
dirpath: zod_1.z.string().optional().describe("Directory path (defaults to host workspace root).")
},
implementation: async ({ dirpath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(dirpath || ".");
const entries = await (0, promises_1.readdir)(targetPath, { withFileTypes: true });
const list = entries.map(e => (e.isDirectory() ? `[DIR] ${e.name}` : `[FILE] ${e.name}`));
return {
success: true,
dirpath: targetPath,
count: list.length,
items: list.slice(0, 100)
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 5. Crear directorio
exports.make_dir = (0, sdk_1.tool)({
name: "make_dir",
description: "Create a directory and parent directories if needed. Relative paths are created in host workspace.",
parameters: {
dirpath: zod_1.z.string().describe("Directory path to create.")
},
implementation: async ({ dirpath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(dirpath);
await (0, promises_1.mkdir)(targetPath, { recursive: true });
return {
success: true,
dirpath: targetPath,
message: `OK: Directory created at ${targetPath}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 6. Eliminar archivo o carpeta
exports.delete_path = (0, sdk_1.tool)({
name: "delete_path",
description: "Delete a file or directory (recursive). Supports relative to workspace, ~, or absolute.",
parameters: {
target_path: zod_1.z.string().describe("File or directory path to delete.")
},
implementation: async ({ target_path }) => {
try {
const p = (0, utils_js_1.resolvePath)(target_path);
await (0, promises_1.rm)(p, { recursive: true, force: true });
return {
success: true,
path: p,
message: `OK: Deleted ${p}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 7. Ejecutar Bash
exports.run_bash = (0, sdk_1.tool)({
name: "run_bash",
description: "Run a bash shell command in host workspace directory.",
parameters: {
command: zod_1.z.string().describe("The bash command to execute.")
},
implementation: async ({ command }) => {
try {
const cwd = (0, utils_js_1.getWorkspaceDir)();
await (0, promises_1.mkdir)(cwd, { recursive: true });
const { stdout, stderr } = await execAsync(command, {
cwd,
timeout: 45000,
maxBuffer: 2 * 1024 * 1024
});
return {
exit_code: 0,
cwd,
stdout: (0, utils_js_1.truncateOutput)(stdout.trim()),
stderr: (0, utils_js_1.truncateOutput)(stderr.trim())
};
}
catch (err) {
return {
exit_code: err.code ?? 1,
stdout: (0, utils_js_1.truncateOutput)(err.stdout ? String(err.stdout).trim() : ""),
stderr: (0, utils_js_1.truncateOutput)(err.stderr ? String(err.stderr).trim() : err.message)
};
}
}
});
// 8. Ejecutar Python
exports.run_python = (0, sdk_1.tool)({
name: "run_python",
description: "Execute Python 3 code directly with host workspace as working directory.",
parameters: {
code: zod_1.z.string().describe("Python code snippet to execute.")
},
implementation: async ({ code }) => {
try {
const cwd = (0, utils_js_1.getWorkspaceDir)();
await (0, promises_1.mkdir)(cwd, { recursive: true });
const { stdout, stderr } = await execAsync("python3 -c " + JSON.stringify(code), {
cwd,
timeout: 45000,
maxBuffer: 2 * 1024 * 1024
});
return {
exit_code: 0,
cwd,
stdout: (0, utils_js_1.truncateOutput)(stdout.trim()),
stderr: (0, utils_js_1.truncateOutput)(stderr.trim())
};
}
catch (err) {
return {
exit_code: err.code ?? 1,
stdout: (0, utils_js_1.truncateOutput)(err.stdout ? String(err.stdout).trim() : ""),
stderr: (0, utils_js_1.truncateOutput)(err.stderr ? String(err.stderr).trim() : err.message)
};
}
}
});
dist / tools / fileTools.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run_python = exports.run_bash = exports.delete_path = exports.make_dir = exports.list_dir = exports.edit_file = exports.write_file = exports.read_file = void 0;
const sdk_1 = require("@lmstudio/sdk");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const child_process_1 = require("child_process");
const util_1 = require("util");
const zod_1 = require("zod");
const utils_js_1 = require("../utils.js");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
// 1. Leer archivo
exports.read_file = (0, sdk_1.tool)({
name: "read_file",
description: "Read the content of a file on the host. Supports relative paths to workspace (/home/arkantu/workspace), home (~/...), or absolute paths.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file (e.g. 'my_file.txt', '~/notes.md', or '/home/user/workspace/project/main.py').")
},
implementation: async ({ filepath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
const content = await (0, promises_1.readFile)(targetPath, "utf-8");
return {
success: true,
filepath: targetPath,
content: (0, utils_js_1.truncateOutput)(content)
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 2. Escribir / Crear archivo
exports.write_file = (0, sdk_1.tool)({
name: "write_file",
description: "Write or overwrite text in a file on the host. Automatically creates parent directories. Relative paths are written to the workspace root.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file (e.g. 'src/index.ts', 'plan.md', or absolute path)."),
content: zod_1.z.string().describe("Text content to write to the file.")
},
implementation: async ({ filepath, content }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
await (0, promises_1.mkdir)((0, path_1.dirname)(targetPath), { recursive: true });
await (0, promises_1.writeFile)(targetPath, content, "utf-8");
return {
success: true,
filepath: targetPath,
message: `OK: File saved at ${targetPath} (${content.length} chars)`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 3. Editar archivo (reemplazo puntual de texto)
exports.edit_file = (0, sdk_1.tool)({
name: "edit_file",
description: "Replace exact text inside an existing file. Relative paths resolve against the workspace root.",
parameters: {
filepath: zod_1.z.string().describe("Path to the file to edit."),
old_text: zod_1.z.string().describe("Exact text substring to replace."),
new_text: zod_1.z.string().describe("New replacement text.")
},
implementation: async ({ filepath, old_text, new_text }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(filepath);
const content = await (0, promises_1.readFile)(targetPath, "utf-8");
if (!content.includes(old_text)) {
return {
success: false,
filepath: targetPath,
error: "old_text not found in file."
};
}
const updated = content.replace(old_text, new_text);
await (0, promises_1.writeFile)(targetPath, updated, "utf-8");
return {
success: true,
filepath: targetPath,
message: `OK: File text replaced successfully in ${targetPath}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 4. Listar directorio
exports.list_dir = (0, sdk_1.tool)({
name: "list_dir",
description: "List files and subdirectories in a directory. Defaults to host workspace root if omitted or relative.",
parameters: {
dirpath: zod_1.z.string().optional().describe("Directory path (defaults to host workspace root).")
},
implementation: async ({ dirpath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(dirpath || ".");
const entries = await (0, promises_1.readdir)(targetPath, { withFileTypes: true });
const list = entries.map(e => (e.isDirectory() ? `[DIR] ${e.name}` : `[FILE] ${e.name}`));
return {
success: true,
dirpath: targetPath,
count: list.length,
items: list.slice(0, 100)
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 5. Crear directorio
exports.make_dir = (0, sdk_1.tool)({
name: "make_dir",
description: "Create a directory and parent directories if needed. Relative paths are created in host workspace.",
parameters: {
dirpath: zod_1.z.string().describe("Directory path to create.")
},
implementation: async ({ dirpath }) => {
try {
const targetPath = (0, utils_js_1.resolvePath)(dirpath);
await (0, promises_1.mkdir)(targetPath, { recursive: true });
return {
success: true,
dirpath: targetPath,
message: `OK: Directory created at ${targetPath}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 6. Eliminar archivo o carpeta
exports.delete_path = (0, sdk_1.tool)({
name: "delete_path",
description: "Delete a file or directory (recursive). Supports relative to workspace, ~, or absolute.",
parameters: {
target_path: zod_1.z.string().describe("File or directory path to delete.")
},
implementation: async ({ target_path }) => {
try {
const p = (0, utils_js_1.resolvePath)(target_path);
await (0, promises_1.rm)(p, { recursive: true, force: true });
return {
success: true,
path: p,
message: `OK: Deleted ${p}`
};
}
catch (err) {
return { success: false, error: err.message };
}
}
});
// 7. Ejecutar Bash
exports.run_bash = (0, sdk_1.tool)({
name: "run_bash",
description: "Run a bash shell command in host workspace directory.",
parameters: {
command: zod_1.z.string().describe("The bash command to execute.")
},
implementation: async ({ command }) => {
try {
const cwd = (0, utils_js_1.getWorkspaceDir)();
await (0, promises_1.mkdir)(cwd, { recursive: true });
const { stdout, stderr } = await execAsync(command, {
cwd,
timeout: 45000,
maxBuffer: 2 * 1024 * 1024
});
return {
exit_code: 0,
cwd,
stdout: (0, utils_js_1.truncateOutput)(stdout.trim()),
stderr: (0, utils_js_1.truncateOutput)(stderr.trim())
};
}
catch (err) {
return {
exit_code: err.code ?? 1,
stdout: (0, utils_js_1.truncateOutput)(err.stdout ? String(err.stdout).trim() : ""),
stderr: (0, utils_js_1.truncateOutput)(err.stderr ? String(err.stderr).trim() : err.message)
};
}
}
});
// 8. Ejecutar Python
exports.run_python = (0, sdk_1.tool)({
name: "run_python",
description: "Execute Python 3 code directly with host workspace as working directory.",
parameters: {
code: zod_1.z.string().describe("Python code snippet to execute.")
},
implementation: async ({ code }) => {
try {
const cwd = (0, utils_js_1.getWorkspaceDir)();
await (0, promises_1.mkdir)(cwd, { recursive: true });
const { stdout, stderr } = await execAsync("python3 -c " + JSON.stringify(code), {
cwd,
timeout: 45000,
maxBuffer: 2 * 1024 * 1024
});
return {
exit_code: 0,
cwd,
stdout: (0, utils_js_1.truncateOutput)(stdout.trim()),
stderr: (0, utils_js_1.truncateOutput)(stderr.trim())
};
}
catch (err) {
return {
exit_code: err.code ?? 1,
stdout: (0, utils_js_1.truncateOutput)(err.stdout ? String(err.stdout).trim() : ""),
stderr: (0, utils_js_1.truncateOutput)(err.stderr ? String(err.stderr).trim() : err.message)
};
}
}
});