@langchain/core 1.1.8 → 1.1.9

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @langchain/core
2
2
 
3
+ ## 1.1.9
4
+
5
+ ### Patch Changes
6
+
7
+ - [#9725](https://github.com/langchain-ai/langchainjs/pull/9725) [`56600b9`](https://github.com/langchain-ai/langchainjs/commit/56600b94f8e185f44d4288b7a9b66c55778938dd) Thanks [@Orenoid](https://github.com/Orenoid)! - fix(langchain): update merge logic for numeric values in `mergeDicts`
8
+
9
+ - [#9736](https://github.com/langchain-ai/langchainjs/pull/9736) [`dc5c2ac`](https://github.com/langchain-ai/langchainjs/commit/dc5c2ac00f86dd2feeba9843d708926a5f38202e) Thanks [@hntrl](https://github.com/hntrl)! - fix(core): handle circular references in `load`
10
+
11
+ - [#9739](https://github.com/langchain-ai/langchainjs/pull/9739) [`c28d24a`](https://github.com/langchain-ai/langchainjs/commit/c28d24a8770f6d0e543cde116b0e38b3baf21301) Thanks [@christian-bromann](https://github.com/christian-bromann)! - fix(core): use getBufferString for message summarization
12
+
13
+ - [#9702](https://github.com/langchain-ai/langchainjs/pull/9702) [`bfcb87d`](https://github.com/langchain-ai/langchainjs/commit/bfcb87d23c580c7881f650960a448fe2e54a30b3) Thanks [@christian-bromann](https://github.com/christian-bromann)! - fix(core): improve interop with Zod
14
+
3
15
  ## 1.1.8
4
16
 
5
17
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"chat_models.d.ts","names":["ZodType","ZodTypeV3","$ZodType","ZodTypeV4","BaseMessage","BaseMessageChunk","BaseMessageLike","AIMessageChunk","MessageOutputVersion","BasePromptValueInterface","LLMResult","ChatGenerationChunk","ChatResult","Generation","BaseLanguageModel","StructuredOutputMethodOptions","ToolDefinition","BaseLanguageModelCallOptions","BaseLanguageModelInput","BaseLanguageModelParams","CallbackManagerForLLMRun","Callbacks","RunnableConfig","BaseCache","StructuredToolInterface","StructuredToolParams","Runnable","RunnableToolLike","ToolChoice","Record","SerializedChatModel","SerializedLLM","BaseChatModelParams","BaseChatModelCallOptions","LangSmithParams","Array","BindToolsInput","BaseChatModel","RunOutput","CallOptions","OutputMessageType","Exclude","Omit","Partial","Promise","AsyncGenerator","messages","cache","llmStringKey","parsedOptions","handledOptions","SimpleChatModel"],"sources":["../../src/language_models/chat_models.d.ts"],"sourcesContent":["import type { ZodType as ZodTypeV3 } from \"zod/v3\";\nimport type { $ZodType as ZodTypeV4 } from \"zod/v4/core\";\nimport { type BaseMessage, BaseMessageChunk, type BaseMessageLike, AIMessageChunk, MessageOutputVersion } from \"../messages/index.js\";\nimport type { BasePromptValueInterface } from \"../prompt_values.js\";\nimport { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from \"../outputs.js\";\nimport { BaseLanguageModel, type StructuredOutputMethodOptions, type ToolDefinition, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from \"./base.js\";\nimport { type CallbackManagerForLLMRun, type Callbacks } from \"../callbacks/manager.js\";\nimport type { RunnableConfig } from \"../runnables/config.js\";\nimport type { BaseCache } from \"../caches/index.js\";\nimport { StructuredToolInterface, StructuredToolParams } from \"../tools/index.js\";\nimport { Runnable, RunnableToolLike } from \"../runnables/base.js\";\nexport type ToolChoice = string | Record<string, any> | \"auto\" | \"any\";\n/**\n * Represents a serialized chat model.\n */\nexport type SerializedChatModel = {\n _model: string;\n _type: string;\n} & Record<string, any>;\n/**\n * Represents a serialized large language model.\n */\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n} & Record<string, any>;\n/**\n * Represents the parameters for a base chat model.\n */\nexport type BaseChatModelParams = BaseLanguageModelParams & {\n /**\n * Whether to disable streaming.\n *\n * If streaming is bypassed, then `stream()` will defer to\n * `invoke()`.\n *\n * - If true, will always bypass streaming case.\n * - If false (default), will always use streaming case if available.\n */\n disableStreaming?: boolean;\n /**\n * Version of `AIMessage` output format to store in message content.\n *\n * `AIMessage.contentBlocks` will lazily parse the contents of `content` into a\n * standard format. This flag can be used to additionally store the standard format\n * as the message content, e.g., for serialization purposes.\n *\n * - \"v0\": provider-specific format in content (can lazily parse with `.contentBlocks`)\n * - \"v1\": standardized format in content (consistent with `.contentBlocks`)\n *\n * You can also set `LC_OUTPUT_VERSION` as an environment variable to \"v1\" to\n * enable this by default.\n *\n * @default \"v0\"\n */\n outputVersion?: MessageOutputVersion;\n};\n/**\n * Represents the call options for a base chat model.\n */\nexport type BaseChatModelCallOptions = BaseLanguageModelCallOptions & {\n /**\n * Specifies how the chat model should use tools.\n * @default undefined\n *\n * Possible values:\n * - \"auto\": The model may choose to use any of the provided tools, or none.\n * - \"any\": The model must use one of the provided tools.\n * - \"none\": The model must not use any tools.\n * - A string (not \"auto\", \"any\", or \"none\"): The name of a specific tool the model must use.\n * - An object: A custom schema specifying tool choice parameters. Specific to the provider.\n *\n * Note: Not all providers support tool_choice. An error will be thrown\n * if used with an unsupported model.\n */\n tool_choice?: ToolChoice;\n /**\n * Version of `AIMessage` output format to store in message content.\n *\n * `AIMessage.contentBlocks` will lazily parse the contents of `content` into a\n * standard format. This flag can be used to additionally store the standard format\n * as the message content, e.g., for serialization purposes.\n *\n * - \"v0\": provider-specific format in content (can lazily parse with `.contentBlocks`)\n * - \"v1\": standardized format in content (consistent with `.contentBlocks`)\n *\n * You can also set `LC_OUTPUT_VERSION` as an environment variable to \"v1\" to\n * enable this by default.\n *\n * @default \"v0\"\n */\n outputVersion?: MessageOutputVersion;\n};\nexport type LangSmithParams = {\n ls_provider?: string;\n ls_model_name?: string;\n ls_model_type: \"chat\";\n ls_temperature?: number;\n ls_max_tokens?: number;\n ls_stop?: Array<string>;\n};\nexport type BindToolsInput = StructuredToolInterface | Record<string, any> | ToolDefinition | RunnableToolLike | StructuredToolParams;\n/**\n * Base class for chat models. It extends the BaseLanguageModel class and\n * provides methods for generating chat based on input messages.\n */\nexport declare abstract class BaseChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions, OutputMessageType extends BaseMessageChunk = AIMessageChunk> extends BaseLanguageModel<OutputMessageType, CallOptions> {\n ParsedCallOptions: Omit<CallOptions, Exclude<keyof RunnableConfig, \"signal\" | \"timeout\" | \"maxConcurrency\">>;\n lc_namespace: string[];\n disableStreaming: boolean;\n outputVersion?: MessageOutputVersion;\n get callKeys(): string[];\n constructor(fields: BaseChatModelParams);\n _combineLLMOutput?(...llmOutputs: LLMResult[\"llmOutput\"][]): LLMResult[\"llmOutput\"];\n protected _separateRunnableConfigFromCallOptionsCompat(options?: Partial<CallOptions>): [RunnableConfig, this[\"ParsedCallOptions\"]];\n /**\n * Bind tool-like objects to this chat model.\n *\n * @param tools A list of tool definitions to bind to this chat model.\n * Can be a structured tool, an OpenAI formatted tool, or an object\n * matching the provider's specific tool schema.\n * @param kwargs Any additional parameters to bind.\n */\n bindTools?(tools: BindToolsInput[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;\n /**\n * Invokes the chat model with a single input.\n * @param input The input for the language model.\n * @param options The call options.\n * @returns A Promise that resolves to a BaseMessageChunk.\n */\n invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<OutputMessageType>;\n _streamResponseChunks(_messages: BaseMessage[], _options: this[\"ParsedCallOptions\"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;\n _streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<OutputMessageType>;\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams;\n /** @ignore */\n _generateUncached(messages: BaseMessageLike[][], parsedOptions: this[\"ParsedCallOptions\"], handledOptions: RunnableConfig, startedRunManagers?: CallbackManagerForLLMRun[]): Promise<LLMResult>;\n _generateCached({ messages, cache, llmStringKey, parsedOptions, handledOptions }: {\n messages: BaseMessageLike[][];\n cache: BaseCache<Generation[]>;\n llmStringKey: string;\n parsedOptions: any;\n handledOptions: RunnableConfig;\n }): Promise<LLMResult & {\n missingPromptIndices: number[];\n startedRunManagers?: CallbackManagerForLLMRun[];\n }>;\n /**\n * Generates chat based on the input messages.\n * @param messages An array of arrays of BaseMessage instances.\n * @param options The call options or an array of stop sequences.\n * @param callbacks The callbacks for the language model.\n * @returns A Promise that resolves to an LLMResult.\n */\n generate(messages: BaseMessageLike[][], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n /**\n * Get the parameters used to invoke the model\n */\n invocationParams(_options?: this[\"ParsedCallOptions\"]): any;\n _modelType(): string;\n abstract _llmType(): string;\n /**\n * Generates a prompt based on the input prompt values.\n * @param promptValues An array of BasePromptValue instances.\n * @param options The call options or an array of stop sequences.\n * @param callbacks The callbacks for the language model.\n * @returns A Promise that resolves to an LLMResult.\n */\n generatePrompt(promptValues: BasePromptValueInterface[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n abstract _generate(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n}\n/**\n * An abstract class that extends BaseChatModel and provides a simple\n * implementation of _generate.\n */\nexport declare abstract class SimpleChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseChatModel<CallOptions> {\n abstract _call(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<string>;\n _generate(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;\n}\n//# sourceMappingURL=chat_models.d.ts.map"],"mappings":";;;;;;;;;;;;;;;;;;KAWY4B,UAAAA,YAAsBC;;;;KAItBC,mBAAAA;;;AAJZ,CAAA,GAOID,MAPQD,CAAAA,MAAU,EAAA,GAAA,CAAA;AAItB;AAOA;AAOA;AA+BYK,KAtCAF,aAAAA,GAsCAE;EAA2BhB,MAAAA,EAAAA,MAAAA;EAerBW,KAAAA,EAAAA,MAAAA;CAgBEpB,GAlEhBqB,MAkEgBrB,CAAAA,MAAAA,EAAAA,GAAAA,CAAAA;AAAoB;AAExC;AAQA;AAA6BgB,KAxEjBQ,mBAAAA,GAAsBb,uBAwELK,GAAAA;EAA0BK;;;;AAA8E;AAKrI;;;;EAAkKtB,gBAAAA,CAAAA,EAAAA,OAAAA;EAA0CiC;;;;;;;;;;;;;;;EAiB3JG,aAAAA,CAAAA,EApE7BnC,oBAoE6BmC;CAAgCzB;;;;AAO/DA,KAtENe,wBAAAA,GAA2BhB,4BAsErBC,GAAAA;EAAkCqB;;;;;;;;;;;;;;EAKgGnB,WAAAA,CAAAA,EA5DlIQ,UA4DkIR;EAAqCV;;;;;;;;;;;;;;;EAkBxH6B,aAAAA,CAAAA,EA9D7C/B,oBA8D6C+B;CAAyBlB;AAAoBX,KA5DlGwB,eAAAA,GA4DkGxB;EAARkC,WAAAA,CAAAA,EAAAA,MAAAA;EAcrEnC,aAAAA,CAAAA,EAAAA,MAAAA;EAAiD8B,aAAAA,EAAAA,MAAAA;EAAyBlB,cAAAA,CAAAA,EAAAA,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ZodTypeV4","BaseMessage","BaseMessageChunk","BaseMessageLike","AIMessageChunk","MessageOutputVersion","BasePromptValueInterface","LLMResult","ChatGenerationChunk","ChatResult","Generation","BaseLanguageModel","StructuredOutputMethodOptions","ToolDefinition","BaseLanguageModelCallOptions","BaseLanguageModelInput","BaseLanguageModelParams","CallbackManagerForLLMRun","Callbacks","RunnableConfig","BaseCache","StructuredToolInterface","StructuredToolParams","Runnable","RunnableToolLike","ToolChoice","Record","SerializedChatModel","SerializedLLM","BaseChatModelParams","BaseChatModelCallOptions","LangSmithParams","Array","BindToolsInput","BaseChatModel","RunOutput","CallOptions","OutputMessageType","Exclude","Omit","Partial","Promise","AsyncGenerator","messages","cache","llmStringKey","parsedOptions","handledOptions","SimpleChatModel"],"sources":["../../src/language_models/chat_models.d.ts"],"sourcesContent":["import type { ZodType as ZodTypeV3 } from \"zod/v3\";\nimport type { $ZodType as ZodTypeV4 } from \"zod/v4/core\";\nimport { type BaseMessage, BaseMessageChunk, type BaseMessageLike, AIMessageChunk, MessageOutputVersion } from \"../messages/index.js\";\nimport type { BasePromptValueInterface } from \"../prompt_values.js\";\nimport { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from \"../outputs.js\";\nimport { BaseLanguageModel, type StructuredOutputMethodOptions, type ToolDefinition, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from \"./base.js\";\nimport { type CallbackManagerForLLMRun, type Callbacks } from \"../callbacks/manager.js\";\nimport type { RunnableConfig } from \"../runnables/config.js\";\nimport type { BaseCache } from \"../caches/index.js\";\nimport { StructuredToolInterface, StructuredToolParams } from \"../tools/index.js\";\nimport { Runnable, RunnableToolLike } from \"../runnables/base.js\";\nexport type ToolChoice = string | Record<string, any> | \"auto\" | \"any\";\n/**\n * Represents a serialized chat model.\n */\nexport type SerializedChatModel = {\n _model: string;\n _type: string;\n} & Record<string, any>;\n/**\n * Represents a serialized large language model.\n */\nexport type SerializedLLM = {\n _model: string;\n _type: string;\n} & Record<string, any>;\n/**\n * Represents the parameters for a base chat model.\n */\nexport type BaseChatModelParams = BaseLanguageModelParams & {\n /**\n * Whether to disable streaming.\n *\n * If streaming is bypassed, then `stream()` will defer to\n * `invoke()`.\n *\n * - If true, will always bypass streaming case.\n * - If false (default), will always use streaming case if available.\n */\n disableStreaming?: boolean;\n /**\n * Version of `AIMessage` output format to store in message content.\n *\n * `AIMessage.contentBlocks` will lazily parse the contents of `content` into a\n * standard format. This flag can be used to additionally store the standard format\n * as the message content, e.g., for serialization purposes.\n *\n * - \"v0\": provider-specific format in content (can lazily parse with `.contentBlocks`)\n * - \"v1\": standardized format in content (consistent with `.contentBlocks`)\n *\n * You can also set `LC_OUTPUT_VERSION` as an environment variable to \"v1\" to\n * enable this by default.\n *\n * @default \"v0\"\n */\n outputVersion?: MessageOutputVersion;\n};\n/**\n * Represents the call options for a base chat model.\n */\nexport type BaseChatModelCallOptions = BaseLanguageModelCallOptions & {\n /**\n * Specifies how the chat model should use tools.\n * @default undefined\n *\n * Possible values:\n * - \"auto\": The model may choose to use any of the provided tools, or none.\n * - \"any\": The model must use one of the provided tools.\n * - \"none\": The model must not use any tools.\n * - A string (not \"auto\", \"any\", or \"none\"): The name of a specific tool the model must use.\n * - An object: A custom schema specifying tool choice parameters. Specific to the provider.\n *\n * Note: Not all providers support tool_choice. An error will be thrown\n * if used with an unsupported model.\n */\n tool_choice?: ToolChoice;\n /**\n * Version of `AIMessage` output format to store in message content.\n *\n * `AIMessage.contentBlocks` will lazily parse the contents of `content` into a\n * standard format. This flag can be used to additionally store the standard format\n * as the message content, e.g., for serialization purposes.\n *\n * - \"v0\": provider-specific format in content (can lazily parse with `.contentBlocks`)\n * - \"v1\": standardized format in content (consistent with `.contentBlocks`)\n *\n * You can also set `LC_OUTPUT_VERSION` as an environment variable to \"v1\" to\n * enable this by default.\n *\n * @default \"v0\"\n */\n outputVersion?: MessageOutputVersion;\n};\nexport type LangSmithParams = {\n ls_provider?: string;\n ls_model_name?: string;\n ls_model_type: \"chat\";\n ls_temperature?: number;\n ls_max_tokens?: number;\n ls_stop?: Array<string>;\n};\nexport type BindToolsInput = StructuredToolInterface | Record<string, any> | ToolDefinition | RunnableToolLike | StructuredToolParams;\n/**\n * Base class for chat models. It extends the BaseLanguageModel class and\n * provides methods for generating chat based on input messages.\n */\nexport declare abstract class BaseChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions, OutputMessageType extends BaseMessageChunk = AIMessageChunk> extends BaseLanguageModel<OutputMessageType, CallOptions> {\n ParsedCallOptions: Omit<CallOptions, Exclude<keyof RunnableConfig, \"signal\" | \"timeout\" | \"maxConcurrency\">>;\n lc_namespace: string[];\n disableStreaming: boolean;\n outputVersion?: MessageOutputVersion;\n get callKeys(): string[];\n constructor(fields: BaseChatModelParams);\n _combineLLMOutput?(...llmOutputs: LLMResult[\"llmOutput\"][]): LLMResult[\"llmOutput\"];\n protected _separateRunnableConfigFromCallOptionsCompat(options?: Partial<CallOptions>): [RunnableConfig, this[\"ParsedCallOptions\"]];\n /**\n * Bind tool-like objects to this chat model.\n *\n * @param tools A list of tool definitions to bind to this chat model.\n * Can be a structured tool, an OpenAI formatted tool, or an object\n * matching the provider's specific tool schema.\n * @param kwargs Any additional parameters to bind.\n */\n bindTools?(tools: BindToolsInput[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;\n /**\n * Invokes the chat model with a single input.\n * @param input The input for the language model.\n * @param options The call options.\n * @returns A Promise that resolves to a BaseMessageChunk.\n */\n invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<OutputMessageType>;\n _streamResponseChunks(_messages: BaseMessage[], _options: this[\"ParsedCallOptions\"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;\n _streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<OutputMessageType>;\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams;\n /** @ignore */\n _generateUncached(messages: BaseMessageLike[][], parsedOptions: this[\"ParsedCallOptions\"], handledOptions: RunnableConfig, startedRunManagers?: CallbackManagerForLLMRun[]): Promise<LLMResult>;\n _generateCached({ messages, cache, llmStringKey, parsedOptions, handledOptions }: {\n messages: BaseMessageLike[][];\n cache: BaseCache<Generation[]>;\n llmStringKey: string;\n parsedOptions: any;\n handledOptions: RunnableConfig;\n }): Promise<LLMResult & {\n missingPromptIndices: number[];\n startedRunManagers?: CallbackManagerForLLMRun[];\n }>;\n /**\n * Generates chat based on the input messages.\n * @param messages An array of arrays of BaseMessage instances.\n * @param options The call options or an array of stop sequences.\n * @param callbacks The callbacks for the language model.\n * @returns A Promise that resolves to an LLMResult.\n */\n generate(messages: BaseMessageLike[][], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n /**\n * Get the parameters used to invoke the model\n */\n invocationParams(_options?: this[\"ParsedCallOptions\"]): any;\n _modelType(): string;\n abstract _llmType(): string;\n /**\n * Generates a prompt based on the input prompt values.\n * @param promptValues An array of BasePromptValue instances.\n * @param options The call options or an array of stop sequences.\n * @param callbacks The callbacks for the language model.\n * @returns A Promise that resolves to an LLMResult.\n */\n generatePrompt(promptValues: BasePromptValueInterface[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;\n abstract _generate(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;\n withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {\n raw: BaseMessage;\n parsed: RunOutput;\n }>;\n}\n/**\n * An abstract class that extends BaseChatModel and provides a simple\n * implementation of _generate.\n */\nexport declare abstract class SimpleChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseChatModel<CallOptions> {\n abstract _call(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<string>;\n _generate(messages: BaseMessage[], options: this[\"ParsedCallOptions\"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;\n}\n//# sourceMappingURL=chat_models.d.ts.map"],"mappings":";;;;;;;;;;;;;;;;;;KAWY4B,UAAAA,YAAsBC;;;;KAItBC,mBAAAA;;;AAJZ,CAAA,GAOID,MAPQD,CAAAA,MAAU,EAAA,GAAA,CAAA;AAItB;AAOA;AAOA;AA+BYK,KAtCAF,aAAAA,GAsCAE;EAA2BhB,MAAAA,EAAAA,MAAAA;EAerBW,KAAAA,EAAAA,MAAAA;CAgBEpB,GAlEhBqB,MAkEgBrB,CAAAA,MAAAA,EAAAA,GAAAA,CAAAA;AAAoB;AAExC;AAQA;AAA6BgB,KAxEjBQ,mBAAAA,GAAsBb,uBAwELK,GAAAA;EAA0BK;;;;AAA8E;AAKrI;;;;EAAkKtB,gBAAAA,CAAAA,EAAAA,OAAAA;EAA0CiC;;;;;;;;;;;;;;;EAiB3JG,aAAAA,CAAAA,EApE7BnC,oBAoE6BmC;CAAgCzB;;;;AAO/DA,KAtENe,wBAAAA,GAA2BhB,4BAsErBC,GAAAA;EAAkCqB;;;;;;;;;;;;;;EAKgGnB,WAAAA,CAAAA,EA5DlIQ,UA4DkIR;EAAqCV;;;;;;;;;;;;;;;EAkBxH6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A8Bd,sCAAsCW,SAASR;SAC3Ld;YACGkC;;;;;;;uBAOca,oCAAoClB,2BAA2BA,kCAAkCI,cAAcE;2BAChHnC,gEAAgEgB,2BAA2BwB;sBAChGxC,gEAAgEgB,2BAA2BwB,QAAQhC"}
@@ -124,7 +124,9 @@ var Serializable = class Serializable {
124
124
  if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
125
125
  });
126
126
  const escapedKwargs = {};
127
- for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = require_validation.escapeIfNeeded(value);
127
+ const visited = /* @__PURE__ */ new WeakSet();
128
+ visited.add(this);
129
+ for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = require_validation.escapeIfNeeded(value, visited);
128
130
  const kwargsWithSecrets = Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs;
129
131
  const processedKwargs = require_map_keys.mapKeys(kwargsWithSecrets, require_map_keys.keyToJson, aliases);
130
132
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"serializable.cjs","names":["obj: T","root: SerializedFields","secretsMap: { [key: string]: string }","current: any","serializableClass: typeof Serializable","kwargs?: SerializedFields","aliases: { [key: string]: string }","secrets: { [key: string]: string }","read: any","write: any","escapedKwargs: SerializedFields","escapeIfNeeded","mapKeys","keyToJson"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.js\";\nimport { escapeIfNeeded } from \"./validation.js\";\n\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n graph?: Record<string, any>;\n}\n\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\n\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {}\n\nexport interface SerializedNotImplemented\n extends BaseSerialized<\"not_implemented\"> {}\n\nexport type Serialized =\n | SerializedConstructor\n | SerializedSecret\n | SerializedNotImplemented;\n\nfunction shallowCopy<T extends object>(obj: T): T {\n return Array.isArray(obj) ? ([...obj] as T) : ({ ...obj } as T);\n}\n\nfunction replaceSecrets(\n root: SerializedFields,\n secretsMap: { [key: string]: string }\n): SerializedFields {\n const result = shallowCopy(root);\n for (const [path, secretId] of Object.entries(secretsMap)) {\n const [last, ...partsReverse] = path.split(\".\").reverse();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current: any = result;\n for (const part of partsReverse.reverse()) {\n if (current[part] === undefined) {\n break;\n }\n current[part] = shallowCopy(current[part]);\n current = current[part];\n }\n if (current[last] !== undefined) {\n current[last] = {\n lc: 1,\n type: \"secret\",\n id: [secretId],\n };\n }\n }\n return result;\n}\n\n/**\n * Get a unique name for the module, rather than parent class implementations.\n * Should not be subclassed, subclass lc_name above instead.\n */\nexport function get_lc_unique_name(\n serializableClass: typeof Serializable\n): string {\n // \"super\" here would refer to the parent class of Serializable,\n // when we want the parent class of the module actually calling this method.\n const parentClass = Object.getPrototypeOf(serializableClass);\n const lcNameIsSubclassed =\n typeof serializableClass.lc_name === \"function\" &&\n (typeof parentClass.lc_name !== \"function\" ||\n serializableClass.lc_name() !== parentClass.lc_name());\n if (lcNameIsSubclassed) {\n return serializableClass.lc_name();\n } else {\n return serializableClass.name;\n }\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\nexport interface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\n\nexport abstract class Serializable implements SerializableInterface {\n lc_serializable = false;\n\n lc_kwargs: SerializedFields;\n\n /**\n * A path to the module that contains the class, eg. [\"langchain\", \"llms\"]\n * Usually should be the same as the entrypoint the class is exported from.\n */\n abstract lc_namespace: string[];\n\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name(): string {\n return this.name;\n }\n\n /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[] {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor as typeof Serializable),\n ];\n }\n\n /**\n * A map of secrets, which will be omitted from serialization.\n * Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n * Values are the secret ids, which will be used when deserializing.\n */\n get lc_secrets(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A map of additional attributes to merge with constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the attribute values, which will be serialized.\n * These attributes need to be accepted by the constructor as arguments.\n */\n get lc_attributes(): SerializedFields | undefined {\n return undefined;\n }\n\n /**\n * A map of aliases for constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the alias that will replace the key in serialization.\n * This is used to eg. make argument names match Python.\n */\n get lc_aliases(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A manual list of keys that should be serialized.\n * If not overridden, all fields passed into the constructor will be serialized.\n */\n get lc_serializable_keys(): string[] | undefined {\n return undefined;\n }\n\n constructor(kwargs?: SerializedFields, ..._args: never[]) {\n if (this.lc_serializable_keys !== undefined) {\n this.lc_kwargs = Object.fromEntries(\n Object.entries(kwargs || {}).filter(([key]) =>\n this.lc_serializable_keys?.includes(key)\n )\n );\n } else {\n this.lc_kwargs = kwargs ?? {};\n }\n }\n\n toJSON(): Serialized {\n if (!this.lc_serializable) {\n return this.toJSONNotImplemented();\n }\n if (\n // eslint-disable-next-line no-instanceof/no-instanceof\n this.lc_kwargs instanceof Serializable ||\n typeof this.lc_kwargs !== \"object\" ||\n Array.isArray(this.lc_kwargs)\n ) {\n // We do not support serialization of classes with arg not a POJO\n // I'm aware the check above isn't as strict as it could be\n return this.toJSONNotImplemented();\n }\n\n const aliases: { [key: string]: string } = {};\n const secrets: { [key: string]: string } = {};\n const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n acc[key] = key in this ? this[key as keyof this] : this.lc_kwargs[key];\n return acc;\n }, {} as SerializedFields);\n // get secrets, attributes and aliases from all superclasses\n for (\n let current = Object.getPrototypeOf(this);\n current;\n current = Object.getPrototypeOf(current)\n ) {\n Object.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n Object.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n Object.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n }\n\n // include all secrets used, even if not in kwargs,\n // will be replaced with sentinel value in replaceSecrets\n Object.keys(secrets).forEach((keyPath) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let read: any = this;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let write: any = kwargs;\n\n const [last, ...partsReverse] = keyPath.split(\".\").reverse();\n for (const key of partsReverse.reverse()) {\n if (!(key in read) || read[key] === undefined) return;\n if (!(key in write) || write[key] === undefined) {\n if (typeof read[key] === \"object\" && read[key] != null) {\n write[key] = {};\n } else if (Array.isArray(read[key])) {\n write[key] = [];\n }\n }\n\n read = read[key];\n write = write[key];\n }\n\n if (last in read && read[last] !== undefined) {\n write[last] = write[last] || read[last];\n }\n });\n\n const escapedKwargs: SerializedFields = {};\n for (const [key, value] of Object.entries(kwargs)) {\n escapedKwargs[key] = escapeIfNeeded(value);\n }\n\n // Now add secret markers - these are added AFTER escaping so they won't be escaped\n const kwargsWithSecrets = Object.keys(secrets).length\n ? replaceSecrets(escapedKwargs, secrets)\n : escapedKwargs;\n\n // Finally transform keys to JSON format\n const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);\n\n return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: processedKwargs,\n };\n }\n\n toJSONNotImplemented(): SerializedNotImplemented {\n return {\n lc: 1,\n type: \"not_implemented\",\n id: this.lc_id,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,YAA8BA,KAAW;AAChD,QAAO,MAAM,QAAQ,IAAI,GAAI,CAAC,GAAG,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IAGH,OAAO,KAAK;IACZ,QAAQ,MAAM;GACf;AAED,OAAI,QAAQ,QAAQ,KAAK,UAAU,QACjC,MAAM,QAAQ,MAAM,SAAS,KAAK;EAErC,EAAC;EAEF,MAAMC,gBAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,cAAc,OAAOC,kCAAe,MAAM;EAI5C,MAAM,oBAAoB,OAAO,KAAK,QAAQ,CAAC,SAC3C,eAAe,eAAe,QAAQ,GACtC;EAGJ,MAAM,kBAAkBC,yBAAQ,mBAAmBC,4BAAW,QAAQ;AAEtE,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;GACT,QAAQ;EACT;CACF;CAED,uBAAiD;AAC/C,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;EACV;CACF;AACF"}
1
+ {"version":3,"file":"serializable.cjs","names":["obj: T","root: SerializedFields","secretsMap: { [key: string]: string }","current: any","serializableClass: typeof Serializable","kwargs?: SerializedFields","aliases: { [key: string]: string }","secrets: { [key: string]: string }","read: any","write: any","escapedKwargs: SerializedFields","escapeIfNeeded","mapKeys","keyToJson"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.js\";\nimport { escapeIfNeeded } from \"./validation.js\";\n\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n graph?: Record<string, any>;\n}\n\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\n\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {}\n\nexport interface SerializedNotImplemented\n extends BaseSerialized<\"not_implemented\"> {}\n\nexport type Serialized =\n | SerializedConstructor\n | SerializedSecret\n | SerializedNotImplemented;\n\nfunction shallowCopy<T extends object>(obj: T): T {\n return Array.isArray(obj) ? ([...obj] as T) : ({ ...obj } as T);\n}\n\nfunction replaceSecrets(\n root: SerializedFields,\n secretsMap: { [key: string]: string }\n): SerializedFields {\n const result = shallowCopy(root);\n for (const [path, secretId] of Object.entries(secretsMap)) {\n const [last, ...partsReverse] = path.split(\".\").reverse();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current: any = result;\n for (const part of partsReverse.reverse()) {\n if (current[part] === undefined) {\n break;\n }\n current[part] = shallowCopy(current[part]);\n current = current[part];\n }\n if (current[last] !== undefined) {\n current[last] = {\n lc: 1,\n type: \"secret\",\n id: [secretId],\n };\n }\n }\n return result;\n}\n\n/**\n * Get a unique name for the module, rather than parent class implementations.\n * Should not be subclassed, subclass lc_name above instead.\n */\nexport function get_lc_unique_name(\n serializableClass: typeof Serializable\n): string {\n // \"super\" here would refer to the parent class of Serializable,\n // when we want the parent class of the module actually calling this method.\n const parentClass = Object.getPrototypeOf(serializableClass);\n const lcNameIsSubclassed =\n typeof serializableClass.lc_name === \"function\" &&\n (typeof parentClass.lc_name !== \"function\" ||\n serializableClass.lc_name() !== parentClass.lc_name());\n if (lcNameIsSubclassed) {\n return serializableClass.lc_name();\n } else {\n return serializableClass.name;\n }\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\nexport interface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\n\nexport abstract class Serializable implements SerializableInterface {\n lc_serializable = false;\n\n lc_kwargs: SerializedFields;\n\n /**\n * A path to the module that contains the class, eg. [\"langchain\", \"llms\"]\n * Usually should be the same as the entrypoint the class is exported from.\n */\n abstract lc_namespace: string[];\n\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name(): string {\n return this.name;\n }\n\n /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[] {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor as typeof Serializable),\n ];\n }\n\n /**\n * A map of secrets, which will be omitted from serialization.\n * Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n * Values are the secret ids, which will be used when deserializing.\n */\n get lc_secrets(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A map of additional attributes to merge with constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the attribute values, which will be serialized.\n * These attributes need to be accepted by the constructor as arguments.\n */\n get lc_attributes(): SerializedFields | undefined {\n return undefined;\n }\n\n /**\n * A map of aliases for constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the alias that will replace the key in serialization.\n * This is used to eg. make argument names match Python.\n */\n get lc_aliases(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A manual list of keys that should be serialized.\n * If not overridden, all fields passed into the constructor will be serialized.\n */\n get lc_serializable_keys(): string[] | undefined {\n return undefined;\n }\n\n constructor(kwargs?: SerializedFields, ..._args: never[]) {\n if (this.lc_serializable_keys !== undefined) {\n this.lc_kwargs = Object.fromEntries(\n Object.entries(kwargs || {}).filter(([key]) =>\n this.lc_serializable_keys?.includes(key)\n )\n );\n } else {\n this.lc_kwargs = kwargs ?? {};\n }\n }\n\n toJSON(): Serialized {\n if (!this.lc_serializable) {\n return this.toJSONNotImplemented();\n }\n if (\n // eslint-disable-next-line no-instanceof/no-instanceof\n this.lc_kwargs instanceof Serializable ||\n typeof this.lc_kwargs !== \"object\" ||\n Array.isArray(this.lc_kwargs)\n ) {\n // We do not support serialization of classes with arg not a POJO\n // I'm aware the check above isn't as strict as it could be\n return this.toJSONNotImplemented();\n }\n\n const aliases: { [key: string]: string } = {};\n const secrets: { [key: string]: string } = {};\n const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n acc[key] = key in this ? this[key as keyof this] : this.lc_kwargs[key];\n return acc;\n }, {} as SerializedFields);\n // get secrets, attributes and aliases from all superclasses\n for (\n let current = Object.getPrototypeOf(this);\n current;\n current = Object.getPrototypeOf(current)\n ) {\n Object.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n Object.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n Object.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n }\n\n // include all secrets used, even if not in kwargs,\n // will be replaced with sentinel value in replaceSecrets\n Object.keys(secrets).forEach((keyPath) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let read: any = this;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let write: any = kwargs;\n\n const [last, ...partsReverse] = keyPath.split(\".\").reverse();\n for (const key of partsReverse.reverse()) {\n if (!(key in read) || read[key] === undefined) return;\n if (!(key in write) || write[key] === undefined) {\n if (typeof read[key] === \"object\" && read[key] != null) {\n write[key] = {};\n } else if (Array.isArray(read[key])) {\n write[key] = [];\n }\n }\n\n read = read[key];\n write = write[key];\n }\n\n if (last in read && read[last] !== undefined) {\n write[last] = write[last] || read[last];\n }\n });\n\n const escapedKwargs: SerializedFields = {};\n const visited = new WeakSet<object>();\n visited.add(this);\n for (const [key, value] of Object.entries(kwargs)) {\n escapedKwargs[key] = escapeIfNeeded(value, visited);\n }\n\n // Now add secret markers - these are added AFTER escaping so they won't be escaped\n const kwargsWithSecrets = Object.keys(secrets).length\n ? replaceSecrets(escapedKwargs, secrets)\n : escapedKwargs;\n\n // Finally transform keys to JSON format\n const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);\n\n return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: processedKwargs,\n };\n }\n\n toJSONNotImplemented(): SerializedNotImplemented {\n return {\n lc: 1,\n type: \"not_implemented\",\n id: this.lc_id,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,YAA8BA,KAAW;AAChD,QAAO,MAAM,QAAQ,IAAI,GAAI,CAAC,GAAG,GAAI,IAAU,EAAE,GAAG,IAAK;AA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AAM;GACf;AAED,OAAI,QAAQ,QAAQ,KAAK,UAAU,QACjC,MAAM,QAAQ,MAAM,SAAS,KAAK;EAErC,EAAC;EAEF,MAAMC,gBAAkC,CAAE;EAC1C,MAAM,0BAAU,IAAI;EACpB,QAAQ,IAAI,KAAK;AACjB,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,cAAc,OAAOC,kCAAe,OAAO,QAAQ;EAIrD,MAAM,oBAAoB,OAAO,KAAK,QAAQ,CAAC,SAC3C,eAAe,eAAe,QAAQ,GACtC;EAGJ,MAAM,kBAAkBC,yBAAQ,mBAAmBC,4BAAW,QAAQ;AAEtE,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;GACT,QAAQ;EACT;CACF;CAED,uBAAiD;AAC/C,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;EACV;CACF;AACF"}
@@ -124,7 +124,9 @@ var Serializable = class Serializable {
124
124
  if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
125
125
  });
126
126
  const escapedKwargs = {};
127
- for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value);
127
+ const visited = /* @__PURE__ */ new WeakSet();
128
+ visited.add(this);
129
+ for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value, visited);
128
130
  const kwargsWithSecrets = Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs;
