UNPKG

cmte

Version:

Design by Committee™ except it's just you and LLMs

116 lines (109 loc) 3.25 kB
import { createLLMXML } from 'llmxml'; import { logger } from './logger.js'; // Create the LLMXML instance with default configuration const llmxml = createLLMXML({ // Default threshold for fuzzy matching (0-1) defaultFuzzyThreshold: 0.7, // Disable warnings by default warningLevel: 'none', // Control XML attribute output includeTitle: false, includeHlevel: false, verbose: false, // Tag name formatting tagFormat: 'PascalCase' }); // Only register warning handler if logging is enabled if (process.argv.includes('--log')) { llmxml.onWarning(warning => { logger.warn('LLMXML warning', warning); }); } /** * Pre-processes rendered markdown to handle potentially problematic syntax for llmxml. * @param {string} markdown * @returns {string} Processed markdown */ function preprocessMarkdownForXml(markdown) { if (!markdown) return ''; // Replace empty code blocks ```\n``` with a placeholder to avoid parse errors const processed = markdown.replace(/```\n```/g, '```\n[intentionally left blank]\n```'); // Add more replacements here if needed for other edge cases return processed; } /** * Convert Markdown content to LLM-XML * @param markdown Markdown content to convert * @returns XML-structured content */ export async function toXML(markdown) { try { const processedMarkdown = preprocessMarkdownForXml(markdown); if (processedMarkdown !== markdown) { logger.debug('Preprocessed Markdown before XML conversion.'); } return await llmxml.toXML(processedMarkdown); } catch (error) { logger.error('Error converting Markdown to XML', { error }); throw new Error(`LLMXML conversion error: ${error.message}`); } } /** * Convert LLM-XML content back to Markdown * @param xml XML content to convert * @returns Markdown-structured content */ export async function toMarkdown(xml) { try { return await llmxml.toMarkdown(xml); } catch (error) { logger.error('Error converting XML to Markdown', { error }); throw new Error(`LLMXML conversion error: ${error.message}`); } } /** * Extract a section from Markdown content * @param content Markdown content * @param sectionName Section name to extract * @param options Extraction options * @returns Extracted section content */ export async function getSection(content, sectionName, options) { try { return await llmxml.getSection(content, sectionName, options); } catch (error) { logger.error('Error extracting section', { error, sectionName }); throw new Error(`Section extraction error: ${error.message}`); } } /** * Extract multiple sections from Markdown content * @param content Markdown content * @param sectionName Section name pattern to extract * @param options Extraction options * @returns Array of matching sections */ export async function getSections(content, sectionName, options) { try { return await llmxml.getSections(content, sectionName, options); } catch (error) { logger.error('Error extracting sections', { error, sectionName }); throw new Error(`Sections extraction error: ${error.message}`); } } export default { toXML, toMarkdown, getSection, getSections };