UNPKG

promptly-ai

Version:

A universal template-based prompt management system for LLM applications

166 lines 6.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateLoader = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const gray_matter_1 = __importDefault(require("gray-matter")); class TemplateLoader { constructor(templatesPath, cacheEnabled = true) { this.templateCache = new Map(); this.templatesPath = templatesPath; this.cacheEnabled = cacheEnabled; } /** * Load and parse a template file */ async loadTemplate(templateName) { // Check cache first if (this.cacheEnabled && this.templateCache.has(templateName)) { return this.templateCache.get(templateName); } const templatePath = path.join(this.templatesPath, `${templateName}.md`); try { const fileContent = await fs.readFile(templatePath, 'utf-8'); const parsed = (0, gray_matter_1.default)(fileContent); // Parse the content to extract system prompt and message sequence const content = parsed.content.trim(); const sections = content.split(/^#\s+/m).filter(Boolean); let systemPrompt; const messages = []; for (const section of sections) { const lines = section.split('\n'); const headerLine = lines[0].trim(); const body = lines.slice(1).join('\n').trim(); // Parse header for role and optional condition // Format: "User Prompt [if condition]" or "Assistant Prompt 2 [if someVariable]" const conditionMatch = headerLine.match(/^(.+?)\s*\[if\s+(.+?)\]$/i); const header = conditionMatch ? conditionMatch[1].trim().toLowerCase() : headerLine.toLowerCase(); const condition = conditionMatch ? conditionMatch[2].trim() : undefined; if (header === 'system prompt' || header === 'system') { systemPrompt = body; } else if (header === 'user prompt' || header === 'user') { messages.push({ role: 'user', content: body, condition }); } else if (header === 'assistant prompt' || header === 'assistant') { messages.push({ role: 'assistant', content: body, condition }); } else if (header.startsWith('user prompt') || header.startsWith('user ')) { // Support numbered user prompts like "User Prompt 1", "User 2", etc. messages.push({ role: 'user', content: body, condition }); } else if (header.startsWith('assistant prompt') || header.startsWith('assistant ')) { // Support numbered assistant prompts like "Assistant Prompt 1", "Assistant 2", etc. messages.push({ role: 'assistant', content: body, condition }); } else { // If no headers found, treat entire content as single user prompt messages.push({ role: 'user', content: content }); break; } } // If no sections found, treat entire content as single user prompt if (!systemPrompt && messages.length === 0) { messages.push({ role: 'user', content: content }); } const template = { metadata: { name: templateName, description: parsed.data.description || `Template for ${templateName}`, model: parsed.data.model, temperature: parsed.data.temperature || 0.7, maxTokens: parsed.data.maxTokens || 1000, maxThinkingTokens: parsed.data.maxThinkingTokens, version: parsed.data.version || '1.0.0', }, content: { systemPrompt, messages, }, }; // Cache the template if (this.cacheEnabled) { this.templateCache.set(templateName, template); } return template; } catch (error) { throw new Error(`Failed to load template "${templateName}": ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Get list of available templates */ async getAvailableTemplates() { try { const files = await fs.readdir(this.templatesPath); return files .filter((file) => file.endsWith('.md')) .map((file) => file.replace('.md', '')); } catch (error) { return []; } } /** * Clear the template cache */ clearCache() { this.templateCache.clear(); } /** * Check if template exists */ async templateExists(templateName) { const templatePath = path.join(this.templatesPath, `${templateName}.md`); try { await fs.access(templatePath); return true; } catch { return false; } } } exports.TemplateLoader = TemplateLoader; //# sourceMappingURL=template-loader.js.map