129
131
  const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);
130
132
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"serializable.js","names":["obj: T","root: SerializedFields","secretsMap: { [key: string]: string }","current: any","serializableClass: typeof Serializable","kwargs?: SerializedFields","aliases: { [key: string]: string }","secrets: { [key: string]: string }","read: any","write: any","escapedKwargs: SerializedFields"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.js\";\nimport { escapeIfNeeded } from \"./validation.js\";\n\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n graph?: Record<string, any>;\n}\n\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\n\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {}\n\nexport interface SerializedNotImplemented\n extends BaseSerialized<\"not_implemented\"> {}\n\nexport type Serialized =\n | SerializedConstructor\n | SerializedSecret\n | SerializedNotImplemented;\n\nfunction shallowCopy<T extends object>(obj: T): T {\n return Array.isArray(obj) ? ([...obj] as T) : ({ ...obj } as T);\n}\n\nfunction replaceSecrets(\n root: SerializedFields,\n secretsMap: { [key: string]: string }\n): SerializedFields {\n const result = shallowCopy(root);\n for (const [path, secretId] of Object.entries(secretsMap)) {\n const [last, ...partsReverse] = path.split(\".\").reverse();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current: any = result;\n for (const part of partsReverse.reverse()) {\n if (current[part] === undefined) {\n break;\n }\n current[part] = shallowCopy(current[part]);\n current = current[part];\n }\n if (current[last] !== undefined) {\n current[last] = {\n lc: 1,\n type: \"secret\",\n id: [secretId],\n };\n }\n }\n return result;\n}\n\n/**\n * Get a unique name for the module, rather than parent class implementations.\n * Should not be subclassed, subclass lc_name above instead.\n */\nexport function get_lc_unique_name(\n serializableClass: typeof Serializable\n): string {\n // \"super\" here would refer to the parent class of Serializable,\n // when we want the parent class of the module actually calling this method.\n const parentClass = Object.getPrototypeOf(serializableClass);\n const lcNameIsSubclassed =\n typeof serializableClass.lc_name === \"function\" &&\n (typeof parentClass.lc_name !== \"function\" ||\n serializableClass.lc_name() !== parentClass.lc_name());\n if (lcNameIsSubclassed) {\n return serializableClass.lc_name();\n } else {\n return serializableClass.name;\n }\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\nexport interface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\n\nexport abstract class Serializable implements SerializableInterface {\n lc_serializable = false;\n\n lc_kwargs: SerializedFields;\n\n /**\n * A path to the module that contains the class, eg. [\"langchain\", \"llms\"]\n * Usually should be the same as the entrypoint the class is exported from.\n */\n abstract lc_namespace: string[];\n\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name(): string {\n return this.name;\n }\n\n /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[] {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor as typeof Serializable),\n ];\n }\n\n /**\n * A map of secrets, which will be omitted from serialization.\n * Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n * Values are the secret ids, which will be used when deserializing.\n */\n get lc_secrets(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A map of additional attributes to merge with constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the attribute values, which will be serialized.\n * These attributes need to be accepted by the constructor as arguments.\n */\n get lc_attributes(): SerializedFields | undefined {\n return undefined;\n }\n\n /**\n * A map of aliases for constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the alias that will replace the key in serialization.\n * This is used to eg. make argument names match Python.\n */\n get lc_aliases(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A manual list of keys that should be serialized.\n * If not overridden, all fields passed into the constructor will be serialized.\n */\n get lc_serializable_keys(): string[] | undefined {\n return undefined;\n }\n\n constructor(kwargs?: SerializedFields, ..._args: never[]) {\n if (this.lc_serializable_keys !== undefined) {\n this.lc_kwargs = Object.fromEntries(\n Object.entries(kwargs || {}).filter(([key]) =>\n this.lc_serializable_keys?.includes(key)\n )\n );\n } else {\n this.lc_kwargs = kwargs ?? {};\n }\n }\n\n toJSON(): Serialized {\n if (!this.lc_serializable) {\n return this.toJSONNotImplemented();\n }\n if (\n // eslint-disable-next-line no-instanceof/no-instanceof\n this.lc_kwargs instanceof Serializable ||\n typeof this.lc_kwargs !== \"object\" ||\n Array.isArray(this.lc_kwargs)\n ) {\n // We do not support serialization of classes with arg not a POJO\n // I'm aware the check above isn't as strict as it could be\n return this.toJSONNotImplemented();\n }\n\n const aliases: { [key: string]: string } = {};\n const secrets: { [key: string]: string } = {};\n const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n acc[key] = key in this ? this[key as keyof this] : this.lc_kwargs[key];\n return acc;\n }, {} as SerializedFields);\n // get secrets, attributes and aliases from all superclasses\n for (\n let current = Object.getPrototypeOf(this);\n current;\n current = Object.getPrototypeOf(current)\n ) {\n Object.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n Object.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n Object.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n }\n\n // include all secrets used, even if not in kwargs,\n // will be replaced with sentinel value in replaceSecrets\n Object.keys(secrets).forEach((keyPath) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let read: any = this;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let write: any = kwargs;\n\n const [last, ...partsReverse] = keyPath.split(\".\").reverse();\n for (const key of partsReverse.reverse()) {\n if (!(key in read) || read[key] === undefined) return;\n if (!(key in write) || write[key] === undefined) {\n if (typeof read[key] === \"object\" && read[key] != null) {\n write[key] = {};\n } else if (Array.isArray(read[key])) {\n write[key] = [];\n }\n }\n\n read = read[key];\n write = write[key];\n }\n\n if (last in read && read[last] !== undefined) {\n write[last] = write[last] || read[last];\n }\n });\n\n const escapedKwargs: SerializedFields = {};\n for (const [key, value] of Object.entries(kwargs)) {\n escapedKwargs[key] = escapeIfNeeded(value);\n }\n\n // Now add secret markers - these are added AFTER escaping so they won't be escaped\n const kwargsWithSecrets = Object.keys(secrets).length\n ? replaceSecrets(escapedKwargs, secrets)\n : escapedKwargs;\n\n // Finally transform keys to JSON format\n const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);\n\n return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: processedKwargs,\n };\n }\n\n toJSONNotImplemented(): SerializedNotImplemented {\n return {\n lc: 1,\n type: \"not_implemented\",\n id: this.lc_id,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,YAA8BA,KAAW;AAChD,QAAO,MAAM,QAAQ,IAAI,GAAI,CAAC,GAAG,GAAI,IAAU,EAAE,GAAG,IAAK;AAC1D;AAED,SAAS,eACPC,MACAC,YACkB;CAClB,MAAM,SAAS,YAAY,KAAK;AAChC,MAAK,MAAM,CAAC,MAAM,SAAS,IAAI,OAAO,QAAQ,WAAW,EAAE;EACzD,MAAM,CAAC,MAAM,GAAG,aAAa,GAAG,KAAK,MAAM,IAAI,CAAC,SAAS;EAEzD,IAAIC,UAAe;AACnB,OAAK,MAAM,QAAQ,aAAa,SAAS,EAAE;AACzC,OAAI,QAAQ,UAAU,OACpB;GAEF,QAAQ,QAAQ,YAAY,QAAQ,MAAM;GAC1C,UAAU,QAAQ;EACnB;AACD,MAAI,QAAQ,UAAU,QACpB,QAAQ,QAAQ;GACd,IAAI;GACJ,MAAM;GACN,IAAI,CAAC,QAAS;EACf;CAEJ;AACD,QAAO;AACR;;;;;AAMD,SAAgB,mBACdC,mBACQ;CAGR,MAAM,cAAc,OAAO,eAAe,kBAAkB;CAC5D,MAAM,qBACJ,OAAO,kBAAkB,YAAY,eACpC,OAAO,YAAY,YAAY,cAC9B,kBAAkB,SAAS,KAAK,YAAY,SAAS;AACzD,KAAI,mBACF,QAAO,kBAAkB,SAAS;KAElC,QAAO,kBAAkB;AAE5B;AAqBD,IAAsB,eAAtB,MAAsB,aAA8C;CAClE,kBAAkB;CAElB;;;;;;;CAcA,OAAO,UAAkB;AACvB,SAAO,KAAK;CACb;;;;CAKD,IAAI,QAAk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QAAa;GAEjB,MAAM,CAAC,MAAM,GAAG,aAAa,GAAG,QAAQ,MAAM,IAAI,CAAC,SAAS;AAC5D,QAAK,MAAM,OAAO,aAAa,SAAS,EAAE;AACxC,QAAI,EAAE,OAAO,SAAS,KAAK,SAAS,OAAW;AAC/C,QAAI,EAAE,OAAO,UAAU,MAAM,SAAS,QACpC;SAAI,OAAO,KAAK,SAAS,YAAY,KAAK,QAAQ,MAChD,MAAM,OAAO,CAAE;cACN,MAAM,QAAQ,KAAK,KAAK,EACjC,MAAM,OAAO,CAAE;IAChB;IAGH,OAAO,KAAK;IACZ,QAAQ,MAAM;GACf;AAED,OAAI,QAAQ,QAAQ,KAAK,UAAU,QACjC,MAAM,QAAQ,MAAM,SAAS,KAAK;EAErC,EAAC;EAEF,MAAMC,gBAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,cAAc,OAAO,eAAe,MAAM;EAI5C,MAAM,oBAAoB,OAAO,KAAK,QAAQ,CAAC,SAC3C,eAAe,eAAe,QAAQ,GACtC;EAGJ,MAAM,kBAAkB,QAAQ,mBAAmB,WAAW,QAAQ;AAEtE,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;GACT,QAAQ;EACT;CACF;CAED,uBAAiD;AAC/C,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;EACV;CACF;AACF"}
1
+ {"version":3,"file":"serializable.js","names":["obj: T","root: SerializedFields","secretsMap: { [key: string]: string }","current: any","serializableClass: typeof Serializable","kwargs?: SerializedFields","aliases: { [key: string]: string }","secrets: { [key: string]: string }","read: any","write: any","escapedKwargs: SerializedFields"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.js\";\nimport { escapeIfNeeded } from \"./validation.js\";\n\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n graph?: Record<string, any>;\n}\n\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\n\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {}\n\nexport interface SerializedNotImplemented\n extends BaseSerialized<\"not_implemented\"> {}\n\nexport type Serialized =\n | SerializedConstructor\n | SerializedSecret\n | SerializedNotImplemented;\n\nfunction shallowCopy<T extends object>(obj: T): T {\n return Array.isArray(obj) ? ([...obj] as T) : ({ ...obj } as T);\n}\n\nfunction replaceSecrets(\n root: SerializedFields,\n secretsMap: { [key: string]: string }\n): SerializedFields {\n const result = shallowCopy(root);\n for (const [path, secretId] of Object.entries(secretsMap)) {\n const [last, ...partsReverse] = path.split(\".\").reverse();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current: any = result;\n for (const part of partsReverse.reverse()) {\n if (current[part] === undefined) {\n break;\n }\n current[part] = shallowCopy(current[part]);\n current = current[part];\n }\n if (current[last] !== undefined) {\n current[last] = {\n lc: 1,\n type: \"secret\",\n id: [secretId],\n };\n }\n }\n return result;\n}\n\n/**\n * Get a unique name for the module, rather than parent class implementations.\n * Should not be subclassed, subclass lc_name above instead.\n */\nexport function get_lc_unique_name(\n serializableClass: typeof Serializable\n): string {\n // \"super\" here would refer to the parent class of Serializable,\n // when we want the parent class of the module actually calling this method.\n const parentClass = Object.getPrototypeOf(serializableClass);\n const lcNameIsSubclassed =\n typeof serializableClass.lc_name === \"function\" &&\n (typeof parentClass.lc_name !== \"function\" ||\n serializableClass.lc_name() !== parentClass.lc_name());\n if (lcNameIsSubclassed) {\n return serializableClass.lc_name();\n } else {\n return serializableClass.name;\n }\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\nexport interface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\n\nexport abstract class Serializable implements SerializableInterface {\n lc_serializable = false;\n\n lc_kwargs: SerializedFields;\n\n /**\n * A path to the module that contains the class, eg. [\"langchain\", \"llms\"]\n * Usually should be the same as the entrypoint the class is exported from.\n */\n abstract lc_namespace: string[];\n\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name(): string {\n return this.name;\n }\n\n /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[] {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor as typeof Serializable),\n ];\n }\n\n /**\n * A map of secrets, which will be omitted from serialization.\n * Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n * Values are the secret ids, which will be used when deserializing.\n */\n get lc_secrets(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A map of additional attributes to merge with constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the attribute values, which will be serialized.\n * These attributes need to be accepted by the constructor as arguments.\n */\n get lc_attributes(): SerializedFields | undefined {\n return undefined;\n }\n\n /**\n * A map of aliases for constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the alias that will replace the key in serialization.\n * This is used to eg. make argument names match Python.\n */\n get lc_aliases(): { [key: string]: string } | undefined {\n return undefined;\n }\n\n /**\n * A manual list of keys that should be serialized.\n * If not overridden, all fields passed into the constructor will be serialized.\n */\n get lc_serializable_keys(): string[] | undefined {\n return undefined;\n }\n\n constructor(kwargs?: SerializedFields, ..._args: never[]) {\n if (this.lc_serializable_keys !== undefined) {\n this.lc_kwargs = Object.fromEntries(\n Object.entries(kwargs || {}).filter(([key]) =>\n this.lc_serializable_keys?.includes(key)\n )\n );\n } else {\n this.lc_kwargs = kwargs ?? {};\n }\n }\n\n toJSON(): Serialized {\n if (!this.lc_serializable) {\n return this.toJSONNotImplemented();\n }\n if (\n // eslint-disable-next-line no-instanceof/no-instanceof\n this.lc_kwargs instanceof Serializable ||\n typeof this.lc_kwargs !== \"object\" ||\n Array.isArray(this.lc_kwargs)\n ) {\n // We do not support serialization of classes with arg not a POJO\n // I'm aware the check above isn't as strict as it could be\n return this.toJSONNotImplemented();\n }\n\n const aliases: { [key: string]: string } = {};\n const secrets: { [key: string]: string } = {};\n const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n acc[key] = key in this ? this[key as keyof this] : this.lc_kwargs[key];\n return acc;\n }, {} as SerializedFields);\n // get secrets, attributes and aliases from all superclasses\n for (\n let current = Object.getPrototypeOf(this);\n current;\n current = Object.getPrototypeOf(current)\n ) {\n Object.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n Object.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n Object.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n }\n\n // include all secrets used, even if not in kwargs,\n // will be replaced with sentinel value in replaceSecrets\n Object.keys(secrets).forEach((keyPath) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let read: any = this;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let write: any = kwargs;\n\n const [last, ...partsReverse] = keyPath.split(\".\").reverse();\n for (const key of partsReverse.reverse()) {\n if (!(key in read) || read[key] === undefined) return;\n if (!(key in write) || write[key] === undefined) {\n if (typeof read[key] === \"object\" && read[key] != null) {\n write[key] = {};\n } else if (Array.isArray(read[key])) {\n write[key] = [];\n }\n }\n\n read = read[key];\n write = write[key];\n }\n\n if (last in read && read[last] !== undefined) {\n write[last] = write[last] || read[last];\n }\n });\n\n const escapedKwargs: SerializedFields = {};\n const visited = new WeakSet<object>();\n visited.add(this);\n for (const [key, value] of Object.entries(kwargs)) {\n escapedKwargs[key] = escapeIfNeeded(value, visited);\n }\n\n // Now add secret markers - these are added AFTER escaping so they won't be escaped\n const kwargsWithSecrets = Object.keys(secrets).length\n ? replaceSecrets(escapedKwargs, secrets)\n : escapedKwargs;\n\n // Finally transform keys to JSON format\n const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);\n\n return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: processedKwargs,\n };\n }\n\n toJSONNotImplemented(): SerializedNotImplemented {\n return {\n lc: 1,\n type: \"not_implemented\",\n id: this.lc_id,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAS,YAA8BA,KAAW;AAChD,QAAO,MAAM,QAAQ,IAAI,GAAI,CAAC,GAAG,GAAI,IAAU,EAAE,GAAG,IAAK;AAC1D;AAED,SAAS,eACPC,MACAC,YACkB;CAClB,MAAM,SAAS,YAAY,KAAK;AAChC,MAAK,MAAM,CAAC,MAAM,SAAS,IAAI,OAAO,QAAQ,WAAW,EAAE;EACzD,MAAM,CAAC,MAAM,GAAG,aAAa,GAAG,KAAK,MAAM,IAAI,CAAC,SAAS;EAEzD,IAAIC,UAAe;AACnB,OAAK,MAAM,QAAQ,aAAa,SAAS,EAAE;AACzC,OAAI,QAAQ,UAAU,OACpB;GAEF,QAAQ,QAAQ,YAAY,QAAQ,MAAM;GAC1C,UAAU,QAAQ;EACnB;AACD,MAAI,QAAQ,UAAU,QACpB,QAAQ,QAAQ;GACd,IAAI;GACJ,MAAM;GACN,IAAI,CAAC,QAAS;EACf;CAEJ;AACD,QAAO;AACR;;;;;AAMD,SAAgB,mBACdC,mBACQ;CAGR,MAAM,cAAc,OAAO,eAAe,kBAAkB;CAC5D,MAAM,qBACJ,OAAO,kBAAkB,YAAY,eACpC,OAAO,YAAY,YAAY,cAC9B,kBAAkB,SAAS,KAAK,YAAY,SAAS;AACzD,KAAI,mBACF,QAAO,kBAAkB,SAAS;KAElC,QAAO,kBAAkB;AAE5B;AAqBD,IAAsB,eAAtB,MAAsB,aAA8C;CAClE,kBAAkB;CAElB;;;;;;;CAcA,OAAO,UAAkB;AACvB,SAAO,KAAK;CACb;;;;CAKD,IAAI,QAAkB;AACpB,SAAO,CACL,GAAG,KAAK,cACR,mBAAmB,KAAK,YAAmC,AAC5D;CACF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CAAC,SAAS;AAC5D,QAAK,MAAM,OAAO,aAAa,SAAS,EAAE;AACxC,QAAI,EAAE,OAAO,SAAS,KAAK,SAAS,OAAW;AAC/C,QAAI,EAAE,OAAO,UAAU,MAAM,SAAS,QACpC;SAAI,OAAO,KAAK,SAAS,YAAY,KAAK,QAAQ,MAChD,MAAM,OAAO,CAAE;cACN,MAAM,QAAQ,KAAK,KAAK,EACjC,MAAM,OAAO,CAAE;IAChB;IAGH,OAAO,KAAK;IACZ,QAAQ,MAAM;GACf;AAED,OAAI,QAAQ,QAAQ,KAAK,UAAU,QACjC,MAAM,QAAQ,MAAM,SAAS,KAAK;EAErC,EAAC;EAEF,MAAMC,gBAAkC,CAAE;EAC1C,MAAM,0BAAU,IAAI;EACpB,QAAQ,IAAI,KAAK;AACjB,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,cAAc,OAAO,eAAe,OAAO,QAAQ;EAIrD,MAAM,oBAAoB,OAAO,KAAK,QAAQ,CAAC,SAC3C,eAAe,eAAe,QAAQ,GACtC;EAGJ,MAAM,kBAAkB,QAAQ,mBAAmB,WAAW,QAAQ;AAEtE,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;GACT,QAAQ;EACT;CACF;CAED,uBAAiD;AAC/C,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;EACV;CACF;AACF"}
@@ -46,6 +46,20 @@ function isSerializableLike(obj) {
46
46
  return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
47
47
  }
48
48
  /**
49
+ * Create a "not_implemented" serialization result for objects that cannot be serialized.
50
+ */
51
+ function createNotImplemented(obj) {
52
+ let id;
53
+ if (obj !== null && typeof obj === "object") if ("lc_id" in obj && Array.isArray(obj.lc_id)) id = obj.lc_id;
54
+ else id = [obj.constructor?.name ?? "Object"];
55
+ else id = [typeof obj];
56
+ return {
57
+ lc: 1,
58
+ type: "not_implemented",
59
+ id
60
+ };
61
+ }
62
+ /**
49
63
  * Escape a value if it needs escaping (contains `lc` key).
50
64
  *
51
65
  * This is a simpler version of `serializeValue` that doesn't handle Serializable
@@ -53,18 +67,21 @@ function isSerializableLike(obj) {
53
67
  * processed by `toJSON()`.
54
68
  *
55
69
  * @param value - The value to potentially escape.
70
+ * @param visited - WeakSet to track visited objects and prevent infinite recursion.
56
71
  * @returns The value with any `lc`-containing objects wrapped in escape markers.
57
72
  */
58
- function escapeIfNeeded(value) {
73
+ function escapeIfNeeded(value, visited = /* @__PURE__ */ new WeakSet()) {
59
74
  if (value !== null && typeof value === "object" && !Array.isArray(value)) {
75
+ if (visited.has(value)) return createNotImplemented(value);
76
+ visited.add(value);
60
77
  if (isSerializableLike(value)) return value;
61
78
  const record = value;
62
79
  if (needsEscaping(record)) return escapeObject(record);
63
80
  const result = {};
64
- for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val);
81
+ for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val, visited);
65
82
  return result;
66
83
  }
67
- if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item));
84
+ if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item, visited));
68
85
  return value;
