retort-js
Version:
Intuitive, production-ready prompt chaining in Javascript
60 lines (59 loc) • 2.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.retort = void 0;
const logger_1 = require("./logger");
const conversation_1 = require("./conversation");
const id_1 = require("./id");
const run_1 = require("./run");
const url_1 = __importDefault(require("url"));
var runHasBeenTriggered = false;
function retort(chatFunction) {
let retortId = (0, id_1.id)("retort");
let _run = (...values) => {
const conversation = new conversation_1.RetortConversation();
async function runInner() {
return chatFunction(conversation, ...values);
}
let executing = runInner();
let scriptInProgress = {
retortId: retortId,
$: conversation,
completionPromise: executing,
};
return scriptInProgress;
};
let returnedModule = {
retortId: retortId,
retortHash: (0, logger_1.createHash)(chatFunction.toString()),
_run: _run,
retortType: "retort",
};
if (!runHasBeenTriggered) {
// Make sure we only run the script once
runHasBeenTriggered = true;
setTimeout(async () => {
// Get the running script from argv
const mainModuleFilename = process.argv[1];
if (!mainModuleFilename) {
return;
}
else {
// Check the script to see if it ends in *.rt.js, *.rt.cjs, or *.rt.mjs
const isRetortScript = mainModuleFilename.match(/\.rt\.(js|cjs|mjs)\/*$/);
if (!isRetortScript) {
return;
}
// Convert the filename to a file url using url module
const mainModuleUrl = url_1.default.pathToFileURL(mainModuleFilename);
// Run the script
const retort = await import(mainModuleUrl.toString());
(0, run_1.run)(retort);
}
}, 0);
}
return returnedModule;
}
exports.retort = retort;