me-engine-one
Version:
The magic file system that regenerates entire AI universes from me.md
224 lines (181 loc) β’ 6.54 kB
JavaScript
/**
* ME Engine - The magic file system that regenerates entire AI universes
*
* This is the heart of Story-002: /me Magic File System
* Edit one file (/me/me.md) β entire AI universe transforms
*/
// Core engines
export { MeParser, ValidationError, meParser } from './lib/me-parser.js';
export { RegenerationEngine, RegenerationError, regenerationEngine } from './lib/regeneration-engine.js';
// Schema and validation
export { MeSchema, ValidationHelpers, DefaultMeConfig } from './schemas/me-schema.js';
// Main ME Engine class that orchestrates everything
export class MeEngine {
constructor(options = {}) {
this.parser = options.parser || meParser;
this.regenerator = options.regenerator || regenerationEngine;
this.logger = options.logger || console;
this.watchMode = false;
}
/**
* The main magic method - regenerate entire universe from me.md
* @param {string} meFilePath - Path to me.md file
* @returns {Object} Regeneration results
*/
async regenerateUniverse(meFilePath = '/me/me.md') {
try {
this.logger.log('π Starting universe regeneration from', meFilePath);
// Parse the magical me.md file
const config = await this.parser.parseMeFile(meFilePath);
// Regenerate everything from the configuration
const results = await this.regenerator.regenerateFromMe(meFilePath);
return {
success: true,
config,
results,
message: 'β¨ Your AI universe has been regenerated!'
};
} catch (error) {
this.logger.error('π₯ Universe regeneration failed:', error.message);
return {
success: false,
error: error.message,
details: error.errors || null,
message: 'β οΈ Regeneration failed - check your me.md file'
};
}
}
/**
* Watch me.md file for changes and auto-regenerate
* @param {string} meFilePath - Path to me.md file
* @param {Function} callback - Optional callback for changes
*/
async watchMeFile(meFilePath = '/me/me.md', callback = null) {
if (this.watchMode) {
this.logger.warn('β οΈ Already watching me.md file');
return;
}
try {
const chokidar = await import('chokidar');
this.logger.log('π Watching', meFilePath, 'for changes...');
this.watchMode = true;
const watcher = chokidar.watch(meFilePath, {
persistent: true,
ignoreInitial: true
});
watcher.on('change', async () => {
this.logger.log('π me.md changed, regenerating universe...');
const results = await this.regenerateUniverse(meFilePath);
if (callback) {
callback(results);
}
if (results.success) {
this.logger.log('β
Universe updated successfully');
} else {
this.logger.error('β Universe update failed:', results.error);
}
});
return watcher;
} catch (error) {
this.logger.error('π₯ Failed to watch me.md file:', error.message);
this.watchMode = false;
throw error;
}
}
/**
* Validate me.md without regenerating
* @param {string} meFilePath - Path to me.md file
* @returns {Object} Validation results
*/
async validateMeFile(meFilePath = '/me/me.md') {
try {
const config = await this.parser.parseMeFile(meFilePath);
return {
valid: true,
config,
message: 'β
me.md is valid'
};
} catch (error) {
return {
valid: false,
error: error.message,
details: error.errors || null,
message: 'β me.md validation failed'
};
}
}
/**
* Generate a sample me.md file
* @param {string} outputPath - Where to write the sample
* @param {Object} customization - Optional customizations
*/
async generateSampleMeFile(outputPath = '/me/me.md', customization = {}) {
const sampleConfig = {
...DefaultMeConfig,
...customization
};
const meContent = await this.generateMeFileContent(sampleConfig);
const { ensureDir, outputFile } = await import('fs-extra');
const { dirname } = await import('path');
await ensureDir(dirname(outputPath));
await outputFile(outputPath, meContent, 'utf8');
this.logger.log('π Sample me.md generated at', outputPath);
return {
path: outputPath,
config: sampleConfig,
content: meContent
};
}
/**
* Generate me.md file content from configuration
* @param {Object} config - Configuration object
* @returns {string} Markdown content with YAML frontmatter
*/
async generateMeFileContent(config) {
const { stringify } = await import('yaml');
// Extract the content section (everything after YAML)
const aboutContent = config.about || `# About Me
I'm ${config.name || 'building something amazing'} with AI. ${config.story || 'This is my story.'}
My AI agents aren't just toolsβthey're creative collaborators trained on my voice, my vision, and my drive to turn ideas into reality.
Welcome to my universe. Let's create something extraordinary.`;
// Create YAML frontmatter
const frontmatter = { ...config };
delete frontmatter.about; // Remove about from YAML since it goes in content
const yamlString = stringify(frontmatter);
return `---
${yamlString}---
${aboutContent}`;
}
/**
* Get engine status and health
*/
getStatus() {
return {
engine: 'ME Engine v1.0.0',
parser: 'active',
regenerator: 'active',
watching: this.watchMode,
timestamp: new Date().toISOString()
};
}
}
// Export singleton instance
import { meParser } from './lib/me-parser.js';
import { regenerationEngine } from './lib/regeneration-engine.js';
import { DefaultMeConfig } from './schemas/me-schema.js';
export const meEngine = new MeEngine({
parser: meParser,
regenerator: regenerationEngine
});
// Convenience method for quick universe regeneration
export async function regenerateUniverse(meFilePath = '/me/me.md') {
return await meEngine.regenerateUniverse(meFilePath);
}
// Convenience method for watching me.md changes
export async function watchMeFile(meFilePath = '/me/me.md', callback = null) {
return await meEngine.watchMeFile(meFilePath, callback);
}
// Convenience method for validation
export async function validateMeFile(meFilePath = '/me/me.md') {
return await meEngine.validateMeFile(meFilePath);
}