@vfarcic/dot-ai
Version:
AI-powered development productivity platform that enhances software development workflows through intelligent automation and AI-driven assistance
120 lines (119 loc) • 5 kB
JavaScript
;
/**
* Shared Prompt Loader
*
* Loads prompt templates from files and replaces variables using Handlebars
* Following CLAUDE.md guidelines for file-based prompts
*
* Extracted from unified-creation-session.ts to be shared across all creation workflows
* Extended to support custom base directories and file extensions
*/
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.loadPrompt = loadPrompt;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const handlebars_1 = __importDefault(require("handlebars"));
// Register custom Handlebars helpers
// Block helper for equality comparison: {{#eq a b}}...{{/eq}}
handlebars_1.default.registerHelper('eq', function (a, b, options) {
if (a === b) {
return options.fn(this);
}
else {
return options.inverse(this);
}
});
// Block helper for truthy check: {{#isTrue value}}...{{/isTrue}}
// Treats various truthy values as true (case-insensitive for strings)
// Truthy: true, "yes", "true", "1", "on" (any case)
handlebars_1.default.registerHelper('isTrue', function (value, options) {
// Handle boolean true
if (value === true) {
return options.fn(this);
}
// Handle string values (case-insensitive)
if (typeof value === 'string') {
const normalized = value.toLowerCase();
if (normalized === 'yes' || normalized === 'true' || normalized === '1' || normalized === 'on') {
return options.fn(this);
}
}
// Handle numeric 1
if (value === 1) {
return options.fn(this);
}
return options.inverse(this);
});
/**
* Load template from file and replace variables using Handlebars
*
* @param templateName - Name of the template file (without extension)
* @param variables - Key-value pairs to replace in template
* @param baseDir - Base directory (relative to project root or absolute path; default: 'prompts')
* @param fileExtension - File extension (default: '.md')
* @returns Processed template content
*
* Supports Handlebars syntax:
* - {{variable}} - Variable interpolation
* - {{#if variable}}...{{/if}} - Conditional blocks
* - {{#each array}}...{{/each}} - Iteration
*/
function loadPrompt(templateName, variables = {}, baseDir = 'prompts', fileExtension = '.md') {
try {
// Support both absolute and relative paths for baseDir
// If baseDir is absolute, use it directly; otherwise resolve relative to project root
const resolvedBaseDir = path.isAbsolute(baseDir)
? baseDir
: path.join(__dirname, '..', '..', baseDir);
const templatePath = path.join(resolvedBaseDir, `${templateName}${fileExtension}`);
const templateContent = fs.readFileSync(templatePath, 'utf8');
// Compile and execute Handlebars template
const template = handlebars_1.default.compile(templateContent);
const result = template(variables);
return result;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
const resolvedPath = path.isAbsolute(baseDir)
? path.join(baseDir, `${templateName}${fileExtension}`)
: path.join(__dirname, '..', '..', baseDir, `${templateName}${fileExtension}`);
console.error(`Failed to load template "${templateName}" from "${baseDir}" (resolved: ${resolvedPath}): ${errorMessage}`);
return `Error loading template: ${templateName}`;
}
}