69
86
  }
70
87
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"validation.cjs","names":["obj: Record<string, unknown>","obj: unknown","value: unknown","result: Record<string, unknown>"],"sources":["../../src/load/validation.ts"],"sourcesContent":["/**\n * Sentinel key used to mark escaped user objects during serialization.\n *\n * When a plain object contains 'lc' key (which could be confused with LC objects),\n * we wrap it as `{\"__lc_escaped__\": {...original...}}`.\n */\nexport const LC_ESCAPED_KEY = \"__lc_escaped__\";\n\n/**\n * Check if an object needs escaping to prevent confusion with LC objects.\n *\n * An object needs escaping if:\n * 1. It has an `'lc'` key (could be confused with LC serialization format)\n * 2. It has only the escape key (would be mistaken for an escaped object)\n */\nexport function needsEscaping(obj: Record<string, unknown>): boolean {\n return (\n \"lc\" in obj || (Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj)\n );\n}\n\n/**\n * Wrap an object in the escape marker.\n *\n * @example\n * ```typescript\n * {\"key\": \"value\"} // becomes {\"__lc_escaped__\": {\"key\": \"value\"}}\n * ```\n */\nexport function escapeObject(\n obj: Record<string, unknown>\n): Record<string, unknown> {\n return { [LC_ESCAPED_KEY]: obj };\n}\n\n/**\n * Check if an object is an escaped user object.\n *\n * @example\n * ```typescript\n * {\"__lc_escaped__\": {...}} // is an escaped object\n * ```\n */\nexport function isEscapedObject(obj: Record<string, unknown>): boolean {\n return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\ninterface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\n/**\n * Check if an object looks like a Serializable instance (duck typing).\n */\nfunction isSerializableLike(obj: unknown): obj is SerializableLike {\n return (\n obj !== null &&\n typeof obj === \"object\" &&\n \"lc_serializable\" in obj &&\n typeof (obj as SerializableLike).toJSON === \"function\"\n );\n}\n\n/**\n * Create a \"not_implemented\" serialization result for objects that cannot be serialized.\n */\nfunction createNotImplemented(obj: unknown): {\n lc: 1;\n type: \"not_implemented\";\n id: string[];\n} {\n let id: string[];\n if (obj !== null && typeof obj === \"object\") {\n if (\"lc_id\" in obj && Array.isArray(obj.lc_id)) {\n id = obj.lc_id as string[];\n } else {\n id = [obj.constructor?.name ?? \"Object\"];\n }\n } else {\n id = [typeof obj];\n }\n return {\n lc: 1,\n type: \"not_implemented\",\n id,\n };\n}\n\n/**\n * Serialize a value with escaping of user objects.\n *\n * Called recursively on kwarg values to escape any plain objects that could be\n * confused with LC objects.\n *\n * @param obj - The value to serialize.\n * @returns The serialized value with user objects escaped as needed.\n */\nexport function serializeValue(obj: unknown): unknown {\n if (isSerializableLike(obj)) {\n // This is an LC object - serialize it properly (not escaped)\n return serializeLcObject(obj);\n }\n\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n // This prevents re-escaping of already-escaped nested content.\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = serializeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => serializeValue(item));\n }\n\n if (\n typeof obj === \"string\" ||\n typeof obj === \"number\" ||\n typeof obj === \"boolean\" ||\n obj === null\n ) {\n return obj;\n }\n\n // Non-JSON-serializable object (Date, custom objects, etc.)\n return createNotImplemented(obj);\n}\n\n/**\n * Serialize a `Serializable` object with escaping of user data in kwargs.\n *\n * @param obj - The `Serializable` object to serialize.\n * @returns The serialized object with user data in kwargs escaped as needed.\n *\n * @remarks\n * Kwargs values are processed with `serializeValue` to escape user data (like\n * metadata) that contains `'lc'` keys. Secret fields (from `lc_secrets`) are\n * skipped because `toJSON()` replaces their values with secret markers.\n */\nexport function serializeLcObject(obj: SerializableLike): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n} {\n // Secret fields are handled by toJSON() - it replaces values with secret markers\n const secretFields = new Set(Object.keys(obj.lc_secrets ?? {}));\n\n const serialized = { ...obj.toJSON() };\n\n // Process kwargs to escape user data that could be confused with LC objects\n // Skip secret fields - toJSON() already converted them to secret markers\n if (serialized.type === \"constructor\" && serialized.kwargs) {\n const newKwargs: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(serialized.kwargs)) {\n if (secretFields.has(key)) {\n newKwargs[key] = value;\n } else {\n newKwargs[key] = serializeValue(value);\n }\n }\n serialized.kwargs = newKwargs;\n }\n\n return serialized;\n}\n\n/**\n * Escape a value if it needs escaping (contains `lc` key).\n *\n * This is a simpler version of `serializeValue` that doesn't handle Serializable\n * objects - it's meant to be called on kwargs values that have already been\n * processed by `toJSON()`.\n *\n * @param value - The value to potentially escape.\n * @returns The value with any `lc`-containing objects wrapped in escape markers.\n */\nexport function escapeIfNeeded(value: unknown): unknown {\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n // Preserve Serializable objects - they have their own toJSON() that will be\n // called by JSON.stringify. We don't want to convert them to plain objects.\n if (isSerializableLike(value)) {\n return value;\n }\n const record = value as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(record)) {\n result[key] = escapeIfNeeded(val);\n }\n return result;\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => escapeIfNeeded(item));\n }\n\n return value;\n}\n\n/**\n * Unescape a value, processing escape markers in object values and arrays.\n *\n * When an escaped object is encountered (`{\"__lc_escaped__\": ...}`), it's\n * unwrapped and the contents are returned AS-IS (no further processing).\n * The contents represent user data that should not be modified.\n *\n * For regular objects and arrays, we recurse to find any nested escape markers.\n *\n * @param obj - The value to unescape.\n * @returns The unescaped value.\n */\nexport function unescapeValue(obj: unknown): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n if (isEscapedObject(record)) {\n // Unwrap and return the user data as-is (no further unescaping).\n // The contents are user data that may contain more escape keys,\n // but those are part of the user's actual data.\n return record[LC_ESCAPED_KEY];\n }\n\n // Regular object - recurse into values to find nested escape markers\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = unescapeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => unescapeValue(item));\n }\n\n return obj;\n}\n"],"mappings":";;;;;;;;AAMA,MA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,CACpB,QAAO,IAAI,IAAI,CAAC,SAAS,cAAc,KAAK,CAAC;AAG/C,QAAO;AACR"}
1
+ {"version":3,"file":"validation.cjs","names":["obj: Record<string, unknown>","obj: unknown","id: string[]","value: unknown","visited: WeakSet<object>","result: Record<string, unknown>"],"sources":["../../src/load/validation.ts"],"sourcesContent":["/**\n * Sentinel key used to mark escaped user objects during serialization.\n *\n * When a plain object contains 'lc' key (which could be confused with LC objects),\n * we wrap it as `{\"__lc_escaped__\": {...original...}}`.\n */\nexport const LC_ESCAPED_KEY = \"__lc_escaped__\";\n\n/**\n * Check if an object needs escaping to prevent confusion with LC objects.\n *\n * An object needs escaping if:\n * 1. It has an `'lc'` key (could be confused with LC serialization format)\n * 2. It has only the escape key (would be mistaken for an escaped object)\n */\nexport function needsEscaping(obj: Record<string, unknown>): boolean {\n return (\n \"lc\" in obj || (Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj)\n );\n}\n\n/**\n * Wrap an object in the escape marker.\n *\n * @example\n * ```typescript\n * {\"key\": \"value\"} // becomes {\"__lc_escaped__\": {\"key\": \"value\"}}\n * ```\n */\nexport function escapeObject(\n obj: Record<string, unknown>\n): Record<string, unknown> {\n return { [LC_ESCAPED_KEY]: obj };\n}\n\n/**\n * Check if an object is an escaped user object.\n *\n * @example\n * ```typescript\n * {\"__lc_escaped__\": {...}} // is an escaped object\n * ```\n */\nexport function isEscapedObject(obj: Record<string, unknown>): boolean {\n return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\ninterface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\n/**\n * Check if an object looks like a Serializable instance (duck typing).\n */\nfunction isSerializableLike(obj: unknown): obj is SerializableLike {\n return (\n obj !== null &&\n typeof obj === \"object\" &&\n \"lc_serializable\" in obj &&\n typeof (obj as SerializableLike).toJSON === \"function\"\n );\n}\n\n/**\n * Create a \"not_implemented\" serialization result for objects that cannot be serialized.\n */\nfunction createNotImplemented(obj: unknown): {\n lc: 1;\n type: \"not_implemented\";\n id: string[];\n} {\n let id: string[];\n if (obj !== null && typeof obj === \"object\") {\n if (\"lc_id\" in obj && Array.isArray(obj.lc_id)) {\n id = obj.lc_id as string[];\n } else {\n id = [obj.constructor?.name ?? \"Object\"];\n }\n } else {\n id = [typeof obj];\n }\n return {\n lc: 1,\n type: \"not_implemented\",\n id,\n };\n}\n\n/**\n * Serialize a value with escaping of user objects.\n *\n * Called recursively on kwarg values to escape any plain objects that could be\n * confused with LC objects.\n *\n * @param obj - The value to serialize.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The serialized value with user objects escaped as needed.\n */\nexport function serializeValue(\n obj: unknown,\n visited: WeakSet<object> = new WeakSet()\n): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n // Check for circular reference first, even for Serializable objects\n if (visited.has(obj)) {\n return createNotImplemented(obj);\n }\n\n if (isSerializableLike(obj)) {\n // This is an LC object - serialize it properly (not escaped)\n return serializeLcObject(obj, visited);\n }\n\n visited.add(obj);\n\n const record = obj as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n // This prevents re-escaping of already-escaped nested content.\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = serializeValue(value, visited);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n // Arrays don't need circular reference tracking since they're handled by object tracking\n return obj.map((item) => serializeValue(item, visited));\n }\n\n if (\n typeof obj === \"string\" ||\n typeof obj === \"number\" ||\n typeof obj === \"boolean\" ||\n obj === null\n ) {\n return obj;\n }\n\n // Non-JSON-serializable object (Date, custom objects, etc.)\n return createNotImplemented(obj);\n}\n\n/**\n * Serialize a `Serializable` object with escaping of user data in kwargs.\n *\n * @param obj - The `Serializable` object to serialize.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The serialized object with user data in kwargs escaped as needed.\n *\n * @remarks\n * Kwargs values are processed with `serializeValue` to escape user data (like\n * metadata) that contains `'lc'` keys. Secret fields (from `lc_secrets`) are\n * skipped because `toJSON()` replaces their values with secret markers.\n */\nexport function serializeLcObject(\n obj: SerializableLike,\n visited: WeakSet<object> = new WeakSet()\n): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n} {\n // Add object to visited set to prevent circular references\n // (e.g., if the object appears in its own kwargs)\n visited.add(obj);\n\n // Secret fields are handled by toJSON() - it replaces values with secret markers\n const secretFields = new Set(Object.keys(obj.lc_secrets ?? {}));\n\n const serialized = { ...obj.toJSON() };\n\n // Process kwargs to escape user data that could be confused with LC objects\n // Skip secret fields - toJSON() already converted them to secret markers\n if (serialized.type === \"constructor\" && serialized.kwargs) {\n const newKwargs: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(serialized.kwargs)) {\n if (secretFields.has(key)) {\n newKwargs[key] = value;\n } else {\n newKwargs[key] = serializeValue(value, visited);\n }\n }\n serialized.kwargs = newKwargs;\n }\n\n return serialized;\n}\n\n/**\n * Escape a value if it needs escaping (contains `lc` key).\n *\n * This is a simpler version of `serializeValue` that doesn't handle Serializable\n * objects - it's meant to be called on kwargs values that have already been\n * processed by `toJSON()`.\n *\n * @param value - The value to potentially escape.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The value with any `lc`-containing objects wrapped in escape markers.\n */\nexport function escapeIfNeeded(\n value: unknown,\n visited: WeakSet<object> = new WeakSet()\n): unknown {\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n // Check for circular reference first, even for Serializable objects\n if (visited.has(value)) {\n // Replace circular reference with a not_implemented marker\n return createNotImplemented(value);\n }\n visited.add(value);\n\n // Preserve Serializable objects - they have their own toJSON() that will be\n // called by JSON.stringify. We don't want to convert them to plain objects.\n if (isSerializableLike(value)) {\n return value;\n }\n\n const record = value as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(record)) {\n result[key] = escapeIfNeeded(val, visited);\n }\n return result;\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => escapeIfNeeded(item, visited));\n }\n\n return value;\n}\n\n/**\n * Unescape a value, processing escape markers in object values and arrays.\n *\n * When an escaped object is encountered (`{\"__lc_escaped__\": ...}`), it's\n * unwrapped and the contents are returned AS-IS (no further processing).\n * The contents represent user data that should not be modified.\n *\n * For regular objects and arrays, we recurse to find any nested escape markers.\n *\n * @param obj - The value to unescape.\n * @returns The unescaped value.\n */\nexport function unescapeValue(obj: unknown): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n if (isEscapedObject(record)) {\n // Unwrap and return the user data as-is (no further unescaping).\n // The contents are user data that may contain more escape keys,\n // but those are part of the user's actual data.\n return record[LC_ESCAPED_KEY];\n }\n\n // Regular object - recurse into values to find nested escape markers\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = unescapeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => unescapeValue(item));\n }\n\n return obj;\n}\n"],"mappings":";;;;;;;;AAMA,MAAa,iBAAiB;;;;;;;;AAS9B,SAAgB,cAAcA,KAAuC;AACnE,QACE,QAAQ,OAAQ,OAAO,KAAK,IAAI,CAAC,WAAW,KAAK,kBAAkB;AAEtE;;;;;;;;;AAUD,SAAgB,aACdA,KACyB;AACzB,QAAO,GAAG,iBAAiB,I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,MAAI,gBAAgB,OAAO,CAIzB,QAAO,OAAO;EAIhB,MAAMI,SAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,OAAO,OAAO,cAAc,MAAM;AAEpC,SAAO;CACR;AAED,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,IAAI,CAAC,SAAS,cAAc,KAAK,CAAC;AAG/C,QAAO;AACR"}
@@ -45,6 +45,20 @@ function isSerializableLike(obj) {
45
45
  return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
46
46
  }
