@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.04 kB
Source Map (JSON)
{"version":3,"file":"system.cjs","names":["BaseMessage","fields:\n | $InferMessageContent<TStructure, \"system\">\n | SystemMessageFields<TStructure>","chunk: string | SystemMessage","mergeContent","obj: unknown","BaseMessageChunk","chunk: SystemMessageChunk<TStructure>","_mergeDicts","x: BaseMessage","x: BaseMessageChunk"],"sources":["../../src/messages/system.ts"],"sourcesContent":["import {\n BaseMessage,\n BaseMessageChunk,\n mergeContent,\n _mergeDicts,\n type BaseMessageFields,\n} from \"./base.js\";\nimport { $InferMessageContent, MessageStructure } from \"./message.js\";\nimport { Constructor } from \"./utils.js\";\n\nexport interface SystemMessageFields<\n TStructure extends MessageStructure = MessageStructure\n> extends BaseMessageFields<TStructure, \"system\"> {}\n\n/**\n * Represents a system message in a conversation.\n */\nexport class SystemMessage<\n TStructure extends MessageStructure = MessageStructure\n> extends BaseMessage<TStructure, \"system\"> {\n static lc_name() {\n return \"SystemMessage\";\n }\n\n readonly type = \"system\" as const;\n\n constructor(\n fields:\n | $InferMessageContent<TStructure, \"system\">\n | SystemMessageFields<TStructure>\n ) {\n super(fields);\n }\n\n /**\n * Concatenates a string or another system message with the current system message.\n * @param chunk - The chunk to concatenate with the system message.\n * @returns A new system message with the concatenated content.\n */\n concat(chunk: string | SystemMessage) {\n if (typeof chunk === \"string\") {\n return new SystemMessage({\n ...this,\n content: mergeContent(this.content, chunk),\n });\n }\n\n if (SystemMessage.isInstance(chunk)) {\n return new SystemMessage({\n ...this,\n additional_kwargs: {\n ...this.additional_kwargs,\n ...chunk.additional_kwargs,\n },\n response_metadata: {\n ...this.response_metadata,\n ...chunk.response_metadata,\n },\n content: mergeContent(this.content, chunk.content),\n });\n }\n\n throw new Error(\"Unexpected chunk type for system message\");\n }\n\n static isInstance(obj: unknown): obj is SystemMessage {\n return super.isInstance(obj) && obj.type === \"system\";\n }\n}\n\n/**\n * Represents a chunk of a system message, which can be concatenated with\n * other system message chunks.\n */\nexport class SystemMessageChunk<\n TStructure extends MessageStructure = MessageStructure\n> extends BaseMessageChunk<TStructure, \"system\"> {\n static lc_name() {\n return \"SystemMessageChunk\";\n }\n\n readonly type = \"system\" as const;\n\n constructor(\n fields:\n | $InferMessageContent<TStructure, \"system\">\n | SystemMessageFields<TStructure>\n ) {\n super(fields);\n }\n\n concat(chunk: SystemMessageChunk<TStructure>) {\n const Cls = this.constructor as Constructor<this>;\n return new Cls({\n content: mergeContent(this.content, chunk.content),\n additional_kwargs: _mergeDicts(\n this.additional_kwargs,\n chunk.additional_kwargs\n ),\n response_metadata: _mergeDicts(\n this.response_metadata,\n chunk.response_metadata\n ),\n id: this.id ?? chunk.id,\n });\n }\n\n static isInstance(obj: unknown): obj is SystemMessageChunk {\n return super.isInstance(obj) && obj.type === \"system\";\n }\n}\n\n/**\n * @deprecated Use {@link SystemMessage.isInstance} instead\n */\nexport function isSystemMessage<TStructure extends MessageStructure>(\n x: BaseMessage\n): x is SystemMessage<TStructure> {\n return x._getType() === \"system\";\n}\n\n/**\n * @deprecated Use {@link SystemMessageChunk.isInstance} instead\n */\nexport function isSystemMessageChunk<TStructure extends MessageStructure>(\n x: BaseMessageChunk\n): x is SystemMessageChunk<TStructure> {\n return x._getType() === \"system\";\n}\n"],"mappings":";;;;;;AAiBA,IAAa,gBAAb,MAAa,sBAEHA,yBAAkC;CAC1C,OAAO,UAAU;AACf,SAAO;CACR;CAED,AAAS,OAAO;CAEhB,YACEC,QAGA;EACA,MAAM,OAAO;CACd;;;;;;CAOD,OAAOC,OAA+B;AACpC,MAAI,OAAO,UAAU,SACnB,QAAO,IAAI,cAAc;GACvB,GAAG;GACH,SAASC,0BAAa,KAAK,SAAS,MAAM;EAC3C;AAGH,MAAI,cAAc,WAAW,MAAM,CACjC,QAAO,IAAI,cAAc;GACvB,GAAG;GACH,mBAAmB;IACjB,GAAG,KAAK;IACR,GAAG,MAAM;GACV;GACD,mBAAmB;IACjB,GAAG,KAAK;IACR,GAAG,M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