@langchain/core 0.3.50 → 0.3.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,13 +4,26 @@ exports.parsePartialJson = exports.parseJsonMarkdown = void 0;
4
4
  function parseJsonMarkdown(s, parser = parsePartialJson) {
5
5
  // eslint-disable-next-line no-param-reassign
6
6
  s = s.trim();
7
- const match = /```(json)?(.*)```/s.exec(s);
8
- if (!match) {
7
+ const firstFenceIndex = s.indexOf("```");
8
+ if (firstFenceIndex === -1) {
9
9
  return parser(s);
10
10
  }
11
- else {
12
- return parser(match[2]);
11
+ let contentAfterFence = s.substring(firstFenceIndex + 3);
12
+ if (contentAfterFence.startsWith("json\n")) {
13
+ contentAfterFence = contentAfterFence.substring(5);
13
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());
14
27
  }
15
28
  exports.parseJsonMarkdown = parseJsonMarkdown;
16
29
  // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
@@ -1,13 +1,26 @@
1
1
  export function parseJsonMarkdown(s, parser = parsePartialJson) {
2
2
  // eslint-disable-next-line no-param-reassign
3
3
  s = s.trim();
4
- const match = /```(json)?(.*)```/s.exec(s);
5
- if (!match) {
4
+ const firstFenceIndex = s.indexOf("```");
5
+ if (firstFenceIndex === -1) {
6
6
  return parser(s);
7
7
  }
8
- else {
9
- return parser(match[2]);
8
+ let contentAfterFence = s.substring(firstFenceIndex + 3);
9
+ if (contentAfterFence.startsWith("json\n")) {
10
+ contentAfterFence = contentAfterFence.substring(5);
10
11
  }
12
+ else if (contentAfterFence.startsWith("json")) {
13
+ contentAfterFence = contentAfterFence.substring(4);
14
+ }
15
+ else if (contentAfterFence.startsWith("\n")) {
16
+ contentAfterFence = contentAfterFence.substring(1);
17
+ }
18
+ const closingFenceIndex = contentAfterFence.indexOf("```");
19
+ let finalContent = contentAfterFence;
20
+ if (closingFenceIndex !== -1) {
21
+ finalContent = contentAfterFence.substring(0, closingFenceIndex);
22
+ }
23
+ return parser(finalContent.trim());
11
24
  }
12
25
  // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
13
26
  // MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.50",
3
+ "version": "0.3.51",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {