mlld
Version:
mlld: llm scripting language
1 lines • 5.85 kB
Source Map (JSON)
{"version":3,"sources":["../interpreter/utils/json-auto-parser.ts"],"names":["tryParseJson","value","trimmed","trim","isJson","originalValue","firstChar","lastChar","length","looksLikeJson","test","integerPattern","parsedInt","Number","isSafeInteger","parsed","JSON","parse","error","shouldAutoParseJson","envVar","process","env","MLLD_AUTO_PARSE_JSON","undefined","toLowerCase","processCommandOutput","output","enableAutoParse","shouldParse","result"],"mappings":";;;AAsBO,SAASA,aAAaC,KAAa,EAAA;AACxC,EAAMC,MAAAA,OAAAA,GAAUD,MAAME,IAAI,EAAA;AAI1B,EAAA,IAAI,CAACD,OAAS,EAAA;AACZ,IAAO,OAAA;MAAEE,MAAQ,EAAA,KAAA;MAAOH,KAAOC,EAAAA,OAAAA;MAASG,aAAeJ,EAAAA;AAAM,KAAA;AAC/D;AAGA,EAAMK,MAAAA,SAAAA,GAAYJ,QAAQ,CAAA,CAAA;AAC1B,EAAA,MAAMK,QAAWL,GAAAA,OAAAA,CAAQA,OAAQM,CAAAA,MAAAA,GAAS,CAAA,CAAA;AAE1C,EAAMC,MAAAA,aAAAA;;AAEHH,IAAAA,SAAAA,KAAc,OAAOC,QAAa,KAAA,GAAA;AAElCD,IAAAA,SAAAA,KAAc,OAAOC,QAAa,KAAA,GAAA;AAElCD,IAAAA,SAAAA,KAAc,OAAOC,QAAa,KAAA,GAAA;IAEnCL,OAAY,KAAA,MAAA,IACZA,OAAY,KAAA,OAAA,IACZA,OAAY,KAAA,MAAA;AAEZ,IAAA,eAAA,CAAgBQ,KAAKR,OAAAA;;AAEvB,EAAA,IAAI,CAACO,aAAe,EAAA;AAClB,IAAO,OAAA;MAAEL,MAAQ,EAAA,KAAA;MAAOH,KAAOC,EAAAA,OAAAA;MAASG,aAAeJ,EAAAA;AAAM,KAAA;AAC/D;AAGA,EAAA,MAAMU,cAAiB,GAAA,SAAA;AACvB,EAAIA,IAAAA,cAAAA,CAAeD,IAAKR,CAAAA,OAAAA,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;AAChB;AATgByB,MAAAA,CAAAA,oBAAAA,EAAAA,sBAAAA,CAAAA","file":"json-auto-parser-DOBRNARE.mjs","sourcesContent":["/**\n * Utilities for automatically detecting and parsing JSON output from commands\n * \n * This helps improve the developer experience by automatically parsing JSON\n * strings into objects/arrays when commands return valid JSON.\n */\n\nexport interface JsonParseResult {\n /** Whether the input was successfully parsed as JSON */\n isJson: boolean;\n /** The parsed value (if JSON) or original value (if not JSON) */\n value: any;\n /** The original string value */\n originalValue: string;\n}\n\n/**\n * Detects if a string contains valid JSON and attempts to parse it\n * \n * @param value - The string value to check and potentially parse\n * @returns JsonParseResult with parsing information\n */\nexport function tryParseJson(value: string): JsonParseResult {\n const trimmed = value.trim();\n \n // Quick heuristic checks to avoid expensive JSON.parse calls\n // on obvious non-JSON strings\n if (!trimmed) {\n return { isJson: false, value: trimmed, originalValue: value };\n }\n \n // Check if it looks like JSON (starts with { [ \" or is a primitive)\n const firstChar = trimmed[0];\n const lastChar = trimmed[trimmed.length - 1];\n \n const looksLikeJson = \n // Object: starts with { and ends with }\n (firstChar === '{' && lastChar === '}') ||\n // Array: starts with [ and ends with ]\n (firstChar === '[' && lastChar === ']') ||\n // String: starts and ends with quotes\n (firstChar === '\"' && lastChar === '\"') ||\n // Primitive values\n trimmed === 'true' ||\n trimmed === 'false' ||\n trimmed === 'null' ||\n // Number (simple check)\n /^-?\\d+\\.?\\d*$/.test(trimmed);\n\n if (!looksLikeJson) {\n return { isJson: false, value: trimmed, originalValue: value };\n }\n\n // Prevent auto-parsing integers that exceed Number's safe range\n const integerPattern = /^-?\\d+$/;\n if (integerPattern.test(trimmed)) {\n const parsedInt = Number(trimmed);\n if (!Number.isSafeInteger(parsedInt)) {\n return { isJson: false, value: trimmed, originalValue: value };\n }\n }\n\n try {\n const parsed = JSON.parse(trimmed);\n return { isJson: true, value: parsed, originalValue: value };\n } catch (error) {\n // Not valid JSON, return original\n return { isJson: false, value: trimmed, originalValue: value };\n }\n}\n\n/**\n * Checks if automatic JSON parsing should be enabled\n * \n * This can be controlled via:\n * 1. Environment variable MLLD_AUTO_PARSE_JSON (true/false)\n * 2. Configuration option (future)\n * \n * @returns true if auto-parsing should be enabled\n */\nexport function shouldAutoParseJson(): boolean {\n const envVar = process.env.MLLD_AUTO_PARSE_JSON;\n \n // Default to true (enabled) unless explicitly disabled\n if (envVar !== undefined) {\n return envVar.toLowerCase() === 'true';\n }\n \n return true; // Default enabled\n}\n\n/**\n * Processes command output with optional JSON auto-parsing\n * \n * @param output - The raw command output\n * @param enableAutoParse - Whether to attempt JSON parsing (defaults to shouldAutoParseJson())\n * @returns The processed output (parsed JSON object/array or original string)\n */\nexport function processCommandOutput(output: string, enableAutoParse?: boolean): any {\n const shouldParse = enableAutoParse ?? shouldAutoParseJson();\n\n if (!shouldParse) {\n return output;\n }\n\n const result = tryParseJson(output);\n return result.value;\n}\n"]}