UNPKG

@langchain/core

Version:
1 lines 5.16 kB
{"version":3,"file":"system.cjs","names":["BaseMessage","mergeContent","BaseMessageChunk","_mergeDicts"],"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 content: mergeContent(this.content, chunk),\n additional_kwargs: this.additional_kwargs,\n response_metadata: this.response_metadata,\n id: this.id,\n name: this.name,\n } as SystemMessageFields<TStructure>);\n }\n\n if (SystemMessage.isInstance(chunk)) {\n return new SystemMessage({\n content: mergeContent(this.content, chunk.content),\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 id: this.id ?? chunk.id,\n name: this.name ?? chunk.name,\n } as SystemMessageFields<TStructure>);\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,aAAAA,YAAkC;CAC1C,OAAO,UAAU;AACf,SAAO;;CAGT,OAAgB;CAEhB,YACE,QAGA;AACA,QAAM,OAAO;;;;;;;CAQf,OAAO,OAA+B;AACpC,MAAI,OAAO,UAAU,SACnB,QAAO,IAAI,cAAc;GACvB,SAASC,aAAAA,aAAa,KAAK,SAAS,MAAM;GAC1C,mBAAmB,KAAK;GACxB,mBAAmB,KAAK;GACxB,IAAI,KAAK;GACT,MAAM,KAAK;GACZ,CAAoC;AAGvC,MAAI,cAAc,WAAW,MAAM,CACjC,QAAO,IAAI,cAAc;GACvB,SAASA,aAAAA,aAAa,KAAK,SAAS,MAAM,QAAQ;GAClD,mBA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