@langchain/core 0.1.55 → 0.1.57

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.
Files changed (54) hide show
  1. package/dist/agents.d.ts +1 -1
  2. package/dist/caches.d.ts +1 -1
  3. package/dist/language_models/chat_models.d.ts +14 -3
  4. package/dist/messages/ai.cjs +213 -0
  5. package/dist/messages/ai.d.ts +37 -0
  6. package/dist/messages/ai.js +207 -0
  7. package/dist/messages/base.cjs +212 -0
  8. package/dist/messages/base.d.ts +137 -0
  9. package/dist/messages/base.js +201 -0
  10. package/dist/messages/chat.cjs +71 -0
  11. package/dist/messages/chat.d.ts +28 -0
  12. package/dist/messages/chat.js +66 -0
  13. package/dist/messages/function.cjs +46 -0
  14. package/dist/messages/function.d.ts +24 -0
  15. package/dist/messages/function.js +41 -0
  16. package/dist/messages/human.cjs +36 -0
  17. package/dist/messages/human.d.ts +17 -0
  18. package/dist/messages/human.js +31 -0
  19. package/dist/messages/index.cjs +27 -633
  20. package/dist/messages/index.d.ts +8 -273
  21. package/dist/messages/index.js +10 -611
  22. package/dist/messages/system.cjs +36 -0
  23. package/dist/messages/system.d.ts +17 -0
  24. package/dist/messages/system.js +31 -0
  25. package/dist/messages/tool.cjs +101 -0
  26. package/dist/messages/tool.d.ts +101 -0
  27. package/dist/messages/tool.js +95 -0
  28. package/dist/messages/utils.cjs +172 -0
  29. package/dist/messages/utils.d.ts +31 -0
  30. package/dist/messages/utils.js +163 -0
  31. package/dist/output_parsers/json.cjs +6 -93
  32. package/dist/output_parsers/json.d.ts +2 -2
  33. package/dist/output_parsers/json.js +2 -88
  34. package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +79 -13
  35. package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +18 -0
  36. package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +75 -12
  37. package/dist/output_parsers/transform.cjs +7 -6
  38. package/dist/output_parsers/transform.d.ts +1 -1
  39. package/dist/output_parsers/transform.js +3 -2
  40. package/dist/prompts/chat.cjs +1 -1
  41. package/dist/prompts/chat.js +1 -1
  42. package/dist/utils/function_calling.cjs +18 -6
  43. package/dist/utils/function_calling.d.ts +2 -1
  44. package/dist/utils/function_calling.js +16 -5
  45. package/dist/utils/json.cjs +93 -0
  46. package/dist/utils/json.d.ts +2 -0
  47. package/dist/utils/json.js +88 -0
  48. package/dist/utils/testing/index.cjs +3 -0
  49. package/dist/utils/testing/index.js +3 -0
  50. package/messages/tool.cjs +1 -0
  51. package/messages/tool.d.cts +1 -0
  52. package/messages/tool.d.ts +1 -0
  53. package/messages/tool.js +1 -0
  54. package/package.json +14 -1
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parsePartialJson = exports.parseJsonMarkdown = exports.JsonOutputParser = void 0;
3
+ exports.parseJsonMarkdown = exports.parsePartialJson = exports.JsonOutputParser = void 0;
4
4
  const transform_js_1 = require("./transform.cjs");
5
5
  const json_patch_js_1 = require("../utils/json_patch.cjs");
6
+ const json_js_1 = require("../utils/json.cjs");
7
+ Object.defineProperty(exports, "parseJsonMarkdown", { enumerable: true, get: function () { return json_js_1.parseJsonMarkdown; } });
8
+ Object.defineProperty(exports, "parsePartialJson", { enumerable: true, get: function () { return json_js_1.parsePartialJson; } });
6
9
  /**
7
10
  * Class for parsing the output of an LLM into a JSON object.
8
11
  */
@@ -37,103 +40,13 @@ class JsonOutputParser extends transform_js_1.BaseCumulativeTransformOutputParse
37
40
  // This should actually return Partial<T>, but there's no way
