UNPKG

@langchain/core

Version:
1 lines 2.97 kB
{"version":3,"file":"web.cjs","names":["name: string","payload: any","config?: RunnableConfig","getCallbackManagerForConfig"],"sources":["../../../src/callbacks/dispatch/web.ts"],"sourcesContent":["import {\n type RunnableConfig,\n getCallbackManagerForConfig,\n} from \"../../runnables/config.js\";\n\n/**\n * Dispatch a custom event. Requires an explicit config object.\n * @param name The name of the custom event.\n * @param payload The data for the custom event.\n * Ideally should be JSON serializable to avoid serialization issues downstream, but not enforced.\n * @param config Config object.\n *\n * @example\n * ```typescript\n * import { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\n *\n * const foo = RunnableLambda.from(async (input: string, config?: RunnableConfig) => {\n * await dispatchCustomEvent(\n * \"my_custom_event\",\n * { arbitraryField: \"someval\" },\n * config\n * );\n * return input;\n * });\n *\n * const callbacks = [{\n * handleCustomEvent: (eventName: string, payload: any) => {\n * // Logs \"my_custom_event\" and { arbitraryField: \"someval\" }\n * console.log(eventName, payload);\n * }\n * }];\n *\n * await foo.invoke(\"hi\", { callbacks })\n * ```\n */\nexport async function dispatchCustomEvent(\n name: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n payload: any,\n config?: RunnableConfig\n) {\n if (config === undefined) {\n throw new Error(\n [\n \"Unable to dispatch a custom event without a parent run id.\",\n `\"dispatchCustomEvent\" can only be called from within an existing run (e.g.,`,\n \"inside a tool or a RunnableLambda).\",\n `\\n\\nIf you continue to see this error, please import from \"@langchain/core/callbacks/dispatch/web\"`,\n \"and explicitly pass in a config parameter.\",\n `\\n\\nOr, if you are calling this from a custom tool, ensure you're using the \"tool\" helper constructor as documented here:`,\n \"\\n |\",\n \"\\n └-> https://js.langchain.com/docs/how_to/custom_tools#tool-function\",\n \"\\n\",\n ].join(\" \")\n );\n }\n const callbackManager = await getCallbackManagerForConfig(config);\n const parentRunId = callbackManager?.getParentRunId();\n // We pass parent id as the current run id here intentionally since events dispatch\n // from within things like RunnableLambda\n if (callbackManager !== undefined && parentRunId !== undefined) {\n await callbackManager.handleCustomEvent?.(name, payload, parentRunId);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,eAAsB,oBACpBA,MAEAC,SACAC,QACA;AACA,KAAI,WAAW,OACb,OAAM,IAAI,MACR;EACE;EACA,CAAC,2EAA2E,CAAC;EAC7E;EACA,CAAC,kGAAkG,CAAC;EACpG;EACA,CAAC,yHAAyH,CAAC;EAC3H;EACA;EACA;CACD,EAAC,KAAK,IAAI;CAGf,MAAM,kBAAkB,MAAMC,2CAA4B,OAAO;CACjE,MAAM,cAAc,iBAAiB,gBAAgB;AAGrD,KAAI,oBAAoB,UAAa,gBAAgB,QACnD,MAAM,gBAAgB,oBAAoB,MAAM,SAAS,YAAY;AAExE"}