tprompter
Version:
```bash $ ask anything ```
106 lines (105 loc) • 4.8 kB
JavaScript
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 { runDisposableServer } from '../utils/disposableServer/index.js';
import open from 'open';
import { InjectLogger } from '../logger/logger.decorator.js';
import { file } from 'tmp-promise';
import { IO } from '../utils/IO.js';
import hljs from 'highlight.js';
import { MarkdownToTerminal } from '../md/MarkdownToTerminal.js';
export var AvailableActions;
(function (AvailableActions) {
AvailableActions["PRINT_TO_CONSOLE"] = "print";
AvailableActions["PRINT_TO_CONSOLE_RAW"] = "print_raw";
AvailableActions["OPEN_IN_CHATGPT"] = "chatgpt";
AvailableActions["COPY_TO_CLIPBOARD"] = "copy";
AvailableActions["RUN_EDITOR"] = "editor";
})(AvailableActions || (AvailableActions = {}));
let Actions = class Actions {
constructor(logger, io, m2t) {
this.logger = logger;
this.io = io;
this.m2t = m2t;
this.actionsMap = {
[AvailableActions.PRINT_TO_CONSOLE]: this.printToConsole,
[AvailableActions.PRINT_TO_CONSOLE_RAW]: this.printToConsoleRaw,
[AvailableActions.OPEN_IN_CHATGPT]: this.openInChatGPT,
[AvailableActions.COPY_TO_CLIPBOARD]: this.copyToClipboard,
[AvailableActions.RUN_EDITOR]: this.runEditor,
};
if (!hljs.listLanguages().includes('fish')) {
hljs.registerLanguage('fish', () => hljs.getLanguage('bash'));
}
}
evaluate(strategy, content) {
return __awaiter(this, void 0, void 0, function* () {
const action = this.actionsMap[strategy];
if (!action) {
throw new Error(`Unknown action: ${strategy}`);
}
this.logger.debug(`Executing action: ${strategy}`);
yield action.call(this, content);
});
}
printToConsole(content) {
return __awaiter(this, void 0, void 0, function* () {
const out = this.m2t.convert(content);
this.printToConsoleRaw(out);
});
}
printToConsoleRaw(content) {
return __awaiter(this, void 0, void 0, function* () {
process.stdout.write(content);
if (!content.endsWith('\n')) {
process.stdout.write('\n');
}
});
}
openInChatGPT(content) {
return __awaiter(this, void 0, void 0, function* () {
const server = yield runDisposableServer(content);
const gptUrl = `https://chatgpt.com/#url=${server.url}`;
open(gptUrl).catch((err) => this.logger.error(err));
});
}
copyToClipboard(content) {
return __awaiter(this, void 0, void 0, function* () {
const clipboardy = yield import('clipboardy');
yield clipboardy.default.write(content);
});
}
runEditor(content) {
return __awaiter(this, void 0, void 0, function* () {
const { path } = yield file({ postfix: '.txt' });
yield this.io.writeFile(path, content);
yield open(path);
});
}
};
Actions = __decorate([
Service(),
__param(0, InjectLogger(Actions)),
__metadata("design:paramtypes", [Object, IO,
MarkdownToTerminal])
], Actions);
export { Actions };