38
41
  // to specify emitted chunks as instances separate from the main output type.
39
42
  async parsePartialResult(generations) {
40
- return parseJsonMarkdown(generations[0].text);
43
+ return (0, json_js_1.parseJsonMarkdown)(generations[0].text);
41
44
  }
42
45
  async parse(text) {
43
- return parseJsonMarkdown(text, JSON.parse);
46
+ return (0, json_js_1.parseJsonMarkdown)(text, JSON.parse);
44
47
  }
45
48
  getFormatInstructions() {
46
49
  return "";
47
50
  }
48
51
  }
49
52
  exports.JsonOutputParser = JsonOutputParser;
50
- function parseJsonMarkdown(s, parser = parsePartialJson) {
51
- // eslint-disable-next-line no-param-reassign
52
- s = s.trim();
53
- const match = /```(json)?(.*)```/s.exec(s);
54
- if (!match) {
55
- return parser(s);
56
- }
57
- else {
58
- return parser(match[2]);
59
- }
60
- }
61
- exports.parseJsonMarkdown = parseJsonMarkdown;
62
- // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
63
- // MIT License
64
- function parsePartialJson(s) {
65
- // If the input is undefined, return null to indicate failure.
66
- if (typeof s === "undefined") {
67
- return null;
68
- }
69
- // Attempt to parse the string as-is.
70
- try {
71
- return JSON.parse(s);
72
- }
73
- catch (error) {
74
- // Pass
75
- }
76
- // Initialize variables.
77
- let new_s = "";
78
- const stack = [];
79
- let isInsideString = false;
80
- let escaped = false;
81
- // Process each character in the string one at a time.
82
- for (let char of s) {
83
- if (isInsideString) {
84
- if (char === '"' && !escaped) {
85
- isInsideString = false;
86
- }
87
- else if (char === "\n" && !escaped) {
88
- char = "\\n"; // Replace the newline character with the escape sequence.
89
- }
90
- else if (char === "\\") {
91
- escaped = !escaped;
92
- }
93
- else {
94
- escaped = false;
95
- }
96
- }
97
- else {
98
- if (char === '"') {
99
- isInsideString = true;
100
- escaped = false;
101
- }
102
- else if (char === "{") {
103
- stack.push("}");
104
- }
105
- else if (char === "[") {
106
- stack.push("]");
107
- }
108
- else if (char === "}" || char === "]") {
109
- if (stack && stack[stack.length - 1] === char) {
110
- stack.pop();
111
- }
112
- else {
113
- // Mismatched closing character; the input is malformed.
114
- return null;
115
- }
116
- }
117
- }
118
- // Append the processed character to the new string.
119
- new_s += char;
120
- }
121
- // If we're still inside a string at the end of processing,
122
- // we need to close the string.
123
- if (isInsideString) {
124
- new_s += '"';
125
- }
126
- // Close any remaining open structures in the reverse order that they were opened.
127
- for (let i = stack.length - 1; i >= 0; i -= 1) {
128
- new_s += stack[i];
129
- }
130
- // Attempt to parse the modified string as JSON.
131
- try {
132
- return JSON.parse(new_s);
133
- }
134
- catch (error) {
135
- // If we still can't parse the string as JSON, return null to indicate failure.
136
- return null;
137
- }
138
- }
139
- exports.parsePartialJson = parsePartialJson;
@@ -1,6 +1,7 @@
1
1
  import { BaseCumulativeTransformOutputParser } from "./transform.js";
2
2
  import { Operation } from "../utils/json_patch.js";
3
3
  import { ChatGeneration, Generation } from "../outputs.js";
4
+ import { parseJsonMarkdown, parsePartialJson } from "../utils/json.js";
4
5
  /**
5
6
  * Class for parsing the output of an LLM into a JSON object.
6
7
  */
@@ -13,5 +14,4 @@ export declare class JsonOutputParser<T extends Record<string, any> = Record<str
13
14
  parse(text: string): Promise<T>;
14
15
  getFormatInstructions(): string;
15
16
  }
