UNPKG

tprompter

Version:
148 lines (147 loc) 6.6 kB
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 __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; 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()); }); }; import { Service } from 'typedi'; import { Actions } from './actions/Actions.js'; import { ArchiveService } from './archive/Archive.js'; import { countTokens } from 'gpt-tokenizer'; import { InjectLogger } from './logger/logger.decorator.js'; import { TemplateService } from './templates/PromptsService.js'; import { IO } from './utils/IO.js'; import { StdinDataReader } from './utils/StdinDataReader.js'; import { openFinder } from './utils/openFinder.js'; import { AssetsService } from './assets/AssetsService.js'; import { LLMService } from './llm/LLMService.js'; let MainController = class MainController { constructor(actions, archive, logger, templateService, io, stdinReader, assetsService, llmService) { this.actions = actions; this.archive = archive; this.logger = logger; this.templateService = templateService; this.io = io; this.stdinReader = stdinReader; this.assetsService = assetsService; this.llmService = llmService; } reportTokensCount(content) { const tokensCount = countTokens(content); this.logger.info(`Tokens count: ${tokensCount}`); } listTemplates() { return this.templateService.listTemplates(); } generateAndEvaluate(nameOrFile, after) { return __awaiter(this, void 0, void 0, function* () { const prompt = yield this.templateService.getPromptByNameOrPath(nameOrFile); if (!prompt) { throw new Error(`Prompt not found: ${nameOrFile}`); } const content = yield prompt.generate(); this.reportTokensCount(content); yield Promise.all([ this.archive.save({ description: nameOrFile, content, type: 'generate' }), this.actions.evaluate(after, content), ]); }); } getFromArchiveByIndexAndEvaluate(index, after) { return __awaiter(this, void 0, void 0, function* () { const record = yield this.archive.fromTheEnd(index); if (!record) { throw new Error('Record not found'); } this.reportTokensCount(record.content); yield this.actions.evaluate(after, record.content); }); } uninstallTemplate(name) { return __awaiter(this, void 0, void 0, function* () { return this.templateService.uninstallTemplate(name); }); } installTemplate(name, filepath) { return __awaiter(this, void 0, void 0, function* () { let content; if (filepath) { content = yield this.io.readFile(filepath); } else { content = yield this.stdinReader.readData('Enter template content'); } return this.templateService.installTemplate(name, content); }); } openTemplatesFolder() { return __awaiter(this, void 0, void 0, function* () { const folder = yield this.templateService.getTemplatesFolder(); yield openFinder(folder); }); } listAssets() { return __awaiter(this, void 0, void 0, function* () { return this.assetsService.listAvailableAssets(); }); } getAsset(name) { return __awaiter(this, void 0, void 0, function* () { const asset = yield this.assetsService.getAsset(name); if (!asset) { throw new Error(`Asset not found: ${name}`); } return asset; }); } agent(nameOrFile, after, model) { return __awaiter(this, void 0, void 0, function* () { const template = yield this.templateService.getPromptByNameOrPath(nameOrFile); if (!template) { throw new Error(`Template not found: ${nameOrFile}`); } const content = yield template.generate(); const response = yield this.llmService.agent(content, model); yield Promise.all([ this.archive.save({ description: nameOrFile, content: response, type: 'agent' }), this.actions.evaluate(after, response), ]); }); } question(question, after, model) { return __awaiter(this, void 0, void 0, function* () { const pipeContent = yield this.stdinReader.readData('', { onlyPipe: true }); const response = yield this.llmService.question(question, pipeContent, model); yield Promise.all([ this.archive.save({ description: question, content: response, type: 'question' }), this.actions.evaluate(after, response), ]); }); } }; MainController = __decorate([ Service(), __param(2, InjectLogger(MainController)), __metadata("design:paramtypes", [Actions, ArchiveService, Object, TemplateService, IO, StdinDataReader, AssetsService, LLMService]) ], MainController); export { MainController };