@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 3.89 kB
Source Map (JSON)
{"version":3,"file":"notification.cjs","names":["HumanMessage"],"sources":["../../src/messages/notification.ts"],"sourcesContent":["import type { BaseMessage } from \"./base.js\";\nimport { HumanMessage } from \"./human.js\";\n\n/** Which feature added a message, and that feature's fields. */\nexport interface NotificationMetadata {\n /** The feature that added the message, e.g. `\"async_subagent\"`. */\n source: string;\n /** The feature's fields, documented by that feature. */\n data?: Record<string, unknown>;\n}\n\n/**\n * Return a copy of `message` marked as added by the agent harness.\n *\n * Use it for a human message the user didn't type, such as a background task\n * update or a conversation summary. It sets `additional_kwargs.lc_source`\n * and, when `data` is given, `additional_kwargs.lc_data`. Any earlier\n * `lc_source` and `lc_data` are removed first. LangChain's chat model\n * integrations don't send these keys to the model.\n *\n * It doesn't change the content. Write the content the model reads yourself.\n *\n * Anyone who can send input to a thread can set these keys. Don't use them to\n * decide whether to trust a message.\n *\n * `mergeMessageRuns` and chunk `concat` join a notification with adjacent\n * human messages; the merged message keeps the metadata and concatenates\n * `lc_source`.\n *\n * @param message - The message to mark.\n * @param metadata - The feature that added the message and its fields. `data` must be JSON-serializable.\n * @returns A shallow copy of `message`, with the same `id`.\n *\n * @example\n * ```typescript\n * import { HumanMessage, addNotificationMetadata } from \"@langchain/core/messages\";\n *\n * const message = addNotificationMetadata(\n * new HumanMessage(\"Background task t_42 finished.\"),\n * { source: \"async_subagent\", data: { task_id: \"t_42\", status: \"success\" } }\n * );\n * ```\n */\nexport function addNotificationMetadata(\n message: HumanMessage,\n metadata: NotificationMetadata\n): HumanMessage {\n const { lc_data: _, ...kwargs } = message.additional_kwargs ?? {};\n const additional_kwargs: Record<string, unknown> = {\n ...kwargs,\n lc_source: metadata.source,\n };\n if (metadata.data !== undefined) {\n additional_kwargs.lc_data = { ...metadata.data };\n }\n const {\n lc_kwargs: _kwargs,\n lc_serializable: _serializable,\n lc_namespace: _namespace,\n type: _type,\n ...fields\n } = message;\n return new HumanMessage({ ...fields, additional_kwargs });\n}\n\n/**\n * Return the notification metadata of `message`.\n *\n * @param message - The message to read.\n * @returns The metadata `addNotificationMetadata` set, or `undefined` for a\n * message without a string `lc_source`. `data` is a copy of `lc_data`, left out\n * unless `lc_data` is an object.\n */\nexport function getNotificationMetadata(\n message: BaseMessage\n): NotificationMetadata | undefined {\n const source = message.additional_kwargs?.lc_source;\n if (typeof source !== \"string\") return undefined;\n const data = message.additional_kwargs?.lc_data;\n const isRecord =\n typeof data === \"object\" && data !== null && !Array.isArray(data);\n return isRecord\n ? { source, data: { ...(data as Record<string, unknown>) } }\n : { source };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,wBACd,SACA,UACc;CACd,MAAM,EAAE,SAAS,GAAG,GAAG,WAAW,QAAQ,qBAAqB,CAAC;CAChE,MAAM,oBAA6C;EACjD,GAAG;EACH,WAAW,SAAS;CACtB;CACA,IAAI,SAAS,SAAS,KAAA,GACpB,kBAAkB,UAAU,EAAE,GAAG,SAAS,KAAK;CAEjD,MAAM,EACJ,WAAW,SACX,iBAAiB,eACjB,cAAc,YACd,MAAM,OACN,GAAG,WACD;CACJ,OAAO,IAAIA,cAAAA,aAAa;EAAE,GAAG;EAAQ;CAAkB,CAAC;AAC1D;;;;;;;;;AAUA,SAAgB,wBACd,SACkC;CAClC,MAAM,SAAS,QAAQ,mBAAmB;CAC1C,IAAI,OAAO,WAAW,UAAU,OAAO,KAAA;CACvC,MAAM,OAAO,QAAQ,mBAAmB;CAGxC,OADE,OAAO,SAAS,YAAY,SAAS,QAAQ,CAAC,MAAM,QAAQ,IAAI,IAE9D;EAAE;EAAQ,MAAM,EAAE,GAAI,KAAiC;CAAE,IACzD,EAAE,OAAO;AACf"}