16
- export declare function parseJsonMarkdown(s: string, parser?: typeof parsePartialJson): any;
17
- export declare function parsePartialJson(s: string): any;
17
+ export { parsePartialJson, parseJsonMarkdown };
@@ -1,5 +1,6 @@
1
1
  import { BaseCumulativeTransformOutputParser } from "./transform.js";
2
2
  import { compare } from "../utils/json_patch.js";
3
+ import { parseJsonMarkdown, parsePartialJson } from "../utils/json.js";
3
4
  /**
4
5
  * Class for parsing the output of an LLM into a JSON object.
5
6
  */
@@ -43,91 +44,4 @@ export class JsonOutputParser extends BaseCumulativeTransformOutputParser {
43
44
  return "";
44
45
  }
45
46
  }
46
- export function parseJsonMarkdown(s, parser = parsePartialJson) {
47
- // eslint-disable-next-line no-param-reassign
48
- s = s.trim();
49
- const match = /```(json)?(.*)```/s.exec(s);
50
- if (!match) {
51
- return parser(s);
52
- }
53
- else {
54
- return parser(match[2]);
55
- }
56
- }
57
- // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
58
- // MIT License
59
- export function parsePartialJson(s) {
60
- // If the input is undefined, return null to indicate failure.
61
- if (typeof s === "undefined") {
62
- return null;
63
- }
64
- // Attempt to parse the string as-is.
65
- try {
66
- return JSON.parse(s);
67
- }
68
- catch (error) {
69
- // Pass
70
- }
71
- // Initialize variables.
72
- let new_s = "";
73
- const stack = [];
74
- let isInsideString = false;
75
- let escaped = false;
76
- // Process each character in the string one at a time.
77
- for (let char of s) {
78
- if (isInsideString) {
79
- if (char === '"' && !escaped) {
80
- isInsideString = false;
81
- }
82
- else if (char === "\n" && !escaped) {
83
- char = "\\n"; // Replace the newline character with the escape sequence.
84
- }
85
- else if (char === "\\") {
86
- escaped = !escaped;
87
- }
88
- else {
89
- escaped = false;
90
- }
91
- }
92
- else {
93
- if (char === '"') {
94
- isInsideString = true;
95
- escaped = false;
96
- }
97
- else if (char === "{") {
98
- stack.push("}");
99
- }
100
- else if (char === "[") {
101
- stack.push("]");
102
- }
103
- else if (char === "}" || char === "]") {
104
- if (stack && stack[stack.length - 1] === char) {
105
- stack.pop();
106
- }
107
- else {
108
- // Mismatched closing character; the input is malformed.
109
- return null;
110
- }
111
- }
112
- }
113
- // Append the processed character to the new string.
114
- new_s += char;
115
- }
116
- // If we're still inside a string at the end of processing,
117
- // we need to close the string.
118
- if (isInsideString) {
119
- new_s += '"';
120
- }
121
- // Close any remaining open structures in the reverse order that they were opened.
122
- for (let i = stack.length - 1; i >= 0; i -= 1) {
123
- new_s += stack[i];
124
- }
125
- // Attempt to parse the modified string as JSON.
126
- try {
127
- return JSON.parse(new_s);
128
- }
129
- catch (error) {
130
- // If we still can't parse the string as JSON, return null to indicate failure.
131
- return null;
132
- }
133
- }
47
+ export { parsePartialJson, parseJsonMarkdown };
@@ -1,7 +1,74 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonOutputKeyToolsParser = exports.JsonOutputToolsParser = void 0;
3
+ exports.JsonOutputKeyToolsParser = exports.JsonOutputToolsParser = exports.makeInvalidToolCall = exports.convertLangChainToolCallToOpenAI = exports.parseToolCall = void 0;
4
4
  const base_js_1 = require("../base.cjs");
