UNPKG

auto-gpt-ts

Version:

my take of Auto-GPT in typescript

58 lines 2.43 kB
"use strict"; 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.executeCommand = exports.getCommand = void 0; const mathjs_1 = require("mathjs"); const command_1 = require("./command"); function getCommand(responseJson) { var _a; try { if (!responseJson.hasOwnProperty("command")) { throw `Error: Missing 'command' object in JSON`; } const command = responseJson["command"]; if (!(0, mathjs_1.isObject)(command)) { throw `Error: 'command' object is not a JSON object`; } if (!command.hasOwnProperty("name")) { throw `Error: Missing 'name' property in 'command' object`; } const commandName = command["name"]; const args = (_a = command["args"]) !== null && _a !== void 0 ? _a : {}; return { commandName: commandName, args, }; } catch (error) { throw `Error: ${error}`; } } exports.getCommand = getCommand; function executeCommand(commandName, args) { return __awaiter(this, void 0, void 0, function* () { try { const commandRegistry = new command_1.CommandRegistry(); const command = commandRegistry.getCommand(commandName); if (command) { return yield command.call(...Object.values(args)); } return (`Unknown command '${commandName}'. Please refer to the 'COMMANDS'` + ` list for available commands and only respond in the specified JSON` + ` format.`); } catch (e) { return `Error: ${String(e)}`; } }); } exports.executeCommand = executeCommand; //# sourceMappingURL=execute-command.js.map