@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 10.5 kB
Source Map (JSON)
{"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;;;;;;CAOD,IAAI,aAAoD;AACtD,SAAO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