cortexweaver
Version:
CortexWeaver is a command-line interface (CLI) tool that orchestrates a swarm of specialized AI agents, powered by Claude Code and Gemini CLI, to assist in software development. It transforms a high-level project plan (plan.md) into a series of coordinate
185 lines • 6.35 kB
JavaScript
"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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateEngine = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
/**
* Simple template engine for processing prompt templates
* Supports Handlebars-like syntax: {{variable}} and {{#if condition}}
*/
class TemplateEngine {
constructor(templateDir = './prompts') {
this.templateCache = new Map();
this.templateDir = templateDir;
}
/**
* Render a template with the given context
*/
async render(templatePath, context) {
const template = await this.loadTemplate(templatePath);
return this.processTemplate(template, context);
}
/**
* Load template from file (with caching)
*/
async loadTemplate(templatePath) {
const fullPath = path.isAbsolute(templatePath)
? templatePath
: path.join(this.templateDir, templatePath);
if (this.templateCache.has(fullPath)) {
return this.templateCache.get(fullPath);
}
try {
const template = await fs.readFile(fullPath, 'utf-8');
this.templateCache.set(fullPath, template);
return template;
}
catch (error) {
throw new Error(`Failed to load template ${fullPath}: ${error.message}`);
}
}
/**
* Process template with context data
*/
processTemplate(template, context) {
let result = template;
// Process conditional blocks first
result = this.processConditionals(result, context);
// Process loops
result = this.processLoops(result, context);
// Process simple variable substitutions
result = this.processVariables(result, context);
return result;
}
/**
* Process {{#if condition}} blocks
*/
processConditionals(template, context) {
const ifRegex = /\{\{#if\s+([^}]+)\}\}([\s\S]*?)\{\{\/if\}\}/g;
return template.replace(ifRegex, (match, condition, content) => {
const value = this.resolveValue(condition.trim(), context);
return this.isTruthy(value) ? content : '';
});
}
/**
* Process {{#each array}} blocks
*/
processLoops(template, context) {
const eachRegex = /\{\{#each\s+([^}]+)\}\}([\s\S]*?)\{\{\/each\}\}/g;
return template.replace(eachRegex, (match, arrayPath, content) => {
const array = this.resolveValue(arrayPath.trim(), context);
if (!Array.isArray(array)) {
return '';
}
return array.map(item => {
// Create context for each item
const itemContext = { ...context, ...item };
return this.processVariables(content, itemContext);
}).join('');
});
}
/**
* Process {{variable}} substitutions
*/
processVariables(template, context) {
const variableRegex = /\{\{([^#\/][^}]*)\}\}/g;
return template.replace(variableRegex, (match, path) => {
const value = this.resolveValue(path.trim(), context);
return this.formatValue(value);
});
}
/**
* Resolve a dot-notation path to a value in the context
*/
resolveValue(path, context) {
const parts = path.split('.');
let current = context;
for (const part of parts) {
if (current === null || current === undefined) {
return '';
}
current = current[part];
}
return current;
}
/**
* Check if a value is truthy for conditionals
*/
isTruthy(value) {
if (value === null || value === undefined)
return false;
if (typeof value === 'boolean')
return value;
if (typeof value === 'number')
return value !== 0;
if (typeof value === 'string')
return value.length > 0;
if (Array.isArray(value))
return value.length > 0;
if (typeof value === 'object')
return Object.keys(value).length > 0;
return !!value;
}
/**
* Format a value for output
*/
formatValue(value) {
if (value === null || value === undefined) {
return '';
}
if (typeof value === 'object') {
// For objects and arrays, try to format them nicely
if (Array.isArray(value)) {
return value.map(item => this.formatValue(item)).join(', ');
}
try {
return JSON.stringify(value, null, 2);
}
catch {
return String(value);
}
}
return String(value);
}
/**
* Clear template cache
*/
clearCache() {
this.templateCache.clear();
}
}
exports.TemplateEngine = TemplateEngine;
//# sourceMappingURL=template-engine.js.map