auto-gpt-ts
Version:
my take of Auto-GPT in typescript
160 lines • 7.02 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecuteShell = exports.ExecuteJavascriptFile = void 0;
const vm2_1 = require("vm2");
const config_1 = require("../config/config");
const command_1 = require("./command");
const child_process_1 = require("child_process");
// import * as ts from "typescript";
const logging_1 = require("../logging");
const path = __importStar(require("path"));
const CFG = new config_1.Config();
const jsVm = new vm2_1.NodeVM({
require: {
external: true,
root: CFG.workspacePath,
},
console: "inherit",
});
let ExecuteJavascriptFile = class ExecuteJavascriptFile {
static executeJavascriptFile(filename) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const res = jsVm.runFile(filename);
return res;
}
catch (err) {
return (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : err;
}
});
}
};
ExecuteJavascriptFile = __decorate([
(0, command_1.CommandDecorator)({
name: "executeJavascriptFile",
description: "Execute Javascript File",
signature: '"filename": string',
})
], ExecuteJavascriptFile);
exports.ExecuteJavascriptFile = ExecuteJavascriptFile;
// @CommandDecorator({
// name: "executeTypescriptCode",
// description: "Execute Typescript Code",
// signature: '"code": string',
// })
// export class ExecuteTypescriptCode {
// static async executeTypescriptCode(code: string) {
// try {
// const jsCode = ts.transpileModule(code, {
// compilerOptions: { module: ts.ModuleKind.CommonJS },
// });
// return jsVm.run(jsCode.outputText);
// } catch (err) {
// return err?.message ?? err;
// }
// }
// }
let lastWorkingDir;
let ExecuteShell = class ExecuteShell {
static executeShellCommandLine(commandLine) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (!lastWorkingDir) {
lastWorkingDir = CFG.workspacePath;
}
let workingDir = lastWorkingDir;
if (!path.relative(workingDir, CFG.workspacePath).startsWith("..")) {
workingDir = CFG.workspacePath;
}
this.logger.info(`Executing shell command ${commandLine} in dir ${workingDir}`);
let stdout = "";
let stderr = "";
try {
yield new Promise((resolve, reject) => {
const cp = (0, child_process_1.spawn)(commandLine + " & echo $PWD", {
cwd: workingDir,
stdio: "pipe",
shell: true,
detached: true,
});
cp.stdout.on("data", (data) => {
stdout += data.toString();
});
cp.stderr.on("data", (data) => {
stderr += data.toString();
});
cp.on("close", (code) => {
if (code === 0) {
resolve();
}
else {
reject(new Error(`Shell command exited with code ${code}`));
}
});
setTimeout(resolve, 1000 * 60 * 2);
});
}
catch (error) {
stderr = (_b = (_a = error === null || error === void 0 ? void 0 : error.stderr) !== null && _a !== void 0 ? _a : error.message) !== null && _b !== void 0 ? _b : error;
}
lastWorkingDir = (_c = stdout.trim().split("\n").pop()) !== null && _c !== void 0 ? _c : lastWorkingDir;
const output = `STDOUT:\n${stdout
.trim()
.split("\n")
.slice(0, -1)
.join("\n")}\nSTDERR:\n${stderr}`;
return output;
});
}
};
ExecuteShell.logger = (0, logging_1.getLogger)("ExecuteShell");
ExecuteShell = __decorate([
(0, command_1.CommandDecorator)({
name: "executeShellCommandLine",
description: "Execute Shell Command, non-interactive commands only",
signature: '"commandLine": string',
enabled: CFG.executeLocalCommands,
disabledReason: `You are not allowed to run local shell commands. To execute shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' in your config. Do not attempt to bypass the restriction.`,
})
], ExecuteShell);
exports.ExecuteShell = ExecuteShell;
//# sourceMappingURL=run-code.js.map