/** Options for {@link extractJson} and {@link tryExtractJson}. */ interface ExtractOptions { /** * Apply conservative, string-aware repairs before parsing — currently the * removal of trailing commas, which models emit often. Never rewrites string * contents. Default `true`. */ repair?: boolean; /** * Restrict which top-level JSON value to accept: an `'object'`, an `'array'`, * or `'any'` (the default). */ expect?: 'object' | 'array' | 'any'; } /** The result of {@link tryExtractJson}. */ type ExtractResult = { found: true; value: T; } | { found: false; value?: undefined; }; /** Thrown by {@link extractJson} when no JSON value can be recovered. */ declare class JsonExtractionError extends Error { /** The original text that no JSON could be extracted from. */ readonly text: string; constructor(message: string, /** The original text that no JSON could be extracted from. */ text: string); } /** * Extract a JSON value from LLM output without throwing. * * Strips `` / `` reasoning blocks, prefers fenced ```json * code blocks, then scans for the first balanced object/array that parses * (applying conservative repair). Returns `{ found: false }` if nothing parses. * * @example * ```ts * const r = tryExtractJson<{ score: number }>('...\n{"score":7}'); * if (r.found) console.log(r.value.score); // 7 * ``` */ declare function tryExtractJson(text: string, options?: ExtractOptions): ExtractResult; /** * Extract a JSON value from LLM output, throwing {@link JsonExtractionError} * if none can be recovered. See {@link tryExtractJson} for the algorithm. */ declare function extractJson(text: string, options?: ExtractOptions): T; declare function stripReasoning(text: string): string; declare function fencedBlocks(text: string): string[]; /** * Find the substrings of complete, balanced JSON objects/arrays in `text`, * in document order. String-aware and delimiter-aware: braces and brackets * inside JSON strings do not affect nesting, and `[` must close with `]`. */ declare function balancedSpans(text: string): string[]; /** * Remove trailing commas (`{"a":1,}` → `{"a":1}`, `[1,2,]` → `[1,2]`), which * models emit frequently. String-aware: a comma inside a string value is never * touched, so this can only ever fix structure, never corrupt content. */ declare function removeTrailingCommas(json: string): string; export { type ExtractOptions, type ExtractResult, JsonExtractionError, balancedSpans, extractJson, fencedBlocks, removeTrailingCommas, stripReasoning, tryExtractJson };