UNPKG

auto-gpt-ts

Version:

my take of Auto-GPT in typescript

144 lines 6.45 kB
"use strict"; 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.CommandDecorator = exports.CommandRegistry = exports.Command = void 0; require("reflect-metadata"); const singelton_1 = require("../singelton"); const logging_1 = require("../logging"); const AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command"; class Command extends logging_1.Loggable { constructor(name, description, method, signature = "", enabled = true, aliases = [], disabledReason = null) { super(name); this.name = name; this.description = description; this.method = method; this.enabled = enabled; this.aliases = aliases; this.disabledReason = disabledReason; this.signature = signature || this.method.toString(); } call(...args) { this.logger.debug(`executing: ${this.name}('${args.join("', '")}')`); if (!this.enabled) { return `Command '${this.name}' is disabled: ${this.disabledReason}`; } return this.method(...args); } toString() { return `${this.name}: ${this.description}, args: ${this.signature}`; } } exports.Command = Command; let CommandRegistry = class CommandRegistry { constructor() { this.commands = {}; } getAllCommands() { return Object.values(this.commands); } register(cmd) { this.commands[cmd.name] = cmd; } unregister(commandName) { if (commandName in this.commands) { delete this.commands[commandName]; } else { throw new Error(`Command '${commandName}' not found in registry.`); } } getCommand(name) { let command = this.commands[name]; if (!command) { command = Object.values(this.commands).find(cmd => cmd.aliases.includes(name)); } return command; } call(command_name, ...args) { if (!(command_name in this.commands)) { throw new Error(`Command '${command_name}' not found in registry.`); } let command = this.commands[command_name]; return command.call(...args); } commandPrompt() { let commandsList = []; for (let [idx, cmd] of Object.entries(this.commands)) { commandsList.push(`${parseInt(idx) + 1}. ${cmd.toString()}`); } return commandsList.join("\n"); } importCommands(module_name) { return __awaiter(this, void 0, void 0, function* () { const path = require.resolve(module_name); // console.log(`Importing commands from ${path}`); let module = yield Promise.resolve(`${path}`).then(s => __importStar(require(s))); for (let attr_name in module) { let attr = module[attr_name]; if (Reflect.hasMetadata(AUTO_GPT_COMMAND_IDENTIFIER, attr)) { this.register(attr.command); } // Register command classes else if (typeof attr === "function" && attr.prototype instanceof Command && attr !== Command) { let cmdInstance = new attr(); this.register(cmdInstance); } } }); } }; CommandRegistry = __decorate([ singelton_1.Singleton ], CommandRegistry); exports.CommandRegistry = CommandRegistry; exports.CommandDecorator = (({ name, description, signature, enabled, disabledReason, register, aliases }) => (target, _) => { const func = target[name].bind(target); const cmd = new Command(name, description, func, signature !== null && signature !== void 0 ? signature : func.toString(), enabled !== null && enabled !== void 0 ? enabled : true, aliases !== null && aliases !== void 0 ? aliases : [], disabledReason !== null && disabledReason !== void 0 ? disabledReason : null); target.command = cmd; // target[AUTO_GPT_COMMAND_IDENTIFIER] = true; Reflect.defineMetadata(AUTO_GPT_COMMAND_IDENTIFIER, true, target); if (register) { new CommandRegistry().register(cmd); } return target; }); //# sourceMappingURL=command.js.map