llmxml
Version:
Convert between markdown and LLM-friendly pseudo-XML
147 lines (143 loc) • 4.54 kB
text/typescript
interface LLMXMLOptions {
/** Default threshold for fuzzy matching (0-1, default: 0.7) */
defaultFuzzyThreshold?: number;
/** Warning emission level (default: 'all') */
warningLevel?: 'all' | 'none' | 'ambiguous-only';
/** Include hlevel attribute in XML output (default: false) */
includeHlevel?: boolean;
/** Include title attribute in XML output (default: false) */
includeTitle?: boolean;
/** Include both hlevel and title attributes (default: false) */
verbose?: boolean;
/** Tag name format (default: 'PascalCase') */
tagFormat?: 'snake_case' | 'SCREAMING_SNAKE' | 'camelCase' | 'PascalCase' | 'UPPERCASE';
}
interface Warning {
/** Warning code */
code: string;
/** Human-readable message */
message: string;
/** Additional context */
details?: any;
}
type ErrorCode = 'SECTION_NOT_FOUND' | 'PARSE_ERROR' | 'INVALID_FORMAT' | 'INVALID_LEVEL' | 'INVALID_SECTION_OPTIONS';
/**
* Custom error class for LLMXML-specific errors
*/
declare class LLMXMLError extends Error {
code: ErrorCode;
details?: unknown | undefined;
/**
* Creates a new LLMXMLError
*
* @param message - Human-readable error message
* @param code - Error code identifying the type of error
* @param details - Optional additional error context
*/
constructor(message: string, code: ErrorCode, details?: unknown | undefined);
}
/**
* Options for section extraction
*/
interface GetSectionOptions {
/** Only match headers at this level */
level?: number;
/** Require exact matches */
exact?: boolean;
/** Include subsections in result */
includeNested?: boolean;
/** Minimum fuzzy match score (0-1) */
fuzzyThreshold?: number;
}
/**
* Heading information from document
*/
interface HeadingInfo {
/** Heading title */
title: string;
/** Heading level (1-6) */
level: number;
/** Hierarchical path to this heading */
path: string[];
}
/**
* Main LLMXML class for converting between Markdown and LLM-friendly pseudo-XML
*/
declare class LLMXML {
private options;
private llmSerializer;
private warningHandlers;
/**
* Creates a new LLMXML instance
*
* @param options - Configuration options
*/
constructor(options?: LLMXMLOptions);
/**
* Convert Markdown to LLM-XML format
*/
toXML(markdown: string): Promise<string>;
/**
* Convert LLM-XML to Markdown format
*/
toMarkdown(xml: string): Promise<string>;
/**
* Extract a single section from the document
*/
getSection(content: string, title: string, options?: GetSectionOptions): Promise<string>;
/**
* Extract multiple matching sections from the document
*/
getSections(content: string, title: string, options?: GetSectionOptions): Promise<string[]>;
/**
* Extract all headings from the document
*
* @param content - Markdown or LLM-XML content
* @returns Array of heading information
*/
getHeadings(content: string): Promise<HeadingInfo[]>;
/**
* Extract headings from AST
*
* @private
* @param ast - Document AST
* @returns Array of heading information
*/
private extractHeadingsFromAST;
/**
* Register a warning handler
*/
onWarning(handler: (warning: Warning) => void): void;
/**
* Remove a previously registered warning handler
*
* @param event - The event type ('warning')
* @param handler - The handler to remove
*/
off(_event: 'warning', handler: (warning: Warning) => void): void;
/**
* Emit a warning to all registered handlers
*
* @param warning - The warning to emit
*/
protected emitWarning(warning: Warning): void;
/**
* Convert Markdown to XML and back to Markdown, preserving all structure
*/
roundTrip(markdown: string): Promise<string>;
/**
* Normalize markdown formatting with consistent spacing
*
* @param markdown - The markdown content to normalize
* @returns Normalized markdown with consistent spacing
*/
normalizeMarkdown(markdown: string): Promise<string>;
}
/**
* Create a new LLMXML instance with the given options
*
* @param options - Configuration options
* @returns A new LLMXML instance
*/
declare function createLLMXML(options?: LLMXMLOptions): LLMXML;
export { type ErrorCode, type GetSectionOptions, type HeadingInfo, LLMXML, LLMXMLError, type LLMXMLOptions, type Warning, createLLMXML };