UNPKG

@meldscience/meld

Version:

pipeable one-shot prompt scripting toolkit

86 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Oneshot = void 0; const fs_1 = require("fs"); const errors_1 = require("./errors"); const config_1 = require("./config"); class Oneshot { constructor(options, provider) { this.options = options; this.provider = provider; this.systemPrompt = ''; // Resolve model alias if it exists const config = (0, config_1.loadConfig)(); this.resolvedModel = config.modelAliases?.[options.model] || options.model; } async process() { // Check if prompt file exists if (!(0, fs_1.existsSync)(this.options.promptFile)) { throw new errors_1.ToolError(`Oneshot: Prompt file not found: ${this.options.promptFile}`, 'FILE_NOT_FOUND'); } // Check if system file exists if (this.options.systemFile && !(0, fs_1.existsSync)(this.options.systemFile)) { throw new errors_1.ToolError(`Oneshot: System file not found: ${this.options.systemFile}`, 'FILE_NOT_FOUND'); } // Initialize system prompt this.systemPrompt = ''; if (this.options.system) { this.systemPrompt = this.options.system; } else if (this.options.systemFile) { try { this.systemPrompt = (0, fs_1.readFileSync)(this.options.systemFile, 'utf-8'); } catch (error) { throw new errors_1.ToolError(`Oneshot: Failed to read system file: ${error.message}`, 'FILE_READ_ERROR'); } } // Get prompt content from file let promptContent; try { promptContent = (0, fs_1.readFileSync)(this.options.promptFile, 'utf-8'); } catch (error) { throw new errors_1.ToolError(`Oneshot: Failed to read prompt: ${error.message}`, 'FILE_READ_ERROR'); } // Process variations const variations = this.options.variations || [undefined]; const iterations = this.options.iterations || 1; // Run all variations and iterations const responses = []; for (const variation of variations) { for (let i = 0; i < iterations; i++) { // Handle variation object or string let variationPrompt = ''; let variationSystem = this.systemPrompt; let variationModel = this.resolvedModel; if (typeof variation === 'string') { variationPrompt = variation; } else if (variation) { variationPrompt = variation.prompt || ''; variationSystem = variation.system || this.systemPrompt; if (variation.model) { const config = (0, config_1.loadConfig)(); variationModel = config.modelAliases?.[variation.model] || variation.model; } } const combinedPrompt = variationPrompt ? `${promptContent}\n\n${variationPrompt}` : promptContent; const response = await this.provider.sendPrompt({ model: variationModel, systemPrompt: variationSystem, userPrompt: combinedPrompt }); responses.push({ variation, systemPrompt: variationSystem, response, model: variationModel }); } } return responses; } } exports.Oneshot = Oneshot; //# sourceMappingURL=oneshot.js.map