UNPKG

auto-gpt-ts

Version:

my take of Auto-GPT in typescript

112 lines 5.51 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 __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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.printAssistantThoughts = exports.LogCycleHandler = exports.USER_INPUT_FILE_NAME = exports.SUMMARY_FILE_NAME = exports.PROMPT_SUMMARY_FILE_NAME = exports.NEXT_ACTION_FILE_NAME = exports.CURRENT_CONTEXT_FILE_NAME = exports.FULL_MESSAGE_HISTORY_FILE_NAME = exports.DEFAULT_PREFIX = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const logging_1 = require("../logging"); const chalk_1 = __importDefault(require("chalk")); const logger = (0, logging_1.getLogger)("LogCycleHandler"); exports.DEFAULT_PREFIX = "agent"; exports.FULL_MESSAGE_HISTORY_FILE_NAME = "full_message_history.json"; exports.CURRENT_CONTEXT_FILE_NAME = "current_context.json"; exports.NEXT_ACTION_FILE_NAME = "next_action.json"; exports.PROMPT_SUMMARY_FILE_NAME = "prompt_summary.json"; exports.SUMMARY_FILE_NAME = "summary.txt"; exports.USER_INPUT_FILE_NAME = "user_input.txt"; class LogCycleHandler { constructor() { this.logCountWithinCycle = 0; } createDirectoryIfNotExists(directoryPath) { if (!fs.existsSync(directoryPath)) { fs.mkdirSync(directoryPath, { recursive: true }); } } createOuterDirectory(aiName, createdAt) { var _a; const logDirectory = "./logs"; const overwriteDebug = process.env.OVERWRITE_DEBUG === "1"; const aiNameShort = (_a = aiName === null || aiName === void 0 ? void 0 : aiName.slice(0, 15)) !== null && _a !== void 0 ? _a : exports.DEFAULT_PREFIX; const outerFolderName = overwriteDebug ? "auto_gpt" : `${createdAt}_${aiNameShort}`; const outerFolderPath = path.join(logDirectory, "DEBUG", outerFolderName); this.createDirectoryIfNotExists(outerFolderPath); return outerFolderPath; } createInnerDirectory(outerFolderPath, cycleCount) { const nestedFolderName = cycleCount.toString().padStart(3, "0"); const nestedFolderPath = path.join(outerFolderPath, nestedFolderName); this.createDirectoryIfNotExists(nestedFolderPath); return nestedFolderPath; } createNestedDirectory(aiName, createdAt, cycleCount) { const outerFolderPath = this.createOuterDirectory(aiName, createdAt); const nestedFolderPath = this.createInnerDirectory(outerFolderPath, cycleCount); return nestedFolderPath; } logCycle(aiName, createdAt, cycleCount, data, fileName) { const nestedFolderPath = this.createNestedDirectory(aiName, createdAt, cycleCount); const jsonOptions = { ensureAscii: false, spaces: 4, }; const jsonStr = JSON.stringify(data, null, jsonOptions.spaces); const logFilePath = path.join(nestedFolderPath, `${this.logCountWithinCycle}_${fileName}`); logger.debug(`Writing to ${logFilePath}, ${jsonStr.length} bytes`); fs.writeFileSync(logFilePath, jsonStr); this.logCountWithinCycle += 1; } } exports.LogCycleHandler = LogCycleHandler; function printAssistantThoughts(aiName, assistantReplyJsonValid, speakMode = false) { const logger = (0, logging_1.getLogger)(); const assistantThoughts = assistantReplyJsonValid.thoughts; const assistantThoughtsText = assistantThoughts.text; logger.info(`${aiName.toUpperCase()} \n ${chalk_1.default.yellow(`THOUGHTS:`)} ${assistantThoughtsText}`); logger.info(`${chalk_1.default.yellow(`REASONING:`)} ${assistantThoughts.reasoning || ""}`); if (assistantThoughts.plan) { let planString = ""; if (typeof assistantThoughts.plan === "string") { planString = assistantThoughts.plan; } else if (Array.isArray(assistantThoughts.plan)) { planString = assistantThoughts.plan.join("\n"); } else { planString = JSON.stringify(assistantThoughts.plan); } const lines = planString.split("\n"); logger.info(`${chalk_1.default.yellow(`PLAN:`)} \n` + lines.map((line) => `${chalk_1.default.green('-')} ${line.replace(/^-+ /, "").trim()}`).join("\n")); } logger.info(`${chalk_1.default.yellow(`CRITICISM:`)} ${assistantThoughts.criticism || ""}`); } exports.printAssistantThoughts = printAssistantThoughts; //# sourceMappingURL=log-cycle.js.map