"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerShellTools = registerShellTools;
const sdk_1 = require("@lmstudio/sdk");
const zod_1 = require("zod");
const executor_1 = require("../executor");
const logger_1 = require("../logger");
async function registerShellTools() {
const tools = [];
tools.push((0, sdk_1.tool)({
name: 'shell_exec',
description: 'Execute a shell command and return the output.',
parameters: {
command: zod_1.z.string().describe('The shell command to execute'),
timeout_ms: zod_1.z.number().optional().describe('Timeout in milliseconds (default: 30000)'),
cwd: zod_1.z.string().optional().describe('Working directory (optional)'),
shell_path: zod_1.z.string().optional().describe('Override shell path (optional)'),
windows_shell: zod_1.z.enum(['powershell', 'cmd']).optional().describe('Windows shell: powershell or cmd'),
env: zod_1.z.record(zod_1.z.string()).optional().describe('Environment variables'),
stdin: zod_1.z.string().optional().describe('Stdin input'),
max_output_bytes: zod_1.z.number().optional().describe('Max output size in bytes'),
},
implementation: async (params) => {
try {
const result = await (0, executor_1.execCommand)(params.command, {
timeoutMs: params.timeout_ms ?? 30000,
cwd: params.cwd,
shellPath: params.shell_path,
windowsShell: params.windows_shell,
env: params.env,
stdin: params.stdin,
maxOutputBytes: params.max_output_bytes,
});
// Normalize line endings for consistent output across platforms
const stdout = (0, executor_1.normalizeLineEndings)(result.stdout);
const stderr = (0, executor_1.normalizeLineEndings)(result.stderr);
return {
success: true,
stdout,
stderr,
exitCode: result.exitCode,
timedOut: result.timedOut,
shell: result.shell,
platform: result.platform,
// Combined output for convenience — stdout + stderr together
output: stdout + (stderr ? '\n' + stderr : ''),
};
}
catch (err) {
logger_1.logger.error('shell_exec failed', { error: err.message, command: params.command });
return {
success: false,
stdout: '',
stderr: err.message,
exitCode: err.exitCode ?? -1,
timedOut: err.timedOut ?? false,
shell: 'unknown',
platform: process.platform,
output: '',
error: err.message,
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: 'run_process',
description: 'Spawn a process with arguments.',
parameters: {
command: zod_1.z.string().describe('The executable to run'),
args: zod_1.z.array(zod_1.z.string()).optional().describe('Arguments to pass'),
timeout_ms: zod_1.z.number().optional().describe('Timeout in milliseconds'),
cwd: zod_1.z.string().optional().describe('Working directory'),
env: zod_1.z.record(zod_1.z.string()).optional().describe('Environment variables'),
stdin: zod_1.z.string().optional().describe('Stdin input'),
max_output_bytes: zod_1.z.number().optional().describe('Max output size in bytes'),
},
implementation: async (params) => {
try {
const result = await (0, executor_1.runProcess)(params.command, params.args ?? [], {
timeoutMs: params.timeout_ms ?? 30000,
cwd: params.cwd,
env: params.env,
stdin: params.stdin,
maxOutputBytes: params.max_output_bytes,
});
// Normalize line endings
const stdout = (0, executor_1.normalizeLineEndings)(result.stdout);
const stderr = (0, executor_1.normalizeLineEndings)(result.stderr);
return {
success: true,
stdout,
stderr,
exitCode: result.exitCode,
timedOut: result.timedOut,
platform: result.platform,
// Combined output for convenience
output: stdout + (stderr ? '\n' + stderr : ''),
};
}
catch (err) {
logger_1.logger.error('run_process failed', { error: err.message, command: params.command });
return {
success: false,
stdout: '',
stderr: err.message,
exitCode: err.exitCode ?? -1,
timedOut: err.timedOut ?? false,
platform: process.platform,
output: '',
error: err.message,
};
}
},
}));
return tools;
}
//# sourceMappingURL=shell_tools.js.map