@langchain/core 1.1.6 → 1.1.8
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 +20 -0
- package/dist/caches/index.d.ts.map +1 -1
- package/dist/callbacks/base.cjs.map +1 -1
- package/dist/callbacks/base.d.cts +2 -2
- package/dist/callbacks/base.d.cts.map +1 -1
- package/dist/callbacks/base.d.ts +2 -2
- package/dist/callbacks/base.d.ts.map +1 -1
- package/dist/callbacks/base.js.map +1 -1
- package/dist/callbacks/manager.cjs +3 -3
- package/dist/callbacks/manager.cjs.map +1 -1
- package/dist/callbacks/manager.d.cts +1 -1
- package/dist/callbacks/manager.d.cts.map +1 -1
- package/dist/callbacks/manager.d.ts +1 -1
- package/dist/callbacks/manager.d.ts.map +1 -1
- package/dist/callbacks/manager.js +3 -3
- package/dist/callbacks/manager.js.map +1 -1
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/load/import_map.cjs +2 -0
- package/dist/load/import_map.cjs.map +1 -1
- package/dist/load/import_map.js +2 -0
- package/dist/load/import_map.js.map +1 -1
- package/dist/load/index.cjs +85 -25
- package/dist/load/index.cjs.map +1 -1
- package/dist/load/index.d.cts +113 -3
- package/dist/load/index.d.cts.map +1 -1
- package/dist/load/index.d.ts +113 -3
- package/dist/load/index.d.ts.map +1 -1
- package/dist/load/index.js +85 -25
- package/dist/load/index.js.map +1 -1
- package/dist/load/serializable.cjs +6 -1
- package/dist/load/serializable.cjs.map +1 -1
- package/dist/load/serializable.d.cts +15 -1
- package/dist/load/serializable.d.cts.map +1 -1
- package/dist/load/serializable.d.ts +15 -1
- package/dist/load/serializable.d.ts.map +1 -1
- package/dist/load/serializable.js +6 -1
- package/dist/load/serializable.js.map +1 -1
- package/dist/load/validation.cjs +98 -0
- package/dist/load/validation.cjs.map +1 -0
- package/dist/load/validation.js +95 -0
- package/dist/load/validation.js.map +1 -0
- package/dist/messages/base.cjs +1 -0
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.d.cts +2 -3
- package/dist/messages/base.d.cts.map +1 -1
- package/dist/messages/base.d.ts +2 -3
- package/dist/messages/base.d.ts.map +1 -1
- package/dist/messages/base.js +1 -0
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/message.cjs.map +1 -1
- package/dist/messages/message.d.cts +34 -1
- package/dist/messages/message.d.cts.map +1 -1
- package/dist/messages/message.d.ts +34 -1
- package/dist/messages/message.d.ts.map +1 -1
- package/dist/messages/message.js.map +1 -1
- package/dist/runnables/base.cjs +1 -1
- package/dist/runnables/base.cjs.map +1 -1
- package/dist/runnables/base.js +1 -1
- package/dist/runnables/base.js.map +1 -1
- package/dist/tracers/base.cjs +5 -2
- package/dist/tracers/base.cjs.map +1 -1
- package/dist/tracers/base.d.cts +1 -1
- package/dist/tracers/base.d.cts.map +1 -1
- package/dist/tracers/base.d.ts +1 -1
- package/dist/tracers/base.d.ts.map +1 -1
- package/dist/tracers/base.js +5 -2
- package/dist/tracers/base.js.map +1 -1
- package/dist/tracers/tracer_langchain.cjs +31 -3
- package/dist/tracers/tracer_langchain.cjs.map +1 -1
- package/dist/tracers/tracer_langchain.d.cts +8 -7
- package/dist/tracers/tracer_langchain.d.cts.map +1 -1
- package/dist/tracers/tracer_langchain.d.ts +8 -7
- package/dist/tracers/tracer_langchain.d.ts.map +1 -1
- package/dist/tracers/tracer_langchain.js +31 -3
- package/dist/tracers/tracer_langchain.js.map +1 -1
- package/dist/utils/context.cjs +107 -0
- package/dist/utils/context.cjs.map +1 -0
- package/dist/utils/context.d.cts +44 -0
- package/dist/utils/context.d.cts.map +1 -0
- package/dist/utils/context.d.ts +44 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +101 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/event_source_parse.d.cts.map +1 -1
- package/dist/utils/stream.d.cts.map +1 -1
- package/dist/vectorstores.d.cts.map +1 -1
- package/package.json +13 -2
- package/utils/context.cjs +1 -0
- package/utils/context.d.cts +1 -0
- package/utils/context.d.ts +1 -0
- package/utils/context.js +1 -0
|
@@ -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","mapKeys","keyToJson"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.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\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 return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: mapKeys(\n Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs,\n keyToJson,\n aliases\n ),\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":";;;;;;;;;AAyBA,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;AAMD,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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AAS;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;AAEF,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;GACT,QAAQC,yBACN,OAAO,KAAK,QAAQ,CAAC,SAAS,eAAe,QAAQ,QAAQ,GAAG,QAChEC,4BACA,QACD;EACF;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 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,M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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,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"}
|
|
@@ -19,6 +19,20 @@ type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplem
|
|
|
19
19
|
* Should not be subclassed, subclass lc_name above instead.
|
|
20
20
|
*/
|
|
21
21
|
declare function get_lc_unique_name(serializableClass: typeof Serializable): string;
|
|
22
|
+
/**
|
|
23
|
+
* Interface for objects that can be serialized.
|
|
24
|
+
* This is a duck-typed interface to avoid circular imports.
|
|
25
|
+
*/
|
|
26
|
+
interface SerializableLike {
|
|
27
|
+
lc_serializable: boolean;
|
|
28
|
+
lc_secrets?: Record<string, string>;
|
|
29
|
+
toJSON(): {
|
|
30
|
+
lc: number;
|
|
31
|
+
type: string;
|
|
32
|
+
id: string[];
|
|
33
|
+
kwargs?: Record<string, unknown>;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
22
36
|
interface SerializableInterface {
|
|
23
37
|
get lc_id(): string[];
|
|
24
38
|
}
|
|
@@ -75,5 +89,5 @@ declare abstract class Serializable implements SerializableInterface {
|
|
|
75
89
|
toJSONNotImplemented(): SerializedNotImplemented;
|
|
76
90
|
}
|
|
77
91
|
//#endregion
|
|
78
|
-
export { BaseSerialized, Serializable, SerializableInterface, Serialized, SerializedConstructor, SerializedNotImplemented, SerializedSecret, get_lc_unique_name };
|
|
92
|
+
export { BaseSerialized, Serializable, SerializableInterface, SerializableLike, Serialized, SerializedConstructor, SerializedNotImplemented, SerializedSecret, get_lc_unique_name };
|
|
79
93
|
//# sourceMappingURL=serializable.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serializable.d.cts","names":["SerializedFields","BaseSerialized","T","Record","SerializedConstructor","SerializedSecret","SerializedNotImplemented","Serialized","get_lc_unique_name","Serializable","SerializableInterface"],"sources":["../../src/load/serializable.d.ts"],"sourcesContent":["import { type SerializedFields } from \"./map_keys.js\";\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n graph?: Record<string, any>;\n}\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {\n}\nexport interface SerializedNotImplemented extends BaseSerialized<\"not_implemented\"> {\n}\nexport type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplemented;\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 declare function get_lc_unique_name(serializableClass: typeof Serializable): string;\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\nexport declare abstract class Serializable implements SerializableInterface {\n lc_serializable: boolean;\n lc_kwargs: SerializedFields;\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 * 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 /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[];\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(): {\n [key: string]: string;\n } | undefined;\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 /**\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(): {\n [key: string]: string;\n } | undefined;\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 constructor(kwargs?: SerializedFields, ..._args: never[]);\n toJSON(): Serialized;\n toJSONNotImplemented(): SerializedNotImplemented;\n}\n//# sourceMappingURL=serializable.d.ts.map"],"mappings":";;;UACiBC;;EAAAA,IAAAA,EAEPC,CAFOD;EAOAG,EAAAA,EAAAA,MAAAA,EAAAA;EAGAC,IAAAA,CAAAA,EAAAA,MAAAA;EAEAC,KAAAA,CAAAA,EAPLH,MAOKG,CAAAA,MAAAA,EAAAA,GAAAA,CAAwB;AAEzC;AAAyBF,UAPRA,qBAAAA,SAA8BH,cAOtBG,CAAAA,aAAAA,CAAAA,CAAAA;EAAwBC,MAAAA,EANrCL,gBAMqCK;;AAA2C,UAJ3EA,gBAAAA,SAAyBJ,cAIkD,CAAA,QAAA,CAAA,CAAA,CAK5F;
|
|
1
|
+
{"version":3,"file":"serializable.d.cts","names":["SerializedFields","BaseSerialized","T","Record","SerializedConstructor","SerializedSecret","SerializedNotImplemented","Serialized","get_lc_unique_name","Serializable","SerializableLike","SerializableInterface"],"sources":["../../src/load/serializable.d.ts"],"sourcesContent":["import { type SerializedFields } from \"./map_keys.js\";\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n graph?: Record<string, any>;\n}\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {\n}\nexport interface SerializedNotImplemented extends BaseSerialized<\"not_implemented\"> {\n}\nexport type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplemented;\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 declare function get_lc_unique_name(serializableClass: typeof Serializable): string;\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}\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\nexport declare abstract class Serializable implements SerializableInterface {\n lc_serializable: boolean;\n lc_kwargs: SerializedFields;\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 * 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 /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[];\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(): {\n [key: string]: string;\n } | undefined;\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 /**\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(): {\n [key: string]: string;\n } | undefined;\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 constructor(kwargs?: SerializedFields, ..._args: never[]);\n toJSON(): Serialized;\n toJSONNotImplemented(): SerializedNotImplemented;\n}\n//# sourceMappingURL=serializable.d.ts.map"],"mappings":";;;UACiBC;;EAAAA,IAAAA,EAEPC,CAFOD;EAOAG,EAAAA,EAAAA,MAAAA,EAAAA;EAGAC,IAAAA,CAAAA,EAAAA,MAAAA;EAEAC,KAAAA,CAAAA,EAPLH,MAOKG,CAAAA,MAAAA,EAAAA,GAAAA,CAAwB;AAEzC;AAAyBF,UAPRA,qBAAAA,SAA8BH,cAOtBG,CAAAA,aAAAA,CAAAA,CAAAA;EAAwBC,MAAAA,EANrCL,gBAMqCK;;AAA2C,UAJ3EA,gBAAAA,SAAyBJ,cAIkD,CAAA,QAAA,CAAA,CAAA,CAK5F;AAKiBS,UAZAJ,wBAAAA,SAAiCL,cAmB3B,CAAA,iBAAA,CAAA,CAAA,CAGvB;AAG8BQ,KAvBlBF,UAAAA,GAAaH,qBAuBiB,GAvBOC,gBAuBP,GAvB0BC,wBAuB1B;;;;;AAkDdA,iBApEJE,kBAAAA,CAoEIF,iBAAAA,EAAAA,OApEyCG,YAoEzCH,CAAAA,EAAAA,MAAAA;;AAlD+C;;;UAb1DI,gBAAAA;;eAEAP;;;;;aAKAA;;;UAGAQ,qBAAAA;;;uBAGaF,YAAAA,YAAwBE;;aAEvCX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA+BUA;;;;;;;;;;;;;;;uBAeAA;YACXO;0BACcD"}
|
|
@@ -19,6 +19,20 @@ type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplem
|
|
|
19
19
|
* Should not be subclassed, subclass lc_name above instead.
|
|
20
20
|
*/
|
|
21
21
|
declare function get_lc_unique_name(serializableClass: typeof Serializable): string;
|
|
22
|
+
/**
|
|
23
|
+
* Interface for objects that can be serialized.
|
|
24
|
+
* This is a duck-typed interface to avoid circular imports.
|
|
25
|
+
*/
|
|
26
|
+
interface SerializableLike {
|
|
27
|
+
lc_serializable: boolean;
|
|
28
|
+
lc_secrets?: Record<string, string>;
|
|
29
|
+
toJSON(): {
|
|
30
|
+
lc: number;
|
|
31
|
+
type: string;
|
|
32
|
+
id: string[];
|
|
33
|
+
kwargs?: Record<string, unknown>;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
22
36
|
interface SerializableInterface {
|
|
23
37
|
get lc_id(): string[];
|
|
24
38
|
}
|
|
@@ -75,5 +89,5 @@ declare abstract class Serializable implements SerializableInterface {
|
|
|
75
89
|
toJSONNotImplemented(): SerializedNotImplemented;
|
|
76
90
|
}
|
|
77
91
|
//#endregion
|
|
78
|
-
export { BaseSerialized, Serializable, SerializableInterface, Serialized, SerializedConstructor, SerializedNotImplemented, SerializedSecret, get_lc_unique_name };
|
|
92
|
+
export { BaseSerialized, Serializable, SerializableInterface, SerializableLike, Serialized, SerializedConstructor, SerializedNotImplemented, SerializedSecret, get_lc_unique_name };
|
|
79
93
|
//# sourceMappingURL=serializable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serializable.d.ts","names":["SerializedFields","BaseSerialized","T","Record","SerializedConstructor","SerializedSecret","SerializedNotImplemented","Serialized","get_lc_unique_name","Serializable","SerializableInterface"],"sources":["../../src/load/serializable.d.ts"],"sourcesContent":["import { type SerializedFields } from \"./map_keys.js\";\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n graph?: Record<string, any>;\n}\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {\n}\nexport interface SerializedNotImplemented extends BaseSerialized<\"not_implemented\"> {\n}\nexport type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplemented;\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 declare function get_lc_unique_name(serializableClass: typeof Serializable): string;\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\nexport declare abstract class Serializable implements SerializableInterface {\n lc_serializable: boolean;\n lc_kwargs: SerializedFields;\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 * 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 /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[];\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(): {\n [key: string]: string;\n } | undefined;\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 /**\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(): {\n [key: string]: string;\n } | undefined;\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 constructor(kwargs?: SerializedFields, ..._args: never[]);\n toJSON(): Serialized;\n toJSONNotImplemented(): SerializedNotImplemented;\n}\n//# sourceMappingURL=serializable.d.ts.map"],"mappings":";;;UACiBC;;EAAAA,IAAAA,EAEPC,CAFOD;EAOAG,EAAAA,EAAAA,MAAAA,EAAAA;EAGAC,IAAAA,CAAAA,EAAAA,MAAAA;EAEAC,KAAAA,CAAAA,EAPLH,MAOKG,CAAAA,MAAAA,EAAAA,GAAAA,CAAwB;AAEzC;AAAyBF,UAPRA,qBAAAA,SAA8BH,cAOtBG,CAAAA,aAAAA,CAAAA,CAAAA;EAAwBC,MAAAA,EANrCL,gBAMqCK;;AAA2C,UAJ3EA,gBAAAA,SAAyBJ,cAIkD,CAAA,QAAA,CAAA,CAAA,CAK5F;
|
|
1
|
+
{"version":3,"file":"serializable.d.ts","names":["SerializedFields","BaseSerialized","T","Record","SerializedConstructor","SerializedSecret","SerializedNotImplemented","Serialized","get_lc_unique_name","Serializable","SerializableLike","SerializableInterface"],"sources":["../../src/load/serializable.d.ts"],"sourcesContent":["import { type SerializedFields } from \"./map_keys.js\";\nexport interface BaseSerialized<T extends string> {\n lc: number;\n type: T;\n id: string[];\n name?: string;\n graph?: Record<string, any>;\n}\nexport interface SerializedConstructor extends BaseSerialized<\"constructor\"> {\n kwargs: SerializedFields;\n}\nexport interface SerializedSecret extends BaseSerialized<\"secret\"> {\n}\nexport interface SerializedNotImplemented extends BaseSerialized<\"not_implemented\"> {\n}\nexport type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplemented;\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 declare function get_lc_unique_name(serializableClass: typeof Serializable): string;\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}\nexport interface SerializableInterface {\n get lc_id(): string[];\n}\nexport declare abstract class Serializable implements SerializableInterface {\n lc_serializable: boolean;\n lc_kwargs: SerializedFields;\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 * 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 /**\n * The final serialized identifier for the module.\n */\n get lc_id(): string[];\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(): {\n [key: string]: string;\n } | undefined;\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 /**\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(): {\n [key: string]: string;\n } | undefined;\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 constructor(kwargs?: SerializedFields, ..._args: never[]);\n toJSON(): Serialized;\n toJSONNotImplemented(): SerializedNotImplemented;\n}\n//# sourceMappingURL=serializable.d.ts.map"],"mappings":";;;UACiBC;;EAAAA,IAAAA,EAEPC,CAFOD;EAOAG,EAAAA,EAAAA,MAAAA,EAAAA;EAGAC,IAAAA,CAAAA,EAAAA,MAAAA;EAEAC,KAAAA,CAAAA,EAPLH,MAOKG,CAAAA,MAAAA,EAAAA,GAAAA,CAAwB;AAEzC;AAAyBF,UAPRA,qBAAAA,SAA8BH,cAOtBG,CAAAA,aAAAA,CAAAA,CAAAA;EAAwBC,MAAAA,EANrCL,gBAMqCK;;AAA2C,UAJ3EA,gBAAAA,SAAyBJ,cAIkD,CAAA,QAAA,CAAA,CAAA,CAK5F;AAKiBS,UAZAJ,wBAAAA,SAAiCL,cAmB3B,CAAA,iBAAA,CAAA,CAAA,CAGvB;AAG8BQ,KAvBlBF,UAAAA,GAAaH,qBAuBiB,GAvBOC,gBAuBP,GAvB0BC,wBAuB1B;;;;;AAkDdA,iBApEJE,kBAAAA,CAoEIF,iBAAAA,EAAAA,OApEyCG,YAoEzCH,CAAAA,EAAAA,MAAAA;;AAlD+C;;;UAb1DI,gBAAAA;;eAEAP;;;;;aAKAA;;;UAGAQ,qBAAAA;;;uBAGaF,YAAAA,YAAwBE;;aAEvCX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA+BUA;;;;;;;;;;;;;;;uBAeAA;YACXO;0BACcD"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { __export } from "../_virtual/rolldown_runtime.js";
|
|
2
2
|
import { keyToJson, mapKeys } from "./map_keys.js";
|
|
3
|
+
import { escapeIfNeeded } from "./validation.js";
|
|
3
4
|
|
|
4
5
|
//#region src/load/serializable.ts
|
|
5
6
|
var serializable_exports = {};
|
|
@@ -122,11 +123,15 @@ var Serializable = class Serializable {
|
|
|
122
123
|
}
|
|
123
124
|
if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
|
|
124
125
|
});
|
|
126
|
+
const escapedKwargs = {};
|
|
127
|
+
for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value);
|
|
128
|
+
const kwargsWithSecrets = Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs;
|
|
129
|
+
const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);
|
|
125
130
|
return {
|
|
126
131
|
lc: 1,
|
|
127
132
|
type: "constructor",
|
|
128
133
|
id: this.lc_id,
|
|
129
|
-
kwargs:
|
|
134
|
+
kwargs: processedKwargs
|
|
130
135
|
};
|
|
131
136
|
}
|
|
132
137
|
toJSONNotImplemented() {
|
|
@@ -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"],"sources":["../../src/load/serializable.ts"],"sourcesContent":["import { type SerializedFields, keyToJson, mapKeys } from \"./map_keys.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\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 return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: mapKeys(\n Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs,\n keyToJson,\n aliases\n ),\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":";;;;;;;;;AAyBA,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,WA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,MAAM;GACN,IAAI,KAAK;GACT,QAAQ,QACN,OAAO,KAAK,QAAQ,CAAC,SAAS,eAAe,QAAQ,QAAQ,GAAG,QAChE,WACA,QACD;EACF;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 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,QACp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I,KAAK;GACT,QAAQ;EACT;CACF;CAED,uBAAiD;AAC/C,SAAO;GACL,IAAI;GACJ,MAAM;GACN,IAAI,KAAK;EACV;CACF;AACF"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/load/validation.ts
|
|
3
|
+
/**
|
|
4
|
+
* Sentinel key used to mark escaped user objects during serialization.
|
|
5
|
+
*
|
|
6
|
+
* When a plain object contains 'lc' key (which could be confused with LC objects),
|
|
7
|
+
* we wrap it as `{"__lc_escaped__": {...original...}}`.
|
|
8
|
+
*/
|
|
9
|
+
const LC_ESCAPED_KEY = "__lc_escaped__";
|
|
10
|
+
/**
|
|
11
|
+
* Check if an object needs escaping to prevent confusion with LC objects.
|
|
12
|
+
*
|
|
13
|
+
* An object needs escaping if:
|
|
14
|
+
* 1. It has an `'lc'` key (could be confused with LC serialization format)
|
|
15
|
+
* 2. It has only the escape key (would be mistaken for an escaped object)
|
|
16
|
+
*/
|
|
17
|
+
function needsEscaping(obj) {
|
|
18
|
+
return "lc" in obj || Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Wrap an object in the escape marker.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* {"key": "value"} // becomes {"__lc_escaped__": {"key": "value"}}
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function escapeObject(obj) {
|
|
29
|
+
return { [LC_ESCAPED_KEY]: obj };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if an object is an escaped user object.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* {"__lc_escaped__": {...}} // is an escaped object
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function isEscapedObject(obj) {
|
|
40
|
+
return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if an object looks like a Serializable instance (duck typing).
|
|
44
|
+
*/
|
|
45
|
+
function isSerializableLike(obj) {
|
|
46
|
+
return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Escape a value if it needs escaping (contains `lc` key).
|
|
50
|
+
*
|
|
51
|
+
* This is a simpler version of `serializeValue` that doesn't handle Serializable
|
|
52
|
+
* objects - it's meant to be called on kwargs values that have already been
|
|
53
|
+
* processed by `toJSON()`.
|
|
54
|
+
*
|
|
55
|
+
* @param value - The value to potentially escape.
|
|
56
|
+
* @returns The value with any `lc`-containing objects wrapped in escape markers.
|
|
57
|
+
*/
|
|
58
|
+
function escapeIfNeeded(value) {
|
|
59
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
60
|
+
if (isSerializableLike(value)) return value;
|
|
61
|
+
const record = value;
|
|
62
|
+
if (needsEscaping(record)) return escapeObject(record);
|
|
63
|
+
const result = {};
|
|
64
|
+
for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val);
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item));
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Unescape a value, processing escape markers in object values and arrays.
|
|
72
|
+
*
|
|
73
|
+
* When an escaped object is encountered (`{"__lc_escaped__": ...}`), it's
|
|
74
|
+
* unwrapped and the contents are returned AS-IS (no further processing).
|
|
75
|
+
* The contents represent user data that should not be modified.
|
|
76
|
+
*
|
|
77
|
+
* For regular objects and arrays, we recurse to find any nested escape markers.
|
|
78
|
+
*
|
|
79
|
+
* @param obj - The value to unescape.
|
|
80
|
+
* @returns The unescaped value.
|
|
81
|
+
*/
|
|
82
|
+
function unescapeValue(obj) {
|
|
83
|
+
if (obj !== null && typeof obj === "object" && !Array.isArray(obj)) {
|
|
84
|
+
const record = obj;
|
|
85
|
+
if (isEscapedObject(record)) return record[LC_ESCAPED_KEY];
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const [key, value] of Object.entries(record)) result[key] = unescapeValue(value);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(obj)) return obj.map((item) => unescapeValue(item));
|
|
91
|
+
return obj;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.escapeIfNeeded = escapeIfNeeded;
|
|
96
|
+
exports.isEscapedObject = isEscapedObject;
|
|
97
|
+
exports.unescapeValue = unescapeValue;
|
|
98
|
+
//# sourceMappingURL=validation.cjs.map
|
|
@@ -0,0 +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,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"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
//#region src/load/validation.ts
|
|
2
|
+
/**
|
|
3
|
+
* Sentinel key used to mark escaped user objects during serialization.
|
|
4
|
+
*
|
|
5
|
+
* When a plain object contains 'lc' key (which could be confused with LC objects),
|
|
6
|
+
* we wrap it as `{"__lc_escaped__": {...original...}}`.
|
|
7
|
+
*/
|
|
8
|
+
const LC_ESCAPED_KEY = "__lc_escaped__";
|
|
9
|
+
/**
|
|
10
|
+
* Check if an object needs escaping to prevent confusion with LC objects.
|
|
11
|
+
*
|
|
12
|
+
* An object needs escaping if:
|
|
13
|
+
* 1. It has an `'lc'` key (could be confused with LC serialization format)
|
|
14
|
+
* 2. It has only the escape key (would be mistaken for an escaped object)
|
|
15
|
+
*/
|
|
16
|
+
function needsEscaping(obj) {
|
|
17
|
+
return "lc" in obj || Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Wrap an object in the escape marker.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* {"key": "value"} // becomes {"__lc_escaped__": {"key": "value"}}
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function escapeObject(obj) {
|
|
28
|
+
return { [LC_ESCAPED_KEY]: obj };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check if an object is an escaped user object.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* {"__lc_escaped__": {...}} // is an escaped object
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function isEscapedObject(obj) {
|
|
39
|
+
return Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if an object looks like a Serializable instance (duck typing).
|
|
43
|
+
*/
|
|
44
|
+
function isSerializableLike(obj) {
|
|
45
|
+
return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Escape a value if it needs escaping (contains `lc` key).
|
|
49
|
+
*
|
|
50
|
+
* This is a simpler version of `serializeValue` that doesn't handle Serializable
|
|
51
|
+
* objects - it's meant to be called on kwargs values that have already been
|
|
52
|
+
* processed by `toJSON()`.
|
|
53
|
+
*
|
|
54
|
+
* @param value - The value to potentially escape.
|
|
55
|
+
* @returns The value with any `lc`-containing objects wrapped in escape markers.
|
|
56
|
+
*/
|
|
57
|
+
function escapeIfNeeded(value) {
|
|
58
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
59
|
+
if (isSerializableLike(value)) return value;
|
|
60
|
+
const record = value;
|
|
61
|
+
if (needsEscaping(record)) return escapeObject(record);
|
|
62
|
+
const result = {};
|
|
63
|
+
for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item));
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Unescape a value, processing escape markers in object values and arrays.
|
|
71
|
+
*
|
|
72
|
+
* When an escaped object is encountered (`{"__lc_escaped__": ...}`), it's
|
|
73
|
+
* unwrapped and the contents are returned AS-IS (no further processing).
|
|
74
|
+
* The contents represent user data that should not be modified.
|
|
75
|
+
*
|
|
76
|
+
* For regular objects and arrays, we recurse to find any nested escape markers.
|
|
77
|
+
*
|
|
78
|
+
* @param obj - The value to unescape.
|
|
79
|
+
* @returns The unescaped value.
|
|
80
|
+
*/
|
|
81
|
+
function unescapeValue(obj) {
|
|
82
|
+
if (obj !== null && typeof obj === "object" && !Array.isArray(obj)) {
|
|
83
|
+
const record = obj;
|
|
84
|
+
if (isEscapedObject(record)) return record[LC_ESCAPED_KEY];
|
|
85
|
+
const result = {};
|
|
86
|
+
for (const [key, value] of Object.entries(record)) result[key] = unescapeValue(value);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(obj)) return obj.map((item) => unescapeValue(item));
|
|
90
|
+
return obj;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { escapeIfNeeded, isEscapedObject, unescapeValue };
|
|
95
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +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,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