/** * LLM JSON Extractor - Robust JSON parsing from LLM responses * * Handles common LLM output patterns: * - "Based on the analysis, here is the JSON: {...}" * - "```json\n{...}\n```" * - "Here's what I found:\n{...}" * - Pure JSON responses * * Features: * - 4-level extraction strategy (pure -> code blocks -> regex -> array) * - Input sanitization (BOM, trailing commas) * - Schema-aware correction prompts * - Required field validation * - Retry-with-correction prompting */ import { Logger } from './logger.js'; /** * Result of JSON extraction attempt */ export interface ExtractionResult { success: boolean; data: T | null; rawResponse: string; extractedJson: string | null; error: string | null; /** How the JSON was extracted */ extractionMethod: 'pure' | 'code-block' | 'regex' | 'failed'; } /** * Options for JSON extraction */ export interface ExtractionOptions { /** Required fields to validate in the parsed JSON */ requiredFields?: string[]; /** Schema for correction prompts (recommended for retry flows) */ schema?: Record; /** Logger instance */ logger?: Logger; } /** * Extract JSON from an LLM response that may contain prose * * Tries multiple extraction strategies in order: * 1. Direct parse (pure JSON) * 2. Remove markdown code blocks * 3. Find JSON object via regex (handles prose around JSON) * 4. Find JSON array via regex */ export declare function extractJson(response: string, options?: ExtractionOptions): ExtractionResult; /** * Generate a correction prompt for retry after failed JSON extraction * * @param originalPrompt - The original prompt that was sent * @param failedResponse - The failed response from the LLM * @param schema - Optional schema to show expected structure (recommended) */ export declare function generateCorrectionPrompt(originalPrompt: string, failedResponse: string, schema?: Record): string; /** * Extract required field names from a JSON schema object * * Handles common schema patterns: * - Simple objects: extracts top-level keys * - Objects with "type" descriptors: extracts keys * * @param schema - The schema object passed to analyzeStructured (any object type) * @returns Array of required field names */ export declare function extractRequiredFieldsFromSchema(schema: unknown): string[]; /** * Build a prompt that strongly encourages JSON-only output * * Uses multiple techniques: * 1. Leading instruction (most important position) * 2. Schema definition * 3. Trailing reminder */ export declare function buildJsonPrompt(context: string, schema: Record, instruction: string): string; /** * Retry wrapper that attempts extraction with correction prompts * * Uses schema-aware correction prompts for better retry success. */ export declare function extractJsonWithRetry(sendPrompt: (prompt: string) => Promise, originalPrompt: string, options?: ExtractionOptions & { maxRetries?: number; }): Promise>; /** * Create a structured output analyzer with built-in retry * * This is the recommended way to get structured JSON from LLMs. * Combines prompt building, extraction, validation, and retry. * * @param analyze - Function that sends prompt to LLM and returns response * @param schema - Expected schema (used for prompt, validation, and correction) * @param options - Configuration options */ export declare function analyzeStructuredWithRetry(analyze: (prompt: string) => Promise, prompt: string, schema: Record, options?: { maxRetries?: number; logger?: Logger; }): Promise<{ success: boolean; data: T | null; error: string | null; }>; //# sourceMappingURL=llm-json-extractor.d.ts.map