auto-gpt-ts
Version:
my take of Auto-GPT in typescript
256 lines • 10.1 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadFile = exports.AppendToFile = exports.WriteToFile = exports.ReadFile = void 0;
const crypto = __importStar(require("crypto"));
const fs = __importStar(require("fs"));
const config_1 = require("../config/config");
const logging_1 = require("../logging");
const chardet_1 = __importDefault(require("chardet"));
const iconv = __importStar(require("iconv-lite"));
const command_1 = require("./command");
const path = __importStar(require("path"));
const axios_1 = __importDefault(require("axios"));
const CFG = new config_1.Config();
const logger = (0, logging_1.getLogger)("file-operations");
function text_checksum(text) {
// Get the hex checksum for the given text
return crypto.createHash("md5").update(text).digest("hex");
}
function* operations_from_log(log_path) {
let log;
try {
log = fs.readFileSync(log_path, "utf-8");
}
catch (err) {
return;
}
for (let line of log.split("\n")) {
line = line.replace("File Operation Logger", "").trim();
if (!line) {
continue;
}
const [operation, tail] = line.split(": ", 2);
if (operation === "write" || operation === "append") {
let [path, checksum] = tail.split(" #", 2).map((x) => x.trim());
if (!checksum) {
checksum = null;
}
yield [operation, path, checksum];
}
else if (operation === "delete") {
yield [operation, tail.trim(), null];
}
}
}
function file_operations_state(log_path) {
const state = {};
for (const [operation, path, checksum] of operations_from_log(log_path)) {
if (operation === "write" || operation === "append") {
state[path] = checksum !== null && checksum !== void 0 ? checksum : "";
}
else if (operation === "delete") {
delete state[path];
}
}
return state;
}
function is_duplicate_operation(operation, filename, checksum = null) {
const state = file_operations_state(CFG.fileLoggerPath);
if (operation === "delete" && !(filename in state)) {
return true;
}
else if (operation === "write" && state[filename] === (checksum !== null && checksum !== void 0 ? checksum : "")) {
return true;
}
return false;
}
function log_operation(operation, filename, checksum = null) {
let log_entry = `${operation}: ${filename}`;
if (checksum !== null) {
log_entry += ` #${checksum}`;
}
logger.debug(`Logging file operation: ${log_entry}`);
AppendToFile.appendToFile(CFG.fileLoggerPath, `${log_entry}\n`, false);
}
function* split_file(content, max_length = 4000, overlap = 0) {
let start = 0;
const content_length = content.length;
while (start < content_length) {
const end = start + max_length;
let chunk;
if (end + overlap < content_length) {
chunk = content.substring(start, end + overlap - 1);
}
else {
chunk = content.substring(start, content_length);
// Account for the case where the last chunk is shorter than the overlap, so it has already been consumed
if (chunk.length <= overlap) {
break;
}
}
yield chunk;
start += max_length - overlap;
}
}
function readable_file_size(size, decimal_places = 2) {
const units = ["B", "KB", "MB", "GB", "TB"];
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(decimal_places)} ${units[unitIndex]}`;
}
let ReadFile = class ReadFile {
static readFile(filename) {
var _a;
if (filename.includes("<filename>")) {
return "Error: Please specify a filename.";
}
try {
const buffer = fs.readFileSync(filename);
const encoding = (_a = chardet_1.default.detect(buffer)) !== null && _a !== void 0 ? _a : "utf-8";
const contents = iconv.decode(buffer, encoding);
logger.debug(`Read file '${filename}' with encoding '${encoding}'`);
return contents;
}
catch (err) {
return `Error: ${err}`;
}
}
};
ReadFile = __decorate([
(0, command_1.CommandDecorator)({
name: "readFile",
description: "Read File",
signature: '"filename": string',
})
], ReadFile);
exports.ReadFile = ReadFile;
let WriteToFile = class WriteToFile {
static writeToFile(filename, text) {
if (filename.includes("<filename>")) {
return "Error: Please specify a filename.";
}
if (text === "<text>") {
return "Error: Please specify text to write.";
}
const checksum = text_checksum(text);
if (is_duplicate_operation("write", filename, checksum)) {
return "Error: File has already been updated.";
}
try {
const directory = path.dirname(filename);
fs.mkdirSync(directory, { recursive: true });
fs.writeFileSync(filename, text, "utf-8");
log_operation("write", filename, checksum);
return "File written successfully.";
}
catch (err) {
return `Error: ${err}`;
}
}
};
WriteToFile = __decorate([
(0, command_1.CommandDecorator)({
name: "writeToFile",
description: "Write to File",
signature: '"filename": string, "text": string',
aliases: ["writeFile", 'createFile'],
})
], WriteToFile);
exports.WriteToFile = WriteToFile;
let AppendToFile = class AppendToFile {
static appendToFile(filename, text, should_log = true) {
try {
if (filename.includes("<filename>")) {
return `Error: Please provide a valid filename.`;
}
const directory = path.dirname(filename);
fs.mkdirSync(directory, { recursive: true });
fs.appendFileSync(filename, text, "utf-8");
if (should_log) {
const content = fs.readFileSync(filename, "utf-8");
const checksum = text_checksum(content);
log_operation("append", filename, checksum);
}
return "Text appended successfully.";
}
catch (err) {
return `Error: ${err}`;
}
}
};
AppendToFile = __decorate([
(0, command_1.CommandDecorator)({
name: "appendToFile",
description: "Append to File",
signature: '"filename": string, "text": string',
})
], AppendToFile);
exports.AppendToFile = AppendToFile;
let DownloadFile = class DownloadFile {
static downloadFile(url, destinationPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield axios_1.default.get(url, { responseType: "arraybuffer" });
const data = response.data;
fs.writeFileSync(destinationPath, data);
return `Successfully downloaded and locally stored file: "${path.basename(destinationPath)}.${path.extname(destinationPath)}"! (Size: ${readable_file_size(Buffer.byteLength(data))})`;
}
catch (err) {
return `Error: ${err}`;
}
});
}
};
DownloadFile = __decorate([
(0, command_1.CommandDecorator)({
name: "downloadFile",
description: "Download File",
signature: '"url":string, "destinationPath": string',
})
], DownloadFile);
exports.DownloadFile = DownloadFile;
//# sourceMappingURL=file-operations.js.map