llm-code-format
Version:
Parsing and serialization of multiple code files in Markdown for LLMs
52 lines (51 loc) • 1.96 kB
TypeScript
export type StreamingParserCallbacks = {
/**
* Called when a file header is detected outside a code fence.
* @param fileName - The detected file name.
* @param format - The header format that was matched.
*/
onFileNameChange: (fileName: string, format: string) => Promise<void>;
/**
* Called for each line emitted from inside a code fence.
* @param line - A line of code from the code block.
*/
onCodeLine: (line: string) => Promise<void>;
/**
* Called for each line outside code fences that is not a file header.
* @param line - A line of text that is not code or a file header.
*/
onNonCodeLine?: (line: string) => Promise<void>;
};
export declare class StreamingMarkdownParser {
private buffer;
private insideCodeFence;
private currentFileName;
private detectedFormat;
private callbacks;
/**
* An array of regex patterns for detecting file headers.
* Currently only supports Bold Format, but can be extended in the future.
*/
private headerPatterns;
constructor(callbacks: StreamingParserCallbacks);
/**
* Processes an incoming chunk from the stream.
* Chunks are buffered until full lines (ending with '\n') are available.
* @param chunk - A string chunk from the stream.
*/
processChunk(chunk: string): Promise<void>;
/**
* Flushes any remaining content in the buffer.
* Should be called once after the stream has ended.
*/
flushRemaining(): Promise<void>;
/**
* Processes a single line.
* If the line is a code fence marker (starting with "```"), it toggles the code block state.
* When inside a code block, every line is emitted via onCodeLine.
* Outside of a code block, the line is checked against header patterns,
* and if a match is found, onFileNameChange is invoked.
* @param line - A single line of text.
*/
private processLine;
}