langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
68 lines (67 loc) • 2.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const types_1 = require("../../types");
const child_process_1 = require("child_process");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class TerminalPlugin {
constructor() {
this.name = "terminal";
this.description = "Executes terminal commands securely.";
this.type = types_1.PluginType.LangCodeTool;
this.RunConfigExample = {
command: "",
timeout: 0,
env: {}
};
this.InitConfigExample = {
safeMode: true,
defaultTimeout: 5000,
workingDir: process.cwd()
};
this.safeMode = false;
this.defaultTimeout = 5000;
this.workingDir = process.cwd();
this.unsafeCommands = ["rm", "reboot", "shutdown", ":(){", "mkfs", "dd"];
}
expose() {
return {
name: this.name,
description: this.description,
type: this.type,
InitConfigExample: this.InitConfigExample,
RunConfigExample: this.RunConfigExample,
safeMode: this.safeMode,
defaultTimeout: this.defaultTimeout,
workingDir: this.workingDir,
unsafeCommands: this.unsafeCommands
};
}
async init(config) {
var _a, _b, _c;
this.safeMode = (_a = config.safeMode) !== null && _a !== void 0 ? _a : true;
this.defaultTimeout = (_b = config.defaultTimeout) !== null && _b !== void 0 ? _b : this.defaultTimeout;
this.workingDir = (_c = config.workingDir) !== null && _c !== void 0 ? _c : process.cwd();
}
async run(args) {
const { command, timeout, env } = args;
if (this.safeMode && this.unsafeCommands.some(cmd => command.includes(cmd))) {
return { error: "⚠️ Komut güvenlik nedeniyle engellendi." };
}
try {
const { stdout, stderr } = await execAsync(command, {
timeout: timeout !== null && timeout !== void 0 ? timeout : this.defaultTimeout,
cwd: this.workingDir,
env: Object.assign(Object.assign({}, process.env), env)
});
return {
stdout: stdout.trim(),
stderr: (stderr === null || stderr === void 0 ? void 0 : stderr.trim()) || null
};
}
catch (err) {
return { error: err.message };
}
}
}
exports.default = TerminalPlugin;