langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
117 lines (116 loc) • 4.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const base_1 = require("../../base");
const types_1 = require("../../types");
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class DockerPlugin {
constructor() {
this.name = "docker";
this.description = "Runs secure Docker commands like docker run, docker exec.";
this.type = types_1.PluginType.LangCodeTool;
this.RunConfigExample = {
command: "",
timeout: 0,
env: {}
};
this.InitConfigExample = {
safeMode: true,
defaultTimeout: 10000,
workingDir: process.cwd()
};
this.safeMode = true;
this.defaultTimeout = 10000;
this.workingDir = process.cwd();
this.unsafeCommands = ["rm", "reboot", "shutdown", "--privileged", "--mount", "-v", "--volume"];
this.allowedDockerCommands = [
"docker run",
"docker exec",
"docker ps",
"docker images",
"docker build",
"docker pull",
"docker push",
"docker stop",
"docker start",
"docker restart",
"docker kill",
"docker rm",
"docker rmi",
"docker logs",
"docker inspect",
"docker volume",
"docker network",
"docker tag",
"docker save",
"docker load",
"docker login",
"docker logout",
"docker info",
"docker stats",
"docker version",
"docker compose",
"docker context",
"docker cp",
"docker diff",
"docker system",
"docker history",
"docker port",
"docker events",
"docker wait",
"docker top",
"docker update",
"docker attach",
"docker rename",
"docker export",
"docker import"
];
this.dfc = new base_1.DockerFastCommands({ debug: true });
}
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,
allowedDockerCommands: this.allowedDockerCommands,
dfc: this.dfc
};
}
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 : 10000;
this.workingDir = (_c = config.workingDir) !== null && _c !== void 0 ? _c : process.cwd();
}
async run(args) {
const { command, timeout, env } = args;
if (!this.allowedDockerCommands.some(cmd => command.startsWith(cmd))) {
return { error: "⛔️ Yalnızca belirli docker komutlarına izin veriliyor." };
}
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 = DockerPlugin;