auto-gpt-ts
Version:
my take of Auto-GPT in typescript
140 lines • 5.62 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 __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.markdownToAnsiStyle = exports.getCurrentGitBranch = exports.readableFileSize = exports.validateYamlFile = exports.cleanInput = void 0;
const prompt = __importStar(require("prompt"));
const yaml = __importStar(require("js-yaml"));
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs"));
const simple_git_1 = require("simple-git");
const logging_1 = require("./logging");
const logger = (0, logging_1.getLogger)();
function cleanInput(promptString = '', talk = false) {
return __awaiter(this, void 0, void 0, function* () {
return logging_1.Logger.addTask(() => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
// ask for input, default when just pressing Enter is y
logger.debug('Asking user via keyboard...');
prompt.start();
const answer = yield prompt.get([promptString]);
// prompt.stop();
return (_b = (_a = answer[promptString]) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
}
catch (error) {
logger.error(error);
logger.info('You interrupted Auto-GPT');
logger.info('Quitting...');
process.exit(0);
}
}));
});
}
exports.cleanInput = cleanInput;
function validateYamlFile(file) {
try {
const data = fs.readFileSync(file, 'utf-8');
yaml.load(data);
}
catch (error) {
if (error.code === 'ENOENT') {
return [
false,
`The file ${chalk_1.default.cyan(file)} wasn't found`,
];
}
return [
false,
`There was an issue while trying to read your AI Settings file: ${error.message}`,
];
}
return [
true,
`Successfully validated ${chalk_1.default.cyan(file)}!`,
];
}
exports.validateYamlFile = validateYamlFile;
function readableFileSize(size, decimal_places = 2) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
const sizeFormatted = size.toFixed(decimal_places);
const unit = units[unitIndex];
return `${sizeFormatted} ${unit}`;
}
exports.readableFileSize = readableFileSize;
function getCurrentGitBranch() {
return __awaiter(this, void 0, void 0, function* () {
try {
const git = (0, simple_git_1.simpleGit)();
const branch = yield git.branch();
return branch.current.split('/').slice(-1)[0];
}
catch (error) {
console.error(error);
return '';
}
});
}
exports.getCurrentGitBranch = getCurrentGitBranch;
function markdownToAnsiStyle(markdown) {
const ansi_lines = [];
for (let line of markdown.split('\n')) {
let line_style = '';
if (line.startsWith('# ')) {
line_style += chalk_1.default.bold;
}
else {
const bold_regex = /(?<!\*)\*(\*?[^*]+\*?)\*(?!\*)/g;
line.replace(bold_regex, (_, p1) => {
line_style += chalk_1.default.bold;
return `${p1}${chalk_1.default.reset}`;
});
}
if (line.match(/^#+ /) !== null) {
line_style += chalk_1.default.cyan;
line = line.replace(/^#+ /, '');
}
ansi_lines.push(`${line_style}${line}${chalk_1.default.reset}`);
}
return ansi_lines.join('\n');
}
exports.markdownToAnsiStyle = markdownToAnsiStyle;
//# sourceMappingURL=utils.js.map