5
+ const json_js_1 = require("../json.cjs");
6
+ function parseToolCall(
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ rawToolCall, options) {
9
+ if (rawToolCall.function === undefined) {
10
+ return undefined;
11
+ }
12
+ let functionArgs;
13
+ if (options?.partial) {
14
+ try {
15
+ functionArgs = (0, json_js_1.parsePartialJson)(rawToolCall.function.arguments ?? "{}");
16
+ }
17
+ catch (e) {
18
+ return undefined;
19
+ }
20
+ }
21
+ else {
22
+ try {
23
+ functionArgs = JSON.parse(rawToolCall.function.arguments);
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ }
26
+ catch (e) {
27
+ throw new base_js_1.OutputParserException([
28
+ `Function "${rawToolCall.function.name}" arguments:`,
29
+ ``,
30
+ rawToolCall.function.arguments,
31
+ ``,
32
+ `are not valid JSON.`,
33
+ `Error: ${e.message}`,
34
+ ].join("\n"));
35
+ }
36
+ }
37
+ const parsedToolCall = {
38
+ name: rawToolCall.function.name,
39
+ args: functionArgs,
40
+ };
41
+ if (options?.returnId) {
42
+ parsedToolCall.id = rawToolCall.id;
43
+ }
44
+ return parsedToolCall;
45
+ }
46
+ exports.parseToolCall = parseToolCall;
47
+ function convertLangChainToolCallToOpenAI(toolCall) {
48
+ if (toolCall.id === undefined) {
49
+ throw new Error(`All OpenAI tool calls must have an "id" field.`);
50
+ }
51
+ return {
52
+ id: toolCall.id,
53
+ type: "function",
54
+ function: {
55
+ name: toolCall.name,
56
+ arguments: JSON.stringify(toolCall.args),
57
+ },
58
+ };
59
+ }
60
+ exports.convertLangChainToolCallToOpenAI = convertLangChainToolCallToOpenAI;
61
+ function makeInvalidToolCall(
62
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
+ rawToolCall, errorMsg) {
64
+ return {
65
+ name: rawToolCall.function?.name,
66
+ args: rawToolCall.function?.arguments,
67
+ id: rawToolCall.id,
68
+ error: errorMsg,
69
+ };
70
+ }
71
+ exports.makeInvalidToolCall = makeInvalidToolCall;
5
72
  /**
6
73
  * Class for parsing the output of a tool-calling LLM into a JSON object.
7
74
  */
