@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 4.31 kB
Source Map (JSON)
{"version":3,"file":"json.cjs","names":["s: string"],"sources":["../../src/utils/json.ts"],"sourcesContent":["export function parseJsonMarkdown(s: string, parser = parsePartialJson) {\n // eslint-disable-next-line no-param-reassign\n s = s.trim();\n\n const firstFenceIndex = s.indexOf(\"```\");\n if (firstFenceIndex === -1) {\n return parser(s);\n }\n\n let contentAfterFence = s.substring(firstFenceIndex + 3);\n\n if (contentAfterFence.startsWith(\"json\\n\")) {\n contentAfterFence = contentAfterFence.substring(5);\n } else if (contentAfterFence.startsWith(\"json\")) {\n contentAfterFence = contentAfterFence.substring(4);\n } else if (contentAfterFence.startsWith(\"\\n\")) {\n contentAfterFence = contentAfterFence.substring(1);\n }\n\n const closingFenceIndex = contentAfterFence.indexOf(\"```\");\n let finalContent = contentAfterFence;\n if (closingFenceIndex !== -1) {\n finalContent = contentAfterFence.substring(0, closingFenceIndex);\n }\n\n return parser(finalContent.trim());\n}\n\n// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py\n// MIT License\nexport function parsePartialJson(s: string) {\n // If the input is undefined, return null to indicate failure.\n if (typeof s === \"undefined\") {\n return null;\n }\n\n // Attempt to parse the string as-is.\n try {\n return JSON.parse(s);\n } catch {\n // Pass\n }\n\n // Initialize variables.\n let new_s = \"\";\n const stack = [];\n let isInsideString = false;\n let escaped = false;\n\n // Process each character in the string one at a time.\n for (let char of s) {\n if (isInsideString) {\n if (char === '\"' && !escaped) {\n isInsideString = false;\n } else if (char === \"\\n\" && !escaped) {\n char = \"\\\\n\"; // Replace the newline character with the escape sequence.\n } else if (char === \"\\\\\") {\n escaped = !escaped;\n } else {\n escaped = false;\n }\n } else {\n if (char === '\"') {\n isInsideString = true;\n escaped = false;\n } else if (char === \"{\") {\n stack.push(\"}\");\n } else if (char === \"[\") {\n stack.push(\"]\");\n } else if (char === \"}\" || char === \"]\") {\n if (stack && stack[stack.length - 1] === char) {\n stack.pop();\n } else {\n // Mismatched closing character; the input is malformed.\n return null;\n }\n }\n }\n\n // Append the processed character to the new string.\n new_s += char;\n }\n\n // If we're still inside a string at the end of processing,\n // we need to close the string.\n if (isInsideString) {\n new_s += '\"';\n }\n\n // Close any remaining open structures in the reverse order that they were opened.\n for (let i = stack.length - 1; i >= 0; i -= 1) {\n new_s += stack[i];\n }\n\n // Attempt to parse the modified string as JSON.\n try {\n return JSON.parse(new_s);\n } catch {\n // If we still can't parse the string as JSON, return null to indicate failure.\n return null;\n }\n}\n"],"mappings":";;AAAA,SAAgB,kBAAkBA,GAAW,SAAS,kBAAkB;CAEtE,IAAI,EAAE,MAAM;CAEZ,MAAM,kBAAkB,EAAE,QAAQ,MAAM;AACxC,KAAI,oBAAoB,GACtB,QAAO,OAAO,EAAE;CAGlB,IAAI,oBAAoB,EAAE,UAAU,kBAAkB,EAAE;AAExD,KAAI,kBAAkB,WAAW,SAAS,EACxC,oBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,OAAO,EAC7C,oBAAoB,kBAAkB,UAAU,EAAE;UACzC,kBAAkB,WAAW,KAAK,EAC3C,oBAAoB,kBAAkB,UAAU,EAAE;CAGpD,MAAM,oBAAoB,kBAAkB,QAAQ,MAAM;CAC1D,IAAI,eAAe;AACnB,KAAI,sBAAsB,IACxB,eAAe,kBAAkB,UAAU,GAAG,kBAAkB;AAGlE,QAAO,OAAO,aAAa,MAAM,CAAC;AACnC;AAID,SAAgB,iBAAiBA,GAAW;AAE1C,KAAI,OAAO,MAAM,YACf,QAAO;AAIT,KAAI;AACF,SAAO,KAAK,MAAM,EAAE;CACrB,QAAO,CAEP;CAGD,IAAI,QAAQ;CACZ,MAAM,QAAQ,CAAE;CAChB,IAAI,iBAAiB;CACrB,IAAI,UAAU;AAGd,MAAK,IAAI,QAAQ,GAAG;AAClB,MAAI,eACF,KAAI,SAAS,QAAO,CAAC,SACnB,iBAAiB;WACR,SAAS,QAAQ,C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