json.cjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parsePartialJson = exports.parseJsonMarkdown = void 0;
  4. function parseJsonMarkdown(s, parser = parsePartialJson) {
  5. // eslint-disable-next-line no-param-reassign
  6. s = s.trim();
  7. const firstFenceIndex = s.indexOf("```");
  8. if (firstFenceIndex === -1) {
  9. return parser(s);
  10. }
  11. let contentAfterFence = s.substring(firstFenceIndex + 3);
  12. if (contentAfterFence.startsWith("json\n")) {
  13. contentAfterFence = contentAfterFence.substring(5);
  14. }
  15. else if (contentAfterFence.startsWith("json")) {
  16. contentAfterFence = contentAfterFence.substring(4);
  17. }
  18. else if (contentAfterFence.startsWith("\n")) {
  19. contentAfterFence = contentAfterFence.substring(1);
  20. }
  21. const closingFenceIndex = contentAfterFence.indexOf("```");
  22. let finalContent = contentAfterFence;
  23. if (closingFenceIndex !== -1) {
  24. finalContent = contentAfterFence.substring(0, closingFenceIndex);
  25. }
  26. return parser(finalContent.trim());
  27. }
  28. exports.parseJsonMarkdown = parseJsonMarkdown;
  29. // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
  30. // MIT License
  31. function parsePartialJson(s) {
  32. // If the input is undefined, return null to indicate failure.
  33. if (typeof s === "undefined") {
  34. return null;
  35. }
  36. // Attempt to parse the string as-is.
  37. try {
  38. return JSON.parse(s);
  39. }
  40. catch (error) {
  41. // Pass
  42. }
  43. // Initialize variables.
  44. let new_s = "";
  45. const stack = [];
  46. let isInsideString = false;
  47. let escaped = false;
  48. // Process each character in the string one at a time.
  49. for (let char of s) {
  50. if (isInsideString) {
  51. if (char === '"' && !escaped) {
  52. isInsideString = false;
  53. }
  54. else if (char === "\n" && !escaped) {
  55. char = "\\n"; // Replace the newline character with the escape sequence.
  56. }
  57. else if (char === "\\") {
  58. escaped = !escaped;
  59. }
  60. else {
  61. escaped = false;
  62. }
  63. }
  64. else {
  65. if (char === '"') {
  66. isInsideString = true;
  67. escaped = false;
  68. }
  69. else if (char === "{") {
  70. stack.push("}");
  71. }
  72. else if (char === "[") {
  73. stack.push("]");
  74. }
  75. else if (char === "}" || char === "]") {
  76. if (stack && stack[stack.length - 1] === char) {
  77. stack.pop();
  78. }
  79. else {
  80. // Mismatched closing character; the input is malformed.
  81. return null;
  82. }
  83. }
  84. }
  85. // Append the processed character to the new string.
  86. new_s += char;
  87. }
  88. // If we're still inside a string at the end of processing,
  89. // we need to close the string.
  90. if (isInsideString) {
  91. new_s += '"';
  92. }
  93. // Close any remaining open structures in the reverse order that they were opened.
  94. for (let i = stack.length - 1; i >= 0; i -= 1) {
  95. new_s += stack[i];
  96. }
  97. // Attempt to parse the modified string as JSON.
  98. try {
  99. return JSON.parse(new_s);
  100. }
  101. catch (error) {
  102. // If we still can't parse the string as JSON, return null to indicate failure.
  103. return null;
  104. }
  105. }
  106. exports.parsePartialJson = parsePartialJson;