@@ -45,28 +112,27 @@ class JsonOutputToolsParser extends base_js_1.BaseLLMOutputParser {
45
112
  const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
46
113
  const parsedToolCalls = [];
47
114
  for (const toolCall of clonedToolCalls) {
48
- if (toolCall.function !== undefined) {
49
- // @ts-expect-error name and arguemnts are defined by Object.defineProperty
50
- const parsedToolCall = {
51
- type: toolCall.function.name,
52
- args: JSON.parse(toolCall.function.arguments),
53
- };
54
- if (this.returnId) {
55
- parsedToolCall.id = toolCall.id;
56
- }
115
+ const parsedToolCall = parseToolCall(toolCall, { partial: true });
116
+ if (parsedToolCall !== undefined) {
57
117
  // backward-compatibility with previous
58
118
  // versions of Langchain JS, which uses `name` and `arguments`
59
- Object.defineProperty(parsedToolCall, "name", {
119
+ // @ts-expect-error name and arguemnts are defined by Object.defineProperty
120
+ const backwardsCompatibleToolCall = {
121
+ type: parsedToolCall.name,
122
+ args: parsedToolCall.args,
123
+ id: parsedToolCall.id,
124
+ };
125
+ Object.defineProperty(backwardsCompatibleToolCall, "name", {
60
126
  get() {
61
127
  return this.type;
62
128
  },
63
129
  });
64
- Object.defineProperty(parsedToolCall, "arguments", {
130
+ Object.defineProperty(backwardsCompatibleToolCall, "arguments", {
65
131
  get() {
66
132
  return this.args;
67
133
  },
68
134
  });
69
- parsedToolCalls.push(parsedToolCall);
135
+ parsedToolCalls.push(backwardsCompatibleToolCall);
70
136
  }
71
137
  }
72
138
  return parsedToolCalls;
@@ -1,6 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { ChatGeneration } from "../../outputs.js";
3
3
  import { BaseLLMOutputParser } from "../base.js";
4
+ import { InvalidToolCall, ToolCall } from "../../messages/tool.js";
4
5
  export type ParsedToolCall = {
5
6
  id?: string;
6
7
  type: string;
@@ -14,6 +15,23 @@ export type JsonOutputToolsParserParams = {
14
15
  /** Whether to return the tool call id. */
15
16
  returnId?: boolean;
16
17
  };
18
+ export declare function parseToolCall(rawToolCall: Record<string, any>, options: {
19
+ returnId?: boolean;
20
+ partial: true;
21
+ }): ToolCall | undefined;
22
+ export declare function parseToolCall(rawToolCall: Record<string, any>, options?: {
23
+ returnId?: boolean;
24
+ partial?: false;
25
+ }): ToolCall;
26
+ export declare function convertLangChainToolCallToOpenAI(toolCall: ToolCall): {
27
+ id: string;
28
+ type: string;
29
+ function: {
30
+ name: string;
31
+ arguments: string;
32
+ };
33
+ };
34
+ export declare function makeInvalidToolCall(rawToolCall: Record<string, any>, errorMsg?: string): InvalidToolCall;
17
35
  /**
18
36
  * Class for parsing the output of a tool-calling LLM into a JSON object.
19
37
  */
@@ -1,4 +1,68 @@
1
1
  import { BaseLLMOutputParser, OutputParserException } from "../base.js";
2
+ import { parsePartialJson } from "../json.js";
3
+ export function parseToolCall(
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ rawToolCall, options) {
6
+ if (rawToolCall.function === undefined) {
7
+ return undefined;
8
+ }
9
+ let functionArgs;
10
+ if (options?.partial) {
11
+ try {
12
+ functionArgs = parsePartialJson(rawToolCall.function.arguments ?? "{}");
13
+ }
14
+ catch (e) {
15
+ return undefined;
16
+ }
17
+ }
18
+ else {
19
+ try {
20
+ functionArgs = JSON.parse(rawToolCall.function.arguments);
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ }
23
+ catch (e) {
24
+ throw new OutputParserException([
25
+ `Function "${rawToolCall.function.name}" arguments:`,
26
+ ``,
27
+ rawToolCall.function.arguments,
28
+ ``,
29
+ `are not valid JSON.`,
30
+ `Error: ${e.message}`,
31
+ ].join("\n"));
32
+ }
33
+ }
34
+ const parsedToolCall = {
35
+ name: rawToolCall.function.name,
36
+ args: functionArgs,
37
+ };
38
+ if (options?.returnId) {
39
+ parsedToolCall.id = rawToolCall.id;
40
+ }
41
+ return parsedToolCall;
42
+ }
43
+ export function convertLangChainToolCallToOpenAI(toolCall) {
44
+ if (toolCall.id === undefined) {
45
+ throw new Error(`All OpenAI tool calls must have an "id" field.`);
46
+ }
47
+ return {
48
+ id: toolCall.id,
49
+ type: "function",
50
+ function: {
51
+ name: toolCall.name,
52
+ arguments: JSON.stringify(toolCall.args),
53
+ },
54
+ };
55
+ }
56
+ export function makeInvalidToolCall(
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ rawToolCall, errorMsg) {
59
+ return {
60
+ name: rawToolCall.function?.name,
61
+ args: rawToolCall.function?.arguments,
62
+ id: rawToolCall.id,
63
+ error: errorMsg,
64
+ };
65
+ }
2
66
  /**
3
67
  * Class for parsing the output of a tool-calling LLM into a JSON object.
4
68
  */
@@ -42,28 +106,27 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
42
106
  const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
43
107
  const parsedToolCalls = [];
44
108
  for (const toolCall of clonedToolCalls) {
45
- if (toolCall.function !== undefined) {
46
- // @ts-expect-error name and arguemnts are defined by Object.defineProperty
47
- const parsedToolCall = {
48
- type: toolCall.function.name,
49
- args: JSON.parse(toolCall.function.arguments),
50
- };
51
- if (this.returnId) {
52
- parsedToolCall.id = toolCall.id;
53
- }
109
+ const parsedToolCall = parseToolCall(toolCall, { partial: true });
110
+ if (parsedToolCall !== undefined) {
54
111
  // backward-compatibility with previous
55
112
  // versions of Langchain JS, which uses `name` and `arguments`
56
- Object.defineProperty(parsedToolCall, "name", {
113
+ // @ts-expect-error name and arguemnts are defined by Object.defineProperty
114
+ const backwardsCompatibleToolCall = {
115
+ type: parsedToolCall.name,
116
+ args: parsedToolCall.args,
117
+ id: parsedToolCall.id,
118
+ };
119
+ Object.defineProperty(backwardsCompatibleToolCall, "name", {
57
120
  get() {
58
121
  return this.type;
59
122
  },
60
123
  });
61
- Object.defineProperty(parsedToolCall, "arguments", {
124
+ Object.defineProperty(backwardsCompatibleToolCall, "arguments", {
62
125
  get() {
63
126
  return this.args;
64
127
  },
65
128
  });
66
- parsedToolCalls.push(parsedToolCall);
129
+ parsedToolCalls.push(backwardsCompatibleToolCall);
67
130
  }
68
131
  }
69
132
  return parsedToolCalls;
@@ -2,9 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseCumulativeTransformOutputParser = exports.BaseTransformOutputParser = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
- const index_js_1 = require("../messages/index.cjs");
5
+ const base_js_2 = require("../messages/base.cjs");
6
+ const utils_js_1 = require("../messages/utils.cjs");
6
7
  const outputs_js_1 = require("../outputs.cjs");
7
- const index_js_2 = require("../utils/@cfworker/json-schema/index.cjs");
8
+ const index_js_1 = require("../utils/@cfworker/json-schema/index.cjs");
8
9
  /**
9
10
  * Class to parse the output of an LLM call that also allows streaming inputs.
10
11
  */
@@ -63,7 +64,7 @@ class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
63
64
  throw new Error("Cannot handle non-string output.");
64
65
  }
65
66
  let chunkGen;
66
- if ((0, index_js_1.isBaseMessageChunk)(chunk)) {
67
+ if ((0, base_js_2.isBaseMessageChunk)(chunk)) {
67
68
  if (typeof chunk.content !== "string") {
68
69
  throw new Error("Cannot handle non-string message output.");
69
70
  }
@@ -72,12 +73,12 @@ class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
72
73
  text: chunk.content,
73
74
  });
74
75
  }
75
- else if ((0, index_js_1.isBaseMessage)(chunk)) {
76
+ else if ((0, base_js_2.isBaseMessage)(chunk)) {
76
77
  if (typeof chunk.content !== "string") {
77
78
  throw new Error("Cannot handle non-string message output.");
78
79
  }
79
80
  chunkGen = new outputs_js_1.ChatGenerationChunk({
80
- message: chunk.toChunk(),
81
+ message: (0, utils_js_1.convertToChunk)(chunk),
81
82
  text: chunk.content,
82
83
  });
83
84
  }
@@ -93,7 +94,7 @@ class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
93
94
  const parsed = await this.parsePartialResult([accGen]);
94
95
  if (parsed !== undefined &&
95
96
  parsed !== null &&
96
- !(0, index_js_2.deepCompareStrict)(parsed, prevParsed)) {
97
+ !(0, index_js_1.deepCompareStrict)(parsed, prevParsed)) {
97
98
  if (this.diff) {
98
99
  yield this._diff(prevParsed, parsed);
99
100
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseOutputParser } from "./base.js";
2
- import { type BaseMessage } from "../messages/index.js";
2
+ import { type BaseMessage } from "../messages/base.js";
3
3
  import type { BaseCallbackConfig } from "../callbacks/manager.js";
4
4
  import { type Generation, type ChatGeneration } from "../outputs.js";
5
5
  /**
@@ -1,5 +1,6 @@
1
1
  import { BaseOutputParser } from "./base.js";
2
- import { isBaseMessage, isBaseMessageChunk, } from "../messages/index.js";
2
+ import { isBaseMessage, isBaseMessageChunk, } from "../messages/base.js";
3
+ import { convertToChunk } from "../messages/utils.js";
3
4
  import { GenerationChunk, ChatGenerationChunk, } from "../outputs.js";
4
5
  import { deepCompareStrict } from "../utils/@cfworker/json-schema/index.js";
5
6
  /**
@@ -73,7 +74,7 @@ export class BaseCumulativeTransformOutputParser extends BaseTransformOutputPars
73
74
  throw new Error("Cannot handle non-string message output.");
74
75
  }
75
76
  chunkGen = new ChatGenerationChunk({
76
- message: chunk.toChunk(),
77
+ message: convertToChunk(chunk),
77
78
  text: chunk.content,
78
79
  });
79
80
  }
@@ -477,7 +477,7 @@ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
477
477
  return { image_url: item.image_url };
478
478
  }
479
479
  else {
480
- throw new Error("Invalid message content");
480
+ return item;
481
481
  }
482
482
  });
483
483
  }
@@ -466,7 +466,7 @@ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
466
466
  return { image_url: item.image_url };
467
467
  }
468
468
  else {
469
- throw new Error("Invalid message content");
469
+ return item;
470
470
  }
471
471
  });
472
472
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertToOpenAITool = exports.convertToOpenAIFunction = void 0;
3
+ exports.isStructuredTool = exports.convertToOpenAITool = exports.convertToOpenAIFunction = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
5
  /**
6
6
  * Formats a `StructuredTool` instance into a format that is compatible
@@ -22,10 +22,22 @@ exports.convertToOpenAIFunction = convertToOpenAIFunction;
22
22
  * function to convert the schema of the `StructuredTool` into a JSON
23
23
  * schema, which is then used as the parameters for the OpenAI tool.
24
24
  */
25
- function convertToOpenAITool(tool) {
26
- return {
27
- type: "function",
28
- function: convertToOpenAIFunction(tool),
29
- };
25
+ function convertToOpenAITool(
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ tool) {
28
+ if (isStructuredTool(tool)) {
29
+ return {
30
+ type: "function",
31
+ function: convertToOpenAIFunction(tool),
32
+ };
33
+ }
34
+ return tool;
30
35
  }
31
36
  exports.convertToOpenAITool = convertToOpenAITool;
37
+ function isStructuredTool(
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ tool) {
40
+ return (tool !== undefined &&
41
+ Array.isArray(tool.lc_namespace));
42
+ }
43
+ exports.isStructuredTool = isStructuredTool;
@@ -13,4 +13,5 @@ export declare function convertToOpenAIFunction(tool: StructuredToolInterface):
13
13
  * function to convert the schema of the `StructuredTool` into a JSON
14
14
  * schema, which is then used as the parameters for the OpenAI tool.
15
15
  */
16
- export declare function convertToOpenAITool(tool: StructuredToolInterface): ToolDefinition;
16
+ export declare function convertToOpenAITool(tool: StructuredToolInterface | Record<string, any>): ToolDefinition;
17
+ export declare function isStructuredTool(tool?: StructuredToolInterface | Record<string, any>): tool is StructuredToolInterface;
@@ -18,9 +18,20 @@ export function convertToOpenAIFunction(tool) {
18
18
  * function to convert the schema of the `StructuredTool` into a JSON
19
19
  * schema, which is then used as the parameters for the OpenAI tool.
20
20
  */
21
- export function convertToOpenAITool(tool) {
22
- return {
23
- type: "function",
24
- function: convertToOpenAIFunction(tool),
25
- };
21
+ export function convertToOpenAITool(
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ tool) {
24
+ if (isStructuredTool(tool)) {
25
+ return {
26
+ type: "function",
27
+ function: convertToOpenAIFunction(tool),
28
+ };
29
+ }
30
+ return tool;
31
+ }
32
+ export function isStructuredTool(
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ tool) {
35
+ return (tool !== undefined &&
36
+ Array.isArray(tool.lc_namespace));
26
37
  }