@meldscience/meld
Version:
pipeable one-shot prompt scripting toolkit
52 lines • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Oneshot = void 0;
const fs_1 = require("fs");
const errors_1 = require("./errors");
class Oneshot {
constructor(options, provider) {
this.options = {
...options,
iterations: options.iterations || 1
};
this.provider = provider;
}
/**
* Orchestrates reading the prompt, combining with system prompt,
* iterating variations, and collecting responses.
*/
async process() {
const { promptFile, systemFile, system, variations, iterations, model } = this.options;
if (!(0, fs_1.existsSync)(promptFile)) {
throw new errors_1.ToolError(`Oneshot: Prompt file not found: ${promptFile}`, 'FILE_NOT_FOUND');
}
const userPrompt = (0, fs_1.readFileSync)(promptFile, 'utf-8');
let systemPrompt = system || '';
if (systemFile) {
if (!(0, fs_1.existsSync)(systemFile)) {
throw new errors_1.ToolError(`Oneshot: System file not found: ${systemFile}`, 'FILE_NOT_FOUND');
}
systemPrompt = (0, fs_1.readFileSync)(systemFile, 'utf-8');
}
const finalVariations = variations && variations.length > 0 ? variations : [undefined];
const resultEnvelopes = [];
for (const v of finalVariations) {
for (let i = 0; i < (iterations || 1); i++) {
const combinedUserPrompt = v ? `${userPrompt}\n\n${v}` : userPrompt;
const response = await this.provider.sendPrompt({
model,
systemPrompt,
userPrompt: combinedUserPrompt
});
resultEnvelopes.push({
variation: v,
systemPrompt,
response
});
}
}
return resultEnvelopes;
}
}
exports.Oneshot = Oneshot;
//# sourceMappingURL=oneshot.js.map