/** * Output Parsing Service * * Handles parsing and extraction of structured outputs from LLM responses. * Extracted from StageExecutor to follow Single Responsibility Principle. * * Features: * - JSON extraction from markdown code blocks * - Fallback parsing for malformed JSON * - Pattern matching for value extraction * - Default value application for missing fields */ import { getLogger } from '../output/logger.js'; type Logger = ReturnType; /** * Result of parsing stage outputs */ export interface ParsedOutputs { /** Extracted output values */ outputs: Record; /** Whether parsing was successful */ success: boolean; /** Method used for extraction */ method: 'fallback' | 'json' | 'pattern'; } /** * Service for parsing LLM outputs into structured data */ export declare class OutputParsingService { private readonly logger; constructor(logger?: Logger); /** * Parse stage outputs from completion content */ parseStageOutputs(content: string, expectedOutputs: string[]): Record; /** * Apply default values for missing expected outputs */ applyDefaultValues(parsedOutputs: Record, expectedOutputs: string[]): Record; /** * Sanitize content by removing control characters and ANSI codes */ private sanitizeContent; /** * Extract JSON content from markdown code blocks * * Handles: * - Standard: ```json\n{...}\n``` * - No newline: ```json{...}``` * - No language: ```{...}``` * - Unclosed blocks: ```json{...} (no closing ```) * - Multiple code blocks (takes first JSON one) */ extractJsonFromCodeBlocks(content: string): string; /** * Get ordered list of code block patterns to try */ private getCodeBlockPatterns; /** * Try to extract JSON using a specific pattern */ private tryExtractWithPattern; /** * Try to extract from unclosed code block */ private tryExtractUnclosedBlock; /** * Check if content looks like JSON (starts with { or [) */ private looksLikeJson; /** * Fix common JSON formatting issues that LLMs often produce */ fixCommonJsonIssues(jsonContent: string): string; /** * Extract missing outputs from nested JSON blocks in the content */ private extractFromNestedJsonBlocks; /** * Process a single JSON block for missing outputs */ private processJsonBlock; /** * Extract missing outputs from a parsed object */ private extractMissingFromObject; /** * Extract missing outputs from nested objects within a parsed object */ private extractFromNestedObjects; /** * Get a sensible default value for a missing output based on its name */ private getDefaultValueForOutput; /** * Check if output name represents an object field */ private isObjectField; /** * Check if output name represents a boolean field */ private isBooleanField; /** * Check if output name represents an array field */ private isArrayField; /** * Check if output name represents a count field */ private isCountField; /** * Fallback extraction when JSON parsing fails */ private fallbackExtraction; /** * Clean content for extraction by removing control characters */ private cleanContentForExtraction; /** * Find JSON object in content. Uses balanced-brace scanning from the first `{` rather * than a greedy regex to the last `}` in the content — the latter over-captures any * trailing prose that happens to contain a stray `}` (e.g. a mention of another JSON * snippet after the real block). */ private findJsonObject; /** * Extract outputs from JSON content */ private extractOutputsFromJson; /** * Extract a single output value from search targets */ private extractSingleOutput; /** * Try to extract a value for a given key from content */ private tryExtractValue; /** * Extract value based on its type */ private extractValueByType; /** * Extract an object or array value by balanced-bracket scanning rather than a * non-greedy regex, which truncates at the first inner closing bracket of any * nested value (e.g. `{"a": {"b": 1}, "c": 2}` truncates to `{"a": {"b": 1}`, * an unbalanced fragment that fails to parse). */ private extractBalancedJsonValue; /** * Extract a string value from JSON content */ private extractStringValue; /** * Unescape JSON string escape sequences */ private unescapeJsonString; /** * Extract a JSON value (object, array, or primitive) using a regex pattern */ private extractJsonValue; /** * Fill missing outputs using pattern matching as a last resort */ private fillMissingOutputsWithPatternMatching; /** * Try to extract a value using simple pattern matching */ private tryPatternMatching; } /** * Get the singleton OutputParsingService instance */ export declare function getOutputParsingService(): OutputParsingService; /** * Reset the singleton instance (for testing) */ export declare function resetOutputParsingService(): void; export {}; //# sourceMappingURL=output-parsing.service.d.ts.map