UNPKG

mlld

Version:

mlld: llm scripting language

77 lines (75 loc) 2.15 kB
import { __name } from './chunk-NJQT543K.mjs'; // interpreter/utils/json-auto-parser.ts function tryParseJson(value) { const trimmed = value.trim(); if (!trimmed) { return { isJson: false, value: trimmed, originalValue: value }; } const firstChar = trimmed[0]; const lastChar = trimmed[trimmed.length - 1]; const looksLikeJson = ( // Object: starts with { and ends with } firstChar === "{" && lastChar === "}" || // Array: starts with [ and ends with ] firstChar === "[" && lastChar === "]" || // String: starts and ends with quotes firstChar === '"' && lastChar === '"' || // Primitive values trimmed === "true" || trimmed === "false" || trimmed === "null" || // Number (simple check) /^-?\d+\.?\d*$/.test(trimmed) ); if (!looksLikeJson) { return { isJson: false, value: trimmed, originalValue: value }; } const integerPattern = /^-?\d+$/; if (integerPattern.test(trimmed)) { const parsedInt = Number(trimmed); if (!Number.isSafeInteger(parsedInt)) { return { isJson: false, value: trimmed, originalValue: value }; } } try { const parsed = JSON.parse(trimmed); return { isJson: true, value: parsed, originalValue: value }; } catch (error) { return { isJson: false, value: trimmed, originalValue: value }; } } __name(tryParseJson, "tryParseJson"); function shouldAutoParseJson() { const envVar = process.env.MLLD_AUTO_PARSE_JSON; if (envVar !== void 0) { return envVar.toLowerCase() === "true"; } return true; } __name(shouldAutoParseJson, "shouldAutoParseJson"); function processCommandOutput(output, enableAutoParse) { const shouldParse = enableAutoParse ?? shouldAutoParseJson(); if (!shouldParse) { return output; } const result = tryParseJson(output); return result.value; } __name(processCommandOutput, "processCommandOutput"); export { processCommandOutput, shouldAutoParseJson, tryParseJson }; //# sourceMappingURL=json-auto-parser-DOBRNARE.mjs.map //# sourceMappingURL=json-auto-parser-DOBRNARE.mjs.map