UNPKG

prompt-version-manager

Version:

Centralized prompt management system for Human Behavior AI agents

192 lines 7.45 kB
"use strict"; /** * Prompt Version Control System * Centralized management of AI agent prompts across HB projects */ 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 __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromptUtils = exports.CommonPrompts = exports.WorkflowAgentPrompts = exports.promptManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); // Types will be exported from interfaces/prompt-types class PromptManager { promptCache = new Map(); baseDir; constructor(baseDir) { this.baseDir = baseDir || __dirname; } /** * Load a prompt template from markdown file */ loadPromptFromFile(filePath) { const fullPath = path.join(this.baseDir, filePath); if (this.promptCache.has(fullPath)) { return this.promptCache.get(fullPath); } try { const content = fs.readFileSync(fullPath, 'utf-8'); // Extract the prompt template from markdown const templateMatch = content.match(/```\n([\s\S]*?)\n```/); if (!templateMatch) { throw new Error(`No prompt template found in ${filePath}`); } const template = templateMatch[1]; this.promptCache.set(fullPath, template); return template; } catch (error) { throw new Error(`Failed to load prompt from ${filePath}: ${error}`); } } /** * Replace template variables with actual values */ interpolateTemplate(template, variables) { let result = template; for (const [key, value] of Object.entries(variables)) { const placeholder = `{${key}}`; result = result.replace(new RegExp(placeholder, 'g'), String(value)); } // Check for unresolved placeholders const unresolvedPlaceholders = result.match(/\{[^}]+\}/g); if (unresolvedPlaceholders) { console.warn(`Warning: Unresolved placeholders found: ${unresolvedPlaceholders.join(', ')}`); } return result; } /** * Get workflow detection prompt */ getWorkflowDetectionPrompt(input) { const template = this.loadPromptFromFile('workflow-agent/workflow-agent.md'); return this.interpolateTemplate(template, input); } /** * Get review agent prompt */ getReviewAgentPrompt(input) { const template = this.loadPromptFromFile('workflow-agent/review-agent.md'); return this.interpolateTemplate(template, input); } /** * Get judge agent prompt */ getJudgeAgentPrompt(input) { const template = this.loadPromptFromFile('workflow-agent/judge-agent.md'); return this.interpolateTemplate(template, input); } /** * Get delete agent prompt */ getDeleteAgentPrompt(input) { const template = this.loadPromptFromFile('workflow-agent/delete-agent.md'); return this.interpolateTemplate(template, input); } /** * Get deduplication agent prompt */ getDeduplicationPrompt(input) { const template = this.loadPromptFromFile('workflow-agent/deduplication-agent.md'); return this.interpolateTemplate(template, input); } /** * Get base templates */ getBaseTemplate(templateName) { const template = this.loadPromptFromFile('common/base-templates.md'); // Extract specific template section const sectionRegex = new RegExp(`### ${templateName}[\\s\\S]*?(?=###|$)`); const match = template.match(sectionRegex); if (!match) { throw new Error(`Template section '${templateName}' not found`); } return match[0]; } /** * Clear the prompt cache (useful for development) */ clearCache() { this.promptCache.clear(); } /** * List all available prompts */ listAvailablePrompts() { const prompts = []; const scanDirectory = (dir, prefix = '') => { const fullDir = path.join(this.baseDir, dir); if (!fs.existsSync(fullDir)) return; const files = fs.readdirSync(fullDir); for (const file of files) { const filePath = path.join(fullDir, file); const stat = fs.statSync(filePath); if (stat.isDirectory()) { scanDirectory(path.join(dir, file), `${prefix}${file}/`); } else if (file.endsWith('.md')) { prompts.push(`${prefix}${file}`); } } }; scanDirectory('workflow-agent', 'workflow-agent/'); scanDirectory('common', 'common/'); return prompts; } } // Create singleton instance exports.promptManager = new PromptManager(); // Export individual prompt getters for convenience exports.WorkflowAgentPrompts = { getWorkflowDetectionPrompt: exports.promptManager.getWorkflowDetectionPrompt.bind(exports.promptManager), getReviewAgentPrompt: exports.promptManager.getReviewAgentPrompt.bind(exports.promptManager), getJudgeAgentPrompt: exports.promptManager.getJudgeAgentPrompt.bind(exports.promptManager), getDeleteAgentPrompt: exports.promptManager.getDeleteAgentPrompt.bind(exports.promptManager), getDeduplicationPrompt: exports.promptManager.getDeduplicationPrompt.bind(exports.promptManager), }; exports.CommonPrompts = { getBaseTemplate: exports.promptManager.getBaseTemplate.bind(exports.promptManager), }; // Export types __exportStar(require("./interfaces/prompt-types"), exports); // For debugging and development exports.PromptUtils = { clearCache: exports.promptManager.clearCache.bind(exports.promptManager), listAvailablePrompts: exports.promptManager.listAvailablePrompts.bind(exports.promptManager), }; //# sourceMappingURL=index.js.map