47
47
  /**
48
+ * Create a "not_implemented" serialization result for objects that cannot be serialized.
49
+ */
50
+ function createNotImplemented(obj) {
51
+ let id;
52
+ if (obj !== null && typeof obj === "object") if ("lc_id" in obj && Array.isArray(obj.lc_id)) id = obj.lc_id;
53
+ else id = [obj.constructor?.name ?? "Object"];
54
+ else id = [typeof obj];
55
+ return {
56
+ lc: 1,
57
+ type: "not_implemented",
58
+ id
59
+ };
60
+ }
61
+ /**
48
62
  * Escape a value if it needs escaping (contains `lc` key).
49
63
  *
50
64
  * This is a simpler version of `serializeValue` that doesn't handle Serializable
@@ -52,18 +66,21 @@ function isSerializableLike(obj) {
52
66
  * processed by `toJSON()`.
53
67
  *
54
68
  * @param value - The value to potentially escape.
69
+ * @param visited - WeakSet to track visited objects and prevent infinite recursion.
55
70
  * @returns The value with any `lc`-containing objects wrapped in escape markers.
56
71
  */
57
- function escapeIfNeeded(value) {
72
+ function escapeIfNeeded(value, visited = /* @__PURE__ */ new WeakSet()) {
58
73
  if (value !== null && typeof value === "object" && !Array.isArray(value)) {
74
+ if (visited.has(value)) return createNotImplemented(value);
75
+ visited.add(value);
59
76
  if (isSerializableLike(value)) return value;
60
77
  const record = value;
61
78
  if (needsEscaping(record)) return escapeObject(record);
62
79
  const result = {};
63
- for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val);
80
+ for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val, visited);
64
81
  return result;
65
82
  }
66
- if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item));
83
+ if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item, visited));
67
84
  return value;
68
85
  }
69
86
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"validation.js","names":["obj: Record<string, unknown>","obj: unknown","value: unknown","result: Record<string, unknown>"],"sources":["../../src/load/validation.ts"],"sourcesContent":["/**\n * Sentinel key used to mark escaped user objects during serialization.\n *\n * When a plain object contains 'lc' key (which could be confused with LC objects),\n * we wrap it as `{\"__lc_escaped__\": {...original...}}`.\n */\nexport const LC_ESCAPED_KEY = \"__lc_escaped__\";\n\n/**\n * Check if an object needs escaping to prevent confusion with LC objects.\n *\n * An object needs escaping if:\n * 1. It has an `'lc'` key (could be confused with LC serialization format)\n * 2. It has only the escape key (would be mistaken for an escaped object)\n */\nexport function needsEscaping(obj: Record<string, unknown>): boolean {\n return (\n \"lc\" in obj || (Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj)\n );\n}\n\n/**\n * Wrap an object in the escape marker.\n *\n * @example\n * ```typescript\n * {\"key\": \"value\"} // becomes {\"__lc_escaped__\": {\"key\": \"value\"}}\n * ```\n */\nexport function escapeObject(\n obj: Record<string, unknown>\n): Record<string, unknown> {\n return { [LC_ESCAPED_KEY]: obj };\n}\n\n/**\n * Check if an object is an escaped user object.\n *\n * @example\n * ```typescript\n * {\"__lc_escaped__\": {...}} // is an escaped object\n * ```\n */\nexport function isEscapedObject(obj: Record<string, unknown>): boolean {\n return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\ninterface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\n/**\n * Check if an object looks like a Serializable instance (duck typing).\n */\nfunction isSerializableLike(obj: unknown): obj is SerializableLike {\n return (\n obj !== null &&\n typeof obj === \"object\" &&\n \"lc_serializable\" in obj &&\n typeof (obj as SerializableLike).toJSON === \"function\"\n );\n}\n\n/**\n * Create a \"not_implemented\" serialization result for objects that cannot be serialized.\n */\nfunction createNotImplemented(obj: unknown): {\n lc: 1;\n type: \"not_implemented\";\n id: string[];\n} {\n let id: string[];\n if (obj !== null && typeof obj === \"object\") {\n if (\"lc_id\" in obj && Array.isArray(obj.lc_id)) {\n id = obj.lc_id as string[];\n } else {\n id = [obj.constructor?.name ?? \"Object\"];\n }\n } else {\n id = [typeof obj];\n }\n return {\n lc: 1,\n type: \"not_implemented\",\n id,\n };\n}\n\n/**\n * Serialize a value with escaping of user objects.\n *\n * Called recursively on kwarg values to escape any plain objects that could be\n * confused with LC objects.\n *\n * @param obj - The value to serialize.\n * @returns The serialized value with user objects escaped as needed.\n */\nexport function serializeValue(obj: unknown): unknown {\n if (isSerializableLike(obj)) {\n // This is an LC object - serialize it properly (not escaped)\n return serializeLcObject(obj);\n }\n\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n // This prevents re-escaping of already-escaped nested content.\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = serializeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => serializeValue(item));\n }\n\n if (\n typeof obj === \"string\" ||\n typeof obj === \"number\" ||\n typeof obj === \"boolean\" ||\n obj === null\n ) {\n return obj;\n }\n\n // Non-JSON-serializable object (Date, custom objects, etc.)\n return createNotImplemented(obj);\n}\n\n/**\n * Serialize a `Serializable` object with escaping of user data in kwargs.\n *\n * @param obj - The `Serializable` object to serialize.\n * @returns The serialized object with user data in kwargs escaped as needed.\n *\n * @remarks\n * Kwargs values are processed with `serializeValue` to escape user data (like\n * metadata) that contains `'lc'` keys. Secret fields (from `lc_secrets`) are\n * skipped because `toJSON()` replaces their values with secret markers.\n */\nexport function serializeLcObject(obj: SerializableLike): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n} {\n // Secret fields are handled by toJSON() - it replaces values with secret markers\n const secretFields = new Set(Object.keys(obj.lc_secrets ?? {}));\n\n const serialized = { ...obj.toJSON() };\n\n // Process kwargs to escape user data that could be confused with LC objects\n // Skip secret fields - toJSON() already converted them to secret markers\n if (serialized.type === \"constructor\" && serialized.kwargs) {\n const newKwargs: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(serialized.kwargs)) {\n if (secretFields.has(key)) {\n newKwargs[key] = value;\n } else {\n newKwargs[key] = serializeValue(value);\n }\n }\n serialized.kwargs = newKwargs;\n }\n\n return serialized;\n}\n\n/**\n * Escape a value if it needs escaping (contains `lc` key).\n *\n * This is a simpler version of `serializeValue` that doesn't handle Serializable\n * objects - it's meant to be called on kwargs values that have already been\n * processed by `toJSON()`.\n *\n * @param value - The value to potentially escape.\n * @returns The value with any `lc`-containing objects wrapped in escape markers.\n */\nexport function escapeIfNeeded(value: unknown): unknown {\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n // Preserve Serializable objects - they have their own toJSON() that will be\n // called by JSON.stringify. We don't want to convert them to plain objects.\n if (isSerializableLike(value)) {\n return value;\n }\n const record = value as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(record)) {\n result[key] = escapeIfNeeded(val);\n }\n return result;\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => escapeIfNeeded(item));\n }\n\n return value;\n}\n\n/**\n * Unescape a value, processing escape markers in object values and arrays.\n *\n * When an escaped object is encountered (`{\"__lc_escaped__\": ...}`), it's\n * unwrapped and the contents are returned AS-IS (no further processing).\n * The contents represent user data that should not be modified.\n *\n * For regular objects and arrays, we recurse to find any nested escape markers.\n *\n * @param obj - The value to unescape.\n * @returns The unescaped value.\n */\nexport function unescapeValue(obj: unknown): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n if (isEscapedObject(record)) {\n // Unwrap and return the user data as-is (no further unescaping).\n // The contents are user data that may contain more escape keys,\n // but those are part of the user's actual data.\n return record[LC_ESCAPED_KEY];\n }\n\n // Regular object - recurse into values to find nested escape markers\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = unescapeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => unescapeValue(item));\n }\n\n return obj;\n}\n"],"mappings":";;;;;;;AAMA,MAAa,iBAAiB;;;;;;;;AAS9B,SAAgB,cAAcA,KAAuC;AACnE,QACE,QAAQ,OAAQ,OAAO,KAAK,IAAI,CAAC,WAAW,KAAK,kBAAkB;AAEtE;;;;;;;;;AAUD,SAAgB,aACdA,KACyB;AACzB,QAAO,GAAG,iBAAiB,IAAK;AACjC;;;;;;;;;AAUD,SAAgB,gBAAgBA,KAAuC;AACrE,QAAO,OAAO,KAAK,IAAI,CAAC,WAAW,KAAK,kBAAkB;AAC3D;;;;AAoBD,SAAS,mBAAmBC,KAAuC;AACjE,QACE,QAAQ,QACR,OAAO,QAAQ,YACf,qBAAqB,OACrB,OAAQ,IAAyB,WAAW;AAE/C;;;;;;;;;;;AA6HD,SAAgB,eAAeC,OAAyB;AACtD,KAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM,EAAE;AAGxE,MAAI,mBAAmB,MAAM,CAC3B,QAAO;EAET,MAAM,SAAS;AAIf,MAAI,cAAc,OAAO,CACvB,QAAO,aAAa,OAAO;EAG7B,MAAMC,SAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,IAAI,IAAI,OAAO,QAAQ,OAAO,EAC7C,OAAO,OAAO,eAAe,IAAI;AAEnC,SAAO;CACR;AAED,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,IAAI,CAAC,SAAS,eAAe,KAAK,CAAC;AAGlD,QAAO;AACR;;;;;;;;;;;;;AAcD,SAAgB,cAAcF,KAAuB;AACnD,KAAI,QAAQ,QAAQ,OAAO,QAAQ,YAAY,CAAC,MAAM,QAAQ,IAAI,EAAE;EAClE,MAAM,SAAS;AACf,MAAI,gBAAgB,OAAO,CAIzB,QAAO,OAAO;EAIhB,MAAME,SAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,OAAO,OAAO,cAAc,MAAM;AAEpC,SAAO;CACR;AAED,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,IAAI,CAAC,SAAS,cAAc,KAAK,CAAC;AAG/C,QAAO;AACR"}
1
+ {"version":3,"file":"validation.js","names":["obj: Record<string, unknown>","obj: unknown","id: string[]","value: unknown","visited: WeakSet<object>","result: Record<string, unknown>"],"sources":["../../src/load/validation.ts"],"sourcesContent":["/**\n * Sentinel key used to mark escaped user objects during serialization.\n *\n * When a plain object contains 'lc' key (which could be confused with LC objects),\n * we wrap it as `{\"__lc_escaped__\": {...original...}}`.\n */\nexport const LC_ESCAPED_KEY = \"__lc_escaped__\";\n\n/**\n * Check if an object needs escaping to prevent confusion with LC objects.\n *\n * An object needs escaping if:\n * 1. It has an `'lc'` key (could be confused with LC serialization format)\n * 2. It has only the escape key (would be mistaken for an escaped object)\n */\nexport function needsEscaping(obj: Record<string, unknown>): boolean {\n return (\n \"lc\" in obj || (Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj)\n );\n}\n\n/**\n * Wrap an object in the escape marker.\n *\n * @example\n * ```typescript\n * {\"key\": \"value\"} // becomes {\"__lc_escaped__\": {\"key\": \"value\"}}\n * ```\n */\nexport function escapeObject(\n obj: Record<string, unknown>\n): Record<string, unknown> {\n return { [LC_ESCAPED_KEY]: obj };\n}\n\n/**\n * Check if an object is an escaped user object.\n *\n * @example\n * ```typescript\n * {\"__lc_escaped__\": {...}} // is an escaped object\n * ```\n */\nexport function isEscapedObject(obj: Record<string, unknown>): boolean {\n return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n\n/**\n * Interface for objects that can be serialized.\n * This is a duck-typed interface to avoid circular imports.\n */\ninterface SerializableLike {\n lc_serializable: boolean;\n lc_secrets?: Record<string, string>;\n toJSON(): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n };\n}\n\n/**\n * Check if an object looks like a Serializable instance (duck typing).\n */\nfunction isSerializableLike(obj: unknown): obj is SerializableLike {\n return (\n obj !== null &&\n typeof obj === \"object\" &&\n \"lc_serializable\" in obj &&\n typeof (obj as SerializableLike).toJSON === \"function\"\n );\n}\n\n/**\n * Create a \"not_implemented\" serialization result for objects that cannot be serialized.\n */\nfunction createNotImplemented(obj: unknown): {\n lc: 1;\n type: \"not_implemented\";\n id: string[];\n} {\n let id: string[];\n if (obj !== null && typeof obj === \"object\") {\n if (\"lc_id\" in obj && Array.isArray(obj.lc_id)) {\n id = obj.lc_id as string[];\n } else {\n id = [obj.constructor?.name ?? \"Object\"];\n }\n } else {\n id = [typeof obj];\n }\n return {\n lc: 1,\n type: \"not_implemented\",\n id,\n };\n}\n\n/**\n * Serialize a value with escaping of user objects.\n *\n * Called recursively on kwarg values to escape any plain objects that could be\n * confused with LC objects.\n *\n * @param obj - The value to serialize.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The serialized value with user objects escaped as needed.\n */\nexport function serializeValue(\n obj: unknown,\n visited: WeakSet<object> = new WeakSet()\n): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n // Check for circular reference first, even for Serializable objects\n if (visited.has(obj)) {\n return createNotImplemented(obj);\n }\n\n if (isSerializableLike(obj)) {\n // This is an LC object - serialize it properly (not escaped)\n return serializeLcObject(obj, visited);\n }\n\n visited.add(obj);\n\n const record = obj as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n // This prevents re-escaping of already-escaped nested content.\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = serializeValue(value, visited);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n // Arrays don't need circular reference tracking since they're handled by object tracking\n return obj.map((item) => serializeValue(item, visited));\n }\n\n if (\n typeof obj === \"string\" ||\n typeof obj === \"number\" ||\n typeof obj === \"boolean\" ||\n obj === null\n ) {\n return obj;\n }\n\n // Non-JSON-serializable object (Date, custom objects, etc.)\n return createNotImplemented(obj);\n}\n\n/**\n * Serialize a `Serializable` object with escaping of user data in kwargs.\n *\n * @param obj - The `Serializable` object to serialize.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The serialized object with user data in kwargs escaped as needed.\n *\n * @remarks\n * Kwargs values are processed with `serializeValue` to escape user data (like\n * metadata) that contains `'lc'` keys. Secret fields (from `lc_secrets`) are\n * skipped because `toJSON()` replaces their values with secret markers.\n */\nexport function serializeLcObject(\n obj: SerializableLike,\n visited: WeakSet<object> = new WeakSet()\n): {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n} {\n // Add object to visited set to prevent circular references\n // (e.g., if the object appears in its own kwargs)\n visited.add(obj);\n\n // Secret fields are handled by toJSON() - it replaces values with secret markers\n const secretFields = new Set(Object.keys(obj.lc_secrets ?? {}));\n\n const serialized = { ...obj.toJSON() };\n\n // Process kwargs to escape user data that could be confused with LC objects\n // Skip secret fields - toJSON() already converted them to secret markers\n if (serialized.type === \"constructor\" && serialized.kwargs) {\n const newKwargs: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(serialized.kwargs)) {\n if (secretFields.has(key)) {\n newKwargs[key] = value;\n } else {\n newKwargs[key] = serializeValue(value, visited);\n }\n }\n serialized.kwargs = newKwargs;\n }\n\n return serialized;\n}\n\n/**\n * Escape a value if it needs escaping (contains `lc` key).\n *\n * This is a simpler version of `serializeValue` that doesn't handle Serializable\n * objects - it's meant to be called on kwargs values that have already been\n * processed by `toJSON()`.\n *\n * @param value - The value to potentially escape.\n * @param visited - WeakSet to track visited objects and prevent infinite recursion.\n * @returns The value with any `lc`-containing objects wrapped in escape markers.\n */\nexport function escapeIfNeeded(\n value: unknown,\n visited: WeakSet<object> = new WeakSet()\n): unknown {\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n // Check for circular reference first, even for Serializable objects\n if (visited.has(value)) {\n // Replace circular reference with a not_implemented marker\n return createNotImplemented(value);\n }\n visited.add(value);\n\n // Preserve Serializable objects - they have their own toJSON() that will be\n // called by JSON.stringify. We don't want to convert them to plain objects.\n if (isSerializableLike(value)) {\n return value;\n }\n\n const record = value as Record<string, unknown>;\n // Check if object needs escaping BEFORE recursing into values.\n // If it needs escaping, wrap it as-is - the contents are user data that\n // will be returned as-is during deserialization (no instantiation).\n if (needsEscaping(record)) {\n return escapeObject(record);\n }\n // Safe object (no 'lc' key) - recurse into values\n const result: Record<string, unknown> = {};\n for (const [key, val] of Object.entries(record)) {\n result[key] = escapeIfNeeded(val, visited);\n }\n return result;\n }\n\n if (Array.isArray(value)) {\n return value.map((item) => escapeIfNeeded(item, visited));\n }\n\n return value;\n}\n\n/**\n * Unescape a value, processing escape markers in object values and arrays.\n *\n * When an escaped object is encountered (`{\"__lc_escaped__\": ...}`), it's\n * unwrapped and the contents are returned AS-IS (no further processing).\n * The contents represent user data that should not be modified.\n *\n * For regular objects and arrays, we recurse to find any nested escape markers.\n *\n * @param obj - The value to unescape.\n * @returns The unescaped value.\n */\nexport function unescapeValue(obj: unknown): unknown {\n if (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n const record = obj as Record<string, unknown>;\n if (isEscapedObject(record)) {\n // Unwrap and return the user data as-is (no further unescaping).\n // The contents are user data that may contain more escape keys,\n // but those are part of the user's actual data.\n return record[LC_ESCAPED_KEY];\n }\n\n // Regular object - recurse into values to find nested escape markers\n const result: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(record)) {\n result[key] = unescapeValue(value);\n }\n return result;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => unescapeValue(item));\n }\n\n return obj;\n}\n"],"mappings":";;;;;;;AAMA,MAAa,iBAAiB;;;;;;;;AAS9B,SAAgB,cAAcA,KAAuC;AACnE,QACE,QAAQ,OAAQ,OAAO,KAAK,IAAI,CAAC,WAAW,KAAK,kBAAkB;AAEtE;;;;;;;;;AAUD,SAAgB,aACdA,KACyB;AACzB,QAAO,GAAG,iBAAiB,IAAK;AACjC;;;;;;;;;AAUD,SAAgB,gBAAgBA,KAAuC;AACrE,QAAO,OAAO,KAAK,IAAI,CAAC,WAAW,KAAK,kBAAkB;AAC3D;;;;AAoBD,SAAS,mBAAmBC,KAAuC;AACjE,QACE,QAAQ,QACR,OAAO,QAAQ,YACf,qBAAqB,OACrB,OAAQ,IAAyB,WAAW;AAE/C;;;;AAKD,SAAS,qBAAqBA,KAI5B;CACA,IAAIC;AACJ,KAAI,QAAQ,QAAQ,OAAO,QAAQ,SACjC,KAAI,WAAW,OAAO,MAAM,QAAQ,IAAI,MAAM,EAC5C,KAAK,IAAI;MAET,KAAK,CAAC,IAAI,aAAa,QAAQ,QAAS;MAG1C,KAAK,CAAC,OAAO,GAAI;AAEnB,QAAO;EACL,IAAI;EACJ,MAAM;EACN;CACD;AACF;;;;;;;;;;;;AAyHD,SAAgB,eACdC,OACAC,0BAA2B,IAAI,WACtB;AACT,KAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,MAAM,EAAE;AAExE,MAAI,QAAQ,IAAI,MAAM,CAEpB,QAAO,qBAAqB,MAAM;EAEpC,QAAQ,IAAI,MAAM;AAIlB,MAAI,mBAAmB,MAAM,CAC3B,QAAO;EAGT,MAAM,SAAS;AAIf,MAAI,cAAc,OAAO,CACvB,QAAO,aAAa,OAAO;EAG7B,MAAMC,SAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,IAAI,IAAI,OAAO,QAAQ,OAAO,EAC7C,OAAO,OAAO,eAAe,KAAK,QAAQ;AAE5C,SAAO;CACR;AAED,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,IAAI,CAAC,SAAS,eAAe,MAAM,QAAQ,CAAC;AAG3D,QAAO;AACR;;;;;;;;;;;;;AAcD,SAAgB,cAAcJ,KAAuB;AACnD,KAAI,QAAQ,QAAQ,OAAO,QAAQ,YAAY,CAAC,MAAM,QAAQ,IAAI,EAAE;EAClE,MAAM,SAAS;AACf,MAAI,gBAAgB,OAAO,CAIzB,QAAO,OAAO;EAIhB,MAAMI,SAAkC,CAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,EAC/C,OAAO,OAAO,cAAc,MAAM;AAEpC,SAAO;CACR;AAED,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,IAAI,CAAC,SAAS,cAAc,KAAK,CAAC;AAG/C,QAAO;AACR"}
@@ -202,6 +202,7 @@ function _mergeDicts(left = {}, right = {}) {
202
202
  ].includes(key)) {
203
203
  if (value) merged[key] = value;
204
204
  } else merged[key] += value;
205
+ else if (typeof merged[key] === "number") merged[key] = merged[key] + value;
205
206
  else if (typeof merged[key] === "object" && !Array.isArray(merged[key])) merged[key] = _mergeDicts(merged[key], value);
206
207
  else if (Array.isArray(merged[key])) merged[key] = _mergeLists(merged[key], value);
207
208
  else if (merged[key] === value) continue;
@@ -1 +1 @@
1
- {"version":3,"file":"base.cjs","names":["firstContent: MessageContent","secondContent: MessageContent","isDataContentBlock","left?: \"success\" | \"error\"","right?: \"success\" | \"error\"","obj: any","depthLimit: number","currentDepth: number","obj","result: Record<string, unknown>","Serializable","arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>","fields: BaseMessageFields<TStructure, TRole>","blocks: Array<ContentBlock>","convertToV1FromDataContent","convertToV1FromChatCompletionsInput","convertToV1FromAnthropicInput","blocks","obj: unknown","isMessage","value: string | undefined","depth: number | null","format: MessageStringFormat","convertToFormattedString","value?: unknown","left: Record<string, any>","right: Record<string, any>","left?: Content[]","right?: Content[]","left: T | undefined","right: T | undefined","x: BaseMessageLike","messageLike?: unknown"],"sources":["../../src/messages/base.ts"],"sourcesContent":["import { Serializable, SerializedConstructor } from \"../load/serializable.js\";\nimport { ContentBlock } from \"./content/index.js\";\nimport { isDataContentBlock } from \"./content/data.js\";\nimport { convertToV1FromAnthropicInput } from \"./block_translators/anthropic.js\";\nimport { convertToV1FromDataContent } from \"./block_translators/data.js\";\nimport { convertToV1FromChatCompletionsInput } from \"./block_translators/openai.js\";\nimport {\n $InferMessageContent,\n $InferResponseMetadata,\n MessageStructure,\n MessageType,\n isMessage,\n Message,\n} from \"./message.js\";\nimport {\n convertToFormattedString,\n type MessageStringFormat,\n} from \"./format.js\";\n\n/** @internal */\nconst MESSAGE_SYMBOL = Symbol.for(\"langchain.message\");\n\nexport interface StoredMessageData {\n content: string;\n role: string | undefined;\n name: string | undefined;\n tool_call_id: string | undefined;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n additional_kwargs?: Record<string, any>;\n /** Response metadata. For example: response headers, logprobs, token counts, model name. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n response_metadata?: Record<string, any>;\n id?: string;\n}\n\nexport interface StoredMessage {\n type: string;\n data: StoredMessageData;\n}\n\nexport interface StoredGeneration {\n text: string;\n message?: StoredMessage;\n}\n\nexport interface StoredMessageV1 {\n type: string;\n role: string | undefined;\n text: string;\n}\n\nexport type MessageContent = string | Array<ContentBlock>;\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n}\n\nexport type BaseMessageFields<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> = Pick<Message, \"id\" | \"name\"> & {\n content?: $InferMessageContent<TStructure, TRole>;\n contentBlocks?: Array<ContentBlock.Standard>;\n /** @deprecated */\n additional_kwargs?: {\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n function_call?: FunctionCall;\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n tool_calls?: OpenAIToolCall[];\n [key: string]: unknown;\n };\n response_metadata?: Partial<$InferResponseMetadata<TStructure, TRole>>;\n};\n\nexport function mergeContent(\n firstContent: MessageContent,\n secondContent: MessageContent\n): MessageContent {\n // If first content is a string\n if (typeof firstContent === \"string\") {\n if (firstContent === \"\") {\n return secondContent;\n }\n if (typeof secondContent === \"string\") {\n return firstContent + secondContent;\n } else if (Array.isArray(secondContent) && secondContent.length === 0) {\n return firstContent;\n } else if (\n Array.isArray(secondContent) &&\n secondContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n {\n type: \"text\",\n source_type: \"text\",\n text: firstContent,\n },\n ...secondContent,\n ];\n } else {\n return [{ type: \"text\", text: firstContent }, ...secondContent];\n }\n // If both are arrays\n } else if (Array.isArray(secondContent)) {\n return (\n _mergeLists(firstContent, secondContent) ?? [\n ...firstContent,\n ...secondContent,\n ]\n );\n } else {\n if (secondContent === \"\") {\n return firstContent;\n } else if (\n Array.isArray(firstContent) &&\n firstContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n ...firstContent,\n {\n type: \"file\",\n source_type: \"text\",\n text: secondContent,\n },\n ];\n } else {\n return [...firstContent, { type: \"text\", text: secondContent }];\n }\n }\n}\n\n/**\n * 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else\n * it will return 'success'.\n *\n * @param {\"success\" | \"error\" | undefined} left The existing value to 'merge' with the new value.\n * @param {\"success\" | \"error\" | undefined} right The new value to 'merge' with the existing value\n * @returns {\"success\" | \"error\"} The 'merged' value.\n */\nexport function _mergeStatus(\n left?: \"success\" | \"error\",\n right?: \"success\" | \"error\"\n): \"success\" | \"error\" | undefined {\n if (left === \"error\" || right === \"error\") {\n return \"error\";\n }\n return \"success\";\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction stringifyWithDepthLimit(obj: any, depthLimit: number): string {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n function helper(obj: any, currentDepth: number): any {\n if (typeof obj !== \"object\" || obj === null || obj === undefined) {\n return obj;\n }\n if (currentDepth >= depthLimit) {\n if (Array.isArray(obj)) {\n return \"[Array]\";\n }\n return \"[Object]\";\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => helper(item, currentDepth + 1));\n }\n\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(obj)) {\n result[key] = helper(obj[key], currentDepth + 1);\n }\n return result;\n }\n\n return JSON.stringify(helper(obj, 0), null, 2);\n}\n\n/**\n * Base class for all types of messages in a conversation. It includes\n * properties like `content`, `name`, and `additional_kwargs`. It also\n * includes methods like `toDict()` and `_getType()`.\n */\nexport abstract class BaseMessage<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n >\n extends Serializable\n implements Message<TStructure, TRole>\n{\n lc_namespace = [\"langchain_core\", \"messages\"];\n\n lc_serializable = true;\n\n get lc_aliases(): Record<string, string> {\n // exclude snake case conversion to pascal case\n return {\n additional_kwargs: \"additional_kwargs\",\n response_metadata: \"response_metadata\",\n };\n }\n\n readonly [MESSAGE_SYMBOL] = true as const;\n\n abstract readonly type: TRole;\n\n id?: string;\n\n /** @inheritdoc */\n name?: string;\n\n content: $InferMessageContent<TStructure, TRole>;\n\n additional_kwargs: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"additional_kwargs\"]\n >;\n\n response_metadata: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"response_metadata\"]\n >;\n\n /**\n * @deprecated Use .getType() instead or import the proper typeguard.\n * For example:\n *\n * ```ts\n * import { isAIMessage } from \"@langchain/core/messages\";\n *\n * const message = new AIMessage(\"Hello!\");\n * isAIMessage(message); // true\n * ```\n */\n _getType(): MessageType {\n return this.type;\n }\n\n /**\n * @deprecated Use .type instead\n * The type of the message.\n */\n getType(): MessageType {\n return this._getType();\n }\n\n constructor(\n arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>\n ) {\n const fields: BaseMessageFields<TStructure, TRole> =\n typeof arg === \"string\" || Array.isArray(arg) ? { content: arg } : arg;\n if (!fields.additional_kwargs) {\n fields.additional_kwargs = {};\n }\n if (!fields.response_metadata) {\n fields.response_metadata = {};\n }\n super(fields);\n this.name = fields.name;\n if (fields.content === undefined && fields.contentBlocks !== undefined) {\n this.content = fields.contentBlocks as $InferMessageContent<\n TStructure,\n TRole\n >;\n this.response_metadata = {\n output_version: \"v1\",\n ...fields.response_metadata,\n };\n } else if (fields.content !== undefined) {\n this.content = fields.content ?? [];\n this.response_metadata = fields.response_metadata;\n } else {\n this.content = [] as $InferMessageContent<TStructure, TRole>;\n this.response_metadata = fields.response_metadata;\n }\n this.additional_kwargs = fields.additional_kwargs;\n this.id = fields.id;\n }\n\n /** Get text content of the message. */\n get text(): string {\n if (typeof this.content === \"string\") {\n return this.content;\n }\n if (!Array.isArray(this.content)) return \"\";\n return this.content\n .map((c) => {\n if (typeof c === \"string\") return c;\n if (c.type === \"text\") return c.text;\n return \"\";\n })\n .join(\"\");\n }\n\n get contentBlocks(): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock> =\n typeof this.content === \"string\"\n ? [{ type: \"text\", text: this.content }]\n : this.content;\n const parsingSteps = [\n convertToV1FromDataContent,\n convertToV1FromChatCompletionsInput,\n convertToV1FromAnthropicInput,\n ];\n const parsedBlocks = parsingSteps.reduce(\n (blocks, step) => step(blocks),\n blocks\n );\n return parsedBlocks as Array<ContentBlock.Standard>;\n }\n\n toDict(): StoredMessage {\n return {\n type: this.getType(),\n data: (this.toJSON() as SerializedConstructor)\n .kwargs as StoredMessageData,\n };\n }\n\n static lc_name() {\n return \"BaseMessage\";\n }\n\n // Can't be protected for silly reasons\n get _printableFields(): Record<string, unknown> {\n return {\n id: this.id,\n content: this.content,\n name: this.name,\n additional_kwargs: this.additional_kwargs,\n response_metadata: this.response_metadata,\n };\n }\n\n static isInstance(obj: unknown): obj is BaseMessage {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n MESSAGE_SYMBOL in obj &&\n obj[MESSAGE_SYMBOL] === true &&\n isMessage(obj)\n );\n }\n\n // this private method is used to update the ID for the runtime\n // value as well as in lc_kwargs for serialisation\n _updateId(value: string | undefined) {\n this.id = value;\n\n // lc_attributes wouldn't work here, because jest compares the\n // whole object\n this.lc_kwargs.id = value;\n }\n\n get [Symbol.toStringTag]() {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.constructor as any).lc_name();\n }\n\n // Override the default behavior of console.log\n [Symbol.for(\"nodejs.util.inspect.custom\")](depth: number | null) {\n if (depth === null) {\n return this;\n }\n const printable = stringifyWithDepthLimit(\n this._printableFields,\n Math.max(4, depth)\n );\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return `${(this.constructor as any).lc_name()} ${printable}`;\n }\n\n toFormattedString(format: MessageStringFormat = \"pretty\"): string {\n return convertToFormattedString(this, format);\n }\n}\n\n/**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\nexport type OpenAIToolCall = {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: FunctionCall;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: \"function\";\n\n index?: number;\n};\n\nexport function isOpenAIToolCallArray(\n value?: unknown\n): value is OpenAIToolCall[] {\n return (\n Array.isArray(value) &&\n value.every((v) => typeof (v as OpenAIToolCall).index === \"number\")\n );\n}\n\nexport function _mergeDicts(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n left: Record<string, any> = {},\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n right: Record<string, any> = {}\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n const merged = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (merged[key] == null) {\n merged[key] = value;\n } else if (value == null) {\n continue;\n } else if (\n typeof merged[key] !== typeof value ||\n Array.isArray(merged[key]) !== Array.isArray(value)\n ) {\n throw new Error(\n `field[${key}] already exists in the message chunk, but with a different type.`\n );\n } else if (typeof merged[key] === \"string\") {\n if (key === \"type\") {\n // Do not merge 'type' fields\n continue;\n } else if (\n [\"id\", \"name\", \"output_version\", \"model_provider\"].includes(key)\n ) {\n // Keep the incoming value for these fields if its defined\n if (value) {\n merged[key] = value;\n }\n } else {\n merged[key] += value;\n }\n } else if (typeof merged[key] === \"object\" && !Array.isArray(merged[key])) {\n merged[key] = _mergeDicts(merged[key], value);\n } else if (Array.isArray(merged[key])) {\n merged[key] = _mergeLists(merged[key], value);\n } else if (merged[key] === value) {\n continue;\n } else {\n console.warn(\n `field[${key}] already exists in this message chunk and value has unsupported type.`\n );\n }\n }\n return merged;\n}\n\nexport function _mergeLists<Content extends ContentBlock>(\n left?: Content[],\n right?: Content[]\n): Content[] | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n } else if (left === undefined || right === undefined) {\n return left || right;\n } else {\n const merged = [...left];\n for (const item of right) {\n if (\n typeof item === \"object\" &&\n item !== null &&\n \"index\" in item &&\n typeof item.index === \"number\"\n ) {\n const toMerge = merged.findIndex((leftItem) => {\n const isObject = typeof leftItem === \"object\";\n const indiciesMatch =\n \"index\" in leftItem && leftItem.index === item.index;\n const idsMatch =\n \"id\" in leftItem && \"id\" in item && leftItem?.id === item?.id;\n const eitherItemMissingID =\n !(\"id\" in leftItem) ||\n !leftItem?.id ||\n !(\"id\" in item) ||\n !item?.id;\n return isObject && indiciesMatch && (idsMatch || eitherItemMissingID);\n });\n if (\n toMerge !== -1 &&\n typeof merged[toMerge] === \"object\" &&\n merged[toMerge] !== null\n ) {\n merged[toMerge] = _mergeDicts(\n merged[toMerge] as Record<string, unknown>,\n item as Record<string, unknown>\n ) as Content;\n } else {\n merged.push(item);\n }\n } else if (\n typeof item === \"object\" &&\n item !== null &&\n \"text\" in item &&\n item.text === \"\"\n ) {\n // No-op - skip empty text blocks\n continue;\n } else {\n merged.push(item);\n }\n }\n return merged;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function _mergeObj<T = any>(\n left: T | undefined,\n right: T | undefined\n): T | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n }\n if (left === undefined || right === undefined) {\n return left ?? right;\n } else if (typeof left !== typeof right) {\n throw new Error(\n `Cannot merge objects of different types.\\nLeft ${typeof left}\\nRight ${typeof right}`\n );\n } else if (typeof left === \"string\" && typeof right === \"string\") {\n return (left + right) as T;\n } else if (Array.isArray(left) && Array.isArray(right)) {\n return _mergeLists(left, right) as T;\n } else if (typeof left === \"object\" && typeof right === \"object\") {\n return _mergeDicts(\n left as Record<string, unknown>,\n right as Record<string, unknown>\n ) as T;\n } else if (left === right) {\n return left;\n } else {\n throw new Error(\n `Can not merge objects of different types.\\nLeft ${left}\\nRight ${right}`\n );\n }\n}\n\n/**\n * Represents a chunk of a message, which can be concatenated with other\n * message chunks. It includes a method `_merge_kwargs_dict()` for merging\n * additional keyword arguments from another `BaseMessageChunk` into this\n * one. It also overrides the `__add__()` method to support concatenation\n * of `BaseMessageChunk` instances.\n */\nexport abstract class BaseMessageChunk<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> extends BaseMessage<TStructure, TRole> {\n abstract concat(chunk: BaseMessageChunk): BaseMessageChunk<TStructure, TRole>;\n\n static isInstance(obj: unknown): obj is BaseMessageChunk {\n if (!super.isInstance(obj)) {\n return false;\n }\n // Check if obj is an instance of BaseMessageChunk by traversing the prototype chain\n let proto = Object.getPrototypeOf(obj);\n while (proto !== null) {\n if (proto === BaseMessageChunk.prototype) {\n return true;\n }\n proto = Object.getPrototypeOf(proto);\n }\n return false;\n }\n}\n\nexport type MessageFieldWithRole = {\n role: MessageType;\n content: MessageContent;\n name?: string;\n} & Record<string, unknown>;\n\nexport function _isMessageFieldWithRole(\n x: BaseMessageLike\n): x is MessageFieldWithRole {\n return typeof (x as MessageFieldWithRole).role === \"string\";\n}\n\nexport type BaseMessageLike =\n | BaseMessage\n | MessageFieldWithRole\n | [MessageType, MessageContent]\n | string\n /**\n * @deprecated Specifying \"type\" is deprecated and will be removed in 0.4.0.\n */\n | ({\n type: MessageType | \"user\" | \"assistant\" | \"placeholder\";\n } & BaseMessageFields &\n Record<string, unknown>)\n | SerializedConstructor;\n\n/**\n * @deprecated Use {@link BaseMessage.isInstance} instead\n */\nexport function isBaseMessage(\n messageLike?: unknown\n): messageLike is BaseMessage {\n return typeof (messageLike as BaseMessage)?._getType === \"function\";\n}\n\n/**\n * @deprecated Use {@link BaseMessageChunk.isInstance} instead\n */\nexport function isBaseMessageChunk(\n messageLike?: unknown\n): messageLike is BaseMessageChunk {\n return BaseMessageChunk.isInstance(messageLike);\n}\n"],"mappings":";;;;;;;;;;AAoBA,MAAM,iBAAiB,OAAO,IAAI,oBAAoB;AAqEtD,SAAgB,aACdA,cACAC,eACgB;AAEhB,KAAI,OAAO,iBAAiB,UAAU;AACpC,MAAI,iBAAiB,GACnB,QAAO;AAET,MAAI,OAAO,kBAAkB,SAC3B,QAAO,eAAe;WACb,MAAM,QAAQ,cAAc,IAAI,cAAc,WAAW,EAClE,QAAO;WAEP,MAAM,QAAQ,cAAc,IAC5B,cAAc,KAAK,CAAC,MAAMC,gCAAmB,EAAE,CAAC,CAEhD,QAAO,CACL;GACE,MAAM;GACN,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O,KAAK,KAAK;EAEpB,WACC,OAAO,SAAS,YAChB,SAAS,QACT,UAAU,QACV,KAAK,SAAS,GAGd;OAEA,OAAO,KAAK,KAAK;AAGrB,SAAO;CACR;AACF;AAGD,SAAgB,UACdC,MACAC,OACe;AACf,KAAI,SAAS,UAAa,UAAU,OAClC,QAAO;AAET,KAAI,SAAS,UAAa,UAAU,OAClC,QAAO,QAAQ;UACN,OAAO,SAAS,OAAO,MAChC,OAAM,IAAI,MACR,CAAC,+CAA+C,EAAE,OAAO,KAAK,QAAQ,EAAE,OAAO,OAAO;UAE/E,OAAO,SAAS,YAAY,OAAO,UAAU,SACtD,QAAQ,OAAO;UACN,MAAM,QAAQ,KAAK,IAAI,MAAM,QAAQ,MAAM,CACpD,QAAO,YAAY,MAAM,MAAM;UACtB,OAAO,SAAS,YAAY,OAAO,UAAU,SACtD,QAAO,YACL,MACA,MACD;UACQ,SAAS,MAClB,QAAO;KAEP,OAAM,IAAI,MACR,CAAC,gDAAgD,EAAE,KAAK,QAAQ,EAAE,OAAO;AAG9E;;;;;;;;AASD,IAAsB,mBAAtB,MAAsB,yBAGZ,YAA+B;CAGvC,OAAO,WAAWZ,KAAuC;AACvD,MAAI,CAAC,MAAM,WAAW,IAAI,CACxB,QAAO;EAGT,IAAI,QAAQ,OAAO,eAAe,IAAI;AACtC,SAAO,UAAU,MAAM;AACrB,OAAI,UAAU,iBAAiB,UAC7B,QAAO;GAET,QAAQ,OAAO,eAAe,MAAM;EACrC;AACD,SAAO;CACR;AACF;AAQD,SAAgB,wBACda,GAC2B;AAC3B,QAAO,OAAQ,EAA2B,SAAS;AACpD;;;;AAmBD,SAAgB,cACdC,aAC4B;AAC5B,QAAO,OAAQ,aAA6B,aAAa;AAC1D;;;;AAKD,SAAgB,mBACdA,aACiC;AACjC,QAAO,iBAAiB,WAAW,YAAY;AAChD"}
1
+ {"version":3,"file":"base.cjs","names":["firstContent: MessageContent","secondContent: MessageContent","isDataContentBlock","left?: \"success\" | \"error\"","right?: \"success\" | \"error\"","obj: any","depthLimit: number","currentDepth: number","obj","result: Record<string, unknown>","Serializable","arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>","fields: BaseMessageFields<TStructure, TRole>","blocks: Array<ContentBlock>","convertToV1FromDataContent","convertToV1FromChatCompletionsInput","convertToV1FromAnthropicInput","blocks","obj: unknown","isMessage","value: string | undefined","depth: number | null","format: MessageStringFormat","convertToFormattedString","value?: unknown","left: Record<string, any>","right: Record<string, any>","left?: Content[]","right?: Content[]","left: T | undefined","right: T | undefined","x: BaseMessageLike","messageLike?: unknown"],"sources":["../../src/messages/base.ts"],"sourcesContent":["import { Serializable, SerializedConstructor } from \"../load/serializable.js\";\nimport { ContentBlock } from \"./content/index.js\";\nimport { isDataContentBlock } from \"./content/data.js\";\nimport { convertToV1FromAnthropicInput } from \"./block_translators/anthropic.js\";\nimport { convertToV1FromDataContent } from \"./block_translators/data.js\";\nimport { convertToV1FromChatCompletionsInput } from \"./block_translators/openai.js\";\nimport {\n $InferMessageContent,\n $InferResponseMetadata,\n MessageStructure,\n MessageType,\n isMessage,\n Message,\n} from \"./message.js\";\nimport {\n convertToFormattedString,\n type MessageStringFormat,\n} from \"./format.js\";\n\n/** @internal */\nconst MESSAGE_SYMBOL = Symbol.for(\"langchain.message\");\n\nexport interface StoredMessageData {\n content: string;\n role: string | undefined;\n name: string | undefined;\n tool_call_id: string | undefined;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n additional_kwargs?: Record<string, any>;\n /** Response metadata. For example: response headers, logprobs, token counts, model name. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n response_metadata?: Record<string, any>;\n id?: string;\n}\n\nexport interface StoredMessage {\n type: string;\n data: StoredMessageData;\n}\n\nexport interface StoredGeneration {\n text: string;\n message?: StoredMessage;\n}\n\nexport interface StoredMessageV1 {\n type: string;\n role: string | undefined;\n text: string;\n}\n\nexport type MessageContent = string | Array<ContentBlock>;\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n}\n\nexport type BaseMessageFields<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> = Pick<Message, \"id\" | \"name\"> & {\n content?: $InferMessageContent<TStructure, TRole>;\n contentBlocks?: Array<ContentBlock.Standard>;\n /** @deprecated */\n additional_kwargs?: {\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n function_call?: FunctionCall;\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n tool_calls?: OpenAIToolCall[];\n [key: string]: unknown;\n };\n response_metadata?: Partial<$InferResponseMetadata<TStructure, TRole>>;\n};\n\nexport function mergeContent(\n firstContent: MessageContent,\n secondContent: MessageContent\n): MessageContent {\n // If first content is a string\n if (typeof firstContent === \"string\") {\n if (firstContent === \"\") {\n return secondContent;\n }\n if (typeof secondContent === \"string\") {\n return firstContent + secondContent;\n } else if (Array.isArray(secondContent) && secondContent.length === 0) {\n return firstContent;\n } else if (\n Array.isArray(secondContent) &&\n secondContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n {\n type: \"text\",\n source_type: \"text\",\n text: firstContent,\n },\n ...secondContent,\n ];\n } else {\n return [{ type: \"text\", text: firstContent }, ...secondContent];\n }\n // If both are arrays\n } else if (Array.isArray(secondContent)) {\n return (\n _mergeLists(firstContent, secondContent) ?? [\n ...firstContent,\n ...secondContent,\n ]\n );\n } else {\n if (secondContent === \"\") {\n return firstContent;\n } else if (\n Array.isArray(firstContent) &&\n firstContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n ...firstContent,\n {\n type: \"file\",\n source_type: \"text\",\n text: secondContent,\n },\n ];\n } else {\n return [...firstContent, { type: \"text\", text: secondContent }];\n }\n }\n}\n\n/**\n * 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else\n * it will return 'success'.\n *\n * @param {\"success\" | \"error\" | undefined} left The existing value to 'merge' with the new value.\n * @param {\"success\" | \"error\" | undefined} right The new value to 'merge' with the existing value\n * @returns {\"success\" | \"error\"} The 'merged' value.\n */\nexport function _mergeStatus(\n left?: \"success\" | \"error\",\n right?: \"success\" | \"error\"\n): \"success\" | \"error\" | undefined {\n if (left === \"error\" || right === \"error\") {\n return \"error\";\n }\n return \"success\";\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction stringifyWithDepthLimit(obj: any, depthLimit: number): string {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n function helper(obj: any, currentDepth: number): any {\n if (typeof obj !== \"object\" || obj === null || obj === undefined) {\n return obj;\n }\n if (currentDepth >= depthLimit) {\n if (Array.isArray(obj)) {\n return \"[Array]\";\n }\n return \"[Object]\";\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => helper(item, currentDepth + 1));\n }\n\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(obj)) {\n result[key] = helper(obj[key], currentDepth + 1);\n }\n return result;\n }\n\n return JSON.stringify(helper(obj, 0), null, 2);\n}\n\n/**\n * Base class for all types of messages in a conversation. It includes\n * properties like `content`, `name`, and `additional_kwargs`. It also\n * includes methods like `toDict()` and `_getType()`.\n */\nexport abstract class BaseMessage<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n >\n extends Serializable\n implements Message<TStructure, TRole>\n{\n lc_namespace = [\"langchain_core\", \"messages\"];\n\n lc_serializable = true;\n\n get lc_aliases(): Record<string, string> {\n // exclude snake case conversion to pascal case\n return {\n additional_kwargs: \"additional_kwargs\",\n response_metadata: \"response_metadata\",\n };\n }\n\n readonly [MESSAGE_SYMBOL] = true as const;\n\n abstract readonly type: TRole;\n\n id?: string;\n\n /** @inheritdoc */\n name?: string;\n\n content: $InferMessageContent<TStructure, TRole>;\n\n additional_kwargs: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"additional_kwargs\"]\n >;\n\n response_metadata: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"response_metadata\"]\n >;\n\n /**\n * @deprecated Use .getType() instead or import the proper typeguard.\n * For example:\n *\n * ```ts\n * import { isAIMessage } from \"@langchain/core/messages\";\n *\n * const message = new AIMessage(\"Hello!\");\n * isAIMessage(message); // true\n * ```\n */\n _getType(): MessageType {\n return this.type;\n }\n\n /**\n * @deprecated Use .type instead\n * The type of the message.\n */\n getType(): MessageType {\n return this._getType();\n }\n\n constructor(\n arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>\n ) {\n const fields: BaseMessageFields<TStructure, TRole> =\n typeof arg === \"string\" || Array.isArray(arg) ? { content: arg } : arg;\n if (!fields.additional_kwargs) {\n fields.additional_kwargs = {};\n }\n if (!fields.response_metadata) {\n fields.response_metadata = {};\n }\n super(fields);\n this.name = fields.name;\n if (fields.content === undefined && fields.contentBlocks !== undefined) {\n this.content = fields.contentBlocks as $InferMessageContent<\n TStructure,\n TRole\n >;\n this.response_metadata = {\n output_version: \"v1\",\n ...fields.response_metadata,\n };\n } else if (fields.content !== undefined) {\n this.content = fields.content ?? [];\n this.response_metadata = fields.response_metadata;\n } else {\n this.content = [] as $InferMessageContent<TStructure, TRole>;\n this.response_metadata = fields.response_metadata;\n }\n this.additional_kwargs = fields.additional_kwargs;\n this.id = fields.id;\n }\n\n /** Get text content of the message. */\n get text(): string {\n if (typeof this.content === \"string\") {\n return this.content;\n }\n if (!Array.isArray(this.content)) return \"\";\n return this.content\n .map((c) => {\n if (typeof c === \"string\") return c;\n if (c.type === \"text\") return c.text;\n return \"\";\n })\n .join(\"\");\n }\n\n get contentBlocks(): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock> =\n typeof this.content === \"string\"\n ? [{ type: \"text\", text: this.content }]\n : this.content;\n const parsingSteps = [\n convertToV1FromDataContent,\n convertToV1FromChatCompletionsInput,\n convertToV1FromAnthropicInput,\n ];\n const parsedBlocks = parsingSteps.reduce(\n (blocks, step) => step(blocks),\n blocks\n );\n return parsedBlocks as Array<ContentBlock.Standard>;\n }\n\n toDict(): StoredMessage {\n return {\n type: this.getType(),\n data: (this.toJSON() as SerializedConstructor)\n .kwargs as StoredMessageData,\n };\n }\n\n static lc_name() {\n return \"BaseMessage\";\n }\n\n // Can't be protected for silly reasons\n get _printableFields(): Record<string, unknown> {\n return {\n id: this.id,\n content: this.content,\n name: this.name,\n additional_kwargs: this.additional_kwargs,\n response_metadata: this.response_metadata,\n };\n }\n\n static isInstance(obj: unknown): obj is BaseMessage {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n MESSAGE_SYMBOL in obj &&\n obj[MESSAGE_SYMBOL] === true &&\n isMessage(obj)\n );\n }\n\n // this private method is used to update the ID for the runtime\n // value as well as in lc_kwargs for serialisation\n _updateId(value: string | undefined) {\n this.id = value;\n\n // lc_attributes wouldn't work here, because jest compares the\n // whole object\n this.lc_kwargs.id = value;\n }\n\n get [Symbol.toStringTag]() {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.constructor as any).lc_name();\n }\n\n // Override the default behavior of console.log\n [Symbol.for(\"nodejs.util.inspect.custom\")](depth: number | null) {\n if (depth === null) {\n return this;\n }\n const printable = stringifyWithDepthLimit(\n this._printableFields,\n Math.max(4, depth)\n );\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return `${(this.constructor as any).lc_name()} ${printable}`;\n }\n\n toFormattedString(format: MessageStringFormat = \"pretty\"): string {\n return convertToFormattedString(this, format);\n }\n}\n\n/**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\nexport type OpenAIToolCall = {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: FunctionCall;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: \"function\";\n\n index?: number;\n};\n\nexport function isOpenAIToolCallArray(\n value?: unknown\n): value is OpenAIToolCall[] {\n return (\n Array.isArray(value) &&\n value.every((v) => typeof (v as OpenAIToolCall).index === \"number\")\n );\n}\n\nexport function _mergeDicts(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n left: Record<string, any> = {},\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n right: Record<string, any> = {}\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n const merged = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (merged[key] == null) {\n merged[key] = value;\n } else if (value == null) {\n continue;\n } else if (\n typeof merged[key] !== typeof value ||\n Array.isArray(merged[key]) !== Array.isArray(value)\n ) {\n throw new Error(\n `field[${key}] already exists in the message chunk, but with a different type.`\n );\n } else if (typeof merged[key] === \"string\") {\n if (key === \"type\") {\n // Do not merge 'type' fields\n continue;\n } else if (\n [\"id\", \"name\", \"output_version\", \"model_provider\"].includes(key)\n ) {\n // Keep the incoming value for these fields if its defined\n if (value) {\n merged[key] = value;\n }\n } else {\n merged[key] += value;\n }\n } else if (typeof merged[key] === \"number\") {\n merged[key] = merged[key] + value;\n } else if (typeof merged[key] === \"object\" && !Array.isArray(merged[key])) {\n merged[key] = _mergeDicts(merged[key], value);\n } else if (Array.isArray(merged[key])) {\n merged[key] = _mergeLists(merged[key], value);\n } else if (merged[key] === value) {\n continue;\n } else {\n console.warn(\n `field[${key}] already exists in this message chunk and value has unsupported type.`\n );\n }\n }\n return merged;\n}\n\nexport function _mergeLists<Content extends ContentBlock>(\n left?: Content[],\n right?: Content[]\n): Content[] | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n } else if (left === undefined || right === undefined) {\n return left || right;\n } else {\n const merged = [...left];\n for (const item of right) {\n if (\n typeof item === \"object\" &&\n item !== null &&\n \"index\" in item &&\n typeof item.index === \"number\"\n ) {\n const toMerge = merged.findIndex((leftItem) => {\n const isObject = typeof leftItem === \"object\";\n const indiciesMatch =\n \"index\" in leftItem && leftItem.index === item.index;\n const idsMatch =\n \"id\" in leftItem && \"id\" in item && leftItem?.id === item?.id;\n const eitherItemMissingID =\n !(\"id\" in leftItem) ||\n !leftItem?.id ||\n !(\"id\" in item) ||\n !item?.id;\n return isObject && indiciesMatch && (idsMatch || eitherItemMissingID);\n });\n if (\n toMerge !== -1 &&\n typeof merged[toMerge] === \"object\" &&\n merged[toMerge] !== null\n ) {\n merged[toMerge] = _mergeDicts(\n merged[toMerge] as Record<string, unknown>,\n item as Record<string, unknown>\n ) as Content;\n } else {\n merged.push(item);\n }\n } else if (\n typeof item === \"object\" &&\n item !== null &&\n \"text\" in item &&\n item.text === \"\"\n ) {\n // No-op - skip empty text blocks\n continue;\n } else {\n merged.push(item);\n }\n }\n return merged;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function _mergeObj<T = any>(\n left: T | undefined,\n right: T | undefined\n): T | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n }\n if (left === undefined || right === undefined) {\n return left ?? right;\n } else if (typeof left !== typeof right) {\n throw new Error(\n `Cannot merge objects of different types.\\nLeft ${typeof left}\\nRight ${typeof right}`\n );\n } else if (typeof left === \"string\" && typeof right === \"string\") {\n return (left + right) as T;\n } else if (Array.isArray(left) && Array.isArray(right)) {\n return _mergeLists(left, right) as T;\n } else if (typeof left === \"object\" && typeof right === \"object\") {\n return _mergeDicts(\n left as Record<string, unknown>,\n right as Record<string, unknown>\n ) as T;\n } else if (left === right) {\n return left;\n } else {\n throw new Error(\n `Can not merge objects of different types.\\nLeft ${left}\\nRight ${right}`\n );\n }\n}\n\n/**\n * Represents a chunk of a message, which can be concatenated with other\n * message chunks. It includes a method `_merge_kwargs_dict()` for merging\n * additional keyword arguments from another `BaseMessageChunk` into this\n * one. It also overrides the `__add__()` method to support concatenation\n * of `BaseMessageChunk` instances.\n */\nexport abstract class BaseMessageChunk<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> extends BaseMessage<TStructure, TRole> {\n abstract concat(chunk: BaseMessageChunk): BaseMessageChunk<TStructure, TRole>;\n\n static isInstance(obj: unknown): obj is BaseMessageChunk {\n if (!super.isInstance(obj)) {\n return false;\n }\n // Check if obj is an instance of BaseMessageChunk by traversing the prototype chain\n let proto = Object.getPrototypeOf(obj);\n while (proto !== null) {\n if (proto === BaseMessageChunk.prototype) {\n return true;\n }\n proto = Object.getPrototypeOf(proto);\n }\n return false;\n }\n}\n\nexport type MessageFieldWithRole = {\n role: MessageType;\n content: MessageContent;\n name?: string;\n} & Record<string, unknown>;\n\nexport function _isMessageFieldWithRole(\n x: BaseMessageLike\n): x is MessageFieldWithRole {\n return typeof (x as MessageFieldWithRole).role === \"string\";\n}\n\nexport type BaseMessageLike =\n | BaseMessage\n | MessageFieldWithRole\n | [MessageType, MessageContent]\n | string\n /**\n * @deprecated Specifying \"type\" is deprecated and will be removed in 0.4.0.\n */\n | ({\n type: MessageType | \"user\" | \"assistant\" | \"placeholder\";\n } & BaseMessageFields &\n Record<string, unknown>)\n | SerializedConstructor;\n\n/**\n * @deprecated Use {@link BaseMessage.isInstance} instead\n */\nexport function isBaseMessage(\n messageLike?: unknown\n): messageLike is BaseMessage {\n return typeof (messageLike as BaseMessage)?._getType === \"function\";\n}\n\n/**\n * @deprecated Use {@link BaseMessageChunk.isInstance} instead\n */\nexport function isBaseMessageChunk(\n messageLike?: unknown\n): messageLike is BaseMessageChunk {\n return BaseMessageChunk.isInstance(messageLike);\n}\n"],"mappings":";;;;;;;;;;AAoBA,MAAM,iBAAiB,OAAO,IAAI,oBAAoB;AAqEtD,SAAgB,aACdA,cACAC,eACgB;AAEhB,KAAI,OAAO,iBAAiB,UAAU;AACpC,MAAI,iBAAiB,GACnB,QAAO;AAET,MAAI,OAAO,kBAAkB,SAC3B,QAAO,eAAe;WACb,MAAM,QAAQ,cAAc,IAAI,cAAc,WAAW,EAClE,QAAO;WAEP,MAAM,QAAQ,cAAc,IAC5B,cAAc,KAAK,CAAC,MAAMC,gCAAmB,EAAE,CAAC,CAEhD,QAAO,CACL;GACE,MAAM;GACN,aAAa;GACb,MAAM;EACP,GACD,G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)) {
203
203
  if (value) merged[key] = value;
204
204
  } else merged[key] += value;
205
+ else if (typeof merged[key] === "number") merged[key] = merged[key] + value;
205
206
  else if (typeof merged[key] === "object" && !Array.isArray(merged[key])) merged[key] = _mergeDicts(merged[key], value);
206
207
  else if (Array.isArray(merged[key])) merged[key] = _mergeLists(merged[key], value);
207
208
  else if (merged[key] === value) continue;